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