3aa33a58f3613e6b345386eb88d0c57e07d82d9b
[mesa.git] / src / mesa / drivers / x11 / xm_dd.c
1 /* $Id: xm_dd.c,v 1.46 2003/04/01 16:41:58 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 5.1
6 *
7 * Copyright (C) 1999-2003 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
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 XMesaSetPlaneMask( xmesa->display, xmesa->xm_draw_buffer->gc, m );
249 }
250 }
251
252
253
254 /**********************************************************************/
255 /*** glClear implementations ***/
256 /**********************************************************************/
257
258
259 static void
260 clear_front_pixmap( GLcontext *ctx, GLboolean all,
261 GLint x, GLint y, GLint width, GLint height )
262 {
263 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
264 if (all) {
265 XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->frontbuffer,
266 xmesa->xm_draw_buffer->cleargc,
267 0, 0,
268 xmesa->xm_draw_buffer->width+1,
269 xmesa->xm_draw_buffer->height+1 );
270 }
271 else {
272 XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->frontbuffer,
273 xmesa->xm_draw_buffer->cleargc,
274 x, xmesa->xm_draw_buffer->height - y - height,
275 width, height );
276 }
277 }
278
279
280 static void
281 clear_back_pixmap( GLcontext *ctx, GLboolean all,
282 GLint x, GLint y, GLint width, GLint height )
283 {
284 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
285 if (all) {
286 XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->backpixmap,
287 xmesa->xm_draw_buffer->cleargc,
288 0, 0,
289 xmesa->xm_draw_buffer->width+1,
290 xmesa->xm_draw_buffer->height+1 );
291 }
292 else {
293 XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->backpixmap,
294 xmesa->xm_draw_buffer->cleargc,
295 x, xmesa->xm_draw_buffer->height - y - height,
296 width, height );
297 }
298 }
299
300
301 static void
302 clear_8bit_ximage( GLcontext *ctx, GLboolean all,
303 GLint x, GLint y, GLint width, GLint height )
304 {
305 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
306 if (all) {
307 size_t n = xmesa->xm_draw_buffer->backimage->bytes_per_line
308 * xmesa->xm_draw_buffer->backimage->height;
309 MEMSET( xmesa->xm_draw_buffer->backimage->data, xmesa->clearpixel, n );
310 }
311 else {
312 GLint i;
313 for (i=0;i<height;i++) {
314 GLubyte *ptr = PIXELADDR1( xmesa->xm_draw_buffer, x, y+i );
315 MEMSET( ptr, xmesa->clearpixel, width );
316 }
317 }
318 }
319
320
321 static void
322 clear_HPCR_ximage( GLcontext *ctx, GLboolean all,
323 GLint x, GLint y, GLint width, GLint height )
324 {
325 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
326 if (all) {
327 GLint i, c16 = (xmesa->xm_draw_buffer->backimage->bytes_per_line>>4)<<4;
328 GLubyte *ptr = (GLubyte *)xmesa->xm_draw_buffer->backimage->data;
329 for (i=0; i<xmesa->xm_draw_buffer->backimage->height; i++) {
330 GLint j;
331 GLubyte *sptr = xmesa->xm_visual->hpcr_clear_ximage_pattern[0];
332 if (i&1) {
333 sptr += 16;
334 }
335 for (j=0; j<c16; j+=16) {
336 ptr[0] = sptr[0];
337 ptr[1] = sptr[1];
338 ptr[2] = sptr[2];
339 ptr[3] = sptr[3];
340 ptr[4] = sptr[4];
341 ptr[5] = sptr[5];
342 ptr[6] = sptr[6];
343 ptr[7] = sptr[7];
344 ptr[8] = sptr[8];
345 ptr[9] = sptr[9];
346 ptr[10] = sptr[10];
347 ptr[11] = sptr[11];
348 ptr[12] = sptr[12];
349 ptr[13] = sptr[13];
350 ptr[14] = sptr[14];
351 ptr[15] = sptr[15];
352 ptr += 16;
353 }
354 for (; j<xmesa->xm_draw_buffer->backimage->bytes_per_line; j++) {
355 *ptr = sptr[j&15];
356 ptr++;
357 }
358 }
359 }
360 else {
361 GLint i;
362 for (i=y; i<y+height; i++) {
363 GLubyte *ptr = PIXELADDR1( xmesa->xm_draw_buffer, x, i );
364 int j;
365 GLubyte *sptr = xmesa->xm_visual->hpcr_clear_ximage_pattern[0];
366 if (i&1) {
367 sptr += 16;
368 }
369 for (j=x; j<x+width; j++) {
370 *ptr = sptr[j&15];
371 ptr++;
372 }
373 }
374 }
375 }
376
377
378 static void
379 clear_16bit_ximage( GLcontext *ctx, GLboolean all,
380 GLint x, GLint y, GLint width, GLint height )
381 {
382 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
383 register GLuint pixel = (GLuint) xmesa->clearpixel;
384 if (xmesa->swapbytes) {
385 pixel = ((pixel >> 8) & 0x00ff) | ((pixel << 8) & 0xff00);
386 }
387 if (all) {
388 register GLuint n;
389 register GLuint *ptr4 = (GLuint *) xmesa->xm_draw_buffer->backimage->data;
390 if ((pixel & 0xff) == ((pixel >> 8) & 0xff)) {
391 /* low and high bytes are equal so use memset() */
392 n = xmesa->xm_draw_buffer->backimage->bytes_per_line
393 * xmesa->xm_draw_buffer->height;
394 MEMSET( ptr4, pixel & 0xff, n );
395 }
396 else {
397 pixel = pixel | (pixel<<16);
398 n = xmesa->xm_draw_buffer->backimage->bytes_per_line
399 * xmesa->xm_draw_buffer->height / 4;
400 do {
401 *ptr4++ = pixel;
402 n--;
403 } while (n!=0);
404
405 if ((xmesa->xm_draw_buffer->backimage->bytes_per_line *
406 xmesa->xm_draw_buffer->height) & 0x2)
407 *(GLushort *)ptr4 = pixel & 0xffff;
408 }
409 }
410 else {
411 register int i, j;
412 for (j=0;j<height;j++) {
413 register GLushort *ptr2 = PIXELADDR2( xmesa->xm_draw_buffer, x, y+j );
414 for (i=0;i<width;i++) {
415 *ptr2++ = pixel;
416 }
417 }
418 }
419 }
420
421
422 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
423 static void
424 clear_24bit_ximage( GLcontext *ctx, GLboolean all,
425 GLint x, GLint y, GLint width, GLint height )
426 {
427 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
428 const GLubyte r = xmesa->clearcolor[0];
429 const GLubyte g = xmesa->clearcolor[1];
430 const GLubyte b = xmesa->clearcolor[2];
431 register GLuint clearPixel;
432 if (xmesa->swapbytes) {
433 clearPixel = (b << 16) | (g << 8) | r;
434 }
435 else {
436 clearPixel = (r << 16) | (g << 8) | b;
437 }
438
439 if (all) {
440 if (r==g && g==b) {
441 /* same value for all three components (gray) */
442 const GLint w3 = xmesa->xm_draw_buffer->width * 3;
443 const GLint h = xmesa->xm_draw_buffer->height;
444 GLint i;
445 for (i = 0; i < h; i++) {
446 bgr_t *ptr3 = PIXELADDR3(xmesa->xm_draw_buffer, 0, i);
447 MEMSET(ptr3, r, w3);
448 }
449 }
450 else {
451 /* the usual case */
452 const GLint w = xmesa->xm_draw_buffer->width;
453 const GLint h = xmesa->xm_draw_buffer->height;
454 GLint i, j;
455 for (i = 0; i < h; i++) {
456 bgr_t *ptr3 = PIXELADDR3(xmesa->xm_draw_buffer, 0, i);
457 for (j = 0; j < w; j++) {
458 ptr3->r = r;
459 ptr3->g = g;
460 ptr3->b = b;
461 ptr3++;
462 }
463 }
464 #if 0 /* this code doesn't work for all window widths */
465 register GLuint *ptr4 = (GLuint *) ptr3;
466 register GLuint px;
467 GLuint pixel4[3];
468 register GLuint *p = pixel4;
469 pixel4[0] = clearPixel | (clearPixel << 24);
470 pixel4[1] = (clearPixel << 16) | (clearPixel >> 8);
471 pixel4[2] = (clearPixel << 8) | (clearPixel >> 16);
472 switch (3 & (int)(ptr3 - (bgr_t*) ptr4)){
473 case 0:
474 break;
475 case 1:
476 px = *ptr4 & 0x00ffffff;
477 px |= pixel4[0] & 0xff000000;
478 *ptr4++ = px;
479 px = *ptr4 & 0xffff0000;
480 px |= pixel4[2] & 0x0000ffff;
481 *ptr4 = px;
482 if (0 == --n)
483 break;
484 case 2:
485 px = *ptr4 & 0x0000fffff;
486 px |= pixel4[1] & 0xffff0000;
487 *ptr4++ = px;
488 px = *ptr4 & 0xffffff00;
489 px |= pixel4[2] & 0x000000ff;
490 *ptr4 = px;
491 if (0 == --n)
492 break;
493 case 3:
494 px = *ptr4 & 0x000000ff;
495 px |= pixel4[2] & 0xffffff00;
496 *ptr4++ = px;
497 --n;
498 break;
499 }
500 while (n > 3) {
501 p = pixel4;
502 *ptr4++ = *p++;
503 *ptr4++ = *p++;
504 *ptr4++ = *p++;
505 n -= 4;
506 }
507 switch (n) {
508 case 3:
509 p = pixel4;
510 *ptr4++ = *p++;
511 *ptr4++ = *p++;
512 px = *ptr4 & 0xffffff00;
513 px |= clearPixel & 0xff;
514 *ptr4 = px;
515 break;
516 case 2:
517 p = pixel4;
518 *ptr4++ = *p++;
519 px = *ptr4 & 0xffff0000;
520 px |= *p & 0xffff;
521 *ptr4 = px;
522 break;
523 case 1:
524 px = *ptr4 & 0xff000000;
525 px |= *p & 0xffffff;
526 *ptr4 = px;
527 break;
528 case 0:
529 break;
530 }
531 #endif
532 }
533 }
534 else {
535 /* only clear subrect of color buffer */
536 if (r==g && g==b) {
537 /* same value for all three components (gray) */
538 GLint j;
539 for (j=0;j<height;j++) {
540 bgr_t *ptr3 = PIXELADDR3( xmesa->xm_draw_buffer, x, y+j );
541 MEMSET(ptr3, r, 3 * width);
542 }
543 }
544 else {
545 /* non-gray clear color */
546 GLint i, j;
547 for (j = 0; j < height; j++) {
548 bgr_t *ptr3 = PIXELADDR3( xmesa->xm_draw_buffer, x, y+j );
549 for (i = 0; i < width; i++) {
550 ptr3->r = r;
551 ptr3->g = g;
552 ptr3->b = b;
553 ptr3++;
554 }
555 }
556 #if 0 /* this code might not always (seems ptr3 always == ptr4) */
557 GLint j;
558 GLuint pixel4[3];
559 pixel4[0] = clearPixel | (clearPixel << 24);
560 pixel4[1] = (clearPixel << 16) | (clearPixel >> 8);
561 pixel4[2] = (clearPixel << 8) | (clearPixel >> 16);
562 for (j=0;j<height;j++) {
563 bgr_t *ptr3 = PIXELADDR3( xmesa->xm_draw_buffer, x, y+j );
564 register GLuint *ptr4 = (GLuint *)ptr3;
565 register GLuint *p, px;
566 GLuint w = width;
567 switch (3 & (int)(ptr3 - (bgr_t*) ptr4)){
568 case 0:
569 break;
570 case 1:
571 px = *ptr4 & 0x00ffffff;
572 px |= pixel4[0] & 0xff000000;
573 *ptr4++ = px;
574 px = *ptr4 & 0xffff0000;
575 px |= pixel4[2] & 0x0000ffff;
576 *ptr4 = px;
577 if (0 == --w)
578 break;
579 case 2:
580 px = *ptr4 & 0x0000fffff;
581 px |= pixel4[1] & 0xffff0000;
582 *ptr4++ = px;
583 px = *ptr4 & 0xffffff00;
584 px |= pixel4[2] & 0x000000ff;
585 *ptr4 = px;
586 if (0 == --w)
587 break;
588 case 3:
589 px = *ptr4 & 0x000000ff;
590 px |= pixel4[2] & 0xffffff00;
591 *ptr4++ = px;
592 --w;
593 break;
594 }
595 while (w > 3){
596 p = pixel4;
597 *ptr4++ = *p++;
598 *ptr4++ = *p++;
599 *ptr4++ = *p++;
600 w -= 4;
601 }
602 switch (w) {
603 case 3:
604 p = pixel4;
605 *ptr4++ = *p++;
606 *ptr4++ = *p++;
607 px = *ptr4 & 0xffffff00;
608 px |= *p & 0xff;
609 *ptr4 = px;
610 break;
611 case 2:
612 p = pixel4;
613 *ptr4++ = *p++;
614 px = *ptr4 & 0xffff0000;
615 px |= *p & 0xffff;
616 *ptr4 = px;
617 break;
618 case 1:
619 px = *ptr4 & 0xff000000;
620 px |= pixel4[0] & 0xffffff;
621 *ptr4 = px;
622 break;
623 case 0:
624 break;
625 }
626 }
627 #endif
628 }
629 }
630 }
631
632
633 static void
634 clear_32bit_ximage( GLcontext *ctx, GLboolean all,
635 GLint x, GLint y, GLint width, GLint height )
636 {
637 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
638 register GLuint pixel = (GLuint) xmesa->clearpixel;
639 if (xmesa->swapbytes) {
640 pixel = ((pixel >> 24) & 0x000000ff)
641 | ((pixel >> 8) & 0x0000ff00)
642 | ((pixel << 8) & 0x00ff0000)
643 | ((pixel << 24) & 0xff000000);
644 }
645 if (all) {
646 register GLint n = xmesa->xm_draw_buffer->width * xmesa->xm_draw_buffer->height;
647 register GLuint *ptr4 = (GLuint *) xmesa->xm_draw_buffer->backimage->data;
648 if (pixel==0) {
649 MEMSET( ptr4, pixel, 4*n );
650 }
651 else {
652 do {
653 *ptr4++ = pixel;
654 n--;
655 } while (n!=0);
656 }
657 }
658 else {
659 register int i, j;
660 for (j=0;j<height;j++) {
661 register GLuint *ptr4 = PIXELADDR4( xmesa->xm_draw_buffer, x, y+j );
662 for (i=0;i<width;i++) {
663 *ptr4++ = pixel;
664 }
665 }
666 }
667 }
668
669
670 static void
671 clear_nbit_ximage( GLcontext *ctx, GLboolean all,
672 GLint x, GLint y, GLint width, GLint height )
673 {
674 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
675 XMesaImage *img = xmesa->xm_draw_buffer->backimage;
676 if (all) {
677 register int i, j;
678 width = xmesa->xm_draw_buffer->width;
679 height = xmesa->xm_draw_buffer->height;
680 for (j=0;j<height;j++) {
681 for (i=0;i<width;i++) {
682 XMesaPutPixel( img, i, j, xmesa->clearpixel );
683 }
684 }
685 }
686 else {
687 /* TODO: optimize this */
688 register int i, j;
689 y = FLIP(xmesa->xm_draw_buffer, y);
690 for (j=0;j<height;j++) {
691 for (i=0;i<width;i++) {
692 XMesaPutPixel( img, x+i, y-j, xmesa->clearpixel );
693 }
694 }
695 }
696 }
697
698
699
700 static void
701 clear_buffers( GLcontext *ctx, GLbitfield mask,
702 GLboolean all, GLint x, GLint y, GLint width, GLint height )
703 {
704 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
705 const GLuint *colorMask = (GLuint *) &ctx->Color.ColorMask;
706
707 if ((mask & (DD_FRONT_LEFT_BIT | DD_BACK_LEFT_BIT)) &&
708 xmesa->xm_draw_buffer->mesa_buffer.UseSoftwareAlphaBuffers &&
709 ctx->Color.ColorMask[ACOMP]) {
710 _swrast_clear_alpha_buffers(ctx);
711 }
712
713 /* we can't handle color or index masking */
714 if (*colorMask == 0xffffffff && ctx->Color.IndexMask == 0xffffffff) {
715 if (mask & DD_FRONT_LEFT_BIT) {
716 ASSERT(xmesa->xm_draw_buffer->front_clear_func);
717 (*xmesa->xm_draw_buffer->front_clear_func)( ctx, all, x, y, width, height );
718 mask &= ~DD_FRONT_LEFT_BIT;
719 }
720 if (mask & DD_BACK_LEFT_BIT) {
721 ASSERT(xmesa->xm_draw_buffer->back_clear_func);
722 (*xmesa->xm_draw_buffer->back_clear_func)( ctx, all, x, y, width, height );
723 mask &= ~DD_BACK_LEFT_BIT;
724 }
725 }
726
727 if (mask)
728 _swrast_Clear( ctx, mask, all, x, y, width, height );
729 }
730
731
732 /*
733 * When we detect that the user has resized the window this function will
734 * get called. Here we'll reallocate the back buffer, depth buffer,
735 * stencil buffer etc. to match the new window size.
736 */
737 void
738 xmesa_resize_buffers( GLframebuffer *buffer )
739 {
740 int height = (int) buffer->Height;
741 /* We can do this cast because the first field in the XMesaBuffer
742 * struct is a GLframebuffer struct. If this weren't true, we'd
743 * need a pointer from the GLframebuffer to the XMesaBuffer.
744 */
745 XMesaBuffer xmBuffer = (XMesaBuffer) buffer;
746
747 xmBuffer->width = buffer->Width;
748 xmBuffer->height = buffer->Height;
749 xmesa_alloc_back_buffer( xmBuffer );
750
751 /* Needed by FLIP macro */
752 xmBuffer->bottom = height - 1;
753
754 if (xmBuffer->backimage) {
755 /* Needed by PIXELADDR1 macro */
756 xmBuffer->ximage_width1 = xmBuffer->backimage->bytes_per_line;
757 xmBuffer->ximage_origin1 = (GLubyte *) xmBuffer->backimage->data
758 + xmBuffer->ximage_width1 * (height-1);
759
760 /* Needed by PIXELADDR2 macro */
761 xmBuffer->ximage_width2 = xmBuffer->backimage->bytes_per_line / 2;
762 xmBuffer->ximage_origin2 = (GLushort *) xmBuffer->backimage->data
763 + xmBuffer->ximage_width2 * (height-1);
764
765 /* Needed by PIXELADDR3 macro */
766 xmBuffer->ximage_width3 = xmBuffer->backimage->bytes_per_line;
767 xmBuffer->ximage_origin3 = (GLubyte *) xmBuffer->backimage->data
768 + xmBuffer->ximage_width3 * (height-1);
769
770 /* Needed by PIXELADDR4 macro */
771 xmBuffer->ximage_width4 = xmBuffer->backimage->width;
772 xmBuffer->ximage_origin4 = (GLuint *) xmBuffer->backimage->data
773 + xmBuffer->ximage_width4 * (height-1);
774 }
775
776 _swrast_alloc_buffers( buffer );
777 }
778
779 #if 0
780 /*
781 * This function implements glDrawPixels() with an XPutImage call when
782 * drawing to the front buffer (X Window drawable).
783 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
784 * XXX top/bottom edge clipping is broken!
785 */
786 static GLboolean
787 drawpixels_8R8G8B( GLcontext *ctx,
788 GLint x, GLint y, GLsizei width, GLsizei height,
789 GLenum format, GLenum type,
790 const struct gl_pixelstore_attrib *unpack,
791 const GLvoid *pixels )
792 {
793 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
794 XMesaDisplay *dpy = xmesa->xm_visual->display;
795 XMesaDrawable buffer = xmesa->xm_draw_buffer->buffer;
796 XMesaGC gc = xmesa->xm_draw_buffer->gc;
797 assert(dpy);
798 assert(buffer);
799 assert(gc);
800
801 /* XXX also check for pixel scale/bias/lookup/zooming! */
802 if (format == GL_BGRA && type == GL_UNSIGNED_BYTE) {
803 int dstX = x;
804 int dstY = y;
805 int w = width;
806 int h = height;
807 int srcX = unpack->SkipPixels;
808 int srcY = unpack->SkipRows;
809 if (_mesa_clip_pixelrect(ctx, &dstX, &dstY, &w, &h, &srcX, &srcY)) {
810 XMesaImage ximage;
811 MEMSET(&ximage, 0, sizeof(XMesaImage));
812 ximage.width = width;
813 ximage.height = height;
814 ximage.format = ZPixmap;
815 ximage.data = (char *) pixels + (height - 1) * width * 4;
816 ximage.byte_order = LSBFirst;
817 ximage.bitmap_unit = 32;
818 ximage.bitmap_bit_order = LSBFirst;
819 ximage.bitmap_pad = 32;
820 ximage.depth = 24;
821 ximage.bytes_per_line = -width * 4;
822 ximage.bits_per_pixel = 32;
823 ximage.red_mask = 0xff0000;
824 ximage.green_mask = 0x00ff00;
825 ximage.blue_mask = 0x0000ff;
826 dstY = FLIP(xmesa->xm_draw_buffer,dstY) - height + 1;
827 XPutImage(dpy, buffer, gc, &ximage, srcX, srcY, dstX, dstY, w, h);
828 return GL_TRUE;
829 }
830 }
831 return GL_FALSE;
832 }
833 #endif
834
835
836
837 static const GLubyte *
838 get_string( GLcontext *ctx, GLenum name )
839 {
840 (void) ctx;
841 switch (name) {
842 case GL_RENDERER:
843 #ifdef XFree86Server
844 return (const GLubyte *) "Mesa GLX Indirect";
845 #else
846 return (const GLubyte *) "Mesa X11";
847 #endif
848 case GL_VENDOR:
849 #ifdef XFree86Server
850 return (const GLubyte *) "Mesa project: www.mesa3d.org";
851 #else
852 return NULL;
853 #endif
854 default:
855 return NULL;
856 }
857 }
858
859
860 static void
861 enable( GLcontext *ctx, GLenum pname, GLboolean state )
862 {
863 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
864
865 switch (pname) {
866 case GL_DITHER:
867 if (state)
868 xmesa->pixelformat = xmesa->xm_visual->dithered_pf;
869 else
870 xmesa->pixelformat = xmesa->xm_visual->undithered_pf;
871 break;
872 default:
873 ; /* silence compiler warning */
874 }
875 }
876
877
878 void xmesa_update_state( GLcontext *ctx, GLuint new_state )
879 {
880 const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
881
882 /* Propogate statechange information to swrast and swrast_setup
883 * modules. The X11 driver has no internal GL-dependent state.
884 */
885 _swrast_InvalidateState( ctx, new_state );
886 _ac_InvalidateState( ctx, new_state );
887 _tnl_InvalidateState( ctx, new_state );
888 _swsetup_InvalidateState( ctx, new_state );
889
890
891 /* setup pointers to front and back buffer clear functions */
892 xmesa->xm_draw_buffer->front_clear_func = clear_front_pixmap;
893 if (xmesa->xm_draw_buffer->backpixmap != XIMAGE) {
894 xmesa->xm_draw_buffer->back_clear_func = clear_back_pixmap;
895 }
896 else if (sizeof(GLushort)!=2 || sizeof(GLuint)!=4) {
897 xmesa->xm_draw_buffer->back_clear_func = clear_nbit_ximage;
898 }
899 else switch (xmesa->xm_visual->BitsPerPixel) {
900 case 8:
901 if (xmesa->xm_visual->hpcr_clear_flag) {
902 xmesa->xm_draw_buffer->back_clear_func = clear_HPCR_ximage;
903 }
904 else {
905 xmesa->xm_draw_buffer->back_clear_func = clear_8bit_ximage;
906 }
907 break;
908 case 16:
909 xmesa->xm_draw_buffer->back_clear_func = clear_16bit_ximage;
910 break;
911 case 24:
912 xmesa->xm_draw_buffer->back_clear_func = clear_24bit_ximage;
913 break;
914 case 32:
915 xmesa->xm_draw_buffer->back_clear_func = clear_32bit_ximage;
916 break;
917 default:
918 xmesa->xm_draw_buffer->back_clear_func = clear_nbit_ximage;
919 break;
920 }
921
922 xmesa_update_span_funcs(ctx);
923 }
924
925
926
927 /* Setup pointers and other driver state that is constant for the life
928 * of a context.
929 */
930 void xmesa_init_pointers( GLcontext *ctx )
931 {
932 TNLcontext *tnl;
933 struct swrast_device_driver *dd = _swrast_GetDeviceDriverReference( ctx );
934
935 ctx->Driver.GetString = get_string;
936 ctx->Driver.GetBufferSize = get_buffer_size;
937 ctx->Driver.Flush = flush;
938 ctx->Driver.Finish = finish;
939
940 /* Software rasterizer pixel paths:
941 */
942 ctx->Driver.Accum = _swrast_Accum;
943 ctx->Driver.Bitmap = _swrast_Bitmap;
944 ctx->Driver.Clear = clear_buffers;
945 ctx->Driver.ResizeBuffers = xmesa_resize_buffers;
946 ctx->Driver.CopyPixels = _swrast_CopyPixels;
947 ctx->Driver.DrawPixels = _swrast_DrawPixels;
948 ctx->Driver.ReadPixels = _swrast_ReadPixels;
949 ctx->Driver.DrawBuffer = _swrast_DrawBuffer;
950
951 /* Software texture functions:
952 */
953 ctx->Driver.ChooseTextureFormat = _mesa_choose_tex_format;
954 ctx->Driver.TexImage1D = _mesa_store_teximage1d;
955 ctx->Driver.TexImage2D = _mesa_store_teximage2d;
956 ctx->Driver.TexImage3D = _mesa_store_teximage3d;
957 ctx->Driver.TexSubImage1D = _mesa_store_texsubimage1d;
958 ctx->Driver.TexSubImage2D = _mesa_store_texsubimage2d;
959 ctx->Driver.TexSubImage3D = _mesa_store_texsubimage3d;
960 ctx->Driver.TestProxyTexImage = _mesa_test_proxy_teximage;
961
962 ctx->Driver.CopyTexImage1D = _swrast_copy_teximage1d;
963 ctx->Driver.CopyTexImage2D = _swrast_copy_teximage2d;
964 ctx->Driver.CopyTexSubImage1D = _swrast_copy_texsubimage1d;
965 ctx->Driver.CopyTexSubImage2D = _swrast_copy_texsubimage2d;
966 ctx->Driver.CopyTexSubImage3D = _swrast_copy_texsubimage3d;
967
968 ctx->Driver.NewTextureObject = _mesa_new_texture_object;
969 ctx->Driver.DeleteTexture = _mesa_delete_texture_object;
970
971 ctx->Driver.CompressedTexImage1D = _mesa_store_compressed_teximage1d;
972 ctx->Driver.CompressedTexImage2D = _mesa_store_compressed_teximage2d;
973 ctx->Driver.CompressedTexImage3D = _mesa_store_compressed_teximage3d;
974 ctx->Driver.CompressedTexSubImage1D = _mesa_store_compressed_texsubimage1d;
975 ctx->Driver.CompressedTexSubImage2D = _mesa_store_compressed_texsubimage2d;
976 ctx->Driver.CompressedTexSubImage3D = _mesa_store_compressed_texsubimage3d;
977
978 /* Swrast hooks for imaging extensions:
979 */
980 ctx->Driver.CopyColorTable = _swrast_CopyColorTable;
981 ctx->Driver.CopyColorSubTable = _swrast_CopyColorSubTable;
982 ctx->Driver.CopyConvolutionFilter1D = _swrast_CopyConvolutionFilter1D;
983 ctx->Driver.CopyConvolutionFilter2D = _swrast_CopyConvolutionFilter2D;
984
985
986 /* Statechange callbacks:
987 */
988 ctx->Driver.ClearIndex = clear_index;
989 ctx->Driver.ClearColor = clear_color;
990 ctx->Driver.IndexMask = index_mask;
991 ctx->Driver.ColorMask = color_mask;
992 ctx->Driver.Enable = enable;
993
994
995 /* Initialize the TNL driver interface:
996 */
997 tnl = TNL_CONTEXT(ctx);
998 tnl->Driver.RunPipeline = _tnl_run_pipeline;
999
1000 dd->SetBuffer = set_buffer;
1001
1002 /* Install swsetup for tnl->Driver.Render.*:
1003 */
1004 _swsetup_Wakeup(ctx);
1005
1006 (void) DitherValues; /* silenced unused var warning */
1007 }
1008
1009
1010
1011
1012
1013 #define XMESA_NEW_POINT (_NEW_POINT | \
1014 _NEW_RENDERMODE | \
1015 _SWRAST_NEW_RASTERMASK)
1016
1017 #define XMESA_NEW_LINE (_NEW_LINE | \
1018 _NEW_TEXTURE | \
1019 _NEW_LIGHT | \
1020 _NEW_DEPTH | \
1021 _NEW_RENDERMODE | \
1022 _SWRAST_NEW_RASTERMASK)
1023
1024 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1025 _NEW_TEXTURE | \
1026 _NEW_LIGHT | \
1027 _NEW_DEPTH | \
1028 _NEW_RENDERMODE | \
1029 _SWRAST_NEW_RASTERMASK)
1030
1031
1032 /* Extend the software rasterizer with our line/point/triangle
1033 * functions.
1034 */
1035 void xmesa_register_swrast_functions( GLcontext *ctx )
1036 {
1037 SWcontext *swrast = SWRAST_CONTEXT( ctx );
1038
1039 swrast->choose_point = xmesa_choose_point;
1040 swrast->choose_line = xmesa_choose_line;
1041 swrast->choose_triangle = xmesa_choose_triangle;
1042
1043 swrast->invalidate_point |= XMESA_NEW_POINT;
1044 swrast->invalidate_line |= XMESA_NEW_LINE;
1045 swrast->invalidate_triangle |= XMESA_NEW_TRIANGLE;
1046 }