5644f665c4c921ac8bb8c891f0d7296b619ba7c6
[mesa.git] / src / glx / dri2_glx.c
1 /*
2 * Copyright © 2008 Red Hat, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Soft-
6 * ware"), to deal in the Software without restriction, including without
7 * limitation the rights to use, copy, modify, merge, publish, distribute,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, provided that the above copyright
10 * notice(s) and this permission notice appear in all copies of the Soft-
11 * ware and that both the above copyright notice(s) and this permission
12 * notice appear in supporting documentation.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16 * ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
17 * RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN
18 * THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSE-
19 * QUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFOR-
22 * MANCE OF THIS SOFTWARE.
23 *
24 * Except as contained in this notice, the name of a copyright holder shall
25 * not be used in advertising or otherwise to promote the sale, use or
26 * other dealings in this Software without prior written authorization of
27 * the copyright holder.
28 *
29 * Authors:
30 * Kristian Høgsberg (krh@redhat.com)
31 */
32
33 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
34
35 #include <X11/Xlib.h>
36 #include <X11/extensions/Xfixes.h>
37 #include "glapi.h"
38 #include "glxclient.h"
39 #include <X11/extensions/dri2proto.h>
40 #include "xf86dri.h"
41 #include <dlfcn.h>
42 #include <fcntl.h>
43 #include <unistd.h>
44 #include <sys/types.h>
45 #include <sys/mman.h>
46 #include <sys/time.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 __DRI2throttleExtension *throttle;
90 const __DRIconfig **driver_configs;
91
92 void *driver;
93 int fd;
94
95 Bool show_fps;
96 };
97
98 struct dri2_context
99 {
100 struct glx_context base;
101 __DRIcontext *driContext;
102 };
103
104 struct dri2_drawable
105 {
106 __GLXDRIdrawable base;
107 __DRIdrawable *driDrawable;
108 __DRIbuffer buffers[5];
109 int bufferCount;
110 int width, height;
111 int have_back;
112 int have_fake_front;
113 int swap_interval;
114
115 double previous_time;
116 unsigned frames;
117 };
118
119 static const struct glx_context_vtable dri2_context_vtable;
120
121 static void
122 dri2_destroy_context(struct glx_context *context)
123 {
124 struct dri2_context *pcp = (struct dri2_context *) context;
125 struct dri2_screen *psc = (struct dri2_screen *) context->psc;
126
127 driReleaseDrawables(&pcp->base);
128
129 free((char *) context->extensions);
130
131 (*psc->core->destroyContext) (pcp->driContext);
132
133 free(pcp);
134 }
135
136 static Bool
137 dri2_bind_context(struct glx_context *context, struct glx_context *old,
138 GLXDrawable draw, GLXDrawable read)
139 {
140 struct dri2_context *pcp = (struct dri2_context *) context;
141 struct dri2_screen *psc = (struct dri2_screen *) pcp->base.psc;
142 struct dri2_drawable *pdraw, *pread;
143 struct dri2_display *pdp;
144
145 pdraw = (struct dri2_drawable *) driFetchDrawable(context, draw);
146 pread = (struct dri2_drawable *) driFetchDrawable(context, read);
147
148 driReleaseDrawables(&pcp->base);
149
150 if (pdraw == NULL || pread == NULL)
151 return GLXBadDrawable;
152
153 if (!(*psc->core->bindContext) (pcp->driContext,
154 pdraw->driDrawable, pread->driDrawable))
155 return GLXBadContext;
156
157 /* If the server doesn't send invalidate events, we may miss a
158 * resize before the rendering starts. Invalidate the buffers now
159 * so the driver will recheck before rendering starts. */
160 pdp = (struct dri2_display *) psc->base.display;
161 if (!pdp->invalidateAvailable) {
162 dri2InvalidateBuffers(psc->base.dpy, pdraw->base.xDrawable);
163 if (pread != pdraw)
164 dri2InvalidateBuffers(psc->base.dpy, pread->base.xDrawable);
165 }
166
167 return Success;
168 }
169
170 static void
171 dri2_unbind_context(struct glx_context *context, struct glx_context *new)
172 {
173 struct dri2_context *pcp = (struct dri2_context *) context;
174 struct dri2_screen *psc = (struct dri2_screen *) pcp->base.psc;
175
176 (*psc->core->unbindContext) (pcp->driContext);
177 }
178
179 static struct glx_context *
180 dri2_create_context(struct glx_screen *base,
181 struct glx_config *config_base,
182 struct glx_context *shareList, int renderType)
183 {
184 struct dri2_context *pcp, *pcp_shared;
185 struct dri2_screen *psc = (struct dri2_screen *) base;
186 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
187 __DRIcontext *shared = NULL;
188
189 if (shareList) {
190 /* If the shareList context is not a DRI2 context, we cannot possibly
191 * create a DRI2 context that shares it.
192 */
193 if (shareList->vtable->destroy != dri2_destroy_context) {
194 return NULL;
195 }
196
197 pcp_shared = (struct dri2_context *) shareList;
198 shared = pcp_shared->driContext;
199 }
200
201 pcp = malloc(sizeof *pcp);
202 if (pcp == NULL)
203 return NULL;
204
205 memset(pcp, 0, sizeof *pcp);
206 if (!glx_context_init(&pcp->base, &psc->base, &config->base)) {
207 free(pcp);
208 return NULL;
209 }
210
211 pcp->driContext =
212 (*psc->dri2->createNewContext) (psc->driScreen,
213 config->driConfig, shared, pcp);
214
215 if (pcp->driContext == NULL) {
216 free(pcp);
217 return NULL;
218 }
219
220 pcp->base.vtable = &dri2_context_vtable;
221
222 return &pcp->base;
223 }
224
225 static struct glx_context *
226 dri2_create_context_attribs(struct glx_screen *base,
227 struct glx_config *config_base,
228 struct glx_context *shareList,
229 unsigned num_attribs,
230 const uint32_t *attribs,
231 unsigned *error)
232 {
233 struct dri2_context *pcp = NULL;
234 struct dri2_context *pcp_shared = NULL;
235 struct dri2_screen *psc = (struct dri2_screen *) base;
236 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
237 __DRIcontext *shared = NULL;
238
239 uint32_t minor_ver = 1;
240 uint32_t major_ver = 2;
241 uint32_t flags = 0;
242 unsigned api;
243 int reset = __DRI_CTX_RESET_NO_NOTIFICATION;
244 uint32_t ctx_attribs[2 * 5];
245 unsigned num_ctx_attribs = 0;
246
247 if (psc->dri2->base.version < 3) {
248 *error = __DRI_CTX_ERROR_NO_MEMORY;
249 goto error_exit;
250 }
251
252 /* Remap the GLX tokens to DRI2 tokens.
253 */
254 if (!dri2_convert_glx_attribs(num_attribs, attribs,
255 &major_ver, &minor_ver, &flags, &api, &reset,
256 error))
257 goto error_exit;
258
259 if (shareList) {
260 pcp_shared = (struct dri2_context *) shareList;
261 shared = pcp_shared->driContext;
262 }
263
264 pcp = malloc(sizeof *pcp);
265 if (pcp == NULL) {
266 *error = __DRI_CTX_ERROR_NO_MEMORY;
267 goto error_exit;
268 }
269
270 memset(pcp, 0, sizeof *pcp);
271 if (!glx_context_init(&pcp->base, &psc->base, &config->base))
272 goto error_exit;
273
274 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
275 ctx_attribs[num_ctx_attribs++] = major_ver;
276 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
277 ctx_attribs[num_ctx_attribs++] = minor_ver;
278
279 /* Only send a value when the non-default value is requested. By doing
280 * this we don't have to check the driver's DRI2 version before sending the
281 * default value.
282 */
283 if (reset != __DRI_CTX_RESET_NO_NOTIFICATION) {
284 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
285 ctx_attribs[num_ctx_attribs++] = reset;
286 }
287
288 if (flags != 0) {
289 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_FLAGS;
290
291 /* The current __DRI_CTX_FLAG_* values are identical to the
292 * GLX_CONTEXT_*_BIT values.
293 */
294 ctx_attribs[num_ctx_attribs++] = flags;
295 }
296
297 pcp->driContext =
298 (*psc->dri2->createContextAttribs) (psc->driScreen,
299 api,
300 config->driConfig,
301 shared,
302 num_ctx_attribs / 2,
303 ctx_attribs,
304 error,
305 pcp);
306
307 if (pcp->driContext == NULL)
308 goto error_exit;
309
310 pcp->base.vtable = &dri2_context_vtable;
311
312 return &pcp->base;
313
314 error_exit:
315 free(pcp);
316
317 return NULL;
318 }
319
320 static void
321 dri2DestroyDrawable(__GLXDRIdrawable *base)
322 {
323 struct dri2_screen *psc = (struct dri2_screen *) base->psc;
324 struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
325 struct glx_display *dpyPriv = psc->base.display;
326 struct dri2_display *pdp = (struct dri2_display *)dpyPriv->dri2Display;
327
328 __glxHashDelete(pdp->dri2Hash, pdraw->base.xDrawable);
329 (*psc->core->destroyDrawable) (pdraw->driDrawable);
330
331 /* If it's a GLX 1.3 drawables, we can destroy the DRI2 drawable
332 * now, as the application explicitly asked to destroy the GLX
333 * drawable. Otherwise, for legacy drawables, we let the DRI2
334 * drawable linger on the server, since there's no good way of
335 * knowing when the application is done with it. The server will
336 * destroy the DRI2 drawable when it destroys the X drawable or the
337 * client exits anyway. */
338 if (pdraw->base.xDrawable != pdraw->base.drawable)
339 DRI2DestroyDrawable(psc->base.dpy, pdraw->base.xDrawable);
340
341 free(pdraw);
342 }
343
344 static __GLXDRIdrawable *
345 dri2CreateDrawable(struct glx_screen *base, XID xDrawable,
346 GLXDrawable drawable, struct glx_config *config_base)
347 {
348 struct dri2_drawable *pdraw;
349 struct dri2_screen *psc = (struct dri2_screen *) base;
350 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
351 struct glx_display *dpyPriv;
352 struct dri2_display *pdp;
353 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
354
355 pdraw = malloc(sizeof(*pdraw));
356 if (!pdraw)
357 return NULL;
358
359 memset(pdraw, 0, sizeof *pdraw);
360 pdraw->base.destroyDrawable = dri2DestroyDrawable;
361 pdraw->base.xDrawable = xDrawable;
362 pdraw->base.drawable = drawable;
363 pdraw->base.psc = &psc->base;
364 pdraw->bufferCount = 0;
365 pdraw->swap_interval = 1; /* default may be overridden below */
366 pdraw->have_back = 0;
367
368 if (psc->config)
369 psc->config->configQueryi(psc->driScreen,
370 "vblank_mode", &vblank_mode);
371
372 switch (vblank_mode) {
373 case DRI_CONF_VBLANK_NEVER:
374 case DRI_CONF_VBLANK_DEF_INTERVAL_0:
375 pdraw->swap_interval = 0;
376 break;
377 case DRI_CONF_VBLANK_DEF_INTERVAL_1:
378 case DRI_CONF_VBLANK_ALWAYS_SYNC:
379 default:
380 pdraw->swap_interval = 1;
381 break;
382 }
383
384 DRI2CreateDrawable(psc->base.dpy, xDrawable);
385
386 dpyPriv = __glXInitialize(psc->base.dpy);
387 pdp = (struct dri2_display *)dpyPriv->dri2Display;;
388 /* Create a new drawable */
389 pdraw->driDrawable =
390 (*psc->dri2->createNewDrawable) (psc->driScreen,
391 config->driConfig, pdraw);
392
393 if (!pdraw->driDrawable) {
394 DRI2DestroyDrawable(psc->base.dpy, xDrawable);
395 free(pdraw);
396 return NULL;
397 }
398
399 if (__glxHashInsert(pdp->dri2Hash, xDrawable, pdraw)) {
400 (*psc->core->destroyDrawable) (pdraw->driDrawable);
401 DRI2DestroyDrawable(psc->base.dpy, xDrawable);
402 free(pdraw);
403 return None;
404 }
405
406
407 #ifdef X_DRI2SwapInterval
408 /*
409 * Make sure server has the same swap interval we do for the new
410 * drawable.
411 */
412 if (pdp->swapAvailable)
413 DRI2SwapInterval(psc->base.dpy, xDrawable, pdraw->swap_interval);
414 #endif
415
416 return &pdraw->base;
417 }
418
419 #ifdef X_DRI2GetMSC
420
421 static int
422 dri2DrawableGetMSC(struct glx_screen *psc, __GLXDRIdrawable *pdraw,
423 int64_t *ust, int64_t *msc, int64_t *sbc)
424 {
425 CARD64 dri2_ust, dri2_msc, dri2_sbc;
426 int ret;
427
428 ret = DRI2GetMSC(psc->dpy, pdraw->xDrawable,
429 &dri2_ust, &dri2_msc, &dri2_sbc);
430 *ust = dri2_ust;
431 *msc = dri2_msc;
432 *sbc = dri2_sbc;
433
434 return ret;
435 }
436
437 #endif
438
439
440 #ifdef X_DRI2WaitMSC
441
442 static int
443 dri2WaitForMSC(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
444 int64_t remainder, int64_t *ust, int64_t *msc, int64_t *sbc)
445 {
446 CARD64 dri2_ust, dri2_msc, dri2_sbc;
447 int ret;
448
449 ret = DRI2WaitMSC(pdraw->psc->dpy, pdraw->xDrawable, target_msc, divisor,
450 remainder, &dri2_ust, &dri2_msc, &dri2_sbc);
451 *ust = dri2_ust;
452 *msc = dri2_msc;
453 *sbc = dri2_sbc;
454
455 return ret;
456 }
457
458 static int
459 dri2WaitForSBC(__GLXDRIdrawable *pdraw, int64_t target_sbc, int64_t *ust,
460 int64_t *msc, int64_t *sbc)
461 {
462 CARD64 dri2_ust, dri2_msc, dri2_sbc;
463 int ret;
464
465 ret = DRI2WaitSBC(pdraw->psc->dpy, pdraw->xDrawable,
466 target_sbc, &dri2_ust, &dri2_msc, &dri2_sbc);
467 *ust = dri2_ust;
468 *msc = dri2_msc;
469 *sbc = dri2_sbc;
470
471 return ret;
472 }
473
474 #endif /* X_DRI2WaitMSC */
475
476 /**
477 * dri2Throttle - Request driver throttling
478 *
479 * This function uses the DRI2 throttle extension to give the
480 * driver the opportunity to throttle on flush front, copysubbuffer
481 * and swapbuffers.
482 */
483 static void
484 dri2Throttle(struct dri2_screen *psc,
485 struct dri2_drawable *draw,
486 enum __DRI2throttleReason reason)
487 {
488 if (psc->throttle) {
489 struct glx_context *gc = __glXGetCurrentContext();
490 struct dri2_context *dri2Ctx = (struct dri2_context *)gc;
491 __DRIcontext *ctx =
492 (dri2Ctx) ? dri2Ctx->driContext : NULL;
493
494 psc->throttle->throttle(ctx, draw->driDrawable, reason);
495 }
496 }
497
498 static void
499 __dri2CopySubBuffer(__GLXDRIdrawable *pdraw, int x, int y,
500 int width, int height,
501 enum __DRI2throttleReason reason)
502 {
503 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
504 struct dri2_screen *psc = (struct dri2_screen *) pdraw->psc;
505 XRectangle xrect;
506 XserverRegion region;
507
508 /* Check we have the right attachments */
509 if (!priv->have_back)
510 return;
511
512 xrect.x = x;
513 xrect.y = priv->height - y - height;
514 xrect.width = width;
515 xrect.height = height;
516
517 #ifdef __DRI2_FLUSH
518 if (psc->f)
519 (*psc->f->flush) (priv->driDrawable);
520 #endif
521
522 dri2Throttle(psc, priv, reason);
523
524 region = XFixesCreateRegion(psc->base.dpy, &xrect, 1);
525 DRI2CopyRegion(psc->base.dpy, pdraw->xDrawable, region,
526 DRI2BufferFrontLeft, DRI2BufferBackLeft);
527
528 /* Refresh the fake front (if present) after we just damaged the real
529 * front.
530 */
531 if (priv->have_fake_front)
532 DRI2CopyRegion(psc->base.dpy, pdraw->xDrawable, region,
533 DRI2BufferFakeFrontLeft, DRI2BufferFrontLeft);
534
535 XFixesDestroyRegion(psc->base.dpy, region);
536 }
537
538 static void
539 dri2CopySubBuffer(__GLXDRIdrawable *pdraw, int x, int y,
540 int width, int height)
541 {
542 __dri2CopySubBuffer(pdraw, x, y, width, height,
543 __DRI2_THROTTLE_COPYSUBBUFFER);
544 }
545
546
547 static void
548 dri2_copy_drawable(struct dri2_drawable *priv, int dest, int src)
549 {
550 XRectangle xrect;
551 XserverRegion region;
552 struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
553
554 xrect.x = 0;
555 xrect.y = 0;
556 xrect.width = priv->width;
557 xrect.height = priv->height;
558
559 #ifdef __DRI2_FLUSH
560 if (psc->f)
561 (*psc->f->flush) (priv->driDrawable);
562 #endif
563
564 region = XFixesCreateRegion(psc->base.dpy, &xrect, 1);
565 DRI2CopyRegion(psc->base.dpy, priv->base.xDrawable, region, dest, src);
566 XFixesDestroyRegion(psc->base.dpy, region);
567
568 }
569
570 static void
571 dri2_wait_x(struct glx_context *gc)
572 {
573 struct dri2_drawable *priv = (struct dri2_drawable *)
574 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
575
576 if (priv == NULL || !priv->have_fake_front)
577 return;
578
579 dri2_copy_drawable(priv, DRI2BufferFakeFrontLeft, DRI2BufferFrontLeft);
580 }
581
582 static void
583 dri2_wait_gl(struct glx_context *gc)
584 {
585 struct dri2_drawable *priv = (struct dri2_drawable *)
586 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
587
588 if (priv == NULL || !priv->have_fake_front)
589 return;
590
591 dri2_copy_drawable(priv, DRI2BufferFrontLeft, DRI2BufferFakeFrontLeft);
592 }
593
594 static void
595 dri2FlushFrontBuffer(__DRIdrawable *driDrawable, void *loaderPrivate)
596 {
597 struct glx_display *priv;
598 struct dri2_display *pdp;
599 struct glx_context *gc;
600 struct dri2_drawable *pdraw = loaderPrivate;
601 struct dri2_screen *psc;
602
603 if (!pdraw)
604 return;
605
606 if (!pdraw->base.psc)
607 return;
608
609 psc = (struct dri2_screen *) pdraw->base.psc;
610
611 priv = __glXInitialize(psc->base.dpy);
612 pdp = (struct dri2_display *) priv->dri2Display;
613 gc = __glXGetCurrentContext();
614
615 dri2Throttle(psc, pdraw, __DRI2_THROTTLE_FLUSHFRONT);
616
617 /* Old servers don't send invalidate events */
618 if (!pdp->invalidateAvailable)
619 dri2InvalidateBuffers(priv->dpy, pdraw->base.xDrawable);
620
621 dri2_wait_gl(gc);
622 }
623
624
625 static void
626 dri2DestroyScreen(struct glx_screen *base)
627 {
628 struct dri2_screen *psc = (struct dri2_screen *) base;
629
630 /* Free the direct rendering per screen data */
631 (*psc->core->destroyScreen) (psc->driScreen);
632 driDestroyConfigs(psc->driver_configs);
633 close(psc->fd);
634 free(psc);
635 }
636
637 /**
638 * Process list of buffer received from the server
639 *
640 * Processes the list of buffers received in a reply from the server to either
641 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
642 */
643 static void
644 process_buffers(struct dri2_drawable * pdraw, DRI2Buffer * buffers,
645 unsigned count)
646 {
647 int i;
648
649 pdraw->bufferCount = count;
650 pdraw->have_fake_front = 0;
651 pdraw->have_back = 0;
652
653 /* This assumes the DRI2 buffer attachment tokens matches the
654 * __DRIbuffer tokens. */
655 for (i = 0; i < count; i++) {
656 pdraw->buffers[i].attachment = buffers[i].attachment;
657 pdraw->buffers[i].name = buffers[i].name;
658 pdraw->buffers[i].pitch = buffers[i].pitch;
659 pdraw->buffers[i].cpp = buffers[i].cpp;
660 pdraw->buffers[i].flags = buffers[i].flags;
661 if (pdraw->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)
662 pdraw->have_fake_front = 1;
663 if (pdraw->buffers[i].attachment == __DRI_BUFFER_BACK_LEFT)
664 pdraw->have_back = 1;
665 }
666
667 }
668
669 unsigned dri2GetSwapEventType(Display* dpy, XID drawable)
670 {
671 struct glx_display *glx_dpy = __glXInitialize(dpy);
672 __GLXDRIdrawable *pdraw;
673 pdraw = dri2GetGlxDrawableFromXDrawableId(dpy, drawable);
674 if (!pdraw || !(pdraw->eventMask & GLX_BUFFER_SWAP_COMPLETE_INTEL_MASK))
675 return 0;
676 return glx_dpy->codes->first_event + GLX_BufferSwapComplete;
677 }
678
679 static void show_fps(struct dri2_drawable *draw)
680 {
681 struct timeval tv;
682 double current_time;
683
684 gettimeofday(&tv, 0);
685 current_time = (double)tv.tv_sec + (double)tv.tv_usec * 0.000001;
686
687 draw->frames++;
688
689 if (draw->previous_time + 1 < current_time) {
690 if (draw->previous_time) {
691 fprintf(stderr, "libGL: FPS = %.1f\n",
692 draw->frames / (current_time - draw->previous_time));
693 }
694 draw->frames = 0;
695 draw->previous_time = current_time;
696 }
697 }
698
699 static int64_t
700 dri2SwapBuffers(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
701 int64_t remainder)
702 {
703 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
704 struct glx_display *dpyPriv = __glXInitialize(priv->base.psc->dpy);
705 struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
706 struct dri2_display *pdp =
707 (struct dri2_display *)dpyPriv->dri2Display;
708 CARD64 ret = 0;
709
710 /* Check we have the right attachments */
711 if (!priv->have_back)
712 return ret;
713
714 /* Old servers can't handle swapbuffers */
715 if (!pdp->swapAvailable) {
716 __dri2CopySubBuffer(pdraw, 0, 0, priv->width, priv->height,
717 __DRI2_THROTTLE_SWAPBUFFER);
718 } else {
719 #ifdef X_DRI2SwapBuffers
720 #ifdef __DRI2_FLUSH
721 if (psc->f) {
722 struct glx_context *gc = __glXGetCurrentContext();
723
724 if (gc) {
725 (*psc->f->flush)(priv->driDrawable);
726 }
727 }
728 #endif
729
730 dri2Throttle(psc, priv, __DRI2_THROTTLE_SWAPBUFFER);
731
732 DRI2SwapBuffers(psc->base.dpy, pdraw->xDrawable,
733 target_msc, divisor, remainder, &ret);
734 #endif
735 }
736
737 if (psc->show_fps) {
738 show_fps(priv);
739 }
740
741 /* Old servers don't send invalidate events */
742 if (!pdp->invalidateAvailable)
743 dri2InvalidateBuffers(dpyPriv->dpy, pdraw->xDrawable);
744
745 return ret;
746 }
747
748 static __DRIbuffer *
749 dri2GetBuffers(__DRIdrawable * driDrawable,
750 int *width, int *height,
751 unsigned int *attachments, int count,
752 int *out_count, void *loaderPrivate)
753 {
754 struct dri2_drawable *pdraw = loaderPrivate;
755 DRI2Buffer *buffers;
756
757 buffers = DRI2GetBuffers(pdraw->base.psc->dpy, pdraw->base.xDrawable,
758 width, height, attachments, count, out_count);
759 if (buffers == NULL)
760 return NULL;
761
762 pdraw->width = *width;
763 pdraw->height = *height;
764 process_buffers(pdraw, buffers, *out_count);
765
766 free(buffers);
767
768 return pdraw->buffers;
769 }
770
771 static __DRIbuffer *
772 dri2GetBuffersWithFormat(__DRIdrawable * driDrawable,
773 int *width, int *height,
774 unsigned int *attachments, int count,
775 int *out_count, void *loaderPrivate)
776 {
777 struct dri2_drawable *pdraw = loaderPrivate;
778 DRI2Buffer *buffers;
779
780 buffers = DRI2GetBuffersWithFormat(pdraw->base.psc->dpy,
781 pdraw->base.xDrawable,
782 width, height, attachments,
783 count, out_count);
784 if (buffers == NULL)
785 return NULL;
786
787 pdraw->width = *width;
788 pdraw->height = *height;
789 process_buffers(pdraw, buffers, *out_count);
790
791 free(buffers);
792
793 return pdraw->buffers;
794 }
795
796 #ifdef X_DRI2SwapInterval
797
798 static int
799 dri2SetSwapInterval(__GLXDRIdrawable *pdraw, int interval)
800 {
801 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
802 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
803 struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
804
805 if (psc->config)
806 psc->config->configQueryi(psc->driScreen,
807 "vblank_mode", &vblank_mode);
808
809 switch (vblank_mode) {
810 case DRI_CONF_VBLANK_NEVER:
811 return GLX_BAD_VALUE;
812 case DRI_CONF_VBLANK_ALWAYS_SYNC:
813 if (interval <= 0)
814 return GLX_BAD_VALUE;
815 break;
816 default:
817 break;
818 }
819
820 DRI2SwapInterval(priv->base.psc->dpy, priv->base.xDrawable, interval);
821 priv->swap_interval = interval;
822
823 return 0;
824 }
825
826 static int
827 dri2GetSwapInterval(__GLXDRIdrawable *pdraw)
828 {
829 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
830
831 return priv->swap_interval;
832 }
833
834 #endif /* X_DRI2SwapInterval */
835
836 static const __DRIdri2LoaderExtension dri2LoaderExtension = {
837 {__DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION},
838 dri2GetBuffers,
839 dri2FlushFrontBuffer,
840 dri2GetBuffersWithFormat,
841 };
842
843 static const __DRIdri2LoaderExtension dri2LoaderExtension_old = {
844 {__DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION},
845 dri2GetBuffers,
846 dri2FlushFrontBuffer,
847 NULL,
848 };
849
850 #ifdef __DRI_USE_INVALIDATE
851 static const __DRIuseInvalidateExtension dri2UseInvalidate = {
852 { __DRI_USE_INVALIDATE, __DRI_USE_INVALIDATE_VERSION }
853 };
854 #endif
855
856 _X_HIDDEN void
857 dri2InvalidateBuffers(Display *dpy, XID drawable)
858 {
859 __GLXDRIdrawable *pdraw =
860 dri2GetGlxDrawableFromXDrawableId(dpy, drawable);
861 struct dri2_screen *psc;
862 struct dri2_drawable *pdp = (struct dri2_drawable *) pdraw;
863
864 if (!pdraw)
865 return;
866
867 psc = (struct dri2_screen *) pdraw->psc;
868
869 #if __DRI2_FLUSH_VERSION >= 3
870 if (pdraw && psc->f && psc->f->base.version >= 3 && psc->f->invalidate)
871 psc->f->invalidate(pdp->driDrawable);
872 #endif
873 }
874
875 static void
876 dri2_bind_tex_image(Display * dpy,
877 GLXDrawable drawable,
878 int buffer, const int *attrib_list)
879 {
880 struct glx_context *gc = __glXGetCurrentContext();
881 struct dri2_context *pcp = (struct dri2_context *) gc;
882 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
883 struct glx_display *dpyPriv = __glXInitialize(dpy);
884 struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
885 struct dri2_display *pdp =
886 (struct dri2_display *) dpyPriv->dri2Display;
887 struct dri2_screen *psc;
888
889 if (pdraw != NULL) {
890 psc = (struct dri2_screen *) base->psc;
891
892 #if __DRI2_FLUSH_VERSION >= 3
893 if (!pdp->invalidateAvailable && psc->f &&
894 psc->f->base.version >= 3 && psc->f->invalidate)
895 psc->f->invalidate(pdraw->driDrawable);
896 #endif
897
898 if (psc->texBuffer->base.version >= 2 &&
899 psc->texBuffer->setTexBuffer2 != NULL) {
900 (*psc->texBuffer->setTexBuffer2) (pcp->driContext,
901 pdraw->base.textureTarget,
902 pdraw->base.textureFormat,
903 pdraw->driDrawable);
904 }
905 else {
906 (*psc->texBuffer->setTexBuffer) (pcp->driContext,
907 pdraw->base.textureTarget,
908 pdraw->driDrawable);
909 }
910 }
911 }
912
913 static void
914 dri2_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer)
915 {
916 #if __DRI_TEX_BUFFER_VERSION >= 3
917 struct glx_context *gc = __glXGetCurrentContext();
918 struct dri2_context *pcp = (struct dri2_context *) gc;
919 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
920 struct glx_display *dpyPriv = __glXInitialize(dpy);
921 struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
922 struct dri2_display *pdp =
923 (struct dri2_display *) dpyPriv->dri2Display;
924 struct dri2_screen *psc;
925
926 if (pdraw != NULL) {
927 psc = (struct dri2_screen *) base->psc;
928
929 if (psc->texBuffer->base.version >= 3 &&
930 psc->texBuffer->releaseTexBuffer != NULL) {
931 (*psc->texBuffer->releaseTexBuffer) (pcp->driContext,
932 pdraw->base.textureTarget,
933 pdraw->driDrawable);
934 }
935 }
936 #endif
937 }
938
939 static const struct glx_context_vtable dri2_context_vtable = {
940 dri2_destroy_context,
941 dri2_bind_context,
942 dri2_unbind_context,
943 dri2_wait_gl,
944 dri2_wait_x,
945 DRI_glXUseXFont,
946 dri2_bind_tex_image,
947 dri2_release_tex_image,
948 NULL, /* get_proc_address */
949 };
950
951 static void
952 dri2BindExtensions(struct dri2_screen *psc, const __DRIextension **extensions)
953 {
954 int i;
955
956 __glXEnableDirectExtension(&psc->base, "GLX_SGI_video_sync");
957 __glXEnableDirectExtension(&psc->base, "GLX_SGI_swap_control");
958 __glXEnableDirectExtension(&psc->base, "GLX_MESA_swap_control");
959 __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
960
961 /* FIXME: if DRI2 version supports it... */
962 __glXEnableDirectExtension(&psc->base, "INTEL_swap_event");
963
964 if (psc->dri2->base.version >= 3) {
965 const unsigned mask = psc->dri2->getAPIMask(psc->driScreen);
966
967 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context");
968 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context_profile");
969
970 if ((mask & (1 << __DRI_API_GLES2)) != 0)
971 __glXEnableDirectExtension(&psc->base,
972 "GLX_EXT_create_context_es2_profile");
973 }
974
975 for (i = 0; extensions[i]; i++) {
976 if ((strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
977 psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
978 __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
979 }
980
981 if ((strcmp(extensions[i]->name, __DRI2_FLUSH) == 0)) {
982 psc->f = (__DRI2flushExtension *) extensions[i];
983 /* internal driver extension, no GL extension exposed */
984 }
985
986 if ((strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0))
987 psc->config = (__DRI2configQueryExtension *) extensions[i];
988
989 if (((strcmp(extensions[i]->name, __DRI2_THROTTLE) == 0)))
990 psc->throttle = (__DRI2throttleExtension *) extensions[i];
991
992 /* DRI2 version 3 is also required because
993 * GLX_ARB_create_context_robustness requires GLX_ARB_create_context.
994 */
995 if (psc->dri2->base.version >= 3
996 && strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0)
997 __glXEnableDirectExtension(&psc->base,
998 "GLX_ARB_create_context_robustness");
999 }
1000 }
1001
1002 static const struct glx_screen_vtable dri2_screen_vtable = {
1003 dri2_create_context,
1004 dri2_create_context_attribs
1005 };
1006
1007 static struct glx_screen *
1008 dri2CreateScreen(int screen, struct glx_display * priv)
1009 {
1010 const __DRIconfig **driver_configs;
1011 const __DRIextension **extensions;
1012 const struct dri2_display *const pdp = (struct dri2_display *)
1013 priv->dri2Display;
1014 struct dri2_screen *psc;
1015 __GLXDRIscreen *psp;
1016 struct glx_config *configs = NULL, *visuals = NULL;
1017 char *driverName, *deviceName, *tmp;
1018 drm_magic_t magic;
1019 int i;
1020
1021 psc = malloc(sizeof *psc);
1022 if (psc == NULL)
1023 return NULL;
1024
1025 memset(psc, 0, sizeof *psc);
1026 psc->fd = -1;
1027
1028 if (!glx_screen_init(&psc->base, screen, priv)) {
1029 free(psc);
1030 return NULL;
1031 }
1032
1033 if (!DRI2Connect(priv->dpy, RootWindow(priv->dpy, screen),
1034 &driverName, &deviceName)) {
1035 glx_screen_cleanup(&psc->base);
1036 free(psc);
1037 InfoMessageF("screen %d does not appear to be DRI2 capable\n", screen);
1038 return NULL;
1039 }
1040
1041 psc->driver = driOpenDriver(driverName);
1042 if (psc->driver == NULL) {
1043 ErrorMessageF("driver pointer missing\n");
1044 goto handle_error;
1045 }
1046
1047 extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
1048 if (extensions == NULL) {
1049 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
1050 goto handle_error;
1051 }
1052
1053 for (i = 0; extensions[i]; i++) {
1054 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
1055 psc->core = (__DRIcoreExtension *) extensions[i];
1056 if (strcmp(extensions[i]->name, __DRI_DRI2) == 0)
1057 psc->dri2 = (__DRIdri2Extension *) extensions[i];
1058 }
1059
1060 if (psc->core == NULL || psc->dri2 == NULL) {
1061 ErrorMessageF("core dri or dri2 extension not found\n");
1062 goto handle_error;
1063 }
1064
1065 #ifdef O_CLOEXEC
1066 psc->fd = open(deviceName, O_RDWR | O_CLOEXEC);
1067 if (psc->fd == -1 && errno == EINVAL)
1068 #endif
1069 {
1070 psc->fd = open(deviceName, O_RDWR);
1071 if (psc->fd != -1)
1072 fcntl(psc->fd, F_SETFD, fcntl(psc->fd, F_GETFD) | FD_CLOEXEC);
1073 }
1074 if (psc->fd < 0) {
1075 ErrorMessageF("failed to open drm device: %s\n", strerror(errno));
1076 goto handle_error;
1077 }
1078
1079 if (drmGetMagic(psc->fd, &magic)) {
1080 ErrorMessageF("failed to get magic\n");
1081 goto handle_error;
1082 }
1083
1084 if (!DRI2Authenticate(priv->dpy, RootWindow(priv->dpy, screen), magic)) {
1085 ErrorMessageF("failed to authenticate magic %d\n", magic);
1086 goto handle_error;
1087 }
1088
1089
1090 /* If the server does not support the protocol for
1091 * DRI2GetBuffersWithFormat, don't supply that interface to the driver.
1092 */
1093 psc->driScreen =
1094 psc->dri2->createNewScreen(screen, psc->fd,
1095 (const __DRIextension **)
1096 &pdp->loader_extensions[0],
1097 &driver_configs, psc);
1098
1099 if (psc->driScreen == NULL) {
1100 ErrorMessageF("failed to create dri screen\n");
1101 goto handle_error;
1102 }
1103
1104 extensions = psc->core->getExtensions(psc->driScreen);
1105 dri2BindExtensions(psc, extensions);
1106
1107 configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs);
1108 visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
1109
1110 if (!configs || !visuals)
1111 goto handle_error;
1112
1113 glx_config_destroy_list(psc->base.configs);
1114 psc->base.configs = configs;
1115 glx_config_destroy_list(psc->base.visuals);
1116 psc->base.visuals = visuals;
1117
1118 psc->driver_configs = driver_configs;
1119
1120 psc->base.vtable = &dri2_screen_vtable;
1121 psp = &psc->vtable;
1122 psc->base.driScreen = psp;
1123 psp->destroyScreen = dri2DestroyScreen;
1124 psp->createDrawable = dri2CreateDrawable;
1125 psp->swapBuffers = dri2SwapBuffers;
1126 psp->getDrawableMSC = NULL;
1127 psp->waitForMSC = NULL;
1128 psp->waitForSBC = NULL;
1129 psp->setSwapInterval = NULL;
1130 psp->getSwapInterval = NULL;
1131
1132 if (pdp->driMinor >= 2) {
1133 #ifdef X_DRI2GetMSC
1134 psp->getDrawableMSC = dri2DrawableGetMSC;
1135 #endif
1136 #ifdef X_DRI2WaitMSC
1137 psp->waitForMSC = dri2WaitForMSC;
1138 psp->waitForSBC = dri2WaitForSBC;
1139 #endif
1140 #ifdef X_DRI2SwapInterval
1141 psp->setSwapInterval = dri2SetSwapInterval;
1142 psp->getSwapInterval = dri2GetSwapInterval;
1143 #endif
1144 #if defined(X_DRI2GetMSC) && defined(X_DRI2WaitMSC) && defined(X_DRI2SwapInterval)
1145 __glXEnableDirectExtension(&psc->base, "GLX_OML_sync_control");
1146 #endif
1147 }
1148
1149 /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
1150 * available.*/
1151 psp->copySubBuffer = dri2CopySubBuffer;
1152 __glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer");
1153
1154 free(driverName);
1155 free(deviceName);
1156
1157 tmp = getenv("LIBGL_SHOW_FPS");
1158 psc->show_fps = tmp && strcmp(tmp, "1") == 0;
1159
1160 return &psc->base;
1161
1162 handle_error:
1163 CriticalErrorMessageF("failed to load driver: %s\n", driverName);
1164
1165 if (configs)
1166 glx_config_destroy_list(configs);
1167 if (visuals)
1168 glx_config_destroy_list(visuals);
1169 if (psc->driScreen)
1170 psc->core->destroyScreen(psc->driScreen);
1171 psc->driScreen = NULL;
1172 if (psc->fd >= 0)
1173 close(psc->fd);
1174 if (psc->driver)
1175 dlclose(psc->driver);
1176
1177 free(driverName);
1178 free(deviceName);
1179 glx_screen_cleanup(&psc->base);
1180 free(psc);
1181
1182 return NULL;
1183 }
1184
1185 /* Called from __glXFreeDisplayPrivate.
1186 */
1187 static void
1188 dri2DestroyDisplay(__GLXDRIdisplay * dpy)
1189 {
1190 struct dri2_display *pdp = (struct dri2_display *) dpy;
1191
1192 __glxHashDestroy(pdp->dri2Hash);
1193 free(dpy);
1194 }
1195
1196 _X_HIDDEN __GLXDRIdrawable *
1197 dri2GetGlxDrawableFromXDrawableId(Display *dpy, XID id)
1198 {
1199 struct glx_display *d = __glXInitialize(dpy);
1200 struct dri2_display *pdp = (struct dri2_display *) d->dri2Display;
1201 __GLXDRIdrawable *pdraw;
1202
1203 if (__glxHashLookup(pdp->dri2Hash, id, (void *) &pdraw) == 0)
1204 return pdraw;
1205
1206 return NULL;
1207 }
1208
1209 /*
1210 * Allocate, initialize and return a __DRIdisplayPrivate object.
1211 * This is called from __glXInitialize() when we are given a new
1212 * display pointer.
1213 */
1214 _X_HIDDEN __GLXDRIdisplay *
1215 dri2CreateDisplay(Display * dpy)
1216 {
1217 struct dri2_display *pdp;
1218 int eventBase, errorBase, i;
1219
1220 if (!DRI2QueryExtension(dpy, &eventBase, &errorBase))
1221 return NULL;
1222
1223 pdp = malloc(sizeof *pdp);
1224 if (pdp == NULL)
1225 return NULL;
1226
1227 if (!DRI2QueryVersion(dpy, &pdp->driMajor, &pdp->driMinor)) {
1228 free(pdp);
1229 return NULL;
1230 }
1231
1232 pdp->driPatch = 0;
1233 pdp->swapAvailable = (pdp->driMinor >= 2);
1234 pdp->invalidateAvailable = (pdp->driMinor >= 3);
1235
1236 pdp->base.destroyDisplay = dri2DestroyDisplay;
1237 pdp->base.createScreen = dri2CreateScreen;
1238
1239 i = 0;
1240 if (pdp->driMinor < 1)
1241 pdp->loader_extensions[i++] = &dri2LoaderExtension_old.base;
1242 else
1243 pdp->loader_extensions[i++] = &dri2LoaderExtension.base;
1244
1245 pdp->loader_extensions[i++] = &systemTimeExtension.base;
1246
1247 #ifdef __DRI_USE_INVALIDATE
1248 pdp->loader_extensions[i++] = &dri2UseInvalidate.base;
1249 #endif
1250 pdp->loader_extensions[i++] = NULL;
1251
1252 pdp->dri2Hash = __glxHashCreate();
1253 if (pdp->dri2Hash == NULL) {
1254 free(pdp);
1255 return NULL;
1256 }
1257
1258 return &pdp->base;
1259 }
1260
1261 #endif /* GLX_DIRECT_RENDERING */