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