1/* 2 * Mesa 3-D graphics library 3 * 4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included 14 * in all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * 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 <stdio.h> 57#include "xm_api.h" 58#include "xm_st.h" 59 60#include "pipe/p_context.h" 61#include "pipe/p_defines.h" 62#include "pipe/p_screen.h" 63#include "pipe/p_state.h" 64#include "frontend/api.h" 65 66#include "util/u_atomic.h" 67#include "util/u_inlines.h" 68#include "util/u_math.h" 69#include "util/u_memory.h" 70 71#include "hud/hud_context.h" 72 73#include "main/errors.h" 74 75#include "xm_public.h" 76#include <GL/glx.h> 77 78 79/* Driver interface routines, set up by xlib backend on library 80 * _init(). These are global in the same way that function names are 81 * global. 82 */ 83static struct xm_driver driver; 84static struct st_api *stapi; 85 86/* Default strict invalidate to false. This means we will not call 87 * XGetGeometry after every swapbuffers, which allows swapbuffers to 88 * remain asynchronous. For apps running at 100fps with synchronous 89 * swapping, a 10% boost is typical. For gears, I see closer to 20% 90 * speedup. 91 * 92 * Note that the work of copying data on swapbuffers doesn't disappear 93 * - this change just allows the X server to execute the PutImage 94 * asynchronously without us effectively blocked until its completion. 95 * 96 * This speeds up even llvmpipe's threaded rasterization as the 97 * swapbuffers operation was a large part of the serial component of 98 * an llvmpipe frame. 99 * 100 * The downside of this is correctness - applications which don't call 101 * glViewport on window resizes will get incorrect rendering. A 102 * better solution would be to have per-frame but asynchronous 103 * invalidation. Xcb almost looks as if it could provide this, but 104 * the API doesn't seem to quite be there. 105 */ 106boolean xmesa_strict_invalidate = FALSE; 107 108void xmesa_set_driver( const struct xm_driver *templ ) 109{ 110 driver = *templ; 111 stapi = driver.create_st_api(); 112 113 xmesa_strict_invalidate = 114 debug_get_bool_option("XMESA_STRICT_INVALIDATE", FALSE); 115} 116 117 118static int 119xmesa_get_param(struct st_manager *smapi, 120 enum st_manager_param param) 121{ 122 switch(param) { 123 case ST_MANAGER_BROKEN_INVALIDATE: 124 return !xmesa_strict_invalidate; 125 default: 126 return 0; 127 } 128} 129 130/* linked list of XMesaDisplay hooks per display */ 131typedef struct _XMesaExtDisplayInfo { 132 struct _XMesaExtDisplayInfo *next; 133 Display *display; 134 struct xmesa_display mesaDisplay; 135} XMesaExtDisplayInfo; 136 137typedef struct _XMesaExtInfo { 138 XMesaExtDisplayInfo *head; 139 int ndisplays; 140} XMesaExtInfo; 141 142static XMesaExtInfo MesaExtInfo; 143 144/* hook to delete XMesaDisplay on XDestroyDisplay */ 145extern void 146xmesa_close_display(Display *display) 147{ 148 XMesaExtDisplayInfo *info, *prev; 149 150 /* These assertions are not valid since screen creation can fail and result 151 * in an empty list 152 assert(MesaExtInfo.ndisplays > 0); 153 assert(MesaExtInfo.head); 154 */ 155 156 _XLockMutex(_Xglobal_lock); 157 /* first find display */ 158 prev = NULL; 159 for (info = MesaExtInfo.head; info; info = info->next) { 160 if (info->display == display) { 161 prev = info; 162 break; 163 } 164 } 165 166 if (info == NULL) { 167 /* no display found */ 168 _XUnlockMutex(_Xglobal_lock); 169 return; 170 } 171 172 /* remove display entry from list */ 173 if (prev != MesaExtInfo.head) { 174 prev->next = info->next; 175 } else { 176 MesaExtInfo.head = info->next; 177 } 178 MesaExtInfo.ndisplays--; 179 180 _XUnlockMutex(_Xglobal_lock); 181 182 /* don't forget to clean up mesaDisplay */ 183 XMesaDisplay xmdpy = &info->mesaDisplay; 184 185 /** 186 * XXX: Don't destroy the screens here, since there may still 187 * be some dangling screen pointers that are used after this point 188 * if (xmdpy->screen) { 189 * xmdpy->screen->destroy(xmdpy->screen); 190 * } 191 */ 192 193 if (xmdpy->smapi->destroy) 194 xmdpy->smapi->destroy(xmdpy->smapi); 195 free(xmdpy->smapi); 196 197 XFree((char *) info); 198} 199 200static XMesaDisplay 201xmesa_init_display( Display *display ) 202{ 203 static mtx_t init_mutex = _MTX_INITIALIZER_NP; 204 XMesaDisplay xmdpy; 205 XMesaExtDisplayInfo *info; 206 207 if (display == NULL) { 208 return NULL; 209 } 210 211 mtx_lock(&init_mutex); 212 213 /* Look for XMesaDisplay which corresponds to this display */ 214 info = MesaExtInfo.head; 215 while(info) { 216 if (info->display == display) { 217 /* Found it */ 218 mtx_unlock(&init_mutex); 219 return &info->mesaDisplay; 220 } 221 info = info->next; 222 } 223 224 /* Not found. Create new XMesaDisplay */ 225 /* first allocate X-related resources and hook destroy callback */ 226 227 /* allocate mesa display info */ 228 info = (XMesaExtDisplayInfo *) Xmalloc(sizeof(XMesaExtDisplayInfo)); 229 if (info == NULL) { 230 mtx_unlock(&init_mutex); 231 return NULL; 232 } 233 info->display = display; 234 235 xmdpy = &info->mesaDisplay; /* to be filled out below */ 236 xmdpy->display = display; 237 xmdpy->pipe = NULL; 238 239 xmdpy->smapi = CALLOC_STRUCT(st_manager); 240 if (!xmdpy->smapi) { 241 Xfree(info); 242 mtx_unlock(&init_mutex); 243 return NULL; 244 } 245 246 xmdpy->screen = driver.create_pipe_screen(display); 247 if (!xmdpy->screen) { 248 free(xmdpy->smapi); 249 Xfree(info); 250 mtx_unlock(&init_mutex); 251 return NULL; 252 } 253 254 /* At this point, both smapi and screen are known to be valid */ 255 xmdpy->smapi->screen = xmdpy->screen; 256 xmdpy->smapi->get_param = xmesa_get_param; 257 (void) mtx_init(&xmdpy->mutex, mtx_plain); 258 259 /* chain to the list of displays */ 260 _XLockMutex(_Xglobal_lock); 261 info->next = MesaExtInfo.head; 262 MesaExtInfo.head = info; 263 MesaExtInfo.ndisplays++; 264 _XUnlockMutex(_Xglobal_lock); 265 266 mtx_unlock(&init_mutex); 267 268 return xmdpy; 269} 270 271 272/**********************************************************************/ 273/***** X Utility Functions *****/ 274/**********************************************************************/ 275 276 277/** 278 * Return the host's byte order as LSBFirst or MSBFirst ala X. 279 */ 280static int host_byte_order( void ) 281{ 282 int i = 1; 283 char *cptr = (char *) &i; 284 return (*cptr==1) ? LSBFirst : MSBFirst; 285} 286 287 288 289 290/** 291 * Return the true number of bits per pixel for XImages. 292 * For example, if we request a 24-bit deep visual we may actually need/get 293 * 32bpp XImages. This function returns the appropriate bpp. 294 * Input: dpy - the X display 295 * visinfo - desribes the visual to be used for XImages 296 * Return: true number of bits per pixel for XImages 297 */ 298static int 299bits_per_pixel( XMesaVisual xmv ) 300{ 301 Display *dpy = xmv->display; 302 XVisualInfo * visinfo = xmv->visinfo; 303 XImage *img; 304 int bitsPerPixel; 305 /* Create a temporary XImage */ 306 img = XCreateImage( dpy, visinfo->visual, visinfo->depth, 307 ZPixmap, 0, /*format, offset*/ 308 malloc(8), /*data*/ 309 1, 1, /*width, height*/ 310 32, /*bitmap_pad*/ 311 0 /*bytes_per_line*/ 312 ); 313 assert(img); 314 /* grab the bits/pixel value */ 315 bitsPerPixel = img->bits_per_pixel; 316 /* free the XImage */ 317 free( img->data ); 318 img->data = NULL; 319 XDestroyImage( img ); 320 return bitsPerPixel; 321} 322 323 324 325/* 326 * Determine if a given X window ID is valid (window exists). 327 * Do this by calling XGetWindowAttributes() for the window and 328 * checking if we catch an X error. 329 * Input: dpy - the display 330 * win - the window to check for existence 331 * Return: GL_TRUE - window exists 332 * GL_FALSE - window doesn't exist 333 */ 334static GLboolean WindowExistsFlag; 335 336static int window_exists_err_handler( Display* dpy, XErrorEvent* xerr ) 337{ 338 (void) dpy; 339 if (xerr->error_code == BadWindow) { 340 WindowExistsFlag = GL_FALSE; 341 } 342 return 0; 343} 344 345static GLboolean window_exists( Display *dpy, Window win ) 346{ 347 XWindowAttributes wa; 348 int (*old_handler)( Display*, XErrorEvent* ); 349 WindowExistsFlag = GL_TRUE; 350 old_handler = XSetErrorHandler(window_exists_err_handler); 351 XGetWindowAttributes( dpy, win, &wa ); /* dummy request */ 352 XSetErrorHandler(old_handler); 353 return WindowExistsFlag; 354} 355 356static Status 357get_drawable_size( Display *dpy, Drawable d, uint *width, uint *height ) 358{ 359 Window root; 360 Status stat; 361 int xpos, ypos; 362 unsigned int w, h, bw, depth; 363 stat = XGetGeometry(dpy, d, &root, &xpos, &ypos, &w, &h, &bw, &depth); 364 *width = w; 365 *height = h; 366 return stat; 367} 368 369 370/** 371 * Return the size of the window (or pixmap) that corresponds to the 372 * given XMesaBuffer. 373 * \param width returns width in pixels 374 * \param height returns height in pixels 375 */ 376void 377xmesa_get_window_size(Display *dpy, XMesaBuffer b, 378 GLuint *width, GLuint *height) 379{ 380 XMesaDisplay xmdpy = xmesa_init_display(dpy); 381 Status stat; 382 383 mtx_lock(&xmdpy->mutex); 384 stat = get_drawable_size(dpy, b->ws.drawable, width, height); 385 mtx_unlock(&xmdpy->mutex); 386 387 if (!stat) { 388 /* probably querying a window that's recently been destroyed */ 389 _mesa_warning(NULL, "XGetGeometry failed!\n"); 390 *width = *height = 1; 391 } 392} 393 394#define GET_REDMASK(__v) __v->mesa_visual.redMask 395#define GET_GREENMASK(__v) __v->mesa_visual.greenMask 396#define GET_BLUEMASK(__v) __v->mesa_visual.blueMask 397 398 399/** 400 * Choose the pixel format for the given visual. 401 * This will tell the gallium driver how to pack pixel data into 402 * drawing surfaces. 403 */ 404static GLuint 405choose_pixel_format(XMesaVisual v) 406{ 407 boolean native_byte_order = (host_byte_order() == 408 ImageByteOrder(v->display)); 409 410 if ( GET_REDMASK(v) == 0x0000ff 411 && GET_GREENMASK(v) == 0x00ff00 412 && GET_BLUEMASK(v) == 0xff0000 413 && v->BitsPerPixel == 32) { 414 if (native_byte_order) { 415 /* no byteswapping needed */ 416 return PIPE_FORMAT_RGBA8888_UNORM; 417 } 418 else { 419 return PIPE_FORMAT_ABGR8888_UNORM; 420 } 421 } 422 else if ( GET_REDMASK(v) == 0xff0000 423 && GET_GREENMASK(v) == 0x00ff00 424 && GET_BLUEMASK(v) == 0x0000ff 425 && v->BitsPerPixel == 32) { 426 if (native_byte_order) { 427 /* no byteswapping needed */ 428 return PIPE_FORMAT_BGRA8888_UNORM; 429 } 430 else { 431 return PIPE_FORMAT_ARGB8888_UNORM; 432 } 433 } 434 else if ( GET_REDMASK(v) == 0x0000ff00 435 && GET_GREENMASK(v) == 0x00ff0000 436 && GET_BLUEMASK(v) == 0xff000000 437 && v->BitsPerPixel == 32) { 438 if (native_byte_order) { 439 /* no byteswapping needed */ 440 return PIPE_FORMAT_ARGB8888_UNORM; 441 } 442 else { 443 return PIPE_FORMAT_BGRA8888_UNORM; 444 } 445 } 446 else if ( GET_REDMASK(v) == 0xf800 447 && GET_GREENMASK(v) == 0x07e0 448 && GET_BLUEMASK(v) == 0x001f 449 && native_byte_order 450 && v->BitsPerPixel == 16) { 451 /* 5-6-5 RGB */ 452 return PIPE_FORMAT_B5G6R5_UNORM; 453 } 454 455 return PIPE_FORMAT_NONE; 456} 457 458 459/** 460 * Choose a depth/stencil format that satisfies the given depth and 461 * stencil sizes. 462 */ 463static enum pipe_format 464choose_depth_stencil_format(XMesaDisplay xmdpy, int depth, int stencil, 465 int sample_count) 466{ 467 const enum pipe_texture_target target = PIPE_TEXTURE_2D; 468 const unsigned tex_usage = PIPE_BIND_DEPTH_STENCIL; 469 enum pipe_format formats[8], fmt; 470 int count, i; 471 472 count = 0; 473 474 if (depth <= 16 && stencil == 0) { 475 formats[count++] = PIPE_FORMAT_Z16_UNORM; 476 } 477 if (depth <= 24 && stencil == 0) { 478 formats[count++] = PIPE_FORMAT_X8Z24_UNORM; 479 formats[count++] = PIPE_FORMAT_Z24X8_UNORM; 480 } 481 if (depth <= 24 && stencil <= 8) { 482 formats[count++] = PIPE_FORMAT_S8_UINT_Z24_UNORM; 483 formats[count++] = PIPE_FORMAT_Z24_UNORM_S8_UINT; 484 } 485 if (depth <= 32 && stencil == 0) { 486 formats[count++] = PIPE_FORMAT_Z32_UNORM; 487 } 488 489 fmt = PIPE_FORMAT_NONE; 490 for (i = 0; i < count; i++) { 491 if (xmdpy->screen->is_format_supported(xmdpy->screen, formats[i], 492 target, sample_count, 493 sample_count, tex_usage)) { 494 fmt = formats[i]; 495 break; 496 } 497 } 498 499 return fmt; 500} 501 502 503 504/**********************************************************************/ 505/***** Linked list of XMesaBuffers *****/ 506/**********************************************************************/ 507 508static XMesaBuffer XMesaBufferList = NULL; 509 510 511/** 512 * Allocate a new XMesaBuffer object which corresponds to the given drawable. 513 * Note that XMesaBuffer is derived from struct gl_framebuffer. 514 * The new XMesaBuffer will not have any size (Width=Height=0). 515 * 516 * \param d the corresponding X drawable (window or pixmap) 517 * \param type either WINDOW, PIXMAP or PBUFFER, describing d 518 * \param vis the buffer's visual 519 * \param cmap the window's colormap, if known. 520 * \return new XMesaBuffer or NULL if any problem 521 */ 522static XMesaBuffer 523create_xmesa_buffer(Drawable d, BufferType type, 524 XMesaVisual vis, Colormap cmap) 525{ 526 XMesaDisplay xmdpy = xmesa_init_display(vis->display); 527 XMesaBuffer b; 528 529 assert(type == WINDOW || type == PIXMAP || type == PBUFFER); 530 531 if (!xmdpy) 532 return NULL; 533 534 b = (XMesaBuffer) CALLOC_STRUCT(xmesa_buffer); 535 if (!b) 536 return NULL; 537 538 b->ws.drawable = d; 539 b->ws.visual = vis->visinfo->visual; 540 b->ws.depth = vis->visinfo->depth; 541 542 b->xm_visual = vis; 543 b->type = type; 544 b->cmap = cmap; 545 546 get_drawable_size(vis->display, d, &b->width, &b->height); 547 548 /* 549 * Create framebuffer, but we'll plug in our own renderbuffers below. 550 */ 551 b->stfb = xmesa_create_st_framebuffer(xmdpy, b); 552 553 /* GLX_EXT_texture_from_pixmap */ 554 b->TextureTarget = 0; 555 b->TextureFormat = GLX_TEXTURE_FORMAT_NONE_EXT; 556 b->TextureMipmap = 0; 557 558 /* insert buffer into linked list */ 559 b->Next = XMesaBufferList; 560 XMesaBufferList = b; 561 562 return b; 563} 564 565 566/** 567 * Find an XMesaBuffer by matching X display and colormap but NOT matching 568 * the notThis buffer. 569 */ 570XMesaBuffer 571xmesa_find_buffer(Display *dpy, Colormap cmap, XMesaBuffer notThis) 572{ 573 XMesaBuffer b; 574 for (b = XMesaBufferList; b; b = b->Next) { 575 if (b->xm_visual->display == dpy && 576 b->cmap == cmap && 577 b != notThis) { 578 return b; 579 } 580 } 581 return NULL; 582} 583 584 585/** 586 * Remove buffer from linked list, delete if no longer referenced. 587 */ 588static void 589xmesa_free_buffer(XMesaBuffer buffer) 590{ 591 XMesaBuffer prev = NULL, b; 592 593 for (b = XMesaBufferList; b; b = b->Next) { 594 if (b == buffer) { 595 /* unlink buffer from list */ 596 if (prev) 597 prev->Next = buffer->Next; 598 else 599 XMesaBufferList = buffer->Next; 600 601 /* Since the X window for the XMesaBuffer is going away, we don't 602 * want to dereference this pointer in the future. 603 */ 604 b->ws.drawable = 0; 605 606 /* Notify the st manager that the associated framebuffer interface 607 * object is no longer valid. 608 */ 609 stapi->destroy_drawable(stapi, buffer->stfb); 610 611 /* XXX we should move the buffer to a delete-pending list and destroy 612 * the buffer until it is no longer current. 613 */ 614 xmesa_destroy_st_framebuffer(buffer->stfb); 615 616 free(buffer); 617 618 return; 619 } 620 /* continue search */ 621 prev = b; 622 } 623 /* buffer not found in XMesaBufferList */ 624 _mesa_problem(NULL,"xmesa_free_buffer() - buffer not found\n"); 625} 626 627 628 629/**********************************************************************/ 630/***** Misc Private Functions *****/ 631/**********************************************************************/ 632 633 634/** 635 * When a context is bound for the first time, we can finally finish 636 * initializing the context's visual and buffer information. 637 * \param v the XMesaVisual to initialize 638 * \param b the XMesaBuffer to initialize (may be NULL) 639 * \param window the window/pixmap we're rendering into 640 * \param cmap the colormap associated with the window/pixmap 641 * \return GL_TRUE=success, GL_FALSE=failure 642 */ 643static GLboolean 644initialize_visual_and_buffer(XMesaVisual v, XMesaBuffer b, 645 Drawable window, Colormap cmap) 646{ 647 assert(!b || b->xm_visual == v); 648 649 /* Save true bits/pixel */ 650 v->BitsPerPixel = bits_per_pixel(v); 651 assert(v->BitsPerPixel > 0); 652 653 /* RGB WINDOW: 654 * We support RGB rendering into almost any kind of visual. 655 */ 656 const int xclass = v->visualType; 657 if (xclass != GLX_TRUE_COLOR && xclass != GLX_DIRECT_COLOR) { 658 _mesa_warning(NULL, 659 "XMesa: RGB mode rendering not supported in given visual.\n"); 660 return GL_FALSE; 661 } 662 663 if (v->BitsPerPixel == 32) { 664 /* We use XImages for all front/back buffers. If an X Window or 665 * X Pixmap is 32bpp, there's no guarantee that the alpha channel 666 * will be preserved. For XImages we're in luck. 667 */ 668 v->mesa_visual.alphaBits = 8; 669 } 670 671 /* 672 * If MESA_INFO env var is set print out some debugging info 673 * which can help Brian figure out what's going on when a user 674 * reports bugs. 675 */ 676 if (getenv("MESA_INFO")) { 677 printf("X/Mesa visual = %p\n", (void *) v); 678 printf("X/Mesa depth = %d\n", v->visinfo->depth); 679 printf("X/Mesa bits per pixel = %d\n", v->BitsPerPixel); 680 } 681 682 return GL_TRUE; 683} 684 685 686 687#define NUM_VISUAL_TYPES 6 688 689/** 690 * Convert an X visual type to a GLX visual type. 691 * 692 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.) 693 * to be converted. 694 * \return If \c visualType is a valid X visual type, a GLX visual type will 695 * be returned. Otherwise \c GLX_NONE will be returned. 696 * 697 * \note 698 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the 699 * DRI CVS tree. 700 */ 701static GLint 702xmesa_convert_from_x_visual_type( int visualType ) 703{ 704 static const int glx_visual_types[ NUM_VISUAL_TYPES ] = { 705 GLX_STATIC_GRAY, GLX_GRAY_SCALE, 706 GLX_STATIC_COLOR, GLX_PSEUDO_COLOR, 707 GLX_TRUE_COLOR, GLX_DIRECT_COLOR 708 }; 709 710 return ( (unsigned) visualType < NUM_VISUAL_TYPES ) 711 ? glx_visual_types[ visualType ] : GLX_NONE; 712} 713 714 715/**********************************************************************/ 716/***** Public Functions *****/ 717/**********************************************************************/ 718 719 720/* 721 * Create a new X/Mesa visual. 722 * Input: display - X11 display 723 * visinfo - an XVisualInfo pointer 724 * rgb_flag - GL_TRUE = RGB mode, 725 * GL_FALSE = color index mode 726 * alpha_flag - alpha buffer requested? 727 * db_flag - GL_TRUE = double-buffered, 728 * GL_FALSE = single buffered 729 * stereo_flag - stereo visual? 730 * ximage_flag - GL_TRUE = use an XImage for back buffer, 731 * GL_FALSE = use an off-screen pixmap for back buffer 732 * depth_size - requested bits/depth values, or zero 733 * stencil_size - requested bits/stencil values, or zero 734 * accum_red_size - requested bits/red accum values, or zero 735 * accum_green_size - requested bits/green accum values, or zero 736 * accum_blue_size - requested bits/blue accum values, or zero 737 * accum_alpha_size - requested bits/alpha accum values, or zero 738 * num_samples - number of samples/pixel if multisampling, or zero 739 * level - visual level, usually 0 740 * visualCaveat - ala the GLX extension, usually GLX_NONE 741 * Return; a new XMesaVisual or 0 if error. 742 */ 743PUBLIC 744XMesaVisual XMesaCreateVisual( Display *display, 745 XVisualInfo * visinfo, 746 GLboolean rgb_flag, 747 GLboolean alpha_flag, 748 GLboolean db_flag, 749 GLboolean stereo_flag, 750 GLboolean ximage_flag, 751 GLint depth_size, 752 GLint stencil_size, 753 GLint accum_red_size, 754 GLint accum_green_size, 755 GLint accum_blue_size, 756 GLint accum_alpha_size, 757 GLint num_samples, 758 GLint level, 759 GLint visualCaveat ) 760{ 761 XMesaDisplay xmdpy = xmesa_init_display(display); 762 XMesaVisual v; 763 GLint red_bits, green_bits, blue_bits, alpha_bits; 764 765 if (!xmdpy) 766 return NULL; 767 768 if (!rgb_flag) 769 return NULL; 770 771 /* For debugging only */ 772 if (getenv("MESA_XSYNC")) { 773 /* This makes debugging X easier. 774 * In your debugger, set a breakpoint on _XError to stop when an 775 * X protocol error is generated. 776 */ 777 XSynchronize( display, 1 ); 778 } 779 780 v = (XMesaVisual) CALLOC_STRUCT(xmesa_visual); 781 if (!v) { 782 return NULL; 783 } 784 785 v->display = display; 786 787 /* Save a copy of the XVisualInfo struct because the user may Xfree() 788 * the struct but we may need some of the information contained in it 789 * at a later time. 790 */ 791 v->visinfo = malloc(sizeof(*visinfo)); 792 if (!v->visinfo) { 793 free(v); 794 return NULL; 795 } 796 memcpy(v->visinfo, visinfo, sizeof(*visinfo)); 797 798 v->ximage_flag = ximage_flag; 799 800 v->mesa_visual.redMask = visinfo->red_mask; 801 v->mesa_visual.greenMask = visinfo->green_mask; 802 v->mesa_visual.blueMask = visinfo->blue_mask; 803 v->visualID = visinfo->visualid; 804 v->screen = visinfo->screen; 805 806#if !(defined(__cplusplus) || defined(c_plusplus)) 807 v->visualType = xmesa_convert_from_x_visual_type(visinfo->class); 808#else 809 v->visualType = xmesa_convert_from_x_visual_type(visinfo->c_class); 810#endif 811 812 if (alpha_flag) 813 v->mesa_visual.alphaBits = 8; 814 815 (void) initialize_visual_and_buffer( v, NULL, 0, 0 ); 816 817 { 818 const int xclass = v->visualType; 819 if (xclass == GLX_TRUE_COLOR || xclass == GLX_DIRECT_COLOR) { 820 red_bits = util_bitcount(GET_REDMASK(v)); 821 green_bits = util_bitcount(GET_GREENMASK(v)); 822 blue_bits = util_bitcount(GET_BLUEMASK(v)); 823 } 824 else { 825 /* this is an approximation */ 826 int depth; 827 depth = v->visinfo->depth; 828 red_bits = depth / 3; 829 depth -= red_bits; 830 green_bits = depth / 2; 831 depth -= green_bits; 832 blue_bits = depth; 833 alpha_bits = 0; 834 assert( red_bits + green_bits + blue_bits == v->visinfo->depth ); 835 } 836 alpha_bits = v->mesa_visual.alphaBits; 837 } 838 839 /* initialize visual */ 840 { 841 struct gl_config *vis = &v->mesa_visual; 842 843 vis->doubleBufferMode = db_flag; 844 vis->stereoMode = stereo_flag; 845 846 vis->redBits = red_bits; 847 vis->greenBits = green_bits; 848 vis->blueBits = blue_bits; 849 vis->alphaBits = alpha_bits; 850 vis->rgbBits = red_bits + green_bits + blue_bits; 851 852 vis->depthBits = depth_size; 853 vis->stencilBits = stencil_size; 854 855 vis->accumRedBits = accum_red_size; 856 vis->accumGreenBits = accum_green_size; 857 vis->accumBlueBits = accum_blue_size; 858 vis->accumAlphaBits = accum_alpha_size; 859 860 vis->samples = num_samples; 861 } 862 863 v->stvis.buffer_mask = ST_ATTACHMENT_FRONT_LEFT_MASK; 864 if (db_flag) 865 v->stvis.buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK; 866 if (stereo_flag) { 867 v->stvis.buffer_mask |= ST_ATTACHMENT_FRONT_RIGHT_MASK; 868 if (db_flag) 869 v->stvis.buffer_mask |= ST_ATTACHMENT_BACK_RIGHT_MASK; 870 } 871 872 v->stvis.color_format = choose_pixel_format(v); 873 874 /* Check format support at requested num_samples (for multisample) */ 875 if (!xmdpy->screen->is_format_supported(xmdpy->screen, 876 v->stvis.color_format, 877 PIPE_TEXTURE_2D, num_samples, 878 num_samples, 879 PIPE_BIND_RENDER_TARGET)) 880 v->stvis.color_format = PIPE_FORMAT_NONE; 881 882 if (v->stvis.color_format == PIPE_FORMAT_NONE) { 883 free(v->visinfo); 884 free(v); 885 return NULL; 886 } 887 888 v->stvis.depth_stencil_format = 889 choose_depth_stencil_format(xmdpy, depth_size, stencil_size, 890 num_samples); 891 892 v->stvis.accum_format = (accum_red_size + 893 accum_green_size + accum_blue_size + accum_alpha_size) ? 894 PIPE_FORMAT_R16G16B16A16_SNORM : PIPE_FORMAT_NONE; 895 896 v->stvis.samples = num_samples; 897 898 return v; 899} 900 901 902PUBLIC 903void XMesaDestroyVisual( XMesaVisual v ) 904{ 905 free(v->visinfo); 906 free(v); 907} 908 909 910/** 911 * Return the informative name. 912 */ 913const char * 914xmesa_get_name(void) 915{ 916 return stapi->name; 917} 918 919 920/** 921 * Do per-display initializations. 922 */ 923int 924xmesa_init( Display *display ) 925{ 926 return xmesa_init_display(display) ? 0 : 1; 927} 928 929 930/** 931 * Create a new XMesaContext. 932 * \param v the XMesaVisual 933 * \param share_list another XMesaContext with which to share display 934 * lists or NULL if no sharing is wanted. 935 * \return an XMesaContext or NULL if error. 936 */ 937PUBLIC 938XMesaContext XMesaCreateContext( XMesaVisual v, XMesaContext share_list, 939 GLuint major, GLuint minor, 940 GLuint profileMask, GLuint contextFlags) 941{ 942 XMesaDisplay xmdpy = xmesa_init_display(v->display); 943 struct st_context_attribs attribs; 944 enum st_context_error ctx_err = 0; 945 XMesaContext c; 946 947 if (!xmdpy) 948 goto no_xmesa_context; 949 950 /* Note: the XMesaContext contains a Mesa struct gl_context struct (inheritance) */ 951 c = (XMesaContext) CALLOC_STRUCT(xmesa_context); 952 if (!c) 953 goto no_xmesa_context; 954 955 c->xm_visual = v; 956 c->xm_buffer = NULL; /* set later by XMesaMakeCurrent */ 957 c->xm_read_buffer = NULL; 958 959 memset(&attribs, 0, sizeof(attribs)); 960 attribs.visual = v->stvis; 961 attribs.major = major; 962 attribs.minor = minor; 963 if (contextFlags & GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB) 964 attribs.flags |= ST_CONTEXT_FLAG_FORWARD_COMPATIBLE; 965 if (contextFlags & GLX_CONTEXT_DEBUG_BIT_ARB) 966 attribs.flags |= ST_CONTEXT_FLAG_DEBUG; 967 if (contextFlags & GLX_CONTEXT_ROBUST_ACCESS_BIT_ARB) 968 attribs.flags |= ST_CONTEXT_FLAG_ROBUST_ACCESS; 969 970 switch (profileMask) { 971 case GLX_CONTEXT_CORE_PROFILE_BIT_ARB: 972 /* There are no profiles before OpenGL 3.2. The 973 * GLX_ARB_create_context_profile spec says: 974 * 975 * "If the requested OpenGL version is less than 3.2, 976 * GLX_CONTEXT_PROFILE_MASK_ARB is ignored and the functionality 977 * of the context is determined solely by the requested version." 978 */ 979 if (major > 3 || (major == 3 && minor >= 2)) { 980 attribs.profile = ST_PROFILE_OPENGL_CORE; 981 break; 982 } 983 FALLTHROUGH; 984 case GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB: 985 /* 986 * The spec also says: 987 * 988 * "If version 3.1 is requested, the context returned may implement 989 * any of the following versions: 990 * 991 * * Version 3.1. The GL_ARB_compatibility extension may or may not 992 * be implemented, as determined by the implementation. 993 * * The core profile of version 3.2 or greater." 994 * 995 * and because Mesa doesn't support GL_ARB_compatibility, the only chance to 996 * honour a 3.1 context is through core profile. 997 */ 998 if (major == 3 && minor == 1) { 999 attribs.profile = ST_PROFILE_OPENGL_CORE; 1000 } else { 1001 attribs.profile = ST_PROFILE_DEFAULT; 1002 } 1003 break; 1004 case GLX_CONTEXT_ES_PROFILE_BIT_EXT: 1005 if (major >= 2) { 1006 attribs.profile = ST_PROFILE_OPENGL_ES2; 1007 } else { 1008 attribs.profile = ST_PROFILE_OPENGL_ES1; 1009 } 1010 break; 1011 default: 1012 assert(0); 1013 goto no_st; 1014 } 1015 1016 c->st = stapi->create_context(stapi, xmdpy->smapi, &attribs, 1017 &ctx_err, (share_list) ? share_list->st : NULL); 1018 if (c->st == NULL) 1019 goto no_st; 1020 1021 c->st->st_manager_private = (void *) c; 1022 1023 c->hud = hud_create(c->st->cso_context, c->st, NULL); 1024 1025 return c; 1026 1027no_st: 1028 free(c); 1029no_xmesa_context: 1030 return NULL; 1031} 1032 1033 1034 1035PUBLIC 1036void XMesaDestroyContext( XMesaContext c ) 1037{ 1038 if (c->hud) { 1039 hud_destroy(c->hud, NULL); 1040 } 1041 1042 c->st->destroy(c->st); 1043 1044 /* FIXME: We should destroy the screen here, but if we do so, surfaces may 1045 * outlive it, causing segfaults 1046 struct pipe_screen *screen = c->st->pipe->screen; 1047 screen->destroy(screen); 1048 */ 1049 1050 free(c); 1051} 1052 1053 1054 1055/** 1056 * Private function for creating an XMesaBuffer which corresponds to an 1057 * X window or pixmap. 1058 * \param v the window's XMesaVisual 1059 * \param w the window we're wrapping 1060 * \return new XMesaBuffer or NULL if error 1061 */ 1062PUBLIC XMesaBuffer 1063XMesaCreateWindowBuffer(XMesaVisual v, Window w) 1064{ 1065 XWindowAttributes attr; 1066 XMesaBuffer b; 1067 Colormap cmap; 1068 int depth; 1069 1070 assert(v); 1071 assert(w); 1072 1073 /* Check that window depth matches visual depth */ 1074 XGetWindowAttributes( v->display, w, &attr ); 1075 depth = attr.depth; 1076 if (v->visinfo->depth != depth) { 1077 _mesa_warning(NULL, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n", 1078 v->visinfo->depth, depth); 1079 return NULL; 1080 } 1081 1082 /* Find colormap */ 1083 if (attr.colormap) { 1084 cmap = attr.colormap; 1085 } 1086 else { 1087 _mesa_warning(NULL, "Window %u has no colormap!\n", (unsigned int) w); 1088 /* this is weird, a window w/out a colormap!? */ 1089 /* OK, let's just allocate a new one and hope for the best */ 1090 cmap = XCreateColormap(v->display, w, attr.visual, AllocNone); 1091 } 1092 1093 b = create_xmesa_buffer((Drawable) w, WINDOW, v, cmap); 1094 if (!b) 1095 return NULL; 1096 1097 if (!initialize_visual_and_buffer( v, b, (Drawable) w, cmap )) { 1098 xmesa_free_buffer(b); 1099 return NULL; 1100 } 1101 1102 return b; 1103} 1104 1105 1106 1107/** 1108 * Create a new XMesaBuffer from an X pixmap. 1109 * 1110 * \param v the XMesaVisual 1111 * \param p the pixmap 1112 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or 1113 * \c GLX_DIRECT_COLOR visual for the pixmap 1114 * \returns new XMesaBuffer or NULL if error 1115 */ 1116PUBLIC XMesaBuffer 1117XMesaCreatePixmapBuffer(XMesaVisual v, Pixmap p, Colormap cmap) 1118{ 1119 XMesaBuffer b; 1120 1121 assert(v); 1122 1123 b = create_xmesa_buffer((Drawable) p, PIXMAP, v, cmap); 1124 if (!b) 1125 return NULL; 1126 1127 if (!initialize_visual_and_buffer(v, b, (Drawable) p, cmap)) { 1128 xmesa_free_buffer(b); 1129 return NULL; 1130 } 1131 1132 return b; 1133} 1134 1135 1136/** 1137 * For GLX_EXT_texture_from_pixmap 1138 */ 1139XMesaBuffer 1140XMesaCreatePixmapTextureBuffer(XMesaVisual v, Pixmap p, 1141 Colormap cmap, 1142 int format, int target, int mipmap) 1143{ 1144 GET_CURRENT_CONTEXT(ctx); 1145 XMesaBuffer b; 1146 1147 assert(v); 1148 1149 b = create_xmesa_buffer((Drawable) p, PIXMAP, v, cmap); 1150 if (!b) 1151 return NULL; 1152 1153 /* get pixmap size */ 1154 xmesa_get_window_size(v->display, b, &b->width, &b->height); 1155 1156 if (target == 0) { 1157 /* examine dims */ 1158 if (ctx->Extensions.ARB_texture_non_power_of_two) { 1159 target = GLX_TEXTURE_2D_EXT; 1160 } 1161 else if ( util_bitcount(b->width) == 1 1162 && util_bitcount(b->height) == 1) { 1163 /* power of two size */ 1164 if (b->height == 1) { 1165 target = GLX_TEXTURE_1D_EXT; 1166 } 1167 else { 1168 target = GLX_TEXTURE_2D_EXT; 1169 } 1170 } 1171 else if (ctx->Extensions.NV_texture_rectangle) { 1172 target = GLX_TEXTURE_RECTANGLE_EXT; 1173 } 1174 else { 1175 /* non power of two textures not supported */ 1176 XMesaDestroyBuffer(b); 1177 return 0; 1178 } 1179 } 1180 1181 b->TextureTarget = target; 1182 b->TextureFormat = format; 1183 b->TextureMipmap = mipmap; 1184 1185 if (!initialize_visual_and_buffer(v, b, (Drawable) p, cmap)) { 1186 xmesa_free_buffer(b); 1187 return NULL; 1188 } 1189 1190 return b; 1191} 1192 1193 1194 1195XMesaBuffer 1196XMesaCreatePBuffer(XMesaVisual v, Colormap cmap, 1197 unsigned int width, unsigned int height) 1198{ 1199 Window root; 1200 Drawable drawable; /* X Pixmap Drawable */ 1201 XMesaBuffer b; 1202 1203 /* allocate pixmap for front buffer */ 1204 root = RootWindow( v->display, v->visinfo->screen ); 1205 drawable = XCreatePixmap(v->display, root, width, height, 1206 v->visinfo->depth); 1207 if (!drawable) 1208 return NULL; 1209 1210 b = create_xmesa_buffer(drawable, PBUFFER, v, cmap); 1211 if (!b) 1212 return NULL; 1213 1214 if (!initialize_visual_and_buffer(v, b, drawable, cmap)) { 1215 xmesa_free_buffer(b); 1216 return NULL; 1217 } 1218 1219 return b; 1220} 1221 1222 1223 1224/* 1225 * Deallocate an XMesaBuffer structure and all related info. 1226 */ 1227PUBLIC void 1228XMesaDestroyBuffer(XMesaBuffer b) 1229{ 1230 xmesa_free_buffer(b); 1231} 1232 1233 1234/** 1235 * Notify the binding context to validate the buffer. 1236 */ 1237void 1238xmesa_notify_invalid_buffer(XMesaBuffer b) 1239{ 1240 p_atomic_inc(&b->stfb->stamp); 1241} 1242 1243 1244/** 1245 * Query the current drawable size and notify the binding context. 1246 */ 1247void 1248xmesa_check_buffer_size(XMesaBuffer b) 1249{ 1250 GLuint old_width, old_height; 1251 1252 if (!b) 1253 return; 1254 1255 if (b->type == PBUFFER) 1256 return; 1257 1258 old_width = b->width; 1259 old_height = b->height; 1260 1261 xmesa_get_window_size(b->xm_visual->display, b, &b->width, &b->height); 1262 1263 if (b->width != old_width || b->height != old_height) 1264 xmesa_notify_invalid_buffer(b); 1265} 1266 1267 1268/* 1269 * Bind buffer b to context c and make c the current rendering context. 1270 */ 1271PUBLIC 1272GLboolean XMesaMakeCurrent2( XMesaContext c, XMesaBuffer drawBuffer, 1273 XMesaBuffer readBuffer ) 1274{ 1275 XMesaContext old_ctx = XMesaGetCurrentContext(); 1276 1277 if (old_ctx && old_ctx != c) { 1278 XMesaFlush(old_ctx); 1279 old_ctx->xm_buffer = NULL; 1280 old_ctx->xm_read_buffer = NULL; 1281 } 1282 1283 if (c) { 1284 if (!drawBuffer != !readBuffer) { 1285 return GL_FALSE; /* must specify zero or two buffers! */ 1286 } 1287 1288 if (c == old_ctx && 1289 c->xm_buffer == drawBuffer && 1290 c->xm_read_buffer == readBuffer) 1291 return GL_TRUE; 1292 1293 xmesa_check_buffer_size(drawBuffer); 1294 if (readBuffer != drawBuffer) 1295 xmesa_check_buffer_size(readBuffer); 1296 1297 c->xm_buffer = drawBuffer; 1298 c->xm_read_buffer = readBuffer; 1299 1300 stapi->make_current(stapi, c->st, 1301 drawBuffer ? drawBuffer->stfb : NULL, 1302 readBuffer ? readBuffer->stfb : NULL); 1303 1304 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */ 1305 if (drawBuffer) 1306 drawBuffer->wasCurrent = GL_TRUE; 1307 } 1308 else { 1309 /* Detach */ 1310 stapi->make_current(stapi, NULL, NULL, NULL); 1311 1312 } 1313 return GL_TRUE; 1314} 1315 1316 1317/* 1318 * Unbind the context c from its buffer. 1319 */ 1320GLboolean XMesaUnbindContext( XMesaContext c ) 1321{ 1322 /* A no-op for XFree86 integration purposes */ 1323 return GL_TRUE; 1324} 1325 1326 1327XMesaContext XMesaGetCurrentContext( void ) 1328{ 1329 struct st_context_iface *st = stapi->get_current(stapi); 1330 return (XMesaContext) (st) ? st->st_manager_private : NULL; 1331} 1332 1333 1334 1335/** 1336 * Swap front and back color buffers and have winsys display front buffer. 1337 * If there's no front color buffer no swap actually occurs. 1338 */ 1339PUBLIC 1340void XMesaSwapBuffers( XMesaBuffer b ) 1341{ 1342 XMesaContext xmctx = XMesaGetCurrentContext(); 1343 1344 /* Need to draw HUD before flushing */ 1345 if (xmctx && xmctx->hud) { 1346 struct pipe_resource *back = 1347 xmesa_get_framebuffer_resource(b->stfb, ST_ATTACHMENT_BACK_LEFT); 1348 hud_run(xmctx->hud, NULL, back); 1349 } 1350 1351 if (xmctx && xmctx->xm_buffer == b) { 1352 xmctx->st->flush( xmctx->st, ST_FLUSH_FRONT, NULL, NULL, NULL); 1353 } 1354 1355 xmesa_swap_st_framebuffer(b->stfb); 1356} 1357 1358 1359 1360/* 1361 * Copy sub-region of back buffer to front buffer 1362 */ 1363void XMesaCopySubBuffer( XMesaBuffer b, int x, int y, int width, int height ) 1364{ 1365 XMesaContext xmctx = XMesaGetCurrentContext(); 1366 1367 xmctx->st->flush( xmctx->st, ST_FLUSH_FRONT, NULL, NULL, NULL); 1368 1369 xmesa_copy_st_framebuffer(b->stfb, 1370 ST_ATTACHMENT_BACK_LEFT, ST_ATTACHMENT_FRONT_LEFT, 1371 x, b->height - y - height, width, height); 1372} 1373 1374 1375 1376void XMesaFlush( XMesaContext c ) 1377{ 1378 if (c && c->xm_visual->display) { 1379 XMesaDisplay xmdpy = xmesa_init_display(c->xm_visual->display); 1380 struct pipe_fence_handle *fence = NULL; 1381 1382 c->st->flush(c->st, ST_FLUSH_FRONT, &fence, NULL, NULL); 1383 if (fence) { 1384 xmdpy->screen->fence_finish(xmdpy->screen, NULL, fence, 1385 PIPE_TIMEOUT_INFINITE); 1386 xmdpy->screen->fence_reference(xmdpy->screen, &fence, NULL); 1387 } 1388 XFlush( c->xm_visual->display ); 1389 } 1390} 1391 1392 1393 1394 1395 1396XMesaBuffer XMesaFindBuffer( Display *dpy, Drawable d ) 1397{ 1398 XMesaBuffer b; 1399 for (b = XMesaBufferList; b; b = b->Next) { 1400 if (b->ws.drawable == d && b->xm_visual->display == dpy) { 1401 return b; 1402 } 1403 } 1404 return NULL; 1405} 1406 1407 1408/** 1409 * Free/destroy all XMesaBuffers associated with given display. 1410 */ 1411void xmesa_destroy_buffers_on_display(Display *dpy) 1412{ 1413 XMesaBuffer b, next; 1414 for (b = XMesaBufferList; b; b = next) { 1415 next = b->Next; 1416 if (b->xm_visual->display == dpy) { 1417 xmesa_free_buffer(b); 1418 /* delete head of list? */ 1419 if (XMesaBufferList == b) { 1420 XMesaBufferList = next; 1421 } 1422 } 1423 } 1424} 1425 1426 1427/* 1428 * Look for XMesaBuffers whose X window has been destroyed. 1429 * Deallocate any such XMesaBuffers. 1430 */ 1431void XMesaGarbageCollect( void ) 1432{ 1433 XMesaBuffer b, next; 1434 for (b=XMesaBufferList; b; b=next) { 1435 next = b->Next; 1436 if (b->xm_visual && 1437 b->xm_visual->display && 1438 b->ws.drawable && 1439 b->type == WINDOW) { 1440 XSync(b->xm_visual->display, False); 1441 if (!window_exists( b->xm_visual->display, b->ws.drawable )) { 1442 /* found a dead window, free the ancillary info */ 1443 XMesaDestroyBuffer( b ); 1444 } 1445 } 1446 } 1447} 1448 1449 1450static enum st_attachment_type xmesa_attachment_type(int glx_attachment) 1451{ 1452 switch(glx_attachment) { 1453 case GLX_FRONT_LEFT_EXT: 1454 return ST_ATTACHMENT_FRONT_LEFT; 1455 case GLX_FRONT_RIGHT_EXT: 1456 return ST_ATTACHMENT_FRONT_RIGHT; 1457 case GLX_BACK_LEFT_EXT: 1458 return ST_ATTACHMENT_BACK_LEFT; 1459 case GLX_BACK_RIGHT_EXT: 1460 return ST_ATTACHMENT_BACK_RIGHT; 1461 default: 1462 assert(0); 1463 return ST_ATTACHMENT_FRONT_LEFT; 1464 } 1465} 1466 1467 1468PUBLIC void 1469XMesaBindTexImage(Display *dpy, XMesaBuffer drawable, int buffer, 1470 const int *attrib_list) 1471{ 1472 struct st_context_iface *st = stapi->get_current(stapi); 1473 struct st_framebuffer_iface* stfbi = drawable->stfb; 1474 struct pipe_resource *res; 1475 int x, y, w, h; 1476 enum st_attachment_type st_attachment = xmesa_attachment_type(buffer); 1477 1478 x = 0; 1479 y = 0; 1480 w = drawable->width; 1481 h = drawable->height; 1482 1483 /* We need to validate our attachments before using them, 1484 * in case the texture doesn't exist yet. */ 1485 xmesa_st_framebuffer_validate_textures(stfbi, w, h, 1 << st_attachment); 1486 res = xmesa_get_attachment(stfbi, st_attachment); 1487 1488 if (res) { 1489 struct pipe_context* pipe = xmesa_get_context(stfbi); 1490 enum pipe_format internal_format = res->format; 1491 struct pipe_transfer *tex_xfer; 1492 char *map; 1493 int line, byte_width; 1494 XImage *img; 1495 1496 internal_format = choose_pixel_format(drawable->xm_visual); 1497 1498 map = pipe_texture_map(pipe, res, 1499 0, 0, /* level, layer */ 1500 PIPE_MAP_WRITE, 1501 x, y, 1502 w, h, &tex_xfer); 1503 if (!map) 1504 return; 1505 1506 /* Grab the XImage that we want to turn into a texture. */ 1507 img = XGetImage(dpy, 1508 drawable->ws.drawable, 1509 x, y, 1510 w, h, 1511 AllPlanes, 1512 ZPixmap); 1513 1514 if (!img) { 1515 pipe_texture_unmap(pipe, tex_xfer); 1516 return; 1517 } 1518 1519 /* The pipe transfer has a pitch rounded up to the nearest 64 pixels. */ 1520 byte_width = w * ((img->bits_per_pixel + 7) / 8); 1521 1522 for (line = 0; line < h; line++) 1523 memcpy(&map[line * tex_xfer->stride], 1524 &img->data[line * img->bytes_per_line], 1525 byte_width); 1526 1527 pipe_texture_unmap(pipe, tex_xfer); 1528 1529 st->teximage(st, 1530 ST_TEXTURE_2D, 1531 0, /* level */ 1532 internal_format, 1533 res, 1534 FALSE /* no mipmap */); 1535 1536 } 1537} 1538 1539 1540 1541PUBLIC void 1542XMesaReleaseTexImage(Display *dpy, XMesaBuffer drawable, int buffer) 1543{ 1544} 1545 1546 1547void 1548XMesaCopyContext(XMesaContext src, XMesaContext dst, unsigned long mask) 1549{ 1550 if (dst->st->copy) 1551 dst->st->copy(dst->st, src->st, mask); 1552} 1553