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_get_swap_interval(struct loader_dri3_drawable
*draw
)
92 struct dri3_drawable
*priv
= loader_drawable_to_dri3_drawable(draw
);
94 return priv
->swap_interval
;
98 glx_dri3_clamp_swap_interval(struct loader_dri3_drawable
*draw
, int interval
)
104 glx_dri3_set_swap_interval(struct loader_dri3_drawable
*draw
, int interval
)
106 struct dri3_drawable
*priv
= loader_drawable_to_dri3_drawable(draw
);
108 priv
->swap_interval
= interval
;
112 glx_dri3_set_drawable_size(struct loader_dri3_drawable
*draw
,
113 int width
, int height
)
119 glx_dri3_in_current_context(struct loader_dri3_drawable
*draw
)
121 struct dri3_drawable
*priv
= loader_drawable_to_dri3_drawable(draw
);
126 struct dri3_context
*pcp
= (struct dri3_context
*) __glXGetCurrentContext();
127 struct dri3_screen
*psc
= (struct dri3_screen
*) priv
->base
.psc
;
129 return (&pcp
->base
!= &dummyContext
) && pcp
->base
.psc
== &psc
->base
;
132 static __DRIcontext
*
133 glx_dri3_get_dri_context(struct loader_dri3_drawable
*draw
)
135 struct glx_context
*gc
= __glXGetCurrentContext();
136 struct dri3_context
*dri3Ctx
= (struct dri3_context
*) gc
;
138 return (gc
!= &dummyContext
) ? dri3Ctx
->driContext
: NULL
;
142 glx_dri3_get_dri_screen(struct loader_dri3_drawable
*draw
)
144 struct glx_context
*gc
= __glXGetCurrentContext();
145 struct dri3_context
*pcp
= (struct dri3_context
*) gc
;
146 struct dri3_screen
*psc
= (struct dri3_screen
*) pcp
->base
.psc
;
148 return (gc
!= &dummyContext
&& psc
) ? psc
->driScreen
: NULL
;
152 glx_dri3_flush_drawable(struct loader_dri3_drawable
*draw
, unsigned flags
)
154 loader_dri3_flush(draw
, flags
, __DRI2_THROTTLE_SWAPBUFFER
);
158 glx_dri3_show_fps(struct loader_dri3_drawable
*draw
, uint64_t current_ust
)
160 struct dri3_drawable
*priv
= loader_drawable_to_dri3_drawable(draw
);
161 const uint64_t interval
=
162 ((struct dri3_screen
*) priv
->base
.psc
)->show_fps_interval
;
169 /* DRI3+Present together uses microseconds for UST. */
170 if (priv
->previous_ust
+ interval
* 1000000 <= current_ust
) {
171 if (priv
->previous_ust
) {
172 fprintf(stderr
, "libGL: FPS = %.1f\n",
173 ((uint64_t) priv
->frames
* 1000000) /
174 (double)(current_ust
- priv
->previous_ust
));
177 priv
->previous_ust
= current_ust
;
181 static const struct loader_dri3_vtable glx_dri3_vtable
= {
182 .get_swap_interval
= glx_dri3_get_swap_interval
,
183 .clamp_swap_interval
= glx_dri3_clamp_swap_interval
,
184 .set_swap_interval
= glx_dri3_set_swap_interval
,
185 .set_drawable_size
= glx_dri3_set_drawable_size
,
186 .in_current_context
= glx_dri3_in_current_context
,
187 .get_dri_context
= glx_dri3_get_dri_context
,
188 .get_dri_screen
= glx_dri3_get_dri_screen
,
189 .flush_drawable
= glx_dri3_flush_drawable
,
190 .show_fps
= glx_dri3_show_fps
,
194 static const struct glx_context_vtable dri3_context_vtable
;
197 dri3_destroy_context(struct glx_context
*context
)
199 struct dri3_context
*pcp
= (struct dri3_context
*) context
;
200 struct dri3_screen
*psc
= (struct dri3_screen
*) context
->psc
;
202 driReleaseDrawables(&pcp
->base
);
204 free((char *) context
->extensions
);
206 (*psc
->core
->destroyContext
) (pcp
->driContext
);
212 dri3_bind_context(struct glx_context
*context
, struct glx_context
*old
,
213 GLXDrawable draw
, GLXDrawable read
)
215 struct dri3_context
*pcp
= (struct dri3_context
*) context
;
216 struct dri3_screen
*psc
= (struct dri3_screen
*) pcp
->base
.psc
;
217 struct dri3_drawable
*pdraw
, *pread
;
218 __DRIdrawable
*dri_draw
= NULL
, *dri_read
= NULL
;
220 pdraw
= (struct dri3_drawable
*) driFetchDrawable(context
, draw
);
221 pread
= (struct dri3_drawable
*) driFetchDrawable(context
, read
);
223 driReleaseDrawables(&pcp
->base
);
226 dri_draw
= pdraw
->loader_drawable
.dri_drawable
;
227 else if (draw
!= None
)
228 return GLXBadDrawable
;
231 dri_read
= pread
->loader_drawable
.dri_drawable
;
232 else if (read
!= None
)
233 return GLXBadDrawable
;
235 if (!(*psc
->core
->bindContext
) (pcp
->driContext
, dri_draw
, dri_read
))
236 return GLXBadContext
;
242 dri3_unbind_context(struct glx_context
*context
, struct glx_context
*new)
244 struct dri3_context
*pcp
= (struct dri3_context
*) context
;
245 struct dri3_screen
*psc
= (struct dri3_screen
*) pcp
->base
.psc
;
247 (*psc
->core
->unbindContext
) (pcp
->driContext
);
250 static struct glx_context
*
251 dri3_create_context_attribs(struct glx_screen
*base
,
252 struct glx_config
*config_base
,
253 struct glx_context
*shareList
,
254 unsigned num_attribs
,
255 const uint32_t *attribs
,
258 struct dri3_context
*pcp
= NULL
;
259 struct dri3_context
*pcp_shared
= NULL
;
260 struct dri3_screen
*psc
= (struct dri3_screen
*) base
;
261 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
262 __DRIcontext
*shared
= NULL
;
264 uint32_t minor_ver
= 1;
265 uint32_t major_ver
= 2;
268 int reset
= __DRI_CTX_RESET_NO_NOTIFICATION
;
269 uint32_t ctx_attribs
[2 * 5];
270 unsigned num_ctx_attribs
= 0;
271 uint32_t render_type
;
273 /* Remap the GLX tokens to DRI2 tokens.
275 if (!dri2_convert_glx_attribs(num_attribs
, attribs
,
276 &major_ver
, &minor_ver
,
277 &render_type
, &flags
, &api
,
281 /* Check the renderType value */
282 if (!validate_renderType_against_config(config_base
, render_type
))
286 pcp_shared
= (struct dri3_context
*) shareList
;
287 shared
= pcp_shared
->driContext
;
290 pcp
= calloc(1, sizeof *pcp
);
292 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
296 if (!glx_context_init(&pcp
->base
, &psc
->base
, &config
->base
))
299 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
300 ctx_attribs
[num_ctx_attribs
++] = major_ver
;
301 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
302 ctx_attribs
[num_ctx_attribs
++] = minor_ver
;
304 /* Only send a value when the non-default value is requested. By doing
305 * this we don't have to check the driver's DRI3 version before sending the
308 if (reset
!= __DRI_CTX_RESET_NO_NOTIFICATION
) {
309 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
310 ctx_attribs
[num_ctx_attribs
++] = reset
;
314 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_FLAGS
;
316 /* The current __DRI_CTX_FLAG_* values are identical to the
317 * GLX_CONTEXT_*_BIT values.
319 ctx_attribs
[num_ctx_attribs
++] = flags
;
323 (*psc
->image_driver
->createContextAttribs
) (psc
->driScreen
,
332 if (pcp
->driContext
== NULL
)
335 pcp
->base
.vtable
= &dri3_context_vtable
;
345 static struct glx_context
*
346 dri3_create_context(struct glx_screen
*base
,
347 struct glx_config
*config_base
,
348 struct glx_context
*shareList
, int renderType
)
352 return dri3_create_context_attribs(base
, config_base
, shareList
,
357 dri3_destroy_drawable(__GLXDRIdrawable
*base
)
359 struct dri3_drawable
*pdraw
= (struct dri3_drawable
*) base
;
361 loader_dri3_drawable_fini(&pdraw
->loader_drawable
);
366 static __GLXDRIdrawable
*
367 dri3_create_drawable(struct glx_screen
*base
, XID xDrawable
,
368 GLXDrawable drawable
, struct glx_config
*config_base
)
370 struct dri3_drawable
*pdraw
;
371 struct dri3_screen
*psc
= (struct dri3_screen
*) base
;
372 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
374 pdraw
= calloc(1, sizeof(*pdraw
));
378 pdraw
->base
.destroyDrawable
= dri3_destroy_drawable
;
379 pdraw
->base
.xDrawable
= xDrawable
;
380 pdraw
->base
.drawable
= drawable
;
381 pdraw
->base
.psc
= &psc
->base
;
383 (void) __glXInitialize(psc
->base
.dpy
);
385 if (loader_dri3_drawable_init(XGetXCBConnection(base
->dpy
),
386 xDrawable
, psc
->driScreen
,
387 psc
->is_different_gpu
, config
->driConfig
,
388 &psc
->loader_dri3_ext
, &glx_dri3_vtable
,
389 &pdraw
->loader_drawable
)) {
397 /** dri3_wait_for_msc
399 * Get the X server to send an event when the target msc/divisor/remainder is
403 dri3_wait_for_msc(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
404 int64_t remainder
, int64_t *ust
, int64_t *msc
, int64_t *sbc
)
406 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
408 loader_dri3_wait_for_msc(&priv
->loader_drawable
, target_msc
, divisor
,
409 remainder
, ust
, msc
, sbc
);
414 /** dri3_drawable_get_msc
416 * Return the current UST/MSC/SBC triplet by asking the server
420 dri3_drawable_get_msc(struct glx_screen
*psc
, __GLXDRIdrawable
*pdraw
,
421 int64_t *ust
, int64_t *msc
, int64_t *sbc
)
423 return dri3_wait_for_msc(pdraw
, 0, 0, 0, ust
, msc
,sbc
);
426 /** dri3_wait_for_sbc
428 * Wait for the completed swap buffer count to reach the specified
429 * target. Presumably the application knows that this will be reached with
430 * outstanding complete events, or we're going to be here awhile.
433 dri3_wait_for_sbc(__GLXDRIdrawable
*pdraw
, int64_t target_sbc
, int64_t *ust
,
434 int64_t *msc
, int64_t *sbc
)
436 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
438 return loader_dri3_wait_for_sbc(&priv
->loader_drawable
, target_sbc
,
443 dri3_copy_sub_buffer(__GLXDRIdrawable
*pdraw
, int x
, int y
,
444 int width
, int height
,
447 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
449 loader_dri3_copy_sub_buffer(&priv
->loader_drawable
, x
, y
,
450 width
, height
, flush
);
454 dri3_wait_x(struct glx_context
*gc
)
456 struct dri3_drawable
*priv
= (struct dri3_drawable
*)
457 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
460 loader_dri3_wait_x(&priv
->loader_drawable
);
464 dri3_wait_gl(struct glx_context
*gc
)
466 struct dri3_drawable
*priv
= (struct dri3_drawable
*)
467 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
470 loader_dri3_wait_gl(&priv
->loader_drawable
);
474 * Called by the driver when it needs to update the real front buffer with the
475 * contents of its fake front buffer.
478 dri3_flush_front_buffer(__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
);
494 loader_dri3_flush(draw
, __DRI2_FLUSH_DRAWABLE
, __DRI2_THROTTLE_FLUSHFRONT
);
496 loader_dri3_wait_gl(draw
);
500 dri_set_background_context(void *loaderPrivate
)
502 struct dri3_context
*pcp
= (struct dri3_context
*)loaderPrivate
;
503 __glXSetCurrentContext(&pcp
->base
);
506 /* The image loader extension record for DRI3
508 static const __DRIimageLoaderExtension imageLoaderExtension
= {
509 .base
= { __DRI_IMAGE_LOADER
, 1 },
511 .getBuffers
= loader_dri3_get_buffers
,
512 .flushFrontBuffer
= dri3_flush_front_buffer
,
515 const __DRIuseInvalidateExtension dri3UseInvalidate
= {
516 .base
= { __DRI_USE_INVALIDATE
, 1 }
519 static const __DRIbackgroundCallableExtension driBackgroundCallable
= {
520 .base
= { __DRI_BACKGROUND_CALLABLE
, 1 },
522 .setBackgroundContext
= dri_set_background_context
,
525 static const __DRIextension
*loader_extensions
[] = {
526 &imageLoaderExtension
.base
,
527 &dri3UseInvalidate
.base
,
528 &driBackgroundCallable
.base
,
532 /** dri3_swap_buffers
534 * Make the current back buffer visible using the present extension
537 dri3_swap_buffers(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
538 int64_t remainder
, Bool flush
)
540 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
541 unsigned flags
= __DRI2_FLUSH_DRAWABLE
;
544 flags
|= __DRI2_FLUSH_CONTEXT
;
546 return loader_dri3_swap_buffers_msc(&priv
->loader_drawable
,
547 target_msc
, divisor
, remainder
,
552 dri3_get_buffer_age(__GLXDRIdrawable
*pdraw
)
554 struct dri3_drawable
*priv
= (struct dri3_drawable
*)pdraw
;
556 return loader_dri3_query_buffer_age(&priv
->loader_drawable
);
559 /** dri3_destroy_screen
562 dri3_destroy_screen(struct glx_screen
*base
)
564 struct dri3_screen
*psc
= (struct dri3_screen
*) base
;
566 /* Free the direct rendering per screen data */
567 (*psc
->core
->destroyScreen
) (psc
->driScreen
);
568 driDestroyConfigs(psc
->driver_configs
);
573 /** dri3_set_swap_interval
575 * Record the application swap interval specification,
578 dri3_set_swap_interval(__GLXDRIdrawable
*pdraw
, int interval
)
580 assert(pdraw
!= NULL
);
582 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
583 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
584 struct dri3_screen
*psc
= (struct dri3_screen
*) priv
->base
.psc
;
587 psc
->config
->configQueryi(psc
->driScreen
,
588 "vblank_mode", &vblank_mode
);
590 switch (vblank_mode
) {
591 case DRI_CONF_VBLANK_NEVER
:
593 return GLX_BAD_VALUE
;
595 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
597 return GLX_BAD_VALUE
;
603 loader_dri3_set_swap_interval(&priv
->loader_drawable
, interval
);
608 /** dri3_get_swap_interval
610 * Return the stored swap interval
613 dri3_get_swap_interval(__GLXDRIdrawable
*pdraw
)
615 assert(pdraw
!= NULL
);
617 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
619 return priv
->swap_interval
;
623 dri3_bind_tex_image(Display
* dpy
,
624 GLXDrawable drawable
,
625 int buffer
, const int *attrib_list
)
627 struct glx_context
*gc
= __glXGetCurrentContext();
628 struct dri3_context
*pcp
= (struct dri3_context
*) gc
;
629 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
630 struct dri3_drawable
*pdraw
= (struct dri3_drawable
*) base
;
631 struct dri3_screen
*psc
;
634 psc
= (struct dri3_screen
*) base
->psc
;
636 (*psc
->f
->invalidate
)(pdraw
->loader_drawable
.dri_drawable
);
640 (*psc
->texBuffer
->setTexBuffer2
) (pcp
->driContext
,
641 pdraw
->base
.textureTarget
,
642 pdraw
->base
.textureFormat
,
643 pdraw
->loader_drawable
.dri_drawable
);
648 dri3_release_tex_image(Display
* dpy
, GLXDrawable drawable
, int buffer
)
650 struct glx_context
*gc
= __glXGetCurrentContext();
651 struct dri3_context
*pcp
= (struct dri3_context
*) gc
;
652 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
653 struct dri3_drawable
*pdraw
= (struct dri3_drawable
*) base
;
654 struct dri3_screen
*psc
;
657 psc
= (struct dri3_screen
*) base
->psc
;
659 if (psc
->texBuffer
->base
.version
>= 3 &&
660 psc
->texBuffer
->releaseTexBuffer
!= NULL
)
661 (*psc
->texBuffer
->releaseTexBuffer
) (pcp
->driContext
,
662 pdraw
->base
.textureTarget
,
663 pdraw
->loader_drawable
.dri_drawable
);
667 static const struct glx_context_vtable dri3_context_vtable
= {
668 .destroy
= dri3_destroy_context
,
669 .bind
= dri3_bind_context
,
670 .unbind
= dri3_unbind_context
,
671 .wait_gl
= dri3_wait_gl
,
672 .wait_x
= dri3_wait_x
,
673 .use_x_font
= DRI_glXUseXFont
,
674 .bind_tex_image
= dri3_bind_tex_image
,
675 .release_tex_image
= dri3_release_tex_image
,
676 .get_proc_address
= NULL
,
677 .interop_query_device_info
= dri3_interop_query_device_info
,
678 .interop_export_object
= dri3_interop_export_object
681 /** dri3_bind_extensions
683 * Enable all of the extensions supported on DRI3
686 dri3_bind_extensions(struct dri3_screen
*psc
, struct glx_display
* priv
,
687 const char *driverName
)
689 const __DRIextension
**extensions
;
693 extensions
= psc
->core
->getExtensions(psc
->driScreen
);
695 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_video_sync");
696 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_swap_control");
697 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_swap_control");
698 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_make_current_read");
699 __glXEnableDirectExtension(&psc
->base
, "GLX_INTEL_swap_event");
701 mask
= psc
->image_driver
->getAPIMask(psc
->driScreen
);
703 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context");
704 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context_profile");
706 if ((mask
& ((1 << __DRI_API_GLES
) |
707 (1 << __DRI_API_GLES2
) |
708 (1 << __DRI_API_GLES3
))) != 0) {
709 __glXEnableDirectExtension(&psc
->base
,
710 "GLX_EXT_create_context_es_profile");
711 __glXEnableDirectExtension(&psc
->base
,
712 "GLX_EXT_create_context_es2_profile");
715 for (i
= 0; extensions
[i
]; i
++) {
716 /* when on a different gpu than the server, the server pixmaps
717 * can have a tiling mode we can't read. Thus we can't create
718 * a texture from them.
720 if (!psc
->is_different_gpu
&&
721 (strcmp(extensions
[i
]->name
, __DRI_TEX_BUFFER
) == 0)) {
722 psc
->texBuffer
= (__DRItexBufferExtension
*) extensions
[i
];
723 __glXEnableDirectExtension(&psc
->base
, "GLX_EXT_texture_from_pixmap");
726 if ((strcmp(extensions
[i
]->name
, __DRI2_FLUSH
) == 0)) {
727 psc
->f
= (__DRI2flushExtension
*) extensions
[i
];
728 /* internal driver extension, no GL extension exposed */
731 if (strcmp(extensions
[i
]->name
, __DRI_IMAGE
) == 0)
732 psc
->image
= (__DRIimageExtension
*) extensions
[i
];
734 if ((strcmp(extensions
[i
]->name
, __DRI2_CONFIG_QUERY
) == 0))
735 psc
->config
= (__DRI2configQueryExtension
*) extensions
[i
];
737 if (strcmp(extensions
[i
]->name
, __DRI2_ROBUSTNESS
) == 0)
738 __glXEnableDirectExtension(&psc
->base
,
739 "GLX_ARB_create_context_robustness");
741 if (strcmp(extensions
[i
]->name
, __DRI2_RENDERER_QUERY
) == 0) {
742 psc
->rendererQuery
= (__DRI2rendererQueryExtension
*) extensions
[i
];
743 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_query_renderer");
746 if (strcmp(extensions
[i
]->name
, __DRI2_INTEROP
) == 0)
747 psc
->interop
= (__DRI2interopExtension
*)extensions
[i
];
751 static const struct glx_screen_vtable dri3_screen_vtable
= {
752 .create_context
= dri3_create_context
,
753 .create_context_attribs
= dri3_create_context_attribs
,
754 .query_renderer_integer
= dri3_query_renderer_integer
,
755 .query_renderer_string
= dri3_query_renderer_string
,
758 /** dri3_create_screen
760 * Initialize DRI3 on the specified screen.
762 * Opens the DRI device, locates the appropriate DRI driver
765 * Checks to see if the driver supports the necessary extensions
767 * Initializes the driver for the screen and sets up our structures
770 static struct glx_screen
*
771 dri3_create_screen(int screen
, struct glx_display
* priv
)
773 xcb_connection_t
*c
= XGetXCBConnection(priv
->dpy
);
774 const __DRIconfig
**driver_configs
;
775 const __DRIextension
**extensions
;
776 const struct dri3_display
*const pdp
= (struct dri3_display
*)
778 struct dri3_screen
*psc
;
780 struct glx_config
*configs
= NULL
, *visuals
= NULL
;
781 char *driverName
, *deviceName
, *tmp
;
784 psc
= calloc(1, sizeof *psc
);
790 if (!glx_screen_init(&psc
->base
, screen
, priv
)) {
795 psc
->fd
= loader_dri3_open(c
, RootWindow(priv
->dpy
, screen
), None
);
797 int conn_error
= xcb_connection_has_error(c
);
799 glx_screen_cleanup(&psc
->base
);
801 InfoMessageF("screen %d does not appear to be DRI3 capable\n", screen
);
804 ErrorMessageF("Connection closed during DRI3 initialization failure");
809 psc
->fd
= loader_get_user_preferred_fd(psc
->fd
, &psc
->is_different_gpu
);
812 driverName
= loader_get_driver_for_fd(psc
->fd
);
814 ErrorMessageF("No driver found\n");
818 psc
->driver
= driOpenDriver(driverName
);
819 if (psc
->driver
== NULL
) {
820 ErrorMessageF("driver pointer missing\n");
824 extensions
= driGetDriverExtensions(psc
->driver
, driverName
);
825 if (extensions
== NULL
)
828 for (i
= 0; extensions
[i
]; i
++) {
829 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
830 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
831 if (strcmp(extensions
[i
]->name
, __DRI_IMAGE_DRIVER
) == 0)
832 psc
->image_driver
= (__DRIimageDriverExtension
*) extensions
[i
];
836 if (psc
->core
== NULL
) {
837 ErrorMessageF("core dri driver extension not found\n");
841 if (psc
->image_driver
== NULL
) {
842 ErrorMessageF("image driver extension not found\n");
847 psc
->image_driver
->createNewScreen2(screen
, psc
->fd
,
848 pdp
->loader_extensions
,
850 &driver_configs
, psc
);
852 if (psc
->driScreen
== NULL
) {
853 ErrorMessageF("failed to create dri screen\n");
857 dri3_bind_extensions(psc
, priv
, driverName
);
859 if (!psc
->image
|| psc
->image
->base
.version
< 7 || !psc
->image
->createImageFromFds
) {
860 ErrorMessageF("Version 7 or imageFromFds image extension not found\n");
864 if (!psc
->f
|| psc
->f
->base
.version
< 4) {
865 ErrorMessageF("Version 4 or later of flush extension not found\n");
869 if (psc
->is_different_gpu
&& psc
->image
->base
.version
< 9) {
870 ErrorMessageF("Different GPU, but image extension version 9 or later not found\n");
874 if (psc
->is_different_gpu
&& !psc
->image
->blitImage
) {
875 ErrorMessageF("Different GPU, but blitImage not implemented for this driver\n");
879 if (!psc
->is_different_gpu
&& (
880 !psc
->texBuffer
|| psc
->texBuffer
->base
.version
< 2 ||
881 !psc
->texBuffer
->setTexBuffer2
883 ErrorMessageF("Version 2 or later of texBuffer extension not found\n");
887 psc
->loader_dri3_ext
.core
= psc
->core
;
888 psc
->loader_dri3_ext
.image_driver
= psc
->image_driver
;
889 psc
->loader_dri3_ext
.flush
= psc
->f
;
890 psc
->loader_dri3_ext
.tex_buffer
= psc
->texBuffer
;
891 psc
->loader_dri3_ext
.image
= psc
->image
;
892 psc
->loader_dri3_ext
.config
= psc
->config
;
894 configs
= driConvertConfigs(psc
->core
, psc
->base
.configs
, driver_configs
);
895 visuals
= driConvertConfigs(psc
->core
, psc
->base
.visuals
, driver_configs
);
897 if (!configs
|| !visuals
) {
898 ErrorMessageF("No matching fbConfigs or visuals found\n");
902 glx_config_destroy_list(psc
->base
.configs
);
903 psc
->base
.configs
= configs
;
904 glx_config_destroy_list(psc
->base
.visuals
);
905 psc
->base
.visuals
= visuals
;
907 psc
->driver_configs
= driver_configs
;
909 psc
->base
.vtable
= &dri3_screen_vtable
;
911 psc
->base
.driScreen
= psp
;
912 psp
->destroyScreen
= dri3_destroy_screen
;
913 psp
->createDrawable
= dri3_create_drawable
;
914 psp
->swapBuffers
= dri3_swap_buffers
;
916 psp
->getDrawableMSC
= dri3_drawable_get_msc
;
917 psp
->waitForMSC
= dri3_wait_for_msc
;
918 psp
->waitForSBC
= dri3_wait_for_sbc
;
919 psp
->setSwapInterval
= dri3_set_swap_interval
;
920 psp
->getSwapInterval
= dri3_get_swap_interval
;
921 __glXEnableDirectExtension(&psc
->base
, "GLX_OML_sync_control");
923 psp
->copySubBuffer
= dri3_copy_sub_buffer
;
924 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_copy_sub_buffer");
926 psp
->getBufferAge
= dri3_get_buffer_age
;
927 __glXEnableDirectExtension(&psc
->base
, "GLX_EXT_buffer_age");
932 tmp
= getenv("LIBGL_SHOW_FPS");
933 psc
->show_fps_interval
= tmp
? atoi(tmp
) : 0;
934 if (psc
->show_fps_interval
< 0)
935 psc
->show_fps_interval
= 0;
937 InfoMessageF("Using DRI3 for screen %d\n", screen
);
942 CriticalErrorMessageF("failed to load driver: %s\n", driverName
);
945 glx_config_destroy_list(configs
);
947 glx_config_destroy_list(visuals
);
949 psc
->core
->destroyScreen(psc
->driScreen
);
950 psc
->driScreen
= NULL
;
954 dlclose(psc
->driver
);
958 glx_screen_cleanup(&psc
->base
);
964 /** dri_destroy_display
966 * Called from __glXFreeDisplayPrivate.
969 dri3_destroy_display(__GLXDRIdisplay
* dpy
)
974 /** dri3_create_display
976 * Allocate, initialize and return a __DRIdisplayPrivate object.
977 * This is called from __glXInitialize() when we are given a new
978 * display pointer. This is public to that function, but hidden from
981 _X_HIDDEN __GLXDRIdisplay
*
982 dri3_create_display(Display
* dpy
)
984 struct dri3_display
*pdp
;
985 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
986 xcb_dri3_query_version_cookie_t dri3_cookie
;
987 xcb_dri3_query_version_reply_t
*dri3_reply
;
988 xcb_present_query_version_cookie_t present_cookie
;
989 xcb_present_query_version_reply_t
*present_reply
;
990 xcb_generic_error_t
*error
;
991 const xcb_query_extension_reply_t
*extension
;
993 xcb_prefetch_extension_data(c
, &xcb_dri3_id
);
994 xcb_prefetch_extension_data(c
, &xcb_present_id
);
996 extension
= xcb_get_extension_data(c
, &xcb_dri3_id
);
997 if (!(extension
&& extension
->present
))
1000 extension
= xcb_get_extension_data(c
, &xcb_present_id
);
1001 if (!(extension
&& extension
->present
))
1004 dri3_cookie
= xcb_dri3_query_version(c
,
1005 XCB_DRI3_MAJOR_VERSION
,
1006 XCB_DRI3_MINOR_VERSION
);
1009 present_cookie
= xcb_present_query_version(c
,
1010 XCB_PRESENT_MAJOR_VERSION
,
1011 XCB_PRESENT_MINOR_VERSION
);
1013 pdp
= malloc(sizeof *pdp
);
1017 dri3_reply
= xcb_dri3_query_version_reply(c
, dri3_cookie
, &error
);
1023 pdp
->dri3Major
= dri3_reply
->major_version
;
1024 pdp
->dri3Minor
= dri3_reply
->minor_version
;
1027 present_reply
= xcb_present_query_version_reply(c
, present_cookie
, &error
);
1028 if (!present_reply
) {
1032 pdp
->presentMajor
= present_reply
->major_version
;
1033 pdp
->presentMinor
= present_reply
->minor_version
;
1034 free(present_reply
);
1036 pdp
->base
.destroyDisplay
= dri3_destroy_display
;
1037 pdp
->base
.createScreen
= dri3_create_screen
;
1039 loader_set_logger(dri_message
);
1041 pdp
->loader_extensions
= loader_extensions
;
1049 #endif /* GLX_DIRECT_RENDERING */