Merge branch 'sprite-coord'
[mesa.git] / src / glx / dri2_glx.c
1 /*
2 * Copyright © 2008 Red Hat, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Soft-
6 * ware"), to deal in the Software without restriction, including without
7 * limitation the rights to use, copy, modify, merge, publish, distribute,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, provided that the above copyright
10 * notice(s) and this permission notice appear in all copies of the Soft-
11 * ware and that both the above copyright notice(s) and this permission
12 * notice appear in supporting documentation.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16 * ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
17 * RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN
18 * THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSE-
19 * QUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFOR-
22 * MANCE OF THIS SOFTWARE.
23 *
24 * Except as contained in this notice, the name of a copyright holder shall
25 * not be used in advertising or otherwise to promote the sale, use or
26 * other dealings in this Software without prior written authorization of
27 * the copyright holder.
28 *
29 * Authors:
30 * Kristian Høgsberg (krh@redhat.com)
31 */
32
33 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
34
35 #include <X11/Xlib.h>
36 #include <X11/extensions/Xfixes.h>
37 #include <X11/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
60 struct 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
78 struct 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
95 struct dri2_context
96 {
97 struct glx_context base;
98 __DRIcontext *driContext;
99 };
100
101 struct 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
113 static const struct glx_context_vtable dri2_context_vtable;
114
115 static void
116 dri2_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
134 static Bool
135 dri2_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
142 pdraw = (struct dri2_drawable *) driFetchDrawable(context, draw);
143 pread = (struct dri2_drawable *) driFetchDrawable(context, read);
144
145 if (pdraw == NULL || pread == NULL)
146 return GLXBadDrawable;
147
148 if ((*psc->core->bindContext) (pcp->driContext,
149 pdraw->driDrawable, pread->driDrawable))
150 return Success;
151
152 return GLXBadContext;
153 }
154
155 static void
156 dri2_unbind_context(struct glx_context *context, struct glx_context *new)
157 {
158 struct dri2_context *pcp = (struct dri2_context *) context;
159 struct dri2_screen *psc = (struct dri2_screen *) pcp->base.psc;
160
161 (*psc->core->unbindContext) (pcp->driContext);
162
163 if (context == new)
164 driReleaseDrawables(&pcp->base);
165 }
166
167 static struct glx_context *
168 dri2_create_context(struct glx_screen *base,
169 struct glx_config *config_base,
170 struct glx_context *shareList, int renderType)
171 {
172 struct dri2_context *pcp, *pcp_shared;
173 struct dri2_screen *psc = (struct dri2_screen *) base;
174 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
175 __DRIcontext *shared = NULL;
176
177 if (shareList) {
178 pcp_shared = (struct dri2_context *) shareList;
179 shared = pcp_shared->driContext;
180 }
181
182 pcp = Xmalloc(sizeof *pcp);
183 if (pcp == NULL)
184 return NULL;
185
186 memset(pcp, 0, sizeof *pcp);
187 if (!glx_context_init(&pcp->base, &psc->base, &config->base)) {
188 Xfree(pcp);
189 return NULL;
190 }
191
192 pcp->driContext =
193 (*psc->dri2->createNewContext) (psc->driScreen,
194 config->driConfig, shared, pcp);
195
196 if (pcp->driContext == NULL) {
197 Xfree(pcp);
198 return NULL;
199 }
200
201 pcp->base.vtable = &dri2_context_vtable;
202
203 return &pcp->base;
204 }
205
206 static void
207 dri2DestroyDrawable(__GLXDRIdrawable *base)
208 {
209 struct dri2_screen *psc = (struct dri2_screen *) base->psc;
210 struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
211 struct glx_display *dpyPriv = psc->base.display;
212 struct dri2_display *pdp = (struct dri2_display *)dpyPriv->dri2Display;
213
214 __glxHashDelete(pdp->dri2Hash, pdraw->base.xDrawable);
215 (*psc->core->destroyDrawable) (pdraw->driDrawable);
216
217 /* If it's a GLX 1.3 drawables, we can destroy the DRI2 drawable
218 * now, as the application explicitly asked to destroy the GLX
219 * drawable. Otherwise, for legacy drawables, we let the DRI2
220 * drawable linger on the server, since there's no good way of
221 * knowing when the application is done with it. The server will
222 * destroy the DRI2 drawable when it destroys the X drawable or the
223 * client exits anyway. */
224 if (pdraw->base.xDrawable != pdraw->base.drawable)
225 DRI2DestroyDrawable(psc->base.dpy, pdraw->base.xDrawable);
226
227 Xfree(pdraw);
228 }
229
230 static __GLXDRIdrawable *
231 dri2CreateDrawable(struct glx_screen *base, XID xDrawable,
232 GLXDrawable drawable, struct glx_config *config_base)
233 {
234 struct dri2_drawable *pdraw;
235 struct dri2_screen *psc = (struct dri2_screen *) base;
236 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
237 struct glx_display *dpyPriv;
238 struct dri2_display *pdp;
239 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
240
241 pdraw = Xmalloc(sizeof(*pdraw));
242 if (!pdraw)
243 return NULL;
244
245 memset(pdraw, 0, sizeof *pdraw);
246 pdraw->base.destroyDrawable = dri2DestroyDrawable;
247 pdraw->base.xDrawable = xDrawable;
248 pdraw->base.drawable = drawable;
249 pdraw->base.psc = &psc->base;
250 pdraw->bufferCount = 0;
251 pdraw->swap_interval = 1; /* default may be overridden below */
252 pdraw->have_back = 0;
253
254 if (psc->config)
255 psc->config->configQueryi(psc->driScreen,
256 "vblank_mode", &vblank_mode);
257
258 switch (vblank_mode) {
259 case DRI_CONF_VBLANK_NEVER:
260 case DRI_CONF_VBLANK_DEF_INTERVAL_0:
261 pdraw->swap_interval = 0;
262 break;
263 case DRI_CONF_VBLANK_DEF_INTERVAL_1:
264 case DRI_CONF_VBLANK_ALWAYS_SYNC:
265 default:
266 pdraw->swap_interval = 1;
267 break;
268 }
269
270 DRI2CreateDrawable(psc->base.dpy, xDrawable);
271
272 dpyPriv = __glXInitialize(psc->base.dpy);
273 pdp = (struct dri2_display *)dpyPriv->dri2Display;;
274 /* Create a new drawable */
275 pdraw->driDrawable =
276 (*psc->dri2->createNewDrawable) (psc->driScreen,
277 config->driConfig, pdraw);
278
279 if (!pdraw->driDrawable) {
280 DRI2DestroyDrawable(psc->base.dpy, xDrawable);
281 Xfree(pdraw);
282 return NULL;
283 }
284
285 if (__glxHashInsert(pdp->dri2Hash, xDrawable, pdraw)) {
286 (*psc->core->destroyDrawable) (pdraw->driDrawable);
287 DRI2DestroyDrawable(psc->base.dpy, xDrawable);
288 Xfree(pdraw);
289 return None;
290 }
291
292
293 #ifdef X_DRI2SwapInterval
294 /*
295 * Make sure server has the same swap interval we do for the new
296 * drawable.
297 */
298 if (pdp->swapAvailable)
299 DRI2SwapInterval(psc->base.dpy, xDrawable, pdraw->swap_interval);
300 #endif
301
302 return &pdraw->base;
303 }
304
305 #ifdef X_DRI2GetMSC
306
307 static int
308 dri2DrawableGetMSC(struct glx_screen *psc, __GLXDRIdrawable *pdraw,
309 int64_t *ust, int64_t *msc, int64_t *sbc)
310 {
311 CARD64 dri2_ust, dri2_msc, dri2_sbc;
312 int ret;
313
314 ret = DRI2GetMSC(psc->dpy, pdraw->xDrawable,
315 &dri2_ust, &dri2_msc, &dri2_sbc);
316 *ust = dri2_ust;
317 *msc = dri2_msc;
318 *sbc = dri2_sbc;
319
320 return ret;
321 }
322
323 #endif
324
325
326 #ifdef X_DRI2WaitMSC
327
328 static int
329 dri2WaitForMSC(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
330 int64_t remainder, int64_t *ust, int64_t *msc, int64_t *sbc)
331 {
332 CARD64 dri2_ust, dri2_msc, dri2_sbc;
333 int ret;
334
335 ret = DRI2WaitMSC(pdraw->psc->dpy, pdraw->xDrawable, target_msc, divisor,
336 remainder, &dri2_ust, &dri2_msc, &dri2_sbc);
337 *ust = dri2_ust;
338 *msc = dri2_msc;
339 *sbc = dri2_sbc;
340
341 return ret;
342 }
343
344 static int
345 dri2WaitForSBC(__GLXDRIdrawable *pdraw, int64_t target_sbc, int64_t *ust,
346 int64_t *msc, int64_t *sbc)
347 {
348 CARD64 dri2_ust, dri2_msc, dri2_sbc;
349 int ret;
350
351 ret = DRI2WaitSBC(pdraw->psc->dpy, pdraw->xDrawable,
352 target_sbc, &dri2_ust, &dri2_msc, &dri2_sbc);
353 *ust = dri2_ust;
354 *msc = dri2_msc;
355 *sbc = dri2_sbc;
356
357 return ret;
358 }
359
360 #endif /* X_DRI2WaitMSC */
361
362 static void
363 dri2CopySubBuffer(__GLXDRIdrawable *pdraw, int x, int y, int width, int height)
364 {
365 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
366 struct dri2_screen *psc = (struct dri2_screen *) pdraw->psc;
367 XRectangle xrect;
368 XserverRegion region;
369
370 /* Check we have the right attachments */
371 if (!priv->have_back)
372 return;
373
374 xrect.x = x;
375 xrect.y = priv->height - y - height;
376 xrect.width = width;
377 xrect.height = height;
378
379 #ifdef __DRI2_FLUSH
380 if (psc->f)
381 (*psc->f->flush) (priv->driDrawable);
382 #endif
383
384 region = XFixesCreateRegion(psc->base.dpy, &xrect, 1);
385 DRI2CopyRegion(psc->base.dpy, pdraw->xDrawable, region,
386 DRI2BufferFrontLeft, DRI2BufferBackLeft);
387
388 /* Refresh the fake front (if present) after we just damaged the real
389 * front.
390 */
391 if (priv->have_fake_front)
392 DRI2CopyRegion(psc->base.dpy, pdraw->xDrawable, region,
393 DRI2BufferFakeFrontLeft, DRI2BufferFrontLeft);
394
395 XFixesDestroyRegion(psc->base.dpy, region);
396 }
397
398 static void
399 dri2_copy_drawable(struct dri2_drawable *priv, int dest, int src)
400 {
401 XRectangle xrect;
402 XserverRegion region;
403 struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
404
405 xrect.x = 0;
406 xrect.y = 0;
407 xrect.width = priv->width;
408 xrect.height = priv->height;
409
410 #ifdef __DRI2_FLUSH
411 if (psc->f)
412 (*psc->f->flush) (priv->driDrawable);
413 #endif
414
415 region = XFixesCreateRegion(psc->base.dpy, &xrect, 1);
416 DRI2CopyRegion(psc->base.dpy, priv->base.xDrawable, region, dest, src);
417 XFixesDestroyRegion(psc->base.dpy, region);
418
419 }
420
421 static void
422 dri2_wait_x(struct glx_context *gc)
423 {
424 struct dri2_drawable *priv = (struct dri2_drawable *)
425 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
426
427 if (priv == NULL || !priv->have_fake_front)
428 return;
429
430 dri2_copy_drawable(priv, DRI2BufferFakeFrontLeft, DRI2BufferFrontLeft);
431 }
432
433 static void
434 dri2_wait_gl(struct glx_context *gc)
435 {
436 struct dri2_drawable *priv = (struct dri2_drawable *)
437 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
438
439 if (priv == NULL || !priv->have_fake_front)
440 return;
441
442 dri2_copy_drawable(priv, DRI2BufferFrontLeft, DRI2BufferFakeFrontLeft);
443 }
444
445 static void
446 dri2FlushFrontBuffer(__DRIdrawable *driDrawable, void *loaderPrivate)
447 {
448 struct dri2_drawable *pdraw = loaderPrivate;
449 struct glx_display *priv = __glXInitialize(pdraw->base.psc->dpy);
450 struct dri2_display *pdp = (struct dri2_display *)priv->dri2Display;
451 struct glx_context *gc = __glXGetCurrentContext();
452
453 /* Old servers don't send invalidate events */
454 if (!pdp->invalidateAvailable)
455 dri2InvalidateBuffers(priv->dpy, pdraw->base.xDrawable);
456
457 dri2_wait_gl(gc);
458 }
459
460
461 static void
462 dri2DestroyScreen(struct glx_screen *base)
463 {
464 struct dri2_screen *psc = (struct dri2_screen *) base;
465
466 /* Free the direct rendering per screen data */
467 (*psc->core->destroyScreen) (psc->driScreen);
468 driDestroyConfigs(psc->driver_configs);
469 close(psc->fd);
470 Xfree(psc);
471 }
472
473 /**
474 * Process list of buffer received from the server
475 *
476 * Processes the list of buffers received in a reply from the server to either
477 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
478 */
479 static void
480 process_buffers(struct dri2_drawable * pdraw, DRI2Buffer * buffers,
481 unsigned count)
482 {
483 int i;
484
485 pdraw->bufferCount = count;
486 pdraw->have_fake_front = 0;
487 pdraw->have_back = 0;
488
489 /* This assumes the DRI2 buffer attachment tokens matches the
490 * __DRIbuffer tokens. */
491 for (i = 0; i < count; i++) {
492 pdraw->buffers[i].attachment = buffers[i].attachment;
493 pdraw->buffers[i].name = buffers[i].name;
494 pdraw->buffers[i].pitch = buffers[i].pitch;
495 pdraw->buffers[i].cpp = buffers[i].cpp;
496 pdraw->buffers[i].flags = buffers[i].flags;
497 if (pdraw->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)
498 pdraw->have_fake_front = 1;
499 if (pdraw->buffers[i].attachment == __DRI_BUFFER_BACK_LEFT)
500 pdraw->have_back = 1;
501 }
502
503 }
504
505 static int64_t
506 dri2SwapBuffers(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
507 int64_t remainder)
508 {
509 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
510 struct glx_display *dpyPriv = __glXInitialize(priv->base.psc->dpy);
511 struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
512 struct dri2_display *pdp =
513 (struct dri2_display *)dpyPriv->dri2Display;
514 CARD64 ret = 0;
515
516 #ifdef __DRI2_FLUSH
517 if (psc->f)
518 (*psc->f->flush)(priv->driDrawable);
519 #endif
520
521 /* Old servers don't send invalidate events */
522 if (!pdp->invalidateAvailable)
523 dri2InvalidateBuffers(dpyPriv->dpy, pdraw->xDrawable);
524
525 /* Old servers can't handle swapbuffers */
526 if (!pdp->swapAvailable) {
527 dri2CopySubBuffer(pdraw, 0, 0, priv->width, priv->height);
528 return 0;
529 }
530
531 #ifdef X_DRI2SwapBuffers
532 DRI2SwapBuffers(psc->base.dpy, pdraw->xDrawable, target_msc, divisor,
533 remainder, &ret);
534 #endif
535
536 return ret;
537 }
538
539 static __DRIbuffer *
540 dri2GetBuffers(__DRIdrawable * driDrawable,
541 int *width, int *height,
542 unsigned int *attachments, int count,
543 int *out_count, void *loaderPrivate)
544 {
545 struct dri2_drawable *pdraw = loaderPrivate;
546 DRI2Buffer *buffers;
547
548 buffers = DRI2GetBuffers(pdraw->base.psc->dpy, pdraw->base.xDrawable,
549 width, height, attachments, count, out_count);
550 if (buffers == NULL)
551 return NULL;
552
553 pdraw->width = *width;
554 pdraw->height = *height;
555 process_buffers(pdraw, buffers, *out_count);
556
557 Xfree(buffers);
558
559 return pdraw->buffers;
560 }
561
562 static __DRIbuffer *
563 dri2GetBuffersWithFormat(__DRIdrawable * driDrawable,
564 int *width, int *height,
565 unsigned int *attachments, int count,
566 int *out_count, void *loaderPrivate)
567 {
568 struct dri2_drawable *pdraw = loaderPrivate;
569 DRI2Buffer *buffers;
570
571 buffers = DRI2GetBuffersWithFormat(pdraw->base.psc->dpy,
572 pdraw->base.xDrawable,
573 width, height, attachments,
574 count, out_count);
575 if (buffers == NULL)
576 return NULL;
577
578 pdraw->width = *width;
579 pdraw->height = *height;
580 process_buffers(pdraw, buffers, *out_count);
581
582 Xfree(buffers);
583
584 return pdraw->buffers;
585 }
586
587 #ifdef X_DRI2SwapInterval
588
589 static int
590 dri2SetSwapInterval(__GLXDRIdrawable *pdraw, int interval)
591 {
592 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
593 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
594 struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
595
596 if (psc->config)
597 psc->config->configQueryi(psc->driScreen,
598 "vblank_mode", &vblank_mode);
599
600 switch (vblank_mode) {
601 case DRI_CONF_VBLANK_NEVER:
602 return GLX_BAD_VALUE;
603 case DRI_CONF_VBLANK_ALWAYS_SYNC:
604 if (interval <= 0)
605 return GLX_BAD_VALUE;
606 break;
607 default:
608 break;
609 }
610
611 DRI2SwapInterval(priv->base.psc->dpy, priv->base.xDrawable, interval);
612 priv->swap_interval = interval;
613
614 return 0;
615 }
616
617 static int
618 dri2GetSwapInterval(__GLXDRIdrawable *pdraw)
619 {
620 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
621
622 return priv->swap_interval;
623 }
624
625 #endif /* X_DRI2SwapInterval */
626
627 static const __DRIdri2LoaderExtension dri2LoaderExtension = {
628 {__DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION},
629 dri2GetBuffers,
630 dri2FlushFrontBuffer,
631 dri2GetBuffersWithFormat,
632 };
633
634 static const __DRIdri2LoaderExtension dri2LoaderExtension_old = {
635 {__DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION},
636 dri2GetBuffers,
637 dri2FlushFrontBuffer,
638 NULL,
639 };
640
641 #ifdef __DRI_USE_INVALIDATE
642 static const __DRIuseInvalidateExtension dri2UseInvalidate = {
643 { __DRI_USE_INVALIDATE, __DRI_USE_INVALIDATE_VERSION }
644 };
645 #endif
646
647 _X_HIDDEN void
648 dri2InvalidateBuffers(Display *dpy, XID drawable)
649 {
650 __GLXDRIdrawable *pdraw =
651 dri2GetGlxDrawableFromXDrawableId(dpy, drawable);
652 struct dri2_screen *psc = (struct dri2_screen *) pdraw->psc;
653 struct dri2_drawable *pdp = (struct dri2_drawable *) pdraw;
654
655 #if __DRI2_FLUSH_VERSION >= 3
656 if (pdraw && psc->f)
657 psc->f->invalidate(pdp->driDrawable);
658 #endif
659 }
660
661 static void
662 dri2_bind_tex_image(Display * dpy,
663 GLXDrawable drawable,
664 int buffer, const int *attrib_list)
665 {
666 struct glx_context *gc = __glXGetCurrentContext();
667 struct dri2_context *pcp = (struct dri2_context *) gc;
668 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
669 struct glx_display *dpyPriv = __glXInitialize(dpy);
670 struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
671 struct dri2_display *pdp =
672 (struct dri2_display *) dpyPriv->dri2Display;
673 struct dri2_screen *psc;
674
675 if (pdraw != NULL) {
676 psc = (struct dri2_screen *) base->psc;
677
678 #if __DRI2_FLUSH_VERSION >= 3
679 if (!pdp->invalidateAvailable && psc->f)
680 psc->f->invalidate(pdraw->driDrawable);
681 #endif
682
683 if (psc->texBuffer->base.version >= 2 &&
684 psc->texBuffer->setTexBuffer2 != NULL) {
685 (*psc->texBuffer->setTexBuffer2) (pcp->driContext,
686 pdraw->base.textureTarget,
687 pdraw->base.textureFormat,
688 pdraw->driDrawable);
689 }
690 else {
691 (*psc->texBuffer->setTexBuffer) (pcp->driContext,
692 pdraw->base.textureTarget,
693 pdraw->driDrawable);
694 }
695 }
696 }
697
698 static void
699 dri2_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer)
700 {
701 }
702
703 static const struct glx_context_vtable dri2_context_vtable = {
704 dri2_destroy_context,
705 dri2_bind_context,
706 dri2_unbind_context,
707 dri2_wait_gl,
708 dri2_wait_x,
709 DRI_glXUseXFont,
710 dri2_bind_tex_image,
711 dri2_release_tex_image,
712 };
713
714 static void
715 dri2BindExtensions(struct dri2_screen *psc, const __DRIextension **extensions)
716 {
717 int i;
718
719 __glXEnableDirectExtension(&psc->base, "GLX_SGI_video_sync");
720 __glXEnableDirectExtension(&psc->base, "GLX_SGI_swap_control");
721 __glXEnableDirectExtension(&psc->base, "GLX_MESA_swap_control");
722 __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
723
724 /* FIXME: if DRI2 version supports it... */
725 __glXEnableDirectExtension(&psc->base, "INTEL_swap_event");
726
727 for (i = 0; extensions[i]; i++) {
728 if ((strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
729 psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
730 __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
731 }
732
733 if ((strcmp(extensions[i]->name, __DRI2_FLUSH) == 0)) {
734 psc->f = (__DRI2flushExtension *) extensions[i];
735 /* internal driver extension, no GL extension exposed */
736 }
737
738 if ((strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0))
739 psc->config = (__DRI2configQueryExtension *) extensions[i];
740 }
741 }
742
743 static const struct glx_screen_vtable dri2_screen_vtable = {
744 dri2_create_context
745 };
746
747 static struct glx_screen *
748 dri2CreateScreen(int screen, struct glx_display * priv)
749 {
750 const __DRIconfig **driver_configs;
751 const __DRIextension **extensions;
752 const struct dri2_display *const pdp = (struct dri2_display *)
753 priv->dri2Display;
754 struct dri2_screen *psc;
755 __GLXDRIscreen *psp;
756 char *driverName, *deviceName;
757 drm_magic_t magic;
758 int i;
759
760 psc = Xmalloc(sizeof *psc);
761 if (psc == NULL)
762 return NULL;
763
764 memset(psc, 0, sizeof *psc);
765 if (!glx_screen_init(&psc->base, screen, priv))
766 return NULL;
767
768 if (!DRI2Connect(priv->dpy, RootWindow(priv->dpy, screen),
769 &driverName, &deviceName)) {
770 XFree(psc);
771 return NULL;
772 }
773
774 psc->driver = driOpenDriver(driverName);
775 if (psc->driver == NULL) {
776 ErrorMessageF("driver pointer missing\n");
777 goto handle_error;
778 }
779
780 extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
781 if (extensions == NULL) {
782 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
783 goto handle_error;
784 }
785
786 for (i = 0; extensions[i]; i++) {
787 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
788 psc->core = (__DRIcoreExtension *) extensions[i];
789 if (strcmp(extensions[i]->name, __DRI_DRI2) == 0)
790 psc->dri2 = (__DRIdri2Extension *) extensions[i];
791 }
792
793 if (psc->core == NULL || psc->dri2 == NULL) {
794 ErrorMessageF("core dri or dri2 extension not found\n");
795 goto handle_error;
796 }
797
798 psc->fd = open(deviceName, O_RDWR);
799 if (psc->fd < 0) {
800 ErrorMessageF("failed to open drm device: %s\n", strerror(errno));
801 goto handle_error;
802 }
803
804 if (drmGetMagic(psc->fd, &magic)) {
805 ErrorMessageF("failed to get magic\n");
806 goto handle_error;
807 }
808
809 if (!DRI2Authenticate(priv->dpy, RootWindow(priv->dpy, screen), magic)) {
810 ErrorMessageF("failed to authenticate magic %d\n", magic);
811 goto handle_error;
812 }
813
814
815 /* If the server does not support the protocol for
816 * DRI2GetBuffersWithFormat, don't supply that interface to the driver.
817 */
818 psc->driScreen =
819 psc->dri2->createNewScreen(screen, psc->fd,
820 (const __DRIextension **)
821 &pdp->loader_extensions[0],
822 &driver_configs, psc);
823
824 if (psc->driScreen == NULL) {
825 ErrorMessageF("failed to create dri screen\n");
826 goto handle_error;
827 }
828
829 extensions = psc->core->getExtensions(psc->driScreen);
830 dri2BindExtensions(psc, extensions);
831
832 psc->base.configs =
833 driConvertConfigs(psc->core, psc->base.configs, driver_configs);
834 psc->base.visuals =
835 driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
836
837 psc->driver_configs = driver_configs;
838
839 psc->base.vtable = &dri2_screen_vtable;
840 psp = &psc->vtable;
841 psc->base.driScreen = psp;
842 psp->destroyScreen = dri2DestroyScreen;
843 psp->createDrawable = dri2CreateDrawable;
844 psp->swapBuffers = dri2SwapBuffers;
845 psp->getDrawableMSC = NULL;
846 psp->waitForMSC = NULL;
847 psp->waitForSBC = NULL;
848 psp->setSwapInterval = NULL;
849 psp->getSwapInterval = NULL;
850
851 if (pdp->driMinor >= 2) {
852 #ifdef X_DRI2GetMSC
853 psp->getDrawableMSC = dri2DrawableGetMSC;
854 #endif
855 #ifdef X_DRI2WaitMSC
856 psp->waitForMSC = dri2WaitForMSC;
857 psp->waitForSBC = dri2WaitForSBC;
858 #endif
859 #ifdef X_DRI2SwapInterval
860 psp->setSwapInterval = dri2SetSwapInterval;
861 psp->getSwapInterval = dri2GetSwapInterval;
862 #endif
863 #if defined(X_DRI2GetMSC) && defined(X_DRI2WaitMSC) && defined(X_DRI2SwapInterval)
864 __glXEnableDirectExtension(&psc->base, "GLX_OML_sync_control");
865 #endif
866 }
867
868 /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
869 * available.*/
870 psp->copySubBuffer = dri2CopySubBuffer;
871 __glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer");
872
873 Xfree(driverName);
874 Xfree(deviceName);
875
876 return &psc->base;
877
878 handle_error:
879 Xfree(driverName);
880 Xfree(deviceName);
881 XFree(psc);
882
883 /* FIXME: clean up here */
884
885 return NULL;
886 }
887
888 /* Called from __glXFreeDisplayPrivate.
889 */
890 static void
891 dri2DestroyDisplay(__GLXDRIdisplay * dpy)
892 {
893 Xfree(dpy);
894 }
895
896 _X_HIDDEN __GLXDRIdrawable *
897 dri2GetGlxDrawableFromXDrawableId(Display *dpy, XID id)
898 {
899 struct glx_display *d = __glXInitialize(dpy);
900 struct dri2_display *pdp = (struct dri2_display *) d->dri2Display;
901 __GLXDRIdrawable *pdraw;
902
903 if (__glxHashLookup(pdp->dri2Hash, id, (void *) &pdraw) == 0)
904 return pdraw;
905
906 return NULL;
907 }
908
909 /*
910 * Allocate, initialize and return a __DRIdisplayPrivate object.
911 * This is called from __glXInitialize() when we are given a new
912 * display pointer.
913 */
914 _X_HIDDEN __GLXDRIdisplay *
915 dri2CreateDisplay(Display * dpy)
916 {
917 struct dri2_display *pdp;
918 int eventBase, errorBase, i;
919
920 if (!DRI2QueryExtension(dpy, &eventBase, &errorBase))
921 return NULL;
922
923 pdp = Xmalloc(sizeof *pdp);
924 if (pdp == NULL)
925 return NULL;
926
927 if (!DRI2QueryVersion(dpy, &pdp->driMajor, &pdp->driMinor)) {
928 Xfree(pdp);
929 return NULL;
930 }
931
932 pdp->driPatch = 0;
933 pdp->swapAvailable = (pdp->driMinor >= 2);
934 pdp->invalidateAvailable = (pdp->driMinor >= 3);
935
936 pdp->base.destroyDisplay = dri2DestroyDisplay;
937 pdp->base.createScreen = dri2CreateScreen;
938
939 i = 0;
940 if (pdp->driMinor < 1)
941 pdp->loader_extensions[i++] = &dri2LoaderExtension_old.base;
942 else
943 pdp->loader_extensions[i++] = &dri2LoaderExtension.base;
944
945 pdp->loader_extensions[i++] = &systemTimeExtension.base;
946
947 #ifdef __DRI_USE_INVALIDATE
948 pdp->loader_extensions[i++] = &dri2UseInvalidate.base;
949 #endif
950 pdp->loader_extensions[i++] = NULL;
951
952 pdp->dri2Hash = __glxHashCreate();
953 if (pdp->dri2Hash == NULL) {
954 Xfree(pdp);
955 return NULL;
956 }
957
958 return &pdp->base;
959 }
960
961 #endif /* GLX_DIRECT_RENDERING */