xm_api.c revision 287c94ea4987033f9c99a2f91c5750c9083504ca
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 * 39 * Byte swapping: If the Mesa host and the X display use a different 40 * byte order then there's some trickiness to be aware of when using 41 * XImages. The byte ordering used for the XImage is that of the X 42 * display, not the Mesa host. 43 * The color-to-pixel encoding for True/DirectColor must be done 44 * according to the display's visual red_mask, green_mask, and blue_mask. 45 * If XPutPixel is used to put a pixel into an XImage then XPutPixel will 46 * do byte swapping if needed. If one wants to directly "poke" the pixel 47 * into the XImage's buffer then the pixel must be byte swapped first. 48 * 49 */ 50 51#ifdef __CYGWIN__ 52#undef WIN32 53#undef __WIN32__ 54#endif 55 56#include "xm_api.h" 57#include "xm_st.h" 58 59#include "main/context.h" 60#include "pipe/p_defines.h" 61#include "pipe/p_screen.h" 62#include "pipe/p_context.h" 63 64#include "xm_public.h" 65#include <GL/glx.h> 66 67 68/* Driver interface routines, set up by xlib backend on library 69 * _init(). These are global in the same way that function names are 70 * global. 71 */ 72static struct xm_driver driver; 73static struct st_api *stapi; 74 75void xmesa_set_driver( const struct xm_driver *templ ) 76{ 77 driver = *templ; 78 stapi = driver.create_st_api(); 79} 80 81 82/* 83 * XXX replace this with a linked list, or better yet, try to attach the 84 * gallium/mesa extra bits to the X Display object with XAddExtension(). 85 */ 86#define MAX_DISPLAYS 10 87static struct xmesa_display Displays[MAX_DISPLAYS]; 88static int NumDisplays = 0; 89 90 91static XMesaDisplay 92xmesa_init_display( Display *display ) 93{ 94 pipe_static_mutex(init_mutex); 95 XMesaDisplay xmdpy; 96 int i; 97 98 pipe_mutex_lock(init_mutex); 99 100 /* Look for XMesaDisplay which corresponds to 'display' */ 101 for (i = 0; i < NumDisplays; i++) { 102 if (Displays[i].display == display) { 103 /* Found it */ 104 pipe_mutex_unlock(init_mutex); 105 return &Displays[i]; 106 } 107 } 108 109 /* Create new XMesaDisplay */ 110 111 assert(NumDisplays < MAX_DISPLAYS); 112 xmdpy = &Displays[NumDisplays]; 113 NumDisplays++; 114 115 if (!xmdpy->display && display) { 116 xmdpy->display = display; 117 xmdpy->screen = driver.create_pipe_screen(display); 118 xmdpy->smapi = CALLOC_STRUCT(st_manager); 119 if (xmdpy->smapi) 120 xmdpy->smapi->screen = xmdpy->screen; 121 122 if (xmdpy->screen && xmdpy->smapi) { 123 pipe_mutex_init(xmdpy->mutex); 124 } 125 else { 126 if (xmdpy->screen) { 127 xmdpy->screen->destroy(xmdpy->screen); 128 xmdpy->screen = NULL; 129 } 130 if (xmdpy->smapi) { 131 FREE(xmdpy->smapi); 132 xmdpy->smapi = NULL; 133 } 134 135 xmdpy->display = NULL; 136 } 137 } 138 if (!xmdpy->display || xmdpy->display != display) 139 xmdpy = NULL; 140 141 pipe_mutex_unlock(init_mutex); 142 143 return xmdpy; 144} 145 146/**********************************************************************/ 147/***** X Utility Functions *****/ 148/**********************************************************************/ 149 150 151/** 152 * Return the host's byte order as LSBFirst or MSBFirst ala X. 153 */ 154static int host_byte_order( void ) 155{ 156 int i = 1; 157 char *cptr = (char *) &i; 158 return (*cptr==1) ? LSBFirst : MSBFirst; 159} 160 161 162 163 164/** 165 * Return the true number of bits per pixel for XImages. 166 * For example, if we request a 24-bit deep visual we may actually need/get 167 * 32bpp XImages. This function returns the appropriate bpp. 168 * Input: dpy - the X display 169 * visinfo - desribes the visual to be used for XImages 170 * Return: true number of bits per pixel for XImages 171 */ 172static int 173bits_per_pixel( XMesaVisual xmv ) 174{ 175 Display *dpy = xmv->display; 176 XVisualInfo * visinfo = xmv->visinfo; 177 XImage *img; 178 int bitsPerPixel; 179 /* Create a temporary XImage */ 180 img = XCreateImage( dpy, visinfo->visual, visinfo->depth, 181 ZPixmap, 0, /*format, offset*/ 182 (char*) MALLOC(8), /*data*/ 183 1, 1, /*width, height*/ 184 32, /*bitmap_pad*/ 185 0 /*bytes_per_line*/ 186 ); 187 assert(img); 188 /* grab the bits/pixel value */ 189 bitsPerPixel = img->bits_per_pixel; 190 /* free the XImage */ 191 free( img->data ); 192 img->data = NULL; 193 XDestroyImage( img ); 194 return bitsPerPixel; 195} 196 197 198 199/* 200 * Determine if a given X window ID is valid (window exists). 201 * Do this by calling XGetWindowAttributes() for the window and 202 * checking if we catch an X error. 203 * Input: dpy - the display 204 * win - the window to check for existance 205 * Return: GL_TRUE - window exists 206 * GL_FALSE - window doesn't exist 207 */ 208static GLboolean WindowExistsFlag; 209 210static int window_exists_err_handler( Display* dpy, XErrorEvent* xerr ) 211{ 212 (void) dpy; 213 if (xerr->error_code == BadWindow) { 214 WindowExistsFlag = GL_FALSE; 215 } 216 return 0; 217} 218 219static GLboolean window_exists( Display *dpy, Window win ) 220{ 221 XWindowAttributes wa; 222 int (*old_handler)( Display*, XErrorEvent* ); 223 WindowExistsFlag = GL_TRUE; 224 old_handler = XSetErrorHandler(window_exists_err_handler); 225 XGetWindowAttributes( dpy, win, &wa ); /* dummy request */ 226 XSetErrorHandler(old_handler); 227 return WindowExistsFlag; 228} 229 230static Status 231get_drawable_size( Display *dpy, Drawable d, uint *width, uint *height ) 232{ 233 Window root; 234 Status stat; 235 int xpos, ypos; 236 unsigned int w, h, bw, depth; 237 stat = XGetGeometry(dpy, d, &root, &xpos, &ypos, &w, &h, &bw, &depth); 238 *width = w; 239 *height = h; 240 return stat; 241} 242 243 244/** 245 * Return the size of the window (or pixmap) that corresponds to the 246 * given XMesaBuffer. 247 * \param width returns width in pixels 248 * \param height returns height in pixels 249 */ 250void 251xmesa_get_window_size(Display *dpy, XMesaBuffer b, 252 GLuint *width, GLuint *height) 253{ 254 XMesaDisplay xmdpy = xmesa_init_display(dpy); 255 Status stat; 256 257 pipe_mutex_lock(xmdpy->mutex); 258 XSync(b->xm_visual->display, 0); /* added for Chromium */ 259 stat = get_drawable_size(dpy, b->ws.drawable, width, height); 260 pipe_mutex_unlock(xmdpy->mutex); 261 262 if (!stat) { 263 /* probably querying a window that's recently been destroyed */ 264 _mesa_warning(NULL, "XGetGeometry failed!\n"); 265 *width = *height = 1; 266 } 267} 268 269#define GET_REDMASK(__v) __v->mesa_visual.redMask 270#define GET_GREENMASK(__v) __v->mesa_visual.greenMask 271#define GET_BLUEMASK(__v) __v->mesa_visual.blueMask 272 273 274/** 275 * Choose the pixel format for the given visual. 276 * This will tell the gallium driver how to pack pixel data into 277 * drawing surfaces. 278 */ 279static GLuint 280choose_pixel_format(XMesaVisual v) 281{ 282 boolean native_byte_order = (host_byte_order() == 283 ImageByteOrder(v->display)); 284 285 if ( GET_REDMASK(v) == 0x0000ff 286 && GET_GREENMASK(v) == 0x00ff00 287 && GET_BLUEMASK(v) == 0xff0000 288 && v->BitsPerPixel == 32) { 289 if (native_byte_order) { 290 /* no byteswapping needed */ 291 return PIPE_FORMAT_R8G8B8A8_UNORM; 292 } 293 else { 294 return PIPE_FORMAT_A8B8G8R8_UNORM; 295 } 296 } 297 else if ( GET_REDMASK(v) == 0xff0000 298 && GET_GREENMASK(v) == 0x00ff00 299 && GET_BLUEMASK(v) == 0x0000ff 300 && v->BitsPerPixel == 32) { 301 if (native_byte_order) { 302 /* no byteswapping needed */ 303 return PIPE_FORMAT_B8G8R8A8_UNORM; 304 } 305 else { 306 return PIPE_FORMAT_A8R8G8B8_UNORM; 307 } 308 } 309 else if ( GET_REDMASK(v) == 0x0000ff00 310 && GET_GREENMASK(v) == 0x00ff0000 311 && GET_BLUEMASK(v) == 0xff000000 312 && v->BitsPerPixel == 32) { 313 if (native_byte_order) { 314 /* no byteswapping needed */ 315 return PIPE_FORMAT_A8R8G8B8_UNORM; 316 } 317 else { 318 return PIPE_FORMAT_B8G8R8A8_UNORM; 319 } 320 } 321 else if ( GET_REDMASK(v) == 0xf800 322 && GET_GREENMASK(v) == 0x07e0 323 && GET_BLUEMASK(v) == 0x001f 324 && native_byte_order 325 && v->BitsPerPixel == 16) { 326 /* 5-6-5 RGB */ 327 return PIPE_FORMAT_B5G6R5_UNORM; 328 } 329 330 return PIPE_FORMAT_NONE; 331} 332 333 334/** 335 * Choose a depth/stencil format that satisfies the given depth and 336 * stencil sizes. 337 */ 338static enum pipe_format 339choose_depth_stencil_format(XMesaDisplay xmdpy, int depth, int stencil) 340{ 341 const enum pipe_texture_target target = PIPE_TEXTURE_2D; 342 const unsigned tex_usage = PIPE_BIND_DEPTH_STENCIL; 343 const unsigned geom_flags = (PIPE_TEXTURE_GEOM_NON_SQUARE | 344 PIPE_TEXTURE_GEOM_NON_POWER_OF_TWO); 345 enum pipe_format formats[8], fmt; 346 int count, i; 347 348 count = 0; 349 350 if (depth <= 16 && stencil == 0) { 351 formats[count++] = PIPE_FORMAT_Z16_UNORM; 352 } 353 if (depth <= 24 && stencil == 0) { 354 formats[count++] = PIPE_FORMAT_X8Z24_UNORM; 355 formats[count++] = PIPE_FORMAT_Z24X8_UNORM; 356 } 357 if (depth <= 24 && stencil <= 8) { 358 formats[count++] = PIPE_FORMAT_S8_USCALED_Z24_UNORM; 359 formats[count++] = PIPE_FORMAT_Z24_UNORM_S8_USCALED; 360 } 361 if (depth <= 32 && stencil == 0) { 362 formats[count++] = PIPE_FORMAT_Z32_UNORM; 363 } 364 365 fmt = PIPE_FORMAT_NONE; 366 for (i = 0; i < count; i++) { 367 if (xmdpy->screen->is_format_supported(xmdpy->screen, formats[i], 368 target, tex_usage, geom_flags)) { 369 fmt = formats[i]; 370 break; 371 } 372 } 373 374 return fmt; 375} 376 377 378 379/**********************************************************************/ 380/***** Linked list of XMesaBuffers *****/ 381/**********************************************************************/ 382 383static XMesaBuffer XMesaBufferList = NULL; 384 385 386/** 387 * Allocate a new XMesaBuffer object which corresponds to the given drawable. 388 * Note that XMesaBuffer is derived from GLframebuffer. 389 * The new XMesaBuffer will not have any size (Width=Height=0). 390 * 391 * \param d the corresponding X drawable (window or pixmap) 392 * \param type either WINDOW, PIXMAP or PBUFFER, describing d 393 * \param vis the buffer's visual 394 * \param cmap the window's colormap, if known. 395 * \return new XMesaBuffer or NULL if any problem 396 */ 397static XMesaBuffer 398create_xmesa_buffer(Drawable d, BufferType type, 399 XMesaVisual vis, Colormap cmap) 400{ 401 XMesaDisplay xmdpy = xmesa_init_display(vis->display); 402 XMesaBuffer b; 403 uint width, height; 404 405 ASSERT(type == WINDOW || type == PIXMAP || type == PBUFFER); 406 407 if (!xmdpy) 408 return NULL; 409 410 b = (XMesaBuffer) CALLOC_STRUCT(xmesa_buffer); 411 if (!b) 412 return NULL; 413 414 b->ws.drawable = d; 415 b->ws.visual = vis->visinfo->visual; 416 b->ws.depth = vis->visinfo->depth; 417 418 b->xm_visual = vis; 419 b->type = type; 420 b->cmap = cmap; 421 422 get_drawable_size(vis->display, d, &width, &height); 423 424 /* 425 * Create framebuffer, but we'll plug in our own renderbuffers below. 426 */ 427 b->stfb = xmesa_create_st_framebuffer(xmdpy, b); 428 429 /* GLX_EXT_texture_from_pixmap */ 430 b->TextureTarget = 0; 431 b->TextureFormat = GLX_TEXTURE_FORMAT_NONE_EXT; 432 b->TextureMipmap = 0; 433 434 /* insert buffer into linked list */ 435 b->Next = XMesaBufferList; 436 XMesaBufferList = b; 437 438 return b; 439} 440 441 442/** 443 * Find an XMesaBuffer by matching X display and colormap but NOT matching 444 * the notThis buffer. 445 */ 446XMesaBuffer 447xmesa_find_buffer(Display *dpy, Colormap cmap, XMesaBuffer notThis) 448{ 449 XMesaBuffer b; 450 for (b = XMesaBufferList; b; b = b->Next) { 451 if (b->xm_visual->display == dpy && 452 b->cmap == cmap && 453 b != notThis) { 454 return b; 455 } 456 } 457 return NULL; 458} 459 460 461/** 462 * Remove buffer from linked list, delete if no longer referenced. 463 */ 464static void 465xmesa_free_buffer(XMesaBuffer buffer) 466{ 467 XMesaBuffer prev = NULL, b; 468 469 for (b = XMesaBufferList; b; b = b->Next) { 470 if (b == buffer) { 471 /* unlink buffer from list */ 472 if (prev) 473 prev->Next = buffer->Next; 474 else 475 XMesaBufferList = buffer->Next; 476 477 /* Since the X window for the XMesaBuffer is going away, we don't 478 * want to dereference this pointer in the future. 479 */ 480 b->ws.drawable = 0; 481 482 /* XXX we should move the buffer to a delete-pending list and destroy 483 * the buffer until it is no longer current. 484 */ 485 xmesa_destroy_st_framebuffer(buffer->stfb); 486 487 free(buffer); 488 489 return; 490 } 491 /* continue search */ 492 prev = b; 493 } 494 /* buffer not found in XMesaBufferList */ 495 _mesa_problem(NULL,"xmesa_free_buffer() - buffer not found\n"); 496} 497 498 499 500/**********************************************************************/ 501/***** Misc Private Functions *****/ 502/**********************************************************************/ 503 504 505/** 506 * When a context is bound for the first time, we can finally finish 507 * initializing the context's visual and buffer information. 508 * \param v the XMesaVisual to initialize 509 * \param b the XMesaBuffer to initialize (may be NULL) 510 * \param rgb_flag TRUE = RGBA mode, FALSE = color index mode 511 * \param window the window/pixmap we're rendering into 512 * \param cmap the colormap associated with the window/pixmap 513 * \return GL_TRUE=success, GL_FALSE=failure 514 */ 515static GLboolean 516initialize_visual_and_buffer(XMesaVisual v, XMesaBuffer b, 517 GLboolean rgb_flag, Drawable window, 518 Colormap cmap) 519{ 520 ASSERT(!b || b->xm_visual == v); 521 522 /* Save true bits/pixel */ 523 v->BitsPerPixel = bits_per_pixel(v); 524 assert(v->BitsPerPixel > 0); 525 526 if (rgb_flag == GL_FALSE) { 527 /* COLOR-INDEXED WINDOW: not supported*/ 528 return GL_FALSE; 529 } 530 else { 531 /* RGB WINDOW: 532 * We support RGB rendering into almost any kind of visual. 533 */ 534 const int xclass = v->mesa_visual.visualType; 535 if (xclass != GLX_TRUE_COLOR && xclass == !GLX_DIRECT_COLOR) { 536 _mesa_warning(NULL, 537 "XMesa: RGB mode rendering not supported in given visual.\n"); 538 return GL_FALSE; 539 } 540 v->mesa_visual.indexBits = 0; 541 542 if (v->BitsPerPixel == 32) { 543 /* We use XImages for all front/back buffers. If an X Window or 544 * X Pixmap is 32bpp, there's no guarantee that the alpha channel 545 * will be preserved. For XImages we're in luck. 546 */ 547 v->mesa_visual.alphaBits = 8; 548 } 549 } 550 551 /* 552 * If MESA_INFO env var is set print out some debugging info 553 * which can help Brian figure out what's going on when a user 554 * reports bugs. 555 */ 556 if (_mesa_getenv("MESA_INFO")) { 557 printf("X/Mesa visual = %p\n", (void *) v); 558 printf("X/Mesa level = %d\n", v->mesa_visual.level); 559 printf("X/Mesa depth = %d\n", v->visinfo->depth); 560 printf("X/Mesa bits per pixel = %d\n", v->BitsPerPixel); 561 } 562 563 return GL_TRUE; 564} 565 566 567 568#define NUM_VISUAL_TYPES 6 569 570/** 571 * Convert an X visual type to a GLX visual type. 572 * 573 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.) 574 * to be converted. 575 * \return If \c visualType is a valid X visual type, a GLX visual type will 576 * be returned. Otherwise \c GLX_NONE will be returned. 577 * 578 * \note 579 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the 580 * DRI CVS tree. 581 */ 582static GLint 583xmesa_convert_from_x_visual_type( int visualType ) 584{ 585 static const int glx_visual_types[ NUM_VISUAL_TYPES ] = { 586 GLX_STATIC_GRAY, GLX_GRAY_SCALE, 587 GLX_STATIC_COLOR, GLX_PSEUDO_COLOR, 588 GLX_TRUE_COLOR, GLX_DIRECT_COLOR 589 }; 590 591 return ( (unsigned) visualType < NUM_VISUAL_TYPES ) 592 ? glx_visual_types[ visualType ] : GLX_NONE; 593} 594 595 596/**********************************************************************/ 597/***** Public Functions *****/ 598/**********************************************************************/ 599 600 601/* 602 * Create a new X/Mesa visual. 603 * Input: display - X11 display 604 * visinfo - an XVisualInfo pointer 605 * rgb_flag - GL_TRUE = RGB mode, 606 * GL_FALSE = color index mode 607 * alpha_flag - alpha buffer requested? 608 * db_flag - GL_TRUE = double-buffered, 609 * GL_FALSE = single buffered 610 * stereo_flag - stereo visual? 611 * ximage_flag - GL_TRUE = use an XImage for back buffer, 612 * GL_FALSE = use an off-screen pixmap for back buffer 613 * depth_size - requested bits/depth values, or zero 614 * stencil_size - requested bits/stencil values, or zero 615 * accum_red_size - requested bits/red accum values, or zero 616 * accum_green_size - requested bits/green accum values, or zero 617 * accum_blue_size - requested bits/blue accum values, or zero 618 * accum_alpha_size - requested bits/alpha accum values, or zero 619 * num_samples - number of samples/pixel if multisampling, or zero 620 * level - visual level, usually 0 621 * visualCaveat - ala the GLX extension, usually GLX_NONE 622 * Return; a new XMesaVisual or 0 if error. 623 */ 624PUBLIC 625XMesaVisual XMesaCreateVisual( Display *display, 626 XVisualInfo * visinfo, 627 GLboolean rgb_flag, 628 GLboolean alpha_flag, 629 GLboolean db_flag, 630 GLboolean stereo_flag, 631 GLboolean ximage_flag, 632 GLint depth_size, 633 GLint stencil_size, 634 GLint accum_red_size, 635 GLint accum_green_size, 636 GLint accum_blue_size, 637 GLint accum_alpha_size, 638 GLint num_samples, 639 GLint level, 640 GLint visualCaveat ) 641{ 642 XMesaDisplay xmdpy = xmesa_init_display(display); 643 XMesaVisual v; 644 GLint red_bits, green_bits, blue_bits, alpha_bits; 645 646 if (!xmdpy) 647 return NULL; 648 649 /* For debugging only */ 650 if (_mesa_getenv("MESA_XSYNC")) { 651 /* This makes debugging X easier. 652 * In your debugger, set a breakpoint on _XError to stop when an 653 * X protocol error is generated. 654 */ 655 XSynchronize( display, 1 ); 656 } 657 658 v = (XMesaVisual) CALLOC_STRUCT(xmesa_visual); 659 if (!v) { 660 return NULL; 661 } 662 663 v->display = display; 664 665 /* Save a copy of the XVisualInfo struct because the user may Xfree() 666 * the struct but we may need some of the information contained in it 667 * at a later time. 668 */ 669 v->visinfo = (XVisualInfo *) MALLOC(sizeof(*visinfo)); 670 if (!v->visinfo) { 671 free(v); 672 return NULL; 673 } 674 memcpy(v->visinfo, visinfo, sizeof(*visinfo)); 675 676 v->ximage_flag = ximage_flag; 677 678 v->mesa_visual.redMask = visinfo->red_mask; 679 v->mesa_visual.greenMask = visinfo->green_mask; 680 v->mesa_visual.blueMask = visinfo->blue_mask; 681 v->mesa_visual.visualID = visinfo->visualid; 682 v->mesa_visual.screen = visinfo->screen; 683 684#if !(defined(__cplusplus) || defined(c_plusplus)) 685 v->mesa_visual.visualType = xmesa_convert_from_x_visual_type(visinfo->class); 686#else 687 v->mesa_visual.visualType = xmesa_convert_from_x_visual_type(visinfo->c_class); 688#endif 689 690 v->mesa_visual.visualRating = visualCaveat; 691 692 if (alpha_flag) 693 v->mesa_visual.alphaBits = 8; 694 695 (void) initialize_visual_and_buffer( v, NULL, rgb_flag, 0, 0 ); 696 697 { 698 const int xclass = v->mesa_visual.visualType; 699 if (xclass == GLX_TRUE_COLOR || xclass == GLX_DIRECT_COLOR) { 700 red_bits = _mesa_bitcount(GET_REDMASK(v)); 701 green_bits = _mesa_bitcount(GET_GREENMASK(v)); 702 blue_bits = _mesa_bitcount(GET_BLUEMASK(v)); 703 } 704 else { 705 /* this is an approximation */ 706 int depth; 707 depth = v->visinfo->depth; 708 red_bits = depth / 3; 709 depth -= red_bits; 710 green_bits = depth / 2; 711 depth -= green_bits; 712 blue_bits = depth; 713 alpha_bits = 0; 714 assert( red_bits + green_bits + blue_bits == v->visinfo->depth ); 715 } 716 alpha_bits = v->mesa_visual.alphaBits; 717 } 718 719 _mesa_initialize_visual( &v->mesa_visual, 720 db_flag, stereo_flag, 721 red_bits, green_bits, 722 blue_bits, alpha_bits, 723 depth_size, 724 stencil_size, 725 accum_red_size, accum_green_size, 726 accum_blue_size, accum_alpha_size, 727 0 ); 728 729 v->stvis.buffer_mask = ST_ATTACHMENT_FRONT_LEFT_MASK; 730 if (db_flag) 731 v->stvis.buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK; 732 if (stereo_flag) { 733 v->stvis.buffer_mask |= ST_ATTACHMENT_FRONT_RIGHT_MASK; 734 if (db_flag) 735 v->stvis.buffer_mask |= ST_ATTACHMENT_BACK_RIGHT_MASK; 736 } 737 738 v->stvis.color_format = choose_pixel_format(v); 739 if (v->stvis.color_format == PIPE_FORMAT_NONE) { 740 FREE(v->visinfo); 741 FREE(v); 742 return NULL; 743 } 744 745 v->stvis.depth_stencil_format = 746 choose_depth_stencil_format(xmdpy, depth_size, stencil_size); 747 748 v->stvis.accum_format = (accum_red_size + 749 accum_green_size + accum_blue_size + accum_alpha_size) ? 750 PIPE_FORMAT_R16G16B16A16_SNORM : PIPE_FORMAT_NONE; 751 752 v->stvis.samples = num_samples; 753 v->stvis.render_buffer = ST_ATTACHMENT_INVALID; 754 755 /* XXX minor hack */ 756 v->mesa_visual.level = level; 757 return v; 758} 759 760 761PUBLIC 762void XMesaDestroyVisual( XMesaVisual v ) 763{ 764 free(v->visinfo); 765 free(v); 766} 767 768 769/** 770 * Do per-display initializations. 771 */ 772void 773xmesa_init( Display *display ) 774{ 775 xmesa_init_display(display); 776} 777 778 779/** 780 * Create a new XMesaContext. 781 * \param v the XMesaVisual 782 * \param share_list another XMesaContext with which to share display 783 * lists or NULL if no sharing is wanted. 784 * \return an XMesaContext or NULL if error. 785 */ 786PUBLIC 787XMesaContext XMesaCreateContext( XMesaVisual v, XMesaContext share_list ) 788{ 789 XMesaDisplay xmdpy = xmesa_init_display(v->display); 790 XMesaContext c; 791 792 if (!xmdpy) 793 return NULL; 794 795 /* Note: the XMesaContext contains a Mesa GLcontext struct (inheritance) */ 796 c = (XMesaContext) CALLOC_STRUCT(xmesa_context); 797 if (!c) 798 return NULL; 799 800 c->xm_visual = v; 801 c->xm_buffer = NULL; /* set later by XMesaMakeCurrent */ 802 c->xm_read_buffer = NULL; 803 804 c->st = stapi->create_context(stapi, xmdpy->smapi, 805 &v->stvis, (share_list) ? share_list->st : NULL); 806 if (c->st == NULL) 807 goto fail; 808 809 c->st->st_manager_private = (void *) c; 810 811 return c; 812 813fail: 814 if (c->st) 815 c->st->destroy(c->st); 816 817 free(c); 818 return NULL; 819} 820 821 822 823PUBLIC 824void XMesaDestroyContext( XMesaContext c ) 825{ 826 c->st->destroy(c->st); 827 828 /* FIXME: We should destroy the screen here, but if we do so, surfaces may 829 * outlive it, causing segfaults 830 struct pipe_screen *screen = c->st->pipe->screen; 831 screen->destroy(screen); 832 */ 833 834 free(c); 835} 836 837 838 839/** 840 * Private function for creating an XMesaBuffer which corresponds to an 841 * X window or pixmap. 842 * \param v the window's XMesaVisual 843 * \param w the window we're wrapping 844 * \return new XMesaBuffer or NULL if error 845 */ 846PUBLIC XMesaBuffer 847XMesaCreateWindowBuffer(XMesaVisual v, Window w) 848{ 849 XWindowAttributes attr; 850 XMesaBuffer b; 851 Colormap cmap; 852 int depth; 853 854 assert(v); 855 assert(w); 856 857 /* Check that window depth matches visual depth */ 858 XGetWindowAttributes( v->display, w, &attr ); 859 depth = attr.depth; 860 if (v->visinfo->depth != depth) { 861 _mesa_warning(NULL, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n", 862 v->visinfo->depth, depth); 863 return NULL; 864 } 865 866 /* Find colormap */ 867 if (attr.colormap) { 868 cmap = attr.colormap; 869 } 870 else { 871 _mesa_warning(NULL, "Window %u has no colormap!\n", (unsigned int) w); 872 /* this is weird, a window w/out a colormap!? */ 873 /* OK, let's just allocate a new one and hope for the best */ 874 cmap = XCreateColormap(v->display, w, attr.visual, AllocNone); 875 } 876 877 b = create_xmesa_buffer((Drawable) w, WINDOW, v, cmap); 878 if (!b) 879 return NULL; 880 881 if (!initialize_visual_and_buffer( v, b, v->mesa_visual.rgbMode, 882 (Drawable) w, cmap )) { 883 xmesa_free_buffer(b); 884 return NULL; 885 } 886 887 return b; 888} 889 890 891 892/** 893 * Create a new XMesaBuffer from an X pixmap. 894 * 895 * \param v the XMesaVisual 896 * \param p the pixmap 897 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or 898 * \c GLX_DIRECT_COLOR visual for the pixmap 899 * \returns new XMesaBuffer or NULL if error 900 */ 901PUBLIC XMesaBuffer 902XMesaCreatePixmapBuffer(XMesaVisual v, Pixmap p, Colormap cmap) 903{ 904 XMesaBuffer b; 905 906 assert(v); 907 908 b = create_xmesa_buffer((Drawable) p, PIXMAP, v, cmap); 909 if (!b) 910 return NULL; 911 912 if (!initialize_visual_and_buffer(v, b, v->mesa_visual.rgbMode, 913 (Drawable) p, cmap)) { 914 xmesa_free_buffer(b); 915 return NULL; 916 } 917 918 return b; 919} 920 921 922/** 923 * For GLX_EXT_texture_from_pixmap 924 */ 925XMesaBuffer 926XMesaCreatePixmapTextureBuffer(XMesaVisual v, Pixmap p, 927 Colormap cmap, 928 int format, int target, int mipmap) 929{ 930 GET_CURRENT_CONTEXT(ctx); 931 XMesaBuffer b; 932 933 assert(v); 934 935 b = create_xmesa_buffer((Drawable) p, PIXMAP, v, cmap); 936 if (!b) 937 return NULL; 938 939 /* get pixmap size */ 940 xmesa_get_window_size(v->display, b, &b->width, &b->height); 941 942 if (target == 0) { 943 /* examine dims */ 944 if (ctx->Extensions.ARB_texture_non_power_of_two) { 945 target = GLX_TEXTURE_2D_EXT; 946 } 947 else if ( _mesa_bitcount(b->width) == 1 948 && _mesa_bitcount(b->height) == 1) { 949 /* power of two size */ 950 if (b->height == 1) { 951 target = GLX_TEXTURE_1D_EXT; 952 } 953 else { 954 target = GLX_TEXTURE_2D_EXT; 955 } 956 } 957 else if (ctx->Extensions.NV_texture_rectangle) { 958 target = GLX_TEXTURE_RECTANGLE_EXT; 959 } 960 else { 961 /* non power of two textures not supported */ 962 XMesaDestroyBuffer(b); 963 return 0; 964 } 965 } 966 967 b->TextureTarget = target; 968 b->TextureFormat = format; 969 b->TextureMipmap = mipmap; 970 971 if (!initialize_visual_and_buffer(v, b, v->mesa_visual.rgbMode, 972 (Drawable) p, cmap)) { 973 xmesa_free_buffer(b); 974 return NULL; 975 } 976 977 return b; 978} 979 980 981 982XMesaBuffer 983XMesaCreatePBuffer(XMesaVisual v, Colormap cmap, 984 unsigned int width, unsigned int height) 985{ 986 Window root; 987 Drawable drawable; /* X Pixmap Drawable */ 988 XMesaBuffer b; 989 990 /* allocate pixmap for front buffer */ 991 root = RootWindow( v->display, v->visinfo->screen ); 992 drawable = XCreatePixmap(v->display, root, width, height, 993 v->visinfo->depth); 994 if (!drawable) 995 return NULL; 996 997 b = create_xmesa_buffer(drawable, PBUFFER, v, cmap); 998 if (!b) 999 return NULL; 1000 1001 if (!initialize_visual_and_buffer(v, b, v->mesa_visual.rgbMode, 1002 drawable, cmap)) { 1003 xmesa_free_buffer(b); 1004 return NULL; 1005 } 1006 1007 return b; 1008} 1009 1010 1011 1012/* 1013 * Deallocate an XMesaBuffer structure and all related info. 1014 */ 1015PUBLIC void 1016XMesaDestroyBuffer(XMesaBuffer b) 1017{ 1018 xmesa_free_buffer(b); 1019} 1020 1021 1022/** 1023 * Query the current drawable size and notify the binding context. 1024 */ 1025void 1026xmesa_check_buffer_size(XMesaBuffer b) 1027{ 1028 XMesaContext xmctx = XMesaGetCurrentContext(); 1029 1030 if (b->type == PBUFFER) 1031 return; 1032 1033 xmesa_get_window_size(b->xm_visual->display, b, &b->width, &b->height); 1034 if (xmctx && xmctx->xm_buffer == b) 1035 xmctx->st->notify_invalid_framebuffer(xmctx->st, b->stfb); 1036} 1037 1038 1039/* 1040 * Bind buffer b to context c and make c the current rendering context. 1041 */ 1042PUBLIC 1043GLboolean XMesaMakeCurrent2( XMesaContext c, XMesaBuffer drawBuffer, 1044 XMesaBuffer readBuffer ) 1045{ 1046 XMesaContext old_ctx = XMesaGetCurrentContext(); 1047 1048 if (old_ctx && old_ctx != c) { 1049 XMesaFlush(old_ctx); 1050 old_ctx->xm_buffer = NULL; 1051 old_ctx->xm_read_buffer = NULL; 1052 } 1053 1054 if (c) { 1055 if (!drawBuffer || !readBuffer) 1056 return GL_FALSE; /* must specify buffers! */ 1057 1058 if (c == old_ctx && 1059 c->xm_buffer == drawBuffer && 1060 c->xm_read_buffer == readBuffer) 1061 return GL_TRUE; 1062 1063 xmesa_check_buffer_size(drawBuffer); 1064 if (readBuffer != drawBuffer) 1065 xmesa_check_buffer_size(readBuffer); 1066 1067 c->xm_buffer = drawBuffer; 1068 c->xm_read_buffer = readBuffer; 1069 1070 stapi->make_current(stapi, c->st, drawBuffer->stfb, readBuffer->stfb); 1071 1072 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */ 1073 drawBuffer->wasCurrent = GL_TRUE; 1074 } 1075 else { 1076 /* Detach */ 1077 stapi->make_current(stapi, NULL, NULL, NULL); 1078 1079 } 1080 return GL_TRUE; 1081} 1082 1083 1084/* 1085 * Unbind the context c from its buffer. 1086 */ 1087GLboolean XMesaUnbindContext( XMesaContext c ) 1088{ 1089 /* A no-op for XFree86 integration purposes */ 1090 return GL_TRUE; 1091} 1092 1093 1094XMesaContext XMesaGetCurrentContext( void ) 1095{ 1096 struct st_context_iface *st = stapi->get_current(stapi); 1097 return (XMesaContext) (st) ? st->st_manager_private : NULL; 1098} 1099 1100 1101 1102/** 1103 * Swap front and back color buffers and have winsys display front buffer. 1104 * If there's no front color buffer no swap actually occurs. 1105 */ 1106PUBLIC 1107void XMesaSwapBuffers( XMesaBuffer b ) 1108{ 1109 XMesaContext xmctx = XMesaGetCurrentContext(); 1110 1111 if (xmctx && xmctx->xm_buffer == b) { 1112 xmctx->st->flush( xmctx->st, 1113 PIPE_FLUSH_RENDER_CACHE | 1114 PIPE_FLUSH_SWAPBUFFERS | 1115 PIPE_FLUSH_FRAME, 1116 NULL); 1117 } 1118 1119 xmesa_swap_st_framebuffer(b->stfb); 1120} 1121 1122 1123 1124/* 1125 * Copy sub-region of back buffer to front buffer 1126 */ 1127void XMesaCopySubBuffer( XMesaBuffer b, int x, int y, int width, int height ) 1128{ 1129 xmesa_copy_st_framebuffer(b->stfb, 1130 ST_ATTACHMENT_BACK_LEFT, ST_ATTACHMENT_FRONT_LEFT, 1131 x, y, width, height); 1132} 1133 1134 1135 1136void XMesaFlush( XMesaContext c ) 1137{ 1138 if (c && c->xm_visual->display) { 1139 XMesaDisplay xmdpy = xmesa_init_display(c->xm_visual->display); 1140 struct pipe_fence_handle *fence = NULL; 1141 1142 c->st->flush(c->st, PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, &fence); 1143 if (fence) { 1144 xmdpy->screen->fence_finish(xmdpy->screen, fence, 0); 1145 xmdpy->screen->fence_reference(xmdpy->screen, &fence, NULL); 1146 } 1147 XSync( c->xm_visual->display, False ); 1148 } 1149} 1150 1151 1152 1153 1154 1155XMesaBuffer XMesaFindBuffer( Display *dpy, Drawable d ) 1156{ 1157 XMesaBuffer b; 1158 for (b = XMesaBufferList; b; b = b->Next) { 1159 if (b->ws.drawable == d && b->xm_visual->display == dpy) { 1160 return b; 1161 } 1162 } 1163 return NULL; 1164} 1165 1166 1167/** 1168 * Free/destroy all XMesaBuffers associated with given display. 1169 */ 1170void xmesa_destroy_buffers_on_display(Display *dpy) 1171{ 1172 XMesaBuffer b, next; 1173 for (b = XMesaBufferList; b; b = next) { 1174 next = b->Next; 1175 if (b->xm_visual->display == dpy) { 1176 xmesa_free_buffer(b); 1177 } 1178 } 1179} 1180 1181 1182/* 1183 * Look for XMesaBuffers whose X window has been destroyed. 1184 * Deallocate any such XMesaBuffers. 1185 */ 1186void XMesaGarbageCollect( void ) 1187{ 1188 XMesaBuffer b, next; 1189 for (b=XMesaBufferList; b; b=next) { 1190 next = b->Next; 1191 if (b->xm_visual && 1192 b->xm_visual->display && 1193 b->ws.drawable && 1194 b->type == WINDOW) { 1195 XSync(b->xm_visual->display, False); 1196 if (!window_exists( b->xm_visual->display, b->ws.drawable )) { 1197 /* found a dead window, free the ancillary info */ 1198 XMesaDestroyBuffer( b ); 1199 } 1200 } 1201 } 1202} 1203 1204 1205 1206 1207PUBLIC void 1208XMesaBindTexImage(Display *dpy, XMesaBuffer drawable, int buffer, 1209 const int *attrib_list) 1210{ 1211} 1212 1213 1214 1215PUBLIC void 1216XMesaReleaseTexImage(Display *dpy, XMesaBuffer drawable, int buffer) 1217{ 1218} 1219 1220 1221void 1222XMesaCopyContext(XMesaContext src, XMesaContext dst, unsigned long mask) 1223{ 1224 if (dst->st->copy) 1225 dst->st->copy(dst->st, src->st, mask); 1226} 1227