dri2_glx.c revision 3750ebd540510324ef5ada769537ae05309adadb
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/extensions/Xdamage.h>
38#include "glapi.h"
39#include "glxclient.h"
40#include <X11/extensions/dri2proto.h>
41#include "xf86dri.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 "xf86drm.h"
48#include "dri2.h"
49#include "dri_common.h"
50
51/* From xmlpool/options.h, user exposed so should be stable */
52#define DRI_CONF_VBLANK_NEVER 0
53#define DRI_CONF_VBLANK_DEF_INTERVAL_0 1
54#define DRI_CONF_VBLANK_DEF_INTERVAL_1 2
55#define DRI_CONF_VBLANK_ALWAYS_SYNC 3
56
57#undef DRI2_MINOR
58#define DRI2_MINOR 1
59
60typedef struct __GLXDRIdisplayPrivateRec __GLXDRIdisplayPrivate;
61typedef struct __GLXDRIcontextPrivateRec __GLXDRIcontextPrivate;
62typedef struct __GLXDRIdrawablePrivateRec __GLXDRIdrawablePrivate;
63
64struct __GLXDRIdisplayPrivateRec
65{
66   __GLXDRIdisplay base;
67
68   /*
69    ** XFree86-DRI version information
70    */
71   int driMajor;
72   int driMinor;
73   int driPatch;
74   int swapAvailable;
75   int invalidateAvailable;
76
77   __glxHashTable *dri2Hash;
78
79   const __DRIextension *loader_extensions[4];
80};
81
82struct __GLXDRIcontextPrivateRec
83{
84   __GLXDRIcontext base;
85   __DRIcontext *driContext;
86   __GLXscreenConfigs *psc;
87};
88
89struct __GLXDRIdrawablePrivateRec
90{
91   __GLXDRIdrawable base;
92   __DRIbuffer buffers[5];
93   int bufferCount;
94   int width, height;
95   int have_back;
96   int have_fake_front;
97   int swap_interval;
98};
99
100static void dri2WaitX(__GLXDRIdrawable * pdraw);
101
102static void
103dri2DestroyContext(__GLXDRIcontext * context,
104                   __GLXscreenConfigs * psc, Display * dpy)
105{
106   __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
107   const __DRIcoreExtension *core = pcp->psc->core;
108
109   (*core->destroyContext) (pcp->driContext);
110
111   Xfree(pcp);
112}
113
114static Bool
115dri2BindContext(__GLXDRIcontext * context,
116                __GLXDRIdrawable * draw, __GLXDRIdrawable * read)
117{
118   __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
119   const __DRIcoreExtension *core = pcp->psc->core;
120
121   return (*core->bindContext) (pcp->driContext,
122                                draw->driDrawable, read->driDrawable);
123}
124
125static void
126dri2UnbindContext(__GLXDRIcontext * context)
127{
128   __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
129   const __DRIcoreExtension *core = pcp->psc->core;
130
131   (*core->unbindContext) (pcp->driContext);
132}
133
134static __GLXDRIcontext *
135dri2CreateContext(__GLXscreenConfigs * psc,
136                  const __GLcontextModes * mode,
137                  GLXContext gc, GLXContext shareList, int renderType)
138{
139   __GLXDRIcontextPrivate *pcp, *pcp_shared;
140   __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) mode;
141   __DRIcontext *shared = NULL;
142
143   if (shareList) {
144      pcp_shared = (__GLXDRIcontextPrivate *) shareList->driContext;
145      shared = pcp_shared->driContext;
146   }
147
148   pcp = Xmalloc(sizeof *pcp);
149   if (pcp == NULL)
150      return NULL;
151
152   pcp->psc = psc;
153   pcp->driContext =
154      (*psc->dri2->createNewContext) (psc->__driScreen,
155                                      config->driConfig, shared, pcp);
156   gc->__driContext = pcp->driContext;
157
158   if (pcp->driContext == NULL) {
159      Xfree(pcp);
160      return NULL;
161   }
162
163   pcp->base.destroyContext = dri2DestroyContext;
164   pcp->base.bindContext = dri2BindContext;
165   pcp->base.unbindContext = dri2UnbindContext;
166
167   return &pcp->base;
168}
169
170static void
171dri2DestroyDrawable(__GLXDRIdrawable *pdraw)
172{
173   const __DRIcoreExtension *core = pdraw->psc->core;
174   __GLXdisplayPrivate *dpyPriv;
175   __GLXDRIdisplayPrivate *pdp;
176
177   dpyPriv = __glXInitialize(pdraw->psc->dpy);
178   pdp = (__GLXDRIdisplayPrivate *)dpyPriv->dri2Display;
179
180   __glxHashDelete(pdp->dri2Hash, pdraw->xDrawable);
181   (*core->destroyDrawable) (pdraw->driDrawable);
182   DRI2DestroyDrawable(pdraw->psc->dpy, pdraw->xDrawable);
183   Xfree(pdraw);
184}
185
186static __GLXDRIdrawable *
187dri2CreateDrawable(__GLXscreenConfigs * psc,
188                   XID xDrawable,
189                   GLXDrawable drawable, const __GLcontextModes * modes)
190{
191   __GLXDRIdrawablePrivate *pdraw;
192   __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes;
193   __GLXdisplayPrivate *dpyPriv;
194   __GLXDRIdisplayPrivate *pdp;
195   GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
196
197   pdraw = Xmalloc(sizeof(*pdraw));
198   if (!pdraw)
199      return NULL;
200
201   pdraw->base.destroyDrawable = dri2DestroyDrawable;
202   pdraw->base.xDrawable = xDrawable;
203   pdraw->base.drawable = drawable;
204   pdraw->base.psc = psc;
205   pdraw->bufferCount = 0;
206   pdraw->swap_interval = 1; /* default may be overridden below */
207   pdraw->have_back = 0;
208
209   if (psc->config)
210      psc->config->configQueryi(psc->__driScreen, "vblank_mode", &vblank_mode);
211
212   switch (vblank_mode) {
213   case DRI_CONF_VBLANK_NEVER:
214   case DRI_CONF_VBLANK_DEF_INTERVAL_0:
215      pdraw->swap_interval = 0;
216      break;
217   case DRI_CONF_VBLANK_DEF_INTERVAL_1:
218   case DRI_CONF_VBLANK_ALWAYS_SYNC:
219   default:
220      pdraw->swap_interval = 1;
221      break;
222   }
223
224   DRI2CreateDrawable(psc->dpy, xDrawable);
225
226   dpyPriv = __glXInitialize(psc->dpy);
227   pdp = (__GLXDRIdisplayPrivate *)dpyPriv->dri2Display;;
228   /* Create a new drawable */
229   pdraw->base.driDrawable =
230      (*psc->dri2->createNewDrawable) (psc->__driScreen,
231                                       config->driConfig, pdraw);
232
233   if (!pdraw->base.driDrawable) {
234      DRI2DestroyDrawable(psc->dpy, xDrawable);
235      Xfree(pdraw);
236      return NULL;
237   }
238
239   if (__glxHashInsert(pdp->dri2Hash, xDrawable, pdraw)) {
240      (*psc->core->destroyDrawable) (pdraw->base.driDrawable);
241      DRI2DestroyDrawable(psc->dpy, xDrawable);
242      Xfree(pdraw);
243      return None;
244   }
245
246
247#ifdef X_DRI2SwapInterval
248   /*
249    * Make sure server has the same swap interval we do for the new
250    * drawable.
251    */
252   if (pdp->swapAvailable)
253      DRI2SwapInterval(psc->dpy, xDrawable, pdraw->swap_interval);
254#endif
255
256   return &pdraw->base;
257}
258
259#ifdef X_DRI2GetMSC
260
261static int
262dri2DrawableGetMSC(__GLXscreenConfigs *psc, __GLXDRIdrawable *pdraw,
263		   int64_t *ust, int64_t *msc, int64_t *sbc)
264{
265   return DRI2GetMSC(psc->dpy, pdraw->xDrawable, ust, msc, sbc);
266}
267
268#endif
269
270
271#ifdef X_DRI2WaitMSC
272
273static int
274dri2WaitForMSC(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
275	       int64_t remainder, int64_t *ust, int64_t *msc, int64_t *sbc)
276{
277   return DRI2WaitMSC(pdraw->psc->dpy, pdraw->xDrawable, target_msc, divisor,
278		      remainder, ust, msc, sbc);
279}
280
281static int
282dri2WaitForSBC(__GLXDRIdrawable *pdraw, int64_t target_sbc, int64_t *ust,
283	       int64_t *msc, int64_t *sbc)
284{
285   return DRI2WaitSBC(pdraw->psc->dpy, pdraw->xDrawable, target_sbc, ust, msc,
286		      sbc);
287}
288
289#endif /* X_DRI2WaitMSC */
290
291static void
292dri2CopySubBuffer(__GLXDRIdrawable *pdraw, int x, int y, int width, int height)
293{
294   __GLXDRIdrawablePrivate *priv = (__GLXDRIdrawablePrivate *) pdraw;
295   XRectangle xrect;
296   XserverRegion region;
297
298   /* Check we have the right attachments */
299   if (!priv->have_back)
300      return;
301
302   xrect.x = x;
303   xrect.y = priv->height - y - height;
304   xrect.width = width;
305   xrect.height = height;
306
307#ifdef __DRI2_FLUSH
308   if (pdraw->psc->f)
309      (*pdraw->psc->f->flush) (pdraw->driDrawable);
310#endif
311
312   region = XFixesCreateRegion(pdraw->psc->dpy, &xrect, 1);
313   /* should get a fence ID back from here at some point */
314   DRI2CopyRegion(pdraw->psc->dpy, pdraw->xDrawable, region,
315                  DRI2BufferFrontLeft, DRI2BufferBackLeft);
316   XFixesDestroyRegion(pdraw->psc->dpy, region);
317
318   /* Refresh the fake front (if present) after we just damaged the real
319    * front.
320    */
321   dri2WaitX(pdraw);
322}
323
324static void
325dri2WaitX(__GLXDRIdrawable *pdraw)
326{
327   __GLXDRIdrawablePrivate *priv = (__GLXDRIdrawablePrivate *) pdraw;
328   XRectangle xrect;
329   XserverRegion region;
330
331   /* Check we have the right attachments */
332   if (!priv->have_fake_front)
333      return;
334
335   xrect.x = 0;
336   xrect.y = 0;
337   xrect.width = priv->width;
338   xrect.height = priv->height;
339
340#ifdef __DRI2_FLUSH
341   if (pdraw->psc->f)
342      (*pdraw->psc->f->flush) (pdraw->driDrawable);
343#endif
344
345   region = XFixesCreateRegion(pdraw->psc->dpy, &xrect, 1);
346   DRI2CopyRegion(pdraw->psc->dpy, pdraw->xDrawable, region,
347                  DRI2BufferFakeFrontLeft, DRI2BufferFrontLeft);
348   XFixesDestroyRegion(pdraw->psc->dpy, region);
349}
350
351static void
352dri2WaitGL(__GLXDRIdrawable * pdraw)
353{
354   __GLXDRIdrawablePrivate *priv = (__GLXDRIdrawablePrivate *) pdraw;
355   XRectangle xrect;
356   XserverRegion region;
357
358   if (!priv->have_fake_front)
359      return;
360
361   xrect.x = 0;
362   xrect.y = 0;
363   xrect.width = priv->width;
364   xrect.height = priv->height;
365
366#ifdef __DRI2_FLUSH
367   if (pdraw->psc->f)
368      (*pdraw->psc->f->flush) (pdraw->driDrawable);
369#endif
370
371   region = XFixesCreateRegion(pdraw->psc->dpy, &xrect, 1);
372   DRI2CopyRegion(pdraw->psc->dpy, pdraw->xDrawable, region,
373                  DRI2BufferFrontLeft, DRI2BufferFakeFrontLeft);
374   XFixesDestroyRegion(pdraw->psc->dpy, region);
375}
376
377static void
378dri2FlushFrontBuffer(__DRIdrawable *driDrawable, void *loaderPrivate)
379{
380   __GLXDRIdrawablePrivate *pdraw = loaderPrivate;
381   __GLXdisplayPrivate *priv = __glXInitialize(pdraw->base.psc->dpy);
382   __GLXDRIdisplayPrivate *pdp = (__GLXDRIdisplayPrivate *)priv->dri2Display;
383
384   /* Old servers don't send invalidate events */
385   if (!pdp->invalidateAvailable)
386       dri2InvalidateBuffers(priv->dpy, pdraw->base.drawable);
387
388   dri2WaitGL(loaderPrivate);
389}
390
391
392static void
393dri2DestroyScreen(__GLXscreenConfigs * psc)
394{
395   /* Free the direct rendering per screen data */
396   (*psc->core->destroyScreen) (psc->__driScreen);
397   close(psc->fd);
398   psc->__driScreen = NULL;
399}
400
401/**
402 * Process list of buffer received from the server
403 *
404 * Processes the list of buffers received in a reply from the server to either
405 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
406 */
407static void
408process_buffers(__GLXDRIdrawablePrivate * pdraw, DRI2Buffer * buffers,
409                unsigned count)
410{
411   int i;
412
413   pdraw->bufferCount = count;
414   pdraw->have_fake_front = 0;
415   pdraw->have_back = 0;
416
417   /* This assumes the DRI2 buffer attachment tokens matches the
418    * __DRIbuffer tokens. */
419   for (i = 0; i < count; i++) {
420      pdraw->buffers[i].attachment = buffers[i].attachment;
421      pdraw->buffers[i].name = buffers[i].name;
422      pdraw->buffers[i].pitch = buffers[i].pitch;
423      pdraw->buffers[i].cpp = buffers[i].cpp;
424      pdraw->buffers[i].flags = buffers[i].flags;
425      if (pdraw->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)
426         pdraw->have_fake_front = 1;
427      if (pdraw->buffers[i].attachment == __DRI_BUFFER_BACK_LEFT)
428         pdraw->have_back = 1;
429   }
430
431}
432
433static int64_t
434dri2SwapBuffers(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
435		int64_t remainder)
436{
437    __GLXDRIdrawablePrivate *priv = (__GLXDRIdrawablePrivate *) pdraw;
438    __GLXdisplayPrivate *dpyPriv = __glXInitialize(priv->base.psc->dpy);
439    __GLXDRIdisplayPrivate *pdp =
440	(__GLXDRIdisplayPrivate *)dpyPriv->dri2Display;
441    int64_t ret;
442
443#ifdef __DRI2_FLUSH
444    if (pdraw->psc->f)
445    	(*pdraw->psc->f->flush)(pdraw->driDrawable);
446#endif
447
448    /* Old servers don't send invalidate events */
449    if (!pdp->invalidateAvailable)
450       dri2InvalidateBuffers(dpyPriv->dpy, pdraw->drawable);
451
452    /* Old servers can't handle swapbuffers */
453    if (!pdp->swapAvailable) {
454       dri2CopySubBuffer(pdraw, 0, 0, priv->width, priv->height);
455       return 0;
456    }
457
458#ifdef X_DRI2SwapBuffers
459    DRI2SwapBuffers(pdraw->psc->dpy, pdraw->xDrawable, target_msc, divisor,
460		    remainder, &ret);
461#endif
462
463    return ret;
464}
465
466static __DRIbuffer *
467dri2GetBuffers(__DRIdrawable * driDrawable,
468               int *width, int *height,
469               unsigned int *attachments, int count,
470               int *out_count, void *loaderPrivate)
471{
472   __GLXDRIdrawablePrivate *pdraw = loaderPrivate;
473   DRI2Buffer *buffers;
474
475   buffers = DRI2GetBuffers(pdraw->base.psc->dpy, pdraw->base.xDrawable,
476                            width, height, attachments, count, out_count);
477   if (buffers == NULL)
478      return NULL;
479
480   pdraw->width = *width;
481   pdraw->height = *height;
482   process_buffers(pdraw, buffers, *out_count);
483
484   Xfree(buffers);
485
486   return pdraw->buffers;
487}
488
489static __DRIbuffer *
490dri2GetBuffersWithFormat(__DRIdrawable * driDrawable,
491                         int *width, int *height,
492                         unsigned int *attachments, int count,
493                         int *out_count, void *loaderPrivate)
494{
495   __GLXDRIdrawablePrivate *pdraw = loaderPrivate;
496   DRI2Buffer *buffers;
497
498   buffers = DRI2GetBuffersWithFormat(pdraw->base.psc->dpy,
499                                      pdraw->base.xDrawable,
500                                      width, height, attachments,
501                                      count, out_count);
502   if (buffers == NULL)
503      return NULL;
504
505   pdraw->width = *width;
506   pdraw->height = *height;
507   process_buffers(pdraw, buffers, *out_count);
508
509   Xfree(buffers);
510
511   return pdraw->buffers;
512}
513
514#ifdef X_DRI2SwapInterval
515
516static void
517dri2SetSwapInterval(__GLXDRIdrawable *pdraw, int interval)
518{
519   __GLXscreenConfigs *psc = pdraw->psc;
520   __GLXDRIdrawablePrivate *priv =  (__GLXDRIdrawablePrivate *) pdraw;
521   GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
522
523   if (psc->config)
524      psc->config->configQueryi(psc->__driScreen, "vblank_mode", &vblank_mode);
525
526   switch (vblank_mode) {
527   case DRI_CONF_VBLANK_NEVER:
528      return;
529   case DRI_CONF_VBLANK_ALWAYS_SYNC:
530      if (interval <= 0)
531	 return;
532      break;
533   default:
534      break;
535   }
536
537   DRI2SwapInterval(priv->base.psc->dpy, pdraw->xDrawable, interval);
538   priv->swap_interval = interval;
539}
540
541static unsigned int
542dri2GetSwapInterval(__GLXDRIdrawable *pdraw)
543{
544   __GLXDRIdrawablePrivate *priv =  (__GLXDRIdrawablePrivate *) pdraw;
545
546  return priv->swap_interval;
547}
548
549#endif /* X_DRI2SwapInterval */
550
551static const __DRIdri2LoaderExtension dri2LoaderExtension = {
552   {__DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION},
553   dri2GetBuffers,
554   dri2FlushFrontBuffer,
555   dri2GetBuffersWithFormat,
556};
557
558static const __DRIdri2LoaderExtension dri2LoaderExtension_old = {
559   {__DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION},
560   dri2GetBuffers,
561   dri2FlushFrontBuffer,
562   NULL,
563};
564
565#ifdef __DRI_USE_INVALIDATE
566static const __DRIuseInvalidateExtension dri2UseInvalidate = {
567   { __DRI_USE_INVALIDATE, __DRI_USE_INVALIDATE_VERSION }
568};
569#endif
570
571_X_HIDDEN void
572dri2InvalidateBuffers(Display *dpy, XID drawable)
573{
574   __GLXDRIdrawable *pdraw =
575      dri2GetGlxDrawableFromXDrawableId(dpy, drawable);
576
577#if __DRI2_FLUSH_VERSION >= 3
578   if (pdraw && pdraw->psc->f)
579       pdraw->psc->f->invalidate(pdraw->driDrawable);
580#endif
581}
582
583static void
584dri2_bind_tex_image(Display * dpy,
585		    GLXDrawable drawable,
586		    int buffer, const int *attrib_list)
587{
588   GLXContext gc = __glXGetCurrentContext();
589   __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, NULL);
590    __GLXdisplayPrivate *dpyPriv = __glXInitialize(dpy);
591    __GLXDRIdisplayPrivate *pdp =
592	(__GLXDRIdisplayPrivate *) dpyPriv->dri2Display;
593
594   if (pdraw != NULL) {
595
596#if __DRI2_FLUSH_VERSION >= 3
597      if (!pdp->invalidateAvailable && pdraw->psc->f)
598	 pdraw->psc->f->invalidate(pdraw->driDrawable);
599#endif
600
601      if (pdraw->psc->texBuffer->base.version >= 2 &&
602	  pdraw->psc->texBuffer->setTexBuffer2 != NULL) {
603	 (*pdraw->psc->texBuffer->setTexBuffer2) (gc->__driContext,
604						  pdraw->textureTarget,
605						  pdraw->textureFormat,
606						  pdraw->driDrawable);
607      }
608      else {
609	 (*pdraw->psc->texBuffer->setTexBuffer) (gc->__driContext,
610						 pdraw->textureTarget,
611						 pdraw->driDrawable);
612      }
613   }
614}
615
616static void
617dri2_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer)
618{
619}
620
621static const struct glx_context_vtable dri2_context_vtable = {
622   dri2_bind_tex_image,
623   dri2_release_tex_image,
624};
625
626static __GLXDRIscreen *
627dri2CreateScreen(__GLXscreenConfigs * psc, int screen,
628                 __GLXdisplayPrivate * priv)
629{
630   const __DRIconfig **driver_configs;
631   const __DRIextension **extensions;
632   const __GLXDRIdisplayPrivate *const pdp = (__GLXDRIdisplayPrivate *)
633      priv->dri2Display;
634   __GLXDRIscreen *psp;
635   char *driverName, *deviceName;
636   drm_magic_t magic;
637   int i;
638
639   psp = Xmalloc(sizeof *psp);
640   if (psp == NULL)
641      return NULL;
642
643   if (!DRI2Connect(psc->dpy, RootWindow(psc->dpy, screen),
644		    &driverName, &deviceName)) {
645      XFree(psp);
646      return NULL;
647   }
648
649   psc->driver = driOpenDriver(driverName);
650   if (psc->driver == NULL) {
651      ErrorMessageF("driver pointer missing\n");
652      goto handle_error;
653   }
654
655   extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
656   if (extensions == NULL) {
657      ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
658      goto handle_error;
659   }
660
661   for (i = 0; extensions[i]; i++) {
662      if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
663	 psc->core = (__DRIcoreExtension *) extensions[i];
664      if (strcmp(extensions[i]->name, __DRI_DRI2) == 0)
665	 psc->dri2 = (__DRIdri2Extension *) extensions[i];
666   }
667
668   if (psc->core == NULL || psc->dri2 == NULL) {
669      ErrorMessageF("core dri or dri2 extension not found\n");
670      goto handle_error;
671   }
672
673   psc->fd = open(deviceName, O_RDWR);
674   if (psc->fd < 0) {
675      ErrorMessageF("failed to open drm device: %s\n", strerror(errno));
676      goto handle_error;
677   }
678
679   if (drmGetMagic(psc->fd, &magic)) {
680      ErrorMessageF("failed to get magic\n");
681      goto handle_error;
682   }
683
684   if (!DRI2Authenticate(psc->dpy, RootWindow(psc->dpy, screen), magic)) {
685      ErrorMessageF("failed to authenticate magic %d\n", magic);
686      goto handle_error;
687   }
688
689
690   /* If the server does not support the protocol for
691    * DRI2GetBuffersWithFormat, don't supply that interface to the driver.
692    */
693   psc->__driScreen =
694      psc->dri2->createNewScreen(screen, psc->fd,
695				 (const __DRIextension **)
696				 &pdp->loader_extensions[0],
697				 &driver_configs, psc);
698
699   if (psc->__driScreen == NULL) {
700      ErrorMessageF("failed to create dri screen\n");
701      goto handle_error;
702   }
703
704   driBindCommonExtensions(psc);
705   dri2BindExtensions(psc);
706
707   psc->configs = driConvertConfigs(psc->core, psc->configs, driver_configs);
708   psc->visuals = driConvertConfigs(psc->core, psc->visuals, driver_configs);
709
710   psc->driver_configs = driver_configs;
711
712   psp->destroyScreen = dri2DestroyScreen;
713   psp->createContext = dri2CreateContext;
714   psp->createDrawable = dri2CreateDrawable;
715   psp->swapBuffers = dri2SwapBuffers;
716   psp->waitGL = dri2WaitGL;
717   psp->waitX = dri2WaitX;
718   psp->getDrawableMSC = NULL;
719   psp->waitForMSC = NULL;
720   psp->waitForSBC = NULL;
721   psp->setSwapInterval = NULL;
722   psp->getSwapInterval = NULL;
723
724   if (pdp->driMinor >= 2) {
725#ifdef X_DRI2GetMSC
726      psp->getDrawableMSC = dri2DrawableGetMSC;
727#endif
728#ifdef X_DRI2WaitMSC
729      psp->waitForMSC = dri2WaitForMSC;
730      psp->waitForSBC = dri2WaitForSBC;
731#endif
732#ifdef X_DRI2SwapInterval
733      psp->setSwapInterval = dri2SetSwapInterval;
734      psp->getSwapInterval = dri2GetSwapInterval;
735#endif
736#if defined(X_DRI2GetMSC) && defined(X_DRI2WaitMSC) && defined(X_DRI2SwapInterval)
737      __glXEnableDirectExtension(psc, "GLX_OML_sync_control");
738#endif
739   }
740
741   /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
742    * available.*/
743   psp->copySubBuffer = dri2CopySubBuffer;
744   __glXEnableDirectExtension(psc, "GLX_MESA_copy_sub_buffer");
745
746   psc->direct_context_vtable = &dri2_context_vtable;
747
748   Xfree(driverName);
749   Xfree(deviceName);
750
751   return psp;
752
753handle_error:
754   Xfree(driverName);
755   Xfree(deviceName);
756   XFree(psp);
757
758   /* FIXME: clean up here */
759
760   return NULL;
761}
762
763/* Called from __glXFreeDisplayPrivate.
764 */
765static void
766dri2DestroyDisplay(__GLXDRIdisplay * dpy)
767{
768   Xfree(dpy);
769}
770
771_X_HIDDEN __GLXDRIdrawable *
772dri2GetGlxDrawableFromXDrawableId(Display *dpy, XID id)
773{
774   __GLXdisplayPrivate *d = __glXInitialize(dpy);
775   __GLXDRIdisplayPrivate *pdp = (__GLXDRIdisplayPrivate *) d->dri2Display;
776   __GLXDRIdrawable *pdraw;
777
778   if (__glxHashLookup(pdp->dri2Hash, id, (void *) &pdraw) == 0)
779      return pdraw;
780
781   return NULL;
782}
783
784/*
785 * Allocate, initialize and return a __DRIdisplayPrivate object.
786 * This is called from __glXInitialize() when we are given a new
787 * display pointer.
788 */
789_X_HIDDEN __GLXDRIdisplay *
790dri2CreateDisplay(Display * dpy)
791{
792   __GLXDRIdisplayPrivate *pdp;
793   int eventBase, errorBase, i;
794
795   if (!DRI2QueryExtension(dpy, &eventBase, &errorBase))
796      return NULL;
797
798   pdp = Xmalloc(sizeof *pdp);
799   if (pdp == NULL)
800      return NULL;
801
802   if (!DRI2QueryVersion(dpy, &pdp->driMajor, &pdp->driMinor)) {
803      Xfree(pdp);
804      return NULL;
805   }
806
807   pdp->driPatch = 0;
808   pdp->swapAvailable = (pdp->driMinor >= 2);
809   pdp->invalidateAvailable = (pdp->driMinor >= 3);
810
811   pdp->base.destroyDisplay = dri2DestroyDisplay;
812   pdp->base.createScreen = dri2CreateScreen;
813
814   i = 0;
815   if (pdp->driMinor < 1)
816      pdp->loader_extensions[i++] = &dri2LoaderExtension_old.base;
817   else
818      pdp->loader_extensions[i++] = &dri2LoaderExtension.base;
819
820   pdp->loader_extensions[i++] = &systemTimeExtension.base;
821
822#ifdef __DRI_USE_INVALIDATE
823   pdp->loader_extensions[i++] = &dri2UseInvalidate.base;
824#endif
825   pdp->loader_extensions[i++] = NULL;
826
827   pdp->dri2Hash = __glxHashCreate();
828   if (pdp->dri2Hash == NULL) {
829      Xfree(pdp);
830      return NULL;
831   }
832
833   return &pdp->base;
834}
835
836#endif /* GLX_DIRECT_RENDERING */
837