xm_dd.c revision 1ccef926be46dce3b6b5c76e812e2fae4e205ce7
1/* 2 * Mesa 3-D graphics library 3 * Version: 6.5.2 4 * 5 * Copyright (C) 1999-2006 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/** 27 * \file xm_dd.h 28 * General device driver functions for Xlib driver. 29 */ 30 31#include "glxheader.h" 32#include "main/bufferobj.h" 33#include "main/buffers.h" 34#include "main/context.h" 35#include "main/colormac.h" 36#include "main/depth.h" 37#include "main/drawpix.h" 38#include "main/extensions.h" 39#include "main/framebuffer.h" 40#include "main/macros.h" 41#include "main/image.h" 42#include "main/imports.h" 43#include "main/mtypes.h" 44#include "main/state.h" 45#include "main/texobj.h" 46#include "main/teximage.h" 47#include "main/texstore.h" 48#include "main/texformat.h" 49#include "swrast/swrast.h" 50#include "swrast/s_context.h" 51#include "swrast_setup/swrast_setup.h" 52#include "tnl/tnl.h" 53#include "tnl/t_context.h" 54#include "drivers/common/meta.h" 55#include "xmesaP.h" 56 57 58 59/* 60 * Dithering kernels and lookup tables. 61 */ 62 63const int xmesa_kernel8[DITH_DY * DITH_DX] = { 64 0 * MAXC, 8 * MAXC, 2 * MAXC, 10 * MAXC, 65 12 * MAXC, 4 * MAXC, 14 * MAXC, 6 * MAXC, 66 3 * MAXC, 11 * MAXC, 1 * MAXC, 9 * MAXC, 67 15 * MAXC, 7 * MAXC, 13 * MAXC, 5 * MAXC, 68}; 69 70const short xmesa_HPCR_DRGB[3][2][16] = { 71 { 72 { 16, -4, 1,-11, 14, -6, 3, -9, 15, -5, 2,-10, 13, -7, 4, -8}, 73 {-15, 5, 0, 12,-13, 7, -2, 10,-14, 6, -1, 11,-12, 8, -3, 9} 74 }, 75 { 76 {-11, 15, -7, 3, -8, 14, -4, 2,-10, 16, -6, 4, -9, 13, -5, 1}, 77 { 12,-14, 8, -2, 9,-13, 5, -1, 11,-15, 7, -3, 10,-12, 6, 0} 78 }, 79 { 80 { 6,-18, 26,-14, 2,-22, 30,-10, 8,-16, 28,-12, 4,-20, 32, -8}, 81 { -4, 20,-24, 16, 0, 24,-28, 12, -6, 18,-26, 14, -2, 22,-30, 10} 82 } 83}; 84 85const int xmesa_kernel1[16] = { 86 0*47, 9*47, 4*47, 12*47, /* 47 = (255*3)/16 */ 87 6*47, 2*47, 14*47, 8*47, 88 10*47, 1*47, 5*47, 11*47, 89 7*47, 13*47, 3*47, 15*47 90}; 91 92 93static void 94finish_or_flush( struct gl_context *ctx ) 95{ 96 const XMesaContext xmesa = XMESA_CONTEXT(ctx); 97 if (xmesa) { 98 _glthread_LOCK_MUTEX(_xmesa_lock); 99 XSync( xmesa->display, False ); 100 _glthread_UNLOCK_MUTEX(_xmesa_lock); 101 } 102} 103 104 105static void 106clear_color( struct gl_context *ctx, const GLfloat color[4] ) 107{ 108 if (ctx->DrawBuffer->Name == 0) { 109 const XMesaContext xmesa = XMESA_CONTEXT(ctx); 110 XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer); 111 112 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[0], color[0]); 113 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[1], color[1]); 114 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[2], color[2]); 115 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[3], color[3]); 116 xmesa->clearpixel = xmesa_color_to_pixel( ctx, 117 xmesa->clearcolor[0], 118 xmesa->clearcolor[1], 119 xmesa->clearcolor[2], 120 xmesa->clearcolor[3], 121 xmesa->xm_visual->undithered_pf ); 122 _glthread_LOCK_MUTEX(_xmesa_lock); 123 XMesaSetForeground( xmesa->display, xmbuf->cleargc, 124 xmesa->clearpixel ); 125 _glthread_UNLOCK_MUTEX(_xmesa_lock); 126 } 127} 128 129 130 131/* Implements glColorMask() */ 132static void 133color_mask(struct gl_context *ctx, 134 GLboolean rmask, GLboolean gmask, GLboolean bmask, GLboolean amask) 135{ 136 const XMesaContext xmesa = XMESA_CONTEXT(ctx); 137 XMesaBuffer xmbuf; 138 const int xclass = xmesa->xm_visual->visualType; 139 (void) amask; 140 141 if (ctx->DrawBuffer->Name != 0) 142 return; 143 144 xmbuf = XMESA_BUFFER(ctx->DrawBuffer); 145 146 if (xclass == GLX_TRUE_COLOR || xclass == GLX_DIRECT_COLOR) { 147 unsigned long m; 148 if (rmask && gmask && bmask) { 149 m = ((unsigned long)~0L); 150 } 151 else { 152 m = 0; 153 if (rmask) m |= GET_REDMASK(xmesa->xm_visual); 154 if (gmask) m |= GET_GREENMASK(xmesa->xm_visual); 155 if (bmask) m |= GET_BLUEMASK(xmesa->xm_visual); 156 } 157 XMesaSetPlaneMask( xmesa->display, xmbuf->cleargc, m ); 158 } 159} 160 161 162 163/**********************************************************************/ 164/*** glClear implementations ***/ 165/**********************************************************************/ 166 167 168/** 169 * Clear the front or back color buffer, if it's implemented with a pixmap. 170 */ 171static void 172clear_pixmap(struct gl_context *ctx, struct xmesa_renderbuffer *xrb, 173 GLint x, GLint y, GLint width, GLint height) 174{ 175 const XMesaContext xmesa = XMESA_CONTEXT(ctx); 176 XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer); 177 178 assert(xmbuf); 179 assert(xrb->pixmap); 180 assert(xmesa); 181 assert(xmesa->display); 182 assert(xrb->pixmap); 183 assert(xmbuf->cleargc); 184 185 XMesaFillRectangle( xmesa->display, xrb->pixmap, xmbuf->cleargc, 186 x, xrb->Base.Height - y - height, 187 width, height ); 188} 189 190 191static void 192clear_8bit_ximage( struct gl_context *ctx, struct xmesa_renderbuffer *xrb, 193 GLint x, GLint y, GLint width, GLint height ) 194{ 195 const XMesaContext xmesa = XMESA_CONTEXT(ctx); 196 GLint i; 197 for (i = 0; i < height; i++) { 198 GLubyte *ptr = PIXEL_ADDR1(xrb, x, y + i); 199 memset( ptr, xmesa->clearpixel, width ); 200 } 201} 202 203 204static void 205clear_HPCR_ximage( struct gl_context *ctx, struct xmesa_renderbuffer *xrb, 206 GLint x, GLint y, GLint width, GLint height ) 207{ 208 const XMesaContext xmesa = XMESA_CONTEXT(ctx); 209 GLint i; 210 for (i = y; i < y + height; i++) { 211 GLubyte *ptr = PIXEL_ADDR1( xrb, x, i ); 212 int j; 213 const GLubyte *sptr = xmesa->xm_visual->hpcr_clear_ximage_pattern[0]; 214 if (i & 1) { 215 sptr += 16; 216 } 217 for (j = x; j < x + width; j++) { 218 *ptr = sptr[j&15]; 219 ptr++; 220 } 221 } 222} 223 224 225static void 226clear_16bit_ximage( struct gl_context *ctx, struct xmesa_renderbuffer *xrb, 227 GLint x, GLint y, GLint width, GLint height) 228{ 229 const XMesaContext xmesa = XMESA_CONTEXT(ctx); 230 GLuint pixel = (GLuint) xmesa->clearpixel; 231 GLint i, j; 232 233 if (xmesa->swapbytes) { 234 pixel = ((pixel >> 8) & 0x00ff) | ((pixel << 8) & 0xff00); 235 } 236 237 for (j = 0; j < height; j++) { 238 GLushort *ptr2 = PIXEL_ADDR2(xrb, x, y + j); 239 for (i = 0; i < width; i++) { 240 ptr2[i] = pixel; 241 } 242 } 243} 244 245 246/* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */ 247static void 248clear_24bit_ximage(struct gl_context *ctx, struct xmesa_renderbuffer *xrb, 249 GLint x, GLint y, GLint width, GLint height) 250{ 251 const XMesaContext xmesa = XMESA_CONTEXT(ctx); 252 const GLubyte r = xmesa->clearcolor[0]; 253 const GLubyte g = xmesa->clearcolor[1]; 254 const GLubyte b = xmesa->clearcolor[2]; 255 256 if (r == g && g == b) { 257 /* same value for all three components (gray) */ 258 GLint j; 259 for (j = 0; j < height; j++) { 260 bgr_t *ptr3 = PIXEL_ADDR3(xrb, x, y + j); 261 memset(ptr3, r, 3 * width); 262 } 263 } 264 else { 265 /* non-gray clear color */ 266 GLint i, j; 267 for (j = 0; j < height; j++) { 268 bgr_t *ptr3 = PIXEL_ADDR3(xrb, x, y + j); 269 for (i = 0; i < width; i++) { 270 ptr3->r = r; 271 ptr3->g = g; 272 ptr3->b = b; 273 ptr3++; 274 } 275 } 276 } 277} 278 279 280static void 281clear_32bit_ximage(struct gl_context *ctx, struct xmesa_renderbuffer *xrb, 282 GLint x, GLint y, GLint width, GLint height) 283{ 284 const XMesaContext xmesa = XMESA_CONTEXT(ctx); 285 register GLuint pixel = (GLuint) xmesa->clearpixel; 286 287 if (!xrb->ximage) 288 return; 289 290 if (xmesa->swapbytes) { 291 pixel = ((pixel >> 24) & 0x000000ff) 292 | ((pixel >> 8) & 0x0000ff00) 293 | ((pixel << 8) & 0x00ff0000) 294 | ((pixel << 24) & 0xff000000); 295 } 296 297 if (width == xrb->Base.Width && height == xrb->Base.Height) { 298 /* clearing whole buffer */ 299 const GLuint n = xrb->Base.Width * xrb->Base.Height; 300 GLuint *ptr4 = (GLuint *) xrb->ximage->data; 301 if (pixel == 0) { 302 /* common case */ 303 memset(ptr4, pixel, 4 * n); 304 } 305 else { 306 GLuint i; 307 for (i = 0; i < n; i++) 308 ptr4[i] = pixel; 309 } 310 } 311 else { 312 /* clearing scissored region */ 313 GLint i, j; 314 for (j = 0; j < height; j++) { 315 GLuint *ptr4 = PIXEL_ADDR4(xrb, x, y + j); 316 for (i = 0; i < width; i++) { 317 ptr4[i] = pixel; 318 } 319 } 320 } 321} 322 323 324static void 325clear_nbit_ximage(struct gl_context *ctx, struct xmesa_renderbuffer *xrb, 326 GLint x, GLint y, GLint width, GLint height) 327{ 328 const XMesaContext xmesa = XMESA_CONTEXT(ctx); 329 XMesaImage *img = xrb->ximage; 330 GLint i, j; 331 332 /* TODO: optimize this */ 333 y = YFLIP(xrb, y); 334 for (j = 0; j < height; j++) { 335 for (i = 0; i < width; i++) { 336 XMesaPutPixel(img, x+i, y-j, xmesa->clearpixel); 337 } 338 } 339} 340 341 342 343static void 344clear_buffers(struct gl_context *ctx, GLbitfield buffers) 345{ 346 if (ctx->DrawBuffer->Name == 0) { 347 /* this is a window system framebuffer */ 348 const GLuint *colorMask = (GLuint *) &ctx->Color.ColorMask[0]; 349 XMesaBuffer b = XMESA_BUFFER(ctx->DrawBuffer); 350 const GLint x = ctx->DrawBuffer->_Xmin; 351 const GLint y = ctx->DrawBuffer->_Ymin; 352 const GLint width = ctx->DrawBuffer->_Xmax - x; 353 const GLint height = ctx->DrawBuffer->_Ymax - y; 354 355 /* we can't handle color or index masking */ 356 if (*colorMask == 0xffffffff && ctx->Color.IndexMask == 0xffffffff) { 357 if (buffers & BUFFER_BIT_FRONT_LEFT) { 358 /* clear front color buffer */ 359 struct gl_renderbuffer *frontRb 360 = ctx->DrawBuffer->Attachment[BUFFER_FRONT_LEFT].Renderbuffer; 361 if (b->frontxrb == xmesa_renderbuffer(frontRb)) { 362 /* renderbuffer is not wrapped - great! */ 363 b->frontxrb->clearFunc(ctx, b->frontxrb, x, y, width, height); 364 buffers &= ~BUFFER_BIT_FRONT_LEFT; 365 } 366 else { 367 /* we can't directly clear an alpha-wrapped color buffer */ 368 } 369 } 370 if (buffers & BUFFER_BIT_BACK_LEFT) { 371 /* clear back color buffer */ 372 struct gl_renderbuffer *backRb 373 = ctx->DrawBuffer->Attachment[BUFFER_BACK_LEFT].Renderbuffer; 374 if (b->backxrb == xmesa_renderbuffer(backRb)) { 375 /* renderbuffer is not wrapped - great! */ 376 b->backxrb->clearFunc(ctx, b->backxrb, x, y, width, height); 377 buffers &= ~BUFFER_BIT_BACK_LEFT; 378 } 379 } 380 } 381 } 382 if (buffers) 383 _swrast_Clear(ctx, buffers); 384} 385 386 387/* XXX these functions haven't been tested in the Xserver environment */ 388 389 390/** 391 * Check if we can do an optimized glDrawPixels into an 8R8G8B visual. 392 */ 393static GLboolean 394can_do_DrawPixels_8R8G8B(struct gl_context *ctx, GLenum format, GLenum type) 395{ 396 if (format == GL_BGRA && 397 type == GL_UNSIGNED_BYTE && 398 ctx->DrawBuffer && 399 ctx->DrawBuffer->Name == 0 && 400 ctx->Pixel.ZoomX == 1.0 && /* no zooming */ 401 ctx->Pixel.ZoomY == 1.0 && 402 ctx->_ImageTransferState == 0 /* no color tables, scale/bias, etc */) { 403 const SWcontext *swrast = SWRAST_CONTEXT(ctx); 404 405 if (swrast->NewState) 406 _swrast_validate_derived( ctx ); 407 408 if ((swrast->_RasterMask & ~CLIP_BIT) == 0) /* no blend, z-test, etc */ { 409 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0]; 410 if (rb) { 411 struct xmesa_renderbuffer *xrb = xmesa_renderbuffer(rb->Wrapped); 412 if (xrb && 413 xrb->pixmap && /* drawing to pixmap or window */ 414 _mesa_get_format_bits(xrb->Base.Format, GL_ALPHA_BITS) == 0) { 415 return GL_TRUE; 416 } 417 } 418 } 419 } 420 return GL_FALSE; 421} 422 423 424/** 425 * This function implements glDrawPixels() with an XPutImage call when 426 * drawing to the front buffer (X Window drawable). 427 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format. 428 */ 429static void 430xmesa_DrawPixels_8R8G8B( struct gl_context *ctx, 431 GLint x, GLint y, GLsizei width, GLsizei height, 432 GLenum format, GLenum type, 433 const struct gl_pixelstore_attrib *unpack, 434 const GLvoid *pixels ) 435{ 436 if (can_do_DrawPixels_8R8G8B(ctx, format, type)) { 437 const SWcontext *swrast = SWRAST_CONTEXT( ctx ); 438 struct gl_pixelstore_attrib clippedUnpack = *unpack; 439 int dstX = x; 440 int dstY = y; 441 int w = width; 442 int h = height; 443 444 if (swrast->NewState) 445 _swrast_validate_derived( ctx ); 446 447 if (unpack->BufferObj->Name) { 448 /* unpack from PBO */ 449 GLubyte *buf; 450 if (!_mesa_validate_pbo_access(2, unpack, width, height, 1, 451 format, type, pixels)) { 452 _mesa_error(ctx, GL_INVALID_OPERATION, 453 "glDrawPixels(invalid PBO access)"); 454 return; 455 } 456 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, 457 GL_PIXEL_UNPACK_BUFFER_EXT, 458 GL_READ_ONLY_ARB, 459 unpack->BufferObj); 460 if (!buf) { 461 /* buffer is already mapped - that's an error */ 462 _mesa_error(ctx, GL_INVALID_OPERATION, 463 "glDrawPixels(PBO is mapped)"); 464 return; 465 } 466 pixels = ADD_POINTERS(buf, pixels); 467 } 468 469 if (_mesa_clip_drawpixels(ctx, &dstX, &dstY, &w, &h, &clippedUnpack)) { 470 const XMesaContext xmesa = XMESA_CONTEXT(ctx); 471 XMesaDisplay *dpy = xmesa->xm_visual->display; 472 XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer); 473 const XMesaGC gc = xmbuf->cleargc; /* effected by glColorMask */ 474 struct xmesa_renderbuffer *xrb 475 = xmesa_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]->Wrapped); 476 const int srcX = clippedUnpack.SkipPixels; 477 const int srcY = clippedUnpack.SkipRows; 478 const int rowLength = clippedUnpack.RowLength; 479 XMesaImage ximage; 480 481 ASSERT(xmesa->xm_visual->dithered_pf == PF_8R8G8B); 482 ASSERT(xmesa->xm_visual->undithered_pf == PF_8R8G8B); 483 ASSERT(dpy); 484 ASSERT(gc); 485 486 /* This is a little tricky since all coordinates up to now have 487 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom 488 * so we have to carefully compute the Y coordinates/addresses here. 489 */ 490 memset(&ximage, 0, sizeof(XMesaImage)); 491 ximage.width = width; 492 ximage.height = height; 493 ximage.format = ZPixmap; 494 ximage.data = (char *) pixels 495 + ((srcY + h - 1) * rowLength + srcX) * 4; 496 ximage.byte_order = LSBFirst; 497 ximage.bitmap_unit = 32; 498 ximage.bitmap_bit_order = LSBFirst; 499 ximage.bitmap_pad = 32; 500 ximage.depth = 32; 501 ximage.bits_per_pixel = 32; 502 ximage.bytes_per_line = -rowLength * 4; /* negative to flip image */ 503 /* it seems we don't need to set the ximage.red/green/blue_mask fields */ 504 /* flip Y axis for dest position */ 505 dstY = YFLIP(xrb, dstY) - h + 1; 506 XPutImage(dpy, xrb->pixmap, gc, &ximage, 0, 0, dstX, dstY, w, h); 507 } 508 509 if (unpack->BufferObj->Name) { 510 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT, 511 unpack->BufferObj); 512 } 513 } 514 else { 515 /* software fallback */ 516 _swrast_DrawPixels(ctx, x, y, width, height, 517 format, type, unpack, pixels); 518 } 519} 520 521 522 523/** 524 * Check if we can do an optimized glDrawPixels into an 5R6G5B visual. 525 */ 526static GLboolean 527can_do_DrawPixels_5R6G5B(struct gl_context *ctx, GLenum format, GLenum type) 528{ 529 if (format == GL_RGB && 530 type == GL_UNSIGNED_SHORT_5_6_5 && 531 !ctx->Color.DitherFlag && /* no dithering */ 532 ctx->DrawBuffer && 533 ctx->DrawBuffer->Name == 0 && 534 ctx->Pixel.ZoomX == 1.0 && /* no zooming */ 535 ctx->Pixel.ZoomY == 1.0 && 536 ctx->_ImageTransferState == 0 /* no color tables, scale/bias, etc */) { 537 const SWcontext *swrast = SWRAST_CONTEXT(ctx); 538 539 if (swrast->NewState) 540 _swrast_validate_derived( ctx ); 541 542 if ((swrast->_RasterMask & ~CLIP_BIT) == 0) /* no blend, z-test, etc */ { 543 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0]; 544 if (rb) { 545 struct xmesa_renderbuffer *xrb = xmesa_renderbuffer(rb->Wrapped); 546 if (xrb && 547 xrb->pixmap && /* drawing to pixmap or window */ 548 _mesa_get_format_bits(xrb->Base.Format, GL_ALPHA_BITS) == 0) { 549 return GL_TRUE; 550 } 551 } 552 } 553 } 554 return GL_FALSE; 555} 556 557 558/** 559 * This function implements glDrawPixels() with an XPutImage call when 560 * drawing to the front buffer (X Window drawable). The image format 561 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to 562 * match the PF_5R6G5B pixel format. 563 */ 564static void 565xmesa_DrawPixels_5R6G5B( struct gl_context *ctx, 566 GLint x, GLint y, GLsizei width, GLsizei height, 567 GLenum format, GLenum type, 568 const struct gl_pixelstore_attrib *unpack, 569 const GLvoid *pixels ) 570{ 571 if (can_do_DrawPixels_5R6G5B(ctx, format, type)) { 572 const SWcontext *swrast = SWRAST_CONTEXT( ctx ); 573 struct gl_pixelstore_attrib clippedUnpack = *unpack; 574 int dstX = x; 575 int dstY = y; 576 int w = width; 577 int h = height; 578 579 if (swrast->NewState) 580 _swrast_validate_derived( ctx ); 581 582 if (unpack->BufferObj->Name) { 583 /* unpack from PBO */ 584 GLubyte *buf; 585 if (!_mesa_validate_pbo_access(2, unpack, width, height, 1, 586 format, type, pixels)) { 587 _mesa_error(ctx, GL_INVALID_OPERATION, 588 "glDrawPixels(invalid PBO access)"); 589 return; 590 } 591 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, 592 GL_PIXEL_UNPACK_BUFFER_EXT, 593 GL_READ_ONLY_ARB, 594 unpack->BufferObj); 595 if (!buf) { 596 /* buffer is already mapped - that's an error */ 597 _mesa_error(ctx, GL_INVALID_OPERATION, 598 "glDrawPixels(PBO is mapped)"); 599 return; 600 } 601 pixels = ADD_POINTERS(buf, pixels); 602 } 603 604 if (_mesa_clip_drawpixels(ctx, &dstX, &dstY, &w, &h, &clippedUnpack)) { 605 const XMesaContext xmesa = XMESA_CONTEXT(ctx); 606 XMesaDisplay *dpy = xmesa->xm_visual->display; 607 XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer); 608 const XMesaGC gc = xmbuf->cleargc; /* effected by glColorMask */ 609 struct xmesa_renderbuffer *xrb 610 = xmesa_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]->Wrapped); 611 const int srcX = clippedUnpack.SkipPixels; 612 const int srcY = clippedUnpack.SkipRows; 613 const int rowLength = clippedUnpack.RowLength; 614 XMesaImage ximage; 615 616 ASSERT(xmesa->xm_visual->undithered_pf == PF_5R6G5B); 617 ASSERT(dpy); 618 ASSERT(gc); 619 620 /* This is a little tricky since all coordinates up to now have 621 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom 622 * so we have to carefully compute the Y coordinates/addresses here. 623 */ 624 memset(&ximage, 0, sizeof(XMesaImage)); 625 ximage.width = width; 626 ximage.height = height; 627 ximage.format = ZPixmap; 628 ximage.data = (char *) pixels 629 + ((srcY + h - 1) * rowLength + srcX) * 2; 630 ximage.byte_order = LSBFirst; 631 ximage.bitmap_unit = 16; 632 ximage.bitmap_bit_order = LSBFirst; 633 ximage.bitmap_pad = 16; 634 ximage.depth = 16; 635 ximage.bits_per_pixel = 16; 636 ximage.bytes_per_line = -rowLength * 2; /* negative to flip image */ 637 /* it seems we don't need to set the ximage.red/green/blue_mask fields */ 638 /* flip Y axis for dest position */ 639 dstY = YFLIP(xrb, dstY) - h + 1; 640 XPutImage(dpy, xrb->pixmap, gc, &ximage, 0, 0, dstX, dstY, w, h); 641 } 642 643 if (unpack->BufferObj->Name) { 644 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT, 645 unpack->BufferObj); 646 } 647 } 648 else { 649 /* software fallback */ 650 _swrast_DrawPixels(ctx, x, y, width, height, 651 format, type, unpack, pixels); 652 } 653} 654 655 656/** 657 * Determine if we can do an optimized glCopyPixels. 658 */ 659static GLboolean 660can_do_CopyPixels(struct gl_context *ctx, GLenum type) 661{ 662 if (type == GL_COLOR && 663 ctx->_ImageTransferState == 0 && /* no color tables, scale/bias, etc */ 664 ctx->Pixel.ZoomX == 1.0 && /* no zooming */ 665 ctx->Pixel.ZoomY == 1.0 && 666 ctx->Color.DrawBuffer[0] == GL_FRONT && /* copy to front buf */ 667 ctx->Pixel.ReadBuffer == GL_FRONT && /* copy from front buf */ 668 ctx->ReadBuffer->_ColorReadBuffer && 669 ctx->DrawBuffer->_ColorDrawBuffers[0]) { 670 const SWcontext *swrast = SWRAST_CONTEXT( ctx ); 671 672 if (swrast->NewState) 673 _swrast_validate_derived( ctx ); 674 675 if ((swrast->_RasterMask & ~CLIP_BIT) == 0x0 && 676 ctx->ReadBuffer && 677 ctx->ReadBuffer->_ColorReadBuffer && 678 ctx->DrawBuffer && 679 ctx->DrawBuffer->_ColorDrawBuffers[0]) { 680 struct xmesa_renderbuffer *srcXrb 681 = xmesa_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer->Wrapped); 682 struct xmesa_renderbuffer *dstXrb 683 = xmesa_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]->Wrapped); 684 if (srcXrb->pixmap && dstXrb->pixmap) { 685 return GL_TRUE; 686 } 687 } 688 } 689 return GL_FALSE; 690} 691 692 693/** 694 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap) 695 * for the color buffer. Don't support zooming, pixel transfer, etc. 696 * We do support copying from one window to another, ala glXMakeCurrentRead. 697 */ 698static void 699xmesa_CopyPixels( struct gl_context *ctx, 700 GLint srcx, GLint srcy, GLsizei width, GLsizei height, 701 GLint destx, GLint desty, GLenum type ) 702{ 703 if (can_do_CopyPixels(ctx, type)) { 704 const XMesaContext xmesa = XMESA_CONTEXT(ctx); 705 XMesaDisplay *dpy = xmesa->xm_visual->display; 706 XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer); 707 const XMesaGC gc = xmbuf->cleargc; /* effected by glColorMask */ 708 struct xmesa_renderbuffer *srcXrb 709 = xmesa_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer->Wrapped); 710 struct xmesa_renderbuffer *dstXrb 711 = xmesa_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]->Wrapped); 712 713 ASSERT(dpy); 714 ASSERT(gc); 715 716 /* Note: we don't do any special clipping work here. We could, 717 * but X will do it for us. 718 */ 719 srcy = YFLIP(srcXrb, srcy) - height + 1; 720 desty = YFLIP(dstXrb, desty) - height + 1; 721 XCopyArea(dpy, srcXrb->pixmap, dstXrb->pixmap, gc, 722 srcx, srcy, width, height, destx, desty); 723 } 724 else { 725 _swrast_CopyPixels(ctx, srcx, srcy, width, height, destx, desty, type ); 726 } 727} 728 729 730 731 732/* 733 * Every driver should implement a GetString function in order to 734 * return a meaningful GL_RENDERER string. 735 */ 736static const GLubyte * 737get_string( struct gl_context *ctx, GLenum name ) 738{ 739 (void) ctx; 740 switch (name) { 741 case GL_RENDERER: 742 return (const GLubyte *) "Mesa X11"; 743 case GL_VENDOR: 744 return NULL; 745 default: 746 return NULL; 747 } 748} 749 750 751/* 752 * We implement the glEnable function only because we care about 753 * dither enable/disable. 754 */ 755static void 756enable( struct gl_context *ctx, GLenum pname, GLboolean state ) 757{ 758 const XMesaContext xmesa = XMESA_CONTEXT(ctx); 759 760 switch (pname) { 761 case GL_DITHER: 762 if (state) 763 xmesa->pixelformat = xmesa->xm_visual->dithered_pf; 764 else 765 xmesa->pixelformat = xmesa->xm_visual->undithered_pf; 766 break; 767 default: 768 ; /* silence compiler warning */ 769 } 770} 771 772 773static void 774clear_color_HPCR_ximage( struct gl_context *ctx, const GLfloat color[4] ) 775{ 776 int i; 777 const XMesaContext xmesa = XMESA_CONTEXT(ctx); 778 779 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[0], color[0]); 780 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[1], color[1]); 781 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[2], color[2]); 782 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[3], color[3]); 783 784 if (color[0] == 0.0 && color[1] == 0.0 && color[2] == 0.0) { 785 /* black is black */ 786 memset( xmesa->xm_visual->hpcr_clear_ximage_pattern, 0x0 , 787 sizeof(xmesa->xm_visual->hpcr_clear_ximage_pattern)); 788 } 789 else { 790 /* build clear pattern */ 791 for (i=0; i<16; i++) { 792 xmesa->xm_visual->hpcr_clear_ximage_pattern[0][i] = 793 DITHER_HPCR(i, 0, 794 xmesa->clearcolor[0], 795 xmesa->clearcolor[1], 796 xmesa->clearcolor[2]); 797 xmesa->xm_visual->hpcr_clear_ximage_pattern[1][i] = 798 DITHER_HPCR(i, 1, 799 xmesa->clearcolor[0], 800 xmesa->clearcolor[1], 801 xmesa->clearcolor[2]); 802 } 803 } 804} 805 806 807static void 808clear_color_HPCR_pixmap( struct gl_context *ctx, const GLfloat color[4] ) 809{ 810 int i; 811 const XMesaContext xmesa = XMESA_CONTEXT(ctx); 812 813 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[0], color[0]); 814 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[1], color[1]); 815 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[2], color[2]); 816 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[3], color[3]); 817 818 if (color[0] == 0.0 && color[1] == 0.0 && color[2] == 0.0) { 819 /* black is black */ 820 for (i=0; i<16; i++) { 821 XMesaPutPixel(xmesa->xm_visual->hpcr_clear_ximage, i, 0, 0); 822 XMesaPutPixel(xmesa->xm_visual->hpcr_clear_ximage, i, 1, 0); 823 } 824 } 825 else { 826 for (i=0; i<16; i++) { 827 XMesaPutPixel(xmesa->xm_visual->hpcr_clear_ximage, i, 0, 828 DITHER_HPCR(i, 0, 829 xmesa->clearcolor[0], 830 xmesa->clearcolor[1], 831 xmesa->clearcolor[2])); 832 XMesaPutPixel(xmesa->xm_visual->hpcr_clear_ximage, i, 1, 833 DITHER_HPCR(i, 1, 834 xmesa->clearcolor[0], 835 xmesa->clearcolor[1], 836 xmesa->clearcolor[2])); 837 } 838 } 839 /* change tile pixmap content */ 840 XMesaPutImage(xmesa->display, 841 (XMesaDrawable)xmesa->xm_visual->hpcr_clear_pixmap, 842 XMESA_BUFFER(ctx->DrawBuffer)->cleargc, 843 xmesa->xm_visual->hpcr_clear_ximage, 0, 0, 0, 0, 16, 2); 844} 845 846 847/** 848 * Called when the driver should update its state, based on the new_state 849 * flags. 850 */ 851void 852xmesa_update_state( struct gl_context *ctx, GLbitfield new_state ) 853{ 854 const XMesaContext xmesa = XMESA_CONTEXT(ctx); 855 856 /* Propagate statechange information to swrast and swrast_setup 857 * modules. The X11 driver has no internal GL-dependent state. 858 */ 859 _swrast_InvalidateState( ctx, new_state ); 860 _tnl_InvalidateState( ctx, new_state ); 861 _vbo_InvalidateState( ctx, new_state ); 862 _swsetup_InvalidateState( ctx, new_state ); 863 864 if (ctx->DrawBuffer->Name != 0) 865 return; 866 867 /* 868 * GL_DITHER, GL_READ/DRAW_BUFFER, buffer binding state, etc. effect 869 * renderbuffer span/clear funcs. 870 * Check _NEW_COLOR to detect dither enable/disable. 871 */ 872 if (new_state & (_NEW_COLOR | _NEW_BUFFERS)) { 873 XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer); 874 struct xmesa_renderbuffer *front_xrb, *back_xrb; 875 876 front_xrb = xmbuf->frontxrb; 877 if (front_xrb) { 878 xmesa_set_renderbuffer_funcs(front_xrb, xmesa->pixelformat, 879 xmesa->xm_visual->BitsPerPixel); 880 front_xrb->clearFunc = clear_pixmap; 881 } 882 883 back_xrb = xmbuf->backxrb; 884 if (back_xrb) { 885 xmesa_set_renderbuffer_funcs(back_xrb, xmesa->pixelformat, 886 xmesa->xm_visual->BitsPerPixel); 887 if (xmbuf->backxrb->pixmap) { 888 back_xrb->clearFunc = clear_pixmap; 889 } 890 else { 891 switch (xmesa->xm_visual->BitsPerPixel) { 892 case 8: 893 if (xmesa->xm_visual->hpcr_clear_flag) { 894 back_xrb->clearFunc = clear_HPCR_ximage; 895 } 896 else { 897 back_xrb->clearFunc = clear_8bit_ximage; 898 } 899 break; 900 case 16: 901 back_xrb->clearFunc = clear_16bit_ximage; 902 break; 903 case 24: 904 back_xrb->clearFunc = clear_24bit_ximage; 905 break; 906 case 32: 907 back_xrb->clearFunc = clear_32bit_ximage; 908 break; 909 default: 910 back_xrb->clearFunc = clear_nbit_ximage; 911 break; 912 } 913 } 914 } 915 } 916 917 if (xmesa->xm_visual->hpcr_clear_flag) { 918 /* this depends on whether we're drawing to the front or back buffer */ 919 /* XXX FIX THIS! */ 920#if 0 921 if (pixmap) { 922 ctx->Driver.ClearColor = clear_color_HPCR_pixmap; 923 } 924 else { 925 ctx->Driver.ClearColor = clear_color_HPCR_ximage; 926 } 927#else 928 (void) clear_color_HPCR_pixmap; 929 (void) clear_color_HPCR_ximage; 930#endif 931 } 932} 933 934 935 936/** 937 * In SW, we don't really compress GL_COMPRESSED_RGB[A] textures! 938 */ 939static gl_format 940choose_tex_format( struct gl_context *ctx, GLint internalFormat, 941 GLenum format, GLenum type ) 942{ 943 switch (internalFormat) { 944 case GL_COMPRESSED_RGB_ARB: 945 return MESA_FORMAT_RGB888; 946 case GL_COMPRESSED_RGBA_ARB: 947 return MESA_FORMAT_RGBA8888; 948 default: 949 return _mesa_choose_tex_format(ctx, internalFormat, format, type); 950 } 951} 952 953 954/** 955 * Called by glViewport. 956 * This is a good time for us to poll the current X window size and adjust 957 * our renderbuffers to match the current window size. 958 * Remember, we have no opportunity to respond to conventional 959 * X Resize/StructureNotify events since the X driver has no event loop. 960 * Thus, we poll. 961 * Note that this trick isn't fool-proof. If the application never calls 962 * glViewport, our notion of the current window size may be incorrect. 963 * That problem led to the GLX_MESA_resize_buffers extension. 964 */ 965static void 966xmesa_viewport(struct gl_context *ctx, GLint x, GLint y, GLsizei w, GLsizei h) 967{ 968 XMesaContext xmctx = XMESA_CONTEXT(ctx); 969 XMesaBuffer xmdrawbuf = XMESA_BUFFER(ctx->WinSysDrawBuffer); 970 XMesaBuffer xmreadbuf = XMESA_BUFFER(ctx->WinSysReadBuffer); 971 xmesa_check_and_update_buffer_size(xmctx, xmdrawbuf); 972 xmesa_check_and_update_buffer_size(xmctx, xmreadbuf); 973 (void) x; 974 (void) y; 975 (void) w; 976 (void) h; 977} 978 979 980#if ENABLE_EXT_timer_query 981 982/* 983 * The GL_EXT_timer_query extension is not enabled for the XServer 984 * indirect renderer. Not sure about how/if wrapping of gettimeofday() 985 * is done, etc. 986 */ 987 988struct xmesa_query_object 989{ 990 struct gl_query_object Base; 991 struct timeval StartTime; 992}; 993 994 995static struct gl_query_object * 996xmesa_new_query_object(struct gl_context *ctx, GLuint id) 997{ 998 struct xmesa_query_object *q = CALLOC_STRUCT(xmesa_query_object); 999 if (q) { 1000 q->Base.Id = id; 1001 q->Base.Ready = GL_TRUE; 1002 } 1003 return &q->Base; 1004} 1005 1006 1007static void 1008xmesa_begin_query(struct gl_context *ctx, struct gl_query_object *q) 1009{ 1010 if (q->Target == GL_TIME_ELAPSED_EXT) { 1011 struct xmesa_query_object *xq = (struct xmesa_query_object *) q; 1012 (void) gettimeofday(&xq->StartTime, NULL); 1013 } 1014} 1015 1016 1017/** 1018 * Return the difference between the two given times in microseconds. 1019 */ 1020#ifdef __VMS 1021#define suseconds_t unsigned int 1022#endif 1023static GLuint64EXT 1024time_diff(const struct timeval *t0, const struct timeval *t1) 1025{ 1026 GLuint64EXT seconds0 = t0->tv_sec & 0xff; /* 0 .. 255 seconds */ 1027 GLuint64EXT seconds1 = t1->tv_sec & 0xff; /* 0 .. 255 seconds */ 1028 GLuint64EXT nanosec0 = (seconds0 * 1000000 + t0->tv_usec) * 1000; 1029 GLuint64EXT nanosec1 = (seconds1 * 1000000 + t1->tv_usec) * 1000; 1030 return nanosec1 - nanosec0; 1031} 1032 1033 1034static void 1035xmesa_end_query(struct gl_context *ctx, struct gl_query_object *q) 1036{ 1037 if (q->Target == GL_TIME_ELAPSED_EXT) { 1038 struct xmesa_query_object *xq = (struct xmesa_query_object *) q; 1039 struct timeval endTime; 1040 (void) gettimeofday(&endTime, NULL); 1041 /* result is in nanoseconds! */ 1042 q->Result = time_diff(&xq->StartTime, &endTime); 1043 } 1044 q->Ready = GL_TRUE; 1045} 1046 1047#endif /* ENABLE_timer_query */ 1048 1049 1050/** 1051 * Initialize the device driver function table with the functions 1052 * we implement in this driver. 1053 */ 1054void 1055xmesa_init_driver_functions( XMesaVisual xmvisual, 1056 struct dd_function_table *driver ) 1057{ 1058 driver->GetString = get_string; 1059 driver->UpdateState = xmesa_update_state; 1060 driver->GetBufferSize = NULL; /* OBSOLETE */ 1061 driver->Flush = finish_or_flush; 1062 driver->Finish = finish_or_flush; 1063 driver->ClearColor = clear_color; 1064 driver->ColorMask = color_mask; 1065 driver->Enable = enable; 1066 driver->Viewport = xmesa_viewport; 1067 if (TEST_META_FUNCS) { 1068 driver->Clear = _mesa_meta_Clear; 1069 driver->CopyPixels = _mesa_meta_CopyPixels; 1070 driver->BlitFramebuffer = _mesa_meta_BlitFramebuffer; 1071 driver->DrawPixels = _mesa_meta_DrawPixels; 1072 driver->Bitmap = _mesa_meta_Bitmap; 1073 } 1074 else { 1075 driver->Clear = clear_buffers; 1076 driver->CopyPixels = xmesa_CopyPixels; 1077 if (xmvisual->undithered_pf == PF_8R8G8B && 1078 xmvisual->dithered_pf == PF_8R8G8B && 1079 xmvisual->BitsPerPixel == 32) { 1080 driver->DrawPixels = xmesa_DrawPixels_8R8G8B; 1081 } 1082 else if (xmvisual->undithered_pf == PF_5R6G5B) { 1083 driver->DrawPixels = xmesa_DrawPixels_5R6G5B; 1084 } 1085 } 1086 1087#if ENABLE_EXT_texure_compression_s3tc 1088 driver->ChooseTextureFormat = choose_tex_format; 1089#else 1090 (void) choose_tex_format; 1091#endif 1092 1093#if ENABLE_EXT_timer_query 1094 driver->NewQueryObject = xmesa_new_query_object; 1095 driver->BeginQuery = xmesa_begin_query; 1096 driver->EndQuery = xmesa_end_query; 1097#endif 1098} 1099 1100 1101#define XMESA_NEW_POINT (_NEW_POINT | \ 1102 _NEW_RENDERMODE | \ 1103 _SWRAST_NEW_RASTERMASK) 1104 1105#define XMESA_NEW_LINE (_NEW_LINE | \ 1106 _NEW_TEXTURE | \ 1107 _NEW_LIGHT | \ 1108 _NEW_DEPTH | \ 1109 _NEW_RENDERMODE | \ 1110 _SWRAST_NEW_RASTERMASK) 1111 1112#define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \ 1113 _NEW_TEXTURE | \ 1114 _NEW_LIGHT | \ 1115 _NEW_DEPTH | \ 1116 _NEW_RENDERMODE | \ 1117 _SWRAST_NEW_RASTERMASK) 1118 1119 1120/** 1121 * Extend the software rasterizer with our line/point/triangle 1122 * functions. 1123 * Called during context creation only. 1124 */ 1125void xmesa_register_swrast_functions( struct gl_context *ctx ) 1126{ 1127 SWcontext *swrast = SWRAST_CONTEXT( ctx ); 1128 1129 swrast->choose_point = xmesa_choose_point; 1130 swrast->choose_line = xmesa_choose_line; 1131 swrast->choose_triangle = xmesa_choose_triangle; 1132 1133 /* XXX these lines have no net effect. Remove??? */ 1134 swrast->InvalidatePointMask |= XMESA_NEW_POINT; 1135 swrast->InvalidateLineMask |= XMESA_NEW_LINE; 1136 swrast->InvalidateTriangleMask |= XMESA_NEW_TRIANGLE; 1137} 1138