2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * 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/framebuffer.h"
37 #include "main/macros.h"
38 #include "main/mipmap.h"
39 #include "main/image.h"
40 #include "main/imports.h"
41 #include "main/mtypes.h"
43 #include "main/texformat.h"
44 #include "swrast/swrast.h"
45 #include "swrast/s_context.h"
46 #include "swrast_setup/swrast_setup.h"
48 #include "tnl/t_context.h"
49 #include "drivers/common/meta.h"
54 finish_or_flush( struct gl_context
*ctx
)
56 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
58 mtx_lock(&_xmesa_lock
);
59 XSync( xmesa
->display
, False
);
60 mtx_unlock(&_xmesa_lock
);
65 /* Implements glColorMask() */
67 color_mask(struct gl_context
*ctx
,
68 GLboolean rmask
, GLboolean gmask
, GLboolean bmask
, GLboolean amask
)
70 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
72 const int xclass
= xmesa
->xm_visual
->visualType
;
75 if (_mesa_is_user_fbo(ctx
->DrawBuffer
))
78 xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
80 if (xclass
== GLX_TRUE_COLOR
|| xclass
== GLX_DIRECT_COLOR
) {
82 if (rmask
&& gmask
&& bmask
) {
83 m
= ((unsigned long)~0L);
87 if (rmask
) m
|= GET_REDMASK(xmesa
->xm_visual
);
88 if (gmask
) m
|= GET_GREENMASK(xmesa
->xm_visual
);
89 if (bmask
) m
|= GET_BLUEMASK(xmesa
->xm_visual
);
91 XMesaSetPlaneMask( xmesa
->display
, xmbuf
->cleargc
, m
);
97 /**********************************************************************/
98 /*** glClear implementations ***/
99 /**********************************************************************/
103 * Clear the front or back color buffer, if it's implemented with a pixmap.
106 clear_pixmap(struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
107 GLint x
, GLint y
, GLint width
, GLint height
)
109 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
110 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
115 assert(xmesa
->display
);
117 assert(xmbuf
->cleargc
);
119 XMesaFillRectangle( xmesa
->display
, xrb
->pixmap
, xmbuf
->cleargc
,
120 x
, xrb
->Base
.Base
.Height
- y
- height
,
126 clear_16bit_ximage( struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
127 GLint x
, GLint y
, GLint width
, GLint height
)
129 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
130 GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
133 if (xmesa
->swapbytes
) {
134 pixel
= ((pixel
>> 8) & 0x00ff) | ((pixel
<< 8) & 0xff00);
137 for (j
= 0; j
< height
; j
++) {
138 GLushort
*ptr2
= PIXEL_ADDR2(xrb
, x
, y
+ j
);
139 for (i
= 0; i
< width
; i
++) {
146 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
148 clear_24bit_ximage(struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
149 GLint x
, GLint y
, GLint width
, GLint height
)
151 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
152 const GLubyte r
= xmesa
->clearcolor
[0];
153 const GLubyte g
= xmesa
->clearcolor
[1];
154 const GLubyte b
= xmesa
->clearcolor
[2];
156 if (r
== g
&& g
== b
) {
157 /* same value for all three components (gray) */
159 for (j
= 0; j
< height
; j
++) {
160 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
161 memset(ptr3
, r
, 3 * width
);
165 /* non-gray clear color */
167 for (j
= 0; j
< height
; j
++) {
168 bgr_t
*ptr3
= PIXEL_ADDR3(xrb
, x
, y
+ j
);
169 for (i
= 0; i
< width
; i
++) {
181 clear_32bit_ximage(struct gl_context
*ctx
, struct xmesa_renderbuffer
*xrb
,
182 GLint x
, GLint y
, GLint width
, GLint height
)
184 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
185 register GLuint pixel
= (GLuint
) xmesa
->clearpixel
;
190 if (xmesa
->swapbytes
) {
191 pixel
= ((pixel
>> 24) & 0x000000ff)
192 | ((pixel
>> 8) & 0x0000ff00)
193 | ((pixel
<< 8) & 0x00ff0000)
194 | ((pixel
<< 24) & 0xff000000);
197 if (width
== xrb
->Base
.Base
.Width
&& height
== xrb
->Base
.Base
.Height
) {
198 /* clearing whole buffer */
199 const GLuint n
= xrb
->Base
.Base
.Width
* xrb
->Base
.Base
.Height
;
200 GLuint
*ptr4
= (GLuint
*) xrb
->ximage
->data
;
203 memset(ptr4
, pixel
, 4 * n
);
207 for (i
= 0; i
< n
; i
++)
212 /* clearing scissored region */
214 for (j
= 0; j
< height
; j
++) {
215 GLuint
*ptr4
= PIXEL_ADDR4(xrb
, x
, y
+ j
);
216 for (i
= 0; i
< width
; i
++) {
225 clear_nbit_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 XMesaImage
*img
= xrb
->ximage
;
232 /* TODO: optimize this */
234 for (j
= 0; j
< height
; j
++) {
235 for (i
= 0; i
< width
; i
++) {
236 XMesaPutPixel(img
, x
+i
, y
-j
, xmesa
->clearpixel
);
244 clear_buffers(struct gl_context
*ctx
, GLbitfield buffers
)
246 if (_mesa_is_winsys_fbo(ctx
->DrawBuffer
)) {
247 /* this is a window system framebuffer */
248 const GLuint
*colorMask
= (GLuint
*) &ctx
->Color
.ColorMask
[0];
249 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
250 XMesaBuffer b
= XMESA_BUFFER(ctx
->DrawBuffer
);
251 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
252 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
253 const GLint width
= ctx
->DrawBuffer
->_Xmax
- x
;
254 const GLint height
= ctx
->DrawBuffer
->_Ymax
- y
;
256 _mesa_unclamped_float_rgba_to_ubyte(xmesa
->clearcolor
,
257 ctx
->Color
.ClearColor
.f
);
258 xmesa
->clearpixel
= xmesa_color_to_pixel(ctx
,
259 xmesa
->clearcolor
[0],
260 xmesa
->clearcolor
[1],
261 xmesa
->clearcolor
[2],
262 xmesa
->clearcolor
[3],
263 xmesa
->xm_visual
->undithered_pf
);
264 XMesaSetForeground(xmesa
->display
, b
->cleargc
, xmesa
->clearpixel
);
266 /* we can't handle color or index masking */
267 if (*colorMask
== 0xffffffff && ctx
->Color
.IndexMask
== 0xffffffff) {
268 if (buffers
& BUFFER_BIT_FRONT_LEFT
) {
269 /* clear front color buffer */
270 struct gl_renderbuffer
*frontRb
271 = ctx
->DrawBuffer
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
;
272 if (b
->frontxrb
== xmesa_renderbuffer(frontRb
)) {
273 /* renderbuffer is not wrapped - great! */
274 b
->frontxrb
->clearFunc(ctx
, b
->frontxrb
, x
, y
, width
, height
);
275 buffers
&= ~BUFFER_BIT_FRONT_LEFT
;
278 /* we can't directly clear an alpha-wrapped color buffer */
281 if (buffers
& BUFFER_BIT_BACK_LEFT
) {
282 /* clear back color buffer */
283 struct gl_renderbuffer
*backRb
284 = ctx
->DrawBuffer
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
;
285 if (b
->backxrb
== xmesa_renderbuffer(backRb
)) {
286 /* renderbuffer is not wrapped - great! */
287 b
->backxrb
->clearFunc(ctx
, b
->backxrb
, x
, y
, width
, height
);
288 buffers
&= ~BUFFER_BIT_BACK_LEFT
;
294 _swrast_Clear(ctx
, buffers
);
298 /* XXX these functions haven't been tested in the Xserver environment */
302 * Check if we can do an optimized glDrawPixels into an 8R8G8B visual.
305 can_do_DrawPixels_8R8G8B(struct gl_context
*ctx
, GLenum format
, GLenum type
)
307 if (format
== GL_BGRA
&&
308 type
== GL_UNSIGNED_BYTE
&&
310 _mesa_is_winsys_fbo(ctx
->DrawBuffer
) &&
311 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
312 ctx
->Pixel
.ZoomY
== 1.0 &&
313 ctx
->_ImageTransferState
== 0 /* no color tables, scale/bias, etc */) {
314 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
316 if (swrast
->NewState
)
317 _swrast_validate_derived( ctx
);
319 if ((swrast
->_RasterMask
& ~CLIP_BIT
) == 0) /* no blend, z-test, etc */ {
320 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0];
322 struct xmesa_renderbuffer
*xrb
= xmesa_renderbuffer(rb
);
324 xrb
->pixmap
&& /* drawing to pixmap or window */
325 _mesa_get_format_bits(xrb
->Base
.Base
.Format
, GL_ALPHA_BITS
) == 0) {
336 * This function implements glDrawPixels() with an XPutImage call when
337 * drawing to the front buffer (X Window drawable).
338 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
341 xmesa_DrawPixels_8R8G8B( struct gl_context
*ctx
,
342 GLint x
, GLint y
, GLsizei width
, GLsizei height
,
343 GLenum format
, GLenum type
,
344 const struct gl_pixelstore_attrib
*unpack
,
345 const GLvoid
*pixels
)
347 if (can_do_DrawPixels_8R8G8B(ctx
, format
, type
)) {
348 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
349 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
355 if (swrast
->NewState
)
356 _swrast_validate_derived( ctx
);
358 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
359 /* unpack from PBO */
361 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
362 format
, type
, INT_MAX
, pixels
)) {
363 _mesa_error(ctx
, GL_INVALID_OPERATION
,
364 "glDrawPixels(invalid PBO access)");
367 buf
= (GLubyte
*) ctx
->Driver
.MapBufferRange(ctx
, 0,
368 unpack
->BufferObj
->Size
,
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
, MAP_INTERNAL
);
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
,
507 pixels
= ADD_POINTERS(buf
, pixels
);
510 if (_mesa_clip_drawpixels(ctx
, &dstX
, &dstY
, &w
, &h
, &clippedUnpack
)) {
511 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
512 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
513 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
514 const XMesaGC gc
= xmbuf
->cleargc
; /* effected by glColorMask */
515 struct xmesa_renderbuffer
*xrb
516 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
517 const int srcX
= clippedUnpack
.SkipPixels
;
518 const int srcY
= clippedUnpack
.SkipRows
;
519 const int rowLength
= clippedUnpack
.RowLength
;
522 assert(xmesa
->xm_visual
->undithered_pf
== PF_5R6G5B
);
526 /* This is a little tricky since all coordinates up to now have
527 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
528 * so we have to carefully compute the Y coordinates/addresses here.
530 memset(&ximage
, 0, sizeof(XMesaImage
));
531 ximage
.width
= width
;
532 ximage
.height
= height
;
533 ximage
.format
= ZPixmap
;
534 ximage
.data
= (char *) pixels
535 + ((srcY
+ h
- 1) * rowLength
+ srcX
) * 2;
536 ximage
.byte_order
= LSBFirst
;
537 ximage
.bitmap_unit
= 16;
538 ximage
.bitmap_bit_order
= LSBFirst
;
539 ximage
.bitmap_pad
= 16;
541 ximage
.bits_per_pixel
= 16;
542 ximage
.bytes_per_line
= -rowLength
* 2; /* negative to flip image */
543 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
544 /* flip Y axis for dest position */
545 dstY
= YFLIP(xrb
, dstY
) - h
+ 1;
546 XPutImage(dpy
, xrb
->pixmap
, gc
, &ximage
, 0, 0, dstX
, dstY
, w
, h
);
549 if (unpack
->BufferObj
->Name
) {
550 ctx
->Driver
.UnmapBuffer(ctx
, unpack
->BufferObj
, MAP_INTERNAL
);
554 /* software fallback */
555 _swrast_DrawPixels(ctx
, x
, y
, width
, height
,
556 format
, type
, unpack
, pixels
);
562 * Determine if we can do an optimized glCopyPixels.
565 can_do_CopyPixels(struct gl_context
*ctx
, GLenum type
)
567 if (type
== GL_COLOR
&&
568 ctx
->_ImageTransferState
== 0 && /* no color tables, scale/bias, etc */
569 ctx
->Pixel
.ZoomX
== 1.0 && /* no zooming */
570 ctx
->Pixel
.ZoomY
== 1.0 &&
571 ctx
->Color
.DrawBuffer
[0] == GL_FRONT
&& /* copy to front buf */
572 ctx
->Pixel
.ReadBuffer
== GL_FRONT
&& /* copy from front buf */
573 ctx
->ReadBuffer
->_ColorReadBuffer
&&
574 ctx
->DrawBuffer
->_ColorDrawBuffers
[0]) {
575 const SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
577 if (swrast
->NewState
)
578 _swrast_validate_derived( ctx
);
580 if ((swrast
->_RasterMask
& ~CLIP_BIT
) == 0x0 &&
582 ctx
->ReadBuffer
->_ColorReadBuffer
&&
584 ctx
->DrawBuffer
->_ColorDrawBuffers
[0]) {
585 struct xmesa_renderbuffer
*srcXrb
586 = xmesa_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
);
587 struct xmesa_renderbuffer
*dstXrb
588 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
589 if (srcXrb
->pixmap
&& dstXrb
->pixmap
) {
599 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
600 * for the color buffer. Don't support zooming, pixel transfer, etc.
601 * We do support copying from one window to another, ala glXMakeCurrentRead.
604 xmesa_CopyPixels( struct gl_context
*ctx
,
605 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
606 GLint destx
, GLint desty
, GLenum type
)
608 if (can_do_CopyPixels(ctx
, type
)) {
609 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
610 XMesaDisplay
*dpy
= xmesa
->xm_visual
->display
;
611 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
612 const XMesaGC gc
= xmbuf
->cleargc
; /* effected by glColorMask */
613 struct xmesa_renderbuffer
*srcXrb
614 = xmesa_renderbuffer(ctx
->ReadBuffer
->_ColorReadBuffer
);
615 struct xmesa_renderbuffer
*dstXrb
616 = xmesa_renderbuffer(ctx
->DrawBuffer
->_ColorDrawBuffers
[0]);
621 /* Note: we don't do any special clipping work here. We could,
622 * but X will do it for us.
624 srcy
= YFLIP(srcXrb
, srcy
) - height
+ 1;
625 desty
= YFLIP(dstXrb
, desty
) - height
+ 1;
626 XCopyArea(dpy
, srcXrb
->pixmap
, dstXrb
->pixmap
, gc
,
627 srcx
, srcy
, width
, height
, destx
, desty
);
630 _swrast_CopyPixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
);
638 * Every driver should implement a GetString function in order to
639 * return a meaningful GL_RENDERER string.
641 static const GLubyte
*
642 get_string( struct gl_context
*ctx
, GLenum name
)
647 return (const GLubyte
*) "Mesa X11";
657 * We implement the glEnable function only because we care about
658 * dither enable/disable.
661 enable( struct gl_context
*ctx
, GLenum pname
, GLboolean state
)
663 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
668 xmesa
->pixelformat
= xmesa
->xm_visual
->dithered_pf
;
670 xmesa
->pixelformat
= xmesa
->xm_visual
->undithered_pf
;
673 ; /* silence compiler warning */
679 * Called when the driver should update its state, based on the new_state
683 xmesa_update_state(struct gl_context
*ctx
)
685 GLbitfield new_state
= ctx
->NewState
;
686 const XMesaContext xmesa
= XMESA_CONTEXT(ctx
);
688 if (new_state
& (_NEW_SCISSOR
| _NEW_BUFFERS
| _NEW_VIEWPORT
))
689 _mesa_update_draw_buffer_bounds(ctx
, ctx
->DrawBuffer
);
691 /* Propagate statechange information to swrast and swrast_setup
692 * modules. The X11 driver has no internal GL-dependent state.
694 _swrast_InvalidateState( ctx
, new_state
);
695 _tnl_InvalidateState( ctx
, new_state
);
696 _swsetup_InvalidateState( ctx
, new_state
);
698 if (_mesa_is_user_fbo(ctx
->DrawBuffer
))
702 * GL_DITHER, GL_READ/DRAW_BUFFER, buffer binding state, etc. effect
703 * renderbuffer span/clear funcs.
704 * Check _NEW_COLOR to detect dither enable/disable.
706 if (new_state
& (_NEW_COLOR
| _NEW_BUFFERS
)) {
707 XMesaBuffer xmbuf
= XMESA_BUFFER(ctx
->DrawBuffer
);
708 struct xmesa_renderbuffer
*front_xrb
, *back_xrb
;
710 front_xrb
= xmbuf
->frontxrb
;
712 front_xrb
->clearFunc
= clear_pixmap
;
715 back_xrb
= xmbuf
->backxrb
;
717 if (xmbuf
->backxrb
->pixmap
) {
718 back_xrb
->clearFunc
= clear_pixmap
;
721 switch (xmesa
->xm_visual
->BitsPerPixel
) {
723 back_xrb
->clearFunc
= clear_16bit_ximage
;
726 back_xrb
->clearFunc
= clear_24bit_ximage
;
729 back_xrb
->clearFunc
= clear_32bit_ximage
;
732 back_xrb
->clearFunc
= clear_nbit_ximage
;
742 * Called by glViewport.
743 * This is a good time for us to poll the current X window size and adjust
744 * our renderbuffers to match the current window size.
745 * Remember, we have no opportunity to respond to conventional
746 * X Resize/StructureNotify events since the X driver has no event loop.
748 * Note that this trick isn't fool-proof. If the application never calls
749 * glViewport, our notion of the current window size may be incorrect.
750 * That problem led to the GLX_MESA_resize_buffers extension.
753 xmesa_viewport(struct gl_context
*ctx
)
755 XMesaContext xmctx
= XMESA_CONTEXT(ctx
);
756 XMesaBuffer xmdrawbuf
= XMESA_BUFFER(ctx
->WinSysDrawBuffer
);
757 XMesaBuffer xmreadbuf
= XMESA_BUFFER(ctx
->WinSysReadBuffer
);
758 xmesa_check_and_update_buffer_size(xmctx
, xmdrawbuf
);
759 xmesa_check_and_update_buffer_size(xmctx
, xmreadbuf
);
763 #if ENABLE_EXT_timer_query
766 * The GL_EXT_timer_query extension is not enabled for the XServer
767 * indirect renderer. Not sure about how/if wrapping of gettimeofday()
771 struct xmesa_query_object
773 struct gl_query_object Base
;
774 struct timeval StartTime
;
778 static struct gl_query_object
*
779 xmesa_new_query_object(struct gl_context
*ctx
, GLuint id
)
781 struct xmesa_query_object
*q
= CALLOC_STRUCT(xmesa_query_object
);
784 q
->Base
.Ready
= GL_TRUE
;
791 xmesa_begin_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
793 if (q
->Target
== GL_TIME_ELAPSED_EXT
) {
794 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
795 (void) gettimeofday(&xq
->StartTime
, NULL
);
801 * Return the difference between the two given times in microseconds.
804 time_diff(const struct timeval
*t0
, const struct timeval
*t1
)
806 GLuint64EXT seconds0
= t0
->tv_sec
& 0xff; /* 0 .. 255 seconds */
807 GLuint64EXT seconds1
= t1
->tv_sec
& 0xff; /* 0 .. 255 seconds */
808 GLuint64EXT nanosec0
= (seconds0
* 1000000 + t0
->tv_usec
) * 1000;
809 GLuint64EXT nanosec1
= (seconds1
* 1000000 + t1
->tv_usec
) * 1000;
810 return nanosec1
- nanosec0
;
815 xmesa_end_query(struct gl_context
*ctx
, struct gl_query_object
*q
)
817 if (q
->Target
== GL_TIME_ELAPSED_EXT
) {
818 struct xmesa_query_object
*xq
= (struct xmesa_query_object
*) q
;
819 struct timeval endTime
;
820 (void) gettimeofday(&endTime
, NULL
);
821 /* result is in nanoseconds! */
822 q
->Result
= time_diff(&xq
->StartTime
, &endTime
);
827 #endif /* ENABLE_timer_query */
831 * Initialize the device driver function table with the functions
832 * we implement in this driver.
835 xmesa_init_driver_functions( XMesaVisual xmvisual
,
836 struct dd_function_table
*driver
)
838 driver
->GetString
= get_string
;
839 driver
->UpdateState
= xmesa_update_state
;
840 driver
->Flush
= finish_or_flush
;
841 driver
->Finish
= finish_or_flush
;
842 driver
->ColorMask
= color_mask
;
843 driver
->Enable
= enable
;
844 driver
->Viewport
= xmesa_viewport
;
845 if (TEST_META_FUNCS
) {
846 driver
->Clear
= _mesa_meta_Clear
;
847 driver
->CopyPixels
= _mesa_meta_CopyPixels
;
848 driver
->BlitFramebuffer
= _mesa_meta_and_swrast_BlitFramebuffer
;
849 driver
->DrawPixels
= _mesa_meta_DrawPixels
;
850 driver
->Bitmap
= _mesa_meta_Bitmap
;
853 driver
->Clear
= clear_buffers
;
854 driver
->CopyPixels
= xmesa_CopyPixels
;
855 if (xmvisual
->undithered_pf
== PF_8R8G8B
&&
856 xmvisual
->dithered_pf
== PF_8R8G8B
&&
857 xmvisual
->BitsPerPixel
== 32) {
858 driver
->DrawPixels
= xmesa_DrawPixels_8R8G8B
;
860 else if (xmvisual
->undithered_pf
== PF_5R6G5B
) {
861 driver
->DrawPixels
= xmesa_DrawPixels_5R6G5B
;
865 driver
->MapRenderbuffer
= xmesa_MapRenderbuffer
;
866 driver
->UnmapRenderbuffer
= xmesa_UnmapRenderbuffer
;
868 driver
->GenerateMipmap
= _mesa_generate_mipmap
;
870 #if ENABLE_EXT_timer_query
871 driver
->NewQueryObject
= xmesa_new_query_object
;
872 driver
->BeginQuery
= xmesa_begin_query
;
873 driver
->EndQuery
= xmesa_end_query
;
878 #define XMESA_NEW_POINT (_NEW_POINT | \
880 _SWRAST_NEW_RASTERMASK)
882 #define XMESA_NEW_LINE (_NEW_LINE | \
887 _SWRAST_NEW_RASTERMASK)
889 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
894 _SWRAST_NEW_RASTERMASK)
898 * Extend the software rasterizer with our line/point/triangle
900 * Called during context creation only.
902 void xmesa_register_swrast_functions( struct gl_context
*ctx
)
904 SWcontext
*swrast
= SWRAST_CONTEXT( ctx
);
906 swrast
->choose_point
= xmesa_choose_point
;
907 swrast
->choose_line
= xmesa_choose_line
;
908 swrast
->choose_triangle
= xmesa_choose_triangle
;
910 /* XXX these lines have no net effect. Remove??? */
911 swrast
->InvalidatePointMask
|= XMESA_NEW_POINT
;
912 swrast
->InvalidateLineMask
|= XMESA_NEW_LINE
;
913 swrast
->InvalidateTriangleMask
|= XMESA_NEW_TRIANGLE
;