glx: Remove an old DEFAULT_DRIVER_DIR default.
[mesa.git] / src / glx / dri3_glx.c
1 /*
2 * Copyright © 2013 Keith Packard
3 *
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.
13 *
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
20 * OF THIS SOFTWARE.
21 */
22
23 /*
24 * Portions of this code were adapted from dri2_glx.c which carries the
25 * following copyright:
26 *
27 * Copyright © 2008 Red Hat, Inc.
28 *
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.
38 *
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.
48 *
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.
53 *
54 * Authors:
55 * Kristian Høgsberg (krh@redhat.com)
56 */
57
58 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
59
60 #include <X11/Xlib.h>
61 #include <X11/extensions/Xfixes.h>
62 #include <X11/Xlib-xcb.h>
63 #include <X11/xshmfence.h>
64 #include <xcb/xcb.h>
65 #include <xcb/dri3.h>
66 #include <xcb/present.h>
67 #include <GL/gl.h>
68 #include "glxclient.h"
69 #include <dlfcn.h>
70 #include <fcntl.h>
71 #include <unistd.h>
72 #include <sys/types.h>
73 #include <sys/mman.h>
74 #include <sys/time.h>
75
76 #include "dri_common.h"
77 #include "dri3_priv.h"
78 #include "loader.h"
79 #include "dri2.h"
80
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 if (!draw)
85 return NULL;
86 return (struct dri3_drawable *)(((void*) draw) - offset);
87 }
88
89 static void
90 glx_dri3_set_drawable_size(struct loader_dri3_drawable *draw,
91 int width, int height)
92 {
93 /* Nothing to do */
94 }
95
96 static bool
97 glx_dri3_in_current_context(struct loader_dri3_drawable *draw)
98 {
99 struct dri3_drawable *priv = loader_drawable_to_dri3_drawable(draw);
100
101 if (!priv)
102 return false;
103
104 struct dri3_context *pcp = (struct dri3_context *) __glXGetCurrentContext();
105 struct dri3_screen *psc = (struct dri3_screen *) priv->base.psc;
106
107 return (&pcp->base != &dummyContext) && pcp->base.psc == &psc->base;
108 }
109
110 static __DRIcontext *
111 glx_dri3_get_dri_context(struct loader_dri3_drawable *draw)
112 {
113 struct glx_context *gc = __glXGetCurrentContext();
114 struct dri3_context *dri3Ctx = (struct dri3_context *) gc;
115
116 return (gc != &dummyContext) ? dri3Ctx->driContext : NULL;
117 }
118
119 static __DRIscreen *
120 glx_dri3_get_dri_screen(void)
121 {
122 struct glx_context *gc = __glXGetCurrentContext();
123 struct dri3_context *pcp = (struct dri3_context *) gc;
124 struct dri3_screen *psc = (struct dri3_screen *) pcp->base.psc;
125
126 return (gc != &dummyContext && psc) ? psc->driScreen : NULL;
127 }
128
129 static void
130 glx_dri3_flush_drawable(struct loader_dri3_drawable *draw, unsigned flags)
131 {
132 loader_dri3_flush(draw, flags, __DRI2_THROTTLE_SWAPBUFFER);
133 }
134
135 static void
136 glx_dri3_show_fps(struct loader_dri3_drawable *draw, uint64_t current_ust)
137 {
138 struct dri3_drawable *priv = loader_drawable_to_dri3_drawable(draw);
139 const uint64_t interval =
140 ((struct dri3_screen *) priv->base.psc)->show_fps_interval;
141
142 if (!interval)
143 return;
144
145 priv->frames++;
146
147 /* DRI3+Present together uses microseconds for UST. */
148 if (priv->previous_ust + interval * 1000000 <= current_ust) {
149 if (priv->previous_ust) {
150 fprintf(stderr, "libGL: FPS = %.1f\n",
151 ((uint64_t) priv->frames * 1000000) /
152 (double)(current_ust - priv->previous_ust));
153 }
154 priv->frames = 0;
155 priv->previous_ust = current_ust;
156 }
157 }
158
159 static const struct loader_dri3_vtable glx_dri3_vtable = {
160 .set_drawable_size = glx_dri3_set_drawable_size,
161 .in_current_context = glx_dri3_in_current_context,
162 .get_dri_context = glx_dri3_get_dri_context,
163 .get_dri_screen = glx_dri3_get_dri_screen,
164 .flush_drawable = glx_dri3_flush_drawable,
165 .show_fps = glx_dri3_show_fps,
166 };
167
168
169 static const struct glx_context_vtable dri3_context_vtable;
170
171 static void
172 dri3_destroy_context(struct glx_context *context)
173 {
174 struct dri3_context *pcp = (struct dri3_context *) context;
175 struct dri3_screen *psc = (struct dri3_screen *) context->psc;
176
177 driReleaseDrawables(&pcp->base);
178
179 free((char *) context->extensions);
180
181 (*psc->core->destroyContext) (pcp->driContext);
182
183 free(pcp);
184 }
185
186 static Bool
187 dri3_bind_context(struct glx_context *context, struct glx_context *old,
188 GLXDrawable draw, GLXDrawable read)
189 {
190 struct dri3_context *pcp = (struct dri3_context *) context;
191 struct dri3_screen *psc = (struct dri3_screen *) pcp->base.psc;
192 struct dri3_drawable *pdraw, *pread;
193 __DRIdrawable *dri_draw = NULL, *dri_read = NULL;
194
195 pdraw = (struct dri3_drawable *) driFetchDrawable(context, draw);
196 pread = (struct dri3_drawable *) driFetchDrawable(context, read);
197
198 driReleaseDrawables(&pcp->base);
199
200 if (pdraw)
201 dri_draw = pdraw->loader_drawable.dri_drawable;
202 else if (draw != None)
203 return GLXBadDrawable;
204
205 if (pread)
206 dri_read = pread->loader_drawable.dri_drawable;
207 else if (read != None)
208 return GLXBadDrawable;
209
210 if (!(*psc->core->bindContext) (pcp->driContext, dri_draw, dri_read))
211 return GLXBadContext;
212
213 if (dri_draw)
214 (*psc->f->invalidate)(dri_draw);
215 if (dri_read && dri_read != dri_draw)
216 (*psc->f->invalidate)(dri_read);
217
218 return Success;
219 }
220
221 static void
222 dri3_unbind_context(struct glx_context *context, struct glx_context *new)
223 {
224 struct dri3_context *pcp = (struct dri3_context *) context;
225 struct dri3_screen *psc = (struct dri3_screen *) pcp->base.psc;
226
227 (*psc->core->unbindContext) (pcp->driContext);
228 }
229
230 static struct glx_context *
231 dri3_create_context_attribs(struct glx_screen *base,
232 struct glx_config *config_base,
233 struct glx_context *shareList,
234 unsigned num_attribs,
235 const uint32_t *attribs,
236 unsigned *error)
237 {
238 struct dri3_context *pcp = NULL;
239 struct dri3_context *pcp_shared = NULL;
240 struct dri3_screen *psc = (struct dri3_screen *) base;
241 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
242 __DRIcontext *shared = NULL;
243
244 uint32_t minor_ver = 1;
245 uint32_t major_ver = 2;
246 uint32_t flags = 0;
247 unsigned api;
248 int reset = __DRI_CTX_RESET_NO_NOTIFICATION;
249 int release = __DRI_CTX_RELEASE_BEHAVIOR_FLUSH;
250 uint32_t ctx_attribs[2 * 6];
251 unsigned num_ctx_attribs = 0;
252 uint32_t render_type;
253
254 /* Remap the GLX tokens to DRI2 tokens.
255 */
256 if (!dri2_convert_glx_attribs(num_attribs, attribs,
257 &major_ver, &minor_ver,
258 &render_type, &flags, &api,
259 &reset, &release, error))
260 goto error_exit;
261
262 /* Check the renderType value */
263 if (!validate_renderType_against_config(config_base, render_type))
264 goto error_exit;
265
266 if (shareList) {
267 pcp_shared = (struct dri3_context *) shareList;
268 shared = pcp_shared->driContext;
269 }
270
271 pcp = calloc(1, sizeof *pcp);
272 if (pcp == NULL) {
273 *error = __DRI_CTX_ERROR_NO_MEMORY;
274 goto error_exit;
275 }
276
277 if (!glx_context_init(&pcp->base, &psc->base, config_base))
278 goto error_exit;
279
280 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
281 ctx_attribs[num_ctx_attribs++] = major_ver;
282 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
283 ctx_attribs[num_ctx_attribs++] = minor_ver;
284
285 /* Only send a value when the non-default value is requested. By doing
286 * this we don't have to check the driver's DRI3 version before sending the
287 * default value.
288 */
289 if (reset != __DRI_CTX_RESET_NO_NOTIFICATION) {
290 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
291 ctx_attribs[num_ctx_attribs++] = reset;
292 }
293
294 if (release != __DRI_CTX_RELEASE_BEHAVIOR_FLUSH) {
295 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_RELEASE_BEHAVIOR;
296 ctx_attribs[num_ctx_attribs++] = release;
297 }
298
299 if (flags != 0) {
300 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_FLAGS;
301
302 /* The current __DRI_CTX_FLAG_* values are identical to the
303 * GLX_CONTEXT_*_BIT values.
304 */
305 ctx_attribs[num_ctx_attribs++] = flags;
306 }
307
308 pcp->driContext =
309 (*psc->image_driver->createContextAttribs) (psc->driScreen,
310 api,
311 config ? config->driConfig
312 : NULL,
313 shared,
314 num_ctx_attribs / 2,
315 ctx_attribs,
316 error,
317 pcp);
318
319 if (pcp->driContext == NULL)
320 goto error_exit;
321
322 pcp->base.vtable = &dri3_context_vtable;
323
324 return &pcp->base;
325
326 error_exit:
327 free(pcp);
328
329 return NULL;
330 }
331
332 static struct glx_context *
333 dri3_create_context(struct glx_screen *base,
334 struct glx_config *config_base,
335 struct glx_context *shareList, int renderType)
336 {
337 unsigned int error;
338 uint32_t attribs[2] = { GLX_RENDER_TYPE, renderType };
339
340 return dri3_create_context_attribs(base, config_base, shareList,
341 1, attribs, &error);
342 }
343
344 static void
345 dri3_destroy_drawable(__GLXDRIdrawable *base)
346 {
347 struct dri3_drawable *pdraw = (struct dri3_drawable *) base;
348
349 loader_dri3_drawable_fini(&pdraw->loader_drawable);
350
351 free(pdraw);
352 }
353
354 static __GLXDRIdrawable *
355 dri3_create_drawable(struct glx_screen *base, XID xDrawable,
356 GLXDrawable drawable, struct glx_config *config_base)
357 {
358 struct dri3_drawable *pdraw;
359 struct dri3_screen *psc = (struct dri3_screen *) base;
360 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
361 bool has_multibuffer = false;
362 #ifdef HAVE_DRI3_MODIFIERS
363 const struct dri3_display *const pdp = (struct dri3_display *)
364 base->display->dri3Display;
365 #endif
366
367 pdraw = calloc(1, sizeof(*pdraw));
368 if (!pdraw)
369 return NULL;
370
371 pdraw->base.destroyDrawable = dri3_destroy_drawable;
372 pdraw->base.xDrawable = xDrawable;
373 pdraw->base.drawable = drawable;
374 pdraw->base.psc = &psc->base;
375
376 #ifdef HAVE_DRI3_MODIFIERS
377 if ((psc->image && psc->image->base.version >= 15) &&
378 (pdp->dri3Major > 1 || (pdp->dri3Major == 1 && pdp->dri3Minor >= 2)) &&
379 (pdp->presentMajor > 1 ||
380 (pdp->presentMajor == 1 && pdp->presentMinor >= 2)))
381 has_multibuffer = true;
382 #endif
383
384 (void) __glXInitialize(psc->base.dpy);
385
386 if (loader_dri3_drawable_init(XGetXCBConnection(base->dpy),
387 xDrawable, psc->driScreen,
388 psc->is_different_gpu, has_multibuffer,
389 config->driConfig,
390 &psc->loader_dri3_ext, &glx_dri3_vtable,
391 &pdraw->loader_drawable)) {
392 free(pdraw);
393 return NULL;
394 }
395
396 return &pdraw->base;
397 }
398
399 /** dri3_wait_for_msc
400 *
401 * Get the X server to send an event when the target msc/divisor/remainder is
402 * reached.
403 */
404 static int
405 dri3_wait_for_msc(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
406 int64_t remainder, int64_t *ust, int64_t *msc, int64_t *sbc)
407 {
408 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
409
410 loader_dri3_wait_for_msc(&priv->loader_drawable, target_msc, divisor,
411 remainder, ust, msc, sbc);
412
413 return 1;
414 }
415
416 /** dri3_drawable_get_msc
417 *
418 * Return the current UST/MSC/SBC triplet by asking the server
419 * for an event
420 */
421 static int
422 dri3_drawable_get_msc(struct glx_screen *psc, __GLXDRIdrawable *pdraw,
423 int64_t *ust, int64_t *msc, int64_t *sbc)
424 {
425 return dri3_wait_for_msc(pdraw, 0, 0, 0, ust, msc,sbc);
426 }
427
428 /** dri3_wait_for_sbc
429 *
430 * Wait for the completed swap buffer count to reach the specified
431 * target. Presumably the application knows that this will be reached with
432 * outstanding complete events, or we're going to be here awhile.
433 */
434 static int
435 dri3_wait_for_sbc(__GLXDRIdrawable *pdraw, int64_t target_sbc, int64_t *ust,
436 int64_t *msc, int64_t *sbc)
437 {
438 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
439
440 return loader_dri3_wait_for_sbc(&priv->loader_drawable, target_sbc,
441 ust, msc, sbc);
442 }
443
444 static void
445 dri3_copy_sub_buffer(__GLXDRIdrawable *pdraw, int x, int y,
446 int width, int height,
447 Bool flush)
448 {
449 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
450
451 loader_dri3_copy_sub_buffer(&priv->loader_drawable, x, y,
452 width, height, flush);
453 }
454
455 static void
456 dri3_wait_x(struct glx_context *gc)
457 {
458 struct dri3_drawable *priv = (struct dri3_drawable *)
459 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
460
461 if (priv)
462 loader_dri3_wait_x(&priv->loader_drawable);
463 }
464
465 static void
466 dri3_wait_gl(struct glx_context *gc)
467 {
468 struct dri3_drawable *priv = (struct dri3_drawable *)
469 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
470
471 if (priv)
472 loader_dri3_wait_gl(&priv->loader_drawable);
473 }
474
475 /**
476 * Called by the driver when it needs to update the real front buffer with the
477 * contents of its fake front buffer.
478 */
479 static void
480 dri3_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)
481 {
482 struct loader_dri3_drawable *draw = loaderPrivate;
483 struct dri3_drawable *pdraw = loader_drawable_to_dri3_drawable(draw);
484 struct dri3_screen *psc;
485
486 if (!pdraw)
487 return;
488
489 if (!pdraw->base.psc)
490 return;
491
492 psc = (struct dri3_screen *) pdraw->base.psc;
493
494 (void) __glXInitialize(psc->base.dpy);
495
496 loader_dri3_flush(draw, __DRI2_FLUSH_DRAWABLE, __DRI2_THROTTLE_FLUSHFRONT);
497
498 (*psc->f->invalidate)(driDrawable);
499 loader_dri3_wait_gl(draw);
500 }
501
502 /**
503 * Make sure all pending swapbuffers have been submitted to hardware
504 *
505 * \param driDrawable[in] Pointer to the dri drawable whose swaps we are
506 * flushing.
507 * \param loaderPrivate[in] Pointer to the corresponding struct
508 * loader_dri_drawable.
509 */
510 static void
511 dri3_flush_swap_buffers(__DRIdrawable *driDrawable, void *loaderPrivate)
512 {
513 struct loader_dri3_drawable *draw = loaderPrivate;
514 struct dri3_drawable *pdraw = loader_drawable_to_dri3_drawable(draw);
515 struct dri3_screen *psc;
516
517 if (!pdraw)
518 return;
519
520 if (!pdraw->base.psc)
521 return;
522
523 psc = (struct dri3_screen *) pdraw->base.psc;
524
525 (void) __glXInitialize(psc->base.dpy);
526 loader_dri3_swapbuffer_barrier(draw);
527 }
528
529 static void
530 dri_set_background_context(void *loaderPrivate)
531 {
532 struct dri3_context *pcp = (struct dri3_context *)loaderPrivate;
533 __glXSetCurrentContext(&pcp->base);
534 }
535
536 static GLboolean
537 dri_is_thread_safe(void *loaderPrivate)
538 {
539 /* Unlike DRI2, DRI3 doesn't call GetBuffers/GetBuffersWithFormat
540 * during draw so we're safe here.
541 */
542 return true;
543 }
544
545 /* The image loader extension record for DRI3
546 */
547 static const __DRIimageLoaderExtension imageLoaderExtension = {
548 .base = { __DRI_IMAGE_LOADER, 3 },
549
550 .getBuffers = loader_dri3_get_buffers,
551 .flushFrontBuffer = dri3_flush_front_buffer,
552 .flushSwapBuffers = dri3_flush_swap_buffers,
553 };
554
555 const __DRIuseInvalidateExtension dri3UseInvalidate = {
556 .base = { __DRI_USE_INVALIDATE, 1 }
557 };
558
559 static const __DRIbackgroundCallableExtension driBackgroundCallable = {
560 .base = { __DRI_BACKGROUND_CALLABLE, 2 },
561
562 .setBackgroundContext = dri_set_background_context,
563 .isThreadSafe = dri_is_thread_safe,
564 };
565
566 static const __DRIextension *loader_extensions[] = {
567 &imageLoaderExtension.base,
568 &dri3UseInvalidate.base,
569 &driBackgroundCallable.base,
570 NULL
571 };
572
573 /** dri3_swap_buffers
574 *
575 * Make the current back buffer visible using the present extension
576 */
577 static int64_t
578 dri3_swap_buffers(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
579 int64_t remainder, Bool flush)
580 {
581 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
582 unsigned flags = __DRI2_FLUSH_DRAWABLE;
583
584 if (flush)
585 flags |= __DRI2_FLUSH_CONTEXT;
586
587 return loader_dri3_swap_buffers_msc(&priv->loader_drawable,
588 target_msc, divisor, remainder,
589 flags, false);
590 }
591
592 static int
593 dri3_get_buffer_age(__GLXDRIdrawable *pdraw)
594 {
595 struct dri3_drawable *priv = (struct dri3_drawable *)pdraw;
596
597 return loader_dri3_query_buffer_age(&priv->loader_drawable);
598 }
599
600 /** dri3_destroy_screen
601 */
602 static void
603 dri3_destroy_screen(struct glx_screen *base)
604 {
605 struct dri3_screen *psc = (struct dri3_screen *) base;
606
607 /* Free the direct rendering per screen data */
608 loader_dri3_close_screen(psc->driScreen);
609 (*psc->core->destroyScreen) (psc->driScreen);
610 driDestroyConfigs(psc->driver_configs);
611 close(psc->fd);
612 free(psc);
613 }
614
615 /** dri3_set_swap_interval
616 *
617 * Record the application swap interval specification,
618 */
619 static int
620 dri3_set_swap_interval(__GLXDRIdrawable *pdraw, int interval)
621 {
622 assert(pdraw != NULL);
623
624 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
625 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
626 struct dri3_screen *psc = (struct dri3_screen *) priv->base.psc;
627
628 if (psc->config)
629 psc->config->configQueryi(psc->driScreen,
630 "vblank_mode", &vblank_mode);
631
632 switch (vblank_mode) {
633 case DRI_CONF_VBLANK_NEVER:
634 if (interval != 0)
635 return GLX_BAD_VALUE;
636 break;
637 case DRI_CONF_VBLANK_ALWAYS_SYNC:
638 if (interval <= 0)
639 return GLX_BAD_VALUE;
640 break;
641 default:
642 break;
643 }
644
645 priv->swap_interval = interval;
646 loader_dri3_set_swap_interval(&priv->loader_drawable, interval);
647
648 return 0;
649 }
650
651 /** dri3_get_swap_interval
652 *
653 * Return the stored swap interval
654 */
655 static int
656 dri3_get_swap_interval(__GLXDRIdrawable *pdraw)
657 {
658 assert(pdraw != NULL);
659
660 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
661
662 return priv->swap_interval;
663 }
664
665 static void
666 dri3_bind_tex_image(Display * dpy,
667 GLXDrawable drawable,
668 int buffer, const int *attrib_list)
669 {
670 struct glx_context *gc = __glXGetCurrentContext();
671 struct dri3_context *pcp = (struct dri3_context *) gc;
672 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
673 struct dri3_drawable *pdraw = (struct dri3_drawable *) base;
674 struct dri3_screen *psc;
675
676 if (pdraw != NULL) {
677 psc = (struct dri3_screen *) base->psc;
678
679 (*psc->f->invalidate)(pdraw->loader_drawable.dri_drawable);
680
681 XSync(dpy, false);
682
683 (*psc->texBuffer->setTexBuffer2) (pcp->driContext,
684 pdraw->base.textureTarget,
685 pdraw->base.textureFormat,
686 pdraw->loader_drawable.dri_drawable);
687 }
688 }
689
690 static void
691 dri3_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer)
692 {
693 struct glx_context *gc = __glXGetCurrentContext();
694 struct dri3_context *pcp = (struct dri3_context *) gc;
695 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
696 struct dri3_drawable *pdraw = (struct dri3_drawable *) base;
697 struct dri3_screen *psc;
698
699 if (pdraw != NULL) {
700 psc = (struct dri3_screen *) base->psc;
701
702 if (psc->texBuffer->base.version >= 3 &&
703 psc->texBuffer->releaseTexBuffer != NULL)
704 (*psc->texBuffer->releaseTexBuffer) (pcp->driContext,
705 pdraw->base.textureTarget,
706 pdraw->loader_drawable.dri_drawable);
707 }
708 }
709
710 static const struct glx_context_vtable dri3_context_vtable = {
711 .destroy = dri3_destroy_context,
712 .bind = dri3_bind_context,
713 .unbind = dri3_unbind_context,
714 .wait_gl = dri3_wait_gl,
715 .wait_x = dri3_wait_x,
716 .use_x_font = DRI_glXUseXFont,
717 .bind_tex_image = dri3_bind_tex_image,
718 .release_tex_image = dri3_release_tex_image,
719 .get_proc_address = NULL,
720 .interop_query_device_info = dri3_interop_query_device_info,
721 .interop_export_object = dri3_interop_export_object
722 };
723
724 /** dri3_bind_extensions
725 *
726 * Enable all of the extensions supported on DRI3
727 */
728 static void
729 dri3_bind_extensions(struct dri3_screen *psc, struct glx_display * priv,
730 const char *driverName)
731 {
732 const __DRIextension **extensions;
733 unsigned mask;
734 int i;
735
736 extensions = psc->core->getExtensions(psc->driScreen);
737
738 __glXEnableDirectExtension(&psc->base, "GLX_SGI_swap_control");
739 __glXEnableDirectExtension(&psc->base, "GLX_MESA_swap_control");
740 __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
741 __glXEnableDirectExtension(&psc->base, "GLX_INTEL_swap_event");
742
743 mask = psc->image_driver->getAPIMask(psc->driScreen);
744
745 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context");
746 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context_profile");
747
748 if ((mask & ((1 << __DRI_API_GLES) |
749 (1 << __DRI_API_GLES2) |
750 (1 << __DRI_API_GLES3))) != 0) {
751 __glXEnableDirectExtension(&psc->base,
752 "GLX_EXT_create_context_es_profile");
753 __glXEnableDirectExtension(&psc->base,
754 "GLX_EXT_create_context_es2_profile");
755 }
756
757 for (i = 0; extensions[i]; i++) {
758 /* when on a different gpu than the server, the server pixmaps
759 * can have a tiling mode we can't read. Thus we can't create
760 * a texture from them.
761 */
762 if (!psc->is_different_gpu &&
763 (strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
764 psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
765 __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
766 }
767
768 if ((strcmp(extensions[i]->name, __DRI2_FLUSH) == 0)) {
769 psc->f = (__DRI2flushExtension *) extensions[i];
770 /* internal driver extension, no GL extension exposed */
771 }
772
773 if (strcmp(extensions[i]->name, __DRI_IMAGE) == 0)
774 psc->image = (__DRIimageExtension *) extensions[i];
775
776 if ((strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0))
777 psc->config = (__DRI2configQueryExtension *) extensions[i];
778
779 if (strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0)
780 __glXEnableDirectExtension(&psc->base,
781 "GLX_ARB_create_context_robustness");
782
783 if (strcmp(extensions[i]->name, __DRI2_RENDERER_QUERY) == 0) {
784 psc->rendererQuery = (__DRI2rendererQueryExtension *) extensions[i];
785 __glXEnableDirectExtension(&psc->base, "GLX_MESA_query_renderer");
786 }
787
788 if (strcmp(extensions[i]->name, __DRI2_INTEROP) == 0)
789 psc->interop = (__DRI2interopExtension*)extensions[i];
790
791 if (strcmp(extensions[i]->name, __DRI2_FLUSH_CONTROL) == 0)
792 __glXEnableDirectExtension(&psc->base,
793 "GLX_ARB_context_flush_control");
794 }
795 }
796
797 static const struct glx_screen_vtable dri3_screen_vtable = {
798 .create_context = dri3_create_context,
799 .create_context_attribs = dri3_create_context_attribs,
800 .query_renderer_integer = dri3_query_renderer_integer,
801 .query_renderer_string = dri3_query_renderer_string,
802 };
803
804 /** dri3_create_screen
805 *
806 * Initialize DRI3 on the specified screen.
807 *
808 * Opens the DRI device, locates the appropriate DRI driver
809 * and loads that.
810 *
811 * Checks to see if the driver supports the necessary extensions
812 *
813 * Initializes the driver for the screen and sets up our structures
814 */
815
816 static struct glx_screen *
817 dri3_create_screen(int screen, struct glx_display * priv)
818 {
819 xcb_connection_t *c = XGetXCBConnection(priv->dpy);
820 const __DRIconfig **driver_configs;
821 const __DRIextension **extensions;
822 const struct dri3_display *const pdp = (struct dri3_display *)
823 priv->dri3Display;
824 struct dri3_screen *psc;
825 __GLXDRIscreen *psp;
826 struct glx_config *configs = NULL, *visuals = NULL;
827 char *driverName, *tmp;
828 int i;
829 unsigned char disable;
830
831 psc = calloc(1, sizeof *psc);
832 if (psc == NULL)
833 return NULL;
834
835 psc->fd = -1;
836
837 if (!glx_screen_init(&psc->base, screen, priv)) {
838 free(psc);
839 return NULL;
840 }
841
842 psc->fd = loader_dri3_open(c, RootWindow(priv->dpy, screen), None);
843 if (psc->fd < 0) {
844 int conn_error = xcb_connection_has_error(c);
845
846 glx_screen_cleanup(&psc->base);
847 free(psc);
848 InfoMessageF("screen %d does not appear to be DRI3 capable\n", screen);
849
850 if (conn_error)
851 ErrorMessageF("Connection closed during DRI3 initialization failure");
852
853 return NULL;
854 }
855
856 psc->fd = loader_get_user_preferred_fd(psc->fd, &psc->is_different_gpu);
857
858 driverName = loader_get_driver_for_fd(psc->fd);
859 if (!driverName) {
860 ErrorMessageF("No driver found\n");
861 goto handle_error;
862 }
863
864 psc->driver = driOpenDriver(driverName);
865 if (psc->driver == NULL) {
866 ErrorMessageF("driver pointer missing\n");
867 goto handle_error;
868 }
869
870 extensions = driGetDriverExtensions(psc->driver, driverName);
871 if (extensions == NULL)
872 goto handle_error;
873
874 for (i = 0; extensions[i]; i++) {
875 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
876 psc->core = (__DRIcoreExtension *) extensions[i];
877 if (strcmp(extensions[i]->name, __DRI_IMAGE_DRIVER) == 0)
878 psc->image_driver = (__DRIimageDriverExtension *) extensions[i];
879 }
880
881
882 if (psc->core == NULL) {
883 ErrorMessageF("core dri driver extension not found\n");
884 goto handle_error;
885 }
886
887 if (psc->image_driver == NULL) {
888 ErrorMessageF("image driver extension not found\n");
889 goto handle_error;
890 }
891
892 psc->driScreen =
893 psc->image_driver->createNewScreen2(screen, psc->fd,
894 pdp->loader_extensions,
895 extensions,
896 &driver_configs, psc);
897
898 if (psc->driScreen == NULL) {
899 ErrorMessageF("failed to create dri screen\n");
900 goto handle_error;
901 }
902
903 dri3_bind_extensions(psc, priv, driverName);
904
905 if (!psc->image || psc->image->base.version < 7 || !psc->image->createImageFromFds) {
906 ErrorMessageF("Version 7 or imageFromFds image extension not found\n");
907 goto handle_error;
908 }
909
910 if (!psc->f || psc->f->base.version < 4) {
911 ErrorMessageF("Version 4 or later of flush extension not found\n");
912 goto handle_error;
913 }
914
915 if (psc->is_different_gpu && psc->image->base.version < 9) {
916 ErrorMessageF("Different GPU, but image extension version 9 or later not found\n");
917 goto handle_error;
918 }
919
920 if (psc->is_different_gpu && !psc->image->blitImage) {
921 ErrorMessageF("Different GPU, but blitImage not implemented for this driver\n");
922 goto handle_error;
923 }
924
925 if (!psc->is_different_gpu && (
926 !psc->texBuffer || psc->texBuffer->base.version < 2 ||
927 !psc->texBuffer->setTexBuffer2
928 )) {
929 ErrorMessageF("Version 2 or later of texBuffer extension not found\n");
930 goto handle_error;
931 }
932
933 psc->loader_dri3_ext.core = psc->core;
934 psc->loader_dri3_ext.image_driver = psc->image_driver;
935 psc->loader_dri3_ext.flush = psc->f;
936 psc->loader_dri3_ext.tex_buffer = psc->texBuffer;
937 psc->loader_dri3_ext.image = psc->image;
938 psc->loader_dri3_ext.config = psc->config;
939
940 configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs);
941 visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
942
943 if (!configs || !visuals) {
944 ErrorMessageF("No matching fbConfigs or visuals found\n");
945 goto handle_error;
946 }
947
948 glx_config_destroy_list(psc->base.configs);
949 psc->base.configs = configs;
950 glx_config_destroy_list(psc->base.visuals);
951 psc->base.visuals = visuals;
952
953 psc->driver_configs = driver_configs;
954
955 psc->base.vtable = &dri3_screen_vtable;
956 psp = &psc->vtable;
957 psc->base.driScreen = psp;
958 psp->destroyScreen = dri3_destroy_screen;
959 psp->createDrawable = dri3_create_drawable;
960 psp->swapBuffers = dri3_swap_buffers;
961
962 psp->getDrawableMSC = dri3_drawable_get_msc;
963 psp->waitForMSC = dri3_wait_for_msc;
964 psp->waitForSBC = dri3_wait_for_sbc;
965 psp->setSwapInterval = dri3_set_swap_interval;
966 psp->getSwapInterval = dri3_get_swap_interval;
967 if (psc->config->configQueryb(psc->driScreen,
968 "glx_disable_oml_sync_control",
969 &disable) || !disable)
970 __glXEnableDirectExtension(&psc->base, "GLX_OML_sync_control");
971
972 if (psc->config->configQueryb(psc->driScreen,
973 "glx_disable_sgi_video_sync",
974 &disable) || !disable)
975 __glXEnableDirectExtension(&psc->base, "GLX_SGI_video_sync");
976
977 psp->copySubBuffer = dri3_copy_sub_buffer;
978 __glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer");
979
980 psp->getBufferAge = dri3_get_buffer_age;
981 if (psc->config->configQueryb(psc->driScreen,
982 "glx_disable_ext_buffer_age",
983 &disable) || !disable)
984 __glXEnableDirectExtension(&psc->base, "GLX_EXT_buffer_age");
985
986 free(driverName);
987
988 tmp = getenv("LIBGL_SHOW_FPS");
989 psc->show_fps_interval = tmp ? atoi(tmp) : 0;
990 if (psc->show_fps_interval < 0)
991 psc->show_fps_interval = 0;
992
993 InfoMessageF("Using DRI3 for screen %d\n", screen);
994
995 return &psc->base;
996
997 handle_error:
998 CriticalErrorMessageF("failed to load driver: %s\n", driverName);
999
1000 if (configs)
1001 glx_config_destroy_list(configs);
1002 if (visuals)
1003 glx_config_destroy_list(visuals);
1004 if (psc->driScreen)
1005 psc->core->destroyScreen(psc->driScreen);
1006 psc->driScreen = NULL;
1007 if (psc->fd >= 0)
1008 close(psc->fd);
1009 if (psc->driver)
1010 dlclose(psc->driver);
1011
1012 free(driverName);
1013 glx_screen_cleanup(&psc->base);
1014 free(psc);
1015
1016 return NULL;
1017 }
1018
1019 /** dri_destroy_display
1020 *
1021 * Called from __glXFreeDisplayPrivate.
1022 */
1023 static void
1024 dri3_destroy_display(__GLXDRIdisplay * dpy)
1025 {
1026 free(dpy);
1027 }
1028
1029 /* Only request versions of these protocols which we actually support. */
1030 #define DRI3_SUPPORTED_MAJOR 1
1031 #define PRESENT_SUPPORTED_MAJOR 1
1032
1033 #ifdef HAVE_DRI3_MODIFIERS
1034 #define DRI3_SUPPORTED_MINOR 2
1035 #define PRESENT_SUPPORTED_MINOR 2
1036 #else
1037 #define PRESENT_SUPPORTED_MINOR 0
1038 #define DRI3_SUPPORTED_MINOR 0
1039 #endif
1040
1041 /** dri3_create_display
1042 *
1043 * Allocate, initialize and return a __DRIdisplayPrivate object.
1044 * This is called from __glXInitialize() when we are given a new
1045 * display pointer. This is public to that function, but hidden from
1046 * outside of libGL.
1047 */
1048 _X_HIDDEN __GLXDRIdisplay *
1049 dri3_create_display(Display * dpy)
1050 {
1051 struct dri3_display *pdp;
1052 xcb_connection_t *c = XGetXCBConnection(dpy);
1053 xcb_dri3_query_version_cookie_t dri3_cookie;
1054 xcb_dri3_query_version_reply_t *dri3_reply;
1055 xcb_present_query_version_cookie_t present_cookie;
1056 xcb_present_query_version_reply_t *present_reply;
1057 xcb_generic_error_t *error;
1058 const xcb_query_extension_reply_t *extension;
1059
1060 xcb_prefetch_extension_data(c, &xcb_dri3_id);
1061 xcb_prefetch_extension_data(c, &xcb_present_id);
1062
1063 extension = xcb_get_extension_data(c, &xcb_dri3_id);
1064 if (!(extension && extension->present))
1065 return NULL;
1066
1067 extension = xcb_get_extension_data(c, &xcb_present_id);
1068 if (!(extension && extension->present))
1069 return NULL;
1070
1071 dri3_cookie = xcb_dri3_query_version(c,
1072 DRI3_SUPPORTED_MAJOR,
1073 DRI3_SUPPORTED_MINOR);
1074 present_cookie = xcb_present_query_version(c,
1075 PRESENT_SUPPORTED_MAJOR,
1076 PRESENT_SUPPORTED_MINOR);
1077
1078 pdp = malloc(sizeof *pdp);
1079 if (pdp == NULL)
1080 return NULL;
1081
1082 dri3_reply = xcb_dri3_query_version_reply(c, dri3_cookie, &error);
1083 if (!dri3_reply) {
1084 free(error);
1085 goto no_extension;
1086 }
1087
1088 pdp->dri3Major = dri3_reply->major_version;
1089 pdp->dri3Minor = dri3_reply->minor_version;
1090 free(dri3_reply);
1091
1092 present_reply = xcb_present_query_version_reply(c, present_cookie, &error);
1093 if (!present_reply) {
1094 free(error);
1095 goto no_extension;
1096 }
1097 pdp->presentMajor = present_reply->major_version;
1098 pdp->presentMinor = present_reply->minor_version;
1099 free(present_reply);
1100
1101 pdp->base.destroyDisplay = dri3_destroy_display;
1102 pdp->base.createScreen = dri3_create_screen;
1103
1104 loader_set_logger(dri_message);
1105
1106 pdp->loader_extensions = loader_extensions;
1107
1108 return &pdp->base;
1109 no_extension:
1110 free(pdp);
1111 return NULL;
1112 }
1113
1114 #endif /* GLX_DIRECT_RENDERING */