glx: Replace DRI2WaitForMSC custom protocol with XCB.
[mesa.git] / src / glx / dri2_glx.c
1 /*
2 * Copyright © 2008 Red Hat, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Soft-
6 * ware"), to deal in the Software without restriction, including without
7 * limitation the rights to use, copy, modify, merge, publish, distribute,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, provided that the above copyright
10 * notice(s) and this permission notice appear in all copies of the Soft-
11 * ware and that both the above copyright notice(s) and this permission
12 * notice appear in supporting documentation.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16 * ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
17 * RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN
18 * THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSE-
19 * QUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFOR-
22 * MANCE OF THIS SOFTWARE.
23 *
24 * Except as contained in this notice, the name of a copyright holder shall
25 * not be used in advertising or otherwise to promote the sale, use or
26 * other dealings in this Software without prior written authorization of
27 * the copyright holder.
28 *
29 * Authors:
30 * Kristian Høgsberg (krh@redhat.com)
31 */
32
33 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
34
35 #include <X11/Xlib.h>
36 #include <X11/extensions/Xfixes.h>
37 #include <X11/Xlib-xcb.h>
38 #include <xcb/xcb.h>
39 #include <xcb/dri2.h>
40 #include "glapi.h"
41 #include "glxclient.h"
42 #include <X11/extensions/dri2proto.h>
43 #include "xf86dri.h"
44 #include <dlfcn.h>
45 #include <fcntl.h>
46 #include <unistd.h>
47 #include <sys/types.h>
48 #include <sys/mman.h>
49 #include <sys/time.h>
50 #include "xf86drm.h"
51 #include "dri2.h"
52 #include "dri_common.h"
53
54 /* From xmlpool/options.h, user exposed so should be stable */
55 #define DRI_CONF_VBLANK_NEVER 0
56 #define DRI_CONF_VBLANK_DEF_INTERVAL_0 1
57 #define DRI_CONF_VBLANK_DEF_INTERVAL_1 2
58 #define DRI_CONF_VBLANK_ALWAYS_SYNC 3
59
60 #undef DRI2_MINOR
61 #define DRI2_MINOR 1
62
63 struct dri2_display
64 {
65 __GLXDRIdisplay base;
66
67 /*
68 ** XFree86-DRI version information
69 */
70 int driMajor;
71 int driMinor;
72 int driPatch;
73 int swapAvailable;
74 int invalidateAvailable;
75
76 __glxHashTable *dri2Hash;
77
78 const __DRIextension *loader_extensions[4];
79 };
80
81 struct dri2_screen {
82 struct glx_screen base;
83
84 __DRIscreen *driScreen;
85 __GLXDRIscreen vtable;
86 const __DRIdri2Extension *dri2;
87 const __DRIcoreExtension *core;
88
89 const __DRI2flushExtension *f;
90 const __DRI2configQueryExtension *config;
91 const __DRItexBufferExtension *texBuffer;
92 const __DRI2throttleExtension *throttle;
93 const __DRIconfig **driver_configs;
94
95 void *driver;
96 int fd;
97
98 Bool show_fps;
99 };
100
101 struct dri2_context
102 {
103 struct glx_context base;
104 __DRIcontext *driContext;
105 };
106
107 struct dri2_drawable
108 {
109 __GLXDRIdrawable base;
110 __DRIdrawable *driDrawable;
111 __DRIbuffer buffers[5];
112 int bufferCount;
113 int width, height;
114 int have_back;
115 int have_fake_front;
116 int swap_interval;
117
118 uint64_t previous_time;
119 unsigned frames;
120 };
121
122 static const struct glx_context_vtable dri2_context_vtable;
123
124 /* For XCB's handling of ust/msc/sbc counters, we have to hand it the high and
125 * low halves separately. This helps you split them.
126 */
127 static void
128 split_counter(uint64_t counter, uint32_t *hi, uint32_t *lo)
129 {
130 *hi = (counter >> 32);
131 *lo = counter & 0xffffffff;
132 }
133
134 static uint64_t
135 merge_counter(uint32_t hi, uint32_t lo)
136 {
137 return ((uint64_t)hi << 32) | lo;
138 }
139
140 static void
141 dri2_destroy_context(struct glx_context *context)
142 {
143 struct dri2_context *pcp = (struct dri2_context *) context;
144 struct dri2_screen *psc = (struct dri2_screen *) context->psc;
145
146 driReleaseDrawables(&pcp->base);
147
148 free((char *) context->extensions);
149
150 (*psc->core->destroyContext) (pcp->driContext);
151
152 free(pcp);
153 }
154
155 static Bool
156 dri2_bind_context(struct glx_context *context, struct glx_context *old,
157 GLXDrawable draw, GLXDrawable read)
158 {
159 struct dri2_context *pcp = (struct dri2_context *) context;
160 struct dri2_screen *psc = (struct dri2_screen *) pcp->base.psc;
161 struct dri2_drawable *pdraw, *pread;
162 struct dri2_display *pdp;
163
164 pdraw = (struct dri2_drawable *) driFetchDrawable(context, draw);
165 pread = (struct dri2_drawable *) driFetchDrawable(context, read);
166
167 driReleaseDrawables(&pcp->base);
168
169 if (pdraw == NULL || pread == NULL)
170 return GLXBadDrawable;
171
172 if (!(*psc->core->bindContext) (pcp->driContext,
173 pdraw->driDrawable, pread->driDrawable))
174 return GLXBadContext;
175
176 /* If the server doesn't send invalidate events, we may miss a
177 * resize before the rendering starts. Invalidate the buffers now
178 * so the driver will recheck before rendering starts. */
179 pdp = (struct dri2_display *) psc->base.display;
180 if (!pdp->invalidateAvailable) {
181 dri2InvalidateBuffers(psc->base.dpy, pdraw->base.xDrawable);
182 if (pread != pdraw)
183 dri2InvalidateBuffers(psc->base.dpy, pread->base.xDrawable);
184 }
185
186 return Success;
187 }
188
189 static void
190 dri2_unbind_context(struct glx_context *context, struct glx_context *new)
191 {
192 struct dri2_context *pcp = (struct dri2_context *) context;
193 struct dri2_screen *psc = (struct dri2_screen *) pcp->base.psc;
194
195 (*psc->core->unbindContext) (pcp->driContext);
196 }
197
198 static struct glx_context *
199 dri2_create_context(struct glx_screen *base,
200 struct glx_config *config_base,
201 struct glx_context *shareList, int renderType)
202 {
203 struct dri2_context *pcp, *pcp_shared;
204 struct dri2_screen *psc = (struct dri2_screen *) base;
205 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
206 __DRIcontext *shared = NULL;
207
208 if (shareList) {
209 /* If the shareList context is not a DRI2 context, we cannot possibly
210 * create a DRI2 context that shares it.
211 */
212 if (shareList->vtable->destroy != dri2_destroy_context) {
213 return NULL;
214 }
215
216 pcp_shared = (struct dri2_context *) shareList;
217 shared = pcp_shared->driContext;
218 }
219
220 pcp = calloc(1, sizeof *pcp);
221 if (pcp == NULL)
222 return NULL;
223
224 if (!glx_context_init(&pcp->base, &psc->base, &config->base)) {
225 free(pcp);
226 return NULL;
227 }
228
229 pcp->driContext =
230 (*psc->dri2->createNewContext) (psc->driScreen,
231 config->driConfig, shared, pcp);
232
233 if (pcp->driContext == NULL) {
234 free(pcp);
235 return NULL;
236 }
237
238 pcp->base.vtable = &dri2_context_vtable;
239
240 return &pcp->base;
241 }
242
243 static struct glx_context *
244 dri2_create_context_attribs(struct glx_screen *base,
245 struct glx_config *config_base,
246 struct glx_context *shareList,
247 unsigned num_attribs,
248 const uint32_t *attribs,
249 unsigned *error)
250 {
251 struct dri2_context *pcp = NULL;
252 struct dri2_context *pcp_shared = NULL;
253 struct dri2_screen *psc = (struct dri2_screen *) base;
254 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
255 __DRIcontext *shared = NULL;
256
257 uint32_t minor_ver = 1;
258 uint32_t major_ver = 2;
259 uint32_t flags = 0;
260 unsigned api;
261 int reset = __DRI_CTX_RESET_NO_NOTIFICATION;
262 uint32_t ctx_attribs[2 * 5];
263 unsigned num_ctx_attribs = 0;
264
265 if (psc->dri2->base.version < 3) {
266 *error = __DRI_CTX_ERROR_NO_MEMORY;
267 goto error_exit;
268 }
269
270 /* Remap the GLX tokens to DRI2 tokens.
271 */
272 if (!dri2_convert_glx_attribs(num_attribs, attribs,
273 &major_ver, &minor_ver, &flags, &api, &reset,
274 error))
275 goto error_exit;
276
277 if (shareList) {
278 pcp_shared = (struct dri2_context *) shareList;
279 shared = pcp_shared->driContext;
280 }
281
282 pcp = calloc(1, sizeof *pcp);
283 if (pcp == NULL) {
284 *error = __DRI_CTX_ERROR_NO_MEMORY;
285 goto error_exit;
286 }
287
288 if (!glx_context_init(&pcp->base, &psc->base, &config->base))
289 goto error_exit;
290
291 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
292 ctx_attribs[num_ctx_attribs++] = major_ver;
293 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
294 ctx_attribs[num_ctx_attribs++] = minor_ver;
295
296 /* Only send a value when the non-default value is requested. By doing
297 * this we don't have to check the driver's DRI2 version before sending the
298 * default value.
299 */
300 if (reset != __DRI_CTX_RESET_NO_NOTIFICATION) {
301 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
302 ctx_attribs[num_ctx_attribs++] = reset;
303 }
304
305 if (flags != 0) {
306 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_FLAGS;
307
308 /* The current __DRI_CTX_FLAG_* values are identical to the
309 * GLX_CONTEXT_*_BIT values.
310 */
311 ctx_attribs[num_ctx_attribs++] = flags;
312 }
313
314 pcp->driContext =
315 (*psc->dri2->createContextAttribs) (psc->driScreen,
316 api,
317 config->driConfig,
318 shared,
319 num_ctx_attribs / 2,
320 ctx_attribs,
321 error,
322 pcp);
323
324 if (pcp->driContext == NULL)
325 goto error_exit;
326
327 pcp->base.vtable = &dri2_context_vtable;
328
329 return &pcp->base;
330
331 error_exit:
332 free(pcp);
333
334 return NULL;
335 }
336
337 static void
338 dri2DestroyDrawable(__GLXDRIdrawable *base)
339 {
340 struct dri2_screen *psc = (struct dri2_screen *) base->psc;
341 struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
342 struct glx_display *dpyPriv = psc->base.display;
343 struct dri2_display *pdp = (struct dri2_display *)dpyPriv->dri2Display;
344
345 __glxHashDelete(pdp->dri2Hash, pdraw->base.xDrawable);
346 (*psc->core->destroyDrawable) (pdraw->driDrawable);
347
348 /* If it's a GLX 1.3 drawables, we can destroy the DRI2 drawable
349 * now, as the application explicitly asked to destroy the GLX
350 * drawable. Otherwise, for legacy drawables, we let the DRI2
351 * drawable linger on the server, since there's no good way of
352 * knowing when the application is done with it. The server will
353 * destroy the DRI2 drawable when it destroys the X drawable or the
354 * client exits anyway. */
355 if (pdraw->base.xDrawable != pdraw->base.drawable)
356 DRI2DestroyDrawable(psc->base.dpy, pdraw->base.xDrawable);
357
358 free(pdraw);
359 }
360
361 static __GLXDRIdrawable *
362 dri2CreateDrawable(struct glx_screen *base, XID xDrawable,
363 GLXDrawable drawable, struct glx_config *config_base)
364 {
365 struct dri2_drawable *pdraw;
366 struct dri2_screen *psc = (struct dri2_screen *) base;
367 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
368 struct glx_display *dpyPriv;
369 struct dri2_display *pdp;
370 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
371
372 pdraw = calloc(1, sizeof(*pdraw));
373 if (!pdraw)
374 return NULL;
375
376 pdraw->base.destroyDrawable = dri2DestroyDrawable;
377 pdraw->base.xDrawable = xDrawable;
378 pdraw->base.drawable = drawable;
379 pdraw->base.psc = &psc->base;
380 pdraw->bufferCount = 0;
381 pdraw->swap_interval = 1; /* default may be overridden below */
382 pdraw->have_back = 0;
383
384 if (psc->config)
385 psc->config->configQueryi(psc->driScreen,
386 "vblank_mode", &vblank_mode);
387
388 switch (vblank_mode) {
389 case DRI_CONF_VBLANK_NEVER:
390 case DRI_CONF_VBLANK_DEF_INTERVAL_0:
391 pdraw->swap_interval = 0;
392 break;
393 case DRI_CONF_VBLANK_DEF_INTERVAL_1:
394 case DRI_CONF_VBLANK_ALWAYS_SYNC:
395 default:
396 pdraw->swap_interval = 1;
397 break;
398 }
399
400 DRI2CreateDrawable(psc->base.dpy, xDrawable);
401
402 dpyPriv = __glXInitialize(psc->base.dpy);
403 pdp = (struct dri2_display *)dpyPriv->dri2Display;;
404 /* Create a new drawable */
405 pdraw->driDrawable =
406 (*psc->dri2->createNewDrawable) (psc->driScreen,
407 config->driConfig, pdraw);
408
409 if (!pdraw->driDrawable) {
410 DRI2DestroyDrawable(psc->base.dpy, xDrawable);
411 free(pdraw);
412 return NULL;
413 }
414
415 if (__glxHashInsert(pdp->dri2Hash, xDrawable, pdraw)) {
416 (*psc->core->destroyDrawable) (pdraw->driDrawable);
417 DRI2DestroyDrawable(psc->base.dpy, xDrawable);
418 free(pdraw);
419 return None;
420 }
421
422
423 #ifdef X_DRI2SwapInterval
424 /*
425 * Make sure server has the same swap interval we do for the new
426 * drawable.
427 */
428 if (pdp->swapAvailable)
429 DRI2SwapInterval(psc->base.dpy, xDrawable, pdraw->swap_interval);
430 #endif
431
432 return &pdraw->base;
433 }
434
435 #ifdef X_DRI2GetMSC
436
437 static int
438 dri2DrawableGetMSC(struct glx_screen *psc, __GLXDRIdrawable *pdraw,
439 int64_t *ust, int64_t *msc, int64_t *sbc)
440 {
441 CARD64 dri2_ust, dri2_msc, dri2_sbc;
442 int ret;
443
444 ret = DRI2GetMSC(psc->dpy, pdraw->xDrawable,
445 &dri2_ust, &dri2_msc, &dri2_sbc);
446 *ust = dri2_ust;
447 *msc = dri2_msc;
448 *sbc = dri2_sbc;
449
450 return ret;
451 }
452
453 #endif
454
455 static int
456 dri2WaitForMSC(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
457 int64_t remainder, int64_t *ust, int64_t *msc, int64_t *sbc)
458 {
459 xcb_connection_t *c = XGetXCBConnection(pdraw->psc->dpy);
460 xcb_dri2_wait_msc_cookie_t wait_msc_cookie;
461 xcb_dri2_wait_msc_reply_t *wait_msc_reply;
462 uint32_t target_msc_hi, target_msc_lo;
463 uint32_t divisor_hi, divisor_lo;
464 uint32_t remainder_hi, remainder_lo;
465
466 split_counter(target_msc, &target_msc_hi, &target_msc_lo);
467 split_counter(divisor, &divisor_hi, &divisor_lo);
468 split_counter(remainder, &remainder_hi, &remainder_lo);
469
470 wait_msc_cookie = xcb_dri2_wait_msc_unchecked(c, pdraw->xDrawable,
471 target_msc_hi, target_msc_lo,
472 divisor_hi, divisor_lo,
473 remainder_hi, remainder_lo);
474 wait_msc_reply = xcb_dri2_wait_msc_reply(c, wait_msc_cookie, NULL);
475 *ust = merge_counter(wait_msc_reply->ust_hi, wait_msc_reply->ust_lo);
476 *msc = merge_counter(wait_msc_reply->msc_hi, wait_msc_reply->msc_lo);
477 *sbc = merge_counter(wait_msc_reply->sbc_hi, wait_msc_reply->sbc_lo);
478 free(wait_msc_reply);
479
480 return 0;
481 }
482
483 static int
484 dri2WaitForSBC(__GLXDRIdrawable *pdraw, int64_t target_sbc, int64_t *ust,
485 int64_t *msc, int64_t *sbc)
486 {
487 xcb_connection_t *c = XGetXCBConnection(pdraw->psc->dpy);
488 xcb_dri2_wait_sbc_cookie_t wait_sbc_cookie;
489 xcb_dri2_wait_sbc_reply_t *wait_sbc_reply;
490 uint32_t target_sbc_hi, target_sbc_lo;
491
492 split_counter(target_sbc, &target_sbc_hi, &target_sbc_lo);
493
494 wait_sbc_cookie = xcb_dri2_wait_sbc_unchecked(c, pdraw->xDrawable,
495 target_sbc_hi, target_sbc_lo);
496 wait_sbc_reply = xcb_dri2_wait_sbc_reply(c, wait_sbc_cookie, NULL);
497 *ust = merge_counter(wait_sbc_reply->ust_hi, wait_sbc_reply->ust_lo);
498 *msc = merge_counter(wait_sbc_reply->msc_hi, wait_sbc_reply->msc_lo);
499 *sbc = merge_counter(wait_sbc_reply->sbc_hi, wait_sbc_reply->sbc_lo);
500 free(wait_sbc_reply);
501
502 return 0;
503 }
504
505 /**
506 * dri2Throttle - Request driver throttling
507 *
508 * This function uses the DRI2 throttle extension to give the
509 * driver the opportunity to throttle on flush front, copysubbuffer
510 * and swapbuffers.
511 */
512 static void
513 dri2Throttle(struct dri2_screen *psc,
514 struct dri2_drawable *draw,
515 enum __DRI2throttleReason reason)
516 {
517 if (psc->throttle) {
518 struct glx_context *gc = __glXGetCurrentContext();
519 struct dri2_context *dri2Ctx = (struct dri2_context *)gc;
520 __DRIcontext *ctx =
521 (dri2Ctx) ? dri2Ctx->driContext : NULL;
522
523 psc->throttle->throttle(ctx, draw->driDrawable, reason);
524 }
525 }
526
527 static void
528 __dri2CopySubBuffer(__GLXDRIdrawable *pdraw, int x, int y,
529 int width, int height,
530 enum __DRI2throttleReason reason)
531 {
532 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
533 struct dri2_screen *psc = (struct dri2_screen *) pdraw->psc;
534 XRectangle xrect;
535 XserverRegion region;
536
537 /* Check we have the right attachments */
538 if (!priv->have_back)
539 return;
540
541 xrect.x = x;
542 xrect.y = priv->height - y - height;
543 xrect.width = width;
544 xrect.height = height;
545
546 if (psc->f)
547 (*psc->f->flush) (priv->driDrawable);
548
549 dri2Throttle(psc, priv, reason);
550
551 region = XFixesCreateRegion(psc->base.dpy, &xrect, 1);
552 DRI2CopyRegion(psc->base.dpy, pdraw->xDrawable, region,
553 DRI2BufferFrontLeft, DRI2BufferBackLeft);
554
555 /* Refresh the fake front (if present) after we just damaged the real
556 * front.
557 */
558 if (priv->have_fake_front)
559 DRI2CopyRegion(psc->base.dpy, pdraw->xDrawable, region,
560 DRI2BufferFakeFrontLeft, DRI2BufferFrontLeft);
561
562 XFixesDestroyRegion(psc->base.dpy, region);
563 }
564
565 static void
566 dri2CopySubBuffer(__GLXDRIdrawable *pdraw, int x, int y,
567 int width, int height)
568 {
569 __dri2CopySubBuffer(pdraw, x, y, width, height,
570 __DRI2_THROTTLE_COPYSUBBUFFER);
571 }
572
573
574 static void
575 dri2_copy_drawable(struct dri2_drawable *priv, int dest, int src)
576 {
577 XRectangle xrect;
578 XserverRegion region;
579 struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
580
581 xrect.x = 0;
582 xrect.y = 0;
583 xrect.width = priv->width;
584 xrect.height = priv->height;
585
586 if (psc->f)
587 (*psc->f->flush) (priv->driDrawable);
588
589 region = XFixesCreateRegion(psc->base.dpy, &xrect, 1);
590 DRI2CopyRegion(psc->base.dpy, priv->base.xDrawable, region, dest, src);
591 XFixesDestroyRegion(psc->base.dpy, region);
592
593 }
594
595 static void
596 dri2_wait_x(struct glx_context *gc)
597 {
598 struct dri2_drawable *priv = (struct dri2_drawable *)
599 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
600
601 if (priv == NULL || !priv->have_fake_front)
602 return;
603
604 dri2_copy_drawable(priv, DRI2BufferFakeFrontLeft, DRI2BufferFrontLeft);
605 }
606
607 static void
608 dri2_wait_gl(struct glx_context *gc)
609 {
610 struct dri2_drawable *priv = (struct dri2_drawable *)
611 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
612
613 if (priv == NULL || !priv->have_fake_front)
614 return;
615
616 dri2_copy_drawable(priv, DRI2BufferFrontLeft, DRI2BufferFakeFrontLeft);
617 }
618
619 static void
620 dri2FlushFrontBuffer(__DRIdrawable *driDrawable, void *loaderPrivate)
621 {
622 struct glx_display *priv;
623 struct dri2_display *pdp;
624 struct glx_context *gc;
625 struct dri2_drawable *pdraw = loaderPrivate;
626 struct dri2_screen *psc;
627
628 if (!pdraw)
629 return;
630
631 if (!pdraw->base.psc)
632 return;
633
634 psc = (struct dri2_screen *) pdraw->base.psc;
635
636 priv = __glXInitialize(psc->base.dpy);
637 pdp = (struct dri2_display *) priv->dri2Display;
638 gc = __glXGetCurrentContext();
639
640 dri2Throttle(psc, pdraw, __DRI2_THROTTLE_FLUSHFRONT);
641
642 /* Old servers don't send invalidate events */
643 if (!pdp->invalidateAvailable)
644 dri2InvalidateBuffers(priv->dpy, pdraw->base.xDrawable);
645
646 dri2_wait_gl(gc);
647 }
648
649
650 static void
651 dri2DestroyScreen(struct glx_screen *base)
652 {
653 struct dri2_screen *psc = (struct dri2_screen *) base;
654
655 /* Free the direct rendering per screen data */
656 (*psc->core->destroyScreen) (psc->driScreen);
657 driDestroyConfigs(psc->driver_configs);
658 close(psc->fd);
659 free(psc);
660 }
661
662 /**
663 * Process list of buffer received from the server
664 *
665 * Processes the list of buffers received in a reply from the server to either
666 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
667 */
668 static void
669 process_buffers(struct dri2_drawable * pdraw, DRI2Buffer * buffers,
670 unsigned count)
671 {
672 int i;
673
674 pdraw->bufferCount = count;
675 pdraw->have_fake_front = 0;
676 pdraw->have_back = 0;
677
678 /* This assumes the DRI2 buffer attachment tokens matches the
679 * __DRIbuffer tokens. */
680 for (i = 0; i < count; i++) {
681 pdraw->buffers[i].attachment = buffers[i].attachment;
682 pdraw->buffers[i].name = buffers[i].name;
683 pdraw->buffers[i].pitch = buffers[i].pitch;
684 pdraw->buffers[i].cpp = buffers[i].cpp;
685 pdraw->buffers[i].flags = buffers[i].flags;
686 if (pdraw->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)
687 pdraw->have_fake_front = 1;
688 if (pdraw->buffers[i].attachment == __DRI_BUFFER_BACK_LEFT)
689 pdraw->have_back = 1;
690 }
691
692 }
693
694 unsigned dri2GetSwapEventType(Display* dpy, XID drawable)
695 {
696 struct glx_display *glx_dpy = __glXInitialize(dpy);
697 __GLXDRIdrawable *pdraw;
698 pdraw = dri2GetGlxDrawableFromXDrawableId(dpy, drawable);
699 if (!pdraw || !(pdraw->eventMask & GLX_BUFFER_SWAP_COMPLETE_INTEL_MASK))
700 return 0;
701 return glx_dpy->codes->first_event + GLX_BufferSwapComplete;
702 }
703
704 static void show_fps(struct dri2_drawable *draw)
705 {
706 struct timeval tv;
707 uint64_t current_time;
708
709 gettimeofday(&tv, 0);
710 current_time = (uint64_t)tv.tv_sec*1000000 + (uint64_t)tv.tv_usec;
711
712 draw->frames++;
713
714 if (draw->previous_time + 1000000 <= current_time) {
715 if (draw->previous_time) {
716 fprintf(stderr, "libGL: FPS = %.1f\n",
717 ((uint64_t)draw->frames * 1000000) /
718 (double)(current_time - draw->previous_time));
719 }
720 draw->frames = 0;
721 draw->previous_time = current_time;
722 }
723 }
724
725 static int64_t
726 dri2SwapBuffers(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
727 int64_t remainder)
728 {
729 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
730 struct glx_display *dpyPriv = __glXInitialize(priv->base.psc->dpy);
731 struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
732 struct dri2_display *pdp =
733 (struct dri2_display *)dpyPriv->dri2Display;
734 CARD64 ret = 0;
735
736 /* Check we have the right attachments */
737 if (!priv->have_back)
738 return ret;
739
740 /* Old servers can't handle swapbuffers */
741 if (!pdp->swapAvailable) {
742 __dri2CopySubBuffer(pdraw, 0, 0, priv->width, priv->height,
743 __DRI2_THROTTLE_SWAPBUFFER);
744 } else {
745 #ifdef X_DRI2SwapBuffers
746 if (psc->f) {
747 struct glx_context *gc = __glXGetCurrentContext();
748
749 if (gc) {
750 (*psc->f->flush)(priv->driDrawable);
751 }
752 }
753
754 dri2Throttle(psc, priv, __DRI2_THROTTLE_SWAPBUFFER);
755
756 DRI2SwapBuffers(psc->base.dpy, pdraw->xDrawable,
757 target_msc, divisor, remainder, &ret);
758 #endif
759 }
760
761 if (psc->show_fps) {
762 show_fps(priv);
763 }
764
765 /* Old servers don't send invalidate events */
766 if (!pdp->invalidateAvailable)
767 dri2InvalidateBuffers(dpyPriv->dpy, pdraw->xDrawable);
768
769 return ret;
770 }
771
772 static __DRIbuffer *
773 dri2GetBuffers(__DRIdrawable * driDrawable,
774 int *width, int *height,
775 unsigned int *attachments, int count,
776 int *out_count, void *loaderPrivate)
777 {
778 struct dri2_drawable *pdraw = loaderPrivate;
779 DRI2Buffer *buffers;
780
781 buffers = DRI2GetBuffers(pdraw->base.psc->dpy, pdraw->base.xDrawable,
782 width, height, attachments, count, out_count);
783 if (buffers == NULL)
784 return NULL;
785
786 pdraw->width = *width;
787 pdraw->height = *height;
788 process_buffers(pdraw, buffers, *out_count);
789
790 free(buffers);
791
792 return pdraw->buffers;
793 }
794
795 static __DRIbuffer *
796 dri2GetBuffersWithFormat(__DRIdrawable * driDrawable,
797 int *width, int *height,
798 unsigned int *attachments, int count,
799 int *out_count, void *loaderPrivate)
800 {
801 struct dri2_drawable *pdraw = loaderPrivate;
802 DRI2Buffer *buffers;
803
804 buffers = DRI2GetBuffersWithFormat(pdraw->base.psc->dpy,
805 pdraw->base.xDrawable,
806 width, height, attachments,
807 count, out_count);
808 if (buffers == NULL)
809 return NULL;
810
811 pdraw->width = *width;
812 pdraw->height = *height;
813 process_buffers(pdraw, buffers, *out_count);
814
815 free(buffers);
816
817 return pdraw->buffers;
818 }
819
820 #ifdef X_DRI2SwapInterval
821
822 static int
823 dri2SetSwapInterval(__GLXDRIdrawable *pdraw, int interval)
824 {
825 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
826 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
827 struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
828
829 if (psc->config)
830 psc->config->configQueryi(psc->driScreen,
831 "vblank_mode", &vblank_mode);
832
833 switch (vblank_mode) {
834 case DRI_CONF_VBLANK_NEVER:
835 return GLX_BAD_VALUE;
836 case DRI_CONF_VBLANK_ALWAYS_SYNC:
837 if (interval <= 0)
838 return GLX_BAD_VALUE;
839 break;
840 default:
841 break;
842 }
843
844 DRI2SwapInterval(priv->base.psc->dpy, priv->base.xDrawable, interval);
845 priv->swap_interval = interval;
846
847 return 0;
848 }
849
850 static int
851 dri2GetSwapInterval(__GLXDRIdrawable *pdraw)
852 {
853 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
854
855 return priv->swap_interval;
856 }
857
858 #endif /* X_DRI2SwapInterval */
859
860 static const __DRIdri2LoaderExtension dri2LoaderExtension = {
861 {__DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION},
862 dri2GetBuffers,
863 dri2FlushFrontBuffer,
864 dri2GetBuffersWithFormat,
865 };
866
867 static const __DRIdri2LoaderExtension dri2LoaderExtension_old = {
868 {__DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION},
869 dri2GetBuffers,
870 dri2FlushFrontBuffer,
871 NULL,
872 };
873
874 static const __DRIuseInvalidateExtension dri2UseInvalidate = {
875 { __DRI_USE_INVALIDATE, __DRI_USE_INVALIDATE_VERSION }
876 };
877
878 _X_HIDDEN void
879 dri2InvalidateBuffers(Display *dpy, XID drawable)
880 {
881 __GLXDRIdrawable *pdraw =
882 dri2GetGlxDrawableFromXDrawableId(dpy, drawable);
883 struct dri2_screen *psc;
884 struct dri2_drawable *pdp = (struct dri2_drawable *) pdraw;
885
886 if (!pdraw)
887 return;
888
889 psc = (struct dri2_screen *) pdraw->psc;
890
891 if (pdraw && psc->f && psc->f->base.version >= 3 && psc->f->invalidate)
892 psc->f->invalidate(pdp->driDrawable);
893 }
894
895 static void
896 dri2_bind_tex_image(Display * dpy,
897 GLXDrawable drawable,
898 int buffer, const int *attrib_list)
899 {
900 struct glx_context *gc = __glXGetCurrentContext();
901 struct dri2_context *pcp = (struct dri2_context *) gc;
902 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
903 struct glx_display *dpyPriv = __glXInitialize(dpy);
904 struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
905 struct dri2_display *pdp =
906 (struct dri2_display *) dpyPriv->dri2Display;
907 struct dri2_screen *psc;
908
909 if (pdraw != NULL) {
910 psc = (struct dri2_screen *) base->psc;
911
912 if (!pdp->invalidateAvailable && psc->f &&
913 psc->f->base.version >= 3 && psc->f->invalidate)
914 psc->f->invalidate(pdraw->driDrawable);
915
916 if (psc->texBuffer->base.version >= 2 &&
917 psc->texBuffer->setTexBuffer2 != NULL) {
918 (*psc->texBuffer->setTexBuffer2) (pcp->driContext,
919 pdraw->base.textureTarget,
920 pdraw->base.textureFormat,
921 pdraw->driDrawable);
922 }
923 else {
924 (*psc->texBuffer->setTexBuffer) (pcp->driContext,
925 pdraw->base.textureTarget,
926 pdraw->driDrawable);
927 }
928 }
929 }
930
931 static void
932 dri2_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer)
933 {
934 #if __DRI_TEX_BUFFER_VERSION >= 3
935 struct glx_context *gc = __glXGetCurrentContext();
936 struct dri2_context *pcp = (struct dri2_context *) gc;
937 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
938 struct glx_display *dpyPriv = __glXInitialize(dpy);
939 struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
940 struct dri2_display *pdp =
941 (struct dri2_display *) dpyPriv->dri2Display;
942 struct dri2_screen *psc;
943
944 if (pdraw != NULL) {
945 psc = (struct dri2_screen *) base->psc;
946
947 if (psc->texBuffer->base.version >= 3 &&
948 psc->texBuffer->releaseTexBuffer != NULL) {
949 (*psc->texBuffer->releaseTexBuffer) (pcp->driContext,
950 pdraw->base.textureTarget,
951 pdraw->driDrawable);
952 }
953 }
954 #endif
955 }
956
957 static const struct glx_context_vtable dri2_context_vtable = {
958 dri2_destroy_context,
959 dri2_bind_context,
960 dri2_unbind_context,
961 dri2_wait_gl,
962 dri2_wait_x,
963 DRI_glXUseXFont,
964 dri2_bind_tex_image,
965 dri2_release_tex_image,
966 NULL, /* get_proc_address */
967 };
968
969 static void
970 dri2BindExtensions(struct dri2_screen *psc, const __DRIextension **extensions)
971 {
972 int i;
973
974 __glXEnableDirectExtension(&psc->base, "GLX_SGI_video_sync");
975 __glXEnableDirectExtension(&psc->base, "GLX_SGI_swap_control");
976 __glXEnableDirectExtension(&psc->base, "GLX_MESA_swap_control");
977 __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
978
979 /* FIXME: if DRI2 version supports it... */
980 __glXEnableDirectExtension(&psc->base, "INTEL_swap_event");
981
982 if (psc->dri2->base.version >= 3) {
983 const unsigned mask = psc->dri2->getAPIMask(psc->driScreen);
984
985 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context");
986 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context_profile");
987
988 if ((mask & (1 << __DRI_API_GLES2)) != 0)
989 __glXEnableDirectExtension(&psc->base,
990 "GLX_EXT_create_context_es2_profile");
991 }
992
993 for (i = 0; extensions[i]; i++) {
994 if ((strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
995 psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
996 __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
997 }
998
999 if ((strcmp(extensions[i]->name, __DRI2_FLUSH) == 0)) {
1000 psc->f = (__DRI2flushExtension *) extensions[i];
1001 /* internal driver extension, no GL extension exposed */
1002 }
1003
1004 if ((strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0))
1005 psc->config = (__DRI2configQueryExtension *) extensions[i];
1006
1007 if (((strcmp(extensions[i]->name, __DRI2_THROTTLE) == 0)))
1008 psc->throttle = (__DRI2throttleExtension *) extensions[i];
1009
1010 /* DRI2 version 3 is also required because
1011 * GLX_ARB_create_context_robustness requires GLX_ARB_create_context.
1012 */
1013 if (psc->dri2->base.version >= 3
1014 && strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0)
1015 __glXEnableDirectExtension(&psc->base,
1016 "GLX_ARB_create_context_robustness");
1017 }
1018 }
1019
1020 static const struct glx_screen_vtable dri2_screen_vtable = {
1021 dri2_create_context,
1022 dri2_create_context_attribs
1023 };
1024
1025 static struct glx_screen *
1026 dri2CreateScreen(int screen, struct glx_display * priv)
1027 {
1028 const __DRIconfig **driver_configs;
1029 const __DRIextension **extensions;
1030 const struct dri2_display *const pdp = (struct dri2_display *)
1031 priv->dri2Display;
1032 struct dri2_screen *psc;
1033 __GLXDRIscreen *psp;
1034 struct glx_config *configs = NULL, *visuals = NULL;
1035 char *driverName, *deviceName, *tmp;
1036 drm_magic_t magic;
1037 int i;
1038
1039 psc = calloc(1, sizeof *psc);
1040 if (psc == NULL)
1041 return NULL;
1042
1043 psc->fd = -1;
1044
1045 if (!glx_screen_init(&psc->base, screen, priv)) {
1046 free(psc);
1047 return NULL;
1048 }
1049
1050 if (!DRI2Connect(priv->dpy, RootWindow(priv->dpy, screen),
1051 &driverName, &deviceName)) {
1052 glx_screen_cleanup(&psc->base);
1053 free(psc);
1054 InfoMessageF("screen %d does not appear to be DRI2 capable\n", screen);
1055 return NULL;
1056 }
1057
1058 psc->driver = driOpenDriver(driverName);
1059 if (psc->driver == NULL) {
1060 ErrorMessageF("driver pointer missing\n");
1061 goto handle_error;
1062 }
1063
1064 extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
1065 if (extensions == NULL) {
1066 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
1067 goto handle_error;
1068 }
1069
1070 for (i = 0; extensions[i]; i++) {
1071 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
1072 psc->core = (__DRIcoreExtension *) extensions[i];
1073 if (strcmp(extensions[i]->name, __DRI_DRI2) == 0)
1074 psc->dri2 = (__DRIdri2Extension *) extensions[i];
1075 }
1076
1077 if (psc->core == NULL || psc->dri2 == NULL) {
1078 ErrorMessageF("core dri or dri2 extension not found\n");
1079 goto handle_error;
1080 }
1081
1082 #ifdef O_CLOEXEC
1083 psc->fd = open(deviceName, O_RDWR | O_CLOEXEC);
1084 if (psc->fd == -1 && errno == EINVAL)
1085 #endif
1086 {
1087 psc->fd = open(deviceName, O_RDWR);
1088 if (psc->fd != -1)
1089 fcntl(psc->fd, F_SETFD, fcntl(psc->fd, F_GETFD) | FD_CLOEXEC);
1090 }
1091 if (psc->fd < 0) {
1092 ErrorMessageF("failed to open drm device: %s\n", strerror(errno));
1093 goto handle_error;
1094 }
1095
1096 if (drmGetMagic(psc->fd, &magic)) {
1097 ErrorMessageF("failed to get magic\n");
1098 goto handle_error;
1099 }
1100
1101 if (!DRI2Authenticate(priv->dpy, RootWindow(priv->dpy, screen), magic)) {
1102 ErrorMessageF("failed to authenticate magic %d\n", magic);
1103 goto handle_error;
1104 }
1105
1106
1107 /* If the server does not support the protocol for
1108 * DRI2GetBuffersWithFormat, don't supply that interface to the driver.
1109 */
1110 psc->driScreen =
1111 psc->dri2->createNewScreen(screen, psc->fd,
1112 (const __DRIextension **)
1113 &pdp->loader_extensions[0],
1114 &driver_configs, psc);
1115
1116 if (psc->driScreen == NULL) {
1117 ErrorMessageF("failed to create dri screen\n");
1118 goto handle_error;
1119 }
1120
1121 extensions = psc->core->getExtensions(psc->driScreen);
1122 dri2BindExtensions(psc, extensions);
1123
1124 configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs);
1125 visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
1126
1127 if (!configs || !visuals)
1128 goto handle_error;
1129
1130 glx_config_destroy_list(psc->base.configs);
1131 psc->base.configs = configs;
1132 glx_config_destroy_list(psc->base.visuals);
1133 psc->base.visuals = visuals;
1134
1135 psc->driver_configs = driver_configs;
1136
1137 psc->base.vtable = &dri2_screen_vtable;
1138 psp = &psc->vtable;
1139 psc->base.driScreen = psp;
1140 psp->destroyScreen = dri2DestroyScreen;
1141 psp->createDrawable = dri2CreateDrawable;
1142 psp->swapBuffers = dri2SwapBuffers;
1143 psp->getDrawableMSC = NULL;
1144 psp->waitForMSC = NULL;
1145 psp->waitForSBC = NULL;
1146 psp->setSwapInterval = NULL;
1147 psp->getSwapInterval = NULL;
1148
1149 if (pdp->driMinor >= 2) {
1150 #ifdef X_DRI2GetMSC
1151 psp->getDrawableMSC = dri2DrawableGetMSC;
1152 #endif
1153 psp->waitForMSC = dri2WaitForMSC;
1154 psp->waitForSBC = dri2WaitForSBC;
1155 #ifdef X_DRI2SwapInterval
1156 psp->setSwapInterval = dri2SetSwapInterval;
1157 psp->getSwapInterval = dri2GetSwapInterval;
1158 #endif
1159 #if defined(X_DRI2GetMSC) && defined(X_DRI2WaitMSC) && defined(X_DRI2SwapInterval)
1160 __glXEnableDirectExtension(&psc->base, "GLX_OML_sync_control");
1161 #endif
1162 }
1163
1164 /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
1165 * available.*/
1166 psp->copySubBuffer = dri2CopySubBuffer;
1167 __glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer");
1168
1169 free(driverName);
1170 free(deviceName);
1171
1172 tmp = getenv("LIBGL_SHOW_FPS");
1173 psc->show_fps = tmp && strcmp(tmp, "1") == 0;
1174
1175 return &psc->base;
1176
1177 handle_error:
1178 CriticalErrorMessageF("failed to load driver: %s\n", driverName);
1179
1180 if (configs)
1181 glx_config_destroy_list(configs);
1182 if (visuals)
1183 glx_config_destroy_list(visuals);
1184 if (psc->driScreen)
1185 psc->core->destroyScreen(psc->driScreen);
1186 psc->driScreen = NULL;
1187 if (psc->fd >= 0)
1188 close(psc->fd);
1189 if (psc->driver)
1190 dlclose(psc->driver);
1191
1192 free(driverName);
1193 free(deviceName);
1194 glx_screen_cleanup(&psc->base);
1195 free(psc);
1196
1197 return NULL;
1198 }
1199
1200 /* Called from __glXFreeDisplayPrivate.
1201 */
1202 static void
1203 dri2DestroyDisplay(__GLXDRIdisplay * dpy)
1204 {
1205 struct dri2_display *pdp = (struct dri2_display *) dpy;
1206
1207 __glxHashDestroy(pdp->dri2Hash);
1208 free(dpy);
1209 }
1210
1211 _X_HIDDEN __GLXDRIdrawable *
1212 dri2GetGlxDrawableFromXDrawableId(Display *dpy, XID id)
1213 {
1214 struct glx_display *d = __glXInitialize(dpy);
1215 struct dri2_display *pdp = (struct dri2_display *) d->dri2Display;
1216 __GLXDRIdrawable *pdraw;
1217
1218 if (__glxHashLookup(pdp->dri2Hash, id, (void *) &pdraw) == 0)
1219 return pdraw;
1220
1221 return NULL;
1222 }
1223
1224 /*
1225 * Allocate, initialize and return a __DRIdisplayPrivate object.
1226 * This is called from __glXInitialize() when we are given a new
1227 * display pointer.
1228 */
1229 _X_HIDDEN __GLXDRIdisplay *
1230 dri2CreateDisplay(Display * dpy)
1231 {
1232 struct dri2_display *pdp;
1233 int eventBase, errorBase, i;
1234
1235 if (!DRI2QueryExtension(dpy, &eventBase, &errorBase))
1236 return NULL;
1237
1238 pdp = malloc(sizeof *pdp);
1239 if (pdp == NULL)
1240 return NULL;
1241
1242 if (!DRI2QueryVersion(dpy, &pdp->driMajor, &pdp->driMinor)) {
1243 free(pdp);
1244 return NULL;
1245 }
1246
1247 pdp->driPatch = 0;
1248 pdp->swapAvailable = (pdp->driMinor >= 2);
1249 pdp->invalidateAvailable = (pdp->driMinor >= 3);
1250
1251 pdp->base.destroyDisplay = dri2DestroyDisplay;
1252 pdp->base.createScreen = dri2CreateScreen;
1253
1254 i = 0;
1255 if (pdp->driMinor < 1)
1256 pdp->loader_extensions[i++] = &dri2LoaderExtension_old.base;
1257 else
1258 pdp->loader_extensions[i++] = &dri2LoaderExtension.base;
1259
1260 pdp->loader_extensions[i++] = &systemTimeExtension.base;
1261
1262 pdp->loader_extensions[i++] = &dri2UseInvalidate.base;
1263
1264 pdp->loader_extensions[i++] = NULL;
1265
1266 pdp->dri2Hash = __glxHashCreate();
1267 if (pdp->dri2Hash == NULL) {
1268 free(pdp);
1269 return NULL;
1270 }
1271
1272 return &pdp->base;
1273 }
1274
1275 #endif /* GLX_DIRECT_RENDERING */