2 * Copyright © 2013 Keith Packard
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * Portions of this code were adapted from dri2_glx.c which carries the
25 * following copyright:
27 * Copyright © 2008 Red Hat, Inc.
29 * Permission is hereby granted, free of charge, to any person obtaining a
30 * copy of this software and associated documentation files (the "Soft-
31 * ware"), to deal in the Software without restriction, including without
32 * limitation the rights to use, copy, modify, merge, publish, distribute,
33 * and/or sell copies of the Software, and to permit persons to whom the
34 * Software is furnished to do so, provided that the above copyright
35 * notice(s) and this permission notice appear in all copies of the Soft-
36 * ware and that both the above copyright notice(s) and this permission
37 * notice appear in supporting documentation.
39 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
40 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
41 * ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
42 * RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN
43 * THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSE-
44 * QUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
45 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
46 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFOR-
47 * MANCE OF THIS SOFTWARE.
49 * Except as contained in this notice, the name of a copyright holder shall
50 * not be used in advertising or otherwise to promote the sale, use or
51 * other dealings in this Software without prior written authorization of
52 * the copyright holder.
55 * Kristian Høgsberg (krh@redhat.com)
58 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
61 #include <X11/extensions/Xfixes.h>
62 #include <X11/Xlib-xcb.h>
63 #include <X11/xshmfence.h>
66 #include <xcb/present.h>
69 #include "glxclient.h"
74 #include <sys/types.h>
79 #include "dri_common.h"
80 #include "dri3_priv.h"
82 static const struct glx_context_vtable dri3_context_vtable
;
85 dri3_fence_reset(xcb_connection_t
*c
, struct dri3_buffer
*buffer
)
87 xshmfence_reset(buffer
->shm_fence
);
91 dri3_fence_set(struct dri3_buffer
*buffer
)
93 xshmfence_trigger(buffer
->shm_fence
);
97 dri3_fence_trigger(xcb_connection_t
*c
, struct dri3_buffer
*buffer
)
99 xcb_sync_trigger_fence(c
, buffer
->sync_fence
);
103 dri3_fence_await(xcb_connection_t
*c
, struct dri3_buffer
*buffer
)
106 xshmfence_await(buffer
->shm_fence
);
110 dri3_fence_triggered(struct dri3_buffer
*buffer
)
112 return xshmfence_query(buffer
->shm_fence
);
116 dri3_destroy_context(struct glx_context
*context
)
118 struct dri3_context
*pcp
= (struct dri3_context
*) context
;
119 struct dri3_screen
*psc
= (struct dri3_screen
*) context
->psc
;
121 driReleaseDrawables(&pcp
->base
);
123 free((char *) context
->extensions
);
125 (*psc
->core
->destroyContext
) (pcp
->driContext
);
131 dri3_bind_context(struct glx_context
*context
, struct glx_context
*old
,
132 GLXDrawable draw
, GLXDrawable read
)
134 struct dri3_context
*pcp
= (struct dri3_context
*) context
;
135 struct dri3_screen
*psc
= (struct dri3_screen
*) pcp
->base
.psc
;
136 struct dri3_drawable
*pdraw
, *pread
;
138 pdraw
= (struct dri3_drawable
*) driFetchDrawable(context
, draw
);
139 pread
= (struct dri3_drawable
*) driFetchDrawable(context
, read
);
141 driReleaseDrawables(&pcp
->base
);
143 if (pdraw
== NULL
|| pread
== NULL
)
144 return GLXBadDrawable
;
146 if (!(*psc
->core
->bindContext
) (pcp
->driContext
,
147 pdraw
->driDrawable
, pread
->driDrawable
))
148 return GLXBadContext
;
154 dri3_unbind_context(struct glx_context
*context
, struct glx_context
*new)
156 struct dri3_context
*pcp
= (struct dri3_context
*) context
;
157 struct dri3_screen
*psc
= (struct dri3_screen
*) pcp
->base
.psc
;
159 (*psc
->core
->unbindContext
) (pcp
->driContext
);
162 static struct glx_context
*
163 dri3_create_context_attribs(struct glx_screen
*base
,
164 struct glx_config
*config_base
,
165 struct glx_context
*shareList
,
166 unsigned num_attribs
,
167 const uint32_t *attribs
,
170 struct dri3_context
*pcp
= NULL
;
171 struct dri3_context
*pcp_shared
= NULL
;
172 struct dri3_screen
*psc
= (struct dri3_screen
*) base
;
173 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
174 __DRIcontext
*shared
= NULL
;
176 uint32_t minor_ver
= 1;
177 uint32_t major_ver
= 2;
180 int reset
= __DRI_CTX_RESET_NO_NOTIFICATION
;
181 uint32_t ctx_attribs
[2 * 5];
182 unsigned num_ctx_attribs
= 0;
183 uint32_t render_type
;
185 /* Remap the GLX tokens to DRI2 tokens.
187 if (!dri2_convert_glx_attribs(num_attribs
, attribs
,
188 &major_ver
, &minor_ver
,
189 &render_type
, &flags
, &api
,
193 /* Check the renderType value */
194 if (!validate_renderType_against_config(config_base
, render_type
))
198 pcp_shared
= (struct dri3_context
*) shareList
;
199 shared
= pcp_shared
->driContext
;
202 pcp
= calloc(1, sizeof *pcp
);
204 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
208 if (!glx_context_init(&pcp
->base
, &psc
->base
, &config
->base
))
211 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
212 ctx_attribs
[num_ctx_attribs
++] = major_ver
;
213 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
214 ctx_attribs
[num_ctx_attribs
++] = minor_ver
;
216 /* Only send a value when the non-default value is requested. By doing
217 * this we don't have to check the driver's DRI3 version before sending the
220 if (reset
!= __DRI_CTX_RESET_NO_NOTIFICATION
) {
221 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
222 ctx_attribs
[num_ctx_attribs
++] = reset
;
226 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_FLAGS
;
228 /* The current __DRI_CTX_FLAG_* values are identical to the
229 * GLX_CONTEXT_*_BIT values.
231 ctx_attribs
[num_ctx_attribs
++] = flags
;
235 (*psc
->image_driver
->createContextAttribs
) (psc
->driScreen
,
244 if (pcp
->driContext
== NULL
)
247 pcp
->base
.vtable
= &dri3_context_vtable
;
257 static struct glx_context
*
258 dri3_create_context(struct glx_screen
*base
,
259 struct glx_config
*config_base
,
260 struct glx_context
*shareList
, int renderType
)
264 return dri3_create_context_attribs(base
, config_base
, shareList
,
269 dri3_free_render_buffer(struct dri3_drawable
*pdraw
, struct dri3_buffer
*buffer
);
272 dri3_destroy_drawable(__GLXDRIdrawable
*base
)
274 struct dri3_screen
*psc
= (struct dri3_screen
*) base
->psc
;
275 struct dri3_drawable
*pdraw
= (struct dri3_drawable
*) base
;
276 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->base
.psc
->dpy
);
279 (*psc
->core
->destroyDrawable
) (pdraw
->driDrawable
);
281 for (i
= 0; i
< DRI3_NUM_BUFFERS
; i
++) {
282 if (pdraw
->buffers
[i
])
283 dri3_free_render_buffer(pdraw
, pdraw
->buffers
[i
]);
286 if (pdraw
->special_event
)
287 xcb_unregister_for_special_event(c
, pdraw
->special_event
);
291 static __GLXDRIdrawable
*
292 dri3_create_drawable(struct glx_screen
*base
, XID xDrawable
,
293 GLXDrawable drawable
, struct glx_config
*config_base
)
295 struct dri3_drawable
*pdraw
;
296 struct dri3_screen
*psc
= (struct dri3_screen
*) base
;
297 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
298 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
300 pdraw
= calloc(1, sizeof(*pdraw
));
304 pdraw
->base
.destroyDrawable
= dri3_destroy_drawable
;
305 pdraw
->base
.xDrawable
= xDrawable
;
306 pdraw
->base
.drawable
= drawable
;
307 pdraw
->base
.psc
= &psc
->base
;
308 pdraw
->swap_interval
= 1; /* default may be overridden below */
309 pdraw
->have_back
= 0;
310 pdraw
->have_fake_front
= 0;
313 psc
->config
->configQueryi(psc
->driScreen
,
314 "vblank_mode", &vblank_mode
);
316 switch (vblank_mode
) {
317 case DRI_CONF_VBLANK_NEVER
:
318 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
319 pdraw
->swap_interval
= 0;
321 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
322 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
324 pdraw
->swap_interval
= 1;
328 (void) __glXInitialize(psc
->base
.dpy
);
330 /* Create a new drawable */
332 (*psc
->image_driver
->createNewDrawable
) (psc
->driScreen
,
333 config
->driConfig
, pdraw
);
335 if (!pdraw
->driDrawable
) {
341 * Make sure server has the same swap interval we do for the new
344 if (psc
->vtable
.setSwapInterval
)
345 psc
->vtable
.setSwapInterval(&pdraw
->base
, pdraw
->swap_interval
);
351 * Process one Present event
354 dri3_handle_present_event(struct dri3_drawable
*priv
, xcb_present_generic_event_t
*ge
)
356 switch (ge
->evtype
) {
357 case XCB_PRESENT_CONFIGURE_NOTIFY
: {
358 xcb_present_configure_notify_event_t
*ce
= (void *) ge
;
360 priv
->width
= ce
->width
;
361 priv
->height
= ce
->height
;
364 case XCB_PRESENT_COMPLETE_NOTIFY
: {
365 xcb_present_complete_notify_event_t
*ce
= (void *) ge
;
367 if (ce
->kind
== XCB_PRESENT_COMPLETE_KIND_PIXMAP
)
368 priv
->present_event_serial
= ce
->serial
;
370 priv
->present_msc_event_serial
= ce
->serial
;
375 case XCB_PRESENT_EVENT_IDLE_NOTIFY
: {
376 xcb_present_idle_notify_event_t
*ie
= (void *) ge
;
379 for (b
= 0; b
< sizeof (priv
->buffers
) / sizeof (priv
->buffers
[0]); b
++) {
380 struct dri3_buffer
*buf
= priv
->buffers
[b
];
382 if (buf
&& buf
->pixmap
== ie
->pixmap
) {
394 dri3_wait_for_msc(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
395 int64_t remainder
, int64_t *ust
, int64_t *msc
, int64_t *sbc
)
397 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->psc
->dpy
);
398 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
399 xcb_generic_event_t
*ev
;
400 xcb_present_generic_event_t
*ge
;
402 /* Ask for the an event for the target MSC */
403 ++priv
->present_msc_request_serial
;
404 xcb_present_notify_msc(c
,
405 priv
->base
.xDrawable
,
406 priv
->present_msc_request_serial
,
413 /* Wait for the event */
414 if (priv
->special_event
) {
415 while (priv
->present_msc_request_serial
!= priv
->present_msc_event_serial
) {
416 ev
= xcb_wait_for_special_event(c
, priv
->special_event
);
420 dri3_handle_present_event(priv
, ge
);
432 dri3_drawable_get_msc(struct glx_screen
*psc
, __GLXDRIdrawable
*pdraw
,
433 int64_t *ust
, int64_t *msc
, int64_t *sbc
)
435 return dri3_wait_for_msc(pdraw
, 0, 0, 0, ust
, msc
,sbc
);
438 /** dri3_wait_for_sbc
440 * Wait for the swap buffer count to increase. The only way this
441 * can happen is if some other thread is doing swap buffers as
442 * we no longer share swap buffer counts with other processes.
444 * I'm not sure this is actually useful as such, and so this
445 * implementation is a kludge that just polls once a second
448 dri3_wait_for_sbc(__GLXDRIdrawable
*pdraw
, int64_t target_sbc
, int64_t *ust
,
449 int64_t *msc
, int64_t *sbc
)
451 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
453 while (priv
->sbc
< target_sbc
) {
456 return dri3_wait_for_msc(pdraw
, 0, 0, 0, ust
, msc
, sbc
);
460 * Asks the driver to flush any queued work necessary for serializing with the
461 * X command stream, and optionally the slightly more strict requirement of
462 * glFlush() equivalence (which would require flushing even if nothing had
463 * been drawn to a window system framebuffer, for example).
466 dri3_flush(struct dri3_screen
*psc
,
467 struct dri3_drawable
*draw
,
469 enum __DRI2throttleReason throttle_reason
)
471 struct glx_context
*gc
= __glXGetCurrentContext();
474 struct dri3_context
*dri3Ctx
= (struct dri3_context
*)gc
;
476 (*psc
->f
->flush_with_flags
)(dri3Ctx
->driContext
, draw
->driDrawable
, flags
, throttle_reason
);
480 static xcb_gcontext_t
481 dri3_drawable_gc(struct dri3_drawable
*priv
)
485 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
489 (priv
->gc
= xcb_generate_id(c
)),
490 priv
->base
.xDrawable
,
491 XCB_GC_GRAPHICS_EXPOSURES
,
497 static struct dri3_buffer
*
498 dri3_back_buffer(struct dri3_drawable
*priv
)
500 return priv
->buffers
[DRI3_BACK_ID(priv
->cur_back
)];
503 static struct dri3_buffer
*
504 dri3_fake_front_buffer(struct dri3_drawable
*priv
)
506 return priv
->buffers
[DRI3_FRONT_ID
];
510 dri3_copy_area (xcb_connection_t
*c
/**< */,
511 xcb_drawable_t src_drawable
/**< */,
512 xcb_drawable_t dst_drawable
/**< */,
513 xcb_gcontext_t gc
/**< */,
514 int16_t src_x
/**< */,
515 int16_t src_y
/**< */,
516 int16_t dst_x
/**< */,
517 int16_t dst_y
/**< */,
518 uint16_t width
/**< */,
519 uint16_t height
/**< */)
521 xcb_void_cookie_t cookie
;
523 cookie
= xcb_copy_area_checked(c
,
533 xcb_discard_reply(c
, cookie
.sequence
);
537 dri3_copy_sub_buffer(__GLXDRIdrawable
*pdraw
, int x
, int y
,
538 int width
, int height
,
541 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
542 struct dri3_screen
*psc
= (struct dri3_screen
*) pdraw
->psc
;
543 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
544 struct dri3_buffer
*back
= dri3_back_buffer(priv
);
548 /* Check we have the right attachments */
549 if (!priv
->have_back
|| priv
->is_pixmap
)
552 flags
= __DRI2_FLUSH_DRAWABLE
;
554 flags
|= __DRI2_FLUSH_CONTEXT
;
555 dri3_flush(psc
, priv
, flags
, __DRI2_THROTTLE_SWAPBUFFER
);
557 y
= priv
->height
- y
- height
;
559 dri3_fence_reset(c
, back
);
561 dri3_back_buffer(priv
)->pixmap
,
562 priv
->base
.xDrawable
,
563 dri3_drawable_gc(priv
),
564 x
, y
, x
, y
, width
, height
);
565 dri3_fence_trigger(c
, back
);
566 /* Refresh the fake front (if present) after we just damaged the real
569 if (priv
->have_fake_front
) {
570 dri3_fence_reset(c
, dri3_fake_front_buffer(priv
));
572 dri3_back_buffer(priv
)->pixmap
,
573 dri3_fake_front_buffer(priv
)->pixmap
,
574 dri3_drawable_gc(priv
),
575 x
, y
, x
, y
, width
, height
);
576 dri3_fence_trigger(c
, dri3_fake_front_buffer(priv
));
577 dri3_fence_await(c
, dri3_fake_front_buffer(priv
));
579 dri3_fence_await(c
, back
);
583 dri3_copy_drawable(struct dri3_drawable
*priv
, Drawable dest
, Drawable src
)
585 struct dri3_screen
*psc
= (struct dri3_screen
*) priv
->base
.psc
;
586 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
588 dri3_flush(psc
, priv
, __DRI2_FLUSH_DRAWABLE
, 0);
590 dri3_fence_reset(c
, dri3_fake_front_buffer(priv
));
593 dri3_drawable_gc(priv
),
594 0, 0, 0, 0, priv
->width
, priv
->height
);
595 dri3_fence_trigger(c
, dri3_fake_front_buffer(priv
));
596 dri3_fence_await(c
, dri3_fake_front_buffer(priv
));
600 dri3_wait_x(struct glx_context
*gc
)
602 struct dri3_drawable
*priv
= (struct dri3_drawable
*)
603 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
605 if (priv
== NULL
|| !priv
->have_fake_front
)
608 dri3_copy_drawable(priv
, dri3_fake_front_buffer(priv
)->pixmap
, priv
->base
.xDrawable
);
612 dri3_wait_gl(struct glx_context
*gc
)
614 struct dri3_drawable
*priv
= (struct dri3_drawable
*)
615 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
617 if (priv
== NULL
|| !priv
->have_fake_front
)
620 dri3_copy_drawable(priv
, priv
->base
.xDrawable
, dri3_fake_front_buffer(priv
)->pixmap
);
624 * Called by the driver when it needs to update the real front buffer with the
625 * contents of its fake front buffer.
628 dri3_flush_front_buffer(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
630 struct glx_context
*gc
;
631 struct dri3_drawable
*pdraw
= loaderPrivate
;
632 struct dri3_screen
*psc
;
637 if (!pdraw
->base
.psc
)
640 psc
= (struct dri3_screen
*) pdraw
->base
.psc
;
642 (void) __glXInitialize(psc
->base
.dpy
);
644 gc
= __glXGetCurrentContext();
646 dri3_flush(psc
, pdraw
, __DRI2_FLUSH_DRAWABLE
, __DRI2_THROTTLE_FLUSHFRONT
);
652 dri3_cpp_for_format(uint32_t format
) {
654 case __DRI_IMAGE_FORMAT_R8
:
656 case __DRI_IMAGE_FORMAT_RGB565
:
657 case __DRI_IMAGE_FORMAT_GR88
:
659 case __DRI_IMAGE_FORMAT_XRGB8888
:
660 case __DRI_IMAGE_FORMAT_ARGB8888
:
661 case __DRI_IMAGE_FORMAT_ABGR8888
:
662 case __DRI_IMAGE_FORMAT_XBGR8888
:
663 case __DRI_IMAGE_FORMAT_XRGB2101010
:
664 case __DRI_IMAGE_FORMAT_ARGB2101010
:
665 case __DRI_IMAGE_FORMAT_SARGB8
:
667 case __DRI_IMAGE_FORMAT_NONE
:
674 /** dri3_alloc_render_buffer
676 * Use the driver createImage function to construct a __DRIimage, then
677 * get a file descriptor for that and create an X pixmap from that
679 * Allocate an xshmfence for synchronization
681 static struct dri3_buffer
*
682 dri3_alloc_render_buffer(struct glx_screen
*glx_screen
, Drawable draw
,
683 unsigned int format
, int width
, int height
, int depth
)
685 struct dri3_screen
*psc
= (struct dri3_screen
*) glx_screen
;
686 Display
*dpy
= glx_screen
->dpy
;
687 struct dri3_buffer
*buffer
;
688 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
690 xcb_sync_fence_t sync_fence
;
691 struct xshmfence
*shm_fence
;
692 int buffer_fd
, fence_fd
;
695 /* Create an xshmfence object and
696 * prepare to send that to the X server
699 fence_fd
= xshmfence_alloc_shm();
702 shm_fence
= xshmfence_map_shm(fence_fd
);
703 if (shm_fence
== NULL
)
706 /* Allocate the image from the driver
708 buffer
= calloc(1, sizeof (struct dri3_buffer
));
712 buffer
->cpp
= dri3_cpp_for_format(format
);
716 buffer
->image
= (*psc
->image
->createImage
) (psc
->driScreen
,
719 __DRI_IMAGE_USE_SHARE
|__DRI_IMAGE_USE_SCANOUT
,
726 /* X wants the stride, so ask the image for it
728 if (!(*psc
->image
->queryImage
)(buffer
->image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
))
729 goto no_buffer_attrib
;
731 buffer
->pitch
= stride
;
733 if (!(*psc
->image
->queryImage
)(buffer
->image
, __DRI_IMAGE_ATTRIB_FD
, &buffer_fd
))
734 goto no_buffer_attrib
;
736 xcb_dri3_pixmap_from_buffer(c
,
737 (pixmap
= xcb_generate_id(c
)),
740 width
, height
, buffer
->pitch
,
741 depth
, buffer
->cpp
* 8,
744 xcb_dri3_fence_from_fd(c
,
746 (sync_fence
= xcb_generate_id(c
)),
750 buffer
->pixmap
= pixmap
;
751 buffer
->own_pixmap
= true;
752 buffer
->sync_fence
= sync_fence
;
753 buffer
->shm_fence
= shm_fence
;
754 buffer
->width
= width
;
755 buffer
->height
= height
;
757 /* Mark the buffer as idle
759 dri3_fence_set(buffer
);
764 (*psc
->image
->destroyImage
)(buffer
->image
);
768 xshmfence_unmap_shm(shm_fence
);
774 /** dri3_free_render_buffer
776 * Free everything associated with one render buffer including pixmap, fence
777 * stuff and the driver image
780 dri3_free_render_buffer(struct dri3_drawable
*pdraw
, struct dri3_buffer
*buffer
)
782 struct dri3_screen
*psc
= (struct dri3_screen
*) pdraw
->base
.psc
;
783 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->base
.psc
->dpy
);
785 if (buffer
->own_pixmap
)
786 xcb_free_pixmap(c
, buffer
->pixmap
);
787 xcb_sync_destroy_fence(c
, buffer
->sync_fence
);
788 xshmfence_unmap_shm(buffer
->shm_fence
);
789 (*psc
->image
->destroyImage
)(buffer
->image
);
794 /** dri3_flush_present_events
796 * Process any present events that have been received from the X server
799 dri3_flush_present_events(struct dri3_drawable
*priv
)
801 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
803 /* Check to see if any configuration changes have occurred
804 * since we were last invoked
806 if (priv
->special_event
) {
807 xcb_generic_event_t
*ev
;
809 while ((ev
= xcb_poll_for_special_event(c
, priv
->special_event
)) != NULL
) {
810 xcb_present_generic_event_t
*ge
= (void *) ev
;
811 dri3_handle_present_event(priv
, ge
);
816 /** dri3_update_drawable
818 * Called the first time we use the drawable and then
819 * after we receive present configure notify events to
820 * track the geometry of the drawable
823 dri3_update_drawable(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
825 struct dri3_drawable
*priv
= loaderPrivate
;
826 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
828 /* First time through, go get the current drawable geometry
830 if (priv
->width
== 0 || priv
->height
== 0 || priv
->depth
== 0) {
831 xcb_get_geometry_cookie_t geom_cookie
;
832 xcb_get_geometry_reply_t
*geom_reply
;
833 xcb_void_cookie_t cookie
;
834 xcb_generic_error_t
*error
;
836 /* Try to select for input on the window.
838 * If the drawable is a window, this will get our events
841 * Otherwise, we'll get a BadWindow error back from this request which
842 * will let us know that the drawable is a pixmap instead.
846 cookie
= xcb_present_select_input_checked(c
,
847 (priv
->eid
= xcb_generate_id(c
)),
848 priv
->base
.xDrawable
,
849 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY
|
850 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY
|
851 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY
);
853 /* Create an XCB event queue to hold present events outside of the usual
854 * application event queue
856 priv
->special_event
= xcb_register_for_special_xge(c
,
861 geom_cookie
= xcb_get_geometry(c
, priv
->base
.xDrawable
);
863 geom_reply
= xcb_get_geometry_reply(c
, geom_cookie
, NULL
);
868 priv
->width
= geom_reply
->width
;
869 priv
->height
= geom_reply
->height
;
870 priv
->depth
= geom_reply
->depth
;
871 priv
->is_pixmap
= false;
875 /* Check to see if our select input call failed. If it failed with a
876 * BadWindow error, then assume the drawable is a pixmap. Destroy the
877 * special event queue created above and mark the drawable as a pixmap
880 error
= xcb_request_check(c
, cookie
);
883 if (error
->error_code
!= BadWindow
) {
887 priv
->is_pixmap
= true;
888 xcb_unregister_for_special_event(c
, priv
->special_event
);
889 priv
->special_event
= NULL
;
892 dri3_flush_present_events(priv
);
896 /* the DRIimage createImage function takes __DRI_IMAGE_FORMAT codes, while
897 * the createImageFromFds call takes __DRI_IMAGE_FOURCC codes. To avoid
898 * complete confusion, just deal in __DRI_IMAGE_FORMAT codes for now and
899 * translate to __DRI_IMAGE_FOURCC codes in the call to createImageFromFds
902 image_format_to_fourcc(int format
)
905 /* Convert from __DRI_IMAGE_FORMAT to __DRI_IMAGE_FOURCC (sigh) */
907 case __DRI_IMAGE_FORMAT_RGB565
: return __DRI_IMAGE_FOURCC_RGB565
;
908 case __DRI_IMAGE_FORMAT_XRGB8888
: return __DRI_IMAGE_FOURCC_XRGB8888
;
909 case __DRI_IMAGE_FORMAT_ARGB8888
: return __DRI_IMAGE_FOURCC_ARGB8888
;
910 case __DRI_IMAGE_FORMAT_ABGR8888
: return __DRI_IMAGE_FOURCC_ABGR8888
;
911 case __DRI_IMAGE_FORMAT_XBGR8888
: return __DRI_IMAGE_FOURCC_XBGR8888
;
916 /** dri3_get_pixmap_buffer
918 * Get the DRM object for a pixmap from the X server and
919 * wrap that with a __DRIimage structure using createImageFromFds
921 static struct dri3_buffer
*
922 dri3_get_pixmap_buffer(__DRIdrawable
*driDrawable
,
924 enum dri3_buffer_type buffer_type
,
927 struct dri3_drawable
*pdraw
= loaderPrivate
;
928 int buf_id
= dri3_pixmap_buf_id(buffer_type
);
929 struct dri3_buffer
*buffer
= pdraw
->buffers
[buf_id
];
931 xcb_dri3_buffer_from_pixmap_cookie_t bp_cookie
;
932 xcb_dri3_buffer_from_pixmap_reply_t
*bp_reply
;
935 struct dri3_screen
*psc
;
937 xcb_sync_fence_t sync_fence
;
938 struct xshmfence
*shm_fence
;
940 __DRIimage
*image_planar
;
946 pixmap
= pdraw
->base
.xDrawable
;
947 psc
= (struct dri3_screen
*) pdraw
->base
.psc
;
949 c
= XGetXCBConnection(dpy
);
951 buffer
= calloc(1, sizeof (struct dri3_buffer
));
955 fence_fd
= xshmfence_alloc_shm();
958 shm_fence
= xshmfence_map_shm(fence_fd
);
959 if (shm_fence
== NULL
) {
964 xcb_dri3_fence_from_fd(c
,
966 (sync_fence
= xcb_generate_id(c
)),
970 /* Get an FD for the pixmap object
972 bp_cookie
= xcb_dri3_buffer_from_pixmap(c
, pixmap
);
973 bp_reply
= xcb_dri3_buffer_from_pixmap_reply(c
, bp_cookie
, NULL
);
976 fds
= xcb_dri3_buffer_from_pixmap_reply_fds(c
, bp_reply
);
978 stride
= bp_reply
->stride
;
981 /* createImageFromFds creates a wrapper __DRIimage structure which
982 * can deal with multiple planes for things like Yuv images. So, once
983 * we've gotten the planar wrapper, pull the single plane out of it and
984 * discard the wrapper.
986 image_planar
= (*psc
->image
->createImageFromFds
) (psc
->driScreen
,
989 image_format_to_fourcc(format
),
991 &stride
, &offset
, buffer
);
996 buffer
->image
= (*psc
->image
->fromPlanar
)(image_planar
, 0, buffer
);
998 (*psc
->image
->destroyImage
)(image_planar
);
1003 buffer
->pixmap
= pixmap
;
1004 buffer
->own_pixmap
= false;
1005 buffer
->width
= bp_reply
->width
;
1006 buffer
->height
= bp_reply
->height
;
1007 buffer
->buffer_type
= buffer_type
;
1008 buffer
->shm_fence
= shm_fence
;
1009 buffer
->sync_fence
= sync_fence
;
1011 pdraw
->buffers
[buf_id
] = buffer
;
1015 xcb_sync_destroy_fence(c
, sync_fence
);
1016 xshmfence_unmap_shm(shm_fence
);
1025 * Find an idle back buffer. If there isn't one, then
1026 * wait for a present idle notify event from the X server
1029 dri3_find_back(xcb_connection_t
*c
, struct dri3_drawable
*priv
)
1032 xcb_generic_event_t
*ev
;
1033 xcb_present_generic_event_t
*ge
;
1037 for (b
= 0; b
< DRI3_NUM_BACK
; b
++) {
1038 int id
= DRI3_BACK_ID(b
);
1039 struct dri3_buffer
*buffer
= priv
->buffers
[id
];
1046 ev
= xcb_wait_for_special_event(c
, priv
->special_event
);
1050 dri3_handle_present_event(priv
, ge
);
1056 * Find a front or back buffer, allocating new ones as necessary
1058 static struct dri3_buffer
*
1059 dri3_get_buffer(__DRIdrawable
*driDrawable
,
1060 unsigned int format
,
1061 enum dri3_buffer_type buffer_type
,
1062 void *loaderPrivate
)
1064 struct dri3_drawable
*priv
= loaderPrivate
;
1065 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
1066 struct dri3_buffer
*buffer
;
1069 if (buffer_type
== dri3_buffer_back
) {
1070 int back
= dri3_find_back(c
, priv
);
1075 priv
->cur_back
= back
;
1076 buf_id
= DRI3_BACK_ID(priv
->cur_back
);
1078 buf_id
= DRI3_FRONT_ID
;
1081 buffer
= priv
->buffers
[buf_id
];
1083 /* Allocate a new buffer if there isn't an old one, or if that
1084 * old one is the wrong size
1086 if (!buffer
|| buffer
->width
!= priv
->width
|| buffer
->height
!= priv
->height
) {
1087 struct dri3_buffer
*new_buffer
;
1089 /* Allocate the new buffers
1091 new_buffer
= dri3_alloc_render_buffer(priv
->base
.psc
,
1092 priv
->base
.xDrawable
,
1093 format
, priv
->width
, priv
->height
, priv
->depth
);
1097 /* When resizing, copy the contents of the old buffer, waiting for that
1098 * copy to complete using our fences before proceeding
1100 switch (buffer_type
) {
1101 case dri3_buffer_back
:
1103 dri3_fence_reset(c
, new_buffer
);
1104 dri3_fence_await(c
, buffer
);
1108 dri3_drawable_gc(priv
),
1109 0, 0, 0, 0, priv
->width
, priv
->height
);
1110 dri3_fence_trigger(c
, new_buffer
);
1111 dri3_free_render_buffer(priv
, buffer
);
1114 case dri3_buffer_front
:
1115 dri3_fence_reset(c
, new_buffer
);
1117 priv
->base
.xDrawable
,
1119 dri3_drawable_gc(priv
),
1120 0, 0, 0, 0, priv
->width
, priv
->height
);
1121 dri3_fence_trigger(c
, new_buffer
);
1124 buffer
= new_buffer
;
1125 buffer
->buffer_type
= buffer_type
;
1126 priv
->buffers
[buf_id
] = buffer
;
1128 dri3_fence_await(c
, buffer
);
1130 /* Return the requested buffer */
1134 /** dri3_free_buffers
1136 * Free the front bufffer or all of the back buffers. Used
1137 * when the application changes which buffers it needs
1140 dri3_free_buffers(__DRIdrawable
*driDrawable
,
1141 enum dri3_buffer_type buffer_type
,
1142 void *loaderPrivate
)
1144 struct dri3_drawable
*priv
= loaderPrivate
;
1145 struct dri3_buffer
*buffer
;
1150 switch (buffer_type
) {
1151 case dri3_buffer_back
:
1152 first_id
= DRI3_BACK_ID(0);
1153 n_id
= DRI3_NUM_BACK
;
1155 case dri3_buffer_front
:
1156 first_id
= DRI3_FRONT_ID
;
1160 for (buf_id
= first_id
; buf_id
< first_id
+ n_id
; buf_id
++) {
1161 buffer
= priv
->buffers
[buf_id
];
1163 dri3_free_render_buffer(priv
, buffer
);
1164 priv
->buffers
[buf_id
] = NULL
;
1169 /** dri3_get_buffers
1171 * The published buffer allocation API.
1172 * Returns all of the necessary buffers, allocating
1176 dri3_get_buffers(__DRIdrawable
*driDrawable
,
1177 unsigned int format
,
1179 void *loaderPrivate
,
1180 uint32_t buffer_mask
,
1181 struct __DRIimageList
*buffers
)
1183 struct dri3_drawable
*priv
= loaderPrivate
;
1184 struct dri3_buffer
*front
, *back
;
1186 buffers
->image_mask
= 0;
1187 buffers
->front
= NULL
;
1188 buffers
->back
= NULL
;
1193 if (!dri3_update_drawable(driDrawable
, loaderPrivate
))
1196 /* pixmaps always have front buffers */
1197 if (priv
->is_pixmap
)
1198 buffer_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
1200 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
1201 if (priv
->is_pixmap
)
1202 front
= dri3_get_pixmap_buffer(driDrawable
,
1207 front
= dri3_get_buffer(driDrawable
,
1215 dri3_free_buffers(driDrawable
, dri3_buffer_front
, loaderPrivate
);
1216 priv
->have_fake_front
= 0;
1219 if (buffer_mask
& __DRI_IMAGE_BUFFER_BACK
) {
1220 back
= dri3_get_buffer(driDrawable
,
1226 priv
->have_back
= 1;
1228 dri3_free_buffers(driDrawable
, dri3_buffer_back
, loaderPrivate
);
1229 priv
->have_back
= 0;
1233 buffers
->image_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
1234 buffers
->front
= front
->image
;
1235 priv
->have_fake_front
= !priv
->is_pixmap
;
1239 buffers
->image_mask
|= __DRI_IMAGE_BUFFER_BACK
;
1240 buffers
->back
= back
->image
;
1243 priv
->stamp
= stamp
;
1248 /* The image loader extension record for DRI3
1250 static const __DRIimageLoaderExtension imageLoaderExtension
= {
1251 {__DRI_IMAGE_LOADER
, __DRI_IMAGE_LOADER_VERSION
},
1252 .getBuffers
= dri3_get_buffers
,
1253 .flushFrontBuffer
= dri3_flush_front_buffer
,
1256 /** dri3_swap_buffers
1258 * Make the current back buffer visible using the present extension
1261 dri3_swap_buffers(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
1262 int64_t remainder
, Bool flush
)
1264 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
1265 struct dri3_screen
*psc
= (struct dri3_screen
*) priv
->base
.psc
;
1266 Display
*dpy
= priv
->base
.psc
->dpy
;
1267 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
1268 int buf_id
= DRI3_BACK_ID(priv
->cur_back
);
1271 unsigned flags
= __DRI2_FLUSH_DRAWABLE
;
1273 flags
|= __DRI2_FLUSH_CONTEXT
;
1274 dri3_flush(psc
, priv
, flags
, __DRI2_THROTTLE_SWAPBUFFER
);
1276 dri3_flush_present_events(priv
);
1278 if (priv
->buffers
[buf_id
] && !priv
->is_pixmap
) {
1279 dri3_fence_reset(c
, priv
->buffers
[buf_id
]);
1281 /* Compute when we want the frame shown by taking the last known successful
1282 * MSC and adding in a swap interval for each outstanding swap request
1284 ++priv
->present_request_serial
;
1285 if (target_msc
== 0)
1286 target_msc
= priv
->msc
+ priv
->swap_interval
* (priv
->present_request_serial
- priv
->present_event_serial
);
1288 priv
->buffers
[buf_id
]->busy
= 1;
1289 xcb_present_pixmap(c
,
1290 priv
->base
.xDrawable
,
1291 priv
->buffers
[buf_id
]->pixmap
,
1292 priv
->present_request_serial
,
1297 None
, /* target_crtc */
1299 priv
->buffers
[buf_id
]->sync_fence
,
1300 XCB_PRESENT_OPTION_NONE
,
1303 remainder
, 0, NULL
);
1306 /* If there's a fake front, then copy the source back buffer
1307 * to the fake front to keep it up to date. This needs
1308 * to reset the fence and make future users block until
1309 * the X server is done copying the bits
1311 if (priv
->have_fake_front
) {
1312 dri3_fence_reset(c
, priv
->buffers
[DRI3_FRONT_ID
]);
1314 priv
->buffers
[buf_id
]->pixmap
,
1315 priv
->buffers
[DRI3_FRONT_ID
]->pixmap
,
1316 dri3_drawable_gc(priv
),
1317 0, 0, 0, 0, priv
->width
, priv
->height
);
1318 dri3_fence_trigger(c
, priv
->buffers
[DRI3_FRONT_ID
]);
1330 * Wrapper around xcb_dri3_open
1333 dri3_open(Display
*dpy
,
1337 xcb_dri3_open_cookie_t cookie
;
1338 xcb_dri3_open_reply_t
*reply
;
1339 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
1340 xcb_generic_error_t
*error
;
1343 cookie
= xcb_dri3_open(c
,
1347 reply
= xcb_dri3_open_reply(c
, cookie
, &error
);
1351 if (reply
->nfd
!= 1) {
1356 fd
= xcb_dri3_open_reply_fds(c
, reply
)[0];
1357 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
1363 /** dri3_destroy_screen
1366 dri3_destroy_screen(struct glx_screen
*base
)
1368 struct dri3_screen
*psc
= (struct dri3_screen
*) base
;
1370 /* Free the direct rendering per screen data */
1371 (*psc
->core
->destroyScreen
) (psc
->driScreen
);
1372 driDestroyConfigs(psc
->driver_configs
);
1377 /** dri3_set_swap_interval
1379 * Record the application swap interval specification,
1382 dri3_set_swap_interval(__GLXDRIdrawable
*pdraw
, int interval
)
1384 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
1385 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
1386 struct dri3_screen
*psc
= (struct dri3_screen
*) priv
->base
.psc
;
1389 psc
->config
->configQueryi(psc
->driScreen
,
1390 "vblank_mode", &vblank_mode
);
1392 switch (vblank_mode
) {
1393 case DRI_CONF_VBLANK_NEVER
:
1395 return GLX_BAD_VALUE
;
1397 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1399 return GLX_BAD_VALUE
;
1405 priv
->swap_interval
= interval
;
1410 /** dri3_get_swap_interval
1412 * Return the stored swap interval
1415 dri3_get_swap_interval(__GLXDRIdrawable
*pdraw
)
1417 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
1419 return priv
->swap_interval
;
1423 dri3_bind_tex_image(Display
* dpy
,
1424 GLXDrawable drawable
,
1425 int buffer
, const int *attrib_list
)
1427 struct glx_context
*gc
= __glXGetCurrentContext();
1428 struct dri3_context
*pcp
= (struct dri3_context
*) gc
;
1429 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
1430 struct dri3_drawable
*pdraw
= (struct dri3_drawable
*) base
;
1431 struct dri3_screen
*psc
;
1433 if (pdraw
!= NULL
) {
1434 psc
= (struct dri3_screen
*) base
->psc
;
1436 (*psc
->f
->invalidate
)(pdraw
->driDrawable
);
1440 (*psc
->texBuffer
->setTexBuffer2
) (pcp
->driContext
,
1441 pdraw
->base
.textureTarget
,
1442 pdraw
->base
.textureFormat
,
1443 pdraw
->driDrawable
);
1448 dri3_release_tex_image(Display
* dpy
, GLXDrawable drawable
, int buffer
)
1450 struct glx_context
*gc
= __glXGetCurrentContext();
1451 struct dri3_context
*pcp
= (struct dri3_context
*) gc
;
1452 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
1453 struct dri3_drawable
*pdraw
= (struct dri3_drawable
*) base
;
1454 struct dri3_screen
*psc
;
1456 if (pdraw
!= NULL
) {
1457 psc
= (struct dri3_screen
*) base
->psc
;
1459 if (psc
->texBuffer
->releaseTexBuffer
)
1460 (*psc
->texBuffer
->releaseTexBuffer
) (pcp
->driContext
,
1461 pdraw
->base
.textureTarget
,
1462 pdraw
->driDrawable
);
1466 static const struct glx_context_vtable dri3_context_vtable
= {
1467 dri3_destroy_context
,
1469 dri3_unbind_context
,
1473 dri3_bind_tex_image
,
1474 dri3_release_tex_image
,
1475 NULL
, /* get_proc_address */
1478 /** dri3_bind_extensions
1480 * Enable all of the extensions supported on DRI3
1483 dri3_bind_extensions(struct dri3_screen
*psc
, struct glx_display
* priv
,
1484 const char *driverName
)
1486 const __DRIextension
**extensions
;
1490 extensions
= psc
->core
->getExtensions(psc
->driScreen
);
1492 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_video_sync");
1493 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_swap_control");
1494 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_swap_control");
1495 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_make_current_read");
1498 * GLX_INTEL_swap_event is broken on the server side, where it's
1499 * currently unconditionally enabled. This completely breaks
1500 * systems running on drivers which don't support that extension.
1501 * There's no way to test for its presence on this side, so instead
1502 * of disabling it unconditionally, just disable it for drivers
1503 * which are known to not support it, or for DDX drivers supporting
1504 * only an older (pre-ScheduleSwap) version of DRI2.
1506 * This is a hack which is required until:
1507 * http://lists.x.org/archives/xorg-devel/2013-February/035449.html
1508 * is merged and updated xserver makes it's way into distros:
1510 // if (pdp->swapAvailable && strcmp(driverName, "vmwgfx") != 0) {
1511 // __glXEnableDirectExtension(&psc->base, "GLX_INTEL_swap_event");
1514 mask
= psc
->image_driver
->getAPIMask(psc
->driScreen
);
1516 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context");
1517 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context_profile");
1519 if ((mask
& (1 << __DRI_API_GLES2
)) != 0)
1520 __glXEnableDirectExtension(&psc
->base
,
1521 "GLX_EXT_create_context_es2_profile");
1523 for (i
= 0; extensions
[i
]; i
++) {
1524 if ((strcmp(extensions
[i
]->name
, __DRI_TEX_BUFFER
) == 0)) {
1525 psc
->texBuffer
= (__DRItexBufferExtension
*) extensions
[i
];
1526 __glXEnableDirectExtension(&psc
->base
, "GLX_EXT_texture_from_pixmap");
1529 if ((strcmp(extensions
[i
]->name
, __DRI2_FLUSH
) == 0)) {
1530 psc
->f
= (__DRI2flushExtension
*) extensions
[i
];
1531 /* internal driver extension, no GL extension exposed */
1534 if ((strcmp(extensions
[i
]->name
, __DRI2_CONFIG_QUERY
) == 0))
1535 psc
->config
= (__DRI2configQueryExtension
*) extensions
[i
];
1537 if (strcmp(extensions
[i
]->name
, __DRI2_ROBUSTNESS
) == 0)
1538 __glXEnableDirectExtension(&psc
->base
,
1539 "GLX_ARB_create_context_robustness");
1543 static const struct glx_screen_vtable dri3_screen_vtable
= {
1544 dri3_create_context
,
1545 dri3_create_context_attribs
1548 /** dri3_create_screen
1550 * Initialize DRI3 on the specified screen.
1552 * Opens the DRI device, locates the appropriate DRI driver
1555 * Checks to see if the driver supports the necessary extensions
1557 * Initializes the driver for the screen and sets up our structures
1560 static struct glx_screen
*
1561 dri3_create_screen(int screen
, struct glx_display
* priv
)
1563 xcb_connection_t
*c
= XGetXCBConnection(priv
->dpy
);
1564 const __DRIconfig
**driver_configs
;
1565 const __DRIextension
**extensions
;
1566 const struct dri3_display
*const pdp
= (struct dri3_display
*)
1568 struct dri3_screen
*psc
;
1569 __GLXDRIscreen
*psp
;
1570 struct glx_config
*configs
= NULL
, *visuals
= NULL
;
1571 char *driverName
, *deviceName
;
1574 psc
= calloc(1, sizeof *psc
);
1580 if (!glx_screen_init(&psc
->base
, screen
, priv
)) {
1585 psc
->fd
= dri3_open(priv
->dpy
, RootWindow(priv
->dpy
, screen
), None
);
1587 int conn_error
= xcb_connection_has_error(c
);
1589 glx_screen_cleanup(&psc
->base
);
1591 InfoMessageF("screen %d does not appear to be DRI3 capable\n", screen
);
1594 ErrorMessageF("Connection closed during DRI3 initialization failure");
1600 driverName
= dri3_get_driver_for_fd(psc
->fd
);
1602 ErrorMessageF("No driver found\n");
1606 psc
->driver
= driOpenDriver(driverName
);
1607 if (psc
->driver
== NULL
) {
1608 ErrorMessageF("driver pointer missing\n");
1612 extensions
= driGetDriverExtensions(psc
->driver
, driverName
);
1613 if (extensions
== NULL
)
1616 for (i
= 0; extensions
[i
]; i
++) {
1617 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
1618 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
1619 if (strcmp(extensions
[i
]->name
, __DRI_IMAGE_DRIVER
) == 0)
1620 psc
->image_driver
= (__DRIimageDriverExtension
*) extensions
[i
];
1624 if (psc
->core
== NULL
) {
1625 ErrorMessageF("core dri driver extension not found\n");
1629 if (psc
->image_driver
== NULL
) {
1630 ErrorMessageF("image driver extension not found\n");
1635 psc
->image_driver
->createNewScreen2(screen
, psc
->fd
,
1636 (const __DRIextension
**)
1637 &pdp
->loader_extensions
[0],
1639 &driver_configs
, psc
);
1641 if (psc
->driScreen
== NULL
) {
1642 ErrorMessageF("failed to create dri screen\n");
1646 extensions
= (*psc
->core
->getExtensions
)(psc
->driScreen
);
1648 for (i
= 0; extensions
[i
]; i
++) {
1649 if (strcmp(extensions
[i
]->name
, __DRI_IMAGE
) == 0)
1650 psc
->image
= (__DRIimageExtension
*) extensions
[i
];
1653 if (psc
->image
== NULL
) {
1654 ErrorMessageF("image extension not found\n");
1658 dri3_bind_extensions(psc
, priv
, driverName
);
1660 if (!psc
->f
|| psc
->f
->base
.version
< 4) {
1661 ErrorMessageF("Version 4 or later of flush extension not found\n");
1665 if (!psc
->texBuffer
|| psc
->texBuffer
->base
.version
< 2 ||
1666 !psc
->texBuffer
->setTexBuffer2
)
1668 ErrorMessageF("Version 2 or later of texBuffer extension not found\n");
1672 configs
= driConvertConfigs(psc
->core
, psc
->base
.configs
, driver_configs
);
1673 visuals
= driConvertConfigs(psc
->core
, psc
->base
.visuals
, driver_configs
);
1675 if (!configs
|| !visuals
)
1678 glx_config_destroy_list(psc
->base
.configs
);
1679 psc
->base
.configs
= configs
;
1680 glx_config_destroy_list(psc
->base
.visuals
);
1681 psc
->base
.visuals
= visuals
;
1683 psc
->driver_configs
= driver_configs
;
1685 psc
->base
.vtable
= &dri3_screen_vtable
;
1687 psc
->base
.driScreen
= psp
;
1688 psp
->destroyScreen
= dri3_destroy_screen
;
1689 psp
->createDrawable
= dri3_create_drawable
;
1690 psp
->swapBuffers
= dri3_swap_buffers
;
1692 psp
->getDrawableMSC
= dri3_drawable_get_msc
;
1693 psp
->waitForMSC
= dri3_wait_for_msc
;
1694 psp
->waitForSBC
= dri3_wait_for_sbc
;
1695 psp
->setSwapInterval
= dri3_set_swap_interval
;
1696 psp
->getSwapInterval
= dri3_get_swap_interval
;
1697 __glXEnableDirectExtension(&psc
->base
, "GLX_OML_sync_control");
1699 psp
->copySubBuffer
= dri3_copy_sub_buffer
;
1700 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_copy_sub_buffer");
1708 CriticalErrorMessageF("failed to load driver: %s\n", driverName
);
1711 glx_config_destroy_list(configs
);
1713 glx_config_destroy_list(visuals
);
1715 psc
->core
->destroyScreen(psc
->driScreen
);
1716 psc
->driScreen
= NULL
;
1720 dlclose(psc
->driver
);
1724 glx_screen_cleanup(&psc
->base
);
1730 /** dri_destroy_display
1732 * Called from __glXFreeDisplayPrivate.
1735 dri3_destroy_display(__GLXDRIdisplay
* dpy
)
1740 /** dri3_create_display
1742 * Allocate, initialize and return a __DRIdisplayPrivate object.
1743 * This is called from __glXInitialize() when we are given a new
1744 * display pointer. This is public to that function, but hidden from
1747 _X_HIDDEN __GLXDRIdisplay
*
1748 dri3_create_display(Display
* dpy
)
1750 struct dri3_display
*pdp
;
1752 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
1753 xcb_dri3_query_version_cookie_t dri3_cookie
;
1754 xcb_dri3_query_version_reply_t
*dri3_reply
;
1755 xcb_present_query_version_cookie_t present_cookie
;
1756 xcb_present_query_version_reply_t
*present_reply
;
1757 xcb_generic_error_t
*error
;
1758 const xcb_query_extension_reply_t
*extension
;
1760 xcb_prefetch_extension_data(c
, &xcb_dri3_id
);
1761 xcb_prefetch_extension_data(c
, &xcb_present_id
);
1763 extension
= xcb_get_extension_data(c
, &xcb_dri3_id
);
1764 if (!(extension
&& extension
->present
))
1767 extension
= xcb_get_extension_data(c
, &xcb_present_id
);
1768 if (!(extension
&& extension
->present
))
1771 dri3_cookie
= xcb_dri3_query_version(c
,
1772 XCB_DRI3_MAJOR_VERSION
,
1773 XCB_DRI3_MINOR_VERSION
);
1776 present_cookie
= xcb_present_query_version(c
,
1777 XCB_PRESENT_MAJOR_VERSION
,
1778 XCB_PRESENT_MINOR_VERSION
);
1780 pdp
= malloc(sizeof *pdp
);
1784 dri3_reply
= xcb_dri3_query_version_reply(c
, dri3_cookie
, &error
);
1790 pdp
->dri3Major
= dri3_reply
->major_version
;
1791 pdp
->dri3Minor
= dri3_reply
->minor_version
;
1794 present_reply
= xcb_present_query_version_reply(c
, present_cookie
, &error
);
1795 if (!present_reply
) {
1799 pdp
->presentMajor
= present_reply
->major_version
;
1800 pdp
->presentMinor
= present_reply
->minor_version
;
1802 pdp
->base
.destroyDisplay
= dri3_destroy_display
;
1803 pdp
->base
.createScreen
= dri3_create_screen
;
1807 pdp
->loader_extensions
[i
++] = &imageLoaderExtension
.base
;
1809 pdp
->loader_extensions
[i
++] = &systemTimeExtension
.base
;
1811 pdp
->loader_extensions
[i
++] = NULL
;
1819 #endif /* GLX_DIRECT_RENDERING */