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