2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
26 #include "glxheader.h"
27 #include "bufferobj.h"
33 #include "extensions.h"
34 #include "framebuffer.h"
43 #include "texformat.h"
45 #include "array_cache/acache.h"
46 #include "swrast/swrast.h"
47 #include "swrast/s_context.h"
48 #include "swrast_setup/swrast_setup.h"
50 #include "tnl/t_context.h"
53 #include <GL/glxtokens.h>
59 * Dithering kernels and lookup tables.
62 const int xmesa_kernel8
[DITH_DY
* DITH_DX
] = {
63 0 * MAXC
, 8 * MAXC
, 2 * MAXC
, 10 * MAXC
,
64 12 * MAXC
, 4 * MAXC
, 14 * MAXC
, 6 * MAXC
,
65 3 * MAXC
, 11 * MAXC
, 1 * MAXC
, 9 * MAXC
,
66 15 * MAXC
, 7 * MAXC
, 13 * MAXC
, 5 * MAXC
,
69 const short xmesa_HPCR_DRGB
[3][2][16] = {
71 { 16, -4, 1,-11, 14, -6, 3, -9, 15, -5, 2,-10, 13, -7, 4, -8},
72 {-15, 5, 0, 12,-13, 7, -2, 10,-14, 6, -1, 11,-12, 8, -3, 9}
75 {-11, 15, -7, 3, -8, 14, -4, 2,-10, 16, -6, 4, -9, 13, -5, 1},
76 { 12,-14, 8, -2, 9,-13, 5, -1, 11,-15, 7, -3, 10,-12, 6, 0}
79 { 6,-18, 26,-14, 2,-22, 30,-10, 8,-16, 28,-12, 4,-20, 32, -8},
80 { -4, 20,-24, 16, 0, 24,-28, 12, -6, 18,-26, 14, -2, 22,-30, 10}
84 const int xmesa_kernel1
[16] = {
85 0*47, 9*47, 4*47, 12*47, /* 47 = (255*3)/16 */
86 6*47, 2*47, 14*47, 8*47,
87 10*47, 1*47, 5*47, 11*47,
88 7*47, 13*47, 3*47, 15*47
93 * Return the size (width, height) of the X window for the given GLframebuffer.
94 * Output: width - width of buffer in pixels.
95 * height - height of buffer in pixels.
98 get_buffer_size( GLframebuffer
*buffer
, GLuint
*width
, GLuint
*height
)
100 /* We can do this cast because the first field in the XMesaBuffer
101 * struct is a GLframebuffer struct. If this weren't true, we'd
102 * need a pointer from the GLframebuffer to the XMesaBuffer.
104 const XMesaBuffer xmBuffer
= (XMesaBuffer
) buffer
;
105 unsigned int winwidth
, winheight
;
107 /* XFree86 GLX renderer */
108 winwidth
= MIN2(xmBuffer
->frontxrb
->drawable
->width
, MAX_WIDTH
);
109 winheight
= MIN2(xmBuffer
->frontxrb
->drawable
->height
, MAX_HEIGHT
);
116 _glthread_LOCK_MUTEX(_xmesa_lock
);
117 XSync(xmBuffer
->xm_visual
->display
, 0); /* added for Chromium */
119 stat
= XGetGeometry( xmBuffer
->xm_visual
->display
, xmBuffer
->frontxrb
->pixmap
,
120 &root
, &winx
, &winy
, &winwidth
, &winheight
, &bw
, &d
);
121 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
124 /* probably querying a window that's recently been destroyed */
125 _mesa_warning(NULL
, "XGetGeometry failed!\n");
126 *width
= *height
= 1;
137 finish_or_flush( GLcontext
*ctx
)
142 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
144 _glthread_LOCK_MUTEX(_xmesa_lock
);
145 XSync( xmesa
->display
, False
);
146 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
153 clear_index( GLcontext
*ctx
, GLuint index
)
155 if (ctx
->DrawBuffer
->Name
== 0) {
156 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
157 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
158 xmesa
->clearpixel
= (unsigned long) index
;
159 XMesaSetForeground( xmesa
->display
, xmbuf
->cleargc
, (unsigned long) index
);
165 clear_color( GLcontext
*ctx
, const GLfloat color
[4] )
167 if (ctx
->DrawBuffer
->Name
== 0) {
168 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
169 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
171 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
172 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
173 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
174 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
175 xmesa
->clearpixel
= xmesa_color_to_pixel( ctx
,
176 xmesa
->clearcolor
[0],
177 xmesa
->clearcolor
[1],
178 xmesa
->clearcolor
[2],
179 xmesa
->clearcolor
[3],
180 xmesa
->xm_visual
->undithered_pf
);
181 _glthread_LOCK_MUTEX(_xmesa_lock
);
182 XMesaSetForeground( xmesa
->display
, xmbuf
->cleargc
,
184 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
190 /* Set index mask ala glIndexMask */
192 index_mask( GLcontext
*ctx
, GLuint mask
)
194 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
195 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
196 /* not sure this conditional is really needed */
197 if (xmbuf
->backxrb
&& xmbuf
->backxrb
->pixmap
) {
199 if (mask
==0xffffffff) {
200 m
= ((unsigned long)~0L);
203 m
= (unsigned long) mask
;
205 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->cleargc
, m
);
206 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->gc
, m
);
211 /* Implements glColorMask() */
213 color_mask(GLcontext
*ctx
,
214 GLboolean rmask
, GLboolean gmask
, GLboolean bmask
, GLboolean amask
)
216 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
218 const int xclass
= xmesa
->xm_visual
->mesa_visual
.visualType
;
221 if (ctx
->DrawBuffer
->Name
!= 0)
224 xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
226 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
228 if (rmask
&& gmask
&& bmask
) {
229 m
= ((unsigned long)~0L);
233 if (rmask
) m
|= GET_REDMASK(xmesa
->xm_visual
);
234 if (gmask
) m
|= GET_GREENMASK(xmesa
->xm_visual
);
235 if (bmask
) m
|= GET_BLUEMASK(xmesa
->xm_visual
);
237 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->cleargc
, m
);
238 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->gc
, m
);
244 /**********************************************************************/
245 /*** glClear implementations ***/
246 /**********************************************************************/
250 * Clear the front or back color buffer, if it's implemented with a pixmap.
253 clear_pixmap(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
, GLboolean all
,
254 GLint x
, GLint y
, GLint width
, GLint height
)
256 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
257 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
262 assert(xmesa
->display
);
264 assert(xmbuf
->cleargc
);
267 XMesaFillRectangle( xmesa
->display
, xrb
->pixmap
, xmbuf
->cleargc
,
268 0, 0, xrb
->Base
.Width
+ 1, xrb
->Base
.Height
+ 1 );
271 XMesaFillRectangle( xmesa
->display
, xrb
->pixmap
, xmbuf
->cleargc
,
272 x
, xrb
->Base
.Height
- y
- height
,
279 clear_8bit_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
280 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
282 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
285 const size_t n
= xrb
->ximage
->bytes_per_line
* xrb
->Base
.Height
;
286 MEMSET( xrb
->ximage
->data
, xmesa
->clearpixel
, n
);
290 for (i
=0;i
<height
;i
++) {
291 GLubyte
*ptr
= PIXEL_ADDR1(xrb
, x
, y
+ i
);
292 MEMSET( ptr
, xmesa
->clearpixel
, width
);
299 clear_HPCR_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
300 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
302 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
305 const GLuint c16
= xrb
->ximage
->bytes_per_line
& ~0xf;
307 GLubyte
*ptr
= (GLubyte
*) xrb
->ximage
->data
;
308 for (i
= 0; i
< xrb
->Base
.Height
; i
++) {
310 const GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
314 for (j
=0; j
<c16
; j
+=16) {
333 for (; j
< (GLuint
) xrb
->ximage
->bytes_per_line
; j
++) {
341 for (i
=y
; i
<y
+height
; i
++) {
342 GLubyte
*ptr
= PIXEL_ADDR1( xrb
, x
, i
);
344 const GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
348 for (j
=x
; j
<x
+width
; j
++) {
358 clear_16bit_ximage( GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
359 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
361 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
362 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
364 if (xmesa
->swapbytes
) {
365 pixel
= ((pixel
>> 8) & 0x00ff) | ((pixel
<< 8) & 0xff00);
369 GLuint
*ptr4
= (GLuint
*) xrb
->ximage
->data
;
370 if ((pixel
& 0xff) == ((pixel
>> 8) & 0xff)) {
371 /* low and high bytes are equal so use memset() */
372 const GLuint n
= xrb
->ximage
->bytes_per_line
* xrb
->Base
.Height
;
373 MEMSET( ptr4
, pixel
& 0xff, n
);
376 const GLuint n
= xrb
->ximage
->bytes_per_line
* xrb
->Base
.Height
/ 4;
378 pixel
= pixel
| (pixel
<<16);
379 for (i
= 0; i
< n
; i
++) {
383 /* might be one last GLushort to set */
384 if ((xrb
->ximage
->bytes_per_line
* xrb
->Base
.Height
) & 0x2)
385 *(GLushort
*)ptr4
= pixel
& 0xffff;
390 for (j
=0;j
<height
;j
++) {
391 GLushort
*ptr2
= PIXEL_ADDR2(xrb
, x
, y
+ j
);
392 for (i
=0;i
<width
;i
++) {
400 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
402 clear_24bit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
403 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
405 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
406 const GLubyte r
= xmesa
->clearcolor
[0];
407 const GLubyte g
= xmesa
->clearcolor
[1];
408 const GLubyte b
= xmesa
->clearcolor
[2];
411 if (r
== g
&& g
== b
) {
412 /* same value for all three components (gray) */
413 const GLint w3
= xrb
->Base
.Width
* 3;
414 const GLint h
= xrb
->Base
.Height
;
416 for (i
= 0; i
< h
; i
++) {
417 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, 0, i
);
423 const GLint w
= xrb
->Base
.Width
;
424 const GLint h
= xrb
->Base
.Height
;
426 for (i
= 0; i
< h
; i
++) {
427 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, 0, i
);
428 for (j
= 0; j
< w
; j
++) {
438 /* only clear subrect of color buffer */
439 if (r
== g
&& g
== b
) {
440 /* same value for all three components (gray) */
442 for (j
=0;j
<height
;j
++) {
443 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
444 MEMSET(ptr3
, r
, 3 * width
);
448 /* non-gray clear color */
450 for (j
= 0; j
< height
; j
++) {
451 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
452 for (i
= 0; i
< width
; i
++) {
465 clear_32bit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
466 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
468 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
469 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
474 if (xmesa
->swapbytes
) {
475 pixel
= ((pixel
>> 24) & 0x000000ff)
476 | ((pixel
>> 8) & 0x0000ff00)
477 | ((pixel
<< 8) & 0x00ff0000)
478 | ((pixel
<< 24) & 0xff000000);
482 const GLuint n
= xrb
->Base
.Width
* xrb
->Base
.Height
;
483 GLuint
*ptr4
= (GLuint
*) xrb
->ximage
->data
;
485 _mesa_memset(ptr4
, pixel
, 4 * n
);
489 for (i
= 0; i
< n
; i
++)
495 for (j
= 0; j
< height
; j
++) {
496 GLuint
*ptr4
= PIXEL_ADDR4(xrb
, x
, y
+ j
);
497 for (i
= 0; i
< width
; i
++) {
506 clear_nbit_ximage(GLcontext
*ctx
, struct xmesa_renderbuffer
*xrb
,
507 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
509 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
510 XMesaImage
*img
= xrb
->ximage
;
513 /* We can ignore 'all' here - x, y, width, height are always right */
516 /* TODO: optimize this */
518 for (j
= 0; j
< height
; j
++) {
519 for (i
= 0; i
< width
; i
++) {
520 XMesaPutPixel(img
, x
+i
, y
-j
, xmesa
->clearpixel
);
528 clear_buffers( GLcontext
*ctx
, GLbitfield mask
,
529 GLboolean all
, GLint x
, GLint y
, GLint width
, GLint height
)
531 if (ctx
->DrawBuffer
->Name
== 0) {
532 /* this is a window system framebuffer */
533 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
;
534 XMesaBuffer b
= XMESA_BUFFER(ctx
->DrawBuffer
);
536 /* we can't handle color or index masking */
537 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
538 if (mask
& BUFFER_BIT_FRONT_LEFT
) {
539 /* clear front color buffer */
540 struct gl_renderbuffer
*frontRb
541 = ctx
->DrawBuffer
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
;
542 if (b
->frontxrb
== xmesa_renderbuffer(frontRb
)) {
543 /* renderbuffer is not wrapped - great! */
544 b
->frontxrb
->clearFunc(ctx
, b
->frontxrb
, all
, x
, y
,
546 mask
&= ~BUFFER_BIT_FRONT_LEFT
;
549 /* we can't directly clear an alpha-wrapped color buffer */
552 if (mask
& BUFFER_BIT_BACK_LEFT
) {
553 /* clear back color buffer */
554 struct gl_renderbuffer
*backRb
555 = ctx
->DrawBuffer
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
;
556 if (b
->backxrb
== xmesa_renderbuffer(backRb
)) {
557 /* renderbuffer is not wrapped - great! */
558 b
->backxrb
->clearFunc(ctx
, b
->backxrb
, all
, x
, y
,
560 mask
&= ~BUFFER_BIT_BACK_LEFT
;
566 _swrast_Clear( ctx
, mask
, all
, x
, y
, width
, height
);
571 * Called by ctx->Driver.ResizeBuffers()
572 * Resize the front/back colorbuffers to match the latest window size.
575 xmesa_resize_buffers(GLcontext
*ctx
, GLframebuffer
*buffer
,
576 GLuint width
, GLuint height
)
578 /* We can do this cast because the first field in the XMesaBuffer
579 * struct is a GLframebuffer struct. If this weren't true, we'd
580 * need a pointer from the GLframebuffer to the XMesaBuffer.
582 XMesaBuffer xmBuffer
= (XMesaBuffer
) buffer
;
584 xmesa_alloc_back_buffer(xmBuffer
, width
, height
);
586 _mesa_resize_framebuffer(ctx
, buffer
, width
, height
);
590 #ifndef XFree86Server
591 /* XXX this was never tested in the Xserver environment */
594 * This function implements glDrawPixels() with an XPutImage call when
595 * drawing to the front buffer (X Window drawable).
596 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
599 xmesa_DrawPixels_8R8G8B( GLcontext
*ctx
,
600 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
601 GLenum format
, GLenum type
,
602 const struct gl_pixelstore_attrib
*unpack
,
603 const GLvoid
*pixels
)
605 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
606 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
607 struct xmesa_renderbuffer
*xrb
= (struct xmesa_renderbuffer
*) rb
->Wrapped
;
609 if (swrast
->NewState
)
610 _swrast_validate_derived( ctx
);
612 if (ctx
->DrawBuffer
->Name
== 0 &&
614 type
== GL_UNSIGNED_BYTE
&&
615 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
616 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
617 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
618 ctx
->Pixel
.ZoomY
== 1.0 &&
620 xrb
->Base
.AlphaBits
== 0)
622 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
623 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
624 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
625 const XMesaGC gc
= xmbuf
->gc
;
630 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
632 ASSERT(xmesa
->xm_visual
->dithered_pf
== PF_8R8G8B
);
633 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_8R8G8B
);
637 if (unpack
->BufferObj
->Name
) {
638 /* unpack from PBO */
640 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
641 format
, type
, pixels
)) {
642 _mesa_error(ctx
, GL_INVALID_OPERATION
,
643 "glDrawPixels(invalid PBO access)");
646 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
647 GL_PIXEL_UNPACK_BUFFER_EXT
,
651 /* buffer is already mapped - that's an error */
652 _mesa_error(ctx
, GL_INVALID_OPERATION
,
653 "glDrawPixels(PBO is mapped)");
656 pixels
= ADD_POINTERS(buf
, pixels
);
659 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
660 /* This is a little tricky since all coordinates up to now have
661 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
662 * so we have to carefully compute the Y coordinates/addresses here.
664 int srcX
= clippedUnpack
.SkipPixels
;
665 int srcY
= clippedUnpack
.SkipRows
;
666 int rowLength
= clippedUnpack
.RowLength
;
668 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
669 ximage
.width
= width
;
670 ximage
.height
= height
;
671 ximage
.format
= ZPixmap
;
672 ximage
.data
= (char *) pixels
673 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 4;
674 ximage
.byte_order
= LSBFirst
;
675 ximage
.bitmap_unit
= 32;
676 ximage
.bitmap_bit_order
= LSBFirst
;
677 ximage
.bitmap_pad
= 32;
679 ximage
.bytes_per_line
= -rowLength
* 4; /* negative to flip image */
680 ximage
.bits_per_pixel
= 32;
681 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
682 /* flip Y axis for dest position */
683 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
684 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
687 if (unpack
->BufferObj
->Name
) {
688 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
693 /* software fallback */
694 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
695 format
, type
, unpack
, pixels
);
702 * This function implements glDrawPixels() with an XPutImage call when
703 * drawing to the front buffer (X Window drawable). The image format
704 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
705 * match the PF_5R6G5B pixel format.
708 xmesa_DrawPixels_5R6G5B( GLcontext
*ctx
,
709 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
710 GLenum format
, GLenum type
,
711 const struct gl_pixelstore_attrib
*unpack
,
712 const GLvoid
*pixels
)
714 struct xmesa_renderbuffer
*xrb
715 = (struct xmesa_renderbuffer
*) ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
716 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
717 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
718 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
719 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
720 const XMesaGC gc
= xmbuf
->gc
;
724 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
);
726 if (swrast
->NewState
)
727 _swrast_validate_derived( ctx
);
731 type
== GL_UNSIGNED_SHORT_5_6_5
&&
732 !ctx
->Color
.DitherFlag
&& /* no dithering */
733 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
734 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
735 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
736 ctx
->Pixel
.ZoomY
== 1.0) {
741 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
743 if (unpack
->BufferObj
->Name
) {
744 /* unpack from PBO */
746 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
747 format
, type
, pixels
)) {
748 _mesa_error(ctx
, GL_INVALID_OPERATION
,
749 "glDrawPixels(invalid PBO access)");
752 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
753 GL_PIXEL_UNPACK_BUFFER_EXT
,
757 /* buffer is already mapped - that's an error */
758 _mesa_error(ctx
, GL_INVALID_OPERATION
,
759 "glDrawPixels(PBO is mapped)");
762 pixels
= ADD_POINTERS(buf
, pixels
);
765 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
766 /* This is a little tricky since all coordinates up to now have
767 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
768 * so we have to carefully compute the Y coordinates/addresses here.
770 int srcX
= clippedUnpack
.SkipPixels
;
771 int srcY
= clippedUnpack
.SkipRows
;
772 int rowLength
= clippedUnpack
.RowLength
;
774 MEMSET(&ximage
, 0, sizeof(XMesaImage
));
775 ximage
.width
= width
;
776 ximage
.height
= height
;
777 ximage
.format
= ZPixmap
;
778 ximage
.data
= (char *) pixels
779 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 2;
780 ximage
.byte_order
= LSBFirst
;
781 ximage
.bitmap_unit
= 16;
782 ximage
.bitmap_bit_order
= LSBFirst
;
783 ximage
.bitmap_pad
= 16;
785 ximage
.bytes_per_line
= -rowLength
* 2; /* negative to flip image */
786 ximage
.bits_per_pixel
= 16;
787 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
788 /* flip Y axis for dest position */
789 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
790 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
793 if (unpack
->BufferObj
->Name
) {
794 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
799 /* software fallback */
800 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
801 format
, type
, unpack
, pixels
);
808 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
809 * for the color buffer. Don't support zooming, pixel transfer, etc.
810 * We do support copying from one window to another, ala glXMakeCurrentRead.
813 xmesa_CopyPixels( GLcontext
*ctx
,
814 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
815 GLint destx
, GLint desty
, GLenum type
)
817 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
818 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
819 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
820 const XMesaGC gc
= ((XMesaBuffer
) ctx
->DrawBuffer
)->gc
;
821 struct xmesa_renderbuffer
*srcXrb
= (struct xmesa_renderbuffer
*)
822 ctx
->ReadBuffer
->_ColorReadBuffer
;
823 struct xmesa_renderbuffer
*dstXrb
= (struct xmesa_renderbuffer
*)
824 ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
829 if (swrast
->NewState
)
830 _swrast_validate_derived( ctx
);
832 if (ctx
->Color
.DrawBuffer
[0] == GL_FRONT
&&
833 ctx
->Pixel
.ReadBuffer
== GL_FRONT
&&
837 (swrast
->_RasterMask
& ~CLIP_BIT
) == 0 && /* no blend, z-test, etc */
838 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
839 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
840 ctx
->Pixel
.ZoomY
== 1.0) {
841 /* Note: we don't do any special clipping work here. We could,
842 * but X will do it for us.
844 srcy
= YFLIP(srcXrb
, srcy
) - height
+ 1;
845 desty
= YFLIP(dstXrb
, desty
) - height
+ 1;
846 XCopyArea(dpy
, srcXrb
->pixmap
, dstXrb
->pixmap
, gc
,
847 srcx
, srcy
, width
, height
, destx
, desty
);
850 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);
853 #endif /* XFree86Server */
858 * Every driver should implement a GetString function in order to
859 * return a meaningful GL_RENDERER string.
861 static const GLubyte
*
862 get_string( GLcontext
*ctx
, GLenum name
)
868 return (const GLubyte
*) "Mesa GLX Indirect";
870 return (const GLubyte
*) "Mesa X11";
874 return (const GLubyte
*) "Mesa project: www.mesa3d.org";
885 * We implement the glEnable function only because we care about
886 * dither enable/disable.
889 enable( GLcontext
*ctx
, GLenum pname
, GLboolean state
)
891 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
896 xmesa
->pixelformat
= xmesa
->xm_visual
->dithered_pf
;
898 xmesa
->pixelformat
= xmesa
->xm_visual
->undithered_pf
;
901 ; /* silence compiler warning */
907 clear_color_HPCR_ximage( GLcontext
*ctx
, const GLfloat color
[4] )
910 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
912 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
913 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
914 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
915 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
917 if (color
[0] == 0.0 && color
[1] == 0.0 && color
[2] == 0.0) {
919 MEMSET( xmesa
->xm_visual
->hpcr_clear_ximage_pattern
, 0x0 ,
920 sizeof(xmesa
->xm_visual
->hpcr_clear_ximage_pattern
));
923 /* build clear pattern */
924 for (i
=0; i
<16; i
++) {
925 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0][i
] =
927 xmesa
->clearcolor
[0],
928 xmesa
->clearcolor
[1],
929 xmesa
->clearcolor
[2]);
930 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[1][i
] =
932 xmesa
->clearcolor
[0],
933 xmesa
->clearcolor
[1],
934 xmesa
->clearcolor
[2]);
941 clear_color_HPCR_pixmap( GLcontext
*ctx
, const GLfloat color
[4] )
944 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
946 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
947 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
948 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
949 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
951 if (color
[0] == 0.0 && color
[1] == 0.0 && color
[2] == 0.0) {
953 for (i
=0; i
<16; i
++) {
954 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0, 0);
955 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1, 0);
959 for (i
=0; i
<16; i
++) {
960 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0,
962 xmesa
->clearcolor
[0],
963 xmesa
->clearcolor
[1],
964 xmesa
->clearcolor
[2]));
965 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1,
967 xmesa
->clearcolor
[0],
968 xmesa
->clearcolor
[1],
969 xmesa
->clearcolor
[2]));
972 /* change tile pixmap content */
973 XMesaPutImage(xmesa
->display
,
974 (XMesaDrawable
)xmesa
->xm_visual
->hpcr_clear_pixmap
,
975 XMESA_BUFFER(ctx
->DrawBuffer
)->cleargc
,
976 xmesa
->xm_visual
->hpcr_clear_ximage
, 0, 0, 0, 0, 16, 2);
981 * Called when the driver should update its state, based on the new_state
985 xmesa_update_state( GLcontext
*ctx
, GLbitfield new_state
)
987 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
989 /* Propagate statechange information to swrast and swrast_setup
990 * modules. The X11 driver has no internal GL-dependent state.
992 _swrast_InvalidateState( ctx
, new_state
);
993 _ac_InvalidateState( ctx
, new_state
);
994 _tnl_InvalidateState( ctx
, new_state
);
995 _swsetup_InvalidateState( ctx
, new_state
);
997 if (ctx
->DrawBuffer
->Name
!= 0)
1001 * GL_DITHER, GL_READ/DRAW_BUFFER, buffer binding state, etc. effect
1002 * renderbuffer span/clear funcs.
1004 if (new_state
& (_NEW_COLOR
| _NEW_PIXEL
| _NEW_BUFFERS
)) {
1005 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
1006 struct xmesa_renderbuffer
*front_xrb
, *back_xrb
;
1008 front_xrb
= xmbuf
->frontxrb
;
1010 xmesa_set_renderbuffer_funcs(front_xrb
, xmesa
->pixelformat
,
1011 xmesa
->xm_visual
->BitsPerPixel
);
1012 front_xrb
->clearFunc
= clear_pixmap
;
1015 back_xrb
= xmbuf
->backxrb
;
1017 xmesa_set_renderbuffer_funcs(back_xrb
, xmesa
->pixelformat
,
1018 xmesa
->xm_visual
->BitsPerPixel
);
1019 if (xmbuf
->backxrb
->pixmap
) {
1020 back_xrb
->clearFunc
= clear_pixmap
;
1023 switch (xmesa
->xm_visual
->BitsPerPixel
) {
1025 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
1026 back_xrb
->clearFunc
= clear_HPCR_ximage
;
1029 back_xrb
->clearFunc
= clear_8bit_ximage
;
1033 back_xrb
->clearFunc
= clear_16bit_ximage
;
1036 back_xrb
->clearFunc
= clear_24bit_ximage
;
1039 back_xrb
->clearFunc
= clear_32bit_ximage
;
1042 back_xrb
->clearFunc
= clear_nbit_ximage
;
1049 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
1050 /* this depends on whether we're drawing to the front or back buffer */
1054 ctx
->Driver
.ClearColor
= clear_color_HPCR_pixmap
;
1057 ctx
->Driver
.ClearColor
= clear_color_HPCR_ximage
;
1060 (void) clear_color_HPCR_pixmap
;
1061 (void) clear_color_HPCR_ximage
;
1069 * Called via ctx->Driver.TestProxyTeximage(). Normally, we'd just use
1070 * the _mesa_test_proxy_teximage() fallback function, but we're going to
1071 * special-case the 3D texture case to allow textures up to 512x512x32
1075 test_proxy_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
1076 GLint internalFormat
, GLenum format
, GLenum type
,
1077 GLint width
, GLint height
, GLint depth
, GLint border
)
1079 if (target
== GL_PROXY_TEXTURE_3D
) {
1080 /* special case for 3D textures */
1081 if (width
* height
* depth
> 512 * 512 * 64 ||
1082 width
< 2 * border
||
1083 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1084 _mesa_bitcount(width
- 2 * border
) != 1) ||
1085 height
< 2 * border
||
1086 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1087 _mesa_bitcount(height
- 2 * border
) != 1) ||
1088 depth
< 2 * border
||
1089 (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
1090 _mesa_bitcount(depth
- 2 * border
) != 1)) {
1091 /* Bad size, or too many texels */
1097 /* use the fallback routine for 1D, 2D, cube and rect targets */
1098 return _mesa_test_proxy_teximage(ctx
, target
, level
, internalFormat
,
1099 format
, type
, width
, height
, depth
,
1106 * In SW, we don't really compress GL_COMPRESSED_RGB[A] textures!
1108 static const struct gl_texture_format
*
1109 choose_tex_format( GLcontext
*ctx
, GLint internalFormat
,
1110 GLenum format
, GLenum type
)
1112 switch (internalFormat
) {
1113 case GL_COMPRESSED_RGB_ARB
:
1114 return &_mesa_texformat_rgb
;
1115 case GL_COMPRESSED_RGBA_ARB
:
1116 return &_mesa_texformat_rgba
;
1118 return _mesa_choose_tex_format(ctx
, internalFormat
, format
, type
);
1124 * Get the current drawing (and reading) window's size and update the
1125 * corresponding gl_framebuffer(s) if needed.
1128 update_framebuffer_size(GLcontext
*ctx
)
1130 struct gl_framebuffer
*fb
= ctx
->WinSysDrawBuffer
;
1131 GLuint newWidth
, newHeight
;
1132 get_buffer_size(fb
, &newWidth
, &newHeight
);
1133 if (newWidth
!= fb
->Width
|| newHeight
!= fb
->Height
) {
1134 xmesa_resize_buffers(ctx
, fb
, newWidth
, newHeight
);
1137 if (ctx
->WinSysReadBuffer
!= ctx
->WinSysDrawBuffer
) {
1138 /* Update readbuffer's size */
1139 struct gl_framebuffer
*fb
= ctx
->WinSysReadBuffer
;
1140 GLuint newWidth
, newHeight
;
1141 get_buffer_size(fb
, &newWidth
, &newHeight
);
1142 if (newWidth
!= fb
->Width
|| newHeight
!= fb
->Height
) {
1143 xmesa_resize_buffers(ctx
, fb
, newWidth
, newHeight
);
1144 ctx
->NewState
|= _NEW_BUFFERS
;
1151 * Called by glViewport.
1152 * This is a good time for us to poll the current X window size and adjust
1153 * our renderbuffers to match the current window size.
1154 * Remember, we have no opportunity to respond to conventional
1155 * X Resize/StructureNotify events since the X driver has no event loop.
1157 * Note that this trick isn't fool-proof. If the application never calls
1158 * glViewport, our notion of the current window size may be incorrect.
1159 * That problem led to the GLX_MESA_resize_buffers extension.
1162 xmesa_viewport(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
1168 update_framebuffer_size(ctx
);
1172 #if ENABLE_EXT_timer_query
1175 * The GL_EXT_timer_query extension is not enabled for the XServer
1176 * indirect renderer. Not sure about how/if wrapping of gettimeofday()
1180 struct xmesa_query_object
1182 struct gl_query_object Base
;
1183 struct timeval StartTime
;
1187 static struct gl_query_object
*
1188 xmesa_new_query_object(GLcontext
*ctx
, GLuint id
)
1190 struct xmesa_query_object
*q
= CALLOC_STRUCT(xmesa_query_object
);
1193 q
->Base
.Ready
= GL_TRUE
;
1200 xmesa_begin_query(GLcontext
*ctx
, GLenum target
, struct gl_query_object
*q
)
1202 if (target
== GL_TIME_ELAPSED_EXT
) {
1203 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1204 (void) gettimeofday(&xq
->StartTime
, NULL
);
1210 * Return the difference between the two given times in microseconds.
1213 #define suseconds_t unsigned int
1216 time_diff(const struct timeval
*t0
, const struct timeval
*t1
)
1218 GLuint64EXT seconds0
= t0
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1219 GLuint64EXT seconds1
= t1
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1220 GLuint64EXT nanosec0
= (seconds0
* 1000000 + t0
->tv_usec
) * 1000;
1221 GLuint64EXT nanosec1
= (seconds1
* 1000000 + t1
->tv_usec
) * 1000;
1222 return nanosec1
- nanosec0
;
1227 xmesa_end_query(GLcontext
*ctx
, GLenum target
, struct gl_query_object
*q
)
1229 if (target
== GL_TIME_ELAPSED_EXT
) {
1230 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1231 struct timeval endTime
;
1232 (void) gettimeofday(&endTime
, NULL
);
1233 /* result is in nanoseconds! */
1234 q
->Result
= time_diff(&xq
->StartTime
, &endTime
);
1239 #endif /* ENABLE_timer_query */
1243 * Initialize the device driver function table with the functions
1244 * we implement in this driver.
1247 xmesa_init_driver_functions( XMesaVisual xmvisual
,
1248 struct dd_function_table
*driver
)
1250 driver
->GetString
= get_string
;
1251 driver
->UpdateState
= xmesa_update_state
;
1252 driver
->GetBufferSize
= get_buffer_size
;
1253 driver
->Flush
= finish_or_flush
;
1254 driver
->Finish
= finish_or_flush
;
1255 driver
->ClearIndex
= clear_index
;
1256 driver
->ClearColor
= clear_color
;
1257 driver
->IndexMask
= index_mask
;
1258 driver
->ColorMask
= color_mask
;
1259 driver
->Enable
= enable
;
1260 driver
->Clear
= clear_buffers
;
1261 driver
->ResizeBuffers
= xmesa_resize_buffers
;
1262 driver
->Viewport
= xmesa_viewport
;
1263 #ifndef XFree86Server
1264 driver
->CopyPixels
= xmesa_CopyPixels
;
1265 if (xmvisual
->undithered_pf
== PF_8R8G8B
&&
1266 xmvisual
->dithered_pf
== PF_8R8G8B
) {
1267 driver
->DrawPixels
= xmesa_DrawPixels_8R8G8B
;
1269 else if (xmvisual
->undithered_pf
== PF_5R6G5B
) {
1270 driver
->DrawPixels
= xmesa_DrawPixels_5R6G5B
;
1273 driver
->TestProxyTexImage
= test_proxy_teximage
;
1274 #if ENABLE_EXT_texure_compression_s3tc
1275 driver
->ChooseTextureFormat
= choose_tex_format
;
1277 (void) choose_tex_format
;
1280 #if ENABLE_EXT_timer_query
1281 driver
->NewQueryObject
= xmesa_new_query_object
;
1282 driver
->BeginQuery
= xmesa_begin_query
;
1283 driver
->EndQuery
= xmesa_end_query
;
1288 #define XMESA_NEW_POINT (_NEW_POINT | \
1290 _SWRAST_NEW_RASTERMASK)
1292 #define XMESA_NEW_LINE (_NEW_LINE | \
1297 _SWRAST_NEW_RASTERMASK)
1299 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1304 _SWRAST_NEW_RASTERMASK)
1308 * Extend the software rasterizer with our line/point/triangle
1310 * Called during context creation only.
1312 void xmesa_register_swrast_functions( GLcontext
*ctx
)
1314 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1316 swrast
->choose_point
= xmesa_choose_point
;
1317 swrast
->choose_line
= xmesa_choose_line
;
1318 swrast
->choose_triangle
= xmesa_choose_triangle
;
1320 /* XXX these lines have no net effect. Remove??? */
1321 swrast
->InvalidatePointMask
|= XMESA_NEW_POINT
;
1322 swrast
->InvalidateLineMask
|= XMESA_NEW_LINE
;
1323 swrast
->InvalidateTriangleMask
|= XMESA_NEW_TRIANGLE
;