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