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