bdf0b66886def6daf22c735c2775ed5704c4dcfd
[mesa.git] / src / mesa / drivers / x11 / xm_dd.c
1 /* $Id: xm_dd.c,v 1.43 2003/03/25 02:26:30 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 4.0.3
6 *
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26 /* $XFree86: xc/extras/Mesa/src/X/xm_dd.c,v 1.2 2002/02/26 23:37:31 tsi Exp $ */
27
28 #include "glxheader.h"
29 #include "context.h"
30 #include "colormac.h"
31 #include "depth.h"
32 #include "drawpix.h"
33 #include "extensions.h"
34 #include "macros.h"
35 #include "imports.h"
36 #include "mtypes.h"
37 #include "state.h"
38 #include "texobj.h"
39 #include "texstore.h"
40 #include "texformat.h"
41 #include "xmesaP.h"
42 #include "array_cache/acache.h"
43 #include "swrast/s_context.h"
44 #include "swrast/swrast.h"
45 #include "swrast/s_alphabuf.h"
46 #include "swrast_setup/swrast_setup.h"
47 #include "tnl/tnl.h"
48 #include "tnl/t_context.h"
49 #include "tnl/t_pipeline.h"
50
51
52 /*
53 * Return the size (width, height) of the X window for the given GLframebuffer.
54 * Output: width - width of buffer in pixels.
55 * height - height of buffer in pixels.
56 */
57 static void
58 get_buffer_size( GLframebuffer *buffer, GLuint *width, GLuint *height )
59 {
60 /* We can do this cast because the first field in the XMesaBuffer
61 * struct is a GLframebuffer struct. If this weren't true, we'd
62 * need a pointer from the GLframebuffer to the XMesaBuffer.
63 */
64 const XMesaBuffer xmBuffer = (XMesaBuffer) buffer;
65 unsigned int winwidth, winheight;
66 #ifdef XFree86Server
67 /* XFree86 GLX renderer */
68 if (xmBuffer->frontbuffer->width > MAX_WIDTH ||
69 xmBuffer->frontbuffer->height > MAX_HEIGHT) {
70 winwidth = buffer->Width;
71 winheight = buffer->Height;
72 } else {
73 winwidth = xmBuffer->frontbuffer->width;
74 winheight = xmBuffer->frontbuffer->height;
75 }
76 #else
77 Window root;
78 int winx, winy;
79 unsigned int bw, d;
80
81 _glthread_LOCK_MUTEX(_xmesa_lock);
82 XSync(xmBuffer->xm_visual->display, 0); /* added for Chromium */
83 XGetGeometry( xmBuffer->xm_visual->display, xmBuffer->frontbuffer, &root,
84 &winx, &winy, &winwidth, &winheight, &bw, &d );
85 _glthread_UNLOCK_MUTEX(_xmesa_lock);
86 #endif
87
88 (void)kernel8; /* Muffle compiler */
89
90 *width = winwidth;
91 *height = winheight;
92 }
93
94
95 static void
96 finish( GLcontext *ctx )
97 {
98 #ifdef XFree86Server
99 /* NOT_NEEDED */
100 #else
101 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
102 if (xmesa) {
103 _glthread_LOCK_MUTEX(_xmesa_lock);
104 XSync( xmesa->display, False );
105 _glthread_UNLOCK_MUTEX(_xmesa_lock);
106 }
107 #endif
108 }
109
110
111 static void
112 flush( GLcontext *ctx )
113 {
114 #ifdef XFree86Server
115 /* NOT_NEEDED */
116 #else
117 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
118 if (xmesa) {
119 _glthread_LOCK_MUTEX(_xmesa_lock);
120 XFlush( xmesa->display );
121 _glthread_UNLOCK_MUTEX(_xmesa_lock);
122 }
123 #endif
124 }
125
126
127
128 /*
129 * This chooses the color buffer for reading and writing spans, points,
130 * lines, and triangles.
131 */
132 static void
133 set_buffer( GLcontext *ctx, GLframebuffer *buffer, GLuint bufferBit )
134 {
135 /* We can make this cast since the XMesaBuffer wraps GLframebuffer.
136 * GLframebuffer is the first member in a XMesaBuffer struct.
137 */
138 XMesaBuffer target = (XMesaBuffer) buffer;
139 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
140
141 /* This assignment tells the span/point/line/triangle functions
142 * which XMesaBuffer to use.
143 */
144 xmesa->xm_buffer = target;
145
146 /*
147 * Now determine front vs back color buffer.
148 */
149 if (bufferBit == FRONT_LEFT_BIT) {
150 target->buffer = target->frontbuffer;
151 }
152 else if (bufferBit == BACK_LEFT_BIT) {
153 ASSERT(target->db_state);
154 if (target->backpixmap) {
155 /* back buffer is a pixmape */
156 target->buffer = target->backpixmap; /* incompatible types? */
157 }
158 else if (target->backimage) {
159 /* back buffer is an XImage */
160 target->buffer = None;
161 }
162 else {
163 /* No back buffer!!!! Must be out of memory, use front buffer */
164 target->buffer = target->frontbuffer;
165 }
166 }
167 else {
168 _mesa_problem(ctx, "invalid buffer 0x%x in set_buffer() in xm_dd.c");
169 return;
170 }
171 xmesa_update_span_funcs(ctx);
172 }
173
174
175
176 static void
177 clear_index( GLcontext *ctx, GLuint index )
178 {
179 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
180 xmesa->clearpixel = (unsigned long) index;
181 XMesaSetForeground( xmesa->display, xmesa->xm_draw_buffer->cleargc,
182 (unsigned long) index );
183 }
184
185
186 static void
187 clear_color( GLcontext *ctx, const GLfloat color[4] )
188 {
189 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
190 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[0], color[0]);
191 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[1], color[1]);
192 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[2], color[2]);
193 CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[3], color[3]);
194 xmesa->clearpixel = xmesa_color_to_pixel( xmesa,
195 xmesa->clearcolor[0],
196 xmesa->clearcolor[1],
197 xmesa->clearcolor[2],
198 xmesa->clearcolor[3],
199 xmesa->xm_visual->undithered_pf );
200 _glthread_LOCK_MUTEX(_xmesa_lock);
201 XMesaSetForeground( xmesa->display, xmesa->xm_draw_buffer->cleargc,
202 xmesa->clearpixel );
203 _glthread_UNLOCK_MUTEX(_xmesa_lock);
204 }
205
206
207
208 /* Set index mask ala glIndexMask */
209 static void
210 index_mask( GLcontext *ctx, GLuint mask )
211 {
212 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
213 if (xmesa->xm_draw_buffer->buffer != XIMAGE) {
214 unsigned long m;
215 if (mask==0xffffffff) {
216 m = ((unsigned long)~0L);
217 }
218 else {
219 m = (unsigned long) mask;
220 }
221 XMesaSetPlaneMask( xmesa->display, xmesa->xm_draw_buffer->cleargc, m );
222 XMesaSetPlaneMask( xmesa->display, xmesa->xm_draw_buffer->gc, m );
223 }
224 }
225
226
227 /* Implements glColorMask() */
228 static void
229 color_mask(GLcontext *ctx,
230 GLboolean rmask, GLboolean gmask, GLboolean bmask, GLboolean amask)
231 {
232 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
233 int xclass = GET_VISUAL_CLASS(xmesa->xm_visual);
234 (void) amask;
235
236 if (xclass == TrueColor || xclass == DirectColor) {
237 unsigned long m;
238 if (rmask && gmask && bmask) {
239 m = ((unsigned long)~0L);
240 }
241 else {
242 m = 0;
243 if (rmask) m |= GET_REDMASK(xmesa->xm_visual);
244 if (gmask) m |= GET_GREENMASK(xmesa->xm_visual);
245 if (bmask) m |= GET_BLUEMASK(xmesa->xm_visual);
246 }
247 XMesaSetPlaneMask( xmesa->display, xmesa->xm_draw_buffer->cleargc, m );
248 }
249 }
250
251
252
253 /**********************************************************************/
254 /*** glClear implementations ***/
255 /**********************************************************************/
256
257
258 static void
259 clear_front_pixmap( GLcontext *ctx, GLboolean all,
260 GLint x, GLint y, GLint width, GLint height )
261 {
262 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
263 if (all) {
264 XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->frontbuffer,
265 xmesa->xm_draw_buffer->cleargc,
266 0, 0,
267 xmesa->xm_draw_buffer->width+1,
268 xmesa->xm_draw_buffer->height+1 );
269 }
270 else {
271 XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->frontbuffer,
272 xmesa->xm_draw_buffer->cleargc,
273 x, xmesa->xm_draw_buffer->height - y - height,
274 width, height );
275 }
276 }
277
278
279 static void
280 clear_back_pixmap( GLcontext *ctx, GLboolean all,
281 GLint x, GLint y, GLint width, GLint height )
282 {
283 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
284 if (all) {
285 XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->backpixmap,
286 xmesa->xm_draw_buffer->cleargc,
287 0, 0,
288 xmesa->xm_draw_buffer->width+1,
289 xmesa->xm_draw_buffer->height+1 );
290 }
291 else {
292 XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->backpixmap,
293 xmesa->xm_draw_buffer->cleargc,
294 x, xmesa->xm_draw_buffer->height - y - height,
295 width, height );
296 }
297 }
298
299
300 static void
301 clear_8bit_ximage( GLcontext *ctx, GLboolean all,
302 GLint x, GLint y, GLint width, GLint height )
303 {
304 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
305 if (all) {
306 size_t n = xmesa->xm_draw_buffer->backimage->bytes_per_line
307 * xmesa->xm_draw_buffer->backimage->height;
308 MEMSET( xmesa->xm_draw_buffer->backimage->data, xmesa->clearpixel, n );
309 }
310 else {
311 GLint i;
312 for (i=0;i<height;i++) {
313 GLubyte *ptr = PIXELADDR1( xmesa->xm_draw_buffer, x, y+i );
314 MEMSET( ptr, xmesa->clearpixel, width );
315 }
316 }
317 }
318
319
320 static void
321 clear_HPCR_ximage( GLcontext *ctx, GLboolean all,
322 GLint x, GLint y, GLint width, GLint height )
323 {
324 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
325 if (all) {
326 GLint i, c16 = (xmesa->xm_draw_buffer->backimage->bytes_per_line>>4)<<4;
327 GLubyte *ptr = (GLubyte *)xmesa->xm_draw_buffer->backimage->data;
328 for (i=0; i<xmesa->xm_draw_buffer->backimage->height; i++) {
329 GLint j;
330 GLubyte *sptr = xmesa->xm_visual->hpcr_clear_ximage_pattern[0];
331 if (i&1) {
332 sptr += 16;
333 }
334 for (j=0; j<c16; j+=16) {
335 ptr[0] = sptr[0];
336 ptr[1] = sptr[1];
337 ptr[2] = sptr[2];
338 ptr[3] = sptr[3];
339 ptr[4] = sptr[4];
340 ptr[5] = sptr[5];
341 ptr[6] = sptr[6];
342 ptr[7] = sptr[7];
343 ptr[8] = sptr[8];
344 ptr[9] = sptr[9];
345 ptr[10] = sptr[10];
346 ptr[11] = sptr[11];
347 ptr[12] = sptr[12];
348 ptr[13] = sptr[13];
349 ptr[14] = sptr[14];
350 ptr[15] = sptr[15];
351 ptr += 16;
352 }
353 for (; j<xmesa->xm_draw_buffer->backimage->bytes_per_line; j++) {
354 *ptr = sptr[j&15];
355 ptr++;
356 }
357 }
358 }
359 else {
360 GLint i;
361 for (i=y; i<y+height; i++) {
362 GLubyte *ptr = PIXELADDR1( xmesa->xm_draw_buffer, x, i );
363 int j;
364 GLubyte *sptr = xmesa->xm_visual->hpcr_clear_ximage_pattern[0];
365 if (i&1) {
366 sptr += 16;
367 }
368 for (j=x; j<x+width; j++) {
369 *ptr = sptr[j&15];
370 ptr++;
371 }
372 }
373 }
374 }
375
376
377 static void
378 clear_16bit_ximage( GLcontext *ctx, GLboolean all,
379 GLint x, GLint y, GLint width, GLint height )
380 {
381 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
382 register GLuint pixel = (GLuint) xmesa->clearpixel;
383 if (xmesa->swapbytes) {
384 pixel = ((pixel >> 8) & 0x00ff) | ((pixel << 8) & 0xff00);
385 }
386 if (all) {
387 register GLuint n;
388 register GLuint *ptr4 = (GLuint *) xmesa->xm_draw_buffer->backimage->data;
389 if ((pixel & 0xff) == ((pixel >> 8) & 0xff)) {
390 /* low and high bytes are equal so use memset() */
391 n = xmesa->xm_draw_buffer->backimage->bytes_per_line
392 * xmesa->xm_draw_buffer->height;
393 MEMSET( ptr4, pixel & 0xff, n );
394 }
395 else {
396 pixel = pixel | (pixel<<16);
397 n = xmesa->xm_draw_buffer->backimage->bytes_per_line
398 * xmesa->xm_draw_buffer->height / 4;
399 do {
400 *ptr4++ = pixel;
401 n--;
402 } while (n!=0);
403
404 if ((xmesa->xm_draw_buffer->backimage->bytes_per_line *
405 xmesa->xm_draw_buffer->height) & 0x2)
406 *(GLushort *)ptr4 = pixel & 0xffff;
407 }
408 }
409 else {
410 register int i, j;
411 for (j=0;j<height;j++) {
412 register GLushort *ptr2 = PIXELADDR2( xmesa->xm_draw_buffer, x, y+j );
413 for (i=0;i<width;i++) {
414 *ptr2++ = pixel;
415 }
416 }
417 }
418 }
419
420
421 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
422 static void
423 clear_24bit_ximage( GLcontext *ctx, GLboolean all,
424 GLint x, GLint y, GLint width, GLint height )
425 {
426 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
427 const GLubyte r = xmesa->clearcolor[0];
428 const GLubyte g = xmesa->clearcolor[1];
429 const GLubyte b = xmesa->clearcolor[2];
430 register GLuint clearPixel;
431 if (xmesa->swapbytes) {
432 clearPixel = (b << 16) | (g << 8) | r;
433 }
434 else {
435 clearPixel = (r << 16) | (g << 8) | b;
436 }
437
438 if (all) {
439 if (r==g && g==b) {
440 /* same value for all three components (gray) */
441 const GLint w3 = xmesa->xm_draw_buffer->width * 3;
442 const GLint h = xmesa->xm_draw_buffer->height;
443 GLint i;
444 for (i = 0; i < h; i++) {
445 bgr_t *ptr3 = PIXELADDR3(xmesa->xm_draw_buffer, 0, i);
446 MEMSET(ptr3, r, w3);
447 }
448 }
449 else {
450 /* the usual case */
451 const GLint w = xmesa->xm_draw_buffer->width;
452 const GLint h = xmesa->xm_draw_buffer->height;
453 GLint i, j;
454 for (i = 0; i < h; i++) {
455 bgr_t *ptr3 = PIXELADDR3(xmesa->xm_draw_buffer, 0, i);
456 for (j = 0; j < w; j++) {
457 ptr3->r = r;
458 ptr3->g = g;
459 ptr3->b = b;
460 ptr3++;
461 }
462 }
463 #if 0 /* this code doesn't work for all window widths */
464 register GLuint *ptr4 = (GLuint *) ptr3;
465 register GLuint px;
466 GLuint pixel4[3];
467 register GLuint *p = pixel4;
468 pixel4[0] = clearPixel | (clearPixel << 24);
469 pixel4[1] = (clearPixel << 16) | (clearPixel >> 8);
470 pixel4[2] = (clearPixel << 8) | (clearPixel >> 16);
471 switch (3 & (int)(ptr3 - (bgr_t*) ptr4)){
472 case 0:
473 break;
474 case 1:
475 px = *ptr4 & 0x00ffffff;
476 px |= pixel4[0] & 0xff000000;
477 *ptr4++ = px;
478 px = *ptr4 & 0xffff0000;
479 px |= pixel4[2] & 0x0000ffff;
480 *ptr4 = px;
481 if (0 == --n)
482 break;
483 case 2:
484 px = *ptr4 & 0x0000fffff;
485 px |= pixel4[1] & 0xffff0000;
486 *ptr4++ = px;
487 px = *ptr4 & 0xffffff00;
488 px |= pixel4[2] & 0x000000ff;
489 *ptr4 = px;
490 if (0 == --n)
491 break;
492 case 3:
493 px = *ptr4 & 0x000000ff;
494 px |= pixel4[2] & 0xffffff00;
495 *ptr4++ = px;
496 --n;
497 break;
498 }
499 while (n > 3) {
500 p = pixel4;
501 *ptr4++ = *p++;
502 *ptr4++ = *p++;
503 *ptr4++ = *p++;
504 n -= 4;
505 }
506 switch (n) {
507 case 3:
508 p = pixel4;
509 *ptr4++ = *p++;
510 *ptr4++ = *p++;
511 px = *ptr4 & 0xffffff00;
512 px |= clearPixel & 0xff;
513 *ptr4 = px;
514 break;
515 case 2:
516 p = pixel4;
517 *ptr4++ = *p++;
518 px = *ptr4 & 0xffff0000;
519 px |= *p & 0xffff;
520 *ptr4 = px;
521 break;
522 case 1:
523 px = *ptr4 & 0xff000000;
524 px |= *p & 0xffffff;
525 *ptr4 = px;
526 break;
527 case 0:
528 break;
529 }
530 #endif
531 }
532 }
533 else {
534 /* only clear subrect of color buffer */
535 if (r==g && g==b) {
536 /* same value for all three components (gray) */
537 GLint j;
538 for (j=0;j<height;j++) {
539 bgr_t *ptr3 = PIXELADDR3( xmesa->xm_draw_buffer, x, y+j );
540 MEMSET(ptr3, r, 3 * width);
541 }
542 }
543 else {
544 /* non-gray clear color */
545 GLint i, j;
546 for (j = 0; j < height; j++) {
547 bgr_t *ptr3 = PIXELADDR3( xmesa->xm_draw_buffer, x, y+j );
548 for (i = 0; i < width; i++) {
549 ptr3->r = r;
550 ptr3->g = g;
551 ptr3->b = b;
552 ptr3++;
553 }
554 }
555 #if 0 /* this code might not always (seems ptr3 always == ptr4) */
556 GLint j;
557 GLuint pixel4[3];
558 pixel4[0] = clearPixel | (clearPixel << 24);
559 pixel4[1] = (clearPixel << 16) | (clearPixel >> 8);
560 pixel4[2] = (clearPixel << 8) | (clearPixel >> 16);
561 for (j=0;j<height;j++) {
562 bgr_t *ptr3 = PIXELADDR3( xmesa->xm_draw_buffer, x, y+j );
563 register GLuint *ptr4 = (GLuint *)ptr3;
564 register GLuint *p, px;
565 GLuint w = width;
566 switch (3 & (int)(ptr3 - (bgr_t*) ptr4)){
567 case 0:
568 break;
569 case 1:
570 px = *ptr4 & 0x00ffffff;
571 px |= pixel4[0] & 0xff000000;
572 *ptr4++ = px;
573 px = *ptr4 & 0xffff0000;
574 px |= pixel4[2] & 0x0000ffff;
575 *ptr4 = px;
576 if (0 == --w)
577 break;
578 case 2:
579 px = *ptr4 & 0x0000fffff;
580 px |= pixel4[1] & 0xffff0000;
581 *ptr4++ = px;
582 px = *ptr4 & 0xffffff00;
583 px |= pixel4[2] & 0x000000ff;
584 *ptr4 = px;
585 if (0 == --w)
586 break;
587 case 3:
588 px = *ptr4 & 0x000000ff;
589 px |= pixel4[2] & 0xffffff00;
590 *ptr4++ = px;
591 --w;
592 break;
593 }
594 while (w > 3){
595 p = pixel4;
596 *ptr4++ = *p++;
597 *ptr4++ = *p++;
598 *ptr4++ = *p++;
599 w -= 4;
600 }
601 switch (w) {
602 case 3:
603 p = pixel4;
604 *ptr4++ = *p++;
605 *ptr4++ = *p++;
606 px = *ptr4 & 0xffffff00;
607 px |= *p & 0xff;
608 *ptr4 = px;
609 break;
610 case 2:
611 p = pixel4;
612 *ptr4++ = *p++;
613 px = *ptr4 & 0xffff0000;
614 px |= *p & 0xffff;
615 *ptr4 = px;
616 break;
617 case 1:
618 px = *ptr4 & 0xff000000;
619 px |= pixel4[0] & 0xffffff;
620 *ptr4 = px;
621 break;
622 case 0:
623 break;
624 }
625 }
626 #endif
627 }
628 }
629 }
630
631
632 static void
633 clear_32bit_ximage( GLcontext *ctx, GLboolean all,
634 GLint x, GLint y, GLint width, GLint height )
635 {
636 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
637 register GLuint pixel = (GLuint) xmesa->clearpixel;
638 if (xmesa->swapbytes) {
639 pixel = ((pixel >> 24) & 0x000000ff)
640 | ((pixel >> 8) & 0x0000ff00)
641 | ((pixel << 8) & 0x00ff0000)
642 | ((pixel << 24) & 0xff000000);
643 }
644 if (all) {
645 register GLint n = xmesa->xm_draw_buffer->width * xmesa->xm_draw_buffer->height;
646 register GLuint *ptr4 = (GLuint *) xmesa->xm_draw_buffer->backimage->data;
647 if (pixel==0) {
648 MEMSET( ptr4, pixel, 4*n );
649 }
650 else {
651 do {
652 *ptr4++ = pixel;
653 n--;
654 } while (n!=0);
655 }
656 }
657 else {
658 register int i, j;
659 for (j=0;j<height;j++) {
660 register GLuint *ptr4 = PIXELADDR4( xmesa->xm_draw_buffer, x, y+j );
661 for (i=0;i<width;i++) {
662 *ptr4++ = pixel;
663 }
664 }
665 }
666 }
667
668
669 static void
670 clear_nbit_ximage( GLcontext *ctx, GLboolean all,
671 GLint x, GLint y, GLint width, GLint height )
672 {
673 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
674 XMesaImage *img = xmesa->xm_draw_buffer->backimage;
675 if (all) {
676 register int i, j;
677 width = xmesa->xm_draw_buffer->width;
678 height = xmesa->xm_draw_buffer->height;
679 for (j=0;j<height;j++) {
680 for (i=0;i<width;i++) {
681 XMesaPutPixel( img, i, j, xmesa->clearpixel );
682 }
683 }
684 }
685 else {
686 /* TODO: optimize this */
687 register int i, j;
688 y = FLIP(xmesa->xm_draw_buffer, y);
689 for (j=0;j<height;j++) {
690 for (i=0;i<width;i++) {
691 XMesaPutPixel( img, x+i, y-j, xmesa->clearpixel );
692 }
693 }
694 }
695 }
696
697
698
699 static void
700 clear_buffers( GLcontext *ctx, GLbitfield mask,
701 GLboolean all, GLint x, GLint y, GLint width, GLint height )
702 {
703 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
704 const GLuint *colorMask = (GLuint *) &ctx->Color.ColorMask;
705
706 if ((mask & (DD_FRONT_LEFT_BIT | DD_BACK_LEFT_BIT)) &&
707 xmesa->xm_draw_buffer->mesa_buffer.UseSoftwareAlphaBuffers &&
708 ctx->Color.ColorMask[ACOMP]) {
709 _swrast_clear_alpha_buffers(ctx);
710 }
711
712 /* we can't handle color or index masking */
713 if (*colorMask == 0xffffffff && ctx->Color.IndexMask == 0xffffffff) {
714 if (mask & DD_FRONT_LEFT_BIT) {
715 ASSERT(xmesa->xm_draw_buffer->front_clear_func);
716 (*xmesa->xm_draw_buffer->front_clear_func)( ctx, all, x, y, width, height );
717 mask &= ~DD_FRONT_LEFT_BIT;
718 }
719 if (mask & DD_BACK_LEFT_BIT) {
720 ASSERT(xmesa->xm_draw_buffer->back_clear_func);
721 (*xmesa->xm_draw_buffer->back_clear_func)( ctx, all, x, y, width, height );
722 mask &= ~DD_BACK_LEFT_BIT;
723 }
724 }
725
726 if (mask)
727 _swrast_Clear( ctx, mask, all, x, y, width, height );
728 }
729
730
731 /*
732 * When we detect that the user has resized the window this function will
733 * get called. Here we'll reallocate the back buffer, depth buffer,
734 * stencil buffer etc. to match the new window size.
735 */
736 void
737 xmesa_resize_buffers( GLframebuffer *buffer )
738 {
739 int height = (int) buffer->Height;
740 /* We can do this cast because the first field in the XMesaBuffer
741 * struct is a GLframebuffer struct. If this weren't true, we'd
742 * need a pointer from the GLframebuffer to the XMesaBuffer.
743 */
744 XMesaBuffer xmBuffer = (XMesaBuffer) buffer;
745
746 xmBuffer->width = buffer->Width;
747 xmBuffer->height = buffer->Height;
748 xmesa_alloc_back_buffer( xmBuffer );
749
750 /* Needed by FLIP macro */
751 xmBuffer->bottom = height - 1;
752
753 if (xmBuffer->backimage) {
754 /* Needed by PIXELADDR1 macro */
755 xmBuffer->ximage_width1 = xmBuffer->backimage->bytes_per_line;
756 xmBuffer->ximage_origin1 = (GLubyte *) xmBuffer->backimage->data
757 + xmBuffer->ximage_width1 * (height-1);
758
759 /* Needed by PIXELADDR2 macro */
760 xmBuffer->ximage_width2 = xmBuffer->backimage->bytes_per_line / 2;
761 xmBuffer->ximage_origin2 = (GLushort *) xmBuffer->backimage->data
762 + xmBuffer->ximage_width2 * (height-1);
763
764 /* Needed by PIXELADDR3 macro */
765 xmBuffer->ximage_width3 = xmBuffer->backimage->bytes_per_line;
766 xmBuffer->ximage_origin3 = (GLubyte *) xmBuffer->backimage->data
767 + xmBuffer->ximage_width3 * (height-1);
768
769 /* Needed by PIXELADDR4 macro */
770 xmBuffer->ximage_width4 = xmBuffer->backimage->width;
771 xmBuffer->ximage_origin4 = (GLuint *) xmBuffer->backimage->data
772 + xmBuffer->ximage_width4 * (height-1);
773 }
774
775 _swrast_alloc_buffers( buffer );
776 }
777
778 #if 0
779 /*
780 * This function implements glDrawPixels() with an XPutImage call when
781 * drawing to the front buffer (X Window drawable).
782 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
783 * XXX top/bottom edge clipping is broken!
784 */
785 static GLboolean
786 drawpixels_8R8G8B( GLcontext *ctx,
787 GLint x, GLint y, GLsizei width, GLsizei height,
788 GLenum format, GLenum type,
789 const struct gl_pixelstore_attrib *unpack,
790 const GLvoid *pixels )
791 {
792 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
793 XMesaDisplay *dpy = xmesa->xm_visual->display;
794 XMesaDrawable buffer = xmesa->xm_draw_buffer->buffer;
795 XMesaGC gc = xmesa->xm_draw_buffer->gc;
796 assert(dpy);
797 assert(buffer);
798 assert(gc);
799
800 /* XXX also check for pixel scale/bias/lookup/zooming! */
801 if (format == GL_BGRA && type == GL_UNSIGNED_BYTE) {
802 int dstX = x;
803 int dstY = y;
804 int w = width;
805 int h = height;
806 int srcX = unpack->SkipPixels;
807 int srcY = unpack->SkipRows;
808 if (_mesa_clip_pixelrect(ctx, &dstX, &dstY, &w, &h, &srcX, &srcY)) {
809 XMesaImage ximage;
810 MEMSET(&ximage, 0, sizeof(XMesaImage));
811 ximage.width = width;
812 ximage.height = height;
813 ximage.format = ZPixmap;
814 ximage.data = (char *) pixels + (height - 1) * width * 4;
815 ximage.byte_order = LSBFirst;
816 ximage.bitmap_unit = 32;
817 ximage.bitmap_bit_order = LSBFirst;
818 ximage.bitmap_pad = 32;
819 ximage.depth = 24;
820 ximage.bytes_per_line = -width * 4;
821 ximage.bits_per_pixel = 32;
822 ximage.red_mask = 0xff0000;
823 ximage.green_mask = 0x00ff00;
824 ximage.blue_mask = 0x0000ff;
825 dstY = FLIP(xmesa->xm_draw_buffer,dstY) - height + 1;
826 XPutImage(dpy, buffer, gc, &ximage, srcX, srcY, dstX, dstY, w, h);
827 return GL_TRUE;
828 }
829 }
830 return GL_FALSE;
831 }
832 #endif
833
834
835
836 static const GLubyte *
837 get_string( GLcontext *ctx, GLenum name )
838 {
839 (void) ctx;
840 switch (name) {
841 case GL_RENDERER:
842 #ifdef XFree86Server
843 return (const GLubyte *) "Mesa GLX Indirect";
844 #else
845 return (const GLubyte *) "Mesa X11";
846 #endif
847 case GL_VENDOR:
848 #ifdef XFree86Server
849 return (const GLubyte *) "Mesa project: www.mesa3d.org";
850 #else
851 return NULL;
852 #endif
853 default:
854 return NULL;
855 }
856 }
857
858
859 static void
860 enable( GLcontext *ctx, GLenum pname, GLboolean state )
861 {
862 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
863
864 switch (pname) {
865 case GL_DITHER:
866 if (state)
867 xmesa->pixelformat = xmesa->xm_visual->dithered_pf;
868 else
869 xmesa->pixelformat = xmesa->xm_visual->undithered_pf;
870 break;
871 default:
872 ; /* silence compiler warning */
873 }
874 }
875
876
877 void xmesa_update_state( GLcontext *ctx, GLuint new_state )
878 {
879 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
880
881 /* Propogate statechange information to swrast and swrast_setup
882 * modules. The X11 driver has no internal GL-dependent state.
883 */
884 _swrast_InvalidateState( ctx, new_state );
885 _ac_InvalidateState( ctx, new_state );
886 _tnl_InvalidateState( ctx, new_state );
887 _swsetup_InvalidateState( ctx, new_state );
888
889
890 /* setup pointers to front and back buffer clear functions */
891 xmesa->xm_draw_buffer->front_clear_func = clear_front_pixmap;
892 if (xmesa->xm_draw_buffer->backpixmap != XIMAGE) {
893 xmesa->xm_draw_buffer->back_clear_func = clear_back_pixmap;
894 }
895 else if (sizeof(GLushort)!=2 || sizeof(GLuint)!=4) {
896 xmesa->xm_draw_buffer->back_clear_func = clear_nbit_ximage;
897 }
898 else switch (xmesa->xm_visual->BitsPerPixel) {
899 case 8:
900 if (xmesa->xm_visual->hpcr_clear_flag) {
901 xmesa->xm_draw_buffer->back_clear_func = clear_HPCR_ximage;
902 }
903 else {
904 xmesa->xm_draw_buffer->back_clear_func = clear_8bit_ximage;
905 }
906 break;
907 case 16:
908 xmesa->xm_draw_buffer->back_clear_func = clear_16bit_ximage;
909 break;
910 case 24:
911 xmesa->xm_draw_buffer->back_clear_func = clear_24bit_ximage;
912 break;
913 case 32:
914 xmesa->xm_draw_buffer->back_clear_func = clear_32bit_ximage;
915 break;
916 default:
917 xmesa->xm_draw_buffer->back_clear_func = clear_nbit_ximage;
918 break;
919 }
920
921 xmesa_update_span_funcs(ctx);
922 }
923
924
925
926 /* Setup pointers and other driver state that is constant for the life
927 * of a context.
928 */
929 void xmesa_init_pointers( GLcontext *ctx )
930 {
931 TNLcontext *tnl;
932 struct swrast_device_driver *dd = _swrast_GetDeviceDriverReference( ctx );
933
934 ctx->Driver.GetString = get_string;
935 ctx->Driver.GetBufferSize = get_buffer_size;
936 ctx->Driver.Flush = flush;
937 ctx->Driver.Finish = finish;
938
939 /* Software rasterizer pixel paths:
940 */
941 ctx->Driver.Accum = _swrast_Accum;
942 ctx->Driver.Bitmap = _swrast_Bitmap;
943 ctx->Driver.Clear = clear_buffers;
944 ctx->Driver.ResizeBuffers = xmesa_resize_buffers;
945 ctx->Driver.CopyPixels = _swrast_CopyPixels;
946 ctx->Driver.DrawPixels = _swrast_DrawPixels;
947 ctx->Driver.ReadPixels = _swrast_ReadPixels;
948 ctx->Driver.DrawBuffer = _swrast_DrawBuffer;
949
950 /* Software texture functions:
951 */
952 ctx->Driver.ChooseTextureFormat = _mesa_choose_tex_format;
953 ctx->Driver.TexImage1D = _mesa_store_teximage1d;
954 ctx->Driver.TexImage2D = _mesa_store_teximage2d;
955 ctx->Driver.TexImage3D = _mesa_store_teximage3d;
956 ctx->Driver.TexSubImage1D = _mesa_store_texsubimage1d;
957 ctx->Driver.TexSubImage2D = _mesa_store_texsubimage2d;
958 ctx->Driver.TexSubImage3D = _mesa_store_texsubimage3d;
959 ctx->Driver.TestProxyTexImage = _mesa_test_proxy_teximage;
960
961 ctx->Driver.CopyTexImage1D = _swrast_copy_teximage1d;
962 ctx->Driver.CopyTexImage2D = _swrast_copy_teximage2d;
963 ctx->Driver.CopyTexSubImage1D = _swrast_copy_texsubimage1d;
964 ctx->Driver.CopyTexSubImage2D = _swrast_copy_texsubimage2d;
965 ctx->Driver.CopyTexSubImage3D = _swrast_copy_texsubimage3d;
966
967 ctx->Driver.NewTextureObject = _mesa_alloc_texture_object;
968 ctx->Driver.DeleteTexture = _mesa_free_texture_object;
969
970 ctx->Driver.CompressedTexImage1D = _mesa_store_compressed_teximage1d;
971 ctx->Driver.CompressedTexImage2D = _mesa_store_compressed_teximage2d;
972 ctx->Driver.CompressedTexImage3D = _mesa_store_compressed_teximage3d;
973 ctx->Driver.CompressedTexSubImage1D = _mesa_store_compressed_texsubimage1d;
974 ctx->Driver.CompressedTexSubImage2D = _mesa_store_compressed_texsubimage2d;
975 ctx->Driver.CompressedTexSubImage3D = _mesa_store_compressed_texsubimage3d;
976
977 /* Swrast hooks for imaging extensions:
978 */
979 ctx->Driver.CopyColorTable = _swrast_CopyColorTable;
980 ctx->Driver.CopyColorSubTable = _swrast_CopyColorSubTable;
981 ctx->Driver.CopyConvolutionFilter1D = _swrast_CopyConvolutionFilter1D;
982 ctx->Driver.CopyConvolutionFilter2D = _swrast_CopyConvolutionFilter2D;
983
984
985 /* Statechange callbacks:
986 */
987 ctx->Driver.ClearIndex = clear_index;
988 ctx->Driver.ClearColor = clear_color;
989 ctx->Driver.IndexMask = index_mask;
990 ctx->Driver.ColorMask = color_mask;
991 ctx->Driver.Enable = enable;
992
993
994 /* Initialize the TNL driver interface:
995 */
996 tnl = TNL_CONTEXT(ctx);
997 tnl->Driver.RunPipeline = _tnl_run_pipeline;
998
999 dd->SetBuffer = set_buffer;
1000
1001 /* Install swsetup for tnl->Driver.Render.*:
1002 */
1003 _swsetup_Wakeup(ctx);
1004
1005 (void) DitherValues; /* silenced unused var warning */
1006 }
1007
1008
1009
1010
1011
1012 #define XMESA_NEW_POINT (_NEW_POINT | \
1013 _NEW_RENDERMODE | \
1014 _SWRAST_NEW_RASTERMASK)
1015
1016 #define XMESA_NEW_LINE (_NEW_LINE | \
1017 _NEW_TEXTURE | \
1018 _NEW_LIGHT | \
1019 _NEW_DEPTH | \
1020 _NEW_RENDERMODE | \
1021 _SWRAST_NEW_RASTERMASK)
1022
1023 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1024 _NEW_TEXTURE | \
1025 _NEW_LIGHT | \
1026 _NEW_DEPTH | \
1027 _NEW_RENDERMODE | \
1028 _SWRAST_NEW_RASTERMASK)
1029
1030
1031 /* Extend the software rasterizer with our line/point/triangle
1032 * functions.
1033 */
1034 void xmesa_register_swrast_functions( GLcontext *ctx )
1035 {
1036 SWcontext *swrast = SWRAST_CONTEXT( ctx );
1037
1038 swrast->choose_point = xmesa_choose_point;
1039 swrast->choose_line = xmesa_choose_line;
1040 swrast->choose_triangle = xmesa_choose_triangle;
1041
1042 swrast->invalidate_point |= XMESA_NEW_POINT;
1043 swrast->invalidate_line |= XMESA_NEW_LINE;
1044 swrast->invalidate_triangle |= XMESA_NEW_TRIANGLE;
1045 }