0a005f893c5ad87db0ee8c4d8c4eef32465659b0
[mesa.git] / src / mesa / drivers / x11 / xm_api.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2007 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 * \file xm_api.c
27 *
28 * All the XMesa* API functions.
29 *
30 *
31 * NOTES:
32 *
33 * The window coordinate system origin (0,0) is in the lower-left corner
34 * of the window. X11's window coordinate origin is in the upper-left
35 * corner of the window. Therefore, most drawing functions in this
36 * file have to flip Y coordinates.
37 *
38 * Define USE_XSHM in the Makefile with -DUSE_XSHM if you want to compile
39 * in support for the MIT Shared Memory extension. If enabled, when you
40 * use an Ximage for the back buffer in double buffered mode, the "swap"
41 * operation will be faster. You must also link with -lXext.
42 *
43 * Byte swapping: If the Mesa host and the X display use a different
44 * byte order then there's some trickiness to be aware of when using
45 * XImages. The byte ordering used for the XImage is that of the X
46 * display, not the Mesa host.
47 * The color-to-pixel encoding for True/DirectColor must be done
48 * according to the display's visual red_mask, green_mask, and blue_mask.
49 * If XPutPixel is used to put a pixel into an XImage then XPutPixel will
50 * do byte swapping if needed. If one wants to directly "poke" the pixel
51 * into the XImage's buffer then the pixel must be byte swapped first. In
52 * Mesa, when byte swapping is needed we use the PF_TRUECOLOR pixel format
53 * and use XPutPixel everywhere except in the implementation of
54 * glClear(GL_COLOR_BUFFER_BIT). We want this function to be fast so
55 * instead of using XPutPixel we "poke" our values after byte-swapping
56 * the clear pixel value if needed.
57 *
58 */
59
60 #ifdef __CYGWIN__
61 #undef WIN32
62 #undef __WIN32__
63 #endif
64
65 #include "glxheader.h"
66 #include "xmesaP.h"
67 #include "main/context.h"
68 #include "main/extensions.h"
69 #include "main/framebuffer.h"
70 #include "main/imports.h"
71 #include "main/macros.h"
72 #include "main/renderbuffer.h"
73 #include "main/teximage.h"
74 #include "main/vtxfmt.h"
75 #include "glapi/glthread.h"
76 #include "swrast/swrast.h"
77 #include "swrast/s_renderbuffer.h"
78 #include "swrast_setup/swrast_setup.h"
79 #include "vbo/vbo.h"
80 #include "tnl/tnl.h"
81 #include "tnl/t_context.h"
82 #include "tnl/t_pipeline.h"
83 #include "drivers/common/driverfuncs.h"
84 #include "drivers/common/meta.h"
85
86 /**
87 * Global X driver lock
88 */
89 _glthread_Mutex _xmesa_lock;
90
91
92
93 /**********************************************************************/
94 /***** X Utility Functions *****/
95 /**********************************************************************/
96
97
98 /**
99 * Return the host's byte order as LSBFirst or MSBFirst ala X.
100 */
101 static int host_byte_order( void )
102 {
103 int i = 1;
104 char *cptr = (char *) &i;
105 return (*cptr==1) ? LSBFirst : MSBFirst;
106 }
107
108
109 /**
110 * Check if the X Shared Memory extension is available.
111 * Return: 0 = not available
112 * 1 = shared XImage support available
113 * 2 = shared Pixmap support available also
114 */
115 static int check_for_xshm( XMesaDisplay *display )
116 {
117 #if defined(USE_XSHM)
118 int ignore;
119
120 if (XQueryExtension( display, "MIT-SHM", &ignore, &ignore, &ignore )) {
121 /* Note: we're no longer calling XShmQueryVersion() here. It seems
122 * to be flakey (triggers a spurious X protocol error when we close
123 * one display connection and start using a new one. XShm has been
124 * around a long time and hasn't changed so if MIT_SHM is supported
125 * we assume we're good to go.
126 */
127 return 2;
128 }
129 else {
130 return 0;
131 }
132 #else
133 /* No XSHM support */
134 return 0;
135 #endif
136 }
137
138
139 /**
140 * Apply gamma correction to an intensity value in [0..max]. Return the
141 * new intensity value.
142 */
143 static GLint
144 gamma_adjust( GLfloat gamma, GLint value, GLint max )
145 {
146 if (gamma == 1.0) {
147 return value;
148 }
149 else {
150 double x = (double) value / (double) max;
151 return IROUND_POS((GLfloat) max * pow(x, 1.0F/gamma));
152 }
153 }
154
155
156
157 /**
158 * Return the true number of bits per pixel for XImages.
159 * For example, if we request a 24-bit deep visual we may actually need/get
160 * 32bpp XImages. This function returns the appropriate bpp.
161 * Input: dpy - the X display
162 * visinfo - desribes the visual to be used for XImages
163 * Return: true number of bits per pixel for XImages
164 */
165 static int
166 bits_per_pixel( XMesaVisual xmv )
167 {
168 XMesaDisplay *dpy = xmv->display;
169 XMesaVisualInfo visinfo = xmv->visinfo;
170 XMesaImage *img;
171 int bitsPerPixel;
172 /* Create a temporary XImage */
173 img = XCreateImage( dpy, visinfo->visual, visinfo->depth,
174 ZPixmap, 0, /*format, offset*/
175 malloc(8), /*data*/
176 1, 1, /*width, height*/
177 32, /*bitmap_pad*/
178 0 /*bytes_per_line*/
179 );
180 assert(img);
181 /* grab the bits/pixel value */
182 bitsPerPixel = img->bits_per_pixel;
183 /* free the XImage */
184 free( img->data );
185 img->data = NULL;
186 XMesaDestroyImage( img );
187 return bitsPerPixel;
188 }
189
190
191
192 /*
193 * Determine if a given X window ID is valid (window exists).
194 * Do this by calling XGetWindowAttributes() for the window and
195 * checking if we catch an X error.
196 * Input: dpy - the display
197 * win - the window to check for existance
198 * Return: GL_TRUE - window exists
199 * GL_FALSE - window doesn't exist
200 */
201 static GLboolean WindowExistsFlag;
202
203 static int window_exists_err_handler( XMesaDisplay* dpy, XErrorEvent* xerr )
204 {
205 (void) dpy;
206 if (xerr->error_code == BadWindow) {
207 WindowExistsFlag = GL_FALSE;
208 }
209 return 0;
210 }
211
212 static GLboolean window_exists( XMesaDisplay *dpy, Window win )
213 {
214 XWindowAttributes wa;
215 int (*old_handler)( XMesaDisplay*, XErrorEvent* );
216 WindowExistsFlag = GL_TRUE;
217 old_handler = XSetErrorHandler(window_exists_err_handler);
218 XGetWindowAttributes( dpy, win, &wa ); /* dummy request */
219 XSetErrorHandler(old_handler);
220 return WindowExistsFlag;
221 }
222
223 static Status
224 get_drawable_size( XMesaDisplay *dpy, Drawable d, GLuint *width, GLuint *height )
225 {
226 Window root;
227 Status stat;
228 int xpos, ypos;
229 unsigned int w, h, bw, depth;
230 stat = XGetGeometry(dpy, d, &root, &xpos, &ypos, &w, &h, &bw, &depth);
231 *width = w;
232 *height = h;
233 return stat;
234 }
235
236
237 /**
238 * Return the size of the window (or pixmap) that corresponds to the
239 * given XMesaBuffer.
240 * \param width returns width in pixels
241 * \param height returns height in pixels
242 */
243 void
244 xmesa_get_window_size(XMesaDisplay *dpy, XMesaBuffer b,
245 GLuint *width, GLuint *height)
246 {
247 Status stat;
248
249 _glthread_LOCK_MUTEX(_xmesa_lock);
250 XSync(b->xm_visual->display, 0); /* added for Chromium */
251 stat = get_drawable_size(dpy, b->frontxrb->pixmap, width, height);
252 _glthread_UNLOCK_MUTEX(_xmesa_lock);
253
254 if (!stat) {
255 /* probably querying a window that's recently been destroyed */
256 _mesa_warning(NULL, "XGetGeometry failed!\n");
257 *width = *height = 1;
258 }
259 }
260
261
262
263 /**********************************************************************/
264 /***** Linked list of XMesaBuffers *****/
265 /**********************************************************************/
266
267 XMesaBuffer XMesaBufferList = NULL;
268
269
270 /**
271 * Allocate a new XMesaBuffer object which corresponds to the given drawable.
272 * Note that XMesaBuffer is derived from struct gl_framebuffer.
273 * The new XMesaBuffer will not have any size (Width=Height=0).
274 *
275 * \param d the corresponding X drawable (window or pixmap)
276 * \param type either WINDOW, PIXMAP or PBUFFER, describing d
277 * \param vis the buffer's visual
278 * \param cmap the window's colormap, if known.
279 * \return new XMesaBuffer or NULL if any problem
280 */
281 static XMesaBuffer
282 create_xmesa_buffer(XMesaDrawable d, BufferType type,
283 XMesaVisual vis, XMesaColormap cmap)
284 {
285 XMesaBuffer b;
286
287 ASSERT(type == WINDOW || type == PIXMAP || type == PBUFFER);
288
289 b = (XMesaBuffer) CALLOC_STRUCT(xmesa_buffer);
290 if (!b)
291 return NULL;
292
293 b->display = vis->display;
294 b->xm_visual = vis;
295 b->type = type;
296 b->cmap = cmap;
297
298 _mesa_initialize_window_framebuffer(&b->mesa_buffer, &vis->mesa_visual);
299 b->mesa_buffer.Delete = xmesa_delete_framebuffer;
300
301 /*
302 * Front renderbuffer
303 */
304 b->frontxrb = xmesa_new_renderbuffer(NULL, 0, vis, GL_FALSE);
305 if (!b->frontxrb) {
306 free(b);
307 return NULL;
308 }
309 b->frontxrb->Parent = b;
310 b->frontxrb->drawable = d;
311 b->frontxrb->pixmap = (XMesaPixmap) d;
312 _mesa_add_renderbuffer(&b->mesa_buffer, BUFFER_FRONT_LEFT,
313 &b->frontxrb->Base.Base);
314
315 /*
316 * Back renderbuffer
317 */
318 if (vis->mesa_visual.doubleBufferMode) {
319 b->backxrb = xmesa_new_renderbuffer(NULL, 0, vis, GL_TRUE);
320 if (!b->backxrb) {
321 /* XXX free front xrb too */
322 free(b);
323 return NULL;
324 }
325 b->backxrb->Parent = b;
326 /* determine back buffer implementation */
327 b->db_mode = vis->ximage_flag ? BACK_XIMAGE : BACK_PIXMAP;
328
329 _mesa_add_renderbuffer(&b->mesa_buffer, BUFFER_BACK_LEFT,
330 &b->backxrb->Base.Base);
331 }
332
333 /*
334 * Other renderbuffer (depth, stencil, etc)
335 */
336 _swrast_add_soft_renderbuffers(&b->mesa_buffer,
337 GL_FALSE, /* color */
338 vis->mesa_visual.haveDepthBuffer,
339 vis->mesa_visual.haveStencilBuffer,
340 vis->mesa_visual.haveAccumBuffer,
341 GL_FALSE, /* software alpha buffer */
342 vis->mesa_visual.numAuxBuffers > 0 );
343
344 /* GLX_EXT_texture_from_pixmap */
345 b->TextureTarget = 0;
346 b->TextureFormat = GLX_TEXTURE_FORMAT_NONE_EXT;
347 b->TextureMipmap = 0;
348
349 /* insert buffer into linked list */
350 b->Next = XMesaBufferList;
351 XMesaBufferList = b;
352
353 return b;
354 }
355
356
357 /**
358 * Find an XMesaBuffer by matching X display and colormap but NOT matching
359 * the notThis buffer.
360 */
361 XMesaBuffer
362 xmesa_find_buffer(XMesaDisplay *dpy, XMesaColormap cmap, XMesaBuffer notThis)
363 {
364 XMesaBuffer b;
365 for (b=XMesaBufferList; b; b=b->Next) {
366 if (b->display==dpy && b->cmap==cmap && b!=notThis) {
367 return b;
368 }
369 }
370 return NULL;
371 }
372
373
374 /**
375 * Remove buffer from linked list, delete if no longer referenced.
376 */
377 static void
378 xmesa_free_buffer(XMesaBuffer buffer)
379 {
380 XMesaBuffer prev = NULL, b;
381
382 for (b = XMesaBufferList; b; b = b->Next) {
383 if (b == buffer) {
384 struct gl_framebuffer *fb = &buffer->mesa_buffer;
385
386 /* unlink buffer from list */
387 if (prev)
388 prev->Next = buffer->Next;
389 else
390 XMesaBufferList = buffer->Next;
391
392 /* mark as delete pending */
393 fb->DeletePending = GL_TRUE;
394
395 /* Since the X window for the XMesaBuffer is going away, we don't
396 * want to dereference this pointer in the future.
397 */
398 b->frontxrb->drawable = 0;
399
400 /* Unreference. If count = zero we'll really delete the buffer */
401 _mesa_reference_framebuffer(&fb, NULL);
402
403 return;
404 }
405 /* continue search */
406 prev = b;
407 }
408 /* buffer not found in XMesaBufferList */
409 _mesa_problem(NULL,"xmesa_free_buffer() - buffer not found\n");
410 }
411
412
413
414
415 /**********************************************************************/
416 /***** Misc Private Functions *****/
417 /**********************************************************************/
418
419
420 /**
421 * Setup RGB rendering for a window with a True/DirectColor visual.
422 */
423 static void
424 setup_truecolor(XMesaVisual v, XMesaBuffer buffer, XMesaColormap cmap)
425 {
426 unsigned long rmask, gmask, bmask;
427 (void) buffer;
428 (void) cmap;
429
430 /* Compute red multiplier (mask) and bit shift */
431 v->rshift = 0;
432 rmask = GET_REDMASK(v);
433 while ((rmask & 1)==0) {
434 v->rshift++;
435 rmask = rmask >> 1;
436 }
437
438 /* Compute green multiplier (mask) and bit shift */
439 v->gshift = 0;
440 gmask = GET_GREENMASK(v);
441 while ((gmask & 1)==0) {
442 v->gshift++;
443 gmask = gmask >> 1;
444 }
445
446 /* Compute blue multiplier (mask) and bit shift */
447 v->bshift = 0;
448 bmask = GET_BLUEMASK(v);
449 while ((bmask & 1)==0) {
450 v->bshift++;
451 bmask = bmask >> 1;
452 }
453
454 /*
455 * Compute component-to-pixel lookup tables and dithering kernel
456 */
457 {
458 static GLubyte kernel[16] = {
459 0*16, 8*16, 2*16, 10*16,
460 12*16, 4*16, 14*16, 6*16,
461 3*16, 11*16, 1*16, 9*16,
462 15*16, 7*16, 13*16, 5*16,
463 };
464 GLint rBits = _mesa_bitcount(rmask);
465 GLint gBits = _mesa_bitcount(gmask);
466 GLint bBits = _mesa_bitcount(bmask);
467 GLint maxBits;
468 GLuint i;
469
470 /* convert pixel components in [0,_mask] to RGB values in [0,255] */
471 for (i=0; i<=rmask; i++)
472 v->PixelToR[i] = (unsigned char) ((i * 255) / rmask);
473 for (i=0; i<=gmask; i++)
474 v->PixelToG[i] = (unsigned char) ((i * 255) / gmask);
475 for (i=0; i<=bmask; i++)
476 v->PixelToB[i] = (unsigned char) ((i * 255) / bmask);
477
478 /* convert RGB values from [0,255] to pixel components */
479
480 for (i=0;i<256;i++) {
481 GLint r = gamma_adjust(v->RedGamma, i, 255);
482 GLint g = gamma_adjust(v->GreenGamma, i, 255);
483 GLint b = gamma_adjust(v->BlueGamma, i, 255);
484 v->RtoPixel[i] = (r >> (8-rBits)) << v->rshift;
485 v->GtoPixel[i] = (g >> (8-gBits)) << v->gshift;
486 v->BtoPixel[i] = (b >> (8-bBits)) << v->bshift;
487 }
488 /* overflow protection */
489 for (i=256;i<512;i++) {
490 v->RtoPixel[i] = v->RtoPixel[255];
491 v->GtoPixel[i] = v->GtoPixel[255];
492 v->BtoPixel[i] = v->BtoPixel[255];
493 }
494
495 /* setup dithering kernel */
496 maxBits = rBits;
497 if (gBits > maxBits) maxBits = gBits;
498 if (bBits > maxBits) maxBits = bBits;
499 for (i=0;i<16;i++) {
500 v->Kernel[i] = kernel[i] >> maxBits;
501 }
502
503 v->undithered_pf = PF_Truecolor;
504 v->dithered_pf = (GET_VISUAL_DEPTH(v)<24) ? PF_Dither_True : PF_Truecolor;
505 }
506
507 /*
508 * Now check for TrueColor visuals which we can optimize.
509 */
510 if ( GET_REDMASK(v) ==0x0000ff
511 && GET_GREENMASK(v)==0x00ff00
512 && GET_BLUEMASK(v) ==0xff0000
513 && CHECK_BYTE_ORDER(v)
514 && v->BitsPerPixel==32
515 && v->RedGamma==1.0 && v->GreenGamma==1.0 && v->BlueGamma==1.0) {
516 /* common 32 bpp config used on SGI, Sun */
517 v->undithered_pf = v->dithered_pf = PF_8A8B8G8R; /* ABGR */
518 }
519 else if (GET_REDMASK(v) == 0xff0000
520 && GET_GREENMASK(v)== 0x00ff00
521 && GET_BLUEMASK(v) == 0x0000ff
522 && CHECK_BYTE_ORDER(v)
523 && v->RedGamma == 1.0 && v->GreenGamma == 1.0 && v->BlueGamma == 1.0){
524 if (v->BitsPerPixel==32) {
525 /* if 32 bpp, and visual indicates 8 bpp alpha channel */
526 if (GET_VISUAL_DEPTH(v) == 32 && v->mesa_visual.alphaBits == 8)
527 v->undithered_pf = v->dithered_pf = PF_8A8R8G8B; /* ARGB */
528 else
529 v->undithered_pf = v->dithered_pf = PF_8R8G8B; /* xRGB */
530 }
531 else if (v->BitsPerPixel == 24) {
532 v->undithered_pf = v->dithered_pf = PF_8R8G8B24; /* RGB */
533 }
534 }
535 else if (GET_REDMASK(v) ==0xf800
536 && GET_GREENMASK(v)==0x07e0
537 && GET_BLUEMASK(v) ==0x001f
538 && CHECK_BYTE_ORDER(v)
539 && v->BitsPerPixel==16
540 && v->RedGamma==1.0 && v->GreenGamma==1.0 && v->BlueGamma==1.0) {
541 /* 5-6-5 RGB */
542 v->undithered_pf = PF_5R6G5B;
543 v->dithered_pf = PF_Dither_5R6G5B;
544 }
545 }
546
547
548 /**
549 * When a context is bound for the first time, we can finally finish
550 * initializing the context's visual and buffer information.
551 * \param v the XMesaVisual to initialize
552 * \param b the XMesaBuffer to initialize (may be NULL)
553 * \param rgb_flag TRUE = RGBA mode, FALSE = color index mode
554 * \param window the window/pixmap we're rendering into
555 * \param cmap the colormap associated with the window/pixmap
556 * \return GL_TRUE=success, GL_FALSE=failure
557 */
558 static GLboolean
559 initialize_visual_and_buffer(XMesaVisual v, XMesaBuffer b,
560 XMesaDrawable window,
561 XMesaColormap cmap)
562 {
563 const int xclass = v->visualType;
564
565
566 ASSERT(!b || b->xm_visual == v);
567
568 /* Save true bits/pixel */
569 v->BitsPerPixel = bits_per_pixel(v);
570 assert(v->BitsPerPixel > 0);
571
572 /* RGB WINDOW:
573 * We support RGB rendering into almost any kind of visual.
574 */
575 if (xclass == GLX_TRUE_COLOR || xclass == GLX_DIRECT_COLOR) {
576 setup_truecolor( v, b, cmap );
577 }
578 else {
579 _mesa_warning(NULL, "XMesa: RGB mode rendering not supported in given visual.\n");
580 return GL_FALSE;
581 }
582 v->mesa_visual.indexBits = 0;
583
584 if (_mesa_getenv("MESA_NO_DITHER")) {
585 v->dithered_pf = v->undithered_pf;
586 }
587
588
589 /*
590 * If MESA_INFO env var is set print out some debugging info
591 * which can help Brian figure out what's going on when a user
592 * reports bugs.
593 */
594 if (_mesa_getenv("MESA_INFO")) {
595 printf("X/Mesa visual = %p\n", (void *) v);
596 printf("X/Mesa dithered pf = %u\n", v->dithered_pf);
597 printf("X/Mesa undithered pf = %u\n", v->undithered_pf);
598 printf("X/Mesa level = %d\n", v->mesa_visual.level);
599 printf("X/Mesa depth = %d\n", GET_VISUAL_DEPTH(v));
600 printf("X/Mesa bits per pixel = %d\n", v->BitsPerPixel);
601 }
602
603 if (b && window) {
604 /* Do window-specific initializations */
605
606 /* these should have been set in create_xmesa_buffer */
607 ASSERT(b->frontxrb->drawable == window);
608 ASSERT(b->frontxrb->pixmap == (XMesaPixmap) window);
609
610 /* Setup for single/double buffering */
611 if (v->mesa_visual.doubleBufferMode) {
612 /* Double buffered */
613 b->shm = check_for_xshm( v->display );
614 }
615
616 /* X11 graphics contexts */
617 b->gc = XCreateGC( v->display, window, 0, NULL );
618 XMesaSetFunction( v->display, b->gc, GXcopy );
619
620 /* cleargc - for glClear() */
621 b->cleargc = XCreateGC( v->display, window, 0, NULL );
622 XMesaSetFunction( v->display, b->cleargc, GXcopy );
623
624 /*
625 * Don't generate Graphics Expose/NoExpose events in swapbuffers().
626 * Patch contributed by Michael Pichler May 15, 1995.
627 */
628 {
629 XGCValues gcvalues;
630 gcvalues.graphics_exposures = False;
631 b->swapgc = XCreateGC(v->display, window,
632 GCGraphicsExposures, &gcvalues);
633 }
634 XMesaSetFunction( v->display, b->swapgc, GXcopy );
635 }
636
637 return GL_TRUE;
638 }
639
640
641
642 /*
643 * Convert an RGBA color to a pixel value.
644 */
645 unsigned long
646 xmesa_color_to_pixel(struct gl_context *ctx,
647 GLubyte r, GLubyte g, GLubyte b, GLubyte a,
648 GLuint pixelFormat)
649 {
650 XMesaContext xmesa = XMESA_CONTEXT(ctx);
651 switch (pixelFormat) {
652 case PF_Truecolor:
653 {
654 unsigned long p;
655 PACK_TRUECOLOR( p, r, g, b );
656 return p;
657 }
658 case PF_8A8B8G8R:
659 return PACK_8A8B8G8R( r, g, b, a );
660 case PF_8A8R8G8B:
661 return PACK_8A8R8G8B( r, g, b, a );
662 case PF_8R8G8B:
663 /* fall through */
664 case PF_8R8G8B24:
665 return PACK_8R8G8B( r, g, b );
666 case PF_5R6G5B:
667 return PACK_5R6G5B( r, g, b );
668 case PF_Dither_True:
669 /* fall through */
670 case PF_Dither_5R6G5B:
671 {
672 unsigned long p;
673 PACK_TRUEDITHER(p, 1, 0, r, g, b);
674 return p;
675 }
676 default:
677 _mesa_problem(ctx, "Bad pixel format in xmesa_color_to_pixel");
678 }
679 return 0;
680 }
681
682
683 #define NUM_VISUAL_TYPES 6
684
685 /**
686 * Convert an X visual type to a GLX visual type.
687 *
688 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
689 * to be converted.
690 * \return If \c visualType is a valid X visual type, a GLX visual type will
691 * be returned. Otherwise \c GLX_NONE will be returned.
692 *
693 * \note
694 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
695 * DRI CVS tree.
696 */
697 static GLint
698 xmesa_convert_from_x_visual_type( int visualType )
699 {
700 static const int glx_visual_types[ NUM_VISUAL_TYPES ] = {
701 GLX_STATIC_GRAY, GLX_GRAY_SCALE,
702 GLX_STATIC_COLOR, GLX_PSEUDO_COLOR,
703 GLX_TRUE_COLOR, GLX_DIRECT_COLOR
704 };
705
706 return ( (unsigned) visualType < NUM_VISUAL_TYPES )
707 ? glx_visual_types[ visualType ] : GLX_NONE;
708 }
709
710
711 /**********************************************************************/
712 /***** Public Functions *****/
713 /**********************************************************************/
714
715
716 /*
717 * Create a new X/Mesa visual.
718 * Input: display - X11 display
719 * visinfo - an XVisualInfo pointer
720 * rgb_flag - GL_TRUE = RGB mode,
721 * GL_FALSE = color index mode
722 * alpha_flag - alpha buffer requested?
723 * db_flag - GL_TRUE = double-buffered,
724 * GL_FALSE = single buffered
725 * stereo_flag - stereo visual?
726 * ximage_flag - GL_TRUE = use an XImage for back buffer,
727 * GL_FALSE = use an off-screen pixmap for back buffer
728 * depth_size - requested bits/depth values, or zero
729 * stencil_size - requested bits/stencil values, or zero
730 * accum_red_size - requested bits/red accum values, or zero
731 * accum_green_size - requested bits/green accum values, or zero
732 * accum_blue_size - requested bits/blue accum values, or zero
733 * accum_alpha_size - requested bits/alpha accum values, or zero
734 * num_samples - number of samples/pixel if multisampling, or zero
735 * level - visual level, usually 0
736 * visualCaveat - ala the GLX extension, usually GLX_NONE
737 * Return; a new XMesaVisual or 0 if error.
738 */
739 PUBLIC
740 XMesaVisual XMesaCreateVisual( XMesaDisplay *display,
741 XMesaVisualInfo visinfo,
742 GLboolean rgb_flag,
743 GLboolean alpha_flag,
744 GLboolean db_flag,
745 GLboolean stereo_flag,
746 GLboolean ximage_flag,
747 GLint depth_size,
748 GLint stencil_size,
749 GLint accum_red_size,
750 GLint accum_green_size,
751 GLint accum_blue_size,
752 GLint accum_alpha_size,
753 GLint num_samples,
754 GLint level,
755 GLint visualCaveat )
756 {
757 char *gamma;
758 XMesaVisual v;
759 GLint red_bits, green_bits, blue_bits, alpha_bits;
760
761 /* For debugging only */
762 if (_mesa_getenv("MESA_XSYNC")) {
763 /* This makes debugging X easier.
764 * In your debugger, set a breakpoint on _XError to stop when an
765 * X protocol error is generated.
766 */
767 XSynchronize( display, 1 );
768 }
769
770 /* Color-index rendering not supported. */
771 if (!rgb_flag)
772 return NULL;
773
774 v = (XMesaVisual) CALLOC_STRUCT(xmesa_visual);
775 if (!v) {
776 return NULL;
777 }
778
779 v->display = display;
780
781 /* Save a copy of the XVisualInfo struct because the user may Xfree()
782 * the struct but we may need some of the information contained in it
783 * at a later time.
784 */
785 v->visinfo = (XVisualInfo *) malloc(sizeof(*visinfo));
786 if(!v->visinfo) {
787 free(v);
788 return NULL;
789 }
790 memcpy(v->visinfo, visinfo, sizeof(*visinfo));
791
792 /* check for MESA_GAMMA environment variable */
793 gamma = _mesa_getenv("MESA_GAMMA");
794 if (gamma) {
795 v->RedGamma = v->GreenGamma = v->BlueGamma = 0.0;
796 sscanf( gamma, "%f %f %f", &v->RedGamma, &v->GreenGamma, &v->BlueGamma );
797 if (v->RedGamma<=0.0) v->RedGamma = 1.0;
798 if (v->GreenGamma<=0.0) v->GreenGamma = v->RedGamma;
799 if (v->BlueGamma<=0.0) v->BlueGamma = v->RedGamma;
800 }
801 else {
802 v->RedGamma = v->GreenGamma = v->BlueGamma = 1.0;
803 }
804
805 v->ximage_flag = ximage_flag;
806
807 v->mesa_visual.redMask = visinfo->red_mask;
808 v->mesa_visual.greenMask = visinfo->green_mask;
809 v->mesa_visual.blueMask = visinfo->blue_mask;
810 v->visualID = visinfo->visualid;
811 v->screen = visinfo->screen;
812
813 #if !(defined(__cplusplus) || defined(c_plusplus))
814 v->visualType = xmesa_convert_from_x_visual_type(visinfo->class);
815 #else
816 v->visualType = xmesa_convert_from_x_visual_type(visinfo->c_class);
817 #endif
818
819 v->mesa_visual.visualRating = visualCaveat;
820
821 if (alpha_flag)
822 v->mesa_visual.alphaBits = 8;
823
824 (void) initialize_visual_and_buffer( v, NULL, 0, 0 );
825
826 {
827 const int xclass = v->visualType;
828 if (xclass == GLX_TRUE_COLOR || xclass == GLX_DIRECT_COLOR) {
829 red_bits = _mesa_bitcount(GET_REDMASK(v));
830 green_bits = _mesa_bitcount(GET_GREENMASK(v));
831 blue_bits = _mesa_bitcount(GET_BLUEMASK(v));
832 }
833 else {
834 /* this is an approximation */
835 int depth;
836 depth = GET_VISUAL_DEPTH(v);
837 red_bits = depth / 3;
838 depth -= red_bits;
839 green_bits = depth / 2;
840 depth -= green_bits;
841 blue_bits = depth;
842 alpha_bits = 0;
843 assert( red_bits + green_bits + blue_bits == GET_VISUAL_DEPTH(v) );
844 }
845 alpha_bits = v->mesa_visual.alphaBits;
846 }
847
848 _mesa_initialize_visual( &v->mesa_visual,
849 db_flag, stereo_flag,
850 red_bits, green_bits,
851 blue_bits, alpha_bits,
852 depth_size,
853 stencil_size,
854 accum_red_size, accum_green_size,
855 accum_blue_size, accum_alpha_size,
856 0 );
857
858 /* XXX minor hack */
859 v->mesa_visual.level = level;
860 return v;
861 }
862
863
864 PUBLIC
865 void XMesaDestroyVisual( XMesaVisual v )
866 {
867 free(v->visinfo);
868 free(v);
869 }
870
871
872
873 /**
874 * Create a new XMesaContext.
875 * \param v the XMesaVisual
876 * \param share_list another XMesaContext with which to share display
877 * lists or NULL if no sharing is wanted.
878 * \return an XMesaContext or NULL if error.
879 */
880 PUBLIC
881 XMesaContext XMesaCreateContext( XMesaVisual v, XMesaContext share_list )
882 {
883 static GLboolean firstTime = GL_TRUE;
884 XMesaContext c;
885 struct gl_context *mesaCtx;
886 struct dd_function_table functions;
887 TNLcontext *tnl;
888
889 if (firstTime) {
890 _glthread_INIT_MUTEX(_xmesa_lock);
891 firstTime = GL_FALSE;
892 }
893
894 /* Note: the XMesaContext contains a Mesa struct gl_context struct (inheritance) */
895 c = (XMesaContext) CALLOC_STRUCT(xmesa_context);
896 if (!c)
897 return NULL;
898
899 mesaCtx = &(c->mesa);
900
901 /* initialize with default driver functions, then plug in XMesa funcs */
902 _mesa_init_driver_functions(&functions);
903 xmesa_init_driver_functions(v, &functions);
904 if (!_mesa_initialize_context(mesaCtx, API_OPENGL_COMPAT, &v->mesa_visual,
905 share_list ? &(share_list->mesa) : (struct gl_context *) NULL,
906 &functions)) {
907 free(c);
908 return NULL;
909 }
910
911 /* Enable this to exercise fixed function -> shader translation
912 * with software rendering.
913 */
914 if (0) {
915 mesaCtx->VertexProgram._MaintainTnlProgram = GL_TRUE;
916 mesaCtx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
917 }
918
919 _mesa_enable_sw_extensions(mesaCtx);
920 _mesa_enable_1_3_extensions(mesaCtx);
921 _mesa_enable_1_4_extensions(mesaCtx);
922 _mesa_enable_1_5_extensions(mesaCtx);
923 _mesa_enable_2_0_extensions(mesaCtx);
924 _mesa_enable_2_1_extensions(mesaCtx);
925 if (mesaCtx->Mesa_DXTn) {
926 _mesa_enable_extension(mesaCtx, "GL_EXT_texture_compression_s3tc");
927 _mesa_enable_extension(mesaCtx, "GL_S3_s3tc");
928 }
929 _mesa_enable_extension(mesaCtx, "GL_3DFX_texture_compression_FXT1");
930 #if ENABLE_EXT_timer_query
931 _mesa_enable_extension(mesaCtx, "GL_EXT_timer_query");
932 #endif
933
934
935 /* finish up xmesa context initializations */
936 c->swapbytes = CHECK_BYTE_ORDER(v) ? GL_FALSE : GL_TRUE;
937 c->xm_visual = v;
938 c->xm_buffer = NULL; /* set later by XMesaMakeCurrent */
939 c->display = v->display;
940 c->pixelformat = v->dithered_pf; /* Dithering is enabled by default */
941
942 /* Initialize the software rasterizer and helper modules.
943 */
944 if (!_swrast_CreateContext( mesaCtx ) ||
945 !_vbo_CreateContext( mesaCtx ) ||
946 !_tnl_CreateContext( mesaCtx ) ||
947 !_swsetup_CreateContext( mesaCtx )) {
948 _mesa_free_context_data(&c->mesa);
949 free(c);
950 return NULL;
951 }
952
953 /* tnl setup */
954 tnl = TNL_CONTEXT(mesaCtx);
955 tnl->Driver.RunPipeline = _tnl_run_pipeline;
956 /* swrast setup */
957 xmesa_register_swrast_functions( mesaCtx );
958 _swsetup_Wakeup(mesaCtx);
959
960 _mesa_meta_init(mesaCtx);
961
962 _mesa_compute_version(ctx);
963
964 /* Exec table initialization requires the version to be computed */
965 _mesa_initialize_exec_table(ctx);
966 _mesa_initialize_vbo_vtxfmt(ctx);
967
968 return c;
969 }
970
971
972
973 PUBLIC
974 void XMesaDestroyContext( XMesaContext c )
975 {
976 struct gl_context *mesaCtx = &c->mesa;
977
978 _mesa_meta_free( mesaCtx );
979
980 _swsetup_DestroyContext( mesaCtx );
981 _swrast_DestroyContext( mesaCtx );
982 _tnl_DestroyContext( mesaCtx );
983 _vbo_DestroyContext( mesaCtx );
984 _mesa_free_context_data( mesaCtx );
985 free( c );
986 }
987
988
989
990 /**
991 * Private function for creating an XMesaBuffer which corresponds to an
992 * X window or pixmap.
993 * \param v the window's XMesaVisual
994 * \param w the window we're wrapping
995 * \return new XMesaBuffer or NULL if error
996 */
997 PUBLIC XMesaBuffer
998 XMesaCreateWindowBuffer(XMesaVisual v, XMesaWindow w)
999 {
1000 XWindowAttributes attr;
1001 XMesaBuffer b;
1002 XMesaColormap cmap;
1003 int depth;
1004
1005 assert(v);
1006 assert(w);
1007
1008 /* Check that window depth matches visual depth */
1009 XGetWindowAttributes( v->display, w, &attr );
1010 depth = attr.depth;
1011 if (GET_VISUAL_DEPTH(v) != depth) {
1012 _mesa_warning(NULL, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
1013 GET_VISUAL_DEPTH(v), depth);
1014 return NULL;
1015 }
1016
1017 /* Find colormap */
1018 if (attr.colormap) {
1019 cmap = attr.colormap;
1020 }
1021 else {
1022 _mesa_warning(NULL, "Window %u has no colormap!\n", (unsigned int) w);
1023 /* this is weird, a window w/out a colormap!? */
1024 /* OK, let's just allocate a new one and hope for the best */
1025 cmap = XCreateColormap(v->display, w, attr.visual, AllocNone);
1026 }
1027
1028 b = create_xmesa_buffer((XMesaDrawable) w, WINDOW, v, cmap);
1029 if (!b)
1030 return NULL;
1031
1032 if (!initialize_visual_and_buffer( v, b, (XMesaDrawable) w, cmap )) {
1033 xmesa_free_buffer(b);
1034 return NULL;
1035 }
1036
1037 return b;
1038 }
1039
1040
1041
1042 /**
1043 * Create a new XMesaBuffer from an X pixmap.
1044 *
1045 * \param v the XMesaVisual
1046 * \param p the pixmap
1047 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
1048 * \c GLX_DIRECT_COLOR visual for the pixmap
1049 * \returns new XMesaBuffer or NULL if error
1050 */
1051 PUBLIC XMesaBuffer
1052 XMesaCreatePixmapBuffer(XMesaVisual v, XMesaPixmap p, XMesaColormap cmap)
1053 {
1054 XMesaBuffer b;
1055
1056 assert(v);
1057
1058 b = create_xmesa_buffer((XMesaDrawable) p, PIXMAP, v, cmap);
1059 if (!b)
1060 return NULL;
1061
1062 if (!initialize_visual_and_buffer(v, b, (XMesaDrawable) p, cmap)) {
1063 xmesa_free_buffer(b);
1064 return NULL;
1065 }
1066
1067 return b;
1068 }
1069
1070
1071 /**
1072 * For GLX_EXT_texture_from_pixmap
1073 */
1074 XMesaBuffer
1075 XMesaCreatePixmapTextureBuffer(XMesaVisual v, XMesaPixmap p,
1076 XMesaColormap cmap,
1077 int format, int target, int mipmap)
1078 {
1079 GET_CURRENT_CONTEXT(ctx);
1080 XMesaBuffer b;
1081 GLuint width, height;
1082
1083 assert(v);
1084
1085 b = create_xmesa_buffer((XMesaDrawable) p, PIXMAP, v, cmap);
1086 if (!b)
1087 return NULL;
1088
1089 /* get pixmap size, update framebuffer/renderbuffer dims */
1090 xmesa_get_window_size(v->display, b, &width, &height);
1091 _mesa_resize_framebuffer(NULL, &(b->mesa_buffer), width, height);
1092
1093 if (target == 0) {
1094 /* examine dims */
1095 if (ctx->Extensions.ARB_texture_non_power_of_two) {
1096 target = GLX_TEXTURE_2D_EXT;
1097 }
1098 else if ( _mesa_bitcount(width) == 1
1099 && _mesa_bitcount(height) == 1) {
1100 /* power of two size */
1101 if (height == 1) {
1102 target = GLX_TEXTURE_1D_EXT;
1103 }
1104 else {
1105 target = GLX_TEXTURE_2D_EXT;
1106 }
1107 }
1108 else if (ctx->Extensions.NV_texture_rectangle) {
1109 target = GLX_TEXTURE_RECTANGLE_EXT;
1110 }
1111 else {
1112 /* non power of two textures not supported */
1113 XMesaDestroyBuffer(b);
1114 return 0;
1115 }
1116 }
1117
1118 b->TextureTarget = target;
1119 b->TextureFormat = format;
1120 b->TextureMipmap = mipmap;
1121
1122 if (!initialize_visual_and_buffer(v, b, (XMesaDrawable) p, cmap)) {
1123 xmesa_free_buffer(b);
1124 return NULL;
1125 }
1126
1127 return b;
1128 }
1129
1130
1131
1132 XMesaBuffer
1133 XMesaCreatePBuffer(XMesaVisual v, XMesaColormap cmap,
1134 unsigned int width, unsigned int height)
1135 {
1136 XMesaWindow root;
1137 XMesaDrawable drawable; /* X Pixmap Drawable */
1138 XMesaBuffer b;
1139
1140 /* allocate pixmap for front buffer */
1141 root = RootWindow( v->display, v->visinfo->screen );
1142 drawable = XCreatePixmap(v->display, root, width, height,
1143 v->visinfo->depth);
1144 if (!drawable)
1145 return NULL;
1146
1147 b = create_xmesa_buffer(drawable, PBUFFER, v, cmap);
1148 if (!b)
1149 return NULL;
1150
1151 if (!initialize_visual_and_buffer(v, b, drawable, cmap)) {
1152 xmesa_free_buffer(b);
1153 return NULL;
1154 }
1155
1156 return b;
1157 }
1158
1159
1160
1161 /*
1162 * Deallocate an XMesaBuffer structure and all related info.
1163 */
1164 PUBLIC void
1165 XMesaDestroyBuffer(XMesaBuffer b)
1166 {
1167 xmesa_free_buffer(b);
1168 }
1169
1170
1171 /**
1172 * Query the current window size and update the corresponding struct gl_framebuffer
1173 * and all attached renderbuffers.
1174 * Called when:
1175 * 1. the first time a buffer is bound to a context.
1176 * 2. from glViewport to poll for window size changes
1177 * 3. from the XMesaResizeBuffers() API function.
1178 * Note: it's possible (and legal) for xmctx to be NULL. That can happen
1179 * when resizing a buffer when no rendering context is bound.
1180 */
1181 void
1182 xmesa_check_and_update_buffer_size(XMesaContext xmctx, XMesaBuffer drawBuffer)
1183 {
1184 GLuint width, height;
1185 xmesa_get_window_size(drawBuffer->display, drawBuffer, &width, &height);
1186 if (drawBuffer->mesa_buffer.Width != width ||
1187 drawBuffer->mesa_buffer.Height != height) {
1188 struct gl_context *ctx = xmctx ? &xmctx->mesa : NULL;
1189 _mesa_resize_framebuffer(ctx, &(drawBuffer->mesa_buffer), width, height);
1190 }
1191 drawBuffer->mesa_buffer.Initialized = GL_TRUE; /* XXX TEMPORARY? */
1192 }
1193
1194
1195 /*
1196 * Bind buffer b to context c and make c the current rendering context.
1197 */
1198 GLboolean XMesaMakeCurrent( XMesaContext c, XMesaBuffer b )
1199 {
1200 return XMesaMakeCurrent2( c, b, b );
1201 }
1202
1203
1204 /*
1205 * Bind buffer b to context c and make c the current rendering context.
1206 */
1207 PUBLIC
1208 GLboolean XMesaMakeCurrent2( XMesaContext c, XMesaBuffer drawBuffer,
1209 XMesaBuffer readBuffer )
1210 {
1211 if (c) {
1212 if (!drawBuffer || !readBuffer)
1213 return GL_FALSE; /* must specify buffers! */
1214
1215 if (&(c->mesa) == _mesa_get_current_context()
1216 && c->mesa.DrawBuffer == &drawBuffer->mesa_buffer
1217 && c->mesa.ReadBuffer == &readBuffer->mesa_buffer
1218 && XMESA_BUFFER(c->mesa.DrawBuffer)->wasCurrent) {
1219 /* same context and buffer, do nothing */
1220 return GL_TRUE;
1221 }
1222
1223 c->xm_buffer = drawBuffer;
1224
1225 /* Call this periodically to detect when the user has begun using
1226 * GL rendering from multiple threads.
1227 */
1228 _glapi_check_multithread();
1229
1230 xmesa_check_and_update_buffer_size(c, drawBuffer);
1231 if (readBuffer != drawBuffer)
1232 xmesa_check_and_update_buffer_size(c, readBuffer);
1233
1234 _mesa_make_current(&(c->mesa),
1235 &drawBuffer->mesa_buffer,
1236 &readBuffer->mesa_buffer);
1237
1238 /*
1239 * Must recompute and set these pixel values because colormap
1240 * can be different for different windows.
1241 */
1242 c->clearpixel = xmesa_color_to_pixel( &c->mesa,
1243 c->clearcolor[0],
1244 c->clearcolor[1],
1245 c->clearcolor[2],
1246 c->clearcolor[3],
1247 c->xm_visual->undithered_pf);
1248 XMesaSetForeground(c->display, drawBuffer->cleargc, c->clearpixel);
1249
1250 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1251 drawBuffer->wasCurrent = GL_TRUE;
1252 }
1253 else {
1254 /* Detach */
1255 _mesa_make_current( NULL, NULL, NULL );
1256 }
1257 return GL_TRUE;
1258 }
1259
1260
1261 /*
1262 * Unbind the context c from its buffer.
1263 */
1264 GLboolean XMesaUnbindContext( XMesaContext c )
1265 {
1266 /* A no-op for XFree86 integration purposes */
1267 return GL_TRUE;
1268 }
1269
1270
1271 XMesaContext XMesaGetCurrentContext( void )
1272 {
1273 GET_CURRENT_CONTEXT(ctx);
1274 if (ctx) {
1275 XMesaContext xmesa = XMESA_CONTEXT(ctx);
1276 return xmesa;
1277 }
1278 else {
1279 return 0;
1280 }
1281 }
1282
1283
1284 XMesaBuffer XMesaGetCurrentBuffer( void )
1285 {
1286 GET_CURRENT_CONTEXT(ctx);
1287 if (ctx) {
1288 XMesaBuffer xmbuf = XMESA_BUFFER(ctx->DrawBuffer);
1289 return xmbuf;
1290 }
1291 else {
1292 return 0;
1293 }
1294 }
1295
1296
1297 /* New in Mesa 3.1 */
1298 XMesaBuffer XMesaGetCurrentReadBuffer( void )
1299 {
1300 GET_CURRENT_CONTEXT(ctx);
1301 if (ctx) {
1302 return XMESA_BUFFER(ctx->ReadBuffer);
1303 }
1304 else {
1305 return 0;
1306 }
1307 }
1308
1309
1310
1311 GLboolean XMesaSetFXmode( GLint mode )
1312 {
1313 (void) mode;
1314 return GL_FALSE;
1315 }
1316
1317
1318
1319 /*
1320 * Copy the back buffer to the front buffer. If there's no back buffer
1321 * this is a no-op.
1322 */
1323 PUBLIC
1324 void XMesaSwapBuffers( XMesaBuffer b )
1325 {
1326 GET_CURRENT_CONTEXT(ctx);
1327
1328 if (!b->backxrb) {
1329 /* single buffered */
1330 return;
1331 }
1332
1333 /* If we're swapping the buffer associated with the current context
1334 * we have to flush any pending rendering commands first.
1335 */
1336 if (ctx && ctx->DrawBuffer == &(b->mesa_buffer))
1337 _mesa_notifySwapBuffers(ctx);
1338
1339 if (b->db_mode) {
1340 if (b->backxrb->ximage) {
1341 /* Copy Ximage (back buf) from client memory to server window */
1342 #if defined(USE_XSHM)
1343 if (b->shm) {
1344 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
1345 XShmPutImage( b->xm_visual->display, b->frontxrb->drawable,
1346 b->swapgc,
1347 b->backxrb->ximage, 0, 0,
1348 0, 0, b->mesa_buffer.Width, b->mesa_buffer.Height,
1349 False );
1350 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
1351 }
1352 else
1353 #endif
1354 {
1355 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
1356 XMesaPutImage( b->xm_visual->display, b->frontxrb->drawable,
1357 b->swapgc,
1358 b->backxrb->ximage, 0, 0,
1359 0, 0, b->mesa_buffer.Width, b->mesa_buffer.Height );
1360 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
1361 }
1362 }
1363 else if (b->backxrb->pixmap) {
1364 /* Copy pixmap (back buf) to window (front buf) on server */
1365 /*_glthread_LOCK_MUTEX(_xmesa_lock);*/
1366 XMesaCopyArea( b->xm_visual->display,
1367 b->backxrb->pixmap, /* source drawable */
1368 b->frontxrb->drawable, /* dest. drawable */
1369 b->swapgc,
1370 0, 0, b->mesa_buffer.Width, b->mesa_buffer.Height,
1371 0, 0 /* dest region */
1372 );
1373 /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
1374 }
1375 }
1376 XSync( b->xm_visual->display, False );
1377 }
1378
1379
1380
1381 /*
1382 * Copy sub-region of back buffer to front buffer
1383 */
1384 void XMesaCopySubBuffer( XMesaBuffer b, int x, int y, int width, int height )
1385 {
1386 GET_CURRENT_CONTEXT(ctx);
1387
1388 /* If we're swapping the buffer associated with the current context
1389 * we have to flush any pending rendering commands first.
1390 */
1391 if (ctx && ctx->DrawBuffer == &(b->mesa_buffer))
1392 _mesa_notifySwapBuffers(ctx);
1393
1394 if (!b->backxrb) {
1395 /* single buffered */
1396 return;
1397 }
1398
1399 if (b->db_mode) {
1400 int yTop = b->mesa_buffer.Height - y - height;
1401 if (b->backxrb->ximage) {
1402 /* Copy Ximage from host's memory to server's window */
1403 #if defined(USE_XSHM)
1404 if (b->shm) {
1405 /* XXX assuming width and height aren't too large! */
1406 XShmPutImage( b->xm_visual->display, b->frontxrb->drawable,
1407 b->swapgc,
1408 b->backxrb->ximage, x, yTop,
1409 x, yTop, width, height, False );
1410 /* wait for finished event??? */
1411 }
1412 else
1413 #endif
1414 {
1415 /* XXX assuming width and height aren't too large! */
1416 XMesaPutImage( b->xm_visual->display, b->frontxrb->drawable,
1417 b->swapgc,
1418 b->backxrb->ximage, x, yTop,
1419 x, yTop, width, height );
1420 }
1421 }
1422 else {
1423 /* Copy pixmap to window on server */
1424 XMesaCopyArea( b->xm_visual->display,
1425 b->backxrb->pixmap, /* source drawable */
1426 b->frontxrb->drawable, /* dest. drawable */
1427 b->swapgc,
1428 x, yTop, width, height, /* source region */
1429 x, yTop /* dest region */
1430 );
1431 }
1432 }
1433 }
1434
1435
1436 /*
1437 * Return a pointer to the XMesa backbuffer Pixmap or XImage. This function
1438 * is a way to get "under the hood" of X/Mesa so one can manipulate the
1439 * back buffer directly.
1440 * Output: pixmap - pointer to back buffer's Pixmap, or 0
1441 * ximage - pointer to back buffer's XImage, or NULL
1442 * Return: GL_TRUE = context is double buffered
1443 * GL_FALSE = context is single buffered
1444 */
1445 GLboolean XMesaGetBackBuffer( XMesaBuffer b,
1446 XMesaPixmap *pixmap,
1447 XMesaImage **ximage )
1448 {
1449 if (b->db_mode) {
1450 if (pixmap)
1451 *pixmap = b->backxrb->pixmap;
1452 if (ximage)
1453 *ximage = b->backxrb->ximage;
1454 return GL_TRUE;
1455 }
1456 else {
1457 *pixmap = 0;
1458 *ximage = NULL;
1459 return GL_FALSE;
1460 }
1461 }
1462
1463
1464 /*
1465 * Return the depth buffer associated with an XMesaBuffer.
1466 * Input: b - the XMesa buffer handle
1467 * Output: width, height - size of buffer in pixels
1468 * bytesPerValue - bytes per depth value (2 or 4)
1469 * buffer - pointer to depth buffer values
1470 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1471 */
1472 GLboolean XMesaGetDepthBuffer( XMesaBuffer b, GLint *width, GLint *height,
1473 GLint *bytesPerValue, void **buffer )
1474 {
1475 struct gl_renderbuffer *rb
1476 = b->mesa_buffer.Attachment[BUFFER_DEPTH].Renderbuffer;
1477 struct xmesa_renderbuffer *xrb = xmesa_renderbuffer(rb);
1478
1479 if (!xrb || !xrb->Base.Buffer) {
1480 *width = 0;
1481 *height = 0;
1482 *bytesPerValue = 0;
1483 *buffer = 0;
1484 return GL_FALSE;
1485 }
1486 else {
1487 *width = b->mesa_buffer.Width;
1488 *height = b->mesa_buffer.Height;
1489 *bytesPerValue = b->mesa_buffer.Visual.depthBits <= 16
1490 ? sizeof(GLushort) : sizeof(GLuint);
1491 *buffer = (void *) xrb->Base.Buffer;
1492 return GL_TRUE;
1493 }
1494 }
1495
1496
1497 void XMesaFlush( XMesaContext c )
1498 {
1499 if (c && c->xm_visual) {
1500 XSync( c->xm_visual->display, False );
1501 }
1502 }
1503
1504
1505
1506 const char *XMesaGetString( XMesaContext c, int name )
1507 {
1508 (void) c;
1509 if (name==XMESA_VERSION) {
1510 return "5.0";
1511 }
1512 else if (name==XMESA_EXTENSIONS) {
1513 return "";
1514 }
1515 else {
1516 return NULL;
1517 }
1518 }
1519
1520
1521
1522 XMesaBuffer XMesaFindBuffer( XMesaDisplay *dpy, XMesaDrawable d )
1523 {
1524 XMesaBuffer b;
1525 for (b=XMesaBufferList; b; b=b->Next) {
1526 if (b->frontxrb->drawable == d && b->display == dpy) {
1527 return b;
1528 }
1529 }
1530 return NULL;
1531 }
1532
1533
1534 /**
1535 * Free/destroy all XMesaBuffers associated with given display.
1536 */
1537 void xmesa_destroy_buffers_on_display(XMesaDisplay *dpy)
1538 {
1539 XMesaBuffer b, next;
1540 for (b = XMesaBufferList; b; b = next) {
1541 next = b->Next;
1542 if (b->display == dpy) {
1543 xmesa_free_buffer(b);
1544 }
1545 }
1546 }
1547
1548
1549 /*
1550 * Look for XMesaBuffers whose X window has been destroyed.
1551 * Deallocate any such XMesaBuffers.
1552 */
1553 void XMesaGarbageCollect( XMesaDisplay* dpy )
1554 {
1555 XMesaBuffer b, next;
1556 for (b=XMesaBufferList; b; b=next) {
1557 next = b->Next;
1558 if (b->display && b->display == dpy && b->frontxrb->drawable && b->type == WINDOW) {
1559 XSync(b->display, False);
1560 if (!window_exists( b->display, b->frontxrb->drawable )) {
1561 /* found a dead window, free the ancillary info */
1562 XMesaDestroyBuffer( b );
1563 }
1564 }
1565 }
1566 }
1567
1568
1569 unsigned long XMesaDitherColor( XMesaContext xmesa, GLint x, GLint y,
1570 GLfloat red, GLfloat green,
1571 GLfloat blue, GLfloat alpha )
1572 {
1573 GLint r = (GLint) (red * 255.0F);
1574 GLint g = (GLint) (green * 255.0F);
1575 GLint b = (GLint) (blue * 255.0F);
1576 GLint a = (GLint) (alpha * 255.0F);
1577
1578 switch (xmesa->pixelformat) {
1579 case PF_Truecolor:
1580 {
1581 unsigned long p;
1582 PACK_TRUECOLOR( p, r, g, b );
1583 return p;
1584 }
1585 case PF_8A8B8G8R:
1586 return PACK_8A8B8G8R( r, g, b, a );
1587 case PF_8A8R8G8B:
1588 return PACK_8A8R8G8B( r, g, b, a );
1589 case PF_8R8G8B:
1590 return PACK_8R8G8B( r, g, b );
1591 case PF_5R6G5B:
1592 return PACK_5R6G5B( r, g, b );
1593 case PF_Dither_5R6G5B:
1594 /* fall through */
1595 case PF_Dither_True:
1596 {
1597 unsigned long p;
1598 PACK_TRUEDITHER(p, x, y, r, g, b);
1599 return p;
1600 }
1601 default:
1602 _mesa_problem(NULL, "Bad pixel format in XMesaDitherColor");
1603 }
1604 return 0;
1605 }
1606
1607
1608 /*
1609 * This is typically called when the window size changes and we need
1610 * to reallocate the buffer's back/depth/stencil/accum buffers.
1611 */
1612 PUBLIC void
1613 XMesaResizeBuffers( XMesaBuffer b )
1614 {
1615 GET_CURRENT_CONTEXT(ctx);
1616 XMesaContext xmctx = XMESA_CONTEXT(ctx);
1617 if (!xmctx)
1618 return;
1619 xmesa_check_and_update_buffer_size(xmctx, b);
1620 }
1621
1622
1623 static GLint
1624 xbuffer_to_renderbuffer(int buffer)
1625 {
1626 assert(MAX_AUX_BUFFERS <= 4);
1627
1628 switch (buffer) {
1629 case GLX_FRONT_LEFT_EXT:
1630 return BUFFER_FRONT_LEFT;
1631 case GLX_FRONT_RIGHT_EXT:
1632 return BUFFER_FRONT_RIGHT;
1633 case GLX_BACK_LEFT_EXT:
1634 return BUFFER_BACK_LEFT;
1635 case GLX_BACK_RIGHT_EXT:
1636 return BUFFER_BACK_RIGHT;
1637 case GLX_AUX0_EXT:
1638 return BUFFER_AUX0;
1639 case GLX_AUX1_EXT:
1640 case GLX_AUX2_EXT:
1641 case GLX_AUX3_EXT:
1642 case GLX_AUX4_EXT:
1643 case GLX_AUX5_EXT:
1644 case GLX_AUX6_EXT:
1645 case GLX_AUX7_EXT:
1646 case GLX_AUX8_EXT:
1647 case GLX_AUX9_EXT:
1648 default:
1649 /* BadValue error */
1650 return -1;
1651 }
1652 }
1653
1654
1655 PUBLIC void
1656 XMesaBindTexImage(XMesaDisplay *dpy, XMesaBuffer drawable, int buffer,
1657 const int *attrib_list)
1658 {
1659 #if 0
1660 GET_CURRENT_CONTEXT(ctx);
1661 const GLuint unit = ctx->Texture.CurrentUnit;
1662 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
1663 struct gl_texture_object *texObj;
1664 #endif
1665 struct gl_renderbuffer *rb;
1666 struct xmesa_renderbuffer *xrb;
1667 GLint b;
1668 XMesaImage *img = NULL;
1669 GLboolean freeImg = GL_FALSE;
1670
1671 b = xbuffer_to_renderbuffer(buffer);
1672 if (b < 0)
1673 return;
1674
1675 if (drawable->TextureFormat == GLX_TEXTURE_FORMAT_NONE_EXT)
1676 return; /* BadMatch error */
1677
1678 rb = drawable->mesa_buffer.Attachment[b].Renderbuffer;
1679 if (!rb) {
1680 /* invalid buffer */
1681 return;
1682 }
1683 xrb = xmesa_renderbuffer(rb);
1684
1685 #if 0
1686 switch (drawable->TextureTarget) {
1687 case GLX_TEXTURE_1D_EXT:
1688 texObj = texUnit->CurrentTex[TEXTURE_1D_INDEX];
1689 break;
1690 case GLX_TEXTURE_2D_EXT:
1691 texObj = texUnit->CurrentTex[TEXTURE_2D_INDEX];
1692 break;
1693 case GLX_TEXTURE_RECTANGLE_EXT:
1694 texObj = texUnit->CurrentTex[TEXTURE_RECT_INDEX];
1695 break;
1696 default:
1697 return; /* BadMatch error */
1698 }
1699 #endif
1700
1701 /*
1702 * The following is a quick and simple way to implement
1703 * BindTexImage. The better way is to write some new FetchTexel()
1704 * functions which would extract texels from XImages. We'd still
1705 * need to use GetImage when texturing from a Pixmap (front buffer)
1706 * but texturing from a back buffer (XImage) would avoid an image
1707 * copy.
1708 */
1709
1710 /* get XImage */
1711 if (xrb->pixmap) {
1712 img = XMesaGetImage(dpy, xrb->pixmap, 0, 0, rb->Width, rb->Height, ~0L,
1713 ZPixmap);
1714 freeImg = GL_TRUE;
1715 }
1716 else if (xrb->ximage) {
1717 img = xrb->ximage;
1718 }
1719
1720 /* store the XImage as a new texture image */
1721 if (img) {
1722 GLenum format, type, intFormat;
1723 if (img->bits_per_pixel == 32) {
1724 format = GL_BGRA;
1725 type = GL_UNSIGNED_BYTE;
1726 intFormat = GL_RGBA;
1727 }
1728 else if (img->bits_per_pixel == 24) {
1729 format = GL_BGR;
1730 type = GL_UNSIGNED_BYTE;
1731 intFormat = GL_RGB;
1732 }
1733 else if (img->bits_per_pixel == 16) {
1734 format = GL_BGR;
1735 type = GL_UNSIGNED_SHORT_5_6_5;
1736 intFormat = GL_RGB;
1737 }
1738 else {
1739 _mesa_problem(NULL, "Unexpected XImage format in XMesaBindTexImage");
1740 return;
1741 }
1742 if (drawable->TextureFormat == GLX_TEXTURE_FORMAT_RGBA_EXT) {
1743 intFormat = GL_RGBA;
1744 }
1745 else if (drawable->TextureFormat == GLX_TEXTURE_FORMAT_RGB_EXT) {
1746 intFormat = GL_RGB;
1747 }
1748
1749 _mesa_TexImage2D(GL_TEXTURE_2D, 0, intFormat, rb->Width, rb->Height, 0,
1750 format, type, img->data);
1751
1752 if (freeImg) {
1753 XMesaDestroyImage(img);
1754 }
1755 }
1756 }
1757
1758
1759
1760 PUBLIC void
1761 XMesaReleaseTexImage(XMesaDisplay *dpy, XMesaBuffer drawable, int buffer)
1762 {
1763 const GLint b = xbuffer_to_renderbuffer(buffer);
1764 if (b < 0)
1765 return;
1766
1767 /* no-op for now */
1768 }
1769