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