glx: decouple dri2.c and GLX, fixing Gallium EGL and d3d1x build
[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 unsigned dri2GetSwapEventType(Display* dpy, XID drawable)
506 {
507 struct glx_display *glx_dpy = __glXInitialize(dpy);
508 __GLXDRIdrawable *pdraw;
509 pdraw = dri2GetGlxDrawableFromXDrawableId(dpy, drawable);
510 if (!pdraw || !(pdraw->eventMask & GLX_BUFFER_SWAP_COMPLETE_INTEL_MASK))
511 return 0;
512 return glx_dpy->codes->first_event + GLX_BufferSwapComplete;
513 }
514
515 static int64_t
516 dri2SwapBuffers(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
517 int64_t remainder)
518 {
519 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
520 struct glx_display *dpyPriv = __glXInitialize(priv->base.psc->dpy);
521 struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
522 struct dri2_display *pdp =
523 (struct dri2_display *)dpyPriv->dri2Display;
524 CARD64 ret = 0;
525
526 #ifdef __DRI2_FLUSH
527 if (psc->f)
528 (*psc->f->flush)(priv->driDrawable);
529 #endif
530
531 /* Old servers don't send invalidate events */
532 if (!pdp->invalidateAvailable)
533 dri2InvalidateBuffers(dpyPriv->dpy, pdraw->xDrawable);
534
535 /* Old servers can't handle swapbuffers */
536 if (!pdp->swapAvailable) {
537 dri2CopySubBuffer(pdraw, 0, 0, priv->width, priv->height);
538 return 0;
539 }
540
541 #ifdef X_DRI2SwapBuffers
542 DRI2SwapBuffers(psc->base.dpy, pdraw->xDrawable, target_msc, divisor,
543 remainder, &ret);
544 #endif
545
546 return ret;
547 }
548
549 static __DRIbuffer *
550 dri2GetBuffers(__DRIdrawable * driDrawable,
551 int *width, int *height,
552 unsigned int *attachments, int count,
553 int *out_count, void *loaderPrivate)
554 {
555 struct dri2_drawable *pdraw = loaderPrivate;
556 DRI2Buffer *buffers;
557
558 buffers = DRI2GetBuffers(pdraw->base.psc->dpy, pdraw->base.xDrawable,
559 width, height, attachments, count, out_count);
560 if (buffers == NULL)
561 return NULL;
562
563 pdraw->width = *width;
564 pdraw->height = *height;
565 process_buffers(pdraw, buffers, *out_count);
566
567 Xfree(buffers);
568
569 return pdraw->buffers;
570 }
571
572 static __DRIbuffer *
573 dri2GetBuffersWithFormat(__DRIdrawable * driDrawable,
574 int *width, int *height,
575 unsigned int *attachments, int count,
576 int *out_count, void *loaderPrivate)
577 {
578 struct dri2_drawable *pdraw = loaderPrivate;
579 DRI2Buffer *buffers;
580
581 buffers = DRI2GetBuffersWithFormat(pdraw->base.psc->dpy,
582 pdraw->base.xDrawable,
583 width, height, attachments,
584 count, out_count);
585 if (buffers == NULL)
586 return NULL;
587
588 pdraw->width = *width;
589 pdraw->height = *height;
590 process_buffers(pdraw, buffers, *out_count);
591
592 Xfree(buffers);
593
594 return pdraw->buffers;
595 }
596
597 #ifdef X_DRI2SwapInterval
598
599 static int
600 dri2SetSwapInterval(__GLXDRIdrawable *pdraw, int interval)
601 {
602 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
603 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
604 struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
605
606 if (psc->config)
607 psc->config->configQueryi(psc->driScreen,
608 "vblank_mode", &vblank_mode);
609
610 switch (vblank_mode) {
611 case DRI_CONF_VBLANK_NEVER:
612 return GLX_BAD_VALUE;
613 case DRI_CONF_VBLANK_ALWAYS_SYNC:
614 if (interval <= 0)
615 return GLX_BAD_VALUE;
616 break;
617 default:
618 break;
619 }
620
621 DRI2SwapInterval(priv->base.psc->dpy, priv->base.xDrawable, interval);
622 priv->swap_interval = interval;
623
624 return 0;
625 }
626
627 static int
628 dri2GetSwapInterval(__GLXDRIdrawable *pdraw)
629 {
630 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
631
632 return priv->swap_interval;
633 }
634
635 #endif /* X_DRI2SwapInterval */
636
637 static const __DRIdri2LoaderExtension dri2LoaderExtension = {
638 {__DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION},
639 dri2GetBuffers,
640 dri2FlushFrontBuffer,
641 dri2GetBuffersWithFormat,
642 };
643
644 static const __DRIdri2LoaderExtension dri2LoaderExtension_old = {
645 {__DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION},
646 dri2GetBuffers,
647 dri2FlushFrontBuffer,
648 NULL,
649 };
650
651 #ifdef __DRI_USE_INVALIDATE
652 static const __DRIuseInvalidateExtension dri2UseInvalidate = {
653 { __DRI_USE_INVALIDATE, __DRI_USE_INVALIDATE_VERSION }
654 };
655 #endif
656
657 _X_HIDDEN void
658 dri2InvalidateBuffers(Display *dpy, XID drawable)
659 {
660 __GLXDRIdrawable *pdraw =
661 dri2GetGlxDrawableFromXDrawableId(dpy, drawable);
662 struct dri2_screen *psc = (struct dri2_screen *) pdraw->psc;
663 struct dri2_drawable *pdp = (struct dri2_drawable *) pdraw;
664
665 #if __DRI2_FLUSH_VERSION >= 3
666 if (pdraw && psc->f)
667 psc->f->invalidate(pdp->driDrawable);
668 #endif
669 }
670
671 static void
672 dri2_bind_tex_image(Display * dpy,
673 GLXDrawable drawable,
674 int buffer, const int *attrib_list)
675 {
676 struct glx_context *gc = __glXGetCurrentContext();
677 struct dri2_context *pcp = (struct dri2_context *) gc;
678 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
679 struct glx_display *dpyPriv = __glXInitialize(dpy);
680 struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
681 struct dri2_display *pdp =
682 (struct dri2_display *) dpyPriv->dri2Display;
683 struct dri2_screen *psc;
684
685 if (pdraw != NULL) {
686 psc = (struct dri2_screen *) base->psc;
687
688 #if __DRI2_FLUSH_VERSION >= 3
689 if (!pdp->invalidateAvailable && psc->f)
690 psc->f->invalidate(pdraw->driDrawable);
691 #endif
692
693 if (psc->texBuffer->base.version >= 2 &&
694 psc->texBuffer->setTexBuffer2 != NULL) {
695 (*psc->texBuffer->setTexBuffer2) (pcp->driContext,
696 pdraw->base.textureTarget,
697 pdraw->base.textureFormat,
698 pdraw->driDrawable);
699 }
700 else {
701 (*psc->texBuffer->setTexBuffer) (pcp->driContext,
702 pdraw->base.textureTarget,
703 pdraw->driDrawable);
704 }
705 }
706 }
707
708 static void
709 dri2_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer)
710 {
711 }
712
713 static const struct glx_context_vtable dri2_context_vtable = {
714 dri2_destroy_context,
715 dri2_bind_context,
716 dri2_unbind_context,
717 dri2_wait_gl,
718 dri2_wait_x,
719 DRI_glXUseXFont,
720 dri2_bind_tex_image,
721 dri2_release_tex_image,
722 };
723
724 static void
725 dri2BindExtensions(struct dri2_screen *psc, const __DRIextension **extensions)
726 {
727 int i;
728
729 __glXEnableDirectExtension(&psc->base, "GLX_SGI_video_sync");
730 __glXEnableDirectExtension(&psc->base, "GLX_SGI_swap_control");
731 __glXEnableDirectExtension(&psc->base, "GLX_MESA_swap_control");
732 __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
733
734 /* FIXME: if DRI2 version supports it... */
735 __glXEnableDirectExtension(&psc->base, "INTEL_swap_event");
736
737 for (i = 0; extensions[i]; i++) {
738 if ((strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
739 psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
740 __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
741 }
742
743 if ((strcmp(extensions[i]->name, __DRI2_FLUSH) == 0)) {
744 psc->f = (__DRI2flushExtension *) extensions[i];
745 /* internal driver extension, no GL extension exposed */
746 }
747
748 if ((strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0))
749 psc->config = (__DRI2configQueryExtension *) extensions[i];
750 }
751 }
752
753 static const struct glx_screen_vtable dri2_screen_vtable = {
754 dri2_create_context
755 };
756
757 static struct glx_screen *
758 dri2CreateScreen(int screen, struct glx_display * priv)
759 {
760 const __DRIconfig **driver_configs;
761 const __DRIextension **extensions;
762 const struct dri2_display *const pdp = (struct dri2_display *)
763 priv->dri2Display;
764 struct dri2_screen *psc;
765 __GLXDRIscreen *psp;
766 char *driverName, *deviceName;
767 drm_magic_t magic;
768 int i;
769
770 psc = Xmalloc(sizeof *psc);
771 if (psc == NULL)
772 return NULL;
773
774 memset(psc, 0, sizeof *psc);
775 if (!glx_screen_init(&psc->base, screen, priv))
776 return NULL;
777
778 if (!DRI2Connect(priv->dpy, RootWindow(priv->dpy, screen),
779 &driverName, &deviceName)) {
780 XFree(psc);
781 return NULL;
782 }
783
784 psc->driver = driOpenDriver(driverName);
785 if (psc->driver == NULL) {
786 ErrorMessageF("driver pointer missing\n");
787 goto handle_error;
788 }
789
790 extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
791 if (extensions == NULL) {
792 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
793 goto handle_error;
794 }
795
796 for (i = 0; extensions[i]; i++) {
797 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
798 psc->core = (__DRIcoreExtension *) extensions[i];
799 if (strcmp(extensions[i]->name, __DRI_DRI2) == 0)
800 psc->dri2 = (__DRIdri2Extension *) extensions[i];
801 }
802
803 if (psc->core == NULL || psc->dri2 == NULL) {
804 ErrorMessageF("core dri or dri2 extension not found\n");
805 goto handle_error;
806 }
807
808 psc->fd = open(deviceName, O_RDWR);
809 if (psc->fd < 0) {
810 ErrorMessageF("failed to open drm device: %s\n", strerror(errno));
811 goto handle_error;
812 }
813
814 if (drmGetMagic(psc->fd, &magic)) {
815 ErrorMessageF("failed to get magic\n");
816 goto handle_error;
817 }
818
819 if (!DRI2Authenticate(priv->dpy, RootWindow(priv->dpy, screen), magic)) {
820 ErrorMessageF("failed to authenticate magic %d\n", magic);
821 goto handle_error;
822 }
823
824
825 /* If the server does not support the protocol for
826 * DRI2GetBuffersWithFormat, don't supply that interface to the driver.
827 */
828 psc->driScreen =
829 psc->dri2->createNewScreen(screen, psc->fd,
830 (const __DRIextension **)
831 &pdp->loader_extensions[0],
832 &driver_configs, psc);
833
834 if (psc->driScreen == NULL) {
835 ErrorMessageF("failed to create dri screen\n");
836 goto handle_error;
837 }
838
839 extensions = psc->core->getExtensions(psc->driScreen);
840 dri2BindExtensions(psc, extensions);
841
842 psc->base.configs =
843 driConvertConfigs(psc->core, psc->base.configs, driver_configs);
844 psc->base.visuals =
845 driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
846
847 psc->driver_configs = driver_configs;
848
849 psc->base.vtable = &dri2_screen_vtable;
850 psp = &psc->vtable;
851 psc->base.driScreen = psp;
852 psp->destroyScreen = dri2DestroyScreen;
853 psp->createDrawable = dri2CreateDrawable;
854 psp->swapBuffers = dri2SwapBuffers;
855 psp->getDrawableMSC = NULL;
856 psp->waitForMSC = NULL;
857 psp->waitForSBC = NULL;
858 psp->setSwapInterval = NULL;
859 psp->getSwapInterval = NULL;
860
861 if (pdp->driMinor >= 2) {
862 #ifdef X_DRI2GetMSC
863 psp->getDrawableMSC = dri2DrawableGetMSC;
864 #endif
865 #ifdef X_DRI2WaitMSC
866 psp->waitForMSC = dri2WaitForMSC;
867 psp->waitForSBC = dri2WaitForSBC;
868 #endif
869 #ifdef X_DRI2SwapInterval
870 psp->setSwapInterval = dri2SetSwapInterval;
871 psp->getSwapInterval = dri2GetSwapInterval;
872 #endif
873 #if defined(X_DRI2GetMSC) && defined(X_DRI2WaitMSC) && defined(X_DRI2SwapInterval)
874 __glXEnableDirectExtension(&psc->base, "GLX_OML_sync_control");
875 #endif
876 }
877
878 /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
879 * available.*/
880 psp->copySubBuffer = dri2CopySubBuffer;
881 __glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer");
882
883 Xfree(driverName);
884 Xfree(deviceName);
885
886 return &psc->base;
887
888 handle_error:
889 Xfree(driverName);
890 Xfree(deviceName);
891 XFree(psc);
892
893 /* FIXME: clean up here */
894
895 return NULL;
896 }
897
898 /* Called from __glXFreeDisplayPrivate.
899 */
900 static void
901 dri2DestroyDisplay(__GLXDRIdisplay * dpy)
902 {
903 Xfree(dpy);
904 }
905
906 _X_HIDDEN __GLXDRIdrawable *
907 dri2GetGlxDrawableFromXDrawableId(Display *dpy, XID id)
908 {
909 struct glx_display *d = __glXInitialize(dpy);
910 struct dri2_display *pdp = (struct dri2_display *) d->dri2Display;
911 __GLXDRIdrawable *pdraw;
912
913 if (__glxHashLookup(pdp->dri2Hash, id, (void *) &pdraw) == 0)
914 return pdraw;
915
916 return NULL;
917 }
918
919 /*
920 * Allocate, initialize and return a __DRIdisplayPrivate object.
921 * This is called from __glXInitialize() when we are given a new
922 * display pointer.
923 */
924 _X_HIDDEN __GLXDRIdisplay *
925 dri2CreateDisplay(Display * dpy)
926 {
927 struct dri2_display *pdp;
928 int eventBase, errorBase, i;
929
930 if (!DRI2QueryExtension(dpy, &eventBase, &errorBase))
931 return NULL;
932
933 pdp = Xmalloc(sizeof *pdp);
934 if (pdp == NULL)
935 return NULL;
936
937 if (!DRI2QueryVersion(dpy, &pdp->driMajor, &pdp->driMinor)) {
938 Xfree(pdp);
939 return NULL;
940 }
941
942 pdp->driPatch = 0;
943 pdp->swapAvailable = (pdp->driMinor >= 2);
944 pdp->invalidateAvailable = (pdp->driMinor >= 3);
945
946 pdp->base.destroyDisplay = dri2DestroyDisplay;
947 pdp->base.createScreen = dri2CreateScreen;
948
949 i = 0;
950 if (pdp->driMinor < 1)
951 pdp->loader_extensions[i++] = &dri2LoaderExtension_old.base;
952 else
953 pdp->loader_extensions[i++] = &dri2LoaderExtension.base;
954
955 pdp->loader_extensions[i++] = &systemTimeExtension.base;
956
957 #ifdef __DRI_USE_INVALIDATE
958 pdp->loader_extensions[i++] = &dri2UseInvalidate.base;
959 #endif
960 pdp->loader_extensions[i++] = NULL;
961
962 pdp->dri2Hash = __glxHashCreate();
963 if (pdp->dri2Hash == NULL) {
964 Xfree(pdp);
965 return NULL;
966 }
967
968 return &pdp->base;
969 }
970
971 #endif /* GLX_DIRECT_RENDERING */