glx: Replace DRI2SwapInterval 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 * Make sure server has the same swap interval we do for the new
424 * drawable.
425 */
426 if (psc->vtable.setSwapInterval)
427 psc->vtable.setSwapInterval(&pdraw->base, pdraw->swap_interval);
428
429 return &pdraw->base;
430 }
431
432 static int
433 dri2DrawableGetMSC(struct glx_screen *psc, __GLXDRIdrawable *pdraw,
434 int64_t *ust, int64_t *msc, int64_t *sbc)
435 {
436 xcb_connection_t *c = XGetXCBConnection(pdraw->psc->dpy);
437 xcb_dri2_get_msc_cookie_t get_msc_cookie;
438 xcb_dri2_get_msc_reply_t *get_msc_reply;
439
440 get_msc_cookie = xcb_dri2_get_msc_unchecked(c, pdraw->xDrawable);
441 get_msc_reply = xcb_dri2_get_msc_reply(c, get_msc_cookie, NULL);
442 *ust = merge_counter(get_msc_reply->ust_hi, get_msc_reply->ust_lo);
443 *msc = merge_counter(get_msc_reply->msc_hi, get_msc_reply->msc_lo);
444 *sbc = merge_counter(get_msc_reply->sbc_hi, get_msc_reply->sbc_lo);
445 free(get_msc_reply);
446
447 return 0;
448 }
449
450 static int
451 dri2WaitForMSC(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
452 int64_t remainder, int64_t *ust, int64_t *msc, int64_t *sbc)
453 {
454 xcb_connection_t *c = XGetXCBConnection(pdraw->psc->dpy);
455 xcb_dri2_wait_msc_cookie_t wait_msc_cookie;
456 xcb_dri2_wait_msc_reply_t *wait_msc_reply;
457 uint32_t target_msc_hi, target_msc_lo;
458 uint32_t divisor_hi, divisor_lo;
459 uint32_t remainder_hi, remainder_lo;
460
461 split_counter(target_msc, &target_msc_hi, &target_msc_lo);
462 split_counter(divisor, &divisor_hi, &divisor_lo);
463 split_counter(remainder, &remainder_hi, &remainder_lo);
464
465 wait_msc_cookie = xcb_dri2_wait_msc_unchecked(c, pdraw->xDrawable,
466 target_msc_hi, target_msc_lo,
467 divisor_hi, divisor_lo,
468 remainder_hi, remainder_lo);
469 wait_msc_reply = xcb_dri2_wait_msc_reply(c, wait_msc_cookie, NULL);
470 *ust = merge_counter(wait_msc_reply->ust_hi, wait_msc_reply->ust_lo);
471 *msc = merge_counter(wait_msc_reply->msc_hi, wait_msc_reply->msc_lo);
472 *sbc = merge_counter(wait_msc_reply->sbc_hi, wait_msc_reply->sbc_lo);
473 free(wait_msc_reply);
474
475 return 0;
476 }
477
478 static int
479 dri2WaitForSBC(__GLXDRIdrawable *pdraw, int64_t target_sbc, int64_t *ust,
480 int64_t *msc, int64_t *sbc)
481 {
482 xcb_connection_t *c = XGetXCBConnection(pdraw->psc->dpy);
483 xcb_dri2_wait_sbc_cookie_t wait_sbc_cookie;
484 xcb_dri2_wait_sbc_reply_t *wait_sbc_reply;
485 uint32_t target_sbc_hi, target_sbc_lo;
486
487 split_counter(target_sbc, &target_sbc_hi, &target_sbc_lo);
488
489 wait_sbc_cookie = xcb_dri2_wait_sbc_unchecked(c, pdraw->xDrawable,
490 target_sbc_hi, target_sbc_lo);
491 wait_sbc_reply = xcb_dri2_wait_sbc_reply(c, wait_sbc_cookie, NULL);
492 *ust = merge_counter(wait_sbc_reply->ust_hi, wait_sbc_reply->ust_lo);
493 *msc = merge_counter(wait_sbc_reply->msc_hi, wait_sbc_reply->msc_lo);
494 *sbc = merge_counter(wait_sbc_reply->sbc_hi, wait_sbc_reply->sbc_lo);
495 free(wait_sbc_reply);
496
497 return 0;
498 }
499
500 /**
501 * dri2Throttle - Request driver throttling
502 *
503 * This function uses the DRI2 throttle extension to give the
504 * driver the opportunity to throttle on flush front, copysubbuffer
505 * and swapbuffers.
506 */
507 static void
508 dri2Throttle(struct dri2_screen *psc,
509 struct dri2_drawable *draw,
510 enum __DRI2throttleReason reason)
511 {
512 if (psc->throttle) {
513 struct glx_context *gc = __glXGetCurrentContext();
514 struct dri2_context *dri2Ctx = (struct dri2_context *)gc;
515 __DRIcontext *ctx =
516 (dri2Ctx) ? dri2Ctx->driContext : NULL;
517
518 psc->throttle->throttle(ctx, draw->driDrawable, reason);
519 }
520 }
521
522 static void
523 __dri2CopySubBuffer(__GLXDRIdrawable *pdraw, int x, int y,
524 int width, int height,
525 enum __DRI2throttleReason reason)
526 {
527 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
528 struct dri2_screen *psc = (struct dri2_screen *) pdraw->psc;
529 XRectangle xrect;
530 XserverRegion region;
531
532 /* Check we have the right attachments */
533 if (!priv->have_back)
534 return;
535
536 xrect.x = x;
537 xrect.y = priv->height - y - height;
538 xrect.width = width;
539 xrect.height = height;
540
541 if (psc->f)
542 (*psc->f->flush) (priv->driDrawable);
543
544 dri2Throttle(psc, priv, reason);
545
546 region = XFixesCreateRegion(psc->base.dpy, &xrect, 1);
547 DRI2CopyRegion(psc->base.dpy, pdraw->xDrawable, region,
548 DRI2BufferFrontLeft, DRI2BufferBackLeft);
549
550 /* Refresh the fake front (if present) after we just damaged the real
551 * front.
552 */
553 if (priv->have_fake_front)
554 DRI2CopyRegion(psc->base.dpy, pdraw->xDrawable, region,
555 DRI2BufferFakeFrontLeft, DRI2BufferFrontLeft);
556
557 XFixesDestroyRegion(psc->base.dpy, region);
558 }
559
560 static void
561 dri2CopySubBuffer(__GLXDRIdrawable *pdraw, int x, int y,
562 int width, int height)
563 {
564 __dri2CopySubBuffer(pdraw, x, y, width, height,
565 __DRI2_THROTTLE_COPYSUBBUFFER);
566 }
567
568
569 static void
570 dri2_copy_drawable(struct dri2_drawable *priv, int dest, int src)
571 {
572 XRectangle xrect;
573 XserverRegion region;
574 struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
575
576 xrect.x = 0;
577 xrect.y = 0;
578 xrect.width = priv->width;
579 xrect.height = priv->height;
580
581 if (psc->f)
582 (*psc->f->flush) (priv->driDrawable);
583
584 region = XFixesCreateRegion(psc->base.dpy, &xrect, 1);
585 DRI2CopyRegion(psc->base.dpy, priv->base.xDrawable, region, dest, src);
586 XFixesDestroyRegion(psc->base.dpy, region);
587
588 }
589
590 static void
591 dri2_wait_x(struct glx_context *gc)
592 {
593 struct dri2_drawable *priv = (struct dri2_drawable *)
594 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
595
596 if (priv == NULL || !priv->have_fake_front)
597 return;
598
599 dri2_copy_drawable(priv, DRI2BufferFakeFrontLeft, DRI2BufferFrontLeft);
600 }
601
602 static void
603 dri2_wait_gl(struct glx_context *gc)
604 {
605 struct dri2_drawable *priv = (struct dri2_drawable *)
606 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
607
608 if (priv == NULL || !priv->have_fake_front)
609 return;
610
611 dri2_copy_drawable(priv, DRI2BufferFrontLeft, DRI2BufferFakeFrontLeft);
612 }
613
614 static void
615 dri2FlushFrontBuffer(__DRIdrawable *driDrawable, void *loaderPrivate)
616 {
617 struct glx_display *priv;
618 struct dri2_display *pdp;
619 struct glx_context *gc;
620 struct dri2_drawable *pdraw = loaderPrivate;
621 struct dri2_screen *psc;
622
623 if (!pdraw)
624 return;
625
626 if (!pdraw->base.psc)
627 return;
628
629 psc = (struct dri2_screen *) pdraw->base.psc;
630
631 priv = __glXInitialize(psc->base.dpy);
632 pdp = (struct dri2_display *) priv->dri2Display;
633 gc = __glXGetCurrentContext();
634
635 dri2Throttle(psc, pdraw, __DRI2_THROTTLE_FLUSHFRONT);
636
637 /* Old servers don't send invalidate events */
638 if (!pdp->invalidateAvailable)
639 dri2InvalidateBuffers(priv->dpy, pdraw->base.xDrawable);
640
641 dri2_wait_gl(gc);
642 }
643
644
645 static void
646 dri2DestroyScreen(struct glx_screen *base)
647 {
648 struct dri2_screen *psc = (struct dri2_screen *) base;
649
650 /* Free the direct rendering per screen data */
651 (*psc->core->destroyScreen) (psc->driScreen);
652 driDestroyConfigs(psc->driver_configs);
653 close(psc->fd);
654 free(psc);
655 }
656
657 /**
658 * Process list of buffer received from the server
659 *
660 * Processes the list of buffers received in a reply from the server to either
661 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
662 */
663 static void
664 process_buffers(struct dri2_drawable * pdraw, DRI2Buffer * buffers,
665 unsigned count)
666 {
667 int i;
668
669 pdraw->bufferCount = count;
670 pdraw->have_fake_front = 0;
671 pdraw->have_back = 0;
672
673 /* This assumes the DRI2 buffer attachment tokens matches the
674 * __DRIbuffer tokens. */
675 for (i = 0; i < count; i++) {
676 pdraw->buffers[i].attachment = buffers[i].attachment;
677 pdraw->buffers[i].name = buffers[i].name;
678 pdraw->buffers[i].pitch = buffers[i].pitch;
679 pdraw->buffers[i].cpp = buffers[i].cpp;
680 pdraw->buffers[i].flags = buffers[i].flags;
681 if (pdraw->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)
682 pdraw->have_fake_front = 1;
683 if (pdraw->buffers[i].attachment == __DRI_BUFFER_BACK_LEFT)
684 pdraw->have_back = 1;
685 }
686
687 }
688
689 unsigned dri2GetSwapEventType(Display* dpy, XID drawable)
690 {
691 struct glx_display *glx_dpy = __glXInitialize(dpy);
692 __GLXDRIdrawable *pdraw;
693 pdraw = dri2GetGlxDrawableFromXDrawableId(dpy, drawable);
694 if (!pdraw || !(pdraw->eventMask & GLX_BUFFER_SWAP_COMPLETE_INTEL_MASK))
695 return 0;
696 return glx_dpy->codes->first_event + GLX_BufferSwapComplete;
697 }
698
699 static void show_fps(struct dri2_drawable *draw)
700 {
701 struct timeval tv;
702 uint64_t current_time;
703
704 gettimeofday(&tv, 0);
705 current_time = (uint64_t)tv.tv_sec*1000000 + (uint64_t)tv.tv_usec;
706
707 draw->frames++;
708
709 if (draw->previous_time + 1000000 <= current_time) {
710 if (draw->previous_time) {
711 fprintf(stderr, "libGL: FPS = %.1f\n",
712 ((uint64_t)draw->frames * 1000000) /
713 (double)(current_time - draw->previous_time));
714 }
715 draw->frames = 0;
716 draw->previous_time = current_time;
717 }
718 }
719
720 static int64_t
721 dri2SwapBuffers(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
722 int64_t remainder)
723 {
724 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
725 struct glx_display *dpyPriv = __glXInitialize(priv->base.psc->dpy);
726 struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
727 struct dri2_display *pdp =
728 (struct dri2_display *)dpyPriv->dri2Display;
729 CARD64 ret = 0;
730
731 /* Check we have the right attachments */
732 if (!priv->have_back)
733 return ret;
734
735 /* Old servers can't handle swapbuffers */
736 if (!pdp->swapAvailable) {
737 __dri2CopySubBuffer(pdraw, 0, 0, priv->width, priv->height,
738 __DRI2_THROTTLE_SWAPBUFFER);
739 } else {
740 #ifdef X_DRI2SwapBuffers
741 if (psc->f) {
742 struct glx_context *gc = __glXGetCurrentContext();
743
744 if (gc) {
745 (*psc->f->flush)(priv->driDrawable);
746 }
747 }
748
749 dri2Throttle(psc, priv, __DRI2_THROTTLE_SWAPBUFFER);
750
751 DRI2SwapBuffers(psc->base.dpy, pdraw->xDrawable,
752 target_msc, divisor, remainder, &ret);
753 #endif
754 }
755
756 if (psc->show_fps) {
757 show_fps(priv);
758 }
759
760 /* Old servers don't send invalidate events */
761 if (!pdp->invalidateAvailable)
762 dri2InvalidateBuffers(dpyPriv->dpy, pdraw->xDrawable);
763
764 return ret;
765 }
766
767 static __DRIbuffer *
768 dri2GetBuffers(__DRIdrawable * driDrawable,
769 int *width, int *height,
770 unsigned int *attachments, int count,
771 int *out_count, void *loaderPrivate)
772 {
773 struct dri2_drawable *pdraw = loaderPrivate;
774 DRI2Buffer *buffers;
775
776 buffers = DRI2GetBuffers(pdraw->base.psc->dpy, pdraw->base.xDrawable,
777 width, height, attachments, count, out_count);
778 if (buffers == NULL)
779 return NULL;
780
781 pdraw->width = *width;
782 pdraw->height = *height;
783 process_buffers(pdraw, buffers, *out_count);
784
785 free(buffers);
786
787 return pdraw->buffers;
788 }
789
790 static __DRIbuffer *
791 dri2GetBuffersWithFormat(__DRIdrawable * driDrawable,
792 int *width, int *height,
793 unsigned int *attachments, int count,
794 int *out_count, void *loaderPrivate)
795 {
796 struct dri2_drawable *pdraw = loaderPrivate;
797 DRI2Buffer *buffers;
798
799 buffers = DRI2GetBuffersWithFormat(pdraw->base.psc->dpy,
800 pdraw->base.xDrawable,
801 width, height, attachments,
802 count, out_count);
803 if (buffers == NULL)
804 return NULL;
805
806 pdraw->width = *width;
807 pdraw->height = *height;
808 process_buffers(pdraw, buffers, *out_count);
809
810 free(buffers);
811
812 return pdraw->buffers;
813 }
814
815 static int
816 dri2SetSwapInterval(__GLXDRIdrawable *pdraw, int interval)
817 {
818 xcb_connection_t *c = XGetXCBConnection(pdraw->psc->dpy);
819 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
820 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
821 struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
822
823 if (psc->config)
824 psc->config->configQueryi(psc->driScreen,
825 "vblank_mode", &vblank_mode);
826
827 switch (vblank_mode) {
828 case DRI_CONF_VBLANK_NEVER:
829 if (interval != 0)
830 return GLX_BAD_VALUE;
831 break;
832 case DRI_CONF_VBLANK_ALWAYS_SYNC:
833 if (interval <= 0)
834 return GLX_BAD_VALUE;
835 break;
836 default:
837 break;
838 }
839
840 xcb_dri2_swap_interval(c, priv->base.xDrawable, interval);
841 priv->swap_interval = interval;
842
843 return 0;
844 }
845
846 static int
847 dri2GetSwapInterval(__GLXDRIdrawable *pdraw)
848 {
849 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
850
851 return priv->swap_interval;
852 }
853
854 static const __DRIdri2LoaderExtension dri2LoaderExtension = {
855 {__DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION},
856 dri2GetBuffers,
857 dri2FlushFrontBuffer,
858 dri2GetBuffersWithFormat,
859 };
860
861 static const __DRIdri2LoaderExtension dri2LoaderExtension_old = {
862 {__DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION},
863 dri2GetBuffers,
864 dri2FlushFrontBuffer,
865 NULL,
866 };
867
868 static const __DRIuseInvalidateExtension dri2UseInvalidate = {
869 { __DRI_USE_INVALIDATE, __DRI_USE_INVALIDATE_VERSION }
870 };
871
872 _X_HIDDEN void
873 dri2InvalidateBuffers(Display *dpy, XID drawable)
874 {
875 __GLXDRIdrawable *pdraw =
876 dri2GetGlxDrawableFromXDrawableId(dpy, drawable);
877 struct dri2_screen *psc;
878 struct dri2_drawable *pdp = (struct dri2_drawable *) pdraw;
879
880 if (!pdraw)
881 return;
882
883 psc = (struct dri2_screen *) pdraw->psc;
884
885 if (pdraw && psc->f && psc->f->base.version >= 3 && psc->f->invalidate)
886 psc->f->invalidate(pdp->driDrawable);
887 }
888
889 static void
890 dri2_bind_tex_image(Display * dpy,
891 GLXDrawable drawable,
892 int buffer, const int *attrib_list)
893 {
894 struct glx_context *gc = __glXGetCurrentContext();
895 struct dri2_context *pcp = (struct dri2_context *) gc;
896 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
897 struct glx_display *dpyPriv = __glXInitialize(dpy);
898 struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
899 struct dri2_display *pdp =
900 (struct dri2_display *) dpyPriv->dri2Display;
901 struct dri2_screen *psc;
902
903 if (pdraw != NULL) {
904 psc = (struct dri2_screen *) base->psc;
905
906 if (!pdp->invalidateAvailable && psc->f &&
907 psc->f->base.version >= 3 && psc->f->invalidate)
908 psc->f->invalidate(pdraw->driDrawable);
909
910 if (psc->texBuffer->base.version >= 2 &&
911 psc->texBuffer->setTexBuffer2 != NULL) {
912 (*psc->texBuffer->setTexBuffer2) (pcp->driContext,
913 pdraw->base.textureTarget,
914 pdraw->base.textureFormat,
915 pdraw->driDrawable);
916 }
917 else {
918 (*psc->texBuffer->setTexBuffer) (pcp->driContext,
919 pdraw->base.textureTarget,
920 pdraw->driDrawable);
921 }
922 }
923 }
924
925 static void
926 dri2_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer)
927 {
928 #if __DRI_TEX_BUFFER_VERSION >= 3
929 struct glx_context *gc = __glXGetCurrentContext();
930 struct dri2_context *pcp = (struct dri2_context *) gc;
931 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
932 struct glx_display *dpyPriv = __glXInitialize(dpy);
933 struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
934 struct dri2_display *pdp =
935 (struct dri2_display *) dpyPriv->dri2Display;
936 struct dri2_screen *psc;
937
938 if (pdraw != NULL) {
939 psc = (struct dri2_screen *) base->psc;
940
941 if (psc->texBuffer->base.version >= 3 &&
942 psc->texBuffer->releaseTexBuffer != NULL) {
943 (*psc->texBuffer->releaseTexBuffer) (pcp->driContext,
944 pdraw->base.textureTarget,
945 pdraw->driDrawable);
946 }
947 }
948 #endif
949 }
950
951 static const struct glx_context_vtable dri2_context_vtable = {
952 dri2_destroy_context,
953 dri2_bind_context,
954 dri2_unbind_context,
955 dri2_wait_gl,
956 dri2_wait_x,
957 DRI_glXUseXFont,
958 dri2_bind_tex_image,
959 dri2_release_tex_image,
960 NULL, /* get_proc_address */
961 };
962
963 static void
964 dri2BindExtensions(struct dri2_screen *psc, const __DRIextension **extensions)
965 {
966 int i;
967
968 __glXEnableDirectExtension(&psc->base, "GLX_SGI_video_sync");
969 __glXEnableDirectExtension(&psc->base, "GLX_SGI_swap_control");
970 __glXEnableDirectExtension(&psc->base, "GLX_MESA_swap_control");
971 __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
972
973 /* FIXME: if DRI2 version supports it... */
974 __glXEnableDirectExtension(&psc->base, "INTEL_swap_event");
975
976 if (psc->dri2->base.version >= 3) {
977 const unsigned mask = psc->dri2->getAPIMask(psc->driScreen);
978
979 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context");
980 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context_profile");
981
982 if ((mask & (1 << __DRI_API_GLES2)) != 0)
983 __glXEnableDirectExtension(&psc->base,
984 "GLX_EXT_create_context_es2_profile");
985 }
986
987 for (i = 0; extensions[i]; i++) {
988 if ((strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
989 psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
990 __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
991 }
992
993 if ((strcmp(extensions[i]->name, __DRI2_FLUSH) == 0)) {
994 psc->f = (__DRI2flushExtension *) extensions[i];
995 /* internal driver extension, no GL extension exposed */
996 }
997
998 if ((strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0))
999 psc->config = (__DRI2configQueryExtension *) extensions[i];
1000
1001 if (((strcmp(extensions[i]->name, __DRI2_THROTTLE) == 0)))
1002 psc->throttle = (__DRI2throttleExtension *) extensions[i];
1003
1004 /* DRI2 version 3 is also required because
1005 * GLX_ARB_create_context_robustness requires GLX_ARB_create_context.
1006 */
1007 if (psc->dri2->base.version >= 3
1008 && strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0)
1009 __glXEnableDirectExtension(&psc->base,
1010 "GLX_ARB_create_context_robustness");
1011 }
1012 }
1013
1014 static const struct glx_screen_vtable dri2_screen_vtable = {
1015 dri2_create_context,
1016 dri2_create_context_attribs
1017 };
1018
1019 static struct glx_screen *
1020 dri2CreateScreen(int screen, struct glx_display * priv)
1021 {
1022 const __DRIconfig **driver_configs;
1023 const __DRIextension **extensions;
1024 const struct dri2_display *const pdp = (struct dri2_display *)
1025 priv->dri2Display;
1026 struct dri2_screen *psc;
1027 __GLXDRIscreen *psp;
1028 struct glx_config *configs = NULL, *visuals = NULL;
1029 char *driverName, *deviceName, *tmp;
1030 drm_magic_t magic;
1031 int i;
1032
1033 psc = calloc(1, sizeof *psc);
1034 if (psc == NULL)
1035 return NULL;
1036
1037 psc->fd = -1;
1038
1039 if (!glx_screen_init(&psc->base, screen, priv)) {
1040 free(psc);
1041 return NULL;
1042 }
1043
1044 if (!DRI2Connect(priv->dpy, RootWindow(priv->dpy, screen),
1045 &driverName, &deviceName)) {
1046 glx_screen_cleanup(&psc->base);
1047 free(psc);
1048 InfoMessageF("screen %d does not appear to be DRI2 capable\n", screen);
1049 return NULL;
1050 }
1051
1052 psc->driver = driOpenDriver(driverName);
1053 if (psc->driver == NULL) {
1054 ErrorMessageF("driver pointer missing\n");
1055 goto handle_error;
1056 }
1057
1058 extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
1059 if (extensions == NULL) {
1060 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
1061 goto handle_error;
1062 }
1063
1064 for (i = 0; extensions[i]; i++) {
1065 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
1066 psc->core = (__DRIcoreExtension *) extensions[i];
1067 if (strcmp(extensions[i]->name, __DRI_DRI2) == 0)
1068 psc->dri2 = (__DRIdri2Extension *) extensions[i];
1069 }
1070
1071 if (psc->core == NULL || psc->dri2 == NULL) {
1072 ErrorMessageF("core dri or dri2 extension not found\n");
1073 goto handle_error;
1074 }
1075
1076 #ifdef O_CLOEXEC
1077 psc->fd = open(deviceName, O_RDWR | O_CLOEXEC);
1078 if (psc->fd == -1 && errno == EINVAL)
1079 #endif
1080 {
1081 psc->fd = open(deviceName, O_RDWR);
1082 if (psc->fd != -1)
1083 fcntl(psc->fd, F_SETFD, fcntl(psc->fd, F_GETFD) | FD_CLOEXEC);
1084 }
1085 if (psc->fd < 0) {
1086 ErrorMessageF("failed to open drm device: %s\n", strerror(errno));
1087 goto handle_error;
1088 }
1089
1090 if (drmGetMagic(psc->fd, &magic)) {
1091 ErrorMessageF("failed to get magic\n");
1092 goto handle_error;
1093 }
1094
1095 if (!DRI2Authenticate(priv->dpy, RootWindow(priv->dpy, screen), magic)) {
1096 ErrorMessageF("failed to authenticate magic %d\n", magic);
1097 goto handle_error;
1098 }
1099
1100
1101 /* If the server does not support the protocol for
1102 * DRI2GetBuffersWithFormat, don't supply that interface to the driver.
1103 */
1104 psc->driScreen =
1105 psc->dri2->createNewScreen(screen, psc->fd,
1106 (const __DRIextension **)
1107 &pdp->loader_extensions[0],
1108 &driver_configs, psc);
1109
1110 if (psc->driScreen == NULL) {
1111 ErrorMessageF("failed to create dri screen\n");
1112 goto handle_error;
1113 }
1114
1115 extensions = psc->core->getExtensions(psc->driScreen);
1116 dri2BindExtensions(psc, extensions);
1117
1118 configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs);
1119 visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
1120
1121 if (!configs || !visuals)
1122 goto handle_error;
1123
1124 glx_config_destroy_list(psc->base.configs);
1125 psc->base.configs = configs;
1126 glx_config_destroy_list(psc->base.visuals);
1127 psc->base.visuals = visuals;
1128
1129 psc->driver_configs = driver_configs;
1130
1131 psc->base.vtable = &dri2_screen_vtable;
1132 psp = &psc->vtable;
1133 psc->base.driScreen = psp;
1134 psp->destroyScreen = dri2DestroyScreen;
1135 psp->createDrawable = dri2CreateDrawable;
1136 psp->swapBuffers = dri2SwapBuffers;
1137 psp->getDrawableMSC = NULL;
1138 psp->waitForMSC = NULL;
1139 psp->waitForSBC = NULL;
1140 psp->setSwapInterval = NULL;
1141 psp->getSwapInterval = NULL;
1142
1143 if (pdp->driMinor >= 2) {
1144 psp->getDrawableMSC = dri2DrawableGetMSC;
1145 psp->waitForMSC = dri2WaitForMSC;
1146 psp->waitForSBC = dri2WaitForSBC;
1147 psp->setSwapInterval = dri2SetSwapInterval;
1148 psp->getSwapInterval = dri2GetSwapInterval;
1149 __glXEnableDirectExtension(&psc->base, "GLX_OML_sync_control");
1150 }
1151
1152 /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
1153 * available.*/
1154 psp->copySubBuffer = dri2CopySubBuffer;
1155 __glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer");
1156
1157 free(driverName);
1158 free(deviceName);
1159
1160 tmp = getenv("LIBGL_SHOW_FPS");
1161 psc->show_fps = tmp && strcmp(tmp, "1") == 0;
1162
1163 return &psc->base;
1164
1165 handle_error:
1166 CriticalErrorMessageF("failed to load driver: %s\n", driverName);
1167
1168 if (configs)
1169 glx_config_destroy_list(configs);
1170 if (visuals)
1171 glx_config_destroy_list(visuals);
1172 if (psc->driScreen)
1173 psc->core->destroyScreen(psc->driScreen);
1174 psc->driScreen = NULL;
1175 if (psc->fd >= 0)
1176 close(psc->fd);
1177 if (psc->driver)
1178 dlclose(psc->driver);
1179
1180 free(driverName);
1181 free(deviceName);
1182 glx_screen_cleanup(&psc->base);
1183 free(psc);
1184
1185 return NULL;
1186 }
1187
1188 /* Called from __glXFreeDisplayPrivate.
1189 */
1190 static void
1191 dri2DestroyDisplay(__GLXDRIdisplay * dpy)
1192 {
1193 struct dri2_display *pdp = (struct dri2_display *) dpy;
1194
1195 __glxHashDestroy(pdp->dri2Hash);
1196 free(dpy);
1197 }
1198
1199 _X_HIDDEN __GLXDRIdrawable *
1200 dri2GetGlxDrawableFromXDrawableId(Display *dpy, XID id)
1201 {
1202 struct glx_display *d = __glXInitialize(dpy);
1203 struct dri2_display *pdp = (struct dri2_display *) d->dri2Display;
1204 __GLXDRIdrawable *pdraw;
1205
1206 if (__glxHashLookup(pdp->dri2Hash, id, (void *) &pdraw) == 0)
1207 return pdraw;
1208
1209 return NULL;
1210 }
1211
1212 /*
1213 * Allocate, initialize and return a __DRIdisplayPrivate object.
1214 * This is called from __glXInitialize() when we are given a new
1215 * display pointer.
1216 */
1217 _X_HIDDEN __GLXDRIdisplay *
1218 dri2CreateDisplay(Display * dpy)
1219 {
1220 struct dri2_display *pdp;
1221 int eventBase, errorBase, i;
1222
1223 if (!DRI2QueryExtension(dpy, &eventBase, &errorBase))
1224 return NULL;
1225
1226 pdp = malloc(sizeof *pdp);
1227 if (pdp == NULL)
1228 return NULL;
1229
1230 if (!DRI2QueryVersion(dpy, &pdp->driMajor, &pdp->driMinor)) {
1231 free(pdp);
1232 return NULL;
1233 }
1234
1235 pdp->driPatch = 0;
1236 pdp->swapAvailable = (pdp->driMinor >= 2);
1237 pdp->invalidateAvailable = (pdp->driMinor >= 3);
1238
1239 pdp->base.destroyDisplay = dri2DestroyDisplay;
1240 pdp->base.createScreen = dri2CreateScreen;
1241
1242 i = 0;
1243 if (pdp->driMinor < 1)
1244 pdp->loader_extensions[i++] = &dri2LoaderExtension_old.base;
1245 else
1246 pdp->loader_extensions[i++] = &dri2LoaderExtension.base;
1247
1248 pdp->loader_extensions[i++] = &systemTimeExtension.base;
1249
1250 pdp->loader_extensions[i++] = &dri2UseInvalidate.base;
1251
1252 pdp->loader_extensions[i++] = NULL;
1253
1254 pdp->dri2Hash = __glxHashCreate();
1255 if (pdp->dri2Hash == NULL) {
1256 free(pdp);
1257 return NULL;
1258 }
1259
1260 return &pdp->base;
1261 }
1262
1263 #endif /* GLX_DIRECT_RENDERING */