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