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