dri2_glx.c revision 3464ebd5
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
60struct dri2_display
61{
62   __GLXDRIdisplay base;
63
64   /*
65    ** XFree86-DRI version information
66    */
67   int driMajor;
68   int driMinor;
69   int driPatch;
70   int swapAvailable;
71   int invalidateAvailable;
72
73   __glxHashTable *dri2Hash;
74
75   const __DRIextension *loader_extensions[4];
76};
77
78struct dri2_screen {
79   struct glx_screen base;
80
81   __DRIscreen *driScreen;
82   __GLXDRIscreen vtable;
83   const __DRIdri2Extension *dri2;
84   const __DRIcoreExtension *core;
85
86   const __DRI2flushExtension *f;
87   const __DRI2configQueryExtension *config;
88   const __DRItexBufferExtension *texBuffer;
89   const __DRIconfig **driver_configs;
90
91   void *driver;
92   int fd;
93};
94
95struct dri2_context
96{
97   struct glx_context base;
98   __DRIcontext *driContext;
99};
100
101struct dri2_drawable
102{
103   __GLXDRIdrawable base;
104   __DRIdrawable *driDrawable;
105   __DRIbuffer buffers[5];
106   int bufferCount;
107   int width, height;
108   int have_back;
109   int have_fake_front;
110   int swap_interval;
111};
112
113static const struct glx_context_vtable dri2_context_vtable;
114
115static void
116dri2_destroy_context(struct glx_context *context)
117{
118   struct dri2_context *pcp = (struct dri2_context *) context;
119   struct dri2_screen *psc = (struct dri2_screen *) context->psc;
120
121   driReleaseDrawables(&pcp->base);
122
123   if (context->xid)
124      glx_send_destroy_context(psc->base.dpy, context->xid);
125
126   if (context->extensions)
127      XFree((char *) context->extensions);
128
129   (*psc->core->destroyContext) (pcp->driContext);
130
131   Xfree(pcp);
132}
133
134static Bool
135dri2_bind_context(struct glx_context *context, struct glx_context *old,
136		  GLXDrawable draw, GLXDrawable read)
137{
138   struct dri2_context *pcp = (struct dri2_context *) context;
139   struct dri2_screen *psc = (struct dri2_screen *) pcp->base.psc;
140   struct dri2_drawable *pdraw, *pread;
141   struct dri2_display *pdp;
142
143   pdraw = (struct dri2_drawable *) driFetchDrawable(context, draw);
144   pread = (struct dri2_drawable *) driFetchDrawable(context, read);
145
146   driReleaseDrawables(&pcp->base);
147
148   if (pdraw == NULL || pread == NULL)
149      return GLXBadDrawable;
150
151   if (!(*psc->core->bindContext) (pcp->driContext,
152				   pdraw->driDrawable, pread->driDrawable))
153      return GLXBadContext;
154
155   /* If the server doesn't send invalidate events, we may miss a
156    * resize before the rendering starts.  Invalidate the buffers now
157    * so the driver will recheck before rendering starts. */
158   pdp = (struct dri2_display *) psc->base.display;
159   if (!pdp->invalidateAvailable) {
160      dri2InvalidateBuffers(psc->base.dpy, pdraw->base.xDrawable);
161      if (pread != pdraw)
162	 dri2InvalidateBuffers(psc->base.dpy, pread->base.xDrawable);
163   }
164
165   return Success;
166}
167
168static void
169dri2_unbind_context(struct glx_context *context, struct glx_context *new)
170{
171   struct dri2_context *pcp = (struct dri2_context *) context;
172   struct dri2_screen *psc = (struct dri2_screen *) pcp->base.psc;
173
174   (*psc->core->unbindContext) (pcp->driContext);
175}
176
177static struct glx_context *
178dri2_create_context(struct glx_screen *base,
179		    struct glx_config *config_base,
180		    struct glx_context *shareList, int renderType)
181{
182   struct dri2_context *pcp, *pcp_shared;
183   struct dri2_screen *psc = (struct dri2_screen *) base;
184   __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
185   __DRIcontext *shared = NULL;
186
187   if (shareList) {
188      pcp_shared = (struct dri2_context *) shareList;
189      shared = pcp_shared->driContext;
190   }
191
192   pcp = Xmalloc(sizeof *pcp);
193   if (pcp == NULL)
194      return NULL;
195
196   memset(pcp, 0, sizeof *pcp);
197   if (!glx_context_init(&pcp->base, &psc->base, &config->base)) {
198      Xfree(pcp);
199      return NULL;
200   }
201
202   pcp->driContext =
203      (*psc->dri2->createNewContext) (psc->driScreen,
204                                      config->driConfig, shared, pcp);
205
206   if (pcp->driContext == NULL) {
207      Xfree(pcp);
208      return NULL;
209   }
210
211   pcp->base.vtable = &dri2_context_vtable;
212
213   return &pcp->base;
214}
215
216static void
217dri2DestroyDrawable(__GLXDRIdrawable *base)
218{
219   struct dri2_screen *psc = (struct dri2_screen *) base->psc;
220   struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
221   struct glx_display *dpyPriv = psc->base.display;
222   struct dri2_display *pdp = (struct dri2_display *)dpyPriv->dri2Display;
223
224   __glxHashDelete(pdp->dri2Hash, pdraw->base.xDrawable);
225   (*psc->core->destroyDrawable) (pdraw->driDrawable);
226
227   /* If it's a GLX 1.3 drawables, we can destroy the DRI2 drawable
228    * now, as the application explicitly asked to destroy the GLX
229    * drawable.  Otherwise, for legacy drawables, we let the DRI2
230    * drawable linger on the server, since there's no good way of
231    * knowing when the application is done with it.  The server will
232    * destroy the DRI2 drawable when it destroys the X drawable or the
233    * client exits anyway. */
234   if (pdraw->base.xDrawable != pdraw->base.drawable)
235      DRI2DestroyDrawable(psc->base.dpy, pdraw->base.xDrawable);
236
237   Xfree(pdraw);
238}
239
240static __GLXDRIdrawable *
241dri2CreateDrawable(struct glx_screen *base, XID xDrawable,
242		   GLXDrawable drawable, struct glx_config *config_base)
243{
244   struct dri2_drawable *pdraw;
245   struct dri2_screen *psc = (struct dri2_screen *) base;
246   __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
247   struct glx_display *dpyPriv;
248   struct dri2_display *pdp;
249   GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
250
251   pdraw = Xmalloc(sizeof(*pdraw));
252   if (!pdraw)
253      return NULL;
254
255   memset(pdraw, 0, sizeof *pdraw);
256   pdraw->base.destroyDrawable = dri2DestroyDrawable;
257   pdraw->base.xDrawable = xDrawable;
258   pdraw->base.drawable = drawable;
259   pdraw->base.psc = &psc->base;
260   pdraw->bufferCount = 0;
261   pdraw->swap_interval = 1; /* default may be overridden below */
262   pdraw->have_back = 0;
263
264   if (psc->config)
265      psc->config->configQueryi(psc->driScreen,
266				"vblank_mode", &vblank_mode);
267
268   switch (vblank_mode) {
269   case DRI_CONF_VBLANK_NEVER:
270   case DRI_CONF_VBLANK_DEF_INTERVAL_0:
271      pdraw->swap_interval = 0;
272      break;
273   case DRI_CONF_VBLANK_DEF_INTERVAL_1:
274   case DRI_CONF_VBLANK_ALWAYS_SYNC:
275   default:
276      pdraw->swap_interval = 1;
277      break;
278   }
279
280   DRI2CreateDrawable(psc->base.dpy, xDrawable);
281
282   dpyPriv = __glXInitialize(psc->base.dpy);
283   pdp = (struct dri2_display *)dpyPriv->dri2Display;;
284   /* Create a new drawable */
285   pdraw->driDrawable =
286      (*psc->dri2->createNewDrawable) (psc->driScreen,
287                                       config->driConfig, pdraw);
288
289   if (!pdraw->driDrawable) {
290      DRI2DestroyDrawable(psc->base.dpy, xDrawable);
291      Xfree(pdraw);
292      return NULL;
293   }
294
295   if (__glxHashInsert(pdp->dri2Hash, xDrawable, pdraw)) {
296      (*psc->core->destroyDrawable) (pdraw->driDrawable);
297      DRI2DestroyDrawable(psc->base.dpy, xDrawable);
298      Xfree(pdraw);
299      return None;
300   }
301
302
303#ifdef X_DRI2SwapInterval
304   /*
305    * Make sure server has the same swap interval we do for the new
306    * drawable.
307    */
308   if (pdp->swapAvailable)
309      DRI2SwapInterval(psc->base.dpy, xDrawable, pdraw->swap_interval);
310#endif
311
312   return &pdraw->base;
313}
314
315#ifdef X_DRI2GetMSC
316
317static int
318dri2DrawableGetMSC(struct glx_screen *psc, __GLXDRIdrawable *pdraw,
319		   int64_t *ust, int64_t *msc, int64_t *sbc)
320{
321   CARD64 dri2_ust, dri2_msc, dri2_sbc;
322   int ret;
323
324   ret = DRI2GetMSC(psc->dpy, pdraw->xDrawable,
325		    &dri2_ust, &dri2_msc, &dri2_sbc);
326   *ust = dri2_ust;
327   *msc = dri2_msc;
328   *sbc = dri2_sbc;
329
330   return ret;
331}
332
333#endif
334
335
336#ifdef X_DRI2WaitMSC
337
338static int
339dri2WaitForMSC(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
340	       int64_t remainder, int64_t *ust, int64_t *msc, int64_t *sbc)
341{
342   CARD64 dri2_ust, dri2_msc, dri2_sbc;
343   int ret;
344
345   ret = DRI2WaitMSC(pdraw->psc->dpy, pdraw->xDrawable, target_msc, divisor,
346		     remainder, &dri2_ust, &dri2_msc, &dri2_sbc);
347   *ust = dri2_ust;
348   *msc = dri2_msc;
349   *sbc = dri2_sbc;
350
351   return ret;
352}
353
354static int
355dri2WaitForSBC(__GLXDRIdrawable *pdraw, int64_t target_sbc, int64_t *ust,
356	       int64_t *msc, int64_t *sbc)
357{
358   CARD64 dri2_ust, dri2_msc, dri2_sbc;
359   int ret;
360
361   ret = DRI2WaitSBC(pdraw->psc->dpy, pdraw->xDrawable,
362		     target_sbc, &dri2_ust, &dri2_msc, &dri2_sbc);
363   *ust = dri2_ust;
364   *msc = dri2_msc;
365   *sbc = dri2_sbc;
366
367   return ret;
368}
369
370#endif /* X_DRI2WaitMSC */
371
372static void
373dri2CopySubBuffer(__GLXDRIdrawable *pdraw, int x, int y, int width, int height)
374{
375   struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
376   struct dri2_screen *psc = (struct dri2_screen *) pdraw->psc;
377   XRectangle xrect;
378   XserverRegion region;
379
380   /* Check we have the right attachments */
381   if (!priv->have_back)
382      return;
383
384   xrect.x = x;
385   xrect.y = priv->height - y - height;
386   xrect.width = width;
387   xrect.height = height;
388
389#ifdef __DRI2_FLUSH
390   if (psc->f)
391      (*psc->f->flush) (priv->driDrawable);
392#endif
393
394   region = XFixesCreateRegion(psc->base.dpy, &xrect, 1);
395   DRI2CopyRegion(psc->base.dpy, pdraw->xDrawable, region,
396                  DRI2BufferFrontLeft, DRI2BufferBackLeft);
397
398   /* Refresh the fake front (if present) after we just damaged the real
399    * front.
400    */
401   if (priv->have_fake_front)
402      DRI2CopyRegion(psc->base.dpy, pdraw->xDrawable, region,
403		     DRI2BufferFakeFrontLeft, DRI2BufferFrontLeft);
404
405   XFixesDestroyRegion(psc->base.dpy, region);
406}
407
408static void
409dri2_copy_drawable(struct dri2_drawable *priv, int dest, int src)
410{
411   XRectangle xrect;
412   XserverRegion region;
413   struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
414
415   xrect.x = 0;
416   xrect.y = 0;
417   xrect.width = priv->width;
418   xrect.height = priv->height;
419
420#ifdef __DRI2_FLUSH
421   if (psc->f)
422      (*psc->f->flush) (priv->driDrawable);
423#endif
424
425   region = XFixesCreateRegion(psc->base.dpy, &xrect, 1);
426   DRI2CopyRegion(psc->base.dpy, priv->base.xDrawable, region, dest, src);
427   XFixesDestroyRegion(psc->base.dpy, region);
428
429}
430
431static void
432dri2_wait_x(struct glx_context *gc)
433{
434   struct dri2_drawable *priv = (struct dri2_drawable *)
435      GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
436
437   if (priv == NULL || !priv->have_fake_front)
438      return;
439
440   dri2_copy_drawable(priv, DRI2BufferFakeFrontLeft, DRI2BufferFrontLeft);
441}
442
443static void
444dri2_wait_gl(struct glx_context *gc)
445{
446   struct dri2_drawable *priv = (struct dri2_drawable *)
447      GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
448
449   if (priv == NULL || !priv->have_fake_front)
450      return;
451
452   dri2_copy_drawable(priv, DRI2BufferFrontLeft, DRI2BufferFakeFrontLeft);
453}
454
455static void
456dri2FlushFrontBuffer(__DRIdrawable *driDrawable, void *loaderPrivate)
457{
458   struct dri2_drawable *pdraw = loaderPrivate;
459   if (!pdraw)
460      return;
461
462   if (!pdraw->base.psc)
463      return;
464
465   struct glx_display *priv = __glXInitialize(pdraw->base.psc->dpy);
466   struct dri2_display *pdp = (struct dri2_display *)priv->dri2Display;
467   struct glx_context *gc = __glXGetCurrentContext();
468
469   /* Old servers don't send invalidate events */
470   if (!pdp->invalidateAvailable)
471       dri2InvalidateBuffers(priv->dpy, pdraw->base.xDrawable);
472
473   dri2_wait_gl(gc);
474}
475
476
477static void
478dri2DestroyScreen(struct glx_screen *base)
479{
480   struct dri2_screen *psc = (struct dri2_screen *) base;
481
482   /* Free the direct rendering per screen data */
483   (*psc->core->destroyScreen) (psc->driScreen);
484   driDestroyConfigs(psc->driver_configs);
485   close(psc->fd);
486   Xfree(psc);
487}
488
489/**
490 * Process list of buffer received from the server
491 *
492 * Processes the list of buffers received in a reply from the server to either
493 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
494 */
495static void
496process_buffers(struct dri2_drawable * pdraw, DRI2Buffer * buffers,
497                unsigned count)
498{
499   int i;
500
501   pdraw->bufferCount = count;
502   pdraw->have_fake_front = 0;
503   pdraw->have_back = 0;
504
505   /* This assumes the DRI2 buffer attachment tokens matches the
506    * __DRIbuffer tokens. */
507   for (i = 0; i < count; i++) {
508      pdraw->buffers[i].attachment = buffers[i].attachment;
509      pdraw->buffers[i].name = buffers[i].name;
510      pdraw->buffers[i].pitch = buffers[i].pitch;
511      pdraw->buffers[i].cpp = buffers[i].cpp;
512      pdraw->buffers[i].flags = buffers[i].flags;
513      if (pdraw->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)
514         pdraw->have_fake_front = 1;
515      if (pdraw->buffers[i].attachment == __DRI_BUFFER_BACK_LEFT)
516         pdraw->have_back = 1;
517   }
518
519}
520
521unsigned dri2GetSwapEventType(Display* dpy, XID drawable)
522{
523      struct glx_display *glx_dpy = __glXInitialize(dpy);
524      __GLXDRIdrawable *pdraw;
525      pdraw = dri2GetGlxDrawableFromXDrawableId(dpy, drawable);
526      if (!pdraw || !(pdraw->eventMask & GLX_BUFFER_SWAP_COMPLETE_INTEL_MASK))
527         return 0;
528      return glx_dpy->codes->first_event + GLX_BufferSwapComplete;
529}
530
531static int64_t
532dri2SwapBuffers(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
533		int64_t remainder)
534{
535    struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
536    struct glx_display *dpyPriv = __glXInitialize(priv->base.psc->dpy);
537    struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
538    struct dri2_display *pdp =
539	(struct dri2_display *)dpyPriv->dri2Display;
540    CARD64 ret = 0;
541
542    /* Check we have the right attachments */
543    if (!priv->have_back)
544	return ret;
545
546    /* Old servers can't handle swapbuffers */
547    if (!pdp->swapAvailable) {
548       dri2CopySubBuffer(pdraw, 0, 0, priv->width, priv->height);
549    } else {
550#ifdef X_DRI2SwapBuffers
551#ifdef __DRI2_FLUSH
552    if (psc->f) {
553       struct glx_context *gc = __glXGetCurrentContext();
554
555       if (gc) {
556	  (*psc->f->flush)(priv->driDrawable);
557       }
558    }
559#endif
560
561       DRI2SwapBuffers(psc->base.dpy, pdraw->xDrawable,
562		       target_msc, divisor, remainder, &ret);
563#endif
564    }
565
566    /* Old servers don't send invalidate events */
567    if (!pdp->invalidateAvailable)
568       dri2InvalidateBuffers(dpyPriv->dpy, pdraw->xDrawable);
569
570    return ret;
571}
572
573static __DRIbuffer *
574dri2GetBuffers(__DRIdrawable * driDrawable,
575               int *width, int *height,
576               unsigned int *attachments, int count,
577               int *out_count, void *loaderPrivate)
578{
579   struct dri2_drawable *pdraw = loaderPrivate;
580   DRI2Buffer *buffers;
581
582   buffers = DRI2GetBuffers(pdraw->base.psc->dpy, pdraw->base.xDrawable,
583                            width, height, attachments, count, out_count);
584   if (buffers == NULL)
585      return NULL;
586
587   pdraw->width = *width;
588   pdraw->height = *height;
589   process_buffers(pdraw, buffers, *out_count);
590
591   Xfree(buffers);
592
593   return pdraw->buffers;
594}
595
596static __DRIbuffer *
597dri2GetBuffersWithFormat(__DRIdrawable * driDrawable,
598                         int *width, int *height,
599                         unsigned int *attachments, int count,
600                         int *out_count, void *loaderPrivate)
601{
602   struct dri2_drawable *pdraw = loaderPrivate;
603   DRI2Buffer *buffers;
604
605   buffers = DRI2GetBuffersWithFormat(pdraw->base.psc->dpy,
606                                      pdraw->base.xDrawable,
607                                      width, height, attachments,
608                                      count, out_count);
609   if (buffers == NULL)
610      return NULL;
611
612   pdraw->width = *width;
613   pdraw->height = *height;
614   process_buffers(pdraw, buffers, *out_count);
615
616   Xfree(buffers);
617
618   return pdraw->buffers;
619}
620
621#ifdef X_DRI2SwapInterval
622
623static int
624dri2SetSwapInterval(__GLXDRIdrawable *pdraw, int interval)
625{
626   struct dri2_drawable *priv =  (struct dri2_drawable *) pdraw;
627   GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
628   struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
629
630   if (psc->config)
631      psc->config->configQueryi(psc->driScreen,
632				"vblank_mode", &vblank_mode);
633
634   switch (vblank_mode) {
635   case DRI_CONF_VBLANK_NEVER:
636      return GLX_BAD_VALUE;
637   case DRI_CONF_VBLANK_ALWAYS_SYNC:
638      if (interval <= 0)
639	 return GLX_BAD_VALUE;
640      break;
641   default:
642      break;
643   }
644
645   DRI2SwapInterval(priv->base.psc->dpy, priv->base.xDrawable, interval);
646   priv->swap_interval = interval;
647
648   return 0;
649}
650
651static int
652dri2GetSwapInterval(__GLXDRIdrawable *pdraw)
653{
654   struct dri2_drawable *priv =  (struct dri2_drawable *) pdraw;
655
656  return priv->swap_interval;
657}
658
659#endif /* X_DRI2SwapInterval */
660
661static const __DRIdri2LoaderExtension dri2LoaderExtension = {
662   {__DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION},
663   dri2GetBuffers,
664   dri2FlushFrontBuffer,
665   dri2GetBuffersWithFormat,
666};
667
668static const __DRIdri2LoaderExtension dri2LoaderExtension_old = {
669   {__DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION},
670   dri2GetBuffers,
671   dri2FlushFrontBuffer,
672   NULL,
673};
674
675#ifdef __DRI_USE_INVALIDATE
676static const __DRIuseInvalidateExtension dri2UseInvalidate = {
677   { __DRI_USE_INVALIDATE, __DRI_USE_INVALIDATE_VERSION }
678};
679#endif
680
681_X_HIDDEN void
682dri2InvalidateBuffers(Display *dpy, XID drawable)
683{
684   __GLXDRIdrawable *pdraw =
685      dri2GetGlxDrawableFromXDrawableId(dpy, drawable);
686   struct dri2_screen *psc;
687   struct dri2_drawable *pdp = (struct dri2_drawable *) pdraw;
688
689   if (!pdraw)
690      return;
691
692   psc = (struct dri2_screen *) pdraw->psc;
693
694#if __DRI2_FLUSH_VERSION >= 3
695   if (pdraw && psc->f && psc->f->base.version >= 3 && psc->f->invalidate)
696       psc->f->invalidate(pdp->driDrawable);
697#endif
698}
699
700static void
701dri2_bind_tex_image(Display * dpy,
702		    GLXDrawable drawable,
703		    int buffer, const int *attrib_list)
704{
705   struct glx_context *gc = __glXGetCurrentContext();
706   struct dri2_context *pcp = (struct dri2_context *) gc;
707   __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
708   struct glx_display *dpyPriv = __glXInitialize(dpy);
709   struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
710   struct dri2_display *pdp =
711      (struct dri2_display *) dpyPriv->dri2Display;
712   struct dri2_screen *psc;
713
714   if (pdraw != NULL) {
715      psc = (struct dri2_screen *) base->psc;
716
717#if __DRI2_FLUSH_VERSION >= 3
718      if (!pdp->invalidateAvailable && psc->f &&
719           psc->f->base.version >= 3 && psc->f->invalidate)
720	 psc->f->invalidate(pdraw->driDrawable);
721#endif
722
723      if (psc->texBuffer->base.version >= 2 &&
724	  psc->texBuffer->setTexBuffer2 != NULL) {
725	 (*psc->texBuffer->setTexBuffer2) (pcp->driContext,
726					   pdraw->base.textureTarget,
727					   pdraw->base.textureFormat,
728					   pdraw->driDrawable);
729      }
730      else {
731	 (*psc->texBuffer->setTexBuffer) (pcp->driContext,
732					  pdraw->base.textureTarget,
733					  pdraw->driDrawable);
734      }
735   }
736}
737
738static void
739dri2_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer)
740{
741#if __DRI_TEX_BUFFER_VERSION >= 3
742   struct glx_context *gc = __glXGetCurrentContext();
743   struct dri2_context *pcp = (struct dri2_context *) gc;
744   __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
745   struct glx_display *dpyPriv = __glXInitialize(dpy);
746   struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
747   struct dri2_display *pdp =
748      (struct dri2_display *) dpyPriv->dri2Display;
749   struct dri2_screen *psc;
750
751   if (pdraw != NULL) {
752      psc = (struct dri2_screen *) base->psc;
753
754      if (psc->texBuffer->base.version >= 3 &&
755          psc->texBuffer->releaseTexBuffer != NULL) {
756         (*psc->texBuffer->releaseTexBuffer) (pcp->driContext,
757                                           pdraw->base.textureTarget,
758                                           pdraw->driDrawable);
759      }
760   }
761#endif
762}
763
764static const struct glx_context_vtable dri2_context_vtable = {
765   dri2_destroy_context,
766   dri2_bind_context,
767   dri2_unbind_context,
768   dri2_wait_gl,
769   dri2_wait_x,
770   DRI_glXUseXFont,
771   dri2_bind_tex_image,
772   dri2_release_tex_image,
773   NULL, /* get_proc_address */
774};
775
776static void
777dri2BindExtensions(struct dri2_screen *psc, const __DRIextension **extensions)
778{
779   int i;
780
781   __glXEnableDirectExtension(&psc->base, "GLX_SGI_video_sync");
782   __glXEnableDirectExtension(&psc->base, "GLX_SGI_swap_control");
783   __glXEnableDirectExtension(&psc->base, "GLX_MESA_swap_control");
784   __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
785
786   /* FIXME: if DRI2 version supports it... */
787   __glXEnableDirectExtension(&psc->base, "INTEL_swap_event");
788
789   for (i = 0; extensions[i]; i++) {
790      if ((strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
791	 psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
792	 __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
793      }
794
795      if ((strcmp(extensions[i]->name, __DRI2_FLUSH) == 0)) {
796	 psc->f = (__DRI2flushExtension *) extensions[i];
797	 /* internal driver extension, no GL extension exposed */
798      }
799
800      if ((strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0))
801	 psc->config = (__DRI2configQueryExtension *) extensions[i];
802   }
803}
804
805static const struct glx_screen_vtable dri2_screen_vtable = {
806   dri2_create_context
807};
808
809static struct glx_screen *
810dri2CreateScreen(int screen, struct glx_display * priv)
811{
812   const __DRIconfig **driver_configs;
813   const __DRIextension **extensions;
814   const struct dri2_display *const pdp = (struct dri2_display *)
815      priv->dri2Display;
816   struct dri2_screen *psc;
817   __GLXDRIscreen *psp;
818   char *driverName, *deviceName;
819   drm_magic_t magic;
820   int i;
821
822   psc = Xmalloc(sizeof *psc);
823   if (psc == NULL)
824      return NULL;
825
826   memset(psc, 0, sizeof *psc);
827   psc->fd = -1;
828
829   if (!glx_screen_init(&psc->base, screen, priv)) {
830      Xfree(psc);
831      return NULL;
832   }
833
834   if (!DRI2Connect(priv->dpy, RootWindow(priv->dpy, screen),
835		    &driverName, &deviceName)) {
836      glx_screen_cleanup(&psc->base);
837      XFree(psc);
838      return NULL;
839   }
840
841   psc->driver = driOpenDriver(driverName);
842   if (psc->driver == NULL) {
843      ErrorMessageF("driver pointer missing\n");
844      goto handle_error;
845   }
846
847   extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
848   if (extensions == NULL) {
849      ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
850      goto handle_error;
851   }
852
853   for (i = 0; extensions[i]; i++) {
854      if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
855	 psc->core = (__DRIcoreExtension *) extensions[i];
856      if (strcmp(extensions[i]->name, __DRI_DRI2) == 0)
857	 psc->dri2 = (__DRIdri2Extension *) extensions[i];
858   }
859
860   if (psc->core == NULL || psc->dri2 == NULL) {
861      ErrorMessageF("core dri or dri2 extension not found\n");
862      goto handle_error;
863   }
864
865   psc->fd = open(deviceName, O_RDWR);
866   if (psc->fd < 0) {
867      ErrorMessageF("failed to open drm device: %s\n", strerror(errno));
868      goto handle_error;
869   }
870
871   if (drmGetMagic(psc->fd, &magic)) {
872      ErrorMessageF("failed to get magic\n");
873      goto handle_error;
874   }
875
876   if (!DRI2Authenticate(priv->dpy, RootWindow(priv->dpy, screen), magic)) {
877      ErrorMessageF("failed to authenticate magic %d\n", magic);
878      goto handle_error;
879   }
880
881
882   /* If the server does not support the protocol for
883    * DRI2GetBuffersWithFormat, don't supply that interface to the driver.
884    */
885   psc->driScreen =
886      psc->dri2->createNewScreen(screen, psc->fd,
887				 (const __DRIextension **)
888				 &pdp->loader_extensions[0],
889				 &driver_configs, psc);
890
891   if (psc->driScreen == NULL) {
892      ErrorMessageF("failed to create dri screen\n");
893      goto handle_error;
894   }
895
896   extensions = psc->core->getExtensions(psc->driScreen);
897   dri2BindExtensions(psc, extensions);
898
899   psc->base.configs =
900      driConvertConfigs(psc->core, psc->base.configs, driver_configs);
901   psc->base.visuals =
902      driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
903
904   psc->driver_configs = driver_configs;
905
906   psc->base.vtable = &dri2_screen_vtable;
907   psp = &psc->vtable;
908   psc->base.driScreen = psp;
909   psp->destroyScreen = dri2DestroyScreen;
910   psp->createDrawable = dri2CreateDrawable;
911   psp->swapBuffers = dri2SwapBuffers;
912   psp->getDrawableMSC = NULL;
913   psp->waitForMSC = NULL;
914   psp->waitForSBC = NULL;
915   psp->setSwapInterval = NULL;
916   psp->getSwapInterval = NULL;
917
918   if (pdp->driMinor >= 2) {
919#ifdef X_DRI2GetMSC
920      psp->getDrawableMSC = dri2DrawableGetMSC;
921#endif
922#ifdef X_DRI2WaitMSC
923      psp->waitForMSC = dri2WaitForMSC;
924      psp->waitForSBC = dri2WaitForSBC;
925#endif
926#ifdef X_DRI2SwapInterval
927      psp->setSwapInterval = dri2SetSwapInterval;
928      psp->getSwapInterval = dri2GetSwapInterval;
929#endif
930#if defined(X_DRI2GetMSC) && defined(X_DRI2WaitMSC) && defined(X_DRI2SwapInterval)
931      __glXEnableDirectExtension(&psc->base, "GLX_OML_sync_control");
932#endif
933   }
934
935   /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
936    * available.*/
937   psp->copySubBuffer = dri2CopySubBuffer;
938   __glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer");
939
940   Xfree(driverName);
941   Xfree(deviceName);
942
943   return &psc->base;
944
945handle_error:
946   if (psc->fd >= 0)
947      close(psc->fd);
948   if (psc->driver)
949      dlclose(psc->driver);
950   Xfree(driverName);
951   Xfree(deviceName);
952   glx_screen_cleanup(&psc->base);
953   XFree(psc);
954
955   return NULL;
956}
957
958/* Called from __glXFreeDisplayPrivate.
959 */
960static void
961dri2DestroyDisplay(__GLXDRIdisplay * dpy)
962{
963   struct dri2_display *pdp = (struct dri2_display *) dpy;
964
965   __glxHashDestroy(pdp->dri2Hash);
966   Xfree(dpy);
967}
968
969_X_HIDDEN __GLXDRIdrawable *
970dri2GetGlxDrawableFromXDrawableId(Display *dpy, XID id)
971{
972   struct glx_display *d = __glXInitialize(dpy);
973   struct dri2_display *pdp = (struct dri2_display *) d->dri2Display;
974   __GLXDRIdrawable *pdraw;
975
976   if (__glxHashLookup(pdp->dri2Hash, id, (void *) &pdraw) == 0)
977      return pdraw;
978
979   return NULL;
980}
981
982/*
983 * Allocate, initialize and return a __DRIdisplayPrivate object.
984 * This is called from __glXInitialize() when we are given a new
985 * display pointer.
986 */
987_X_HIDDEN __GLXDRIdisplay *
988dri2CreateDisplay(Display * dpy)
989{
990   struct dri2_display *pdp;
991   int eventBase, errorBase, i;
992
993   if (!DRI2QueryExtension(dpy, &eventBase, &errorBase))
994      return NULL;
995
996   pdp = Xmalloc(sizeof *pdp);
997   if (pdp == NULL)
998      return NULL;
999
1000   if (!DRI2QueryVersion(dpy, &pdp->driMajor, &pdp->driMinor)) {
1001      Xfree(pdp);
1002      return NULL;
1003   }
1004
1005   pdp->driPatch = 0;
1006   pdp->swapAvailable = (pdp->driMinor >= 2);
1007   pdp->invalidateAvailable = (pdp->driMinor >= 3);
1008
1009   pdp->base.destroyDisplay = dri2DestroyDisplay;
1010   pdp->base.createScreen = dri2CreateScreen;
1011
1012   i = 0;
1013   if (pdp->driMinor < 1)
1014      pdp->loader_extensions[i++] = &dri2LoaderExtension_old.base;
1015   else
1016      pdp->loader_extensions[i++] = &dri2LoaderExtension.base;
1017
1018   pdp->loader_extensions[i++] = &systemTimeExtension.base;
1019
1020#ifdef __DRI_USE_INVALIDATE
1021   pdp->loader_extensions[i++] = &dri2UseInvalidate.base;
1022#endif
1023   pdp->loader_extensions[i++] = NULL;
1024
1025   pdp->dri2Hash = __glxHashCreate();
1026   if (pdp->dri2Hash == NULL) {
1027      Xfree(pdp);
1028      return NULL;
1029   }
1030
1031   return &pdp->base;
1032}
1033
1034#endif /* GLX_DIRECT_RENDERING */
1035