dri3_glx.c revision 993e1d59
11.28Sthorpej/* 21.1Snisimura * Copyright © 2013 Keith Packard 31.1Snisimura * 41.18Srmind * Permission to use, copy, modify, distribute, and sell this software and its 51.1Snisimura * documentation for any purpose is hereby granted without fee, provided that 61.1Snisimura * the above copyright notice appear in all copies and that both that copyright 71.9Sagc * notice and this permission notice appear in supporting documentation, and 81.9Sagc * that the name of the copyright holders not be used in advertising or 91.9Sagc * publicity pertaining to distribution of the software without specific, 101.9Sagc * written prior permission. The copyright holders make no representations 111.9Sagc * about the suitability of this software for any purpose. It is provided "as 121.9Sagc * is" without express or implied warranty. 131.9Sagc * 141.9Sagc * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 151.9Sagc * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 161.9Sagc * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 171.9Sagc * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 181.9Sagc * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 191.9Sagc * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 201.9Sagc * OF THIS SOFTWARE. 211.9Sagc */ 221.9Sagc 231.9Sagc/* 241.9Sagc * Portions of this code were adapted from dri2_glx.c which carries the 251.9Sagc * following copyright: 261.9Sagc * 271.9Sagc * Copyright © 2008 Red Hat, Inc. 281.9Sagc * 291.9Sagc * Permission is hereby granted, free of charge, to any person obtaining a 301.9Sagc * copy of this software and associated documentation files (the "Soft- 311.9Sagc * ware"), to deal in the Software without restriction, including without 321.9Sagc * limitation the rights to use, copy, modify, merge, publish, distribute, 331.9Sagc * and/or sell copies of the Software, and to permit persons to whom the 341.9Sagc * Software is furnished to do so, provided that the above copyright 351.9Sagc * notice(s) and this permission notice appear in all copies of the Soft- 361.9Sagc * ware and that both the above copyright notice(s) and this permission 371.9Sagc * notice appear in supporting documentation. 381.9Sagc * 391.9Sagc * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 401.1Snisimura * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- 411.1Snisimura * ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY 421.1Snisimura * RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN 431.7Sthorpej * THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSE- 441.7Sthorpej * QUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 451.23Stsutsui * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 461.23Stsutsui * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFOR- 471.23Stsutsui * MANCE OF THIS SOFTWARE. 481.1Snisimura * 491.1Snisimura * Except as contained in this notice, the name of a copyright holder shall 501.6Sthorpej * not be used in advertising or otherwise to promote the sale, use or 511.1Snisimura * other dealings in this Software without prior written authorization of 521.1Snisimura * the copyright holder. 531.1Snisimura * 541.1Snisimura * Authors: 551.1Snisimura * Kristian Høgsberg (krh@redhat.com) 561.1Snisimura */ 571.1Snisimura 581.21Srin#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL) 591.1Snisimura 601.1Snisimura#include <X11/Xlib.h> 611.1Snisimura#include <X11/extensions/Xfixes.h> 621.1Snisimura#include <X11/Xlib-xcb.h> 631.1Snisimura#include <X11/xshmfence.h> 641.1Snisimura#include <xcb/xcb.h> 651.1Snisimura#include <xcb/dri3.h> 661.1Snisimura#include <xcb/present.h> 671.26Stsutsui#include <GL/gl.h> 681.1Snisimura#include "glxclient.h" 691.1Snisimura#include <dlfcn.h> 701.1Snisimura#include <fcntl.h> 711.1Snisimura#include <unistd.h> 721.1Snisimura#include <sys/types.h> 731.1Snisimura#include <sys/mman.h> 741.1Snisimura#include <sys/time.h> 751.1Snisimura 761.1Snisimura#include "dri_common.h" 771.1Snisimura#include "dri3_priv.h" 781.1Snisimura#include "loader.h" 791.1Snisimura#include "dri2.h" 801.1Snisimura 811.24Stsutsuistatic struct dri3_drawable * 821.24Stsutsuiloader_drawable_to_dri3_drawable(struct loader_dri3_drawable *draw) { 831.1Snisimura size_t offset = offsetof(struct dri3_drawable, loader_drawable); 841.1Snisimura if (!draw) 851.27Sthorpej return NULL; 861.27Sthorpej return (struct dri3_drawable *)(((void*) draw) - offset); 871.27Sthorpej} 881.27Sthorpej 891.27Sthorpejstatic void 901.27Sthorpejglx_dri3_set_drawable_size(struct loader_dri3_drawable *draw, 911.27Sthorpej int width, int height) 921.1Snisimura{ 931.19Stsutsui /* Nothing to do */ 941.19Stsutsui} 951.1Snisimura 961.27Sthorpejstatic bool 971.28Sthorpejglx_dri3_in_current_context(struct loader_dri3_drawable *draw) 981.28Sthorpej{ 991.27Sthorpej struct dri3_drawable *priv = loader_drawable_to_dri3_drawable(draw); 1001.10Syamt 1011.27Sthorpej if (!priv) 1021.1Snisimura return false; 1031.1Snisimura 1041.8Sthorpej struct dri3_context *pcp = (struct dri3_context *) __glXGetCurrentContext(); 1051.1Snisimura struct dri3_screen *psc = (struct dri3_screen *) priv->base.psc; 1061.1Snisimura 1071.1Snisimura return (&pcp->base != &dummyContext) && pcp->base.psc == &psc->base; 1081.25Sandvar} 1091.1Snisimura 1101.1Snisimurastatic __DRIcontext * 1111.1Snisimuraglx_dri3_get_dri_context(struct loader_dri3_drawable *draw) 1121.1Snisimura{ 1131.1Snisimura struct glx_context *gc = __glXGetCurrentContext(); 1141.1Snisimura struct dri3_context *dri3Ctx = (struct dri3_context *) gc; 1151.3Sthorpej 1161.27Sthorpej return (gc != &dummyContext) ? dri3Ctx->driContext : NULL; 1171.27Sthorpej} 1181.27Sthorpej 1191.27Sthorpejstatic __DRIscreen * 1201.4Sthorpejglx_dri3_get_dri_screen(void) 1211.1Snisimura{ 1221.1Snisimura struct glx_context *gc = __glXGetCurrentContext(); 1231.1Snisimura struct dri3_context *pcp = (struct dri3_context *) gc; 1241.1Snisimura struct dri3_screen *psc = (struct dri3_screen *) pcp->base.psc; 1251.1Snisimura 1261.13Sthorpej return (gc != &dummyContext && psc) ? psc->driScreen : NULL; 1271.1Snisimura} 1281.27Sthorpej 1291.1Snisimurastatic void 1301.1Snisimuraglx_dri3_flush_drawable(struct loader_dri3_drawable *draw, unsigned flags) 131{ 132 loader_dri3_flush(draw, flags, __DRI2_THROTTLE_SWAPBUFFER); 133} 134 135static void 136glx_dri3_show_fps(struct loader_dri3_drawable *draw, uint64_t current_ust) 137{ 138 struct dri3_drawable *priv = loader_drawable_to_dri3_drawable(draw); 139 const uint64_t interval = 140 ((struct dri3_screen *) priv->base.psc)->show_fps_interval; 141 142 if (!interval) 143 return; 144 145 priv->frames++; 146 147 /* DRI3+Present together uses microseconds for UST. */ 148 if (priv->previous_ust + interval * 1000000 <= current_ust) { 149 if (priv->previous_ust) { 150 fprintf(stderr, "libGL: FPS = %.1f\n", 151 ((uint64_t) priv->frames * 1000000) / 152 (double)(current_ust - priv->previous_ust)); 153 } 154 priv->frames = 0; 155 priv->previous_ust = current_ust; 156 } 157} 158 159static const struct loader_dri3_vtable glx_dri3_vtable = { 160 .set_drawable_size = glx_dri3_set_drawable_size, 161 .in_current_context = glx_dri3_in_current_context, 162 .get_dri_context = glx_dri3_get_dri_context, 163 .get_dri_screen = glx_dri3_get_dri_screen, 164 .flush_drawable = glx_dri3_flush_drawable, 165 .show_fps = glx_dri3_show_fps, 166}; 167 168 169static const struct glx_context_vtable dri3_context_vtable; 170 171static void 172dri3_destroy_context(struct glx_context *context) 173{ 174 struct dri3_context *pcp = (struct dri3_context *) context; 175 struct dri3_screen *psc = (struct dri3_screen *) context->psc; 176 177 driReleaseDrawables(&pcp->base); 178 179 free((char *) context->extensions); 180 181 (*psc->core->destroyContext) (pcp->driContext); 182 183 free(pcp); 184} 185 186static Bool 187dri3_bind_context(struct glx_context *context, struct glx_context *old, 188 GLXDrawable draw, GLXDrawable read) 189{ 190 struct dri3_context *pcp = (struct dri3_context *) context; 191 struct dri3_screen *psc = (struct dri3_screen *) pcp->base.psc; 192 struct dri3_drawable *pdraw, *pread; 193 __DRIdrawable *dri_draw = NULL, *dri_read = NULL; 194 195 pdraw = (struct dri3_drawable *) driFetchDrawable(context, draw); 196 pread = (struct dri3_drawable *) driFetchDrawable(context, read); 197 198 driReleaseDrawables(&pcp->base); 199 200 if (pdraw) 201 dri_draw = pdraw->loader_drawable.dri_drawable; 202 else if (draw != None) 203 return GLXBadDrawable; 204 205 if (pread) 206 dri_read = pread->loader_drawable.dri_drawable; 207 else if (read != None) 208 return GLXBadDrawable; 209 210 if (!(*psc->core->bindContext) (pcp->driContext, dri_draw, dri_read)) 211 return GLXBadContext; 212 213 if (dri_draw) 214 (*psc->f->invalidate)(dri_draw); 215 if (dri_read && dri_read != dri_draw) 216 (*psc->f->invalidate)(dri_read); 217 218 return Success; 219} 220 221static void 222dri3_unbind_context(struct glx_context *context, struct glx_context *new) 223{ 224 struct dri3_context *pcp = (struct dri3_context *) context; 225 struct dri3_screen *psc = (struct dri3_screen *) pcp->base.psc; 226 227 (*psc->core->unbindContext) (pcp->driContext); 228} 229 230static struct glx_context * 231dri3_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 dri3_context *pcp = NULL; 239 struct dri3_context *pcp_shared = NULL; 240 struct dri3_screen *psc = (struct dri3_screen *) base; 241 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base; 242 __DRIcontext *shared = NULL; 243 244 uint32_t minor_ver = 1; 245 uint32_t major_ver = 2; 246 uint32_t flags = 0; 247 unsigned api; 248 int reset = __DRI_CTX_RESET_NO_NOTIFICATION; 249 int release = __DRI_CTX_RELEASE_BEHAVIOR_FLUSH; 250 uint32_t ctx_attribs[2 * 6]; 251 unsigned num_ctx_attribs = 0; 252 uint32_t render_type; 253 254 /* Remap the GLX tokens to DRI2 tokens. 255 */ 256 if (!dri2_convert_glx_attribs(num_attribs, attribs, 257 &major_ver, &minor_ver, 258 &render_type, &flags, &api, 259 &reset, &release, error)) 260 goto error_exit; 261 262 /* Check the renderType value */ 263 if (!validate_renderType_against_config(config_base, render_type)) 264 goto error_exit; 265 266 if (shareList) { 267 pcp_shared = (struct dri3_context *) shareList; 268 shared = pcp_shared->driContext; 269 } 270 271 pcp = calloc(1, sizeof *pcp); 272 if (pcp == NULL) { 273 *error = __DRI_CTX_ERROR_NO_MEMORY; 274 goto error_exit; 275 } 276 277 if (!glx_context_init(&pcp->base, &psc->base, config_base)) 278 goto error_exit; 279 280 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MAJOR_VERSION; 281 ctx_attribs[num_ctx_attribs++] = major_ver; 282 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MINOR_VERSION; 283 ctx_attribs[num_ctx_attribs++] = minor_ver; 284 285 /* Only send a value when the non-default value is requested. By doing 286 * this we don't have to check the driver's DRI3 version before sending the 287 * default value. 288 */ 289 if (reset != __DRI_CTX_RESET_NO_NOTIFICATION) { 290 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_RESET_STRATEGY; 291 ctx_attribs[num_ctx_attribs++] = reset; 292 } 293 294 if (release != __DRI_CTX_RELEASE_BEHAVIOR_FLUSH) { 295 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_RELEASE_BEHAVIOR; 296 ctx_attribs[num_ctx_attribs++] = release; 297 } 298 299 if (flags != 0) { 300 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_FLAGS; 301 302 /* The current __DRI_CTX_FLAG_* values are identical to the 303 * GLX_CONTEXT_*_BIT values. 304 */ 305 ctx_attribs[num_ctx_attribs++] = flags; 306 } 307 308 pcp->driContext = 309 (*psc->image_driver->createContextAttribs) (psc->driScreen, 310 api, 311 config ? config->driConfig 312 : NULL, 313 shared, 314 num_ctx_attribs / 2, 315 ctx_attribs, 316 error, 317 pcp); 318 319 if (pcp->driContext == NULL) 320 goto error_exit; 321 322 pcp->base.vtable = &dri3_context_vtable; 323 324 return &pcp->base; 325 326error_exit: 327 free(pcp); 328 329 return NULL; 330} 331 332static struct glx_context * 333dri3_create_context(struct glx_screen *base, 334 struct glx_config *config_base, 335 struct glx_context *shareList, int renderType) 336{ 337 unsigned int error; 338 uint32_t attribs[2] = { GLX_RENDER_TYPE, renderType }; 339 340 return dri3_create_context_attribs(base, config_base, shareList, 341 1, attribs, &error); 342} 343 344static void 345dri3_destroy_drawable(__GLXDRIdrawable *base) 346{ 347 struct dri3_drawable *pdraw = (struct dri3_drawable *) base; 348 349 loader_dri3_drawable_fini(&pdraw->loader_drawable); 350 351 free(pdraw); 352} 353 354static __GLXDRIdrawable * 355dri3_create_drawable(struct glx_screen *base, XID xDrawable, 356 GLXDrawable drawable, struct glx_config *config_base) 357{ 358 struct dri3_drawable *pdraw; 359 struct dri3_screen *psc = (struct dri3_screen *) base; 360 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base; 361 bool has_multibuffer = false; 362#ifdef HAVE_DRI3_MODIFIERS 363 const struct dri3_display *const pdp = (struct dri3_display *) 364 base->display->dri3Display; 365#endif 366 367 pdraw = calloc(1, sizeof(*pdraw)); 368 if (!pdraw) 369 return NULL; 370 371 pdraw->base.destroyDrawable = dri3_destroy_drawable; 372 pdraw->base.xDrawable = xDrawable; 373 pdraw->base.drawable = drawable; 374 pdraw->base.psc = &psc->base; 375 376#ifdef HAVE_DRI3_MODIFIERS 377 if ((psc->image && psc->image->base.version >= 15) && 378 (pdp->dri3Major > 1 || (pdp->dri3Major == 1 && pdp->dri3Minor >= 2)) && 379 (pdp->presentMajor > 1 || 380 (pdp->presentMajor == 1 && pdp->presentMinor >= 2))) 381 has_multibuffer = true; 382#endif 383 384 (void) __glXInitialize(psc->base.dpy); 385 386 if (loader_dri3_drawable_init(XGetXCBConnection(base->dpy), 387 xDrawable, psc->driScreen, 388 psc->is_different_gpu, has_multibuffer, 389 config->driConfig, 390 &psc->loader_dri3_ext, &glx_dri3_vtable, 391 &pdraw->loader_drawable)) { 392 free(pdraw); 393 return NULL; 394 } 395 396 return &pdraw->base; 397} 398 399/** dri3_wait_for_msc 400 * 401 * Get the X server to send an event when the target msc/divisor/remainder is 402 * reached. 403 */ 404static int 405dri3_wait_for_msc(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor, 406 int64_t remainder, int64_t *ust, int64_t *msc, int64_t *sbc) 407{ 408 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw; 409 410 loader_dri3_wait_for_msc(&priv->loader_drawable, target_msc, divisor, 411 remainder, ust, msc, sbc); 412 413 return 1; 414} 415 416/** dri3_drawable_get_msc 417 * 418 * Return the current UST/MSC/SBC triplet by asking the server 419 * for an event 420 */ 421static int 422dri3_drawable_get_msc(struct glx_screen *psc, __GLXDRIdrawable *pdraw, 423 int64_t *ust, int64_t *msc, int64_t *sbc) 424{ 425 return dri3_wait_for_msc(pdraw, 0, 0, 0, ust, msc,sbc); 426} 427 428/** dri3_wait_for_sbc 429 * 430 * Wait for the completed swap buffer count to reach the specified 431 * target. Presumably the application knows that this will be reached with 432 * outstanding complete events, or we're going to be here awhile. 433 */ 434static int 435dri3_wait_for_sbc(__GLXDRIdrawable *pdraw, int64_t target_sbc, int64_t *ust, 436 int64_t *msc, int64_t *sbc) 437{ 438 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw; 439 440 return loader_dri3_wait_for_sbc(&priv->loader_drawable, target_sbc, 441 ust, msc, sbc); 442} 443 444static void 445dri3_copy_sub_buffer(__GLXDRIdrawable *pdraw, int x, int y, 446 int width, int height, 447 Bool flush) 448{ 449 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw; 450 451 loader_dri3_copy_sub_buffer(&priv->loader_drawable, x, y, 452 width, height, flush); 453} 454 455static void 456dri3_wait_x(struct glx_context *gc) 457{ 458 struct dri3_drawable *priv = (struct dri3_drawable *) 459 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable); 460 461 if (priv) 462 loader_dri3_wait_x(&priv->loader_drawable); 463} 464 465static void 466dri3_wait_gl(struct glx_context *gc) 467{ 468 struct dri3_drawable *priv = (struct dri3_drawable *) 469 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable); 470 471 if (priv) 472 loader_dri3_wait_gl(&priv->loader_drawable); 473} 474 475/** 476 * Called by the driver when it needs to update the real front buffer with the 477 * contents of its fake front buffer. 478 */ 479static void 480dri3_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate) 481{ 482 struct loader_dri3_drawable *draw = loaderPrivate; 483 struct dri3_drawable *pdraw = loader_drawable_to_dri3_drawable(draw); 484 struct dri3_screen *psc; 485 486 if (!pdraw) 487 return; 488 489 if (!pdraw->base.psc) 490 return; 491 492 psc = (struct dri3_screen *) pdraw->base.psc; 493 494 (void) __glXInitialize(psc->base.dpy); 495 496 loader_dri3_flush(draw, __DRI2_FLUSH_DRAWABLE, __DRI2_THROTTLE_FLUSHFRONT); 497 498 (*psc->f->invalidate)(driDrawable); 499 loader_dri3_wait_gl(draw); 500} 501 502/** 503 * Make sure all pending swapbuffers have been submitted to hardware 504 * 505 * \param driDrawable[in] Pointer to the dri drawable whose swaps we are 506 * flushing. 507 * \param loaderPrivate[in] Pointer to the corresponding struct 508 * loader_dri_drawable. 509 */ 510static void 511dri3_flush_swap_buffers(__DRIdrawable *driDrawable, void *loaderPrivate) 512{ 513 struct loader_dri3_drawable *draw = loaderPrivate; 514 struct dri3_drawable *pdraw = loader_drawable_to_dri3_drawable(draw); 515 struct dri3_screen *psc; 516 517 if (!pdraw) 518 return; 519 520 if (!pdraw->base.psc) 521 return; 522 523 psc = (struct dri3_screen *) pdraw->base.psc; 524 525 (void) __glXInitialize(psc->base.dpy); 526 loader_dri3_swapbuffer_barrier(draw); 527} 528 529static void 530dri_set_background_context(void *loaderPrivate) 531{ 532 struct dri3_context *pcp = (struct dri3_context *)loaderPrivate; 533 __glXSetCurrentContext(&pcp->base); 534} 535 536static GLboolean 537dri_is_thread_safe(void *loaderPrivate) 538{ 539 /* Unlike DRI2, DRI3 doesn't call GetBuffers/GetBuffersWithFormat 540 * during draw so we're safe here. 541 */ 542 return true; 543} 544 545/* The image loader extension record for DRI3 546 */ 547static const __DRIimageLoaderExtension imageLoaderExtension = { 548 .base = { __DRI_IMAGE_LOADER, 3 }, 549 550 .getBuffers = loader_dri3_get_buffers, 551 .flushFrontBuffer = dri3_flush_front_buffer, 552 .flushSwapBuffers = dri3_flush_swap_buffers, 553}; 554 555const __DRIuseInvalidateExtension dri3UseInvalidate = { 556 .base = { __DRI_USE_INVALIDATE, 1 } 557}; 558 559static const __DRIbackgroundCallableExtension driBackgroundCallable = { 560 .base = { __DRI_BACKGROUND_CALLABLE, 2 }, 561 562 .setBackgroundContext = dri_set_background_context, 563 .isThreadSafe = dri_is_thread_safe, 564}; 565 566static const __DRIextension *loader_extensions[] = { 567 &imageLoaderExtension.base, 568 &dri3UseInvalidate.base, 569 &driBackgroundCallable.base, 570 NULL 571}; 572 573/** dri3_swap_buffers 574 * 575 * Make the current back buffer visible using the present extension 576 */ 577static int64_t 578dri3_swap_buffers(__GLXDRIdrawable *pdraw, int64_t target_msc, int64_t divisor, 579 int64_t remainder, Bool flush) 580{ 581 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw; 582 unsigned flags = __DRI2_FLUSH_DRAWABLE; 583 584 if (flush) 585 flags |= __DRI2_FLUSH_CONTEXT; 586 587 return loader_dri3_swap_buffers_msc(&priv->loader_drawable, 588 target_msc, divisor, remainder, 589 flags, false); 590} 591 592static int 593dri3_get_buffer_age(__GLXDRIdrawable *pdraw) 594{ 595 struct dri3_drawable *priv = (struct dri3_drawable *)pdraw; 596 597 return loader_dri3_query_buffer_age(&priv->loader_drawable); 598} 599 600/** dri3_destroy_screen 601 */ 602static void 603dri3_destroy_screen(struct glx_screen *base) 604{ 605 struct dri3_screen *psc = (struct dri3_screen *) base; 606 607 /* Free the direct rendering per screen data */ 608 loader_dri3_close_screen(psc->driScreen); 609 (*psc->core->destroyScreen) (psc->driScreen); 610 driDestroyConfigs(psc->driver_configs); 611 close(psc->fd); 612 free(psc); 613} 614 615/** dri3_set_swap_interval 616 * 617 * Record the application swap interval specification, 618 */ 619static int 620dri3_set_swap_interval(__GLXDRIdrawable *pdraw, int interval) 621{ 622 assert(pdraw != NULL); 623 624 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw; 625 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1; 626 struct dri3_screen *psc = (struct dri3_screen *) priv->base.psc; 627 628 if (psc->config) 629 psc->config->configQueryi(psc->driScreen, 630 "vblank_mode", &vblank_mode); 631 632 switch (vblank_mode) { 633 case DRI_CONF_VBLANK_NEVER: 634 if (interval != 0) 635 return GLX_BAD_VALUE; 636 break; 637 case DRI_CONF_VBLANK_ALWAYS_SYNC: 638 if (interval <= 0) 639 return GLX_BAD_VALUE; 640 break; 641 default: 642 break; 643 } 644 645 loader_dri3_set_swap_interval(&priv->loader_drawable, interval); 646 647 return 0; 648} 649 650/** dri3_get_swap_interval 651 * 652 * Return the stored swap interval 653 */ 654static int 655dri3_get_swap_interval(__GLXDRIdrawable *pdraw) 656{ 657 assert(pdraw != NULL); 658 659 struct dri3_drawable *priv = (struct dri3_drawable *) pdraw; 660 661 return priv->loader_drawable.swap_interval; 662} 663 664static void 665dri3_bind_tex_image(Display * dpy, 666 GLXDrawable drawable, 667 int buffer, const int *attrib_list) 668{ 669 struct glx_context *gc = __glXGetCurrentContext(); 670 struct dri3_context *pcp = (struct dri3_context *) gc; 671 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable); 672 struct dri3_drawable *pdraw = (struct dri3_drawable *) base; 673 struct dri3_screen *psc; 674 675 if (pdraw != NULL) { 676 psc = (struct dri3_screen *) base->psc; 677 678 (*psc->f->invalidate)(pdraw->loader_drawable.dri_drawable); 679 680 XSync(dpy, false); 681 682 (*psc->texBuffer->setTexBuffer2) (pcp->driContext, 683 pdraw->base.textureTarget, 684 pdraw->base.textureFormat, 685 pdraw->loader_drawable.dri_drawable); 686 } 687} 688 689static void 690dri3_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer) 691{ 692 struct glx_context *gc = __glXGetCurrentContext(); 693 struct dri3_context *pcp = (struct dri3_context *) gc; 694 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable); 695 struct dri3_drawable *pdraw = (struct dri3_drawable *) base; 696 struct dri3_screen *psc; 697 698 if (pdraw != NULL) { 699 psc = (struct dri3_screen *) base->psc; 700 701 if (psc->texBuffer->base.version >= 3 && 702 psc->texBuffer->releaseTexBuffer != NULL) 703 (*psc->texBuffer->releaseTexBuffer) (pcp->driContext, 704 pdraw->base.textureTarget, 705 pdraw->loader_drawable.dri_drawable); 706 } 707} 708 709static const struct glx_context_vtable dri3_context_vtable = { 710 .destroy = dri3_destroy_context, 711 .bind = dri3_bind_context, 712 .unbind = dri3_unbind_context, 713 .wait_gl = dri3_wait_gl, 714 .wait_x = dri3_wait_x, 715 .use_x_font = DRI_glXUseXFont, 716 .bind_tex_image = dri3_bind_tex_image, 717 .release_tex_image = dri3_release_tex_image, 718 .get_proc_address = NULL, 719 .interop_query_device_info = dri3_interop_query_device_info, 720 .interop_export_object = dri3_interop_export_object 721}; 722 723/** dri3_bind_extensions 724 * 725 * Enable all of the extensions supported on DRI3 726 */ 727static void 728dri3_bind_extensions(struct dri3_screen *psc, struct glx_display * priv, 729 const char *driverName) 730{ 731 const __DRIextension **extensions; 732 unsigned mask; 733 int i; 734 735 extensions = psc->core->getExtensions(psc->driScreen); 736 737 __glXEnableDirectExtension(&psc->base, "GLX_SGI_swap_control"); 738 __glXEnableDirectExtension(&psc->base, "GLX_MESA_swap_control"); 739 __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read"); 740 __glXEnableDirectExtension(&psc->base, "GLX_INTEL_swap_event"); 741 742 mask = psc->image_driver->getAPIMask(psc->driScreen); 743 744 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context"); 745 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context_profile"); 746 747 if ((mask & ((1 << __DRI_API_GLES) | 748 (1 << __DRI_API_GLES2) | 749 (1 << __DRI_API_GLES3))) != 0) { 750 __glXEnableDirectExtension(&psc->base, 751 "GLX_EXT_create_context_es_profile"); 752 __glXEnableDirectExtension(&psc->base, 753 "GLX_EXT_create_context_es2_profile"); 754 } 755 756 for (i = 0; extensions[i]; i++) { 757 /* when on a different gpu than the server, the server pixmaps 758 * can have a tiling mode we can't read. Thus we can't create 759 * a texture from them. 760 */ 761 if (!psc->is_different_gpu && 762 (strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) { 763 psc->texBuffer = (__DRItexBufferExtension *) extensions[i]; 764 __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap"); 765 } 766 767 if ((strcmp(extensions[i]->name, __DRI2_FLUSH) == 0)) { 768 psc->f = (__DRI2flushExtension *) extensions[i]; 769 /* internal driver extension, no GL extension exposed */ 770 } 771 772 if (strcmp(extensions[i]->name, __DRI_IMAGE) == 0) 773 psc->image = (__DRIimageExtension *) extensions[i]; 774 775 if ((strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0)) 776 psc->config = (__DRI2configQueryExtension *) extensions[i]; 777 778 if (strcmp(extensions[i]->name, __DRI2_ROBUSTNESS) == 0) 779 __glXEnableDirectExtension(&psc->base, 780 "GLX_ARB_create_context_robustness"); 781 782 if (strcmp(extensions[i]->name, __DRI2_RENDERER_QUERY) == 0) { 783 psc->rendererQuery = (__DRI2rendererQueryExtension *) extensions[i]; 784 __glXEnableDirectExtension(&psc->base, "GLX_MESA_query_renderer"); 785 } 786 787 if (strcmp(extensions[i]->name, __DRI2_INTEROP) == 0) 788 psc->interop = (__DRI2interopExtension*)extensions[i]; 789 790 if (strcmp(extensions[i]->name, __DRI2_FLUSH_CONTROL) == 0) 791 __glXEnableDirectExtension(&psc->base, 792 "GLX_ARB_context_flush_control"); 793 } 794} 795 796static const struct glx_screen_vtable dri3_screen_vtable = { 797 .create_context = dri3_create_context, 798 .create_context_attribs = dri3_create_context_attribs, 799 .query_renderer_integer = dri3_query_renderer_integer, 800 .query_renderer_string = dri3_query_renderer_string, 801}; 802 803/** dri3_create_screen 804 * 805 * Initialize DRI3 on the specified screen. 806 * 807 * Opens the DRI device, locates the appropriate DRI driver 808 * and loads that. 809 * 810 * Checks to see if the driver supports the necessary extensions 811 * 812 * Initializes the driver for the screen and sets up our structures 813 */ 814 815static struct glx_screen * 816dri3_create_screen(int screen, struct glx_display * priv) 817{ 818 xcb_connection_t *c = XGetXCBConnection(priv->dpy); 819 const __DRIconfig **driver_configs; 820 const __DRIextension **extensions; 821 const struct dri3_display *const pdp = (struct dri3_display *) 822 priv->dri3Display; 823 struct dri3_screen *psc; 824 __GLXDRIscreen *psp; 825 struct glx_config *configs = NULL, *visuals = NULL; 826 char *driverName, *tmp; 827 int i; 828 unsigned char disable; 829 830 psc = calloc(1, sizeof *psc); 831 if (psc == NULL) 832 return NULL; 833 834 psc->fd = -1; 835 836 if (!glx_screen_init(&psc->base, screen, priv)) { 837 free(psc); 838 return NULL; 839 } 840 841 psc->fd = loader_dri3_open(c, RootWindow(priv->dpy, screen), None); 842 if (psc->fd < 0) { 843 int conn_error = xcb_connection_has_error(c); 844 845 glx_screen_cleanup(&psc->base); 846 free(psc); 847 InfoMessageF("screen %d does not appear to be DRI3 capable\n", screen); 848 849 if (conn_error) 850 ErrorMessageF("Connection closed during DRI3 initialization failure"); 851 852 return NULL; 853 } 854 855 psc->fd = loader_get_user_preferred_fd(psc->fd, &psc->is_different_gpu); 856 857 driverName = loader_get_driver_for_fd(psc->fd); 858 if (!driverName) { 859 ErrorMessageF("No driver found\n"); 860 goto handle_error; 861 } 862 863 psc->driver = driOpenDriver(driverName); 864 if (psc->driver == NULL) { 865 ErrorMessageF("driver pointer missing\n"); 866 goto handle_error; 867 } 868 869 extensions = driGetDriverExtensions(psc->driver, driverName); 870 if (extensions == NULL) 871 goto handle_error; 872 873 for (i = 0; extensions[i]; i++) { 874 if (strcmp(extensions[i]->name, __DRI_CORE) == 0) 875 psc->core = (__DRIcoreExtension *) extensions[i]; 876 if (strcmp(extensions[i]->name, __DRI_IMAGE_DRIVER) == 0) 877 psc->image_driver = (__DRIimageDriverExtension *) extensions[i]; 878 } 879 880 881 if (psc->core == NULL) { 882 ErrorMessageF("core dri driver extension not found\n"); 883 goto handle_error; 884 } 885 886 if (psc->image_driver == NULL) { 887 ErrorMessageF("image driver extension not found\n"); 888 goto handle_error; 889 } 890 891 psc->driScreen = 892 psc->image_driver->createNewScreen2(screen, psc->fd, 893 pdp->loader_extensions, 894 extensions, 895 &driver_configs, psc); 896 897 if (psc->driScreen == NULL) { 898 ErrorMessageF("failed to create dri screen\n"); 899 goto handle_error; 900 } 901 902 dri3_bind_extensions(psc, priv, driverName); 903 904 if (!psc->image || psc->image->base.version < 7 || !psc->image->createImageFromFds) { 905 ErrorMessageF("Version 7 or imageFromFds image extension not found\n"); 906 goto handle_error; 907 } 908 909 if (!psc->f || psc->f->base.version < 4) { 910 ErrorMessageF("Version 4 or later of flush extension not found\n"); 911 goto handle_error; 912 } 913 914 if (psc->is_different_gpu && psc->image->base.version < 9) { 915 ErrorMessageF("Different GPU, but image extension version 9 or later not found\n"); 916 goto handle_error; 917 } 918 919 if (psc->is_different_gpu && !psc->image->blitImage) { 920 ErrorMessageF("Different GPU, but blitImage not implemented for this driver\n"); 921 goto handle_error; 922 } 923 924 if (!psc->is_different_gpu && ( 925 !psc->texBuffer || psc->texBuffer->base.version < 2 || 926 !psc->texBuffer->setTexBuffer2 927 )) { 928 ErrorMessageF("Version 2 or later of texBuffer extension not found\n"); 929 goto handle_error; 930 } 931 932 psc->loader_dri3_ext.core = psc->core; 933 psc->loader_dri3_ext.image_driver = psc->image_driver; 934 psc->loader_dri3_ext.flush = psc->f; 935 psc->loader_dri3_ext.tex_buffer = psc->texBuffer; 936 psc->loader_dri3_ext.image = psc->image; 937 psc->loader_dri3_ext.config = psc->config; 938 939 configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs); 940 visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs); 941 942 if (!configs || !visuals) { 943 ErrorMessageF("No matching fbConfigs or visuals found\n"); 944 goto handle_error; 945 } 946 947 glx_config_destroy_list(psc->base.configs); 948 psc->base.configs = configs; 949 glx_config_destroy_list(psc->base.visuals); 950 psc->base.visuals = visuals; 951 952 psc->driver_configs = driver_configs; 953 954 psc->base.vtable = &dri3_screen_vtable; 955 psp = &psc->vtable; 956 psc->base.driScreen = psp; 957 psp->destroyScreen = dri3_destroy_screen; 958 psp->createDrawable = dri3_create_drawable; 959 psp->swapBuffers = dri3_swap_buffers; 960 961 psp->getDrawableMSC = dri3_drawable_get_msc; 962 psp->waitForMSC = dri3_wait_for_msc; 963 psp->waitForSBC = dri3_wait_for_sbc; 964 psp->setSwapInterval = dri3_set_swap_interval; 965 psp->getSwapInterval = dri3_get_swap_interval; 966 if (psc->config->configQueryb(psc->driScreen, 967 "glx_disable_oml_sync_control", 968 &disable) || !disable) 969 __glXEnableDirectExtension(&psc->base, "GLX_OML_sync_control"); 970 971 if (psc->config->configQueryb(psc->driScreen, 972 "glx_disable_sgi_video_sync", 973 &disable) || !disable) 974 __glXEnableDirectExtension(&psc->base, "GLX_SGI_video_sync"); 975 976 psp->copySubBuffer = dri3_copy_sub_buffer; 977 __glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer"); 978 979 psp->getBufferAge = dri3_get_buffer_age; 980 if (psc->config->configQueryb(psc->driScreen, 981 "glx_disable_ext_buffer_age", 982 &disable) || !disable) 983 __glXEnableDirectExtension(&psc->base, "GLX_EXT_buffer_age"); 984 985 free(driverName); 986 987 tmp = getenv("LIBGL_SHOW_FPS"); 988 psc->show_fps_interval = tmp ? atoi(tmp) : 0; 989 if (psc->show_fps_interval < 0) 990 psc->show_fps_interval = 0; 991 992 InfoMessageF("Using DRI3 for screen %d\n", screen); 993 994 return &psc->base; 995 996handle_error: 997 CriticalErrorMessageF("failed to load driver: %s\n", driverName); 998 999 if (configs) 1000 glx_config_destroy_list(configs); 1001 if (visuals) 1002 glx_config_destroy_list(visuals); 1003 if (psc->driScreen) 1004 psc->core->destroyScreen(psc->driScreen); 1005 psc->driScreen = NULL; 1006 if (psc->fd >= 0) 1007 close(psc->fd); 1008 if (psc->driver) 1009 dlclose(psc->driver); 1010 1011 free(driverName); 1012 glx_screen_cleanup(&psc->base); 1013 free(psc); 1014 1015 return NULL; 1016} 1017 1018/** dri_destroy_display 1019 * 1020 * Called from __glXFreeDisplayPrivate. 1021 */ 1022static void 1023dri3_destroy_display(__GLXDRIdisplay * dpy) 1024{ 1025 free(dpy); 1026} 1027 1028/* Only request versions of these protocols which we actually support. */ 1029#define DRI3_SUPPORTED_MAJOR 1 1030#define PRESENT_SUPPORTED_MAJOR 1 1031 1032#ifdef HAVE_DRI3_MODIFIERS 1033#define DRI3_SUPPORTED_MINOR 2 1034#define PRESENT_SUPPORTED_MINOR 2 1035#else 1036#define PRESENT_SUPPORTED_MINOR 0 1037#define DRI3_SUPPORTED_MINOR 0 1038#endif 1039 1040/** dri3_create_display 1041 * 1042 * Allocate, initialize and return a __DRIdisplayPrivate object. 1043 * This is called from __glXInitialize() when we are given a new 1044 * display pointer. This is public to that function, but hidden from 1045 * outside of libGL. 1046 */ 1047_X_HIDDEN __GLXDRIdisplay * 1048dri3_create_display(Display * dpy) 1049{ 1050 struct dri3_display *pdp; 1051 xcb_connection_t *c = XGetXCBConnection(dpy); 1052 xcb_dri3_query_version_cookie_t dri3_cookie; 1053 xcb_dri3_query_version_reply_t *dri3_reply; 1054 xcb_present_query_version_cookie_t present_cookie; 1055 xcb_present_query_version_reply_t *present_reply; 1056 xcb_generic_error_t *error; 1057 const xcb_query_extension_reply_t *extension; 1058 1059 xcb_prefetch_extension_data(c, &xcb_dri3_id); 1060 xcb_prefetch_extension_data(c, &xcb_present_id); 1061 1062 extension = xcb_get_extension_data(c, &xcb_dri3_id); 1063 if (!(extension && extension->present)) 1064 return NULL; 1065 1066 extension = xcb_get_extension_data(c, &xcb_present_id); 1067 if (!(extension && extension->present)) 1068 return NULL; 1069 1070 dri3_cookie = xcb_dri3_query_version(c, 1071 DRI3_SUPPORTED_MAJOR, 1072 DRI3_SUPPORTED_MINOR); 1073 present_cookie = xcb_present_query_version(c, 1074 PRESENT_SUPPORTED_MAJOR, 1075 PRESENT_SUPPORTED_MINOR); 1076 1077 pdp = malloc(sizeof *pdp); 1078 if (pdp == NULL) 1079 return NULL; 1080 1081 dri3_reply = xcb_dri3_query_version_reply(c, dri3_cookie, &error); 1082 if (!dri3_reply) { 1083 free(error); 1084 goto no_extension; 1085 } 1086 1087 pdp->dri3Major = dri3_reply->major_version; 1088 pdp->dri3Minor = dri3_reply->minor_version; 1089 free(dri3_reply); 1090 1091 present_reply = xcb_present_query_version_reply(c, present_cookie, &error); 1092 if (!present_reply) { 1093 free(error); 1094 goto no_extension; 1095 } 1096 pdp->presentMajor = present_reply->major_version; 1097 pdp->presentMinor = present_reply->minor_version; 1098 free(present_reply); 1099 1100 pdp->base.destroyDisplay = dri3_destroy_display; 1101 pdp->base.createScreen = dri3_create_screen; 1102 1103 loader_set_logger(dri_message); 1104 1105 pdp->loader_extensions = loader_extensions; 1106 1107 return &pdp->base; 1108no_extension: 1109 free(pdp); 1110 return NULL; 1111} 1112 1113#endif /* GLX_DIRECT_RENDERING */ 1114