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