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