56c30ab9491922eaff8765a6a33fd15663bec900
[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 "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
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 this was never tested in the Xserver environment */
424
425 /**
426 * This function implements glDrawPixels() with an XPutImage call when
427 * drawing to the front buffer (X Window drawable).
428 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
429 */
430 static void
431 xmesa_DrawPixels_8R8G8B( GLcontext *ctx,
432 GLint x, GLint y, GLsizei width, GLsizei height,
433 GLenum format, GLenum type,
434 const struct gl_pixelstore_attrib *unpack,
435 const GLvoid *pixels )
436 {
437 const SWcontext *swrast = SWRAST_CONTEXT( ctx );
438 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0];
439 struct xmesa_renderbuffer *xrb;
440
441 if (!rb)
442 return;
443
444 xrb = xmesa_renderbuffer(rb->Wrapped);
445
446 if (swrast->NewState)
447 _swrast_validate_derived( ctx );
448
449 if (ctx->DrawBuffer->Name == 0 &&
450 format == GL_BGRA &&
451 type == GL_UNSIGNED_BYTE &&
452 (swrast->_RasterMask & ~CLIP_BIT) == 0 && /* no blend, z-test, etc */
453 ctx->_ImageTransferState == 0 && /* no color tables, scale/bias, etc */
454 ctx->Pixel.ZoomX == 1.0 && /* no zooming */
455 ctx->Pixel.ZoomY == 1.0 &&
456 xrb->pixmap && /* drawing to pixmap or window */
457 xrb->Base.AlphaBits == 0)
458 {
459 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
460 XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
461 XMesaDisplay *dpy = xmesa->xm_visual->display;
462 const XMesaGC gc = xmbuf->cleargc; /* effected by glColorMask */
463 int dstX = x;
464 int dstY = y;
465 int w = width;
466 int h = height;
467 struct gl_pixelstore_attrib clippedUnpack = *unpack;
468
469 ASSERT(xmesa->xm_visual->dithered_pf == PF_8R8G8B);
470 ASSERT(xmesa->xm_visual->undithered_pf == PF_8R8G8B);
471 ASSERT(dpy);
472 ASSERT(gc);
473
474 if (unpack->BufferObj->Name) {
475 /* unpack from PBO */
476 GLubyte *buf;
477 if (!_mesa_validate_pbo_access(2, unpack, width, height, 1,
478 format, type, pixels)) {
479 _mesa_error(ctx, GL_INVALID_OPERATION,
480 "glDrawPixels(invalid PBO access)");
481 return;
482 }
483 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx,
484 GL_PIXEL_UNPACK_BUFFER_EXT,
485 GL_READ_ONLY_ARB,
486 unpack->BufferObj);
487 if (!buf) {
488 /* buffer is already mapped - that's an error */
489 _mesa_error(ctx, GL_INVALID_OPERATION,
490 "glDrawPixels(PBO is mapped)");
491 return;
492 }
493 pixels = ADD_POINTERS(buf, pixels);
494 }
495
496 if (_mesa_clip_drawpixels(ctx, &dstX, &dstY, &w, &h, &clippedUnpack)) {
497 /* This is a little tricky since all coordinates up to now have
498 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
499 * so we have to carefully compute the Y coordinates/addresses here.
500 */
501 int srcX = clippedUnpack.SkipPixels;
502 int srcY = clippedUnpack.SkipRows;
503 int rowLength = clippedUnpack.RowLength;
504 XMesaImage ximage;
505 MEMSET(&ximage, 0, sizeof(XMesaImage));
506 ximage.width = width;
507 ximage.height = height;
508 ximage.format = ZPixmap;
509 ximage.data = (char *) pixels
510 + ((srcY + h - 1) * rowLength + srcX) * 4;
511 ximage.byte_order = LSBFirst;
512 ximage.bitmap_unit = 32;
513 ximage.bitmap_bit_order = LSBFirst;
514 ximage.bitmap_pad = 32;
515 ximage.depth = 24;
516 ximage.bytes_per_line = -rowLength * 4; /* negative to flip image */
517 ximage.bits_per_pixel = 32;
518 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
519 /* flip Y axis for dest position */
520 dstY = YFLIP(xrb, dstY) - h + 1;
521 XPutImage(dpy, xrb->pixmap, gc, &ximage, 0, 0, dstX, dstY, w, h);
522 }
523
524 if (unpack->BufferObj->Name) {
525 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
526 unpack->BufferObj);
527 }
528 }
529 else {
530 /* software fallback */
531 _swrast_DrawPixels(ctx, x, y, width, height,
532 format, type, unpack, pixels);
533 }
534 }
535
536
537
538 /**
539 * This function implements glDrawPixels() with an XPutImage call when
540 * drawing to the front buffer (X Window drawable). The image format
541 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
542 * match the PF_5R6G5B pixel format.
543 */
544 static void
545 xmesa_DrawPixels_5R6G5B( GLcontext *ctx,
546 GLint x, GLint y, GLsizei width, GLsizei height,
547 GLenum format, GLenum type,
548 const struct gl_pixelstore_attrib *unpack,
549 const GLvoid *pixels )
550 {
551 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
552 const SWcontext *swrast = SWRAST_CONTEXT( ctx );
553 XMesaDisplay *dpy = xmesa->xm_visual->display;
554 XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
555 const XMesaGC gc = xmbuf->cleargc; /* effected by glColorMask */
556 struct xmesa_renderbuffer *xrb;
557
558 ASSERT(dpy);
559 ASSERT(gc);
560 ASSERT(xmesa->xm_visual->undithered_pf == PF_5R6G5B);
561
562 if (!ctx->DrawBuffer->_ColorDrawBuffers[0])
563 return;
564
565 xrb = xmesa_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]->Wrapped);
566
567 if (swrast->NewState)
568 _swrast_validate_derived( ctx );
569
570 if (xrb->pixmap && /* drawing to pixmap or window */
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 XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
662 const XMesaGC gc = xmbuf->cleargc; /* effected by glColorMask */
663 struct xmesa_renderbuffer *srcXrb, *dstXrb;
664
665 if (!ctx->ReadBuffer->_ColorReadBuffer ||
666 !ctx->DrawBuffer->_ColorDrawBuffers[0])
667 return;
668
669 ASSERT(dpy);
670 ASSERT(gc);
671
672 srcXrb = xmesa_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer->Wrapped);
673 dstXrb = xmesa_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]->Wrapped);
674
675 if (swrast->NewState)
676 _swrast_validate_derived( ctx );
677
678 if (ctx->Color.DrawBuffer[0] == GL_FRONT &&
679 ctx->Pixel.ReadBuffer == GL_FRONT &&
680 srcXrb->pixmap &&
681 dstXrb->pixmap &&
682 type == GL_COLOR &&
683 (swrast->_RasterMask & ~CLIP_BIT) == 0 && /* no blend, z-test, etc */
684 ctx->_ImageTransferState == 0 && /* no color tables, scale/bias, etc */
685 ctx->Pixel.ZoomX == 1.0 && /* no zooming */
686 ctx->Pixel.ZoomY == 1.0) {
687 /* Note: we don't do any special clipping work here. We could,
688 * but X will do it for us.
689 */
690 srcy = YFLIP(srcXrb, srcy) - height + 1;
691 desty = YFLIP(dstXrb, desty) - height + 1;
692 XCopyArea(dpy, srcXrb->pixmap, dstXrb->pixmap, gc,
693 srcx, srcy, width, height, destx, desty);
694 }
695 else {
696 _swrast_CopyPixels(ctx, srcx, srcy, width, height, destx, desty, type );
697 }
698 }
699 #endif /* XFree86Server */
700
701
702
703 /*
704 * Every driver should implement a GetString function in order to
705 * return a meaningful GL_RENDERER string.
706 */
707 static const GLubyte *
708 get_string( GLcontext *ctx, GLenum name )
709 {
710 (void) ctx;
711 switch (name) {
712 case GL_RENDERER:
713 #ifdef XFree86Server
714 return (const GLubyte *) "Mesa GLX Indirect";
715 #else
716 return (const GLubyte *) "Mesa X11";
717 #endif
718 case GL_VENDOR:
719 #ifdef XFree86Server
720 return (const GLubyte *) "Mesa project: www.mesa3d.org";
721 #else
722 return NULL;
723 #endif
724 default:
725 return NULL;
726 }
727 }
728
729
730 /*
731 * We implement the glEnable function only because we care about
732 * dither enable/disable.
733 */
734 static void
735 enable( GLcontext *ctx, GLenum pname, GLboolean state )
736 {
737 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
738
739 switch (pname) {
740 case GL_DITHER:
741 if (state)
742 xmesa->pixelformat = xmesa->xm_visual->dithered_pf;
743 else
744 xmesa->pixelformat = xmesa->xm_visual->undithered_pf;
745 break;
746 default:
747 ; /* silence compiler warning */
748 }
749 }
750
751
752 static void
753 clear_color_HPCR_ximage( GLcontext *ctx, const GLfloat color[4] )
754 {
755 int i;
756 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
757
758 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[0], color[0]);
759 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[1], color[1]);
760 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[2], color[2]);
761 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[3], color[3]);
762
763 if (color[0] == 0.0 && color[1] == 0.0 && color[2] == 0.0) {
764 /* black is black */
765 MEMSET( xmesa->xm_visual->hpcr_clear_ximage_pattern, 0x0 ,
766 sizeof(xmesa->xm_visual->hpcr_clear_ximage_pattern));
767 }
768 else {
769 /* build clear pattern */
770 for (i=0; i<16; i++) {
771 xmesa->xm_visual->hpcr_clear_ximage_pattern[0][i] =
772 DITHER_HPCR(i, 0,
773 xmesa->clearcolor[0],
774 xmesa->clearcolor[1],
775 xmesa->clearcolor[2]);
776 xmesa->xm_visual->hpcr_clear_ximage_pattern[1][i] =
777 DITHER_HPCR(i, 1,
778 xmesa->clearcolor[0],
779 xmesa->clearcolor[1],
780 xmesa->clearcolor[2]);
781 }
782 }
783 }
784
785
786 static void
787 clear_color_HPCR_pixmap( GLcontext *ctx, const GLfloat color[4] )
788 {
789 int i;
790 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
791
792 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[0], color[0]);
793 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[1], color[1]);
794 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[2], color[2]);
795 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[3], color[3]);
796
797 if (color[0] == 0.0 && color[1] == 0.0 && color[2] == 0.0) {
798 /* black is black */
799 for (i=0; i<16; i++) {
800 XMesaPutPixel(xmesa->xm_visual->hpcr_clear_ximage, i, 0, 0);
801 XMesaPutPixel(xmesa->xm_visual->hpcr_clear_ximage, i, 1, 0);
802 }
803 }
804 else {
805 for (i=0; i<16; i++) {
806 XMesaPutPixel(xmesa->xm_visual->hpcr_clear_ximage, i, 0,
807 DITHER_HPCR(i, 0,
808 xmesa->clearcolor[0],
809 xmesa->clearcolor[1],
810 xmesa->clearcolor[2]));
811 XMesaPutPixel(xmesa->xm_visual->hpcr_clear_ximage, i, 1,
812 DITHER_HPCR(i, 1,
813 xmesa->clearcolor[0],
814 xmesa->clearcolor[1],
815 xmesa->clearcolor[2]));
816 }
817 }
818 /* change tile pixmap content */
819 XMesaPutImage(xmesa->display,
820 (XMesaDrawable)xmesa->xm_visual->hpcr_clear_pixmap,
821 XMESA_BUFFER(ctx->DrawBuffer)->cleargc,
822 xmesa->xm_visual->hpcr_clear_ximage, 0, 0, 0, 0, 16, 2);
823 }
824
825
826 /**
827 * Called when the driver should update its state, based on the new_state
828 * flags.
829 */
830 void
831 xmesa_update_state( GLcontext *ctx, GLbitfield new_state )
832 {
833 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
834
835 /* Propagate statechange information to swrast and swrast_setup
836 * modules. The X11 driver has no internal GL-dependent state.
837 */
838 _swrast_InvalidateState( ctx, new_state );
839 _tnl_InvalidateState( ctx, new_state );
840 _vbo_InvalidateState( ctx, new_state );
841 _swsetup_InvalidateState( ctx, new_state );
842
843 if (ctx->DrawBuffer->Name != 0)
844 return;
845
846 /*
847 * GL_DITHER, GL_READ/DRAW_BUFFER, buffer binding state, etc. effect
848 * renderbuffer span/clear funcs.
849 */
850 if (new_state & (_NEW_COLOR | _NEW_PIXEL | _NEW_BUFFERS)) {
851 XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
852 struct xmesa_renderbuffer *front_xrb, *back_xrb;
853
854 front_xrb = xmbuf->frontxrb;
855 if (front_xrb) {
856 xmesa_set_renderbuffer_funcs(front_xrb, xmesa->pixelformat,
857 xmesa->xm_visual->BitsPerPixel);
858 front_xrb->clearFunc = clear_pixmap;
859 }
860
861 back_xrb = xmbuf->backxrb;
862 if (back_xrb) {
863 xmesa_set_renderbuffer_funcs(back_xrb, xmesa->pixelformat,
864 xmesa->xm_visual->BitsPerPixel);
865 if (xmbuf->backxrb->pixmap) {
866 back_xrb->clearFunc = clear_pixmap;
867 }
868 else {
869 switch (xmesa->xm_visual->BitsPerPixel) {
870 case 8:
871 if (xmesa->xm_visual->hpcr_clear_flag) {
872 back_xrb->clearFunc = clear_HPCR_ximage;
873 }
874 else {
875 back_xrb->clearFunc = clear_8bit_ximage;
876 }
877 break;
878 case 16:
879 back_xrb->clearFunc = clear_16bit_ximage;
880 break;
881 case 24:
882 back_xrb->clearFunc = clear_24bit_ximage;
883 break;
884 case 32:
885 back_xrb->clearFunc = clear_32bit_ximage;
886 break;
887 default:
888 back_xrb->clearFunc = clear_nbit_ximage;
889 break;
890 }
891 }
892 }
893 }
894
895 if (xmesa->xm_visual->hpcr_clear_flag) {
896 /* this depends on whether we're drawing to the front or back buffer */
897 /* XXX FIX THIS! */
898 #if 0
899 if (pixmap) {
900 ctx->Driver.ClearColor = clear_color_HPCR_pixmap;
901 }
902 else {
903 ctx->Driver.ClearColor = clear_color_HPCR_ximage;
904 }
905 #else
906 (void) clear_color_HPCR_pixmap;
907 (void) clear_color_HPCR_ximage;
908 #endif
909 }
910 }
911
912
913
914 /**
915 * Called via ctx->Driver.TestProxyTeximage(). Normally, we'd just use
916 * the _mesa_test_proxy_teximage() fallback function, but we're going to
917 * special-case the 3D texture case to allow textures up to 512x512x32
918 * texels.
919 */
920 static GLboolean
921 test_proxy_teximage(GLcontext *ctx, GLenum target, GLint level,
922 GLint internalFormat, GLenum format, GLenum type,
923 GLint width, GLint height, GLint depth, GLint border)
924 {
925 if (target == GL_PROXY_TEXTURE_3D) {
926 /* special case for 3D textures */
927 if (width * height * depth > 512 * 512 * 64 ||
928 width < 2 * border ||
929 (!ctx->Extensions.ARB_texture_non_power_of_two &&
930 _mesa_bitcount(width - 2 * border) != 1) ||
931 height < 2 * border ||
932 (!ctx->Extensions.ARB_texture_non_power_of_two &&
933 _mesa_bitcount(height - 2 * border) != 1) ||
934 depth < 2 * border ||
935 (!ctx->Extensions.ARB_texture_non_power_of_two &&
936 _mesa_bitcount(depth - 2 * border) != 1)) {
937 /* Bad size, or too many texels */
938 return GL_FALSE;
939 }
940 return GL_TRUE;
941 }
942 else {
943 /* use the fallback routine for 1D, 2D, cube and rect targets */
944 return _mesa_test_proxy_teximage(ctx, target, level, internalFormat,
945 format, type, width, height, depth,
946 border);
947 }
948 }
949
950
951 /**
952 * In SW, we don't really compress GL_COMPRESSED_RGB[A] textures!
953 */
954 static const struct gl_texture_format *
955 choose_tex_format( GLcontext *ctx, GLint internalFormat,
956 GLenum format, GLenum type )
957 {
958 switch (internalFormat) {
959 case GL_COMPRESSED_RGB_ARB:
960 return &_mesa_texformat_rgb;
961 case GL_COMPRESSED_RGBA_ARB:
962 return &_mesa_texformat_rgba;
963 default:
964 return _mesa_choose_tex_format(ctx, internalFormat, format, type);
965 }
966 }
967
968
969 /**
970 * Called by glViewport.
971 * This is a good time for us to poll the current X window size and adjust
972 * our renderbuffers to match the current window size.
973 * Remember, we have no opportunity to respond to conventional
974 * X Resize/StructureNotify events since the X driver has no event loop.
975 * Thus, we poll.
976 * Note that this trick isn't fool-proof. If the application never calls
977 * glViewport, our notion of the current window size may be incorrect.
978 * That problem led to the GLX_MESA_resize_buffers extension.
979 */
980 static void
981 xmesa_viewport(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
982 {
983 XMesaContext xmctx = XMESA_CONTEXT(ctx);
984 XMesaBuffer xmdrawbuf = XMESA_BUFFER(ctx->WinSysDrawBuffer);
985 XMesaBuffer xmreadbuf = XMESA_BUFFER(ctx->WinSysReadBuffer);
986 xmesa_check_and_update_buffer_size(xmctx, xmdrawbuf);
987 xmesa_check_and_update_buffer_size(xmctx, xmreadbuf);
988 (void) x;
989 (void) y;
990 (void) w;
991 (void) h;
992 }
993
994
995 #if ENABLE_EXT_timer_query
996
997 /*
998 * The GL_EXT_timer_query extension is not enabled for the XServer
999 * indirect renderer. Not sure about how/if wrapping of gettimeofday()
1000 * is done, etc.
1001 */
1002
1003 struct xmesa_query_object
1004 {
1005 struct gl_query_object Base;
1006 struct timeval StartTime;
1007 };
1008
1009
1010 static struct gl_query_object *
1011 xmesa_new_query_object(GLcontext *ctx, GLuint id)
1012 {
1013 struct xmesa_query_object *q = CALLOC_STRUCT(xmesa_query_object);
1014 if (q) {
1015 q->Base.Id = id;
1016 q->Base.Ready = GL_TRUE;
1017 }
1018 return &q->Base;
1019 }
1020
1021
1022 static void
1023 xmesa_begin_query(GLcontext *ctx, GLenum target, struct gl_query_object *q)
1024 {
1025 if (target == GL_TIME_ELAPSED_EXT) {
1026 struct xmesa_query_object *xq = (struct xmesa_query_object *) q;
1027 (void) gettimeofday(&xq->StartTime, NULL);
1028 }
1029 }
1030
1031
1032 /**
1033 * Return the difference between the two given times in microseconds.
1034 */
1035 #ifdef __VMS
1036 #define suseconds_t unsigned int
1037 #endif
1038 static GLuint64EXT
1039 time_diff(const struct timeval *t0, const struct timeval *t1)
1040 {
1041 GLuint64EXT seconds0 = t0->tv_sec & 0xff; /* 0 .. 255 seconds */
1042 GLuint64EXT seconds1 = t1->tv_sec & 0xff; /* 0 .. 255 seconds */
1043 GLuint64EXT nanosec0 = (seconds0 * 1000000 + t0->tv_usec) * 1000;
1044 GLuint64EXT nanosec1 = (seconds1 * 1000000 + t1->tv_usec) * 1000;
1045 return nanosec1 - nanosec0;
1046 }
1047
1048
1049 static void
1050 xmesa_end_query(GLcontext *ctx, GLenum target, struct gl_query_object *q)
1051 {
1052 if (target == GL_TIME_ELAPSED_EXT) {
1053 struct xmesa_query_object *xq = (struct xmesa_query_object *) q;
1054 struct timeval endTime;
1055 (void) gettimeofday(&endTime, NULL);
1056 /* result is in nanoseconds! */
1057 q->Result = time_diff(&xq->StartTime, &endTime);
1058 }
1059 q->Ready = GL_TRUE;
1060 }
1061
1062 #endif /* ENABLE_timer_query */
1063
1064
1065 /**
1066 * Initialize the device driver function table with the functions
1067 * we implement in this driver.
1068 */
1069 void
1070 xmesa_init_driver_functions( XMesaVisual xmvisual,
1071 struct dd_function_table *driver )
1072 {
1073 driver->GetString = get_string;
1074 driver->UpdateState = xmesa_update_state;
1075 driver->GetBufferSize = NULL; /* OBSOLETE */
1076 driver->Flush = finish_or_flush;
1077 driver->Finish = finish_or_flush;
1078 driver->ClearIndex = clear_index;
1079 driver->ClearColor = clear_color;
1080 driver->IndexMask = index_mask;
1081 driver->ColorMask = color_mask;
1082 driver->Enable = enable;
1083 driver->Clear = clear_buffers;
1084 driver->Viewport = xmesa_viewport;
1085 #ifndef XFree86Server
1086 driver->CopyPixels = xmesa_CopyPixels;
1087 if (xmvisual->undithered_pf == PF_8R8G8B &&
1088 xmvisual->dithered_pf == PF_8R8G8B) {
1089 driver->DrawPixels = xmesa_DrawPixels_8R8G8B;
1090 }
1091 else if (xmvisual->undithered_pf == PF_5R6G5B) {
1092 driver->DrawPixels = xmesa_DrawPixels_5R6G5B;
1093 }
1094 #endif
1095 driver->TestProxyTexImage = test_proxy_teximage;
1096 #if ENABLE_EXT_texure_compression_s3tc
1097 driver->ChooseTextureFormat = choose_tex_format;
1098 #else
1099 (void) choose_tex_format;
1100 #endif
1101
1102 #if ENABLE_EXT_timer_query
1103 driver->NewQueryObject = xmesa_new_query_object;
1104 driver->BeginQuery = xmesa_begin_query;
1105 driver->EndQuery = xmesa_end_query;
1106 #endif
1107 }
1108
1109
1110 #define XMESA_NEW_POINT (_NEW_POINT | \
1111 _NEW_RENDERMODE | \
1112 _SWRAST_NEW_RASTERMASK)
1113
1114 #define XMESA_NEW_LINE (_NEW_LINE | \
1115 _NEW_TEXTURE | \
1116 _NEW_LIGHT | \
1117 _NEW_DEPTH | \
1118 _NEW_RENDERMODE | \
1119 _SWRAST_NEW_RASTERMASK)
1120
1121 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1122 _NEW_TEXTURE | \
1123 _NEW_LIGHT | \
1124 _NEW_DEPTH | \
1125 _NEW_RENDERMODE | \
1126 _SWRAST_NEW_RASTERMASK)
1127
1128
1129 /**
1130 * Extend the software rasterizer with our line/point/triangle
1131 * functions.
1132 * Called during context creation only.
1133 */
1134 void xmesa_register_swrast_functions( GLcontext *ctx )
1135 {
1136 SWcontext *swrast = SWRAST_CONTEXT( ctx );
1137
1138 swrast->choose_point = xmesa_choose_point;
1139 swrast->choose_line = xmesa_choose_line;
1140 swrast->choose_triangle = xmesa_choose_triangle;
1141
1142 /* XXX these lines have no net effect. Remove??? */
1143 swrast->InvalidatePointMask |= XMESA_NEW_POINT;
1144 swrast->InvalidateLineMask |= XMESA_NEW_LINE;
1145 swrast->InvalidateTriangleMask |= XMESA_NEW_TRIANGLE;
1146 }