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"
81 static struct dri3_drawable
*
82 loader_drawable_to_dri3_drawable(struct loader_dri3_drawable
*draw
) {
83 size_t offset
= offsetof(struct dri3_drawable
, loader_drawable
);
84 return (struct dri3_drawable
*)(((void*) draw
) - offset
);
88 glx_dri3_get_swap_interval(struct loader_dri3_drawable
*draw
)
90 struct dri3_drawable
*priv
= loader_drawable_to_dri3_drawable(draw
);
92 return priv
->swap_interval
;
96 glx_dri3_clamp_swap_interval(struct loader_dri3_drawable
*draw
, int interval
)
102 glx_dri3_set_swap_interval(struct loader_dri3_drawable
*draw
, int interval
)
104 struct dri3_drawable
*priv
= loader_drawable_to_dri3_drawable(draw
);
106 priv
->swap_interval
= interval
;
110 glx_dri3_set_drawable_size(struct loader_dri3_drawable
*draw
,
111 int width
, int height
)
117 glx_dri3_in_current_context(struct loader_dri3_drawable
*draw
)
119 struct dri3_drawable
*priv
= loader_drawable_to_dri3_drawable(draw
);
120 struct dri3_context
*pcp
= (struct dri3_context
*) __glXGetCurrentContext();
121 struct dri3_screen
*psc
= (struct dri3_screen
*) priv
->base
.psc
;
123 return (&pcp
->base
!= &dummyContext
) && pcp
->base
.psc
== &psc
->base
;
126 static __DRIcontext
*
127 glx_dri3_get_dri_context(struct loader_dri3_drawable
*draw
)
129 struct glx_context
*gc
= __glXGetCurrentContext();
130 struct dri3_context
*dri3Ctx
= (struct dri3_context
*) gc
;
132 return (gc
!= &dummyContext
) ? dri3Ctx
->driContext
: NULL
;
136 glx_dri3_get_dri_screen(struct loader_dri3_drawable
*draw
)
138 struct glx_context
*gc
= __glXGetCurrentContext();
139 struct dri3_context
*pcp
= (struct dri3_context
*) gc
;
140 struct dri3_screen
*psc
= (struct dri3_screen
*) pcp
->base
.psc
;
142 return (gc
!= &dummyContext
&& psc
) ? psc
->driScreen
: NULL
;
146 glx_dri3_flush_drawable(struct loader_dri3_drawable
*draw
, unsigned flags
)
148 loader_dri3_flush(draw
, flags
, __DRI2_THROTTLE_SWAPBUFFER
);
152 glx_dri3_show_fps(struct loader_dri3_drawable
*draw
, uint64_t current_ust
)
154 struct dri3_drawable
*priv
= loader_drawable_to_dri3_drawable(draw
);
155 const uint64_t interval
=
156 ((struct dri3_screen
*) priv
->base
.psc
)->show_fps_interval
;
163 /* DRI3+Present together uses microseconds for UST. */
164 if (priv
->previous_ust
+ interval
* 1000000 <= current_ust
) {
165 if (priv
->previous_ust
) {
166 fprintf(stderr
, "libGL: FPS = %.1f\n",
167 ((uint64_t) priv
->frames
* 1000000) /
168 (double)(current_ust
- priv
->previous_ust
));
171 priv
->previous_ust
= current_ust
;
175 static const struct loader_dri3_vtable glx_dri3_vtable
= {
176 .get_swap_interval
= glx_dri3_get_swap_interval
,
177 .clamp_swap_interval
= glx_dri3_clamp_swap_interval
,
178 .set_swap_interval
= glx_dri3_set_swap_interval
,
179 .set_drawable_size
= glx_dri3_set_drawable_size
,
180 .in_current_context
= glx_dri3_in_current_context
,
181 .get_dri_context
= glx_dri3_get_dri_context
,
182 .get_dri_screen
= glx_dri3_get_dri_screen
,
183 .flush_drawable
= glx_dri3_flush_drawable
,
184 .show_fps
= glx_dri3_show_fps
,
188 static const struct glx_context_vtable dri3_context_vtable
;
191 dri3_destroy_context(struct glx_context
*context
)
193 struct dri3_context
*pcp
= (struct dri3_context
*) context
;
194 struct dri3_screen
*psc
= (struct dri3_screen
*) context
->psc
;
196 driReleaseDrawables(&pcp
->base
);
198 free((char *) context
->extensions
);
200 (*psc
->core
->destroyContext
) (pcp
->driContext
);
206 dri3_bind_context(struct glx_context
*context
, struct glx_context
*old
,
207 GLXDrawable draw
, GLXDrawable read
)
209 struct dri3_context
*pcp
= (struct dri3_context
*) context
;
210 struct dri3_screen
*psc
= (struct dri3_screen
*) pcp
->base
.psc
;
211 struct dri3_drawable
*pdraw
, *pread
;
213 pdraw
= (struct dri3_drawable
*) driFetchDrawable(context
, draw
);
214 pread
= (struct dri3_drawable
*) driFetchDrawable(context
, read
);
216 driReleaseDrawables(&pcp
->base
);
218 if (pdraw
== NULL
|| pread
== NULL
)
219 return GLXBadDrawable
;
221 if (!(*psc
->core
->bindContext
) (pcp
->driContext
,
222 pdraw
->loader_drawable
.dri_drawable
,
223 pread
->loader_drawable
.dri_drawable
))
224 return GLXBadContext
;
230 dri3_unbind_context(struct glx_context
*context
, struct glx_context
*new)
232 struct dri3_context
*pcp
= (struct dri3_context
*) context
;
233 struct dri3_screen
*psc
= (struct dri3_screen
*) pcp
->base
.psc
;
235 (*psc
->core
->unbindContext
) (pcp
->driContext
);
238 static struct glx_context
*
239 dri3_create_context_attribs(struct glx_screen
*base
,
240 struct glx_config
*config_base
,
241 struct glx_context
*shareList
,
242 unsigned num_attribs
,
243 const uint32_t *attribs
,
246 struct dri3_context
*pcp
= NULL
;
247 struct dri3_context
*pcp_shared
= NULL
;
248 struct dri3_screen
*psc
= (struct dri3_screen
*) base
;
249 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
250 __DRIcontext
*shared
= NULL
;
252 uint32_t minor_ver
= 1;
253 uint32_t major_ver
= 2;
256 int reset
= __DRI_CTX_RESET_NO_NOTIFICATION
;
257 uint32_t ctx_attribs
[2 * 5];
258 unsigned num_ctx_attribs
= 0;
259 uint32_t render_type
;
261 /* Remap the GLX tokens to DRI2 tokens.
263 if (!dri2_convert_glx_attribs(num_attribs
, attribs
,
264 &major_ver
, &minor_ver
,
265 &render_type
, &flags
, &api
,
269 /* Check the renderType value */
270 if (!validate_renderType_against_config(config_base
, render_type
))
274 pcp_shared
= (struct dri3_context
*) shareList
;
275 shared
= pcp_shared
->driContext
;
278 pcp
= calloc(1, sizeof *pcp
);
280 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
284 if (!glx_context_init(&pcp
->base
, &psc
->base
, &config
->base
))
287 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
288 ctx_attribs
[num_ctx_attribs
++] = major_ver
;
289 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
290 ctx_attribs
[num_ctx_attribs
++] = minor_ver
;
292 /* Only send a value when the non-default value is requested. By doing
293 * this we don't have to check the driver's DRI3 version before sending the
296 if (reset
!= __DRI_CTX_RESET_NO_NOTIFICATION
) {
297 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
298 ctx_attribs
[num_ctx_attribs
++] = reset
;
302 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_FLAGS
;
304 /* The current __DRI_CTX_FLAG_* values are identical to the
305 * GLX_CONTEXT_*_BIT values.
307 ctx_attribs
[num_ctx_attribs
++] = flags
;
311 (*psc
->image_driver
->createContextAttribs
) (psc
->driScreen
,
320 if (pcp
->driContext
== NULL
)
323 pcp
->base
.vtable
= &dri3_context_vtable
;
333 static struct glx_context
*
334 dri3_create_context(struct glx_screen
*base
,
335 struct glx_config
*config_base
,
336 struct glx_context
*shareList
, int renderType
)
340 return dri3_create_context_attribs(base
, config_base
, shareList
,
345 dri3_destroy_drawable(__GLXDRIdrawable
*base
)
347 struct dri3_drawable
*pdraw
= (struct dri3_drawable
*) base
;
349 loader_dri3_drawable_fini(&pdraw
->loader_drawable
);
354 static __GLXDRIdrawable
*
355 dri3_create_drawable(struct glx_screen
*base
, XID xDrawable
,
356 GLXDrawable drawable
, struct glx_config
*config_base
)
358 struct dri3_drawable
*pdraw
;
359 struct dri3_screen
*psc
= (struct dri3_screen
*) base
;
360 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
362 pdraw
= calloc(1, sizeof(*pdraw
));
366 pdraw
->base
.destroyDrawable
= dri3_destroy_drawable
;
367 pdraw
->base
.xDrawable
= xDrawable
;
368 pdraw
->base
.drawable
= drawable
;
369 pdraw
->base
.psc
= &psc
->base
;
371 (void) __glXInitialize(psc
->base
.dpy
);
373 if (loader_dri3_drawable_init(XGetXCBConnection(base
->dpy
),
374 xDrawable
, psc
->driScreen
,
375 psc
->is_different_gpu
, config
->driConfig
,
376 &psc
->loader_dri3_ext
, &glx_dri3_vtable
,
377 &pdraw
->loader_drawable
)) {
385 /** dri3_wait_for_msc
387 * Get the X server to send an event when the target msc/divisor/remainder is
391 dri3_wait_for_msc(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
392 int64_t remainder
, int64_t *ust
, int64_t *msc
, int64_t *sbc
)
394 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
396 loader_dri3_wait_for_msc(&priv
->loader_drawable
, target_msc
, divisor
,
397 remainder
, ust
, msc
, sbc
);
402 /** dri3_drawable_get_msc
404 * Return the current UST/MSC/SBC triplet by asking the server
408 dri3_drawable_get_msc(struct glx_screen
*psc
, __GLXDRIdrawable
*pdraw
,
409 int64_t *ust
, int64_t *msc
, int64_t *sbc
)
411 return dri3_wait_for_msc(pdraw
, 0, 0, 0, ust
, msc
,sbc
);
414 /** dri3_wait_for_sbc
416 * Wait for the completed swap buffer count to reach the specified
417 * target. Presumably the application knows that this will be reached with
418 * outstanding complete events, or we're going to be here awhile.
421 dri3_wait_for_sbc(__GLXDRIdrawable
*pdraw
, int64_t target_sbc
, int64_t *ust
,
422 int64_t *msc
, int64_t *sbc
)
424 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
426 return loader_dri3_wait_for_sbc(&priv
->loader_drawable
, target_sbc
,
431 dri3_copy_sub_buffer(__GLXDRIdrawable
*pdraw
, int x
, int y
,
432 int width
, int height
,
435 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
437 loader_dri3_copy_sub_buffer(&priv
->loader_drawable
, x
, y
,
438 width
, height
, flush
);
442 dri3_wait_x(struct glx_context
*gc
)
444 struct dri3_drawable
*priv
= (struct dri3_drawable
*)
445 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
448 loader_dri3_wait_x(&priv
->loader_drawable
);
452 dri3_wait_gl(struct glx_context
*gc
)
454 struct dri3_drawable
*priv
= (struct dri3_drawable
*)
455 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
458 loader_dri3_wait_gl(&priv
->loader_drawable
);
462 * Called by the driver when it needs to update the real front buffer with the
463 * contents of its fake front buffer.
466 dri3_flush_front_buffer(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
468 struct loader_dri3_drawable
*draw
= loaderPrivate
;
469 struct dri3_drawable
*pdraw
= loader_drawable_to_dri3_drawable(draw
);
470 struct dri3_screen
*psc
;
475 if (!pdraw
->base
.psc
)
478 psc
= (struct dri3_screen
*) pdraw
->base
.psc
;
480 (void) __glXInitialize(psc
->base
.dpy
);
482 loader_dri3_flush(draw
, __DRI2_FLUSH_DRAWABLE
, __DRI2_THROTTLE_FLUSHFRONT
);
484 loader_dri3_wait_gl(draw
);
487 /* The image loader extension record for DRI3
489 static const __DRIimageLoaderExtension imageLoaderExtension
= {
490 .base
= { __DRI_IMAGE_LOADER
, 1 },
492 .getBuffers
= loader_dri3_get_buffers
,
493 .flushFrontBuffer
= dri3_flush_front_buffer
,
496 const __DRIuseInvalidateExtension dri3UseInvalidate
= {
497 .base
= { __DRI_USE_INVALIDATE
, 1 }
500 static const __DRIextension
*loader_extensions
[] = {
501 &imageLoaderExtension
.base
,
502 &systemTimeExtension
.base
,
503 &dri3UseInvalidate
.base
,
507 /** dri3_swap_buffers
509 * Make the current back buffer visible using the present extension
512 dri3_swap_buffers(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
513 int64_t remainder
, Bool flush
)
515 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
516 unsigned flags
= __DRI2_FLUSH_DRAWABLE
;
519 flags
|= __DRI2_FLUSH_CONTEXT
;
521 return loader_dri3_swap_buffers_msc(&priv
->loader_drawable
,
522 target_msc
, divisor
, remainder
,
527 dri3_get_buffer_age(__GLXDRIdrawable
*pdraw
)
529 struct dri3_drawable
*priv
= (struct dri3_drawable
*)pdraw
;
531 return loader_dri3_query_buffer_age(&priv
->loader_drawable
);
534 /** dri3_destroy_screen
537 dri3_destroy_screen(struct glx_screen
*base
)
539 struct dri3_screen
*psc
= (struct dri3_screen
*) base
;
541 /* Free the direct rendering per screen data */
542 (*psc
->core
->destroyScreen
) (psc
->driScreen
);
543 driDestroyConfigs(psc
->driver_configs
);
548 /** dri3_set_swap_interval
550 * Record the application swap interval specification,
553 dri3_set_swap_interval(__GLXDRIdrawable
*pdraw
, int interval
)
555 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
556 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
557 struct dri3_screen
*psc
= (struct dri3_screen
*) priv
->base
.psc
;
560 psc
->config
->configQueryi(psc
->driScreen
,
561 "vblank_mode", &vblank_mode
);
563 switch (vblank_mode
) {
564 case DRI_CONF_VBLANK_NEVER
:
566 return GLX_BAD_VALUE
;
568 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
570 return GLX_BAD_VALUE
;
576 loader_dri3_set_swap_interval(&priv
->loader_drawable
, interval
);
581 /** dri3_get_swap_interval
583 * Return the stored swap interval
586 dri3_get_swap_interval(__GLXDRIdrawable
*pdraw
)
588 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
590 return priv
->swap_interval
;
594 dri3_bind_tex_image(Display
* dpy
,
595 GLXDrawable drawable
,
596 int buffer
, const int *attrib_list
)
598 struct glx_context
*gc
= __glXGetCurrentContext();
599 struct dri3_context
*pcp
= (struct dri3_context
*) gc
;
600 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
601 struct dri3_drawable
*pdraw
= (struct dri3_drawable
*) base
;
602 struct dri3_screen
*psc
;
605 psc
= (struct dri3_screen
*) base
->psc
;
607 (*psc
->f
->invalidate
)(pdraw
->loader_drawable
.dri_drawable
);
611 (*psc
->texBuffer
->setTexBuffer2
) (pcp
->driContext
,
612 pdraw
->base
.textureTarget
,
613 pdraw
->base
.textureFormat
,
614 pdraw
->loader_drawable
.dri_drawable
);
619 dri3_release_tex_image(Display
* dpy
, GLXDrawable drawable
, int buffer
)
621 struct glx_context
*gc
= __glXGetCurrentContext();
622 struct dri3_context
*pcp
= (struct dri3_context
*) gc
;
623 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
624 struct dri3_drawable
*pdraw
= (struct dri3_drawable
*) base
;
625 struct dri3_screen
*psc
;
628 psc
= (struct dri3_screen
*) base
->psc
;
630 if (psc
->texBuffer
->base
.version
>= 3 &&
631 psc
->texBuffer
->releaseTexBuffer
!= NULL
)
632 (*psc
->texBuffer
->releaseTexBuffer
) (pcp
->driContext
,
633 pdraw
->base
.textureTarget
,
634 pdraw
->loader_drawable
.dri_drawable
);
638 static const struct glx_context_vtable dri3_context_vtable
= {
639 .destroy
= dri3_destroy_context
,
640 .bind
= dri3_bind_context
,
641 .unbind
= dri3_unbind_context
,
642 .wait_gl
= dri3_wait_gl
,
643 .wait_x
= dri3_wait_x
,
644 .use_x_font
= DRI_glXUseXFont
,
645 .bind_tex_image
= dri3_bind_tex_image
,
646 .release_tex_image
= dri3_release_tex_image
,
647 .get_proc_address
= NULL
,
648 .interop_query_device_info
= dri3_interop_query_device_info
,
649 .interop_export_object
= dri3_interop_export_object
652 /** dri3_bind_extensions
654 * Enable all of the extensions supported on DRI3
657 dri3_bind_extensions(struct dri3_screen
*psc
, struct glx_display
* priv
,
658 const char *driverName
)
660 const __DRIextension
**extensions
;
664 extensions
= psc
->core
->getExtensions(psc
->driScreen
);
666 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_video_sync");
667 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_swap_control");
668 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_swap_control");
669 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_make_current_read");
670 __glXEnableDirectExtension(&psc
->base
, "GLX_INTEL_swap_event");
672 mask
= psc
->image_driver
->getAPIMask(psc
->driScreen
);
674 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context");
675 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context_profile");
677 if ((mask
& ((1 << __DRI_API_GLES
) |
678 (1 << __DRI_API_GLES2
) |
679 (1 << __DRI_API_GLES3
))) != 0) {
680 __glXEnableDirectExtension(&psc
->base
,
681 "GLX_EXT_create_context_es_profile");
682 __glXEnableDirectExtension(&psc
->base
,
683 "GLX_EXT_create_context_es2_profile");
686 for (i
= 0; extensions
[i
]; i
++) {
687 /* when on a different gpu than the server, the server pixmaps
688 * can have a tiling mode we can't read. Thus we can't create
689 * a texture from them.
691 if (!psc
->is_different_gpu
&&
692 (strcmp(extensions
[i
]->name
, __DRI_TEX_BUFFER
) == 0)) {
693 psc
->texBuffer
= (__DRItexBufferExtension
*) extensions
[i
];
694 __glXEnableDirectExtension(&psc
->base
, "GLX_EXT_texture_from_pixmap");
697 if ((strcmp(extensions
[i
]->name
, __DRI2_FLUSH
) == 0)) {
698 psc
->f
= (__DRI2flushExtension
*) extensions
[i
];
699 /* internal driver extension, no GL extension exposed */
702 if (strcmp(extensions
[i
]->name
, __DRI_IMAGE
) == 0)
703 psc
->image
= (__DRIimageExtension
*) extensions
[i
];
705 if ((strcmp(extensions
[i
]->name
, __DRI2_CONFIG_QUERY
) == 0))
706 psc
->config
= (__DRI2configQueryExtension
*) extensions
[i
];
708 if (strcmp(extensions
[i
]->name
, __DRI2_ROBUSTNESS
) == 0)
709 __glXEnableDirectExtension(&psc
->base
,
710 "GLX_ARB_create_context_robustness");
712 if (strcmp(extensions
[i
]->name
, __DRI2_RENDERER_QUERY
) == 0) {
713 psc
->rendererQuery
= (__DRI2rendererQueryExtension
*) extensions
[i
];
714 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_query_renderer");
717 if (strcmp(extensions
[i
]->name
, __DRI2_INTEROP
) == 0)
718 psc
->interop
= (__DRI2interopExtension
*)extensions
[i
];
722 static const struct glx_screen_vtable dri3_screen_vtable
= {
723 .create_context
= dri3_create_context
,
724 .create_context_attribs
= dri3_create_context_attribs
,
725 .query_renderer_integer
= dri3_query_renderer_integer
,
726 .query_renderer_string
= dri3_query_renderer_string
,
729 /** dri3_create_screen
731 * Initialize DRI3 on the specified screen.
733 * Opens the DRI device, locates the appropriate DRI driver
736 * Checks to see if the driver supports the necessary extensions
738 * Initializes the driver for the screen and sets up our structures
741 static struct glx_screen
*
742 dri3_create_screen(int screen
, struct glx_display
* priv
)
744 xcb_connection_t
*c
= XGetXCBConnection(priv
->dpy
);
745 const __DRIconfig
**driver_configs
;
746 const __DRIextension
**extensions
;
747 const struct dri3_display
*const pdp
= (struct dri3_display
*)
749 struct dri3_screen
*psc
;
751 struct glx_config
*configs
= NULL
, *visuals
= NULL
;
752 char *driverName
, *deviceName
, *tmp
;
755 psc
= calloc(1, sizeof *psc
);
761 if (!glx_screen_init(&psc
->base
, screen
, priv
)) {
766 psc
->fd
= loader_dri3_open(c
, RootWindow(priv
->dpy
, screen
), None
);
768 int conn_error
= xcb_connection_has_error(c
);
770 glx_screen_cleanup(&psc
->base
);
772 InfoMessageF("screen %d does not appear to be DRI3 capable\n", screen
);
775 ErrorMessageF("Connection closed during DRI3 initialization failure");
780 psc
->fd
= loader_get_user_preferred_fd(psc
->fd
, &psc
->is_different_gpu
);
783 driverName
= loader_get_driver_for_fd(psc
->fd
);
785 ErrorMessageF("No driver found\n");
789 psc
->driver
= driOpenDriver(driverName
);
790 if (psc
->driver
== NULL
) {
791 ErrorMessageF("driver pointer missing\n");
795 extensions
= driGetDriverExtensions(psc
->driver
, driverName
);
796 if (extensions
== NULL
)
799 for (i
= 0; extensions
[i
]; i
++) {
800 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
801 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
802 if (strcmp(extensions
[i
]->name
, __DRI_IMAGE_DRIVER
) == 0)
803 psc
->image_driver
= (__DRIimageDriverExtension
*) extensions
[i
];
807 if (psc
->core
== NULL
) {
808 ErrorMessageF("core dri driver extension not found\n");
812 if (psc
->image_driver
== NULL
) {
813 ErrorMessageF("image driver extension not found\n");
818 psc
->image_driver
->createNewScreen2(screen
, psc
->fd
,
819 pdp
->loader_extensions
,
821 &driver_configs
, psc
);
823 if (psc
->driScreen
== NULL
) {
824 ErrorMessageF("failed to create dri screen\n");
828 dri3_bind_extensions(psc
, priv
, driverName
);
830 if (!psc
->image
|| psc
->image
->base
.version
< 7 || !psc
->image
->createImageFromFds
) {
831 ErrorMessageF("Version 7 or imageFromFds image extension not found\n");
835 if (!psc
->f
|| psc
->f
->base
.version
< 4) {
836 ErrorMessageF("Version 4 or later of flush extension not found\n");
840 if (psc
->is_different_gpu
&& psc
->image
->base
.version
< 9) {
841 ErrorMessageF("Different GPU, but image extension version 9 or later not found\n");
845 if (psc
->is_different_gpu
&& !psc
->image
->blitImage
) {
846 ErrorMessageF("Different GPU, but blitImage not implemented for this driver\n");
850 if (!psc
->is_different_gpu
&& (
851 !psc
->texBuffer
|| psc
->texBuffer
->base
.version
< 2 ||
852 !psc
->texBuffer
->setTexBuffer2
854 ErrorMessageF("Version 2 or later of texBuffer extension not found\n");
858 psc
->loader_dri3_ext
.core
= psc
->core
;
859 psc
->loader_dri3_ext
.image_driver
= psc
->image_driver
;
860 psc
->loader_dri3_ext
.flush
= psc
->f
;
861 psc
->loader_dri3_ext
.tex_buffer
= psc
->texBuffer
;
862 psc
->loader_dri3_ext
.image
= psc
->image
;
863 psc
->loader_dri3_ext
.config
= psc
->config
;
865 configs
= driConvertConfigs(psc
->core
, psc
->base
.configs
, driver_configs
);
866 visuals
= driConvertConfigs(psc
->core
, psc
->base
.visuals
, driver_configs
);
868 if (!configs
|| !visuals
) {
869 ErrorMessageF("No matching fbConfigs or visuals found\n");
873 glx_config_destroy_list(psc
->base
.configs
);
874 psc
->base
.configs
= configs
;
875 glx_config_destroy_list(psc
->base
.visuals
);
876 psc
->base
.visuals
= visuals
;
878 psc
->driver_configs
= driver_configs
;
880 psc
->base
.vtable
= &dri3_screen_vtable
;
882 psc
->base
.driScreen
= psp
;
883 psp
->destroyScreen
= dri3_destroy_screen
;
884 psp
->createDrawable
= dri3_create_drawable
;
885 psp
->swapBuffers
= dri3_swap_buffers
;
887 psp
->getDrawableMSC
= dri3_drawable_get_msc
;
888 psp
->waitForMSC
= dri3_wait_for_msc
;
889 psp
->waitForSBC
= dri3_wait_for_sbc
;
890 psp
->setSwapInterval
= dri3_set_swap_interval
;
891 psp
->getSwapInterval
= dri3_get_swap_interval
;
892 __glXEnableDirectExtension(&psc
->base
, "GLX_OML_sync_control");
894 psp
->copySubBuffer
= dri3_copy_sub_buffer
;
895 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_copy_sub_buffer");
897 psp
->getBufferAge
= dri3_get_buffer_age
;
898 __glXEnableDirectExtension(&psc
->base
, "GLX_EXT_buffer_age");
903 tmp
= getenv("LIBGL_SHOW_FPS");
904 psc
->show_fps_interval
= tmp
? atoi(tmp
) : 0;
905 if (psc
->show_fps_interval
< 0)
906 psc
->show_fps_interval
= 0;
908 InfoMessageF("Using DRI3 for screen %d\n", screen
);
913 CriticalErrorMessageF("failed to load driver: %s\n", driverName
);
916 glx_config_destroy_list(configs
);
918 glx_config_destroy_list(visuals
);
920 psc
->core
->destroyScreen(psc
->driScreen
);
921 psc
->driScreen
= NULL
;
925 dlclose(psc
->driver
);
929 glx_screen_cleanup(&psc
->base
);
935 /** dri_destroy_display
937 * Called from __glXFreeDisplayPrivate.
940 dri3_destroy_display(__GLXDRIdisplay
* dpy
)
945 /** dri3_create_display
947 * Allocate, initialize and return a __DRIdisplayPrivate object.
948 * This is called from __glXInitialize() when we are given a new
949 * display pointer. This is public to that function, but hidden from
952 _X_HIDDEN __GLXDRIdisplay
*
953 dri3_create_display(Display
* dpy
)
955 struct dri3_display
*pdp
;
956 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
957 xcb_dri3_query_version_cookie_t dri3_cookie
;
958 xcb_dri3_query_version_reply_t
*dri3_reply
;
959 xcb_present_query_version_cookie_t present_cookie
;
960 xcb_present_query_version_reply_t
*present_reply
;
961 xcb_generic_error_t
*error
;
962 const xcb_query_extension_reply_t
*extension
;
964 xcb_prefetch_extension_data(c
, &xcb_dri3_id
);
965 xcb_prefetch_extension_data(c
, &xcb_present_id
);
967 extension
= xcb_get_extension_data(c
, &xcb_dri3_id
);
968 if (!(extension
&& extension
->present
))
971 extension
= xcb_get_extension_data(c
, &xcb_present_id
);
972 if (!(extension
&& extension
->present
))
975 dri3_cookie
= xcb_dri3_query_version(c
,
976 XCB_DRI3_MAJOR_VERSION
,
977 XCB_DRI3_MINOR_VERSION
);
980 present_cookie
= xcb_present_query_version(c
,
981 XCB_PRESENT_MAJOR_VERSION
,
982 XCB_PRESENT_MINOR_VERSION
);
984 pdp
= malloc(sizeof *pdp
);
988 dri3_reply
= xcb_dri3_query_version_reply(c
, dri3_cookie
, &error
);
994 pdp
->dri3Major
= dri3_reply
->major_version
;
995 pdp
->dri3Minor
= dri3_reply
->minor_version
;
998 present_reply
= xcb_present_query_version_reply(c
, present_cookie
, &error
);
999 if (!present_reply
) {
1003 pdp
->presentMajor
= present_reply
->major_version
;
1004 pdp
->presentMinor
= present_reply
->minor_version
;
1005 free(present_reply
);
1007 pdp
->base
.destroyDisplay
= dri3_destroy_display
;
1008 pdp
->base
.createScreen
= dri3_create_screen
;
1010 loader_set_logger(dri_message
);
1012 pdp
->loader_extensions
= loader_extensions
;
1020 #endif /* GLX_DIRECT_RENDERING */