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