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