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