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
, const GLfloat color
[4] )
109 if (ctx
->DrawBuffer
->Name
== 0) {
110 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
111 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
113 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
114 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
115 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
116 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
117 xmesa
->clearpixel
= xmesa_color_to_pixel( ctx
,
118 xmesa
->clearcolor
[0],
119 xmesa
->clearcolor
[1],
120 xmesa
->clearcolor
[2],
121 xmesa
->clearcolor
[3],
122 xmesa
->xm_visual
->undithered_pf
);
123 _glthread_LOCK_MUTEX(_xmesa_lock
);
124 XMesaSetForeground( xmesa
->display
, xmbuf
->cleargc
,
126 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
132 /* Implements glColorMask() */
134 color_mask(struct gl_context
*ctx
,
135 GLboolean rmask
, GLboolean gmask
, GLboolean bmask
, GLboolean amask
)
137 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
139 const int xclass
= xmesa
->xm_visual
->visualType
;
142 if (ctx
->DrawBuffer
->Name
!= 0)
145 xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
147 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
149 if (rmask
&& gmask
&& bmask
) {
150 m
= ((unsigned long)~0L);
154 if (rmask
) m
|= GET_REDMASK(xmesa
->xm_visual
);
155 if (gmask
) m
|= GET_GREENMASK(xmesa
->xm_visual
);
156 if (bmask
) m
|= GET_BLUEMASK(xmesa
->xm_visual
);
158 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->cleargc
, m
);
164 /**********************************************************************/
165 /*** glClear implementations ***/
166 /**********************************************************************/
170 * Clear the front or back color buffer, if it's implemented with a pixmap.
173 clear_pixmap(struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
174 GLint x
, GLint y
, GLint width
, GLint height
)
176 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
177 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
182 assert(xmesa
->display
);
184 assert(xmbuf
->cleargc
);
186 XMesaFillRectangle( xmesa
->display
, xrb
->pixmap
, xmbuf
->cleargc
,
187 x
, xrb
->Base
.Height
- y
- height
,
193 clear_8bit_ximage( struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
194 GLint x
, GLint y
, GLint width
, GLint height
)
196 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
198 for (i
= 0; i
< height
; i
++) {
199 GLubyte
*ptr
= PIXEL_ADDR1(xrb
, x
, y
+ i
);
200 memset( ptr
, xmesa
->clearpixel
, width
);
206 clear_HPCR_ximage( struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
207 GLint x
, GLint y
, GLint width
, GLint height
)
209 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
211 for (i
= y
; i
< y
+ height
; i
++) {
212 GLubyte
*ptr
= PIXEL_ADDR1( xrb
, x
, i
);
214 const GLubyte
*sptr
= xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0];
218 for (j
= x
; j
< x
+ width
; j
++) {
227 clear_16bit_ximage( struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
228 GLint x
, GLint y
, GLint width
, GLint height
)
230 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
231 GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
234 if (xmesa
->swapbytes
) {
235 pixel
= ((pixel
>> 8) & 0x00ff) | ((pixel
<< 8) & 0xff00);
238 for (j
= 0; j
< height
; j
++) {
239 GLushort
*ptr2
= PIXEL_ADDR2(xrb
, x
, y
+ j
);
240 for (i
= 0; i
< width
; i
++) {
247 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
249 clear_24bit_ximage(struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
250 GLint x
, GLint y
, GLint width
, GLint height
)
252 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
253 const GLubyte r
= xmesa
->clearcolor
[0];
254 const GLubyte g
= xmesa
->clearcolor
[1];
255 const GLubyte b
= xmesa
->clearcolor
[2];
257 if (r
== g
&& g
== b
) {
258 /* same value for all three components (gray) */
260 for (j
= 0; j
< height
; j
++) {
261 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
262 memset(ptr3
, r
, 3 * width
);
266 /* non-gray clear color */
268 for (j
= 0; j
< height
; j
++) {
269 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
270 for (i
= 0; i
< width
; i
++) {
282 clear_32bit_ximage(struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
283 GLint x
, GLint y
, GLint width
, GLint height
)
285 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
286 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
291 if (xmesa
->swapbytes
) {
292 pixel
= ((pixel
>> 24) & 0x000000ff)
293 | ((pixel
>> 8) & 0x0000ff00)
294 | ((pixel
<< 8) & 0x00ff0000)
295 | ((pixel
<< 24) & 0xff000000);
298 if (width
== xrb
->Base
.Width
&& height
== xrb
->Base
.Height
) {
299 /* clearing whole buffer */
300 const GLuint n
= xrb
->Base
.Width
* xrb
->Base
.Height
;
301 GLuint
*ptr4
= (GLuint
*) xrb
->ximage
->data
;
304 memset(ptr4
, pixel
, 4 * n
);
308 for (i
= 0; i
< n
; i
++)
313 /* clearing scissored region */
315 for (j
= 0; j
< height
; j
++) {
316 GLuint
*ptr4
= PIXEL_ADDR4(xrb
, x
, y
+ j
);
317 for (i
= 0; i
< width
; i
++) {
326 clear_nbit_ximage(struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
327 GLint x
, GLint y
, GLint width
, GLint height
)
329 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
330 XMesaImage
*img
= xrb
->ximage
;
333 /* TODO: optimize this */
335 for (j
= 0; j
< height
; j
++) {
336 for (i
= 0; i
< width
; i
++) {
337 XMesaPutPixel(img
, x
+i
, y
-j
, xmesa
->clearpixel
);
345 clear_buffers(struct gl_context
*ctx
, GLbitfield buffers
)
347 if (ctx
->DrawBuffer
->Name
== 0) {
348 /* this is a window system framebuffer */
349 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
[0];
350 XMesaBuffer b
= XMESA_BUFFER(ctx
->DrawBuffer
);
351 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
352 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
353 const GLint width
= ctx
->DrawBuffer
->_Xmax
- x
;
354 const GLint height
= ctx
->DrawBuffer
->_Ymax
- y
;
356 /* we can't handle color or index masking */
357 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
358 if (buffers
& BUFFER_BIT_FRONT_LEFT
) {
359 /* clear front color buffer */
360 struct gl_renderbuffer
*frontRb
361 = ctx
->DrawBuffer
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
;
362 if (b
->frontxrb
== xmesa_renderbuffer(frontRb
)) {
363 /* renderbuffer is not wrapped - great! */
364 b
->frontxrb
->clearFunc(ctx
, b
->frontxrb
, x
, y
, width
, height
);
365 buffers
&= ~BUFFER_BIT_FRONT_LEFT
;
368 /* we can't directly clear an alpha-wrapped color buffer */
371 if (buffers
& BUFFER_BIT_BACK_LEFT
) {
372 /* clear back color buffer */
373 struct gl_renderbuffer
*backRb
374 = ctx
->DrawBuffer
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
;
375 if (b
->backxrb
== xmesa_renderbuffer(backRb
)) {
376 /* renderbuffer is not wrapped - great! */
377 b
->backxrb
->clearFunc(ctx
, b
->backxrb
, x
, y
, width
, height
);
378 buffers
&= ~BUFFER_BIT_BACK_LEFT
;
384 _swrast_Clear(ctx
, buffers
);
388 /* XXX these functions haven't been tested in the Xserver environment */
392 * Check if we can do an optimized glDrawPixels into an 8R8G8B visual.
395 can_do_DrawPixels_8R8G8B(struct gl_context
*ctx
, GLenum format
, GLenum type
)
397 if (format
== GL_BGRA
&&
398 type
== GL_UNSIGNED_BYTE
&&
400 ctx
->DrawBuffer
->Name
== 0 &&
401 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
402 ctx
->Pixel
.ZoomY
== 1.0 &&
403 ctx
->_ImageTransferState
== 0 /* no color tables, scale/bias, etc */) {
404 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
406 if (swrast
->NewState
)
407 _swrast_validate_derived( ctx
);
409 if ((swrast
->_RasterMask
& ~CLIP_BIT
) == 0) /* no blend, z-test, etc */ {
410 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0];
412 struct xmesa_renderbuffer
*xrb
= xmesa_renderbuffer(rb
->Wrapped
);
414 xrb
->pixmap
&& /* drawing to pixmap or window */
415 _mesa_get_format_bits(xrb
->Base
.Format
, GL_ALPHA_BITS
) == 0) {
426 * This function implements glDrawPixels() with an XPutImage call when
427 * drawing to the front buffer (X Window drawable).
428 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
431 xmesa_DrawPixels_8R8G8B( struct gl_context
*ctx
,
432 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
433 GLenum format
, GLenum type
,
434 const struct gl_pixelstore_attrib
*unpack
,
435 const GLvoid
*pixels
)
437 if (can_do_DrawPixels_8R8G8B(ctx
, format
, type
)) {
438 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
439 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
445 if (swrast
->NewState
)
446 _swrast_validate_derived( ctx
);
448 if (unpack
->BufferObj
->Name
) {
449 /* unpack from PBO */
451 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
452 format
, type
, pixels
)) {
453 _mesa_error(ctx
, GL_INVALID_OPERATION
,
454 "glDrawPixels(invalid PBO access)");
457 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
458 GL_PIXEL_UNPACK_BUFFER_EXT
,
462 /* buffer is already mapped - that's an error */
463 _mesa_error(ctx
, GL_INVALID_OPERATION
,
464 "glDrawPixels(PBO is mapped)");
467 pixels
= ADD_POINTERS(buf
, pixels
);
470 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
471 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
472 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
473 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
474 const XMesaGC gc
= xmbuf
->cleargc
; /* effected by glColorMask */
475 struct xmesa_renderbuffer
*xrb
476 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]->Wrapped
);
477 const int srcX
= clippedUnpack
.SkipPixels
;
478 const int srcY
= clippedUnpack
.SkipRows
;
479 const int rowLength
= clippedUnpack
.RowLength
;
482 ASSERT(xmesa
->xm_visual
->dithered_pf
== PF_8R8G8B
);
483 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_8R8G8B
);
487 /* This is a little tricky since all coordinates up to now have
488 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
489 * so we have to carefully compute the Y coordinates/addresses here.
491 memset(&ximage
, 0, sizeof(XMesaImage
));
492 ximage
.width
= width
;
493 ximage
.height
= height
;
494 ximage
.format
= ZPixmap
;
495 ximage
.data
= (char *) pixels
496 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 4;
497 ximage
.byte_order
= LSBFirst
;
498 ximage
.bitmap_unit
= 32;
499 ximage
.bitmap_bit_order
= LSBFirst
;
500 ximage
.bitmap_pad
= 32;
502 ximage
.bits_per_pixel
= 32;
503 ximage
.bytes_per_line
= -rowLength
* 4; /* negative to flip image */
504 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
505 /* flip Y axis for dest position */
506 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
507 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
510 if (unpack
->BufferObj
->Name
) {
511 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
516 /* software fallback */
517 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
518 format
, type
, unpack
, pixels
);
525 * Check if we can do an optimized glDrawPixels into an 5R6G5B visual.
528 can_do_DrawPixels_5R6G5B(struct gl_context
*ctx
, GLenum format
, GLenum type
)
530 if (format
== GL_RGB
&&
531 type
== GL_UNSIGNED_SHORT_5_6_5
&&
532 !ctx
->Color
.DitherFlag
&& /* no dithering */
534 ctx
->DrawBuffer
->Name
== 0 &&
535 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
536 ctx
->Pixel
.ZoomY
== 1.0 &&
537 ctx
->_ImageTransferState
== 0 /* no color tables, scale/bias, etc */) {
538 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
540 if (swrast
->NewState
)
541 _swrast_validate_derived( ctx
);
543 if ((swrast
->_RasterMask
& ~CLIP_BIT
) == 0) /* no blend, z-test, etc */ {
544 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0];
546 struct xmesa_renderbuffer
*xrb
= xmesa_renderbuffer(rb
->Wrapped
);
548 xrb
->pixmap
&& /* drawing to pixmap or window */
549 _mesa_get_format_bits(xrb
->Base
.Format
, GL_ALPHA_BITS
) == 0) {
560 * This function implements glDrawPixels() with an XPutImage call when
561 * drawing to the front buffer (X Window drawable). The image format
562 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
563 * match the PF_5R6G5B pixel format.
566 xmesa_DrawPixels_5R6G5B( struct gl_context
*ctx
,
567 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
568 GLenum format
, GLenum type
,
569 const struct gl_pixelstore_attrib
*unpack
,
570 const GLvoid
*pixels
)
572 if (can_do_DrawPixels_5R6G5B(ctx
, format
, type
)) {
573 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
574 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
580 if (swrast
->NewState
)
581 _swrast_validate_derived( ctx
);
583 if (unpack
->BufferObj
->Name
) {
584 /* unpack from PBO */
586 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
587 format
, type
, pixels
)) {
588 _mesa_error(ctx
, GL_INVALID_OPERATION
,
589 "glDrawPixels(invalid PBO access)");
592 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
,
593 GL_PIXEL_UNPACK_BUFFER_EXT
,
597 /* buffer is already mapped - that's an error */
598 _mesa_error(ctx
, GL_INVALID_OPERATION
,
599 "glDrawPixels(PBO is mapped)");
602 pixels
= ADD_POINTERS(buf
, pixels
);
605 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
606 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
607 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
608 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
609 const XMesaGC gc
= xmbuf
->cleargc
; /* effected by glColorMask */
610 struct xmesa_renderbuffer
*xrb
611 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]->Wrapped
);
612 const int srcX
= clippedUnpack
.SkipPixels
;
613 const int srcY
= clippedUnpack
.SkipRows
;
614 const int rowLength
= clippedUnpack
.RowLength
;
617 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
);
621 /* This is a little tricky since all coordinates up to now have
622 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
623 * so we have to carefully compute the Y coordinates/addresses here.
625 memset(&ximage
, 0, sizeof(XMesaImage
));
626 ximage
.width
= width
;
627 ximage
.height
= height
;
628 ximage
.format
= ZPixmap
;
629 ximage
.data
= (char *) pixels
630 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 2;
631 ximage
.byte_order
= LSBFirst
;
632 ximage
.bitmap_unit
= 16;
633 ximage
.bitmap_bit_order
= LSBFirst
;
634 ximage
.bitmap_pad
= 16;
636 ximage
.bits_per_pixel
= 16;
637 ximage
.bytes_per_line
= -rowLength
* 2; /* negative to flip image */
638 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
639 /* flip Y axis for dest position */
640 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
641 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
644 if (unpack
->BufferObj
->Name
) {
645 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
650 /* software fallback */
651 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
652 format
, type
, unpack
, pixels
);
658 * Determine if we can do an optimized glCopyPixels.
661 can_do_CopyPixels(struct gl_context
*ctx
, GLenum type
)
663 if (type
== GL_COLOR
&&
664 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
665 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
666 ctx
->Pixel
.ZoomY
== 1.0 &&
667 ctx
->Color
.DrawBuffer
[0] == GL_FRONT
&& /* copy to front buf */
668 ctx
->Pixel
.ReadBuffer
== GL_FRONT
&& /* copy from front buf */
669 ctx
->ReadBuffer
->_ColorReadBuffer
&&
670 ctx
->DrawBuffer
->_ColorDrawBuffers
[0]) {
671 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
673 if (swrast
->NewState
)
674 _swrast_validate_derived( ctx
);
676 if ((swrast
->_RasterMask
& ~CLIP_BIT
) == 0x0 &&
678 ctx
->ReadBuffer
->_ColorReadBuffer
&&
680 ctx
->DrawBuffer
->_ColorDrawBuffers
[0]) {
681 struct xmesa_renderbuffer
*srcXrb
682 = xmesa_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
->Wrapped
);
683 struct xmesa_renderbuffer
*dstXrb
684 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]->Wrapped
);
685 if (srcXrb
->pixmap
&& dstXrb
->pixmap
) {
695 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
696 * for the color buffer. Don't support zooming, pixel transfer, etc.
697 * We do support copying from one window to another, ala glXMakeCurrentRead.
700 xmesa_CopyPixels( struct gl_context
*ctx
,
701 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
702 GLint destx
, GLint desty
, GLenum type
)
704 if (can_do_CopyPixels(ctx
, type
)) {
705 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
706 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
707 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
708 const XMesaGC gc
= xmbuf
->cleargc
; /* effected by glColorMask */
709 struct xmesa_renderbuffer
*srcXrb
710 = xmesa_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
->Wrapped
);
711 struct xmesa_renderbuffer
*dstXrb
712 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]->Wrapped
);
717 /* Note: we don't do any special clipping work here. We could,
718 * but X will do it for us.
720 srcy
= YFLIP(srcXrb
, srcy
) - height
+ 1;
721 desty
= YFLIP(dstXrb
, desty
) - height
+ 1;
722 XCopyArea(dpy
, srcXrb
->pixmap
, dstXrb
->pixmap
, gc
,
723 srcx
, srcy
, width
, height
, destx
, desty
);
726 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);
734 * Every driver should implement a GetString function in order to
735 * return a meaningful GL_RENDERER string.
737 static const GLubyte
*
738 get_string( struct gl_context
*ctx
, GLenum name
)
743 return (const GLubyte
*) "Mesa X11";
753 * We implement the glEnable function only because we care about
754 * dither enable/disable.
757 enable( struct gl_context
*ctx
, GLenum pname
, GLboolean state
)
759 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
764 xmesa
->pixelformat
= xmesa
->xm_visual
->dithered_pf
;
766 xmesa
->pixelformat
= xmesa
->xm_visual
->undithered_pf
;
769 ; /* silence compiler warning */
775 clear_color_HPCR_ximage( struct gl_context
*ctx
, const GLfloat color
[4] )
778 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
780 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
781 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
782 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
783 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
785 if (color
[0] == 0.0 && color
[1] == 0.0 && color
[2] == 0.0) {
787 memset( xmesa
->xm_visual
->hpcr_clear_ximage_pattern
, 0x0 ,
788 sizeof(xmesa
->xm_visual
->hpcr_clear_ximage_pattern
));
791 /* build clear pattern */
792 for (i
=0; i
<16; i
++) {
793 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[0][i
] =
795 xmesa
->clearcolor
[0],
796 xmesa
->clearcolor
[1],
797 xmesa
->clearcolor
[2]);
798 xmesa
->xm_visual
->hpcr_clear_ximage_pattern
[1][i
] =
800 xmesa
->clearcolor
[0],
801 xmesa
->clearcolor
[1],
802 xmesa
->clearcolor
[2]);
809 clear_color_HPCR_pixmap( struct gl_context
*ctx
, const GLfloat color
[4] )
812 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
814 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[0], color
[0]);
815 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[1], color
[1]);
816 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[2], color
[2]);
817 CLAMPED_FLOAT_TO_UBYTE(xmesa
->clearcolor
[3], color
[3]);
819 if (color
[0] == 0.0 && color
[1] == 0.0 && color
[2] == 0.0) {
821 for (i
=0; i
<16; i
++) {
822 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0, 0);
823 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1, 0);
827 for (i
=0; i
<16; i
++) {
828 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 0,
830 xmesa
->clearcolor
[0],
831 xmesa
->clearcolor
[1],
832 xmesa
->clearcolor
[2]));
833 XMesaPutPixel(xmesa
->xm_visual
->hpcr_clear_ximage
, i
, 1,
835 xmesa
->clearcolor
[0],
836 xmesa
->clearcolor
[1],
837 xmesa
->clearcolor
[2]));
840 /* change tile pixmap content */
841 XMesaPutImage(xmesa
->display
,
842 (XMesaDrawable
)xmesa
->xm_visual
->hpcr_clear_pixmap
,
843 XMESA_BUFFER(ctx
->DrawBuffer
)->cleargc
,
844 xmesa
->xm_visual
->hpcr_clear_ximage
, 0, 0, 0, 0, 16, 2);
849 * Called when the driver should update its state, based on the new_state
853 xmesa_update_state( struct gl_context
*ctx
, GLbitfield new_state
)
855 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
857 /* Propagate statechange information to swrast and swrast_setup
858 * modules. The X11 driver has no internal GL-dependent state.
860 _swrast_InvalidateState( ctx
, new_state
);
861 _tnl_InvalidateState( ctx
, new_state
);
862 _vbo_InvalidateState( ctx
, new_state
);
863 _swsetup_InvalidateState( ctx
, new_state
);
865 if (ctx
->DrawBuffer
->Name
!= 0)
869 * GL_DITHER, GL_READ/DRAW_BUFFER, buffer binding state, etc. effect
870 * renderbuffer span/clear funcs.
871 * Check _NEW_COLOR to detect dither enable/disable.
873 if (new_state
& (_NEW_COLOR
| _NEW_BUFFERS
)) {
874 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
875 struct xmesa_renderbuffer
*front_xrb
, *back_xrb
;
877 front_xrb
= xmbuf
->frontxrb
;
879 xmesa_set_renderbuffer_funcs(front_xrb
, xmesa
->pixelformat
,
880 xmesa
->xm_visual
->BitsPerPixel
);
881 front_xrb
->clearFunc
= clear_pixmap
;
884 back_xrb
= xmbuf
->backxrb
;
886 xmesa_set_renderbuffer_funcs(back_xrb
, xmesa
->pixelformat
,
887 xmesa
->xm_visual
->BitsPerPixel
);
888 if (xmbuf
->backxrb
->pixmap
) {
889 back_xrb
->clearFunc
= clear_pixmap
;
892 switch (xmesa
->xm_visual
->BitsPerPixel
) {
894 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
895 back_xrb
->clearFunc
= clear_HPCR_ximage
;
898 back_xrb
->clearFunc
= clear_8bit_ximage
;
902 back_xrb
->clearFunc
= clear_16bit_ximage
;
905 back_xrb
->clearFunc
= clear_24bit_ximage
;
908 back_xrb
->clearFunc
= clear_32bit_ximage
;
911 back_xrb
->clearFunc
= clear_nbit_ximage
;
918 if (xmesa
->xm_visual
->hpcr_clear_flag
) {
919 /* this depends on whether we're drawing to the front or back buffer */
923 ctx
->Driver
.ClearColor
= clear_color_HPCR_pixmap
;
926 ctx
->Driver
.ClearColor
= clear_color_HPCR_ximage
;
929 (void) clear_color_HPCR_pixmap
;
930 (void) clear_color_HPCR_ximage
;
938 * In SW, we don't really compress GL_COMPRESSED_RGB[A] textures!
941 choose_tex_format( struct gl_context
*ctx
, GLint internalFormat
,
942 GLenum format
, GLenum type
)
944 switch (internalFormat
) {
945 case GL_COMPRESSED_RGB_ARB
:
946 return MESA_FORMAT_RGB888
;
947 case GL_COMPRESSED_RGBA_ARB
:
948 return MESA_FORMAT_RGBA8888
;
950 return _mesa_choose_tex_format(ctx
, internalFormat
, format
, type
);
956 * Called by glViewport.
957 * This is a good time for us to poll the current X window size and adjust
958 * our renderbuffers to match the current window size.
959 * Remember, we have no opportunity to respond to conventional
960 * X Resize/StructureNotify events since the X driver has no event loop.
962 * Note that this trick isn't fool-proof. If the application never calls
963 * glViewport, our notion of the current window size may be incorrect.
964 * That problem led to the GLX_MESA_resize_buffers extension.
967 xmesa_viewport(struct gl_context
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
969 XMesaContext xmctx
= XMESA_CONTEXT(ctx
);
970 XMesaBuffer xmdrawbuf
= XMESA_BUFFER(ctx
->WinSysDrawBuffer
);
971 XMesaBuffer xmreadbuf
= XMESA_BUFFER(ctx
->WinSysReadBuffer
);
972 xmesa_check_and_update_buffer_size(xmctx
, xmdrawbuf
);
973 xmesa_check_and_update_buffer_size(xmctx
, xmreadbuf
);
981 #if ENABLE_EXT_timer_query
984 * The GL_EXT_timer_query extension is not enabled for the XServer
985 * indirect renderer. Not sure about how/if wrapping of gettimeofday()
989 struct xmesa_query_object
991 struct gl_query_object Base
;
992 struct timeval StartTime
;
996 static struct gl_query_object
*
997 xmesa_new_query_object(struct gl_context
*ctx
, GLuint id
)
999 struct xmesa_query_object
*q
= CALLOC_STRUCT(xmesa_query_object
);
1002 q
->Base
.Ready
= GL_TRUE
;
1009 xmesa_begin_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
1011 if (q
->Target
== GL_TIME_ELAPSED_EXT
) {
1012 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1013 (void) gettimeofday(&xq
->StartTime
, NULL
);
1019 * Return the difference between the two given times in microseconds.
1022 #define suseconds_t unsigned int
1025 time_diff(const struct timeval
*t0
, const struct timeval
*t1
)
1027 GLuint64EXT seconds0
= t0
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1028 GLuint64EXT seconds1
= t1
->tv_sec
& 0xff; /* 0 .. 255 seconds */
1029 GLuint64EXT nanosec0
= (seconds0
* 1000000 + t0
->tv_usec
) * 1000;
1030 GLuint64EXT nanosec1
= (seconds1
* 1000000 + t1
->tv_usec
) * 1000;
1031 return nanosec1
- nanosec0
;
1036 xmesa_end_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
1038 if (q
->Target
== GL_TIME_ELAPSED_EXT
) {
1039 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
1040 struct timeval endTime
;
1041 (void) gettimeofday(&endTime
, NULL
);
1042 /* result is in nanoseconds! */
1043 q
->Result
= time_diff(&xq
->StartTime
, &endTime
);
1048 #endif /* ENABLE_timer_query */
1052 * Initialize the device driver function table with the functions
1053 * we implement in this driver.
1056 xmesa_init_driver_functions( XMesaVisual xmvisual
,
1057 struct dd_function_table
*driver
)
1059 driver
->GetString
= get_string
;
1060 driver
->UpdateState
= xmesa_update_state
;
1061 driver
->GetBufferSize
= NULL
; /* OBSOLETE */
1062 driver
->Flush
= finish_or_flush
;
1063 driver
->Finish
= finish_or_flush
;
1064 driver
->ClearColor
= clear_color
;
1065 driver
->ColorMask
= color_mask
;
1066 driver
->Enable
= enable
;
1067 driver
->Viewport
= xmesa_viewport
;
1068 if (TEST_META_FUNCS
) {
1069 driver
->Clear
= _mesa_meta_Clear
;
1070 driver
->CopyPixels
= _mesa_meta_CopyPixels
;
1071 driver
->BlitFramebuffer
= _mesa_meta_BlitFramebuffer
;
1072 driver
->DrawPixels
= _mesa_meta_DrawPixels
;
1073 driver
->Bitmap
= _mesa_meta_Bitmap
;
1076 driver
->Clear
= clear_buffers
;
1077 driver
->CopyPixels
= xmesa_CopyPixels
;
1078 if (xmvisual
->undithered_pf
== PF_8R8G8B
&&
1079 xmvisual
->dithered_pf
== PF_8R8G8B
&&
1080 xmvisual
->BitsPerPixel
== 32) {
1081 driver
->DrawPixels
= xmesa_DrawPixels_8R8G8B
;
1083 else if (xmvisual
->undithered_pf
== PF_5R6G5B
) {
1084 driver
->DrawPixels
= xmesa_DrawPixels_5R6G5B
;
1088 #if ENABLE_EXT_texure_compression_s3tc
1089 driver
->ChooseTextureFormat
= choose_tex_format
;
1091 (void) choose_tex_format
;
1094 #if ENABLE_EXT_timer_query
1095 driver
->NewQueryObject
= xmesa_new_query_object
;
1096 driver
->BeginQuery
= xmesa_begin_query
;
1097 driver
->EndQuery
= xmesa_end_query
;
1102 #define XMESA_NEW_POINT (_NEW_POINT | \
1104 _SWRAST_NEW_RASTERMASK)
1106 #define XMESA_NEW_LINE (_NEW_LINE | \
1111 _SWRAST_NEW_RASTERMASK)
1113 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1118 _SWRAST_NEW_RASTERMASK)
1122 * Extend the software rasterizer with our line/point/triangle
1124 * Called during context creation only.
1126 void xmesa_register_swrast_functions( struct gl_context
*ctx
)
1128 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
1130 swrast
->choose_point
= xmesa_choose_point
;
1131 swrast
->choose_line
= xmesa_choose_line
;
1132 swrast
->choose_triangle
= xmesa_choose_triangle
;
1134 /* XXX these lines have no net effect. Remove??? */
1135 swrast
->InvalidatePointMask
|= XMESA_NEW_POINT
;
1136 swrast
->InvalidateLineMask
|= XMESA_NEW_LINE
;
1137 swrast
->InvalidateTriangleMask
|= XMESA_NEW_TRIANGLE
;