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