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_driver.h"
38 #include "dri_screen.h"
39 #include "dri_context.h"
40 #include "dri_drawable.h"
43 * DRI2 flush extension.
46 dri2_flush_drawable(__DRIdrawable
*draw
)
51 dri2_invalidate_drawable(__DRIdrawable
*dPriv
)
53 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
54 struct dri_context
*ctx
= dri_context(dPriv
->driContextPriv
);
56 dri2InvalidateDrawable(dPriv
);
57 drawable
->dPriv
->lastStamp
= *drawable
->dPriv
->pStamp
;
60 ctx
->st
->notify_invalid_framebuffer(ctx
->st
, &drawable
->base
);
63 static const __DRI2flushExtension dri2FlushExtension
= {
64 { __DRI2_FLUSH
, __DRI2_FLUSH_VERSION
},
66 dri2_invalidate_drawable
,
70 * These are used for GLX_EXT_texture_from_pixmap
73 dri2_set_tex_buffer2(__DRIcontext
*pDRICtx
, GLint target
,
74 GLint format
, __DRIdrawable
*dPriv
)
76 struct dri_context
*ctx
= dri_context(pDRICtx
);
77 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
78 struct pipe_resource
*pt
;
80 dri_drawable_validate_att(drawable
, ST_ATTACHMENT_FRONT_LEFT
);
82 pt
= drawable
->textures
[ST_ATTACHMENT_FRONT_LEFT
];
85 enum pipe_format internal_format
= pt
->format
;
87 if (format
== __DRI_TEXTURE_FORMAT_RGB
) {
88 /* only need to cover the formats recognized by dri_fill_st_visual */
89 switch (internal_format
) {
90 case PIPE_FORMAT_B8G8R8A8_UNORM
:
91 internal_format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
93 case PIPE_FORMAT_A8R8G8B8_UNORM
:
94 internal_format
= PIPE_FORMAT_X8R8G8B8_UNORM
;
101 ctx
->st
->teximage(ctx
->st
,
102 (target
== GL_TEXTURE_2D
) ? ST_TEXTURE_2D
: ST_TEXTURE_RECT
,
103 0, internal_format
, pt
, FALSE
);
108 dri2_set_tex_buffer(__DRIcontext
*pDRICtx
, GLint target
,
109 __DRIdrawable
*dPriv
)
111 dri2_set_tex_buffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
114 static const __DRItexBufferExtension dri2TexBufferExtension
= {
115 { __DRI_TEX_BUFFER
, __DRI_TEX_BUFFER_VERSION
},
117 dri2_set_tex_buffer2
,
121 * Get the format and binding of an attachment.
124 dri2_drawable_get_format(struct dri_drawable
*drawable
,
125 enum st_attachment_type statt
,
126 enum pipe_format
*format
,
130 case ST_ATTACHMENT_FRONT_LEFT
:
131 case ST_ATTACHMENT_BACK_LEFT
:
132 case ST_ATTACHMENT_FRONT_RIGHT
:
133 case ST_ATTACHMENT_BACK_RIGHT
:
134 *format
= drawable
->stvis
.color_format
;
135 *bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
137 case ST_ATTACHMENT_DEPTH_STENCIL
:
138 *format
= drawable
->stvis
.depth_stencil_format
;
139 *bind
= PIPE_BIND_DEPTH_STENCIL
; /* XXX sampler? */
142 *format
= PIPE_FORMAT_NONE
;
150 * Retrieve __DRIbuffer from the DRI loader.
153 dri2_drawable_get_buffers(struct dri_drawable
*drawable
,
154 const enum st_attachment_type
*statts
,
157 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
158 struct __DRIdri2LoaderExtensionRec
*loader
= drawable
->sPriv
->dri2
.loader
;
160 __DRIbuffer
*buffers
;
162 unsigned attachments
[10];
163 unsigned num_attachments
, i
;
166 with_format
= dri_with_format(drawable
->sPriv
);
170 /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
172 attachments
[num_attachments
++] = __DRI_BUFFER_FRONT_LEFT
;
174 for (i
= 0; i
< *count
; i
++) {
175 enum pipe_format format
;
179 dri2_drawable_get_format(drawable
, statts
[i
], &format
, &bind
);
180 if (format
== PIPE_FORMAT_NONE
)
184 case ST_ATTACHMENT_FRONT_LEFT
:
188 att
= __DRI_BUFFER_FRONT_LEFT
;
190 case ST_ATTACHMENT_BACK_LEFT
:
191 att
= __DRI_BUFFER_BACK_LEFT
;
193 case ST_ATTACHMENT_FRONT_RIGHT
:
194 att
= __DRI_BUFFER_FRONT_RIGHT
;
196 case ST_ATTACHMENT_BACK_RIGHT
:
197 att
= __DRI_BUFFER_BACK_RIGHT
;
199 case ST_ATTACHMENT_DEPTH_STENCIL
:
200 att
= __DRI_BUFFER_DEPTH_STENCIL
;
207 bpp
= util_format_get_blocksizebits(format
);
210 attachments
[num_attachments
++] = att
;
212 attachments
[num_attachments
++] = bpp
;
218 num_attachments
/= 2;
219 buffers
= loader
->getBuffersWithFormat(dri_drawable
,
220 &dri_drawable
->w
, &dri_drawable
->h
,
221 attachments
, num_attachments
,
222 &num_buffers
, dri_drawable
->loaderPrivate
);
225 buffers
= loader
->getBuffers(dri_drawable
,
226 &dri_drawable
->w
, &dri_drawable
->h
,
227 attachments
, num_attachments
,
228 &num_buffers
, dri_drawable
->loaderPrivate
);
232 /* set one cliprect to cover the whole dri_drawable */
235 dri_drawable
->backX
= 0;
236 dri_drawable
->backY
= 0;
237 dri_drawable
->numClipRects
= 1;
238 dri_drawable
->pClipRects
[0].x1
= 0;
239 dri_drawable
->pClipRects
[0].y1
= 0;
240 dri_drawable
->pClipRects
[0].x2
= dri_drawable
->w
;
241 dri_drawable
->pClipRects
[0].y2
= dri_drawable
->h
;
242 dri_drawable
->numBackClipRects
= 1;
243 dri_drawable
->pBackClipRects
[0].x1
= 0;
244 dri_drawable
->pBackClipRects
[0].y1
= 0;
245 dri_drawable
->pBackClipRects
[0].x2
= dri_drawable
->w
;
246 dri_drawable
->pBackClipRects
[0].y2
= dri_drawable
->h
;
248 *count
= num_buffers
;
255 * Process __DRIbuffer and convert them into pipe_resources.
258 dri2_drawable_process_buffers(struct dri_drawable
*drawable
,
259 __DRIbuffer
*buffers
, unsigned count
)
261 struct dri_screen
*screen
= dri_screen(drawable
->sPriv
);
262 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
263 struct pipe_resource templ
;
264 struct winsys_handle whandle
;
265 boolean have_depth
= FALSE
;
268 if (drawable
->old_num
== count
&&
269 drawable
->old_w
== dri_drawable
->w
&&
270 drawable
->old_h
== dri_drawable
->h
&&
271 memcmp(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * count
) == 0)
274 for (i
= 0; i
< ST_ATTACHMENT_COUNT
; i
++)
275 pipe_resource_reference(&drawable
->textures
[i
], NULL
);
277 memset(&templ
, 0, sizeof(templ
));
278 templ
.target
= PIPE_TEXTURE_2D
;
279 templ
.last_level
= 0;
280 templ
.width0
= dri_drawable
->w
;
281 templ
.height0
= dri_drawable
->h
;
284 memset(&whandle
, 0, sizeof(whandle
));
286 for (i
= 0; i
< count
; i
++) {
287 __DRIbuffer
*buf
= &buffers
[i
];
288 enum st_attachment_type statt
;
289 enum pipe_format format
;
291 switch (buf
->attachment
) {
292 case __DRI_BUFFER_FRONT_LEFT
:
293 if (!screen
->auto_fake_front
) {
294 statt
= ST_ATTACHMENT_INVALID
;
298 case __DRI_BUFFER_FAKE_FRONT_LEFT
:
299 statt
= ST_ATTACHMENT_FRONT_LEFT
;
301 case __DRI_BUFFER_BACK_LEFT
:
302 statt
= ST_ATTACHMENT_BACK_LEFT
;
304 case __DRI_BUFFER_DEPTH
:
305 case __DRI_BUFFER_DEPTH_STENCIL
:
306 case __DRI_BUFFER_STENCIL
:
307 /* use only the first depth/stencil buffer */
310 statt
= ST_ATTACHMENT_DEPTH_STENCIL
;
313 statt
= ST_ATTACHMENT_INVALID
;
317 statt
= ST_ATTACHMENT_INVALID
;
321 dri2_drawable_get_format(drawable
, statt
, &format
, &bind
);
322 if (statt
== ST_ATTACHMENT_INVALID
|| format
== PIPE_FORMAT_NONE
)
325 templ
.format
= format
;
327 whandle
.handle
= buf
->name
;
328 whandle
.stride
= buf
->pitch
;
330 drawable
->textures
[statt
] =
331 screen
->base
.screen
->resource_from_handle(screen
->base
.screen
,
335 drawable
->old_num
= count
;
336 drawable
->old_w
= dri_drawable
->w
;
337 drawable
->old_h
= dri_drawable
->h
;
338 memcpy(drawable
->old
, buffers
, sizeof(__DRIbuffer
) * count
);
342 * Backend functions for st_framebuffer interface.
346 dri2_allocate_textures(struct dri_drawable
*drawable
,
347 const enum st_attachment_type
*statts
,
350 __DRIbuffer
*buffers
;
351 unsigned num_buffers
= count
;
353 buffers
= dri2_drawable_get_buffers(drawable
, statts
, &num_buffers
);
355 dri2_drawable_process_buffers(drawable
, buffers
, num_buffers
);
359 dri2_flush_frontbuffer(struct dri_drawable
*drawable
,
360 enum st_attachment_type statt
)
362 __DRIdrawable
*dri_drawable
= drawable
->dPriv
;
363 struct __DRIdri2LoaderExtensionRec
*loader
= drawable
->sPriv
->dri2
.loader
;
365 if (loader
->flushFrontBuffer
== NULL
)
368 if (statt
== ST_ATTACHMENT_FRONT_LEFT
) {
369 loader
->flushFrontBuffer(dri_drawable
, dri_drawable
->loaderPrivate
);
374 dri2_lookup_egl_image(struct dri_context
*ctx
, void *handle
)
376 __DRIimageLookupExtension
*loader
= ctx
->sPriv
->dri2
.image
;
379 if (!loader
->lookupEGLImage
)
382 img
= loader
->lookupEGLImage(ctx
->cPriv
, handle
, ctx
->cPriv
->loaderPrivate
);
388 dri2_create_image_from_name(__DRIcontext
*context
,
389 int width
, int height
, int format
,
390 int name
, int pitch
, void *loaderPrivate
)
392 struct dri_screen
*screen
= dri_screen(context
->driScreenPriv
);
394 struct pipe_resource templ
;
395 struct winsys_handle whandle
;
399 tex_usage
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
402 case __DRI_IMAGE_FORMAT_RGB565
:
403 pf
= PIPE_FORMAT_B5G6R5_UNORM
;
405 case __DRI_IMAGE_FORMAT_XRGB8888
:
406 pf
= PIPE_FORMAT_B8G8R8X8_UNORM
;
408 case __DRI_IMAGE_FORMAT_ARGB8888
:
409 pf
= PIPE_FORMAT_B8G8R8A8_UNORM
;
412 pf
= PIPE_FORMAT_NONE
;
415 if (pf
== PIPE_FORMAT_NONE
)
418 img
= CALLOC_STRUCT(__DRIimageRec
);
422 memset(&templ
, 0, sizeof(templ
));
423 templ
.bind
= tex_usage
;
425 templ
.target
= PIPE_TEXTURE_2D
;
426 templ
.last_level
= 0;
427 templ
.width0
= width
;
428 templ
.height0
= height
;
431 memset(&whandle
, 0, sizeof(whandle
));
432 whandle
.handle
= name
;
433 whandle
.stride
= pitch
* util_format_get_blocksize(pf
);
435 img
->texture
= screen
->base
.screen
->resource_from_handle(screen
->base
.screen
,
445 img
->loader_private
= loaderPrivate
;
451 dri2_create_image_from_renderbuffer(__DRIcontext
*context
,
452 int renderbuffer
, void *loaderPrivate
)
454 struct dri_context
*ctx
= dri_context(context
->driverPrivate
);
456 if (!ctx
->st
->get_resource_for_egl_image
)
464 dri2_destroy_image(__DRIimage
*img
)
466 pipe_resource_reference(&img
->texture
, NULL
);
470 static struct __DRIimageExtensionRec dri2ImageExtension
= {
471 { __DRI_IMAGE
, __DRI_IMAGE_VERSION
},
472 dri2_create_image_from_name
,
473 dri2_create_image_from_renderbuffer
,
478 * Backend function init_screen.
481 static const __DRIextension
*dri_screen_extensions
[] = {
482 &driReadDrawableExtension
,
483 &driCopySubBufferExtension
.base
,
484 &driSwapControlExtension
.base
,
485 &driMediaStreamCounterExtension
.base
,
486 &dri2TexBufferExtension
.base
,
487 &dri2FlushExtension
.base
,
488 &dri2ImageExtension
.base
,
489 &dri2ConfigQueryExtension
.base
,
494 * This is the driver specific part of the createNewScreen entry point.
496 * Returns the __GLcontextModes supported by this driver.
498 static const __DRIconfig
**
499 dri2_init_screen(__DRIscreen
* sPriv
)
501 const __DRIconfig
**configs
;
502 struct dri_screen
*screen
;
503 struct pipe_screen
*pscreen
;
505 screen
= CALLOC_STRUCT(dri_screen
);
509 screen
->sPriv
= sPriv
;
510 screen
->fd
= sPriv
->fd
;
512 sPriv
->private = (void *)screen
;
513 sPriv
->extensions
= dri_screen_extensions
;
515 pscreen
= driver_descriptor
.create_screen(screen
->fd
);
516 /* dri_init_screen_helper checks pscreen for us */
518 configs
= dri_init_screen_helper(screen
, pscreen
, 32);
522 screen
->auto_fake_front
= dri_with_format(sPriv
);
523 screen
->broken_invalidate
= !sPriv
->dri2
.useInvalidate
;
527 dri_destroy_screen_helper(screen
);
533 dri2_create_context(gl_api api
, const __GLcontextModes
* visual
,
534 __DRIcontext
* cPriv
, void *sharedContextPrivate
)
536 struct dri_context
*ctx
= NULL
;
538 if (!dri_create_context(api
, visual
, cPriv
, sharedContextPrivate
))
541 ctx
= cPriv
->driverPrivate
;
543 ctx
->lookup_egl_image
= dri2_lookup_egl_image
;
549 dri2_create_buffer(__DRIscreen
* sPriv
,
550 __DRIdrawable
* dPriv
,
551 const __GLcontextModes
* visual
, boolean isPixmap
)
553 struct dri_drawable
*drawable
= NULL
;
555 if (!dri_create_buffer(sPriv
, dPriv
, visual
, isPixmap
))
558 drawable
= dPriv
->driverPrivate
;
560 drawable
->allocate_textures
= dri2_allocate_textures
;
561 drawable
->flush_frontbuffer
= dri2_flush_frontbuffer
;
567 * DRI driver virtual function table.
569 * DRI versions differ in their implementation of init_screen and swap_buffers.
571 const struct __DriverAPIRec driDriverAPI
= {
573 .InitScreen2
= dri2_init_screen
,
574 .DestroyScreen
= dri_destroy_screen
,
575 .CreateContext
= dri2_create_context
,
576 .DestroyContext
= dri_destroy_context
,
577 .CreateBuffer
= dri2_create_buffer
,
578 .DestroyBuffer
= dri_destroy_buffer
,
579 .MakeCurrent
= dri_make_current
,
580 .UnbindContext
= dri_unbind_context
,
583 .GetDrawableMSC
= NULL
,
587 .CopySubBuffer
= NULL
,
590 /* This is the table of extensions that the loader will dlsym() for. */
591 PUBLIC
const __DRIextension
*__driDriverExtensions
[] = {
592 &driCoreExtension
.base
,
593 &driLegacyExtension
.base
,
594 &driDRI2Extension
.base
,
598 /* vim: set sw=3 ts=8 sts=3 expandtab: */