1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
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/context.h"
34 #include "main/colormac.h"
35 #include "main/fbobject.h"
36 #include "main/framebuffer.h"
37 #include "main/macros.h"
38 #include "main/mipmap.h"
39 #include "main/image.h"
40 #include "main/imports.h"
41 #include "main/mtypes.h"
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
finish_or_flush(struct gl_context * ctx)54 finish_or_flush( struct gl_context *ctx )
55 {
56 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
57 if (xmesa) {
58 mtx_lock(&_xmesa_lock);
59 XSync( xmesa->display, False );
60 mtx_unlock(&_xmesa_lock);
61 }
62 }
63
64
65 /* Implements glColorMask() */
66 static void
color_mask(struct gl_context * ctx,GLboolean rmask,GLboolean gmask,GLboolean bmask,GLboolean amask)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
clear_pixmap(struct gl_context * ctx,struct xmesa_renderbuffer * xrb,GLint x,GLint y,GLint width,GLint height)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
clear_16bit_ximage(struct gl_context * ctx,struct xmesa_renderbuffer * xrb,GLint x,GLint y,GLint width,GLint height)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
clear_24bit_ximage(struct gl_context * ctx,struct xmesa_renderbuffer * xrb,GLint x,GLint y,GLint width,GLint height)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
clear_32bit_ximage(struct gl_context * ctx,struct xmesa_renderbuffer * xrb,GLint x,GLint y,GLint width,GLint height)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
clear_nbit_ximage(struct gl_context * ctx,struct xmesa_renderbuffer * xrb,GLint x,GLint y,GLint width,GLint height)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
clear_buffers(struct gl_context * ctx,GLbitfield buffers)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
can_do_DrawPixels_8R8G8B(struct gl_context * ctx,GLenum format,GLenum type)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
xmesa_DrawPixels_8R8G8B(struct gl_context * ctx,GLint x,GLint y,GLsizei width,GLsizei height,GLenum format,GLenum type,const struct gl_pixelstore_attrib * unpack,const GLvoid * pixels)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 MAP_INTERNAL);
372 if (!buf) {
373 return; /* error */
374 }
375 pixels = ADD_POINTERS(buf, pixels);
376 }
377
378 if (_mesa_clip_drawpixels(ctx, &dstX, &dstY, &w, &h, &clippedUnpack)) {
379 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
380 XMesaDisplay *dpy = xmesa->xm_visual->display;
381 XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
382 const XMesaGC gc = xmbuf->cleargc; /* effected by glColorMask */
383 struct xmesa_renderbuffer *xrb
384 = xmesa_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
385 const int srcX = clippedUnpack.SkipPixels;
386 const int srcY = clippedUnpack.SkipRows;
387 const int rowLength = clippedUnpack.RowLength;
388 XMesaImage ximage;
389
390 assert(xmesa->xm_visual->dithered_pf == PF_8R8G8B);
391 assert(xmesa->xm_visual->undithered_pf == PF_8R8G8B);
392 assert(dpy);
393 assert(gc);
394
395 /* This is a little tricky since all coordinates up to now have
396 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
397 * so we have to carefully compute the Y coordinates/addresses here.
398 */
399 memset(&ximage, 0, sizeof(XMesaImage));
400 ximage.width = width;
401 ximage.height = height;
402 ximage.format = ZPixmap;
403 ximage.data = (char *) pixels
404 + ((srcY + h - 1) * rowLength + srcX) * 4;
405 ximage.byte_order = LSBFirst;
406 ximage.bitmap_unit = 32;
407 ximage.bitmap_bit_order = LSBFirst;
408 ximage.bitmap_pad = 32;
409 ximage.depth = 32;
410 ximage.bits_per_pixel = 32;
411 ximage.bytes_per_line = -rowLength * 4; /* negative to flip image */
412 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
413 /* flip Y axis for dest position */
414 dstY = YFLIP(xrb, dstY) - h + 1;
415 XPutImage(dpy, xrb->pixmap, gc, &ximage, 0, 0, dstX, dstY, w, h);
416 }
417
418 if (_mesa_is_bufferobj(unpack->BufferObj)) {
419 ctx->Driver.UnmapBuffer(ctx, unpack->BufferObj, MAP_INTERNAL);
420 }
421 }
422 else {
423 /* software fallback */
424 _swrast_DrawPixels(ctx, x, y, width, height,
425 format, type, unpack, pixels);
426 }
427 }
428
429
430
431 /**
432 * Check if we can do an optimized glDrawPixels into an 5R6G5B visual.
433 */
434 static GLboolean
can_do_DrawPixels_5R6G5B(struct gl_context * ctx,GLenum format,GLenum type)435 can_do_DrawPixels_5R6G5B(struct gl_context *ctx, GLenum format, GLenum type)
436 {
437 if (format == GL_RGB &&
438 type == GL_UNSIGNED_SHORT_5_6_5 &&
439 !ctx->Color.DitherFlag && /* no dithering */
440 ctx->DrawBuffer &&
441 _mesa_is_winsys_fbo(ctx->DrawBuffer) &&
442 ctx->Pixel.ZoomX == 1.0 && /* no zooming */
443 ctx->Pixel.ZoomY == 1.0 &&
444 ctx->_ImageTransferState == 0 /* no color tables, scale/bias, etc */) {
445 const SWcontext *swrast = SWRAST_CONTEXT(ctx);
446
447 if (swrast->NewState)
448 _swrast_validate_derived( ctx );
449
450 if ((swrast->_RasterMask & ~CLIP_BIT) == 0) /* no blend, z-test, etc */ {
451 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0];
452 if (rb) {
453 struct xmesa_renderbuffer *xrb = xmesa_renderbuffer(rb);
454 if (xrb &&
455 xrb->pixmap && /* drawing to pixmap or window */
456 _mesa_get_format_bits(xrb->Base.Base.Format, GL_ALPHA_BITS) == 0) {
457 return GL_TRUE;
458 }
459 }
460 }
461 }
462 return GL_FALSE;
463 }
464
465
466 /**
467 * This function implements glDrawPixels() with an XPutImage call when
468 * drawing to the front buffer (X Window drawable). The image format
469 * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
470 * match the PF_5R6G5B pixel format.
471 */
472 static void
xmesa_DrawPixels_5R6G5B(struct gl_context * ctx,GLint x,GLint y,GLsizei width,GLsizei height,GLenum format,GLenum type,const struct gl_pixelstore_attrib * unpack,const GLvoid * pixels)473 xmesa_DrawPixels_5R6G5B( struct gl_context *ctx,
474 GLint x, GLint y, GLsizei width, GLsizei height,
475 GLenum format, GLenum type,
476 const struct gl_pixelstore_attrib *unpack,
477 const GLvoid *pixels )
478 {
479 if (can_do_DrawPixels_5R6G5B(ctx, format, type)) {
480 const SWcontext *swrast = SWRAST_CONTEXT( ctx );
481 struct gl_pixelstore_attrib clippedUnpack = *unpack;
482 int dstX = x;
483 int dstY = y;
484 int w = width;
485 int h = height;
486
487 if (swrast->NewState)
488 _swrast_validate_derived( ctx );
489
490 if (_mesa_is_bufferobj(unpack->BufferObj)) {
491 /* unpack from PBO */
492 GLubyte *buf;
493 if (!_mesa_validate_pbo_access(2, unpack, width, height, 1,
494 format, type, INT_MAX, pixels)) {
495 _mesa_error(ctx, GL_INVALID_OPERATION,
496 "glDrawPixels(invalid PBO access)");
497 return;
498 }
499 buf = (GLubyte *) ctx->Driver.MapBufferRange(ctx, 0,
500 unpack->BufferObj->Size,
501 GL_MAP_READ_BIT,
502 unpack->BufferObj,
503 MAP_INTERNAL);
504 if (!buf) {
505 return; /* error */
506 }
507 pixels = ADD_POINTERS(buf, pixels);
508 }
509
510 if (_mesa_clip_drawpixels(ctx, &dstX, &dstY, &w, &h, &clippedUnpack)) {
511 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
512 XMesaDisplay *dpy = xmesa->xm_visual->display;
513 XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
514 const XMesaGC gc = xmbuf->cleargc; /* effected by glColorMask */
515 struct xmesa_renderbuffer *xrb
516 = xmesa_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
517 const int srcX = clippedUnpack.SkipPixels;
518 const int srcY = clippedUnpack.SkipRows;
519 const int rowLength = clippedUnpack.RowLength;
520 XMesaImage ximage;
521
522 assert(xmesa->xm_visual->undithered_pf == PF_5R6G5B);
523 assert(dpy);
524 assert(gc);
525
526 /* This is a little tricky since all coordinates up to now have
527 * been in the OpenGL bottom-to-top orientation. X is top-to-bottom
528 * so we have to carefully compute the Y coordinates/addresses here.
529 */
530 memset(&ximage, 0, sizeof(XMesaImage));
531 ximage.width = width;
532 ximage.height = height;
533 ximage.format = ZPixmap;
534 ximage.data = (char *) pixels
535 + ((srcY + h - 1) * rowLength + srcX) * 2;
536 ximage.byte_order = LSBFirst;
537 ximage.bitmap_unit = 16;
538 ximage.bitmap_bit_order = LSBFirst;
539 ximage.bitmap_pad = 16;
540 ximage.depth = 16;
541 ximage.bits_per_pixel = 16;
542 ximage.bytes_per_line = -rowLength * 2; /* negative to flip image */
543 /* it seems we don't need to set the ximage.red/green/blue_mask fields */
544 /* flip Y axis for dest position */
545 dstY = YFLIP(xrb, dstY) - h + 1;
546 XPutImage(dpy, xrb->pixmap, gc, &ximage, 0, 0, dstX, dstY, w, h);
547 }
548
549 if (unpack->BufferObj->Name) {
550 ctx->Driver.UnmapBuffer(ctx, unpack->BufferObj, MAP_INTERNAL);
551 }
552 }
553 else {
554 /* software fallback */
555 _swrast_DrawPixels(ctx, x, y, width, height,
556 format, type, unpack, pixels);
557 }
558 }
559
560
561 /**
562 * Determine if we can do an optimized glCopyPixels.
563 */
564 static GLboolean
can_do_CopyPixels(struct gl_context * ctx,GLenum type)565 can_do_CopyPixels(struct gl_context *ctx, GLenum type)
566 {
567 if (type == GL_COLOR &&
568 ctx->_ImageTransferState == 0 && /* no color tables, scale/bias, etc */
569 ctx->Pixel.ZoomX == 1.0 && /* no zooming */
570 ctx->Pixel.ZoomY == 1.0 &&
571 ctx->Color.DrawBuffer[0] == GL_FRONT && /* copy to front buf */
572 ctx->Pixel.ReadBuffer == GL_FRONT && /* copy from front buf */
573 ctx->ReadBuffer->_ColorReadBuffer &&
574 ctx->DrawBuffer->_ColorDrawBuffers[0]) {
575 const SWcontext *swrast = SWRAST_CONTEXT( ctx );
576
577 if (swrast->NewState)
578 _swrast_validate_derived( ctx );
579
580 if ((swrast->_RasterMask & ~CLIP_BIT) == 0x0 &&
581 ctx->ReadBuffer &&
582 ctx->ReadBuffer->_ColorReadBuffer &&
583 ctx->DrawBuffer &&
584 ctx->DrawBuffer->_ColorDrawBuffers[0]) {
585 struct xmesa_renderbuffer *srcXrb
586 = xmesa_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
587 struct xmesa_renderbuffer *dstXrb
588 = xmesa_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
589 if (srcXrb->pixmap && dstXrb->pixmap) {
590 return GL_TRUE;
591 }
592 }
593 }
594 return GL_FALSE;
595 }
596
597
598 /**
599 * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
600 * for the color buffer. Don't support zooming, pixel transfer, etc.
601 * We do support copying from one window to another, ala glXMakeCurrentRead.
602 */
603 static void
xmesa_CopyPixels(struct gl_context * ctx,GLint srcx,GLint srcy,GLsizei width,GLsizei height,GLint destx,GLint desty,GLenum type)604 xmesa_CopyPixels( struct gl_context *ctx,
605 GLint srcx, GLint srcy, GLsizei width, GLsizei height,
606 GLint destx, GLint desty, GLenum type )
607 {
608 if (can_do_CopyPixels(ctx, type)) {
609 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
610 XMesaDisplay *dpy = xmesa->xm_visual->display;
611 XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
612 const XMesaGC gc = xmbuf->cleargc; /* effected by glColorMask */
613 struct xmesa_renderbuffer *srcXrb
614 = xmesa_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
615 struct xmesa_renderbuffer *dstXrb
616 = xmesa_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
617
618 assert(dpy);
619 assert(gc);
620
621 /* Note: we don't do any special clipping work here. We could,
622 * but X will do it for us.
623 */
624 srcy = YFLIP(srcXrb, srcy) - height + 1;
625 desty = YFLIP(dstXrb, desty) - height + 1;
626 XCopyArea(dpy, srcXrb->pixmap, dstXrb->pixmap, gc,
627 srcx, srcy, width, height, destx, desty);
628 }
629 else {
630 _swrast_CopyPixels(ctx, srcx, srcy, width, height, destx, desty, type );
631 }
632 }
633
634
635
636
637 /*
638 * Every driver should implement a GetString function in order to
639 * return a meaningful GL_RENDERER string.
640 */
641 static const GLubyte *
get_string(struct gl_context * ctx,GLenum name)642 get_string( struct gl_context *ctx, GLenum name )
643 {
644 (void) ctx;
645 switch (name) {
646 case GL_RENDERER:
647 return (const GLubyte *) "Mesa X11";
648 case GL_VENDOR:
649 return NULL;
650 default:
651 return NULL;
652 }
653 }
654
655
656 /*
657 * We implement the glEnable function only because we care about
658 * dither enable/disable.
659 */
660 static void
enable(struct gl_context * ctx,GLenum pname,GLboolean state)661 enable( struct gl_context *ctx, GLenum pname, GLboolean state )
662 {
663 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
664
665 switch (pname) {
666 case GL_DITHER:
667 if (state)
668 xmesa->pixelformat = xmesa->xm_visual->dithered_pf;
669 else
670 xmesa->pixelformat = xmesa->xm_visual->undithered_pf;
671 break;
672 default:
673 ; /* silence compiler warning */
674 }
675 }
676
677
678 /**
679 * Called when the driver should update its state, based on the new_state
680 * flags.
681 */
682 static void
xmesa_update_state(struct gl_context * ctx)683 xmesa_update_state(struct gl_context *ctx)
684 {
685 GLbitfield new_state = ctx->NewState;
686 const XMesaContext xmesa = XMESA_CONTEXT(ctx);
687
688 if (new_state & (_NEW_SCISSOR | _NEW_BUFFERS | _NEW_VIEWPORT))
689 _mesa_update_draw_buffer_bounds(ctx, ctx->DrawBuffer);
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 _swsetup_InvalidateState( ctx, new_state );
697
698 if (_mesa_is_user_fbo(ctx->DrawBuffer))
699 return;
700
701 /*
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.
705 */
706 if (new_state & (_NEW_COLOR | _NEW_BUFFERS)) {
707 XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
708 struct xmesa_renderbuffer *front_xrb, *back_xrb;
709
710 front_xrb = xmbuf->frontxrb;
711 if (front_xrb) {
712 front_xrb->clearFunc = clear_pixmap;
713 }
714
715 back_xrb = xmbuf->backxrb;
716 if (back_xrb) {
717 if (xmbuf->backxrb->pixmap) {
718 back_xrb->clearFunc = clear_pixmap;
719 }
720 else {
721 switch (xmesa->xm_visual->BitsPerPixel) {
722 case 16:
723 back_xrb->clearFunc = clear_16bit_ximage;
724 break;
725 case 24:
726 back_xrb->clearFunc = clear_24bit_ximage;
727 break;
728 case 32:
729 back_xrb->clearFunc = clear_32bit_ximage;
730 break;
731 default:
732 back_xrb->clearFunc = clear_nbit_ximage;
733 break;
734 }
735 }
736 }
737 }
738 }
739
740
741 /**
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.
747 * Thus, we poll.
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.
751 */
752 static void
xmesa_viewport(struct gl_context * ctx)753 xmesa_viewport(struct gl_context *ctx)
754 {
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);
760 }
761
762
763 #if ENABLE_EXT_timer_query
764
765 /*
766 * The GL_EXT_timer_query extension is not enabled for the XServer
767 * indirect renderer. Not sure about how/if wrapping of gettimeofday()
768 * is done, etc.
769 */
770
771 struct xmesa_query_object
772 {
773 struct gl_query_object Base;
774 struct timeval StartTime;
775 };
776
777
778 static struct gl_query_object *
xmesa_new_query_object(struct gl_context * ctx,GLuint id)779 xmesa_new_query_object(struct gl_context *ctx, GLuint id)
780 {
781 struct xmesa_query_object *q = CALLOC_STRUCT(xmesa_query_object);
782 if (q) {
783 q->Base.Id = id;
784 q->Base.Ready = GL_TRUE;
785 }
786 return &q->Base;
787 }
788
789
790 static void
xmesa_begin_query(struct gl_context * ctx,struct gl_query_object * q)791 xmesa_begin_query(struct gl_context *ctx, struct gl_query_object *q)
792 {
793 if (q->Target == GL_TIME_ELAPSED_EXT) {
794 struct xmesa_query_object *xq = (struct xmesa_query_object *) q;
795 (void) gettimeofday(&xq->StartTime, NULL);
796 }
797 }
798
799
800 /**
801 * Return the difference between the two given times in microseconds.
802 */
803 static GLuint64EXT
time_diff(const struct timeval * t0,const struct timeval * t1)804 time_diff(const struct timeval *t0, const struct timeval *t1)
805 {
806 GLuint64EXT seconds0 = t0->tv_sec & 0xff; /* 0 .. 255 seconds */
807 GLuint64EXT seconds1 = t1->tv_sec & 0xff; /* 0 .. 255 seconds */
808 GLuint64EXT nanosec0 = (seconds0 * 1000000 + t0->tv_usec) * 1000;
809 GLuint64EXT nanosec1 = (seconds1 * 1000000 + t1->tv_usec) * 1000;
810 return nanosec1 - nanosec0;
811 }
812
813
814 static void
xmesa_end_query(struct gl_context * ctx,struct gl_query_object * q)815 xmesa_end_query(struct gl_context *ctx, struct gl_query_object *q)
816 {
817 if (q->Target == GL_TIME_ELAPSED_EXT) {
818 struct xmesa_query_object *xq = (struct xmesa_query_object *) q;
819 struct timeval endTime;
820 (void) gettimeofday(&endTime, NULL);
821 /* result is in nanoseconds! */
822 q->Result = time_diff(&xq->StartTime, &endTime);
823 }
824 q->Ready = GL_TRUE;
825 }
826
827 #endif /* ENABLE_timer_query */
828
829
830 /**
831 * Initialize the device driver function table with the functions
832 * we implement in this driver.
833 */
834 void
xmesa_init_driver_functions(XMesaVisual xmvisual,struct dd_function_table * driver)835 xmesa_init_driver_functions( XMesaVisual xmvisual,
836 struct dd_function_table *driver )
837 {
838 driver->GetString = get_string;
839 driver->UpdateState = xmesa_update_state;
840 driver->Flush = finish_or_flush;
841 driver->Finish = finish_or_flush;
842 driver->ColorMask = color_mask;
843 driver->Enable = enable;
844 driver->Viewport = xmesa_viewport;
845 if (TEST_META_FUNCS) {
846 driver->Clear = _mesa_meta_Clear;
847 driver->CopyPixels = _mesa_meta_CopyPixels;
848 driver->BlitFramebuffer = _mesa_meta_and_swrast_BlitFramebuffer;
849 driver->DrawPixels = _mesa_meta_DrawPixels;
850 driver->Bitmap = _mesa_meta_Bitmap;
851 }
852 else {
853 driver->Clear = clear_buffers;
854 driver->CopyPixels = xmesa_CopyPixels;
855 if (xmvisual->undithered_pf == PF_8R8G8B &&
856 xmvisual->dithered_pf == PF_8R8G8B &&
857 xmvisual->BitsPerPixel == 32) {
858 driver->DrawPixels = xmesa_DrawPixels_8R8G8B;
859 }
860 else if (xmvisual->undithered_pf == PF_5R6G5B) {
861 driver->DrawPixels = xmesa_DrawPixels_5R6G5B;
862 }
863 }
864
865 driver->MapRenderbuffer = xmesa_MapRenderbuffer;
866 driver->UnmapRenderbuffer = xmesa_UnmapRenderbuffer;
867
868 driver->GenerateMipmap = _mesa_generate_mipmap;
869
870 #if ENABLE_EXT_timer_query
871 driver->NewQueryObject = xmesa_new_query_object;
872 driver->BeginQuery = xmesa_begin_query;
873 driver->EndQuery = xmesa_end_query;
874 #endif
875 }
876
877
878 #define XMESA_NEW_POINT (_NEW_POINT | \
879 _NEW_RENDERMODE | \
880 _SWRAST_NEW_RASTERMASK)
881
882 #define XMESA_NEW_LINE (_NEW_LINE | \
883 _NEW_TEXTURE | \
884 _NEW_LIGHT | \
885 _NEW_DEPTH | \
886 _NEW_RENDERMODE | \
887 _SWRAST_NEW_RASTERMASK)
888
889 #define XMESA_NEW_TRIANGLE (_NEW_POLYGON | \
890 _NEW_TEXTURE | \
891 _NEW_LIGHT | \
892 _NEW_DEPTH | \
893 _NEW_RENDERMODE | \
894 _SWRAST_NEW_RASTERMASK)
895
896
897 /**
898 * Extend the software rasterizer with our line/point/triangle
899 * functions.
900 * Called during context creation only.
901 */
xmesa_register_swrast_functions(struct gl_context * ctx)902 void xmesa_register_swrast_functions( struct gl_context *ctx )
903 {
904 SWcontext *swrast = SWRAST_CONTEXT( ctx );
905
906 swrast->choose_point = xmesa_choose_point;
907 swrast->choose_line = xmesa_choose_line;
908 swrast->choose_triangle = xmesa_choose_triangle;
909
910 /* XXX these lines have no net effect. Remove??? */
911 swrast->InvalidatePointMask |= XMESA_NEW_POINT;
912 swrast->InvalidateLineMask |= XMESA_NEW_LINE;
913 swrast->InvalidateTriangleMask |= XMESA_NEW_TRIANGLE;
914 }
915