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