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
);
86 return (struct dri3_drawable
*)(((void*) draw
) - offset
);
90 glx_dri3_set_drawable_size(struct loader_dri3_drawable
*draw
,
91 int width
, int height
)
97 glx_dri3_in_current_context(struct loader_dri3_drawable
*draw
)
99 struct dri3_drawable
*priv
= loader_drawable_to_dri3_drawable(draw
);
104 struct dri3_context
*pcp
= (struct dri3_context
*) __glXGetCurrentContext();
105 struct dri3_screen
*psc
= (struct dri3_screen
*) priv
->base
.psc
;
107 return (&pcp
->base
!= &dummyContext
) && pcp
->base
.psc
== &psc
->base
;
110 static __DRIcontext
*
111 glx_dri3_get_dri_context(struct loader_dri3_drawable
*draw
)
113 struct glx_context
*gc
= __glXGetCurrentContext();
114 struct dri3_context
*dri3Ctx
= (struct dri3_context
*) gc
;
116 return (gc
!= &dummyContext
) ? dri3Ctx
->driContext
: NULL
;
120 glx_dri3_flush_drawable(struct loader_dri3_drawable
*draw
, unsigned flags
)
122 loader_dri3_flush(draw
, flags
, __DRI2_THROTTLE_SWAPBUFFER
);
126 glx_dri3_show_fps(struct loader_dri3_drawable
*draw
, uint64_t current_ust
)
128 struct dri3_drawable
*priv
= loader_drawable_to_dri3_drawable(draw
);
129 const uint64_t interval
=
130 ((struct dri3_screen
*) priv
->base
.psc
)->show_fps_interval
;
137 /* DRI3+Present together uses microseconds for UST. */
138 if (priv
->previous_ust
+ interval
* 1000000 <= current_ust
) {
139 if (priv
->previous_ust
) {
140 fprintf(stderr
, "libGL: FPS = %.1f\n",
141 ((uint64_t) priv
->frames
* 1000000) /
142 (double)(current_ust
- priv
->previous_ust
));
145 priv
->previous_ust
= current_ust
;
149 static const struct loader_dri3_vtable glx_dri3_vtable
= {
150 .set_drawable_size
= glx_dri3_set_drawable_size
,
151 .in_current_context
= glx_dri3_in_current_context
,
152 .get_dri_context
= glx_dri3_get_dri_context
,
153 .flush_drawable
= glx_dri3_flush_drawable
,
154 .show_fps
= glx_dri3_show_fps
,
158 static const struct glx_context_vtable dri3_context_vtable
;
161 dri3_destroy_context(struct glx_context
*context
)
163 struct dri3_context
*pcp
= (struct dri3_context
*) context
;
164 struct dri3_screen
*psc
= (struct dri3_screen
*) context
->psc
;
166 driReleaseDrawables(&pcp
->base
);
168 free((char *) context
->extensions
);
170 (*psc
->core
->destroyContext
) (pcp
->driContext
);
176 dri3_bind_context(struct glx_context
*context
, struct glx_context
*old
,
177 GLXDrawable draw
, GLXDrawable read
)
179 struct dri3_context
*pcp
= (struct dri3_context
*) context
;
180 struct dri3_screen
*psc
= (struct dri3_screen
*) pcp
->base
.psc
;
181 struct dri3_drawable
*pdraw
, *pread
;
182 __DRIdrawable
*dri_draw
= NULL
, *dri_read
= NULL
;
184 pdraw
= (struct dri3_drawable
*) driFetchDrawable(context
, draw
);
185 pread
= (struct dri3_drawable
*) driFetchDrawable(context
, read
);
187 driReleaseDrawables(&pcp
->base
);
190 dri_draw
= pdraw
->loader_drawable
.dri_drawable
;
191 else if (draw
!= None
)
192 return GLXBadDrawable
;
195 dri_read
= pread
->loader_drawable
.dri_drawable
;
196 else if (read
!= None
)
197 return GLXBadDrawable
;
199 if (!(*psc
->core
->bindContext
) (pcp
->driContext
, dri_draw
, dri_read
))
200 return GLXBadContext
;
203 (*psc
->f
->invalidate
)(dri_draw
);
204 if (dri_read
&& dri_read
!= dri_draw
)
205 (*psc
->f
->invalidate
)(dri_read
);
211 dri3_unbind_context(struct glx_context
*context
, struct glx_context
*new)
213 struct dri3_context
*pcp
= (struct dri3_context
*) context
;
214 struct dri3_screen
*psc
= (struct dri3_screen
*) pcp
->base
.psc
;
216 (*psc
->core
->unbindContext
) (pcp
->driContext
);
219 static struct glx_context
*
220 dri3_create_context_attribs(struct glx_screen
*base
,
221 struct glx_config
*config_base
,
222 struct glx_context
*shareList
,
223 unsigned num_attribs
,
224 const uint32_t *attribs
,
227 struct dri3_context
*pcp
= NULL
;
228 struct dri3_context
*pcp_shared
= NULL
;
229 struct dri3_screen
*psc
= (struct dri3_screen
*) base
;
230 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
231 __DRIcontext
*shared
= NULL
;
233 uint32_t minor_ver
= 1;
234 uint32_t major_ver
= 2;
237 int reset
= __DRI_CTX_RESET_NO_NOTIFICATION
;
238 uint32_t ctx_attribs
[2 * 5];
239 unsigned num_ctx_attribs
= 0;
240 uint32_t render_type
;
242 /* Remap the GLX tokens to DRI2 tokens.
244 if (!dri2_convert_glx_attribs(num_attribs
, attribs
,
245 &major_ver
, &minor_ver
,
246 &render_type
, &flags
, &api
,
250 /* Check the renderType value */
251 if (!validate_renderType_against_config(config_base
, render_type
))
255 pcp_shared
= (struct dri3_context
*) shareList
;
256 shared
= pcp_shared
->driContext
;
259 pcp
= calloc(1, sizeof *pcp
);
261 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
265 if (!glx_context_init(&pcp
->base
, &psc
->base
, &config
->base
))
268 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
269 ctx_attribs
[num_ctx_attribs
++] = major_ver
;
270 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
271 ctx_attribs
[num_ctx_attribs
++] = minor_ver
;
273 /* Only send a value when the non-default value is requested. By doing
274 * this we don't have to check the driver's DRI3 version before sending the
277 if (reset
!= __DRI_CTX_RESET_NO_NOTIFICATION
) {
278 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
279 ctx_attribs
[num_ctx_attribs
++] = reset
;
283 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_FLAGS
;
285 /* The current __DRI_CTX_FLAG_* values are identical to the
286 * GLX_CONTEXT_*_BIT values.
288 ctx_attribs
[num_ctx_attribs
++] = flags
;
292 (*psc
->image_driver
->createContextAttribs
) (psc
->driScreen
,
301 if (pcp
->driContext
== NULL
)
304 pcp
->base
.vtable
= &dri3_context_vtable
;
314 static struct glx_context
*
315 dri3_create_context(struct glx_screen
*base
,
316 struct glx_config
*config_base
,
317 struct glx_context
*shareList
, int renderType
)
321 return dri3_create_context_attribs(base
, config_base
, shareList
,
326 dri3_destroy_drawable(__GLXDRIdrawable
*base
)
328 struct dri3_drawable
*pdraw
= (struct dri3_drawable
*) base
;
330 loader_dri3_drawable_fini(&pdraw
->loader_drawable
);
335 static __GLXDRIdrawable
*
336 dri3_create_drawable(struct glx_screen
*base
, XID xDrawable
,
337 GLXDrawable drawable
, struct glx_config
*config_base
)
339 struct dri3_drawable
*pdraw
;
340 struct dri3_screen
*psc
= (struct dri3_screen
*) base
;
341 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
343 pdraw
= calloc(1, sizeof(*pdraw
));
347 pdraw
->base
.destroyDrawable
= dri3_destroy_drawable
;
348 pdraw
->base
.xDrawable
= xDrawable
;
349 pdraw
->base
.drawable
= drawable
;
350 pdraw
->base
.psc
= &psc
->base
;
352 (void) __glXInitialize(psc
->base
.dpy
);
354 if (loader_dri3_drawable_init(XGetXCBConnection(base
->dpy
),
355 xDrawable
, psc
->driScreen
,
356 psc
->is_different_gpu
, config
->driConfig
,
357 &psc
->loader_dri3_ext
, &glx_dri3_vtable
,
358 &pdraw
->loader_drawable
)) {
366 /** dri3_wait_for_msc
368 * Get the X server to send an event when the target msc/divisor/remainder is
372 dri3_wait_for_msc(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
373 int64_t remainder
, int64_t *ust
, int64_t *msc
, int64_t *sbc
)
375 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
377 loader_dri3_wait_for_msc(&priv
->loader_drawable
, target_msc
, divisor
,
378 remainder
, ust
, msc
, sbc
);
383 /** dri3_drawable_get_msc
385 * Return the current UST/MSC/SBC triplet by asking the server
389 dri3_drawable_get_msc(struct glx_screen
*psc
, __GLXDRIdrawable
*pdraw
,
390 int64_t *ust
, int64_t *msc
, int64_t *sbc
)
392 return dri3_wait_for_msc(pdraw
, 0, 0, 0, ust
, msc
,sbc
);
395 /** dri3_wait_for_sbc
397 * Wait for the completed swap buffer count to reach the specified
398 * target. Presumably the application knows that this will be reached with
399 * outstanding complete events, or we're going to be here awhile.
402 dri3_wait_for_sbc(__GLXDRIdrawable
*pdraw
, int64_t target_sbc
, int64_t *ust
,
403 int64_t *msc
, int64_t *sbc
)
405 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
407 return loader_dri3_wait_for_sbc(&priv
->loader_drawable
, target_sbc
,
412 dri3_copy_sub_buffer(__GLXDRIdrawable
*pdraw
, int x
, int y
,
413 int width
, int height
,
416 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
418 loader_dri3_copy_sub_buffer(&priv
->loader_drawable
, x
, y
,
419 width
, height
, flush
);
423 dri3_wait_x(struct glx_context
*gc
)
425 struct dri3_drawable
*priv
= (struct dri3_drawable
*)
426 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
429 loader_dri3_wait_x(&priv
->loader_drawable
);
433 dri3_wait_gl(struct glx_context
*gc
)
435 struct dri3_drawable
*priv
= (struct dri3_drawable
*)
436 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
439 loader_dri3_wait_gl(&priv
->loader_drawable
);
443 * Called by the driver when it needs to update the real front buffer with the
444 * contents of its fake front buffer.
447 dri3_flush_front_buffer(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
449 struct loader_dri3_drawable
*draw
= loaderPrivate
;
450 struct dri3_drawable
*pdraw
= loader_drawable_to_dri3_drawable(draw
);
451 struct dri3_screen
*psc
;
456 if (!pdraw
->base
.psc
)
459 psc
= (struct dri3_screen
*) pdraw
->base
.psc
;
461 (void) __glXInitialize(psc
->base
.dpy
);
463 loader_dri3_flush(draw
, __DRI2_FLUSH_DRAWABLE
, __DRI2_THROTTLE_FLUSHFRONT
);
465 (*psc
->f
->invalidate
)(driDrawable
);
466 loader_dri3_wait_gl(draw
);
470 * Make sure all pending swapbuffers have been submitted to hardware
472 * \param driDrawable[in] Pointer to the dri drawable whose swaps we are
474 * \param loaderPrivate[in] Pointer to the corresponding struct
475 * loader_dri_drawable.
478 dri3_flush_swap_buffers(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
480 struct loader_dri3_drawable
*draw
= loaderPrivate
;
481 struct dri3_drawable
*pdraw
= loader_drawable_to_dri3_drawable(draw
);
482 struct dri3_screen
*psc
;
487 if (!pdraw
->base
.psc
)
490 psc
= (struct dri3_screen
*) pdraw
->base
.psc
;
492 (void) __glXInitialize(psc
->base
.dpy
);
493 loader_dri3_swapbuffer_barrier(draw
);
497 dri_set_background_context(void *loaderPrivate
)
499 struct dri3_context
*pcp
= (struct dri3_context
*)loaderPrivate
;
500 __glXSetCurrentContext(&pcp
->base
);
504 dri_is_thread_safe(void *loaderPrivate
)
506 /* Unlike DRI2, DRI3 doesn't call GetBuffers/GetBuffersWithFormat
507 * during draw so we're safe here.
512 /* The image loader extension record for DRI3
514 static const __DRIimageLoaderExtension imageLoaderExtension
= {
515 .base
= { __DRI_IMAGE_LOADER
, 3 },
517 .getBuffers
= loader_dri3_get_buffers
,
518 .flushFrontBuffer
= dri3_flush_front_buffer
,
519 .flushSwapBuffers
= dri3_flush_swap_buffers
,
522 const __DRIuseInvalidateExtension dri3UseInvalidate
= {
523 .base
= { __DRI_USE_INVALIDATE
, 1 }
526 static const __DRIbackgroundCallableExtension driBackgroundCallable
= {
527 .base
= { __DRI_BACKGROUND_CALLABLE
, 2 },
529 .setBackgroundContext
= dri_set_background_context
,
530 .isThreadSafe
= dri_is_thread_safe
,
533 static const __DRIextension
*loader_extensions
[] = {
534 &imageLoaderExtension
.base
,
535 &dri3UseInvalidate
.base
,
536 &driBackgroundCallable
.base
,
540 /** dri3_swap_buffers
542 * Make the current back buffer visible using the present extension
545 dri3_swap_buffers(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
546 int64_t remainder
, Bool flush
)
548 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
549 unsigned flags
= __DRI2_FLUSH_DRAWABLE
;
552 flags
|= __DRI2_FLUSH_CONTEXT
;
554 return loader_dri3_swap_buffers_msc(&priv
->loader_drawable
,
555 target_msc
, divisor
, remainder
,
560 dri3_get_buffer_age(__GLXDRIdrawable
*pdraw
)
562 struct dri3_drawable
*priv
= (struct dri3_drawable
*)pdraw
;
564 return loader_dri3_query_buffer_age(&priv
->loader_drawable
);
567 /** dri3_destroy_screen
570 dri3_destroy_screen(struct glx_screen
*base
)
572 struct dri3_screen
*psc
= (struct dri3_screen
*) base
;
574 /* Free the direct rendering per screen data */
575 loader_dri3_close_screen(psc
->driScreen
);
576 (*psc
->core
->destroyScreen
) (psc
->driScreen
);
577 driDestroyConfigs(psc
->driver_configs
);
582 /** dri3_set_swap_interval
584 * Record the application swap interval specification,
587 dri3_set_swap_interval(__GLXDRIdrawable
*pdraw
, int interval
)
589 assert(pdraw
!= NULL
);
591 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
592 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
593 struct dri3_screen
*psc
= (struct dri3_screen
*) priv
->base
.psc
;
596 psc
->config
->configQueryi(psc
->driScreen
,
597 "vblank_mode", &vblank_mode
);
599 switch (vblank_mode
) {
600 case DRI_CONF_VBLANK_NEVER
:
602 return GLX_BAD_VALUE
;
604 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
606 return GLX_BAD_VALUE
;
612 priv
->swap_interval
= interval
;
613 loader_dri3_set_swap_interval(&priv
->loader_drawable
, interval
);
618 /** dri3_get_swap_interval
620 * Return the stored swap interval
623 dri3_get_swap_interval(__GLXDRIdrawable
*pdraw
)
625 assert(pdraw
!= NULL
);
627 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
629 return priv
->swap_interval
;
633 dri3_bind_tex_image(Display
* dpy
,
634 GLXDrawable drawable
,
635 int buffer
, const int *attrib_list
)
637 struct glx_context
*gc
= __glXGetCurrentContext();
638 struct dri3_context
*pcp
= (struct dri3_context
*) gc
;
639 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
640 struct dri3_drawable
*pdraw
= (struct dri3_drawable
*) base
;
641 struct dri3_screen
*psc
;
644 psc
= (struct dri3_screen
*) base
->psc
;
646 (*psc
->f
->invalidate
)(pdraw
->loader_drawable
.dri_drawable
);
650 (*psc
->texBuffer
->setTexBuffer2
) (pcp
->driContext
,
651 pdraw
->base
.textureTarget
,
652 pdraw
->base
.textureFormat
,
653 pdraw
->loader_drawable
.dri_drawable
);
658 dri3_release_tex_image(Display
* dpy
, GLXDrawable drawable
, int buffer
)
660 struct glx_context
*gc
= __glXGetCurrentContext();
661 struct dri3_context
*pcp
= (struct dri3_context
*) gc
;
662 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
663 struct dri3_drawable
*pdraw
= (struct dri3_drawable
*) base
;
664 struct dri3_screen
*psc
;
667 psc
= (struct dri3_screen
*) base
->psc
;
669 if (psc
->texBuffer
->base
.version
>= 3 &&
670 psc
->texBuffer
->releaseTexBuffer
!= NULL
)
671 (*psc
->texBuffer
->releaseTexBuffer
) (pcp
->driContext
,
672 pdraw
->base
.textureTarget
,
673 pdraw
->loader_drawable
.dri_drawable
);
677 static const struct glx_context_vtable dri3_context_vtable
= {
678 .destroy
= dri3_destroy_context
,
679 .bind
= dri3_bind_context
,
680 .unbind
= dri3_unbind_context
,
681 .wait_gl
= dri3_wait_gl
,
682 .wait_x
= dri3_wait_x
,
683 .use_x_font
= DRI_glXUseXFont
,
684 .bind_tex_image
= dri3_bind_tex_image
,
685 .release_tex_image
= dri3_release_tex_image
,
686 .get_proc_address
= NULL
,
687 .interop_query_device_info
= dri3_interop_query_device_info
,
688 .interop_export_object
= dri3_interop_export_object
691 /** dri3_bind_extensions
693 * Enable all of the extensions supported on DRI3
696 dri3_bind_extensions(struct dri3_screen
*psc
, struct glx_display
* priv
,
697 const char *driverName
)
699 const __DRIextension
**extensions
;
703 extensions
= psc
->core
->getExtensions(psc
->driScreen
);
705 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_video_sync");
706 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_swap_control");
707 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_swap_control");
708 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_make_current_read");
709 __glXEnableDirectExtension(&psc
->base
, "GLX_INTEL_swap_event");
711 mask
= psc
->image_driver
->getAPIMask(psc
->driScreen
);
713 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context");
714 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context_profile");
716 if ((mask
& ((1 << __DRI_API_GLES
) |
717 (1 << __DRI_API_GLES2
) |
718 (1 << __DRI_API_GLES3
))) != 0) {
719 __glXEnableDirectExtension(&psc
->base
,
720 "GLX_EXT_create_context_es_profile");
721 __glXEnableDirectExtension(&psc
->base
,
722 "GLX_EXT_create_context_es2_profile");
725 for (i
= 0; extensions
[i
]; i
++) {
726 /* when on a different gpu than the server, the server pixmaps
727 * can have a tiling mode we can't read. Thus we can't create
728 * a texture from them.
730 if (!psc
->is_different_gpu
&&
731 (strcmp(extensions
[i
]->name
, __DRI_TEX_BUFFER
) == 0)) {
732 psc
->texBuffer
= (__DRItexBufferExtension
*) extensions
[i
];
733 __glXEnableDirectExtension(&psc
->base
, "GLX_EXT_texture_from_pixmap");
736 if ((strcmp(extensions
[i
]->name
, __DRI2_FLUSH
) == 0)) {
737 psc
->f
= (__DRI2flushExtension
*) extensions
[i
];
738 /* internal driver extension, no GL extension exposed */
741 if (strcmp(extensions
[i
]->name
, __DRI_IMAGE
) == 0)
742 psc
->image
= (__DRIimageExtension
*) extensions
[i
];
744 if ((strcmp(extensions
[i
]->name
, __DRI2_CONFIG_QUERY
) == 0))
745 psc
->config
= (__DRI2configQueryExtension
*) extensions
[i
];
747 if (strcmp(extensions
[i
]->name
, __DRI2_ROBUSTNESS
) == 0)
748 __glXEnableDirectExtension(&psc
->base
,
749 "GLX_ARB_create_context_robustness");
751 if (strcmp(extensions
[i
]->name
, __DRI2_RENDERER_QUERY
) == 0) {
752 psc
->rendererQuery
= (__DRI2rendererQueryExtension
*) extensions
[i
];
753 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_query_renderer");
756 if (strcmp(extensions
[i
]->name
, __DRI2_INTEROP
) == 0)
757 psc
->interop
= (__DRI2interopExtension
*)extensions
[i
];
761 static const struct glx_screen_vtable dri3_screen_vtable
= {
762 .create_context
= dri3_create_context
,
763 .create_context_attribs
= dri3_create_context_attribs
,
764 .query_renderer_integer
= dri3_query_renderer_integer
,
765 .query_renderer_string
= dri3_query_renderer_string
,
768 /** dri3_create_screen
770 * Initialize DRI3 on the specified screen.
772 * Opens the DRI device, locates the appropriate DRI driver
775 * Checks to see if the driver supports the necessary extensions
777 * Initializes the driver for the screen and sets up our structures
780 static struct glx_screen
*
781 dri3_create_screen(int screen
, struct glx_display
* priv
)
783 xcb_connection_t
*c
= XGetXCBConnection(priv
->dpy
);
784 const __DRIconfig
**driver_configs
;
785 const __DRIextension
**extensions
;
786 const struct dri3_display
*const pdp
= (struct dri3_display
*)
788 struct dri3_screen
*psc
;
790 struct glx_config
*configs
= NULL
, *visuals
= NULL
;
791 char *driverName
, *deviceName
, *tmp
;
793 unsigned char disable
;
795 psc
= calloc(1, sizeof *psc
);
801 if (!glx_screen_init(&psc
->base
, screen
, priv
)) {
806 psc
->fd
= loader_dri3_open(c
, RootWindow(priv
->dpy
, screen
), None
);
808 int conn_error
= xcb_connection_has_error(c
);
810 glx_screen_cleanup(&psc
->base
);
812 InfoMessageF("screen %d does not appear to be DRI3 capable\n", screen
);
815 ErrorMessageF("Connection closed during DRI3 initialization failure");
820 psc
->fd
= loader_get_user_preferred_fd(psc
->fd
, &psc
->is_different_gpu
);
823 driverName
= loader_get_driver_for_fd(psc
->fd
);
825 ErrorMessageF("No driver found\n");
829 psc
->driver
= driOpenDriver(driverName
);
830 if (psc
->driver
== NULL
) {
831 ErrorMessageF("driver pointer missing\n");
835 extensions
= driGetDriverExtensions(psc
->driver
, driverName
);
836 if (extensions
== NULL
)
839 for (i
= 0; extensions
[i
]; i
++) {
840 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
841 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
842 if (strcmp(extensions
[i
]->name
, __DRI_IMAGE_DRIVER
) == 0)
843 psc
->image_driver
= (__DRIimageDriverExtension
*) extensions
[i
];
847 if (psc
->core
== NULL
) {
848 ErrorMessageF("core dri driver extension not found\n");
852 if (psc
->image_driver
== NULL
) {
853 ErrorMessageF("image driver extension not found\n");
858 psc
->image_driver
->createNewScreen2(screen
, psc
->fd
,
859 pdp
->loader_extensions
,
861 &driver_configs
, psc
);
863 if (psc
->driScreen
== NULL
) {
864 ErrorMessageF("failed to create dri screen\n");
868 dri3_bind_extensions(psc
, priv
, driverName
);
870 if (!psc
->image
|| psc
->image
->base
.version
< 7 || !psc
->image
->createImageFromFds
) {
871 ErrorMessageF("Version 7 or imageFromFds image extension not found\n");
875 if (!psc
->f
|| psc
->f
->base
.version
< 4) {
876 ErrorMessageF("Version 4 or later of flush extension not found\n");
880 if (psc
->is_different_gpu
&& psc
->image
->base
.version
< 9) {
881 ErrorMessageF("Different GPU, but image extension version 9 or later not found\n");
885 if (psc
->is_different_gpu
&& !psc
->image
->blitImage
) {
886 ErrorMessageF("Different GPU, but blitImage not implemented for this driver\n");
890 if (!psc
->is_different_gpu
&& (
891 !psc
->texBuffer
|| psc
->texBuffer
->base
.version
< 2 ||
892 !psc
->texBuffer
->setTexBuffer2
894 ErrorMessageF("Version 2 or later of texBuffer extension not found\n");
898 psc
->loader_dri3_ext
.core
= psc
->core
;
899 psc
->loader_dri3_ext
.image_driver
= psc
->image_driver
;
900 psc
->loader_dri3_ext
.flush
= psc
->f
;
901 psc
->loader_dri3_ext
.tex_buffer
= psc
->texBuffer
;
902 psc
->loader_dri3_ext
.image
= psc
->image
;
903 psc
->loader_dri3_ext
.config
= psc
->config
;
905 configs
= driConvertConfigs(psc
->core
, psc
->base
.configs
, driver_configs
);
906 visuals
= driConvertConfigs(psc
->core
, psc
->base
.visuals
, driver_configs
);
908 if (!configs
|| !visuals
) {
909 ErrorMessageF("No matching fbConfigs or visuals found\n");
913 glx_config_destroy_list(psc
->base
.configs
);
914 psc
->base
.configs
= configs
;
915 glx_config_destroy_list(psc
->base
.visuals
);
916 psc
->base
.visuals
= visuals
;
918 psc
->driver_configs
= driver_configs
;
920 psc
->base
.vtable
= &dri3_screen_vtable
;
922 psc
->base
.driScreen
= psp
;
923 psp
->destroyScreen
= dri3_destroy_screen
;
924 psp
->createDrawable
= dri3_create_drawable
;
925 psp
->swapBuffers
= dri3_swap_buffers
;
927 psp
->getDrawableMSC
= dri3_drawable_get_msc
;
928 psp
->waitForMSC
= dri3_wait_for_msc
;
929 psp
->waitForSBC
= dri3_wait_for_sbc
;
930 psp
->setSwapInterval
= dri3_set_swap_interval
;
931 psp
->getSwapInterval
= dri3_get_swap_interval
;
932 if (psc
->config
->configQueryb(psc
->driScreen
,
933 "glx_disable_oml_sync_control",
934 &disable
) || !disable
)
935 __glXEnableDirectExtension(&psc
->base
, "GLX_OML_sync_control");
937 psp
->copySubBuffer
= dri3_copy_sub_buffer
;
938 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_copy_sub_buffer");
940 psp
->getBufferAge
= dri3_get_buffer_age
;
941 if (psc
->config
->configQueryb(psc
->driScreen
,
942 "glx_disable_ext_buffer_age",
943 &disable
) || !disable
)
944 __glXEnableDirectExtension(&psc
->base
, "GLX_EXT_buffer_age");
949 tmp
= getenv("LIBGL_SHOW_FPS");
950 psc
->show_fps_interval
= tmp
? atoi(tmp
) : 0;
951 if (psc
->show_fps_interval
< 0)
952 psc
->show_fps_interval
= 0;
954 InfoMessageF("Using DRI3 for screen %d\n", screen
);
959 CriticalErrorMessageF("failed to load driver: %s\n", driverName
);
962 glx_config_destroy_list(configs
);
964 glx_config_destroy_list(visuals
);
966 psc
->core
->destroyScreen(psc
->driScreen
);
967 psc
->driScreen
= NULL
;
971 dlclose(psc
->driver
);
975 glx_screen_cleanup(&psc
->base
);
981 /** dri_destroy_display
983 * Called from __glXFreeDisplayPrivate.
986 dri3_destroy_display(__GLXDRIdisplay
* dpy
)
991 /** dri3_create_display
993 * Allocate, initialize and return a __DRIdisplayPrivate object.
994 * This is called from __glXInitialize() when we are given a new
995 * display pointer. This is public to that function, but hidden from
998 _X_HIDDEN __GLXDRIdisplay
*
999 dri3_create_display(Display
* dpy
)
1001 struct dri3_display
*pdp
;
1002 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
1003 xcb_dri3_query_version_cookie_t dri3_cookie
;
1004 xcb_dri3_query_version_reply_t
*dri3_reply
;
1005 xcb_present_query_version_cookie_t present_cookie
;
1006 xcb_present_query_version_reply_t
*present_reply
;
1007 xcb_generic_error_t
*error
;
1008 const xcb_query_extension_reply_t
*extension
;
1010 xcb_prefetch_extension_data(c
, &xcb_dri3_id
);
1011 xcb_prefetch_extension_data(c
, &xcb_present_id
);
1013 extension
= xcb_get_extension_data(c
, &xcb_dri3_id
);
1014 if (!(extension
&& extension
->present
))
1017 extension
= xcb_get_extension_data(c
, &xcb_present_id
);
1018 if (!(extension
&& extension
->present
))
1021 dri3_cookie
= xcb_dri3_query_version(c
,
1022 XCB_DRI3_MAJOR_VERSION
,
1023 XCB_DRI3_MINOR_VERSION
);
1026 present_cookie
= xcb_present_query_version(c
,
1027 XCB_PRESENT_MAJOR_VERSION
,
1028 XCB_PRESENT_MINOR_VERSION
);
1030 pdp
= malloc(sizeof *pdp
);
1034 dri3_reply
= xcb_dri3_query_version_reply(c
, dri3_cookie
, &error
);
1040 pdp
->dri3Major
= dri3_reply
->major_version
;
1041 pdp
->dri3Minor
= dri3_reply
->minor_version
;
1044 present_reply
= xcb_present_query_version_reply(c
, present_cookie
, &error
);
1045 if (!present_reply
) {
1049 pdp
->presentMajor
= present_reply
->major_version
;
1050 pdp
->presentMinor
= present_reply
->minor_version
;
1051 free(present_reply
);
1053 pdp
->base
.destroyDisplay
= dri3_destroy_display
;
1054 pdp
->base
.createScreen
= dri3_create_screen
;
1056 loader_set_logger(dri_message
);
1058 pdp
->loader_extensions
= loader_extensions
;
1066 #endif /* GLX_DIRECT_RENDERING */