drisw_glx.c revision 6e8897ff9f90601ebf6eed500ad942c11b54d1f7
15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/* 25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * Copyright 2008 George Sapountzis 35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * 45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * Permission is hereby granted, free of charge, to any person obtaining a 55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * copy of this software and associated documentation files (the "Software"), 65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * to deal in the Software without restriction, including without limitation 75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * the rights to use, copy, modify, merge, publish, distribute, sublicense, 85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * and/or sell copies of the Software, and to permit persons to whom the 95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * Software is furnished to do so, subject to the following conditions: 105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * 115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * The above copyright notice and this permission notice (including the next 125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * paragraph) shall be included in all copies or substantial portions of the 135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * Software. 145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * 155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * SOFTWARE. 225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) */ 235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifdef GLX_DIRECT_RENDERING 255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <X11/Xlib.h> 275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "glxclient.h" 285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <dlfcn.h> 295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "dri_common.h" 305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)typedef struct __GLXDRIdisplayPrivateRec __GLXDRIdisplayPrivate; 325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)typedef struct __GLXDRIcontextPrivateRec __GLXDRIcontextPrivate; 335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)typedef struct __GLXDRIdrawablePrivateRec __GLXDRIdrawablePrivate; 345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)struct __GLXDRIdisplayPrivateRec 365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles){ 375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) __GLXDRIdisplay base; 385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}; 395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)struct __GLXDRIcontextPrivateRec 4146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles){ 425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) __GLXDRIcontext base; 435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) __DRIcontext *driContext; 445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) __GLXscreenConfigs *psc; 455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}; 465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)struct __GLXDRIdrawablePrivateRec 485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles){ 495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) __GLXDRIdrawable base; 505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) GC gc; 525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) GC swapgc; 53 54 XVisualInfo *visinfo; 55 XImage *ximage; 56 int bpp; 57}; 58 59/** 60 * swrast loader functions 61 */ 62 63static Bool 64XCreateDrawable(__GLXDRIdrawablePrivate * pdp, 65 Display * dpy, XID drawable, int visualid) 66{ 67 XGCValues gcvalues; 68 long visMask; 69 XVisualInfo visTemp; 70 int num_visuals; 71 72 /* create GC's */ 73 pdp->gc = XCreateGC(dpy, drawable, 0, NULL); 74 pdp->swapgc = XCreateGC(dpy, drawable, 0, NULL); 75 76 gcvalues.function = GXcopy; 77 gcvalues.graphics_exposures = False; 78 XChangeGC(dpy, pdp->gc, GCFunction, &gcvalues); 79 XChangeGC(dpy, pdp->swapgc, GCFunction, &gcvalues); 80 XChangeGC(dpy, pdp->swapgc, GCGraphicsExposures, &gcvalues); 81 82 /* create XImage */ 83 visTemp.screen = DefaultScreen(dpy); 84 visTemp.visualid = visualid; 85 visMask = (VisualScreenMask | VisualIDMask); 86 pdp->visinfo = XGetVisualInfo(dpy, visMask, &visTemp, &num_visuals); 87 88 pdp->ximage = XCreateImage(dpy, pdp->visinfo->visual, pdp->visinfo->depth, ZPixmap, 0, /* format, offset */ 89 NULL, /* data */ 90 0, 0, /* size */ 91 32, /* bitmap_pad */ 92 0); /* bytes_per_line */ 93 94 /* get the true number of bits per pixel */ 95 pdp->bpp = pdp->ximage->bits_per_pixel; 96 97 return True; 98} 99 100static void 101XDestroyDrawable(__GLXDRIdrawablePrivate * pdp, Display * dpy, XID drawable) 102{ 103 XDestroyImage(pdp->ximage); 104 XFree(pdp->visinfo); 105 106 XFreeGC(dpy, pdp->gc); 107 XFreeGC(dpy, pdp->swapgc); 108} 109 110static void 111swrastGetDrawableInfo(__DRIdrawable * draw, 112 int *x, int *y, int *w, int *h, void *loaderPrivate) 113{ 114 __GLXDRIdrawablePrivate *pdp = loaderPrivate; 115 __GLXDRIdrawable *pdraw = &(pdp->base); 116 Display *dpy = pdraw->psc->dpy; 117 Drawable drawable; 118 119 Window root; 120 Status stat; 121 unsigned int bw, depth; 122 123 drawable = pdraw->xDrawable; 124 125 stat = XGetGeometry(dpy, drawable, &root, 126 x, y, (unsigned int *) w, (unsigned int *) h, 127 &bw, &depth); 128} 129 130static inline int 131bytes_per_line(int w, int bpp, unsigned mul) 132{ 133 unsigned mask = mul - 1; 134 135 return ((w * bpp + mask) & ~mask) / 8; 136} 137 138static void 139swrastPutImage(__DRIdrawable * draw, int op, 140 int x, int y, int w, int h, char *data, void *loaderPrivate) 141{ 142 __GLXDRIdrawablePrivate *pdp = loaderPrivate; 143 __GLXDRIdrawable *pdraw = &(pdp->base); 144 Display *dpy = pdraw->psc->dpy; 145 Drawable drawable; 146 XImage *ximage; 147 GC gc; 148 149 switch (op) { 150 case __DRI_SWRAST_IMAGE_OP_DRAW: 151 gc = pdp->gc; 152 break; 153 case __DRI_SWRAST_IMAGE_OP_SWAP: 154 gc = pdp->swapgc; 155 break; 156 default: 157 return; 158 } 159 160 drawable = pdraw->xDrawable; 161 162 ximage = pdp->ximage; 163 ximage->data = data; 164 ximage->width = w; 165 ximage->height = h; 166 ximage->bytes_per_line = bytes_per_line(w, pdp->bpp, 32); 167 168 XPutImage(dpy, drawable, gc, ximage, 0, 0, x, y, w, h); 169 170 ximage->data = NULL; 171} 172 173static void 174swrastGetImage(__DRIdrawable * draw, 175 int x, int y, int w, int h, char *data, void *loaderPrivate) 176{ 177 __GLXDRIdrawablePrivate *pdp = loaderPrivate; 178 __GLXDRIdrawable *pdraw = &(pdp->base); 179 Display *dpy = pdraw->psc->dpy; 180 Drawable drawable; 181 XImage *ximage; 182 183 drawable = pdraw->xDrawable; 184 185 ximage = pdp->ximage; 186 ximage->data = data; 187 ximage->width = w; 188 ximage->height = h; 189 ximage->bytes_per_line = bytes_per_line(w, pdp->bpp, 32); 190 191 XGetSubImage(dpy, drawable, x, y, w, h, ~0L, ZPixmap, ximage, 0, 0); 192 193 ximage->data = NULL; 194} 195 196static const __DRIswrastLoaderExtension swrastLoaderExtension = { 197 {__DRI_SWRAST_LOADER, __DRI_SWRAST_LOADER_VERSION}, 198 swrastGetDrawableInfo, 199 swrastPutImage, 200 swrastGetImage 201}; 202 203static const __DRIextension *loader_extensions[] = { 204 &systemTimeExtension.base, 205 &swrastLoaderExtension.base, 206 NULL 207}; 208 209/** 210 * GLXDRI functions 211 */ 212 213static void 214driDestroyContext(__GLXDRIcontext * context, 215 __GLXscreenConfigs * psc, Display * dpy) 216{ 217 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context; 218 const __DRIcoreExtension *core = pcp->psc->core; 219 220 (*core->destroyContext) (pcp->driContext); 221 222 Xfree(pcp); 223} 224 225static Bool 226driBindContext(__GLXDRIcontext * context, 227 __GLXDRIdrawable * draw, __GLXDRIdrawable * read) 228{ 229 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context; 230 const __DRIcoreExtension *core = pcp->psc->core; 231 232 return (*core->bindContext) (pcp->driContext, 233 draw->driDrawable, read->driDrawable); 234} 235 236static void 237driUnbindContext(__GLXDRIcontext * context) 238{ 239 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context; 240 const __DRIcoreExtension *core = pcp->psc->core; 241 242 (*core->unbindContext) (pcp->driContext); 243} 244 245static __GLXDRIcontext * 246driCreateContext(__GLXscreenConfigs * psc, 247 const __GLcontextModes * mode, 248 GLXContext gc, GLXContext shareList, int renderType) 249{ 250 __GLXDRIcontextPrivate *pcp, *pcp_shared; 251 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) mode; 252 const __DRIcoreExtension *core; 253 __DRIcontext *shared = NULL; 254 255 if (!psc || !psc->driScreen) 256 return NULL; 257 258 core = psc->core; 259 260 if (shareList) { 261 pcp_shared = (__GLXDRIcontextPrivate *) shareList->driContext; 262 shared = pcp_shared->driContext; 263 } 264 265 pcp = Xmalloc(sizeof *pcp); 266 if (pcp == NULL) 267 return NULL; 268 269 pcp->psc = psc; 270 pcp->driContext = 271 (*core->createNewContext) (psc->__driScreen, 272 config->driConfig, shared, pcp); 273 if (pcp->driContext == NULL) { 274 Xfree(pcp); 275 return NULL; 276 } 277 278 pcp->base.destroyContext = driDestroyContext; 279 pcp->base.bindContext = driBindContext; 280 pcp->base.unbindContext = driUnbindContext; 281 282 return &pcp->base; 283} 284 285static void 286driDestroyDrawable(__GLXDRIdrawable * pdraw) 287{ 288 __GLXDRIdrawablePrivate *pdp = (__GLXDRIdrawablePrivate *) pdraw; 289 const __DRIcoreExtension *core = pdraw->psc->core; 290 291 (*core->destroyDrawable) (pdraw->driDrawable); 292 293 XDestroyDrawable(pdp, pdraw->psc->dpy, pdraw->drawable); 294 Xfree(pdp); 295} 296 297static __GLXDRIdrawable * 298driCreateDrawable(__GLXscreenConfigs * psc, 299 XID xDrawable, 300 GLXDrawable drawable, const __GLcontextModes * modes) 301{ 302 __GLXDRIdrawable *pdraw; 303 __GLXDRIdrawablePrivate *pdp; 304 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes; 305 const __DRIswrastExtension *swrast = psc->swrast; 306 307 /* Old dri can't handle GLX 1.3+ drawable constructors. */ 308 if (xDrawable != drawable) 309 return NULL; 310 311 pdp = Xmalloc(sizeof(*pdp)); 312 if (!pdp) 313 return NULL; 314 315 pdraw = &(pdp->base); 316 pdraw->xDrawable = xDrawable; 317 pdraw->drawable = drawable; 318 pdraw->psc = psc; 319 320 XCreateDrawable(pdp, psc->dpy, xDrawable, modes->visualID); 321 322 /* Create a new drawable */ 323 pdraw->driDrawable = 324 (*swrast->createNewDrawable) (psc->__driScreen, config->driConfig, pdp); 325 326 if (!pdraw->driDrawable) { 327 XDestroyDrawable(pdp, psc->dpy, xDrawable); 328 Xfree(pdp); 329 return NULL; 330 } 331 332 pdraw->destroyDrawable = driDestroyDrawable; 333 334 return pdraw; 335} 336 337static void 338driSwapBuffers(__GLXDRIdrawable * pdraw) 339{ 340 (*pdraw->psc->core->swapBuffers) (pdraw->driDrawable); 341} 342 343static void 344driDestroyScreen(__GLXscreenConfigs * psc) 345{ 346 /* Free the direct rendering per screen data */ 347 (*psc->core->destroyScreen) (psc->__driScreen); 348 psc->__driScreen = NULL; 349 if (psc->driver) 350 dlclose(psc->driver); 351} 352 353static __GLXDRIscreen * 354driCreateScreen(__GLXscreenConfigs * psc, int screen, 355 __GLXdisplayPrivate * priv) 356{ 357 __GLXDRIscreen *psp; 358 const __DRIconfig **driver_configs; 359 const __DRIextension **extensions; 360 const char *driverName = "swrast"; 361 int i; 362 363 psp = Xcalloc(1, sizeof *psp); 364 if (psp == NULL) 365 return NULL; 366 367 /* Initialize per screen dynamic client GLX extensions */ 368 psc->ext_list_first_time = GL_TRUE; 369 370 psc->driver = driOpenDriver(driverName); 371 if (psc->driver == NULL) 372 goto handle_error; 373 374 extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS); 375 if (extensions == NULL) { 376 ErrorMessageF("driver exports no extensions (%s)\n", dlerror()); 377 goto handle_error; 378 } 379 380 for (i = 0; extensions[i]; i++) { 381 if (strcmp(extensions[i]->name, __DRI_CORE) == 0) 382 psc->core = (__DRIcoreExtension *) extensions[i]; 383 if (strcmp(extensions[i]->name, __DRI_SWRAST) == 0) 384 psc->swrast = (__DRIswrastExtension *) extensions[i]; 385 } 386 387 if (psc->core == NULL || psc->swrast == NULL) { 388 ErrorMessageF("core dri extension not found\n"); 389 goto handle_error; 390 } 391 392 psc->__driScreen = 393 psc->swrast->createNewScreen(screen, 394 loader_extensions, &driver_configs, psc); 395 if (psc->__driScreen == NULL) { 396 ErrorMessageF("failed to create dri screen\n"); 397 goto handle_error; 398 } 399 400 driBindExtensions(psc); 401 driBindCommonExtensions(psc); 402 403 psc->configs = driConvertConfigs(psc->core, psc->configs, driver_configs); 404 psc->visuals = driConvertConfigs(psc->core, psc->visuals, driver_configs); 405 406 psc->driver_configs = driver_configs; 407 408 psp->destroyScreen = driDestroyScreen; 409 psp->createContext = driCreateContext; 410 psp->createDrawable = driCreateDrawable; 411 psp->swapBuffers = driSwapBuffers; 412 psp->waitX = NULL; 413 psp->waitGL = NULL; 414 415 return psp; 416 417 handle_error: 418 Xfree(psp); 419 420 if (psc->driver) 421 dlclose(psc->driver); 422 423 ErrorMessageF("reverting to indirect rendering\n"); 424 425 return NULL; 426} 427 428/* Called from __glXFreeDisplayPrivate. 429 */ 430static void 431driDestroyDisplay(__GLXDRIdisplay * dpy) 432{ 433 Xfree(dpy); 434} 435 436/* 437 * Allocate, initialize and return a __DRIdisplayPrivate object. 438 * This is called from __glXInitialize() when we are given a new 439 * display pointer. 440 */ 441_X_HIDDEN __GLXDRIdisplay * 442driswCreateDisplay(Display * dpy) 443{ 444 __GLXDRIdisplayPrivate *pdpyp; 445 446 pdpyp = Xmalloc(sizeof *pdpyp); 447 if (pdpyp == NULL) 448 return NULL; 449 450 pdpyp->base.destroyDisplay = driDestroyDisplay; 451 pdpyp->base.createScreen = driCreateScreen; 452 453 return &pdpyp->base; 454} 455 456#endif /* GLX_DIRECT_RENDERING */ 457