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