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