egl/x11: Support DRI3 v1.1
[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 const struct dri3_display *const pdp = (struct dri3_display *)
361 base->display->dri3Display;
362 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
363 bool has_multibuffer = false;
364
365 pdraw = calloc(1, sizeof(*pdraw));
366 if (!pdraw)
367 return NULL;
368
369 pdraw->base.destroyDrawable = dri3_destroy_drawable;
370 pdraw->base.xDrawable = xDrawable;
371 pdraw->base.drawable = drawable;
372 pdraw->base.psc = &psc->base;
373
374 if ((psc->image && psc->image->base.version >= 15) &&
375 (pdp->dri3Major > 1 || (pdp->dri3Major == 1 && pdp->dri3Minor >= 2)))
376 has_multibuffer = true;
377
378 (void) __glXInitialize(psc->base.dpy);
379
380 if (loader_dri3_drawable_init(XGetXCBConnection(base->dpy),
381 xDrawable, psc->driScreen,
382 psc->is_different_gpu, has_multibuffer,
383 config->driConfig,
384 &psc->loader_dri3_ext, &glx_dri3_vtable,
385 &pdraw->loader_drawable)) {
386 free(pdraw);
387 return NULL;
388 }
389
390 return &pdraw->base;
391 }
392
393 /** dri3_wait_for_msc
394 *
395 * Get the X server to send an event when the target msc/divisor/remainder is
396 * reached.
397 */
398 static int
399 dri3_wait_for_msc(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
400 int64_t remainder, int64_t *ust, int64_t *msc, int64_t *sbc)
401 {
402 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
403
404 loader_dri3_wait_for_msc(&priv->loader_drawable, target_msc, divisor,
405 remainder, ust, msc, sbc);
406
407 return 1;
408 }
409
410 /** dri3_drawable_get_msc
411 *
412 * Return the current UST/MSC/SBC triplet by asking the server
413 * for an event
414 */
415 static int
416 dri3_drawable_get_msc(struct glx_screen *psc, __GLXDRIdrawable *pdraw,
417 int64_t *ust, int64_t *msc, int64_t *sbc)
418 {
419 return dri3_wait_for_msc(pdraw, 0, 0, 0, ust, msc,sbc);
420 }
421
422 /** dri3_wait_for_sbc
423 *
424 * Wait for the completed swap buffer count to reach the specified
425 * target. Presumably the application knows that this will be reached with
426 * outstanding complete events, or we're going to be here awhile.
427 */
428 static int
429 dri3_wait_for_sbc(__GLXDRIdrawable *pdraw, int64_t target_sbc, int64_t *ust,
430 int64_t *msc, int64_t *sbc)
431 {
432 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
433
434 return loader_dri3_wait_for_sbc(&priv->loader_drawable, target_sbc,
435 ust, msc, sbc);
436 }
437
438 static void
439 dri3_copy_sub_buffer(__GLXDRIdrawable *pdraw, int x, int y,
440 int width, int height,
441 Bool flush)
442 {
443 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
444
445 loader_dri3_copy_sub_buffer(&priv->loader_drawable, x, y,
446 width, height, flush);
447 }
448
449 static void
450 dri3_wait_x(struct glx_context *gc)
451 {
452 struct dri3_drawable *priv = (struct dri3_drawable *)
453 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
454
455 if (priv)
456 loader_dri3_wait_x(&priv->loader_drawable);
457 }
458
459 static void
460 dri3_wait_gl(struct glx_context *gc)
461 {
462 struct dri3_drawable *priv = (struct dri3_drawable *)
463 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
464
465 if (priv)
466 loader_dri3_wait_gl(&priv->loader_drawable);
467 }
468
469 /**
470 * Called by the driver when it needs to update the real front buffer with the
471 * contents of its fake front buffer.
472 */
473 static void
474 dri3_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)
475 {
476 struct loader_dri3_drawable *draw = loaderPrivate;
477 struct dri3_drawable *pdraw = loader_drawable_to_dri3_drawable(draw);
478 struct dri3_screen *psc;
479
480 if (!pdraw)
481 return;
482
483 if (!pdraw->base.psc)
484 return;
485
486 psc = (struct dri3_screen *) pdraw->base.psc;
487
488 (void) __glXInitialize(psc->base.dpy);
489
490 loader_dri3_flush(draw, __DRI2_FLUSH_DRAWABLE, __DRI2_THROTTLE_FLUSHFRONT);
491
492 (*psc->f->invalidate)(driDrawable);
493 loader_dri3_wait_gl(draw);
494 }
495
496 /**
497 * Make sure all pending swapbuffers have been submitted to hardware
498 *
499 * \param driDrawable[in] Pointer to the dri drawable whose swaps we are
500 * flushing.
501 * \param loaderPrivate[in] Pointer to the corresponding struct
502 * loader_dri_drawable.
503 */
504 static void
505 dri3_flush_swap_buffers(__DRIdrawable *driDrawable, void *loaderPrivate)
506 {
507 struct loader_dri3_drawable *draw = loaderPrivate;
508 struct dri3_drawable *pdraw = loader_drawable_to_dri3_drawable(draw);
509 struct dri3_screen *psc;
510
511 if (!pdraw)
512 return;
513
514 if (!pdraw->base.psc)
515 return;
516
517 psc = (struct dri3_screen *) pdraw->base.psc;
518
519 (void) __glXInitialize(psc->base.dpy);
520 loader_dri3_swapbuffer_barrier(draw);
521 }
522
523 static void
524 dri_set_background_context(void *loaderPrivate)
525 {
526 struct dri3_context *pcp = (struct dri3_context *)loaderPrivate;
527 __glXSetCurrentContext(&pcp->base);
528 }
529
530 static GLboolean
531 dri_is_thread_safe(void *loaderPrivate)
532 {
533 /* Unlike DRI2, DRI3 doesn't call GetBuffers/GetBuffersWithFormat
534 * during draw so we're safe here.
535 */
536 return true;
537 }
538
539 /* The image loader extension record for DRI3
540 */
541 static const __DRIimageLoaderExtension imageLoaderExtension = {
542 .base = { __DRI_IMAGE_LOADER, 3 },
543
544 .getBuffers = loader_dri3_get_buffers,
545 .flushFrontBuffer = dri3_flush_front_buffer,
546 .flushSwapBuffers = dri3_flush_swap_buffers,
547 };
548
549 const __DRIuseInvalidateExtension dri3UseInvalidate = {
550 .base = { __DRI_USE_INVALIDATE, 1 }
551 };
552
553 static const __DRIbackgroundCallableExtension driBackgroundCallable = {
554 .base = { __DRI_BACKGROUND_CALLABLE, 2 },
555
556 .setBackgroundContext = dri_set_background_context,
557 .isThreadSafe = dri_is_thread_safe,
558 };
559
560 static const __DRIextension *loader_extensions[] = {
561 &imageLoaderExtension.base,
562 &dri3UseInvalidate.base,
563 &driBackgroundCallable.base,
564 NULL
565 };
566
567 /** dri3_swap_buffers
568 *
569 * Make the current back buffer visible using the present extension
570 */
571 static int64_t
572 dri3_swap_buffers(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
573 int64_t remainder, Bool flush)
574 {
575 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
576 unsigned flags = __DRI2_FLUSH_DRAWABLE;
577
578 if (flush)
579 flags |= __DRI2_FLUSH_CONTEXT;
580
581 return loader_dri3_swap_buffers_msc(&priv->loader_drawable,
582 target_msc, divisor, remainder,
583 flags, false);
584 }
585
586 static int
587 dri3_get_buffer_age(__GLXDRIdrawable *pdraw)
588 {
589 struct dri3_drawable *priv = (struct dri3_drawable *)pdraw;
590
591 return loader_dri3_query_buffer_age(&priv->loader_drawable);
592 }
593
594 /** dri3_destroy_screen
595 */
596 static void
597 dri3_destroy_screen(struct glx_screen *base)
598 {
599 struct dri3_screen *psc = (struct dri3_screen *) base;
600
601 /* Free the direct rendering per screen data */
602 loader_dri3_close_screen(psc->driScreen);
603 (*psc->core->destroyScreen) (psc->driScreen);
604 driDestroyConfigs(psc->driver_configs);
605 close(psc->fd);
606 free(psc);
607 }
608
609 /** dri3_set_swap_interval
610 *
611 * Record the application swap interval specification,
612 */
613 static int
614 dri3_set_swap_interval(__GLXDRIdrawable *pdraw, int interval)
615 {
616 assert(pdraw != NULL);
617
618 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
619 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
620 struct dri3_screen *psc = (struct dri3_screen *) priv->base.psc;
621
622 if (psc->config)
623 psc->config->configQueryi(psc->driScreen,
624 "vblank_mode", &vblank_mode);
625
626 switch (vblank_mode) {
627 case DRI_CONF_VBLANK_NEVER:
628 if (interval != 0)
629 return GLX_BAD_VALUE;
630 break;
631 case DRI_CONF_VBLANK_ALWAYS_SYNC:
632 if (interval <= 0)
633 return GLX_BAD_VALUE;
634 break;
635 default:
636 break;
637 }
638
639 priv->swap_interval = interval;
640 loader_dri3_set_swap_interval(&priv->loader_drawable, interval);
641
642 return 0;
643 }
644
645 /** dri3_get_swap_interval
646 *
647 * Return the stored swap interval
648 */
649 static int
650 dri3_get_swap_interval(__GLXDRIdrawable *pdraw)
651 {
652 assert(pdraw != NULL);
653
654 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
655
656 return priv->swap_interval;
657 }
658
659 static void
660 dri3_bind_tex_image(Display * dpy,
661 GLXDrawable drawable,
662 int buffer, const int *attrib_list)
663 {
664 struct glx_context *gc = __glXGetCurrentContext();
665 struct dri3_context *pcp = (struct dri3_context *) gc;
666 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
667 struct dri3_drawable *pdraw = (struct dri3_drawable *) base;
668 struct dri3_screen *psc;
669
670 if (pdraw != NULL) {
671 psc = (struct dri3_screen *) base->psc;
672
673 (*psc->f->invalidate)(pdraw->loader_drawable.dri_drawable);
674
675 XSync(dpy, false);
676
677 (*psc->texBuffer->setTexBuffer2) (pcp->driContext,
678 pdraw->base.textureTarget,
679 pdraw->base.textureFormat,
680 pdraw->loader_drawable.dri_drawable);
681 }
682 }
683
684 static void
685 dri3_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer)
686 {
687 struct glx_context *gc = __glXGetCurrentContext();
688 struct dri3_context *pcp = (struct dri3_context *) gc;
689 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
690 struct dri3_drawable *pdraw = (struct dri3_drawable *) base;
691 struct dri3_screen *psc;
692
693 if (pdraw != NULL) {
694 psc = (struct dri3_screen *) base->psc;
695
696 if (psc->texBuffer->base.version >= 3 &&
697 psc->texBuffer->releaseTexBuffer != NULL)
698 (*psc->texBuffer->releaseTexBuffer) (pcp->driContext,
699 pdraw->base.textureTarget,
700 pdraw->loader_drawable.dri_drawable);
701 }
702 }
703
704 static const struct glx_context_vtable dri3_context_vtable = {
705 .destroy = dri3_destroy_context,
706 .bind = dri3_bind_context,
707 .unbind = dri3_unbind_context,
708 .wait_gl = dri3_wait_gl,
709 .wait_x = dri3_wait_x,
710 .use_x_font = DRI_glXUseXFont,
711 .bind_tex_image = dri3_bind_tex_image,
712 .release_tex_image = dri3_release_tex_image,
713 .get_proc_address = NULL,
714 .interop_query_device_info = dri3_interop_query_device_info,
715 .interop_export_object = dri3_interop_export_object
716 };
717
718 /** dri3_bind_extensions
719 *
720 * Enable all of the extensions supported on DRI3
721 */
722 static void
723 dri3_bind_extensions(struct dri3_screen *psc, struct glx_display * priv,
724 const char *driverName)
725 {
726 const __DRIextension **extensions;
727 unsigned mask;
728 int i;
729
730 extensions = psc->core->getExtensions(psc->driScreen);
731
732 __glXEnableDirectExtension(&psc->base, "GLX_SGI_swap_control");
733 __glXEnableDirectExtension(&psc->base, "GLX_MESA_swap_control");
734 __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
735 __glXEnableDirectExtension(&psc->base, "GLX_INTEL_swap_event");
736
737 mask = psc->image_driver->getAPIMask(psc->driScreen);
738
739 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context");
740 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context_profile");
741
742 if ((mask & ((1 << __DRI_API_GLES) |
743 (1 << __DRI_API_GLES2) |
744 (1 << __DRI_API_GLES3))) != 0) {
745 __glXEnableDirectExtension(&psc->base,
746 "GLX_EXT_create_context_es_profile");
747 __glXEnableDirectExtension(&psc->base,
748 "GLX_EXT_create_context_es2_profile");
749 }
750
751 for (i = 0; extensions[i]; i++) {
752 /* when on a different gpu than the server, the server pixmaps
753 * can have a tiling mode we can't read. Thus we can't create
754 * a texture from them.
755 */
756 if (!psc->is_different_gpu &&
757 (strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
758 psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
759 __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
760 }
761
762 if ((strcmp(extensions[i]->name, __DRI2_FLUSH) == 0)) {
763 psc->f = (__DRI2flushExtension *) extensions[i];
764 /* internal driver extension, no GL extension exposed */
765 }
766
767 if (strcmp(extensions[i]->name, __DRI_IMAGE) == 0)
768 psc->image = (__DRIimageExtension *) extensions[i];
769
770 if ((strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0))
771 psc->config = (__DRI2configQueryExtension *) extensions[i];
772
773 if (strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0)
774 __glXEnableDirectExtension(&psc->base,
775 "GLX_ARB_create_context_robustness");
776
777 if (strcmp(extensions[i]->name, __DRI2_RENDERER_QUERY) == 0) {
778 psc->rendererQuery = (__DRI2rendererQueryExtension *) extensions[i];
779 __glXEnableDirectExtension(&psc->base, "GLX_MESA_query_renderer");
780 }
781
782 if (strcmp(extensions[i]->name, __DRI2_INTEROP) == 0)
783 psc->interop = (__DRI2interopExtension*)extensions[i];
784
785 if (strcmp(extensions[i]->name, __DRI2_FLUSH_CONTROL) == 0)
786 __glXEnableDirectExtension(&psc->base,
787 "GLX_ARB_context_flush_control");
788 }
789 }
790
791 static const struct glx_screen_vtable dri3_screen_vtable = {
792 .create_context = dri3_create_context,
793 .create_context_attribs = dri3_create_context_attribs,
794 .query_renderer_integer = dri3_query_renderer_integer,
795 .query_renderer_string = dri3_query_renderer_string,
796 };
797
798 /** dri3_create_screen
799 *
800 * Initialize DRI3 on the specified screen.
801 *
802 * Opens the DRI device, locates the appropriate DRI driver
803 * and loads that.
804 *
805 * Checks to see if the driver supports the necessary extensions
806 *
807 * Initializes the driver for the screen and sets up our structures
808 */
809
810 static struct glx_screen *
811 dri3_create_screen(int screen, struct glx_display * priv)
812 {
813 xcb_connection_t *c = XGetXCBConnection(priv->dpy);
814 const __DRIconfig **driver_configs;
815 const __DRIextension **extensions;
816 const struct dri3_display *const pdp = (struct dri3_display *)
817 priv->dri3Display;
818 struct dri3_screen *psc;
819 __GLXDRIscreen *psp;
820 struct glx_config *configs = NULL, *visuals = NULL;
821 char *driverName, *tmp;
822 int i;
823 unsigned char disable;
824
825 psc = calloc(1, sizeof *psc);
826 if (psc == NULL)
827 return NULL;
828
829 psc->fd = -1;
830
831 if (!glx_screen_init(&psc->base, screen, priv)) {
832 free(psc);
833 return NULL;
834 }
835
836 psc->fd = loader_dri3_open(c, RootWindow(priv->dpy, screen), None);
837 if (psc->fd < 0) {
838 int conn_error = xcb_connection_has_error(c);
839
840 glx_screen_cleanup(&psc->base);
841 free(psc);
842 InfoMessageF("screen %d does not appear to be DRI3 capable\n", screen);
843
844 if (conn_error)
845 ErrorMessageF("Connection closed during DRI3 initialization failure");
846
847 return NULL;
848 }
849
850 psc->fd = loader_get_user_preferred_fd(psc->fd, &psc->is_different_gpu);
851
852 driverName = loader_get_driver_for_fd(psc->fd);
853 if (!driverName) {
854 ErrorMessageF("No driver found\n");
855 goto handle_error;
856 }
857
858 psc->driver = driOpenDriver(driverName);
859 if (psc->driver == NULL) {
860 ErrorMessageF("driver pointer missing\n");
861 goto handle_error;
862 }
863
864 extensions = driGetDriverExtensions(psc->driver, driverName);
865 if (extensions == NULL)
866 goto handle_error;
867
868 for (i = 0; extensions[i]; i++) {
869 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
870 psc->core = (__DRIcoreExtension *) extensions[i];
871 if (strcmp(extensions[i]->name, __DRI_IMAGE_DRIVER) == 0)
872 psc->image_driver = (__DRIimageDriverExtension *) extensions[i];
873 }
874
875
876 if (psc->core == NULL) {
877 ErrorMessageF("core dri driver extension not found\n");
878 goto handle_error;
879 }
880
881 if (psc->image_driver == NULL) {
882 ErrorMessageF("image driver extension not found\n");
883 goto handle_error;
884 }
885
886 psc->driScreen =
887 psc->image_driver->createNewScreen2(screen, psc->fd,
888 pdp->loader_extensions,
889 extensions,
890 &driver_configs, psc);
891
892 if (psc->driScreen == NULL) {
893 ErrorMessageF("failed to create dri screen\n");
894 goto handle_error;
895 }
896
897 dri3_bind_extensions(psc, priv, driverName);
898
899 if (!psc->image || psc->image->base.version < 7 || !psc->image->createImageFromFds) {
900 ErrorMessageF("Version 7 or imageFromFds image extension not found\n");
901 goto handle_error;
902 }
903
904 if (!psc->f || psc->f->base.version < 4) {
905 ErrorMessageF("Version 4 or later of flush extension not found\n");
906 goto handle_error;
907 }
908
909 if (psc->is_different_gpu && psc->image->base.version < 9) {
910 ErrorMessageF("Different GPU, but image extension version 9 or later not found\n");
911 goto handle_error;
912 }
913
914 if (psc->is_different_gpu && !psc->image->blitImage) {
915 ErrorMessageF("Different GPU, but blitImage not implemented for this driver\n");
916 goto handle_error;
917 }
918
919 if (!psc->is_different_gpu && (
920 !psc->texBuffer || psc->texBuffer->base.version < 2 ||
921 !psc->texBuffer->setTexBuffer2
922 )) {
923 ErrorMessageF("Version 2 or later of texBuffer extension not found\n");
924 goto handle_error;
925 }
926
927 psc->loader_dri3_ext.core = psc->core;
928 psc->loader_dri3_ext.image_driver = psc->image_driver;
929 psc->loader_dri3_ext.flush = psc->f;
930 psc->loader_dri3_ext.tex_buffer = psc->texBuffer;
931 psc->loader_dri3_ext.image = psc->image;
932 psc->loader_dri3_ext.config = psc->config;
933
934 configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs);
935 visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
936
937 if (!configs || !visuals) {
938 ErrorMessageF("No matching fbConfigs or visuals found\n");
939 goto handle_error;
940 }
941
942 glx_config_destroy_list(psc->base.configs);
943 psc->base.configs = configs;
944 glx_config_destroy_list(psc->base.visuals);
945 psc->base.visuals = visuals;
946
947 psc->driver_configs = driver_configs;
948
949 psc->base.vtable = &dri3_screen_vtable;
950 psp = &psc->vtable;
951 psc->base.driScreen = psp;
952 psp->destroyScreen = dri3_destroy_screen;
953 psp->createDrawable = dri3_create_drawable;
954 psp->swapBuffers = dri3_swap_buffers;
955
956 psp->getDrawableMSC = dri3_drawable_get_msc;
957 psp->waitForMSC = dri3_wait_for_msc;
958 psp->waitForSBC = dri3_wait_for_sbc;
959 psp->setSwapInterval = dri3_set_swap_interval;
960 psp->getSwapInterval = dri3_get_swap_interval;
961 if (psc->config->configQueryb(psc->driScreen,
962 "glx_disable_oml_sync_control",
963 &disable) || !disable)
964 __glXEnableDirectExtension(&psc->base, "GLX_OML_sync_control");
965
966 if (psc->config->configQueryb(psc->driScreen,
967 "glx_disable_sgi_video_sync",
968 &disable) || !disable)
969 __glXEnableDirectExtension(&psc->base, "GLX_SGI_video_sync");
970
971 psp->copySubBuffer = dri3_copy_sub_buffer;
972 __glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer");
973
974 psp->getBufferAge = dri3_get_buffer_age;
975 if (psc->config->configQueryb(psc->driScreen,
976 "glx_disable_ext_buffer_age",
977 &disable) || !disable)
978 __glXEnableDirectExtension(&psc->base, "GLX_EXT_buffer_age");
979
980 free(driverName);
981
982 tmp = getenv("LIBGL_SHOW_FPS");
983 psc->show_fps_interval = tmp ? atoi(tmp) : 0;
984 if (psc->show_fps_interval < 0)
985 psc->show_fps_interval = 0;
986
987 InfoMessageF("Using DRI3 for screen %d\n", screen);
988
989 return &psc->base;
990
991 handle_error:
992 CriticalErrorMessageF("failed to load driver: %s\n", driverName);
993
994 if (configs)
995 glx_config_destroy_list(configs);
996 if (visuals)
997 glx_config_destroy_list(visuals);
998 if (psc->driScreen)
999 psc->core->destroyScreen(psc->driScreen);
1000 psc->driScreen = NULL;
1001 if (psc->fd >= 0)
1002 close(psc->fd);
1003 if (psc->driver)
1004 dlclose(psc->driver);
1005
1006 free(driverName);
1007 glx_screen_cleanup(&psc->base);
1008 free(psc);
1009
1010 return NULL;
1011 }
1012
1013 /** dri_destroy_display
1014 *
1015 * Called from __glXFreeDisplayPrivate.
1016 */
1017 static void
1018 dri3_destroy_display(__GLXDRIdisplay * dpy)
1019 {
1020 free(dpy);
1021 }
1022
1023 /** dri3_create_display
1024 *
1025 * Allocate, initialize and return a __DRIdisplayPrivate object.
1026 * This is called from __glXInitialize() when we are given a new
1027 * display pointer. This is public to that function, but hidden from
1028 * outside of libGL.
1029 */
1030 _X_HIDDEN __GLXDRIdisplay *
1031 dri3_create_display(Display * dpy)
1032 {
1033 struct dri3_display *pdp;
1034 xcb_connection_t *c = XGetXCBConnection(dpy);
1035 xcb_dri3_query_version_cookie_t dri3_cookie;
1036 xcb_dri3_query_version_reply_t *dri3_reply;
1037 xcb_present_query_version_cookie_t present_cookie;
1038 xcb_present_query_version_reply_t *present_reply;
1039 xcb_generic_error_t *error;
1040 const xcb_query_extension_reply_t *extension;
1041
1042 xcb_prefetch_extension_data(c, &xcb_dri3_id);
1043 xcb_prefetch_extension_data(c, &xcb_present_id);
1044
1045 extension = xcb_get_extension_data(c, &xcb_dri3_id);
1046 if (!(extension && extension->present))
1047 return NULL;
1048
1049 extension = xcb_get_extension_data(c, &xcb_present_id);
1050 if (!(extension && extension->present))
1051 return NULL;
1052
1053 dri3_cookie = xcb_dri3_query_version(c,
1054 XCB_DRI3_MAJOR_VERSION,
1055 XCB_DRI3_MINOR_VERSION);
1056
1057
1058 present_cookie = xcb_present_query_version(c,
1059 XCB_PRESENT_MAJOR_VERSION,
1060 XCB_PRESENT_MINOR_VERSION);
1061
1062 pdp = malloc(sizeof *pdp);
1063 if (pdp == NULL)
1064 return NULL;
1065
1066 dri3_reply = xcb_dri3_query_version_reply(c, dri3_cookie, &error);
1067 if (!dri3_reply) {
1068 free(error);
1069 goto no_extension;
1070 }
1071
1072 pdp->dri3Major = dri3_reply->major_version;
1073 pdp->dri3Minor = dri3_reply->minor_version;
1074 free(dri3_reply);
1075
1076 present_reply = xcb_present_query_version_reply(c, present_cookie, &error);
1077 if (!present_reply) {
1078 free(error);
1079 goto no_extension;
1080 }
1081 pdp->presentMajor = present_reply->major_version;
1082 pdp->presentMinor = present_reply->minor_version;
1083 free(present_reply);
1084
1085 pdp->base.destroyDisplay = dri3_destroy_display;
1086 pdp->base.createScreen = dri3_create_screen;
1087
1088 loader_set_logger(dri_message);
1089
1090 pdp->loader_extensions = loader_extensions;
1091
1092 return &pdp->base;
1093 no_extension:
1094 free(pdp);
1095 return NULL;
1096 }
1097
1098 #endif /* GLX_DIRECT_RENDERING */