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