Refactored several variables out of XMesaVisual in favor of identical
[mesa.git] / src / mesa / drivers / x11 / xm_dd.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.1
4 *
5 * Copyright (C) 1999-2004 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 #include "glxheader.h"
27 #include "bufferobj.h"
28 #include "context.h"
29 #include "colormac.h"
30 #include "depth.h"
31 #include "drawpix.h"
32 #include "extensions.h"
33 #include "macros.h"
34 #include "imports.h"
35 #include "mtypes.h"
36 #include "state.h"
37 #include "texobj.h"
38 #include "teximage.h"
39 #include "texstore.h"
40 #include "texformat.h"
41 #include "xmesaP.h"
42 #include "array_cache/acache.h"
43 #include "swrast/swrast.h"
44 #include "swrast/s_context.h"
45 #include "swrast/s_drawpix.h"
46 #include "swrast/s_alphabuf.h"
47 #include "swrast_setup/swrast_setup.h"
48 #include "tnl/tnl.h"
49 #include "tnl/t_context.h"
50
51 #ifdef XFree86Server
52 #include <GL/glxtokens.h>
53 #endif
54
55 /*
56 * Return the size (width, height) of the X window for the given GLframebuffer.
57 * Output: width - width of buffer in pixels.
58 * height - height of buffer in pixels.
59 */
60 static void
61 get_buffer_size( GLframebuffer *buffer, GLuint *width, GLuint *height )
62 {
63 /* We can do this cast because the first field in the XMesaBuffer
64 * struct is a GLframebuffer struct. If this weren't true, we'd
65 * need a pointer from the GLframebuffer to the XMesaBuffer.
66 */
67 const XMesaBuffer xmBuffer = (XMesaBuffer) buffer;
68 unsigned int winwidth, winheight;
69 #ifdef XFree86Server
70 /* XFree86 GLX renderer */
71 if (xmBuffer->frontbuffer->width > MAX_WIDTH ||
72 xmBuffer->frontbuffer->height > MAX_HEIGHT) {
73 winwidth = buffer->Width;
74 winheight = buffer->Height;
75 } else {
76 winwidth = xmBuffer->frontbuffer->width;
77 winheight = xmBuffer->frontbuffer->height;
78 }
79 #else
80 Window root;
81 int winx, winy;
82 unsigned int bw, d;
83
84 _glthread_LOCK_MUTEX(_xmesa_lock);
85 XSync(xmBuffer->xm_visual->display, 0); /* added for Chromium */
86 XGetGeometry( xmBuffer->xm_visual->display, xmBuffer->frontbuffer, &root,
87 &winx, &winy, &winwidth, &winheight, &bw, &d );
88 _glthread_UNLOCK_MUTEX(_xmesa_lock);
89 #endif
90
91 (void)kernel8; /* Muffle compiler */
92
93 *width = winwidth;
94 *height = winheight;
95 }
96
97
98 static void
99 finish_or_flush( GLcontext *ctx )
100 {
101 #ifdef XFree86Server
102 /* NOT_NEEDED */
103 #else
104 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
105 if (xmesa) {
106 _glthread_LOCK_MUTEX(_xmesa_lock);
107 XSync( xmesa->display, False );
108 _glthread_UNLOCK_MUTEX(_xmesa_lock);
109 }
110 #endif
111 }
112
113
114
115 /*
116 * This chooses the color buffer for reading and writing spans, points,
117 * lines, and triangles.
118 */
119 void
120 xmesa_set_buffer( GLcontext *ctx, GLframebuffer *buffer, GLuint bufferBit )
121 {
122 /* We can make this cast since the XMesaBuffer wraps GLframebuffer.
123 * GLframebuffer is the first member in a XMesaBuffer struct.
124 */
125 XMesaBuffer target = (XMesaBuffer) buffer;
126 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
127
128 /* This assignment tells the span/point/line/triangle functions
129 * which XMesaBuffer to use.
130 */
131 xmesa->xm_buffer = target;
132
133 /*
134 * Now determine front vs back color buffer.
135 */
136 if (bufferBit == FRONT_LEFT_BIT) {
137 target->buffer = target->frontbuffer;
138 }
139 else if (bufferBit == BACK_LEFT_BIT) {
140 ASSERT(target->db_state);
141 if (target->backpixmap) {
142 /* back buffer is a pixmap */
143 target->buffer = (XMesaDrawable) target->backpixmap;
144 }
145 else if (target->backimage) {
146 /* back buffer is an XImage */
147 target->buffer = None;
148 }
149 else {
150 /* No back buffer!!!! Must be out of memory, use front buffer */
151 target->buffer = target->frontbuffer;
152 }
153 }
154 else {
155 _mesa_problem(ctx, "invalid buffer 0x%x in set_buffer() in xm_dd.c");
156 return;
157 }
158 xmesa_update_span_funcs(ctx);
159 }
160
161
162
163 static void
164 clear_index( GLcontext *ctx, GLuint index )
165 {
166 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
167 xmesa->clearpixel = (unsigned long) index;
168 XMesaSetForeground( xmesa->display, xmesa->xm_draw_buffer->cleargc,
169 (unsigned long) index );
170 }
171
172
173 static void
174 clear_color( GLcontext *ctx, const GLfloat color[4] )
175 {
176 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
177 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[0], color[0]);
178 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[1], color[1]);
179 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[2], color[2]);
180 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[3], color[3]);
181 xmesa->clearpixel = xmesa_color_to_pixel( xmesa,
182 xmesa->clearcolor[0],
183 xmesa->clearcolor[1],
184 xmesa->clearcolor[2],
185 xmesa->clearcolor[3],
186 xmesa->xm_visual->undithered_pf );
187 _glthread_LOCK_MUTEX(_xmesa_lock);
188 XMesaSetForeground( xmesa->display, xmesa->xm_draw_buffer->cleargc,
189 xmesa->clearpixel );
190 _glthread_UNLOCK_MUTEX(_xmesa_lock);
191 }
192
193
194
195 /* Set index mask ala glIndexMask */
196 static void
197 index_mask( GLcontext *ctx, GLuint mask )
198 {
199 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
200 if (xmesa->xm_draw_buffer->buffer != XIMAGE) {
201 unsigned long m;
202 if (mask==0xffffffff) {
203 m = ((unsigned long)~0L);
204 }
205 else {
206 m = (unsigned long) mask;
207 }
208 XMesaSetPlaneMask( xmesa->display, xmesa->xm_draw_buffer->cleargc, m );
209 XMesaSetPlaneMask( xmesa->display, xmesa->xm_draw_buffer->gc, m );
210 }
211 }
212
213
214 /* Implements glColorMask() */
215 static void
216 color_mask(GLcontext *ctx,
217 GLboolean rmask, GLboolean gmask, GLboolean bmask, GLboolean amask)
218 {
219 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
220 const int xclass = xmesa->xm_visual->mesa_visual.visualType;
221 (void) amask;
222
223 if (xclass == GLX_TRUE_COLOR || xclass == GLX_DIRECT_COLOR) {
224 unsigned long m;
225 if (rmask && gmask && bmask) {
226 m = ((unsigned long)~0L);
227 }
228 else {
229 m = 0;
230 if (rmask) m |= GET_REDMASK(xmesa->xm_visual);
231 if (gmask) m |= GET_GREENMASK(xmesa->xm_visual);
232 if (bmask) m |= GET_BLUEMASK(xmesa->xm_visual);
233 }
234 XMesaSetPlaneMask( xmesa->display, xmesa->xm_draw_buffer->cleargc, m );
235 XMesaSetPlaneMask( xmesa->display, xmesa->xm_draw_buffer->gc, m );
236 }
237 }
238
239
240
241 /**********************************************************************/
242 /*** glClear implementations ***/
243 /**********************************************************************/
244
245
246 static void
247 clear_front_pixmap( GLcontext *ctx, GLboolean all,
248 GLint x, GLint y, GLint width, GLint height )
249 {
250 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
251 if (all) {
252 XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->frontbuffer,
253 xmesa->xm_draw_buffer->cleargc,
254 0, 0,
255 xmesa->xm_draw_buffer->width+1,
256 xmesa->xm_draw_buffer->height+1 );
257 }
258 else {
259 XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->frontbuffer,
260 xmesa->xm_draw_buffer->cleargc,
261 x, xmesa->xm_draw_buffer->height - y - height,
262 width, height );
263 }
264 }
265
266
267 static void
268 clear_back_pixmap( GLcontext *ctx, GLboolean all,
269 GLint x, GLint y, GLint width, GLint height )
270 {
271 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
272 if (all) {
273 XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->backpixmap,
274 xmesa->xm_draw_buffer->cleargc,
275 0, 0,
276 xmesa->xm_draw_buffer->width+1,
277 xmesa->xm_draw_buffer->height+1 );
278 }
279 else {
280 XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->backpixmap,
281 xmesa->xm_draw_buffer->cleargc,
282 x, xmesa->xm_draw_buffer->height - y - height,
283 width, height );
284 }
285 }
286
287
288 static void
289 clear_8bit_ximage( GLcontext *ctx, GLboolean all,
290 GLint x, GLint y, GLint width, GLint height )
291 {
292 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
293 if (all) {
294 size_t n = xmesa->xm_draw_buffer->backimage->bytes_per_line
295 * xmesa->xm_draw_buffer->backimage->height;
296 MEMSET( xmesa->xm_draw_buffer->backimage->data, xmesa->clearpixel, n );
297 }
298 else {
299 GLint i;
300 for (i=0;i<height;i++) {
301 GLubyte *ptr = PIXELADDR1( xmesa->xm_draw_buffer, x, y+i );
302 MEMSET( ptr, xmesa->clearpixel, width );
303 }
304 }
305 }
306
307
308 static void
309 clear_HPCR_ximage( GLcontext *ctx, GLboolean all,
310 GLint x, GLint y, GLint width, GLint height )
311 {
312 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
313 if (all) {
314 GLint i, c16 = (xmesa->xm_draw_buffer->backimage->bytes_per_line>>4)<<4;
315 GLubyte *ptr = (GLubyte *)xmesa->xm_draw_buffer->backimage->data;
316 for (i=0; i<xmesa->xm_draw_buffer->backimage->height; i++) {
317 GLint j;
318 GLubyte *sptr = xmesa->xm_visual->hpcr_clear_ximage_pattern[0];
319 if (i&1) {
320 sptr += 16;
321 }
322 for (j=0; j<c16; j+=16) {
323 ptr[0] = sptr[0];
324 ptr[1] = sptr[1];
325 ptr[2] = sptr[2];
326 ptr[3] = sptr[3];
327 ptr[4] = sptr[4];
328 ptr[5] = sptr[5];
329 ptr[6] = sptr[6];
330 ptr[7] = sptr[7];
331 ptr[8] = sptr[8];
332 ptr[9] = sptr[9];
333 ptr[10] = sptr[10];
334 ptr[11] = sptr[11];
335 ptr[12] = sptr[12];
336 ptr[13] = sptr[13];
337 ptr[14] = sptr[14];
338 ptr[15] = sptr[15];
339 ptr += 16;
340 }
341 for (; j<xmesa->xm_draw_buffer->backimage->bytes_per_line; j++) {
342 *ptr = sptr[j&15];
343 ptr++;
344 }
345 }
346 }
347 else {
348 GLint i;
349 for (i=y; i<y+height; i++) {
350 GLubyte *ptr = PIXELADDR1( xmesa->xm_draw_buffer, x, i );
351 int j;
352 GLubyte *sptr = xmesa->xm_visual->hpcr_clear_ximage_pattern[0];
353 if (i&1) {
354 sptr += 16;
355 }
356 for (j=x; j<x+width; j++) {
357 *ptr = sptr[j&15];
358 ptr++;
359 }
360 }
361 }
362 }
363
364
365 static void
366 clear_16bit_ximage( GLcontext *ctx, GLboolean all,
367 GLint x, GLint y, GLint width, GLint height )
368 {
369 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
370 register GLuint pixel = (GLuint) xmesa->clearpixel;
371 if (xmesa->swapbytes) {
372 pixel = ((pixel >> 8) & 0x00ff) | ((pixel << 8) & 0xff00);
373 }
374 if (all) {
375 register GLuint n;
376 register GLuint *ptr4 = (GLuint *) xmesa->xm_draw_buffer->backimage->data;
377 if ((pixel & 0xff) == ((pixel >> 8) & 0xff)) {
378 /* low and high bytes are equal so use memset() */
379 n = xmesa->xm_draw_buffer->backimage->bytes_per_line
380 * xmesa->xm_draw_buffer->height;
381 MEMSET( ptr4, pixel & 0xff, n );
382 }
383 else {
384 pixel = pixel | (pixel<<16);
385 n = xmesa->xm_draw_buffer->backimage->bytes_per_line
386 * xmesa->xm_draw_buffer->height / 4;
387 do {
388 *ptr4++ = pixel;
389 n--;
390 } while (n!=0);
391
392 if ((xmesa->xm_draw_buffer->backimage->bytes_per_line *
393 xmesa->xm_draw_buffer->height) & 0x2)
394 *(GLushort *)ptr4 = pixel & 0xffff;
395 }
396 }
397 else {
398 register int i, j;
399 for (j=0;j<height;j++) {
400 register GLushort *ptr2 = PIXELADDR2( xmesa->xm_draw_buffer, x, y+j );
401 for (i=0;i<width;i++) {
402 *ptr2++ = pixel;
403 }
404 }
405 }
406 }
407
408
409 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
410 static void
411 clear_24bit_ximage( GLcontext *ctx, GLboolean all,
412 GLint x, GLint y, GLint width, GLint height )
413 {
414 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
415 const GLubyte r = xmesa->clearcolor[0];
416 const GLubyte g = xmesa->clearcolor[1];
417 const GLubyte b = xmesa->clearcolor[2];
418 #if 0 /* See below */
419 register GLuint clearPixel;
420 if (xmesa->swapbytes) {
421 clearPixel = (b << 16) | (g << 8) | r;
422 }
423 else {
424 clearPixel = (r << 16) | (g << 8) | b;
425 }
426 #endif
427
428 if (all) {
429 if (r==g && g==b) {
430 /* same value for all three components (gray) */
431 const GLint w3 = xmesa->xm_draw_buffer->width * 3;
432 const GLint h = xmesa->xm_draw_buffer->height;
433 GLint i;
434 for (i = 0; i < h; i++) {
435 bgr_t *ptr3 = PIXELADDR3(xmesa->xm_draw_buffer, 0, i);
436 MEMSET(ptr3, r, w3);
437 }
438 }
439 else {
440 /* the usual case */
441 const GLint w = xmesa->xm_draw_buffer->width;
442 const GLint h = xmesa->xm_draw_buffer->height;
443 GLint i, j;
444 for (i = 0; i < h; i++) {
445 bgr_t *ptr3 = PIXELADDR3(xmesa->xm_draw_buffer, 0, i);
446 for (j = 0; j < w; j++) {
447 ptr3->r = r;
448 ptr3->g = g;
449 ptr3->b = b;
450 ptr3++;
451 }
452 }
453 #if 0 /* this code doesn't work for all window widths */
454 register GLuint *ptr4 = (GLuint *) ptr3;
455 register GLuint px;
456 GLuint pixel4[3];
457 register GLuint *p = pixel4;
458 pixel4[0] = clearPixel | (clearPixel << 24);
459 pixel4[1] = (clearPixel << 16) | (clearPixel >> 8);
460 pixel4[2] = (clearPixel << 8) | (clearPixel >> 16);
461 switch (3 & (int)(ptr3 - (bgr_t*) ptr4)){
462 case 0:
463 break;
464 case 1:
465 px = *ptr4 & 0x00ffffff;
466 px |= pixel4[0] & 0xff000000;
467 *ptr4++ = px;
468 px = *ptr4 & 0xffff0000;
469 px |= pixel4[2] & 0x0000ffff;
470 *ptr4 = px;
471 if (0 == --n)
472 break;
473 case 2:
474 px = *ptr4 & 0x0000fffff;
475 px |= pixel4[1] & 0xffff0000;
476 *ptr4++ = px;
477 px = *ptr4 & 0xffffff00;
478 px |= pixel4[2] & 0x000000ff;
479 *ptr4 = px;
480 if (0 == --n)
481 break;
482 case 3:
483 px = *ptr4 & 0x000000ff;
484 px |= pixel4[2] & 0xffffff00;
485 *ptr4++ = px;
486 --n;
487 break;
488 }
489 while (n > 3) {
490 p = pixel4;
491 *ptr4++ = *p++;
492 *ptr4++ = *p++;
493 *ptr4++ = *p++;
494 n -= 4;
495 }
496 switch (n) {
497 case 3:
498 p = pixel4;
499 *ptr4++ = *p++;
500 *ptr4++ = *p++;
501 px = *ptr4 & 0xffffff00;
502 px |= clearPixel & 0xff;
503 *ptr4 = px;
504 break;
505 case 2:
506 p = pixel4;
507 *ptr4++ = *p++;
508 px = *ptr4 & 0xffff0000;
509 px |= *p & 0xffff;
510 *ptr4 = px;
511 break;
512 case 1:
513 px = *ptr4 & 0xff000000;
514 px |= *p & 0xffffff;
515 *ptr4 = px;
516 break;
517 case 0:
518 break;
519 }
520 #endif
521 }
522 }
523 else {
524 /* only clear subrect of color buffer */
525 if (r==g && g==b) {
526 /* same value for all three components (gray) */
527 GLint j;
528 for (j=0;j<height;j++) {
529 bgr_t *ptr3 = PIXELADDR3( xmesa->xm_draw_buffer, x, y+j );
530 MEMSET(ptr3, r, 3 * width);
531 }
532 }
533 else {
534 /* non-gray clear color */
535 GLint i, j;
536 for (j = 0; j < height; j++) {
537 bgr_t *ptr3 = PIXELADDR3( xmesa->xm_draw_buffer, x, y+j );
538 for (i = 0; i < width; i++) {
539 ptr3->r = r;
540 ptr3->g = g;
541 ptr3->b = b;
542 ptr3++;
543 }
544 }
545 #if 0 /* this code might not always (seems ptr3 always == ptr4) */
546 GLint j;
547 GLuint pixel4[3];
548 pixel4[0] = clearPixel | (clearPixel << 24);
549 pixel4[1] = (clearPixel << 16) | (clearPixel >> 8);
550 pixel4[2] = (clearPixel << 8) | (clearPixel >> 16);
551 for (j=0;j<height;j++) {
552 bgr_t *ptr3 = PIXELADDR3( xmesa->xm_draw_buffer, x, y+j );
553 register GLuint *ptr4 = (GLuint *)ptr3;
554 register GLuint *p, px;
555 GLuint w = width;
556 switch (3 & (int)(ptr3 - (bgr_t*) ptr4)){
557 case 0:
558 break;
559 case 1:
560 px = *ptr4 & 0x00ffffff;
561 px |= pixel4[0] & 0xff000000;
562 *ptr4++ = px;
563 px = *ptr4 & 0xffff0000;
564 px |= pixel4[2] & 0x0000ffff;
565 *ptr4 = px;
566 if (0 == --w)
567 break;
568 case 2:
569 px = *ptr4 & 0x0000fffff;
570 px |= pixel4[1] & 0xffff0000;
571 *ptr4++ = px;
572 px = *ptr4 & 0xffffff00;
573 px |= pixel4[2] & 0x000000ff;
574 *ptr4 = px;
575 if (0 == --w)
576 break;
577 case 3:
578 px = *ptr4 & 0x000000ff;
579 px |= pixel4[2] & 0xffffff00;
580 *ptr4++ = px;
581 --w;
582 break;
583 }
584 while (w > 3){
585 p = pixel4;
586 *ptr4++ = *p++;
587 *ptr4++ = *p++;
588 *ptr4++ = *p++;
589 w -= 4;
590 }
591 switch (w) {
592 case 3:
593 p = pixel4;
594 *ptr4++ = *p++;
595 *ptr4++ = *p++;
596 px = *ptr4 & 0xffffff00;
597 px |= *p & 0xff;
598 *ptr4 = px;
599 break;
600 case 2:
601 p = pixel4;
602 *ptr4++ = *p++;
603 px = *ptr4 & 0xffff0000;
604 px |= *p & 0xffff;
605 *ptr4 = px;
606 break;
607 case 1:
608 px = *ptr4 & 0xff000000;
609 px |= pixel4[0] & 0xffffff;
610 *ptr4 = px;
611 break;
612 case 0:
613 break;
614 }
615 }
616 #endif
617 }
618 }
619 }
620
621
622 static void
623 clear_32bit_ximage( GLcontext *ctx, GLboolean all,
624 GLint x, GLint y, GLint width, GLint height )
625 {
626 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
627 register GLuint pixel = (GLuint) xmesa->clearpixel;
628 if (xmesa->swapbytes) {
629 pixel = ((pixel >> 24) & 0x000000ff)
630 | ((pixel >> 8) & 0x0000ff00)
631 | ((pixel << 8) & 0x00ff0000)
632 | ((pixel << 24) & 0xff000000);
633 }
634 if (all) {
635 register GLint n = xmesa->xm_draw_buffer->width * xmesa->xm_draw_buffer->height;
636 register GLuint *ptr4 = (GLuint *) xmesa->xm_draw_buffer->backimage->data;
637 if (pixel==0) {
638 MEMSET( ptr4, pixel, 4*n );
639 }
640 else {
641 do {
642 *ptr4++ = pixel;
643 n--;
644 } while (n!=0);
645 }
646 }
647 else {
648 register int i, j;
649 for (j=0;j<height;j++) {
650 register GLuint *ptr4 = PIXELADDR4( xmesa->xm_draw_buffer, x, y+j );
651 for (i=0;i<width;i++) {
652 *ptr4++ = pixel;
653 }
654 }
655 }
656 }
657
658
659 static void
660 clear_nbit_ximage( GLcontext *ctx, GLboolean all,
661 GLint x, GLint y, GLint width, GLint height )
662 {
663 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
664 XMesaImage *img = xmesa->xm_draw_buffer->backimage;
665 if (all) {
666 register int i, j;
667 width = xmesa->xm_draw_buffer->width;
668 height = xmesa->xm_draw_buffer->height;
669 for (j=0;j<height;j++) {
670 for (i=0;i<width;i++) {
671 XMesaPutPixel( img, i, j, xmesa->clearpixel );
672 }
673 }
674 }
675 else {
676 /* TODO: optimize this */
677 register int i, j;
678 y = FLIP(xmesa->xm_draw_buffer, y);
679 for (j=0;j<height;j++) {
680 for (i=0;i<width;i++) {
681 XMesaPutPixel( img, x+i, y-j, xmesa->clearpixel );
682 }
683 }
684 }
685 }
686
687
688
689 static void
690 clear_buffers( GLcontext *ctx, GLbitfield mask,
691 GLboolean all, GLint x, GLint y, GLint width, GLint height )
692 {
693 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
694 const GLuint *colorMask = (GLuint *) &ctx->Color.ColorMask;
695
696 if ((mask & (DD_FRONT_LEFT_BIT | DD_BACK_LEFT_BIT)) &&
697 xmesa->xm_draw_buffer->mesa_buffer.UseSoftwareAlphaBuffers &&
698 ctx->Color.ColorMask[ACOMP]) {
699 _swrast_clear_alpha_buffers(ctx);
700 }
701
702 /* we can't handle color or index masking */
703 if (*colorMask == 0xffffffff && ctx->Color.IndexMask == 0xffffffff) {
704 if (mask & DD_FRONT_LEFT_BIT) {
705 ASSERT(xmesa->xm_draw_buffer->front_clear_func);
706 (*xmesa->xm_draw_buffer->front_clear_func)( ctx, all, x, y, width, height );
707 mask &= ~DD_FRONT_LEFT_BIT;
708 }
709 if (mask & DD_BACK_LEFT_BIT) {
710 ASSERT(xmesa->xm_draw_buffer->back_clear_func);
711 (*xmesa->xm_draw_buffer->back_clear_func)( ctx, all, x, y, width, height );
712 mask &= ~DD_BACK_LEFT_BIT;
713 }
714 }
715
716 if (mask)
717 _swrast_Clear( ctx, mask, all, x, y, width, height );
718 }
719
720
721 /*
722 * When we detect that the user has resized the window this function will
723 * get called. Here we'll reallocate the back buffer, depth buffer,
724 * stencil buffer etc. to match the new window size.
725 */
726 void
727 xmesa_resize_buffers( GLframebuffer *buffer )
728 {
729 int height = (int) buffer->Height;
730 /* We can do this cast because the first field in the XMesaBuffer
731 * struct is a GLframebuffer struct. If this weren't true, we'd
732 * need a pointer from the GLframebuffer to the XMesaBuffer.
733 */
734 XMesaBuffer xmBuffer = (XMesaBuffer) buffer;
735
736 xmBuffer->width = buffer->Width;
737 xmBuffer->height = buffer->Height;
738 xmesa_alloc_back_buffer( xmBuffer );
739
740 /* Needed by FLIP macro */
741 xmBuffer->bottom = height - 1;
742
743 if (xmBuffer->backimage) {
744 /* Needed by PIXELADDR1 macro */
745 xmBuffer->ximage_width1 = xmBuffer->backimage->bytes_per_line;
746 xmBuffer->ximage_origin1 = (GLubyte *) xmBuffer->backimage->data
747 + xmBuffer->ximage_width1 * (height-1);
748
749 /* Needed by PIXELADDR2 macro */
750 xmBuffer->ximage_width2 = xmBuffer->backimage->bytes_per_line / 2;
751 xmBuffer->ximage_origin2 = (GLushort *) xmBuffer->backimage->data
752 + xmBuffer->ximage_width2 * (height-1);
753
754 /* Needed by PIXELADDR3 macro */
755 xmBuffer->ximage_width3 = xmBuffer->backimage->bytes_per_line;
756 xmBuffer->ximage_origin3 = (GLubyte *) xmBuffer->backimage->data
757 + xmBuffer->ximage_width3 * (height-1);
758
759 /* Needed by PIXELADDR4 macro */
760 xmBuffer->ximage_width4 = xmBuffer->backimage->width;
761 xmBuffer->ximage_origin4 = (GLuint *) xmBuffer->backimage->data
762 + xmBuffer->ximage_width4 * (height-1);
763 }
764
765 _swrast_alloc_buffers( buffer );
766 }
767
768
769 #ifndef XFree86Server
770 /* XXX this was never tested in the Xserver environment */
771
772 /**
773 * This function implements glDrawPixels() with an XPutImage call when
774 * drawing to the front buffer (X Window drawable).
775 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
776 */
777 static void
778 xmesa_DrawPixels_8R8G8B( GLcontext *ctx,
779 GLint x, GLint y, GLsizei width, GLsizei height,
780 GLenum format, GLenum type,
781 const struct gl_pixelstore_attrib *unpack,
782 const GLvoid *pixels )
783 {
784 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
785 const SWcontext *swrast = SWRAST_CONTEXT( ctx );
786 XMesaDisplay *dpy = xmesa->xm_visual->display;
787 const XMesaDrawable buffer = xmesa->xm_draw_buffer->buffer;
788 const XMesaGC gc = xmesa->xm_draw_buffer->gc;
789
790 ASSERT(dpy);
791 ASSERT(gc);
792 ASSERT(xmesa->xm_visual->dithered_pf == PF_8R8G8B);
793 ASSERT(xmesa->xm_visual->undithered_pf == PF_8R8G8B);
794
795 if (swrast->NewState)
796 _swrast_validate_derived( ctx );
797
798 if (buffer && /* buffer != 0 means it's a Window or Pixmap */
799 format == GL_BGRA &&
800 type == GL_UNSIGNED_BYTE &&
801 (swrast->_RasterMask & ~CLIP_BIT) == 0 && /* no blend, z-test, etc */
802 ctx->_ImageTransferState == 0 && /* no color tables, scale/bias, etc */
803 ctx->Pixel.ZoomX == 1.0 && /* no zooming */
804 ctx->Pixel.ZoomY == 1.0) {
805 int dstX = x;
806 int dstY = y;
807 int w = width;
808 int h = height;
809 int srcX = unpack->SkipPixels;
810 int srcY = unpack->SkipRows;
811 int rowLength = unpack->RowLength ? unpack->RowLength : width;
812 if (_swrast_clip_pixelrect(ctx, &dstX, &dstY, &w, &h, &srcX, &srcY)) {
813 /* This is a little tricky since all coordinates up to now have
814 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
815 * so we have to carefully compute the Y coordinates/addresses here.
816 */
817 XMesaImage ximage;
818 MEMSET(&ximage, 0, sizeof(XMesaImage));
819 ximage.width = width;
820 ximage.height = height;
821 ximage.format = ZPixmap;
822 ximage.data = (char *) pixels
823 + ((srcY + h - 1) * rowLength + srcX) * 4;
824 ximage.byte_order = LSBFirst;
825 ximage.bitmap_unit = 32;
826 ximage.bitmap_bit_order = LSBFirst;
827 ximage.bitmap_pad = 32;
828 ximage.depth = 24;
829 ximage.bytes_per_line = -rowLength * 4; /* negative to flip image */
830 ximage.bits_per_pixel = 32;
831 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
832 /* flip Y axis for dest position */
833 dstY = FLIP(xmesa->xm_draw_buffer, dstY) - h + 1;
834 XPutImage(dpy, buffer, gc, &ximage, 0, 0, dstX, dstY, w, h);
835 }
836 }
837 else {
838 /* software fallback */
839 _swrast_DrawPixels(ctx, x, y, width, height,
840 format, type, unpack, pixels);
841 }
842 }
843
844
845
846 /**
847 * This function implements glDrawPixels() with an XPutImage call when
848 * drawing to the front buffer (X Window drawable). The image format
849 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
850 * match the PF_5R6G5B pixel format.
851 */
852 static void
853 xmesa_DrawPixels_5R6G5B( GLcontext *ctx,
854 GLint x, GLint y, GLsizei width, GLsizei height,
855 GLenum format, GLenum type,
856 const struct gl_pixelstore_attrib *unpack,
857 const GLvoid *pixels )
858 {
859 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
860 const SWcontext *swrast = SWRAST_CONTEXT( ctx );
861 XMesaDisplay *dpy = xmesa->xm_visual->display;
862 const XMesaDrawable buffer = xmesa->xm_draw_buffer->buffer;
863 const XMesaGC gc = xmesa->xm_draw_buffer->gc;
864
865 ASSERT(dpy);
866 ASSERT(gc);
867 ASSERT(xmesa->xm_visual->undithered_pf == PF_5R6G5B);
868
869 if (swrast->NewState)
870 _swrast_validate_derived( ctx );
871
872 if (buffer && /* buffer != 0 means it's a Window or Pixmap */
873 format == GL_RGB &&
874 type == GL_UNSIGNED_SHORT_5_6_5 &&
875 !ctx->Color.DitherFlag && /* no dithering */
876 (swrast->_RasterMask & ~CLIP_BIT) == 0 && /* no blend, z-test, etc */
877 ctx->_ImageTransferState == 0 && /* no color tables, scale/bias, etc */
878 ctx->Pixel.ZoomX == 1.0 && /* no zooming */
879 ctx->Pixel.ZoomY == 1.0) {
880 int dstX = x;
881 int dstY = y;
882 int w = width;
883 int h = height;
884 int srcX = unpack->SkipPixels;
885 int srcY = unpack->SkipRows;
886 int rowLength = unpack->RowLength ? unpack->RowLength : width;
887 if (_swrast_clip_pixelrect(ctx, &dstX, &dstY, &w, &h, &srcX, &srcY)) {
888 /* This is a little tricky since all coordinates up to now have
889 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
890 * so we have to carefully compute the Y coordinates/addresses here.
891 */
892 XMesaImage ximage;
893 MEMSET(&ximage, 0, sizeof(XMesaImage));
894 ximage.width = width;
895 ximage.height = height;
896 ximage.format = ZPixmap;
897 ximage.data = (char *) pixels
898 + ((srcY + h - 1) * rowLength + srcX) * 2;
899 ximage.byte_order = LSBFirst;
900 ximage.bitmap_unit = 16;
901 ximage.bitmap_bit_order = LSBFirst;
902 ximage.bitmap_pad = 16;
903 ximage.depth = 16;
904 ximage.bytes_per_line = -rowLength * 2; /* negative to flip image */
905 ximage.bits_per_pixel = 16;
906 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
907 /* flip Y axis for dest position */
908 dstY = FLIP(xmesa->xm_draw_buffer, dstY) - h + 1;
909 XPutImage(dpy, buffer, gc, &ximage, 0, 0, dstX, dstY, w, h);
910 }
911 }
912 else {
913 /* software fallback */
914 _swrast_DrawPixels(ctx, x, y, width, height,
915 format, type, unpack, pixels);
916 }
917 }
918
919
920
921 /**
922 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
923 * for the color buffer. Don't support zooming, pixel transfer, etc.
924 * We do support copying from one window to another, ala glXMakeCurrentRead.
925 */
926 static void
927 xmesa_CopyPixels( GLcontext *ctx,
928 GLint srcx, GLint srcy, GLsizei width, GLsizei height,
929 GLint destx, GLint desty, GLenum type )
930 {
931 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
932 const SWcontext *swrast = SWRAST_CONTEXT( ctx );
933 XMesaDisplay *dpy = xmesa->xm_visual->display;
934 const XMesaDrawable drawBuffer = xmesa->xm_draw_buffer->buffer;
935 const XMesaDrawable readBuffer = xmesa->xm_read_buffer->buffer;
936 const XMesaGC gc = xmesa->xm_draw_buffer->gc;
937
938 ASSERT(dpy);
939 ASSERT(gc);
940
941 if (swrast->NewState)
942 _swrast_validate_derived( ctx );
943
944 if (ctx->Color.DrawBuffer == GL_FRONT &&
945 ctx->Pixel.ReadBuffer == GL_FRONT &&
946 drawBuffer && /* buffer != 0 means it's a Window or Pixmap */
947 readBuffer &&
948 type == GL_COLOR &&
949 (swrast->_RasterMask & ~CLIP_BIT) == 0 && /* no blend, z-test, etc */
950 ctx->_ImageTransferState == 0 && /* no color tables, scale/bias, etc */
951 ctx->Pixel.ZoomX == 1.0 && /* no zooming */
952 ctx->Pixel.ZoomY == 1.0) {
953 /* Note: we don't do any special clipping work here. We could,
954 * but X will do it for us.
955 */
956 srcy = FLIP(xmesa->xm_read_buffer, srcy) - height + 1;
957 desty = FLIP(xmesa->xm_draw_buffer, desty) - height + 1;
958 XCopyArea(dpy, readBuffer, drawBuffer, gc,
959 srcx, srcy, width, height, destx, desty);
960 }
961 else {
962 _swrast_CopyPixels(ctx, srcx, srcy, width, height, destx, desty, type );
963 }
964 }
965 #endif /* XFree86Server */
966
967
968
969 /*
970 * Every driver should implement a GetString function in order to
971 * return a meaningful GL_RENDERER string.
972 */
973 static const GLubyte *
974 get_string( GLcontext *ctx, GLenum name )
975 {
976 (void) ctx;
977 switch (name) {
978 case GL_RENDERER:
979 #ifdef XFree86Server
980 return (const GLubyte *) "Mesa GLX Indirect";
981 #else
982 return (const GLubyte *) "Mesa X11";
983 #endif
984 case GL_VENDOR:
985 #ifdef XFree86Server
986 return (const GLubyte *) "Mesa project: www.mesa3d.org";
987 #else
988 return NULL;
989 #endif
990 default:
991 return NULL;
992 }
993 }
994
995
996 /*
997 * We implement the glEnable function only because we care about
998 * dither enable/disable.
999 */
1000 static void
1001 enable( GLcontext *ctx, GLenum pname, GLboolean state )
1002 {
1003 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
1004
1005 switch (pname) {
1006 case GL_DITHER:
1007 if (state)
1008 xmesa->pixelformat = xmesa->xm_visual->dithered_pf;
1009 else
1010 xmesa->pixelformat = xmesa->xm_visual->undithered_pf;
1011 break;
1012 default:
1013 ; /* silence compiler warning */
1014 }
1015 }
1016
1017
1018 void xmesa_update_state( GLcontext *ctx, GLuint new_state )
1019 {
1020 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
1021
1022 /* Propogate statechange information to swrast and swrast_setup
1023 * modules. The X11 driver has no internal GL-dependent state.
1024 */
1025 _swrast_InvalidateState( ctx, new_state );
1026 _ac_InvalidateState( ctx, new_state );
1027 _tnl_InvalidateState( ctx, new_state );
1028 _swsetup_InvalidateState( ctx, new_state );
1029
1030
1031 /* setup pointers to front and back buffer clear functions */
1032 xmesa->xm_draw_buffer->front_clear_func = clear_front_pixmap;
1033 if (xmesa->xm_draw_buffer->backpixmap != XIMAGE) {
1034 xmesa->xm_draw_buffer->back_clear_func = clear_back_pixmap;
1035 }
1036 else if (sizeof(GLushort)!=2 || sizeof(GLuint)!=4) {
1037 xmesa->xm_draw_buffer->back_clear_func = clear_nbit_ximage;
1038 }
1039 else switch (xmesa->xm_visual->BitsPerPixel) {
1040 case 8:
1041 if (xmesa->xm_visual->hpcr_clear_flag) {
1042 xmesa->xm_draw_buffer->back_clear_func = clear_HPCR_ximage;
1043 }
1044 else {
1045 xmesa->xm_draw_buffer->back_clear_func = clear_8bit_ximage;
1046 }
1047 break;
1048 case 16:
1049 xmesa->xm_draw_buffer->back_clear_func = clear_16bit_ximage;
1050 break;
1051 case 24:
1052 xmesa->xm_draw_buffer->back_clear_func = clear_24bit_ximage;
1053 break;
1054 case 32:
1055 xmesa->xm_draw_buffer->back_clear_func = clear_32bit_ximage;
1056 break;
1057 default:
1058 xmesa->xm_draw_buffer->back_clear_func = clear_nbit_ximage;
1059 break;
1060 }
1061
1062 xmesa_update_span_funcs(ctx);
1063 }
1064
1065
1066
1067 /**
1068 * Called via ctx->Driver.TestProxyTeximage(). Normally, we'd just use
1069 * the _mesa_test_proxy_teximage() fallback function, but we're going to
1070 * special-case the 3D texture case to allow textures up to 512x512x32
1071 * texels.
1072 */
1073 static GLboolean
1074 test_proxy_teximage(GLcontext *ctx, GLenum target, GLint level,
1075 GLint internalFormat, GLenum format, GLenum type,
1076 GLint width, GLint height, GLint depth, GLint border)
1077 {
1078 if (target == GL_PROXY_TEXTURE_3D) {
1079 /* special case for 3D textures */
1080 if (width * height * depth > 512 * 512 * 64 ||
1081 width < 2 * border ||
1082 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1083 _mesa_bitcount(width - 2 * border) != 1) ||
1084 height < 2 * border ||
1085 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1086 _mesa_bitcount(height - 2 * border) != 1) ||
1087 depth < 2 * border ||
1088 (!ctx->Extensions.ARB_texture_non_power_of_two &&
1089 _mesa_bitcount(depth - 2 * border) != 1)) {
1090 /* Bad size, or too many texels */
1091 return GL_FALSE;
1092 }
1093 return GL_TRUE;
1094 }
1095 else {
1096 /* use the fallback routine for 1D, 2D, cube and rect targets */
1097 return _mesa_test_proxy_teximage(ctx, target, level, internalFormat,
1098 format, type, width, height, depth,
1099 border);
1100 }
1101 }
1102
1103
1104 /**
1105 * Initialize the device driver function table with the functions
1106 * we implement in this driver.
1107 */
1108 void xmesa_init_driver_functions( XMesaVisual xmvisual,
1109 struct dd_function_table *driver )
1110 {
1111 driver->GetString = get_string;
1112 driver->UpdateState = xmesa_update_state;
1113 driver->GetBufferSize = get_buffer_size;
1114 driver->Flush = finish_or_flush;
1115 driver->Finish = finish_or_flush;
1116 driver->ClearIndex = clear_index;
1117 driver->ClearColor = clear_color;
1118 driver->IndexMask = index_mask;
1119 driver->ColorMask = color_mask;
1120 driver->Enable = enable;
1121 driver->Clear = clear_buffers;
1122 driver->ResizeBuffers = xmesa_resize_buffers;
1123 #ifndef XFree86Server
1124 driver->CopyPixels = /*_swrast_CopyPixels;*/xmesa_CopyPixels;
1125 if (xmvisual->undithered_pf == PF_8R8G8B &&
1126 xmvisual->dithered_pf == PF_8R8G8B) {
1127 driver->DrawPixels = xmesa_DrawPixels_8R8G8B;
1128 }
1129 else if (xmvisual->undithered_pf == PF_5R6G5B) {
1130 driver->DrawPixels = xmesa_DrawPixels_5R6G5B;
1131 }
1132 #endif
1133 driver->TestProxyTexImage = test_proxy_teximage;
1134 }
1135
1136
1137 #define XMESA_NEW_POINT (_NEW_POINT | \
1138 _NEW_RENDERMODE | \
1139 _SWRAST_NEW_RASTERMASK)
1140
1141 #define XMESA_NEW_LINE (_NEW_LINE | \
1142 _NEW_TEXTURE | \
1143 _NEW_LIGHT | \
1144 _NEW_DEPTH | \
1145 _NEW_RENDERMODE | \
1146 _SWRAST_NEW_RASTERMASK)
1147
1148 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1149 _NEW_TEXTURE | \
1150 _NEW_LIGHT | \
1151 _NEW_DEPTH | \
1152 _NEW_RENDERMODE | \
1153 _SWRAST_NEW_RASTERMASK)
1154
1155
1156 /* Extend the software rasterizer with our line/point/triangle
1157 * functions.
1158 */
1159 void xmesa_register_swrast_functions( GLcontext *ctx )
1160 {
1161 SWcontext *swrast = SWRAST_CONTEXT( ctx );
1162 struct swrast_device_driver *dd = _swrast_GetDeviceDriverReference(ctx);
1163
1164 dd->SetBuffer = xmesa_set_buffer;
1165
1166 swrast->choose_point = xmesa_choose_point;
1167 swrast->choose_line = xmesa_choose_line;
1168 swrast->choose_triangle = xmesa_choose_triangle;
1169
1170 swrast->invalidate_point |= XMESA_NEW_POINT;
1171 swrast->invalidate_line |= XMESA_NEW_LINE;
1172 swrast->invalidate_triangle |= XMESA_NEW_TRIANGLE;
1173 }