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