dri3: Switch to libxshmfence version 1.1
[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 "glapi.h"
69 #include "glxclient.h"
70 #include "xf86dri.h"
71 #include <dlfcn.h>
72 #include <fcntl.h>
73 #include <unistd.h>
74 #include <sys/types.h>
75 #include <sys/mman.h>
76 #include <sys/time.h>
77
78 #include "xf86drm.h"
79 #include "dri_common.h"
80 #include "dri3_priv.h"
81
82 static const struct glx_context_vtable dri3_context_vtable;
83
84 static inline void
85 dri3_fence_reset(xcb_connection_t *c, struct dri3_buffer *buffer)
86 {
87 xshmfence_reset(buffer->shm_fence);
88 }
89
90 static inline void
91 dri3_fence_set(struct dri3_buffer *buffer)
92 {
93 xshmfence_trigger(buffer->shm_fence);
94 }
95
96 static inline void
97 dri3_fence_trigger(xcb_connection_t *c, struct dri3_buffer *buffer)
98 {
99 xcb_sync_trigger_fence(c, buffer->sync_fence);
100 }
101
102 static inline void
103 dri3_fence_await(xcb_connection_t *c, struct dri3_buffer *buffer)
104 {
105 xcb_flush(c);
106 xshmfence_await(buffer->shm_fence);
107 }
108
109 static inline Bool
110 dri3_fence_triggered(struct dri3_buffer *buffer)
111 {
112 return xshmfence_query(buffer->shm_fence);
113 }
114
115 static void
116 dri3_destroy_context(struct glx_context *context)
117 {
118 struct dri3_context *pcp = (struct dri3_context *) context;
119 struct dri3_screen *psc = (struct dri3_screen *) context->psc;
120
121 driReleaseDrawables(&pcp->base);
122
123 free((char *) context->extensions);
124
125 (*psc->core->destroyContext) (pcp->driContext);
126
127 free(pcp);
128 }
129
130 static Bool
131 dri3_bind_context(struct glx_context *context, struct glx_context *old,
132 GLXDrawable draw, GLXDrawable read)
133 {
134 struct dri3_context *pcp = (struct dri3_context *) context;
135 struct dri3_screen *psc = (struct dri3_screen *) pcp->base.psc;
136 struct dri3_drawable *pdraw, *pread;
137
138 pdraw = (struct dri3_drawable *) driFetchDrawable(context, draw);
139 pread = (struct dri3_drawable *) driFetchDrawable(context, read);
140
141 driReleaseDrawables(&pcp->base);
142
143 if (pdraw == NULL || pread == NULL)
144 return GLXBadDrawable;
145
146 if (!(*psc->core->bindContext) (pcp->driContext,
147 pdraw->driDrawable, pread->driDrawable))
148 return GLXBadContext;
149
150 return Success;
151 }
152
153 static void
154 dri3_unbind_context(struct glx_context *context, struct glx_context *new)
155 {
156 struct dri3_context *pcp = (struct dri3_context *) context;
157 struct dri3_screen *psc = (struct dri3_screen *) pcp->base.psc;
158
159 (*psc->core->unbindContext) (pcp->driContext);
160 }
161
162 static struct glx_context *
163 dri3_create_context_attribs(struct glx_screen *base,
164 struct glx_config *config_base,
165 struct glx_context *shareList,
166 unsigned num_attribs,
167 const uint32_t *attribs,
168 unsigned *error)
169 {
170 struct dri3_context *pcp = NULL;
171 struct dri3_context *pcp_shared = NULL;
172 struct dri3_screen *psc = (struct dri3_screen *) base;
173 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
174 __DRIcontext *shared = NULL;
175
176 uint32_t minor_ver = 1;
177 uint32_t major_ver = 2;
178 uint32_t flags = 0;
179 unsigned api;
180 int reset = __DRI_CTX_RESET_NO_NOTIFICATION;
181 uint32_t ctx_attribs[2 * 5];
182 unsigned num_ctx_attribs = 0;
183 uint32_t render_type;
184
185 /* Remap the GLX tokens to DRI2 tokens.
186 */
187 if (!dri2_convert_glx_attribs(num_attribs, attribs,
188 &major_ver, &minor_ver,
189 &render_type, &flags, &api,
190 &reset, error))
191 goto error_exit;
192
193 /* Check the renderType value */
194 if (!validate_renderType_against_config(config_base, render_type))
195 goto error_exit;
196
197 if (shareList) {
198 pcp_shared = (struct dri3_context *) shareList;
199 shared = pcp_shared->driContext;
200 }
201
202 pcp = calloc(1, sizeof *pcp);
203 if (pcp == NULL) {
204 *error = __DRI_CTX_ERROR_NO_MEMORY;
205 goto error_exit;
206 }
207
208 if (!glx_context_init(&pcp->base, &psc->base, &config->base))
209 goto error_exit;
210
211 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
212 ctx_attribs[num_ctx_attribs++] = major_ver;
213 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
214 ctx_attribs[num_ctx_attribs++] = minor_ver;
215
216 /* Only send a value when the non-default value is requested. By doing
217 * this we don't have to check the driver's DRI3 version before sending the
218 * default value.
219 */
220 if (reset != __DRI_CTX_RESET_NO_NOTIFICATION) {
221 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
222 ctx_attribs[num_ctx_attribs++] = reset;
223 }
224
225 if (flags != 0) {
226 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_FLAGS;
227
228 /* The current __DRI_CTX_FLAG_* values are identical to the
229 * GLX_CONTEXT_*_BIT values.
230 */
231 ctx_attribs[num_ctx_attribs++] = flags;
232 }
233
234 pcp->driContext =
235 (*psc->image_driver->createContextAttribs) (psc->driScreen,
236 api,
237 config->driConfig,
238 shared,
239 num_ctx_attribs / 2,
240 ctx_attribs,
241 error,
242 pcp);
243
244 if (pcp->driContext == NULL)
245 goto error_exit;
246
247 pcp->base.vtable = &dri3_context_vtable;
248
249 return &pcp->base;
250
251 error_exit:
252 free(pcp);
253
254 return NULL;
255 }
256
257 static struct glx_context *
258 dri3_create_context(struct glx_screen *base,
259 struct glx_config *config_base,
260 struct glx_context *shareList, int renderType)
261 {
262 unsigned int error;
263
264 return dri3_create_context_attribs(base, config_base, shareList,
265 0, NULL, &error);
266 }
267
268 static void
269 dri3_destroy_drawable(__GLXDRIdrawable *base)
270 {
271 struct dri3_screen *psc = (struct dri3_screen *) base->psc;
272 struct dri3_drawable *pdraw = (struct dri3_drawable *) base;
273
274 (*psc->core->destroyDrawable) (pdraw->driDrawable);
275
276 free(pdraw);
277 }
278
279 static __GLXDRIdrawable *
280 dri3_create_drawable(struct glx_screen *base, XID xDrawable,
281 GLXDrawable drawable, struct glx_config *config_base)
282 {
283 struct dri3_drawable *pdraw;
284 struct dri3_screen *psc = (struct dri3_screen *) base;
285 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
286 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
287
288 pdraw = calloc(1, sizeof(*pdraw));
289 if (!pdraw)
290 return NULL;
291
292 pdraw->base.destroyDrawable = dri3_destroy_drawable;
293 pdraw->base.xDrawable = xDrawable;
294 pdraw->base.drawable = drawable;
295 pdraw->base.psc = &psc->base;
296 pdraw->swap_interval = 1; /* default may be overridden below */
297 pdraw->have_back = 0;
298 pdraw->have_fake_front = 0;
299
300 if (psc->config)
301 psc->config->configQueryi(psc->driScreen,
302 "vblank_mode", &vblank_mode);
303
304 switch (vblank_mode) {
305 case DRI_CONF_VBLANK_NEVER:
306 case DRI_CONF_VBLANK_DEF_INTERVAL_0:
307 pdraw->swap_interval = 0;
308 break;
309 case DRI_CONF_VBLANK_DEF_INTERVAL_1:
310 case DRI_CONF_VBLANK_ALWAYS_SYNC:
311 default:
312 pdraw->swap_interval = 1;
313 break;
314 }
315
316 (void) __glXInitialize(psc->base.dpy);
317
318 /* Create a new drawable */
319 pdraw->driDrawable =
320 (*psc->image_driver->createNewDrawable) (psc->driScreen,
321 config->driConfig, pdraw);
322
323 if (!pdraw->driDrawable) {
324 free(pdraw);
325 return NULL;
326 }
327
328 /*
329 * Make sure server has the same swap interval we do for the new
330 * drawable.
331 */
332 if (psc->vtable.setSwapInterval)
333 psc->vtable.setSwapInterval(&pdraw->base, pdraw->swap_interval);
334
335 return &pdraw->base;
336 }
337
338 /*
339 * Process one Present event
340 */
341 static void
342 dri3_handle_present_event(struct dri3_drawable *priv, xcb_present_generic_event_t *ge)
343 {
344 switch (ge->evtype) {
345 case XCB_PRESENT_CONFIGURE_NOTIFY: {
346 xcb_present_configure_notify_event_t *ce = (void *) ge;
347
348 priv->width = ce->width;
349 priv->height = ce->height;
350 break;
351 }
352 case XCB_PRESENT_COMPLETE_NOTIFY: {
353 xcb_present_complete_notify_event_t *ce = (void *) ge;
354
355 if (ce->kind == XCB_PRESENT_COMPLETE_KIND_PIXMAP)
356 priv->present_event_serial = ce->serial;
357 else
358 priv->present_msc_event_serial = ce->serial;
359 priv->ust = ce->ust;
360 priv->msc = ce->msc;
361 break;
362 }
363 case XCB_PRESENT_EVENT_IDLE_NOTIFY: {
364 xcb_present_idle_notify_event_t *ie = (void *) ge;
365 int b;
366
367 for (b = 0; b < sizeof (priv->buffers) / sizeof (priv->buffers[0]); b++) {
368 struct dri3_buffer *buf = priv->buffers[b];
369
370 if (buf && buf->pixmap == ie->pixmap) {
371 buf->busy = 0;
372 break;
373 }
374 }
375 break;
376 }
377 }
378 free(ge);
379 }
380
381 static int
382 dri3_wait_for_msc(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
383 int64_t remainder, int64_t *ust, int64_t *msc, int64_t *sbc)
384 {
385 xcb_connection_t *c = XGetXCBConnection(pdraw->psc->dpy);
386 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
387 xcb_generic_event_t *ev;
388 xcb_present_generic_event_t *ge;
389
390 /* Ask for the an event for the target MSC */
391 ++priv->present_msc_request_serial;
392 xcb_present_notify_msc(c,
393 priv->base.xDrawable,
394 priv->present_msc_request_serial,
395 target_msc,
396 divisor,
397 remainder);
398
399 xcb_flush(c);
400
401 /* Wait for the event */
402 if (priv->special_event) {
403 while (priv->present_msc_request_serial != priv->present_msc_event_serial) {
404 ev = xcb_wait_for_special_event(c, priv->special_event);
405 if (!ev)
406 break;
407 ge = (void *) ev;
408 dri3_handle_present_event(priv, ge);
409 }
410 }
411
412 *ust = priv->ust;
413 *msc = priv->msc;
414 *sbc = priv->sbc;
415
416 return 1;
417 }
418
419 static int
420 dri3_drawable_get_msc(struct glx_screen *psc, __GLXDRIdrawable *pdraw,
421 int64_t *ust, int64_t *msc, int64_t *sbc)
422 {
423 return dri3_wait_for_msc(pdraw, 0, 0, 0, ust, msc,sbc);
424 }
425
426 /** dri3_wait_for_sbc
427 *
428 * Wait for the swap buffer count to increase. The only way this
429 * can happen is if some other thread is doing swap buffers as
430 * we no longer share swap buffer counts with other processes.
431 *
432 * I'm not sure this is actually useful as such, and so this
433 * implementation is a kludge that just polls once a second
434 */
435 static int
436 dri3_wait_for_sbc(__GLXDRIdrawable *pdraw, int64_t target_sbc, int64_t *ust,
437 int64_t *msc, int64_t *sbc)
438 {
439 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
440
441 while (priv->sbc < target_sbc) {
442 sleep(1);
443 }
444 return dri3_wait_for_msc(pdraw, 0, 0, 0, ust, msc, sbc);
445 }
446
447 /**
448 * Asks the driver to flush any queued work necessary for serializing with the
449 * X command stream, and optionally the slightly more strict requirement of
450 * glFlush() equivalence (which would require flushing even if nothing had
451 * been drawn to a window system framebuffer, for example).
452 */
453 static void
454 dri3_flush(struct dri3_screen *psc,
455 struct dri3_drawable *draw,
456 unsigned flags,
457 enum __DRI2throttleReason throttle_reason)
458 {
459 struct glx_context *gc = __glXGetCurrentContext();
460
461 if (gc) {
462 struct dri3_context *dri3Ctx = (struct dri3_context *)gc;
463
464 (*psc->f->flush_with_flags)(dri3Ctx->driContext, draw->driDrawable, flags, throttle_reason);
465 }
466 }
467
468 static xcb_gcontext_t
469 dri3_drawable_gc(struct dri3_drawable *priv)
470 {
471 if (!priv->gc) {
472 uint32_t v;
473 xcb_connection_t *c = XGetXCBConnection(priv->base.psc->dpy);
474
475 v = 0;
476 xcb_create_gc(c,
477 (priv->gc = xcb_generate_id(c)),
478 priv->base.xDrawable,
479 XCB_GC_GRAPHICS_EXPOSURES,
480 &v);
481 }
482 return priv->gc;
483 }
484
485 static struct dri3_buffer *
486 dri3_back_buffer(struct dri3_drawable *priv)
487 {
488 return priv->buffers[DRI3_BACK_ID(priv->cur_back)];
489 }
490
491 static struct dri3_buffer *
492 dri3_fake_front_buffer(struct dri3_drawable *priv)
493 {
494 return priv->buffers[DRI3_FRONT_ID];
495 }
496
497 static void
498 dri3_copy_area (xcb_connection_t *c /**< */,
499 xcb_drawable_t src_drawable /**< */,
500 xcb_drawable_t dst_drawable /**< */,
501 xcb_gcontext_t gc /**< */,
502 int16_t src_x /**< */,
503 int16_t src_y /**< */,
504 int16_t dst_x /**< */,
505 int16_t dst_y /**< */,
506 uint16_t width /**< */,
507 uint16_t height /**< */)
508 {
509 xcb_void_cookie_t cookie;
510
511 cookie = xcb_copy_area_checked(c,
512 src_drawable,
513 dst_drawable,
514 gc,
515 src_x,
516 src_y,
517 dst_x,
518 dst_y,
519 width,
520 height);
521 xcb_discard_reply(c, cookie.sequence);
522 }
523
524 static void
525 dri3_copy_sub_buffer(__GLXDRIdrawable *pdraw, int x, int y,
526 int width, int height,
527 Bool flush)
528 {
529 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
530 struct dri3_screen *psc = (struct dri3_screen *) pdraw->psc;
531 xcb_connection_t *c = XGetXCBConnection(priv->base.psc->dpy);
532 struct dri3_buffer *back = dri3_back_buffer(priv);
533
534 unsigned flags;
535
536 /* Check we have the right attachments */
537 if (!priv->have_back || priv->is_pixmap)
538 return;
539
540 flags = __DRI2_FLUSH_DRAWABLE;
541 if (flush)
542 flags |= __DRI2_FLUSH_CONTEXT;
543 dri3_flush(psc, priv, flags, __DRI2_THROTTLE_SWAPBUFFER);
544
545 y = priv->height - y - height;
546
547 dri3_fence_reset(c, back);
548 dri3_copy_area(c,
549 dri3_back_buffer(priv)->pixmap,
550 priv->base.xDrawable,
551 dri3_drawable_gc(priv),
552 x, y, x, y, width, height);
553 dri3_fence_trigger(c, back);
554 /* Refresh the fake front (if present) after we just damaged the real
555 * front.
556 */
557 if (priv->have_fake_front) {
558 dri3_fence_reset(c, dri3_fake_front_buffer(priv));
559 dri3_copy_area(c,
560 dri3_back_buffer(priv)->pixmap,
561 dri3_fake_front_buffer(priv)->pixmap,
562 dri3_drawable_gc(priv),
563 x, y, x, y, width, height);
564 dri3_fence_trigger(c, dri3_fake_front_buffer(priv));
565 dri3_fence_await(c, dri3_fake_front_buffer(priv));
566 }
567 dri3_fence_await(c, back);
568 }
569
570 static void
571 dri3_copy_drawable(struct dri3_drawable *priv, Drawable dest, Drawable src)
572 {
573 struct dri3_screen *psc = (struct dri3_screen *) priv->base.psc;
574 xcb_connection_t *c = XGetXCBConnection(priv->base.psc->dpy);
575
576 dri3_flush(psc, priv, __DRI2_FLUSH_DRAWABLE, 0);
577
578 dri3_fence_reset(c, dri3_fake_front_buffer(priv));
579 dri3_copy_area(c,
580 src, dest,
581 dri3_drawable_gc(priv),
582 0, 0, 0, 0, priv->width, priv->height);
583 dri3_fence_trigger(c, dri3_fake_front_buffer(priv));
584 dri3_fence_await(c, dri3_fake_front_buffer(priv));
585 }
586
587 static void
588 dri3_wait_x(struct glx_context *gc)
589 {
590 struct dri3_drawable *priv = (struct dri3_drawable *)
591 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
592
593 if (priv == NULL || !priv->have_fake_front)
594 return;
595
596 dri3_copy_drawable(priv, dri3_fake_front_buffer(priv)->pixmap, priv->base.xDrawable);
597 }
598
599 static void
600 dri3_wait_gl(struct glx_context *gc)
601 {
602 struct dri3_drawable *priv = (struct dri3_drawable *)
603 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
604
605 if (priv == NULL || !priv->have_fake_front)
606 return;
607
608 dri3_copy_drawable(priv, priv->base.xDrawable, dri3_fake_front_buffer(priv)->pixmap);
609 }
610
611 /**
612 * Called by the driver when it needs to update the real front buffer with the
613 * contents of its fake front buffer.
614 */
615 static void
616 dri3_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)
617 {
618 struct glx_context *gc;
619 struct dri3_drawable *pdraw = loaderPrivate;
620 struct dri3_screen *psc;
621
622 if (!pdraw)
623 return;
624
625 if (!pdraw->base.psc)
626 return;
627
628 psc = (struct dri3_screen *) pdraw->base.psc;
629
630 (void) __glXInitialize(psc->base.dpy);
631
632 gc = __glXGetCurrentContext();
633
634 dri3_flush(psc, pdraw, __DRI2_FLUSH_DRAWABLE, __DRI2_THROTTLE_FLUSHFRONT);
635
636 dri3_wait_gl(gc);
637 }
638
639 static uint32_t
640 dri3_cpp_for_format(uint32_t format) {
641 switch (format) {
642 case __DRI_IMAGE_FORMAT_R8:
643 return 1;
644 case __DRI_IMAGE_FORMAT_RGB565:
645 case __DRI_IMAGE_FORMAT_GR88:
646 return 2;
647 case __DRI_IMAGE_FORMAT_XRGB8888:
648 case __DRI_IMAGE_FORMAT_ARGB8888:
649 case __DRI_IMAGE_FORMAT_ABGR8888:
650 case __DRI_IMAGE_FORMAT_XBGR8888:
651 case __DRI_IMAGE_FORMAT_XRGB2101010:
652 case __DRI_IMAGE_FORMAT_ARGB2101010:
653 case __DRI_IMAGE_FORMAT_SARGB8:
654 return 4;
655 case __DRI_IMAGE_FORMAT_NONE:
656 default:
657 return 0;
658 }
659 }
660
661
662 /** dri3_alloc_render_buffer
663 *
664 * Use the driver createImage function to construct a __DRIimage, then
665 * get a file descriptor for that and create an X pixmap from that
666 *
667 * Allocate an xshmfence for synchronization
668 */
669 static struct dri3_buffer *
670 dri3_alloc_render_buffer(struct glx_screen *glx_screen, Drawable draw,
671 unsigned int format, int width, int height, int depth)
672 {
673 struct dri3_screen *psc = (struct dri3_screen *) glx_screen;
674 Display *dpy = glx_screen->dpy;
675 struct dri3_buffer *buffer;
676 xcb_connection_t *c = XGetXCBConnection(dpy);
677 xcb_pixmap_t pixmap;
678 xcb_sync_fence_t sync_fence;
679 struct xshmfence *shm_fence;
680 int buffer_fd, fence_fd;
681 int stride;
682
683 /* Create an xshmfence object and
684 * prepare to send that to the X server
685 */
686
687 fence_fd = xshmfence_alloc_shm();
688 if (fence_fd < 0)
689 return NULL;
690 shm_fence = xshmfence_map_shm(fence_fd);
691 if (shm_fence == NULL)
692 goto no_shm_fence;
693
694 /* Allocate the image from the driver
695 */
696 buffer = calloc(1, sizeof (struct dri3_buffer));
697 if (!buffer)
698 goto no_buffer;
699
700 buffer->cpp = dri3_cpp_for_format(format);
701 if (!buffer->cpp)
702 goto no_image;
703
704 buffer->image = (*psc->image->createImage) (psc->driScreen,
705 width, height,
706 format,
707 __DRI_IMAGE_USE_SHARE|__DRI_IMAGE_USE_SCANOUT,
708 buffer);
709
710
711 if (!buffer->image)
712 goto no_image;
713
714 /* X wants the stride, so ask the image for it
715 */
716 if (!(*psc->image->queryImage)(buffer->image, __DRI_IMAGE_ATTRIB_STRIDE, &stride))
717 goto no_buffer_attrib;
718
719 buffer->pitch = stride;
720
721 if (!(*psc->image->queryImage)(buffer->image, __DRI_IMAGE_ATTRIB_FD, &buffer_fd))
722 goto no_buffer_attrib;
723
724 xcb_dri3_pixmap_from_buffer(c,
725 (pixmap = xcb_generate_id(c)),
726 draw,
727 buffer->size,
728 width, height, buffer->pitch,
729 depth, buffer->cpp * 8,
730 buffer_fd);
731
732 xcb_dri3_fence_from_fd(c,
733 pixmap,
734 (sync_fence = xcb_generate_id(c)),
735 false,
736 fence_fd);
737
738 buffer->pixmap = pixmap;
739 buffer->sync_fence = sync_fence;
740 buffer->shm_fence = shm_fence;
741 buffer->width = width;
742 buffer->height = height;
743
744 /* Mark the buffer as idle
745 */
746 dri3_fence_set(buffer);
747
748 return buffer;
749
750 no_buffer_attrib:
751 (*psc->image->destroyImage)(buffer->image);
752 no_image:
753 free(buffer);
754 no_buffer:
755 xshmfence_unmap_shm(shm_fence);
756 no_shm_fence:
757 close(fence_fd);
758 return NULL;
759 }
760
761 /** dri3_free_render_buffer
762 *
763 * Free everything associated with one render buffer including pixmap, fence
764 * stuff and the driver image
765 */
766 static void
767 dri3_free_render_buffer(struct dri3_drawable *pdraw, struct dri3_buffer *buffer)
768 {
769 struct dri3_screen *psc = (struct dri3_screen *) pdraw->base.psc;
770 xcb_connection_t *c = XGetXCBConnection(pdraw->base.psc->dpy);
771
772 xcb_free_pixmap(c, buffer->pixmap);
773 xcb_sync_destroy_fence(c, buffer->sync_fence);
774 xshmfence_unmap_shm(buffer->shm_fence);
775 (*psc->image->destroyImage)(buffer->image);
776 free(buffer);
777 }
778
779
780 /** dri3_flush_present_events
781 *
782 * Process any present events that have been received from the X server
783 */
784 static void
785 dri3_flush_present_events(struct dri3_drawable *priv)
786 {
787 xcb_connection_t *c = XGetXCBConnection(priv->base.psc->dpy);
788
789 /* Check to see if any configuration changes have occurred
790 * since we were last invoked
791 */
792 if (priv->special_event) {
793 xcb_generic_event_t *ev;
794
795 while ((ev = xcb_poll_for_special_event(c, priv->special_event)) != NULL) {
796 xcb_present_generic_event_t *ge = (void *) ev;
797 dri3_handle_present_event(priv, ge);
798 }
799 }
800 }
801
802 /** dri3_update_drawable
803 *
804 * Called the first time we use the drawable and then
805 * after we receive present configure notify events to
806 * track the geometry of the drawable
807 */
808 static int
809 dri3_update_drawable(__DRIdrawable *driDrawable, void *loaderPrivate)
810 {
811 struct dri3_drawable *priv = loaderPrivate;
812 xcb_connection_t *c = XGetXCBConnection(priv->base.psc->dpy);
813
814 /* First time through, go get the current drawable geometry
815 */
816 if (priv->width == 0 || priv->height == 0 || priv->depth == 0) {
817 xcb_get_geometry_cookie_t geom_cookie;
818 xcb_get_geometry_reply_t *geom_reply;
819 xcb_void_cookie_t cookie;
820 xcb_generic_error_t *error;
821
822 /* Try to select for input on the window.
823 *
824 * If the drawable is a window, this will get our events
825 * delivered.
826 *
827 * Otherwise, we'll get a BadWindow error back from this request which
828 * will let us know that the drawable is a pixmap instead.
829 */
830
831
832 cookie = xcb_present_select_input_checked(c,
833 (priv->eid = xcb_generate_id(c)),
834 priv->base.xDrawable,
835 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY|
836 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY|
837 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY);
838
839 /* Create an XCB event queue to hold present events outside of the usual
840 * application event queue
841 */
842 priv->special_event = xcb_register_for_special_xge(c,
843 &xcb_present_id,
844 priv->eid,
845 priv->stamp);
846
847 geom_cookie = xcb_get_geometry(c, priv->base.xDrawable);
848
849 geom_reply = xcb_get_geometry_reply(c, geom_cookie, NULL);
850
851 if (!geom_reply)
852 return false;
853
854 priv->width = geom_reply->width;
855 priv->height = geom_reply->height;
856 priv->depth = geom_reply->depth;
857 priv->is_pixmap = false;
858
859 free(geom_reply);
860
861 /* Check to see if our select input call failed. If it failed with a
862 * BadWindow error, then assume the drawable is a pixmap. Destroy the
863 * special event queue created above and mark the drawable as a pixmap
864 */
865
866 error = xcb_request_check(c, cookie);
867
868 if (error) {
869 if (error->error_code != BadWindow) {
870 free(error);
871 return false;
872 }
873 priv->is_pixmap = true;
874 xcb_unregister_for_special_event(c, priv->special_event);
875 priv->special_event = NULL;
876 }
877 }
878 dri3_flush_present_events(priv);
879 return true;
880 }
881
882 /* the DRIimage createImage function takes __DRI_IMAGE_FORMAT codes, while
883 * the createImageFromFds call takes __DRI_IMAGE_FOURCC codes. To avoid
884 * complete confusion, just deal in __DRI_IMAGE_FORMAT codes for now and
885 * translate to __DRI_IMAGE_FOURCC codes in the call to createImageFromFds
886 */
887 static int
888 image_format_to_fourcc(int format)
889 {
890
891 /* Convert from __DRI_IMAGE_FORMAT to __DRI_IMAGE_FOURCC (sigh) */
892 switch (format) {
893 case __DRI_IMAGE_FORMAT_RGB565: return __DRI_IMAGE_FOURCC_RGB565;
894 case __DRI_IMAGE_FORMAT_XRGB8888: return __DRI_IMAGE_FOURCC_XRGB8888;
895 case __DRI_IMAGE_FORMAT_ARGB8888: return __DRI_IMAGE_FOURCC_ARGB8888;
896 case __DRI_IMAGE_FORMAT_ABGR8888: return __DRI_IMAGE_FOURCC_ABGR8888;
897 case __DRI_IMAGE_FORMAT_XBGR8888: return __DRI_IMAGE_FOURCC_XBGR8888;
898 }
899 return 0;
900 }
901
902 /** dri3_get_pixmap_buffer
903 *
904 * Get the DRM object for a pixmap from the X server and
905 * wrap that with a __DRIimage structure using createImageFromFds
906 */
907 static struct dri3_buffer *
908 dri3_get_pixmap_buffer(__DRIdrawable *driDrawable,
909 unsigned int format,
910 enum dri3_buffer_type buffer_type,
911 void *loaderPrivate)
912 {
913 struct dri3_drawable *pdraw = loaderPrivate;
914 int buf_id = dri3_pixmap_buf_id(buffer_type);
915 struct dri3_buffer *buffer = pdraw->buffers[buf_id];
916 Pixmap pixmap;
917 xcb_dri3_buffer_from_pixmap_cookie_t bp_cookie;
918 xcb_dri3_buffer_from_pixmap_reply_t *bp_reply;
919 int *fds;
920 Display *dpy;
921 struct dri3_screen *psc;
922 xcb_connection_t *c;
923 xcb_sync_fence_t sync_fence;
924 struct xshmfence *shm_fence;
925 int fence_fd;
926 __DRIimage *image_planar;
927 int stride, offset;
928
929 if (buffer)
930 return buffer;
931
932 pixmap = pdraw->base.xDrawable;
933 psc = (struct dri3_screen *) pdraw->base.psc;
934 dpy = psc->base.dpy;
935 c = XGetXCBConnection(dpy);
936
937 buffer = calloc(1, sizeof (struct dri3_buffer));
938 if (!buffer)
939 goto no_buffer;
940
941 fence_fd = xshmfence_alloc_shm();
942 if (fence_fd < 0)
943 goto no_fence;
944 shm_fence = xshmfence_map_shm(fence_fd);
945 if (shm_fence == NULL) {
946 close (fence_fd);
947 goto no_fence;
948 }
949
950 xcb_dri3_fence_from_fd(c,
951 pixmap,
952 (sync_fence = xcb_generate_id(c)),
953 false,
954 fence_fd);
955
956 /* Get an FD for the pixmap object
957 */
958 bp_cookie = xcb_dri3_buffer_from_pixmap(c, pixmap);
959 bp_reply = xcb_dri3_buffer_from_pixmap_reply(c, bp_cookie, NULL);
960 if (!bp_reply)
961 goto no_image;
962 fds = xcb_dri3_buffer_from_pixmap_reply_fds(c, bp_reply);
963
964 stride = bp_reply->stride;
965 offset = 0;
966
967 /* createImageFromFds creates a wrapper __DRIimage structure which
968 * can deal with multiple planes for things like Yuv images. So, once
969 * we've gotten the planar wrapper, pull the single plane out of it and
970 * discard the wrapper.
971 */
972 image_planar = (*psc->image->createImageFromFds) (psc->driScreen,
973 bp_reply->width,
974 bp_reply->height,
975 image_format_to_fourcc(format),
976 fds, 1,
977 &stride, &offset, buffer);
978 close(fds[0]);
979 if (!image_planar)
980 goto no_image;
981
982 buffer->image = (*psc->image->fromPlanar)(image_planar, 0, buffer);
983
984 (*psc->image->destroyImage)(image_planar);
985
986 if (!buffer->image)
987 goto no_image;
988
989 buffer->pixmap = pixmap;
990 buffer->width = bp_reply->width;
991 buffer->height = bp_reply->height;
992 buffer->buffer_type = buffer_type;
993 buffer->shm_fence = shm_fence;
994 buffer->sync_fence = sync_fence;
995
996 pdraw->buffers[buf_id] = buffer;
997 return buffer;
998
999 no_image:
1000 xcb_sync_destroy_fence(c, sync_fence);
1001 xshmfence_unmap_shm(shm_fence);
1002 no_fence:
1003 free(buffer);
1004 no_buffer:
1005 return NULL;
1006 }
1007
1008 /** dri3_find_back
1009 *
1010 * Find an idle back buffer. If there isn't one, then
1011 * wait for a present idle notify event from the X server
1012 */
1013 static int
1014 dri3_find_back(xcb_connection_t *c, struct dri3_drawable *priv)
1015 {
1016 int b;
1017 xcb_generic_event_t *ev;
1018 xcb_present_generic_event_t *ge;
1019
1020 for (;;) {
1021
1022 for (b = 0; b < DRI3_MAX_BACK; b++) {
1023 int id = DRI3_BACK_ID(b);
1024 struct dri3_buffer *buffer = priv->buffers[id];
1025
1026 if (!buffer)
1027 return b;
1028 if (!buffer->busy)
1029 return b;
1030 }
1031 ev = xcb_wait_for_special_event(c, priv->special_event);
1032 if (!ev)
1033 return -1;
1034 ge = (void *) ev;
1035 dri3_handle_present_event(priv, ge);
1036 }
1037 }
1038
1039 /** dri3_get_buffer
1040 *
1041 * Find a front or back buffer, allocating new ones as necessary
1042 */
1043 static struct dri3_buffer *
1044 dri3_get_buffer(__DRIdrawable *driDrawable,
1045 unsigned int format,
1046 enum dri3_buffer_type buffer_type,
1047 void *loaderPrivate)
1048 {
1049 struct dri3_drawable *priv = loaderPrivate;
1050 xcb_connection_t *c = XGetXCBConnection(priv->base.psc->dpy);
1051 struct dri3_buffer *buffer;
1052 int buf_id;
1053
1054 if (buffer_type == dri3_buffer_back) {
1055 int back = dri3_find_back(c, priv);
1056
1057 if (back < 0)
1058 return NULL;
1059
1060 priv->cur_back = back;
1061 buf_id = DRI3_BACK_ID(priv->cur_back);
1062 } else {
1063 buf_id = DRI3_FRONT_ID;
1064 }
1065
1066 buffer = priv->buffers[buf_id];
1067
1068 /* Allocate a new buffer if there isn't an old one, or if that
1069 * old one is the wrong size
1070 */
1071 if (!buffer || buffer->width != priv->width || buffer->height != priv->height) {
1072 struct dri3_buffer *new_buffer;
1073
1074 /* Allocate the new buffers
1075 */
1076 new_buffer = dri3_alloc_render_buffer(priv->base.psc,
1077 priv->base.xDrawable,
1078 format, priv->width, priv->height, priv->depth);
1079 if (!new_buffer)
1080 return NULL;
1081
1082 /* When resizing, copy the contents of the old buffer, waiting for that
1083 * copy to complete using our fences before proceeding
1084 */
1085 switch (buffer_type) {
1086 case dri3_buffer_back:
1087 if (buffer) {
1088 dri3_fence_reset(c, new_buffer);
1089 dri3_fence_await(c, buffer);
1090 dri3_copy_area(c,
1091 buffer->pixmap,
1092 new_buffer->pixmap,
1093 dri3_drawable_gc(priv),
1094 0, 0, 0, 0, priv->width, priv->height);
1095 dri3_fence_trigger(c, new_buffer);
1096 dri3_free_render_buffer(priv, buffer);
1097 }
1098 break;
1099 case dri3_buffer_front:
1100 dri3_fence_reset(c, new_buffer);
1101 dri3_copy_area(c,
1102 priv->base.xDrawable,
1103 new_buffer->pixmap,
1104 dri3_drawable_gc(priv),
1105 0, 0, 0, 0, priv->width, priv->height);
1106 dri3_fence_trigger(c, new_buffer);
1107 break;
1108 }
1109 buffer = new_buffer;
1110 buffer->buffer_type = buffer_type;
1111 priv->buffers[buf_id] = buffer;
1112 }
1113 dri3_fence_await(c, buffer);
1114
1115 /* Return the requested buffer */
1116 return buffer;
1117 }
1118
1119 /** dri3_free_buffers
1120 *
1121 * Free the front bufffer or all of the back buffers. Used
1122 * when the application changes which buffers it needs
1123 */
1124 static void
1125 dri3_free_buffers(__DRIdrawable *driDrawable,
1126 enum dri3_buffer_type buffer_type,
1127 void *loaderPrivate)
1128 {
1129 struct dri3_drawable *priv = loaderPrivate;
1130 struct dri3_buffer *buffer;
1131 int first_id;
1132 int n_id;
1133 int buf_id;
1134
1135 switch (buffer_type) {
1136 case dri3_buffer_back:
1137 first_id = DRI3_BACK_ID(0);
1138 n_id = DRI3_MAX_BACK;
1139 break;
1140 case dri3_buffer_front:
1141 first_id = DRI3_FRONT_ID;
1142 n_id = 1;
1143 }
1144
1145 for (buf_id = first_id; buf_id < first_id + n_id; buf_id++) {
1146 buffer = priv->buffers[buf_id];
1147 if (buffer) {
1148 dri3_free_render_buffer(priv, buffer);
1149 priv->buffers[buf_id] = NULL;
1150 }
1151 }
1152 }
1153
1154 /** dri3_get_buffers
1155 *
1156 * The published buffer allocation API.
1157 * Returns all of the necessary buffers, allocating
1158 * as needed.
1159 */
1160 static int
1161 dri3_get_buffers(__DRIdrawable *driDrawable,
1162 unsigned int format,
1163 uint32_t *stamp,
1164 void *loaderPrivate,
1165 uint32_t buffer_mask,
1166 struct __DRIimageList *buffers)
1167 {
1168 struct dri3_drawable *priv = loaderPrivate;
1169 struct dri3_buffer *front, *back;
1170
1171 buffers->image_mask = 0;
1172 buffers->front = NULL;
1173 buffers->back = NULL;
1174
1175 front = NULL;
1176 back = NULL;
1177
1178 if (!dri3_update_drawable(driDrawable, loaderPrivate))
1179 return false;
1180
1181 /* pixmaps always have front buffers */
1182 if (priv->is_pixmap)
1183 buffer_mask |= __DRI_IMAGE_BUFFER_FRONT;
1184
1185 if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
1186 if (priv->is_pixmap)
1187 front = dri3_get_pixmap_buffer(driDrawable,
1188 format,
1189 dri3_buffer_front,
1190 loaderPrivate);
1191 else
1192 front = dri3_get_buffer(driDrawable,
1193 format,
1194 dri3_buffer_front,
1195 loaderPrivate);
1196
1197 if (!front)
1198 return false;
1199 } else {
1200 dri3_free_buffers(driDrawable, dri3_buffer_front, loaderPrivate);
1201 priv->have_fake_front = 0;
1202 }
1203
1204 if (buffer_mask & __DRI_IMAGE_BUFFER_BACK) {
1205 back = dri3_get_buffer(driDrawable,
1206 format,
1207 dri3_buffer_back,
1208 loaderPrivate);
1209 if (!back)
1210 return false;
1211 priv->have_back = 1;
1212 } else {
1213 dri3_free_buffers(driDrawable, dri3_buffer_back, loaderPrivate);
1214 priv->have_back = 0;
1215 }
1216
1217 if (front) {
1218 buffers->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
1219 buffers->front = front->image;
1220 priv->have_fake_front = !priv->is_pixmap;
1221 }
1222
1223 if (back) {
1224 buffers->image_mask |= __DRI_IMAGE_BUFFER_BACK;
1225 buffers->back = back->image;
1226 }
1227
1228 priv->stamp = stamp;
1229
1230 return true;
1231 }
1232
1233 /* The image loader extension record for DRI3
1234 */
1235 static const __DRIimageLoaderExtension imageLoaderExtension = {
1236 {__DRI_IMAGE_LOADER, __DRI_IMAGE_LOADER_VERSION},
1237 .getBuffers = dri3_get_buffers,
1238 .flushFrontBuffer = dri3_flush_front_buffer,
1239 };
1240
1241 /** dri3_swap_buffers
1242 *
1243 * Make the current back buffer visible using the present extension
1244 */
1245 static int64_t
1246 dri3_swap_buffers(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
1247 int64_t remainder, Bool flush)
1248 {
1249 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
1250 struct dri3_screen *psc = (struct dri3_screen *) priv->base.psc;
1251 Display *dpy = priv->base.psc->dpy;
1252 xcb_connection_t *c = XGetXCBConnection(dpy);
1253 int buf_id = DRI3_BACK_ID(priv->cur_back);
1254 int64_t ret = 0;
1255
1256 unsigned flags = __DRI2_FLUSH_DRAWABLE;
1257 if (flush)
1258 flags |= __DRI2_FLUSH_CONTEXT;
1259 dri3_flush(psc, priv, flags, __DRI2_THROTTLE_SWAPBUFFER);
1260
1261 dri3_flush_present_events(priv);
1262
1263 if (priv->buffers[buf_id] && !priv->is_pixmap) {
1264 dri3_fence_reset(c, priv->buffers[buf_id]);
1265
1266 /* Compute when we want the frame shown by taking the last known successful
1267 * MSC and adding in a swap interval for each outstanding swap request
1268 */
1269 ++priv->present_request_serial;
1270 if (target_msc == 0)
1271 target_msc = priv->msc + priv->swap_interval * (priv->present_request_serial - priv->present_event_serial);
1272
1273 priv->buffers[buf_id]->busy = 1;
1274 xcb_present_pixmap(c,
1275 priv->base.xDrawable,
1276 priv->buffers[buf_id]->pixmap,
1277 priv->present_request_serial,
1278 0, /* valid */
1279 0, /* update */
1280 0, /* x_off */
1281 0, /* y_off */
1282 None, /* target_crtc */
1283 None,
1284 priv->buffers[buf_id]->sync_fence,
1285 XCB_PRESENT_OPTION_NONE,
1286 target_msc,
1287 divisor,
1288 remainder, 0, NULL);
1289 ret = ++priv->sbc;
1290
1291 /* If there's a fake front, then copy the source back buffer
1292 * to the fake front to keep it up to date. This needs
1293 * to reset the fence and make future users block until
1294 * the X server is done copying the bits
1295 */
1296 if (priv->have_fake_front) {
1297 dri3_fence_reset(c, priv->buffers[DRI3_FRONT_ID]);
1298 dri3_copy_area(c,
1299 priv->buffers[buf_id]->pixmap,
1300 priv->buffers[DRI3_FRONT_ID]->pixmap,
1301 dri3_drawable_gc(priv),
1302 0, 0, 0, 0, priv->width, priv->height);
1303 dri3_fence_trigger(c, priv->buffers[DRI3_FRONT_ID]);
1304 }
1305 xcb_flush(c);
1306 if (priv->stamp)
1307 ++(*priv->stamp);
1308 }
1309
1310 return ret;
1311 }
1312
1313 /** dri3_open
1314 *
1315 * Wrapper around xcb_dri3_open
1316 */
1317 static int
1318 dri3_open(Display *dpy,
1319 Window root,
1320 CARD32 provider)
1321 {
1322 xcb_dri3_open_cookie_t cookie;
1323 xcb_dri3_open_reply_t *reply;
1324 xcb_connection_t *c = XGetXCBConnection(dpy);
1325 xcb_generic_error_t *error;
1326 int fd;
1327
1328 cookie = xcb_dri3_open(c,
1329 root,
1330 provider);
1331
1332 reply = xcb_dri3_open_reply(c, cookie, &error);
1333 if (!reply)
1334 return -1;
1335
1336 if (reply->nfd != 1) {
1337 free(reply);
1338 return -1;
1339 }
1340
1341 fd = xcb_dri3_open_reply_fds(c, reply)[0];
1342 fcntl(fd, F_SETFD, FD_CLOEXEC);
1343
1344 return fd;
1345 }
1346
1347
1348 /** dri3_destroy_screen
1349 */
1350 static void
1351 dri3_destroy_screen(struct glx_screen *base)
1352 {
1353 struct dri3_screen *psc = (struct dri3_screen *) base;
1354
1355 /* Free the direct rendering per screen data */
1356 (*psc->core->destroyScreen) (psc->driScreen);
1357 driDestroyConfigs(psc->driver_configs);
1358 close(psc->fd);
1359 free(psc);
1360 }
1361
1362 /** dri3_set_swap_interval
1363 *
1364 * Record the application swap interval specification,
1365 */
1366 static int
1367 dri3_set_swap_interval(__GLXDRIdrawable *pdraw, int interval)
1368 {
1369 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
1370 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
1371 struct dri3_screen *psc = (struct dri3_screen *) priv->base.psc;
1372
1373 if (psc->config)
1374 psc->config->configQueryi(psc->driScreen,
1375 "vblank_mode", &vblank_mode);
1376
1377 switch (vblank_mode) {
1378 case DRI_CONF_VBLANK_NEVER:
1379 if (interval != 0)
1380 return GLX_BAD_VALUE;
1381 break;
1382 case DRI_CONF_VBLANK_ALWAYS_SYNC:
1383 if (interval <= 0)
1384 return GLX_BAD_VALUE;
1385 break;
1386 default:
1387 break;
1388 }
1389
1390 priv->swap_interval = interval;
1391
1392 return 0;
1393 }
1394
1395 /** dri3_get_swap_interval
1396 *
1397 * Return the stored swap interval
1398 */
1399 static int
1400 dri3_get_swap_interval(__GLXDRIdrawable *pdraw)
1401 {
1402 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw;
1403
1404 return priv->swap_interval;
1405 }
1406
1407 static void
1408 dri3_bind_tex_image(Display * dpy,
1409 GLXDrawable drawable,
1410 int buffer, const int *attrib_list)
1411 {
1412 struct glx_context *gc = __glXGetCurrentContext();
1413 struct dri3_context *pcp = (struct dri3_context *) gc;
1414 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
1415 struct dri3_drawable *pdraw = (struct dri3_drawable *) base;
1416 struct dri3_screen *psc;
1417
1418 if (pdraw != NULL) {
1419 psc = (struct dri3_screen *) base->psc;
1420
1421 (*psc->f->invalidate)(pdraw->driDrawable);
1422
1423 XSync(dpy, false);
1424
1425 (*psc->texBuffer->setTexBuffer2) (pcp->driContext,
1426 pdraw->base.textureTarget,
1427 pdraw->base.textureFormat,
1428 pdraw->driDrawable);
1429 }
1430 }
1431
1432 static void
1433 dri3_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer)
1434 {
1435 struct glx_context *gc = __glXGetCurrentContext();
1436 struct dri3_context *pcp = (struct dri3_context *) gc;
1437 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
1438 struct dri3_drawable *pdraw = (struct dri3_drawable *) base;
1439 struct dri3_screen *psc;
1440
1441 if (pdraw != NULL) {
1442 psc = (struct dri3_screen *) base->psc;
1443
1444 if (psc->texBuffer->releaseTexBuffer)
1445 (*psc->texBuffer->releaseTexBuffer) (pcp->driContext,
1446 pdraw->base.textureTarget,
1447 pdraw->driDrawable);
1448 }
1449 }
1450
1451 static const struct glx_context_vtable dri3_context_vtable = {
1452 dri3_destroy_context,
1453 dri3_bind_context,
1454 dri3_unbind_context,
1455 dri3_wait_gl,
1456 dri3_wait_x,
1457 DRI_glXUseXFont,
1458 dri3_bind_tex_image,
1459 dri3_release_tex_image,
1460 NULL, /* get_proc_address */
1461 };
1462
1463 /** dri3_bind_extensions
1464 *
1465 * Enable all of the extensions supported on DRI3
1466 */
1467 static void
1468 dri3_bind_extensions(struct dri3_screen *psc, struct glx_display * priv,
1469 const char *driverName)
1470 {
1471 const __DRIextension **extensions;
1472 unsigned mask;
1473 int i;
1474
1475 extensions = psc->core->getExtensions(psc->driScreen);
1476
1477 __glXEnableDirectExtension(&psc->base, "GLX_SGI_video_sync");
1478 __glXEnableDirectExtension(&psc->base, "GLX_SGI_swap_control");
1479 __glXEnableDirectExtension(&psc->base, "GLX_MESA_swap_control");
1480 __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
1481
1482 /*
1483 * GLX_INTEL_swap_event is broken on the server side, where it's
1484 * currently unconditionally enabled. This completely breaks
1485 * systems running on drivers which don't support that extension.
1486 * There's no way to test for its presence on this side, so instead
1487 * of disabling it unconditionally, just disable it for drivers
1488 * which are known to not support it, or for DDX drivers supporting
1489 * only an older (pre-ScheduleSwap) version of DRI2.
1490 *
1491 * This is a hack which is required until:
1492 * http://lists.x.org/archives/xorg-devel/2013-February/035449.html
1493 * is merged and updated xserver makes it's way into distros:
1494 */
1495 // if (pdp->swapAvailable && strcmp(driverName, "vmwgfx") != 0) {
1496 // __glXEnableDirectExtension(&psc->base, "GLX_INTEL_swap_event");
1497 // }
1498
1499 mask = psc->image_driver->getAPIMask(psc->driScreen);
1500
1501 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context");
1502 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context_profile");
1503
1504 if ((mask & (1 << __DRI_API_GLES2)) != 0)
1505 __glXEnableDirectExtension(&psc->base,
1506 "GLX_EXT_create_context_es2_profile");
1507
1508 for (i = 0; extensions[i]; i++) {
1509 if ((strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
1510 psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
1511 __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
1512 }
1513
1514 if ((strcmp(extensions[i]->name, __DRI2_FLUSH) == 0)) {
1515 psc->f = (__DRI2flushExtension *) extensions[i];
1516 /* internal driver extension, no GL extension exposed */
1517 }
1518
1519 if ((strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0))
1520 psc->config = (__DRI2configQueryExtension *) extensions[i];
1521
1522 if (strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0)
1523 __glXEnableDirectExtension(&psc->base,
1524 "GLX_ARB_create_context_robustness");
1525 }
1526 }
1527
1528 static const struct glx_screen_vtable dri3_screen_vtable = {
1529 dri3_create_context,
1530 dri3_create_context_attribs
1531 };
1532
1533 /** dri3_create_screen
1534 *
1535 * Initialize DRI3 on the specified screen.
1536 *
1537 * Opens the DRI device, locates the appropriate DRI driver
1538 * and loads that.
1539 *
1540 * Checks to see if the driver supports the necessary extensions
1541 *
1542 * Initializes the driver for the screen and sets up our structures
1543 */
1544
1545 static struct glx_screen *
1546 dri3_create_screen(int screen, struct glx_display * priv)
1547 {
1548 xcb_connection_t *c = XGetXCBConnection(priv->dpy);
1549 const __DRIconfig **driver_configs;
1550 const __DRIextension **extensions;
1551 const struct dri3_display *const pdp = (struct dri3_display *)
1552 priv->dri3Display;
1553 struct dri3_screen *psc;
1554 __GLXDRIscreen *psp;
1555 struct glx_config *configs = NULL, *visuals = NULL;
1556 char *driverName, *deviceName;
1557 int i;
1558
1559 psc = calloc(1, sizeof *psc);
1560 if (psc == NULL)
1561 return NULL;
1562
1563 psc->fd = -1;
1564
1565 if (!glx_screen_init(&psc->base, screen, priv)) {
1566 free(psc);
1567 return NULL;
1568 }
1569
1570 psc->fd = dri3_open(priv->dpy, RootWindow(priv->dpy, screen), None);
1571 if (psc->fd < 0) {
1572 int conn_error = xcb_connection_has_error(c);
1573
1574 glx_screen_cleanup(&psc->base);
1575 free(psc);
1576 InfoMessageF("screen %d does not appear to be DRI3 capable\n", screen);
1577
1578 if (conn_error)
1579 ErrorMessageF("Connection closed during DRI3 initialization failure");
1580
1581 return NULL;
1582 }
1583 deviceName = NULL;
1584
1585 driverName = dri3_get_driver_for_fd(psc->fd);
1586 if (!driverName) {
1587 ErrorMessageF("No driver found\n");
1588 goto handle_error;
1589 }
1590
1591 psc->driver = driOpenDriver(driverName);
1592 if (psc->driver == NULL) {
1593 ErrorMessageF("driver pointer missing\n");
1594 goto handle_error;
1595 }
1596
1597 extensions = driGetDriverExtensions(psc->driver, driverName);
1598 if (extensions == NULL)
1599 goto handle_error;
1600
1601 for (i = 0; extensions[i]; i++) {
1602 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
1603 psc->core = (__DRIcoreExtension *) extensions[i];
1604 if (strcmp(extensions[i]->name, __DRI_IMAGE_DRIVER) == 0)
1605 psc->image_driver = (__DRIimageDriverExtension *) extensions[i];
1606 }
1607
1608
1609 if (psc->core == NULL) {
1610 ErrorMessageF("core dri driver extension not found\n");
1611 goto handle_error;
1612 }
1613
1614 if (psc->image_driver == NULL) {
1615 ErrorMessageF("image driver extension not found\n");
1616 goto handle_error;
1617 }
1618
1619 psc->driScreen =
1620 psc->image_driver->createNewScreen2(screen, psc->fd,
1621 (const __DRIextension **)
1622 &pdp->loader_extensions[0],
1623 extensions,
1624 &driver_configs, psc);
1625
1626 if (psc->driScreen == NULL) {
1627 ErrorMessageF("failed to create dri screen\n");
1628 goto handle_error;
1629 }
1630
1631 extensions = (*psc->core->getExtensions)(psc->driScreen);
1632
1633 for (i = 0; extensions[i]; i++) {
1634 if (strcmp(extensions[i]->name, __DRI_IMAGE) == 0)
1635 psc->image = (__DRIimageExtension *) extensions[i];
1636 }
1637
1638 if (psc->image == NULL) {
1639 ErrorMessageF("image extension not found\n");
1640 goto handle_error;
1641 }
1642
1643 dri3_bind_extensions(psc, priv, driverName);
1644
1645 if (!psc->f || psc->f->base.version < 4) {
1646 ErrorMessageF("Version 4 or later of flush extension not found\n");
1647 goto handle_error;
1648 }
1649
1650 if (!psc->texBuffer || psc->texBuffer->base.version < 2 ||
1651 !psc->texBuffer->setTexBuffer2)
1652 {
1653 ErrorMessageF("Version 2 or later of texBuffer extension not found\n");
1654 goto handle_error;
1655 }
1656
1657 configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs);
1658 visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
1659
1660 if (!configs || !visuals)
1661 goto handle_error;
1662
1663 glx_config_destroy_list(psc->base.configs);
1664 psc->base.configs = configs;
1665 glx_config_destroy_list(psc->base.visuals);
1666 psc->base.visuals = visuals;
1667
1668 psc->driver_configs = driver_configs;
1669
1670 psc->base.vtable = &dri3_screen_vtable;
1671 psp = &psc->vtable;
1672 psc->base.driScreen = psp;
1673 psp->destroyScreen = dri3_destroy_screen;
1674 psp->createDrawable = dri3_create_drawable;
1675 psp->swapBuffers = dri3_swap_buffers;
1676
1677 psp->getDrawableMSC = dri3_drawable_get_msc;
1678 psp->waitForMSC = dri3_wait_for_msc;
1679 psp->waitForSBC = dri3_wait_for_sbc;
1680 psp->setSwapInterval = dri3_set_swap_interval;
1681 psp->getSwapInterval = dri3_get_swap_interval;
1682 __glXEnableDirectExtension(&psc->base, "GLX_OML_sync_control");
1683
1684 psp->copySubBuffer = dri3_copy_sub_buffer;
1685 __glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer");
1686
1687 free(driverName);
1688 free(deviceName);
1689
1690 return &psc->base;
1691
1692 handle_error:
1693 CriticalErrorMessageF("failed to load driver: %s\n", driverName);
1694
1695 if (configs)
1696 glx_config_destroy_list(configs);
1697 if (visuals)
1698 glx_config_destroy_list(visuals);
1699 if (psc->driScreen)
1700 psc->core->destroyScreen(psc->driScreen);
1701 psc->driScreen = NULL;
1702 if (psc->fd >= 0)
1703 close(psc->fd);
1704 if (psc->driver)
1705 dlclose(psc->driver);
1706
1707 free(driverName);
1708 free(deviceName);
1709 glx_screen_cleanup(&psc->base);
1710 free(psc);
1711
1712 return NULL;
1713 }
1714
1715 /** dri_destroy_display
1716 *
1717 * Called from __glXFreeDisplayPrivate.
1718 */
1719 static void
1720 dri3_destroy_display(__GLXDRIdisplay * dpy)
1721 {
1722 free(dpy);
1723 }
1724
1725 /** dri3_create_display
1726 *
1727 * Allocate, initialize and return a __DRIdisplayPrivate object.
1728 * This is called from __glXInitialize() when we are given a new
1729 * display pointer. This is public to that function, but hidden from
1730 * outside of libGL.
1731 */
1732 _X_HIDDEN __GLXDRIdisplay *
1733 dri3_create_display(Display * dpy)
1734 {
1735 struct dri3_display *pdp;
1736 int i;
1737 xcb_connection_t *c = XGetXCBConnection(dpy);
1738 xcb_dri3_query_version_cookie_t dri3_cookie;
1739 xcb_dri3_query_version_reply_t *dri3_reply;
1740 xcb_present_query_version_cookie_t present_cookie;
1741 xcb_present_query_version_reply_t *present_reply;
1742 xcb_generic_error_t *error;
1743 const xcb_query_extension_reply_t *extension;
1744
1745 xcb_prefetch_extension_data(c, &xcb_dri3_id);
1746 xcb_prefetch_extension_data(c, &xcb_present_id);
1747
1748 extension = xcb_get_extension_data(c, &xcb_dri3_id);
1749 if (!(extension && extension->present))
1750 return NULL;
1751
1752 extension = xcb_get_extension_data(c, &xcb_present_id);
1753 if (!(extension && extension->present))
1754 return NULL;
1755
1756 dri3_cookie = xcb_dri3_query_version(c,
1757 XCB_DRI3_MAJOR_VERSION,
1758 XCB_DRI3_MINOR_VERSION);
1759
1760
1761 present_cookie = xcb_present_query_version(c,
1762 XCB_PRESENT_MAJOR_VERSION,
1763 XCB_PRESENT_MINOR_VERSION);
1764
1765 pdp = malloc(sizeof *pdp);
1766 if (pdp == NULL)
1767 return NULL;
1768
1769 dri3_reply = xcb_dri3_query_version_reply(c, dri3_cookie, &error);
1770 if (!dri3_reply) {
1771 free(error);
1772 goto no_extension;
1773 }
1774
1775 pdp->dri3Major = dri3_reply->major_version;
1776 pdp->dri3Minor = dri3_reply->minor_version;
1777 free(dri3_reply);
1778
1779 present_reply = xcb_present_query_version_reply(c, present_cookie, &error);
1780 if (!present_reply) {
1781 free(error);
1782 goto no_extension;
1783 }
1784 pdp->presentMajor = present_reply->major_version;
1785 pdp->presentMinor = present_reply->minor_version;
1786
1787 pdp->base.destroyDisplay = dri3_destroy_display;
1788 pdp->base.createScreen = dri3_create_screen;
1789
1790 i = 0;
1791
1792 pdp->loader_extensions[i++] = &imageLoaderExtension.base;
1793
1794 pdp->loader_extensions[i++] = &systemTimeExtension.base;
1795
1796 pdp->loader_extensions[i++] = NULL;
1797
1798 return &pdp->base;
1799 no_extension:
1800 free(pdp);
1801 return NULL;
1802 }
1803
1804 #endif /* GLX_DIRECT_RENDERING */