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