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