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