xm_api.c revision ba7218061b6a6c09d5d20f12de6267673276e094
1/* 2 * Mesa 3-D graphics library 3 * Version: 7.1 4 * 5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the "Software"), 9 * to deal in the Software without restriction, including without limitation 10 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 11 * and/or sell copies of the Software, and to permit persons to whom the 12 * Software is furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included 15 * in all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 */ 24 25/** 26 * \file xm_api.c 27 * 28 * All the XMesa* API functions. 29 * 30 * 31 * NOTES: 32 * 33 * The window coordinate system origin (0,0) is in the lower-left corner 34 * of the window. X11's window coordinate origin is in the upper-left 35 * corner of the window. Therefore, most drawing functions in this 36 * file have to flip Y coordinates. 37 * 38 * Define USE_XSHM in the Makefile with -DUSE_XSHM if you want to compile 39 * in support for the MIT Shared Memory extension. If enabled, when you 40 * use an Ximage for the back buffer in double buffered mode, the "swap" 41 * operation will be faster. You must also link with -lXext. 42 * 43 * Byte swapping: If the Mesa host and the X display use a different 44 * byte order then there's some trickiness to be aware of when using 45 * XImages. The byte ordering used for the XImage is that of the X 46 * display, not the Mesa host. 47 * The color-to-pixel encoding for True/DirectColor must be done 48 * according to the display's visual red_mask, green_mask, and blue_mask. 49 * If XPutPixel is used to put a pixel into an XImage then XPutPixel will 50 * do byte swapping if needed. If one wants to directly "poke" the pixel 51 * into the XImage's buffer then the pixel must be byte swapped first. In 52 * Mesa, when byte swapping is needed we use the PF_TRUECOLOR pixel format 53 * and use XPutPixel everywhere except in the implementation of 54 * glClear(GL_COLOR_BUFFER_BIT). We want this function to be fast so 55 * instead of using XPutPixel we "poke" our values after byte-swapping 56 * the clear pixel value if needed. 57 * 58 */ 59 60#ifdef __CYGWIN__ 61#undef WIN32 62#undef __WIN32__ 63#endif 64 65#include "glxheader.h" 66#include "xmesaP.h" 67#include "main/context.h" 68#include "main/extensions.h" 69#include "main/framebuffer.h" 70#include "main/imports.h" 71#include "main/macros.h" 72#include "main/renderbuffer.h" 73#include "main/teximage.h" 74#include "glapi/glthread.h" 75#include "swrast/swrast.h" 76#include "swrast/s_renderbuffer.h" 77#include "swrast_setup/swrast_setup.h" 78#include "vbo/vbo.h" 79#include "tnl/tnl.h" 80#include "tnl/t_context.h" 81#include "tnl/t_pipeline.h" 82#include "drivers/common/driverfuncs.h" 83#include "drivers/common/meta.h" 84 85/** 86 * Global X driver lock 87 */ 88_glthread_Mutex _xmesa_lock; 89 90 91 92/**********************************************************************/ 93/***** X Utility Functions *****/ 94/**********************************************************************/ 95 96 97/** 98 * Return the host's byte order as LSBFirst or MSBFirst ala X. 99 */ 100static int host_byte_order( void ) 101{ 102 int i = 1; 103 char *cptr = (char *) &i; 104 return (*cptr==1) ? LSBFirst : MSBFirst; 105} 106 107 108/** 109 * Check if the X Shared Memory extension is available. 110 * Return: 0 = not available 111 * 1 = shared XImage support available 112 * 2 = shared Pixmap support available also 113 */ 114static int check_for_xshm( XMesaDisplay *display ) 115{ 116#if defined(USE_XSHM) 117 int ignore; 118 119 if (XQueryExtension( display, "MIT-SHM", &ignore, &ignore, &ignore )) { 120 /* Note: we're no longer calling XShmQueryVersion() here. It seems 121 * to be flakey (triggers a spurious X protocol error when we close 122 * one display connection and start using a new one. XShm has been 123 * around a long time and hasn't changed so if MIT_SHM is supported 124 * we assume we're good to go. 125 */ 126 return 2; 127 } 128 else { 129 return 0; 130 } 131#else 132 /* No XSHM support */ 133 return 0; 134#endif 135} 136 137 138/** 139 * Apply gamma correction to an intensity value in [0..max]. Return the 140 * new intensity value. 141 */ 142static GLint 143gamma_adjust( GLfloat gamma, GLint value, GLint max ) 144{ 145 if (gamma == 1.0) { 146 return value; 147 } 148 else { 149 double x = (double) value / (double) max; 150 return IROUND_POS((GLfloat) max * pow(x, 1.0F/gamma)); 151 } 152} 153 154 155 156/** 157 * Return the true number of bits per pixel for XImages. 158 * For example, if we request a 24-bit deep visual we may actually need/get 159 * 32bpp XImages. This function returns the appropriate bpp. 160 * Input: dpy - the X display 161 * visinfo - desribes the visual to be used for XImages 162 * Return: true number of bits per pixel for XImages 163 */ 164static int 165bits_per_pixel( XMesaVisual xmv ) 166{ 167 XMesaDisplay *dpy = xmv->display; 168 XMesaVisualInfo visinfo = xmv->visinfo; 169 XMesaImage *img; 170 int bitsPerPixel; 171 /* Create a temporary XImage */ 172 img = XCreateImage( dpy, visinfo->visual, visinfo->depth, 173 ZPixmap, 0, /*format, offset*/ 174 (char*) MALLOC(8), /*data*/ 175 1, 1, /*width, height*/ 176 32, /*bitmap_pad*/ 177 0 /*bytes_per_line*/ 178 ); 179 assert(img); 180 /* grab the bits/pixel value */ 181 bitsPerPixel = img->bits_per_pixel; 182 /* free the XImage */ 183 free( img->data ); 184 img->data = NULL; 185 XMesaDestroyImage( img ); 186 return bitsPerPixel; 187} 188 189 190 191/* 192 * Determine if a given X window ID is valid (window exists). 193 * Do this by calling XGetWindowAttributes() for the window and 194 * checking if we catch an X error. 195 * Input: dpy - the display 196 * win - the window to check for existance 197 * Return: GL_TRUE - window exists 198 * GL_FALSE - window doesn't exist 199 */ 200static GLboolean WindowExistsFlag; 201 202static int window_exists_err_handler( XMesaDisplay* dpy, XErrorEvent* xerr ) 203{ 204 (void) dpy; 205 if (xerr->error_code == BadWindow) { 206 WindowExistsFlag = GL_FALSE; 207 } 208 return 0; 209} 210 211static GLboolean window_exists( XMesaDisplay *dpy, Window win ) 212{ 213 XWindowAttributes wa; 214 int (*old_handler)( XMesaDisplay*, XErrorEvent* ); 215 WindowExistsFlag = GL_TRUE; 216 old_handler = XSetErrorHandler(window_exists_err_handler); 217 XGetWindowAttributes( dpy, win, &wa ); /* dummy request */ 218 XSetErrorHandler(old_handler); 219 return WindowExistsFlag; 220} 221 222static Status 223get_drawable_size( XMesaDisplay *dpy, Drawable d, GLuint *width, GLuint *height ) 224{ 225 Window root; 226 Status stat; 227 int xpos, ypos; 228 unsigned int w, h, bw, depth; 229 stat = XGetGeometry(dpy, d, &root, &xpos, &ypos, &w, &h, &bw, &depth); 230 *width = w; 231 *height = h; 232 return stat; 233} 234 235 236/** 237 * Return the size of the window (or pixmap) that corresponds to the 238 * given XMesaBuffer. 239 * \param width returns width in pixels 240 * \param height returns height in pixels 241 */ 242void 243xmesa_get_window_size(XMesaDisplay *dpy, XMesaBuffer b, 244 GLuint *width, GLuint *height) 245{ 246 Status stat; 247 248 _glthread_LOCK_MUTEX(_xmesa_lock); 249 XSync(b->xm_visual->display, 0); /* added for Chromium */ 250 stat = get_drawable_size(dpy, b->frontxrb->pixmap, width, height); 251 _glthread_UNLOCK_MUTEX(_xmesa_lock); 252 253 if (!stat) { 254 /* probably querying a window that's recently been destroyed */ 255 _mesa_warning(NULL, "XGetGeometry failed!\n"); 256 *width = *height = 1; 257 } 258} 259 260 261 262/**********************************************************************/ 263/***** Linked list of XMesaBuffers *****/ 264/**********************************************************************/ 265 266XMesaBuffer XMesaBufferList = NULL; 267 268 269/** 270 * Allocate a new XMesaBuffer object which corresponds to the given drawable. 271 * Note that XMesaBuffer is derived from struct gl_framebuffer. 272 * The new XMesaBuffer will not have any size (Width=Height=0). 273 * 274 * \param d the corresponding X drawable (window or pixmap) 275 * \param type either WINDOW, PIXMAP or PBUFFER, describing d 276 * \param vis the buffer's visual 277 * \param cmap the window's colormap, if known. 278 * \return new XMesaBuffer or NULL if any problem 279 */ 280static XMesaBuffer 281create_xmesa_buffer(XMesaDrawable d, BufferType type, 282 XMesaVisual vis, XMesaColormap cmap) 283{ 284 XMesaBuffer b; 285 286 ASSERT(type == WINDOW || type == PIXMAP || type == PBUFFER); 287 288 b = (XMesaBuffer) CALLOC_STRUCT(xmesa_buffer); 289 if (!b) 290 return NULL; 291 292 b->display = vis->display; 293 b->xm_visual = vis; 294 b->type = type; 295 b->cmap = cmap; 296 297 _mesa_initialize_window_framebuffer(&b->mesa_buffer, &vis->mesa_visual); 298 b->mesa_buffer.Delete = xmesa_delete_framebuffer; 299 300 /* 301 * Front renderbuffer 302 */ 303 b->frontxrb = xmesa_new_renderbuffer(NULL, 0, vis, GL_FALSE); 304 if (!b->frontxrb) { 305 free(b); 306 return NULL; 307 } 308 b->frontxrb->Parent = b; 309 b->frontxrb->drawable = d; 310 b->frontxrb->pixmap = (XMesaPixmap) d; 311 _mesa_add_renderbuffer(&b->mesa_buffer, BUFFER_FRONT_LEFT, 312 &b->frontxrb->Base.Base); 313 314 /* 315 * Back renderbuffer 316 */ 317 if (vis->mesa_visual.doubleBufferMode) { 318 b->backxrb = xmesa_new_renderbuffer(NULL, 0, vis, GL_TRUE); 319 if (!b->backxrb) { 320 /* XXX free front xrb too */ 321 free(b); 322 return NULL; 323 } 324 b->backxrb->Parent = b; 325 /* determine back buffer implementation */ 326 b->db_mode = vis->ximage_flag ? BACK_XIMAGE : BACK_PIXMAP; 327 328 _mesa_add_renderbuffer(&b->mesa_buffer, BUFFER_BACK_LEFT, 329 &b->backxrb->Base.Base); 330 } 331 332 /* 333 * Other renderbuffer (depth, stencil, etc) 334 */ 335 _swrast_add_soft_renderbuffers(&b->mesa_buffer, 336 GL_FALSE, /* color */ 337 vis->mesa_visual.haveDepthBuffer, 338 vis->mesa_visual.haveStencilBuffer, 339 vis->mesa_visual.haveAccumBuffer, 340 GL_FALSE, /* software alpha buffer */ 341 vis->mesa_visual.numAuxBuffers > 0 ); 342 343 /* GLX_EXT_texture_from_pixmap */ 344 b->TextureTarget = 0; 345 b->TextureFormat = GLX_TEXTURE_FORMAT_NONE_EXT; 346 b->TextureMipmap = 0; 347 348 /* insert buffer into linked list */ 349 b->Next = XMesaBufferList; 350 XMesaBufferList = b; 351 352 return b; 353} 354 355 356/** 357 * Find an XMesaBuffer by matching X display and colormap but NOT matching 358 * the notThis buffer. 359 */ 360XMesaBuffer 361xmesa_find_buffer(XMesaDisplay *dpy, XMesaColormap cmap, XMesaBuffer notThis) 362{ 363 XMesaBuffer b; 364 for (b=XMesaBufferList; b; b=b->Next) { 365 if (b->display==dpy && b->cmap==cmap && b!=notThis) { 366 return b; 367 } 368 } 369 return NULL; 370} 371 372 373/** 374 * Remove buffer from linked list, delete if no longer referenced. 375 */ 376static void 377xmesa_free_buffer(XMesaBuffer buffer) 378{ 379 XMesaBuffer prev = NULL, b; 380 381 for (b = XMesaBufferList; b; b = b->Next) { 382 if (b == buffer) { 383 struct gl_framebuffer *fb = &buffer->mesa_buffer; 384 385 /* unlink buffer from list */ 386 if (prev) 387 prev->Next = buffer->Next; 388 else 389 XMesaBufferList = buffer->Next; 390 391 /* mark as delete pending */ 392 fb->DeletePending = GL_TRUE; 393 394 /* Since the X window for the XMesaBuffer is going away, we don't 395 * want to dereference this pointer in the future. 396 */ 397 b->frontxrb->drawable = 0; 398 399 /* Unreference. If count = zero we'll really delete the buffer */ 400 _mesa_reference_framebuffer(&fb, NULL); 401 402 return; 403 } 404 /* continue search */ 405 prev = b; 406 } 407 /* buffer not found in XMesaBufferList */ 408 _mesa_problem(NULL,"xmesa_free_buffer() - buffer not found\n"); 409} 410 411 412 413 414/**********************************************************************/ 415/***** Misc Private Functions *****/ 416/**********************************************************************/ 417 418 419/** 420 * Setup RGB rendering for a window with a True/DirectColor visual. 421 */ 422static void 423setup_truecolor(XMesaVisual v, XMesaBuffer buffer, XMesaColormap cmap) 424{ 425 unsigned long rmask, gmask, bmask; 426 (void) buffer; 427 (void) cmap; 428 429 /* Compute red multiplier (mask) and bit shift */ 430 v->rshift = 0; 431 rmask = GET_REDMASK(v); 432 while ((rmask & 1)==0) { 433 v->rshift++; 434 rmask = rmask >> 1; 435 } 436 437 /* Compute green multiplier (mask) and bit shift */ 438 v->gshift = 0; 439 gmask = GET_GREENMASK(v); 440 while ((gmask & 1)==0) { 441 v->gshift++; 442 gmask = gmask >> 1; 443 } 444 445 /* Compute blue multiplier (mask) and bit shift */ 446 v->bshift = 0; 447 bmask = GET_BLUEMASK(v); 448 while ((bmask & 1)==0) { 449 v->bshift++; 450 bmask = bmask >> 1; 451 } 452 453 /* 454 * Compute component-to-pixel lookup tables and dithering kernel 455 */ 456 { 457 static GLubyte kernel[16] = { 458 0*16, 8*16, 2*16, 10*16, 459 12*16, 4*16, 14*16, 6*16, 460 3*16, 11*16, 1*16, 9*16, 461 15*16, 7*16, 13*16, 5*16, 462 }; 463 GLint rBits = _mesa_bitcount(rmask); 464 GLint gBits = _mesa_bitcount(gmask); 465 GLint bBits = _mesa_bitcount(bmask); 466 GLint maxBits; 467 GLuint i; 468 469 /* convert pixel components in [0,_mask] to RGB values in [0,255] */ 470 for (i=0; i<=rmask; i++) 471 v->PixelToR[i] = (unsigned char) ((i * 255) / rmask); 472 for (i=0; i<=gmask; i++) 473 v->PixelToG[i] = (unsigned char) ((i * 255) / gmask); 474 for (i=0; i<=bmask; i++) 475 v->PixelToB[i] = (unsigned char) ((i * 255) / bmask); 476 477 /* convert RGB values from [0,255] to pixel components */ 478 479 for (i=0;i<256;i++) { 480 GLint r = gamma_adjust(v->RedGamma, i, 255); 481 GLint g = gamma_adjust(v->GreenGamma, i, 255); 482 GLint b = gamma_adjust(v->BlueGamma, i, 255); 483 v->RtoPixel[i] = (r >> (8-rBits)) << v->rshift; 484 v->GtoPixel[i] = (g >> (8-gBits)) << v->gshift; 485 v->BtoPixel[i] = (b >> (8-bBits)) << v->bshift; 486 } 487 /* overflow protection */ 488 for (i=256;i<512;i++) { 489 v->RtoPixel[i] = v->RtoPixel[255]; 490 v->GtoPixel[i] = v->GtoPixel[255]; 491 v->BtoPixel[i] = v->BtoPixel[255]; 492 } 493 494 /* setup dithering kernel */ 495 maxBits = rBits; 496 if (gBits > maxBits) maxBits = gBits; 497 if (bBits > maxBits) maxBits = bBits; 498 for (i=0;i<16;i++) { 499 v->Kernel[i] = kernel[i] >> maxBits; 500 } 501 502 v->undithered_pf = PF_Truecolor; 503 v->dithered_pf = (GET_VISUAL_DEPTH(v)<24) ? PF_Dither_True : PF_Truecolor; 504 } 505 506 /* 507 * Now check for TrueColor visuals which we can optimize. 508 */ 509 if ( GET_REDMASK(v) ==0x0000ff 510 && GET_GREENMASK(v)==0x00ff00 511 && GET_BLUEMASK(v) ==0xff0000 512 && CHECK_BYTE_ORDER(v) 513 && v->BitsPerPixel==32 514 && v->RedGamma==1.0 && v->GreenGamma==1.0 && v->BlueGamma==1.0) { 515 /* common 32 bpp config used on SGI, Sun */ 516 v->undithered_pf = v->dithered_pf = PF_8A8B8G8R; /* ABGR */ 517 } 518 else if (GET_REDMASK(v) == 0xff0000 519 && GET_GREENMASK(v)== 0x00ff00 520 && GET_BLUEMASK(v) == 0x0000ff 521 && CHECK_BYTE_ORDER(v) 522 && v->RedGamma == 1.0 && v->GreenGamma == 1.0 && v->BlueGamma == 1.0){ 523 if (v->BitsPerPixel==32) { 524 /* if 32 bpp, and visual indicates 8 bpp alpha channel */ 525 if (GET_VISUAL_DEPTH(v) == 32 && v->mesa_visual.alphaBits == 8) 526 v->undithered_pf = v->dithered_pf = PF_8A8R8G8B; /* ARGB */ 527 else 528 v->undithered_pf = v->dithered_pf = PF_8R8G8B; /* xRGB */ 529 } 530 else if (v->BitsPerPixel == 24) { 531 v->undithered_pf = v->dithered_pf = PF_8R8G8B24; /* RGB */ 532 } 533 } 534 else if (GET_REDMASK(v) ==0xf800 535 && GET_GREENMASK(v)==0x07e0 536 && GET_BLUEMASK(v) ==0x001f 537 && CHECK_BYTE_ORDER(v) 538 && v->BitsPerPixel==16 539 && v->RedGamma==1.0 && v->GreenGamma==1.0 && v->BlueGamma==1.0) { 540 /* 5-6-5 RGB */ 541 v->undithered_pf = PF_5R6G5B; 542 v->dithered_pf = PF_Dither_5R6G5B; 543 } 544} 545 546 547/** 548 * When a context is bound for the first time, we can finally finish 549 * initializing the context's visual and buffer information. 550 * \param v the XMesaVisual to initialize 551 * \param b the XMesaBuffer to initialize (may be NULL) 552 * \param rgb_flag TRUE = RGBA mode, FALSE = color index mode 553 * \param window the window/pixmap we're rendering into 554 * \param cmap the colormap associated with the window/pixmap 555 * \return GL_TRUE=success, GL_FALSE=failure 556 */ 557static GLboolean 558initialize_visual_and_buffer(XMesaVisual v, XMesaBuffer b, 559 XMesaDrawable window, 560 XMesaColormap cmap) 561{ 562 const int xclass = v->visualType; 563 564 565 ASSERT(!b || b->xm_visual == v); 566 567 /* Save true bits/pixel */ 568 v->BitsPerPixel = bits_per_pixel(v); 569 assert(v->BitsPerPixel > 0); 570 571 /* RGB WINDOW: 572 * We support RGB rendering into almost any kind of visual. 573 */ 574 if (xclass == GLX_TRUE_COLOR || xclass == GLX_DIRECT_COLOR) { 575 setup_truecolor( v, b, cmap ); 576 } 577 else { 578 _mesa_warning(NULL, "XMesa: RGB mode rendering not supported in given visual.\n"); 579 return GL_FALSE; 580 } 581 v->mesa_visual.indexBits = 0; 582 583 if (_mesa_getenv("MESA_NO_DITHER")) { 584 v->dithered_pf = v->undithered_pf; 585 } 586 587 588 /* 589 * If MESA_INFO env var is set print out some debugging info 590 * which can help Brian figure out what's going on when a user 591 * reports bugs. 592 */ 593 if (_mesa_getenv("MESA_INFO")) { 594 printf("X/Mesa visual = %p\n", (void *) v); 595 printf("X/Mesa dithered pf = %u\n", v->dithered_pf); 596 printf("X/Mesa undithered pf = %u\n", v->undithered_pf); 597 printf("X/Mesa level = %d\n", v->mesa_visual.level); 598 printf("X/Mesa depth = %d\n", GET_VISUAL_DEPTH(v)); 599 printf("X/Mesa bits per pixel = %d\n", v->BitsPerPixel); 600 } 601 602 if (b && window) { 603 /* Do window-specific initializations */ 604 605 /* these should have been set in create_xmesa_buffer */ 606 ASSERT(b->frontxrb->drawable == window); 607 ASSERT(b->frontxrb->pixmap == (XMesaPixmap) window); 608 609 /* Setup for single/double buffering */ 610 if (v->mesa_visual.doubleBufferMode) { 611 /* Double buffered */ 612 b->shm = check_for_xshm( v->display ); 613 } 614 615 /* X11 graphics contexts */ 616 b->gc = XCreateGC( v->display, window, 0, NULL ); 617 XMesaSetFunction( v->display, b->gc, GXcopy ); 618 619 /* cleargc - for glClear() */ 620 b->cleargc = XCreateGC( v->display, window, 0, NULL ); 621 XMesaSetFunction( v->display, b->cleargc, GXcopy ); 622 623 /* 624 * Don't generate Graphics Expose/NoExpose events in swapbuffers(). 625 * Patch contributed by Michael Pichler May 15, 1995. 626 */ 627 { 628 XGCValues gcvalues; 629 gcvalues.graphics_exposures = False; 630 b->swapgc = XCreateGC(v->display, window, 631 GCGraphicsExposures, &gcvalues); 632 } 633 XMesaSetFunction( v->display, b->swapgc, GXcopy ); 634 } 635 636 return GL_TRUE; 637} 638 639 640 641/* 642 * Convert an RGBA color to a pixel value. 643 */ 644unsigned long 645xmesa_color_to_pixel(struct gl_context *ctx, 646 GLubyte r, GLubyte g, GLubyte b, GLubyte a, 647 GLuint pixelFormat) 648{ 649 XMesaContext xmesa = XMESA_CONTEXT(ctx); 650 switch (pixelFormat) { 651 case PF_Truecolor: 652 { 653 unsigned long p; 654 PACK_TRUECOLOR( p, r, g, b ); 655 return p; 656 } 657 case PF_8A8B8G8R: 658 return PACK_8A8B8G8R( r, g, b, a ); 659 case PF_8A8R8G8B: 660 return PACK_8A8R8G8B( r, g, b, a ); 661 case PF_8R8G8B: 662 /* fall through */ 663 case PF_8R8G8B24: 664 return PACK_8R8G8B( r, g, b ); 665 case PF_5R6G5B: 666 return PACK_5R6G5B( r, g, b ); 667 case PF_Dither_True: 668 /* fall through */ 669 case PF_Dither_5R6G5B: 670 { 671 unsigned long p; 672 PACK_TRUEDITHER(p, 1, 0, r, g, b); 673 return p; 674 } 675 default: 676 _mesa_problem(ctx, "Bad pixel format in xmesa_color_to_pixel"); 677 } 678 return 0; 679} 680 681 682#define NUM_VISUAL_TYPES 6 683 684/** 685 * Convert an X visual type to a GLX visual type. 686 * 687 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.) 688 * to be converted. 689 * \return If \c visualType is a valid X visual type, a GLX visual type will 690 * be returned. Otherwise \c GLX_NONE will be returned. 691 * 692 * \note 693 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the 694 * DRI CVS tree. 695 */ 696static GLint 697xmesa_convert_from_x_visual_type( int visualType ) 698{ 699 static const int glx_visual_types[ NUM_VISUAL_TYPES ] = { 700 GLX_STATIC_GRAY, GLX_GRAY_SCALE, 701 GLX_STATIC_COLOR, GLX_PSEUDO_COLOR, 702 GLX_TRUE_COLOR, GLX_DIRECT_COLOR 703 }; 704 705 return ( (unsigned) visualType < NUM_VISUAL_TYPES ) 706 ? glx_visual_types[ visualType ] : GLX_NONE; 707} 708 709 710/**********************************************************************/ 711/***** Public Functions *****/ 712/**********************************************************************/ 713 714 715/* 716 * Create a new X/Mesa visual. 717 * Input: display - X11 display 718 * visinfo - an XVisualInfo pointer 719 * rgb_flag - GL_TRUE = RGB mode, 720 * GL_FALSE = color index mode 721 * alpha_flag - alpha buffer requested? 722 * db_flag - GL_TRUE = double-buffered, 723 * GL_FALSE = single buffered 724 * stereo_flag - stereo visual? 725 * ximage_flag - GL_TRUE = use an XImage for back buffer, 726 * GL_FALSE = use an off-screen pixmap for back buffer 727 * depth_size - requested bits/depth values, or zero 728 * stencil_size - requested bits/stencil values, or zero 729 * accum_red_size - requested bits/red accum values, or zero 730 * accum_green_size - requested bits/green accum values, or zero 731 * accum_blue_size - requested bits/blue accum values, or zero 732 * accum_alpha_size - requested bits/alpha accum values, or zero 733 * num_samples - number of samples/pixel if multisampling, or zero 734 * level - visual level, usually 0 735 * visualCaveat - ala the GLX extension, usually GLX_NONE 736 * Return; a new XMesaVisual or 0 if error. 737 */ 738PUBLIC 739XMesaVisual XMesaCreateVisual( XMesaDisplay *display, 740 XMesaVisualInfo visinfo, 741 GLboolean rgb_flag, 742 GLboolean alpha_flag, 743 GLboolean db_flag, 744 GLboolean stereo_flag, 745 GLboolean ximage_flag, 746 GLint depth_size, 747 GLint stencil_size, 748 GLint accum_red_size, 749 GLint accum_green_size, 750 GLint accum_blue_size, 751 GLint accum_alpha_size, 752 GLint num_samples, 753 GLint level, 754 GLint visualCaveat ) 755{ 756 char *gamma; 757 XMesaVisual v; 758 GLint red_bits, green_bits, blue_bits, alpha_bits; 759 760 /* For debugging only */ 761 if (_mesa_getenv("MESA_XSYNC")) { 762 /* This makes debugging X easier. 763 * In your debugger, set a breakpoint on _XError to stop when an 764 * X protocol error is generated. 765 */ 766 XSynchronize( display, 1 ); 767 } 768 769 /* Color-index rendering not supported. */ 770 if (!rgb_flag) 771 return NULL; 772 773 v = (XMesaVisual) CALLOC_STRUCT(xmesa_visual); 774 if (!v) { 775 return NULL; 776 } 777 778 v->display = display; 779 780 /* Save a copy of the XVisualInfo struct because the user may Xfree() 781 * the struct but we may need some of the information contained in it 782 * at a later time. 783 */ 784 v->visinfo = (XVisualInfo *) MALLOC(sizeof(*visinfo)); 785 if(!v->visinfo) { 786 free(v); 787 return NULL; 788 } 789 memcpy(v->visinfo, visinfo, sizeof(*visinfo)); 790 791 /* check for MESA_GAMMA environment variable */ 792 gamma = _mesa_getenv("MESA_GAMMA"); 793 if (gamma) { 794 v->RedGamma = v->GreenGamma = v->BlueGamma = 0.0; 795 sscanf( gamma, "%f %f %f", &v->RedGamma, &v->GreenGamma, &v->BlueGamma ); 796 if (v->RedGamma<=0.0) v->RedGamma = 1.0; 797 if (v->GreenGamma<=0.0) v->GreenGamma = v->RedGamma; 798 if (v->BlueGamma<=0.0) v->BlueGamma = v->RedGamma; 799 } 800 else { 801 v->RedGamma = v->GreenGamma = v->BlueGamma = 1.0; 802 } 803 804 v->ximage_flag = ximage_flag; 805 806 v->mesa_visual.redMask = visinfo->red_mask; 807 v->mesa_visual.greenMask = visinfo->green_mask; 808 v->mesa_visual.blueMask = visinfo->blue_mask; 809 v->visualID = visinfo->visualid; 810 v->screen = visinfo->screen; 811 812#if !(defined(__cplusplus) || defined(c_plusplus)) 813 v->visualType = xmesa_convert_from_x_visual_type(visinfo->class); 814#else 815 v->visualType = xmesa_convert_from_x_visual_type(visinfo->c_class); 816#endif 817 818 v->mesa_visual.visualRating = visualCaveat; 819 820 if (alpha_flag) 821 v->mesa_visual.alphaBits = 8; 822 823 (void) initialize_visual_and_buffer( v, NULL, 0, 0 ); 824 825 { 826 const int xclass = v->visualType; 827 if (xclass == GLX_TRUE_COLOR || xclass == GLX_DIRECT_COLOR) { 828 red_bits = _mesa_bitcount(GET_REDMASK(v)); 829 green_bits = _mesa_bitcount(GET_GREENMASK(v)); 830 blue_bits = _mesa_bitcount(GET_BLUEMASK(v)); 831 } 832 else { 833 /* this is an approximation */ 834 int depth; 835 depth = GET_VISUAL_DEPTH(v); 836 red_bits = depth / 3; 837 depth -= red_bits; 838 green_bits = depth / 2; 839 depth -= green_bits; 840 blue_bits = depth; 841 alpha_bits = 0; 842 assert( red_bits + green_bits + blue_bits == GET_VISUAL_DEPTH(v) ); 843 } 844 alpha_bits = v->mesa_visual.alphaBits; 845 } 846 847 _mesa_initialize_visual( &v->mesa_visual, 848 db_flag, stereo_flag, 849 red_bits, green_bits, 850 blue_bits, alpha_bits, 851 depth_size, 852 stencil_size, 853 accum_red_size, accum_green_size, 854 accum_blue_size, accum_alpha_size, 855 0 ); 856 857 /* XXX minor hack */ 858 v->mesa_visual.level = level; 859 return v; 860} 861 862 863PUBLIC 864void XMesaDestroyVisual( XMesaVisual v ) 865{ 866 free(v->visinfo); 867 free(v); 868} 869 870 871 872/** 873 * Create a new XMesaContext. 874 * \param v the XMesaVisual 875 * \param share_list another XMesaContext with which to share display 876 * lists or NULL if no sharing is wanted. 877 * \return an XMesaContext or NULL if error. 878 */ 879PUBLIC 880XMesaContext XMesaCreateContext( XMesaVisual v, XMesaContext share_list ) 881{ 882 static GLboolean firstTime = GL_TRUE; 883 XMesaContext c; 884 struct gl_context *mesaCtx; 885 struct dd_function_table functions; 886 TNLcontext *tnl; 887 888 if (firstTime) { 889 _glthread_INIT_MUTEX(_xmesa_lock); 890 firstTime = GL_FALSE; 891 } 892 893 /* Note: the XMesaContext contains a Mesa struct gl_context struct (inheritance) */ 894 c = (XMesaContext) CALLOC_STRUCT(xmesa_context); 895 if (!c) 896 return NULL; 897 898 mesaCtx = &(c->mesa); 899 900 /* initialize with default driver functions, then plug in XMesa funcs */ 901 _mesa_init_driver_functions(&functions); 902 xmesa_init_driver_functions(v, &functions); 903 if (!_mesa_initialize_context(mesaCtx, API_OPENGL, &v->mesa_visual, 904 share_list ? &(share_list->mesa) : (struct gl_context *) NULL, 905 &functions, (void *) c)) { 906 free(c); 907 return NULL; 908 } 909 910 /* Enable this to exercise fixed function -> shader translation 911 * with software rendering. 912 */ 913 if (0) { 914 mesaCtx->VertexProgram._MaintainTnlProgram = GL_TRUE; 915 mesaCtx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE; 916 } 917 918 _mesa_enable_sw_extensions(mesaCtx); 919 _mesa_enable_1_3_extensions(mesaCtx); 920 _mesa_enable_1_4_extensions(mesaCtx); 921 _mesa_enable_1_5_extensions(mesaCtx); 922 _mesa_enable_2_0_extensions(mesaCtx); 923 _mesa_enable_2_1_extensions(mesaCtx); 924 if (mesaCtx->Mesa_DXTn) { 925 _mesa_enable_extension(mesaCtx, "GL_EXT_texture_compression_s3tc"); 926 _mesa_enable_extension(mesaCtx, "GL_S3_s3tc"); 927 } 928 _mesa_enable_extension(mesaCtx, "GL_3DFX_texture_compression_FXT1"); 929#if ENABLE_EXT_timer_query 930 _mesa_enable_extension(mesaCtx, "GL_EXT_timer_query"); 931#endif 932 933 934 /* finish up xmesa context initializations */ 935 c->swapbytes = CHECK_BYTE_ORDER(v) ? GL_FALSE : GL_TRUE; 936 c->xm_visual = v; 937 c->xm_buffer = NULL; /* set later by XMesaMakeCurrent */ 938 c->display = v->display; 939 c->pixelformat = v->dithered_pf; /* Dithering is enabled by default */ 940 941 /* Initialize the software rasterizer and helper modules. 942 */ 943 if (!_swrast_CreateContext( mesaCtx ) || 944 !_vbo_CreateContext( mesaCtx ) || 945 !_tnl_CreateContext( mesaCtx ) || 946 !_swsetup_CreateContext( mesaCtx )) { 947 _mesa_free_context_data(&c->mesa); 948 free(c); 949 return NULL; 950 } 951 952 /* tnl setup */ 953 tnl = TNL_CONTEXT(mesaCtx); 954 tnl->Driver.RunPipeline = _tnl_run_pipeline; 955 /* swrast setup */ 956 xmesa_register_swrast_functions( mesaCtx ); 957 _swsetup_Wakeup(mesaCtx); 958 959 _mesa_meta_init(mesaCtx); 960 961 return c; 962} 963 964 965 966PUBLIC 967void XMesaDestroyContext( XMesaContext c ) 968{ 969 struct gl_context *mesaCtx = &c->mesa; 970 971 _mesa_meta_free( mesaCtx ); 972 973 _swsetup_DestroyContext( mesaCtx ); 974 _swrast_DestroyContext( mesaCtx ); 975 _tnl_DestroyContext( mesaCtx ); 976 _vbo_DestroyContext( mesaCtx ); 977 _mesa_free_context_data( mesaCtx ); 978 free( c ); 979} 980 981 982 983/** 984 * Private function for creating an XMesaBuffer which corresponds to an 985 * X window or pixmap. 986 * \param v the window's XMesaVisual 987 * \param w the window we're wrapping 988 * \return new XMesaBuffer or NULL if error 989 */ 990PUBLIC XMesaBuffer 991XMesaCreateWindowBuffer(XMesaVisual v, XMesaWindow w) 992{ 993 XWindowAttributes attr; 994 XMesaBuffer b; 995 XMesaColormap cmap; 996 int depth; 997 998 assert(v); 999 assert(w); 1000 1001 /* Check that window depth matches visual depth */ 1002 XGetWindowAttributes( v->display, w, &attr ); 1003 depth = attr.depth; 1004 if (GET_VISUAL_DEPTH(v) != depth) { 1005 _mesa_warning(NULL, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n", 1006 GET_VISUAL_DEPTH(v), depth); 1007 return NULL; 1008 } 1009 1010 /* Find colormap */ 1011 if (attr.colormap) { 1012 cmap = attr.colormap; 1013 } 1014 else { 1015 _mesa_warning(NULL, "Window %u has no colormap!\n", (unsigned int) w); 1016 /* this is weird, a window w/out a colormap!? */ 1017 /* OK, let's just allocate a new one and hope for the best */ 1018 cmap = XCreateColormap(v->display, w, attr.visual, AllocNone); 1019 } 1020 1021 b = create_xmesa_buffer((XMesaDrawable) w, WINDOW, v, cmap); 1022 if (!b) 1023 return NULL; 1024 1025 if (!initialize_visual_and_buffer( v, b, (XMesaDrawable) w, cmap )) { 1026 xmesa_free_buffer(b); 1027 return NULL; 1028 } 1029 1030 return b; 1031} 1032 1033 1034 1035/** 1036 * Create a new XMesaBuffer from an X pixmap. 1037 * 1038 * \param v the XMesaVisual 1039 * \param p the pixmap 1040 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or 1041 * \c GLX_DIRECT_COLOR visual for the pixmap 1042 * \returns new XMesaBuffer or NULL if error 1043 */ 1044PUBLIC XMesaBuffer 1045XMesaCreatePixmapBuffer(XMesaVisual v, XMesaPixmap p, XMesaColormap cmap) 1046{ 1047 XMesaBuffer b; 1048 1049 assert(v); 1050 1051 b = create_xmesa_buffer((XMesaDrawable) p, PIXMAP, v, cmap); 1052 if (!b) 1053 return NULL; 1054 1055 if (!initialize_visual_and_buffer(v, b, (XMesaDrawable) p, cmap)) { 1056 xmesa_free_buffer(b); 1057 return NULL; 1058 } 1059 1060 return b; 1061} 1062 1063 1064/** 1065 * For GLX_EXT_texture_from_pixmap 1066 */ 1067XMesaBuffer 1068XMesaCreatePixmapTextureBuffer(XMesaVisual v, XMesaPixmap p, 1069 XMesaColormap cmap, 1070 int format, int target, int mipmap) 1071{ 1072 GET_CURRENT_CONTEXT(ctx); 1073 XMesaBuffer b; 1074 GLuint width, height; 1075 1076 assert(v); 1077 1078 b = create_xmesa_buffer((XMesaDrawable) p, PIXMAP, v, cmap); 1079 if (!b) 1080 return NULL; 1081 1082 /* get pixmap size, update framebuffer/renderbuffer dims */ 1083 xmesa_get_window_size(v->display, b, &width, &height); 1084 _mesa_resize_framebuffer(NULL, &(b->mesa_buffer), width, height); 1085 1086 if (target == 0) { 1087 /* examine dims */ 1088 if (ctx->Extensions.ARB_texture_non_power_of_two) { 1089 target = GLX_TEXTURE_2D_EXT; 1090 } 1091 else if ( _mesa_bitcount(width) == 1 1092 && _mesa_bitcount(height) == 1) { 1093 /* power of two size */ 1094 if (height == 1) { 1095 target = GLX_TEXTURE_1D_EXT; 1096 } 1097 else { 1098 target = GLX_TEXTURE_2D_EXT; 1099 } 1100 } 1101 else if (ctx->Extensions.NV_texture_rectangle) { 1102 target = GLX_TEXTURE_RECTANGLE_EXT; 1103 } 1104 else { 1105 /* non power of two textures not supported */ 1106 XMesaDestroyBuffer(b); 1107 return 0; 1108 } 1109 } 1110 1111 b->TextureTarget = target; 1112 b->TextureFormat = format; 1113 b->TextureMipmap = mipmap; 1114 1115 if (!initialize_visual_and_buffer(v, b, (XMesaDrawable) p, cmap)) { 1116 xmesa_free_buffer(b); 1117 return NULL; 1118 } 1119 1120 return b; 1121} 1122 1123 1124 1125XMesaBuffer 1126XMesaCreatePBuffer(XMesaVisual v, XMesaColormap cmap, 1127 unsigned int width, unsigned int height) 1128{ 1129 XMesaWindow root; 1130 XMesaDrawable drawable; /* X Pixmap Drawable */ 1131 XMesaBuffer b; 1132 1133 /* allocate pixmap for front buffer */ 1134 root = RootWindow( v->display, v->visinfo->screen ); 1135 drawable = XCreatePixmap(v->display, root, width, height, 1136 v->visinfo->depth); 1137 if (!drawable) 1138 return NULL; 1139 1140 b = create_xmesa_buffer(drawable, PBUFFER, v, cmap); 1141 if (!b) 1142 return NULL; 1143 1144 if (!initialize_visual_and_buffer(v, b, drawable, cmap)) { 1145 xmesa_free_buffer(b); 1146 return NULL; 1147 } 1148 1149 return b; 1150} 1151 1152 1153 1154/* 1155 * Deallocate an XMesaBuffer structure and all related info. 1156 */ 1157PUBLIC void 1158XMesaDestroyBuffer(XMesaBuffer b) 1159{ 1160 xmesa_free_buffer(b); 1161} 1162 1163 1164/** 1165 * Query the current window size and update the corresponding struct gl_framebuffer 1166 * and all attached renderbuffers. 1167 * Called when: 1168 * 1. the first time a buffer is bound to a context. 1169 * 2. from glViewport to poll for window size changes 1170 * 3. from the XMesaResizeBuffers() API function. 1171 * Note: it's possible (and legal) for xmctx to be NULL. That can happen 1172 * when resizing a buffer when no rendering context is bound. 1173 */ 1174void 1175xmesa_check_and_update_buffer_size(XMesaContext xmctx, XMesaBuffer drawBuffer) 1176{ 1177 GLuint width, height; 1178 xmesa_get_window_size(drawBuffer->display, drawBuffer, &width, &height); 1179 if (drawBuffer->mesa_buffer.Width != width || 1180 drawBuffer->mesa_buffer.Height != height) { 1181 struct gl_context *ctx = xmctx ? &xmctx->mesa : NULL; 1182 _mesa_resize_framebuffer(ctx, &(drawBuffer->mesa_buffer), width, height); 1183 } 1184 drawBuffer->mesa_buffer.Initialized = GL_TRUE; /* XXX TEMPORARY? */ 1185} 1186 1187 1188/* 1189 * Bind buffer b to context c and make c the current rendering context. 1190 */ 1191GLboolean XMesaMakeCurrent( XMesaContext c, XMesaBuffer b ) 1192{ 1193 return XMesaMakeCurrent2( c, b, b ); 1194} 1195 1196 1197/* 1198 * Bind buffer b to context c and make c the current rendering context. 1199 */ 1200PUBLIC 1201GLboolean XMesaMakeCurrent2( XMesaContext c, XMesaBuffer drawBuffer, 1202 XMesaBuffer readBuffer ) 1203{ 1204 if (c) { 1205 if (!drawBuffer || !readBuffer) 1206 return GL_FALSE; /* must specify buffers! */ 1207 1208 if (&(c->mesa) == _mesa_get_current_context() 1209 && c->mesa.DrawBuffer == &drawBuffer->mesa_buffer 1210 && c->mesa.ReadBuffer == &readBuffer->mesa_buffer 1211 && XMESA_BUFFER(c->mesa.DrawBuffer)->wasCurrent) { 1212 /* same context and buffer, do nothing */ 1213 return GL_TRUE; 1214 } 1215 1216 c->xm_buffer = drawBuffer; 1217 1218 /* Call this periodically to detect when the user has begun using 1219 * GL rendering from multiple threads. 1220 */ 1221 _glapi_check_multithread(); 1222 1223 xmesa_check_and_update_buffer_size(c, drawBuffer); 1224 if (readBuffer != drawBuffer) 1225 xmesa_check_and_update_buffer_size(c, readBuffer); 1226 1227 _mesa_make_current(&(c->mesa), 1228 &drawBuffer->mesa_buffer, 1229 &readBuffer->mesa_buffer); 1230 1231 /* 1232 * Must recompute and set these pixel values because colormap 1233 * can be different for different windows. 1234 */ 1235 c->clearpixel = xmesa_color_to_pixel( &c->mesa, 1236 c->clearcolor[0], 1237 c->clearcolor[1], 1238 c->clearcolor[2], 1239 c->clearcolor[3], 1240 c->xm_visual->undithered_pf); 1241 XMesaSetForeground(c->display, drawBuffer->cleargc, c->clearpixel); 1242 1243 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */ 1244 drawBuffer->wasCurrent = GL_TRUE; 1245 } 1246 else { 1247 /* Detach */ 1248 _mesa_make_current( NULL, NULL, NULL ); 1249 } 1250 return GL_TRUE; 1251} 1252 1253 1254/* 1255 * Unbind the context c from its buffer. 1256 */ 1257GLboolean XMesaUnbindContext( XMesaContext c ) 1258{ 1259 /* A no-op for XFree86 integration purposes */ 1260 return GL_TRUE; 1261} 1262 1263 1264XMesaContext XMesaGetCurrentContext( void ) 1265{ 1266 GET_CURRENT_CONTEXT(ctx); 1267 if (ctx) { 1268 XMesaContext xmesa = XMESA_CONTEXT(ctx); 1269 return xmesa; 1270 } 1271 else { 1272 return 0; 1273 } 1274} 1275 1276 1277XMesaBuffer XMesaGetCurrentBuffer( void ) 1278{ 1279 GET_CURRENT_CONTEXT(ctx); 1280 if (ctx) { 1281 XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer); 1282 return xmbuf; 1283 } 1284 else { 1285 return 0; 1286 } 1287} 1288 1289 1290/* New in Mesa 3.1 */ 1291XMesaBuffer XMesaGetCurrentReadBuffer( void ) 1292{ 1293 GET_CURRENT_CONTEXT(ctx); 1294 if (ctx) { 1295 return XMESA_BUFFER(ctx->ReadBuffer); 1296 } 1297 else { 1298 return 0; 1299 } 1300} 1301 1302 1303 1304GLboolean XMesaSetFXmode( GLint mode ) 1305{ 1306 (void) mode; 1307 return GL_FALSE; 1308} 1309 1310 1311 1312/* 1313 * Copy the back buffer to the front buffer. If there's no back buffer 1314 * this is a no-op. 1315 */ 1316PUBLIC 1317void XMesaSwapBuffers( XMesaBuffer b ) 1318{ 1319 GET_CURRENT_CONTEXT(ctx); 1320 1321 if (!b->backxrb) { 1322 /* single buffered */ 1323 return; 1324 } 1325 1326 /* If we're swapping the buffer associated with the current context 1327 * we have to flush any pending rendering commands first. 1328 */ 1329 if (ctx && ctx->DrawBuffer == &(b->mesa_buffer)) 1330 _mesa_notifySwapBuffers(ctx); 1331 1332 if (b->db_mode) { 1333 if (b->backxrb->ximage) { 1334 /* Copy Ximage (back buf) from client memory to server window */ 1335#if defined(USE_XSHM) 1336 if (b->shm) { 1337 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/ 1338 XShmPutImage( b->xm_visual->display, b->frontxrb->drawable, 1339 b->swapgc, 1340 b->backxrb->ximage, 0, 0, 1341 0, 0, b->mesa_buffer.Width, b->mesa_buffer.Height, 1342 False ); 1343 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/ 1344 } 1345 else 1346#endif 1347 { 1348 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/ 1349 XMesaPutImage( b->xm_visual->display, b->frontxrb->drawable, 1350 b->swapgc, 1351 b->backxrb->ximage, 0, 0, 1352 0, 0, b->mesa_buffer.Width, b->mesa_buffer.Height ); 1353 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/ 1354 } 1355 } 1356 else if (b->backxrb->pixmap) { 1357 /* Copy pixmap (back buf) to window (front buf) on server */ 1358 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/ 1359 XMesaCopyArea( b->xm_visual->display, 1360 b->backxrb->pixmap, /* source drawable */ 1361 b->frontxrb->drawable, /* dest. drawable */ 1362 b->swapgc, 1363 0, 0, b->mesa_buffer.Width, b->mesa_buffer.Height, 1364 0, 0 /* dest region */ 1365 ); 1366 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/ 1367 } 1368 } 1369 XSync( b->xm_visual->display, False ); 1370} 1371 1372 1373 1374/* 1375 * Copy sub-region of back buffer to front buffer 1376 */ 1377void XMesaCopySubBuffer( XMesaBuffer b, int x, int y, int width, int height ) 1378{ 1379 GET_CURRENT_CONTEXT(ctx); 1380 1381 /* If we're swapping the buffer associated with the current context 1382 * we have to flush any pending rendering commands first. 1383 */ 1384 if (ctx && ctx->DrawBuffer == &(b->mesa_buffer)) 1385 _mesa_notifySwapBuffers(ctx); 1386 1387 if (!b->backxrb) { 1388 /* single buffered */ 1389 return; 1390 } 1391 1392 if (b->db_mode) { 1393 int yTop = b->mesa_buffer.Height - y - height; 1394 if (b->backxrb->ximage) { 1395 /* Copy Ximage from host's memory to server's window */ 1396#if defined(USE_XSHM) 1397 if (b->shm) { 1398 /* XXX assuming width and height aren't too large! */ 1399 XShmPutImage( b->xm_visual->display, b->frontxrb->drawable, 1400 b->swapgc, 1401 b->backxrb->ximage, x, yTop, 1402 x, yTop, width, height, False ); 1403 /* wait for finished event??? */ 1404 } 1405 else 1406#endif 1407 { 1408 /* XXX assuming width and height aren't too large! */ 1409 XMesaPutImage( b->xm_visual->display, b->frontxrb->drawable, 1410 b->swapgc, 1411 b->backxrb->ximage, x, yTop, 1412 x, yTop, width, height ); 1413 } 1414 } 1415 else { 1416 /* Copy pixmap to window on server */ 1417 XMesaCopyArea( b->xm_visual->display, 1418 b->backxrb->pixmap, /* source drawable */ 1419 b->frontxrb->drawable, /* dest. drawable */ 1420 b->swapgc, 1421 x, yTop, width, height, /* source region */ 1422 x, yTop /* dest region */ 1423 ); 1424 } 1425 } 1426} 1427 1428 1429/* 1430 * Return a pointer to the XMesa backbuffer Pixmap or XImage. This function 1431 * is a way to get "under the hood" of X/Mesa so one can manipulate the 1432 * back buffer directly. 1433 * Output: pixmap - pointer to back buffer's Pixmap, or 0 1434 * ximage - pointer to back buffer's XImage, or NULL 1435 * Return: GL_TRUE = context is double buffered 1436 * GL_FALSE = context is single buffered 1437 */ 1438GLboolean XMesaGetBackBuffer( XMesaBuffer b, 1439 XMesaPixmap *pixmap, 1440 XMesaImage **ximage ) 1441{ 1442 if (b->db_mode) { 1443 if (pixmap) 1444 *pixmap = b->backxrb->pixmap; 1445 if (ximage) 1446 *ximage = b->backxrb->ximage; 1447 return GL_TRUE; 1448 } 1449 else { 1450 *pixmap = 0; 1451 *ximage = NULL; 1452 return GL_FALSE; 1453 } 1454} 1455 1456 1457/* 1458 * Return the depth buffer associated with an XMesaBuffer. 1459 * Input: b - the XMesa buffer handle 1460 * Output: width, height - size of buffer in pixels 1461 * bytesPerValue - bytes per depth value (2 or 4) 1462 * buffer - pointer to depth buffer values 1463 * Return: GL_TRUE or GL_FALSE to indicate success or failure. 1464 */ 1465GLboolean XMesaGetDepthBuffer( XMesaBuffer b, GLint *width, GLint *height, 1466 GLint *bytesPerValue, void **buffer ) 1467{ 1468 struct gl_renderbuffer *rb 1469 = b->mesa_buffer.Attachment[BUFFER_DEPTH].Renderbuffer; 1470 struct xmesa_renderbuffer *xrb = xmesa_renderbuffer(rb); 1471 1472 if (!xrb || !xrb->Base.Buffer) { 1473 *width = 0; 1474 *height = 0; 1475 *bytesPerValue = 0; 1476 *buffer = 0; 1477 return GL_FALSE; 1478 } 1479 else { 1480 *width = b->mesa_buffer.Width; 1481 *height = b->mesa_buffer.Height; 1482 *bytesPerValue = b->mesa_buffer.Visual.depthBits <= 16 1483 ? sizeof(GLushort) : sizeof(GLuint); 1484 *buffer = (void *) xrb->Base.Buffer; 1485 return GL_TRUE; 1486 } 1487} 1488 1489 1490void XMesaFlush( XMesaContext c ) 1491{ 1492 if (c && c->xm_visual) { 1493 XSync( c->xm_visual->display, False ); 1494 } 1495} 1496 1497 1498 1499const char *XMesaGetString( XMesaContext c, int name ) 1500{ 1501 (void) c; 1502 if (name==XMESA_VERSION) { 1503 return "5.0"; 1504 } 1505 else if (name==XMESA_EXTENSIONS) { 1506 return ""; 1507 } 1508 else { 1509 return NULL; 1510 } 1511} 1512 1513 1514 1515XMesaBuffer XMesaFindBuffer( XMesaDisplay *dpy, XMesaDrawable d ) 1516{ 1517 XMesaBuffer b; 1518 for (b=XMesaBufferList; b; b=b->Next) { 1519 if (b->frontxrb->drawable == d && b->display == dpy) { 1520 return b; 1521 } 1522 } 1523 return NULL; 1524} 1525 1526 1527/** 1528 * Free/destroy all XMesaBuffers associated with given display. 1529 */ 1530void xmesa_destroy_buffers_on_display(XMesaDisplay *dpy) 1531{ 1532 XMesaBuffer b, next; 1533 for (b = XMesaBufferList; b; b = next) { 1534 next = b->Next; 1535 if (b->display == dpy) { 1536 xmesa_free_buffer(b); 1537 } 1538 } 1539} 1540 1541 1542/* 1543 * Look for XMesaBuffers whose X window has been destroyed. 1544 * Deallocate any such XMesaBuffers. 1545 */ 1546void XMesaGarbageCollect( XMesaDisplay* dpy ) 1547{ 1548 XMesaBuffer b, next; 1549 for (b=XMesaBufferList; b; b=next) { 1550 next = b->Next; 1551 if (b->display && b->display == dpy && b->frontxrb->drawable && b->type == WINDOW) { 1552 XSync(b->display, False); 1553 if (!window_exists( b->display, b->frontxrb->drawable )) { 1554 /* found a dead window, free the ancillary info */ 1555 XMesaDestroyBuffer( b ); 1556 } 1557 } 1558 } 1559} 1560 1561 1562unsigned long XMesaDitherColor( XMesaContext xmesa, GLint x, GLint y, 1563 GLfloat red, GLfloat green, 1564 GLfloat blue, GLfloat alpha ) 1565{ 1566 GLint r = (GLint) (red * 255.0F); 1567 GLint g = (GLint) (green * 255.0F); 1568 GLint b = (GLint) (blue * 255.0F); 1569 GLint a = (GLint) (alpha * 255.0F); 1570 1571 switch (xmesa->pixelformat) { 1572 case PF_Truecolor: 1573 { 1574 unsigned long p; 1575 PACK_TRUECOLOR( p, r, g, b ); 1576 return p; 1577 } 1578 case PF_8A8B8G8R: 1579 return PACK_8A8B8G8R( r, g, b, a ); 1580 case PF_8A8R8G8B: 1581 return PACK_8A8R8G8B( r, g, b, a ); 1582 case PF_8R8G8B: 1583 return PACK_8R8G8B( r, g, b ); 1584 case PF_5R6G5B: 1585 return PACK_5R6G5B( r, g, b ); 1586 case PF_Dither_5R6G5B: 1587 /* fall through */ 1588 case PF_Dither_True: 1589 { 1590 unsigned long p; 1591 PACK_TRUEDITHER(p, x, y, r, g, b); 1592 return p; 1593 } 1594 default: 1595 _mesa_problem(NULL, "Bad pixel format in XMesaDitherColor"); 1596 } 1597 return 0; 1598} 1599 1600 1601/* 1602 * This is typically called when the window size changes and we need 1603 * to reallocate the buffer's back/depth/stencil/accum buffers. 1604 */ 1605PUBLIC void 1606XMesaResizeBuffers( XMesaBuffer b ) 1607{ 1608 GET_CURRENT_CONTEXT(ctx); 1609 XMesaContext xmctx = XMESA_CONTEXT(ctx); 1610 if (!xmctx) 1611 return; 1612 xmesa_check_and_update_buffer_size(xmctx, b); 1613} 1614 1615 1616static GLint 1617xbuffer_to_renderbuffer(int buffer) 1618{ 1619 assert(MAX_AUX_BUFFERS <= 4); 1620 1621 switch (buffer) { 1622 case GLX_FRONT_LEFT_EXT: 1623 return BUFFER_FRONT_LEFT; 1624 case GLX_FRONT_RIGHT_EXT: 1625 return BUFFER_FRONT_RIGHT; 1626 case GLX_BACK_LEFT_EXT: 1627 return BUFFER_BACK_LEFT; 1628 case GLX_BACK_RIGHT_EXT: 1629 return BUFFER_BACK_RIGHT; 1630 case GLX_AUX0_EXT: 1631 return BUFFER_AUX0; 1632 case GLX_AUX1_EXT: 1633 case GLX_AUX2_EXT: 1634 case GLX_AUX3_EXT: 1635 case GLX_AUX4_EXT: 1636 case GLX_AUX5_EXT: 1637 case GLX_AUX6_EXT: 1638 case GLX_AUX7_EXT: 1639 case GLX_AUX8_EXT: 1640 case GLX_AUX9_EXT: 1641 default: 1642 /* BadValue error */ 1643 return -1; 1644 } 1645} 1646 1647 1648PUBLIC void 1649XMesaBindTexImage(XMesaDisplay *dpy, XMesaBuffer drawable, int buffer, 1650 const int *attrib_list) 1651{ 1652#if 0 1653 GET_CURRENT_CONTEXT(ctx); 1654 const GLuint unit = ctx->Texture.CurrentUnit; 1655 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit]; 1656 struct gl_texture_object *texObj; 1657#endif 1658 struct gl_renderbuffer *rb; 1659 struct xmesa_renderbuffer *xrb; 1660 GLint b; 1661 XMesaImage *img = NULL; 1662 GLboolean freeImg = GL_FALSE; 1663 1664 b = xbuffer_to_renderbuffer(buffer); 1665 if (b < 0) 1666 return; 1667 1668 if (drawable->TextureFormat == GLX_TEXTURE_FORMAT_NONE_EXT) 1669 return; /* BadMatch error */ 1670 1671 rb = drawable->mesa_buffer.Attachment[b].Renderbuffer; 1672 if (!rb) { 1673 /* invalid buffer */ 1674 return; 1675 } 1676 xrb = xmesa_renderbuffer(rb); 1677 1678#if 0 1679 switch (drawable->TextureTarget) { 1680 case GLX_TEXTURE_1D_EXT: 1681 texObj = texUnit->CurrentTex[TEXTURE_1D_INDEX]; 1682 break; 1683 case GLX_TEXTURE_2D_EXT: 1684 texObj = texUnit->CurrentTex[TEXTURE_2D_INDEX]; 1685 break; 1686 case GLX_TEXTURE_RECTANGLE_EXT: 1687 texObj = texUnit->CurrentTex[TEXTURE_RECT_INDEX]; 1688 break; 1689 default: 1690 return; /* BadMatch error */ 1691 } 1692#endif 1693 1694 /* 1695 * The following is a quick and simple way to implement 1696 * BindTexImage. The better way is to write some new FetchTexel() 1697 * functions which would extract texels from XImages. We'd still 1698 * need to use GetImage when texturing from a Pixmap (front buffer) 1699 * but texturing from a back buffer (XImage) would avoid an image 1700 * copy. 1701 */ 1702 1703 /* get XImage */ 1704 if (xrb->pixmap) { 1705 img = XMesaGetImage(dpy, xrb->pixmap, 0, 0, rb->Width, rb->Height, ~0L, 1706 ZPixmap); 1707 freeImg = GL_TRUE; 1708 } 1709 else if (xrb->ximage) { 1710 img = xrb->ximage; 1711 } 1712 1713 /* store the XImage as a new texture image */ 1714 if (img) { 1715 GLenum format, type, intFormat; 1716 if (img->bits_per_pixel == 32) { 1717 format = GL_BGRA; 1718 type = GL_UNSIGNED_BYTE; 1719 intFormat = GL_RGBA; 1720 } 1721 else if (img->bits_per_pixel == 24) { 1722 format = GL_BGR; 1723 type = GL_UNSIGNED_BYTE; 1724 intFormat = GL_RGB; 1725 } 1726 else if (img->bits_per_pixel == 16) { 1727 format = GL_BGR; 1728 type = GL_UNSIGNED_SHORT_5_6_5; 1729 intFormat = GL_RGB; 1730 } 1731 else { 1732 _mesa_problem(NULL, "Unexpected XImage format in XMesaBindTexImage"); 1733 return; 1734 } 1735 if (drawable->TextureFormat == GLX_TEXTURE_FORMAT_RGBA_EXT) { 1736 intFormat = GL_RGBA; 1737 } 1738 else if (drawable->TextureFormat == GLX_TEXTURE_FORMAT_RGB_EXT) { 1739 intFormat = GL_RGB; 1740 } 1741 1742 _mesa_TexImage2D(GL_TEXTURE_2D, 0, intFormat, rb->Width, rb->Height, 0, 1743 format, type, img->data); 1744 1745 if (freeImg) { 1746 XMesaDestroyImage(img); 1747 } 1748 } 1749} 1750 1751 1752 1753PUBLIC void 1754XMesaReleaseTexImage(XMesaDisplay *dpy, XMesaBuffer drawable, int buffer) 1755{ 1756 const GLint b = xbuffer_to_renderbuffer(buffer); 1757 if (b < 0) 1758 return; 1759 1760 /* no-op for now */ 1761} 1762 1763