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