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/context.h"
34 #include "main/colormac.h"
35 #include "main/fbobject.h"
36 #include "main/macros.h"
37 #include "main/image.h"
38 #include "main/imports.h"
39 #include "main/mtypes.h"
41 #include "main/texformat.h"
42 #include "swrast/swrast.h"
43 #include "swrast/s_context.h"
44 #include "swrast_setup/swrast_setup.h"
46 #include "tnl/t_context.h"
47 #include "drivers/common/meta.h"
52 finish_or_flush( struct gl_context
*ctx
)
54 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
56 _glthread_LOCK_MUTEX(_xmesa_lock
);
57 XSync( xmesa
->display
, False
);
58 _glthread_UNLOCK_MUTEX(_xmesa_lock
);
63 /* Implements glColorMask() */
65 color_mask(struct gl_context
*ctx
,
66 GLboolean rmask
, GLboolean gmask
, GLboolean bmask
, GLboolean amask
)
68 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
70 const int xclass
= xmesa
->xm_visual
->visualType
;
73 if (_mesa_is_user_fbo(ctx
->DrawBuffer
))
76 xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
78 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
80 if (rmask
&& gmask
&& bmask
) {
81 m
= ((unsigned long)~0L);
85 if (rmask
) m
|= GET_REDMASK(xmesa
->xm_visual
);
86 if (gmask
) m
|= GET_GREENMASK(xmesa
->xm_visual
);
87 if (bmask
) m
|= GET_BLUEMASK(xmesa
->xm_visual
);
89 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->cleargc
, m
);
95 /**********************************************************************/
96 /*** glClear implementations ***/
97 /**********************************************************************/
101 * Clear the front or back color buffer, if it's implemented with a pixmap.
104 clear_pixmap(struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
105 GLint x
, GLint y
, GLint width
, GLint height
)
107 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
108 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
113 assert(xmesa
->display
);
115 assert(xmbuf
->cleargc
);
117 XMesaFillRectangle( xmesa
->display
, xrb
->pixmap
, xmbuf
->cleargc
,
118 x
, xrb
->Base
.Base
.Height
- y
- height
,
124 clear_16bit_ximage( struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
125 GLint x
, GLint y
, GLint width
, GLint height
)
127 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
128 GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
131 if (xmesa
->swapbytes
) {
132 pixel
= ((pixel
>> 8) & 0x00ff) | ((pixel
<< 8) & 0xff00);
135 for (j
= 0; j
< height
; j
++) {
136 GLushort
*ptr2
= PIXEL_ADDR2(xrb
, x
, y
+ j
);
137 for (i
= 0; i
< width
; i
++) {
144 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
146 clear_24bit_ximage(struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
147 GLint x
, GLint y
, GLint width
, GLint height
)
149 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
150 const GLubyte r
= xmesa
->clearcolor
[0];
151 const GLubyte g
= xmesa
->clearcolor
[1];
152 const GLubyte b
= xmesa
->clearcolor
[2];
154 if (r
== g
&& g
== b
) {
155 /* same value for all three components (gray) */
157 for (j
= 0; j
< height
; j
++) {
158 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
159 memset(ptr3
, r
, 3 * width
);
163 /* non-gray clear color */
165 for (j
= 0; j
< height
; j
++) {
166 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
167 for (i
= 0; i
< width
; i
++) {
179 clear_32bit_ximage(struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
180 GLint x
, GLint y
, GLint width
, GLint height
)
182 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
183 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
188 if (xmesa
->swapbytes
) {
189 pixel
= ((pixel
>> 24) & 0x000000ff)
190 | ((pixel
>> 8) & 0x0000ff00)
191 | ((pixel
<< 8) & 0x00ff0000)
192 | ((pixel
<< 24) & 0xff000000);
195 if (width
== xrb
->Base
.Base
.Width
&& height
== xrb
->Base
.Base
.Height
) {
196 /* clearing whole buffer */
197 const GLuint n
= xrb
->Base
.Base
.Width
* xrb
->Base
.Base
.Height
;
198 GLuint
*ptr4
= (GLuint
*) xrb
->ximage
->data
;
201 memset(ptr4
, pixel
, 4 * n
);
205 for (i
= 0; i
< n
; i
++)
210 /* clearing scissored region */
212 for (j
= 0; j
< height
; j
++) {
213 GLuint
*ptr4
= PIXEL_ADDR4(xrb
, x
, y
+ j
);
214 for (i
= 0; i
< width
; i
++) {
223 clear_nbit_ximage(struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
224 GLint x
, GLint y
, GLint width
, GLint height
)
226 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
227 XMesaImage
*img
= xrb
->ximage
;
230 /* TODO: optimize this */
232 for (j
= 0; j
< height
; j
++) {
233 for (i
= 0; i
< width
; i
++) {
234 XMesaPutPixel(img
, x
+i
, y
-j
, xmesa
->clearpixel
);
242 clear_buffers(struct gl_context
*ctx
, GLbitfield buffers
)
244 if (_mesa_is_winsys_fbo(ctx
->DrawBuffer
)) {
245 /* this is a window system framebuffer */
246 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
[0];
247 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
248 XMesaBuffer b
= XMESA_BUFFER(ctx
->DrawBuffer
);
249 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
250 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
251 const GLint width
= ctx
->DrawBuffer
->_Xmax
- x
;
252 const GLint height
= ctx
->DrawBuffer
->_Ymax
- y
;
254 _mesa_unclamped_float_rgba_to_ubyte(xmesa
->clearcolor
,
255 ctx
->Color
.ClearColor
.f
);
256 xmesa
->clearpixel
= xmesa_color_to_pixel(ctx
,
257 xmesa
->clearcolor
[0],
258 xmesa
->clearcolor
[1],
259 xmesa
->clearcolor
[2],
260 xmesa
->clearcolor
[3],
261 xmesa
->xm_visual
->undithered_pf
);
262 XMesaSetForeground(xmesa
->display
, b
->cleargc
, xmesa
->clearpixel
);
264 /* we can't handle color or index masking */
265 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
266 if (buffers
& BUFFER_BIT_FRONT_LEFT
) {
267 /* clear front color buffer */
268 struct gl_renderbuffer
*frontRb
269 = ctx
->DrawBuffer
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
;
270 if (b
->frontxrb
== xmesa_renderbuffer(frontRb
)) {
271 /* renderbuffer is not wrapped - great! */
272 b
->frontxrb
->clearFunc(ctx
, b
->frontxrb
, x
, y
, width
, height
);
273 buffers
&= ~BUFFER_BIT_FRONT_LEFT
;
276 /* we can't directly clear an alpha-wrapped color buffer */
279 if (buffers
& BUFFER_BIT_BACK_LEFT
) {
280 /* clear back color buffer */
281 struct gl_renderbuffer
*backRb
282 = ctx
->DrawBuffer
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
;
283 if (b
->backxrb
== xmesa_renderbuffer(backRb
)) {
284 /* renderbuffer is not wrapped - great! */
285 b
->backxrb
->clearFunc(ctx
, b
->backxrb
, x
, y
, width
, height
);
286 buffers
&= ~BUFFER_BIT_BACK_LEFT
;
292 _swrast_Clear(ctx
, buffers
);
296 /* XXX these functions haven't been tested in the Xserver environment */
300 * Check if we can do an optimized glDrawPixels into an 8R8G8B visual.
303 can_do_DrawPixels_8R8G8B(struct gl_context
*ctx
, GLenum format
, GLenum type
)
305 if (format
== GL_BGRA
&&
306 type
== GL_UNSIGNED_BYTE
&&
308 _mesa_is_winsys_fbo(ctx
->DrawBuffer
) &&
309 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
310 ctx
->Pixel
.ZoomY
== 1.0 &&
311 ctx
->_ImageTransferState
== 0 /* no color tables, scale/bias, etc */) {
312 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
314 if (swrast
->NewState
)
315 _swrast_validate_derived( ctx
);
317 if ((swrast
->_RasterMask
& ~CLIP_BIT
) == 0) /* no blend, z-test, etc */ {
318 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0];
320 struct xmesa_renderbuffer
*xrb
= xmesa_renderbuffer(rb
);
322 xrb
->pixmap
&& /* drawing to pixmap or window */
323 _mesa_get_format_bits(xrb
->Base
.Base
.Format
, GL_ALPHA_BITS
) == 0) {
334 * This function implements glDrawPixels() with an XPutImage call when
335 * drawing to the front buffer (X Window drawable).
336 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
339 xmesa_DrawPixels_8R8G8B( struct gl_context
*ctx
,
340 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
341 GLenum format
, GLenum type
,
342 const struct gl_pixelstore_attrib
*unpack
,
343 const GLvoid
*pixels
)
345 if (can_do_DrawPixels_8R8G8B(ctx
, format
, type
)) {
346 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
347 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
353 if (swrast
->NewState
)
354 _swrast_validate_derived( ctx
);
356 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
357 /* unpack from PBO */
359 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
360 format
, type
, INT_MAX
, pixels
)) {
361 _mesa_error(ctx
, GL_INVALID_OPERATION
,
362 "glDrawPixels(invalid PBO access)");
365 buf
= (GLubyte
*) ctx
->Driver
.MapBufferRange(ctx
, 0,
366 unpack
->BufferObj
->Size
,
370 /* buffer is already mapped - that's an error */
371 _mesa_error(ctx
, GL_INVALID_OPERATION
,
372 "glDrawPixels(PBO is mapped)");
375 pixels
= ADD_POINTERS(buf
, pixels
);
378 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
379 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
380 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
381 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
382 const XMesaGC gc
= xmbuf
->cleargc
; /* effected by glColorMask */
383 struct xmesa_renderbuffer
*xrb
384 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
385 const int srcX
= clippedUnpack
.SkipPixels
;
386 const int srcY
= clippedUnpack
.SkipRows
;
387 const int rowLength
= clippedUnpack
.RowLength
;
390 ASSERT(xmesa
->xm_visual
->dithered_pf
== PF_8R8G8B
);
391 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_8R8G8B
);
395 /* This is a little tricky since all coordinates up to now have
396 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
397 * so we have to carefully compute the Y coordinates/addresses here.
399 memset(&ximage
, 0, sizeof(XMesaImage
));
400 ximage
.width
= width
;
401 ximage
.height
= height
;
402 ximage
.format
= ZPixmap
;
403 ximage
.data
= (char *) pixels
404 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 4;
405 ximage
.byte_order
= LSBFirst
;
406 ximage
.bitmap_unit
= 32;
407 ximage
.bitmap_bit_order
= LSBFirst
;
408 ximage
.bitmap_pad
= 32;
410 ximage
.bits_per_pixel
= 32;
411 ximage
.bytes_per_line
= -rowLength
* 4; /* negative to flip image */
412 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
413 /* flip Y axis for dest position */
414 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
415 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
418 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
419 ctx
->Driver
.UnmapBuffer(ctx
, unpack
->BufferObj
);
423 /* software fallback */
424 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
425 format
, type
, unpack
, pixels
);
432 * Check if we can do an optimized glDrawPixels into an 5R6G5B visual.
435 can_do_DrawPixels_5R6G5B(struct gl_context
*ctx
, GLenum format
, GLenum type
)
437 if (format
== GL_RGB
&&
438 type
== GL_UNSIGNED_SHORT_5_6_5
&&
439 !ctx
->Color
.DitherFlag
&& /* no dithering */
441 _mesa_is_winsys_fbo(ctx
->DrawBuffer
) &&
442 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
443 ctx
->Pixel
.ZoomY
== 1.0 &&
444 ctx
->_ImageTransferState
== 0 /* no color tables, scale/bias, etc */) {
445 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
447 if (swrast
->NewState
)
448 _swrast_validate_derived( ctx
);
450 if ((swrast
->_RasterMask
& ~CLIP_BIT
) == 0) /* no blend, z-test, etc */ {
451 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0];
453 struct xmesa_renderbuffer
*xrb
= xmesa_renderbuffer(rb
);
455 xrb
->pixmap
&& /* drawing to pixmap or window */
456 _mesa_get_format_bits(xrb
->Base
.Base
.Format
, GL_ALPHA_BITS
) == 0) {
467 * This function implements glDrawPixels() with an XPutImage call when
468 * drawing to the front buffer (X Window drawable). The image format
469 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
470 * match the PF_5R6G5B pixel format.
473 xmesa_DrawPixels_5R6G5B( struct gl_context
*ctx
,
474 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
475 GLenum format
, GLenum type
,
476 const struct gl_pixelstore_attrib
*unpack
,
477 const GLvoid
*pixels
)
479 if (can_do_DrawPixels_5R6G5B(ctx
, format
, type
)) {
480 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
481 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
487 if (swrast
->NewState
)
488 _swrast_validate_derived( ctx
);
490 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
491 /* unpack from PBO */
493 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
494 format
, type
, INT_MAX
, pixels
)) {
495 _mesa_error(ctx
, GL_INVALID_OPERATION
,
496 "glDrawPixels(invalid PBO access)");
499 buf
= (GLubyte
*) ctx
->Driver
.MapBufferRange(ctx
, 0,
500 unpack
->BufferObj
->Size
,
504 /* buffer is already mapped - that's an error */
505 _mesa_error(ctx
, GL_INVALID_OPERATION
,
506 "glDrawPixels(PBO is mapped)");
509 pixels
= ADD_POINTERS(buf
, pixels
);
512 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
513 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
514 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
515 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
516 const XMesaGC gc
= xmbuf
->cleargc
; /* effected by glColorMask */
517 struct xmesa_renderbuffer
*xrb
518 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
519 const int srcX
= clippedUnpack
.SkipPixels
;
520 const int srcY
= clippedUnpack
.SkipRows
;
521 const int rowLength
= clippedUnpack
.RowLength
;
524 ASSERT(xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
);
528 /* This is a little tricky since all coordinates up to now have
529 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
530 * so we have to carefully compute the Y coordinates/addresses here.
532 memset(&ximage
, 0, sizeof(XMesaImage
));
533 ximage
.width
= width
;
534 ximage
.height
= height
;
535 ximage
.format
= ZPixmap
;
536 ximage
.data
= (char *) pixels
537 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 2;
538 ximage
.byte_order
= LSBFirst
;
539 ximage
.bitmap_unit
= 16;
540 ximage
.bitmap_bit_order
= LSBFirst
;
541 ximage
.bitmap_pad
= 16;
543 ximage
.bits_per_pixel
= 16;
544 ximage
.bytes_per_line
= -rowLength
* 2; /* negative to flip image */
545 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
546 /* flip Y axis for dest position */
547 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
548 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
551 if (unpack
->BufferObj
->Name
) {
552 ctx
->Driver
.UnmapBuffer(ctx
, unpack
->BufferObj
);
556 /* software fallback */
557 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
558 format
, type
, unpack
, pixels
);
564 * Determine if we can do an optimized glCopyPixels.
567 can_do_CopyPixels(struct gl_context
*ctx
, GLenum type
)
569 if (type
== GL_COLOR
&&
570 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
571 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
572 ctx
->Pixel
.ZoomY
== 1.0 &&
573 ctx
->Color
.DrawBuffer
[0] == GL_FRONT
&& /* copy to front buf */
574 ctx
->Pixel
.ReadBuffer
== GL_FRONT
&& /* copy from front buf */
575 ctx
->ReadBuffer
->_ColorReadBuffer
&&
576 ctx
->DrawBuffer
->_ColorDrawBuffers
[0]) {
577 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
579 if (swrast
->NewState
)
580 _swrast_validate_derived( ctx
);
582 if ((swrast
->_RasterMask
& ~CLIP_BIT
) == 0x0 &&
584 ctx
->ReadBuffer
->_ColorReadBuffer
&&
586 ctx
->DrawBuffer
->_ColorDrawBuffers
[0]) {
587 struct xmesa_renderbuffer
*srcXrb
588 = xmesa_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
);
589 struct xmesa_renderbuffer
*dstXrb
590 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
591 if (srcXrb
->pixmap
&& dstXrb
->pixmap
) {
601 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
602 * for the color buffer. Don't support zooming, pixel transfer, etc.
603 * We do support copying from one window to another, ala glXMakeCurrentRead.
606 xmesa_CopyPixels( struct gl_context
*ctx
,
607 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
608 GLint destx
, GLint desty
, GLenum type
)
610 if (can_do_CopyPixels(ctx
, type
)) {
611 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
612 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
613 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
614 const XMesaGC gc
= xmbuf
->cleargc
; /* effected by glColorMask */
615 struct xmesa_renderbuffer
*srcXrb
616 = xmesa_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
);
617 struct xmesa_renderbuffer
*dstXrb
618 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
623 /* Note: we don't do any special clipping work here. We could,
624 * but X will do it for us.
626 srcy
= YFLIP(srcXrb
, srcy
) - height
+ 1;
627 desty
= YFLIP(dstXrb
, desty
) - height
+ 1;
628 XCopyArea(dpy
, srcXrb
->pixmap
, dstXrb
->pixmap
, gc
,
629 srcx
, srcy
, width
, height
, destx
, desty
);
632 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);
640 * Every driver should implement a GetString function in order to
641 * return a meaningful GL_RENDERER string.
643 static const GLubyte
*
644 get_string( struct gl_context
*ctx
, GLenum name
)
649 return (const GLubyte
*) "Mesa X11";
659 * We implement the glEnable function only because we care about
660 * dither enable/disable.
663 enable( struct gl_context
*ctx
, GLenum pname
, GLboolean state
)
665 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
670 xmesa
->pixelformat
= xmesa
->xm_visual
->dithered_pf
;
672 xmesa
->pixelformat
= xmesa
->xm_visual
->undithered_pf
;
675 ; /* silence compiler warning */
681 * Called when the driver should update its state, based on the new_state
685 xmesa_update_state( struct gl_context
*ctx
, GLbitfield new_state
)
687 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
689 /* Propagate statechange information to swrast and swrast_setup
690 * modules. The X11 driver has no internal GL-dependent state.
692 _swrast_InvalidateState( ctx
, new_state
);
693 _tnl_InvalidateState( ctx
, new_state
);
694 _vbo_InvalidateState( ctx
, new_state
);
695 _swsetup_InvalidateState( ctx
, new_state
);
697 if (_mesa_is_user_fbo(ctx
->DrawBuffer
))
701 * GL_DITHER, GL_READ/DRAW_BUFFER, buffer binding state, etc. effect
702 * renderbuffer span/clear funcs.
703 * Check _NEW_COLOR to detect dither enable/disable.
705 if (new_state
& (_NEW_COLOR
| _NEW_BUFFERS
)) {
706 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
707 struct xmesa_renderbuffer
*front_xrb
, *back_xrb
;
709 front_xrb
= xmbuf
->frontxrb
;
711 front_xrb
->clearFunc
= clear_pixmap
;
714 back_xrb
= xmbuf
->backxrb
;
716 if (xmbuf
->backxrb
->pixmap
) {
717 back_xrb
->clearFunc
= clear_pixmap
;
720 switch (xmesa
->xm_visual
->BitsPerPixel
) {
722 back_xrb
->clearFunc
= clear_16bit_ximage
;
725 back_xrb
->clearFunc
= clear_24bit_ximage
;
728 back_xrb
->clearFunc
= clear_32bit_ximage
;
731 back_xrb
->clearFunc
= clear_nbit_ximage
;
742 * In SW, we don't really compress GL_COMPRESSED_RGB[A] textures!
745 choose_tex_format( struct gl_context
*ctx
, GLint internalFormat
,
746 GLenum format
, GLenum type
)
748 switch (internalFormat
) {
749 case GL_COMPRESSED_RGB_ARB
:
750 return MESA_FORMAT_RGB888
;
751 case GL_COMPRESSED_RGBA_ARB
:
752 return MESA_FORMAT_RGBA8888
;
754 return _mesa_choose_tex_format(ctx
, internalFormat
, format
, type
);
760 * Called by glViewport.
761 * This is a good time for us to poll the current X window size and adjust
762 * our renderbuffers to match the current window size.
763 * Remember, we have no opportunity to respond to conventional
764 * X Resize/StructureNotify events since the X driver has no event loop.
766 * Note that this trick isn't fool-proof. If the application never calls
767 * glViewport, our notion of the current window size may be incorrect.
768 * That problem led to the GLX_MESA_resize_buffers extension.
771 xmesa_viewport(struct gl_context
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
773 XMesaContext xmctx
= XMESA_CONTEXT(ctx
);
774 XMesaBuffer xmdrawbuf
= XMESA_BUFFER(ctx
->WinSysDrawBuffer
);
775 XMesaBuffer xmreadbuf
= XMESA_BUFFER(ctx
->WinSysReadBuffer
);
776 xmesa_check_and_update_buffer_size(xmctx
, xmdrawbuf
);
777 xmesa_check_and_update_buffer_size(xmctx
, xmreadbuf
);
785 #if ENABLE_EXT_timer_query
788 * The GL_EXT_timer_query extension is not enabled for the XServer
789 * indirect renderer. Not sure about how/if wrapping of gettimeofday()
793 struct xmesa_query_object
795 struct gl_query_object Base
;
796 struct timeval StartTime
;
800 static struct gl_query_object
*
801 xmesa_new_query_object(struct gl_context
*ctx
, GLuint id
)
803 struct xmesa_query_object
*q
= CALLOC_STRUCT(xmesa_query_object
);
806 q
->Base
.Ready
= GL_TRUE
;
813 xmesa_begin_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
815 if (q
->Target
== GL_TIME_ELAPSED_EXT
) {
816 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
817 (void) gettimeofday(&xq
->StartTime
, NULL
);
823 * Return the difference between the two given times in microseconds.
826 #define suseconds_t unsigned int
829 time_diff(const struct timeval
*t0
, const struct timeval
*t1
)
831 GLuint64EXT seconds0
= t0
->tv_sec
& 0xff; /* 0 .. 255 seconds */
832 GLuint64EXT seconds1
= t1
->tv_sec
& 0xff; /* 0 .. 255 seconds */
833 GLuint64EXT nanosec0
= (seconds0
* 1000000 + t0
->tv_usec
) * 1000;
834 GLuint64EXT nanosec1
= (seconds1
* 1000000 + t1
->tv_usec
) * 1000;
835 return nanosec1
- nanosec0
;
840 xmesa_end_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
842 if (q
->Target
== GL_TIME_ELAPSED_EXT
) {
843 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
844 struct timeval endTime
;
845 (void) gettimeofday(&endTime
, NULL
);
846 /* result is in nanoseconds! */
847 q
->Result
= time_diff(&xq
->StartTime
, &endTime
);
852 #endif /* ENABLE_timer_query */
856 * Initialize the device driver function table with the functions
857 * we implement in this driver.
860 xmesa_init_driver_functions( XMesaVisual xmvisual
,
861 struct dd_function_table
*driver
)
863 driver
->GetString
= get_string
;
864 driver
->UpdateState
= xmesa_update_state
;
865 driver
->GetBufferSize
= NULL
; /* OBSOLETE */
866 driver
->Flush
= finish_or_flush
;
867 driver
->Finish
= finish_or_flush
;
868 driver
->ColorMask
= color_mask
;
869 driver
->Enable
= enable
;
870 driver
->Viewport
= xmesa_viewport
;
871 if (TEST_META_FUNCS
) {
872 driver
->Clear
= _mesa_meta_Clear
;
873 driver
->CopyPixels
= _mesa_meta_CopyPixels
;
874 driver
->BlitFramebuffer
= _mesa_meta_BlitFramebuffer
;
875 driver
->DrawPixels
= _mesa_meta_DrawPixels
;
876 driver
->Bitmap
= _mesa_meta_Bitmap
;
879 driver
->Clear
= clear_buffers
;
880 driver
->CopyPixels
= xmesa_CopyPixels
;
881 if (xmvisual
->undithered_pf
== PF_8R8G8B
&&
882 xmvisual
->dithered_pf
== PF_8R8G8B
&&
883 xmvisual
->BitsPerPixel
== 32) {
884 driver
->DrawPixels
= xmesa_DrawPixels_8R8G8B
;
886 else if (xmvisual
->undithered_pf
== PF_5R6G5B
) {
887 driver
->DrawPixels
= xmesa_DrawPixels_5R6G5B
;
891 driver
->MapRenderbuffer
= xmesa_MapRenderbuffer
;
892 driver
->UnmapRenderbuffer
= xmesa_UnmapRenderbuffer
;
894 #if ENABLE_EXT_texure_compression_s3tc
895 driver
->ChooseTextureFormat
= choose_tex_format
;
897 (void) choose_tex_format
;
900 #if ENABLE_EXT_timer_query
901 driver
->NewQueryObject
= xmesa_new_query_object
;
902 driver
->BeginQuery
= xmesa_begin_query
;
903 driver
->EndQuery
= xmesa_end_query
;
908 #define XMESA_NEW_POINT (_NEW_POINT | \
910 _SWRAST_NEW_RASTERMASK)
912 #define XMESA_NEW_LINE (_NEW_LINE | \
917 _SWRAST_NEW_RASTERMASK)
919 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
924 _SWRAST_NEW_RASTERMASK)
928 * Extend the software rasterizer with our line/point/triangle
930 * Called during context creation only.
932 void xmesa_register_swrast_functions( struct gl_context
*ctx
)
934 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
936 swrast
->choose_point
= xmesa_choose_point
;
937 swrast
->choose_line
= xmesa_choose_line
;
938 swrast
->choose_triangle
= xmesa_choose_triangle
;
940 /* XXX these lines have no net effect. Remove??? */
941 swrast
->InvalidatePointMask
|= XMESA_NEW_POINT
;
942 swrast
->InvalidateLineMask
|= XMESA_NEW_LINE
;
943 swrast
->InvalidateTriangleMask
|= XMESA_NEW_TRIANGLE
;