2 * Mesa 3-D graphics library
5 * Copyright 2009, VMware, Inc.
7 * Copyright (C) 2010 LunarG Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * Keith Whitwell <keithw@vmware.com>
28 * Jakob Bornecrantz <wallbraker@gmail.com>
29 * Chia-I Wu <olv@lunarg.com>
32 #include "util/u_memory.h"
33 #include "util/u_inlines.h"
34 #include "util/u_format.h"
35 #include "util/u_debug.h"
36 #include "state_tracker/drm_api.h"
38 #include "dri_screen.h"
39 #include "dri_context.h"
40 #include "dri_drawable.h"
41 #include "dri_st_api.h"
44 #include "GL/internal/dri_interface.h"
47 * DRI2 flush extension.
50 dri2_flush_drawable(__DRIdrawable
*draw
)
55 dri2_invalidate_drawable(__DRIdrawable
*dPriv
)
57 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
58 struct dri_context
*ctx
= dri_context(dPriv
->driContextPriv
);
60 dri2InvalidateDrawable(dPriv
);
61 drawable
->dPriv
->lastStamp
= *drawable
->dPriv
->pStamp
;
64 ctx
->st
->notify_invalid_framebuffer(ctx
->st
, drawable
->stfb
);
67 static const __DRI2flushExtension dri2FlushExtension
= {
68 { __DRI2_FLUSH
, __DRI2_FLUSH_VERSION
},
70 dri2_invalidate_drawable
,
74 * These are used for GLX_EXT_texture_from_pixmap
77 dri2_set_tex_buffer2(__DRIcontext
*pDRICtx
, GLint target
,
78 GLint format
, __DRIdrawable
*dPriv
)
80 struct dri_context
*ctx
= dri_context(pDRICtx
);
81 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
82 struct pipe_resource
*pt
;
84 dri_st_framebuffer_validate_att(drawable
->stfb
, ST_ATTACHMENT_FRONT_LEFT
);
86 pt
= drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
];
89 enum pipe_format internal_format
= pt
->format
;
91 if (format
== __DRI_TEXTURE_FORMAT_RGB
) {
92 /* only need to cover the formats recognized by dri_fill_st_visual */
93 switch (internal_format
) {
94 case PIPE_FORMAT_B8G8R8A8_UNORM
:
95 internal_format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
97 case PIPE_FORMAT_A8R8G8B8_UNORM
:
98 internal_format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
105 ctx
->st
->teximage(ctx
->st
,
106 (target
== GL_TEXTURE_2D
) ? ST_TEXTURE_2D
: ST_TEXTURE_RECT
,
107 0, internal_format
, pt
, FALSE
);
112 dri2_set_tex_buffer(__DRIcontext
*pDRICtx
, GLint target
,
113 __DRIdrawable
*dPriv
)
115 dri2_set_tex_buffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
118 static const __DRItexBufferExtension dri2TexBufferExtension
= {
119 { __DRI_TEX_BUFFER
, __DRI_TEX_BUFFER_VERSION
},
121 dri2_set_tex_buffer2
,
125 * Get the format and binding of an attachment.
128 dri2_drawable_get_format(struct dri_drawable
*drawable
,
129 enum st_attachment_type statt
,
130 enum pipe_format
*format
,
134 case ST_ATTACHMENT_FRONT_LEFT
:
135 case ST_ATTACHMENT_BACK_LEFT
:
136 case ST_ATTACHMENT_FRONT_RIGHT
:
137 case ST_ATTACHMENT_BACK_RIGHT
:
138 *format
= drawable
->stvis
.color_format
;
139 *bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
141 case ST_ATTACHMENT_DEPTH_STENCIL
:
142 *format
= drawable
->stvis
.depth_stencil_format
;
143 *bind
= PIPE_BIND_DEPTH_STENCIL
; /* XXX sampler? */
146 *format
= PIPE_FORMAT_NONE
;
154 * Retrieve __DRIbuffer from the DRI loader.
157 dri2_drawable_get_buffers(struct dri_drawable
*drawable
,
158 const enum st_attachment_type
*statts
,
161 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
162 struct __DRIdri2LoaderExtensionRec
*loader
= drawable
->sPriv
->dri2
.loader
;
164 __DRIbuffer
*buffers
;
166 unsigned attachments
[10];
167 unsigned num_attachments
, i
;
170 with_format
= dri_with_format(drawable
->sPriv
);
174 /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
176 attachments
[num_attachments
++] = __DRI_BUFFER_FRONT_LEFT
;
178 for (i
= 0; i
< *count
; i
++) {
179 enum pipe_format format
;
183 dri2_drawable_get_format(drawable
, statts
[i
], &format
, &bind
);
184 if (format
== PIPE_FORMAT_NONE
)
188 case ST_ATTACHMENT_FRONT_LEFT
:
192 att
= __DRI_BUFFER_FRONT_LEFT
;
194 case ST_ATTACHMENT_BACK_LEFT
:
195 att
= __DRI_BUFFER_BACK_LEFT
;
197 case ST_ATTACHMENT_FRONT_RIGHT
:
198 att
= __DRI_BUFFER_FRONT_RIGHT
;
200 case ST_ATTACHMENT_BACK_RIGHT
:
201 att
= __DRI_BUFFER_BACK_RIGHT
;
203 case ST_ATTACHMENT_DEPTH_STENCIL
:
204 att
= __DRI_BUFFER_DEPTH_STENCIL
;
211 bpp
= util_format_get_blocksizebits(format
);
214 attachments
[num_attachments
++] = att
;
216 attachments
[num_attachments
++] = bpp
;
222 num_attachments
/= 2;
223 buffers
= loader
->getBuffersWithFormat(dri_drawable
,
224 &dri_drawable
->w
, &dri_drawable
->h
,
225 attachments
, num_attachments
,
226 &num_buffers
, dri_drawable
->loaderPrivate
);
229 buffers
= loader
->getBuffers(dri_drawable
,
230 &dri_drawable
->w
, &dri_drawable
->h
,
231 attachments
, num_attachments
,
232 &num_buffers
, dri_drawable
->loaderPrivate
);
236 /* set one cliprect to cover the whole dri_drawable */
239 dri_drawable
->backX
= 0;
240 dri_drawable
->backY
= 0;
241 dri_drawable
->numClipRects
= 1;
242 dri_drawable
->pClipRects
[0].x1
= 0;
243 dri_drawable
->pClipRects
[0].y1
= 0;
244 dri_drawable
->pClipRects
[0].x2
= dri_drawable
->w
;
245 dri_drawable
->pClipRects
[0].y2
= dri_drawable
->h
;
246 dri_drawable
->numBackClipRects
= 1;
247 dri_drawable
->pBackClipRects
[0].x1
= 0;
248 dri_drawable
->pBackClipRects
[0].y1
= 0;
249 dri_drawable
->pBackClipRects
[0].x2
= dri_drawable
->w
;
250 dri_drawable
->pBackClipRects
[0].y2
= dri_drawable
->h
;
252 *count
= num_buffers
;
259 * Process __DRIbuffer and convert them into pipe_resources.
262 dri2_drawable_process_buffers(struct dri_drawable
*drawable
,
263 __DRIbuffer
*buffers
, unsigned count
)
265 struct dri_screen
*screen
= dri_screen(drawable
->sPriv
);
266 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
267 struct pipe_resource templ
;
268 struct winsys_handle whandle
;
269 boolean have_depth
= FALSE
;
272 if (drawable
->old_num
== count
&&
273 drawable
->old_w
== dri_drawable
->w
&&
274 drawable
->old_h
== dri_drawable
->h
&&
275 memcmp(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * count
) == 0)
278 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
279 pipe_resource_reference(&drawable
->textures
[i
], NULL
);
281 memset(&templ
, 0, sizeof(templ
));
282 templ
.target
= PIPE_TEXTURE_2D
;
283 templ
.last_level
= 0;
284 templ
.width0
= dri_drawable
->w
;
285 templ
.height0
= dri_drawable
->h
;
288 memset(&whandle
, 0, sizeof(whandle
));
290 for (i
= 0; i
< count
; i
++) {
291 __DRIbuffer
*buf
= &buffers
[i
];
292 enum st_attachment_type statt
;
293 enum pipe_format format
;
295 switch (buf
->attachment
) {
296 case __DRI_BUFFER_FRONT_LEFT
:
297 if (!screen
->auto_fake_front
) {
298 statt
= ST_ATTACHMENT_INVALID
;
302 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
303 statt
= ST_ATTACHMENT_FRONT_LEFT
;
305 case __DRI_BUFFER_BACK_LEFT
:
306 statt
= ST_ATTACHMENT_BACK_LEFT
;
308 case __DRI_BUFFER_DEPTH
:
309 case __DRI_BUFFER_DEPTH_STENCIL
:
310 case __DRI_BUFFER_STENCIL
:
311 /* use only the first depth/stencil buffer */
314 statt
= ST_ATTACHMENT_DEPTH_STENCIL
;
317 statt
= ST_ATTACHMENT_INVALID
;
321 statt
= ST_ATTACHMENT_INVALID
;
325 dri2_drawable_get_format(drawable
, statt
, &format
, &bind
);
326 if (statt
== ST_ATTACHMENT_INVALID
|| format
== PIPE_FORMAT_NONE
)
329 templ
.format
= format
;
331 whandle
.handle
= buf
->name
;
332 whandle
.stride
= buf
->pitch
;
334 drawable
->textures
[statt
] =
335 screen
->pipe_screen
->resource_from_handle(screen
->pipe_screen
,
339 drawable
->old_num
= count
;
340 drawable
->old_w
= dri_drawable
->w
;
341 drawable
->old_h
= dri_drawable
->h
;
342 memcpy(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * count
);
346 * Backend functions for st_framebuffer interface.
350 dri2_allocate_textures(struct dri_drawable
*drawable
,
351 const enum st_attachment_type
*statts
,
354 __DRIbuffer
*buffers
;
355 unsigned num_buffers
= count
;
357 buffers
= dri2_drawable_get_buffers(drawable
, statts
, &num_buffers
);
358 dri2_drawable_process_buffers(drawable
, buffers
, num_buffers
);
362 dri2_flush_frontbuffer(struct dri_drawable
*drawable
,
363 enum st_attachment_type statt
)
365 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
366 struct __DRIdri2LoaderExtensionRec
*loader
= drawable
->sPriv
->dri2
.loader
;
368 if (loader
->flushFrontBuffer
== NULL
)
371 if (statt
== ST_ATTACHMENT_FRONT_LEFT
) {
372 loader
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
377 dri2_lookup_egl_image(struct dri_context
*ctx
, void *handle
)
379 __DRIimageLookupExtension
*loader
= ctx
->sPriv
->dri2
.image
;
382 if (!loader
->lookupEGLImage
)
385 img
= loader
->lookupEGLImage(ctx
->cPriv
, handle
, ctx
->cPriv
->loaderPrivate
);
391 dri2_create_image_from_name(__DRIcontext
*context
,
392 int width
, int height
, int format
,
393 int name
, int pitch
, void *loaderPrivate
)
395 struct dri_screen
*screen
= dri_screen(context
->driScreenPriv
);
397 struct pipe_resource templ
;
398 struct winsys_handle whandle
;
402 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
405 case __DRI_IMAGE_FORMAT_RGB565
:
406 pf
= PIPE_FORMAT_B5G6R5_UNORM
;
408 case __DRI_IMAGE_FORMAT_XRGB8888
:
409 pf
= PIPE_FORMAT_B8G8R8X8_UNORM
;
411 case __DRI_IMAGE_FORMAT_ARGB8888
:
412 pf
= PIPE_FORMAT_B8G8R8A8_UNORM
;
415 pf
= PIPE_FORMAT_NONE
;
418 if (pf
== PIPE_FORMAT_NONE
)
421 img
= CALLOC_STRUCT(__DRIimageRec
);
425 memset(&templ
, 0, sizeof(templ
));
426 templ
.bind
= tex_usage
;
428 templ
.target
= PIPE_TEXTURE_2D
;
429 templ
.last_level
= 0;
430 templ
.width0
= width
;
431 templ
.height0
= height
;
434 memset(&whandle
, 0, sizeof(whandle
));
435 whandle
.handle
= name
;
436 whandle
.stride
= pitch
* util_format_get_blocksize(pf
);
438 img
->texture
= screen
->pipe_screen
->resource_from_handle(screen
->pipe_screen
,
448 img
->loader_private
= loaderPrivate
;
454 dri2_create_image_from_renderbuffer(__DRIcontext
*context
,
455 int renderbuffer
, void *loaderPrivate
)
457 struct dri_context
*ctx
= dri_context(context
->driverPrivate
);
459 if (!ctx
->st
->get_resource_for_egl_image
)
467 dri2_destroy_image(__DRIimage
*img
)
469 pipe_resource_reference(&img
->texture
, NULL
);
473 static struct __DRIimageExtensionRec dri2ImageExtension
= {
474 { __DRI_IMAGE
, __DRI_IMAGE_VERSION
},
475 dri2_create_image_from_name
,
476 dri2_create_image_from_renderbuffer
,
481 * Backend function init_screen.
484 static const __DRIextension
*dri_screen_extensions
[] = {
485 &driReadDrawableExtension
,
486 &driCopySubBufferExtension
.base
,
487 &driSwapControlExtension
.base
,
488 &driFrameTrackingExtension
.base
,
489 &driMediaStreamCounterExtension
.base
,
490 &dri2TexBufferExtension
.base
,
491 &dri2FlushExtension
.base
,
492 &dri2ImageExtension
.base
,
497 * This is the driver specific part of the createNewScreen entry point.
499 * Returns the __GLcontextModes supported by this driver.
502 dri2_init_screen(__DRIscreen
* sPriv
)
504 const __DRIconfig
**configs
;
505 struct dri_screen
*screen
;
506 struct pipe_screen
*pscreen
;
508 screen
= CALLOC_STRUCT(dri_screen
);
512 screen
->api
= drm_api_create();
513 screen
->sPriv
= sPriv
;
514 screen
->fd
= sPriv
->fd
;
515 screen
->lookup_egl_image
= dri2_lookup_egl_image
;
517 sPriv
->private = (void *)screen
;
518 sPriv
->extensions
= dri_screen_extensions
;
520 pscreen
= screen
->api
->create_screen(screen
->api
, screen
->fd
, NULL
);
521 /* dri_init_screen_helper checks pscreen for us */
523 configs
= dri_init_screen_helper(screen
, pscreen
, 32);
527 screen
->auto_fake_front
= dri_with_format(sPriv
);
531 dri_destroy_screen_helper(screen
);
536 /* This is the table of extensions that the loader will dlsym() for. */
537 PUBLIC
const __DRIextension
*__driDriverExtensions
[] = {
538 &driCoreExtension
.base
,
539 &driLegacyExtension
.base
,
540 &driDRI2Extension
.base
,
544 /* vim: set sw=3 ts=8 sts=3 expandtab: */