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