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