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>
68 #include "glxclient.h"
72 #include <sys/types.h>
76 #include "dri_common.h"
77 #include "dri3_priv.h"
80 static const struct glx_context_vtable dri3_context_vtable
;
83 dri3_fence_reset(xcb_connection_t
*c
, struct dri3_buffer
*buffer
)
85 xshmfence_reset(buffer
->shm_fence
);
89 dri3_fence_set(struct dri3_buffer
*buffer
)
91 xshmfence_trigger(buffer
->shm_fence
);
95 dri3_fence_trigger(xcb_connection_t
*c
, struct dri3_buffer
*buffer
)
97 xcb_sync_trigger_fence(c
, buffer
->sync_fence
);
101 dri3_fence_await(xcb_connection_t
*c
, struct dri3_buffer
*buffer
)
104 xshmfence_await(buffer
->shm_fence
);
108 dri3_fence_triggered(struct dri3_buffer
*buffer
)
110 return xshmfence_query(buffer
->shm_fence
);
114 dri3_destroy_context(struct glx_context
*context
)
116 struct dri3_context
*pcp
= (struct dri3_context
*) context
;
117 struct dri3_screen
*psc
= (struct dri3_screen
*) context
->psc
;
119 driReleaseDrawables(&pcp
->base
);
121 free((char *) context
->extensions
);
123 (*psc
->core
->destroyContext
) (pcp
->driContext
);
129 dri3_bind_context(struct glx_context
*context
, struct glx_context
*old
,
130 GLXDrawable draw
, GLXDrawable read
)
132 struct dri3_context
*pcp
= (struct dri3_context
*) context
;
133 struct dri3_screen
*psc
= (struct dri3_screen
*) pcp
->base
.psc
;
134 struct dri3_drawable
*pdraw
, *pread
;
136 pdraw
= (struct dri3_drawable
*) driFetchDrawable(context
, draw
);
137 pread
= (struct dri3_drawable
*) driFetchDrawable(context
, read
);
139 driReleaseDrawables(&pcp
->base
);
141 if (pdraw
== NULL
|| pread
== NULL
)
142 return GLXBadDrawable
;
144 if (!(*psc
->core
->bindContext
) (pcp
->driContext
,
145 pdraw
->driDrawable
, pread
->driDrawable
))
146 return GLXBadContext
;
152 dri3_unbind_context(struct glx_context
*context
, struct glx_context
*new)
154 struct dri3_context
*pcp
= (struct dri3_context
*) context
;
155 struct dri3_screen
*psc
= (struct dri3_screen
*) pcp
->base
.psc
;
157 (*psc
->core
->unbindContext
) (pcp
->driContext
);
160 static struct glx_context
*
161 dri3_create_context_attribs(struct glx_screen
*base
,
162 struct glx_config
*config_base
,
163 struct glx_context
*shareList
,
164 unsigned num_attribs
,
165 const uint32_t *attribs
,
168 struct dri3_context
*pcp
= NULL
;
169 struct dri3_context
*pcp_shared
= NULL
;
170 struct dri3_screen
*psc
= (struct dri3_screen
*) base
;
171 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
172 __DRIcontext
*shared
= NULL
;
174 uint32_t minor_ver
= 1;
175 uint32_t major_ver
= 2;
178 int reset
= __DRI_CTX_RESET_NO_NOTIFICATION
;
179 uint32_t ctx_attribs
[2 * 5];
180 unsigned num_ctx_attribs
= 0;
181 uint32_t render_type
;
183 /* Remap the GLX tokens to DRI2 tokens.
185 if (!dri2_convert_glx_attribs(num_attribs
, attribs
,
186 &major_ver
, &minor_ver
,
187 &render_type
, &flags
, &api
,
191 /* Check the renderType value */
192 if (!validate_renderType_against_config(config_base
, render_type
))
196 pcp_shared
= (struct dri3_context
*) shareList
;
197 shared
= pcp_shared
->driContext
;
200 pcp
= calloc(1, sizeof *pcp
);
202 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
206 if (!glx_context_init(&pcp
->base
, &psc
->base
, &config
->base
))
209 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
210 ctx_attribs
[num_ctx_attribs
++] = major_ver
;
211 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
212 ctx_attribs
[num_ctx_attribs
++] = minor_ver
;
214 /* Only send a value when the non-default value is requested. By doing
215 * this we don't have to check the driver's DRI3 version before sending the
218 if (reset
!= __DRI_CTX_RESET_NO_NOTIFICATION
) {
219 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
220 ctx_attribs
[num_ctx_attribs
++] = reset
;
224 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_FLAGS
;
226 /* The current __DRI_CTX_FLAG_* values are identical to the
227 * GLX_CONTEXT_*_BIT values.
229 ctx_attribs
[num_ctx_attribs
++] = flags
;
233 (*psc
->image_driver
->createContextAttribs
) (psc
->driScreen
,
242 if (pcp
->driContext
== NULL
)
245 pcp
->base
.vtable
= &dri3_context_vtable
;
255 static struct glx_context
*
256 dri3_create_context(struct glx_screen
*base
,
257 struct glx_config
*config_base
,
258 struct glx_context
*shareList
, int renderType
)
262 return dri3_create_context_attribs(base
, config_base
, shareList
,
267 dri3_free_render_buffer(struct dri3_drawable
*pdraw
, struct dri3_buffer
*buffer
);
270 dri3_update_num_back(struct dri3_drawable
*priv
)
275 if (priv
->swap_interval
== 0)
280 dri3_destroy_drawable(__GLXDRIdrawable
*base
)
282 struct dri3_screen
*psc
= (struct dri3_screen
*) base
->psc
;
283 struct dri3_drawable
*pdraw
= (struct dri3_drawable
*) base
;
284 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->base
.psc
->dpy
);
287 (*psc
->core
->destroyDrawable
) (pdraw
->driDrawable
);
289 for (i
= 0; i
< DRI3_NUM_BUFFERS
; i
++) {
290 if (pdraw
->buffers
[i
])
291 dri3_free_render_buffer(pdraw
, pdraw
->buffers
[i
]);
294 if (pdraw
->special_event
)
295 xcb_unregister_for_special_event(c
, pdraw
->special_event
);
299 static __GLXDRIdrawable
*
300 dri3_create_drawable(struct glx_screen
*base
, XID xDrawable
,
301 GLXDrawable drawable
, struct glx_config
*config_base
)
303 struct dri3_drawable
*pdraw
;
304 struct dri3_screen
*psc
= (struct dri3_screen
*) base
;
305 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
306 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
308 pdraw
= calloc(1, sizeof(*pdraw
));
312 pdraw
->base
.destroyDrawable
= dri3_destroy_drawable
;
313 pdraw
->base
.xDrawable
= xDrawable
;
314 pdraw
->base
.drawable
= drawable
;
315 pdraw
->base
.psc
= &psc
->base
;
316 pdraw
->swap_interval
= 1; /* default may be overridden below */
317 pdraw
->have_back
= 0;
318 pdraw
->have_fake_front
= 0;
321 psc
->config
->configQueryi(psc
->driScreen
,
322 "vblank_mode", &vblank_mode
);
324 switch (vblank_mode
) {
325 case DRI_CONF_VBLANK_NEVER
:
326 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
327 pdraw
->swap_interval
= 0;
329 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
330 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
332 pdraw
->swap_interval
= 1;
336 dri3_update_num_back(pdraw
);
338 (void) __glXInitialize(psc
->base
.dpy
);
340 /* Create a new drawable */
342 (*psc
->image_driver
->createNewDrawable
) (psc
->driScreen
,
343 config
->driConfig
, pdraw
);
345 if (!pdraw
->driDrawable
) {
351 * Make sure server has the same swap interval we do for the new
354 if (psc
->vtable
.setSwapInterval
)
355 psc
->vtable
.setSwapInterval(&pdraw
->base
, pdraw
->swap_interval
);
361 * Process one Present event
364 dri3_handle_present_event(struct dri3_drawable
*priv
, xcb_present_generic_event_t
*ge
)
366 switch (ge
->evtype
) {
367 case XCB_PRESENT_CONFIGURE_NOTIFY
: {
368 xcb_present_configure_notify_event_t
*ce
= (void *) ge
;
370 priv
->width
= ce
->width
;
371 priv
->height
= ce
->height
;
374 case XCB_PRESENT_COMPLETE_NOTIFY
: {
375 xcb_present_complete_notify_event_t
*ce
= (void *) ge
;
377 /* Compute the processed SBC number from the received 32-bit serial number merged
378 * with the upper 32-bits of the sent 64-bit serial number while checking for
381 if (ce
->kind
== XCB_PRESENT_COMPLETE_KIND_PIXMAP
) {
382 priv
->recv_sbc
= (priv
->send_sbc
& 0xffffffff00000000LL
) | ce
->serial
;
383 if (priv
->recv_sbc
> priv
->send_sbc
)
384 priv
->recv_sbc
-= 0x100000000;
386 case XCB_PRESENT_COMPLETE_MODE_FLIP
:
387 priv
->flipping
= true;
389 case XCB_PRESENT_COMPLETE_MODE_COPY
:
390 priv
->flipping
= false;
393 dri3_update_num_back(priv
);
395 priv
->recv_msc_serial
= ce
->serial
;
401 case XCB_PRESENT_EVENT_IDLE_NOTIFY
: {
402 xcb_present_idle_notify_event_t
*ie
= (void *) ge
;
405 for (b
= 0; b
< sizeof (priv
->buffers
) / sizeof (priv
->buffers
[0]); b
++) {
406 struct dri3_buffer
*buf
= priv
->buffers
[b
];
408 if (buf
&& buf
->pixmap
== ie
->pixmap
) {
410 if (priv
->num_back
<= b
&& b
< DRI3_MAX_BACK
) {
411 dri3_free_render_buffer(priv
, buf
);
412 priv
->buffers
[b
] = NULL
;
424 dri3_wait_for_event(__GLXDRIdrawable
*pdraw
)
426 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->psc
->dpy
);
427 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
428 xcb_generic_event_t
*ev
;
429 xcb_present_generic_event_t
*ge
;
432 ev
= xcb_wait_for_special_event(c
, priv
->special_event
);
436 dri3_handle_present_event(priv
, ge
);
440 /** dri3_wait_for_msc
442 * Get the X server to send an event when the target msc/divisor/remainder is
446 dri3_wait_for_msc(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
447 int64_t remainder
, int64_t *ust
, int64_t *msc
, int64_t *sbc
)
449 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->psc
->dpy
);
450 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
453 /* Ask for the an event for the target MSC */
454 msc_serial
= ++priv
->send_msc_serial
;
455 xcb_present_notify_msc(c
,
456 priv
->base
.xDrawable
,
464 /* Wait for the event */
465 if (priv
->special_event
) {
466 while ((int32_t) (msc_serial
- priv
->recv_msc_serial
) > 0) {
467 if (!dri3_wait_for_event(pdraw
))
474 *sbc
= priv
->recv_sbc
;
479 /** dri3_drawable_get_msc
481 * Return the current UST/MSC/SBC triplet by asking the server
485 dri3_drawable_get_msc(struct glx_screen
*psc
, __GLXDRIdrawable
*pdraw
,
486 int64_t *ust
, int64_t *msc
, int64_t *sbc
)
488 return dri3_wait_for_msc(pdraw
, 0, 0, 0, ust
, msc
,sbc
);
491 /** dri3_wait_for_sbc
493 * Wait for the completed swap buffer count to reach the specified
494 * target. Presumably the application knows that this will be reached with
495 * outstanding complete events, or we're going to be here awhile.
498 dri3_wait_for_sbc(__GLXDRIdrawable
*pdraw
, int64_t target_sbc
, int64_t *ust
,
499 int64_t *msc
, int64_t *sbc
)
501 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
503 while (priv
->recv_sbc
< target_sbc
) {
504 if (!dri3_wait_for_event(pdraw
))
510 *sbc
= priv
->recv_sbc
;
515 * Asks the driver to flush any queued work necessary for serializing with the
516 * X command stream, and optionally the slightly more strict requirement of
517 * glFlush() equivalence (which would require flushing even if nothing had
518 * been drawn to a window system framebuffer, for example).
521 dri3_flush(struct dri3_screen
*psc
,
522 struct dri3_drawable
*draw
,
524 enum __DRI2throttleReason throttle_reason
)
526 struct glx_context
*gc
= __glXGetCurrentContext();
529 struct dri3_context
*dri3Ctx
= (struct dri3_context
*)gc
;
531 (*psc
->f
->flush_with_flags
)(dri3Ctx
->driContext
, draw
->driDrawable
, flags
, throttle_reason
);
535 static xcb_gcontext_t
536 dri3_drawable_gc(struct dri3_drawable
*priv
)
540 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
544 (priv
->gc
= xcb_generate_id(c
)),
545 priv
->base
.xDrawable
,
546 XCB_GC_GRAPHICS_EXPOSURES
,
552 static struct dri3_buffer
*
553 dri3_back_buffer(struct dri3_drawable
*priv
)
555 return priv
->buffers
[DRI3_BACK_ID(priv
->cur_back
)];
558 static struct dri3_buffer
*
559 dri3_fake_front_buffer(struct dri3_drawable
*priv
)
561 return priv
->buffers
[DRI3_FRONT_ID
];
565 dri3_copy_area (xcb_connection_t
*c
/**< */,
566 xcb_drawable_t src_drawable
/**< */,
567 xcb_drawable_t dst_drawable
/**< */,
568 xcb_gcontext_t gc
/**< */,
569 int16_t src_x
/**< */,
570 int16_t src_y
/**< */,
571 int16_t dst_x
/**< */,
572 int16_t dst_y
/**< */,
573 uint16_t width
/**< */,
574 uint16_t height
/**< */)
576 xcb_void_cookie_t cookie
;
578 cookie
= xcb_copy_area_checked(c
,
588 xcb_discard_reply(c
, cookie
.sequence
);
592 dri3_copy_sub_buffer(__GLXDRIdrawable
*pdraw
, int x
, int y
,
593 int width
, int height
,
596 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
597 struct dri3_screen
*psc
= (struct dri3_screen
*) pdraw
->psc
;
598 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
599 struct dri3_buffer
*back
= dri3_back_buffer(priv
);
603 /* Check we have the right attachments */
604 if (!priv
->have_back
|| priv
->is_pixmap
)
607 flags
= __DRI2_FLUSH_DRAWABLE
;
609 flags
|= __DRI2_FLUSH_CONTEXT
;
610 dri3_flush(psc
, priv
, flags
, __DRI2_THROTTLE_SWAPBUFFER
);
612 y
= priv
->height
- y
- height
;
614 dri3_fence_reset(c
, back
);
616 dri3_back_buffer(priv
)->pixmap
,
617 priv
->base
.xDrawable
,
618 dri3_drawable_gc(priv
),
619 x
, y
, x
, y
, width
, height
);
620 dri3_fence_trigger(c
, back
);
621 /* Refresh the fake front (if present) after we just damaged the real
624 if (priv
->have_fake_front
) {
625 dri3_fence_reset(c
, dri3_fake_front_buffer(priv
));
627 dri3_back_buffer(priv
)->pixmap
,
628 dri3_fake_front_buffer(priv
)->pixmap
,
629 dri3_drawable_gc(priv
),
630 x
, y
, x
, y
, width
, height
);
631 dri3_fence_trigger(c
, dri3_fake_front_buffer(priv
));
632 dri3_fence_await(c
, dri3_fake_front_buffer(priv
));
634 dri3_fence_await(c
, back
);
638 dri3_copy_drawable(struct dri3_drawable
*priv
, Drawable dest
, Drawable src
)
640 struct dri3_screen
*psc
= (struct dri3_screen
*) priv
->base
.psc
;
641 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
643 dri3_flush(psc
, priv
, __DRI2_FLUSH_DRAWABLE
, 0);
645 dri3_fence_reset(c
, dri3_fake_front_buffer(priv
));
648 dri3_drawable_gc(priv
),
649 0, 0, 0, 0, priv
->width
, priv
->height
);
650 dri3_fence_trigger(c
, dri3_fake_front_buffer(priv
));
651 dri3_fence_await(c
, dri3_fake_front_buffer(priv
));
655 dri3_wait_x(struct glx_context
*gc
)
657 struct dri3_drawable
*priv
= (struct dri3_drawable
*)
658 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
660 if (priv
== NULL
|| !priv
->have_fake_front
)
663 dri3_copy_drawable(priv
, dri3_fake_front_buffer(priv
)->pixmap
, priv
->base
.xDrawable
);
667 dri3_wait_gl(struct glx_context
*gc
)
669 struct dri3_drawable
*priv
= (struct dri3_drawable
*)
670 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
672 if (priv
== NULL
|| !priv
->have_fake_front
)
675 dri3_copy_drawable(priv
, priv
->base
.xDrawable
, dri3_fake_front_buffer(priv
)->pixmap
);
679 * Called by the driver when it needs to update the real front buffer with the
680 * contents of its fake front buffer.
683 dri3_flush_front_buffer(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
685 struct glx_context
*gc
;
686 struct dri3_drawable
*pdraw
= loaderPrivate
;
687 struct dri3_screen
*psc
;
692 if (!pdraw
->base
.psc
)
695 psc
= (struct dri3_screen
*) pdraw
->base
.psc
;
697 (void) __glXInitialize(psc
->base
.dpy
);
699 gc
= __glXGetCurrentContext();
701 dri3_flush(psc
, pdraw
, __DRI2_FLUSH_DRAWABLE
, __DRI2_THROTTLE_FLUSHFRONT
);
707 dri3_cpp_for_format(uint32_t format
) {
709 case __DRI_IMAGE_FORMAT_R8
:
711 case __DRI_IMAGE_FORMAT_RGB565
:
712 case __DRI_IMAGE_FORMAT_GR88
:
714 case __DRI_IMAGE_FORMAT_XRGB8888
:
715 case __DRI_IMAGE_FORMAT_ARGB8888
:
716 case __DRI_IMAGE_FORMAT_ABGR8888
:
717 case __DRI_IMAGE_FORMAT_XBGR8888
:
718 case __DRI_IMAGE_FORMAT_XRGB2101010
:
719 case __DRI_IMAGE_FORMAT_ARGB2101010
:
720 case __DRI_IMAGE_FORMAT_SARGB8
:
722 case __DRI_IMAGE_FORMAT_NONE
:
729 /** dri3_alloc_render_buffer
731 * Use the driver createImage function to construct a __DRIimage, then
732 * get a file descriptor for that and create an X pixmap from that
734 * Allocate an xshmfence for synchronization
736 static struct dri3_buffer
*
737 dri3_alloc_render_buffer(struct glx_screen
*glx_screen
, Drawable draw
,
738 unsigned int format
, int width
, int height
, int depth
)
740 struct dri3_screen
*psc
= (struct dri3_screen
*) glx_screen
;
741 Display
*dpy
= glx_screen
->dpy
;
742 struct dri3_buffer
*buffer
;
743 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
745 xcb_sync_fence_t sync_fence
;
746 struct xshmfence
*shm_fence
;
747 int buffer_fd
, fence_fd
;
750 /* Create an xshmfence object and
751 * prepare to send that to the X server
754 fence_fd
= xshmfence_alloc_shm();
757 shm_fence
= xshmfence_map_shm(fence_fd
);
758 if (shm_fence
== NULL
)
761 /* Allocate the image from the driver
763 buffer
= calloc(1, sizeof (struct dri3_buffer
));
767 buffer
->cpp
= dri3_cpp_for_format(format
);
771 buffer
->image
= (*psc
->image
->createImage
) (psc
->driScreen
,
774 __DRI_IMAGE_USE_SHARE
|__DRI_IMAGE_USE_SCANOUT
,
781 /* X wants the stride, so ask the image for it
783 if (!(*psc
->image
->queryImage
)(buffer
->image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
))
784 goto no_buffer_attrib
;
786 buffer
->pitch
= stride
;
788 if (!(*psc
->image
->queryImage
)(buffer
->image
, __DRI_IMAGE_ATTRIB_FD
, &buffer_fd
))
789 goto no_buffer_attrib
;
791 xcb_dri3_pixmap_from_buffer(c
,
792 (pixmap
= xcb_generate_id(c
)),
795 width
, height
, buffer
->pitch
,
796 depth
, buffer
->cpp
* 8,
799 xcb_dri3_fence_from_fd(c
,
801 (sync_fence
= xcb_generate_id(c
)),
805 buffer
->pixmap
= pixmap
;
806 buffer
->own_pixmap
= true;
807 buffer
->sync_fence
= sync_fence
;
808 buffer
->shm_fence
= shm_fence
;
809 buffer
->width
= width
;
810 buffer
->height
= height
;
812 /* Mark the buffer as idle
814 dri3_fence_set(buffer
);
819 (*psc
->image
->destroyImage
)(buffer
->image
);
823 xshmfence_unmap_shm(shm_fence
);
829 /** dri3_free_render_buffer
831 * Free everything associated with one render buffer including pixmap, fence
832 * stuff and the driver image
835 dri3_free_render_buffer(struct dri3_drawable
*pdraw
, struct dri3_buffer
*buffer
)
837 struct dri3_screen
*psc
= (struct dri3_screen
*) pdraw
->base
.psc
;
838 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->base
.psc
->dpy
);
840 if (buffer
->own_pixmap
)
841 xcb_free_pixmap(c
, buffer
->pixmap
);
842 xcb_sync_destroy_fence(c
, buffer
->sync_fence
);
843 xshmfence_unmap_shm(buffer
->shm_fence
);
844 (*psc
->image
->destroyImage
)(buffer
->image
);
849 /** dri3_flush_present_events
851 * Process any present events that have been received from the X server
854 dri3_flush_present_events(struct dri3_drawable
*priv
)
856 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
858 /* Check to see if any configuration changes have occurred
859 * since we were last invoked
861 if (priv
->special_event
) {
862 xcb_generic_event_t
*ev
;
864 while ((ev
= xcb_poll_for_special_event(c
, priv
->special_event
)) != NULL
) {
865 xcb_present_generic_event_t
*ge
= (void *) ev
;
866 dri3_handle_present_event(priv
, ge
);
871 /** dri3_update_drawable
873 * Called the first time we use the drawable and then
874 * after we receive present configure notify events to
875 * track the geometry of the drawable
878 dri3_update_drawable(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
880 struct dri3_drawable
*priv
= loaderPrivate
;
881 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
883 /* First time through, go get the current drawable geometry
885 if (priv
->width
== 0 || priv
->height
== 0 || priv
->depth
== 0) {
886 xcb_get_geometry_cookie_t geom_cookie
;
887 xcb_get_geometry_reply_t
*geom_reply
;
888 xcb_void_cookie_t cookie
;
889 xcb_generic_error_t
*error
;
891 /* Try to select for input on the window.
893 * If the drawable is a window, this will get our events
896 * Otherwise, we'll get a BadWindow error back from this request which
897 * will let us know that the drawable is a pixmap instead.
901 cookie
= xcb_present_select_input_checked(c
,
902 (priv
->eid
= xcb_generate_id(c
)),
903 priv
->base
.xDrawable
,
904 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY
|
905 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY
|
906 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY
);
908 /* Create an XCB event queue to hold present events outside of the usual
909 * application event queue
911 priv
->special_event
= xcb_register_for_special_xge(c
,
916 geom_cookie
= xcb_get_geometry(c
, priv
->base
.xDrawable
);
918 geom_reply
= xcb_get_geometry_reply(c
, geom_cookie
, NULL
);
923 priv
->width
= geom_reply
->width
;
924 priv
->height
= geom_reply
->height
;
925 priv
->depth
= geom_reply
->depth
;
926 priv
->is_pixmap
= false;
930 /* Check to see if our select input call failed. If it failed with a
931 * BadWindow error, then assume the drawable is a pixmap. Destroy the
932 * special event queue created above and mark the drawable as a pixmap
935 error
= xcb_request_check(c
, cookie
);
938 if (error
->error_code
!= BadWindow
) {
942 priv
->is_pixmap
= true;
943 xcb_unregister_for_special_event(c
, priv
->special_event
);
944 priv
->special_event
= NULL
;
947 dri3_flush_present_events(priv
);
951 /* the DRIimage createImage function takes __DRI_IMAGE_FORMAT codes, while
952 * the createImageFromFds call takes __DRI_IMAGE_FOURCC codes. To avoid
953 * complete confusion, just deal in __DRI_IMAGE_FORMAT codes for now and
954 * translate to __DRI_IMAGE_FOURCC codes in the call to createImageFromFds
957 image_format_to_fourcc(int format
)
960 /* Convert from __DRI_IMAGE_FORMAT to __DRI_IMAGE_FOURCC (sigh) */
962 case __DRI_IMAGE_FORMAT_SARGB8
: return __DRI_IMAGE_FOURCC_SARGB8888
;
963 case __DRI_IMAGE_FORMAT_RGB565
: return __DRI_IMAGE_FOURCC_RGB565
;
964 case __DRI_IMAGE_FORMAT_XRGB8888
: return __DRI_IMAGE_FOURCC_XRGB8888
;
965 case __DRI_IMAGE_FORMAT_ARGB8888
: return __DRI_IMAGE_FOURCC_ARGB8888
;
966 case __DRI_IMAGE_FORMAT_ABGR8888
: return __DRI_IMAGE_FOURCC_ABGR8888
;
967 case __DRI_IMAGE_FORMAT_XBGR8888
: return __DRI_IMAGE_FOURCC_XBGR8888
;
972 /** dri3_get_pixmap_buffer
974 * Get the DRM object for a pixmap from the X server and
975 * wrap that with a __DRIimage structure using createImageFromFds
977 static struct dri3_buffer
*
978 dri3_get_pixmap_buffer(__DRIdrawable
*driDrawable
,
980 enum dri3_buffer_type buffer_type
,
983 struct dri3_drawable
*pdraw
= loaderPrivate
;
984 int buf_id
= dri3_pixmap_buf_id(buffer_type
);
985 struct dri3_buffer
*buffer
= pdraw
->buffers
[buf_id
];
987 xcb_dri3_buffer_from_pixmap_cookie_t bp_cookie
;
988 xcb_dri3_buffer_from_pixmap_reply_t
*bp_reply
;
991 struct dri3_screen
*psc
;
993 xcb_sync_fence_t sync_fence
;
994 struct xshmfence
*shm_fence
;
996 __DRIimage
*image_planar
;
1002 pixmap
= pdraw
->base
.xDrawable
;
1003 psc
= (struct dri3_screen
*) pdraw
->base
.psc
;
1004 dpy
= psc
->base
.dpy
;
1005 c
= XGetXCBConnection(dpy
);
1007 buffer
= calloc(1, sizeof (struct dri3_buffer
));
1011 fence_fd
= xshmfence_alloc_shm();
1014 shm_fence
= xshmfence_map_shm(fence_fd
);
1015 if (shm_fence
== NULL
) {
1020 xcb_dri3_fence_from_fd(c
,
1022 (sync_fence
= xcb_generate_id(c
)),
1026 /* Get an FD for the pixmap object
1028 bp_cookie
= xcb_dri3_buffer_from_pixmap(c
, pixmap
);
1029 bp_reply
= xcb_dri3_buffer_from_pixmap_reply(c
, bp_cookie
, NULL
);
1032 fds
= xcb_dri3_buffer_from_pixmap_reply_fds(c
, bp_reply
);
1034 stride
= bp_reply
->stride
;
1037 /* createImageFromFds creates a wrapper __DRIimage structure which
1038 * can deal with multiple planes for things like Yuv images. So, once
1039 * we've gotten the planar wrapper, pull the single plane out of it and
1040 * discard the wrapper.
1042 image_planar
= (*psc
->image
->createImageFromFds
) (psc
->driScreen
,
1045 image_format_to_fourcc(format
),
1047 &stride
, &offset
, buffer
);
1052 buffer
->image
= (*psc
->image
->fromPlanar
)(image_planar
, 0, buffer
);
1054 (*psc
->image
->destroyImage
)(image_planar
);
1059 buffer
->pixmap
= pixmap
;
1060 buffer
->own_pixmap
= false;
1061 buffer
->width
= bp_reply
->width
;
1062 buffer
->height
= bp_reply
->height
;
1063 buffer
->buffer_type
= buffer_type
;
1064 buffer
->shm_fence
= shm_fence
;
1065 buffer
->sync_fence
= sync_fence
;
1067 pdraw
->buffers
[buf_id
] = buffer
;
1071 xcb_sync_destroy_fence(c
, sync_fence
);
1072 xshmfence_unmap_shm(shm_fence
);
1081 * Find an idle back buffer. If there isn't one, then
1082 * wait for a present idle notify event from the X server
1085 dri3_find_back(xcb_connection_t
*c
, struct dri3_drawable
*priv
)
1088 xcb_generic_event_t
*ev
;
1089 xcb_present_generic_event_t
*ge
;
1092 for (b
= 0; b
< priv
->num_back
; b
++) {
1093 int id
= DRI3_BACK_ID((b
+ priv
->cur_back
) % priv
->num_back
);
1094 struct dri3_buffer
*buffer
= priv
->buffers
[id
];
1096 if (!buffer
|| !buffer
->busy
) {
1097 priv
->cur_back
= id
;
1102 ev
= xcb_wait_for_special_event(c
, priv
->special_event
);
1106 dri3_handle_present_event(priv
, ge
);
1112 * Find a front or back buffer, allocating new ones as necessary
1114 static struct dri3_buffer
*
1115 dri3_get_buffer(__DRIdrawable
*driDrawable
,
1116 unsigned int format
,
1117 enum dri3_buffer_type buffer_type
,
1118 void *loaderPrivate
)
1120 struct dri3_drawable
*priv
= loaderPrivate
;
1121 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
1122 struct dri3_buffer
*buffer
;
1125 if (buffer_type
== dri3_buffer_back
) {
1126 buf_id
= dri3_find_back(c
, priv
);
1131 buf_id
= DRI3_FRONT_ID
;
1134 buffer
= priv
->buffers
[buf_id
];
1136 /* Allocate a new buffer if there isn't an old one, or if that
1137 * old one is the wrong size
1139 if (!buffer
|| buffer
->width
!= priv
->width
|| buffer
->height
!= priv
->height
) {
1140 struct dri3_buffer
*new_buffer
;
1142 /* Allocate the new buffers
1144 new_buffer
= dri3_alloc_render_buffer(priv
->base
.psc
,
1145 priv
->base
.xDrawable
,
1146 format
, priv
->width
, priv
->height
, priv
->depth
);
1150 /* When resizing, copy the contents of the old buffer, waiting for that
1151 * copy to complete using our fences before proceeding
1153 switch (buffer_type
) {
1154 case dri3_buffer_back
:
1156 dri3_fence_reset(c
, new_buffer
);
1157 dri3_fence_await(c
, buffer
);
1161 dri3_drawable_gc(priv
),
1162 0, 0, 0, 0, priv
->width
, priv
->height
);
1163 dri3_fence_trigger(c
, new_buffer
);
1164 dri3_free_render_buffer(priv
, buffer
);
1167 case dri3_buffer_front
:
1168 dri3_fence_reset(c
, new_buffer
);
1170 priv
->base
.xDrawable
,
1172 dri3_drawable_gc(priv
),
1173 0, 0, 0, 0, priv
->width
, priv
->height
);
1174 dri3_fence_trigger(c
, new_buffer
);
1177 buffer
= new_buffer
;
1178 buffer
->buffer_type
= buffer_type
;
1179 priv
->buffers
[buf_id
] = buffer
;
1181 dri3_fence_await(c
, buffer
);
1183 /* Return the requested buffer */
1187 /** dri3_free_buffers
1189 * Free the front bufffer or all of the back buffers. Used
1190 * when the application changes which buffers it needs
1193 dri3_free_buffers(__DRIdrawable
*driDrawable
,
1194 enum dri3_buffer_type buffer_type
,
1195 void *loaderPrivate
)
1197 struct dri3_drawable
*priv
= loaderPrivate
;
1198 struct dri3_buffer
*buffer
;
1203 switch (buffer_type
) {
1204 case dri3_buffer_back
:
1205 first_id
= DRI3_BACK_ID(0);
1206 n_id
= DRI3_MAX_BACK
;
1208 case dri3_buffer_front
:
1209 first_id
= DRI3_FRONT_ID
;
1213 for (buf_id
= first_id
; buf_id
< first_id
+ n_id
; buf_id
++) {
1214 buffer
= priv
->buffers
[buf_id
];
1216 dri3_free_render_buffer(priv
, buffer
);
1217 priv
->buffers
[buf_id
] = NULL
;
1222 /** dri3_get_buffers
1224 * The published buffer allocation API.
1225 * Returns all of the necessary buffers, allocating
1229 dri3_get_buffers(__DRIdrawable
*driDrawable
,
1230 unsigned int format
,
1232 void *loaderPrivate
,
1233 uint32_t buffer_mask
,
1234 struct __DRIimageList
*buffers
)
1236 struct dri3_drawable
*priv
= loaderPrivate
;
1237 struct dri3_buffer
*front
, *back
;
1239 buffers
->image_mask
= 0;
1240 buffers
->front
= NULL
;
1241 buffers
->back
= NULL
;
1246 if (!dri3_update_drawable(driDrawable
, loaderPrivate
))
1249 /* pixmaps always have front buffers */
1250 if (priv
->is_pixmap
)
1251 buffer_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
1253 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
1254 if (priv
->is_pixmap
)
1255 front
= dri3_get_pixmap_buffer(driDrawable
,
1260 front
= dri3_get_buffer(driDrawable
,
1268 dri3_free_buffers(driDrawable
, dri3_buffer_front
, loaderPrivate
);
1269 priv
->have_fake_front
= 0;
1272 if (buffer_mask
& __DRI_IMAGE_BUFFER_BACK
) {
1273 back
= dri3_get_buffer(driDrawable
,
1279 priv
->have_back
= 1;
1281 dri3_free_buffers(driDrawable
, dri3_buffer_back
, loaderPrivate
);
1282 priv
->have_back
= 0;
1286 buffers
->image_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
1287 buffers
->front
= front
->image
;
1288 priv
->have_fake_front
= !priv
->is_pixmap
;
1292 buffers
->image_mask
|= __DRI_IMAGE_BUFFER_BACK
;
1293 buffers
->back
= back
->image
;
1296 priv
->stamp
= stamp
;
1301 /* The image loader extension record for DRI3
1303 static const __DRIimageLoaderExtension imageLoaderExtension
= {
1304 .base
= { __DRI_IMAGE_LOADER
, 1 },
1306 .getBuffers
= dri3_get_buffers
,
1307 .flushFrontBuffer
= dri3_flush_front_buffer
,
1310 /** dri3_swap_buffers
1312 * Make the current back buffer visible using the present extension
1315 dri3_swap_buffers(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
1316 int64_t remainder
, Bool flush
)
1318 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
1319 struct dri3_screen
*psc
= (struct dri3_screen
*) priv
->base
.psc
;
1320 Display
*dpy
= priv
->base
.psc
->dpy
;
1321 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
1322 int buf_id
= DRI3_BACK_ID(priv
->cur_back
);
1325 unsigned flags
= __DRI2_FLUSH_DRAWABLE
;
1327 flags
|= __DRI2_FLUSH_CONTEXT
;
1328 dri3_flush(psc
, priv
, flags
, __DRI2_THROTTLE_SWAPBUFFER
);
1330 dri3_flush_present_events(priv
);
1332 if (priv
->buffers
[buf_id
] && !priv
->is_pixmap
) {
1333 dri3_fence_reset(c
, priv
->buffers
[buf_id
]);
1335 /* Compute when we want the frame shown by taking the last known successful
1336 * MSC and adding in a swap interval for each outstanding swap request
1339 if (target_msc
== 0)
1340 target_msc
= priv
->msc
+ priv
->swap_interval
* (priv
->send_sbc
- priv
->recv_sbc
);
1342 priv
->buffers
[buf_id
]->busy
= 1;
1343 priv
->buffers
[buf_id
]->last_swap
= priv
->send_sbc
;
1344 xcb_present_pixmap(c
,
1345 priv
->base
.xDrawable
,
1346 priv
->buffers
[buf_id
]->pixmap
,
1347 (uint32_t) priv
->send_sbc
,
1352 None
, /* target_crtc */
1354 priv
->buffers
[buf_id
]->sync_fence
,
1355 XCB_PRESENT_OPTION_NONE
,
1358 remainder
, 0, NULL
);
1359 ret
= (int64_t) priv
->send_sbc
;
1361 /* If there's a fake front, then copy the source back buffer
1362 * to the fake front to keep it up to date. This needs
1363 * to reset the fence and make future users block until
1364 * the X server is done copying the bits
1366 if (priv
->have_fake_front
) {
1367 dri3_fence_reset(c
, priv
->buffers
[DRI3_FRONT_ID
]);
1369 priv
->buffers
[buf_id
]->pixmap
,
1370 priv
->buffers
[DRI3_FRONT_ID
]->pixmap
,
1371 dri3_drawable_gc(priv
),
1372 0, 0, 0, 0, priv
->width
, priv
->height
);
1373 dri3_fence_trigger(c
, priv
->buffers
[DRI3_FRONT_ID
]);
1384 dri3_get_buffer_age(__GLXDRIdrawable
*pdraw
)
1386 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->psc
->dpy
);
1387 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
1388 int back_id
= DRI3_BACK_ID(dri3_find_back(c
, priv
));
1390 if (back_id
< 0 || !priv
->buffers
[back_id
])
1393 if (priv
->buffers
[back_id
]->last_swap
!= 0)
1394 return priv
->send_sbc
- priv
->buffers
[back_id
]->last_swap
+ 1;
1401 * Wrapper around xcb_dri3_open
1404 dri3_open(Display
*dpy
,
1408 xcb_dri3_open_cookie_t cookie
;
1409 xcb_dri3_open_reply_t
*reply
;
1410 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
1413 cookie
= xcb_dri3_open(c
,
1417 reply
= xcb_dri3_open_reply(c
, cookie
, NULL
);
1421 if (reply
->nfd
!= 1) {
1426 fd
= xcb_dri3_open_reply_fds(c
, reply
)[0];
1427 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
1433 /** dri3_destroy_screen
1436 dri3_destroy_screen(struct glx_screen
*base
)
1438 struct dri3_screen
*psc
= (struct dri3_screen
*) base
;
1440 /* Free the direct rendering per screen data */
1441 (*psc
->core
->destroyScreen
) (psc
->driScreen
);
1442 driDestroyConfigs(psc
->driver_configs
);
1447 /** dri3_set_swap_interval
1449 * Record the application swap interval specification,
1452 dri3_set_swap_interval(__GLXDRIdrawable
*pdraw
, int interval
)
1454 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
1455 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
1456 struct dri3_screen
*psc
= (struct dri3_screen
*) priv
->base
.psc
;
1459 psc
->config
->configQueryi(psc
->driScreen
,
1460 "vblank_mode", &vblank_mode
);
1462 switch (vblank_mode
) {
1463 case DRI_CONF_VBLANK_NEVER
:
1465 return GLX_BAD_VALUE
;
1467 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1469 return GLX_BAD_VALUE
;
1475 priv
->swap_interval
= interval
;
1476 dri3_update_num_back(priv
);
1481 /** dri3_get_swap_interval
1483 * Return the stored swap interval
1486 dri3_get_swap_interval(__GLXDRIdrawable
*pdraw
)
1488 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
1490 return priv
->swap_interval
;
1494 dri3_bind_tex_image(Display
* dpy
,
1495 GLXDrawable drawable
,
1496 int buffer
, const int *attrib_list
)
1498 struct glx_context
*gc
= __glXGetCurrentContext();
1499 struct dri3_context
*pcp
= (struct dri3_context
*) gc
;
1500 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
1501 struct dri3_drawable
*pdraw
= (struct dri3_drawable
*) base
;
1502 struct dri3_screen
*psc
;
1504 if (pdraw
!= NULL
) {
1505 psc
= (struct dri3_screen
*) base
->psc
;
1507 (*psc
->f
->invalidate
)(pdraw
->driDrawable
);
1511 (*psc
->texBuffer
->setTexBuffer2
) (pcp
->driContext
,
1512 pdraw
->base
.textureTarget
,
1513 pdraw
->base
.textureFormat
,
1514 pdraw
->driDrawable
);
1519 dri3_release_tex_image(Display
* dpy
, GLXDrawable drawable
, int buffer
)
1521 struct glx_context
*gc
= __glXGetCurrentContext();
1522 struct dri3_context
*pcp
= (struct dri3_context
*) gc
;
1523 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
1524 struct dri3_drawable
*pdraw
= (struct dri3_drawable
*) base
;
1525 struct dri3_screen
*psc
;
1527 if (pdraw
!= NULL
) {
1528 psc
= (struct dri3_screen
*) base
->psc
;
1530 if (psc
->texBuffer
->releaseTexBuffer
)
1531 (*psc
->texBuffer
->releaseTexBuffer
) (pcp
->driContext
,
1532 pdraw
->base
.textureTarget
,
1533 pdraw
->driDrawable
);
1537 static const struct glx_context_vtable dri3_context_vtable
= {
1538 dri3_destroy_context
,
1540 dri3_unbind_context
,
1544 dri3_bind_tex_image
,
1545 dri3_release_tex_image
,
1546 NULL
, /* get_proc_address */
1549 /** dri3_bind_extensions
1551 * Enable all of the extensions supported on DRI3
1554 dri3_bind_extensions(struct dri3_screen
*psc
, struct glx_display
* priv
,
1555 const char *driverName
)
1557 const __DRIextension
**extensions
;
1561 extensions
= psc
->core
->getExtensions(psc
->driScreen
);
1563 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_video_sync");
1564 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_swap_control");
1565 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_swap_control");
1566 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_make_current_read");
1567 __glXEnableDirectExtension(&psc
->base
, "GLX_INTEL_swap_event");
1569 mask
= psc
->image_driver
->getAPIMask(psc
->driScreen
);
1571 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context");
1572 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context_profile");
1574 if ((mask
& (1 << __DRI_API_GLES2
)) != 0)
1575 __glXEnableDirectExtension(&psc
->base
,
1576 "GLX_EXT_create_context_es2_profile");
1578 for (i
= 0; extensions
[i
]; i
++) {
1579 if ((strcmp(extensions
[i
]->name
, __DRI_TEX_BUFFER
) == 0)) {
1580 psc
->texBuffer
= (__DRItexBufferExtension
*) extensions
[i
];
1581 __glXEnableDirectExtension(&psc
->base
, "GLX_EXT_texture_from_pixmap");
1584 if ((strcmp(extensions
[i
]->name
, __DRI2_FLUSH
) == 0)) {
1585 psc
->f
= (__DRI2flushExtension
*) extensions
[i
];
1586 /* internal driver extension, no GL extension exposed */
1589 if ((strcmp(extensions
[i
]->name
, __DRI2_CONFIG_QUERY
) == 0))
1590 psc
->config
= (__DRI2configQueryExtension
*) extensions
[i
];
1592 if (strcmp(extensions
[i
]->name
, __DRI2_ROBUSTNESS
) == 0)
1593 __glXEnableDirectExtension(&psc
->base
,
1594 "GLX_ARB_create_context_robustness");
1598 static const struct glx_screen_vtable dri3_screen_vtable
= {
1599 dri3_create_context
,
1600 dri3_create_context_attribs
1603 /** dri3_create_screen
1605 * Initialize DRI3 on the specified screen.
1607 * Opens the DRI device, locates the appropriate DRI driver
1610 * Checks to see if the driver supports the necessary extensions
1612 * Initializes the driver for the screen and sets up our structures
1615 static struct glx_screen
*
1616 dri3_create_screen(int screen
, struct glx_display
* priv
)
1618 xcb_connection_t
*c
= XGetXCBConnection(priv
->dpy
);
1619 const __DRIconfig
**driver_configs
;
1620 const __DRIextension
**extensions
;
1621 const struct dri3_display
*const pdp
= (struct dri3_display
*)
1623 struct dri3_screen
*psc
;
1624 __GLXDRIscreen
*psp
;
1625 struct glx_config
*configs
= NULL
, *visuals
= NULL
;
1626 char *driverName
, *deviceName
;
1629 psc
= calloc(1, sizeof *psc
);
1635 if (!glx_screen_init(&psc
->base
, screen
, priv
)) {
1640 psc
->fd
= dri3_open(priv
->dpy
, RootWindow(priv
->dpy
, screen
), None
);
1642 int conn_error
= xcb_connection_has_error(c
);
1644 glx_screen_cleanup(&psc
->base
);
1646 InfoMessageF("screen %d does not appear to be DRI3 capable\n", screen
);
1649 ErrorMessageF("Connection closed during DRI3 initialization failure");
1655 driverName
= loader_get_driver_for_fd(psc
->fd
, 0);
1657 ErrorMessageF("No driver found\n");
1661 psc
->driver
= driOpenDriver(driverName
);
1662 if (psc
->driver
== NULL
) {
1663 ErrorMessageF("driver pointer missing\n");
1667 extensions
= driGetDriverExtensions(psc
->driver
, driverName
);
1668 if (extensions
== NULL
)
1671 for (i
= 0; extensions
[i
]; i
++) {
1672 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
1673 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
1674 if (strcmp(extensions
[i
]->name
, __DRI_IMAGE_DRIVER
) == 0)
1675 psc
->image_driver
= (__DRIimageDriverExtension
*) extensions
[i
];
1679 if (psc
->core
== NULL
) {
1680 ErrorMessageF("core dri driver extension not found\n");
1684 if (psc
->image_driver
== NULL
) {
1685 ErrorMessageF("image driver extension not found\n");
1690 psc
->image_driver
->createNewScreen2(screen
, psc
->fd
,
1691 (const __DRIextension
**)
1692 &pdp
->loader_extensions
[0],
1694 &driver_configs
, psc
);
1696 if (psc
->driScreen
== NULL
) {
1697 ErrorMessageF("failed to create dri screen\n");
1701 extensions
= (*psc
->core
->getExtensions
)(psc
->driScreen
);
1703 for (i
= 0; extensions
[i
]; i
++) {
1704 if (strcmp(extensions
[i
]->name
, __DRI_IMAGE
) == 0)
1705 psc
->image
= (__DRIimageExtension
*) extensions
[i
];
1708 if (psc
->image
== NULL
) {
1709 ErrorMessageF("image extension not found\n");
1713 dri3_bind_extensions(psc
, priv
, driverName
);
1715 if (!psc
->f
|| psc
->f
->base
.version
< 4) {
1716 ErrorMessageF("Version 4 or later of flush extension not found\n");
1720 if (!psc
->texBuffer
|| psc
->texBuffer
->base
.version
< 2 ||
1721 !psc
->texBuffer
->setTexBuffer2
)
1723 ErrorMessageF("Version 2 or later of texBuffer extension not found\n");
1727 configs
= driConvertConfigs(psc
->core
, psc
->base
.configs
, driver_configs
);
1728 visuals
= driConvertConfigs(psc
->core
, psc
->base
.visuals
, driver_configs
);
1730 if (!configs
|| !visuals
)
1733 glx_config_destroy_list(psc
->base
.configs
);
1734 psc
->base
.configs
= configs
;
1735 glx_config_destroy_list(psc
->base
.visuals
);
1736 psc
->base
.visuals
= visuals
;
1738 psc
->driver_configs
= driver_configs
;
1740 psc
->base
.vtable
= &dri3_screen_vtable
;
1742 psc
->base
.driScreen
= psp
;
1743 psp
->destroyScreen
= dri3_destroy_screen
;
1744 psp
->createDrawable
= dri3_create_drawable
;
1745 psp
->swapBuffers
= dri3_swap_buffers
;
1747 psp
->getDrawableMSC
= dri3_drawable_get_msc
;
1748 psp
->waitForMSC
= dri3_wait_for_msc
;
1749 psp
->waitForSBC
= dri3_wait_for_sbc
;
1750 psp
->setSwapInterval
= dri3_set_swap_interval
;
1751 psp
->getSwapInterval
= dri3_get_swap_interval
;
1752 __glXEnableDirectExtension(&psc
->base
, "GLX_OML_sync_control");
1754 psp
->copySubBuffer
= dri3_copy_sub_buffer
;
1755 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_copy_sub_buffer");
1757 psp
->getBufferAge
= dri3_get_buffer_age
;
1758 __glXEnableDirectExtension(&psc
->base
, "GLX_EXT_buffer_age");
1766 CriticalErrorMessageF("failed to load driver: %s\n", driverName
);
1769 glx_config_destroy_list(configs
);
1771 glx_config_destroy_list(visuals
);
1773 psc
->core
->destroyScreen(psc
->driScreen
);
1774 psc
->driScreen
= NULL
;
1778 dlclose(psc
->driver
);
1782 glx_screen_cleanup(&psc
->base
);
1788 /** dri_destroy_display
1790 * Called from __glXFreeDisplayPrivate.
1793 dri3_destroy_display(__GLXDRIdisplay
* dpy
)
1798 /** dri3_create_display
1800 * Allocate, initialize and return a __DRIdisplayPrivate object.
1801 * This is called from __glXInitialize() when we are given a new
1802 * display pointer. This is public to that function, but hidden from
1805 _X_HIDDEN __GLXDRIdisplay
*
1806 dri3_create_display(Display
* dpy
)
1808 struct dri3_display
*pdp
;
1810 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
1811 xcb_dri3_query_version_cookie_t dri3_cookie
;
1812 xcb_dri3_query_version_reply_t
*dri3_reply
;
1813 xcb_present_query_version_cookie_t present_cookie
;
1814 xcb_present_query_version_reply_t
*present_reply
;
1815 xcb_generic_error_t
*error
;
1816 const xcb_query_extension_reply_t
*extension
;
1818 xcb_prefetch_extension_data(c
, &xcb_dri3_id
);
1819 xcb_prefetch_extension_data(c
, &xcb_present_id
);
1821 extension
= xcb_get_extension_data(c
, &xcb_dri3_id
);
1822 if (!(extension
&& extension
->present
))
1825 extension
= xcb_get_extension_data(c
, &xcb_present_id
);
1826 if (!(extension
&& extension
->present
))
1829 dri3_cookie
= xcb_dri3_query_version(c
,
1830 XCB_DRI3_MAJOR_VERSION
,
1831 XCB_DRI3_MINOR_VERSION
);
1834 present_cookie
= xcb_present_query_version(c
,
1835 XCB_PRESENT_MAJOR_VERSION
,
1836 XCB_PRESENT_MINOR_VERSION
);
1838 pdp
= malloc(sizeof *pdp
);
1842 dri3_reply
= xcb_dri3_query_version_reply(c
, dri3_cookie
, &error
);
1848 pdp
->dri3Major
= dri3_reply
->major_version
;
1849 pdp
->dri3Minor
= dri3_reply
->minor_version
;
1852 present_reply
= xcb_present_query_version_reply(c
, present_cookie
, &error
);
1853 if (!present_reply
) {
1857 pdp
->presentMajor
= present_reply
->major_version
;
1858 pdp
->presentMinor
= present_reply
->minor_version
;
1859 free(present_reply
);
1861 pdp
->base
.destroyDisplay
= dri3_destroy_display
;
1862 pdp
->base
.createScreen
= dri3_create_screen
;
1864 loader_set_logger(dri_message
);
1867 pdp
->loader_extensions
[i
++] = &imageLoaderExtension
.base
;
1869 pdp
->loader_extensions
[i
++] = &systemTimeExtension
.base
;
1871 pdp
->loader_extensions
[i
++] = NULL
;
1879 #endif /* GLX_DIRECT_RENDERING */