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