Merge remote-tracking branch 'jekstrand/wip/i965-uniforms' 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_GLES2)) != 0)
669 __glXEnableDirectExtension(&psc->base,
670 "GLX_EXT_create_context_es2_profile");
671
672 for (i = 0; extensions[i]; i++) {
673 /* when on a different gpu than the server, the server pixmaps
674 * can have a tiling mode we can't read. Thus we can't create
675 * a texture from them.
676 */
677 if (!psc->is_different_gpu &&
678 (strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
679 psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
680 __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
681 }
682
683 if ((strcmp(extensions[i]->name, __DRI2_FLUSH) == 0)) {
684 psc->f = (__DRI2flushExtension *) extensions[i];
685 /* internal driver extension, no GL extension exposed */
686 }
687
688 if (strcmp(extensions[i]->name, __DRI_IMAGE) == 0)
689 psc->image = (__DRIimageExtension *) extensions[i];
690
691 if ((strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0))
692 psc->config = (__DRI2configQueryExtension *) extensions[i];
693
694 if (strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0)
695 __glXEnableDirectExtension(&psc->base,
696 "GLX_ARB_create_context_robustness");
697
698 if (strcmp(extensions[i]->name, __DRI2_RENDERER_QUERY) == 0) {
699 psc->rendererQuery = (__DRI2rendererQueryExtension *) extensions[i];
700 __glXEnableDirectExtension(&psc->base, "GLX_MESA_query_renderer");
701 }
702 }
703 }
704
705 static const struct glx_screen_vtable dri3_screen_vtable = {
706 .create_context = dri3_create_context,
707 .create_context_attribs = dri3_create_context_attribs,
708 .query_renderer_integer = dri3_query_renderer_integer,
709 .query_renderer_string = dri3_query_renderer_string,
710 };
711
712 /** dri3_create_screen
713 *
714 * Initialize DRI3 on the specified screen.
715 *
716 * Opens the DRI device, locates the appropriate DRI driver
717 * and loads that.
718 *
719 * Checks to see if the driver supports the necessary extensions
720 *
721 * Initializes the driver for the screen and sets up our structures
722 */
723
724 static struct glx_screen *
725 dri3_create_screen(int screen, struct glx_display * priv)
726 {
727 xcb_connection_t *c = XGetXCBConnection(priv->dpy);
728 const __DRIconfig **driver_configs;
729 const __DRIextension **extensions;
730 const struct dri3_display *const pdp = (struct dri3_display *)
731 priv->dri3Display;
732 struct dri3_screen *psc;
733 __GLXDRIscreen *psp;
734 struct glx_config *configs = NULL, *visuals = NULL;
735 char *driverName, *deviceName, *tmp;
736 int i;
737
738 psc = calloc(1, sizeof *psc);
739 if (psc == NULL)
740 return NULL;
741
742 psc->fd = -1;
743
744 if (!glx_screen_init(&psc->base, screen, priv)) {
745 free(psc);
746 return NULL;
747 }
748
749 psc->fd = loader_dri3_open(c, RootWindow(priv->dpy, screen), None);
750 if (psc->fd < 0) {
751 int conn_error = xcb_connection_has_error(c);
752
753 glx_screen_cleanup(&psc->base);
754 free(psc);
755 InfoMessageF("screen %d does not appear to be DRI3 capable\n", screen);
756
757 if (conn_error)
758 ErrorMessageF("Connection closed during DRI3 initialization failure");
759
760 return NULL;
761 }
762
763 psc->fd = loader_get_user_preferred_fd(psc->fd, &psc->is_different_gpu);
764 deviceName = NULL;
765
766 driverName = loader_get_driver_for_fd(psc->fd, 0);
767 if (!driverName) {
768 ErrorMessageF("No driver found\n");
769 goto handle_error;
770 }
771
772 psc->driver = driOpenDriver(driverName);
773 if (psc->driver == NULL) {
774 ErrorMessageF("driver pointer missing\n");
775 goto handle_error;
776 }
777
778 extensions = driGetDriverExtensions(psc->driver, driverName);
779 if (extensions == NULL)
780 goto handle_error;
781
782 for (i = 0; extensions[i]; i++) {
783 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
784 psc->core = (__DRIcoreExtension *) extensions[i];
785 if (strcmp(extensions[i]->name, __DRI_IMAGE_DRIVER) == 0)
786 psc->image_driver = (__DRIimageDriverExtension *) extensions[i];
787 }
788
789
790 if (psc->core == NULL) {
791 ErrorMessageF("core dri driver extension not found\n");
792 goto handle_error;
793 }
794
795 if (psc->image_driver == NULL) {
796 ErrorMessageF("image driver extension not found\n");
797 goto handle_error;
798 }
799
800 psc->driScreen =
801 psc->image_driver->createNewScreen2(screen, psc->fd,
802 pdp->loader_extensions,
803 extensions,
804 &driver_configs, psc);
805
806 if (psc->driScreen == NULL) {
807 ErrorMessageF("failed to create dri screen\n");
808 goto handle_error;
809 }
810
811 dri3_bind_extensions(psc, priv, driverName);
812
813 if (!psc->image || psc->image->base.version < 7 || !psc->image->createImageFromFds) {
814 ErrorMessageF("Version 7 or imageFromFds image extension not found\n");
815 goto handle_error;
816 }
817
818 if (!psc->f || psc->f->base.version < 4) {
819 ErrorMessageF("Version 4 or later of flush extension not found\n");
820 goto handle_error;
821 }
822
823 if (psc->is_different_gpu && psc->image->base.version < 9) {
824 ErrorMessageF("Different GPU, but image extension version 9 or later not found\n");
825 goto handle_error;
826 }
827
828 if (psc->is_different_gpu && !psc->image->blitImage) {
829 ErrorMessageF("Different GPU, but blitImage not implemented for this driver\n");
830 goto handle_error;
831 }
832
833 if (!psc->is_different_gpu && (
834 !psc->texBuffer || psc->texBuffer->base.version < 2 ||
835 !psc->texBuffer->setTexBuffer2
836 )) {
837 ErrorMessageF("Version 2 or later of texBuffer extension not found\n");
838 goto handle_error;
839 }
840
841 psc->loader_dri3_ext.core = psc->core;
842 psc->loader_dri3_ext.image_driver = psc->image_driver;
843 psc->loader_dri3_ext.flush = psc->f;
844 psc->loader_dri3_ext.tex_buffer = psc->texBuffer;
845 psc->loader_dri3_ext.image = psc->image;
846 psc->loader_dri3_ext.config = psc->config;
847
848 configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs);
849 visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
850
851 if (!configs || !visuals) {
852 ErrorMessageF("No matching fbConfigs or visuals found\n");
853 goto handle_error;
854 }
855
856 glx_config_destroy_list(psc->base.configs);
857 psc->base.configs = configs;
858 glx_config_destroy_list(psc->base.visuals);
859 psc->base.visuals = visuals;
860
861 psc->driver_configs = driver_configs;
862
863 psc->base.vtable = &dri3_screen_vtable;
864 psp = &psc->vtable;
865 psc->base.driScreen = psp;
866 psp->destroyScreen = dri3_destroy_screen;
867 psp->createDrawable = dri3_create_drawable;
868 psp->swapBuffers = dri3_swap_buffers;
869
870 psp->getDrawableMSC = dri3_drawable_get_msc;
871 psp->waitForMSC = dri3_wait_for_msc;
872 psp->waitForSBC = dri3_wait_for_sbc;
873 psp->setSwapInterval = dri3_set_swap_interval;
874 psp->getSwapInterval = dri3_get_swap_interval;
875 __glXEnableDirectExtension(&psc->base, "GLX_OML_sync_control");
876
877 psp->copySubBuffer = dri3_copy_sub_buffer;
878 __glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer");
879
880 psp->getBufferAge = dri3_get_buffer_age;
881 __glXEnableDirectExtension(&psc->base, "GLX_EXT_buffer_age");
882
883 free(driverName);
884 free(deviceName);
885
886 tmp = getenv("LIBGL_SHOW_FPS");
887 psc->show_fps_interval = tmp ? atoi(tmp) : 0;
888 if (psc->show_fps_interval < 0)
889 psc->show_fps_interval = 0;
890
891 InfoMessageF("Using DRI3 for screen %d\n", screen);
892
893 return &psc->base;
894
895 handle_error:
896 CriticalErrorMessageF("failed to load driver: %s\n", driverName);
897
898 if (configs)
899 glx_config_destroy_list(configs);
900 if (visuals)
901 glx_config_destroy_list(visuals);
902 if (psc->driScreen)
903 psc->core->destroyScreen(psc->driScreen);
904 psc->driScreen = NULL;
905 if (psc->fd >= 0)
906 close(psc->fd);
907 if (psc->driver)
908 dlclose(psc->driver);
909
910 free(driverName);
911 free(deviceName);
912 glx_screen_cleanup(&psc->base);
913 free(psc);
914
915 return NULL;
916 }
917
918 /** dri_destroy_display
919 *
920 * Called from __glXFreeDisplayPrivate.
921 */
922 static void
923 dri3_destroy_display(__GLXDRIdisplay * dpy)
924 {
925 free(dpy);
926 }
927
928 /** dri3_create_display
929 *
930 * Allocate, initialize and return a __DRIdisplayPrivate object.
931 * This is called from __glXInitialize() when we are given a new
932 * display pointer. This is public to that function, but hidden from
933 * outside of libGL.
934 */
935 _X_HIDDEN __GLXDRIdisplay *
936 dri3_create_display(Display * dpy)
937 {
938 struct dri3_display *pdp;
939 xcb_connection_t *c = XGetXCBConnection(dpy);
940 xcb_dri3_query_version_cookie_t dri3_cookie;
941 xcb_dri3_query_version_reply_t *dri3_reply;
942 xcb_present_query_version_cookie_t present_cookie;
943 xcb_present_query_version_reply_t *present_reply;
944 xcb_generic_error_t *error;
945 const xcb_query_extension_reply_t *extension;
946
947 xcb_prefetch_extension_data(c, &xcb_dri3_id);
948 xcb_prefetch_extension_data(c, &xcb_present_id);
949
950 extension = xcb_get_extension_data(c, &xcb_dri3_id);
951 if (!(extension && extension->present))
952 return NULL;
953
954 extension = xcb_get_extension_data(c, &xcb_present_id);
955 if (!(extension && extension->present))
956 return NULL;
957
958 dri3_cookie = xcb_dri3_query_version(c,
959 XCB_DRI3_MAJOR_VERSION,
960 XCB_DRI3_MINOR_VERSION);
961
962
963 present_cookie = xcb_present_query_version(c,
964 XCB_PRESENT_MAJOR_VERSION,
965 XCB_PRESENT_MINOR_VERSION);
966
967 pdp = malloc(sizeof *pdp);
968 if (pdp == NULL)
969 return NULL;
970
971 dri3_reply = xcb_dri3_query_version_reply(c, dri3_cookie, &error);
972 if (!dri3_reply) {
973 free(error);
974 goto no_extension;
975 }
976
977 pdp->dri3Major = dri3_reply->major_version;
978 pdp->dri3Minor = dri3_reply->minor_version;
979 free(dri3_reply);
980
981 present_reply = xcb_present_query_version_reply(c, present_cookie, &error);
982 if (!present_reply) {
983 free(error);
984 goto no_extension;
985 }
986 pdp->presentMajor = present_reply->major_version;
987 pdp->presentMinor = present_reply->minor_version;
988 free(present_reply);
989
990 pdp->base.destroyDisplay = dri3_destroy_display;
991 pdp->base.createScreen = dri3_create_screen;
992
993 loader_set_logger(dri_message);
994
995 pdp->loader_extensions = loader_extensions;
996
997 return &pdp->base;
998 no_extension:
999 free(pdp);
1000 return NULL;
1001 }
1002
1003 #endif /* GLX_DIRECT_RENDERING */