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.
28 * General device driver functions for Xlib driver.
31 #include "glxheader.h"
32 #include "main/bufferobj.h"
33 #include "main/buffers.h"
34 #include "main/context.h"
35 #include "main/colormac.h"
36 #include "main/depth.h"
37 #include "main/drawpix.h"
38 #include "main/extensions.h"
39 #include "main/framebuffer.h"
40 #include "main/macros.h"
41 #include "main/image.h"
42 #include "main/imports.h"
43 #include "main/mtypes.h"
45 #include "main/state.h"
46 #include "main/texobj.h"
47 #include "main/teximage.h"
48 #include "main/texstore.h"
49 #include "main/texformat.h"
50 #include "swrast/swrast.h"
51 #include "swrast/s_context.h"
52 #include "swrast_setup/swrast_setup.h"
54 #include "tnl/t_context.h"
55 #include "drivers/common/meta.h"
61 * Dithering kernels and lookup tables.
64 const int xmesa_kernel8
[DITH_DY
* DITH_DX
] = {
65 0 * MAXC
, 8 * MAXC
, 2 * MAXC
, 10 * MAXC
,
66 12 * MAXC
, 4 * MAXC
, 14 * MAXC
, 6 * MAXC
,
67 3 * MAXC
, 11 * MAXC
, 1 * MAXC
, 9 * MAXC
,
68 15 * MAXC
, 7 * MAXC
, 13 * MAXC
, 5 * MAXC
,
71 const short xmesa_HPCR_DRGB
[3][2][16] = {
73 { 16, -4, 1,-11, 14, -6, 3, -9, 15, -5, 2,-10, 13, -7, 4, -8},
74 {-15, 5, 0, 12,-13, 7, -2, 10,-14, 6, -1, 11,-12, 8, -3, 9}
77 {-11, 15, -7, 3, -8, 14, -4, 2,-10, 16, -6, 4, -9, 13, -5, 1},
78 { 12,-14, 8, -2, 9,-13, 5, -1, 11,-15, 7, -3, 10,-12, 6, 0}
81 { 6,-18, 26,-14, 2,-22, 30,-10, 8,-16, 28,-12, 4,-20, 32, -8},
82 { -4, 20,-24, 16, 0, 24,-28, 12, -6, 18,-26, 14, -2, 22,-30, 10}
86 const int xmesa_kernel1
[16] = {
87 0*47, 9*47, 4*47, 12*47, /* 47 = (255*3)/16 */
88 6*47, 2*47, 14*47, 8*47,
89 10*47, 1*47, 5*47, 11*47,
90 7*47, 13*47, 3*47, 15*47
95 finish_or_flush( struct gl_context
*ctx
)
97 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
99 _glthread_LOCK_MUTEX(_xmesa_lock
);
100 XSync( xmesa
->display
, False
);
101 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
107 clear_color( struct gl_context
*ctx
,
108 const union gl_color_union color
)
110 if (ctx
->DrawBuffer
->Name
== 0) {
111 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
112 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
114 _mesa_unclamped_float_rgba_to_ubyte(xmesa
->clearcolor
, color
.f
);
115 xmesa
->clearpixel
= xmesa_color_to_pixel( ctx
,
116 xmesa
->clearcolor
[0],
117 xmesa
->clearcolor
[1],
118 xmesa
->clearcolor
[2],
119 xmesa
->clearcolor
[3],
120 xmesa
->xm_visual
->undithered_pf
);
121 _glthread_LOCK_MUTEX(_xmesa_lock
);
122 XMesaSetForeground( xmesa
->display
, xmbuf
->cleargc
,
124 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
130 /* Implements glColorMask() */
132 color_mask(struct gl_context
*ctx
,
133 GLboolean rmask
, GLboolean gmask
, GLboolean bmask
, GLboolean amask
)
135 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
137 const int xclass
= xmesa
->xm_visual
->visualType
;
140 if (ctx
->DrawBuffer
->Name
!= 0)
143 xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
145 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
147 if (rmask
&& gmask
&& bmask
) {
148 m
= ((unsigned long)~0L);
152 if (rmask
) m
|= GET_REDMASK(xmesa
->xm_visual
);
153 if (gmask
) m
|= GET_GREENMASK(xmesa
->xm_visual
);
154 if (bmask
) m
|= GET_BLUEMASK(xmesa
->xm_visual
);
156 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->cleargc
, m
);
162 /**********************************************************************/
163 /*** glClear implementations ***/
164 /**********************************************************************/
168 * Clear the front or back color buffer, if it's implemented with a pixmap.
171 clear_pixmap(struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
172 GLint x
, GLint y
, GLint width
, GLint height
)
174 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
175 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
180 assert(xmesa
->display
);
182 assert(xmbuf
->cleargc
);
184 XMesaFillRectangle( xmesa
->display
, xrb
->pixmap
, xmbuf
->cleargc
,
185 x
, xrb
->Base
.Height
- y
- height
,
191 clear_8bit_ximage( struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
192 GLint x
, GLint y
, GLint width
, GLint height
)
194 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
196 for (i
= 0; i
< height
; i
++) {
197 GLubyte
*ptr
= PIXEL_ADDR1(xrb
, x
, y
+ i
);
198 memset( ptr
, xmesa
->clearpixel
, width
);
204 clear_HPCR_ximage( struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
205 GLint x
, GLint y
, GLint width
, GLint height
)
207 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
209 for (i
= y
; i
< y
+ height
; i
++) {
210 GLubyte
*ptr
= PIXEL_ADDR1( xrb
, x
, i
);
212 const GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
216 for (j
= x
; j
< x
+ width
; j
++) {
225 clear_16bit_ximage( struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
226 GLint x
, GLint y
, GLint width
, GLint height
)
228 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
229 GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
232 if (xmesa
->swapbytes
) {
233 pixel
= ((pixel
>> 8) & 0x00ff) | ((pixel
<< 8) & 0xff00);
236 for (j
= 0; j
< height
; j
++) {
237 GLushort
*ptr2
= PIXEL_ADDR2(xrb
, x
, y
+ j
);
238 for (i
= 0; i
< width
; i
++) {
245 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
247 clear_24bit_ximage(struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
248 GLint x
, GLint y
, GLint width
, GLint height
)
250 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
251 const GLubyte r
= xmesa
->clearcolor
[0];
252 const GLubyte g
= xmesa
->clearcolor
[1];
253 const GLubyte b
= xmesa
->clearcolor
[2];
255 if (r
== g
&& g
== b
) {
256 /* same value for all three components (gray) */
258 for (j
= 0; j
< height
; j
++) {
259 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
260 memset(ptr3
, r
, 3 * width
);
264 /* non-gray clear color */
266 for (j
= 0; j
< height
; j
++) {
267 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
268 for (i
= 0; i
< width
; i
++) {
280 clear_32bit_ximage(struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
281 GLint x
, GLint y
, GLint width
, GLint height
)
283 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
284 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
289 if (xmesa
->swapbytes
) {
290 pixel
= ((pixel
>> 24) & 0x000000ff)
291 | ((pixel
>> 8) & 0x0000ff00)
292 | ((pixel
<< 8) & 0x00ff0000)
293 | ((pixel
<< 24) & 0xff000000);
296 if (width
== xrb
->Base
.Width
&& height
== xrb
->Base
.Height
) {
297 /* clearing whole buffer */
298 const GLuint n
= xrb
->Base
.Width
* xrb
->Base
.Height
;
299 GLuint
*ptr4
= (GLuint
*) xrb
->ximage
->data
;
302 memset(ptr4
, pixel
, 4 * n
);
306 for (i
= 0; i
< n
; i
++)
311 /* clearing scissored region */
313 for (j
= 0; j
< height
; j
++) {
314 GLuint
*ptr4
= PIXEL_ADDR4(xrb
, x
, y
+ j
);
315 for (i
= 0; i
< width
; i
++) {
324 clear_nbit_ximage(struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
325 GLint x
, GLint y
, GLint width
, GLint height
)
327 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
328 XMesaImage
*img
= xrb
->ximage
;
331 /* TODO: optimize this */
333 for (j
= 0; j
< height
; j
++) {
334 for (i
= 0; i
< width
; i
++) {
335 XMesaPutPixel(img
, x
+i
, y
-j
, xmesa
->clearpixel
);
343 clear_buffers(struct gl_context
*ctx
, GLbitfield buffers
)
345 if (ctx
->DrawBuffer
->Name
== 0) {
346 /* this is a window system framebuffer */
347 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
[0];
348 XMesaBuffer b
= XMESA_BUFFER(ctx
->DrawBuffer
);
349 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
350 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
351 const GLint width
= ctx
->DrawBuffer
->_Xmax
- x
;
352 const GLint height
= ctx
->DrawBuffer
->_Ymax
- y
;
354 /* we can't handle color or index masking */
355 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
356 if (buffers
& BUFFER_BIT_FRONT_LEFT
) {
357 /* clear front color buffer */
358 struct gl_renderbuffer
*frontRb
359 = ctx
->DrawBuffer
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
;
360 if (b
->frontxrb
== xmesa_renderbuffer(frontRb
)) {
361 /* renderbuffer is not wrapped - great! */
362 b
->frontxrb
->clearFunc(ctx
, b
->frontxrb
, x
, y
, width
, height
);
363 buffers
&= ~BUFFER_BIT_FRONT_LEFT
;
366 /* we can't directly clear an alpha-wrapped color buffer */
369 if (buffers
& BUFFER_BIT_BACK_LEFT
) {
370 /* clear back color buffer */
371 struct gl_renderbuffer
*backRb
372 = ctx
->DrawBuffer
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
;
373 if (b
->backxrb
== xmesa_renderbuffer(backRb
)) {
374 /* renderbuffer is not wrapped - great! */
375 b
->backxrb
->clearFunc(ctx
, b
->backxrb
, x
, y
, width
, height
);
376 buffers
&= ~BUFFER_BIT_BACK_LEFT
;
382 _swrast_Clear(ctx
, buffers
);
386 /* XXX these functions haven't been tested in the Xserver environment */
390 * Check if we can do an optimized glDrawPixels into an 8R8G8B visual.
393 can_do_DrawPixels_8R8G8B(struct gl_context
*ctx
, GLenum format
, GLenum type
)
395 if (format
== GL_BGRA
&&
396 type
== GL_UNSIGNED_BYTE
&&
398 ctx
->DrawBuffer
->Name
== 0 &&
399 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
400 ctx
->Pixel
.ZoomY
== 1.0 &&
401 ctx
->_ImageTransferState
== 0 /* no color tables, scale/bias, etc */) {
402 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
404 if (swrast
->NewState
)
405 _swrast_validate_derived( ctx
);
407 if ((swrast
->_RasterMask
& ~CLIP_BIT
) == 0) /* no blend, z-test, etc */ {
408 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0];
410 struct xmesa_renderbuffer
*xrb
= xmesa_renderbuffer(rb
->Wrapped
);
412 xrb
->pixmap
&& /* drawing to pixmap or window */
413 _mesa_get_format_bits(xrb
->Base
.Format
, GL_ALPHA_BITS
) == 0) {
424 * This function implements glDrawPixels() with an XPutImage call when
425 * drawing to the front buffer (X Window drawable).
426 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
429 xmesa_DrawPixels_8R8G8B( struct gl_context
*ctx
,
430 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
431 GLenum format
, GLenum type
,
432 const struct gl_pixelstore_attrib
*unpack
,
433 const GLvoid
*pixels
)
435 if (can_do_DrawPixels_8R8G8B(ctx
, format
, type
)) {
436 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
437 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
443 if (swrast
->NewState
)
444 _swrast_validate_derived( ctx
);
446 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
447 /* unpack from PBO */
449 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
450 format
, type
, INT_MAX
, pixels
)) {
451 _mesa_error(ctx
, GL_INVALID_OPERATION
,
452 "glDrawPixels(invalid PBO access)");
455 buf
= (GLubyte
*) ctx
->Driver
.MapBufferRange(ctx
, 0,
456 unpack
->BufferObj
->Size
,
460 /* buffer is already mapped - that's an error */
461 _mesa_error(ctx
, GL_INVALID_OPERATION
,
462 "glDrawPixels(PBO is mapped)");
465 pixels
= ADD_POINTERS(buf
, pixels
);
468 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
469 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
470 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
471 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
472 const XMesaGC gc
= xmbuf
->cleargc
; /* effected by glColorMask */
473 struct xmesa_renderbuffer
*xrb
474 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]->Wrapped
);
475 const int srcX
= clippedUnpack
.SkipPixels
;
476 const int srcY
= clippedUnpack
.SkipRows
;
477 const int rowLength
= clippedUnpack
.RowLength
;
480 ASSERT(xmesa
->xm_visual
->dithered_pf
== PF_8R8G8B
);
481 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_8R8G8B
);
485 /* This is a little tricky since all coordinates up to now have
486 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
487 * so we have to carefully compute the Y coordinates/addresses here.
489 memset(&ximage
, 0, sizeof(XMesaImage
));
490 ximage
.width
= width
;
491 ximage
.height
= height
;
492 ximage
.format
= ZPixmap
;
493 ximage
.data
= (char *) pixels
494 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 4;
495 ximage
.byte_order
= LSBFirst
;
496 ximage
.bitmap_unit
= 32;
497 ximage
.bitmap_bit_order
= LSBFirst
;
498 ximage
.bitmap_pad
= 32;
500 ximage
.bits_per_pixel
= 32;
501 ximage
.bytes_per_line
= -rowLength
* 4; /* negative to flip image */
502 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
503 /* flip Y axis for dest position */
504 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
505 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
508 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
509 ctx
->Driver
.UnmapBuffer(ctx
, unpack
->BufferObj
);
513 /* software fallback */
514 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
515 format
, type
, unpack
, pixels
);
522 * Check if we can do an optimized glDrawPixels into an 5R6G5B visual.
525 can_do_DrawPixels_5R6G5B(struct gl_context
*ctx
, GLenum format
, GLenum type
)
527 if (format
== GL_RGB
&&
528 type
== GL_UNSIGNED_SHORT_5_6_5
&&
529 !ctx
->Color
.DitherFlag
&& /* no dithering */
531 ctx
->DrawBuffer
->Name
== 0 &&
532 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
533 ctx
->Pixel
.ZoomY
== 1.0 &&
534 ctx
->_ImageTransferState
== 0 /* no color tables, scale/bias, etc */) {
535 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
537 if (swrast
->NewState
)
538 _swrast_validate_derived( ctx
);
540 if ((swrast
->_RasterMask
& ~CLIP_BIT
) == 0) /* no blend, z-test, etc */ {
541 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0];
543 struct xmesa_renderbuffer
*xrb
= xmesa_renderbuffer(rb
->Wrapped
);
545 xrb
->pixmap
&& /* drawing to pixmap or window */
546 _mesa_get_format_bits(xrb
->Base
.Format
, GL_ALPHA_BITS
) == 0) {
557 * This function implements glDrawPixels() with an XPutImage call when
558 * drawing to the front buffer (X Window drawable). The image format
559 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
560 * match the PF_5R6G5B pixel format.
563 xmesa_DrawPixels_5R6G5B( struct gl_context
*ctx
,
564 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
565 GLenum format
, GLenum type
,
566 const struct gl_pixelstore_attrib
*unpack
,
567 const GLvoid
*pixels
)
569 if (can_do_DrawPixels_5R6G5B(ctx
, format
, type
)) {
570 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
571 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
577 if (swrast
->NewState
)
578 _swrast_validate_derived( ctx
);
580 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
581 /* unpack from PBO */
583 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
584 format
, type
, INT_MAX
, pixels
)) {
585 _mesa_error(ctx
, GL_INVALID_OPERATION
,
586 "glDrawPixels(invalid PBO access)");
589 buf
= (GLubyte
*) ctx
->Driver
.MapBufferRange(ctx
, 0,
590 unpack
->BufferObj
->Size
,
594 /* buffer is already mapped - that's an error */
595 _mesa_error(ctx
, GL_INVALID_OPERATION
,
596 "glDrawPixels(PBO is mapped)");
599 pixels
= ADD_POINTERS(buf
, pixels
);
602 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
603 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
604 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
605 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
606 const XMesaGC gc
= xmbuf
->cleargc
; /* effected by glColorMask */
607 struct xmesa_renderbuffer
*xrb
608 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]->Wrapped
);
609 const int srcX
= clippedUnpack
.SkipPixels
;
610 const int srcY
= clippedUnpack
.SkipRows
;
611 const int rowLength
= clippedUnpack
.RowLength
;
614 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
);
618 /* This is a little tricky since all coordinates up to now have
619 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
620 * so we have to carefully compute the Y coordinates/addresses here.
622 memset(&ximage
, 0, sizeof(XMesaImage
));
623 ximage
.width
= width
;
624 ximage
.height
= height
;
625 ximage
.format
= ZPixmap
;
626 ximage
.data
= (char *) pixels
627 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 2;
628 ximage
.byte_order
= LSBFirst
;
629 ximage
.bitmap_unit
= 16;
630 ximage
.bitmap_bit_order
= LSBFirst
;
631 ximage
.bitmap_pad
= 16;
633 ximage
.bits_per_pixel
= 16;
634 ximage
.bytes_per_line
= -rowLength
* 2; /* negative to flip image */
635 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
636 /* flip Y axis for dest position */
637 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
638 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
641 if (unpack
->BufferObj
->Name
) {
642 ctx
->Driver
.UnmapBuffer(ctx
, unpack
->BufferObj
);
646 /* software fallback */
647 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
648 format
, type
, unpack
, pixels
);
654 * Determine if we can do an optimized glCopyPixels.
657 can_do_CopyPixels(struct gl_context
*ctx
, GLenum type
)
659 if (type
== GL_COLOR
&&
660 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
661 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
662 ctx
->Pixel
.ZoomY
== 1.0 &&
663 ctx
->Color
.DrawBuffer
[0] == GL_FRONT
&& /* copy to front buf */
664 ctx
->Pixel
.ReadBuffer
== GL_FRONT
&& /* copy from front buf */
665 ctx
->ReadBuffer
->_ColorReadBuffer
&&
666 ctx
->DrawBuffer
->_ColorDrawBuffers
[0]) {
667 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
669 if (swrast
->NewState
)
670 _swrast_validate_derived( ctx
);
672 if ((swrast
->_RasterMask
& ~CLIP_BIT
) == 0x0 &&
674 ctx
->ReadBuffer
->_ColorReadBuffer
&&
676 ctx
->DrawBuffer
->_ColorDrawBuffers
[0]) {
677 struct xmesa_renderbuffer
*srcXrb
678 = xmesa_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
->Wrapped
);
679 struct xmesa_renderbuffer
*dstXrb
680 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]->Wrapped
);
681 if (srcXrb
->pixmap
&& dstXrb
->pixmap
) {
691 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
692 * for the color buffer. Don't support zooming, pixel transfer, etc.
693 * We do support copying from one window to another, ala glXMakeCurrentRead.
696 xmesa_CopyPixels( struct gl_context
*ctx
,
697 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
698 GLint destx
, GLint desty
, GLenum type
)
700 if (can_do_CopyPixels(ctx
, type
)) {
701 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
702 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
703 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
704 const XMesaGC gc
= xmbuf
->cleargc
; /* effected by glColorMask */
705 struct xmesa_renderbuffer
*srcXrb
706 = xmesa_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
->Wrapped
);
707 struct xmesa_renderbuffer
*dstXrb
708 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]->Wrapped
);
713 /* Note: we don't do any special clipping work here. We could,
714 * but X will do it for us.
716 srcy
= YFLIP(srcXrb
, srcy
) - height
+ 1;
717 desty
= YFLIP(dstXrb
, desty
) - height
+ 1;
718 XCopyArea(dpy
, srcXrb
->pixmap
, dstXrb
->pixmap
, gc
,
719 srcx
, srcy
, width
, height
, destx
, desty
);
722 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);
730 * Every driver should implement a GetString function in order to
731 * return a meaningful GL_RENDERER string.
733 static const GLubyte
*
734 get_string( struct gl_context
*ctx
, GLenum name
)
739 return (const GLubyte
*) "Mesa X11";
749 * We implement the glEnable function only because we care about
750 * dither enable/disable.
753 enable( struct gl_context
*ctx
, GLenum pname
, GLboolean state
)
755 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
760 xmesa
->pixelformat
= xmesa
->xm_visual
->dithered_pf
;
762 xmesa
->pixelformat
= xmesa
->xm_visual
->undithered_pf
;
765 ; /* silence compiler warning */
771 clear_color_HPCR_ximage( struct gl_context
*ctx
,
772 const union gl_color_union color
)
775 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
777 _mesa_unclamped_float_rgba_to_ubyte(xmesa
->clearcolor
, color
.f
);
779 if (color
.f
[0] == 0.0 && color
.f
[1] == 0.0 && color
.f
[2] == 0.0) {
781 memset( xmesa
->xm_visual
->hpcr_clear_ximage_pattern
, 0x0 ,
782 sizeof(xmesa
->xm_visual
->hpcr_clear_ximage_pattern
));
785 /* build clear pattern */
786 for (i
=0; i
<16; i
++) {
787 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0][i
] =
789 xmesa
->clearcolor
[0],
790 xmesa
->clearcolor
[1],
791 xmesa
->clearcolor
[2]);
792 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[1][i
] =
794 xmesa
->clearcolor
[0],
795 xmesa
->clearcolor
[1],
796 xmesa
->clearcolor
[2]);
803 clear_color_HPCR_pixmap( struct gl_context
*ctx
,
804 const union gl_color_union color
)
807 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
809 _mesa_unclamped_float_rgba_to_ubyte(xmesa
->clearcolor
, color
.f
);
811 if (color
.f
[0] == 0.0 && color
.f
[1] == 0.0 && color
.f
[2] == 0.0) {
813 for (i
=0; i
<16; i
++) {
814 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0, 0);
815 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1, 0);
819 for (i
=0; i
<16; i
++) {
820 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0,
822 xmesa
->clearcolor
[0],
823 xmesa
->clearcolor
[1],
824 xmesa
->clearcolor
[2]));
825 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1,
827 xmesa
->clearcolor
[0],
828 xmesa
->clearcolor
[1],
829 xmesa
->clearcolor
[2]));
832 /* change tile pixmap content */
833 XMesaPutImage(xmesa
->display
,
834 (XMesaDrawable
)xmesa
->xm_visual
->hpcr_clear_pixmap
,
835 XMESA_BUFFER(ctx
->DrawBuffer
)->cleargc
,
836 xmesa
->xm_visual
->hpcr_clear_ximage
, 0, 0, 0, 0, 16, 2);
841 * Called when the driver should update its state, based on the new_state
845 xmesa_update_state( struct gl_context
*ctx
, GLbitfield new_state
)
847 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
849 /* Propagate statechange information to swrast and swrast_setup
850 * modules. The X11 driver has no internal GL-dependent state.
852 _swrast_InvalidateState( ctx
, new_state
);
853 _tnl_InvalidateState( ctx
, new_state
);
854 _vbo_InvalidateState( ctx
, new_state
);
855 _swsetup_InvalidateState( ctx
, new_state
);
857 if (ctx
->DrawBuffer
->Name
!= 0)
861 * GL_DITHER, GL_READ/DRAW_BUFFER, buffer binding state, etc. effect
862 * renderbuffer span/clear funcs.
863 * Check _NEW_COLOR to detect dither enable/disable.
865 if (new_state
& (_NEW_COLOR
| _NEW_BUFFERS
)) {
866 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
867 struct xmesa_renderbuffer
*front_xrb
, *back_xrb
;
869 front_xrb
= xmbuf
->frontxrb
;
871 xmesa_set_renderbuffer_funcs(front_xrb
, xmesa
->pixelformat
,
872 xmesa
->xm_visual
->BitsPerPixel
);
873 front_xrb
->clearFunc
= clear_pixmap
;
876 back_xrb
= xmbuf
->backxrb
;
878 xmesa_set_renderbuffer_funcs(back_xrb
, xmesa
->pixelformat
,
879 xmesa
->xm_visual
->BitsPerPixel
);
880 if (xmbuf
->backxrb
->pixmap
) {
881 back_xrb
->clearFunc
= clear_pixmap
;
884 switch (xmesa
->xm_visual
->BitsPerPixel
) {
886 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
887 back_xrb
->clearFunc
= clear_HPCR_ximage
;
890 back_xrb
->clearFunc
= clear_8bit_ximage
;
894 back_xrb
->clearFunc
= clear_16bit_ximage
;
897 back_xrb
->clearFunc
= clear_24bit_ximage
;
900 back_xrb
->clearFunc
= clear_32bit_ximage
;
903 back_xrb
->clearFunc
= clear_nbit_ximage
;
910 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
911 /* this depends on whether we're drawing to the front or back buffer */
915 ctx
->Driver
.ClearColor
= clear_color_HPCR_pixmap
;
918 ctx
->Driver
.ClearColor
= clear_color_HPCR_ximage
;
921 (void) clear_color_HPCR_pixmap
;
922 (void) clear_color_HPCR_ximage
;
930 * In SW, we don't really compress GL_COMPRESSED_RGB[A] textures!
933 choose_tex_format( struct gl_context
*ctx
, GLint internalFormat
,
934 GLenum format
, GLenum type
)
936 switch (internalFormat
) {
937 case GL_COMPRESSED_RGB_ARB
:
938 return MESA_FORMAT_RGB888
;
939 case GL_COMPRESSED_RGBA_ARB
:
940 return MESA_FORMAT_RGBA8888
;
942 return _mesa_choose_tex_format(ctx
, internalFormat
, format
, type
);
948 * Called by glViewport.
949 * This is a good time for us to poll the current X window size and adjust
950 * our renderbuffers to match the current window size.
951 * Remember, we have no opportunity to respond to conventional
952 * X Resize/StructureNotify events since the X driver has no event loop.
954 * Note that this trick isn't fool-proof. If the application never calls
955 * glViewport, our notion of the current window size may be incorrect.
956 * That problem led to the GLX_MESA_resize_buffers extension.
959 xmesa_viewport(struct gl_context
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
961 XMesaContext xmctx
= XMESA_CONTEXT(ctx
);
962 XMesaBuffer xmdrawbuf
= XMESA_BUFFER(ctx
->WinSysDrawBuffer
);
963 XMesaBuffer xmreadbuf
= XMESA_BUFFER(ctx
->WinSysReadBuffer
);
964 xmesa_check_and_update_buffer_size(xmctx
, xmdrawbuf
);
965 xmesa_check_and_update_buffer_size(xmctx
, xmreadbuf
);
973 #if ENABLE_EXT_timer_query
976 * The GL_EXT_timer_query extension is not enabled for the XServer
977 * indirect renderer. Not sure about how/if wrapping of gettimeofday()
981 struct xmesa_query_object
983 struct gl_query_object Base
;
984 struct timeval StartTime
;
988 static struct gl_query_object
*
989 xmesa_new_query_object(struct gl_context
*ctx
, GLuint id
)
991 struct xmesa_query_object
*q
= CALLOC_STRUCT(xmesa_query_object
);
994 q
->Base
.Ready
= GL_TRUE
;
1001 xmesa_begin_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
1003 if (q
->Target
== GL_TIME_ELAPSED_EXT
) {
1004 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1005 (void) gettimeofday(&xq
->StartTime
, NULL
);
1011 * Return the difference between the two given times in microseconds.
1014 #define suseconds_t unsigned int
1017 time_diff(const struct timeval
*t0
, const struct timeval
*t1
)
1019 GLuint64EXT seconds0
= t0
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1020 GLuint64EXT seconds1
= t1
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1021 GLuint64EXT nanosec0
= (seconds0
* 1000000 + t0
->tv_usec
) * 1000;
1022 GLuint64EXT nanosec1
= (seconds1
* 1000000 + t1
->tv_usec
) * 1000;
1023 return nanosec1
- nanosec0
;
1028 xmesa_end_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
1030 if (q
->Target
== GL_TIME_ELAPSED_EXT
) {
1031 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1032 struct timeval endTime
;
1033 (void) gettimeofday(&endTime
, NULL
);
1034 /* result is in nanoseconds! */
1035 q
->Result
= time_diff(&xq
->StartTime
, &endTime
);
1040 #endif /* ENABLE_timer_query */
1044 * Initialize the device driver function table with the functions
1045 * we implement in this driver.
1048 xmesa_init_driver_functions( XMesaVisual xmvisual
,
1049 struct dd_function_table
*driver
)
1051 driver
->GetString
= get_string
;
1052 driver
->UpdateState
= xmesa_update_state
;
1053 driver
->GetBufferSize
= NULL
; /* OBSOLETE */
1054 driver
->Flush
= finish_or_flush
;
1055 driver
->Finish
= finish_or_flush
;
1056 driver
->ClearColor
= clear_color
;
1057 driver
->ColorMask
= color_mask
;
1058 driver
->Enable
= enable
;
1059 driver
->Viewport
= xmesa_viewport
;
1060 if (TEST_META_FUNCS
) {
1061 driver
->Clear
= _mesa_meta_Clear
;
1062 driver
->CopyPixels
= _mesa_meta_CopyPixels
;
1063 driver
->BlitFramebuffer
= _mesa_meta_BlitFramebuffer
;
1064 driver
->DrawPixels
= _mesa_meta_DrawPixels
;
1065 driver
->Bitmap
= _mesa_meta_Bitmap
;
1068 driver
->Clear
= clear_buffers
;
1069 driver
->CopyPixels
= xmesa_CopyPixels
;
1070 if (xmvisual
->undithered_pf
== PF_8R8G8B
&&
1071 xmvisual
->dithered_pf
== PF_8R8G8B
&&
1072 xmvisual
->BitsPerPixel
== 32) {
1073 driver
->DrawPixels
= xmesa_DrawPixels_8R8G8B
;
1075 else if (xmvisual
->undithered_pf
== PF_5R6G5B
) {
1076 driver
->DrawPixels
= xmesa_DrawPixels_5R6G5B
;
1080 #if ENABLE_EXT_texure_compression_s3tc
1081 driver
->ChooseTextureFormat
= choose_tex_format
;
1083 (void) choose_tex_format
;
1086 #if ENABLE_EXT_timer_query
1087 driver
->NewQueryObject
= xmesa_new_query_object
;
1088 driver
->BeginQuery
= xmesa_begin_query
;
1089 driver
->EndQuery
= xmesa_end_query
;
1094 #define XMESA_NEW_POINT (_NEW_POINT | \
1096 _SWRAST_NEW_RASTERMASK)
1098 #define XMESA_NEW_LINE (_NEW_LINE | \
1103 _SWRAST_NEW_RASTERMASK)
1105 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1110 _SWRAST_NEW_RASTERMASK)
1114 * Extend the software rasterizer with our line/point/triangle
1116 * Called during context creation only.
1118 void xmesa_register_swrast_functions( struct gl_context
*ctx
)
1120 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1122 swrast
->choose_point
= xmesa_choose_point
;
1123 swrast
->choose_line
= xmesa_choose_line
;
1124 swrast
->choose_triangle
= xmesa_choose_triangle
;
1126 /* XXX these lines have no net effect. Remove??? */
1127 swrast
->InvalidatePointMask
|= XMESA_NEW_POINT
;
1128 swrast
->InvalidateLineMask
|= XMESA_NEW_LINE
;
1129 swrast
->InvalidateTriangleMask
|= XMESA_NEW_TRIANGLE
;