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