Use calloc instead of malloc/memset-0
[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 = 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 #ifdef __DRI2_FLUSH
515 if (psc->f)
516 (*psc->f->flush) (priv->driDrawable);
517 #endif
518
519 dri2Throttle(psc, priv, reason);
520
521 region = XFixesCreateRegion(psc->base.dpy, &xrect, 1);
522 DRI2CopyRegion(psc->base.dpy, pdraw->xDrawable, region,
523 DRI2BufferFrontLeft, DRI2BufferBackLeft);
524
525 /* Refresh the fake front (if present) after we just damaged the real
526 * front.
527 */
528 if (priv->have_fake_front)
529 DRI2CopyRegion(psc->base.dpy, pdraw->xDrawable, region,
530 DRI2BufferFakeFrontLeft, DRI2BufferFrontLeft);
531
532 XFixesDestroyRegion(psc->base.dpy, region);
533 }
534
535 static void
536 dri2CopySubBuffer(__GLXDRIdrawable *pdraw, int x, int y,
537 int width, int height)
538 {
539 __dri2CopySubBuffer(pdraw, x, y, width, height,
540 __DRI2_THROTTLE_COPYSUBBUFFER);
541 }
542
543
544 static void
545 dri2_copy_drawable(struct dri2_drawable *priv, int dest, int src)
546 {
547 XRectangle xrect;
548 XserverRegion region;
549 struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
550
551 xrect.x = 0;
552 xrect.y = 0;
553 xrect.width = priv->width;
554 xrect.height = priv->height;
555
556 #ifdef __DRI2_FLUSH
557 if (psc->f)
558 (*psc->f->flush) (priv->driDrawable);
559 #endif
560
561 region = XFixesCreateRegion(psc->base.dpy, &xrect, 1);
562 DRI2CopyRegion(psc->base.dpy, priv->base.xDrawable, region, dest, src);
563 XFixesDestroyRegion(psc->base.dpy, region);
564
565 }
566
567 static void
568 dri2_wait_x(struct glx_context *gc)
569 {
570 struct dri2_drawable *priv = (struct dri2_drawable *)
571 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
572
573 if (priv == NULL || !priv->have_fake_front)
574 return;
575
576 dri2_copy_drawable(priv, DRI2BufferFakeFrontLeft, DRI2BufferFrontLeft);
577 }
578
579 static void
580 dri2_wait_gl(struct glx_context *gc)
581 {
582 struct dri2_drawable *priv = (struct dri2_drawable *)
583 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
584
585 if (priv == NULL || !priv->have_fake_front)
586 return;
587
588 dri2_copy_drawable(priv, DRI2BufferFrontLeft, DRI2BufferFakeFrontLeft);
589 }
590
591 static void
592 dri2FlushFrontBuffer(__DRIdrawable *driDrawable, void *loaderPrivate)
593 {
594 struct glx_display *priv;
595 struct dri2_display *pdp;
596 struct glx_context *gc;
597 struct dri2_drawable *pdraw = loaderPrivate;
598 struct dri2_screen *psc;
599
600 if (!pdraw)
601 return;
602
603 if (!pdraw->base.psc)
604 return;
605
606 psc = (struct dri2_screen *) pdraw->base.psc;
607
608 priv = __glXInitialize(psc->base.dpy);
609 pdp = (struct dri2_display *) priv->dri2Display;
610 gc = __glXGetCurrentContext();
611
612 dri2Throttle(psc, pdraw, __DRI2_THROTTLE_FLUSHFRONT);
613
614 /* Old servers don't send invalidate events */
615 if (!pdp->invalidateAvailable)
616 dri2InvalidateBuffers(priv->dpy, pdraw->base.xDrawable);
617
618 dri2_wait_gl(gc);
619 }
620
621
622 static void
623 dri2DestroyScreen(struct glx_screen *base)
624 {
625 struct dri2_screen *psc = (struct dri2_screen *) base;
626
627 /* Free the direct rendering per screen data */
628 (*psc->core->destroyScreen) (psc->driScreen);
629 driDestroyConfigs(psc->driver_configs);
630 close(psc->fd);
631 free(psc);
632 }
633
634 /**
635 * Process list of buffer received from the server
636 *
637 * Processes the list of buffers received in a reply from the server to either
638 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
639 */
640 static void
641 process_buffers(struct dri2_drawable * pdraw, DRI2Buffer * buffers,
642 unsigned count)
643 {
644 int i;
645
646 pdraw->bufferCount = count;
647 pdraw->have_fake_front = 0;
648 pdraw->have_back = 0;
649
650 /* This assumes the DRI2 buffer attachment tokens matches the
651 * __DRIbuffer tokens. */
652 for (i = 0; i < count; i++) {
653 pdraw->buffers[i].attachment = buffers[i].attachment;
654 pdraw->buffers[i].name = buffers[i].name;
655 pdraw->buffers[i].pitch = buffers[i].pitch;
656 pdraw->buffers[i].cpp = buffers[i].cpp;
657 pdraw->buffers[i].flags = buffers[i].flags;
658 if (pdraw->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)
659 pdraw->have_fake_front = 1;
660 if (pdraw->buffers[i].attachment == __DRI_BUFFER_BACK_LEFT)
661 pdraw->have_back = 1;
662 }
663
664 }
665
666 unsigned dri2GetSwapEventType(Display* dpy, XID drawable)
667 {
668 struct glx_display *glx_dpy = __glXInitialize(dpy);
669 __GLXDRIdrawable *pdraw;
670 pdraw = dri2GetGlxDrawableFromXDrawableId(dpy, drawable);
671 if (!pdraw || !(pdraw->eventMask & GLX_BUFFER_SWAP_COMPLETE_INTEL_MASK))
672 return 0;
673 return glx_dpy->codes->first_event + GLX_BufferSwapComplete;
674 }
675
676 static void show_fps(struct dri2_drawable *draw)
677 {
678 struct timeval tv;
679 double current_time;
680
681 gettimeofday(&tv, 0);
682 current_time = (double)tv.tv_sec + (double)tv.tv_usec * 0.000001;
683
684 draw->frames++;
685
686 if (draw->previous_time + 1 < current_time) {
687 if (draw->previous_time) {
688 fprintf(stderr, "libGL: FPS = %.1f\n",
689 draw->frames / (current_time - draw->previous_time));
690 }
691 draw->frames = 0;
692 draw->previous_time = current_time;
693 }
694 }
695
696 static int64_t
697 dri2SwapBuffers(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
698 int64_t remainder)
699 {
700 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
701 struct glx_display *dpyPriv = __glXInitialize(priv->base.psc->dpy);
702 struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
703 struct dri2_display *pdp =
704 (struct dri2_display *)dpyPriv->dri2Display;
705 CARD64 ret = 0;
706
707 /* Check we have the right attachments */
708 if (!priv->have_back)
709 return ret;
710
711 /* Old servers can't handle swapbuffers */
712 if (!pdp->swapAvailable) {
713 __dri2CopySubBuffer(pdraw, 0, 0, priv->width, priv->height,
714 __DRI2_THROTTLE_SWAPBUFFER);
715 } else {
716 #ifdef X_DRI2SwapBuffers
717 #ifdef __DRI2_FLUSH
718 if (psc->f) {
719 struct glx_context *gc = __glXGetCurrentContext();
720
721 if (gc) {
722 (*psc->f->flush)(priv->driDrawable);
723 }
724 }
725 #endif
726
727 dri2Throttle(psc, priv, __DRI2_THROTTLE_SWAPBUFFER);
728
729 DRI2SwapBuffers(psc->base.dpy, pdraw->xDrawable,
730 target_msc, divisor, remainder, &ret);
731 #endif
732 }
733
734 if (psc->show_fps) {
735 show_fps(priv);
736 }
737
738 /* Old servers don't send invalidate events */
739 if (!pdp->invalidateAvailable)
740 dri2InvalidateBuffers(dpyPriv->dpy, pdraw->xDrawable);
741
742 return ret;
743 }
744
745 static __DRIbuffer *
746 dri2GetBuffers(__DRIdrawable * driDrawable,
747 int *width, int *height,
748 unsigned int *attachments, int count,
749 int *out_count, void *loaderPrivate)
750 {
751 struct dri2_drawable *pdraw = loaderPrivate;
752 DRI2Buffer *buffers;
753
754 buffers = DRI2GetBuffers(pdraw->base.psc->dpy, pdraw->base.xDrawable,
755 width, height, attachments, count, out_count);
756 if (buffers == NULL)
757 return NULL;
758
759 pdraw->width = *width;
760 pdraw->height = *height;
761 process_buffers(pdraw, buffers, *out_count);
762
763 free(buffers);
764
765 return pdraw->buffers;
766 }
767
768 static __DRIbuffer *
769 dri2GetBuffersWithFormat(__DRIdrawable * driDrawable,
770 int *width, int *height,
771 unsigned int *attachments, int count,
772 int *out_count, void *loaderPrivate)
773 {
774 struct dri2_drawable *pdraw = loaderPrivate;
775 DRI2Buffer *buffers;
776
777 buffers = DRI2GetBuffersWithFormat(pdraw->base.psc->dpy,
778 pdraw->base.xDrawable,
779 width, height, attachments,
780 count, out_count);
781 if (buffers == NULL)
782 return NULL;
783
784 pdraw->width = *width;
785 pdraw->height = *height;
786 process_buffers(pdraw, buffers, *out_count);
787
788 free(buffers);
789
790 return pdraw->buffers;
791 }
792
793 #ifdef X_DRI2SwapInterval
794
795 static int
796 dri2SetSwapInterval(__GLXDRIdrawable *pdraw, int interval)
797 {
798 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
799 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
800 struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
801
802 if (psc->config)
803 psc->config->configQueryi(psc->driScreen,
804 "vblank_mode", &vblank_mode);
805
806 switch (vblank_mode) {
807 case DRI_CONF_VBLANK_NEVER:
808 return GLX_BAD_VALUE;
809 case DRI_CONF_VBLANK_ALWAYS_SYNC:
810 if (interval <= 0)
811 return GLX_BAD_VALUE;
812 break;
813 default:
814 break;
815 }
816
817 DRI2SwapInterval(priv->base.psc->dpy, priv->base.xDrawable, interval);
818 priv->swap_interval = interval;
819
820 return 0;
821 }
822
823 static int
824 dri2GetSwapInterval(__GLXDRIdrawable *pdraw)
825 {
826 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
827
828 return priv->swap_interval;
829 }
830
831 #endif /* X_DRI2SwapInterval */
832
833 static const __DRIdri2LoaderExtension dri2LoaderExtension = {
834 {__DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION},
835 dri2GetBuffers,
836 dri2FlushFrontBuffer,
837 dri2GetBuffersWithFormat,
838 };
839
840 static const __DRIdri2LoaderExtension dri2LoaderExtension_old = {
841 {__DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION},
842 dri2GetBuffers,
843 dri2FlushFrontBuffer,
844 NULL,
845 };
846
847 #ifdef __DRI_USE_INVALIDATE
848 static const __DRIuseInvalidateExtension dri2UseInvalidate = {
849 { __DRI_USE_INVALIDATE, __DRI_USE_INVALIDATE_VERSION }
850 };
851 #endif
852
853 _X_HIDDEN void
854 dri2InvalidateBuffers(Display *dpy, XID drawable)
855 {
856 __GLXDRIdrawable *pdraw =
857 dri2GetGlxDrawableFromXDrawableId(dpy, drawable);
858 struct dri2_screen *psc;
859 struct dri2_drawable *pdp = (struct dri2_drawable *) pdraw;
860
861 if (!pdraw)
862 return;
863
864 psc = (struct dri2_screen *) pdraw->psc;
865
866 #if __DRI2_FLUSH_VERSION >= 3
867 if (pdraw && psc->f && psc->f->base.version >= 3 && psc->f->invalidate)
868 psc->f->invalidate(pdp->driDrawable);
869 #endif
870 }
871
872 static void
873 dri2_bind_tex_image(Display * dpy,
874 GLXDrawable drawable,
875 int buffer, const int *attrib_list)
876 {
877 struct glx_context *gc = __glXGetCurrentContext();
878 struct dri2_context *pcp = (struct dri2_context *) gc;
879 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
880 struct glx_display *dpyPriv = __glXInitialize(dpy);
881 struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
882 struct dri2_display *pdp =
883 (struct dri2_display *) dpyPriv->dri2Display;
884 struct dri2_screen *psc;
885
886 if (pdraw != NULL) {
887 psc = (struct dri2_screen *) base->psc;
888
889 #if __DRI2_FLUSH_VERSION >= 3
890 if (!pdp->invalidateAvailable && psc->f &&
891 psc->f->base.version >= 3 && psc->f->invalidate)
892 psc->f->invalidate(pdraw->driDrawable);
893 #endif
894
895 if (psc->texBuffer->base.version >= 2 &&
896 psc->texBuffer->setTexBuffer2 != NULL) {
897 (*psc->texBuffer->setTexBuffer2) (pcp->driContext,
898 pdraw->base.textureTarget,
899 pdraw->base.textureFormat,
900 pdraw->driDrawable);
901 }
902 else {
903 (*psc->texBuffer->setTexBuffer) (pcp->driContext,
904 pdraw->base.textureTarget,
905 pdraw->driDrawable);
906 }
907 }
908 }
909
910 static void
911 dri2_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer)
912 {
913 #if __DRI_TEX_BUFFER_VERSION >= 3
914 struct glx_context *gc = __glXGetCurrentContext();
915 struct dri2_context *pcp = (struct dri2_context *) gc;
916 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
917 struct glx_display *dpyPriv = __glXInitialize(dpy);
918 struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
919 struct dri2_display *pdp =
920 (struct dri2_display *) dpyPriv->dri2Display;
921 struct dri2_screen *psc;
922
923 if (pdraw != NULL) {
924 psc = (struct dri2_screen *) base->psc;
925
926 if (psc->texBuffer->base.version >= 3 &&
927 psc->texBuffer->releaseTexBuffer != NULL) {
928 (*psc->texBuffer->releaseTexBuffer) (pcp->driContext,
929 pdraw->base.textureTarget,
930 pdraw->driDrawable);
931 }
932 }
933 #endif
934 }
935
936 static const struct glx_context_vtable dri2_context_vtable = {
937 dri2_destroy_context,
938 dri2_bind_context,
939 dri2_unbind_context,
940 dri2_wait_gl,
941 dri2_wait_x,
942 DRI_glXUseXFont,
943 dri2_bind_tex_image,
944 dri2_release_tex_image,
945 NULL, /* get_proc_address */
946 };
947
948 static void
949 dri2BindExtensions(struct dri2_screen *psc, const __DRIextension **extensions)
950 {
951 int i;
952
953 __glXEnableDirectExtension(&psc->base, "GLX_SGI_video_sync");
954 __glXEnableDirectExtension(&psc->base, "GLX_SGI_swap_control");
955 __glXEnableDirectExtension(&psc->base, "GLX_MESA_swap_control");
956 __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
957
958 /* FIXME: if DRI2 version supports it... */
959 __glXEnableDirectExtension(&psc->base, "INTEL_swap_event");
960
961 if (psc->dri2->base.version >= 3) {
962 const unsigned mask = psc->dri2->getAPIMask(psc->driScreen);
963
964 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context");
965 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context_profile");
966
967 if ((mask & (1 << __DRI_API_GLES2)) != 0)
968 __glXEnableDirectExtension(&psc->base,
969 "GLX_EXT_create_context_es2_profile");
970 }
971
972 for (i = 0; extensions[i]; i++) {
973 if ((strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
974 psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
975 __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
976 }
977
978 if ((strcmp(extensions[i]->name, __DRI2_FLUSH) == 0)) {
979 psc->f = (__DRI2flushExtension *) extensions[i];
980 /* internal driver extension, no GL extension exposed */
981 }
982
983 if ((strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0))
984 psc->config = (__DRI2configQueryExtension *) extensions[i];
985
986 if (((strcmp(extensions[i]->name, __DRI2_THROTTLE) == 0)))
987 psc->throttle = (__DRI2throttleExtension *) extensions[i];
988
989 /* DRI2 version 3 is also required because
990 * GLX_ARB_create_context_robustness requires GLX_ARB_create_context.
991 */
992 if (psc->dri2->base.version >= 3
993 && strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0)
994 __glXEnableDirectExtension(&psc->base,
995 "GLX_ARB_create_context_robustness");
996 }
997 }
998
999 static const struct glx_screen_vtable dri2_screen_vtable = {
1000 dri2_create_context,
1001 dri2_create_context_attribs
1002 };
1003
1004 static struct glx_screen *
1005 dri2CreateScreen(int screen, struct glx_display * priv)
1006 {
1007 const __DRIconfig **driver_configs;
1008 const __DRIextension **extensions;
1009 const struct dri2_display *const pdp = (struct dri2_display *)
1010 priv->dri2Display;
1011 struct dri2_screen *psc;
1012 __GLXDRIscreen *psp;
1013 struct glx_config *configs = NULL, *visuals = NULL;
1014 char *driverName, *deviceName, *tmp;
1015 drm_magic_t magic;
1016 int i;
1017
1018 psc = calloc(1, sizeof *psc);
1019 if (psc == NULL)
1020 return NULL;
1021
1022 psc->fd = -1;
1023
1024 if (!glx_screen_init(&psc->base, screen, priv)) {
1025 free(psc);
1026 return NULL;
1027 }
1028
1029 if (!DRI2Connect(priv->dpy, RootWindow(priv->dpy, screen),
1030 &driverName, &deviceName)) {
1031 glx_screen_cleanup(&psc->base);
1032 free(psc);
1033 InfoMessageF("screen %d does not appear to be DRI2 capable\n", screen);
1034 return NULL;
1035 }
1036
1037 psc->driver = driOpenDriver(driverName);
1038 if (psc->driver == NULL) {
1039 ErrorMessageF("driver pointer missing\n");
1040 goto handle_error;
1041 }
1042
1043 extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
1044 if (extensions == NULL) {
1045 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
1046 goto handle_error;
1047 }
1048
1049 for (i = 0; extensions[i]; i++) {
1050 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
1051 psc->core = (__DRIcoreExtension *) extensions[i];
1052 if (strcmp(extensions[i]->name, __DRI_DRI2) == 0)
1053 psc->dri2 = (__DRIdri2Extension *) extensions[i];
1054 }
1055
1056 if (psc->core == NULL || psc->dri2 == NULL) {
1057 ErrorMessageF("core dri or dri2 extension not found\n");
1058 goto handle_error;
1059 }
1060
1061 #ifdef O_CLOEXEC
1062 psc->fd = open(deviceName, O_RDWR | O_CLOEXEC);
1063 if (psc->fd == -1 && errno == EINVAL)
1064 #endif
1065 {
1066 psc->fd = open(deviceName, O_RDWR);
1067 if (psc->fd != -1)
1068 fcntl(psc->fd, F_SETFD, fcntl(psc->fd, F_GETFD) | FD_CLOEXEC);
1069 }
1070 if (psc->fd < 0) {
1071 ErrorMessageF("failed to open drm device: %s\n", strerror(errno));
1072 goto handle_error;
1073 }
1074
1075 if (drmGetMagic(psc->fd, &magic)) {
1076 ErrorMessageF("failed to get magic\n");
1077 goto handle_error;
1078 }
1079
1080 if (!DRI2Authenticate(priv->dpy, RootWindow(priv->dpy, screen), magic)) {
1081 ErrorMessageF("failed to authenticate magic %d\n", magic);
1082 goto handle_error;
1083 }
1084
1085
1086 /* If the server does not support the protocol for
1087 * DRI2GetBuffersWithFormat, don't supply that interface to the driver.
1088 */
1089 psc->driScreen =
1090 psc->dri2->createNewScreen(screen, psc->fd,
1091 (const __DRIextension **)
1092 &pdp->loader_extensions[0],
1093 &driver_configs, psc);
1094
1095 if (psc->driScreen == NULL) {
1096 ErrorMessageF("failed to create dri screen\n");
1097 goto handle_error;
1098 }
1099
1100 extensions = psc->core->getExtensions(psc->driScreen);
1101 dri2BindExtensions(psc, extensions);
1102
1103 configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs);
1104 visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
1105
1106 if (!configs || !visuals)
1107 goto handle_error;
1108
1109 glx_config_destroy_list(psc->base.configs);
1110 psc->base.configs = configs;
1111 glx_config_destroy_list(psc->base.visuals);
1112 psc->base.visuals = visuals;
1113
1114 psc->driver_configs = driver_configs;
1115
1116 psc->base.vtable = &dri2_screen_vtable;
1117 psp = &psc->vtable;
1118 psc->base.driScreen = psp;
1119 psp->destroyScreen = dri2DestroyScreen;
1120 psp->createDrawable = dri2CreateDrawable;
1121 psp->swapBuffers = dri2SwapBuffers;
1122 psp->getDrawableMSC = NULL;
1123 psp->waitForMSC = NULL;
1124 psp->waitForSBC = NULL;
1125 psp->setSwapInterval = NULL;
1126 psp->getSwapInterval = NULL;
1127
1128 if (pdp->driMinor >= 2) {
1129 #ifdef X_DRI2GetMSC
1130 psp->getDrawableMSC = dri2DrawableGetMSC;
1131 #endif
1132 #ifdef X_DRI2WaitMSC
1133 psp->waitForMSC = dri2WaitForMSC;
1134 psp->waitForSBC = dri2WaitForSBC;
1135 #endif
1136 #ifdef X_DRI2SwapInterval
1137 psp->setSwapInterval = dri2SetSwapInterval;
1138 psp->getSwapInterval = dri2GetSwapInterval;
1139 #endif
1140 #if defined(X_DRI2GetMSC) && defined(X_DRI2WaitMSC) && defined(X_DRI2SwapInterval)
1141 __glXEnableDirectExtension(&psc->base, "GLX_OML_sync_control");
1142 #endif
1143 }
1144
1145 /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
1146 * available.*/
1147 psp->copySubBuffer = dri2CopySubBuffer;
1148 __glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer");
1149
1150 free(driverName);
1151 free(deviceName);
1152
1153 tmp = getenv("LIBGL_SHOW_FPS");
1154 psc->show_fps = tmp && strcmp(tmp, "1") == 0;
1155
1156 return &psc->base;
1157
1158 handle_error:
1159 CriticalErrorMessageF("failed to load driver: %s\n", driverName);
1160
1161 if (configs)
1162 glx_config_destroy_list(configs);
1163 if (visuals)
1164 glx_config_destroy_list(visuals);
1165 if (psc->driScreen)
1166 psc->core->destroyScreen(psc->driScreen);
1167 psc->driScreen = NULL;
1168 if (psc->fd >= 0)
1169 close(psc->fd);
1170 if (psc->driver)
1171 dlclose(psc->driver);
1172
1173 free(driverName);
1174 free(deviceName);
1175 glx_screen_cleanup(&psc->base);
1176 free(psc);
1177
1178 return NULL;
1179 }
1180
1181 /* Called from __glXFreeDisplayPrivate.
1182 */
1183 static void
1184 dri2DestroyDisplay(__GLXDRIdisplay * dpy)
1185 {
1186 struct dri2_display *pdp = (struct dri2_display *) dpy;
1187
1188 __glxHashDestroy(pdp->dri2Hash);
1189 free(dpy);
1190 }
1191
1192 _X_HIDDEN __GLXDRIdrawable *
1193 dri2GetGlxDrawableFromXDrawableId(Display *dpy, XID id)
1194 {
1195 struct glx_display *d = __glXInitialize(dpy);
1196 struct dri2_display *pdp = (struct dri2_display *) d->dri2Display;
1197 __GLXDRIdrawable *pdraw;
1198
1199 if (__glxHashLookup(pdp->dri2Hash, id, (void *) &pdraw) == 0)
1200 return pdraw;
1201
1202 return NULL;
1203 }
1204
1205 /*
1206 * Allocate, initialize and return a __DRIdisplayPrivate object.
1207 * This is called from __glXInitialize() when we are given a new
1208 * display pointer.
1209 */
1210 _X_HIDDEN __GLXDRIdisplay *
1211 dri2CreateDisplay(Display * dpy)
1212 {
1213 struct dri2_display *pdp;
1214 int eventBase, errorBase, i;
1215
1216 if (!DRI2QueryExtension(dpy, &eventBase, &errorBase))
1217 return NULL;
1218
1219 pdp = malloc(sizeof *pdp);
1220 if (pdp == NULL)
1221 return NULL;
1222
1223 if (!DRI2QueryVersion(dpy, &pdp->driMajor, &pdp->driMinor)) {
1224 free(pdp);
1225 return NULL;
1226 }
1227
1228 pdp->driPatch = 0;
1229 pdp->swapAvailable = (pdp->driMinor >= 2);
1230 pdp->invalidateAvailable = (pdp->driMinor >= 3);
1231
1232 pdp->base.destroyDisplay = dri2DestroyDisplay;
1233 pdp->base.createScreen = dri2CreateScreen;
1234
1235 i = 0;
1236 if (pdp->driMinor < 1)
1237 pdp->loader_extensions[i++] = &dri2LoaderExtension_old.base;
1238 else
1239 pdp->loader_extensions[i++] = &dri2LoaderExtension.base;
1240
1241 pdp->loader_extensions[i++] = &systemTimeExtension.base;
1242
1243 #ifdef __DRI_USE_INVALIDATE
1244 pdp->loader_extensions[i++] = &dri2UseInvalidate.base;
1245 #endif
1246 pdp->loader_extensions[i++] = NULL;
1247
1248 pdp->dri2Hash = __glxHashCreate();
1249 if (pdp->dri2Hash == NULL) {
1250 free(pdp);
1251 return NULL;
1252 }
1253
1254 return &pdp->base;
1255 }
1256
1257 #endif /* GLX_DIRECT_RENDERING */