ee2431267319ed05a38febc4b62a4b73dfe3e977
[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 loader_dri3_wait_x(&priv->loader_drawable);
441 }
442
443 static void
444 dri3_wait_gl(struct glx_context *gc)
445 {
446 struct dri3_drawable *priv = (struct dri3_drawable *)
447 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
448
449 loader_dri3_wait_gl(&priv->loader_drawable);
450 }
451
452 /**
453 * Called by the driver when it needs to update the real front buffer with the
454 * contents of its fake front buffer.
455 */
456 static void
457 dri3_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)
458 {
459 struct loader_dri3_drawable *draw = loaderPrivate;
460 struct dri3_drawable *pdraw = loader_drawable_to_dri3_drawable(draw);
461 struct dri3_screen *psc;
462
463 if (!pdraw)
464 return;
465
466 if (!pdraw->base.psc)
467 return;
468
469 psc = (struct dri3_screen *) pdraw->base.psc;
470
471 (void) __glXInitialize(psc->base.dpy);
472
473 loader_dri3_flush(draw, __DRI2_FLUSH_DRAWABLE, __DRI2_THROTTLE_FLUSHFRONT);
474
475 loader_dri3_wait_gl(draw);
476 }
477
478 /* The image loader extension record for DRI3
479 */
480 static const __DRIimageLoaderExtension imageLoaderExtension = {
481 .base = { __DRI_IMAGE_LOADER, 1 },
482
483 .getBuffers = loader_dri3_get_buffers,
484 .flushFrontBuffer = dri3_flush_front_buffer,
485 };
486
487 const __DRIuseInvalidateExtension dri3UseInvalidate = {
488 .base = { __DRI_USE_INVALIDATE, 1 }
489 };
490
491 static const __DRIextension *loader_extensions[] = {
492 &imageLoaderExtension.base,
493 &systemTimeExtension.base,
494 &dri3UseInvalidate.base,
495 NULL
496 };
497
498 /** dri3_swap_buffers
499 *
500 * Make the current back buffer visible using the present extension
501 */
502 static int64_t
503 dri3_swap_buffers(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
504 int64_t remainder, Bool flush)
505 {
506 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
507 unsigned flags = __DRI2_FLUSH_DRAWABLE;
508
509 if (flush)
510 flags |= __DRI2_FLUSH_CONTEXT;
511
512 return loader_dri3_swap_buffers_msc(&priv->loader_drawable,
513 target_msc, divisor, remainder,
514 flags, false);
515 }
516
517 static int
518 dri3_get_buffer_age(__GLXDRIdrawable *pdraw)
519 {
520 struct dri3_drawable *priv = (struct dri3_drawable *)pdraw;
521
522 return loader_dri3_query_buffer_age(&priv->loader_drawable);
523 }
524
525 /** dri3_destroy_screen
526 */
527 static void
528 dri3_destroy_screen(struct glx_screen *base)
529 {
530 struct dri3_screen *psc = (struct dri3_screen *) base;
531
532 /* Free the direct rendering per screen data */
533 (*psc->core->destroyScreen) (psc->driScreen);
534 driDestroyConfigs(psc->driver_configs);
535 close(psc->fd);
536 free(psc);
537 }
538
539 /** dri3_set_swap_interval
540 *
541 * Record the application swap interval specification,
542 */
543 static int
544 dri3_set_swap_interval(__GLXDRIdrawable *pdraw, int interval)
545 {
546 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
547 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
548 struct dri3_screen *psc = (struct dri3_screen *) priv->base.psc;
549
550 if (psc->config)
551 psc->config->configQueryi(psc->driScreen,
552 "vblank_mode", &vblank_mode);
553
554 switch (vblank_mode) {
555 case DRI_CONF_VBLANK_NEVER:
556 if (interval != 0)
557 return GLX_BAD_VALUE;
558 break;
559 case DRI_CONF_VBLANK_ALWAYS_SYNC:
560 if (interval <= 0)
561 return GLX_BAD_VALUE;
562 break;
563 default:
564 break;
565 }
566
567 loader_dri3_set_swap_interval(&priv->loader_drawable, interval);
568
569 return 0;
570 }
571
572 /** dri3_get_swap_interval
573 *
574 * Return the stored swap interval
575 */
576 static int
577 dri3_get_swap_interval(__GLXDRIdrawable *pdraw)
578 {
579 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
580
581 return priv->swap_interval;
582 }
583
584 static void
585 dri3_bind_tex_image(Display * dpy,
586 GLXDrawable drawable,
587 int buffer, const int *attrib_list)
588 {
589 struct glx_context *gc = __glXGetCurrentContext();
590 struct dri3_context *pcp = (struct dri3_context *) gc;
591 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
592 struct dri3_drawable *pdraw = (struct dri3_drawable *) base;
593 struct dri3_screen *psc;
594
595 if (pdraw != NULL) {
596 psc = (struct dri3_screen *) base->psc;
597
598 (*psc->f->invalidate)(pdraw->loader_drawable.dri_drawable);
599
600 XSync(dpy, false);
601
602 (*psc->texBuffer->setTexBuffer2) (pcp->driContext,
603 pdraw->base.textureTarget,
604 pdraw->base.textureFormat,
605 pdraw->loader_drawable.dri_drawable);
606 }
607 }
608
609 static void
610 dri3_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer)
611 {
612 struct glx_context *gc = __glXGetCurrentContext();
613 struct dri3_context *pcp = (struct dri3_context *) gc;
614 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
615 struct dri3_drawable *pdraw = (struct dri3_drawable *) base;
616 struct dri3_screen *psc;
617
618 if (pdraw != NULL) {
619 psc = (struct dri3_screen *) base->psc;
620
621 if (psc->texBuffer->base.version >= 3 &&
622 psc->texBuffer->releaseTexBuffer != NULL)
623 (*psc->texBuffer->releaseTexBuffer) (pcp->driContext,
624 pdraw->base.textureTarget,
625 pdraw->loader_drawable.dri_drawable);
626 }
627 }
628
629 static const struct glx_context_vtable dri3_context_vtable = {
630 .destroy = dri3_destroy_context,
631 .bind = dri3_bind_context,
632 .unbind = dri3_unbind_context,
633 .wait_gl = dri3_wait_gl,
634 .wait_x = dri3_wait_x,
635 .use_x_font = DRI_glXUseXFont,
636 .bind_tex_image = dri3_bind_tex_image,
637 .release_tex_image = dri3_release_tex_image,
638 .get_proc_address = NULL,
639 };
640
641 /** dri3_bind_extensions
642 *
643 * Enable all of the extensions supported on DRI3
644 */
645 static void
646 dri3_bind_extensions(struct dri3_screen *psc, struct glx_display * priv,
647 const char *driverName)
648 {
649 const __DRIextension **extensions;
650 unsigned mask;
651 int i;
652
653 extensions = psc->core->getExtensions(psc->driScreen);
654
655 __glXEnableDirectExtension(&psc->base, "GLX_SGI_video_sync");
656 __glXEnableDirectExtension(&psc->base, "GLX_SGI_swap_control");
657 __glXEnableDirectExtension(&psc->base, "GLX_MESA_swap_control");
658 __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
659 __glXEnableDirectExtension(&psc->base, "GLX_INTEL_swap_event");
660
661 mask = psc->image_driver->getAPIMask(psc->driScreen);
662
663 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context");
664 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context_profile");
665
666 if ((mask & (1 << __DRI_API_GLES2)) != 0)
667 __glXEnableDirectExtension(&psc->base,
668 "GLX_EXT_create_context_es2_profile");
669
670 for (i = 0; extensions[i]; i++) {
671 /* when on a different gpu than the server, the server pixmaps
672 * can have a tiling mode we can't read. Thus we can't create
673 * a texture from them.
674 */
675 if (!psc->is_different_gpu &&
676 (strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
677 psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
678 __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
679 }
680
681 if ((strcmp(extensions[i]->name, __DRI2_FLUSH) == 0)) {
682 psc->f = (__DRI2flushExtension *) extensions[i];
683 /* internal driver extension, no GL extension exposed */
684 }
685
686 if (strcmp(extensions[i]->name, __DRI_IMAGE) == 0)
687 psc->image = (__DRIimageExtension *) extensions[i];
688
689 if ((strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0))
690 psc->config = (__DRI2configQueryExtension *) extensions[i];
691
692 if (strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0)
693 __glXEnableDirectExtension(&psc->base,
694 "GLX_ARB_create_context_robustness");
695
696 if (strcmp(extensions[i]->name, __DRI2_RENDERER_QUERY) == 0) {
697 psc->rendererQuery = (__DRI2rendererQueryExtension *) extensions[i];
698 __glXEnableDirectExtension(&psc->base, "GLX_MESA_query_renderer");
699 }
700 }
701 }
702
703 static const struct glx_screen_vtable dri3_screen_vtable = {
704 .create_context = dri3_create_context,
705 .create_context_attribs = dri3_create_context_attribs,
706 .query_renderer_integer = dri3_query_renderer_integer,
707 .query_renderer_string = dri3_query_renderer_string,
708 };
709
710 /** dri3_create_screen
711 *
712 * Initialize DRI3 on the specified screen.
713 *
714 * Opens the DRI device, locates the appropriate DRI driver
715 * and loads that.
716 *
717 * Checks to see if the driver supports the necessary extensions
718 *
719 * Initializes the driver for the screen and sets up our structures
720 */
721
722 static struct glx_screen *
723 dri3_create_screen(int screen, struct glx_display * priv)
724 {
725 xcb_connection_t *c = XGetXCBConnection(priv->dpy);
726 const __DRIconfig **driver_configs;
727 const __DRIextension **extensions;
728 const struct dri3_display *const pdp = (struct dri3_display *)
729 priv->dri3Display;
730 struct dri3_screen *psc;
731 __GLXDRIscreen *psp;
732 struct glx_config *configs = NULL, *visuals = NULL;
733 char *driverName, *deviceName, *tmp;
734 int i;
735
736 psc = calloc(1, sizeof *psc);
737 if (psc == NULL)
738 return NULL;
739
740 psc->fd = -1;
741
742 if (!glx_screen_init(&psc->base, screen, priv)) {
743 free(psc);
744 return NULL;
745 }
746
747 psc->fd = loader_dri3_open(c, RootWindow(priv->dpy, screen), None);
748 if (psc->fd < 0) {
749 int conn_error = xcb_connection_has_error(c);
750
751 glx_screen_cleanup(&psc->base);
752 free(psc);
753 InfoMessageF("screen %d does not appear to be DRI3 capable\n", screen);
754
755 if (conn_error)
756 ErrorMessageF("Connection closed during DRI3 initialization failure");
757
758 return NULL;
759 }
760
761 psc->fd = loader_get_user_preferred_fd(psc->fd, &psc->is_different_gpu);
762 deviceName = NULL;
763
764 driverName = loader_get_driver_for_fd(psc->fd, 0);
765 if (!driverName) {
766 ErrorMessageF("No driver found\n");
767 goto handle_error;
768 }
769
770 psc->driver = driOpenDriver(driverName);
771 if (psc->driver == NULL) {
772 ErrorMessageF("driver pointer missing\n");
773 goto handle_error;
774 }
775
776 extensions = driGetDriverExtensions(psc->driver, driverName);
777 if (extensions == NULL)
778 goto handle_error;
779
780 for (i = 0; extensions[i]; i++) {
781 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
782 psc->core = (__DRIcoreExtension *) extensions[i];
783 if (strcmp(extensions[i]->name, __DRI_IMAGE_DRIVER) == 0)
784 psc->image_driver = (__DRIimageDriverExtension *) extensions[i];
785 }
786
787
788 if (psc->core == NULL) {
789 ErrorMessageF("core dri driver extension not found\n");
790 goto handle_error;
791 }
792
793 if (psc->image_driver == NULL) {
794 ErrorMessageF("image driver extension not found\n");
795 goto handle_error;
796 }
797
798 psc->driScreen =
799 psc->image_driver->createNewScreen2(screen, psc->fd,
800 pdp->loader_extensions,
801 extensions,
802 &driver_configs, psc);
803
804 if (psc->driScreen == NULL) {
805 ErrorMessageF("failed to create dri screen\n");
806 goto handle_error;
807 }
808
809 dri3_bind_extensions(psc, priv, driverName);
810
811 if (!psc->image || psc->image->base.version < 7 || !psc->image->createImageFromFds) {
812 ErrorMessageF("Version 7 or imageFromFds image extension not found\n");
813 goto handle_error;
814 }
815
816 if (!psc->f || psc->f->base.version < 4) {
817 ErrorMessageF("Version 4 or later of flush extension not found\n");
818 goto handle_error;
819 }
820
821 if (psc->is_different_gpu && psc->image->base.version < 9) {
822 ErrorMessageF("Different GPU, but image extension version 9 or later not found\n");
823 goto handle_error;
824 }
825
826 if (psc->is_different_gpu && !psc->image->blitImage) {
827 ErrorMessageF("Different GPU, but blitImage not implemented for this driver\n");
828 goto handle_error;
829 }
830
831 if (!psc->is_different_gpu && (
832 !psc->texBuffer || psc->texBuffer->base.version < 2 ||
833 !psc->texBuffer->setTexBuffer2
834 )) {
835 ErrorMessageF("Version 2 or later of texBuffer extension not found\n");
836 goto handle_error;
837 }
838
839 psc->loader_dri3_ext.core = psc->core;
840 psc->loader_dri3_ext.image_driver = psc->image_driver;
841 psc->loader_dri3_ext.flush = psc->f;
842 psc->loader_dri3_ext.tex_buffer = psc->texBuffer;
843 psc->loader_dri3_ext.image = psc->image;
844 psc->loader_dri3_ext.config = psc->config;
845
846 configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs);
847 visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
848
849 if (!configs || !visuals) {
850 ErrorMessageF("No matching fbConfigs or visuals found\n");
851 goto handle_error;
852 }
853
854 glx_config_destroy_list(psc->base.configs);
855 psc->base.configs = configs;
856 glx_config_destroy_list(psc->base.visuals);
857 psc->base.visuals = visuals;
858
859 psc->driver_configs = driver_configs;
860
861 psc->base.vtable = &dri3_screen_vtable;
862 psp = &psc->vtable;
863 psc->base.driScreen = psp;
864 psp->destroyScreen = dri3_destroy_screen;
865 psp->createDrawable = dri3_create_drawable;
866 psp->swapBuffers = dri3_swap_buffers;
867
868 psp->getDrawableMSC = dri3_drawable_get_msc;
869 psp->waitForMSC = dri3_wait_for_msc;
870 psp->waitForSBC = dri3_wait_for_sbc;
871 psp->setSwapInterval = dri3_set_swap_interval;
872 psp->getSwapInterval = dri3_get_swap_interval;
873 __glXEnableDirectExtension(&psc->base, "GLX_OML_sync_control");
874
875 psp->copySubBuffer = dri3_copy_sub_buffer;
876 __glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer");
877
878 psp->getBufferAge = dri3_get_buffer_age;
879 __glXEnableDirectExtension(&psc->base, "GLX_EXT_buffer_age");
880
881 free(driverName);
882 free(deviceName);
883
884 tmp = getenv("LIBGL_SHOW_FPS");
885 psc->show_fps_interval = tmp ? atoi(tmp) : 0;
886 if (psc->show_fps_interval < 0)
887 psc->show_fps_interval = 0;
888
889 InfoMessageF("Using DRI3 for screen %d\n", screen);
890
891 return &psc->base;
892
893 handle_error:
894 CriticalErrorMessageF("failed to load driver: %s\n", driverName);
895
896 if (configs)
897 glx_config_destroy_list(configs);
898 if (visuals)
899 glx_config_destroy_list(visuals);
900 if (psc->driScreen)
901 psc->core->destroyScreen(psc->driScreen);
902 psc->driScreen = NULL;
903 if (psc->fd >= 0)
904 close(psc->fd);
905 if (psc->driver)
906 dlclose(psc->driver);
907
908 free(driverName);
909 free(deviceName);
910 glx_screen_cleanup(&psc->base);
911 free(psc);
912
913 return NULL;
914 }
915
916 /** dri_destroy_display
917 *
918 * Called from __glXFreeDisplayPrivate.
919 */
920 static void
921 dri3_destroy_display(__GLXDRIdisplay * dpy)
922 {
923 free(dpy);
924 }
925
926 /** dri3_create_display
927 *
928 * Allocate, initialize and return a __DRIdisplayPrivate object.
929 * This is called from __glXInitialize() when we are given a new
930 * display pointer. This is public to that function, but hidden from
931 * outside of libGL.
932 */
933 _X_HIDDEN __GLXDRIdisplay *
934 dri3_create_display(Display * dpy)
935 {
936 struct dri3_display *pdp;
937 xcb_connection_t *c = XGetXCBConnection(dpy);
938 xcb_dri3_query_version_cookie_t dri3_cookie;
939 xcb_dri3_query_version_reply_t *dri3_reply;
940 xcb_present_query_version_cookie_t present_cookie;
941 xcb_present_query_version_reply_t *present_reply;
942 xcb_generic_error_t *error;
943 const xcb_query_extension_reply_t *extension;
944
945 xcb_prefetch_extension_data(c, &xcb_dri3_id);
946 xcb_prefetch_extension_data(c, &xcb_present_id);
947
948 extension = xcb_get_extension_data(c, &xcb_dri3_id);
949 if (!(extension && extension->present))
950 return NULL;
951
952 extension = xcb_get_extension_data(c, &xcb_present_id);
953 if (!(extension && extension->present))
954 return NULL;
955
956 dri3_cookie = xcb_dri3_query_version(c,
957 XCB_DRI3_MAJOR_VERSION,
958 XCB_DRI3_MINOR_VERSION);
959
960
961 present_cookie = xcb_present_query_version(c,
962 XCB_PRESENT_MAJOR_VERSION,
963 XCB_PRESENT_MINOR_VERSION);
964
965 pdp = malloc(sizeof *pdp);
966 if (pdp == NULL)
967 return NULL;
968
969 dri3_reply = xcb_dri3_query_version_reply(c, dri3_cookie, &error);
970 if (!dri3_reply) {
971 free(error);
972 goto no_extension;
973 }
974
975 pdp->dri3Major = dri3_reply->major_version;
976 pdp->dri3Minor = dri3_reply->minor_version;
977 free(dri3_reply);
978
979 present_reply = xcb_present_query_version_reply(c, present_cookie, &error);
980 if (!present_reply) {
981 free(error);
982 goto no_extension;
983 }
984 pdp->presentMajor = present_reply->major_version;
985 pdp->presentMinor = present_reply->minor_version;
986 free(present_reply);
987
988 pdp->base.destroyDisplay = dri3_destroy_display;
989 pdp->base.createScreen = dri3_create_screen;
990
991 loader_set_logger(dri_message);
992
993 pdp->loader_extensions = loader_extensions;
994
995 return &pdp->base;
996 no_extension:
997 free(pdp);
998 return NULL;
999 }
1000
1001 #endif /* GLX_DIRECT_RENDERING */