glx/dri: use the implemented version of __DRIgetDrawableInfoExtension
[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 .base = { __DRI_DRI2_LOADER, 3 },
959
960 .getBuffers = dri2GetBuffers,
961 .flushFrontBuffer = dri2FlushFrontBuffer,
962 .getBuffersWithFormat = dri2GetBuffersWithFormat,
963 };
964
965 static const __DRIdri2LoaderExtension dri2LoaderExtension_old = {
966 .base = { __DRI_DRI2_LOADER, 3 },
967
968 .getBuffers = dri2GetBuffers,
969 .flushFrontBuffer = dri2FlushFrontBuffer,
970 .getBuffersWithFormat = NULL,
971 };
972
973 static const __DRIuseInvalidateExtension dri2UseInvalidate = {
974 { __DRI_USE_INVALIDATE, __DRI_USE_INVALIDATE_VERSION }
975 };
976
977 _X_HIDDEN void
978 dri2InvalidateBuffers(Display *dpy, XID drawable)
979 {
980 __GLXDRIdrawable *pdraw =
981 dri2GetGlxDrawableFromXDrawableId(dpy, drawable);
982 struct dri2_screen *psc;
983 struct dri2_drawable *pdp = (struct dri2_drawable *) pdraw;
984
985 if (!pdraw)
986 return;
987
988 psc = (struct dri2_screen *) pdraw->psc;
989
990 if (pdraw && psc->f && psc->f->base.version >= 3 && psc->f->invalidate)
991 psc->f->invalidate(pdp->driDrawable);
992 }
993
994 static void
995 dri2_bind_tex_image(Display * dpy,
996 GLXDrawable drawable,
997 int buffer, const int *attrib_list)
998 {
999 struct glx_context *gc = __glXGetCurrentContext();
1000 struct dri2_context *pcp = (struct dri2_context *) gc;
1001 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
1002 struct glx_display *dpyPriv = __glXInitialize(dpy);
1003 struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
1004 struct dri2_display *pdp;
1005 struct dri2_screen *psc;
1006
1007 if (dpyPriv == NULL)
1008 return;
1009
1010 pdp = (struct dri2_display *) dpyPriv->dri2Display;
1011
1012 if (pdraw != NULL) {
1013 psc = (struct dri2_screen *) base->psc;
1014
1015 if (!pdp->invalidateAvailable && psc->f &&
1016 psc->f->base.version >= 3 && psc->f->invalidate)
1017 psc->f->invalidate(pdraw->driDrawable);
1018
1019 if (psc->texBuffer->base.version >= 2 &&
1020 psc->texBuffer->setTexBuffer2 != NULL) {
1021 (*psc->texBuffer->setTexBuffer2) (pcp->driContext,
1022 pdraw->base.textureTarget,
1023 pdraw->base.textureFormat,
1024 pdraw->driDrawable);
1025 }
1026 else {
1027 (*psc->texBuffer->setTexBuffer) (pcp->driContext,
1028 pdraw->base.textureTarget,
1029 pdraw->driDrawable);
1030 }
1031 }
1032 }
1033
1034 static void
1035 dri2_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer)
1036 {
1037 #if __DRI_TEX_BUFFER_VERSION >= 3
1038 struct glx_context *gc = __glXGetCurrentContext();
1039 struct dri2_context *pcp = (struct dri2_context *) gc;
1040 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
1041 struct glx_display *dpyPriv = __glXInitialize(dpy);
1042 struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
1043 struct dri2_display *pdp =
1044 (struct dri2_display *) dpyPriv->dri2Display;
1045 struct dri2_screen *psc;
1046
1047 if (pdraw != NULL) {
1048 psc = (struct dri2_screen *) base->psc;
1049
1050 if (psc->texBuffer->base.version >= 3 &&
1051 psc->texBuffer->releaseTexBuffer != NULL) {
1052 (*psc->texBuffer->releaseTexBuffer) (pcp->driContext,
1053 pdraw->base.textureTarget,
1054 pdraw->driDrawable);
1055 }
1056 }
1057 #endif
1058 }
1059
1060 static const struct glx_context_vtable dri2_context_vtable = {
1061 dri2_destroy_context,
1062 dri2_bind_context,
1063 dri2_unbind_context,
1064 dri2_wait_gl,
1065 dri2_wait_x,
1066 DRI_glXUseXFont,
1067 dri2_bind_tex_image,
1068 dri2_release_tex_image,
1069 NULL, /* get_proc_address */
1070 };
1071
1072 static void
1073 dri2BindExtensions(struct dri2_screen *psc, struct glx_display * priv,
1074 const char *driverName)
1075 {
1076 const struct dri2_display *const pdp = (struct dri2_display *)
1077 priv->dri2Display;
1078 const __DRIextension **extensions;
1079 int i;
1080
1081 extensions = psc->core->getExtensions(psc->driScreen);
1082
1083 __glXEnableDirectExtension(&psc->base, "GLX_SGI_video_sync");
1084 __glXEnableDirectExtension(&psc->base, "GLX_SGI_swap_control");
1085 __glXEnableDirectExtension(&psc->base, "GLX_MESA_swap_control");
1086 __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
1087
1088 /*
1089 * GLX_INTEL_swap_event is broken on the server side, where it's
1090 * currently unconditionally enabled. This completely breaks
1091 * systems running on drivers which don't support that extension.
1092 * There's no way to test for its presence on this side, so instead
1093 * of disabling it unconditionally, just disable it for drivers
1094 * which are known to not support it, or for DDX drivers supporting
1095 * only an older (pre-ScheduleSwap) version of DRI2.
1096 *
1097 * This is a hack which is required until:
1098 * http://lists.x.org/archives/xorg-devel/2013-February/035449.html
1099 * is merged and updated xserver makes it's way into distros:
1100 */
1101 if (pdp->swapAvailable && strcmp(driverName, "vmwgfx") != 0) {
1102 __glXEnableDirectExtension(&psc->base, "GLX_INTEL_swap_event");
1103 }
1104
1105 if (psc->dri2->base.version >= 3) {
1106 const unsigned mask = psc->dri2->getAPIMask(psc->driScreen);
1107
1108 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context");
1109 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context_profile");
1110
1111 if ((mask & (1 << __DRI_API_GLES2)) != 0)
1112 __glXEnableDirectExtension(&psc->base,
1113 "GLX_EXT_create_context_es2_profile");
1114 }
1115
1116 for (i = 0; extensions[i]; i++) {
1117 if ((strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
1118 psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
1119 __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
1120 }
1121
1122 if ((strcmp(extensions[i]->name, __DRI2_FLUSH) == 0)) {
1123 psc->f = (__DRI2flushExtension *) extensions[i];
1124 /* internal driver extension, no GL extension exposed */
1125 }
1126
1127 if ((strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0))
1128 psc->config = (__DRI2configQueryExtension *) extensions[i];
1129
1130 if (((strcmp(extensions[i]->name, __DRI2_THROTTLE) == 0)))
1131 psc->throttle = (__DRI2throttleExtension *) extensions[i];
1132
1133 /* DRI2 version 3 is also required because
1134 * GLX_ARB_create_context_robustness requires GLX_ARB_create_context.
1135 */
1136 if (psc->dri2->base.version >= 3
1137 && strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0)
1138 __glXEnableDirectExtension(&psc->base,
1139 "GLX_ARB_create_context_robustness");
1140
1141 /* DRI2 version 3 is also required because GLX_MESA_query_renderer
1142 * requires GLX_ARB_create_context_profile.
1143 */
1144 if (psc->dri2->base.version >= 3
1145 && strcmp(extensions[i]->name, __DRI2_RENDERER_QUERY) == 0) {
1146 psc->rendererQuery = (__DRI2rendererQueryExtension *) extensions[i];
1147 __glXEnableDirectExtension(&psc->base, "GLX_MESA_query_renderer");
1148 }
1149 }
1150 }
1151
1152 static const struct glx_screen_vtable dri2_screen_vtable = {
1153 dri2_create_context,
1154 dri2_create_context_attribs,
1155 dri2_query_renderer_integer,
1156 dri2_query_renderer_string,
1157 };
1158
1159 static struct glx_screen *
1160 dri2CreateScreen(int screen, struct glx_display * priv)
1161 {
1162 const __DRIconfig **driver_configs;
1163 const __DRIextension **extensions;
1164 const struct dri2_display *const pdp = (struct dri2_display *)
1165 priv->dri2Display;
1166 struct dri2_screen *psc;
1167 __GLXDRIscreen *psp;
1168 struct glx_config *configs = NULL, *visuals = NULL;
1169 char *driverName = NULL, *loader_driverName, *deviceName, *tmp;
1170 drm_magic_t magic;
1171 int i;
1172
1173 psc = calloc(1, sizeof *psc);
1174 if (psc == NULL)
1175 return NULL;
1176
1177 psc->fd = -1;
1178
1179 if (!glx_screen_init(&psc->base, screen, priv)) {
1180 free(psc);
1181 return NULL;
1182 }
1183
1184 if (!DRI2Connect(priv->dpy, RootWindow(priv->dpy, screen),
1185 &driverName, &deviceName)) {
1186 glx_screen_cleanup(&psc->base);
1187 free(psc);
1188 InfoMessageF("screen %d does not appear to be DRI2 capable\n", screen);
1189 return NULL;
1190 }
1191
1192 #ifdef O_CLOEXEC
1193 psc->fd = open(deviceName, O_RDWR | O_CLOEXEC);
1194 if (psc->fd == -1 && errno == EINVAL)
1195 #endif
1196 {
1197 psc->fd = open(deviceName, O_RDWR);
1198 if (psc->fd != -1)
1199 fcntl(psc->fd, F_SETFD, fcntl(psc->fd, F_GETFD) | FD_CLOEXEC);
1200 }
1201 if (psc->fd < 0) {
1202 ErrorMessageF("failed to open drm device: %s\n", strerror(errno));
1203 goto handle_error;
1204 }
1205
1206 /* If Mesa knows about the appropriate driver for this fd, then trust it.
1207 * Otherwise, default to the server's value.
1208 */
1209 loader_driverName = loader_get_driver_for_fd(psc->fd, 0);
1210 if (loader_driverName) {
1211 free(driverName);
1212 driverName = loader_driverName;
1213 }
1214
1215 psc->driver = driOpenDriver(driverName);
1216 if (psc->driver == NULL) {
1217 ErrorMessageF("driver pointer missing\n");
1218 goto handle_error;
1219 }
1220
1221 extensions = driGetDriverExtensions(psc->driver, driverName);
1222 if (extensions == NULL)
1223 goto handle_error;
1224
1225 for (i = 0; extensions[i]; i++) {
1226 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
1227 psc->core = (__DRIcoreExtension *) extensions[i];
1228 if (strcmp(extensions[i]->name, __DRI_DRI2) == 0)
1229 psc->dri2 = (__DRIdri2Extension *) extensions[i];
1230 }
1231
1232 if (psc->core == NULL || psc->dri2 == NULL) {
1233 ErrorMessageF("core dri or dri2 extension not found\n");
1234 goto handle_error;
1235 }
1236
1237 if (drmGetMagic(psc->fd, &magic)) {
1238 ErrorMessageF("failed to get magic\n");
1239 goto handle_error;
1240 }
1241
1242 if (!DRI2Authenticate(priv->dpy, RootWindow(priv->dpy, screen), magic)) {
1243 ErrorMessageF("failed to authenticate magic %d\n", magic);
1244 goto handle_error;
1245 }
1246
1247 if (psc->dri2->base.version >= 4) {
1248 psc->driScreen =
1249 psc->dri2->createNewScreen2(screen, psc->fd,
1250 (const __DRIextension **)
1251 &pdp->loader_extensions[0],
1252 extensions,
1253 &driver_configs, psc);
1254 } else {
1255 psc->driScreen =
1256 psc->dri2->createNewScreen(screen, psc->fd,
1257 (const __DRIextension **)
1258 &pdp->loader_extensions[0],
1259 &driver_configs, psc);
1260 }
1261
1262 if (psc->driScreen == NULL) {
1263 ErrorMessageF("failed to create dri screen\n");
1264 goto handle_error;
1265 }
1266
1267 dri2BindExtensions(psc, priv, driverName);
1268
1269 configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs);
1270 visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
1271
1272 if (!configs || !visuals)
1273 goto handle_error;
1274
1275 glx_config_destroy_list(psc->base.configs);
1276 psc->base.configs = configs;
1277 glx_config_destroy_list(psc->base.visuals);
1278 psc->base.visuals = visuals;
1279
1280 psc->driver_configs = driver_configs;
1281
1282 psc->base.vtable = &dri2_screen_vtable;
1283 psp = &psc->vtable;
1284 psc->base.driScreen = psp;
1285 psp->destroyScreen = dri2DestroyScreen;
1286 psp->createDrawable = dri2CreateDrawable;
1287 psp->swapBuffers = dri2SwapBuffers;
1288 psp->getDrawableMSC = NULL;
1289 psp->waitForMSC = NULL;
1290 psp->waitForSBC = NULL;
1291 psp->setSwapInterval = NULL;
1292 psp->getSwapInterval = NULL;
1293
1294 if (pdp->driMinor >= 2) {
1295 psp->getDrawableMSC = dri2DrawableGetMSC;
1296 psp->waitForMSC = dri2WaitForMSC;
1297 psp->waitForSBC = dri2WaitForSBC;
1298 psp->setSwapInterval = dri2SetSwapInterval;
1299 psp->getSwapInterval = dri2GetSwapInterval;
1300 __glXEnableDirectExtension(&psc->base, "GLX_OML_sync_control");
1301 }
1302
1303 /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
1304 * available.*/
1305 psp->copySubBuffer = dri2CopySubBuffer;
1306 __glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer");
1307
1308 free(driverName);
1309 free(deviceName);
1310
1311 tmp = getenv("LIBGL_SHOW_FPS");
1312 psc->show_fps_interval = (tmp) ? atoi(tmp) : 0;
1313 if (psc->show_fps_interval < 0)
1314 psc->show_fps_interval = 0;
1315
1316 return &psc->base;
1317
1318 handle_error:
1319 CriticalErrorMessageF("failed to load driver: %s\n", driverName);
1320
1321 if (configs)
1322 glx_config_destroy_list(configs);
1323 if (visuals)
1324 glx_config_destroy_list(visuals);
1325 if (psc->driScreen)
1326 psc->core->destroyScreen(psc->driScreen);
1327 psc->driScreen = NULL;
1328 if (psc->fd >= 0)
1329 close(psc->fd);
1330 if (psc->driver)
1331 dlclose(psc->driver);
1332
1333 free(driverName);
1334 free(deviceName);
1335 glx_screen_cleanup(&psc->base);
1336 free(psc);
1337
1338 return NULL;
1339 }
1340
1341 /* Called from __glXFreeDisplayPrivate.
1342 */
1343 static void
1344 dri2DestroyDisplay(__GLXDRIdisplay * dpy)
1345 {
1346 struct dri2_display *pdp = (struct dri2_display *) dpy;
1347
1348 __glxHashDestroy(pdp->dri2Hash);
1349 free(dpy);
1350 }
1351
1352 _X_HIDDEN __GLXDRIdrawable *
1353 dri2GetGlxDrawableFromXDrawableId(Display *dpy, XID id)
1354 {
1355 struct glx_display *d = __glXInitialize(dpy);
1356 struct dri2_display *pdp = (struct dri2_display *) d->dri2Display;
1357 __GLXDRIdrawable *pdraw;
1358
1359 if (__glxHashLookup(pdp->dri2Hash, id, (void *) &pdraw) == 0)
1360 return pdraw;
1361
1362 return NULL;
1363 }
1364
1365 /*
1366 * Allocate, initialize and return a __DRIdisplayPrivate object.
1367 * This is called from __glXInitialize() when we are given a new
1368 * display pointer.
1369 */
1370 _X_HIDDEN __GLXDRIdisplay *
1371 dri2CreateDisplay(Display * dpy)
1372 {
1373 struct dri2_display *pdp;
1374 int eventBase, errorBase, i;
1375
1376 if (!DRI2QueryExtension(dpy, &eventBase, &errorBase))
1377 return NULL;
1378
1379 pdp = malloc(sizeof *pdp);
1380 if (pdp == NULL)
1381 return NULL;
1382
1383 if (!DRI2QueryVersion(dpy, &pdp->driMajor, &pdp->driMinor)) {
1384 free(pdp);
1385 return NULL;
1386 }
1387
1388 pdp->driPatch = 0;
1389 pdp->swapAvailable = (pdp->driMinor >= 2);
1390 pdp->invalidateAvailable = (pdp->driMinor >= 3);
1391
1392 pdp->base.destroyDisplay = dri2DestroyDisplay;
1393 pdp->base.createScreen = dri2CreateScreen;
1394
1395 i = 0;
1396 if (pdp->driMinor < 1)
1397 pdp->loader_extensions[i++] = &dri2LoaderExtension_old.base;
1398 else
1399 pdp->loader_extensions[i++] = &dri2LoaderExtension.base;
1400
1401 pdp->loader_extensions[i++] = &systemTimeExtension.base;
1402
1403 pdp->loader_extensions[i++] = &dri2UseInvalidate.base;
1404
1405 pdp->loader_extensions[i++] = NULL;
1406
1407 pdp->dri2Hash = __glxHashCreate();
1408 if (pdp->dri2Hash == NULL) {
1409 free(pdp);
1410 return NULL;
1411 }
1412
1413 return &pdp->base;
1414 }
1415
1416 #endif /* GLX_DIRECT_RENDERING */