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