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/mipmap.h"
38 #include "main/image.h"
39 #include "main/imports.h"
40 #include "main/mtypes.h"
42 #include "main/texformat.h"
43 #include "swrast/swrast.h"
44 #include "swrast/s_context.h"
45 #include "swrast_setup/swrast_setup.h"
47 #include "tnl/t_context.h"
48 #include "drivers/common/meta.h"
53 finish_or_flush( struct gl_context *ctx )
55 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
57 _glthread_LOCK_MUTEX(_xmesa_lock);
58 XSync( xmesa->display, False );
59 _glthread_UNLOCK_MUTEX(_xmesa_lock);
64 /* Implements glColorMask() */
66 color_mask(struct gl_context *ctx,
67 GLboolean rmask, GLboolean gmask, GLboolean bmask, GLboolean amask)
69 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
71 const int xclass = xmesa->xm_visual->visualType;
74 if (_mesa_is_user_fbo(ctx->DrawBuffer))
77 xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
79 if (xclass == GLX_TRUE_COLOR || xclass == GLX_DIRECT_COLOR) {
81 if (rmask && gmask && bmask) {
82 m = ((unsigned long)~0L);
86 if (rmask) m |= GET_REDMASK(xmesa->xm_visual);
87 if (gmask) m |= GET_GREENMASK(xmesa->xm_visual);
88 if (bmask) m |= GET_BLUEMASK(xmesa->xm_visual);
90 XMesaSetPlaneMask( xmesa->display, xmbuf->cleargc, m );
96 /**********************************************************************/
97 /*** glClear implementations ***/
98 /**********************************************************************/
102 * Clear the front or back color buffer, if it's implemented with a pixmap.
105 clear_pixmap(struct gl_context *ctx, struct xmesa_renderbuffer *xrb,
106 GLint x, GLint y, GLint width, GLint height)
108 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
109 XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
114 assert(xmesa->display);
116 assert(xmbuf->cleargc);
118 XMesaFillRectangle( xmesa->display, xrb->pixmap, xmbuf->cleargc,
119 x, xrb->Base.Base.Height - y - height,
125 clear_16bit_ximage( struct gl_context *ctx, struct xmesa_renderbuffer *xrb,
126 GLint x, GLint y, GLint width, GLint height)
128 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
129 GLuint pixel = (GLuint) xmesa->clearpixel;
132 if (xmesa->swapbytes) {
133 pixel = ((pixel >> 8) & 0x00ff) | ((pixel << 8) & 0xff00);
136 for (j = 0; j < height; j++) {
137 GLushort *ptr2 = PIXEL_ADDR2(xrb, x, y + j);
138 for (i = 0; i < width; i++) {
145 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
147 clear_24bit_ximage(struct gl_context *ctx, struct xmesa_renderbuffer *xrb,
148 GLint x, GLint y, GLint width, GLint height)
150 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
151 const GLubyte r = xmesa->clearcolor[0];
152 const GLubyte g = xmesa->clearcolor[1];
153 const GLubyte b = xmesa->clearcolor[2];
155 if (r == g && g == b) {
156 /* same value for all three components (gray) */
158 for (j = 0; j < height; j++) {
159 bgr_t *ptr3 = PIXEL_ADDR3(xrb, x, y + j);
160 memset(ptr3, r, 3 * width);
164 /* non-gray clear color */
166 for (j = 0; j < height; j++) {
167 bgr_t *ptr3 = PIXEL_ADDR3(xrb, x, y + j);
168 for (i = 0; i < width; i++) {
180 clear_32bit_ximage(struct gl_context *ctx, struct xmesa_renderbuffer *xrb,
181 GLint x, GLint y, GLint width, GLint height)
183 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
184 register GLuint pixel = (GLuint) xmesa->clearpixel;
189 if (xmesa->swapbytes) {
190 pixel = ((pixel >> 24) & 0x000000ff)
191 | ((pixel >> 8) & 0x0000ff00)
192 | ((pixel << 8) & 0x00ff0000)
193 | ((pixel << 24) & 0xff000000);
196 if (width == xrb->Base.Base.Width && height == xrb->Base.Base.Height) {
197 /* clearing whole buffer */
198 const GLuint n = xrb->Base.Base.Width * xrb->Base.Base.Height;
199 GLuint *ptr4 = (GLuint *) xrb->ximage->data;
202 memset(ptr4, pixel, 4 * n);
206 for (i = 0; i < n; i++)
211 /* clearing scissored region */
213 for (j = 0; j < height; j++) {
214 GLuint *ptr4 = PIXEL_ADDR4(xrb, x, y + j);
215 for (i = 0; i < width; i++) {
224 clear_nbit_ximage(struct gl_context *ctx, struct xmesa_renderbuffer *xrb,
225 GLint x, GLint y, GLint width, GLint height)
227 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
228 XMesaImage *img = xrb->ximage;
231 /* TODO: optimize this */
233 for (j = 0; j < height; j++) {
234 for (i = 0; i < width; i++) {
235 XMesaPutPixel(img, x+i, y-j, xmesa->clearpixel);
243 clear_buffers(struct gl_context *ctx, GLbitfield buffers)
245 if (_mesa_is_winsys_fbo(ctx->DrawBuffer)) {
246 /* this is a window system framebuffer */
247 const GLuint *colorMask = (GLuint *) &ctx->Color.ColorMask[0];
248 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
249 XMesaBuffer b = XMESA_BUFFER(ctx->DrawBuffer);
250 const GLint x = ctx->DrawBuffer->_Xmin;
251 const GLint y = ctx->DrawBuffer->_Ymin;
252 const GLint width = ctx->DrawBuffer->_Xmax - x;
253 const GLint height = ctx->DrawBuffer->_Ymax - y;
255 _mesa_unclamped_float_rgba_to_ubyte(xmesa->clearcolor,
256 ctx->Color.ClearColor.f);
257 xmesa->clearpixel = xmesa_color_to_pixel(ctx,
258 xmesa->clearcolor[0],
259 xmesa->clearcolor[1],
260 xmesa->clearcolor[2],
261 xmesa->clearcolor[3],
262 xmesa->xm_visual->undithered_pf);
263 XMesaSetForeground(xmesa->display, b->cleargc, xmesa->clearpixel);
265 /* we can't handle color or index masking */
266 if (*colorMask == 0xffffffff && ctx->Color.IndexMask == 0xffffffff) {
267 if (buffers & BUFFER_BIT_FRONT_LEFT) {
268 /* clear front color buffer */
269 struct gl_renderbuffer *frontRb
270 = ctx->DrawBuffer->Attachment[BUFFER_FRONT_LEFT].Renderbuffer;
271 if (b->frontxrb == xmesa_renderbuffer(frontRb)) {
272 /* renderbuffer is not wrapped - great! */
273 b->frontxrb->clearFunc(ctx, b->frontxrb, x, y, width, height);
274 buffers &= ~BUFFER_BIT_FRONT_LEFT;
277 /* we can't directly clear an alpha-wrapped color buffer */
280 if (buffers & BUFFER_BIT_BACK_LEFT) {
281 /* clear back color buffer */
282 struct gl_renderbuffer *backRb
283 = ctx->DrawBuffer->Attachment[BUFFER_BACK_LEFT].Renderbuffer;
284 if (b->backxrb == xmesa_renderbuffer(backRb)) {
285 /* renderbuffer is not wrapped - great! */
286 b->backxrb->clearFunc(ctx, b->backxrb, x, y, width, height);
287 buffers &= ~BUFFER_BIT_BACK_LEFT;
293 _swrast_Clear(ctx, buffers);
297 /* XXX these functions haven't been tested in the Xserver environment */
301 * Check if we can do an optimized glDrawPixels into an 8R8G8B visual.
304 can_do_DrawPixels_8R8G8B(struct gl_context *ctx, GLenum format, GLenum type)
306 if (format == GL_BGRA &&
307 type == GL_UNSIGNED_BYTE &&
309 _mesa_is_winsys_fbo(ctx->DrawBuffer) &&
310 ctx->Pixel.ZoomX == 1.0 && /* no zooming */
311 ctx->Pixel.ZoomY == 1.0 &&
312 ctx->_ImageTransferState == 0 /* no color tables, scale/bias, etc */) {
313 const SWcontext *swrast = SWRAST_CONTEXT(ctx);
315 if (swrast->NewState)
316 _swrast_validate_derived( ctx );
318 if ((swrast->_RasterMask & ~CLIP_BIT) == 0) /* no blend, z-test, etc */ {
319 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0];
321 struct xmesa_renderbuffer *xrb = xmesa_renderbuffer(rb);
323 xrb->pixmap && /* drawing to pixmap or window */
324 _mesa_get_format_bits(xrb->Base.Base.Format, GL_ALPHA_BITS) == 0) {
335 * This function implements glDrawPixels() with an XPutImage call when
336 * drawing to the front buffer (X Window drawable).
337 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
340 xmesa_DrawPixels_8R8G8B( struct gl_context *ctx,
341 GLint x, GLint y, GLsizei width, GLsizei height,
342 GLenum format, GLenum type,
343 const struct gl_pixelstore_attrib *unpack,
344 const GLvoid *pixels )
346 if (can_do_DrawPixels_8R8G8B(ctx, format, type)) {
347 const SWcontext *swrast = SWRAST_CONTEXT( ctx );
348 struct gl_pixelstore_attrib clippedUnpack = *unpack;
354 if (swrast->NewState)
355 _swrast_validate_derived( ctx );
357 if (_mesa_is_bufferobj(unpack->BufferObj)) {
358 /* unpack from PBO */
360 if (!_mesa_validate_pbo_access(2, unpack, width, height, 1,
361 format, type, INT_MAX, pixels)) {
362 _mesa_error(ctx, GL_INVALID_OPERATION,
363 "glDrawPixels(invalid PBO access)");
366 buf = (GLubyte *) ctx->Driver.MapBufferRange(ctx, 0,
367 unpack->BufferObj->Size,
371 /* buffer is already mapped - that's an error */
372 _mesa_error(ctx, GL_INVALID_OPERATION,
373 "glDrawPixels(PBO is mapped)");
376 pixels = ADD_POINTERS(buf, pixels);
379 if (_mesa_clip_drawpixels(ctx, &dstX, &dstY, &w, &h, &clippedUnpack)) {
380 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
381 XMesaDisplay *dpy = xmesa->xm_visual->display;
382 XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
383 const XMesaGC gc = xmbuf->cleargc; /* effected by glColorMask */
384 struct xmesa_renderbuffer *xrb
385 = xmesa_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
386 const int srcX = clippedUnpack.SkipPixels;
387 const int srcY = clippedUnpack.SkipRows;
388 const int rowLength = clippedUnpack.RowLength;
391 ASSERT(xmesa->xm_visual->dithered_pf == PF_8R8G8B);
392 ASSERT(xmesa->xm_visual->undithered_pf == PF_8R8G8B);
396 /* This is a little tricky since all coordinates up to now have
397 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
398 * so we have to carefully compute the Y coordinates/addresses here.
400 memset(&ximage, 0, sizeof(XMesaImage));
401 ximage.width = width;
402 ximage.height = height;
403 ximage.format = ZPixmap;
404 ximage.data = (char *) pixels
405 + ((srcY + h - 1) * rowLength + srcX) * 4;
406 ximage.byte_order = LSBFirst;
407 ximage.bitmap_unit = 32;
408 ximage.bitmap_bit_order = LSBFirst;
409 ximage.bitmap_pad = 32;
411 ximage.bits_per_pixel = 32;
412 ximage.bytes_per_line = -rowLength * 4; /* negative to flip image */
413 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
414 /* flip Y axis for dest position */
415 dstY = YFLIP(xrb, dstY) - h + 1;
416 XPutImage(dpy, xrb->pixmap, gc, &ximage, 0, 0, dstX, dstY, w, h);
419 if (_mesa_is_bufferobj(unpack->BufferObj)) {
420 ctx->Driver.UnmapBuffer(ctx, unpack->BufferObj);
424 /* software fallback */
425 _swrast_DrawPixels(ctx, x, y, width, height,
426 format, type, unpack, pixels);
433 * Check if we can do an optimized glDrawPixels into an 5R6G5B visual.
436 can_do_DrawPixels_5R6G5B(struct gl_context *ctx, GLenum format, GLenum type)
438 if (format == GL_RGB &&
439 type == GL_UNSIGNED_SHORT_5_6_5 &&
440 !ctx->Color.DitherFlag && /* no dithering */
442 _mesa_is_winsys_fbo(ctx->DrawBuffer) &&
443 ctx->Pixel.ZoomX == 1.0 && /* no zooming */
444 ctx->Pixel.ZoomY == 1.0 &&
445 ctx->_ImageTransferState == 0 /* no color tables, scale/bias, etc */) {
446 const SWcontext *swrast = SWRAST_CONTEXT(ctx);
448 if (swrast->NewState)
449 _swrast_validate_derived( ctx );
451 if ((swrast->_RasterMask & ~CLIP_BIT) == 0) /* no blend, z-test, etc */ {
452 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0];
454 struct xmesa_renderbuffer *xrb = xmesa_renderbuffer(rb);
456 xrb->pixmap && /* drawing to pixmap or window */
457 _mesa_get_format_bits(xrb->Base.Base.Format, GL_ALPHA_BITS) == 0) {
468 * This function implements glDrawPixels() with an XPutImage call when
469 * drawing to the front buffer (X Window drawable). The image format
470 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
471 * match the PF_5R6G5B pixel format.
474 xmesa_DrawPixels_5R6G5B( struct gl_context *ctx,
475 GLint x, GLint y, GLsizei width, GLsizei height,
476 GLenum format, GLenum type,
477 const struct gl_pixelstore_attrib *unpack,
478 const GLvoid *pixels )
480 if (can_do_DrawPixels_5R6G5B(ctx, format, type)) {
481 const SWcontext *swrast = SWRAST_CONTEXT( ctx );
482 struct gl_pixelstore_attrib clippedUnpack = *unpack;
488 if (swrast->NewState)
489 _swrast_validate_derived( ctx );
491 if (_mesa_is_bufferobj(unpack->BufferObj)) {
492 /* unpack from PBO */
494 if (!_mesa_validate_pbo_access(2, unpack, width, height, 1,
495 format, type, INT_MAX, pixels)) {
496 _mesa_error(ctx, GL_INVALID_OPERATION,
497 "glDrawPixels(invalid PBO access)");
500 buf = (GLubyte *) ctx->Driver.MapBufferRange(ctx, 0,
501 unpack->BufferObj->Size,
505 /* buffer is already mapped - that's an error */
506 _mesa_error(ctx, GL_INVALID_OPERATION,
507 "glDrawPixels(PBO is mapped)");
510 pixels = ADD_POINTERS(buf, pixels);
513 if (_mesa_clip_drawpixels(ctx, &dstX, &dstY, &w, &h, &clippedUnpack)) {
514 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
515 XMesaDisplay *dpy = xmesa->xm_visual->display;
516 XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
517 const XMesaGC gc = xmbuf->cleargc; /* effected by glColorMask */
518 struct xmesa_renderbuffer *xrb
519 = xmesa_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
520 const int srcX = clippedUnpack.SkipPixels;
521 const int srcY = clippedUnpack.SkipRows;
522 const int rowLength = clippedUnpack.RowLength;
525 ASSERT(xmesa->xm_visual->undithered_pf == PF_5R6G5B);
529 /* This is a little tricky since all coordinates up to now have
530 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
531 * so we have to carefully compute the Y coordinates/addresses here.
533 memset(&ximage, 0, sizeof(XMesaImage));
534 ximage.width = width;
535 ximage.height = height;
536 ximage.format = ZPixmap;
537 ximage.data = (char *) pixels
538 + ((srcY + h - 1) * rowLength + srcX) * 2;
539 ximage.byte_order = LSBFirst;
540 ximage.bitmap_unit = 16;
541 ximage.bitmap_bit_order = LSBFirst;
542 ximage.bitmap_pad = 16;
544 ximage.bits_per_pixel = 16;
545 ximage.bytes_per_line = -rowLength * 2; /* negative to flip image */
546 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
547 /* flip Y axis for dest position */
548 dstY = YFLIP(xrb, dstY) - h + 1;
549 XPutImage(dpy, xrb->pixmap, gc, &ximage, 0, 0, dstX, dstY, w, h);
552 if (unpack->BufferObj->Name) {
553 ctx->Driver.UnmapBuffer(ctx, unpack->BufferObj);
557 /* software fallback */
558 _swrast_DrawPixels(ctx, x, y, width, height,
559 format, type, unpack, pixels);
565 * Determine if we can do an optimized glCopyPixels.
568 can_do_CopyPixels(struct gl_context *ctx, GLenum type)
570 if (type == GL_COLOR &&
571 ctx->_ImageTransferState == 0 && /* no color tables, scale/bias, etc */
572 ctx->Pixel.ZoomX == 1.0 && /* no zooming */
573 ctx->Pixel.ZoomY == 1.0 &&
574 ctx->Color.DrawBuffer[0] == GL_FRONT && /* copy to front buf */
575 ctx->Pixel.ReadBuffer == GL_FRONT && /* copy from front buf */
576 ctx->ReadBuffer->_ColorReadBuffer &&
577 ctx->DrawBuffer->_ColorDrawBuffers[0]) {
578 const SWcontext *swrast = SWRAST_CONTEXT( ctx );
580 if (swrast->NewState)
581 _swrast_validate_derived( ctx );
583 if ((swrast->_RasterMask & ~CLIP_BIT) == 0x0 &&
585 ctx->ReadBuffer->_ColorReadBuffer &&
587 ctx->DrawBuffer->_ColorDrawBuffers[0]) {
588 struct xmesa_renderbuffer *srcXrb
589 = xmesa_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
590 struct xmesa_renderbuffer *dstXrb
591 = xmesa_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
592 if (srcXrb->pixmap && dstXrb->pixmap) {
602 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
603 * for the color buffer. Don't support zooming, pixel transfer, etc.
604 * We do support copying from one window to another, ala glXMakeCurrentRead.
607 xmesa_CopyPixels( struct gl_context *ctx,
608 GLint srcx, GLint srcy, GLsizei width, GLsizei height,
609 GLint destx, GLint desty, GLenum type )
611 if (can_do_CopyPixels(ctx, type)) {
612 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
613 XMesaDisplay *dpy = xmesa->xm_visual->display;
614 XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
615 const XMesaGC gc = xmbuf->cleargc; /* effected by glColorMask */
616 struct xmesa_renderbuffer *srcXrb
617 = xmesa_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
618 struct xmesa_renderbuffer *dstXrb
619 = xmesa_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
624 /* Note: we don't do any special clipping work here. We could,
625 * but X will do it for us.
627 srcy = YFLIP(srcXrb, srcy) - height + 1;
628 desty = YFLIP(dstXrb, desty) - height + 1;
629 XCopyArea(dpy, srcXrb->pixmap, dstXrb->pixmap, gc,
630 srcx, srcy, width, height, destx, desty);
633 _swrast_CopyPixels(ctx, srcx, srcy, width, height, destx, desty, type );
641 * Every driver should implement a GetString function in order to
642 * return a meaningful GL_RENDERER string.
644 static const GLubyte *
645 get_string( struct gl_context *ctx, GLenum name )
650 return (const GLubyte *) "Mesa X11";
660 * We implement the glEnable function only because we care about
661 * dither enable/disable.
664 enable( struct gl_context *ctx, GLenum pname, GLboolean state )
666 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
671 xmesa->pixelformat = xmesa->xm_visual->dithered_pf;
673 xmesa->pixelformat = xmesa->xm_visual->undithered_pf;
676 ; /* silence compiler warning */
682 * Called when the driver should update its state, based on the new_state
686 xmesa_update_state( struct gl_context *ctx, GLbitfield new_state )
688 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
690 /* Propagate statechange information to swrast and swrast_setup
691 * modules. The X11 driver has no internal GL-dependent state.
693 _swrast_InvalidateState( ctx, new_state );
694 _tnl_InvalidateState( ctx, new_state );
695 _vbo_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, GLint x, GLint y, GLsizei w, GLsizei h)
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);
767 #if ENABLE_EXT_timer_query
770 * The GL_EXT_timer_query extension is not enabled for the XServer
771 * indirect renderer. Not sure about how/if wrapping of gettimeofday()
775 struct xmesa_query_object
777 struct gl_query_object Base;
778 struct timeval StartTime;
782 static struct gl_query_object *
783 xmesa_new_query_object(struct gl_context *ctx, GLuint id)
785 struct xmesa_query_object *q = CALLOC_STRUCT(xmesa_query_object);
788 q->Base.Ready = GL_TRUE;
795 xmesa_begin_query(struct gl_context *ctx, struct gl_query_object *q)
797 if (q->Target == GL_TIME_ELAPSED_EXT) {
798 struct xmesa_query_object *xq = (struct xmesa_query_object *) q;
799 (void) gettimeofday(&xq->StartTime, NULL);
805 * Return the difference between the two given times in microseconds.
808 time_diff(const struct timeval *t0, const struct timeval *t1)
810 GLuint64EXT seconds0 = t0->tv_sec & 0xff; /* 0 .. 255 seconds */
811 GLuint64EXT seconds1 = t1->tv_sec & 0xff; /* 0 .. 255 seconds */
812 GLuint64EXT nanosec0 = (seconds0 * 1000000 + t0->tv_usec) * 1000;
813 GLuint64EXT nanosec1 = (seconds1 * 1000000 + t1->tv_usec) * 1000;
814 return nanosec1 - nanosec0;
819 xmesa_end_query(struct gl_context *ctx, struct gl_query_object *q)
821 if (q->Target == GL_TIME_ELAPSED_EXT) {
822 struct xmesa_query_object *xq = (struct xmesa_query_object *) q;
823 struct timeval endTime;
824 (void) gettimeofday(&endTime, NULL);
825 /* result is in nanoseconds! */
826 q->Result = time_diff(&xq->StartTime, &endTime);
831 #endif /* ENABLE_timer_query */
835 * Initialize the device driver function table with the functions
836 * we implement in this driver.
839 xmesa_init_driver_functions( XMesaVisual xmvisual,
840 struct dd_function_table *driver )
842 driver->GetString = get_string;
843 driver->UpdateState = xmesa_update_state;
844 driver->GetBufferSize = NULL; /* OBSOLETE */
845 driver->Flush = finish_or_flush;
846 driver->Finish = finish_or_flush;
847 driver->ColorMask = color_mask;
848 driver->Enable = enable;
849 driver->Viewport = xmesa_viewport;
850 if (TEST_META_FUNCS) {
851 driver->Clear = _mesa_meta_Clear;
852 driver->CopyPixels = _mesa_meta_CopyPixels;
853 driver->BlitFramebuffer = _mesa_meta_BlitFramebuffer;
854 driver->DrawPixels = _mesa_meta_DrawPixels;
855 driver->Bitmap = _mesa_meta_Bitmap;
858 driver->Clear = clear_buffers;
859 driver->CopyPixels = xmesa_CopyPixels;
860 if (xmvisual->undithered_pf == PF_8R8G8B &&
861 xmvisual->dithered_pf == PF_8R8G8B &&
862 xmvisual->BitsPerPixel == 32) {
863 driver->DrawPixels = xmesa_DrawPixels_8R8G8B;
865 else if (xmvisual->undithered_pf == PF_5R6G5B) {
866 driver->DrawPixels = xmesa_DrawPixels_5R6G5B;
870 driver->MapRenderbuffer = xmesa_MapRenderbuffer;
871 driver->UnmapRenderbuffer = xmesa_UnmapRenderbuffer;
873 driver->GenerateMipmap = _mesa_generate_mipmap;
875 #if ENABLE_EXT_timer_query
876 driver->NewQueryObject = xmesa_new_query_object;
877 driver->BeginQuery = xmesa_begin_query;
878 driver->EndQuery = xmesa_end_query;
883 #define XMESA_NEW_POINT (_NEW_POINT | \
885 _SWRAST_NEW_RASTERMASK)
887 #define XMESA_NEW_LINE (_NEW_LINE | \
892 _SWRAST_NEW_RASTERMASK)
894 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
899 _SWRAST_NEW_RASTERMASK)
903 * Extend the software rasterizer with our line/point/triangle
905 * Called during context creation only.
907 void xmesa_register_swrast_functions( struct gl_context *ctx )
909 SWcontext *swrast = SWRAST_CONTEXT( ctx );
911 swrast->choose_point = xmesa_choose_point;
912 swrast->choose_line = xmesa_choose_line;
913 swrast->choose_triangle = xmesa_choose_triangle;
915 /* XXX these lines have no net effect. Remove??? */
916 swrast->InvalidatePointMask |= XMESA_NEW_POINT;
917 swrast->InvalidateLineMask |= XMESA_NEW_LINE;
918 swrast->InvalidateTriangleMask |= XMESA_NEW_TRIANGLE;