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