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