xm_dd.c revision 093dc9e548537e6c77e33064a584f849ad90dfa5
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 * 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.
23 */
24
25
26/**
27 * \file xm_dd.h
28 * General device driver functions for Xlib driver.
29 */
30
31#include "glxheader.h"
32#include "main/bufferobj.h"
33#include "main/buffers.h"
34#include "main/context.h"
35#include "main/colormac.h"
36#include "main/depth.h"
37#include "main/drawpix.h"
38#include "main/extensions.h"
39#include "main/framebuffer.h"
40#include "main/macros.h"
41#include "main/image.h"
42#include "main/imports.h"
43#include "main/mtypes.h"
44#include "main/pbo.h"
45#include "main/state.h"
46#include "main/texobj.h"
47#include "main/teximage.h"
48#include "main/texstore.h"
49#include "main/texformat.h"
50#include "swrast/swrast.h"
51#include "swrast/s_context.h"
52#include "swrast_setup/swrast_setup.h"
53#include "tnl/tnl.h"
54#include "tnl/t_context.h"
55#include "drivers/common/meta.h"
56#include "xmesaP.h"
57
58
59
60/*
61 * Dithering kernels and lookup tables.
62 */
63
64const int xmesa_kernel8[DITH_DY * DITH_DX] = {
65    0 * MAXC,  8 * MAXC,  2 * MAXC, 10 * MAXC,
66   12 * MAXC,  4 * MAXC, 14 * MAXC,  6 * MAXC,
67    3 * MAXC, 11 * MAXC,  1 * MAXC,  9 * MAXC,
68   15 * MAXC,  7 * MAXC, 13 * MAXC,  5 * MAXC,
69};
70
71const short xmesa_HPCR_DRGB[3][2][16] = {
72   {
73      { 16, -4,  1,-11, 14, -6,  3, -9, 15, -5,  2,-10, 13, -7,  4, -8},
74      {-15,  5,  0, 12,-13,  7, -2, 10,-14,  6, -1, 11,-12,  8, -3,  9}
75   },
76   {
77      {-11, 15, -7,  3, -8, 14, -4,  2,-10, 16, -6,  4, -9, 13, -5,  1},
78      { 12,-14,  8, -2,  9,-13,  5, -1, 11,-15,  7, -3, 10,-12,  6,  0}
79   },
80   {
81      {  6,-18, 26,-14,  2,-22, 30,-10,  8,-16, 28,-12,  4,-20, 32, -8},
82      { -4, 20,-24, 16,  0, 24,-28, 12, -6, 18,-26, 14, -2, 22,-30, 10}
83   }
84};
85
86const int xmesa_kernel1[16] = {
87   0*47,  9*47,  4*47, 12*47,     /* 47 = (255*3)/16 */
88   6*47,  2*47, 14*47,  8*47,
89  10*47,  1*47,  5*47, 11*47,
90   7*47, 13*47,  3*47, 15*47
91};
92
93
94static void
95finish_or_flush( struct gl_context *ctx )
96{
97   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
98   if (xmesa) {
99      _glthread_LOCK_MUTEX(_xmesa_lock);
100      XSync( xmesa->display, False );
101      _glthread_UNLOCK_MUTEX(_xmesa_lock);
102   }
103}
104
105
106static void
107clear_color( struct gl_context *ctx,
108             const union gl_color_union color )
109{
110   if (ctx->DrawBuffer->Name == 0) {
111      const XMesaContext xmesa = XMESA_CONTEXT(ctx);
112      XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
113
114      UNCLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[0], color.f[0]);
115      UNCLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[1], color.f[1]);
116      UNCLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[2], color.f[2]);
117      UNCLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[3], color.f[3]);
118      xmesa->clearpixel = xmesa_color_to_pixel( ctx,
119                                                xmesa->clearcolor[0],
120                                                xmesa->clearcolor[1],
121                                                xmesa->clearcolor[2],
122                                                xmesa->clearcolor[3],
123                                                xmesa->xm_visual->undithered_pf );
124      _glthread_LOCK_MUTEX(_xmesa_lock);
125      XMesaSetForeground( xmesa->display, xmbuf->cleargc,
126                          xmesa->clearpixel );
127      _glthread_UNLOCK_MUTEX(_xmesa_lock);
128   }
129}
130
131
132
133/* Implements glColorMask() */
134static void
135color_mask(struct gl_context *ctx,
136           GLboolean rmask, GLboolean gmask, GLboolean bmask, GLboolean amask)
137{
138   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
139   XMesaBuffer xmbuf;
140   const int xclass = xmesa->xm_visual->visualType;
141   (void) amask;
142
143   if (ctx->DrawBuffer->Name != 0)
144      return;
145
146   xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
147
148   if (xclass == GLX_TRUE_COLOR || xclass == GLX_DIRECT_COLOR) {
149      unsigned long m;
150      if (rmask && gmask && bmask) {
151         m = ((unsigned long)~0L);
152      }
153      else {
154         m = 0;
155         if (rmask)   m |= GET_REDMASK(xmesa->xm_visual);
156         if (gmask)   m |= GET_GREENMASK(xmesa->xm_visual);
157         if (bmask)   m |= GET_BLUEMASK(xmesa->xm_visual);
158      }
159      XMesaSetPlaneMask( xmesa->display, xmbuf->cleargc, m );
160   }
161}
162
163
164
165/**********************************************************************/
166/*** glClear implementations                                        ***/
167/**********************************************************************/
168
169
170/**
171 * Clear the front or back color buffer, if it's implemented with a pixmap.
172 */
173static void
174clear_pixmap(struct gl_context *ctx, struct xmesa_renderbuffer *xrb,
175             GLint x, GLint y, GLint width, GLint height)
176{
177   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
178   XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
179
180   assert(xmbuf);
181   assert(xrb->pixmap);
182   assert(xmesa);
183   assert(xmesa->display);
184   assert(xrb->pixmap);
185   assert(xmbuf->cleargc);
186
187   XMesaFillRectangle( xmesa->display, xrb->pixmap, xmbuf->cleargc,
188                       x, xrb->Base.Height - y - height,
189                       width, height );
190}
191
192
193static void
194clear_8bit_ximage( struct gl_context *ctx, struct xmesa_renderbuffer *xrb,
195                   GLint x, GLint y, GLint width, GLint height )
196{
197   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
198   GLint i;
199   for (i = 0; i < height; i++) {
200      GLubyte *ptr = PIXEL_ADDR1(xrb, x, y + i);
201      memset( ptr, xmesa->clearpixel, width );
202   }
203}
204
205
206static void
207clear_HPCR_ximage( struct gl_context *ctx, struct xmesa_renderbuffer *xrb,
208                   GLint x, GLint y, GLint width, GLint height )
209{
210   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
211   GLint i;
212   for (i = y; i < y + height; i++) {
213      GLubyte *ptr = PIXEL_ADDR1( xrb, x, i );
214      int j;
215      const GLubyte *sptr = xmesa->xm_visual->hpcr_clear_ximage_pattern[0];
216      if (i & 1) {
217         sptr += 16;
218      }
219      for (j = x; j < x + width; j++) {
220         *ptr = sptr[j&15];
221         ptr++;
222      }
223   }
224}
225
226
227static void
228clear_16bit_ximage( struct gl_context *ctx, struct xmesa_renderbuffer *xrb,
229                    GLint x, GLint y, GLint width, GLint height)
230{
231   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
232   GLuint pixel = (GLuint) xmesa->clearpixel;
233   GLint i, j;
234
235   if (xmesa->swapbytes) {
236      pixel = ((pixel >> 8) & 0x00ff) | ((pixel << 8) & 0xff00);
237   }
238
239   for (j = 0; j < height; j++) {
240      GLushort *ptr2 = PIXEL_ADDR2(xrb, x, y + j);
241      for (i = 0; i < width; i++) {
242         ptr2[i] = pixel;
243      }
244   }
245}
246
247
248/* Optimized code provided by Nozomi Ytow <noz@xfree86.org> */
249static void
250clear_24bit_ximage(struct gl_context *ctx, struct xmesa_renderbuffer *xrb,
251                   GLint x, GLint y, GLint width, GLint height)
252{
253   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
254   const GLubyte r = xmesa->clearcolor[0];
255   const GLubyte g = xmesa->clearcolor[1];
256   const GLubyte b = xmesa->clearcolor[2];
257
258   if (r == g && g == b) {
259      /* same value for all three components (gray) */
260      GLint j;
261      for (j = 0; j < height; j++) {
262         bgr_t *ptr3 = PIXEL_ADDR3(xrb, x, y + j);
263         memset(ptr3, r, 3 * width);
264      }
265   }
266   else {
267      /* non-gray clear color */
268      GLint i, j;
269      for (j = 0; j < height; j++) {
270         bgr_t *ptr3 = PIXEL_ADDR3(xrb, x, y + j);
271         for (i = 0; i < width; i++) {
272            ptr3->r = r;
273            ptr3->g = g;
274            ptr3->b = b;
275            ptr3++;
276         }
277      }
278   }
279}
280
281
282static void
283clear_32bit_ximage(struct gl_context *ctx, struct xmesa_renderbuffer *xrb,
284                   GLint x, GLint y, GLint width, GLint height)
285{
286   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
287   register GLuint pixel = (GLuint) xmesa->clearpixel;
288
289   if (!xrb->ximage)
290      return;
291
292   if (xmesa->swapbytes) {
293      pixel = ((pixel >> 24) & 0x000000ff)
294            | ((pixel >> 8)  & 0x0000ff00)
295            | ((pixel << 8)  & 0x00ff0000)
296            | ((pixel << 24) & 0xff000000);
297   }
298
299   if (width == xrb->Base.Width && height == xrb->Base.Height) {
300      /* clearing whole buffer */
301      const GLuint n = xrb->Base.Width * xrb->Base.Height;
302      GLuint *ptr4 = (GLuint *) xrb->ximage->data;
303      if (pixel == 0) {
304         /* common case */
305         memset(ptr4, pixel, 4 * n);
306      }
307      else {
308         GLuint i;
309         for (i = 0; i < n; i++)
310            ptr4[i] = pixel;
311      }
312   }
313   else {
314      /* clearing scissored region */
315      GLint i, j;
316      for (j = 0; j < height; j++) {
317         GLuint *ptr4 = PIXEL_ADDR4(xrb, x, y + j);
318         for (i = 0; i < width; i++) {
319            ptr4[i] = pixel;
320         }
321      }
322   }
323}
324
325
326static void
327clear_nbit_ximage(struct gl_context *ctx, struct xmesa_renderbuffer *xrb,
328                  GLint x, GLint y, GLint width, GLint height)
329{
330   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
331   XMesaImage *img = xrb->ximage;
332   GLint i, j;
333
334   /* TODO: optimize this */
335   y = YFLIP(xrb, y);
336   for (j = 0; j < height; j++) {
337      for (i = 0; i < width; i++) {
338         XMesaPutPixel(img, x+i, y-j, xmesa->clearpixel);
339      }
340   }
341}
342
343
344
345static void
346clear_buffers(struct gl_context *ctx, GLbitfield buffers)
347{
348   if (ctx->DrawBuffer->Name == 0) {
349      /* this is a window system framebuffer */
350      const GLuint *colorMask = (GLuint *) &ctx->Color.ColorMask[0];
351      XMesaBuffer b = XMESA_BUFFER(ctx->DrawBuffer);
352      const GLint x = ctx->DrawBuffer->_Xmin;
353      const GLint y = ctx->DrawBuffer->_Ymin;
354      const GLint width = ctx->DrawBuffer->_Xmax - x;
355      const GLint height = ctx->DrawBuffer->_Ymax - y;
356
357      /* we can't handle color or index masking */
358      if (*colorMask == 0xffffffff && ctx->Color.IndexMask == 0xffffffff) {
359         if (buffers & BUFFER_BIT_FRONT_LEFT) {
360            /* clear front color buffer */
361            struct gl_renderbuffer *frontRb
362               = ctx->DrawBuffer->Attachment[BUFFER_FRONT_LEFT].Renderbuffer;
363            if (b->frontxrb == xmesa_renderbuffer(frontRb)) {
364               /* renderbuffer is not wrapped - great! */
365               b->frontxrb->clearFunc(ctx, b->frontxrb, x, y, width, height);
366               buffers &= ~BUFFER_BIT_FRONT_LEFT;
367            }
368            else {
369               /* we can't directly clear an alpha-wrapped color buffer */
370            }
371         }
372         if (buffers & BUFFER_BIT_BACK_LEFT) {
373            /* clear back color buffer */
374            struct gl_renderbuffer *backRb
375               = ctx->DrawBuffer->Attachment[BUFFER_BACK_LEFT].Renderbuffer;
376            if (b->backxrb == xmesa_renderbuffer(backRb)) {
377               /* renderbuffer is not wrapped - great! */
378               b->backxrb->clearFunc(ctx, b->backxrb, x, y, width, height);
379               buffers &= ~BUFFER_BIT_BACK_LEFT;
380            }
381         }
382      }
383   }
384   if (buffers)
385      _swrast_Clear(ctx, buffers);
386}
387
388
389/* XXX these functions haven't been tested in the Xserver environment */
390
391
392/**
393 * Check if we can do an optimized glDrawPixels into an 8R8G8B visual.
394 */
395static GLboolean
396can_do_DrawPixels_8R8G8B(struct gl_context *ctx, GLenum format, GLenum type)
397{
398   if (format == GL_BGRA &&
399       type == GL_UNSIGNED_BYTE &&
400       ctx->DrawBuffer &&
401       ctx->DrawBuffer->Name == 0 &&
402       ctx->Pixel.ZoomX == 1.0 &&        /* no zooming */
403       ctx->Pixel.ZoomY == 1.0 &&
404       ctx->_ImageTransferState == 0 /* no color tables, scale/bias, etc */) {
405      const SWcontext *swrast = SWRAST_CONTEXT(ctx);
406
407      if (swrast->NewState)
408         _swrast_validate_derived( ctx );
409
410      if ((swrast->_RasterMask & ~CLIP_BIT) == 0) /* no blend, z-test, etc */ {
411         struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0];
412         if (rb) {
413            struct xmesa_renderbuffer *xrb = xmesa_renderbuffer(rb->Wrapped);
414            if (xrb &&
415                xrb->pixmap && /* drawing to pixmap or window */
416                _mesa_get_format_bits(xrb->Base.Format, GL_ALPHA_BITS) == 0) {
417               return GL_TRUE;
418            }
419         }
420      }
421   }
422   return GL_FALSE;
423}
424
425
426/**
427 * This function implements glDrawPixels() with an XPutImage call when
428 * drawing to the front buffer (X Window drawable).
429 * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
430 */
431static void
432xmesa_DrawPixels_8R8G8B( struct gl_context *ctx,
433                         GLint x, GLint y, GLsizei width, GLsizei height,
434                         GLenum format, GLenum type,
435                         const struct gl_pixelstore_attrib *unpack,
436                         const GLvoid *pixels )
437{
438   if (can_do_DrawPixels_8R8G8B(ctx, format, type)) {
439      const SWcontext *swrast = SWRAST_CONTEXT( ctx );
440      struct gl_pixelstore_attrib clippedUnpack = *unpack;
441      int dstX = x;
442      int dstY = y;
443      int w = width;
444      int h = height;
445
446      if (swrast->NewState)
447         _swrast_validate_derived( ctx );
448
449      if (_mesa_is_bufferobj(unpack->BufferObj)) {
450         /* unpack from PBO */
451         GLubyte *buf;
452         if (!_mesa_validate_pbo_access(2, unpack, width, height, 1,
453                                        format, type, INT_MAX, pixels)) {
454            _mesa_error(ctx, GL_INVALID_OPERATION,
455                        "glDrawPixels(invalid PBO access)");
456            return;
457         }
458         buf = (GLubyte *) ctx->Driver.MapBufferRange(ctx, 0,
459						      unpack->BufferObj->Size,
460						      GL_MAP_READ_BIT,
461						      unpack->BufferObj);
462         if (!buf) {
463            /* buffer is already mapped - that's an error */
464            _mesa_error(ctx, GL_INVALID_OPERATION,
465                        "glDrawPixels(PBO is mapped)");
466            return;
467         }
468         pixels = ADD_POINTERS(buf, pixels);
469      }
470
471      if (_mesa_clip_drawpixels(ctx, &dstX, &dstY, &w, &h, &clippedUnpack)) {
472         const XMesaContext xmesa = XMESA_CONTEXT(ctx);
473         XMesaDisplay *dpy = xmesa->xm_visual->display;
474         XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
475         const XMesaGC gc = xmbuf->cleargc;  /* effected by glColorMask */
476         struct xmesa_renderbuffer *xrb
477            = xmesa_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]->Wrapped);
478         const int srcX = clippedUnpack.SkipPixels;
479         const int srcY = clippedUnpack.SkipRows;
480         const int rowLength = clippedUnpack.RowLength;
481         XMesaImage ximage;
482
483         ASSERT(xmesa->xm_visual->dithered_pf == PF_8R8G8B);
484         ASSERT(xmesa->xm_visual->undithered_pf == PF_8R8G8B);
485         ASSERT(dpy);
486         ASSERT(gc);
487
488         /* This is a little tricky since all coordinates up to now have
489          * been in the OpenGL bottom-to-top orientation.  X is top-to-bottom
490          * so we have to carefully compute the Y coordinates/addresses here.
491          */
492         memset(&ximage, 0, sizeof(XMesaImage));
493         ximage.width = width;
494         ximage.height = height;
495         ximage.format = ZPixmap;
496         ximage.data = (char *) pixels
497            + ((srcY + h - 1) * rowLength + srcX) * 4;
498         ximage.byte_order = LSBFirst;
499         ximage.bitmap_unit = 32;
500         ximage.bitmap_bit_order = LSBFirst;
501         ximage.bitmap_pad = 32;
502         ximage.depth = 32;
503         ximage.bits_per_pixel = 32;
504         ximage.bytes_per_line = -rowLength * 4; /* negative to flip image */
505         /* it seems we don't need to set the ximage.red/green/blue_mask fields */
506         /* flip Y axis for dest position */
507         dstY = YFLIP(xrb, dstY) - h + 1;
508         XPutImage(dpy, xrb->pixmap, gc, &ximage, 0, 0, dstX, dstY, w, h);
509      }
510
511      if (_mesa_is_bufferobj(unpack->BufferObj)) {
512         ctx->Driver.UnmapBuffer(ctx, unpack->BufferObj);
513      }
514   }
515   else {
516      /* software fallback */
517      _swrast_DrawPixels(ctx, x, y, width, height,
518                         format, type, unpack, pixels);
519   }
520}
521
522
523
524/**
525 * Check if we can do an optimized glDrawPixels into an 5R6G5B visual.
526 */
527static GLboolean
528can_do_DrawPixels_5R6G5B(struct gl_context *ctx, GLenum format, GLenum type)
529{
530   if (format == GL_RGB &&
531       type == GL_UNSIGNED_SHORT_5_6_5 &&
532       !ctx->Color.DitherFlag &&  /* no dithering */
533       ctx->DrawBuffer &&
534       ctx->DrawBuffer->Name == 0 &&
535       ctx->Pixel.ZoomX == 1.0 &&        /* no zooming */
536       ctx->Pixel.ZoomY == 1.0 &&
537       ctx->_ImageTransferState == 0 /* no color tables, scale/bias, etc */) {
538      const SWcontext *swrast = SWRAST_CONTEXT(ctx);
539
540      if (swrast->NewState)
541         _swrast_validate_derived( ctx );
542
543      if ((swrast->_RasterMask & ~CLIP_BIT) == 0) /* no blend, z-test, etc */ {
544         struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0];
545         if (rb) {
546            struct xmesa_renderbuffer *xrb = xmesa_renderbuffer(rb->Wrapped);
547            if (xrb &&
548                xrb->pixmap && /* drawing to pixmap or window */
549                _mesa_get_format_bits(xrb->Base.Format, GL_ALPHA_BITS) == 0) {
550               return GL_TRUE;
551            }
552         }
553      }
554   }
555   return GL_FALSE;
556}
557
558
559/**
560 * This function implements glDrawPixels() with an XPutImage call when
561 * drawing to the front buffer (X Window drawable).  The image format
562 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
563 * match the PF_5R6G5B pixel format.
564 */
565static void
566xmesa_DrawPixels_5R6G5B( struct gl_context *ctx,
567                         GLint x, GLint y, GLsizei width, GLsizei height,
568                         GLenum format, GLenum type,
569                         const struct gl_pixelstore_attrib *unpack,
570                         const GLvoid *pixels )
571{
572   if (can_do_DrawPixels_5R6G5B(ctx, format, type)) {
573      const SWcontext *swrast = SWRAST_CONTEXT( ctx );
574      struct gl_pixelstore_attrib clippedUnpack = *unpack;
575      int dstX = x;
576      int dstY = y;
577      int w = width;
578      int h = height;
579
580      if (swrast->NewState)
581         _swrast_validate_derived( ctx );
582
583      if (_mesa_is_bufferobj(unpack->BufferObj)) {
584         /* unpack from PBO */
585         GLubyte *buf;
586         if (!_mesa_validate_pbo_access(2, unpack, width, height, 1,
587                                        format, type, INT_MAX, pixels)) {
588            _mesa_error(ctx, GL_INVALID_OPERATION,
589                        "glDrawPixels(invalid PBO access)");
590            return;
591         }
592         buf = (GLubyte *) ctx->Driver.MapBufferRange(ctx, 0,
593						      unpack->BufferObj->Size,
594						      GL_MAP_READ_BIT,
595						      unpack->BufferObj);
596         if (!buf) {
597            /* buffer is already mapped - that's an error */
598            _mesa_error(ctx, GL_INVALID_OPERATION,
599                        "glDrawPixels(PBO is mapped)");
600            return;
601         }
602         pixels = ADD_POINTERS(buf, pixels);
603      }
604
605      if (_mesa_clip_drawpixels(ctx, &dstX, &dstY, &w, &h, &clippedUnpack)) {
606         const XMesaContext xmesa = XMESA_CONTEXT(ctx);
607         XMesaDisplay *dpy = xmesa->xm_visual->display;
608         XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
609         const XMesaGC gc = xmbuf->cleargc;  /* effected by glColorMask */
610         struct xmesa_renderbuffer *xrb
611            = xmesa_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]->Wrapped);
612         const int srcX = clippedUnpack.SkipPixels;
613         const int srcY = clippedUnpack.SkipRows;
614         const int rowLength = clippedUnpack.RowLength;
615         XMesaImage ximage;
616
617         ASSERT(xmesa->xm_visual->undithered_pf == PF_5R6G5B);
618         ASSERT(dpy);
619         ASSERT(gc);
620
621         /* This is a little tricky since all coordinates up to now have
622          * been in the OpenGL bottom-to-top orientation.  X is top-to-bottom
623          * so we have to carefully compute the Y coordinates/addresses here.
624          */
625         memset(&ximage, 0, sizeof(XMesaImage));
626         ximage.width = width;
627         ximage.height = height;
628         ximage.format = ZPixmap;
629         ximage.data = (char *) pixels
630            + ((srcY + h - 1) * rowLength + srcX) * 2;
631         ximage.byte_order = LSBFirst;
632         ximage.bitmap_unit = 16;
633         ximage.bitmap_bit_order = LSBFirst;
634         ximage.bitmap_pad = 16;
635         ximage.depth = 16;
636         ximage.bits_per_pixel = 16;
637         ximage.bytes_per_line = -rowLength * 2; /* negative to flip image */
638         /* it seems we don't need to set the ximage.red/green/blue_mask fields */
639         /* flip Y axis for dest position */
640         dstY = YFLIP(xrb, dstY) - h + 1;
641         XPutImage(dpy, xrb->pixmap, gc, &ximage, 0, 0, dstX, dstY, w, h);
642      }
643
644      if (unpack->BufferObj->Name) {
645         ctx->Driver.UnmapBuffer(ctx, unpack->BufferObj);
646      }
647   }
648   else {
649      /* software fallback */
650      _swrast_DrawPixels(ctx, x, y, width, height,
651                         format, type, unpack, pixels);
652   }
653}
654
655
656/**
657 * Determine if we can do an optimized glCopyPixels.
658 */
659static GLboolean
660can_do_CopyPixels(struct gl_context *ctx, GLenum type)
661{
662   if (type == GL_COLOR &&
663       ctx->_ImageTransferState == 0 &&  /* no color tables, scale/bias, etc */
664       ctx->Pixel.ZoomX == 1.0 &&        /* no zooming */
665       ctx->Pixel.ZoomY == 1.0 &&
666       ctx->Color.DrawBuffer[0] == GL_FRONT &&  /* copy to front buf */
667       ctx->Pixel.ReadBuffer == GL_FRONT &&    /* copy from front buf */
668       ctx->ReadBuffer->_ColorReadBuffer &&
669       ctx->DrawBuffer->_ColorDrawBuffers[0]) {
670      const SWcontext *swrast = SWRAST_CONTEXT( ctx );
671
672      if (swrast->NewState)
673         _swrast_validate_derived( ctx );
674
675      if ((swrast->_RasterMask & ~CLIP_BIT) == 0x0 &&
676          ctx->ReadBuffer &&
677          ctx->ReadBuffer->_ColorReadBuffer &&
678          ctx->DrawBuffer &&
679          ctx->DrawBuffer->_ColorDrawBuffers[0]) {
680         struct xmesa_renderbuffer *srcXrb
681            = xmesa_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer->Wrapped);
682         struct xmesa_renderbuffer *dstXrb
683            = xmesa_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]->Wrapped);
684         if (srcXrb->pixmap && dstXrb->pixmap) {
685            return GL_TRUE;
686         }
687      }
688   }
689   return GL_FALSE;
690}
691
692
693/**
694 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
695 * for the color buffer.  Don't support zooming, pixel transfer, etc.
696 * We do support copying from one window to another, ala glXMakeCurrentRead.
697 */
698static void
699xmesa_CopyPixels( struct gl_context *ctx,
700                  GLint srcx, GLint srcy, GLsizei width, GLsizei height,
701                  GLint destx, GLint desty, GLenum type )
702{
703   if (can_do_CopyPixels(ctx, type)) {
704      const XMesaContext xmesa = XMESA_CONTEXT(ctx);
705      XMesaDisplay *dpy = xmesa->xm_visual->display;
706      XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
707      const XMesaGC gc = xmbuf->cleargc;  /* effected by glColorMask */
708      struct xmesa_renderbuffer *srcXrb
709         = xmesa_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer->Wrapped);
710      struct xmesa_renderbuffer *dstXrb
711         = xmesa_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]->Wrapped);
712
713      ASSERT(dpy);
714      ASSERT(gc);
715
716      /* Note: we don't do any special clipping work here.  We could,
717       * but X will do it for us.
718       */
719      srcy = YFLIP(srcXrb, srcy) - height + 1;
720      desty = YFLIP(dstXrb, desty) - height + 1;
721      XCopyArea(dpy, srcXrb->pixmap, dstXrb->pixmap, gc,
722                srcx, srcy, width, height, destx, desty);
723   }
724   else {
725      _swrast_CopyPixels(ctx, srcx, srcy, width, height, destx, desty, type );
726   }
727}
728
729
730
731
732/*
733 * Every driver should implement a GetString function in order to
734 * return a meaningful GL_RENDERER string.
735 */
736static const GLubyte *
737get_string( struct gl_context *ctx, GLenum name )
738{
739   (void) ctx;
740   switch (name) {
741      case GL_RENDERER:
742         return (const GLubyte *) "Mesa X11";
743      case GL_VENDOR:
744         return NULL;
745      default:
746         return NULL;
747   }
748}
749
750
751/*
752 * We implement the glEnable function only because we care about
753 * dither enable/disable.
754 */
755static void
756enable( struct gl_context *ctx, GLenum pname, GLboolean state )
757{
758   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
759
760   switch (pname) {
761      case GL_DITHER:
762         if (state)
763            xmesa->pixelformat = xmesa->xm_visual->dithered_pf;
764         else
765            xmesa->pixelformat = xmesa->xm_visual->undithered_pf;
766         break;
767      default:
768         ;  /* silence compiler warning */
769   }
770}
771
772
773static void
774clear_color_HPCR_ximage( struct gl_context *ctx,
775                         const union gl_color_union color )
776{
777   int i;
778   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
779
780   UNCLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[0], color.f[0]);
781   UNCLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[1], color.f[1]);
782   UNCLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[2], color.f[2]);
783   UNCLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[3], color.f[3]);
784
785   if (color.f[0] == 0.0 && color.f[1] == 0.0 && color.f[2] == 0.0) {
786      /* black is black */
787      memset( xmesa->xm_visual->hpcr_clear_ximage_pattern, 0x0 ,
788              sizeof(xmesa->xm_visual->hpcr_clear_ximage_pattern));
789   }
790   else {
791      /* build clear pattern */
792      for (i=0; i<16; i++) {
793         xmesa->xm_visual->hpcr_clear_ximage_pattern[0][i] =
794            DITHER_HPCR(i, 0,
795                        xmesa->clearcolor[0],
796                        xmesa->clearcolor[1],
797                        xmesa->clearcolor[2]);
798         xmesa->xm_visual->hpcr_clear_ximage_pattern[1][i]    =
799            DITHER_HPCR(i, 1,
800                        xmesa->clearcolor[0],
801                        xmesa->clearcolor[1],
802                        xmesa->clearcolor[2]);
803      }
804   }
805}
806
807
808static void
809clear_color_HPCR_pixmap( struct gl_context *ctx,
810                         const union gl_color_union color )
811{
812   int i;
813   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
814
815   UNCLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[0], color.f[0]);
816   UNCLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[1], color.f[1]);
817   UNCLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[2], color.f[2]);
818   UNCLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[3], color.f[3]);
819
820   if (color.f[0] == 0.0 && color.f[1] == 0.0 && color.f[2] == 0.0) {
821      /* black is black */
822      for (i=0; i<16; i++) {
823         XMesaPutPixel(xmesa->xm_visual->hpcr_clear_ximage, i, 0, 0);
824         XMesaPutPixel(xmesa->xm_visual->hpcr_clear_ximage, i, 1, 0);
825      }
826   }
827   else {
828      for (i=0; i<16; i++) {
829         XMesaPutPixel(xmesa->xm_visual->hpcr_clear_ximage, i, 0,
830                       DITHER_HPCR(i, 0,
831                                   xmesa->clearcolor[0],
832                                   xmesa->clearcolor[1],
833                                   xmesa->clearcolor[2]));
834         XMesaPutPixel(xmesa->xm_visual->hpcr_clear_ximage, i, 1,
835                       DITHER_HPCR(i, 1,
836                                   xmesa->clearcolor[0],
837                                   xmesa->clearcolor[1],
838                                   xmesa->clearcolor[2]));
839      }
840   }
841   /* change tile pixmap content */
842   XMesaPutImage(xmesa->display,
843		 (XMesaDrawable)xmesa->xm_visual->hpcr_clear_pixmap,
844		 XMESA_BUFFER(ctx->DrawBuffer)->cleargc,
845		 xmesa->xm_visual->hpcr_clear_ximage, 0, 0, 0, 0, 16, 2);
846}
847
848
849/**
850 * Called when the driver should update its state, based on the new_state
851 * flags.
852 */
853void
854xmesa_update_state( struct gl_context *ctx, GLbitfield new_state )
855{
856   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
857
858   /* Propagate statechange information to swrast and swrast_setup
859    * modules.  The X11 driver has no internal GL-dependent state.
860    */
861   _swrast_InvalidateState( ctx, new_state );
862   _tnl_InvalidateState( ctx, new_state );
863   _vbo_InvalidateState( ctx, new_state );
864   _swsetup_InvalidateState( ctx, new_state );
865
866   if (ctx->DrawBuffer->Name != 0)
867      return;
868
869   /*
870    * GL_DITHER, GL_READ/DRAW_BUFFER, buffer binding state, etc. effect
871    * renderbuffer span/clear funcs.
872    * Check _NEW_COLOR to detect dither enable/disable.
873    */
874   if (new_state & (_NEW_COLOR | _NEW_BUFFERS)) {
875      XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
876      struct xmesa_renderbuffer *front_xrb, *back_xrb;
877
878      front_xrb = xmbuf->frontxrb;
879      if (front_xrb) {
880         xmesa_set_renderbuffer_funcs(front_xrb, xmesa->pixelformat,
881                                      xmesa->xm_visual->BitsPerPixel);
882         front_xrb->clearFunc = clear_pixmap;
883      }
884
885      back_xrb = xmbuf->backxrb;
886      if (back_xrb) {
887         xmesa_set_renderbuffer_funcs(back_xrb, xmesa->pixelformat,
888                                      xmesa->xm_visual->BitsPerPixel);
889         if (xmbuf->backxrb->pixmap) {
890            back_xrb->clearFunc = clear_pixmap;
891         }
892         else {
893            switch (xmesa->xm_visual->BitsPerPixel) {
894            case 8:
895               if (xmesa->xm_visual->hpcr_clear_flag) {
896                  back_xrb->clearFunc = clear_HPCR_ximage;
897               }
898               else {
899                  back_xrb->clearFunc = clear_8bit_ximage;
900               }
901               break;
902            case 16:
903               back_xrb->clearFunc = clear_16bit_ximage;
904               break;
905            case 24:
906               back_xrb->clearFunc = clear_24bit_ximage;
907               break;
908            case 32:
909               back_xrb->clearFunc = clear_32bit_ximage;
910               break;
911            default:
912               back_xrb->clearFunc = clear_nbit_ximage;
913               break;
914            }
915         }
916      }
917   }
918
919   if (xmesa->xm_visual->hpcr_clear_flag) {
920      /* this depends on whether we're drawing to the front or back buffer */
921      /* XXX FIX THIS! */
922#if 0
923      if (pixmap) {
924         ctx->Driver.ClearColor = clear_color_HPCR_pixmap;
925      }
926      else {
927         ctx->Driver.ClearColor = clear_color_HPCR_ximage;
928      }
929#else
930      (void) clear_color_HPCR_pixmap;
931      (void) clear_color_HPCR_ximage;
932#endif
933   }
934}
935
936
937
938/**
939 * In SW, we don't really compress GL_COMPRESSED_RGB[A] textures!
940 */
941static gl_format
942choose_tex_format( struct gl_context *ctx, GLint internalFormat,
943                   GLenum format, GLenum type )
944{
945   switch (internalFormat) {
946      case GL_COMPRESSED_RGB_ARB:
947         return MESA_FORMAT_RGB888;
948      case GL_COMPRESSED_RGBA_ARB:
949         return MESA_FORMAT_RGBA8888;
950      default:
951         return _mesa_choose_tex_format(ctx, internalFormat, format, type);
952   }
953}
954
955
956/**
957 * Called by glViewport.
958 * This is a good time for us to poll the current X window size and adjust
959 * our renderbuffers to match the current window size.
960 * Remember, we have no opportunity to respond to conventional
961 * X Resize/StructureNotify events since the X driver has no event loop.
962 * Thus, we poll.
963 * Note that this trick isn't fool-proof.  If the application never calls
964 * glViewport, our notion of the current window size may be incorrect.
965 * That problem led to the GLX_MESA_resize_buffers extension.
966 */
967static void
968xmesa_viewport(struct gl_context *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
969{
970   XMesaContext xmctx = XMESA_CONTEXT(ctx);
971   XMesaBuffer xmdrawbuf = XMESA_BUFFER(ctx->WinSysDrawBuffer);
972   XMesaBuffer xmreadbuf = XMESA_BUFFER(ctx->WinSysReadBuffer);
973   xmesa_check_and_update_buffer_size(xmctx, xmdrawbuf);
974   xmesa_check_and_update_buffer_size(xmctx, xmreadbuf);
975   (void) x;
976   (void) y;
977   (void) w;
978   (void) h;
979}
980
981
982#if ENABLE_EXT_timer_query
983
984/*
985 * The GL_EXT_timer_query extension is not enabled for the XServer
986 * indirect renderer.  Not sure about how/if wrapping of gettimeofday()
987 * is done, etc.
988 */
989
990struct xmesa_query_object
991{
992   struct gl_query_object Base;
993   struct timeval StartTime;
994};
995
996
997static struct gl_query_object *
998xmesa_new_query_object(struct gl_context *ctx, GLuint id)
999{
1000   struct xmesa_query_object *q = CALLOC_STRUCT(xmesa_query_object);
1001   if (q) {
1002      q->Base.Id = id;
1003      q->Base.Ready = GL_TRUE;
1004   }
1005   return &q->Base;
1006}
1007
1008
1009static void
1010xmesa_begin_query(struct gl_context *ctx, struct gl_query_object *q)
1011{
1012   if (q->Target == GL_TIME_ELAPSED_EXT) {
1013      struct xmesa_query_object *xq = (struct xmesa_query_object *) q;
1014      (void) gettimeofday(&xq->StartTime, NULL);
1015   }
1016}
1017
1018
1019/**
1020 * Return the difference between the two given times in microseconds.
1021 */
1022#ifdef __VMS
1023#define suseconds_t unsigned int
1024#endif
1025static GLuint64EXT
1026time_diff(const struct timeval *t0, const struct timeval *t1)
1027{
1028   GLuint64EXT seconds0 = t0->tv_sec & 0xff;  /* 0 .. 255 seconds */
1029   GLuint64EXT seconds1 = t1->tv_sec & 0xff;  /* 0 .. 255 seconds */
1030   GLuint64EXT nanosec0 = (seconds0 * 1000000 + t0->tv_usec) * 1000;
1031   GLuint64EXT nanosec1 = (seconds1 * 1000000 + t1->tv_usec) * 1000;
1032   return nanosec1 - nanosec0;
1033}
1034
1035
1036static void
1037xmesa_end_query(struct gl_context *ctx, struct gl_query_object *q)
1038{
1039   if (q->Target == GL_TIME_ELAPSED_EXT) {
1040      struct xmesa_query_object *xq = (struct xmesa_query_object *) q;
1041      struct timeval endTime;
1042      (void) gettimeofday(&endTime, NULL);
1043      /* result is in nanoseconds! */
1044      q->Result = time_diff(&xq->StartTime, &endTime);
1045   }
1046   q->Ready = GL_TRUE;
1047}
1048
1049#endif /* ENABLE_timer_query */
1050
1051
1052/**
1053 * Initialize the device driver function table with the functions
1054 * we implement in this driver.
1055 */
1056void
1057xmesa_init_driver_functions( XMesaVisual xmvisual,
1058                             struct dd_function_table *driver )
1059{
1060   driver->GetString = get_string;
1061   driver->UpdateState = xmesa_update_state;
1062   driver->GetBufferSize = NULL; /* OBSOLETE */
1063   driver->Flush = finish_or_flush;
1064   driver->Finish = finish_or_flush;
1065   driver->ClearColor = clear_color;
1066   driver->ColorMask = color_mask;
1067   driver->Enable = enable;
1068   driver->Viewport = xmesa_viewport;
1069   if (TEST_META_FUNCS) {
1070      driver->Clear = _mesa_meta_Clear;
1071      driver->CopyPixels = _mesa_meta_CopyPixels;
1072      driver->BlitFramebuffer = _mesa_meta_BlitFramebuffer;
1073      driver->DrawPixels = _mesa_meta_DrawPixels;
1074      driver->Bitmap = _mesa_meta_Bitmap;
1075   }
1076   else {
1077      driver->Clear = clear_buffers;
1078      driver->CopyPixels = xmesa_CopyPixels;
1079      if (xmvisual->undithered_pf == PF_8R8G8B &&
1080          xmvisual->dithered_pf == PF_8R8G8B &&
1081          xmvisual->BitsPerPixel == 32) {
1082         driver->DrawPixels = xmesa_DrawPixels_8R8G8B;
1083      }
1084      else if (xmvisual->undithered_pf == PF_5R6G5B) {
1085         driver->DrawPixels = xmesa_DrawPixels_5R6G5B;
1086      }
1087   }
1088
1089#if ENABLE_EXT_texure_compression_s3tc
1090   driver->ChooseTextureFormat = choose_tex_format;
1091#else
1092   (void) choose_tex_format;
1093#endif
1094
1095#if ENABLE_EXT_timer_query
1096   driver->NewQueryObject = xmesa_new_query_object;
1097   driver->BeginQuery = xmesa_begin_query;
1098   driver->EndQuery = xmesa_end_query;
1099#endif
1100}
1101
1102
1103#define XMESA_NEW_POINT  (_NEW_POINT | \
1104                          _NEW_RENDERMODE | \
1105                          _SWRAST_NEW_RASTERMASK)
1106
1107#define XMESA_NEW_LINE   (_NEW_LINE | \
1108                          _NEW_TEXTURE | \
1109                          _NEW_LIGHT | \
1110                          _NEW_DEPTH | \
1111                          _NEW_RENDERMODE | \
1112                          _SWRAST_NEW_RASTERMASK)
1113
1114#define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
1115                            _NEW_TEXTURE | \
1116                            _NEW_LIGHT | \
1117                            _NEW_DEPTH | \
1118                            _NEW_RENDERMODE | \
1119                            _SWRAST_NEW_RASTERMASK)
1120
1121
1122/**
1123 * Extend the software rasterizer with our line/point/triangle
1124 * functions.
1125 * Called during context creation only.
1126 */
1127void xmesa_register_swrast_functions( struct gl_context *ctx )
1128{
1129   SWcontext *swrast = SWRAST_CONTEXT( ctx );
1130
1131   swrast->choose_point = xmesa_choose_point;
1132   swrast->choose_line = xmesa_choose_line;
1133   swrast->choose_triangle = xmesa_choose_triangle;
1134
1135   /* XXX these lines have no net effect.  Remove??? */
1136   swrast->InvalidatePointMask |= XMESA_NEW_POINT;
1137   swrast->InvalidateLineMask |= XMESA_NEW_LINE;
1138   swrast->InvalidateTriangleMask |= XMESA_NEW_TRIANGLE;
1139}
1140