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