loader: remove clamp_swap_interval()
[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 int
90 glx_dri3_get_swap_interval(struct loader_dri3_drawable *draw)
91 {
92 struct dri3_drawable *priv = loader_drawable_to_dri3_drawable(draw);
93
94 return priv->swap_interval;
95 }
96
97 static void
98 glx_dri3_set_swap_interval(struct loader_dri3_drawable *draw, int interval)
99 {
100 struct dri3_drawable *priv = loader_drawable_to_dri3_drawable(draw);
101
102 priv->swap_interval = interval;
103 }
104
105 static void
106 glx_dri3_set_drawable_size(struct loader_dri3_drawable *draw,
107 int width, int height)
108 {
109 /* Nothing to do */
110 }
111
112 static bool
113 glx_dri3_in_current_context(struct loader_dri3_drawable *draw)
114 {
115 struct dri3_drawable *priv = loader_drawable_to_dri3_drawable(draw);
116
117 if (!priv)
118 return false;
119
120 struct dri3_context *pcp = (struct dri3_context *) __glXGetCurrentContext();
121 struct dri3_screen *psc = (struct dri3_screen *) priv->base.psc;
122
123 return (&pcp->base != &dummyContext) && pcp->base.psc == &psc->base;
124 }
125
126 static __DRIcontext *
127 glx_dri3_get_dri_context(struct loader_dri3_drawable *draw)
128 {
129 struct glx_context *gc = __glXGetCurrentContext();
130 struct dri3_context *dri3Ctx = (struct dri3_context *) gc;
131
132 return (gc != &dummyContext) ? dri3Ctx->driContext : NULL;
133 }
134
135 static __DRIscreen *
136 glx_dri3_get_dri_screen(struct loader_dri3_drawable *draw)
137 {
138 struct glx_context *gc = __glXGetCurrentContext();
139 struct dri3_context *pcp = (struct dri3_context *) gc;
140 struct dri3_screen *psc = (struct dri3_screen *) pcp->base.psc;
141
142 return (gc != &dummyContext && psc) ? psc->driScreen : NULL;
143 }
144
145 static void
146 glx_dri3_flush_drawable(struct loader_dri3_drawable *draw, unsigned flags)
147 {
148 loader_dri3_flush(draw, flags, __DRI2_THROTTLE_SWAPBUFFER);
149 }
150
151 static void
152 glx_dri3_show_fps(struct loader_dri3_drawable *draw, uint64_t current_ust)
153 {
154 struct dri3_drawable *priv = loader_drawable_to_dri3_drawable(draw);
155 const uint64_t interval =
156 ((struct dri3_screen *) priv->base.psc)->show_fps_interval;
157
158 if (!interval)
159 return;
160
161 priv->frames++;
162
163 /* DRI3+Present together uses microseconds for UST. */
164 if (priv->previous_ust + interval * 1000000 <= current_ust) {
165 if (priv->previous_ust) {
166 fprintf(stderr, "libGL: FPS = %.1f\n",
167 ((uint64_t) priv->frames * 1000000) /
168 (double)(current_ust - priv->previous_ust));
169 }
170 priv->frames = 0;
171 priv->previous_ust = current_ust;
172 }
173 }
174
175 static const struct loader_dri3_vtable glx_dri3_vtable = {
176 .get_swap_interval = glx_dri3_get_swap_interval,
177 .set_swap_interval = glx_dri3_set_swap_interval,
178 .set_drawable_size = glx_dri3_set_drawable_size,
179 .in_current_context = glx_dri3_in_current_context,
180 .get_dri_context = glx_dri3_get_dri_context,
181 .get_dri_screen = glx_dri3_get_dri_screen,
182 .flush_drawable = glx_dri3_flush_drawable,
183 .show_fps = glx_dri3_show_fps,
184 };
185
186
187 static const struct glx_context_vtable dri3_context_vtable;
188
189 static void
190 dri3_destroy_context(struct glx_context *context)
191 {
192 struct dri3_context *pcp = (struct dri3_context *) context;
193 struct dri3_screen *psc = (struct dri3_screen *) context->psc;
194
195 driReleaseDrawables(&pcp->base);
196
197 free((char *) context->extensions);
198
199 (*psc->core->destroyContext) (pcp->driContext);
200
201 free(pcp);
202 }
203
204 static Bool
205 dri3_bind_context(struct glx_context *context, struct glx_context *old,
206 GLXDrawable draw, GLXDrawable read)
207 {
208 struct dri3_context *pcp = (struct dri3_context *) context;
209 struct dri3_screen *psc = (struct dri3_screen *) pcp->base.psc;
210 struct dri3_drawable *pdraw, *pread;
211 __DRIdrawable *dri_draw = NULL, *dri_read = NULL;
212
213 pdraw = (struct dri3_drawable *) driFetchDrawable(context, draw);
214 pread = (struct dri3_drawable *) driFetchDrawable(context, read);
215
216 driReleaseDrawables(&pcp->base);
217
218 if (pdraw)
219 dri_draw = pdraw->loader_drawable.dri_drawable;
220 else if (draw != None)
221 return GLXBadDrawable;
222
223 if (pread)
224 dri_read = pread->loader_drawable.dri_drawable;
225 else if (read != None)
226 return GLXBadDrawable;
227
228 if (!(*psc->core->bindContext) (pcp->driContext, dri_draw, dri_read))
229 return GLXBadContext;
230
231 if (dri_draw)
232 (*psc->f->invalidate)(dri_draw);
233 if (dri_read && dri_read != dri_draw)
234 (*psc->f->invalidate)(dri_read);
235
236 return Success;
237 }
238
239 static void
240 dri3_unbind_context(struct glx_context *context, struct glx_context *new)
241 {
242 struct dri3_context *pcp = (struct dri3_context *) context;
243 struct dri3_screen *psc = (struct dri3_screen *) pcp->base.psc;
244
245 (*psc->core->unbindContext) (pcp->driContext);
246 }
247
248 static struct glx_context *
249 dri3_create_context_attribs(struct glx_screen *base,
250 struct glx_config *config_base,
251 struct glx_context *shareList,
252 unsigned num_attribs,
253 const uint32_t *attribs,
254 unsigned *error)
255 {
256 struct dri3_context *pcp = NULL;
257 struct dri3_context *pcp_shared = NULL;
258 struct dri3_screen *psc = (struct dri3_screen *) base;
259 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
260 __DRIcontext *shared = NULL;
261
262 uint32_t minor_ver = 1;
263 uint32_t major_ver = 2;
264 uint32_t flags = 0;
265 unsigned api;
266 int reset = __DRI_CTX_RESET_NO_NOTIFICATION;
267 uint32_t ctx_attribs[2 * 5];
268 unsigned num_ctx_attribs = 0;
269 uint32_t render_type;
270
271 /* Remap the GLX tokens to DRI2 tokens.
272 */
273 if (!dri2_convert_glx_attribs(num_attribs, attribs,
274 &major_ver, &minor_ver,
275 &render_type, &flags, &api,
276 &reset, error))
277 goto error_exit;
278
279 /* Check the renderType value */
280 if (!validate_renderType_against_config(config_base, render_type))
281 goto error_exit;
282
283 if (shareList) {
284 pcp_shared = (struct dri3_context *) shareList;
285 shared = pcp_shared->driContext;
286 }
287
288 pcp = calloc(1, sizeof *pcp);
289 if (pcp == NULL) {
290 *error = __DRI_CTX_ERROR_NO_MEMORY;
291 goto error_exit;
292 }
293
294 if (!glx_context_init(&pcp->base, &psc->base, &config->base))
295 goto error_exit;
296
297 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
298 ctx_attribs[num_ctx_attribs++] = major_ver;
299 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
300 ctx_attribs[num_ctx_attribs++] = minor_ver;
301
302 /* Only send a value when the non-default value is requested. By doing
303 * this we don't have to check the driver's DRI3 version before sending the
304 * default value.
305 */
306 if (reset != __DRI_CTX_RESET_NO_NOTIFICATION) {
307 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
308 ctx_attribs[num_ctx_attribs++] = reset;
309 }
310
311 if (flags != 0) {
312 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_FLAGS;
313
314 /* The current __DRI_CTX_FLAG_* values are identical to the
315 * GLX_CONTEXT_*_BIT values.
316 */
317 ctx_attribs[num_ctx_attribs++] = flags;
318 }
319
320 pcp->driContext =
321 (*psc->image_driver->createContextAttribs) (psc->driScreen,
322 api,
323 config->driConfig,
324 shared,
325 num_ctx_attribs / 2,
326 ctx_attribs,
327 error,
328 pcp);
329
330 if (pcp->driContext == NULL)
331 goto error_exit;
332
333 pcp->base.vtable = &dri3_context_vtable;
334
335 return &pcp->base;
336
337 error_exit:
338 free(pcp);
339
340 return NULL;
341 }
342
343 static struct glx_context *
344 dri3_create_context(struct glx_screen *base,
345 struct glx_config *config_base,
346 struct glx_context *shareList, int renderType)
347 {
348 unsigned int error;
349
350 return dri3_create_context_attribs(base, config_base, shareList,
351 0, NULL, &error);
352 }
353
354 static void
355 dri3_destroy_drawable(__GLXDRIdrawable *base)
356 {
357 struct dri3_drawable *pdraw = (struct dri3_drawable *) base;
358
359 loader_dri3_drawable_fini(&pdraw->loader_drawable);
360
361 free(pdraw);
362 }
363
364 static __GLXDRIdrawable *
365 dri3_create_drawable(struct glx_screen *base, XID xDrawable,
366 GLXDrawable drawable, struct glx_config *config_base)
367 {
368 struct dri3_drawable *pdraw;
369 struct dri3_screen *psc = (struct dri3_screen *) base;
370 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
371
372 pdraw = calloc(1, sizeof(*pdraw));
373 if (!pdraw)
374 return NULL;
375
376 pdraw->base.destroyDrawable = dri3_destroy_drawable;
377 pdraw->base.xDrawable = xDrawable;
378 pdraw->base.drawable = drawable;
379 pdraw->base.psc = &psc->base;
380
381 (void) __glXInitialize(psc->base.dpy);
382
383 if (loader_dri3_drawable_init(XGetXCBConnection(base->dpy),
384 xDrawable, psc->driScreen,
385 psc->is_different_gpu, 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 static void
499 dri_set_background_context(void *loaderPrivate)
500 {
501 struct dri3_context *pcp = (struct dri3_context *)loaderPrivate;
502 __glXSetCurrentContext(&pcp->base);
503 }
504
505 static GLboolean
506 dri_is_thread_safe(void *loaderPrivate)
507 {
508 /* Unlike DRI2, DRI3 doesn't call GetBuffers/GetBuffersWithFormat
509 * during draw so we're safe here.
510 */
511 return true;
512 }
513
514 /* The image loader extension record for DRI3
515 */
516 static const __DRIimageLoaderExtension imageLoaderExtension = {
517 .base = { __DRI_IMAGE_LOADER, 1 },
518
519 .getBuffers = loader_dri3_get_buffers,
520 .flushFrontBuffer = dri3_flush_front_buffer,
521 };
522
523 const __DRIuseInvalidateExtension dri3UseInvalidate = {
524 .base = { __DRI_USE_INVALIDATE, 1 }
525 };
526
527 static const __DRIbackgroundCallableExtension driBackgroundCallable = {
528 .base = { __DRI_BACKGROUND_CALLABLE, 2 },
529
530 .setBackgroundContext = dri_set_background_context,
531 .isThreadSafe = dri_is_thread_safe,
532 };
533
534 static const __DRIextension *loader_extensions[] = {
535 &imageLoaderExtension.base,
536 &dri3UseInvalidate.base,
537 &driBackgroundCallable.base,
538 NULL
539 };
540
541 /** dri3_swap_buffers
542 *
543 * Make the current back buffer visible using the present extension
544 */
545 static int64_t
546 dri3_swap_buffers(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
547 int64_t remainder, Bool flush)
548 {
549 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
550 unsigned flags = __DRI2_FLUSH_DRAWABLE;
551
552 if (flush)
553 flags |= __DRI2_FLUSH_CONTEXT;
554
555 return loader_dri3_swap_buffers_msc(&priv->loader_drawable,
556 target_msc, divisor, remainder,
557 flags, false);
558 }
559
560 static int
561 dri3_get_buffer_age(__GLXDRIdrawable *pdraw)
562 {
563 struct dri3_drawable *priv = (struct dri3_drawable *)pdraw;
564
565 return loader_dri3_query_buffer_age(&priv->loader_drawable);
566 }
567
568 /** dri3_destroy_screen
569 */
570 static void
571 dri3_destroy_screen(struct glx_screen *base)
572 {
573 struct dri3_screen *psc = (struct dri3_screen *) base;
574
575 /* Free the direct rendering per screen data */
576 (*psc->core->destroyScreen) (psc->driScreen);
577 driDestroyConfigs(psc->driver_configs);
578 close(psc->fd);
579 free(psc);
580 }
581
582 /** dri3_set_swap_interval
583 *
584 * Record the application swap interval specification,
585 */
586 static int
587 dri3_set_swap_interval(__GLXDRIdrawable *pdraw, int interval)
588 {
589 assert(pdraw != NULL);
590
591 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
592 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
593 struct dri3_screen *psc = (struct dri3_screen *) priv->base.psc;
594
595 if (psc->config)
596 psc->config->configQueryi(psc->driScreen,
597 "vblank_mode", &vblank_mode);
598
599 switch (vblank_mode) {
600 case DRI_CONF_VBLANK_NEVER:
601 if (interval != 0)
602 return GLX_BAD_VALUE;
603 break;
604 case DRI_CONF_VBLANK_ALWAYS_SYNC:
605 if (interval <= 0)
606 return GLX_BAD_VALUE;
607 break;
608 default:
609 break;
610 }
611
612 loader_dri3_set_swap_interval(&priv->loader_drawable, interval);
613
614 return 0;
615 }
616
617 /** dri3_get_swap_interval
618 *
619 * Return the stored swap interval
620 */
621 static int
622 dri3_get_swap_interval(__GLXDRIdrawable *pdraw)
623 {
624 assert(pdraw != NULL);
625
626 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
627
628 return priv->swap_interval;
629 }
630
631 static void
632 dri3_bind_tex_image(Display * dpy,
633 GLXDrawable drawable,
634 int buffer, const int *attrib_list)
635 {
636 struct glx_context *gc = __glXGetCurrentContext();
637 struct dri3_context *pcp = (struct dri3_context *) gc;
638 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
639 struct dri3_drawable *pdraw = (struct dri3_drawable *) base;
640 struct dri3_screen *psc;
641
642 if (pdraw != NULL) {
643 psc = (struct dri3_screen *) base->psc;
644
645 (*psc->f->invalidate)(pdraw->loader_drawable.dri_drawable);
646
647 XSync(dpy, false);
648
649 (*psc->texBuffer->setTexBuffer2) (pcp->driContext,
650 pdraw->base.textureTarget,
651 pdraw->base.textureFormat,
652 pdraw->loader_drawable.dri_drawable);
653 }
654 }
655
656 static void
657 dri3_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer)
658 {
659 struct glx_context *gc = __glXGetCurrentContext();
660 struct dri3_context *pcp = (struct dri3_context *) gc;
661 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
662 struct dri3_drawable *pdraw = (struct dri3_drawable *) base;
663 struct dri3_screen *psc;
664
665 if (pdraw != NULL) {
666 psc = (struct dri3_screen *) base->psc;
667
668 if (psc->texBuffer->base.version >= 3 &&
669 psc->texBuffer->releaseTexBuffer != NULL)
670 (*psc->texBuffer->releaseTexBuffer) (pcp->driContext,
671 pdraw->base.textureTarget,
672 pdraw->loader_drawable.dri_drawable);
673 }
674 }
675
676 static const struct glx_context_vtable dri3_context_vtable = {
677 .destroy = dri3_destroy_context,
678 .bind = dri3_bind_context,
679 .unbind = dri3_unbind_context,
680 .wait_gl = dri3_wait_gl,
681 .wait_x = dri3_wait_x,
682 .use_x_font = DRI_glXUseXFont,
683 .bind_tex_image = dri3_bind_tex_image,
684 .release_tex_image = dri3_release_tex_image,
685 .get_proc_address = NULL,
686 .interop_query_device_info = dri3_interop_query_device_info,
687 .interop_export_object = dri3_interop_export_object
688 };
689
690 /** dri3_bind_extensions
691 *
692 * Enable all of the extensions supported on DRI3
693 */
694 static void
695 dri3_bind_extensions(struct dri3_screen *psc, struct glx_display * priv,
696 const char *driverName)
697 {
698 const __DRIextension **extensions;
699 unsigned mask;
700 int i;
701
702 extensions = psc->core->getExtensions(psc->driScreen);
703
704 __glXEnableDirectExtension(&psc->base, "GLX_SGI_video_sync");
705 __glXEnableDirectExtension(&psc->base, "GLX_SGI_swap_control");
706 __glXEnableDirectExtension(&psc->base, "GLX_MESA_swap_control");
707 __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
708 __glXEnableDirectExtension(&psc->base, "GLX_INTEL_swap_event");
709
710 mask = psc->image_driver->getAPIMask(psc->driScreen);
711
712 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context");
713 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context_profile");
714
715 if ((mask & ((1 << __DRI_API_GLES) |
716 (1 << __DRI_API_GLES2) |
717 (1 << __DRI_API_GLES3))) != 0) {
718 __glXEnableDirectExtension(&psc->base,
719 "GLX_EXT_create_context_es_profile");
720 __glXEnableDirectExtension(&psc->base,
721 "GLX_EXT_create_context_es2_profile");
722 }
723
724 for (i = 0; extensions[i]; i++) {
725 /* when on a different gpu than the server, the server pixmaps
726 * can have a tiling mode we can't read. Thus we can't create
727 * a texture from them.
728 */
729 if (!psc->is_different_gpu &&
730 (strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
731 psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
732 __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
733 }
734
735 if ((strcmp(extensions[i]->name, __DRI2_FLUSH) == 0)) {
736 psc->f = (__DRI2flushExtension *) extensions[i];
737 /* internal driver extension, no GL extension exposed */
738 }
739
740 if (strcmp(extensions[i]->name, __DRI_IMAGE) == 0)
741 psc->image = (__DRIimageExtension *) extensions[i];
742
743 if ((strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0))
744 psc->config = (__DRI2configQueryExtension *) extensions[i];
745
746 if (strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0)
747 __glXEnableDirectExtension(&psc->base,
748 "GLX_ARB_create_context_robustness");
749
750 if (strcmp(extensions[i]->name, __DRI2_RENDERER_QUERY) == 0) {
751 psc->rendererQuery = (__DRI2rendererQueryExtension *) extensions[i];
752 __glXEnableDirectExtension(&psc->base, "GLX_MESA_query_renderer");
753 }
754
755 if (strcmp(extensions[i]->name, __DRI2_INTEROP) == 0)
756 psc->interop = (__DRI2interopExtension*)extensions[i];
757 }
758 }
759
760 static const struct glx_screen_vtable dri3_screen_vtable = {
761 .create_context = dri3_create_context,
762 .create_context_attribs = dri3_create_context_attribs,
763 .query_renderer_integer = dri3_query_renderer_integer,
764 .query_renderer_string = dri3_query_renderer_string,
765 };
766
767 /** dri3_create_screen
768 *
769 * Initialize DRI3 on the specified screen.
770 *
771 * Opens the DRI device, locates the appropriate DRI driver
772 * and loads that.
773 *
774 * Checks to see if the driver supports the necessary extensions
775 *
776 * Initializes the driver for the screen and sets up our structures
777 */
778
779 static struct glx_screen *
780 dri3_create_screen(int screen, struct glx_display * priv)
781 {
782 xcb_connection_t *c = XGetXCBConnection(priv->dpy);
783 const __DRIconfig **driver_configs;
784 const __DRIextension **extensions;
785 const struct dri3_display *const pdp = (struct dri3_display *)
786 priv->dri3Display;
787 struct dri3_screen *psc;
788 __GLXDRIscreen *psp;
789 struct glx_config *configs = NULL, *visuals = NULL;
790 char *driverName, *deviceName, *tmp;
791 int i;
792 unsigned char disable;
793
794 psc = calloc(1, sizeof *psc);
795 if (psc == NULL)
796 return NULL;
797
798 psc->fd = -1;
799
800 if (!glx_screen_init(&psc->base, screen, priv)) {
801 free(psc);
802 return NULL;
803 }
804
805 psc->fd = loader_dri3_open(c, RootWindow(priv->dpy, screen), None);
806 if (psc->fd < 0) {
807 int conn_error = xcb_connection_has_error(c);
808
809 glx_screen_cleanup(&psc->base);
810 free(psc);
811 InfoMessageF("screen %d does not appear to be DRI3 capable\n", screen);
812
813 if (conn_error)
814 ErrorMessageF("Connection closed during DRI3 initialization failure");
815
816 return NULL;
817 }
818
819 psc->fd = loader_get_user_preferred_fd(psc->fd, &psc->is_different_gpu);
820 deviceName = NULL;
821
822 driverName = loader_get_driver_for_fd(psc->fd);
823 if (!driverName) {
824 ErrorMessageF("No driver found\n");
825 goto handle_error;
826 }
827
828 psc->driver = driOpenDriver(driverName);
829 if (psc->driver == NULL) {
830 ErrorMessageF("driver pointer missing\n");
831 goto handle_error;
832 }
833
834 extensions = driGetDriverExtensions(psc->driver, driverName);
835 if (extensions == NULL)
836 goto handle_error;
837
838 for (i = 0; extensions[i]; i++) {
839 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
840 psc->core = (__DRIcoreExtension *) extensions[i];
841 if (strcmp(extensions[i]->name, __DRI_IMAGE_DRIVER) == 0)
842 psc->image_driver = (__DRIimageDriverExtension *) extensions[i];
843 }
844
845
846 if (psc->core == NULL) {
847 ErrorMessageF("core dri driver extension not found\n");
848 goto handle_error;
849 }
850
851 if (psc->image_driver == NULL) {
852 ErrorMessageF("image driver extension not found\n");
853 goto handle_error;
854 }
855
856 psc->driScreen =
857 psc->image_driver->createNewScreen2(screen, psc->fd,
858 pdp->loader_extensions,
859 extensions,
860 &driver_configs, psc);
861
862 if (psc->driScreen == NULL) {
863 ErrorMessageF("failed to create dri screen\n");
864 goto handle_error;
865 }
866
867 dri3_bind_extensions(psc, priv, driverName);
868
869 if (!psc->image || psc->image->base.version < 7 || !psc->image->createImageFromFds) {
870 ErrorMessageF("Version 7 or imageFromFds image extension not found\n");
871 goto handle_error;
872 }
873
874 if (!psc->f || psc->f->base.version < 4) {
875 ErrorMessageF("Version 4 or later of flush extension not found\n");
876 goto handle_error;
877 }
878
879 if (psc->is_different_gpu && psc->image->base.version < 9) {
880 ErrorMessageF("Different GPU, but image extension version 9 or later not found\n");
881 goto handle_error;
882 }
883
884 if (psc->is_different_gpu && !psc->image->blitImage) {
885 ErrorMessageF("Different GPU, but blitImage not implemented for this driver\n");
886 goto handle_error;
887 }
888
889 if (!psc->is_different_gpu && (
890 !psc->texBuffer || psc->texBuffer->base.version < 2 ||
891 !psc->texBuffer->setTexBuffer2
892 )) {
893 ErrorMessageF("Version 2 or later of texBuffer extension not found\n");
894 goto handle_error;
895 }
896
897 psc->loader_dri3_ext.core = psc->core;
898 psc->loader_dri3_ext.image_driver = psc->image_driver;
899 psc->loader_dri3_ext.flush = psc->f;
900 psc->loader_dri3_ext.tex_buffer = psc->texBuffer;
901 psc->loader_dri3_ext.image = psc->image;
902 psc->loader_dri3_ext.config = psc->config;
903
904 configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs);
905 visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
906
907 if (!configs || !visuals) {
908 ErrorMessageF("No matching fbConfigs or visuals found\n");
909 goto handle_error;
910 }
911
912 glx_config_destroy_list(psc->base.configs);
913 psc->base.configs = configs;
914 glx_config_destroy_list(psc->base.visuals);
915 psc->base.visuals = visuals;
916
917 psc->driver_configs = driver_configs;
918
919 psc->base.vtable = &dri3_screen_vtable;
920 psp = &psc->vtable;
921 psc->base.driScreen = psp;
922 psp->destroyScreen = dri3_destroy_screen;
923 psp->createDrawable = dri3_create_drawable;
924 psp->swapBuffers = dri3_swap_buffers;
925
926 psp->getDrawableMSC = dri3_drawable_get_msc;
927 psp->waitForMSC = dri3_wait_for_msc;
928 psp->waitForSBC = dri3_wait_for_sbc;
929 psp->setSwapInterval = dri3_set_swap_interval;
930 psp->getSwapInterval = dri3_get_swap_interval;
931 if (psc->config->configQueryb(psc->driScreen,
932 "glx_disable_oml_sync_control",
933 &disable) || !disable)
934 __glXEnableDirectExtension(&psc->base, "GLX_OML_sync_control");
935
936 psp->copySubBuffer = dri3_copy_sub_buffer;
937 __glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer");
938
939 psp->getBufferAge = dri3_get_buffer_age;
940 if (psc->config->configQueryb(psc->driScreen,
941 "glx_disable_ext_buffer_age",
942 &disable) || !disable)
943 __glXEnableDirectExtension(&psc->base, "GLX_EXT_buffer_age");
944
945 free(driverName);
946 free(deviceName);
947
948 tmp = getenv("LIBGL_SHOW_FPS");
949 psc->show_fps_interval = tmp ? atoi(tmp) : 0;
950 if (psc->show_fps_interval < 0)
951 psc->show_fps_interval = 0;
952
953 InfoMessageF("Using DRI3 for screen %d\n", screen);
954
955 return &psc->base;
956
957 handle_error:
958 CriticalErrorMessageF("failed to load driver: %s\n", driverName);
959
960 if (configs)
961 glx_config_destroy_list(configs);
962 if (visuals)
963 glx_config_destroy_list(visuals);
964 if (psc->driScreen)
965 psc->core->destroyScreen(psc->driScreen);
966 psc->driScreen = NULL;
967 if (psc->fd >= 0)
968 close(psc->fd);
969 if (psc->driver)
970 dlclose(psc->driver);
971
972 free(driverName);
973 free(deviceName);
974 glx_screen_cleanup(&psc->base);
975 free(psc);
976
977 return NULL;
978 }
979
980 /** dri_destroy_display
981 *
982 * Called from __glXFreeDisplayPrivate.
983 */
984 static void
985 dri3_destroy_display(__GLXDRIdisplay * dpy)
986 {
987 free(dpy);
988 }
989
990 /** dri3_create_display
991 *
992 * Allocate, initialize and return a __DRIdisplayPrivate object.
993 * This is called from __glXInitialize() when we are given a new
994 * display pointer. This is public to that function, but hidden from
995 * outside of libGL.
996 */
997 _X_HIDDEN __GLXDRIdisplay *
998 dri3_create_display(Display * dpy)
999 {
1000 struct dri3_display *pdp;
1001 xcb_connection_t *c = XGetXCBConnection(dpy);
1002 xcb_dri3_query_version_cookie_t dri3_cookie;
1003 xcb_dri3_query_version_reply_t *dri3_reply;
1004 xcb_present_query_version_cookie_t present_cookie;
1005 xcb_present_query_version_reply_t *present_reply;
1006 xcb_generic_error_t *error;
1007 const xcb_query_extension_reply_t *extension;
1008
1009 xcb_prefetch_extension_data(c, &xcb_dri3_id);
1010 xcb_prefetch_extension_data(c, &xcb_present_id);
1011
1012 extension = xcb_get_extension_data(c, &xcb_dri3_id);
1013 if (!(extension && extension->present))
1014 return NULL;
1015
1016 extension = xcb_get_extension_data(c, &xcb_present_id);
1017 if (!(extension && extension->present))
1018 return NULL;
1019
1020 dri3_cookie = xcb_dri3_query_version(c,
1021 XCB_DRI3_MAJOR_VERSION,
1022 XCB_DRI3_MINOR_VERSION);
1023
1024
1025 present_cookie = xcb_present_query_version(c,
1026 XCB_PRESENT_MAJOR_VERSION,
1027 XCB_PRESENT_MINOR_VERSION);
1028
1029 pdp = malloc(sizeof *pdp);
1030 if (pdp == NULL)
1031 return NULL;
1032
1033 dri3_reply = xcb_dri3_query_version_reply(c, dri3_cookie, &error);
1034 if (!dri3_reply) {
1035 free(error);
1036 goto no_extension;
1037 }
1038
1039 pdp->dri3Major = dri3_reply->major_version;
1040 pdp->dri3Minor = dri3_reply->minor_version;
1041 free(dri3_reply);
1042
1043 present_reply = xcb_present_query_version_reply(c, present_cookie, &error);
1044 if (!present_reply) {
1045 free(error);
1046 goto no_extension;
1047 }
1048 pdp->presentMajor = present_reply->major_version;
1049 pdp->presentMinor = present_reply->minor_version;
1050 free(present_reply);
1051
1052 pdp->base.destroyDisplay = dri3_destroy_display;
1053 pdp->base.createScreen = dri3_create_screen;
1054
1055 loader_set_logger(dri_message);
1056
1057 pdp->loader_extensions = loader_extensions;
1058
1059 return &pdp->base;
1060 no_extension:
1061 free(pdp);
1062 return NULL;
1063 }
1064
1065 #endif /* GLX_DIRECT_RENDERING */