xm_api.c revision 27558a160a9fe91745728d7626995cd88f8fe339
1/* $Id: xm_api.c,v 1.54 2003/03/01 01:50:24 brianp Exp $ */ 2 3/* 4 * Mesa 3-D graphics library 5 * Version: 5.0.1 6 * 7 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved. 8 * 9 * Permission is hereby granted, free of charge, to any person obtaining a 10 * copy of this software and associated documentation files (the "Software"), 11 * to deal in the Software without restriction, including without limitation 12 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 13 * and/or sell copies of the Software, and to permit persons to whom the 14 * Software is furnished to do so, subject to the following conditions: 15 * 16 * The above copyright notice and this permission notice shall be included 17 * in all copies or substantial portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 */ 26/* $XFree86: xc/extras/Mesa/src/X/xm_api.c,v 1.2 2002/02/26 23:37:31 tsi Exp $ */ 27 28/* 29 * This file contains the implementations of all the XMesa* functions. 30 * 31 * 32 * NOTES: 33 * 34 * The window coordinate system origin (0,0) is in the lower-left corner 35 * of the window. X11's window coordinate origin is in the upper-left 36 * corner of the window. Therefore, most drawing functions in this 37 * file have to flip Y coordinates. 38 * 39 * Define USE_XSHM in the Makefile with -DUSE_XSHM if you want to compile 40 * in support for the MIT Shared Memory extension. If enabled, when you 41 * use an Ximage for the back buffer in double buffered mode, the "swap" 42 * operation will be faster. You must also link with -lXext. 43 * 44 * Byte swapping: If the Mesa host and the X display use a different 45 * byte order then there's some trickiness to be aware of when using 46 * XImages. The byte ordering used for the XImage is that of the X 47 * display, not the Mesa host. 48 * The color-to-pixel encoding for True/DirectColor must be done 49 * according to the display's visual red_mask, green_mask, and blue_mask. 50 * If XPutPixel is used to put a pixel into an XImage then XPutPixel will 51 * do byte swapping if needed. If one wants to directly "poke" the pixel 52 * into the XImage's buffer then the pixel must be byte swapped first. In 53 * Mesa, when byte swapping is needed we use the PF_TRUECOLOR pixel format 54 * and use XPutPixel everywhere except in the implementation of 55 * glClear(GL_COLOR_BUFFER_BIT). We want this function to be fast so 56 * instead of using XPutPixel we "poke" our values after byte-swapping 57 * the clear pixel value if needed. 58 * 59 */ 60 61#ifdef __CYGWIN__ 62#undef WIN32 63#undef __WIN32__ 64#endif 65 66#include "glxheader.h" 67#include "GL/xmesa.h" 68#include "xmesaP.h" 69#include "context.h" 70#include "extensions.h" 71#include "glthread.h" 72#include "imports.h" 73#include "matrix.h" 74#include "mtypes.h" 75#include "macros.h" 76#include "texformat.h" 77#include "texstore.h" 78#include "swrast/swrast.h" 79#include "swrast_setup/swrast_setup.h" 80#include "array_cache/acache.h" 81#include "tnl/tnl.h" 82 83#ifndef GLX_NONE_EXT 84#define GLX_NONE_EXT 0x8000 85#endif 86 87 88/* 89 * Global X driver lock 90 */ 91_glthread_Mutex _xmesa_lock; 92 93 94 95/* 96 * Lookup tables for HPCR pixel format: 97 */ 98static short hpcr_rgbTbl[3][256] = { 99{ 100 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 101 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31, 102 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39, 103 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 104 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 105 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 106 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 107 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 108112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 109128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 110144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 111160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 112176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 113192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 114208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 115224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239 116}, 117{ 118 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 119 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31, 120 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39, 121 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 122 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 123 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 124 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 125 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 126112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 127128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 128144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 129160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 130176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 131192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 132208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 133224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239 134}, 135{ 136 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39, 137 40, 40, 41, 41, 42, 42, 43, 43, 44, 44, 45, 45, 46, 46, 47, 47, 138 48, 48, 49, 49, 50, 50, 51, 51, 52, 52, 53, 53, 54, 54, 55, 55, 139 56, 56, 57, 57, 58, 58, 59, 59, 60, 60, 61, 61, 62, 62, 63, 63, 140 64, 64, 65, 65, 66, 66, 67, 67, 68, 68, 69, 69, 70, 70, 71, 71, 141 72, 72, 73, 73, 74, 74, 75, 75, 76, 76, 77, 77, 78, 78, 79, 79, 142 80, 80, 81, 81, 82, 82, 83, 83, 84, 84, 85, 85, 86, 86, 87, 87, 143 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 144 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 145112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 146128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 147144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 148160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 149176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 150192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 151208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223 152} 153}; 154 155 156 157/**********************************************************************/ 158/***** X Utility Functions *****/ 159/**********************************************************************/ 160 161 162/* 163 * Return the host's byte order as LSBFirst or MSBFirst ala X. 164 */ 165#ifndef XFree86Server 166static int host_byte_order( void ) 167{ 168 int i = 1; 169 char *cptr = (char *) &i; 170 return (*cptr==1) ? LSBFirst : MSBFirst; 171} 172#endif 173 174 175/* 176 * Error handling. 177 */ 178#ifndef XFree86Server 179static int mesaXErrorFlag = 0; 180 181static int mesaHandleXError( XMesaDisplay *dpy, XErrorEvent *event ) 182{ 183 (void) dpy; 184 (void) event; 185 mesaXErrorFlag = 1; 186 return 0; 187} 188#endif 189 190 191/* 192 * Check if the X Shared Memory extension is available. 193 * Return: 0 = not available 194 * 1 = shared XImage support available 195 * 2 = shared Pixmap support available also 196 */ 197#ifndef XFree86Server 198static int check_for_xshm( XMesaDisplay *display ) 199{ 200#ifdef USE_XSHM 201 int major, minor, ignore; 202 Bool pixmaps; 203 204 if (XQueryExtension( display, "MIT-SHM", &ignore, &ignore, &ignore )) { 205 if (XShmQueryVersion( display, &major, &minor, &pixmaps )==True) { 206 return (pixmaps==True) ? 2 : 1; 207 } 208 else { 209 return 0; 210 } 211 } 212 else { 213 return 0; 214 } 215#else 216 /* Can't compile XSHM support */ 217 return 0; 218#endif 219} 220#endif 221 222 223/* 224 * Return the width and height of the given drawable. 225 */ 226static void get_drawable_size( XMesaDisplay *dpy, XMesaDrawable d, 227 unsigned int *width, unsigned int *height) 228{ 229#ifdef XFree86Server 230 (void) dpy; 231 *width = d->width; 232 *height = d->height; 233#else 234 Window root; 235 int x, y; 236 unsigned int bw, depth; 237 238 _glthread_LOCK_MUTEX(_xmesa_lock); 239 XGetGeometry( dpy, d, &root, &x, &y, width, height, &bw, &depth ); 240 _glthread_UNLOCK_MUTEX(_xmesa_lock); 241#endif 242} 243 244 245/* 246 * Apply gamma correction to an intensity value in [0..max]. Return the 247 * new intensity value. 248 */ 249static GLint gamma_adjust( GLfloat gamma, GLint value, GLint max ) 250{ 251 if (gamma == 1.0) { 252 return value; 253 } 254 else { 255 double x = (double) value / (double) max; 256 return IROUND_POS((GLfloat) max * _mesa_pow(x, 1.0F/gamma)); 257 } 258} 259 260 261 262/* 263 * Return the true number of bits per pixel for XImages. 264 * For example, if we request a 24-bit deep visual we may actually need/get 265 * 32bpp XImages. This function returns the appropriate bpp. 266 * Input: dpy - the X display 267 * visinfo - desribes the visual to be used for XImages 268 * Return: true number of bits per pixel for XImages 269 */ 270#ifdef XFree86Server 271 272static int bits_per_pixel( XMesaVisual xmv ) 273{ 274 XMesaVisualInfo visinfo = xmv->visinfo; 275 const int depth = visinfo->nplanes; 276 int i; 277 for (i = 0; i < screenInfo.numPixmapFormats; i++) { 278 if (screenInfo.formats[i].depth == depth) 279 return screenInfo.formats[i].bitsPerPixel; 280 } 281 return depth; /* should never get here, but this should be safe */ 282} 283 284#else 285 286static int bits_per_pixel( XMesaVisual xmv ) 287{ 288 XMesaDisplay *dpy = xmv->display; 289 XMesaVisualInfo visinfo = xmv->visinfo; 290 XMesaImage *img; 291 int bitsPerPixel; 292 /* Create a temporary XImage */ 293 img = XCreateImage( dpy, visinfo->visual, visinfo->depth, 294 ZPixmap, 0, /*format, offset*/ 295 (char*) MALLOC(8), /*data*/ 296 1, 1, /*width, height*/ 297 32, /*bitmap_pad*/ 298 0 /*bytes_per_line*/ 299 ); 300 assert(img); 301 /* grab the bits/pixel value */ 302 bitsPerPixel = img->bits_per_pixel; 303 /* free the XImage */ 304 FREE( img->data ); 305 img->data = NULL; 306 XMesaDestroyImage( img ); 307 return bitsPerPixel; 308} 309#endif 310 311 312 313/* 314 * Determine if a given X window ID is valid (window exists). 315 * Do this by calling XGetWindowAttributes() for the window and 316 * checking if we catch an X error. 317 * Input: dpy - the display 318 * win - the window to check for existance 319 * Return: GL_TRUE - window exists 320 * GL_FALSE - window doesn't exist 321 */ 322#ifndef XFree86Server 323static GLboolean WindowExistsFlag; 324 325static int window_exists_err_handler( XMesaDisplay* dpy, XErrorEvent* xerr ) 326{ 327 (void) dpy; 328 if (xerr->error_code == BadWindow) { 329 WindowExistsFlag = GL_FALSE; 330 } 331 return 0; 332} 333 334static GLboolean window_exists( XMesaDisplay *dpy, Window win ) 335{ 336 XWindowAttributes wa; 337 int (*old_handler)( XMesaDisplay*, XErrorEvent* ); 338 WindowExistsFlag = GL_TRUE; 339 old_handler = XSetErrorHandler(window_exists_err_handler); 340 XGetWindowAttributes( dpy, win, &wa ); /* dummy request */ 341 XSetErrorHandler(old_handler); 342 return WindowExistsFlag; 343} 344#endif 345 346 347 348/**********************************************************************/ 349/***** Linked list of XMesaBuffers *****/ 350/**********************************************************************/ 351 352static XMesaBuffer XMesaBufferList = NULL; 353 354 355/* Allocate a new XMesaBuffer, add to linked list */ 356static XMesaBuffer alloc_xmesa_buffer(void) 357{ 358 XMesaBuffer b = (XMesaBuffer) CALLOC_STRUCT(xmesa_buffer); 359 if (b) { 360 b->Next = XMesaBufferList; 361 XMesaBufferList = b; 362 } 363 return b; 364} 365 366 367/* 368 * Find an XMesaBuffer by matching X display and colormap but NOT matching 369 * the notThis buffer. 370 */ 371static XMesaBuffer find_xmesa_buffer(XMesaDisplay *dpy, 372 XMesaColormap cmap, 373 XMesaBuffer notThis) 374{ 375 XMesaBuffer b; 376 for (b=XMesaBufferList; b; b=b->Next) { 377 if (b->display==dpy && b->cmap==cmap && b!=notThis) { 378 return b; 379 } 380 } 381 return NULL; 382} 383 384 385/* 386 * Free an XMesaBuffer, remove from linked list, perhaps free X colormap 387 * entries. 388 */ 389static void free_xmesa_buffer(int client, XMesaBuffer buffer) 390{ 391 XMesaBuffer prev = NULL, b; 392 (void) client; 393 for (b=XMesaBufferList; b; b=b->Next) { 394 if (b==buffer) { 395 /* unlink bufer from list */ 396 if (prev) 397 prev->Next = buffer->Next; 398 else 399 XMesaBufferList = buffer->Next; 400 /* Check to free X colors */ 401 if (buffer->num_alloced>0) { 402 /* If no other buffer uses this X colormap then free the colors. */ 403 if (!find_xmesa_buffer(buffer->display, buffer->cmap, buffer)) { 404#ifdef XFree86Server 405 (void)FreeColors(buffer->cmap, client, 406 buffer->num_alloced, buffer->alloced_colors, 407 0); 408#else 409 XFreeColors(buffer->display, buffer->cmap, 410 buffer->alloced_colors, buffer->num_alloced, 0); 411#endif 412 } 413 } 414 415 _mesa_free_framebuffer_data(&buffer->mesa_buffer); 416 FREE(buffer); 417 418 return; 419 } 420 /* continue search */ 421 prev = b; 422 } 423 /* buffer not found in XMesaBufferList */ 424 _mesa_problem(NULL,"free_xmesa_buffer() - buffer not found\n"); 425} 426 427 428/* Copy X color table stuff from one XMesaBuffer to another. */ 429static void copy_colortable_info(XMesaBuffer dst, const XMesaBuffer src) 430{ 431 MEMCPY(dst->color_table, src->color_table, sizeof(src->color_table)); 432 MEMCPY(dst->pixel_to_r, src->pixel_to_r, sizeof(src->pixel_to_r)); 433 MEMCPY(dst->pixel_to_g, src->pixel_to_g, sizeof(src->pixel_to_g)); 434 MEMCPY(dst->pixel_to_b, src->pixel_to_b, sizeof(src->pixel_to_b)); 435 dst->num_alloced = src->num_alloced; 436 MEMCPY(dst->alloced_colors, src->alloced_colors, 437 sizeof(src->alloced_colors)); 438} 439 440 441 442/**********************************************************************/ 443/***** Misc Private Functions *****/ 444/**********************************************************************/ 445 446 447/* 448 * Return number of bits set in n. 449 */ 450static int bitcount( unsigned long n ) 451{ 452 int bits; 453 for (bits=0; n>0; n=n>>1) { 454 if (n&1) { 455 bits++; 456 } 457 } 458 return bits; 459} 460 461 462 463/* 464 * Allocate a shared memory XImage back buffer for the given XMesaBuffer. 465 * Return: GL_TRUE if success, GL_FALSE if error 466 */ 467#ifndef XFree86Server 468static GLboolean alloc_shm_back_buffer( XMesaBuffer b ) 469{ 470#ifdef USE_XSHM 471 /* 472 * We have to do a _lot_ of error checking here to be sure we can 473 * really use the XSHM extension. It seems different servers trigger 474 * errors at different points if the extension won't work. Therefore 475 * we have to be very careful... 476 */ 477 GC gc; 478 int (*old_handler)( XMesaDisplay *, XErrorEvent * ); 479 480 b->backimage = XShmCreateImage( b->xm_visual->display, 481 b->xm_visual->visinfo->visual, 482 b->xm_visual->visinfo->depth, 483 ZPixmap, NULL, &b->shminfo, 484 b->width, b->height ); 485 if (b->backimage == NULL) { 486 _mesa_warning(NULL, "alloc_back_buffer: Shared memory error (XShmCreateImage), disabling."); 487 b->shm = 0; 488 return GL_FALSE; 489 } 490 491 b->shminfo.shmid = shmget( IPC_PRIVATE, b->backimage->bytes_per_line 492 * b->backimage->height, IPC_CREAT|0777 ); 493 if (b->shminfo.shmid < 0) { 494 _mesa_warning(NULL, "shmget failed while allocating back buffer"); 495 XDestroyImage( b->backimage ); 496 b->backimage = NULL; 497 _mesa_warning(NULL, "alloc_back_buffer: Shared memory error (shmget), disabling."); 498 b->shm = 0; 499 return GL_FALSE; 500 } 501 502 b->shminfo.shmaddr = b->backimage->data 503 = (char*)shmat( b->shminfo.shmid, 0, 0 ); 504 if (b->shminfo.shmaddr == (char *) -1) { 505 _mesa_warning(NULL, "shmat() failed while allocating back buffer"); 506 XDestroyImage( b->backimage ); 507 shmctl( b->shminfo.shmid, IPC_RMID, 0 ); 508 b->backimage = NULL; 509 _mesa_warning(NULL, "alloc_back_buffer: Shared memory error (shmat), disabling."); 510 b->shm = 0; 511 return GL_FALSE; 512 } 513 514 b->shminfo.readOnly = False; 515 mesaXErrorFlag = 0; 516 old_handler = XSetErrorHandler( mesaHandleXError ); 517 /* This may trigger the X protocol error we're ready to catch: */ 518 XShmAttach( b->xm_visual->display, &b->shminfo ); 519 XSync( b->xm_visual->display, False ); 520 521 if (mesaXErrorFlag) { 522 /* we are on a remote display, this error is normal, don't print it */ 523 XFlush( b->xm_visual->display ); 524 mesaXErrorFlag = 0; 525 XDestroyImage( b->backimage ); 526 shmdt( b->shminfo.shmaddr ); 527 shmctl( b->shminfo.shmid, IPC_RMID, 0 ); 528 b->backimage = NULL; 529 b->shm = 0; 530 (void) XSetErrorHandler( old_handler ); 531 return GL_FALSE; 532 } 533 534 shmctl( b->shminfo.shmid, IPC_RMID, 0 ); /* nobody else needs it */ 535 536 /* Finally, try an XShmPutImage to be really sure the extension works */ 537 gc = XCreateGC( b->xm_visual->display, b->frontbuffer, 0, NULL ); 538 XShmPutImage( b->xm_visual->display, b->frontbuffer, gc, 539 b->backimage, 0, 0, 0, 0, 1, 1 /*one pixel*/, False ); 540 XSync( b->xm_visual->display, False ); 541 XFreeGC( b->xm_visual->display, gc ); 542 (void) XSetErrorHandler( old_handler ); 543 if (mesaXErrorFlag) { 544 XFlush( b->xm_visual->display ); 545 mesaXErrorFlag = 0; 546 XDestroyImage( b->backimage ); 547 shmdt( b->shminfo.shmaddr ); 548 shmctl( b->shminfo.shmid, IPC_RMID, 0 ); 549 b->backimage = NULL; 550 b->shm = 0; 551 return GL_FALSE; 552 } 553 554 if (b->backimage) { 555 int height = b->backimage->height; 556 /* Needed by PIXELADDR1 macro */ 557 b->ximage_width1 = b->backimage->bytes_per_line; 558 b->ximage_origin1 = (GLubyte *) b->backimage->data 559 + b->ximage_width1 * (height-1); 560 /* Needed by PIXELADDR2 macro */ 561 b->ximage_width2 = b->backimage->bytes_per_line / 2; 562 b->ximage_origin2 = (GLushort *) b->backimage->data 563 + b->ximage_width2 * (height-1); 564 /* Needed by PIXELADDR3 macro */ 565 b->ximage_width3 = b->backimage->bytes_per_line; 566 b->ximage_origin3 = (GLubyte *) b->backimage->data 567 + b->ximage_width3 * (height-1); 568 /* Needed by PIXELADDR4 macro */ 569 b->ximage_width4 = b->backimage->width; 570 b->ximage_origin4 = (GLuint *) b->backimage->data 571 + b->ximage_width4 * (height-1); 572 } 573 574 return GL_TRUE; 575#else 576 /* Can't compile XSHM support */ 577 return GL_FALSE; 578#endif 579} 580#endif 581 582 583 584/* 585 * Setup an off-screen pixmap or Ximage to use as the back buffer. 586 * Input: b - the X/Mesa buffer 587 */ 588void xmesa_alloc_back_buffer( XMesaBuffer b ) 589{ 590 if (b->db_state==BACK_XIMAGE) { 591 /* Deallocate the old backimage, if any */ 592 if (b->backimage) { 593#if defined(USE_XSHM) && !defined(XFree86Server) 594 if (b->shm) { 595 XShmDetach( b->xm_visual->display, &b->shminfo ); 596 XDestroyImage( b->backimage ); 597 shmdt( b->shminfo.shmaddr ); 598 } 599 else 600#endif 601 XMesaDestroyImage( b->backimage ); 602 b->backimage = NULL; 603 } 604 605 /* Allocate new back buffer */ 606#ifdef XFree86Server 607 { 608 /* Allocate a regular XImage for the back buffer. */ 609 b->backimage = XMesaCreateImage(b->xm_visual->BitsPerPixel, 610 b->width, b->height, NULL); 611#else 612 if (b->shm==0 || alloc_shm_back_buffer(b)==GL_FALSE) { 613 /* Allocate a regular XImage for the back buffer. */ 614 b->backimage = XCreateImage( b->xm_visual->display, 615 b->xm_visual->visinfo->visual, 616 GET_VISUAL_DEPTH(b->xm_visual), 617 ZPixmap, 0, /* format, offset */ 618 NULL, b->width, b->height, 619 8, 0 ); /* pad, bytes_per_line */ 620#endif 621 if (!b->backimage) { 622 _mesa_warning(NULL, "alloc_back_buffer: XCreateImage failed."); 623 } 624 b->backimage->data = (char *) MALLOC( b->backimage->height 625 * b->backimage->bytes_per_line ); 626 if (!b->backimage->data) { 627 _mesa_warning(NULL, "alloc_back_buffer: MALLOC failed."); 628 XMesaDestroyImage( b->backimage ); 629 b->backimage = NULL; 630 } 631 } 632 b->backpixmap = None; 633 } 634 else if (b->db_state==BACK_PIXMAP) { 635 XMesaPixmap old_pixmap = b->backpixmap; 636 /* Free the old back pixmap */ 637 if (b->backpixmap) { 638 XMesaFreePixmap( b->xm_visual->display, b->backpixmap ); 639 } 640 /* Allocate new back pixmap */ 641 b->backpixmap = XMesaCreatePixmap( b->xm_visual->display, b->frontbuffer, 642 b->width, b->height, 643 GET_VISUAL_DEPTH(b->xm_visual) ); 644 b->backimage = NULL; 645 /* update other references to backpixmap */ 646 if (b->buffer==(XMesaDrawable)old_pixmap) { 647 b->buffer = (XMesaDrawable)b->backpixmap; 648 } 649 } 650} 651 652 653 654/* 655 * A replacement for XAllocColor. This function should never 656 * fail to allocate a color. When XAllocColor fails, we return 657 * the nearest matching color. If we have to allocate many colors 658 * this function isn't too efficient; the XQueryColors() could be 659 * done just once. 660 * Written by Michael Pichler, Brian Paul, Mark Kilgard 661 * Input: dpy - X display 662 * cmap - X colormap 663 * cmapSize - size of colormap 664 * In/Out: color - the XColor struct 665 * Output: exact - 1=exact color match, 0=closest match 666 * alloced - 1=XAlloc worked, 0=XAlloc failed 667 */ 668static void 669noFaultXAllocColor( int client, 670 XMesaDisplay *dpy, 671 XMesaColormap cmap, 672 int cmapSize, 673 XMesaColor *color, 674 int *exact, int *alloced ) 675{ 676#ifdef XFree86Server 677 Pixel *ppixIn; 678 xrgb *ctable; 679#else 680 /* we'll try to cache ctable for better remote display performance */ 681 static Display *prevDisplay = NULL; 682 static XMesaColormap prevCmap = 0; 683 static int prevCmapSize = 0; 684 static XMesaColor *ctable = NULL; 685#endif 686 XMesaColor subColor; 687 int i, bestmatch; 688 double mindist; /* 3*2^16^2 exceeds long int precision. */ 689 690 (void) client; 691 692 /* First try just using XAllocColor. */ 693#ifdef XFree86Server 694 if (AllocColor(cmap, 695 &color->red, &color->green, &color->blue, 696 &color->pixel, 697 client) == Success) { 698#else 699 if (XAllocColor(dpy, cmap, color)) { 700#endif 701 *exact = 1; 702 *alloced = 1; 703 return; 704 } 705 706 /* Alloc failed, search for closest match */ 707 708 /* Retrieve color table entries. */ 709 /* XXX alloca candidate. */ 710#ifdef XFree86Server 711 ppixIn = (Pixel *) MALLOC(cmapSize * sizeof(Pixel)); 712 ctable = (xrgb *) MALLOC(cmapSize * sizeof(xrgb)); 713 for (i = 0; i < cmapSize; i++) { 714 ppixIn[i] = i; 715 } 716 QueryColors(cmap, cmapSize, ppixIn, ctable); 717#else 718 if (prevDisplay != dpy || prevCmap != cmap 719 || prevCmapSize != cmapSize || !ctable) { 720 /* free previously cached color table */ 721 if (ctable) 722 FREE(ctable); 723 /* Get the color table from X */ 724 ctable = (XMesaColor *) MALLOC(cmapSize * sizeof(XMesaColor)); 725 assert(ctable); 726 for (i = 0; i < cmapSize; i++) { 727 ctable[i].pixel = i; 728 } 729 XQueryColors(dpy, cmap, ctable, cmapSize); 730 prevDisplay = dpy; 731 prevCmap = cmap; 732 prevCmapSize = cmapSize; 733 } 734#endif 735 736 /* Find best match. */ 737 bestmatch = -1; 738 mindist = 0.0; 739 for (i = 0; i < cmapSize; i++) { 740 double dr = 0.30 * ((double) color->red - (double) ctable[i].red); 741 double dg = 0.59 * ((double) color->green - (double) ctable[i].green); 742 double db = 0.11 * ((double) color->blue - (double) ctable[i].blue); 743 double dist = dr * dr + dg * dg + db * db; 744 if (bestmatch < 0 || dist < mindist) { 745 bestmatch = i; 746 mindist = dist; 747 } 748 } 749 750 /* Return result. */ 751 subColor.red = ctable[bestmatch].red; 752 subColor.green = ctable[bestmatch].green; 753 subColor.blue = ctable[bestmatch].blue; 754 /* Try to allocate the closest match color. This should only 755 * fail if the cell is read/write. Otherwise, we're incrementing 756 * the cell's reference count. 757 */ 758#ifdef XFree86Server 759 if (AllocColor(cmap, 760 &subColor.red, &subColor.green, &subColor.blue, 761 &subColor.pixel, 762 client) == Success) { 763#else 764 if (XAllocColor(dpy, cmap, &subColor)) { 765#endif 766 *alloced = 1; 767 } 768 else { 769 /* do this to work around a problem reported by Frank Ortega */ 770 subColor.pixel = (unsigned long) bestmatch; 771 subColor.red = ctable[bestmatch].red; 772 subColor.green = ctable[bestmatch].green; 773 subColor.blue = ctable[bestmatch].blue; 774 subColor.flags = DoRed | DoGreen | DoBlue; 775 *alloced = 0; 776 } 777#ifdef XFree86Server 778 FREE(ppixIn); 779 FREE(ctable); 780#else 781 /* don't free table, save it for next time */ 782#endif 783 784 *color = subColor; 785 *exact = 0; 786} 787 788 789 790 791/* 792 * Do setup for PF_GRAYSCALE pixel format. 793 * Note that buffer may be NULL. 794 */ 795static GLboolean setup_grayscale( int client, XMesaVisual v, 796 XMesaBuffer buffer, XMesaColormap cmap ) 797{ 798 if (GET_VISUAL_DEPTH(v)<4 || GET_VISUAL_DEPTH(v)>16) { 799 return GL_FALSE; 800 } 801 802 if (buffer) { 803 XMesaBuffer prevBuffer; 804 805 if (!cmap) { 806 return GL_FALSE; 807 } 808 809 prevBuffer = find_xmesa_buffer(v->display, cmap, buffer); 810 if (prevBuffer && 811 (buffer->xm_visual->mesa_visual.rgbMode == 812 prevBuffer->xm_visual->mesa_visual.rgbMode)) { 813 /* Copy colormap stuff from previous XMesaBuffer which uses same 814 * X colormap. Do this to avoid time spent in noFaultXAllocColor. 815 */ 816 copy_colortable_info(buffer, prevBuffer); 817 } 818 else { 819 /* Allocate 256 shades of gray */ 820 int gray; 821 int colorsfailed = 0; 822 for (gray=0;gray<256;gray++) { 823 GLint r = gamma_adjust( v->RedGamma, gray, 255 ); 824 GLint g = gamma_adjust( v->GreenGamma, gray, 255 ); 825 GLint b = gamma_adjust( v->BlueGamma, gray, 255 ); 826 int exact, alloced; 827 XMesaColor xcol; 828 xcol.red = (r << 8) | r; 829 xcol.green = (g << 8) | g; 830 xcol.blue = (b << 8) | b; 831 noFaultXAllocColor( client, v->display, 832 cmap, GET_COLORMAP_SIZE(v), 833 &xcol, &exact, &alloced ); 834 if (!exact) { 835 colorsfailed++; 836 } 837 if (alloced) { 838 assert(buffer->num_alloced<256); 839 buffer->alloced_colors[buffer->num_alloced] = xcol.pixel; 840 buffer->num_alloced++; 841 } 842 843 /*OLD 844 assert(gray < 576); 845 buffer->color_table[gray*3+0] = xcol.pixel; 846 buffer->color_table[gray*3+1] = xcol.pixel; 847 buffer->color_table[gray*3+2] = xcol.pixel; 848 assert(xcol.pixel < 65536); 849 buffer->pixel_to_r[xcol.pixel] = gray * 30 / 100; 850 buffer->pixel_to_g[xcol.pixel] = gray * 59 / 100; 851 buffer->pixel_to_b[xcol.pixel] = gray * 11 / 100; 852 */ 853 buffer->color_table[gray] = xcol.pixel; 854 assert(xcol.pixel < 65536); 855 buffer->pixel_to_r[xcol.pixel] = gray; 856 buffer->pixel_to_g[xcol.pixel] = gray; 857 buffer->pixel_to_b[xcol.pixel] = gray; 858 } 859 860 if (colorsfailed && _mesa_getenv("MESA_DEBUG")) { 861 _mesa_warning(NULL, 862 "Note: %d out of 256 needed colors do not match exactly.\n", 863 colorsfailed ); 864 } 865 } 866 } 867 868 v->dithered_pf = PF_Grayscale; 869 v->undithered_pf = PF_Grayscale; 870 return GL_TRUE; 871} 872 873 874 875/* 876 * Setup RGB rendering for a window with a PseudoColor, StaticColor, 877 * or 8-bit TrueColor visual visual. We try to allocate a palette of 225 878 * colors (5 red, 9 green, 5 blue) and dither to approximate a 24-bit RGB 879 * color. While this function was originally designed just for 8-bit 880 * visuals, it has also proven to work from 4-bit up to 16-bit visuals. 881 * Dithering code contributed by Bob Mercier. 882 */ 883static GLboolean setup_dithered_color( int client, XMesaVisual v, 884 XMesaBuffer buffer, XMesaColormap cmap ) 885{ 886 if (GET_VISUAL_DEPTH(v)<4 || GET_VISUAL_DEPTH(v)>16) { 887 return GL_FALSE; 888 } 889 890 if (buffer) { 891 XMesaBuffer prevBuffer; 892 893 if (!cmap) { 894 return GL_FALSE; 895 } 896 897 prevBuffer = find_xmesa_buffer(v->display, cmap, buffer); 898 if (prevBuffer && 899 (buffer->xm_visual->mesa_visual.rgbMode == 900 prevBuffer->xm_visual->mesa_visual.rgbMode)) { 901 /* Copy colormap stuff from previous, matching XMesaBuffer. 902 * Do this to avoid time spent in noFaultXAllocColor. 903 */ 904 copy_colortable_info(buffer, prevBuffer); 905 } 906 else { 907 /* Allocate X colors and initialize color_table[], red_table[], etc */ 908 int r, g, b, i; 909 int colorsfailed = 0; 910 for (r = 0; r < DITH_R; r++) { 911 for (g = 0; g < DITH_G; g++) { 912 for (b = 0; b < DITH_B; b++) { 913 XMesaColor xcol; 914 int exact, alloced; 915 xcol.red =gamma_adjust(v->RedGamma, r*65535/(DITH_R-1),65535); 916 xcol.green=gamma_adjust(v->GreenGamma, g*65535/(DITH_G-1),65535); 917 xcol.blue =gamma_adjust(v->BlueGamma, b*65535/(DITH_B-1),65535); 918 noFaultXAllocColor( client, v->display, 919 cmap, GET_COLORMAP_SIZE(v), 920 &xcol, &exact, &alloced ); 921 if (!exact) { 922 colorsfailed++; 923 } 924 if (alloced) { 925 assert(buffer->num_alloced<256); 926 buffer->alloced_colors[buffer->num_alloced] = xcol.pixel; 927 buffer->num_alloced++; 928 } 929 i = DITH_MIX( r, g, b ); 930 assert(i < 576); 931 buffer->color_table[i] = xcol.pixel; 932 assert(xcol.pixel < 65536); 933 buffer->pixel_to_r[xcol.pixel] = r * 255 / (DITH_R-1); 934 buffer->pixel_to_g[xcol.pixel] = g * 255 / (DITH_G-1); 935 buffer->pixel_to_b[xcol.pixel] = b * 255 / (DITH_B-1); 936 } 937 } 938 } 939 940 if (colorsfailed && _mesa_getenv("MESA_DEBUG")) { 941 _mesa_warning(NULL, 942 "Note: %d out of %d needed colors do not match exactly.\n", 943 colorsfailed, DITH_R * DITH_G * DITH_B ); 944 } 945 } 946 } 947 948 v->dithered_pf = PF_Dither; 949 v->undithered_pf = PF_Lookup; 950 return GL_TRUE; 951} 952 953 954/* 955 * Setup for Hewlett Packard Color Recovery 8-bit TrueColor mode. 956 * HPCR simulates 24-bit color fidelity with an 8-bit frame buffer. 957 * Special dithering tables have to be initialized. 958 */ 959static void setup_8bit_hpcr( XMesaVisual v ) 960{ 961 /* HP Color Recovery contributed by: Alex De Bruyn (ad@lms.be) 962 * To work properly, the atom _HP_RGB_SMOOTH_MAP_LIST must be defined 963 * on the root window AND the colormap obtainable by XGetRGBColormaps 964 * for that atom must be set on the window. (see also tkInitWindow) 965 * If that colormap is not set, the output will look stripy. 966 */ 967 968 /* Setup color tables with gamma correction */ 969 int i; 970 double g; 971 972 g = 1.0 / v->RedGamma; 973 for (i=0; i<256; i++) { 974 GLint red = IROUND_POS(255.0 * _mesa_pow( hpcr_rgbTbl[0][i]/255.0, g )); 975 v->hpcr_rgbTbl[0][i] = CLAMP( red, 16, 239 ); 976 } 977 978 g = 1.0 / v->GreenGamma; 979 for (i=0; i<256; i++) { 980 GLint green = IROUND_POS(255.0 * _mesa_pow( hpcr_rgbTbl[1][i]/255.0, g )); 981 v->hpcr_rgbTbl[1][i] = CLAMP( green, 16, 239 ); 982 } 983 984 g = 1.0 / v->BlueGamma; 985 for (i=0; i<256; i++) { 986 GLint blue = IROUND_POS(255.0 * _mesa_pow( hpcr_rgbTbl[2][i]/255.0, g )); 987 v->hpcr_rgbTbl[2][i] = CLAMP( blue, 32, 223 ); 988 } 989 v->undithered_pf = PF_HPCR; /* can't really disable dithering for now */ 990 v->dithered_pf = PF_HPCR; 991 992 /* which method should I use to clear */ 993 /* GL_FALSE: keep the ordinary method */ 994 /* GL_TRUE : clear with dither pattern */ 995 v->hpcr_clear_flag = _mesa_getenv("MESA_HPCR_CLEAR") ? GL_TRUE : GL_FALSE; 996 997 if (v->hpcr_clear_flag) { 998 v->hpcr_clear_pixmap = XMesaCreatePixmap(v->display, 999 DefaultRootWindow(v->display), 1000 16, 2, 8); 1001#ifndef XFree86Server 1002 v->hpcr_clear_ximage = XGetImage(v->display, v->hpcr_clear_pixmap, 1003 0, 0, 16, 2, AllPlanes, ZPixmap); 1004#endif 1005 } 1006} 1007 1008 1009/* 1010 * Setup RGB rendering for a window with a True/DirectColor visual. 1011 */ 1012static void setup_truecolor( XMesaVisual v, XMesaBuffer buffer, 1013 XMesaColormap cmap ) 1014{ 1015 unsigned long rmask, gmask, bmask; 1016 (void) buffer; 1017 (void) cmap; 1018 1019 /* Compute red multiplier (mask) and bit shift */ 1020 v->rshift = 0; 1021 rmask = GET_REDMASK(v); 1022 while ((rmask & 1)==0) { 1023 v->rshift++; 1024 rmask = rmask >> 1; 1025 } 1026 1027 /* Compute green multiplier (mask) and bit shift */ 1028 v->gshift = 0; 1029 gmask = GET_GREENMASK(v); 1030 while ((gmask & 1)==0) { 1031 v->gshift++; 1032 gmask = gmask >> 1; 1033 } 1034 1035 /* Compute blue multiplier (mask) and bit shift */ 1036 v->bshift = 0; 1037 bmask = GET_BLUEMASK(v); 1038 while ((bmask & 1)==0) { 1039 v->bshift++; 1040 bmask = bmask >> 1; 1041 } 1042 1043 /* 1044 * Compute component-to-pixel lookup tables and dithering kernel 1045 */ 1046 { 1047 static GLubyte kernel[16] = { 1048 0*16, 8*16, 2*16, 10*16, 1049 12*16, 4*16, 14*16, 6*16, 1050 3*16, 11*16, 1*16, 9*16, 1051 15*16, 7*16, 13*16, 5*16, 1052 }; 1053 GLint rBits = bitcount(rmask); 1054 GLint gBits = bitcount(gmask); 1055 GLint bBits = bitcount(bmask); 1056 GLint maxBits; 1057 GLuint i; 1058 1059 /* convert pixel components in [0,_mask] to RGB values in [0,255] */ 1060 for (i=0; i<=rmask; i++) 1061 v->PixelToR[i] = (unsigned char) ((i * 255) / rmask); 1062 for (i=0; i<=gmask; i++) 1063 v->PixelToG[i] = (unsigned char) ((i * 255) / gmask); 1064 for (i=0; i<=bmask; i++) 1065 v->PixelToB[i] = (unsigned char) ((i * 255) / bmask); 1066 1067 /* convert RGB values from [0,255] to pixel components */ 1068 1069 for (i=0;i<256;i++) { 1070 GLint r = gamma_adjust(v->RedGamma, i, 255); 1071 GLint g = gamma_adjust(v->GreenGamma, i, 255); 1072 GLint b = gamma_adjust(v->BlueGamma, i, 255); 1073 v->RtoPixel[i] = (r >> (8-rBits)) << v->rshift; 1074 v->GtoPixel[i] = (g >> (8-gBits)) << v->gshift; 1075 v->BtoPixel[i] = (b >> (8-bBits)) << v->bshift; 1076 } 1077 /* overflow protection */ 1078 for (i=256;i<512;i++) { 1079 v->RtoPixel[i] = v->RtoPixel[255]; 1080 v->GtoPixel[i] = v->GtoPixel[255]; 1081 v->BtoPixel[i] = v->BtoPixel[255]; 1082 } 1083 1084 /* setup dithering kernel */ 1085 maxBits = rBits; 1086 if (gBits > maxBits) maxBits = gBits; 1087 if (bBits > maxBits) maxBits = bBits; 1088 for (i=0;i<16;i++) { 1089 v->Kernel[i] = kernel[i] >> maxBits; 1090 } 1091 1092 v->undithered_pf = PF_Truecolor; 1093 v->dithered_pf = (GET_VISUAL_DEPTH(v)<24) ? PF_Dither_True : PF_Truecolor; 1094 } 1095 1096 /* 1097 * Now check for TrueColor visuals which we can optimize. 1098 */ 1099 if ( GET_REDMASK(v) ==0x0000ff 1100 && GET_GREENMASK(v)==0x00ff00 1101 && GET_BLUEMASK(v) ==0xff0000 1102 && CHECK_BYTE_ORDER(v) 1103 && v->BitsPerPixel==32 1104 && sizeof(GLuint)==4 1105 && v->RedGamma==1.0 && v->GreenGamma==1.0 && v->BlueGamma==1.0) { 1106 /* common 32 bpp config used on SGI, Sun */ 1107 v->undithered_pf = v->dithered_pf = PF_8A8B8G8R; 1108 } 1109 else if (GET_REDMASK(v) ==0xff0000 1110 && GET_GREENMASK(v)==0x00ff00 1111 && GET_BLUEMASK(v) ==0x0000ff 1112 && CHECK_BYTE_ORDER(v) 1113 && v->BitsPerPixel==32 1114 && sizeof(GLuint)==4 1115 && v->RedGamma==1.0 && v->GreenGamma==1.0 && v->BlueGamma==1.0) { 1116 /* common 32 bpp config used on Linux, HP, IBM */ 1117 v->undithered_pf = v->dithered_pf = PF_8R8G8B; 1118 } 1119 else if (GET_REDMASK(v) ==0xff0000 1120 && GET_GREENMASK(v)==0x00ff00 1121 && GET_BLUEMASK(v) ==0x0000ff 1122 && CHECK_BYTE_ORDER(v) 1123 && v->BitsPerPixel==24 1124 && sizeof(GLuint)==4 1125 && v->RedGamma==1.0 && v->GreenGamma==1.0 && v->BlueGamma==1.0) { 1126 /* common packed 24 bpp config used on Linux */ 1127 v->undithered_pf = v->dithered_pf = PF_8R8G8B24; 1128 } 1129 else if (GET_REDMASK(v) ==0xf800 1130 && GET_GREENMASK(v)==0x07e0 1131 && GET_BLUEMASK(v) ==0x001f 1132 && CHECK_BYTE_ORDER(v) 1133 && v->BitsPerPixel==16 1134 && sizeof(GLushort)==2 1135 && v->RedGamma==1.0 && v->GreenGamma==1.0 && v->BlueGamma==1.0) { 1136 /* 5-6-5 color weight on common PC VGA boards */ 1137 v->undithered_pf = PF_5R6G5B; 1138 v->dithered_pf = PF_Dither_5R6G5B; 1139 } 1140 else if (GET_REDMASK(v) ==0xe0 1141 && GET_GREENMASK(v)==0x1c 1142 && GET_BLUEMASK(v) ==0x03 1143 && CHECK_FOR_HPCR(v)) { 1144 setup_8bit_hpcr( v ); 1145 } 1146} 1147 1148 1149 1150/* 1151 * Setup RGB rendering for a window with a monochrome visual. 1152 */ 1153static void setup_monochrome( XMesaVisual v, XMesaBuffer b ) 1154{ 1155 (void) b; 1156 v->dithered_pf = v->undithered_pf = PF_1Bit; 1157 /* if black=1 then we must flip pixel values */ 1158 v->bitFlip = (GET_BLACK_PIXEL(v) != 0); 1159} 1160 1161 1162 1163/* 1164 * When a context is "made current" for the first time, we can finally 1165 * finish initializing the context's visual and buffer information. 1166 * Input: v - the XMesaVisual to initialize 1167 * b - the XMesaBuffer to initialize (may be NULL) 1168 * rgb_flag - TRUE = RGBA mode, FALSE = color index mode 1169 * window - the window/pixmap we're rendering into 1170 * cmap - the colormap associated with the window/pixmap 1171 * Return: GL_TRUE=success, GL_FALSE=failure 1172 */ 1173static GLboolean initialize_visual_and_buffer( int client, 1174 XMesaVisual v, 1175 XMesaBuffer b, 1176 GLboolean rgb_flag, 1177 XMesaDrawable window, 1178 XMesaColormap cmap ) 1179{ 1180#ifndef XFree86Server 1181 XGCValues gcvalues; 1182#endif 1183 1184 if (b) { 1185 assert(b->xm_visual == v); 1186 } 1187 1188 /* Save true bits/pixel */ 1189 v->BitsPerPixel = bits_per_pixel(v); 1190 assert(v->BitsPerPixel > 0); 1191 1192 1193 if (rgb_flag==GL_FALSE) { 1194 /* COLOR-INDEXED WINDOW: 1195 * Even if the visual is TrueColor or DirectColor we treat it as 1196 * being color indexed. This is weird but might be useful to someone. 1197 */ 1198 v->dithered_pf = v->undithered_pf = PF_Index; 1199 v->index_bits = GET_VISUAL_DEPTH(v); 1200 } 1201 else { 1202 /* RGB WINDOW: 1203 * We support RGB rendering into almost any kind of visual. 1204 */ 1205 int xclass; 1206 xclass = GET_VISUAL_CLASS(v); 1207 if (xclass==TrueColor || xclass==DirectColor) { 1208 setup_truecolor( v, b, cmap ); 1209 } 1210 else if (xclass==StaticGray && GET_VISUAL_DEPTH(v)==1) { 1211 setup_monochrome( v, b ); 1212 } 1213 else if (xclass==GrayScale || xclass==StaticGray) { 1214 if (!setup_grayscale( client, v, b, cmap )) { 1215 return GL_FALSE; 1216 } 1217 } 1218 else if ((xclass==PseudoColor || xclass==StaticColor) 1219 && GET_VISUAL_DEPTH(v)>=4 && GET_VISUAL_DEPTH(v)<=16) { 1220 if (!setup_dithered_color( client, v, b, cmap )) { 1221 return GL_FALSE; 1222 } 1223 } 1224 else { 1225 _mesa_warning(NULL, "XMesa: RGB mode rendering not supported in given visual."); 1226 return GL_FALSE; 1227 } 1228 v->index_bits = 0; 1229 1230 if (_mesa_getenv("MESA_NO_DITHER")) { 1231 v->dithered_pf = v->undithered_pf; 1232 } 1233 } 1234 1235 1236 /* 1237 * If MESA_INFO env var is set print out some debugging info 1238 * which can help Brian figure out what's going on when a user 1239 * reports bugs. 1240 */ 1241 if (_mesa_getenv("MESA_INFO")) { 1242 _mesa_printf("X/Mesa visual = %p\n", (void *) v); 1243 _mesa_printf("X/Mesa dithered pf = %u\n", v->dithered_pf); 1244 _mesa_printf("X/Mesa undithered pf = %u\n", v->undithered_pf); 1245 _mesa_printf("X/Mesa level = %d\n", v->level); 1246 _mesa_printf("X/Mesa depth = %d\n", GET_VISUAL_DEPTH(v)); 1247 _mesa_printf("X/Mesa bits per pixel = %d\n", v->BitsPerPixel); 1248 } 1249 1250 if (b && window) { 1251 /* Do window-specific initializations */ 1252 1253 /* Window dimensions */ 1254 unsigned int w, h; 1255 get_drawable_size( v->display, window, &w, &h ); 1256 b->width = w; 1257 b->height = h; 1258 1259 b->frontbuffer = window; 1260 1261 /* Setup for single/double buffering */ 1262 if (v->mesa_visual.doubleBufferMode) { 1263 /* Double buffered */ 1264#ifndef XFree86Server 1265 b->shm = check_for_xshm( v->display ); 1266#endif 1267 xmesa_alloc_back_buffer( b ); 1268 if (b->db_state==BACK_PIXMAP) { 1269 b->buffer = (XMesaDrawable)b->backpixmap; 1270 } 1271 else { 1272 b->buffer = XIMAGE; 1273 } 1274 } 1275 else { 1276 /* Single Buffered */ 1277 b->buffer = b->frontbuffer; 1278 } 1279 1280 /* X11 graphics contexts */ 1281#ifdef XFree86Server 1282 b->gc = CreateScratchGC(v->display, window->depth); 1283#else 1284 b->gc = XCreateGC( v->display, window, 0, NULL ); 1285#endif 1286 XMesaSetFunction( v->display, b->gc, GXcopy ); 1287 1288 /* cleargc - for glClear() */ 1289#ifdef XFree86Server 1290 b->cleargc = CreateScratchGC(v->display, window->depth); 1291#else 1292 b->cleargc = XCreateGC( v->display, window, 0, NULL ); 1293#endif 1294 XMesaSetFunction( v->display, b->cleargc, GXcopy ); 1295 1296 /* 1297 * Don't generate Graphics Expose/NoExpose events in swapbuffers(). 1298 * Patch contributed by Michael Pichler May 15, 1995. 1299 */ 1300#ifdef XFree86Server 1301 b->swapgc = CreateScratchGC(v->display, window->depth); 1302 { 1303 CARD32 v[1]; 1304 v[0] = FALSE; 1305 dixChangeGC(NullClient, b->swapgc, GCGraphicsExposures, v, NULL); 1306 } 1307#else 1308 gcvalues.graphics_exposures = False; 1309 b->swapgc = XCreateGC( v->display, window, 1310 GCGraphicsExposures, &gcvalues); 1311#endif 1312 XMesaSetFunction( v->display, b->swapgc, GXcopy ); 1313 /* 1314 * Set fill style and tile pixmap once for all for HPCR stuff 1315 * (instead of doing it each time in clear_color_HPCR_pixmap()) 1316 * Initialize whole stuff 1317 * Patch contributed by Jacques Leroy March 8, 1998. 1318 */ 1319 if (v->hpcr_clear_flag && b->buffer!=XIMAGE) { 1320 int i; 1321 for (i=0; i<16; i++) 1322 { 1323 XMesaPutPixel(v->hpcr_clear_ximage, i, 0, 0); 1324 XMesaPutPixel(v->hpcr_clear_ximage, i, 1, 0); 1325 } 1326 XMesaPutImage(b->display, (XMesaDrawable)v->hpcr_clear_pixmap, 1327 b->cleargc, v->hpcr_clear_ximage, 0, 0, 0, 0, 16, 2); 1328 XMesaSetFillStyle( v->display, b->cleargc, FillTiled); 1329 XMesaSetTile( v->display, b->cleargc, v->hpcr_clear_pixmap ); 1330 } 1331 1332 /* Initialize the row buffer XImage for use in write_color_span() */ 1333#ifdef XFree86Server 1334 b->rowimage = XMesaCreateImage(GET_VISUAL_DEPTH(v), MAX_WIDTH, 1, 1335 (char *)MALLOC(MAX_WIDTH*4)); 1336#else 1337 b->rowimage = XCreateImage( v->display, 1338 v->visinfo->visual, 1339 v->visinfo->depth, 1340 ZPixmap, 0, /*format, offset*/ 1341 (char*) MALLOC(MAX_WIDTH*4), /*data*/ 1342 MAX_WIDTH, 1, /*width, height*/ 1343 32, /*bitmap_pad*/ 1344 0 /*bytes_per_line*/ ); 1345#endif 1346 } 1347 1348 return GL_TRUE; 1349} 1350 1351 1352 1353/* 1354 * Convert an RGBA color to a pixel value. 1355 */ 1356unsigned long 1357xmesa_color_to_pixel( XMesaContext xmesa, GLubyte r, GLubyte g, GLubyte b, GLubyte a, 1358 GLuint pixelFormat) 1359{ 1360 switch (pixelFormat) { 1361 case PF_Index: 1362 return 0; 1363 case PF_Truecolor: 1364 { 1365 unsigned long p; 1366 PACK_TRUECOLOR( p, r, g, b ); 1367 return p; 1368 } 1369 case PF_8A8B8G8R: 1370 return PACK_8A8B8G8R( r, g, b, a ); 1371 case PF_8R8G8B: 1372 /* fall through */ 1373 case PF_8R8G8B24: 1374 return PACK_8R8G8B( r, g, b ); 1375 case PF_5R6G5B: 1376 return PACK_5R6G5B( r, g, b ); 1377 case PF_Dither: 1378 { 1379 DITHER_SETUP; 1380 return DITHER( 1, 0, r, g, b ); 1381 } 1382 case PF_1Bit: 1383 /* 382 = (3*255)/2 */ 1384 return ((r+g+b) > 382) ^ xmesa->xm_visual->bitFlip; 1385 case PF_HPCR: 1386 return DITHER_HPCR(1, 1, r, g, b); 1387 case PF_Lookup: 1388 { 1389 LOOKUP_SETUP; 1390 return LOOKUP( r, g, b ); 1391 } 1392 case PF_Grayscale: 1393 return GRAY_RGB( r, g, b ); 1394 case PF_Dither_True: 1395 /* fall through */ 1396 case PF_Dither_5R6G5B: 1397 { 1398 unsigned long p; 1399 PACK_TRUEDITHER(p, 1, 0, r, g, b); 1400 return p; 1401 } 1402 default: 1403 _mesa_problem(NULL, "Bad pixel format in xmesa_color_to_pixel"); 1404 } 1405 return 0; 1406} 1407 1408 1409/**********************************************************************/ 1410/***** Public Functions *****/ 1411/**********************************************************************/ 1412 1413 1414/* 1415 * Create a new X/Mesa visual. 1416 * Input: display - X11 display 1417 * visinfo - an XVisualInfo pointer 1418 * rgb_flag - GL_TRUE = RGB mode, 1419 * GL_FALSE = color index mode 1420 * alpha_flag - alpha buffer requested? 1421 * db_flag - GL_TRUE = double-buffered, 1422 * GL_FALSE = single buffered 1423 * stereo_flag - stereo visual? 1424 * ximage_flag - GL_TRUE = use an XImage for back buffer, 1425 * GL_FALSE = use an off-screen pixmap for back buffer 1426 * depth_size - requested bits/depth values, or zero 1427 * stencil_size - requested bits/stencil values, or zero 1428 * accum_red_size - requested bits/red accum values, or zero 1429 * accum_green_size - requested bits/green accum values, or zero 1430 * accum_blue_size - requested bits/blue accum values, or zero 1431 * accum_alpha_size - requested bits/alpha accum values, or zero 1432 * num_samples - number of samples/pixel if multisampling, or zero 1433 * level - visual level, usually 0 1434 * visualCaveat - ala the GLX extension, usually GLX_NONE_EXT 1435 * Return; a new XMesaVisual or 0 if error. 1436 */ 1437XMesaVisual XMesaCreateVisual( XMesaDisplay *display, 1438 XMesaVisualInfo visinfo, 1439 GLboolean rgb_flag, 1440 GLboolean alpha_flag, 1441 GLboolean db_flag, 1442 GLboolean stereo_flag, 1443 GLboolean ximage_flag, 1444 GLint depth_size, 1445 GLint stencil_size, 1446 GLint accum_red_size, 1447 GLint accum_green_size, 1448 GLint accum_blue_size, 1449 GLint accum_alpha_size, 1450 GLint num_samples, 1451 GLint level, 1452 GLint visualCaveat ) 1453{ 1454 char *gamma; 1455 XMesaVisual v; 1456 GLint red_bits, green_bits, blue_bits, alpha_bits; 1457 1458 /* For debugging only */ 1459 if (_mesa_getenv("MESA_XSYNC")) { 1460 /* This makes debugging X easier. 1461 * In your debugger, set a breakpoint on _XError to stop when an 1462 * X protocol error is generated. 1463 */ 1464#ifdef XFree86Server 1465 /* NOT_NEEDED */ 1466#else 1467 XSynchronize( display, 1 ); 1468#endif 1469 } 1470 1471 v = (XMesaVisual) CALLOC_STRUCT(xmesa_visual); 1472 if (!v) { 1473 return NULL; 1474 } 1475 1476 /* 1477 * In the X server, NULL is passed in for the display. It will have 1478 * to be set before using this visual. See XMesaSetVisualDisplay() 1479 * below. 1480 */ 1481 v->display = display; 1482 1483 /* Save a copy of the XVisualInfo struct because the user may XFREE() 1484 * the struct but we may need some of the information contained in it 1485 * at a later time. 1486 */ 1487#ifdef XFree86Server 1488 v->visinfo = visinfo; 1489#else 1490 v->visinfo = (XVisualInfo *) MALLOC(sizeof(*visinfo)); 1491 if(!v->visinfo) { 1492 FREE(v); 1493 return NULL; 1494 } 1495 MEMCPY(v->visinfo, visinfo, sizeof(*visinfo)); 1496#endif 1497 1498#ifdef XFree86Server 1499 /* Initialize the depth of the screen */ 1500 { 1501 PixmapFormatRec *format; 1502 1503 for (format = screenInfo.formats; 1504 format->depth != display->rootDepth; 1505 format++) 1506 ; 1507 v->screen_depth = format->bitsPerPixel; 1508 } 1509#endif 1510 1511 /* check for MESA_GAMMA environment variable */ 1512 gamma = _mesa_getenv("MESA_GAMMA"); 1513 if (gamma) { 1514 v->RedGamma = v->GreenGamma = v->BlueGamma = 0.0; 1515 sscanf( gamma, "%f %f %f", &v->RedGamma, &v->GreenGamma, &v->BlueGamma ); 1516 if (v->RedGamma<=0.0) v->RedGamma = 1.0; 1517 if (v->GreenGamma<=0.0) v->GreenGamma = v->RedGamma; 1518 if (v->BlueGamma<=0.0) v->BlueGamma = v->RedGamma; 1519 } 1520 else { 1521 v->RedGamma = v->GreenGamma = v->BlueGamma = 1.0; 1522 } 1523 1524 v->ximage_flag = ximage_flag; 1525 v->level = level; 1526 v->VisualCaveat = visualCaveat; 1527 1528 (void) initialize_visual_and_buffer( 0, v, NULL, rgb_flag, 0, 0 ); 1529 1530 { 1531 int xclass; 1532 xclass = GET_VISUAL_CLASS(v); 1533 if (xclass==TrueColor || xclass==DirectColor) { 1534 red_bits = bitcount(GET_REDMASK(v)); 1535 green_bits = bitcount(GET_GREENMASK(v)); 1536 blue_bits = bitcount(GET_BLUEMASK(v)); 1537 alpha_bits = 0; 1538 } 1539 else { 1540 /* this is an approximation */ 1541 int depth; 1542 depth = GET_VISUAL_DEPTH(v); 1543 red_bits = depth / 3; 1544 depth -= red_bits; 1545 green_bits = depth / 2; 1546 depth -= green_bits; 1547 blue_bits = depth; 1548 alpha_bits = 0; 1549 assert( red_bits + green_bits + blue_bits == GET_VISUAL_DEPTH(v) ); 1550 } 1551 } 1552 1553 if (alpha_flag && alpha_bits == 0) 1554 alpha_bits = 8; 1555 1556 _mesa_initialize_visual( &v->mesa_visual, 1557 rgb_flag, db_flag, stereo_flag, 1558 red_bits, green_bits, 1559 blue_bits, alpha_bits, 1560 v->index_bits, 1561 depth_size, 1562 stencil_size, 1563 accum_red_size, accum_green_size, 1564 accum_blue_size, accum_alpha_size, 1565 0 ); 1566 return v; 1567} 1568 1569 1570void XMesaSetVisualDisplay( XMesaDisplay *dpy, XMesaVisual v ) 1571{ 1572 v->display = dpy; 1573} 1574 1575 1576void XMesaDestroyVisual( XMesaVisual v ) 1577{ 1578#ifndef XFree86Server 1579 FREE(v->visinfo); 1580#endif 1581 FREE(v); 1582} 1583 1584 1585 1586/* 1587 * Create a new XMesaContext. 1588 * Input: v - XMesaVisual 1589 * share_list - another XMesaContext with which to share display 1590 * lists or NULL if no sharing is wanted. 1591 * Return: an XMesaContext or NULL if error. 1592 */ 1593XMesaContext XMesaCreateContext( XMesaVisual v, XMesaContext share_list ) 1594{ 1595 static GLboolean firstTime = GL_TRUE; 1596 XMesaContext c; 1597 GLcontext *ctx; 1598 GLboolean direct = GL_TRUE; /* not really */ 1599 1600 if (firstTime) { 1601 _glthread_INIT_MUTEX(_xmesa_lock); 1602 firstTime = GL_FALSE; 1603 } 1604 1605 c = (XMesaContext) CALLOC_STRUCT(xmesa_context); 1606 if (!c) { 1607 return NULL; 1608 } 1609 1610 ctx = c->gl_ctx = _mesa_create_context( &v->mesa_visual, 1611 share_list ? share_list->gl_ctx : (GLcontext *) NULL, 1612 (void *) c, direct); 1613 if (!c->gl_ctx) { 1614 FREE(c); 1615 return NULL; 1616 } 1617 1618 _mesa_enable_sw_extensions(ctx); 1619 _mesa_enable_1_3_extensions(ctx); 1620 _mesa_enable_1_4_extensions(ctx); 1621 1622 if (CHECK_BYTE_ORDER(v)) { 1623 c->swapbytes = GL_FALSE; 1624 } 1625 else { 1626 c->swapbytes = GL_TRUE; 1627 } 1628 1629 c->xm_visual = v; 1630 c->xm_draw_buffer = NULL; /* set later by XMesaMakeCurrent */ 1631 c->xm_read_buffer = NULL; /* set later by XMesaMakeCurrent */ 1632 c->xm_buffer = NULL; /* set later by XMesaMakeCurrent */ 1633 c->display = v->display; 1634 c->pixelformat = v->dithered_pf; /* Dithering is enabled by default */ 1635 1636 ctx->Driver.UpdateState = xmesa_update_state; 1637 1638 /* Initialize the software rasterizer and helper modules. 1639 */ 1640 _swrast_CreateContext( ctx ); 1641 _ac_CreateContext( ctx ); 1642 _tnl_CreateContext( ctx ); 1643 _swsetup_CreateContext( ctx ); 1644 1645 xmesa_register_swrast_functions( ctx ); 1646 1647 /* Set up some constant pointers: 1648 */ 1649 xmesa_init_pointers( ctx ); 1650 1651 return c; 1652} 1653 1654 1655 1656 1657void XMesaDestroyContext( XMesaContext c ) 1658{ 1659#ifdef FX 1660 if (c->xm_draw_buffer && c->xm_buffer->FXctx) 1661 fxMesaDestroyContext(c->xm_draw_buffer->FXctx); 1662#endif 1663 if (c->gl_ctx) { 1664 _swsetup_DestroyContext( c->gl_ctx ); 1665 _swrast_DestroyContext( c->gl_ctx ); 1666 _tnl_DestroyContext( c->gl_ctx ); 1667 _ac_DestroyContext( c->gl_ctx ); 1668 _mesa_destroy_context( c->gl_ctx ); 1669 } 1670 1671 FREE( c ); 1672} 1673 1674 1675 1676/* 1677 * XXX this isn't a public function! It's a hack for the 3Dfx driver. 1678 * Create a new XMesaBuffer from an X window. 1679 * Input: v - the XMesaVisual 1680 * w - the window 1681 * c - the context 1682 * Return: new XMesaBuffer or NULL if error 1683 */ 1684XMesaBuffer XMesaCreateWindowBuffer2( XMesaVisual v, XMesaWindow w, 1685 XMesaContext c ) 1686{ 1687#ifndef XFree86Server 1688 XWindowAttributes attr; 1689#endif 1690#ifdef FX 1691 char *fxEnvVar; 1692#endif 1693 int client = 0; 1694 1695 XMesaBuffer b = alloc_xmesa_buffer(); 1696 if (!b) { 1697 return NULL; 1698 } 1699 1700 (void) c; 1701 1702#ifdef XFree86Server 1703 client = CLIENT_ID(((XMesaDrawable)w)->id); 1704#endif 1705 1706 assert(v); 1707 1708#ifdef XFree86Server 1709 if (GET_VISUAL_DEPTH(v) != ((XMesaDrawable)w)->depth) { 1710#else 1711 XGetWindowAttributes( v->display, w, &attr ); 1712 1713 if (GET_VISUAL_DEPTH(v) != attr.depth) { 1714#endif 1715 _mesa_warning(NULL, "XMesaCreateWindowBuffer: depth mismatch between visual and window!\n"); 1716 return NULL; 1717 } 1718 1719 b->xm_visual = v; 1720 b->type = WINDOW; 1721 b->display = v->display; 1722#ifdef XFree86Server 1723 b->cmap = (ColormapPtr)LookupIDByType(wColormap(w), RT_COLORMAP); 1724#else 1725 if (attr.colormap) { 1726 b->cmap = attr.colormap; 1727 } 1728 else { 1729 _mesa_warning(NULL, "Window %u has no colormap!\n", (unsigned int) w); 1730 /* this is weird, a window w/out a colormap!? */ 1731 /* OK, let's just allocate a new one and hope for the best */ 1732 b->cmap = XCreateColormap(v->display, w, attr.visual, AllocNone); 1733 } 1734#endif 1735 1736 /* determine back buffer implementation */ 1737 if (v->mesa_visual.doubleBufferMode) { 1738 if (v->ximage_flag) { 1739 b->db_state = BACK_XIMAGE; 1740 } 1741 else { 1742 b->db_state = BACK_PIXMAP; 1743 } 1744 } 1745 else { 1746 b->db_state = 0; 1747 } 1748 1749 _mesa_initialize_framebuffer(&b->mesa_buffer, 1750 &v->mesa_visual, 1751 v->mesa_visual.depthBits > 0, 1752 v->mesa_visual.stencilBits > 0, 1753 v->mesa_visual.accumRedBits > 0, 1754 v->mesa_visual.alphaBits > 0 ); 1755 1756 if (!initialize_visual_and_buffer( client, v, b, v->mesa_visual.rgbMode, 1757 (XMesaDrawable)w, b->cmap )) { 1758 free_xmesa_buffer(client, b); 1759 return NULL; 1760 } 1761 1762#ifdef FX 1763 fxEnvVar = _mesa_getenv("MESA_GLX_FX"); 1764 if (fxEnvVar) { 1765 if (fxEnvVar[0]!='d') { 1766 int attribs[100]; 1767 int numAttribs = 0; 1768 int hw; 1769 if (v->mesa_visual.depthBits > 0) { 1770 attribs[numAttribs++] = FXMESA_DEPTH_SIZE; 1771 attribs[numAttribs++] = 1; 1772 } 1773 if (v->mesa_visual.doubleBufferMode) { 1774 attribs[numAttribs++] = FXMESA_DOUBLEBUFFER; 1775 } 1776 if (v->mesa_visual.accumRedBits > 0) { 1777 attribs[numAttribs++] = FXMESA_ACCUM_SIZE; 1778 attribs[numAttribs++] = v->mesa_visual.accumRedBits; 1779 } 1780 if (v->mesa_visual.stencilBits > 0) { 1781 attribs[numAttribs++] = FXMESA_STENCIL_SIZE; 1782 attribs[numAttribs++] = v->mesa_visual.stencilBits; 1783 } 1784 if (v->mesa_visual.alphaBits > 0) { 1785 attribs[numAttribs++] = FXMESA_ALPHA_SIZE; 1786 attribs[numAttribs++] = 1; 1787 } 1788 if (c->gl_ctx) { 1789#define FXMESA_SHARE_CONTEXT 990099 /* keep in sync with fxapi.c! */ 1790 attribs[numAttribs++] = FXMESA_SHARE_CONTEXT; 1791 attribs[numAttribs++] = (int) c->gl_ctx; 1792 } 1793 attribs[numAttribs++] = FXMESA_NONE; 1794 1795 if ((hw = fxQueryHardware())==GR_SSTTYPE_VOODOO) { 1796 b->FXctx = fxMesaCreateBestContext(0, b->width, b->height, attribs); 1797 if ((v->undithered_pf!=PF_Index) && (b->backimage)) { 1798 b->FXisHackUsable = b->FXctx ? GL_TRUE : GL_FALSE; 1799 if (fxEnvVar[0]=='w' || fxEnvVar[0]=='W') 1800 b->FXwindowHack = b->FXctx ? GL_TRUE : GL_FALSE; 1801 else 1802 b->FXwindowHack = GL_FALSE; 1803 } 1804 } 1805 else { 1806 if (fxEnvVar[0]=='w' || fxEnvVar[0]=='W') 1807 b->FXctx = fxMesaCreateContext(w, GR_RESOLUTION_NONE, 1808 GR_REFRESH_75Hz, attribs); 1809 else 1810 b->FXctx = fxMesaCreateBestContext(0, b->width, b->height, attribs); 1811 b->FXisHackUsable = GL_FALSE; 1812 b->FXwindowHack = GL_FALSE; 1813 } 1814 /* 1815 fprintf(stderr, 1816 "voodoo %d, wid %d height %d hack: usable %d active %d\n", 1817 hw, b->width, b->height, b->FXisHackUsable, b->FXwindowHack); 1818 */ 1819 } 1820 } 1821 else { 1822 _mesa_warning(NULL, "WARNING: This Mesa Library includes the Glide driver but\n"); 1823 _mesa_warning(NULL, " you have not defined the MESA_GLX_FX env. var.\n"); 1824 _mesa_warning(NULL, " (check the README.3DFX file for more information).\n\n"); 1825 _mesa_warning(NULL, " you can disable this message with a 'export MESA_GLX_FX=disable'.\n"); 1826 } 1827#endif 1828 1829 return b; 1830} 1831 1832 1833XMesaBuffer XMesaCreateWindowBuffer( XMesaVisual v, XMesaWindow w ) 1834{ 1835 return XMesaCreateWindowBuffer2( v, w, NULL ); 1836} 1837 1838 1839/* 1840 * Create a new XMesaBuffer from an X pixmap. 1841 * Input: v - the XMesaVisual 1842 * p - the pixmap 1843 * cmap - the colormap, may be 0 if using a TrueColor or DirectColor 1844 * visual for the pixmap 1845 * Return: new XMesaBuffer or NULL if error 1846 */ 1847XMesaBuffer XMesaCreatePixmapBuffer( XMesaVisual v, 1848 XMesaPixmap p, XMesaColormap cmap ) 1849{ 1850 int client = 0; 1851 XMesaBuffer b = alloc_xmesa_buffer(); 1852 if (!b) { 1853 return NULL; 1854 } 1855 1856 1857#ifdef XFree86Server 1858 client = CLIENT_ID(((XMesaDrawable)p)->id); 1859#endif 1860 1861 assert(v); 1862 1863 b->xm_visual = v; 1864 b->type = PIXMAP; 1865 b->display = v->display; 1866 b->cmap = cmap; 1867 1868 /* determine back buffer implementation */ 1869 if (v->mesa_visual.doubleBufferMode) { 1870 if (v->ximage_flag) { 1871 b->db_state = BACK_XIMAGE; 1872 } 1873 else { 1874 b->db_state = BACK_PIXMAP; 1875 } 1876 } 1877 else { 1878 b->db_state = 0; 1879 } 1880 1881 _mesa_initialize_framebuffer(&b->mesa_buffer, 1882 &v->mesa_visual, 1883 v->mesa_visual.depthBits > 0, 1884 v->mesa_visual.stencilBits > 0, 1885 v->mesa_visual.accumRedBits + 1886 v->mesa_visual.accumGreenBits + 1887 v->mesa_visual.accumBlueBits > 0, 1888 v->mesa_visual.alphaBits > 0 ); 1889 1890 if (!initialize_visual_and_buffer(client, v, b, v->mesa_visual.rgbMode, 1891 (XMesaDrawable)p, cmap)) { 1892 free_xmesa_buffer(client, b); 1893 return NULL; 1894 } 1895 1896 return b; 1897} 1898 1899 1900 1901XMesaBuffer XMesaCreatePBuffer( XMesaVisual v, XMesaColormap cmap, 1902 unsigned int width, unsigned int height ) 1903{ 1904#ifdef XFree86Server 1905 return 0; 1906#else 1907 int client = 0; 1908 XMesaWindow root; 1909 XMesaDrawable drawable; /* X Pixmap Drawable */ 1910 XMesaBuffer b = alloc_xmesa_buffer(); 1911 if (!b) { 1912 return NULL; 1913 } 1914 1915 b->xm_visual = v; 1916 b->type = PBUFFER; 1917 b->display = v->display; 1918 b->cmap = cmap; 1919 1920 /* allocate pixmap for front buffer */ 1921 root = RootWindow( v->display, v->visinfo->screen ); 1922 drawable = XCreatePixmap( v->display, root, width, height, v->visinfo->depth ); 1923 1924 /* determine back buffer implementation */ 1925 if (v->mesa_visual.doubleBufferMode) { 1926 if (v->ximage_flag) { 1927 b->db_state = BACK_XIMAGE; 1928 } 1929 else { 1930 b->db_state = BACK_PIXMAP; 1931 } 1932 } 1933 else { 1934 b->db_state = 0; 1935 } 1936 1937 _mesa_initialize_framebuffer(&b->mesa_buffer, 1938 &v->mesa_visual, 1939 v->mesa_visual.depthBits > 0, 1940 v->mesa_visual.stencilBits > 0, 1941 v->mesa_visual.accumRedBits + 1942 v->mesa_visual.accumGreenBits + 1943 v->mesa_visual.accumBlueBits > 0, 1944 v->mesa_visual.alphaBits > 0 ); 1945 1946 if (!initialize_visual_and_buffer(client, v, b, v->mesa_visual.rgbMode, 1947 drawable, cmap)) { 1948 free_xmesa_buffer(client, b); 1949 return NULL; 1950 } 1951 1952 return b; 1953#endif 1954} 1955 1956 1957 1958/* 1959 * Deallocate an XMesaBuffer structure and all related info. 1960 */ 1961void XMesaDestroyBuffer( XMesaBuffer b ) 1962{ 1963 int client = 0; 1964 1965#ifdef XFree86Server 1966 if (b->frontbuffer) 1967 client = CLIENT_ID(b->frontbuffer->id); 1968#endif 1969 1970 if (b->gc) XMesaFreeGC( b->xm_visual->display, b->gc ); 1971 if (b->cleargc) XMesaFreeGC( b->xm_visual->display, b->cleargc ); 1972 if (b->swapgc) XMesaFreeGC( b->xm_visual->display, b->swapgc ); 1973 1974 if (b->backimage) { 1975#if defined(USE_XSHM) && !defined(XFree86Server) 1976 if (b->shm) { 1977 XShmDetach( b->xm_visual->display, &b->shminfo ); 1978 XDestroyImage( b->backimage ); 1979 shmdt( b->shminfo.shmaddr ); 1980 } 1981 else 1982#endif 1983 XMesaDestroyImage( b->backimage ); 1984 } 1985 if (b->backpixmap) { 1986 XMesaFreePixmap( b->xm_visual->display, b->backpixmap ); 1987 if (b->xm_visual->hpcr_clear_flag) { 1988 XMesaFreePixmap( b->xm_visual->display, 1989 b->xm_visual->hpcr_clear_pixmap ); 1990 XMesaDestroyImage( b->xm_visual->hpcr_clear_ximage ); 1991 } 1992 } 1993 if (b->rowimage) { 1994 FREE( b->rowimage->data ); 1995 b->rowimage->data = NULL; 1996 XMesaDestroyImage( b->rowimage ); 1997 } 1998 1999 free_xmesa_buffer(client, b); 2000} 2001 2002 2003 2004/* 2005 * Bind buffer b to context c and make c the current rendering context. 2006 */ 2007GLboolean XMesaMakeCurrent( XMesaContext c, XMesaBuffer b ) 2008{ 2009 return XMesaMakeCurrent2( c, b, b ); 2010} 2011 2012 2013/* 2014 * Bind buffer b to context c and make c the current rendering context. 2015 */ 2016GLboolean XMesaMakeCurrent2( XMesaContext c, XMesaBuffer drawBuffer, 2017 XMesaBuffer readBuffer ) 2018{ 2019 if (c) { 2020 if (!drawBuffer || !readBuffer) 2021 return GL_FALSE; /* must specify buffers! */ 2022 2023#ifdef FX 2024 if (drawBuffer->FXctx) { 2025 fxMesaMakeCurrent(drawBuffer->FXctx); 2026 2027 c->xm_draw_buffer = drawBuffer; 2028 c->xm_read_buffer = readBuffer; 2029 c->xm_buffer = drawBuffer; 2030 2031 return GL_TRUE; 2032 } 2033#endif 2034 if (c->gl_ctx == _mesa_get_current_context() 2035 && c->xm_draw_buffer == drawBuffer 2036 && c->xm_read_buffer == readBuffer 2037 && c->xm_draw_buffer->wasCurrent) { 2038 /* same context and buffer, do nothing */ 2039 return GL_TRUE; 2040 } 2041 2042 c->xm_draw_buffer = drawBuffer; 2043 c->xm_read_buffer = readBuffer; 2044 c->xm_buffer = drawBuffer; 2045 2046 _mesa_make_current2(c->gl_ctx, 2047 &drawBuffer->mesa_buffer, 2048 &readBuffer->mesa_buffer); 2049 2050 if (c->gl_ctx->Viewport.Width == 0) { 2051 /* initialize viewport to window size */ 2052 _mesa_Viewport( 0, 0, drawBuffer->width, drawBuffer->height ); 2053 c->gl_ctx->Scissor.Width = drawBuffer->width; 2054 c->gl_ctx->Scissor.Height = drawBuffer->height; 2055 } 2056 2057 if (c->xm_visual->mesa_visual.rgbMode) { 2058 /* 2059 * Must recompute and set these pixel values because colormap 2060 * can be different for different windows. 2061 */ 2062 c->clearpixel = xmesa_color_to_pixel( c, 2063 c->clearcolor[0], 2064 c->clearcolor[1], 2065 c->clearcolor[2], 2066 c->clearcolor[3], 2067 c->xm_visual->undithered_pf); 2068 XMesaSetForeground(c->display, c->xm_draw_buffer->cleargc, c->clearpixel); 2069 } 2070 2071 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */ 2072 c->xm_draw_buffer->wasCurrent = GL_TRUE; 2073 } 2074 else { 2075 /* Detach */ 2076 _mesa_make_current2( NULL, NULL, NULL ); 2077 } 2078 return GL_TRUE; 2079} 2080 2081 2082/* 2083 * Unbind the context c from its buffer. 2084 */ 2085GLboolean XMesaUnbindContext( XMesaContext c ) 2086{ 2087 /* A no-op for XFree86 integration purposes */ 2088 return GL_TRUE; 2089} 2090 2091 2092XMesaContext XMesaGetCurrentContext( void ) 2093{ 2094 GET_CURRENT_CONTEXT(ctx); 2095 if (ctx) { 2096 XMesaContext xmesa = (XMesaContext) ctx->DriverCtx; 2097 return xmesa; 2098 } 2099 else { 2100 return 0; 2101 } 2102} 2103 2104 2105XMesaBuffer XMesaGetCurrentBuffer( void ) 2106{ 2107 GET_CURRENT_CONTEXT(ctx); 2108 if (ctx) { 2109 XMesaContext xmesa = (XMesaContext) ctx->DriverCtx; 2110 return xmesa->xm_draw_buffer; 2111 } 2112 else { 2113 return 0; 2114 } 2115} 2116 2117 2118/* New in Mesa 3.1 */ 2119XMesaBuffer XMesaGetCurrentReadBuffer( void ) 2120{ 2121 GET_CURRENT_CONTEXT(ctx); 2122 if (ctx) { 2123 XMesaContext xmesa = (XMesaContext) ctx->DriverCtx; 2124 return xmesa->xm_read_buffer; 2125 } 2126 else { 2127 return 0; 2128 } 2129} 2130 2131 2132GLboolean XMesaForceCurrent(XMesaContext c) 2133{ 2134 if (c) { 2135 if (c->gl_ctx != _mesa_get_current_context()) { 2136 _mesa_make_current(c->gl_ctx, &c->xm_draw_buffer->mesa_buffer); 2137 } 2138 } 2139 else { 2140 _mesa_make_current(NULL, NULL); 2141 } 2142 return GL_TRUE; 2143} 2144 2145 2146GLboolean XMesaLoseCurrent(XMesaContext c) 2147{ 2148 (void) c; 2149 _mesa_make_current(NULL, NULL); 2150 return GL_TRUE; 2151} 2152 2153 2154/* 2155 * Switch 3Dfx support hack between window and full-screen mode. 2156 */ 2157GLboolean XMesaSetFXmode( GLint mode ) 2158{ 2159#ifdef FX 2160 const char *fx = _mesa_getenv("MESA_GLX_FX"); 2161 if (fx && fx[0] != 'd') { 2162 GET_CURRENT_CONTEXT(ctx); 2163 GrHwConfiguration hw; 2164 if (!FX_grSstQueryHardware(&hw)) { 2165 /*fprintf(stderr, "!grSstQueryHardware\n");*/ 2166 return GL_FALSE; 2167 } 2168 if (hw.num_sst < 1) { 2169 /*fprintf(stderr, "hw.num_sst < 1\n");*/ 2170 return GL_FALSE; 2171 } 2172 if (ctx) { 2173 XMesaContext xmesa = (XMesaContext) ctx->DriverCtx; 2174 if (mode == XMESA_FX_WINDOW) { 2175 if (xmesa->xm_draw_buffer->FXisHackUsable) { 2176 FX_grSstControl(GR_CONTROL_DEACTIVATE); 2177 xmesa->xm_draw_buffer->FXwindowHack = GL_TRUE; 2178 return GL_TRUE; 2179 } 2180 } 2181 else if (mode == XMESA_FX_FULLSCREEN) { 2182 FX_grSstControl(GR_CONTROL_ACTIVATE); 2183 xmesa->xm_draw_buffer->FXwindowHack = GL_FALSE; 2184 return GL_TRUE; 2185 } 2186 else { 2187 /* Error: Bad mode value */ 2188 } 2189 } 2190 } 2191 /*fprintf(stderr, "fallthrough\n");*/ 2192#else 2193 (void) mode; 2194#endif 2195 return GL_FALSE; 2196} 2197 2198 2199 2200#ifdef FX 2201/* 2202 * Read image from VooDoo frame buffer into X/Mesa's back XImage. 2203 */ 2204static void FXgetImage( XMesaBuffer b ) 2205{ 2206 GET_CURRENT_CONTEXT(ctx); 2207 static unsigned short pixbuf[MAX_WIDTH]; 2208 GLuint x, y; 2209 int xpos, ypos; 2210 XMesaWindow root; 2211 unsigned int bw, depth, width, height; 2212 XMesaContext xmesa = (XMesaContext) ctx->DriverCtx; 2213 2214#ifdef XFree86Server 2215 x = b->frontbuffer->x; 2216 y = b->frontbuffer->y; 2217 width = b->frontbuffer->width; 2218 height = b->frontbuffer->height; 2219 depth = b->frontbuffer->depth; 2220#else 2221 XGetGeometry( b->xm_visual->display, b->frontbuffer, 2222 &root, &xpos, &ypos, &width, &height, &bw, &depth); 2223#endif 2224 if (b->width != width || b->height != height) { 2225 b->width = MIN2((int)width, b->FXctx->width); 2226 b->height = MIN2((int)height, b->FXctx->height); 2227 if (b->width & 1) 2228 b->width--; /* prevent odd width */ 2229 xmesa_alloc_back_buffer( b ); 2230 } 2231 2232 grLfbWriteColorFormat(GR_COLORFORMAT_ARGB); 2233 if (b->xm_visual->undithered_pf==PF_5R6G5B) { 2234 /* Special case: 16bpp RGB */ 2235 grLfbReadRegion( GR_BUFFER_FRONTBUFFER, /* src buffer */ 2236 0, b->FXctx->height - b->height, /*pos*/ 2237 b->width, b->height, /* size */ 2238 b->width * sizeof(GLushort), /* stride */ 2239 b->backimage->data); /* dest buffer */ 2240 } 2241 else if (b->xm_visual->dithered_pf==PF_Dither 2242 && GET_VISUAL_DEPTH(b->xm_visual)==8) { 2243 /* Special case: 8bpp RGB */ 2244 for (y=0;y<b->height;y++) { 2245 GLubyte *ptr = (GLubyte*) b->backimage->data 2246 + b->backimage->bytes_per_line * y; 2247 XDITHER_SETUP(y); 2248 2249 /* read row from 3Dfx frame buffer */ 2250 grLfbReadRegion( GR_BUFFER_FRONTBUFFER, 2251 0, b->FXctx->height-(b->height-y), 2252 b->width, 1, 2253 0, 2254 pixbuf ); 2255 2256 /* write to XImage back buffer */ 2257 for (x=0;x<b->width;x++) { 2258 GLubyte r = (pixbuf[x] & 0xf800) >> 8; 2259 GLubyte g = (pixbuf[x] & 0x07e0) >> 3; 2260 GLubyte b = (pixbuf[x] & 0x001f) << 3; 2261 *ptr++ = XDITHER( x, r, g, b); 2262 } 2263 } 2264 } 2265 else { 2266 /* General case: slow! */ 2267 for (y=0;y<b->height;y++) { 2268 /* read row from 3Dfx frame buffer */ 2269 grLfbReadRegion( GR_BUFFER_FRONTBUFFER, 2270 0, b->FXctx->height-(b->height-y), 2271 b->width, 1, 2272 0, 2273 pixbuf ); 2274 2275 /* write to XImage back buffer */ 2276 for (x=0;x<b->width;x++) { 2277 XMesaPutPixel(b->backimage,x,y, 2278 xmesa_color_to_pixel(xmesa, 2279 (pixbuf[x] & 0xf800) >> 8, 2280 (pixbuf[x] & 0x07e0) >> 3, 2281 (pixbuf[x] & 0x001f) << 3, 2282 0xff, 2283 b->xm_visual->undithered_pf)); 2284 } 2285 } 2286 } 2287 grLfbWriteColorFormat(GR_COLORFORMAT_ABGR); 2288} 2289#endif 2290 2291 2292/* 2293 * Copy the back buffer to the front buffer. If there's no back buffer 2294 * this is a no-op. 2295 */ 2296void XMesaSwapBuffers( XMesaBuffer b ) 2297{ 2298 GET_CURRENT_CONTEXT(ctx); 2299 2300 /* If we're swapping the buffer associated with the current context 2301 * we have to flush any pending rendering commands first. 2302 */ 2303 if (ctx && ctx->DrawBuffer == &(b->mesa_buffer)) 2304 _mesa_notifySwapBuffers(ctx); 2305 2306 if (b->db_state) { 2307#ifdef FX 2308 if (b->FXctx) { 2309 fxMesaSwapBuffers(); 2310 2311 if (b->FXwindowHack) 2312 FXgetImage(b); 2313 else 2314 return; 2315 } 2316#endif 2317 if (b->backimage) { 2318 /* Copy Ximage from host's memory to server's window */ 2319#if defined(USE_XSHM) && !defined(XFree86Server) 2320 if (b->shm) { 2321 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/ 2322 XShmPutImage( b->xm_visual->display, b->frontbuffer, 2323 b->swapgc, 2324 b->backimage, 0, 0, 2325 0, 0, b->width, b->height, False ); 2326 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/ 2327 } 2328 else 2329#endif 2330 { 2331 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/ 2332 XMesaPutImage( b->xm_visual->display, b->frontbuffer, 2333 b->swapgc, 2334 b->backimage, 0, 0, 2335 0, 0, b->width, b->height ); 2336 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/ 2337 } 2338 } 2339 else { 2340 /* Copy pixmap to window on server */ 2341 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/ 2342 XMesaCopyArea( b->xm_visual->display, 2343 b->backpixmap, /* source drawable */ 2344 b->frontbuffer, /* dest. drawable */ 2345 b->swapgc, 2346 0, 0, b->width, b->height, /* source region */ 2347 0, 0 /* dest region */ 2348 ); 2349 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/ 2350 } 2351 } 2352#if !defined(XFree86Server) 2353 XSync( b->xm_visual->display, False ); 2354#endif 2355} 2356 2357 2358 2359/* 2360 * Copy sub-region of back buffer to front buffer 2361 */ 2362void XMesaCopySubBuffer( XMesaBuffer b, int x, int y, int width, int height ) 2363{ 2364 GET_CURRENT_CONTEXT(ctx); 2365 2366 /* If we're swapping the buffer associated with the current context 2367 * we have to flush any pending rendering commands first. 2368 */ 2369 if (ctx && ctx->DrawBuffer == &(b->mesa_buffer)) 2370 _mesa_notifySwapBuffers(ctx); 2371 2372 if (b->db_state) { 2373 int yTop = b->height - y - height; 2374#ifdef FX 2375 if (b->FXctx) { 2376 fxMesaSwapBuffers(); 2377 if (b->FXwindowHack) 2378 FXgetImage(b); 2379 else 2380 return; 2381 } 2382#endif 2383 if (b->backimage) { 2384 /* Copy Ximage from host's memory to server's window */ 2385#if defined(USE_XSHM) && !defined(XFree86Server) 2386 if (b->shm) { 2387 /* XXX assuming width and height aren't too large! */ 2388 XShmPutImage( b->xm_visual->display, b->frontbuffer, 2389 b->swapgc, 2390 b->backimage, x, yTop, 2391 x, yTop, width, height, False ); 2392 /* wait for finished event??? */ 2393 } 2394 else 2395#endif 2396 { 2397 /* XXX assuming width and height aren't too large! */ 2398 XMesaPutImage( b->xm_visual->display, b->frontbuffer, 2399 b->swapgc, 2400 b->backimage, x, yTop, 2401 x, yTop, width, height ); 2402 } 2403 } 2404 else { 2405 /* Copy pixmap to window on server */ 2406 XMesaCopyArea( b->xm_visual->display, 2407 b->backpixmap, /* source drawable */ 2408 b->frontbuffer, /* dest. drawable */ 2409 b->swapgc, 2410 x, yTop, width, height, /* source region */ 2411 x, yTop /* dest region */ 2412 ); 2413 } 2414 } 2415} 2416 2417 2418/* 2419 * Return a pointer to the XMesa backbuffer Pixmap or XImage. This function 2420 * is a way to get "under the hood" of X/Mesa so one can manipulate the 2421 * back buffer directly. 2422 * Output: pixmap - pointer to back buffer's Pixmap, or 0 2423 * ximage - pointer to back buffer's XImage, or NULL 2424 * Return: GL_TRUE = context is double buffered 2425 * GL_FALSE = context is single buffered 2426 */ 2427GLboolean XMesaGetBackBuffer( XMesaBuffer b, 2428 XMesaPixmap *pixmap, 2429 XMesaImage **ximage ) 2430{ 2431 if (b->db_state) { 2432 if (pixmap) *pixmap = b->backpixmap; 2433 if (ximage) *ximage = b->backimage; 2434 return GL_TRUE; 2435 } 2436 else { 2437 *pixmap = 0; 2438 *ximage = NULL; 2439 return GL_FALSE; 2440 } 2441} 2442 2443 2444/* 2445 * Return the depth buffer associated with an XMesaBuffer. 2446 * Input: b - the XMesa buffer handle 2447 * Output: width, height - size of buffer in pixels 2448 * bytesPerValue - bytes per depth value (2 or 4) 2449 * buffer - pointer to depth buffer values 2450 * Return: GL_TRUE or GL_FALSE to indicate success or failure. 2451 */ 2452GLboolean XMesaGetDepthBuffer( XMesaBuffer b, GLint *width, GLint *height, 2453 GLint *bytesPerValue, void **buffer ) 2454{ 2455 if (!b->mesa_buffer.DepthBuffer) { 2456 *width = 0; 2457 *height = 0; 2458 *bytesPerValue = 0; 2459 *buffer = 0; 2460 return GL_FALSE; 2461 } 2462 else { 2463 *width = b->mesa_buffer.Width; 2464 *height = b->mesa_buffer.Height; 2465 *bytesPerValue = sizeof(GLdepth); 2466 *buffer = b->mesa_buffer.DepthBuffer; 2467 return GL_TRUE; 2468 } 2469} 2470 2471 2472void XMesaFlush( XMesaContext c ) 2473{ 2474 if (c && c->xm_visual) { 2475#ifdef XFree86Server 2476 /* NOT_NEEDED */ 2477#else 2478 XSync( c->xm_visual->display, False ); 2479#endif 2480 } 2481} 2482 2483 2484 2485const char *XMesaGetString( XMesaContext c, int name ) 2486{ 2487 (void) c; 2488 if (name==XMESA_VERSION) { 2489 return "5.0"; 2490 } 2491 else if (name==XMESA_EXTENSIONS) { 2492 return ""; 2493 } 2494 else { 2495 return NULL; 2496 } 2497} 2498 2499 2500 2501XMesaBuffer XMesaFindBuffer( XMesaDisplay *dpy, XMesaDrawable d ) 2502{ 2503 XMesaBuffer b; 2504 for (b=XMesaBufferList; b; b=b->Next) { 2505 if (b->frontbuffer==d && b->display==dpy) { 2506 return b; 2507 } 2508 } 2509 return NULL; 2510} 2511 2512 2513 2514/* 2515 * Look for XMesaBuffers whose X window has been destroyed. 2516 * Deallocate any such XMesaBuffers. 2517 */ 2518void XMesaGarbageCollect( void ) 2519{ 2520 XMesaBuffer b, next; 2521 for (b=XMesaBufferList; b; b=next) { 2522 next = b->Next; 2523 if (b->display && b->frontbuffer && b->type == WINDOW) { 2524#ifdef XFree86Server 2525 /* NOT_NEEDED */ 2526#else 2527 XSync(b->display, False); 2528 if (!window_exists( b->display, b->frontbuffer )) { 2529 /* found a dead window, free the ancillary info */ 2530 XMesaDestroyBuffer( b ); 2531 } 2532#endif 2533 } 2534 } 2535} 2536 2537 2538void XMesaReset( void ) 2539{ 2540 while (XMesaBufferList) 2541 XMesaDestroyBuffer(XMesaBufferList); 2542 2543 XMesaBufferList = NULL; 2544} 2545 2546 2547unsigned long XMesaDitherColor( XMesaContext xmesa, GLint x, GLint y, 2548 GLfloat red, GLfloat green, 2549 GLfloat blue, GLfloat alpha ) 2550{ 2551 GLint r = (GLint) (red * 255.0F); 2552 GLint g = (GLint) (green * 255.0F); 2553 GLint b = (GLint) (blue * 255.0F); 2554 GLint a = (GLint) (alpha * 255.0F); 2555 2556 switch (xmesa->pixelformat) { 2557 case PF_Index: 2558 return 0; 2559 case PF_Truecolor: 2560 { 2561 unsigned long p; 2562 PACK_TRUECOLOR( p, r, g, b ); 2563 return p; 2564 } 2565 case PF_8A8B8G8R: 2566 return PACK_8A8B8G8R( r, g, b, a ); 2567 case PF_8R8G8B: 2568 return PACK_8R8G8B( r, g, b ); 2569 case PF_5R6G5B: 2570 return PACK_5R6G5B( r, g, b ); 2571 case PF_Dither: 2572 { 2573 DITHER_SETUP; 2574 return DITHER( x, y, r, g, b ); 2575 } 2576 case PF_1Bit: 2577 /* 382 = (3*255)/2 */ 2578 return ((r+g+b) > 382) ^ xmesa->xm_visual->bitFlip; 2579 case PF_HPCR: 2580 return DITHER_HPCR(x, y, r, g, b); 2581 case PF_Lookup: 2582 { 2583 LOOKUP_SETUP; 2584 return LOOKUP( r, g, b ); 2585 } 2586 case PF_Grayscale: 2587 return GRAY_RGB( r, g, b ); 2588 case PF_Dither_5R6G5B: 2589 /* fall through */ 2590 case PF_Dither_True: 2591 { 2592 unsigned long p; 2593 PACK_TRUEDITHER(p, x, y, r, g, b); 2594 return p; 2595 } 2596 default: 2597 _mesa_problem(NULL, "Bad pixel format in XMesaDitherColor"); 2598 } 2599 return 0; 2600} 2601 2602 2603/* 2604 * This is typically called when the window size changes and we need 2605 * to reallocate the buffer's back/depth/stencil/accum buffers. 2606 */ 2607void XMesaResizeBuffers( XMesaBuffer b ) 2608{ 2609 xmesa_resize_buffers( &(b->mesa_buffer) ); 2610 2611} 2612 2613