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