OSDN Git Service

07abf06464840a64463260abc11cc403257fe4e6
[android-x86/external-mesa.git] / src / mesa / drivers / x11 / xm_dd.c
1 /*
2  * Mesa 3-D graphics library
3  * Version:  6.5.2
4  *
5  * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
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  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  * OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26
27 /**
28  * \file xm_dd.h
29  * General device driver functions for Xlib driver.
30  */
31
32 #include "glxheader.h"
33 #include "main/bufferobj.h"
34 #include "main/context.h"
35 #include "main/colormac.h"
36 #include "main/fbobject.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"
42 #include "main/pbo.h"
43 #include "main/texformat.h"
44 #include "swrast/swrast.h"
45 #include "swrast/s_context.h"
46 #include "swrast_setup/swrast_setup.h"
47 #include "tnl/tnl.h"
48 #include "tnl/t_context.h"
49 #include "drivers/common/meta.h"
50 #include "xmesaP.h"
51
52
53 static void
54 finish_or_flush( struct gl_context *ctx )
55 {
56    const XMesaContext xmesa = XMESA_CONTEXT(ctx);
57    if (xmesa) {
58       _glthread_LOCK_MUTEX(_xmesa_lock);
59       XSync( xmesa->display, False );
60       _glthread_UNLOCK_MUTEX(_xmesa_lock);
61    }
62 }
63
64
65 /* Implements glColorMask() */
66 static void
67 color_mask(struct gl_context *ctx,
68            GLboolean rmask, GLboolean gmask, GLboolean bmask, GLboolean amask)
69 {
70    const XMesaContext xmesa = XMESA_CONTEXT(ctx);
71    XMesaBuffer xmbuf;
72    const int xclass = xmesa->xm_visual->visualType;
73    (void) amask;
74
75    if (_mesa_is_user_fbo(ctx->DrawBuffer))
76       return;
77
78    xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
79
80    if (xclass == GLX_TRUE_COLOR || xclass == GLX_DIRECT_COLOR) {
81       unsigned long m;
82       if (rmask && gmask && bmask) {
83          m = ((unsigned long)~0L);
84       }
85       else {
86          m = 0;
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);
90       }
91       XMesaSetPlaneMask( xmesa->display, xmbuf->cleargc, m );
92    }
93 }
94
95
96
97 /**********************************************************************/
98 /*** glClear implementations                                        ***/
99 /**********************************************************************/
100
101
102 /**
103  * Clear the front or back color buffer, if it's implemented with a pixmap.
104  */
105 static void
106 clear_pixmap(struct gl_context *ctx, struct xmesa_renderbuffer *xrb,
107              GLint x, GLint y, GLint width, GLint height)
108 {
109    const XMesaContext xmesa = XMESA_CONTEXT(ctx);
110    XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
111
112    assert(xmbuf);
113    assert(xrb->pixmap);
114    assert(xmesa);
115    assert(xmesa->display);
116    assert(xrb->pixmap);
117    assert(xmbuf->cleargc);
118
119    XMesaFillRectangle( xmesa->display, xrb->pixmap, xmbuf->cleargc,
120                        x, xrb->Base.Base.Height - y - height,
121                        width, height );
122 }
123
124
125 static void
126 clear_16bit_ximage( struct gl_context *ctx, struct xmesa_renderbuffer *xrb,
127                     GLint x, GLint y, GLint width, GLint height)
128 {
129    const XMesaContext xmesa = XMESA_CONTEXT(ctx);
130    GLuint pixel = (GLuint) xmesa->clearpixel;
131    GLint i, j;
132
133    if (xmesa->swapbytes) {
134       pixel = ((pixel >> 8) & 0x00ff) | ((pixel << 8) & 0xff00);
135    }
136
137    for (j = 0; j < height; j++) {
138       GLushort *ptr2 = PIXEL_ADDR2(xrb, x, y + j);
139       for (i = 0; i < width; i++) {
140          ptr2[i] = pixel;
141       }
142    }
143 }
144
145
146 /* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
147 static void
148 clear_24bit_ximage(struct gl_context *ctx, struct xmesa_renderbuffer *xrb,
149                    GLint x, GLint y, GLint width, GLint height)
150 {
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];
155
156    if (r == g && g == b) {
157       /* same value for all three components (gray) */
158       GLint j;
159       for (j = 0; j < height; j++) {
160          bgr_t *ptr3 = PIXEL_ADDR3(xrb, x, y + j);
161          memset(ptr3, r, 3 * width);
162       }
163    }
164    else {
165       /* non-gray clear color */
166       GLint i, j;
167       for (j = 0; j < height; j++) {
168          bgr_t *ptr3 = PIXEL_ADDR3(xrb, x, y + j);
169          for (i = 0; i < width; i++) {
170             ptr3->r = r;
171             ptr3->g = g;
172             ptr3->b = b;
173             ptr3++;
174          }
175       }
176    }
177 }
178
179
180 static void
181 clear_32bit_ximage(struct gl_context *ctx, struct xmesa_renderbuffer *xrb,
182                    GLint x, GLint y, GLint width, GLint height)
183 {
184    const XMesaContext xmesa = XMESA_CONTEXT(ctx);
185    register GLuint pixel = (GLuint) xmesa->clearpixel;
186
187    if (!xrb->ximage)
188       return;
189
190    if (xmesa->swapbytes) {
191       pixel = ((pixel >> 24) & 0x000000ff)
192             | ((pixel >> 8)  & 0x0000ff00)
193             | ((pixel << 8)  & 0x00ff0000)
194             | ((pixel << 24) & 0xff000000);
195    }
196
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;
201       if (pixel == 0) {
202          /* common case */
203          memset(ptr4, pixel, 4 * n);
204       }
205       else {
206          GLuint i;
207          for (i = 0; i < n; i++)
208             ptr4[i] = pixel;
209       }
210    }
211    else {
212       /* clearing scissored region */
213       GLint i, j;
214       for (j = 0; j < height; j++) {
215          GLuint *ptr4 = PIXEL_ADDR4(xrb, x, y + j);
216          for (i = 0; i < width; i++) {
217             ptr4[i] = pixel;
218          }
219       }
220    }
221 }
222
223
224 static void
225 clear_nbit_ximage(struct gl_context *ctx, struct xmesa_renderbuffer *xrb,
226                   GLint x, GLint y, GLint width, GLint height)
227 {
228    const XMesaContext xmesa = XMESA_CONTEXT(ctx);
229    XMesaImage *img = xrb->ximage;
230    GLint i, j;
231
232    /* TODO: optimize this */
233    y = YFLIP(xrb, y);
234    for (j = 0; j < height; j++) {
235       for (i = 0; i < width; i++) {
236          XMesaPutPixel(img, x+i, y-j, xmesa->clearpixel);
237       }
238    }
239 }
240
241
242
243 static void
244 clear_buffers(struct gl_context *ctx, GLbitfield buffers)
245 {
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;
255
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);
265
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;
276             }
277             else {
278                /* we can't directly clear an alpha-wrapped color buffer */
279             }
280          }
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;
289             }
290          }
291       }
292    }
293    if (buffers)
294       _swrast_Clear(ctx, buffers);
295 }
296
297
298 /* XXX these functions haven't been tested in the Xserver environment */
299
300
301 /**
302  * Check if we can do an optimized glDrawPixels into an 8R8G8B visual.
303  */
304 static GLboolean
305 can_do_DrawPixels_8R8G8B(struct gl_context *ctx, GLenum format, GLenum type)
306 {
307    if (format == GL_BGRA &&
308        type == GL_UNSIGNED_BYTE &&
309        ctx->DrawBuffer &&
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);
315
316       if (swrast->NewState)
317          _swrast_validate_derived( ctx );
318       
319       if ((swrast->_RasterMask & ~CLIP_BIT) == 0) /* no blend, z-test, etc */ {
320          struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0];
321          if (rb) {
322             struct xmesa_renderbuffer *xrb = xmesa_renderbuffer(rb);
323             if (xrb &&
324                 xrb->pixmap && /* drawing to pixmap or window */
325                 _mesa_get_format_bits(xrb->Base.Base.Format, GL_ALPHA_BITS) == 0) {
326                return GL_TRUE;
327             }
328          }
329       }
330    }
331    return GL_FALSE;
332 }
333
334
335 /**
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.
339  */
340 static void
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 )
346 {
347    if (can_do_DrawPixels_8R8G8B(ctx, format, type)) {
348       const SWcontext *swrast = SWRAST_CONTEXT( ctx );
349       struct gl_pixelstore_attrib clippedUnpack = *unpack;
350       int dstX = x;
351       int dstY = y;
352       int w = width;
353       int h = height;
354
355       if (swrast->NewState)
356          _swrast_validate_derived( ctx );
357
358       if (_mesa_is_bufferobj(unpack->BufferObj)) {
359          /* unpack from PBO */
360          GLubyte *buf;
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)");
365             return;
366          }
367          buf = (GLubyte *) ctx->Driver.MapBufferRange(ctx, 0,
368                                                       unpack->BufferObj->Size,
369                                                       GL_MAP_READ_BIT,
370                                                       unpack->BufferObj);
371          if (!buf) {
372             /* buffer is already mapped - that's an error */
373             _mesa_error(ctx, GL_INVALID_OPERATION,
374                         "glDrawPixels(PBO is mapped)");
375             return;
376          }
377          pixels = ADD_POINTERS(buf, pixels);
378       }
379
380       if (_mesa_clip_drawpixels(ctx, &dstX, &dstY, &w, &h, &clippedUnpack)) {
381          const XMesaContext xmesa = XMESA_CONTEXT(ctx);
382          XMesaDisplay *dpy = xmesa->xm_visual->display;
383          XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
384          const XMesaGC gc = xmbuf->cleargc;  /* effected by glColorMask */
385          struct xmesa_renderbuffer *xrb
386             = xmesa_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
387          const int srcX = clippedUnpack.SkipPixels;
388          const int srcY = clippedUnpack.SkipRows;
389          const int rowLength = clippedUnpack.RowLength;
390          XMesaImage ximage;
391
392          ASSERT(xmesa->xm_visual->dithered_pf == PF_8R8G8B);
393          ASSERT(xmesa->xm_visual->undithered_pf == PF_8R8G8B);
394          ASSERT(dpy);
395          ASSERT(gc);
396
397          /* This is a little tricky since all coordinates up to now have
398           * been in the OpenGL bottom-to-top orientation.  X is top-to-bottom
399           * so we have to carefully compute the Y coordinates/addresses here.
400           */
401          memset(&ximage, 0, sizeof(XMesaImage));
402          ximage.width = width;
403          ximage.height = height;
404          ximage.format = ZPixmap;
405          ximage.data = (char *) pixels
406             + ((srcY + h - 1) * rowLength + srcX) * 4;
407          ximage.byte_order = LSBFirst;
408          ximage.bitmap_unit = 32;
409          ximage.bitmap_bit_order = LSBFirst;
410          ximage.bitmap_pad = 32;
411          ximage.depth = 32;
412          ximage.bits_per_pixel = 32;
413          ximage.bytes_per_line = -rowLength * 4; /* negative to flip image */
414          /* it seems we don't need to set the ximage.red/green/blue_mask fields */
415          /* flip Y axis for dest position */
416          dstY = YFLIP(xrb, dstY) - h + 1;
417          XPutImage(dpy, xrb->pixmap, gc, &ximage, 0, 0, dstX, dstY, w, h);
418       }
419
420       if (_mesa_is_bufferobj(unpack->BufferObj)) {
421          ctx->Driver.UnmapBuffer(ctx, unpack->BufferObj);
422       }
423    }
424    else {
425       /* software fallback */
426       _swrast_DrawPixels(ctx, x, y, width, height,
427                          format, type, unpack, pixels);
428    }
429 }
430
431
432
433 /**
434  * Check if we can do an optimized glDrawPixels into an 5R6G5B visual.
435  */
436 static GLboolean
437 can_do_DrawPixels_5R6G5B(struct gl_context *ctx, GLenum format, GLenum type)
438 {
439    if (format == GL_RGB &&
440        type == GL_UNSIGNED_SHORT_5_6_5 &&
441        !ctx->Color.DitherFlag &&  /* no dithering */
442        ctx->DrawBuffer &&
443        _mesa_is_winsys_fbo(ctx->DrawBuffer) &&
444        ctx->Pixel.ZoomX == 1.0 &&        /* no zooming */
445        ctx->Pixel.ZoomY == 1.0 &&
446        ctx->_ImageTransferState == 0 /* no color tables, scale/bias, etc */) {
447       const SWcontext *swrast = SWRAST_CONTEXT(ctx);
448
449       if (swrast->NewState)
450          _swrast_validate_derived( ctx );
451       
452       if ((swrast->_RasterMask & ~CLIP_BIT) == 0) /* no blend, z-test, etc */ {
453          struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0];
454          if (rb) {
455             struct xmesa_renderbuffer *xrb = xmesa_renderbuffer(rb);
456             if (xrb &&
457                 xrb->pixmap && /* drawing to pixmap or window */
458                 _mesa_get_format_bits(xrb->Base.Base.Format, GL_ALPHA_BITS) == 0) {
459                return GL_TRUE;
460             }
461          }
462       }
463    }
464    return GL_FALSE;
465 }
466
467
468 /**
469  * This function implements glDrawPixels() with an XPutImage call when
470  * drawing to the front buffer (X Window drawable).  The image format
471  * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
472  * match the PF_5R6G5B pixel format.
473  */
474 static void
475 xmesa_DrawPixels_5R6G5B( struct gl_context *ctx,
476                          GLint x, GLint y, GLsizei width, GLsizei height,
477                          GLenum format, GLenum type,
478                          const struct gl_pixelstore_attrib *unpack,
479                          const GLvoid *pixels )
480 {
481    if (can_do_DrawPixels_5R6G5B(ctx, format, type)) {
482       const SWcontext *swrast = SWRAST_CONTEXT( ctx );
483       struct gl_pixelstore_attrib clippedUnpack = *unpack;
484       int dstX = x;
485       int dstY = y;
486       int w = width;
487       int h = height;
488
489       if (swrast->NewState)
490          _swrast_validate_derived( ctx );
491       
492       if (_mesa_is_bufferobj(unpack->BufferObj)) {
493          /* unpack from PBO */
494          GLubyte *buf;
495          if (!_mesa_validate_pbo_access(2, unpack, width, height, 1,
496                                         format, type, INT_MAX, pixels)) {
497             _mesa_error(ctx, GL_INVALID_OPERATION,
498                         "glDrawPixels(invalid PBO access)");
499             return;
500          }
501          buf = (GLubyte *) ctx->Driver.MapBufferRange(ctx, 0,
502                                                       unpack->BufferObj->Size,
503                                                       GL_MAP_READ_BIT,
504                                                       unpack->BufferObj);
505          if (!buf) {
506             /* buffer is already mapped - that's an error */
507             _mesa_error(ctx, GL_INVALID_OPERATION,
508                         "glDrawPixels(PBO is mapped)");
509             return;
510          }
511          pixels = ADD_POINTERS(buf, pixels);
512       }
513
514       if (_mesa_clip_drawpixels(ctx, &dstX, &dstY, &w, &h, &clippedUnpack)) {
515          const XMesaContext xmesa = XMESA_CONTEXT(ctx);
516          XMesaDisplay *dpy = xmesa->xm_visual->display;
517          XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
518          const XMesaGC gc = xmbuf->cleargc;  /* effected by glColorMask */
519          struct xmesa_renderbuffer *xrb
520             = xmesa_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
521          const int srcX = clippedUnpack.SkipPixels;
522          const int srcY = clippedUnpack.SkipRows;
523          const int rowLength = clippedUnpack.RowLength;
524          XMesaImage ximage;
525
526          ASSERT(xmesa->xm_visual->undithered_pf == PF_5R6G5B);
527          ASSERT(dpy);
528          ASSERT(gc);
529
530          /* This is a little tricky since all coordinates up to now have
531           * been in the OpenGL bottom-to-top orientation.  X is top-to-bottom
532           * so we have to carefully compute the Y coordinates/addresses here.
533           */
534          memset(&ximage, 0, sizeof(XMesaImage));
535          ximage.width = width;
536          ximage.height = height;
537          ximage.format = ZPixmap;
538          ximage.data = (char *) pixels
539             + ((srcY + h - 1) * rowLength + srcX) * 2;
540          ximage.byte_order = LSBFirst;
541          ximage.bitmap_unit = 16;
542          ximage.bitmap_bit_order = LSBFirst;
543          ximage.bitmap_pad = 16;
544          ximage.depth = 16;
545          ximage.bits_per_pixel = 16;
546          ximage.bytes_per_line = -rowLength * 2; /* negative to flip image */
547          /* it seems we don't need to set the ximage.red/green/blue_mask fields */
548          /* flip Y axis for dest position */
549          dstY = YFLIP(xrb, dstY) - h + 1;
550          XPutImage(dpy, xrb->pixmap, gc, &ximage, 0, 0, dstX, dstY, w, h);
551       }
552
553       if (unpack->BufferObj->Name) {
554          ctx->Driver.UnmapBuffer(ctx, unpack->BufferObj);
555       }
556    }
557    else {
558       /* software fallback */
559       _swrast_DrawPixels(ctx, x, y, width, height,
560                          format, type, unpack, pixels);
561    }
562 }
563
564
565 /**
566  * Determine if we can do an optimized glCopyPixels.
567  */
568 static GLboolean
569 can_do_CopyPixels(struct gl_context *ctx, GLenum type)
570 {
571    if (type == GL_COLOR &&
572        ctx->_ImageTransferState == 0 &&  /* no color tables, scale/bias, etc */
573        ctx->Pixel.ZoomX == 1.0 &&        /* no zooming */
574        ctx->Pixel.ZoomY == 1.0 &&
575        ctx->Color.DrawBuffer[0] == GL_FRONT &&  /* copy to front buf */
576        ctx->Pixel.ReadBuffer == GL_FRONT &&    /* copy from front buf */
577        ctx->ReadBuffer->_ColorReadBuffer &&
578        ctx->DrawBuffer->_ColorDrawBuffers[0]) {
579       const SWcontext *swrast = SWRAST_CONTEXT( ctx );
580
581       if (swrast->NewState)
582          _swrast_validate_derived( ctx );
583
584       if ((swrast->_RasterMask & ~CLIP_BIT) == 0x0 &&
585           ctx->ReadBuffer &&
586           ctx->ReadBuffer->_ColorReadBuffer &&
587           ctx->DrawBuffer &&
588           ctx->DrawBuffer->_ColorDrawBuffers[0]) {
589          struct xmesa_renderbuffer *srcXrb
590             = xmesa_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
591          struct xmesa_renderbuffer *dstXrb
592             = xmesa_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
593          if (srcXrb->pixmap && dstXrb->pixmap) {
594             return GL_TRUE;
595          }
596       }
597    }
598    return GL_FALSE;
599 }
600
601
602 /**
603  * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
604  * for the color buffer.  Don't support zooming, pixel transfer, etc.
605  * We do support copying from one window to another, ala glXMakeCurrentRead.
606  */
607 static void
608 xmesa_CopyPixels( struct gl_context *ctx,
609                   GLint srcx, GLint srcy, GLsizei width, GLsizei height,
610                   GLint destx, GLint desty, GLenum type )
611 {
612    if (can_do_CopyPixels(ctx, type)) {
613       const XMesaContext xmesa = XMESA_CONTEXT(ctx);
614       XMesaDisplay *dpy = xmesa->xm_visual->display;
615       XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
616       const XMesaGC gc = xmbuf->cleargc;  /* effected by glColorMask */
617       struct xmesa_renderbuffer *srcXrb
618          = xmesa_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
619       struct xmesa_renderbuffer *dstXrb
620          = xmesa_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
621
622       ASSERT(dpy);
623       ASSERT(gc);
624
625       /* Note: we don't do any special clipping work here.  We could,
626        * but X will do it for us.
627        */
628       srcy = YFLIP(srcXrb, srcy) - height + 1;
629       desty = YFLIP(dstXrb, desty) - height + 1;
630       XCopyArea(dpy, srcXrb->pixmap, dstXrb->pixmap, gc,
631                 srcx, srcy, width, height, destx, desty);
632    }
633    else {
634       _swrast_CopyPixels(ctx, srcx, srcy, width, height, destx, desty, type );
635    }
636 }
637
638
639
640
641 /*
642  * Every driver should implement a GetString function in order to
643  * return a meaningful GL_RENDERER string.
644  */
645 static const GLubyte *
646 get_string( struct gl_context *ctx, GLenum name )
647 {
648    (void) ctx;
649    switch (name) {
650       case GL_RENDERER:
651          return (const GLubyte *) "Mesa X11";
652       case GL_VENDOR:
653          return NULL;
654       default:
655          return NULL;
656    }
657 }
658
659
660 /*
661  * We implement the glEnable function only because we care about
662  * dither enable/disable.
663  */
664 static void
665 enable( struct gl_context *ctx, GLenum pname, GLboolean state )
666 {
667    const XMesaContext xmesa = XMESA_CONTEXT(ctx);
668
669    switch (pname) {
670       case GL_DITHER:
671          if (state)
672             xmesa->pixelformat = xmesa->xm_visual->dithered_pf;
673          else
674             xmesa->pixelformat = xmesa->xm_visual->undithered_pf;
675          break;
676       default:
677          ;  /* silence compiler warning */
678    }
679 }
680
681
682 /**
683  * Called when the driver should update its state, based on the new_state
684  * flags.
685  */
686 void
687 xmesa_update_state( struct gl_context *ctx, GLbitfield new_state )
688 {
689    const XMesaContext xmesa = XMESA_CONTEXT(ctx);
690
691    /* Propagate statechange information to swrast and swrast_setup
692     * modules.  The X11 driver has no internal GL-dependent state.
693     */
694    _swrast_InvalidateState( ctx, new_state );
695    _tnl_InvalidateState( ctx, new_state );
696    _vbo_InvalidateState( ctx, new_state );
697    _swsetup_InvalidateState( ctx, new_state );
698
699    if (_mesa_is_user_fbo(ctx->DrawBuffer))
700       return;
701
702    /*
703     * GL_DITHER, GL_READ/DRAW_BUFFER, buffer binding state, etc. effect
704     * renderbuffer span/clear funcs.
705     * Check _NEW_COLOR to detect dither enable/disable.
706     */
707    if (new_state & (_NEW_COLOR | _NEW_BUFFERS)) {
708       XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
709       struct xmesa_renderbuffer *front_xrb, *back_xrb;
710
711       front_xrb = xmbuf->frontxrb;
712       if (front_xrb) {
713          front_xrb->clearFunc = clear_pixmap;
714       }
715
716       back_xrb = xmbuf->backxrb;
717       if (back_xrb) {
718          if (xmbuf->backxrb->pixmap) {
719             back_xrb->clearFunc = clear_pixmap;
720          }
721          else {
722             switch (xmesa->xm_visual->BitsPerPixel) {
723             case 16:
724                back_xrb->clearFunc = clear_16bit_ximage;
725                break;
726             case 24:
727                back_xrb->clearFunc = clear_24bit_ximage;
728                break;
729             case 32:
730                back_xrb->clearFunc = clear_32bit_ximage;
731                break;
732             default:
733                back_xrb->clearFunc = clear_nbit_ximage;
734                break;
735             }
736          }
737       }
738    }
739 }
740
741
742 /**
743  * Called by glViewport.
744  * This is a good time for us to poll the current X window size and adjust
745  * our renderbuffers to match the current window size.
746  * Remember, we have no opportunity to respond to conventional
747  * X Resize/StructureNotify events since the X driver has no event loop.
748  * Thus, we poll.
749  * Note that this trick isn't fool-proof.  If the application never calls
750  * glViewport, our notion of the current window size may be incorrect.
751  * That problem led to the GLX_MESA_resize_buffers extension.
752  */
753 static void
754 xmesa_viewport(struct gl_context *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
755 {
756    XMesaContext xmctx = XMESA_CONTEXT(ctx);
757    XMesaBuffer xmdrawbuf = XMESA_BUFFER(ctx->WinSysDrawBuffer);
758    XMesaBuffer xmreadbuf = XMESA_BUFFER(ctx->WinSysReadBuffer);
759    xmesa_check_and_update_buffer_size(xmctx, xmdrawbuf);
760    xmesa_check_and_update_buffer_size(xmctx, xmreadbuf);
761    (void) x;
762    (void) y;
763    (void) w;
764    (void) h;
765 }
766
767
768 #if ENABLE_EXT_timer_query
769
770 /*
771  * The GL_EXT_timer_query extension is not enabled for the XServer
772  * indirect renderer.  Not sure about how/if wrapping of gettimeofday()
773  * is done, etc.
774  */
775
776 struct xmesa_query_object
777 {
778    struct gl_query_object Base;
779    struct timeval StartTime;
780 };
781
782
783 static struct gl_query_object *
784 xmesa_new_query_object(struct gl_context *ctx, GLuint id)
785 {
786    struct xmesa_query_object *q = CALLOC_STRUCT(xmesa_query_object);
787    if (q) {
788       q->Base.Id = id;
789       q->Base.Ready = GL_TRUE;
790    }
791    return &q->Base;
792 }
793
794
795 static void
796 xmesa_begin_query(struct gl_context *ctx, struct gl_query_object *q)
797 {
798    if (q->Target == GL_TIME_ELAPSED_EXT) {
799       struct xmesa_query_object *xq = (struct xmesa_query_object *) q;
800       (void) gettimeofday(&xq->StartTime, NULL);
801    }
802 }
803
804
805 /**
806  * Return the difference between the two given times in microseconds.
807  */
808 static GLuint64EXT
809 time_diff(const struct timeval *t0, const struct timeval *t1)
810 {
811    GLuint64EXT seconds0 = t0->tv_sec & 0xff;  /* 0 .. 255 seconds */
812    GLuint64EXT seconds1 = t1->tv_sec & 0xff;  /* 0 .. 255 seconds */
813    GLuint64EXT nanosec0 = (seconds0 * 1000000 + t0->tv_usec) * 1000;
814    GLuint64EXT nanosec1 = (seconds1 * 1000000 + t1->tv_usec) * 1000;
815    return nanosec1 - nanosec0;
816 }
817
818
819 static void
820 xmesa_end_query(struct gl_context *ctx, struct gl_query_object *q)
821 {
822    if (q->Target == GL_TIME_ELAPSED_EXT) {
823       struct xmesa_query_object *xq = (struct xmesa_query_object *) q;
824       struct timeval endTime;
825       (void) gettimeofday(&endTime, NULL);
826       /* result is in nanoseconds! */
827       q->Result = time_diff(&xq->StartTime, &endTime);
828    }
829    q->Ready = GL_TRUE;
830 }
831
832 #endif /* ENABLE_timer_query */
833
834
835 /**
836  * Initialize the device driver function table with the functions
837  * we implement in this driver.
838  */
839 void
840 xmesa_init_driver_functions( XMesaVisual xmvisual,
841                              struct dd_function_table *driver )
842 {
843    driver->GetString = get_string;
844    driver->UpdateState = xmesa_update_state;
845    driver->GetBufferSize = NULL; /* OBSOLETE */
846    driver->Flush = finish_or_flush;
847    driver->Finish = finish_or_flush;
848    driver->ColorMask = color_mask;
849    driver->Enable = enable;
850    driver->Viewport = xmesa_viewport;
851    if (TEST_META_FUNCS) {
852       driver->Clear = _mesa_meta_Clear;
853       driver->CopyPixels = _mesa_meta_CopyPixels;
854       driver->BlitFramebuffer = _mesa_meta_BlitFramebuffer;
855       driver->DrawPixels = _mesa_meta_DrawPixels;
856       driver->Bitmap = _mesa_meta_Bitmap;
857    }
858    else {
859       driver->Clear = clear_buffers;
860       driver->CopyPixels = xmesa_CopyPixels;
861       if (xmvisual->undithered_pf == PF_8R8G8B &&
862           xmvisual->dithered_pf == PF_8R8G8B &&
863           xmvisual->BitsPerPixel == 32) {
864          driver->DrawPixels = xmesa_DrawPixels_8R8G8B;
865       }
866       else if (xmvisual->undithered_pf == PF_5R6G5B) {
867          driver->DrawPixels = xmesa_DrawPixels_5R6G5B;
868       }
869    }
870
871    driver->MapRenderbuffer = xmesa_MapRenderbuffer;
872    driver->UnmapRenderbuffer = xmesa_UnmapRenderbuffer;
873
874    driver->GenerateMipmap = _mesa_generate_mipmap;
875
876 #if ENABLE_EXT_timer_query
877    driver->NewQueryObject = xmesa_new_query_object;
878    driver->BeginQuery = xmesa_begin_query;
879    driver->EndQuery = xmesa_end_query;
880 #endif
881 }
882
883
884 #define XMESA_NEW_POINT  (_NEW_POINT | \
885                           _NEW_RENDERMODE | \
886                           _SWRAST_NEW_RASTERMASK)
887
888 #define XMESA_NEW_LINE   (_NEW_LINE | \
889                           _NEW_TEXTURE | \
890                           _NEW_LIGHT | \
891                           _NEW_DEPTH | \
892                           _NEW_RENDERMODE | \
893                           _SWRAST_NEW_RASTERMASK)
894
895 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
896                             _NEW_TEXTURE | \
897                             _NEW_LIGHT | \
898                             _NEW_DEPTH | \
899                             _NEW_RENDERMODE | \
900                             _SWRAST_NEW_RASTERMASK)
901
902
903 /**
904  * Extend the software rasterizer with our line/point/triangle
905  * functions.
906  * Called during context creation only.
907  */
908 void xmesa_register_swrast_functions( struct gl_context *ctx )
909 {
910    SWcontext *swrast = SWRAST_CONTEXT( ctx );
911
912    swrast->choose_point = xmesa_choose_point;
913    swrast->choose_line = xmesa_choose_line;
914    swrast->choose_triangle = xmesa_choose_triangle;
915
916    /* XXX these lines have no net effect.  Remove??? */
917    swrast->InvalidatePointMask |= XMESA_NEW_POINT;
918    swrast->InvalidateLineMask |= XMESA_NEW_LINE;
919    swrast->InvalidateTriangleMask |= XMESA_NEW_TRIANGLE;
920 }