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