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