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