1 /*
2 * Copyright © 2008 Red Hat, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Soft-
6 * ware"), to deal in the Software without restriction, including without
7 * limitation the rights to use, copy, modify, merge, publish, distribute,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, provided that the above copyright
10 * notice(s) and this permission notice appear in all copies of the Soft-
11 * ware and that both the above copyright notice(s) and this permission
12 * notice appear in supporting documentation.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16 * ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
17 * RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN
18 * THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSE-
19 * QUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFOR-
22 * MANCE OF THIS SOFTWARE.
23 *
24 * Except as contained in this notice, the name of a copyright holder shall
25 * not be used in advertising or otherwise to promote the sale, use or
26 * other dealings in this Software without prior written authorization of
27 * the copyright holder.
28 *
29 * Authors:
30 * Kristian Høgsberg (krh@redhat.com)
31 */
32
33 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
34
35 #include <X11/Xlib.h>
36 #include <X11/extensions/Xfixes.h>
37 #include <X11/Xlib-xcb.h>
38 #include <xcb/xcb.h>
39 #include <xcb/dri2.h>
40 #include "glxclient.h"
41 #include <X11/extensions/dri2proto.h>
42 #include <dlfcn.h>
43 #include <fcntl.h>
44 #include <unistd.h>
45 #include <sys/types.h>
46 #include <sys/mman.h>
47 #include <sys/time.h>
48 #include "dri2.h"
49 #include "dri_common.h"
50 #include "dri2_priv.h"
51 #include "loader.h"
52
53 /* From driconf.h, user exposed so should be stable */
54 #define DRI_CONF_VBLANK_NEVER 0
55 #define DRI_CONF_VBLANK_DEF_INTERVAL_0 1
56 #define DRI_CONF_VBLANK_DEF_INTERVAL_1 2
57 #define DRI_CONF_VBLANK_ALWAYS_SYNC 3
58
59 #undef DRI2_MINOR
60 #define DRI2_MINOR 1
61
62 struct dri2_display
63 {
64 __GLXDRIdisplay base;
65
66 /*
67 ** XFree86-DRI version information
68 */
69 int driMajor;
70 int driMinor;
71 int driPatch;
72 int swapAvailable;
73 int invalidateAvailable;
74
75 __glxHashTable *dri2Hash;
76
77 const __DRIextension *loader_extensions[5];
78 };
79
80 struct dri2_drawable
81 {
82 __GLXDRIdrawable base;
83 __DRIdrawable *driDrawable;
84 __DRIbuffer buffers[5];
85 int bufferCount;
86 int width, height;
87 int have_back;
88 int have_fake_front;
89 int swap_interval;
90
91 uint64_t previous_time;
92 unsigned frames;
93 };
94
95 static const struct glx_context_vtable dri2_context_vtable;
96
97 /* For XCB's handling of ust/msc/sbc counters, we have to hand it the high and
98 * low halves separately. This helps you split them.
99 */
100 static void
split_counter(uint64_t counter,uint32_t * hi,uint32_t * lo)101 split_counter(uint64_t counter, uint32_t *hi, uint32_t *lo)
102 {
103 *hi = (counter >> 32);
104 *lo = counter & 0xffffffff;
105 }
106
107 static uint64_t
merge_counter(uint32_t hi,uint32_t lo)108 merge_counter(uint32_t hi, uint32_t lo)
109 {
110 return ((uint64_t)hi << 32) | lo;
111 }
112
113 static void
dri2_destroy_context(struct glx_context * context)114 dri2_destroy_context(struct glx_context *context)
115 {
116 struct dri2_context *pcp = (struct dri2_context *) context;
117 struct dri2_screen *psc = (struct dri2_screen *) context->psc;
118
119 driReleaseDrawables(&pcp->base);
120
121 free((char *) context->extensions);
122
123 (*psc->core->destroyContext) (pcp->driContext);
124
125 free(pcp);
126 }
127
128 static Bool
dri2_bind_context(struct glx_context * context,struct glx_context * old,GLXDrawable draw,GLXDrawable read)129 dri2_bind_context(struct glx_context *context, struct glx_context *old,
130 GLXDrawable draw, GLXDrawable read)
131 {
132 struct dri2_context *pcp = (struct dri2_context *) context;
133 struct dri2_screen *psc = (struct dri2_screen *) pcp->base.psc;
134 struct dri2_drawable *pdraw, *pread;
135 __DRIdrawable *dri_draw = NULL, *dri_read = NULL;
136 struct glx_display *dpyPriv = psc->base.display;
137 struct dri2_display *pdp;
138
139 pdraw = (struct dri2_drawable *) driFetchDrawable(context, draw);
140 pread = (struct dri2_drawable *) driFetchDrawable(context, read);
141
142 driReleaseDrawables(&pcp->base);
143
144 if (pdraw)
145 dri_draw = pdraw->driDrawable;
146 else if (draw != None)
147 return GLXBadDrawable;
148
149 if (pread)
150 dri_read = pread->driDrawable;
151 else if (read != None)
152 return GLXBadDrawable;
153
154 if (!(*psc->core->bindContext) (pcp->driContext, dri_draw, dri_read))
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 *) dpyPriv->dri2Display;
161 if (!pdp->invalidateAvailable && pdraw) {
162 dri2InvalidateBuffers(psc->base.dpy, pdraw->base.xDrawable);
163 if (pread != pdraw && pread)
164 dri2InvalidateBuffers(psc->base.dpy, pread->base.xDrawable);
165 }
166
167 return Success;
168 }
169
170 static void
dri2_unbind_context(struct glx_context * context,struct glx_context * new)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 *
dri2_create_context_attribs(struct glx_screen * base,struct glx_config * config_base,struct glx_context * shareList,unsigned num_attribs,const uint32_t * attribs,unsigned * error)180 dri2_create_context_attribs(struct glx_screen *base,
181 struct glx_config *config_base,
182 struct glx_context *shareList,
183 unsigned num_attribs,
184 const uint32_t *attribs,
185 unsigned *error)
186 {
187 struct dri2_context *pcp = NULL;
188 struct dri2_context *pcp_shared = NULL;
189 struct dri2_screen *psc = (struct dri2_screen *) base;
190 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
191 __DRIcontext *shared = NULL;
192
193 struct dri_ctx_attribs dca;
194 uint32_t ctx_attribs[2 * 6];
195 unsigned num_ctx_attribs = 0;
196
197 *error = dri_convert_glx_attribs(num_attribs, attribs, &dca);
198 if (*error != __DRI_CTX_ERROR_SUCCESS)
199 goto error_exit;
200
201 /* Check the renderType value */
202 if (!validate_renderType_against_config(config_base, dca.render_type))
203 goto error_exit;
204
205 if (shareList) {
206 /* We can't share with an indirect context */
207 if (!shareList->isDirect)
208 return NULL;
209
210 /* The GLX_ARB_create_context_no_error specs say:
211 *
212 * BadMatch is generated if the value of GLX_CONTEXT_OPENGL_NO_ERROR_ARB
213 * used to create <share_context> does not match the value of
214 * GLX_CONTEXT_OPENGL_NO_ERROR_ARB for the context being created.
215 */
216 if (!!shareList->noError != !!dca.no_error) {
217 *error = __DRI_CTX_ERROR_BAD_FLAG;
218 return NULL;
219 }
220
221 pcp_shared = (struct dri2_context *) shareList;
222 shared = pcp_shared->driContext;
223 }
224
225 pcp = calloc(1, sizeof *pcp);
226 if (pcp == NULL) {
227 *error = __DRI_CTX_ERROR_NO_MEMORY;
228 goto error_exit;
229 }
230
231 if (!glx_context_init(&pcp->base, &psc->base, config_base))
232 goto error_exit;
233
234 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
235 ctx_attribs[num_ctx_attribs++] = dca.major_ver;
236 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
237 ctx_attribs[num_ctx_attribs++] = dca.minor_ver;
238
239 /* Only send a value when the non-default value is requested. By doing
240 * this we don't have to check the driver's DRI2 version before sending the
241 * default value.
242 */
243 if (dca.reset != __DRI_CTX_RESET_NO_NOTIFICATION) {
244 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
245 ctx_attribs[num_ctx_attribs++] = dca.reset;
246 }
247
248 if (dca.release != __DRI_CTX_RELEASE_BEHAVIOR_FLUSH) {
249 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_RELEASE_BEHAVIOR;
250 ctx_attribs[num_ctx_attribs++] = dca.release;
251 }
252
253 if (dca.no_error) {
254 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_NO_ERROR;
255 ctx_attribs[num_ctx_attribs++] = dca.no_error;
256 pcp->base.noError = GL_TRUE;
257 }
258
259 if (dca.flags != 0) {
260 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_FLAGS;
261 ctx_attribs[num_ctx_attribs++] = dca.flags;
262 }
263
264 /* The renderType is retrieved from attribs, or set to default
265 * of GLX_RGBA_TYPE.
266 */
267 pcp->base.renderType = dca.render_type;
268
269 pcp->driContext =
270 (*psc->dri2->createContextAttribs) (psc->driScreen,
271 dca.api,
272 config ? config->driConfig : NULL,
273 shared,
274 num_ctx_attribs / 2,
275 ctx_attribs,
276 error,
277 pcp);
278
279 if (pcp->driContext == NULL)
280 goto error_exit;
281
282 pcp->base.vtable = base->context_vtable;
283
284 return &pcp->base;
285
286 error_exit:
287 free(pcp);
288
289 return NULL;
290 }
291
292 static void
dri2DestroyDrawable(__GLXDRIdrawable * base)293 dri2DestroyDrawable(__GLXDRIdrawable *base)
294 {
295 struct dri2_screen *psc = (struct dri2_screen *) base->psc;
296 struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
297 struct glx_display *dpyPriv = psc->base.display;
298 struct dri2_display *pdp = (struct dri2_display *)dpyPriv->dri2Display;
299
300 __glxHashDelete(pdp->dri2Hash, pdraw->base.xDrawable);
301 (*psc->core->destroyDrawable) (pdraw->driDrawable);
302
303 /* If it's a GLX 1.3 drawables, we can destroy the DRI2 drawable
304 * now, as the application explicitly asked to destroy the GLX
305 * drawable. Otherwise, for legacy drawables, we let the DRI2
306 * drawable linger on the server, since there's no good way of
307 * knowing when the application is done with it. The server will
308 * destroy the DRI2 drawable when it destroys the X drawable or the
309 * client exits anyway. */
310 if (pdraw->base.xDrawable != pdraw->base.drawable)
311 DRI2DestroyDrawable(psc->base.dpy, pdraw->base.xDrawable);
312
313 free(pdraw);
314 }
315
316 static __GLXDRIdrawable *
dri2CreateDrawable(struct glx_screen * base,XID xDrawable,GLXDrawable drawable,int type,struct glx_config * config_base)317 dri2CreateDrawable(struct glx_screen *base, XID xDrawable,
318 GLXDrawable drawable, int type,
319 struct glx_config *config_base)
320 {
321 struct dri2_drawable *pdraw;
322 struct dri2_screen *psc = (struct dri2_screen *) base;
323 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
324 struct glx_display *dpyPriv;
325 struct dri2_display *pdp;
326 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
327
328 dpyPriv = __glXInitialize(psc->base.dpy);
329 if (dpyPriv == NULL)
330 return NULL;
331
332 pdraw = calloc(1, sizeof(*pdraw));
333 if (!pdraw)
334 return NULL;
335
336 pdraw->base.destroyDrawable = dri2DestroyDrawable;
337 pdraw->base.xDrawable = xDrawable;
338 pdraw->base.drawable = drawable;
339 pdraw->base.psc = &psc->base;
340 pdraw->bufferCount = 0;
341 pdraw->swap_interval = 1; /* default may be overridden below */
342 pdraw->have_back = 0;
343
344 if (psc->config)
345 psc->config->configQueryi(psc->driScreen,
346 "vblank_mode", &vblank_mode);
347
348 switch (vblank_mode) {
349 case DRI_CONF_VBLANK_NEVER:
350 case DRI_CONF_VBLANK_DEF_INTERVAL_0:
351 pdraw->swap_interval = 0;
352 break;
353 case DRI_CONF_VBLANK_DEF_INTERVAL_1:
354 case DRI_CONF_VBLANK_ALWAYS_SYNC:
355 default:
356 pdraw->swap_interval = 1;
357 break;
358 }
359
360 DRI2CreateDrawable(psc->base.dpy, xDrawable);
361 pdp = (struct dri2_display *)dpyPriv->dri2Display;
362 /* Create a new drawable */
363 pdraw->driDrawable =
364 (*psc->dri2->createNewDrawable) (psc->driScreen,
365 config->driConfig, pdraw);
366
367 if (!pdraw->driDrawable) {
368 DRI2DestroyDrawable(psc->base.dpy, xDrawable);
369 free(pdraw);
370 return NULL;
371 }
372
373 if (__glxHashInsert(pdp->dri2Hash, xDrawable, pdraw)) {
374 (*psc->core->destroyDrawable) (pdraw->driDrawable);
375 DRI2DestroyDrawable(psc->base.dpy, xDrawable);
376 free(pdraw);
377 return None;
378 }
379
380 /*
381 * Make sure server has the same swap interval we do for the new
382 * drawable.
383 */
384 if (psc->vtable.setSwapInterval)
385 psc->vtable.setSwapInterval(&pdraw->base, pdraw->swap_interval);
386
387 return &pdraw->base;
388 }
389
390 static int
dri2DrawableGetMSC(struct glx_screen * psc,__GLXDRIdrawable * pdraw,int64_t * ust,int64_t * msc,int64_t * sbc)391 dri2DrawableGetMSC(struct glx_screen *psc, __GLXDRIdrawable *pdraw,
392 int64_t *ust, int64_t *msc, int64_t *sbc)
393 {
394 xcb_connection_t *c = XGetXCBConnection(pdraw->psc->dpy);
395 xcb_dri2_get_msc_cookie_t get_msc_cookie;
396 xcb_dri2_get_msc_reply_t *get_msc_reply;
397
398 get_msc_cookie = xcb_dri2_get_msc_unchecked(c, pdraw->xDrawable);
399 get_msc_reply = xcb_dri2_get_msc_reply(c, get_msc_cookie, NULL);
400
401 if (!get_msc_reply)
402 return 0;
403
404 *ust = merge_counter(get_msc_reply->ust_hi, get_msc_reply->ust_lo);
405 *msc = merge_counter(get_msc_reply->msc_hi, get_msc_reply->msc_lo);
406 *sbc = merge_counter(get_msc_reply->sbc_hi, get_msc_reply->sbc_lo);
407 free(get_msc_reply);
408
409 return 1;
410 }
411
412 static int
dri2WaitForMSC(__GLXDRIdrawable * pdraw,int64_t target_msc,int64_t divisor,int64_t remainder,int64_t * ust,int64_t * msc,int64_t * sbc)413 dri2WaitForMSC(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
414 int64_t remainder, int64_t *ust, int64_t *msc, int64_t *sbc)
415 {
416 xcb_connection_t *c = XGetXCBConnection(pdraw->psc->dpy);
417 xcb_dri2_wait_msc_cookie_t wait_msc_cookie;
418 xcb_dri2_wait_msc_reply_t *wait_msc_reply;
419 uint32_t target_msc_hi, target_msc_lo;
420 uint32_t divisor_hi, divisor_lo;
421 uint32_t remainder_hi, remainder_lo;
422
423 split_counter(target_msc, &target_msc_hi, &target_msc_lo);
424 split_counter(divisor, &divisor_hi, &divisor_lo);
425 split_counter(remainder, &remainder_hi, &remainder_lo);
426
427 wait_msc_cookie = xcb_dri2_wait_msc_unchecked(c, pdraw->xDrawable,
428 target_msc_hi, target_msc_lo,
429 divisor_hi, divisor_lo,
430 remainder_hi, remainder_lo);
431 wait_msc_reply = xcb_dri2_wait_msc_reply(c, wait_msc_cookie, NULL);
432
433 if (!wait_msc_reply)
434 return 0;
435
436 *ust = merge_counter(wait_msc_reply->ust_hi, wait_msc_reply->ust_lo);
437 *msc = merge_counter(wait_msc_reply->msc_hi, wait_msc_reply->msc_lo);
438 *sbc = merge_counter(wait_msc_reply->sbc_hi, wait_msc_reply->sbc_lo);
439 free(wait_msc_reply);
440
441 return 1;
442 }
443
444 static int
dri2WaitForSBC(__GLXDRIdrawable * pdraw,int64_t target_sbc,int64_t * ust,int64_t * msc,int64_t * sbc)445 dri2WaitForSBC(__GLXDRIdrawable *pdraw, int64_t target_sbc, int64_t *ust,
446 int64_t *msc, int64_t *sbc)
447 {
448 xcb_connection_t *c = XGetXCBConnection(pdraw->psc->dpy);
449 xcb_dri2_wait_sbc_cookie_t wait_sbc_cookie;
450 xcb_dri2_wait_sbc_reply_t *wait_sbc_reply;
451 uint32_t target_sbc_hi, target_sbc_lo;
452
453 split_counter(target_sbc, &target_sbc_hi, &target_sbc_lo);
454
455 wait_sbc_cookie = xcb_dri2_wait_sbc_unchecked(c, pdraw->xDrawable,
456 target_sbc_hi, target_sbc_lo);
457 wait_sbc_reply = xcb_dri2_wait_sbc_reply(c, wait_sbc_cookie, NULL);
458
459 if (!wait_sbc_reply)
460 return 0;
461
462 *ust = merge_counter(wait_sbc_reply->ust_hi, wait_sbc_reply->ust_lo);
463 *msc = merge_counter(wait_sbc_reply->msc_hi, wait_sbc_reply->msc_lo);
464 *sbc = merge_counter(wait_sbc_reply->sbc_hi, wait_sbc_reply->sbc_lo);
465 free(wait_sbc_reply);
466
467 return 1;
468 }
469
470 static __DRIcontext *
dri2GetCurrentContext()471 dri2GetCurrentContext()
472 {
473 struct glx_context *gc = __glXGetCurrentContext();
474 struct dri2_context *dri2Ctx = (struct dri2_context *)gc;
475
476 return (gc != &dummyContext) ? dri2Ctx->driContext : NULL;
477 }
478
479 /**
480 * dri2Throttle - Request driver throttling
481 *
482 * This function uses the DRI2 throttle extension to give the
483 * driver the opportunity to throttle on flush front, copysubbuffer
484 * and swapbuffers.
485 */
486 static void
dri2Throttle(struct dri2_screen * psc,struct dri2_drawable * draw,enum __DRI2throttleReason reason)487 dri2Throttle(struct dri2_screen *psc,
488 struct dri2_drawable *draw,
489 enum __DRI2throttleReason reason)
490 {
491 if (psc->throttle) {
492 __DRIcontext *ctx = dri2GetCurrentContext();
493
494 psc->throttle->throttle(ctx, draw->driDrawable, reason);
495 }
496 }
497
498 /**
499 * Asks the driver to flush any queued work necessary for serializing with the
500 * X command stream, and optionally the slightly more strict requirement of
501 * glFlush() equivalence (which would require flushing even if nothing had
502 * been drawn to a window system framebuffer, for example).
503 */
504 static void
dri2Flush(struct dri2_screen * psc,__DRIcontext * ctx,struct dri2_drawable * draw,unsigned flags,enum __DRI2throttleReason throttle_reason)505 dri2Flush(struct dri2_screen *psc,
506 __DRIcontext *ctx,
507 struct dri2_drawable *draw,
508 unsigned flags,
509 enum __DRI2throttleReason throttle_reason)
510 {
511 if (ctx && psc->f && psc->f->base.version >= 4) {
512 psc->f->flush_with_flags(ctx, draw->driDrawable, flags, throttle_reason);
513 } else {
514 if (flags & __DRI2_FLUSH_CONTEXT)
515 glFlush();
516
517 if (psc->f)
518 psc->f->flush(draw->driDrawable);
519
520 dri2Throttle(psc, draw, throttle_reason);
521 }
522 }
523
524 static void
__dri2CopySubBuffer(__GLXDRIdrawable * pdraw,int x,int y,int width,int height,enum __DRI2throttleReason reason,Bool flush)525 __dri2CopySubBuffer(__GLXDRIdrawable *pdraw, int x, int y,
526 int width, int height,
527 enum __DRI2throttleReason reason, Bool flush)
528 {
529 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
530 struct dri2_screen *psc = (struct dri2_screen *) pdraw->psc;
531 XRectangle xrect;
532 XserverRegion region;
533 __DRIcontext *ctx = dri2GetCurrentContext();
534 unsigned flags;
535
536 /* Check we have the right attachments */
537 if (!priv->have_back)
538 return;
539
540 xrect.x = x;
541 xrect.y = priv->height - y - height;
542 xrect.width = width;
543 xrect.height = height;
544
545 flags = __DRI2_FLUSH_DRAWABLE;
546 if (flush)
547 flags |= __DRI2_FLUSH_CONTEXT;
548 dri2Flush(psc, ctx, priv, flags, __DRI2_THROTTLE_COPYSUBBUFFER);
549
550 region = XFixesCreateRegion(psc->base.dpy, &xrect, 1);
551 DRI2CopyRegion(psc->base.dpy, pdraw->xDrawable, region,
552 DRI2BufferFrontLeft, DRI2BufferBackLeft);
553
554 /* Refresh the fake front (if present) after we just damaged the real
555 * front.
556 */
557 if (priv->have_fake_front)
558 DRI2CopyRegion(psc->base.dpy, pdraw->xDrawable, region,
559 DRI2BufferFakeFrontLeft, DRI2BufferFrontLeft);
560
561 XFixesDestroyRegion(psc->base.dpy, region);
562 }
563
564 static void
dri2CopySubBuffer(__GLXDRIdrawable * pdraw,int x,int y,int width,int height,Bool flush)565 dri2CopySubBuffer(__GLXDRIdrawable *pdraw, int x, int y,
566 int width, int height, Bool flush)
567 {
568 __dri2CopySubBuffer(pdraw, x, y, width, height,
569 __DRI2_THROTTLE_COPYSUBBUFFER, flush);
570 }
571
572
573 static void
dri2_copy_drawable(struct dri2_drawable * priv,int dest,int src)574 dri2_copy_drawable(struct dri2_drawable *priv, int dest, int src)
575 {
576 XRectangle xrect;
577 XserverRegion region;
578 struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
579
580 xrect.x = 0;
581 xrect.y = 0;
582 xrect.width = priv->width;
583 xrect.height = priv->height;
584
585 if (psc->f)
586 (*psc->f->flush) (priv->driDrawable);
587
588 region = XFixesCreateRegion(psc->base.dpy, &xrect, 1);
589 DRI2CopyRegion(psc->base.dpy, priv->base.xDrawable, region, dest, src);
590 XFixesDestroyRegion(psc->base.dpy, region);
591
592 }
593
594 static void
dri2_wait_x(struct glx_context * gc)595 dri2_wait_x(struct glx_context *gc)
596 {
597 struct dri2_drawable *priv = (struct dri2_drawable *)
598 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
599
600 if (priv == NULL || !priv->have_fake_front)
601 return;
602
603 dri2_copy_drawable(priv, DRI2BufferFakeFrontLeft, DRI2BufferFrontLeft);
604 }
605
606 static void
dri2_wait_gl(struct glx_context * gc)607 dri2_wait_gl(struct glx_context *gc)
608 {
609 struct dri2_drawable *priv = (struct dri2_drawable *)
610 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
611
612 if (priv == NULL || !priv->have_fake_front)
613 return;
614
615 dri2_copy_drawable(priv, DRI2BufferFrontLeft, DRI2BufferFakeFrontLeft);
616 }
617
618 /**
619 * Called by the driver when it needs to update the real front buffer with the
620 * contents of its fake front buffer.
621 */
622 static void
dri2FlushFrontBuffer(__DRIdrawable * driDrawable,void * loaderPrivate)623 dri2FlushFrontBuffer(__DRIdrawable *driDrawable, void *loaderPrivate)
624 {
625 struct glx_display *priv;
626 struct dri2_display *pdp;
627 struct glx_context *gc;
628 struct dri2_drawable *pdraw = loaderPrivate;
629 struct dri2_screen *psc;
630
631 if (!pdraw)
632 return;
633
634 if (!pdraw->base.psc)
635 return;
636
637 psc = (struct dri2_screen *) pdraw->base.psc;
638
639 priv = __glXInitialize(psc->base.dpy);
640
641 if (priv == NULL)
642 return;
643
644 pdp = (struct dri2_display *) priv->dri2Display;
645 gc = __glXGetCurrentContext();
646
647 dri2Throttle(psc, pdraw, __DRI2_THROTTLE_FLUSHFRONT);
648
649 /* Old servers don't send invalidate events */
650 if (!pdp->invalidateAvailable)
651 dri2InvalidateBuffers(priv->dpy, pdraw->base.xDrawable);
652
653 dri2_wait_gl(gc);
654 }
655
656
657 static void
dri2DestroyScreen(struct glx_screen * base)658 dri2DestroyScreen(struct glx_screen *base)
659 {
660 struct dri2_screen *psc = (struct dri2_screen *) base;
661
662 /* Free the direct rendering per screen data */
663 (*psc->core->destroyScreen) (psc->driScreen);
664 driDestroyConfigs(psc->driver_configs);
665 free(psc->driverName);
666 close(psc->fd);
667 free(psc);
668 }
669
670 /**
671 * Process list of buffer received from the server
672 *
673 * Processes the list of buffers received in a reply from the server to either
674 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
675 */
676 static void
process_buffers(struct dri2_drawable * pdraw,DRI2Buffer * buffers,unsigned count)677 process_buffers(struct dri2_drawable * pdraw, DRI2Buffer * buffers,
678 unsigned count)
679 {
680 int i;
681
682 pdraw->bufferCount = count;
683 pdraw->have_fake_front = 0;
684 pdraw->have_back = 0;
685
686 /* This assumes the DRI2 buffer attachment tokens matches the
687 * __DRIbuffer tokens. */
688 for (i = 0; i < count; i++) {
689 pdraw->buffers[i].attachment = buffers[i].attachment;
690 pdraw->buffers[i].name = buffers[i].name;
691 pdraw->buffers[i].pitch = buffers[i].pitch;
692 pdraw->buffers[i].cpp = buffers[i].cpp;
693 pdraw->buffers[i].flags = buffers[i].flags;
694 if (pdraw->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)
695 pdraw->have_fake_front = 1;
696 if (pdraw->buffers[i].attachment == __DRI_BUFFER_BACK_LEFT)
697 pdraw->have_back = 1;
698 }
699
700 }
701
dri2GetSwapEventType(Display * dpy,XID drawable)702 unsigned dri2GetSwapEventType(Display* dpy, XID drawable)
703 {
704 struct glx_display *glx_dpy = __glXInitialize(dpy);
705 __GLXDRIdrawable *pdraw;
706 pdraw = dri2GetGlxDrawableFromXDrawableId(dpy, drawable);
707 if (!pdraw || !(pdraw->eventMask & GLX_BUFFER_SWAP_COMPLETE_INTEL_MASK))
708 return 0;
709 return glx_dpy->codes.first_event + GLX_BufferSwapComplete;
710 }
711
show_fps(struct dri2_drawable * draw)712 static void show_fps(struct dri2_drawable *draw)
713 {
714 const int interval =
715 ((struct dri2_screen *) draw->base.psc)->show_fps_interval;
716 struct timeval tv;
717 uint64_t current_time;
718
719 gettimeofday(&tv, NULL);
720 current_time = (uint64_t)tv.tv_sec*1000000 + (uint64_t)tv.tv_usec;
721
722 draw->frames++;
723
724 if (draw->previous_time + interval * 1000000 <= current_time) {
725 if (draw->previous_time) {
726 fprintf(stderr, "libGL: FPS = %.2f\n",
727 ((uint64_t)draw->frames * 1000000) /
728 (double)(current_time - draw->previous_time));
729 }
730 draw->frames = 0;
731 draw->previous_time = current_time;
732 }
733 }
734
735 static int64_t
dri2XcbSwapBuffers(Display * dpy,__GLXDRIdrawable * pdraw,int64_t target_msc,int64_t divisor,int64_t remainder)736 dri2XcbSwapBuffers(Display *dpy,
737 __GLXDRIdrawable *pdraw,
738 int64_t target_msc,
739 int64_t divisor,
740 int64_t remainder)
741 {
742 xcb_dri2_swap_buffers_cookie_t swap_buffers_cookie;
743 xcb_dri2_swap_buffers_reply_t *swap_buffers_reply;
744 uint32_t target_msc_hi, target_msc_lo;
745 uint32_t divisor_hi, divisor_lo;
746 uint32_t remainder_hi, remainder_lo;
747 int64_t ret = 0;
748 xcb_connection_t *c = XGetXCBConnection(dpy);
749
750 split_counter(target_msc, &target_msc_hi, &target_msc_lo);
751 split_counter(divisor, &divisor_hi, &divisor_lo);
752 split_counter(remainder, &remainder_hi, &remainder_lo);
753
754 swap_buffers_cookie =
755 xcb_dri2_swap_buffers_unchecked(c, pdraw->xDrawable,
756 target_msc_hi, target_msc_lo,
757 divisor_hi, divisor_lo,
758 remainder_hi, remainder_lo);
759
760 /* Immediately wait on the swapbuffers reply. If we didn't, we'd have
761 * to do so some time before reusing a (non-pageflipped) backbuffer.
762 * Otherwise, the new rendering could get ahead of the X Server's
763 * dispatch of the swapbuffer and you'd display garbage.
764 *
765 * We use XSync() first to reap the invalidate events through the event
766 * filter, to ensure that the next drawing doesn't use an invalidated
767 * buffer.
768 */
769 XSync(dpy, False);
770
771 swap_buffers_reply =
772 xcb_dri2_swap_buffers_reply(c, swap_buffers_cookie, NULL);
773 if (swap_buffers_reply) {
774 ret = merge_counter(swap_buffers_reply->swap_hi,
775 swap_buffers_reply->swap_lo);
776 free(swap_buffers_reply);
777 }
778 return ret;
779 }
780
781 static int64_t
dri2SwapBuffers(__GLXDRIdrawable * pdraw,int64_t target_msc,int64_t divisor,int64_t remainder,Bool flush)782 dri2SwapBuffers(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor,
783 int64_t remainder, Bool flush)
784 {
785 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
786 struct glx_display *dpyPriv = __glXInitialize(priv->base.psc->dpy);
787 struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
788 struct dri2_display *pdp =
789 (struct dri2_display *)dpyPriv->dri2Display;
790 int64_t ret = 0;
791
792 /* Check we have the right attachments */
793 if (!priv->have_back)
794 return ret;
795
796 /* Old servers can't handle swapbuffers */
797 if (!pdp->swapAvailable) {
798 __dri2CopySubBuffer(pdraw, 0, 0, priv->width, priv->height,
799 __DRI2_THROTTLE_SWAPBUFFER, flush);
800 } else {
801 __DRIcontext *ctx = dri2GetCurrentContext();
802 unsigned flags = __DRI2_FLUSH_DRAWABLE;
803 if (flush)
804 flags |= __DRI2_FLUSH_CONTEXT;
805 dri2Flush(psc, ctx, priv, flags, __DRI2_THROTTLE_SWAPBUFFER);
806
807 ret = dri2XcbSwapBuffers(pdraw->psc->dpy, pdraw,
808 target_msc, divisor, remainder);
809 }
810
811 if (psc->show_fps_interval) {
812 show_fps(priv);
813 }
814
815 /* Old servers don't send invalidate events */
816 if (!pdp->invalidateAvailable)
817 dri2InvalidateBuffers(dpyPriv->dpy, pdraw->xDrawable);
818
819 return ret;
820 }
821
822 static __DRIbuffer *
dri2GetBuffers(__DRIdrawable * driDrawable,int * width,int * height,unsigned int * attachments,int count,int * out_count,void * loaderPrivate)823 dri2GetBuffers(__DRIdrawable * driDrawable,
824 int *width, int *height,
825 unsigned int *attachments, int count,
826 int *out_count, void *loaderPrivate)
827 {
828 struct dri2_drawable *pdraw = loaderPrivate;
829 DRI2Buffer *buffers;
830
831 buffers = DRI2GetBuffers(pdraw->base.psc->dpy, pdraw->base.xDrawable,
832 width, height, attachments, count, out_count);
833 if (buffers == NULL)
834 return NULL;
835
836 pdraw->width = *width;
837 pdraw->height = *height;
838 process_buffers(pdraw, buffers, *out_count);
839
840 free(buffers);
841
842 return pdraw->buffers;
843 }
844
845 static __DRIbuffer *
dri2GetBuffersWithFormat(__DRIdrawable * driDrawable,int * width,int * height,unsigned int * attachments,int count,int * out_count,void * loaderPrivate)846 dri2GetBuffersWithFormat(__DRIdrawable * driDrawable,
847 int *width, int *height,
848 unsigned int *attachments, int count,
849 int *out_count, void *loaderPrivate)
850 {
851 struct dri2_drawable *pdraw = loaderPrivate;
852 DRI2Buffer *buffers;
853
854 buffers = DRI2GetBuffersWithFormat(pdraw->base.psc->dpy,
855 pdraw->base.xDrawable,
856 width, height, attachments,
857 count, out_count);
858 if (buffers == NULL)
859 return NULL;
860
861 pdraw->width = *width;
862 pdraw->height = *height;
863 process_buffers(pdraw, buffers, *out_count);
864
865 free(buffers);
866
867 return pdraw->buffers;
868 }
869
870 static int
dri2SetSwapInterval(__GLXDRIdrawable * pdraw,int interval)871 dri2SetSwapInterval(__GLXDRIdrawable *pdraw, int interval)
872 {
873 xcb_connection_t *c = XGetXCBConnection(pdraw->psc->dpy);
874 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
875 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
876 struct dri2_screen *psc = (struct dri2_screen *) priv->base.psc;
877
878 if (psc->config)
879 psc->config->configQueryi(psc->driScreen,
880 "vblank_mode", &vblank_mode);
881
882 switch (vblank_mode) {
883 case DRI_CONF_VBLANK_NEVER:
884 if (interval != 0)
885 return GLX_BAD_VALUE;
886 break;
887 case DRI_CONF_VBLANK_ALWAYS_SYNC:
888 if (interval <= 0)
889 return GLX_BAD_VALUE;
890 break;
891 default:
892 break;
893 }
894
895 xcb_dri2_swap_interval(c, priv->base.xDrawable, interval);
896 priv->swap_interval = interval;
897
898 return 0;
899 }
900
901 static int
dri2GetSwapInterval(__GLXDRIdrawable * pdraw)902 dri2GetSwapInterval(__GLXDRIdrawable *pdraw)
903 {
904 struct dri2_drawable *priv = (struct dri2_drawable *) pdraw;
905
906 return priv->swap_interval;
907 }
908
909 static void
driSetBackgroundContext(void * loaderPrivate)910 driSetBackgroundContext(void *loaderPrivate)
911 {
912 struct dri2_context *pcp = (struct dri2_context *) loaderPrivate;
913 __glXSetCurrentContext(&pcp->base);
914 }
915
916 static GLboolean
driIsThreadSafe(void * loaderPrivate)917 driIsThreadSafe(void *loaderPrivate)
918 {
919 struct dri2_context *pcp = (struct dri2_context *) loaderPrivate;
920 /* Check Xlib is running in thread safe mode
921 *
922 * 'lock_fns' is the XLockDisplay function pointer of the X11 display 'dpy'.
923 * It wll be NULL if XInitThreads wasn't called.
924 */
925 return pcp->base.psc->dpy->lock_fns != NULL;
926 }
927
928 static const __DRIdri2LoaderExtension dri2LoaderExtension = {
929 .base = { __DRI_DRI2_LOADER, 3 },
930
931 .getBuffers = dri2GetBuffers,
932 .flushFrontBuffer = dri2FlushFrontBuffer,
933 .getBuffersWithFormat = dri2GetBuffersWithFormat,
934 };
935
936 static const __DRIdri2LoaderExtension dri2LoaderExtension_old = {
937 .base = { __DRI_DRI2_LOADER, 3 },
938
939 .getBuffers = dri2GetBuffers,
940 .flushFrontBuffer = dri2FlushFrontBuffer,
941 .getBuffersWithFormat = NULL,
942 };
943
944 static const __DRIuseInvalidateExtension dri2UseInvalidate = {
945 .base = { __DRI_USE_INVALIDATE, 1 }
946 };
947
948 static const __DRIbackgroundCallableExtension driBackgroundCallable = {
949 .base = { __DRI_BACKGROUND_CALLABLE, 2 },
950
951 .setBackgroundContext = driSetBackgroundContext,
952 .isThreadSafe = driIsThreadSafe,
953 };
954
955 _X_HIDDEN void
dri2InvalidateBuffers(Display * dpy,XID drawable)956 dri2InvalidateBuffers(Display *dpy, XID drawable)
957 {
958 __GLXDRIdrawable *pdraw =
959 dri2GetGlxDrawableFromXDrawableId(dpy, drawable);
960 struct dri2_screen *psc;
961 struct dri2_drawable *pdp = (struct dri2_drawable *) pdraw;
962
963 if (!pdraw)
964 return;
965
966 psc = (struct dri2_screen *) pdraw->psc;
967
968 if (psc->f && psc->f->base.version >= 3 && psc->f->invalidate)
969 psc->f->invalidate(pdp->driDrawable);
970 }
971
972 static void
dri2_bind_tex_image(__GLXDRIdrawable * base,int buffer,const int * attrib_list)973 dri2_bind_tex_image(__GLXDRIdrawable *base,
974 int buffer, const int *attrib_list)
975 {
976 struct glx_context *gc = __glXGetCurrentContext();
977 struct dri2_context *pcp = (struct dri2_context *) gc;
978 struct glx_display *dpyPriv = __glXInitialize(gc->currentDpy);
979 struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
980 struct dri2_display *pdp;
981 struct dri2_screen *psc;
982
983 pdp = (struct dri2_display *) dpyPriv->dri2Display;
984
985 if (pdraw != NULL) {
986 psc = (struct dri2_screen *) base->psc;
987
988 if (!pdp->invalidateAvailable && psc->f &&
989 psc->f->base.version >= 3 && psc->f->invalidate)
990 psc->f->invalidate(pdraw->driDrawable);
991
992 if (psc->texBuffer->base.version >= 2 &&
993 psc->texBuffer->setTexBuffer2 != NULL) {
994 (*psc->texBuffer->setTexBuffer2) (pcp->driContext,
995 pdraw->base.textureTarget,
996 pdraw->base.textureFormat,
997 pdraw->driDrawable);
998 }
999 else {
1000 (*psc->texBuffer->setTexBuffer) (pcp->driContext,
1001 pdraw->base.textureTarget,
1002 pdraw->driDrawable);
1003 }
1004 }
1005 }
1006
1007 static void
dri2_release_tex_image(__GLXDRIdrawable * base,int buffer)1008 dri2_release_tex_image(__GLXDRIdrawable *base, int buffer)
1009 {
1010 struct glx_context *gc = __glXGetCurrentContext();
1011 struct dri2_context *pcp = (struct dri2_context *) gc;
1012 struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
1013 struct dri2_screen *psc;
1014
1015 if (pdraw != NULL) {
1016 psc = (struct dri2_screen *) base->psc;
1017
1018 if (psc->texBuffer->base.version >= 3 &&
1019 psc->texBuffer->releaseTexBuffer != NULL) {
1020 (*psc->texBuffer->releaseTexBuffer) (pcp->driContext,
1021 pdraw->base.textureTarget,
1022 pdraw->driDrawable);
1023 }
1024 }
1025 }
1026
1027 static const struct glx_context_vtable dri2_context_vtable = {
1028 .destroy = dri2_destroy_context,
1029 .bind = dri2_bind_context,
1030 .unbind = dri2_unbind_context,
1031 .wait_gl = dri2_wait_gl,
1032 .wait_x = dri2_wait_x,
1033 .interop_query_device_info = dri2_interop_query_device_info,
1034 .interop_export_object = dri2_interop_export_object
1035 };
1036
1037 static void
dri2BindExtensions(struct dri2_screen * psc,struct glx_display * priv,const char * driverName)1038 dri2BindExtensions(struct dri2_screen *psc, struct glx_display * priv,
1039 const char *driverName)
1040 {
1041 const struct dri2_display *const pdp = (struct dri2_display *)
1042 priv->dri2Display;
1043 const unsigned mask = psc->dri2->getAPIMask(psc->driScreen);
1044 const __DRIextension **extensions;
1045 int i;
1046
1047 extensions = psc->core->getExtensions(psc->driScreen);
1048
1049 __glXEnableDirectExtension(&psc->base, "GLX_EXT_swap_control");
1050 __glXEnableDirectExtension(&psc->base, "GLX_SGI_swap_control");
1051 __glXEnableDirectExtension(&psc->base, "GLX_MESA_swap_control");
1052 __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
1053
1054 /*
1055 * GLX_INTEL_swap_event is broken on the server side, where it's
1056 * currently unconditionally enabled. This completely breaks
1057 * systems running on drivers which don't support that extension.
1058 * There's no way to test for its presence on this side, so instead
1059 * of disabling it unconditionally, just disable it for drivers
1060 * which are known to not support it, or for DDX drivers supporting
1061 * only an older (pre-ScheduleSwap) version of DRI2.
1062 *
1063 * This is a hack which is required until:
1064 * http://lists.x.org/archives/xorg-devel/2013-February/035449.html
1065 * is merged and updated xserver makes it's way into distros:
1066 */
1067 if (pdp->swapAvailable && strcmp(driverName, "vmwgfx") != 0) {
1068 __glXEnableDirectExtension(&psc->base, "GLX_INTEL_swap_event");
1069 }
1070
1071 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context");
1072 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context_profile");
1073 __glXEnableDirectExtension(&psc->base, "GLX_EXT_no_config_context");
1074
1075 if ((mask & ((1 << __DRI_API_GLES) |
1076 (1 << __DRI_API_GLES2) |
1077 (1 << __DRI_API_GLES3))) != 0) {
1078 __glXEnableDirectExtension(&psc->base,
1079 "GLX_EXT_create_context_es_profile");
1080 __glXEnableDirectExtension(&psc->base,
1081 "GLX_EXT_create_context_es2_profile");
1082 }
1083
1084 for (i = 0; extensions[i]; i++) {
1085 if ((strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
1086 psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
1087 __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
1088 }
1089
1090 if ((strcmp(extensions[i]->name, __DRI2_FLUSH) == 0)) {
1091 psc->f = (__DRI2flushExtension *) extensions[i];
1092 /* internal driver extension, no GL extension exposed */
1093 }
1094
1095 if ((strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0))
1096 psc->config = (__DRI2configQueryExtension *) extensions[i];
1097
1098 if (((strcmp(extensions[i]->name, __DRI2_THROTTLE) == 0)))
1099 psc->throttle = (__DRI2throttleExtension *) extensions[i];
1100
1101 if (strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0)
1102 __glXEnableDirectExtension(&psc->base,
1103 "GLX_ARB_create_context_robustness");
1104
1105 if (strcmp(extensions[i]->name, __DRI2_RENDERER_QUERY) == 0) {
1106 psc->rendererQuery = (__DRI2rendererQueryExtension *) extensions[i];
1107 __glXEnableDirectExtension(&psc->base, "GLX_MESA_query_renderer");
1108 unsigned int no_error = 0;
1109 if (psc->rendererQuery->queryInteger(psc->driScreen,
1110 __DRI2_RENDERER_HAS_NO_ERROR_CONTEXT,
1111 &no_error) == 0 && no_error)
1112 __glXEnableDirectExtension(&psc->base,
1113 "GLX_ARB_create_context_no_error");
1114 }
1115
1116 if (strcmp(extensions[i]->name, __DRI2_INTEROP) == 0)
1117 psc->interop = (__DRI2interopExtension*)extensions[i];
1118
1119 if (strcmp(extensions[i]->name, __DRI2_FLUSH_CONTROL) == 0)
1120 __glXEnableDirectExtension(&psc->base,
1121 "GLX_ARB_context_flush_control");
1122 }
1123 }
1124
1125 static char *
dri2_get_driver_name(struct glx_screen * glx_screen)1126 dri2_get_driver_name(struct glx_screen *glx_screen)
1127 {
1128 struct dri2_screen *psc = (struct dri2_screen *)glx_screen;
1129
1130 return psc->driverName;
1131 }
1132
1133 static const struct glx_screen_vtable dri2_screen_vtable = {
1134 .create_context = dri_common_create_context,
1135 .create_context_attribs = dri2_create_context_attribs,
1136 .query_renderer_integer = dri2_query_renderer_integer,
1137 .query_renderer_string = dri2_query_renderer_string,
1138 .get_driver_name = dri2_get_driver_name,
1139 };
1140
1141 static struct glx_screen *
dri2CreateScreen(int screen,struct glx_display * priv)1142 dri2CreateScreen(int screen, struct glx_display * priv)
1143 {
1144 const __DRIconfig **driver_configs;
1145 const __DRIextension **extensions;
1146 const struct dri2_display *const pdp = (struct dri2_display *)
1147 priv->dri2Display;
1148 struct dri2_screen *psc;
1149 __GLXDRIscreen *psp;
1150 struct glx_config *configs = NULL, *visuals = NULL;
1151 char *driverName = NULL, *loader_driverName, *deviceName, *tmp;
1152 drm_magic_t magic;
1153 int i;
1154
1155 psc = calloc(1, sizeof *psc);
1156 if (psc == NULL)
1157 return NULL;
1158
1159 psc->fd = -1;
1160
1161 if (!glx_screen_init(&psc->base, screen, priv)) {
1162 free(psc);
1163 return NULL;
1164 }
1165
1166 if (!DRI2Connect(priv->dpy, RootWindow(priv->dpy, screen),
1167 &driverName, &deviceName)) {
1168 glx_screen_cleanup(&psc->base);
1169 free(psc);
1170 InfoMessageF("screen %d does not appear to be DRI2 capable\n", screen);
1171 return NULL;
1172 }
1173
1174 psc->fd = loader_open_device(deviceName);
1175 if (psc->fd < 0) {
1176 ErrorMessageF("failed to open %s: %s\n", deviceName, strerror(errno));
1177 goto handle_error;
1178 }
1179
1180 if (drmGetMagic(psc->fd, &magic)) {
1181 ErrorMessageF("failed to get magic\n");
1182 goto handle_error;
1183 }
1184
1185 if (!DRI2Authenticate(priv->dpy, RootWindow(priv->dpy, screen), magic)) {
1186 ErrorMessageF("failed to authenticate magic %d\n", magic);
1187 goto handle_error;
1188 }
1189
1190 /* If Mesa knows about the appropriate driver for this fd, then trust it.
1191 * Otherwise, default to the server's value.
1192 */
1193 loader_driverName = loader_get_driver_for_fd(psc->fd);
1194 if (loader_driverName) {
1195 free(driverName);
1196 driverName = loader_driverName;
1197 }
1198 psc->driverName = driverName;
1199
1200 extensions = driOpenDriver(driverName, &psc->driver);
1201 if (extensions == NULL)
1202 goto handle_error;
1203
1204 for (i = 0; extensions[i]; i++) {
1205 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
1206 psc->core = (__DRIcoreExtension *) extensions[i];
1207 if (strcmp(extensions[i]->name, __DRI_DRI2) == 0)
1208 psc->dri2 = (__DRIdri2Extension *) extensions[i];
1209 }
1210
1211 if (psc->core == NULL || psc->dri2 == NULL || psc->dri2->base.version < 3) {
1212 ErrorMessageF("core dri or dri2 extension not found\n");
1213 goto handle_error;
1214 }
1215
1216 if (psc->dri2->base.version >= 4) {
1217 psc->driScreen =
1218 psc->dri2->createNewScreen2(screen, psc->fd,
1219 (const __DRIextension **)
1220 &pdp->loader_extensions[0],
1221 extensions,
1222 &driver_configs, psc);
1223 } else {
1224 psc->driScreen =
1225 psc->dri2->createNewScreen(screen, psc->fd,
1226 (const __DRIextension **)
1227 &pdp->loader_extensions[0],
1228 &driver_configs, psc);
1229 }
1230
1231 if (psc->driScreen == NULL) {
1232 ErrorMessageF("glx: failed to create dri2 screen\n");
1233 goto handle_error;
1234 }
1235
1236 dri2BindExtensions(psc, priv, driverName);
1237
1238 configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs);
1239 visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
1240
1241 if (!configs || !visuals) {
1242 ErrorMessageF("No matching fbConfigs or visuals found\n");
1243 goto handle_error;
1244 }
1245
1246 glx_config_destroy_list(psc->base.configs);
1247 psc->base.configs = configs;
1248 glx_config_destroy_list(psc->base.visuals);
1249 psc->base.visuals = visuals;
1250
1251 psc->driver_configs = driver_configs;
1252
1253 psc->base.vtable = &dri2_screen_vtable;
1254 psc->base.context_vtable = &dri2_context_vtable;
1255 psp = &psc->vtable;
1256 psc->base.driScreen = psp;
1257 psp->destroyScreen = dri2DestroyScreen;
1258 psp->createDrawable = dri2CreateDrawable;
1259 psp->swapBuffers = dri2SwapBuffers;
1260 psp->getDrawableMSC = NULL;
1261 psp->waitForMSC = NULL;
1262 psp->waitForSBC = NULL;
1263 psp->setSwapInterval = NULL;
1264 psp->getSwapInterval = NULL;
1265 psp->getBufferAge = NULL;
1266 psp->bindTexImage = dri2_bind_tex_image;
1267 psp->releaseTexImage = dri2_release_tex_image;
1268
1269 if (pdp->driMinor >= 2) {
1270 psp->getDrawableMSC = dri2DrawableGetMSC;
1271 psp->waitForMSC = dri2WaitForMSC;
1272 psp->waitForSBC = dri2WaitForSBC;
1273 psp->setSwapInterval = dri2SetSwapInterval;
1274 psp->getSwapInterval = dri2GetSwapInterval;
1275
1276 __glXEnableDirectExtension(&psc->base, "GLX_OML_sync_control");
1277 }
1278
1279 __glXEnableDirectExtension(&psc->base, "GLX_SGI_video_sync");
1280
1281 if (psc->config->base.version > 1 &&
1282 psc->config->configQuerys(psc->driScreen, "glx_extension_override",
1283 &tmp) == 0)
1284 __glXParseExtensionOverride(&psc->base, tmp);
1285
1286 if (psc->config->base.version > 1 &&
1287 psc->config->configQuerys(psc->driScreen,
1288 "indirect_gl_extension_override",
1289 &tmp) == 0)
1290 __IndirectGlParseExtensionOverride(&psc->base, tmp);
1291
1292 if (psc->config->base.version > 1) {
1293 uint8_t force = false;
1294 if (psc->config->configQueryb(psc->driScreen, "force_direct_glx_context",
1295 &force) == 0) {
1296 psc->base.force_direct_context = force;
1297 }
1298
1299 uint8_t invalid_glx_destroy_window = false;
1300 if (psc->config->configQueryb(psc->driScreen,
1301 "allow_invalid_glx_destroy_window",
1302 &invalid_glx_destroy_window) == 0) {
1303 psc->base.allow_invalid_glx_destroy_window = invalid_glx_destroy_window;
1304 }
1305 }
1306
1307 /* DRI2 supports SubBuffer through DRI2CopyRegion, so it's always
1308 * available.*/
1309 psp->copySubBuffer = dri2CopySubBuffer;
1310 __glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer");
1311
1312 free(deviceName);
1313
1314 tmp = getenv("LIBGL_SHOW_FPS");
1315 psc->show_fps_interval = (tmp) ? atoi(tmp) : 0;
1316 if (psc->show_fps_interval < 0)
1317 psc->show_fps_interval = 0;
1318
1319 InfoMessageF("Using DRI2 for screen %d\n", screen);
1320
1321 return &psc->base;
1322
1323 handle_error:
1324 CriticalErrorMessageF("failed to load driver: %s\n", driverName);
1325
1326 if (configs)
1327 glx_config_destroy_list(configs);
1328 if (visuals)
1329 glx_config_destroy_list(visuals);
1330 if (psc->driScreen)
1331 psc->core->destroyScreen(psc->driScreen);
1332 psc->driScreen = NULL;
1333 if (psc->fd >= 0)
1334 close(psc->fd);
1335 if (psc->driver)
1336 dlclose(psc->driver);
1337
1338 free(deviceName);
1339 glx_screen_cleanup(&psc->base);
1340 free(psc);
1341
1342 return NULL;
1343 }
1344
1345 /* Called from __glXFreeDisplayPrivate.
1346 */
1347 static void
dri2DestroyDisplay(__GLXDRIdisplay * dpy)1348 dri2DestroyDisplay(__GLXDRIdisplay * dpy)
1349 {
1350 struct dri2_display *pdp = (struct dri2_display *) dpy;
1351
1352 __glxHashDestroy(pdp->dri2Hash);
1353 free(dpy);
1354 }
1355
1356 _X_HIDDEN __GLXDRIdrawable *
dri2GetGlxDrawableFromXDrawableId(Display * dpy,XID id)1357 dri2GetGlxDrawableFromXDrawableId(Display *dpy, XID id)
1358 {
1359 struct glx_display *d = __glXInitialize(dpy);
1360 struct dri2_display *pdp = (struct dri2_display *) d->dri2Display;
1361 __GLXDRIdrawable *pdraw;
1362
1363 if (__glxHashLookup(pdp->dri2Hash, id, (void *) &pdraw) == 0)
1364 return pdraw;
1365
1366 return NULL;
1367 }
1368
1369 /*
1370 * Allocate, initialize and return a __DRIdisplayPrivate object.
1371 * This is called from __glXInitialize() when we are given a new
1372 * display pointer.
1373 */
1374 _X_HIDDEN __GLXDRIdisplay *
dri2CreateDisplay(Display * dpy)1375 dri2CreateDisplay(Display * dpy)
1376 {
1377 struct dri2_display *pdp;
1378 int eventBase, errorBase, i;
1379
1380 if (!DRI2QueryExtension(dpy, &eventBase, &errorBase))
1381 return NULL;
1382
1383 pdp = malloc(sizeof *pdp);
1384 if (pdp == NULL)
1385 return NULL;
1386
1387 if (!DRI2QueryVersion(dpy, &pdp->driMajor, &pdp->driMinor)) {
1388 free(pdp);
1389 return NULL;
1390 }
1391
1392 pdp->driPatch = 0;
1393 pdp->swapAvailable = (pdp->driMinor >= 2);
1394 pdp->invalidateAvailable = (pdp->driMinor >= 3);
1395
1396 pdp->base.destroyDisplay = dri2DestroyDisplay;
1397 pdp->base.createScreen = dri2CreateScreen;
1398
1399 i = 0;
1400 if (pdp->driMinor < 1)
1401 pdp->loader_extensions[i++] = &dri2LoaderExtension_old.base;
1402 else
1403 pdp->loader_extensions[i++] = &dri2LoaderExtension.base;
1404
1405 pdp->loader_extensions[i++] = &dri2UseInvalidate.base;
1406
1407 pdp->loader_extensions[i++] = &driBackgroundCallable.base;
1408
1409 pdp->loader_extensions[i++] = NULL;
1410
1411 pdp->dri2Hash = __glxHashCreate();
1412 if (pdp->dri2Hash == NULL) {
1413 free(pdp);
1414 return NULL;
1415 }
1416
1417 return &pdp->base;
1418 }
1419
1420 #endif /* GLX_DIRECT_RENDERING */
1421