drisw_glx.c revision 4dbd13cb3f5aeff6ddc85fd091b4677369c19778
1/* 2 * Copyright 2008 George Sapountzis 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 * SOFTWARE. 22 */ 23 24#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL) 25 26#include <X11/Xlib.h> 27#include "glxclient.h" 28#include <dlfcn.h> 29#include "dri_common.h" 30 31struct drisw_display 32{ 33 __GLXDRIdisplay base; 34}; 35 36struct drisw_context 37{ 38 struct glx_context base; 39 __DRIcontext *driContext; 40 41}; 42 43struct drisw_screen 44{ 45 struct glx_screen base; 46 47 __DRIscreen *driScreen; 48 __GLXDRIscreen vtable; 49 const __DRIcoreExtension *core; 50 const __DRIswrastExtension *swrast; 51 const __DRItexBufferExtension *texBuffer; 52 53 const __DRIconfig **driver_configs; 54 55 void *driver; 56}; 57 58struct drisw_drawable 59{ 60 __GLXDRIdrawable base; 61 62 GC gc; 63 GC swapgc; 64 65 __DRIdrawable *driDrawable; 66 XVisualInfo *visinfo; 67 XImage *ximage; 68}; 69 70static Bool 71XCreateDrawable(struct drisw_drawable * pdp, 72 Display * dpy, XID drawable, int visualid) 73{ 74 XGCValues gcvalues; 75 long visMask; 76 XVisualInfo visTemp; 77 int num_visuals; 78 79 /* create GC's */ 80 pdp->gc = XCreateGC(dpy, drawable, 0, NULL); 81 pdp->swapgc = XCreateGC(dpy, drawable, 0, NULL); 82 83 gcvalues.function = GXcopy; 84 gcvalues.graphics_exposures = False; 85 XChangeGC(dpy, pdp->gc, GCFunction, &gcvalues); 86 XChangeGC(dpy, pdp->swapgc, GCFunction, &gcvalues); 87 XChangeGC(dpy, pdp->swapgc, GCGraphicsExposures, &gcvalues); 88 89 /* visual */ 90 visTemp.screen = DefaultScreen(dpy); 91 visTemp.visualid = visualid; 92 visMask = (VisualScreenMask | VisualIDMask); 93 pdp->visinfo = XGetVisualInfo(dpy, visMask, &visTemp, &num_visuals); 94 95 /* create XImage */ 96 pdp->ximage = XCreateImage(dpy, 97 pdp->visinfo->visual, 98 pdp->visinfo->depth, 99 ZPixmap, 0, /* format, offset */ 100 NULL, /* data */ 101 0, 0, /* width, height */ 102 32, /* bitmap_pad */ 103 0); /* bytes_per_line */ 104 105 /** 106 * swrast does not handle 24-bit depth with 24 bpp, so let X do the 107 * the conversion for us. 108 */ 109 if (pdp->ximage->bits_per_pixel == 24) 110 pdp->ximage->bits_per_pixel = 32; 111 112 return True; 113} 114 115static void 116XDestroyDrawable(struct drisw_drawable * pdp, Display * dpy, XID drawable) 117{ 118 XDestroyImage(pdp->ximage); 119 XFree(pdp->visinfo); 120 121 XFreeGC(dpy, pdp->gc); 122 XFreeGC(dpy, pdp->swapgc); 123} 124 125/** 126 * swrast loader functions 127 */ 128 129static void 130swrastGetDrawableInfo(__DRIdrawable * draw, 131 int *x, int *y, int *w, int *h, 132 void *loaderPrivate) 133{ 134 struct drisw_drawable *pdp = loaderPrivate; 135 __GLXDRIdrawable *pdraw = &(pdp->base); 136 Display *dpy = pdraw->psc->dpy; 137 Drawable drawable; 138 139 Window root; 140 unsigned uw, uh, bw, depth; 141 142 drawable = pdraw->xDrawable; 143 144 XGetGeometry(dpy, drawable, &root, x, y, &uw, &uh, &bw, &depth); 145 *w = uw; 146 *h = uh; 147} 148 149/** 150 * Align renderbuffer pitch. 151 * 152 * This should be chosen by the driver and the loader (libGL, xserver/glx) 153 * should use the driver provided pitch. 154 * 155 * It seems that the xorg loader (that is the xserver loading swrast_dri for 156 * indirect rendering, not client-side libGL) requires that the pitch is 157 * exactly the image width padded to 32 bits. XXX 158 * 159 * The above restriction can probably be overcome by using ScratchPixmap and 160 * CopyArea in the xserver, similar to ShmPutImage, and setting the width of 161 * the scratch pixmap to 'pitch / cpp'. 162 */ 163static inline int 164bytes_per_line(unsigned pitch_bits, unsigned mul) 165{ 166 unsigned mask = mul - 1; 167 168 return ((pitch_bits + mask) & ~mask) / 8; 169} 170 171static void 172swrastPutImage(__DRIdrawable * draw, int op, 173 int x, int y, int w, int h, 174 char *data, void *loaderPrivate) 175{ 176 struct drisw_drawable *pdp = loaderPrivate; 177 __GLXDRIdrawable *pdraw = &(pdp->base); 178 Display *dpy = pdraw->psc->dpy; 179 Drawable drawable; 180 XImage *ximage; 181 GC gc; 182 183 switch (op) { 184 case __DRI_SWRAST_IMAGE_OP_DRAW: 185 gc = pdp->gc; 186 break; 187 case __DRI_SWRAST_IMAGE_OP_SWAP: 188 gc = pdp->swapgc; 189 break; 190 default: 191 return; 192 } 193 194 drawable = pdraw->xDrawable; 195 196 ximage = pdp->ximage; 197 ximage->data = data; 198 ximage->width = w; 199 ximage->height = h; 200 ximage->bytes_per_line = bytes_per_line(w * ximage->bits_per_pixel, 32); 201 202 XPutImage(dpy, drawable, gc, ximage, 0, 0, x, y, w, h); 203 204 ximage->data = NULL; 205} 206 207static void 208swrastGetImage(__DRIdrawable * read, 209 int x, int y, int w, int h, 210 char *data, void *loaderPrivate) 211{ 212 struct drisw_drawable *prp = loaderPrivate; 213 __GLXDRIdrawable *pread = &(prp->base); 214 Display *dpy = pread->psc->dpy; 215 Drawable readable; 216 XImage *ximage; 217 218 readable = pread->xDrawable; 219 220 ximage = prp->ximage; 221 ximage->data = data; 222 ximage->width = w; 223 ximage->height = h; 224 ximage->bytes_per_line = bytes_per_line(w * ximage->bits_per_pixel, 32); 225 226 XGetSubImage(dpy, readable, x, y, w, h, ~0L, ZPixmap, ximage, 0, 0); 227 228 ximage->data = NULL; 229} 230 231static const __DRIswrastLoaderExtension swrastLoaderExtension = { 232 {__DRI_SWRAST_LOADER, __DRI_SWRAST_LOADER_VERSION}, 233 swrastGetDrawableInfo, 234 swrastPutImage, 235 swrastGetImage 236}; 237 238static const __DRIextension *loader_extensions[] = { 239 &systemTimeExtension.base, 240 &swrastLoaderExtension.base, 241 NULL 242}; 243 244/** 245 * GLXDRI functions 246 */ 247 248static void 249drisw_destroy_context(struct glx_context *context) 250{ 251 struct drisw_context *pcp = (struct drisw_context *) context; 252 struct drisw_screen *psc = (struct drisw_screen *) context->psc; 253 254 driReleaseDrawables(&pcp->base); 255 256 if (context->extensions) 257 XFree((char *) context->extensions); 258 259 (*psc->core->destroyContext) (pcp->driContext); 260 261 Xfree(pcp); 262} 263 264static int 265drisw_bind_context(struct glx_context *context, struct glx_context *old, 266 GLXDrawable draw, GLXDrawable read) 267{ 268 struct drisw_context *pcp = (struct drisw_context *) context; 269 struct drisw_screen *psc = (struct drisw_screen *) pcp->base.psc; 270 struct drisw_drawable *pdraw, *pread; 271 272 pdraw = (struct drisw_drawable *) driFetchDrawable(context, draw); 273 pread = (struct drisw_drawable *) driFetchDrawable(context, read); 274 275 driReleaseDrawables(&pcp->base); 276 277 if (pdraw == NULL || pread == NULL) 278 return GLXBadDrawable; 279 280 if ((*psc->core->bindContext) (pcp->driContext, 281 pdraw->driDrawable, pread->driDrawable)) 282 return Success; 283 284 return GLXBadContext; 285} 286 287static void 288drisw_unbind_context(struct glx_context *context, struct glx_context *new) 289{ 290 struct drisw_context *pcp = (struct drisw_context *) context; 291 struct drisw_screen *psc = (struct drisw_screen *) pcp->base.psc; 292 293 (*psc->core->unbindContext) (pcp->driContext); 294} 295 296static void 297drisw_bind_tex_image(Display * dpy, 298 GLXDrawable drawable, 299 int buffer, const int *attrib_list) 300{ 301 struct glx_context *gc = __glXGetCurrentContext(); 302 struct drisw_context *pcp = (struct drisw_context *) gc; 303 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable); 304 struct drisw_drawable *pdraw = (struct drisw_drawable *) base; 305 struct drisw_screen *psc; 306 307 __glXInitialize(dpy); 308 309 if (pdraw != NULL) { 310 psc = (struct drisw_screen *) base->psc; 311 312 if (!psc->texBuffer) 313 return; 314 315 if (psc->texBuffer->base.version >= 2 && 316 psc->texBuffer->setTexBuffer2 != NULL) { 317 (*psc->texBuffer->setTexBuffer2) (pcp->driContext, 318 pdraw->base.textureTarget, 319 pdraw->base.textureFormat, 320 pdraw->driDrawable); 321 } 322 else { 323 (*psc->texBuffer->setTexBuffer) (pcp->driContext, 324 pdraw->base.textureTarget, 325 pdraw->driDrawable); 326 } 327 } 328} 329 330static void 331drisw_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer) 332{ 333#if __DRI_TEX_BUFFER_VERSION >= 3 334 struct glx_context *gc = __glXGetCurrentContext(); 335 struct dri2_context *pcp = (struct dri2_context *) gc; 336 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable); 337 struct glx_display *dpyPriv = __glXInitialize(dpy); 338 struct dri2_drawable *pdraw = (struct dri2_drawable *) base; 339 struct dri2_screen *psc; 340 341 if (pdraw != NULL) { 342 psc = (struct dri2_screen *) base->psc; 343 344 if (!psc->texBuffer) 345 return; 346 347 if (psc->texBuffer->base.version >= 3 && 348 psc->texBuffer->releaseTexBuffer != NULL) { 349 (*psc->texBuffer->releaseTexBuffer) (pcp->driContext, 350 pdraw->base.textureTarget, 351 pdraw->driDrawable); 352 } 353 } 354#endif 355} 356 357static const struct glx_context_vtable drisw_context_vtable = { 358 drisw_destroy_context, 359 drisw_bind_context, 360 drisw_unbind_context, 361 NULL, 362 NULL, 363 DRI_glXUseXFont, 364 drisw_bind_tex_image, 365 drisw_release_tex_image, 366 NULL, /* get_proc_address */ 367}; 368 369static struct glx_context * 370drisw_create_context(struct glx_screen *base, 371 struct glx_config *config_base, 372 struct glx_context *shareList, int renderType) 373{ 374 struct drisw_context *pcp, *pcp_shared; 375 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base; 376 struct drisw_screen *psc = (struct drisw_screen *) base; 377 __DRIcontext *shared = NULL; 378 379 if (!psc->base.driScreen) 380 return NULL; 381 382 if (shareList) { 383 /* If the shareList context is not a DRISW context, we cannot possibly 384 * create a DRISW context that shares it. 385 */ 386 if (shareList->vtable->destroy != drisw_destroy_context) { 387 return NULL; 388 } 389 390 pcp_shared = (struct drisw_context *) shareList; 391 shared = pcp_shared->driContext; 392 } 393 394 pcp = Xmalloc(sizeof *pcp); 395 if (pcp == NULL) 396 return NULL; 397 398 memset(pcp, 0, sizeof *pcp); 399 if (!glx_context_init(&pcp->base, &psc->base, &config->base)) { 400 Xfree(pcp); 401 return NULL; 402 } 403 404 pcp->driContext = 405 (*psc->core->createNewContext) (psc->driScreen, 406 config->driConfig, shared, pcp); 407 if (pcp->driContext == NULL) { 408 Xfree(pcp); 409 return NULL; 410 } 411 412 pcp->base.vtable = &drisw_context_vtable; 413 414 return &pcp->base; 415} 416 417static void 418driswDestroyDrawable(__GLXDRIdrawable * pdraw) 419{ 420 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw; 421 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc; 422 423 (*psc->core->destroyDrawable) (pdp->driDrawable); 424 425 XDestroyDrawable(pdp, pdraw->psc->dpy, pdraw->drawable); 426 Xfree(pdp); 427} 428 429static __GLXDRIdrawable * 430driswCreateDrawable(struct glx_screen *base, XID xDrawable, 431 GLXDrawable drawable, struct glx_config *modes) 432{ 433 struct drisw_drawable *pdp; 434 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes; 435 struct drisw_screen *psc = (struct drisw_screen *) base; 436 437 const __DRIswrastExtension *swrast = psc->swrast; 438 439 pdp = Xmalloc(sizeof(*pdp)); 440 if (!pdp) 441 return NULL; 442 443 memset(pdp, 0, sizeof *pdp); 444 pdp->base.xDrawable = xDrawable; 445 pdp->base.drawable = drawable; 446 pdp->base.psc = &psc->base; 447 448 XCreateDrawable(pdp, psc->base.dpy, xDrawable, modes->visualID); 449 450 /* Create a new drawable */ 451 pdp->driDrawable = 452 (*swrast->createNewDrawable) (psc->driScreen, config->driConfig, pdp); 453 454 if (!pdp->driDrawable) { 455 XDestroyDrawable(pdp, psc->base.dpy, xDrawable); 456 Xfree(pdp); 457 return NULL; 458 } 459 460 pdp->base.destroyDrawable = driswDestroyDrawable; 461 462 return &pdp->base; 463} 464 465static int64_t 466driswSwapBuffers(__GLXDRIdrawable * pdraw, 467 int64_t target_msc, int64_t divisor, int64_t remainder) 468{ 469 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw; 470 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc; 471 472 (void) target_msc; 473 (void) divisor; 474 (void) remainder; 475 476 (*psc->core->swapBuffers) (pdp->driDrawable); 477 478 return 0; 479} 480 481static void 482driswDestroyScreen(struct glx_screen *base) 483{ 484 struct drisw_screen *psc = (struct drisw_screen *) base; 485 486 /* Free the direct rendering per screen data */ 487 (*psc->core->destroyScreen) (psc->driScreen); 488 driDestroyConfigs(psc->driver_configs); 489 psc->driScreen = NULL; 490 if (psc->driver) 491 dlclose(psc->driver); 492} 493 494static void * 495driOpenSwrast(void) 496{ 497 void *driver = NULL; 498 499 if (driver == NULL) 500 driver = driOpenDriver("swrast"); 501 502 return driver; 503} 504 505static const struct glx_screen_vtable drisw_screen_vtable = { 506 drisw_create_context 507}; 508 509static void 510driswBindExtensions(struct drisw_screen *psc, const __DRIextension **extensions) 511{ 512 int i; 513 514 __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read"); 515 516 /* FIXME: Figure out what other extensions can be ported here from dri2. */ 517 for (i = 0; extensions[i]; i++) { 518 if ((strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) { 519 psc->texBuffer = (__DRItexBufferExtension *) extensions[i]; 520 __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap"); 521 } 522 } 523} 524 525static struct glx_screen * 526driswCreateScreen(int screen, struct glx_display *priv) 527{ 528 __GLXDRIscreen *psp; 529 const __DRIconfig **driver_configs; 530 const __DRIextension **extensions; 531 struct drisw_screen *psc; 532 struct glx_config *configs = NULL, *visuals = NULL; 533 int i; 534 535 psc = Xcalloc(1, sizeof *psc); 536 if (psc == NULL) 537 return NULL; 538 539 memset(psc, 0, sizeof *psc); 540 if (!glx_screen_init(&psc->base, screen, priv)) { 541 Xfree(psc); 542 return NULL; 543 } 544 545 psc->driver = driOpenSwrast(); 546 if (psc->driver == NULL) 547 goto handle_error; 548 549 extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS); 550 if (extensions == NULL) { 551 ErrorMessageF("driver exports no extensions (%s)\n", dlerror()); 552 goto handle_error; 553 } 554 555 for (i = 0; extensions[i]; i++) { 556 if (strcmp(extensions[i]->name, __DRI_CORE) == 0) 557 psc->core = (__DRIcoreExtension *) extensions[i]; 558 if (strcmp(extensions[i]->name, __DRI_SWRAST) == 0) 559 psc->swrast = (__DRIswrastExtension *) extensions[i]; 560 } 561 562 if (psc->core == NULL || psc->swrast == NULL) { 563 ErrorMessageF("core dri extension not found\n"); 564 goto handle_error; 565 } 566 567 psc->driScreen = 568 psc->swrast->createNewScreen(screen, loader_extensions, 569 &driver_configs, psc); 570 if (psc->driScreen == NULL) { 571 ErrorMessageF("failed to create dri screen\n"); 572 goto handle_error; 573 } 574 575 extensions = psc->core->getExtensions(psc->driScreen); 576 driswBindExtensions(psc, extensions); 577 578 configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs); 579 visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs); 580 581 if (!configs || !visuals) 582 goto handle_error; 583 584 glx_config_destroy_list(psc->base.configs); 585 psc->base.configs = configs; 586 glx_config_destroy_list(psc->base.visuals); 587 psc->base.visuals = visuals; 588 589 psc->driver_configs = driver_configs; 590 591 psc->base.vtable = &drisw_screen_vtable; 592 psp = &psc->vtable; 593 psc->base.driScreen = psp; 594 psp->destroyScreen = driswDestroyScreen; 595 psp->createDrawable = driswCreateDrawable; 596 psp->swapBuffers = driswSwapBuffers; 597 598 return &psc->base; 599 600 handle_error: 601 if (configs) 602 glx_config_destroy_list(configs); 603 if (visuals) 604 glx_config_destroy_list(visuals); 605 if (psc->driScreen) 606 psc->core->destroyScreen(psc->driScreen); 607 psc->driScreen = NULL; 608 609 if (psc->driver) 610 dlclose(psc->driver); 611 glx_screen_cleanup(&psc->base); 612 Xfree(psc); 613 614 ErrorMessageF("reverting to indirect rendering\n"); 615 616 return NULL; 617} 618 619/* Called from __glXFreeDisplayPrivate. 620 */ 621static void 622driswDestroyDisplay(__GLXDRIdisplay * dpy) 623{ 624 Xfree(dpy); 625} 626 627/* 628 * Allocate, initialize and return a __DRIdisplayPrivate object. 629 * This is called from __glXInitialize() when we are given a new 630 * display pointer. 631 */ 632_X_HIDDEN __GLXDRIdisplay * 633driswCreateDisplay(Display * dpy) 634{ 635 struct drisw_display *pdpyp; 636 637 pdpyp = Xmalloc(sizeof *pdpyp); 638 if (pdpyp == NULL) 639 return NULL; 640 641 pdpyp->base.destroyDisplay = driswDestroyDisplay; 642 pdpyp->base.createScreen = driswCreateScreen; 643 644 return &pdpyp->base; 645} 646 647#endif /* GLX_DIRECT_RENDERING */ 648