glx/dri2: Add DRI2 support for GLX_MESA_query_renderer
[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 #include "dri2_priv.h"
54
55 /* From xmlpool/options.h, user exposed so should be stable */
56 #define DRI_CONF_VBLANK_NEVER 0
57 #define DRI_CONF_VBLANK_DEF_INTERVAL_0 1
58 #define DRI_CONF_VBLANK_DEF_INTERVAL_1 2
59 #define DRI_CONF_VBLANK_ALWAYS_SYNC 3
60
61 #undef DRI2_MINOR
62 #define DRI2_MINOR 1
63
64 struct dri2_display
65 {
66 __GLXDRIdisplay base;
67
68 /*
69 ** XFree86-DRI version information
70 */
71 int driMajor;
72 int driMinor;
73 int driPatch;
74 int swapAvailable;
75 int invalidateAvailable;
76
77 __glxHashTable *dri2Hash;
78
79 const __DRIextension *loader_extensions[4];
80 };
81
82 struct dri2_context
83 {
84 struct glx_context base;
85 __DRIcontext *driContext;
86 };
87
88 struct dri2_drawable
89 {
90 __GLXDRIdrawable base;
91 __DRIdrawable *driDrawable;
92 __DRIbuffer buffers[5];
93 int bufferCount;
94 int width, height;
95 int have_back;
96 int have_fake_front;
97 int swap_interval;
98
99 uint64_t previous_time;
100 unsigned frames;
101 };
102
103 static const struct glx_context_vtable dri2_context_vtable;
104
105 /* For XCB's handling of ust/msc/sbc counters, we have to hand it the high and
106 * low halves separately. This helps you split them.
107 */
108 static void
109 split_counter(uint64_t counter, uint32_t *hi, uint32_t *lo)
110 {
111 *hi = (counter >> 32);
112 *lo = counter & 0xffffffff;
113 }
114
115 static uint64_t
116 merge_counter(uint32_t hi, uint32_t lo)
117 {
118 return ((uint64_t)hi << 32) | lo;
119 }
120
121 static void
122 dri2_destroy_context(struct glx_context *context)
123 {
124 struct dri2_context *pcp = (struct dri2_context *) context;
125 struct dri2_screen *psc = (struct dri2_screen *) context->psc;
126
127 driReleaseDrawables(&pcp->base);
128
129 free((char *) context->extensions);
130
131 (*psc->core->destroyContext) (pcp->driContext);
132
133 free(pcp);
134 }
135
136 static Bool
137 dri2_bind_context(struct glx_context *context, struct glx_context *old,
138 GLXDrawable draw, GLXDrawable read)
139 {
140 struct dri2_context *pcp = (struct dri2_context *) context;
141 struct dri2_screen *psc = (struct dri2_screen *) pcp->base.psc;
142 struct dri2_drawable *pdraw, *pread;
143 struct dri2_display *pdp;
144
145 pdraw = (struct dri2_drawable *) driFetchDrawable(context, draw);
146 pread = (struct dri2_drawable *) driFetchDrawable(context, read);
147
148 driReleaseDrawables(&pcp->base);
149
150 if (pdraw == NULL || pread == NULL)
151 return GLXBadDrawable;
152
153 if (!(*psc->core->bindContext) (pcp->driContext,
154 pdraw->driDrawable, pread->driDrawable))
155 return GLXBadContext;
156
157 /* If the server doesn't send invalidate events, we may miss a
158 * resize before the rendering starts. Invalidate the buffers now
159 * so the driver will recheck before rendering starts. */
160 pdp = (struct dri2_display *) psc->base.display;
161 if (!pdp->invalidateAvailable) {
162 dri2InvalidateBuffers(psc->base.dpy, pdraw->base.xDrawable);
163 if (pread != pdraw)
164 dri2InvalidateBuffers(psc->base.dpy, pread->base.xDrawable);
165 }
166
167 return Success;
168 }
169
170 static void
171 dri2_unbind_context(struct glx_context *context, struct glx_context *new)
172 {
173 struct dri2_context *pcp = (struct dri2_context *) context;
174 struct dri2_screen *psc = (struct dri2_screen *) pcp->base.psc;
175
176 (*psc->core->unbindContext) (pcp->driContext);
177 }
178
179 static struct glx_context *
180 dri2_create_context(struct glx_screen *base,
181 struct glx_config *config_base,
182 struct glx_context *shareList, int renderType)
183 {
184 struct dri2_context *pcp, *pcp_shared;
185 struct dri2_screen *psc = (struct dri2_screen *) base;
186 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
187 __DRIcontext *shared = NULL;
188
189 /* Check the renderType value */
190 if (!validate_renderType_against_config(config_base, renderType))
191 return NULL;
192
193 if (shareList) {
194 /* If the shareList context is not a DRI2 context, we cannot possibly
195 * create a DRI2 context that shares it.
196 */
197 if (shareList->vtable->destroy != dri2_destroy_context) {
198 return NULL;
199 }
200
201 pcp_shared = (struct dri2_context *) shareList;
202 shared = pcp_shared->driContext;
203 }
204
205 pcp = calloc(1, sizeof *pcp);
206 if (pcp == NULL)
207 return NULL;
208
209 if (!glx_context_init(&pcp->base, &psc->base, &config->base)) {
210 free(pcp);
211 return NULL;
212 }
213
214 pcp->base.renderType = renderType;
215
216 pcp->driContext =
217 (*psc->dri2->createNewContext) (psc->driScreen,
218 config->driConfig, shared, pcp);
219
220 if (pcp->driContext == NULL) {
221 free(pcp);
222 return NULL;
223 }
224
225 pcp->base.vtable = &dri2_context_vtable;
226
227 return &pcp->base;
228 }
229
230 static struct glx_context *
231 dri2_create_context_attribs(struct glx_screen *base,
232 struct glx_config *config_base,
233 struct glx_context *shareList,
234 unsigned num_attribs,
235 const uint32_t *attribs,
236 unsigned *error)
237 {
238 struct dri2_context *pcp = NULL;
239 struct dri2_context *pcp_shared = NULL;
240 struct dri2_screen *psc = (struct dri2_screen *) base;
241 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
242 __DRIcontext *shared = NULL;
243
244 uint32_t minor_ver;
245 uint32_t major_ver;
246 uint32_t renderType;
247 uint32_t flags;
248 unsigned api;
249 int reset;
250 uint32_t ctx_attribs[2 * 5];
251 unsigned num_ctx_attribs = 0;
252
253 if (psc->dri2->base.version < 3) {
254 *error = __DRI_CTX_ERROR_NO_MEMORY;
255 goto error_exit;
256 }
257
258 /* Remap the GLX tokens to DRI2 tokens.
259 */
260 if (!dri2_convert_glx_attribs(num_attribs, attribs,
261 &major_ver, &minor_ver, &renderType, &flags,
262 &api, &reset, error))
263 goto error_exit;
264
265 /* Check the renderType value */
266 if (!validate_renderType_against_config(config_base, renderType))
267 goto error_exit;
268
269 if (shareList) {
270 pcp_shared = (struct dri2_context *) shareList;
271 shared = pcp_shared->driContext;
272 }
273
274 pcp = calloc(1, sizeof *pcp);
275 if (pcp == NULL) {
276 *error = __DRI_CTX_ERROR_NO_MEMORY;
277 goto error_exit;
278 }
279
280 if (!glx_context_init(&pcp->base, &psc->base, &config->base))
281 goto error_exit;
282
283 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
284 ctx_attribs[num_ctx_attribs++] = major_ver;
285 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
286 ctx_attribs[num_ctx_attribs++] = minor_ver;
287
288 /* Only send a value when the non-default value is requested. By doing
289 * this we don't have to check the driver's DRI2 version before sending the
290 * default value.
291 */
292 if (reset != __DRI_CTX_RESET_NO_NOTIFICATION) {
293 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
294 ctx_attribs[num_ctx_attribs++] = reset;
295 }
296
297 if (flags != 0) {
298 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_FLAGS;
299
300 /* The current __DRI_CTX_FLAG_* values are identical to the
301 * GLX_CONTEXT_*_BIT values.
302 */
303 ctx_attribs[num_ctx_attribs++] = flags;
304 }
305
306 /* The renderType is retrieved from attribs, or set to default
307 * of GLX_RGBA_TYPE.
308 */
309 pcp->base.renderType = renderType;
310
311 pcp->driContext =
312 (*psc->dri2->createContextAttribs) (psc->driScreen,
313 api,
314 config->driConfig,
315 shared,
316 num_ctx_attribs / 2,
317 ctx_attribs,
318 error,
319 pcp);
320
321 if (pcp->driContext == NULL)
322 goto error_exit;
323
324 pcp->base.vtable = &dri2_context_vtable;
325
326 return &pcp->base;
327
328 error_exit:
329 free(pcp);
330
331 return NULL;
332 }
333
334 static void
335 dri2DestroyDrawable(__GLXDRIdrawable *base)
336 {
337 struct dri2_screen *psc = (struct dri2_screen *) base->psc;
338 struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
339 struct glx_display *dpyPriv = psc->base.display;
340 struct dri2_display *pdp = (struct dri2_display *)dpyPriv->dri2Display;
341
342 __glxHashDelete(pdp->dri2Hash, pdraw->base.xDrawable);
343 (*psc->core->destroyDrawable) (pdraw->driDrawable);
344
345 /* If it's a GLX 1.3 drawables, we can destroy the DRI2 drawable
346 * now, as the application explicitly asked to destroy the GLX
347 * drawable. Otherwise, for legacy drawables, we let the DRI2
348 * drawable linger on the server, since there's no good way of
349 * knowing when the application is done with it. The server will
350 * destroy the DRI2 drawable when it destroys the X drawable or the
351 * client exits anyway. */
352 if (pdraw->base.xDrawable != pdraw->base.drawable)
353 DRI2DestroyDrawable(psc->base.dpy, pdraw->base.xDrawable);
354
355 free(pdraw);
356 }
357
358 static __GLXDRIdrawable *
359 dri2CreateDrawable(struct glx_screen *base, XID xDrawable,
360 GLXDrawable drawable, struct glx_config *config_base)
361 {
362 struct dri2_drawable *pdraw;
363 struct dri2_screen *psc = (struct dri2_screen *) base;
364 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
365 struct glx_display *dpyPriv;
366 struct dri2_display *pdp;
367 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
368
369 pdraw = calloc(1, sizeof(*pdraw));
370 if (!pdraw)
371 return NULL;
372
373 pdraw->base.destroyDrawable = dri2DestroyDrawable;
374 pdraw->base.xDrawable = xDrawable;
375 pdraw->base.drawable = drawable;
376 pdraw->base.psc = &psc->base;
377 pdraw->bufferCount = 0;
378 pdraw->swap_interval = 1; /* default may be overridden below */
379 pdraw->have_back = 0;
380
381 if (psc->config)
382 psc->config->configQueryi(psc->driScreen,
383 "vblank_mode", &vblank_mode);
384
385 switch (vblank_mode) {
386 case DRI_CONF_VBLANK_NEVER:
387 case DRI_CONF_VBLANK_DEF_INTERVAL_0:
388 pdraw->swap_interval = 0;
389 break;
390 case DRI_CONF_VBLANK_DEF_INTERVAL_1:
391 case DRI_CONF_VBLANK_ALWAYS_SYNC:
392 default:
393 pdraw->swap_interval = 1;
394 break;
395 }
396
397 DRI2CreateDrawable(psc->base.dpy, xDrawable);
398
399 dpyPriv = __glXInitialize(psc->base.dpy);
400 pdp = (struct dri2_display *)dpyPriv->dri2Display;;
401 /* Create a new drawable */
402 pdraw->driDrawable =
403 (*psc->dri2->createNewDrawable) (psc->driScreen,
404 config->driConfig, pdraw);
405
406 if (!pdraw->driDrawable) {
407 DRI2DestroyDrawable(psc->base.dpy, xDrawable);
408 free(pdraw);
409 return NULL;
410 }
411
412 if (__glxHashInsert(pdp->dri2Hash, xDrawable, pdraw)) {
413 (*psc->core->destroyDrawable) (pdraw->driDrawable);
414 DRI2DestroyDrawable(psc->base.dpy, xDrawable);
415 free(pdraw);
416 return None;
417 }
418
419 /*
420 * Make sure server has the same swap interval we do for the new
421 * drawable.
422 */
423 if (psc->vtable.setSwapInterval)
424 psc->vtable.setSwapInterval(&pdraw->base, pdraw->swap_interval);
425
426 return &pdraw->base;
427 }
428
429 static int
430 dri2DrawableGetMSC(struct glx_screen *psc, __GLXDRIdrawable *pdraw,
431 int64_t *ust, int64_t *msc, int64_t *sbc)
432 {
433 xcb_connection_t *c = XGetXCBConnection(pdraw->psc->dpy);
434 xcb_dri2_get_msc_cookie_t get_msc_cookie;
435 xcb_dri2_get_msc_reply_t *get_msc_reply;
436
437 get_msc_cookie = xcb_dri2_get_msc_unchecked(c, pdraw->xDrawable);
438 get_msc_reply = xcb_dri2_get_msc_reply(c, get_msc_cookie, NULL);
439
440 if (!get_msc_reply)
441 return 0;
442
443 *ust = merge_counter(get_msc_reply->ust_hi, get_msc_reply->ust_lo);
444 *msc = merge_counter(get_msc_reply->msc_hi, get_msc_reply->msc_lo);
445 *sbc = merge_counter(get_msc_reply->sbc_hi, get_msc_reply->sbc_lo);
446 free(get_msc_reply);
447
448 return 1;
449 }
450
451 static int
452 dri2WaitForMSC(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
453 int64_t remainder, int64_t *ust, int64_t *msc, int64_t *sbc)
454 {
455 xcb_connection_t *c = XGetXCBConnection(pdraw->psc->dpy);
456 xcb_dri2_wait_msc_cookie_t wait_msc_cookie;
457 xcb_dri2_wait_msc_reply_t *wait_msc_reply;
458 uint32_t target_msc_hi, target_msc_lo;
459 uint32_t divisor_hi, divisor_lo;
460 uint32_t remainder_hi, remainder_lo;
461
462 split_counter(target_msc, &target_msc_hi, &target_msc_lo);
463 split_counter(divisor, &divisor_hi, &divisor_lo);
464 split_counter(remainder, &remainder_hi, &remainder_lo);
465
466 wait_msc_cookie = xcb_dri2_wait_msc_unchecked(c, pdraw->xDrawable,
467 target_msc_hi, target_msc_lo,
468 divisor_hi, divisor_lo,
469 remainder_hi, remainder_lo);
470 wait_msc_reply = xcb_dri2_wait_msc_reply(c, wait_msc_cookie, NULL);
471
472 if (!wait_msc_reply)
473 return 0;
474
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 1;
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
498 if (!wait_sbc_reply)
499 return 0;
500
501 *ust = merge_counter(wait_sbc_reply->ust_hi, wait_sbc_reply->ust_lo);
502 *msc = merge_counter(wait_sbc_reply->msc_hi, wait_sbc_reply->msc_lo);
503 *sbc = merge_counter(wait_sbc_reply->sbc_hi, wait_sbc_reply->sbc_lo);
504 free(wait_sbc_reply);
505
506 return 1;
507 }
508
509 static __DRIcontext *
510 dri2GetCurrentContext()
511 {
512 struct glx_context *gc = __glXGetCurrentContext();
513 struct dri2_context *dri2Ctx = (struct dri2_context *)gc;
514
515 return dri2Ctx ? dri2Ctx->driContext : NULL;
516 }
517
518 /**
519 * dri2Throttle - Request driver throttling
520 *
521 * This function uses the DRI2 throttle extension to give the
522 * driver the opportunity to throttle on flush front, copysubbuffer
523 * and swapbuffers.
524 */
525 static void
526 dri2Throttle(struct dri2_screen *psc,
527 struct dri2_drawable *draw,
528 enum __DRI2throttleReason reason)
529 {
530 if (psc->throttle) {
531 __DRIcontext *ctx = dri2GetCurrentContext();
532
533 psc->throttle->throttle(ctx, draw->driDrawable, reason);
534 }
535 }
536
537 /**
538 * Asks the driver to flush any queued work necessary for serializing with the
539 * X command stream, and optionally the slightly more strict requirement of
540 * glFlush() equivalence (which would require flushing even if nothing had
541 * been drawn to a window system framebuffer, for example).
542 */
543 static void
544 dri2Flush(struct dri2_screen *psc,
545 __DRIcontext *ctx,
546 struct dri2_drawable *draw,
547 unsigned flags,
548 enum __DRI2throttleReason throttle_reason)
549 {
550 if (ctx && psc->f && psc->f->base.version >= 4) {
551 psc->f->flush_with_flags(ctx, draw->driDrawable, flags, throttle_reason);
552 } else {
553 if (flags & __DRI2_FLUSH_CONTEXT)
554 glFlush();
555
556 if (psc->f)
557 psc->f->flush(draw->driDrawable);
558
559 dri2Throttle(psc, draw, throttle_reason);
560 }
561 }
562
563 static void
564 __dri2CopySubBuffer(__GLXDRIdrawable *pdraw, int x, int y,
565 int width, int height,
566 enum __DRI2throttleReason reason, Bool flush)
567 {
568 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
569 struct dri2_screen *psc = (struct dri2_screen *) pdraw->psc;
570 XRectangle xrect;
571 XserverRegion region;
572 __DRIcontext *ctx = dri2GetCurrentContext();
573 unsigned flags;
574
575 /* Check we have the right attachments */
576 if (!priv->have_back)
577 return;
578
579 xrect.x = x;
580 xrect.y = priv->height - y - height;
581 xrect.width = width;
582 xrect.height = height;
583
584 flags = __DRI2_FLUSH_DRAWABLE;
585 if (flush)
586 flags |= __DRI2_FLUSH_CONTEXT;
587 dri2Flush(psc, ctx, priv, flags, __DRI2_THROTTLE_SWAPBUFFER);
588
589 region = XFixesCreateRegion(psc->base.dpy, &xrect, 1);
590 DRI2CopyRegion(psc->base.dpy, pdraw->xDrawable, region,
591 DRI2BufferFrontLeft, DRI2BufferBackLeft);
592
593 /* Refresh the fake front (if present) after we just damaged the real
594 * front.
595 */
596 if (priv->have_fake_front)
597 DRI2CopyRegion(psc->base.dpy, pdraw->xDrawable, region,
598 DRI2BufferFakeFrontLeft, DRI2BufferFrontLeft);
599
600 XFixesDestroyRegion(psc->base.dpy, region);
601 }
602
603 static void
604 dri2CopySubBuffer(__GLXDRIdrawable *pdraw, int x, int y,
605 int width, int height, Bool flush)
606 {
607 __dri2CopySubBuffer(pdraw, x, y, width, height,
608 __DRI2_THROTTLE_COPYSUBBUFFER, flush);
609 }
610
611
612 static void
613 dri2_copy_drawable(struct dri2_drawable *priv, int dest, int src)
614 {
615 XRectangle xrect;
616 XserverRegion region;
617 struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
618
619 xrect.x = 0;
620 xrect.y = 0;
621 xrect.width = priv->width;
622 xrect.height = priv->height;
623
624 if (psc->f)
625 (*psc->f->flush) (priv->driDrawable);
626
627 region = XFixesCreateRegion(psc->base.dpy, &xrect, 1);
628 DRI2CopyRegion(psc->base.dpy, priv->base.xDrawable, region, dest, src);
629 XFixesDestroyRegion(psc->base.dpy, region);
630
631 }
632
633 static void
634 dri2_wait_x(struct glx_context *gc)
635 {
636 struct dri2_drawable *priv = (struct dri2_drawable *)
637 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
638
639 if (priv == NULL || !priv->have_fake_front)
640 return;
641
642 dri2_copy_drawable(priv, DRI2BufferFakeFrontLeft, DRI2BufferFrontLeft);
643 }
644
645 static void
646 dri2_wait_gl(struct glx_context *gc)
647 {
648 struct dri2_drawable *priv = (struct dri2_drawable *)
649 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
650
651 if (priv == NULL || !priv->have_fake_front)
652 return;
653
654 dri2_copy_drawable(priv, DRI2BufferFrontLeft, DRI2BufferFakeFrontLeft);
655 }
656
657 /**
658 * Called by the driver when it needs to update the real front buffer with the
659 * contents of its fake front buffer.
660 */
661 static void
662 dri2FlushFrontBuffer(__DRIdrawable *driDrawable, void *loaderPrivate)
663 {
664 struct glx_display *priv;
665 struct dri2_display *pdp;
666 struct glx_context *gc;
667 struct dri2_drawable *pdraw = loaderPrivate;
668 struct dri2_screen *psc;
669
670 if (!pdraw)
671 return;
672
673 if (!pdraw->base.psc)
674 return;
675
676 psc = (struct dri2_screen *) pdraw->base.psc;
677
678 priv = __glXInitialize(psc->base.dpy);
679 pdp = (struct dri2_display *) priv->dri2Display;
680 gc = __glXGetCurrentContext();
681
682 dri2Throttle(psc, pdraw, __DRI2_THROTTLE_FLUSHFRONT);
683
684 /* Old servers don't send invalidate events */
685 if (!pdp->invalidateAvailable)
686 dri2InvalidateBuffers(priv->dpy, pdraw->base.xDrawable);
687
688 dri2_wait_gl(gc);
689 }
690
691
692 static void
693 dri2DestroyScreen(struct glx_screen *base)
694 {
695 struct dri2_screen *psc = (struct dri2_screen *) base;
696
697 /* Free the direct rendering per screen data */
698 (*psc->core->destroyScreen) (psc->driScreen);
699 driDestroyConfigs(psc->driver_configs);
700 close(psc->fd);
701 free(psc);
702 }
703
704 /**
705 * Process list of buffer received from the server
706 *
707 * Processes the list of buffers received in a reply from the server to either
708 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
709 */
710 static void
711 process_buffers(struct dri2_drawable * pdraw, DRI2Buffer * buffers,
712 unsigned count)
713 {
714 int i;
715
716 pdraw->bufferCount = count;
717 pdraw->have_fake_front = 0;
718 pdraw->have_back = 0;
719
720 /* This assumes the DRI2 buffer attachment tokens matches the
721 * __DRIbuffer tokens. */
722 for (i = 0; i < count; i++) {
723 pdraw->buffers[i].attachment = buffers[i].attachment;
724 pdraw->buffers[i].name = buffers[i].name;
725 pdraw->buffers[i].pitch = buffers[i].pitch;
726 pdraw->buffers[i].cpp = buffers[i].cpp;
727 pdraw->buffers[i].flags = buffers[i].flags;
728 if (pdraw->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)
729 pdraw->have_fake_front = 1;
730 if (pdraw->buffers[i].attachment == __DRI_BUFFER_BACK_LEFT)
731 pdraw->have_back = 1;
732 }
733
734 }
735
736 unsigned dri2GetSwapEventType(Display* dpy, XID drawable)
737 {
738 struct glx_display *glx_dpy = __glXInitialize(dpy);
739 __GLXDRIdrawable *pdraw;
740 pdraw = dri2GetGlxDrawableFromXDrawableId(dpy, drawable);
741 if (!pdraw || !(pdraw->eventMask & GLX_BUFFER_SWAP_COMPLETE_INTEL_MASK))
742 return 0;
743 return glx_dpy->codes->first_event + GLX_BufferSwapComplete;
744 }
745
746 static void show_fps(struct dri2_drawable *draw)
747 {
748 const int interval =
749 ((struct dri2_screen *) draw->base.psc)->show_fps_interval;
750 struct timeval tv;
751 uint64_t current_time;
752
753 gettimeofday(&tv, 0);
754 current_time = (uint64_t)tv.tv_sec*1000000 + (uint64_t)tv.tv_usec;
755
756 draw->frames++;
757
758 if (draw->previous_time + interval * 1000000 <= current_time) {
759 if (draw->previous_time) {
760 fprintf(stderr, "libGL: FPS = %.1f\n",
761 ((uint64_t)draw->frames * 1000000) /
762 (double)(current_time - draw->previous_time));
763 }
764 draw->frames = 0;
765 draw->previous_time = current_time;
766 }
767 }
768
769 static int64_t
770 dri2XcbSwapBuffers(Display *dpy,
771 __GLXDRIdrawable *pdraw,
772 int64_t target_msc,
773 int64_t divisor,
774 int64_t remainder)
775 {
776 xcb_dri2_swap_buffers_cookie_t swap_buffers_cookie;
777 xcb_dri2_swap_buffers_reply_t *swap_buffers_reply;
778 uint32_t target_msc_hi, target_msc_lo;
779 uint32_t divisor_hi, divisor_lo;
780 uint32_t remainder_hi, remainder_lo;
781 int64_t ret = 0;
782 xcb_connection_t *c = XGetXCBConnection(dpy);
783
784 split_counter(target_msc, &target_msc_hi, &target_msc_lo);
785 split_counter(divisor, &divisor_hi, &divisor_lo);
786 split_counter(remainder, &remainder_hi, &remainder_lo);
787
788 swap_buffers_cookie =
789 xcb_dri2_swap_buffers_unchecked(c, pdraw->xDrawable,
790 target_msc_hi, target_msc_lo,
791 divisor_hi, divisor_lo,
792 remainder_hi, remainder_lo);
793
794 /* Immediately wait on the swapbuffers reply. If we didn't, we'd have
795 * to do so some time before reusing a (non-pageflipped) backbuffer.
796 * Otherwise, the new rendering could get ahead of the X Server's
797 * dispatch of the swapbuffer and you'd display garbage.
798 *
799 * We use XSync() first to reap the invalidate events through the event
800 * filter, to ensure that the next drawing doesn't use an invalidated
801 * buffer.
802 */
803 XSync(dpy, False);
804
805 swap_buffers_reply =
806 xcb_dri2_swap_buffers_reply(c, swap_buffers_cookie, NULL);
807 if (swap_buffers_reply) {
808 ret = merge_counter(swap_buffers_reply->swap_hi,
809 swap_buffers_reply->swap_lo);
810 free(swap_buffers_reply);
811 }
812 return ret;
813 }
814
815 static int64_t
816 dri2SwapBuffers(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
817 int64_t remainder, Bool flush)
818 {
819 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
820 struct glx_display *dpyPriv = __glXInitialize(priv->base.psc->dpy);
821 struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
822 struct dri2_display *pdp =
823 (struct dri2_display *)dpyPriv->dri2Display;
824 int64_t ret = 0;
825
826 /* Check we have the right attachments */
827 if (!priv->have_back)
828 return ret;
829
830 /* Old servers can't handle swapbuffers */
831 if (!pdp->swapAvailable) {
832 __dri2CopySubBuffer(pdraw, 0, 0, priv->width, priv->height,
833 __DRI2_THROTTLE_SWAPBUFFER, flush);
834 } else {
835 __DRIcontext *ctx = dri2GetCurrentContext();
836 unsigned flags = __DRI2_FLUSH_DRAWABLE;
837 if (flush)
838 flags |= __DRI2_FLUSH_CONTEXT;
839 dri2Flush(psc, ctx, priv, flags, __DRI2_THROTTLE_SWAPBUFFER);
840
841 ret = dri2XcbSwapBuffers(pdraw->psc->dpy, pdraw,
842 target_msc, divisor, remainder);
843 }
844
845 if (psc->show_fps_interval) {
846 show_fps(priv);
847 }
848
849 /* Old servers don't send invalidate events */
850 if (!pdp->invalidateAvailable)
851 dri2InvalidateBuffers(dpyPriv->dpy, pdraw->xDrawable);
852
853 return ret;
854 }
855
856 static __DRIbuffer *
857 dri2GetBuffers(__DRIdrawable * driDrawable,
858 int *width, int *height,
859 unsigned int *attachments, int count,
860 int *out_count, void *loaderPrivate)
861 {
862 struct dri2_drawable *pdraw = loaderPrivate;
863 DRI2Buffer *buffers;
864
865 buffers = DRI2GetBuffers(pdraw->base.psc->dpy, pdraw->base.xDrawable,
866 width, height, attachments, count, out_count);
867 if (buffers == NULL)
868 return NULL;
869
870 pdraw->width = *width;
871 pdraw->height = *height;
872 process_buffers(pdraw, buffers, *out_count);
873
874 free(buffers);
875
876 return pdraw->buffers;
877 }
878
879 static __DRIbuffer *
880 dri2GetBuffersWithFormat(__DRIdrawable * driDrawable,
881 int *width, int *height,
882 unsigned int *attachments, int count,
883 int *out_count, void *loaderPrivate)
884 {
885 struct dri2_drawable *pdraw = loaderPrivate;
886 DRI2Buffer *buffers;
887
888 buffers = DRI2GetBuffersWithFormat(pdraw->base.psc->dpy,
889 pdraw->base.xDrawable,
890 width, height, attachments,
891 count, out_count);
892 if (buffers == NULL)
893 return NULL;
894
895 pdraw->width = *width;
896 pdraw->height = *height;
897 process_buffers(pdraw, buffers, *out_count);
898
899 free(buffers);
900
901 return pdraw->buffers;
902 }
903
904 static int
905 dri2SetSwapInterval(__GLXDRIdrawable *pdraw, int interval)
906 {
907 xcb_connection_t *c = XGetXCBConnection(pdraw->psc->dpy);
908 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
909 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
910 struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
911
912 if (psc->config)
913 psc->config->configQueryi(psc->driScreen,
914 "vblank_mode", &vblank_mode);
915
916 switch (vblank_mode) {
917 case DRI_CONF_VBLANK_NEVER:
918 if (interval != 0)
919 return GLX_BAD_VALUE;
920 break;
921 case DRI_CONF_VBLANK_ALWAYS_SYNC:
922 if (interval <= 0)
923 return GLX_BAD_VALUE;
924 break;
925 default:
926 break;
927 }
928
929 xcb_dri2_swap_interval(c, priv->base.xDrawable, interval);
930 priv->swap_interval = interval;
931
932 return 0;
933 }
934
935 static int
936 dri2GetSwapInterval(__GLXDRIdrawable *pdraw)
937 {
938 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
939
940 return priv->swap_interval;
941 }
942
943 static const __DRIdri2LoaderExtension dri2LoaderExtension = {
944 {__DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION},
945 dri2GetBuffers,
946 dri2FlushFrontBuffer,
947 dri2GetBuffersWithFormat,
948 };
949
950 static const __DRIdri2LoaderExtension dri2LoaderExtension_old = {
951 {__DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION},
952 dri2GetBuffers,
953 dri2FlushFrontBuffer,
954 NULL,
955 };
956
957 static const __DRIuseInvalidateExtension dri2UseInvalidate = {
958 { __DRI_USE_INVALIDATE, __DRI_USE_INVALIDATE_VERSION }
959 };
960
961 _X_HIDDEN void
962 dri2InvalidateBuffers(Display *dpy, XID drawable)
963 {
964 __GLXDRIdrawable *pdraw =
965 dri2GetGlxDrawableFromXDrawableId(dpy, drawable);
966 struct dri2_screen *psc;
967 struct dri2_drawable *pdp = (struct dri2_drawable *) pdraw;
968
969 if (!pdraw)
970 return;
971
972 psc = (struct dri2_screen *) pdraw->psc;
973
974 if (pdraw && psc->f && psc->f->base.version >= 3 && psc->f->invalidate)
975 psc->f->invalidate(pdp->driDrawable);
976 }
977
978 static void
979 dri2_bind_tex_image(Display * dpy,
980 GLXDrawable drawable,
981 int buffer, const int *attrib_list)
982 {
983 struct glx_context *gc = __glXGetCurrentContext();
984 struct dri2_context *pcp = (struct dri2_context *) gc;
985 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
986 struct glx_display *dpyPriv = __glXInitialize(dpy);
987 struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
988 struct dri2_display *pdp =
989 (struct dri2_display *) dpyPriv->dri2Display;
990 struct dri2_screen *psc;
991
992 if (pdraw != NULL) {
993 psc = (struct dri2_screen *) base->psc;
994
995 if (!pdp->invalidateAvailable && psc->f &&
996 psc->f->base.version >= 3 && psc->f->invalidate)
997 psc->f->invalidate(pdraw->driDrawable);
998
999 if (psc->texBuffer->base.version >= 2 &&
1000 psc->texBuffer->setTexBuffer2 != NULL) {
1001 (*psc->texBuffer->setTexBuffer2) (pcp->driContext,
1002 pdraw->base.textureTarget,
1003 pdraw->base.textureFormat,
1004 pdraw->driDrawable);
1005 }
1006 else {
1007 (*psc->texBuffer->setTexBuffer) (pcp->driContext,
1008 pdraw->base.textureTarget,
1009 pdraw->driDrawable);
1010 }
1011 }
1012 }
1013
1014 static void
1015 dri2_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer)
1016 {
1017 #if __DRI_TEX_BUFFER_VERSION >= 3
1018 struct glx_context *gc = __glXGetCurrentContext();
1019 struct dri2_context *pcp = (struct dri2_context *) gc;
1020 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
1021 struct glx_display *dpyPriv = __glXInitialize(dpy);
1022 struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
1023 struct dri2_display *pdp =
1024 (struct dri2_display *) dpyPriv->dri2Display;
1025 struct dri2_screen *psc;
1026
1027 if (pdraw != NULL) {
1028 psc = (struct dri2_screen *) base->psc;
1029
1030 if (psc->texBuffer->base.version >= 3 &&
1031 psc->texBuffer->releaseTexBuffer != NULL) {
1032 (*psc->texBuffer->releaseTexBuffer) (pcp->driContext,
1033 pdraw->base.textureTarget,
1034 pdraw->driDrawable);
1035 }
1036 }
1037 #endif
1038 }
1039
1040 static const struct glx_context_vtable dri2_context_vtable = {
1041 dri2_destroy_context,
1042 dri2_bind_context,
1043 dri2_unbind_context,
1044 dri2_wait_gl,
1045 dri2_wait_x,
1046 DRI_glXUseXFont,
1047 dri2_bind_tex_image,
1048 dri2_release_tex_image,
1049 NULL, /* get_proc_address */
1050 };
1051
1052 static void
1053 dri2BindExtensions(struct dri2_screen *psc, struct glx_display * priv,
1054 const char *driverName)
1055 {
1056 const struct dri2_display *const pdp = (struct dri2_display *)
1057 priv->dri2Display;
1058 const __DRIextension **extensions;
1059 int i;
1060
1061 extensions = psc->core->getExtensions(psc->driScreen);
1062
1063 __glXEnableDirectExtension(&psc->base, "GLX_SGI_video_sync");
1064 __glXEnableDirectExtension(&psc->base, "GLX_SGI_swap_control");
1065 __glXEnableDirectExtension(&psc->base, "GLX_MESA_swap_control");
1066 __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
1067
1068 /*
1069 * GLX_INTEL_swap_event is broken on the server side, where it's
1070 * currently unconditionally enabled. This completely breaks
1071 * systems running on drivers which don't support that extension.
1072 * There's no way to test for its presence on this side, so instead
1073 * of disabling it unconditionally, just disable it for drivers
1074 * which are known to not support it, or for DDX drivers supporting
1075 * only an older (pre-ScheduleSwap) version of DRI2.
1076 *
1077 * This is a hack which is required until:
1078 * http://lists.x.org/archives/xorg-devel/2013-February/035449.html
1079 * is merged and updated xserver makes it's way into distros:
1080 */
1081 if (pdp->swapAvailable && strcmp(driverName, "vmwgfx") != 0) {
1082 __glXEnableDirectExtension(&psc->base, "GLX_INTEL_swap_event");
1083 }
1084
1085 if (psc->dri2->base.version >= 3) {
1086 const unsigned mask = psc->dri2->getAPIMask(psc->driScreen);
1087
1088 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context");
1089 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context_profile");
1090
1091 if ((mask & (1 << __DRI_API_GLES2)) != 0)
1092 __glXEnableDirectExtension(&psc->base,
1093 "GLX_EXT_create_context_es2_profile");
1094 }
1095
1096 for (i = 0; extensions[i]; i++) {
1097 if ((strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
1098 psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
1099 __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
1100 }
1101
1102 if ((strcmp(extensions[i]->name, __DRI2_FLUSH) == 0)) {
1103 psc->f = (__DRI2flushExtension *) extensions[i];
1104 /* internal driver extension, no GL extension exposed */
1105 }
1106
1107 if ((strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0))
1108 psc->config = (__DRI2configQueryExtension *) extensions[i];
1109
1110 if (((strcmp(extensions[i]->name, __DRI2_THROTTLE) == 0)))
1111 psc->throttle = (__DRI2throttleExtension *) extensions[i];
1112
1113 /* DRI2 version 3 is also required because
1114 * GLX_ARB_create_context_robustness requires GLX_ARB_create_context.
1115 */
1116 if (psc->dri2->base.version >= 3
1117 && strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0)
1118 __glXEnableDirectExtension(&psc->base,
1119 "GLX_ARB_create_context_robustness");
1120
1121 /* DRI2 version 3 is also required because GLX_MESA_query_renderer
1122 * requires GLX_ARB_create_context_profile.
1123 */
1124 if (psc->dri2->base.version >= 3
1125 && strcmp(extensions[i]->name, __DRI2_RENDERER_QUERY) == 0) {
1126 psc->rendererQuery = (__DRI2rendererQueryExtension *) extensions[i];
1127 __glXEnableDirectExtension(&psc->base, "GLX_MESA_query_renderer");
1128 }
1129 }
1130 }
1131
1132 static const struct glx_screen_vtable dri2_screen_vtable = {
1133 dri2_create_context,
1134 dri2_create_context_attribs,
1135 dri2_query_renderer_integer,
1136 dri2_query_renderer_string,
1137 };
1138
1139 static struct glx_screen *
1140 dri2CreateScreen(int screen, struct glx_display * priv)
1141 {
1142 const __DRIconfig **driver_configs;
1143 const __DRIextension **extensions;
1144 const struct dri2_display *const pdp = (struct dri2_display *)
1145 priv->dri2Display;
1146 struct dri2_screen *psc;
1147 __GLXDRIscreen *psp;
1148 struct glx_config *configs = NULL, *visuals = NULL;
1149 char *driverName, *deviceName, *tmp;
1150 drm_magic_t magic;
1151 int i;
1152
1153 psc = calloc(1, sizeof *psc);
1154 if (psc == NULL)
1155 return NULL;
1156
1157 psc->fd = -1;
1158
1159 if (!glx_screen_init(&psc->base, screen, priv)) {
1160 free(psc);
1161 return NULL;
1162 }
1163
1164 if (!DRI2Connect(priv->dpy, RootWindow(priv->dpy, screen),
1165 &driverName, &deviceName)) {
1166 glx_screen_cleanup(&psc->base);
1167 free(psc);
1168 InfoMessageF("screen %d does not appear to be DRI2 capable\n", screen);
1169 return NULL;
1170 }
1171
1172 psc->driver = driOpenDriver(driverName);
1173 if (psc->driver == NULL) {
1174 ErrorMessageF("driver pointer missing\n");
1175 goto handle_error;
1176 }
1177
1178 extensions = driGetDriverExtensions(psc->driver, driverName);
1179 if (extensions == NULL)
1180 goto handle_error;
1181
1182 for (i = 0; extensions[i]; i++) {
1183 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
1184 psc->core = (__DRIcoreExtension *) extensions[i];
1185 if (strcmp(extensions[i]->name, __DRI_DRI2) == 0)
1186 psc->dri2 = (__DRIdri2Extension *) extensions[i];
1187 }
1188
1189 if (psc->core == NULL || psc->dri2 == NULL) {
1190 ErrorMessageF("core dri or dri2 extension not found\n");
1191 goto handle_error;
1192 }
1193
1194 #ifdef O_CLOEXEC
1195 psc->fd = open(deviceName, O_RDWR | O_CLOEXEC);
1196 if (psc->fd == -1 && errno == EINVAL)
1197 #endif
1198 {
1199 psc->fd = open(deviceName, O_RDWR);
1200 if (psc->fd != -1)
1201 fcntl(psc->fd, F_SETFD, fcntl(psc->fd, F_GETFD) | FD_CLOEXEC);
1202 }
1203 if (psc->fd < 0) {
1204 ErrorMessageF("failed to open drm device: %s\n", strerror(errno));
1205 goto handle_error;
1206 }
1207
1208 if (drmGetMagic(psc->fd, &magic)) {
1209 ErrorMessageF("failed to get magic\n");
1210 goto handle_error;
1211 }
1212
1213 if (!DRI2Authenticate(priv->dpy, RootWindow(priv->dpy, screen), magic)) {
1214 ErrorMessageF("failed to authenticate magic %d\n", magic);
1215 goto handle_error;
1216 }
1217
1218 if (psc->dri2->base.version >= 4) {
1219 psc->driScreen =
1220 psc->dri2->createNewScreen2(screen, psc->fd,
1221 (const __DRIextension **)
1222 &pdp->loader_extensions[0],
1223 extensions,
1224 &driver_configs, psc);
1225 } else {
1226 psc->driScreen =
1227 psc->dri2->createNewScreen(screen, psc->fd,
1228 (const __DRIextension **)
1229 &pdp->loader_extensions[0],
1230 &driver_configs, psc);
1231 }
1232
1233 if (psc->driScreen == NULL) {
1234 ErrorMessageF("failed to create dri screen\n");
1235 goto handle_error;
1236 }
1237
1238 dri2BindExtensions(psc, priv, driverName);
1239
1240 configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs);
1241 visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
1242
1243 if (!configs || !visuals)
1244 goto handle_error;
1245
1246 glx_config_destroy_list(psc->base.configs);
1247 psc->base.configs = configs;
1248 glx_config_destroy_list(psc->base.visuals);
1249 psc->base.visuals = visuals;
1250
1251 psc->driver_configs = driver_configs;
1252
1253 psc->base.vtable = &dri2_screen_vtable;
1254 psp = &psc->vtable;
1255 psc->base.driScreen = psp;
1256 psp->destroyScreen = dri2DestroyScreen;
1257 psp->createDrawable = dri2CreateDrawable;
1258 psp->swapBuffers = dri2SwapBuffers;
1259 psp->getDrawableMSC = NULL;
1260 psp->waitForMSC = NULL;
1261 psp->waitForSBC = NULL;
1262 psp->setSwapInterval = NULL;
1263 psp->getSwapInterval = NULL;
1264
1265 if (pdp->driMinor >= 2) {
1266 psp->getDrawableMSC = dri2DrawableGetMSC;
1267 psp->waitForMSC = dri2WaitForMSC;
1268 psp->waitForSBC = dri2WaitForSBC;
1269 psp->setSwapInterval = dri2SetSwapInterval;
1270 psp->getSwapInterval = dri2GetSwapInterval;
1271 __glXEnableDirectExtension(&psc->base, "GLX_OML_sync_control");
1272 }
1273
1274 /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
1275 * available.*/
1276 psp->copySubBuffer = dri2CopySubBuffer;
1277 __glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer");
1278
1279 free(driverName);
1280 free(deviceName);
1281
1282 tmp = getenv("LIBGL_SHOW_FPS");
1283 psc->show_fps_interval = (tmp) ? atoi(tmp) : 0;
1284 if (psc->show_fps_interval < 0)
1285 psc->show_fps_interval = 0;
1286
1287 return &psc->base;
1288
1289 handle_error:
1290 CriticalErrorMessageF("failed to load driver: %s\n", driverName);
1291
1292 if (configs)
1293 glx_config_destroy_list(configs);
1294 if (visuals)
1295 glx_config_destroy_list(visuals);
1296 if (psc->driScreen)
1297 psc->core->destroyScreen(psc->driScreen);
1298 psc->driScreen = NULL;
1299 if (psc->fd >= 0)
1300 close(psc->fd);
1301 if (psc->driver)
1302 dlclose(psc->driver);
1303
1304 free(driverName);
1305 free(deviceName);
1306 glx_screen_cleanup(&psc->base);
1307 free(psc);
1308
1309 return NULL;
1310 }
1311
1312 /* Called from __glXFreeDisplayPrivate.
1313 */
1314 static void
1315 dri2DestroyDisplay(__GLXDRIdisplay * dpy)
1316 {
1317 struct dri2_display *pdp = (struct dri2_display *) dpy;
1318
1319 __glxHashDestroy(pdp->dri2Hash);
1320 free(dpy);
1321 }
1322
1323 _X_HIDDEN __GLXDRIdrawable *
1324 dri2GetGlxDrawableFromXDrawableId(Display *dpy, XID id)
1325 {
1326 struct glx_display *d = __glXInitialize(dpy);
1327 struct dri2_display *pdp = (struct dri2_display *) d->dri2Display;
1328 __GLXDRIdrawable *pdraw;
1329
1330 if (__glxHashLookup(pdp->dri2Hash, id, (void *) &pdraw) == 0)
1331 return pdraw;
1332
1333 return NULL;
1334 }
1335
1336 /*
1337 * Allocate, initialize and return a __DRIdisplayPrivate object.
1338 * This is called from __glXInitialize() when we are given a new
1339 * display pointer.
1340 */
1341 _X_HIDDEN __GLXDRIdisplay *
1342 dri2CreateDisplay(Display * dpy)
1343 {
1344 struct dri2_display *pdp;
1345 int eventBase, errorBase, i;
1346
1347 if (!DRI2QueryExtension(dpy, &eventBase, &errorBase))
1348 return NULL;
1349
1350 pdp = malloc(sizeof *pdp);
1351 if (pdp == NULL)
1352 return NULL;
1353
1354 if (!DRI2QueryVersion(dpy, &pdp->driMajor, &pdp->driMinor)) {
1355 free(pdp);
1356 return NULL;
1357 }
1358
1359 pdp->driPatch = 0;
1360 pdp->swapAvailable = (pdp->driMinor >= 2);
1361 pdp->invalidateAvailable = (pdp->driMinor >= 3);
1362
1363 pdp->base.destroyDisplay = dri2DestroyDisplay;
1364 pdp->base.createScreen = dri2CreateScreen;
1365
1366 i = 0;
1367 if (pdp->driMinor < 1)
1368 pdp->loader_extensions[i++] = &dri2LoaderExtension_old.base;
1369 else
1370 pdp->loader_extensions[i++] = &dri2LoaderExtension.base;
1371
1372 pdp->loader_extensions[i++] = &systemTimeExtension.base;
1373
1374 pdp->loader_extensions[i++] = &dri2UseInvalidate.base;
1375
1376 pdp->loader_extensions[i++] = NULL;
1377
1378 pdp->dri2Hash = __glxHashCreate();
1379 if (pdp->dri2Hash == NULL) {
1380 free(pdp);
1381 return NULL;
1382 }
1383
1384 return &pdp->base;
1385 }
1386
1387 #endif /* GLX_DIRECT_RENDERING */