Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / gallium / winsys / xlib / 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.
52 *
53 */
54
55 #ifdef __CYGWIN__
56 #undef WIN32
57 #undef __WIN32__
58 #endif
59
60 #include "glxheader.h"
61 #include "GL/xmesa.h"
62 #include "xmesaP.h"
63 #include "main/context.h"
64 #include "main/framebuffer.h"
65
66 #include "state_tracker/st_public.h"
67 #include "state_tracker/st_context.h"
68 #include "pipe/p_defines.h"
69 #include "pipe/p_screen.h"
70 #include "pipe/p_context.h"
71
72 #include "xm_winsys_aub.h"
73
74 /**
75 * Global X driver lock
76 */
77 pipe_mutex _xmesa_lock;
78
79
80 int xmesa_mode;
81
82
83 /**********************************************************************/
84 /***** X Utility Functions *****/
85 /**********************************************************************/
86
87
88 /**
89 * Return the host's byte order as LSBFirst or MSBFirst ala X.
90 */
91 #ifndef XFree86Server
92 static int host_byte_order( void )
93 {
94 int i = 1;
95 char *cptr = (char *) &i;
96 return (*cptr==1) ? LSBFirst : MSBFirst;
97 }
98 #endif
99
100
101 /**
102 * Check if the X Shared Memory extension is available.
103 * Return: 0 = not available
104 * 1 = shared XImage support available
105 * 2 = shared Pixmap support available also
106 */
107 int xmesa_check_for_xshm( XMesaDisplay *display )
108 {
109 #if defined(USE_XSHM) && !defined(XFree86Server)
110 int major, minor, ignore;
111 Bool pixmaps;
112
113 if (getenv("SP_NO_RAST"))
114 return 0;
115
116 if (getenv("MESA_NOSHM")) {
117 return 0;
118 }
119
120 if (XQueryExtension( display, "MIT-SHM", &ignore, &ignore, &ignore )) {
121 if (XShmQueryVersion( display, &major, &minor, &pixmaps )==True) {
122 return (pixmaps==True) ? 2 : 1;
123 }
124 else {
125 return 0;
126 }
127 }
128 else {
129 return 0;
130 }
131 #else
132 /* No XSHM support */
133 return 0;
134 #endif
135 }
136
137
138 /**
139 * Return the true number of bits per pixel for XImages.
140 * For example, if we request a 24-bit deep visual we may actually need/get
141 * 32bpp XImages. This function returns the appropriate bpp.
142 * Input: dpy - the X display
143 * visinfo - desribes the visual to be used for XImages
144 * Return: true number of bits per pixel for XImages
145 */
146 static int
147 bits_per_pixel( XMesaVisual xmv )
148 {
149 #ifdef XFree86Server
150 const int depth = xmv->nplanes;
151 int i;
152 assert(depth > 0);
153 for (i = 0; i < screenInfo.numPixmapFormats; i++) {
154 if (screenInfo.formats[i].depth == depth)
155 return screenInfo.formats[i].bitsPerPixel;
156 }
157 return depth; /* should never get here, but this should be safe */
158 #else
159 XMesaDisplay *dpy = xmv->display;
160 XMesaVisualInfo visinfo = xmv->visinfo;
161 XMesaImage *img;
162 int bitsPerPixel;
163 /* Create a temporary XImage */
164 img = XCreateImage( dpy, visinfo->visual, visinfo->depth,
165 ZPixmap, 0, /*format, offset*/
166 (char*) MALLOC(8), /*data*/
167 1, 1, /*width, height*/
168 32, /*bitmap_pad*/
169 0 /*bytes_per_line*/
170 );
171 assert(img);
172 /* grab the bits/pixel value */
173 bitsPerPixel = img->bits_per_pixel;
174 /* free the XImage */
175 _mesa_free( img->data );
176 img->data = NULL;
177 XMesaDestroyImage( img );
178 return bitsPerPixel;
179 #endif
180 }
181
182
183
184 /*
185 * Determine if a given X window ID is valid (window exists).
186 * Do this by calling XGetWindowAttributes() for the window and
187 * checking if we catch an X error.
188 * Input: dpy - the display
189 * win - the window to check for existance
190 * Return: GL_TRUE - window exists
191 * GL_FALSE - window doesn't exist
192 */
193 #ifndef XFree86Server
194 static GLboolean WindowExistsFlag;
195
196 static int window_exists_err_handler( XMesaDisplay* dpy, XErrorEvent* xerr )
197 {
198 (void) dpy;
199 if (xerr->error_code == BadWindow) {
200 WindowExistsFlag = GL_FALSE;
201 }
202 return 0;
203 }
204
205 static GLboolean window_exists( XMesaDisplay *dpy, Window win )
206 {
207 XWindowAttributes wa;
208 int (*old_handler)( XMesaDisplay*, XErrorEvent* );
209 WindowExistsFlag = GL_TRUE;
210 old_handler = XSetErrorHandler(window_exists_err_handler);
211 XGetWindowAttributes( dpy, win, &wa ); /* dummy request */
212 XSetErrorHandler(old_handler);
213 return WindowExistsFlag;
214 }
215
216 static Status
217 get_drawable_size( XMesaDisplay *dpy, Drawable d, uint *width, uint *height )
218 {
219 Window root;
220 Status stat;
221 int xpos, ypos;
222 unsigned int w, h, bw, depth;
223 stat = XGetGeometry(dpy, d, &root, &xpos, &ypos, &w, &h, &bw, &depth);
224 *width = w;
225 *height = h;
226 return stat;
227 }
228 #endif
229
230
231 /**
232 * Return the size of the window (or pixmap) that corresponds to the
233 * given XMesaBuffer.
234 * \param width returns width in pixels
235 * \param height returns height in pixels
236 */
237 static void
238 xmesa_get_window_size(XMesaDisplay *dpy, XMesaBuffer b,
239 GLuint *width, GLuint *height)
240 {
241 #ifdef XFree86Server
242 *width = MIN2(b->drawable->width, MAX_WIDTH);
243 *height = MIN2(b->drawable->height, MAX_HEIGHT);
244 #else
245 Status stat;
246
247 pipe_mutex_lock(_xmesa_lock);
248 XSync(b->xm_visual->display, 0); /* added for Chromium */
249 stat = get_drawable_size(dpy, b->drawable, width, height);
250 pipe_mutex_unlock(_xmesa_lock);
251
252 if (!stat) {
253 /* probably querying a window that's recently been destroyed */
254 _mesa_warning(NULL, "XGetGeometry failed!\n");
255 *width = *height = 1;
256 }
257 #endif
258 }
259
260
261 /**
262 * Choose the pixel format for the given visual.
263 * This will tell the gallium driver how to pack pixel data into
264 * drawing surfaces.
265 */
266 static GLuint
267 choose_pixel_format(XMesaVisual v)
268 {
269 if ( GET_REDMASK(v) == 0x0000ff
270 && GET_GREENMASK(v) == 0x00ff00
271 && GET_BLUEMASK(v) == 0xff0000
272 && v->BitsPerPixel == 32) {
273 if (CHECK_BYTE_ORDER(v)) {
274 /* no byteswapping needed */
275 return 0 /* PIXEL_FORMAT_U_A8_B8_G8_R8 */;
276 }
277 else {
278 return PIPE_FORMAT_R8G8B8A8_UNORM;
279 }
280 }
281 else if ( GET_REDMASK(v) == 0xff0000
282 && GET_GREENMASK(v) == 0x00ff00
283 && GET_BLUEMASK(v) == 0x0000ff
284 && v->BitsPerPixel == 32) {
285 if (CHECK_BYTE_ORDER(v)) {
286 /* no byteswapping needed */
287 return PIPE_FORMAT_A8R8G8B8_UNORM;
288 }
289 else {
290 return PIPE_FORMAT_B8G8R8A8_UNORM;
291 }
292 }
293 else if ( GET_REDMASK(v) == 0xf800
294 && GET_GREENMASK(v) == 0x07e0
295 && GET_BLUEMASK(v) == 0x001f
296 && CHECK_BYTE_ORDER(v)
297 && v->BitsPerPixel == 16) {
298 /* 5-6-5 RGB */
299 return PIPE_FORMAT_R5G6B5_UNORM;
300 }
301
302 assert(0);
303 return 0;
304 }
305
306
307
308 /**********************************************************************/
309 /***** Linked list of XMesaBuffers *****/
310 /**********************************************************************/
311
312 XMesaBuffer XMesaBufferList = NULL;
313
314
315 /**
316 * Allocate a new XMesaBuffer object which corresponds to the given drawable.
317 * Note that XMesaBuffer is derived from GLframebuffer.
318 * The new XMesaBuffer will not have any size (Width=Height=0).
319 *
320 * \param d the corresponding X drawable (window or pixmap)
321 * \param type either WINDOW, PIXMAP or PBUFFER, describing d
322 * \param vis the buffer's visual
323 * \param cmap the window's colormap, if known.
324 * \return new XMesaBuffer or NULL if any problem
325 */
326 static XMesaBuffer
327 create_xmesa_buffer(XMesaDrawable d, BufferType type,
328 XMesaVisual vis, XMesaColormap cmap)
329 {
330 XMesaBuffer b;
331 GLframebuffer *fb;
332 enum pipe_format colorFormat, depthFormat, stencilFormat;
333 uint width, height;
334
335 ASSERT(type == WINDOW || type == PIXMAP || type == PBUFFER);
336
337 b = (XMesaBuffer) CALLOC_STRUCT(xmesa_buffer);
338 if (!b)
339 return NULL;
340
341 b->drawable = d;
342
343 b->xm_visual = vis;
344 b->type = type;
345 b->cmap = cmap;
346
347 /* determine PIPE_FORMATs for buffers */
348 colorFormat = choose_pixel_format(vis);
349
350 if (vis->mesa_visual.depthBits == 0)
351 depthFormat = PIPE_FORMAT_NONE;
352 #ifdef GALLIUM_CELL /* XXX temporary for Cell! */
353 else
354 depthFormat = PIPE_FORMAT_S8Z24_UNORM;
355 #else
356 else if (vis->mesa_visual.depthBits <= 16)
357 depthFormat = PIPE_FORMAT_Z16_UNORM;
358 else if (vis->mesa_visual.depthBits <= 24)
359 depthFormat = PIPE_FORMAT_S8Z24_UNORM;
360 else
361 depthFormat = PIPE_FORMAT_Z32_UNORM;
362 #endif
363
364 if (vis->mesa_visual.stencilBits == 8) {
365 if (depthFormat == PIPE_FORMAT_S8Z24_UNORM)
366 stencilFormat = depthFormat;
367 else
368 stencilFormat = PIPE_FORMAT_S8_UNORM;
369 }
370 else {
371 /* no stencil */
372 stencilFormat = PIPE_FORMAT_NONE;
373 if (depthFormat == PIPE_FORMAT_S8Z24_UNORM) {
374 /* use 24-bit Z, undefined stencil channel */
375 depthFormat = PIPE_FORMAT_X8Z24_UNORM;
376 }
377 }
378
379
380 get_drawable_size(vis->display, d, &width, &height);
381
382 /*
383 * Create framebuffer, but we'll plug in our own renderbuffers below.
384 */
385 b->stfb = st_create_framebuffer(&vis->mesa_visual,
386 colorFormat, depthFormat, stencilFormat,
387 width, height,
388 (void *) b);
389 fb = &b->stfb->Base;
390
391 /*
392 * Create scratch XImage for xmesa_display_surface()
393 */
394 b->tempImage = XCreateImage(vis->display,
395 vis->visinfo->visual,
396 vis->visinfo->depth,
397 ZPixmap, 0, /* format, offset */
398 NULL, /* data */
399 0, 0, /* size */
400 32, /* bitmap_pad */
401 0); /* bytes_per_line */
402
403 /* GLX_EXT_texture_from_pixmap */
404 b->TextureTarget = 0;
405 b->TextureFormat = GLX_TEXTURE_FORMAT_NONE_EXT;
406 b->TextureMipmap = 0;
407
408 /* insert buffer into linked list */
409 b->Next = XMesaBufferList;
410 XMesaBufferList = b;
411
412 return b;
413 }
414
415
416 /**
417 * Find an XMesaBuffer by matching X display and colormap but NOT matching
418 * the notThis buffer.
419 */
420 XMesaBuffer
421 xmesa_find_buffer(XMesaDisplay *dpy, XMesaColormap cmap, XMesaBuffer notThis)
422 {
423 XMesaBuffer b;
424 for (b = XMesaBufferList; b; b = b->Next) {
425 if (b->xm_visual->display == dpy &&
426 b->cmap == cmap &&
427 b != notThis) {
428 return b;
429 }
430 }
431 return NULL;
432 }
433
434
435 /**
436 * Remove buffer from linked list, delete if no longer referenced.
437 */
438 static void
439 xmesa_free_buffer(XMesaBuffer buffer)
440 {
441 XMesaBuffer prev = NULL, b;
442
443 for (b = XMesaBufferList; b; b = b->Next) {
444 if (b == buffer) {
445 struct gl_framebuffer *fb = &buffer->stfb->Base;
446
447 /* unlink buffer from list */
448 if (prev)
449 prev->Next = buffer->Next;
450 else
451 XMesaBufferList = buffer->Next;
452
453 /* mark as delete pending */
454 fb->DeletePending = GL_TRUE;
455
456 /* Since the X window for the XMesaBuffer is going away, we don't
457 * want to dereference this pointer in the future.
458 */
459 b->drawable = 0;
460
461 buffer->tempImage->data = NULL;
462 XDestroyImage(buffer->tempImage);
463
464 /* Unreference. If count = zero we'll really delete the buffer */
465 _mesa_unreference_framebuffer(&fb);
466
467 XFreeGC(b->xm_visual->display, b->gc);
468
469 free(buffer);
470
471 return;
472 }
473 /* continue search */
474 prev = b;
475 }
476 /* buffer not found in XMesaBufferList */
477 _mesa_problem(NULL,"xmesa_free_buffer() - buffer not found\n");
478 }
479
480
481
482 /**********************************************************************/
483 /***** Misc Private Functions *****/
484 /**********************************************************************/
485
486
487 /**
488 * When a context is bound for the first time, we can finally finish
489 * initializing the context's visual and buffer information.
490 * \param v the XMesaVisual to initialize
491 * \param b the XMesaBuffer to initialize (may be NULL)
492 * \param rgb_flag TRUE = RGBA mode, FALSE = color index mode
493 * \param window the window/pixmap we're rendering into
494 * \param cmap the colormap associated with the window/pixmap
495 * \return GL_TRUE=success, GL_FALSE=failure
496 */
497 static GLboolean
498 initialize_visual_and_buffer(XMesaVisual v, XMesaBuffer b,
499 GLboolean rgb_flag, XMesaDrawable window,
500 XMesaColormap cmap)
501 {
502 #ifdef XFree86Server
503 int client = (window) ? CLIENT_ID(window->id) : 0;
504 #endif
505
506 ASSERT(!b || b->xm_visual == v);
507
508 /* Save true bits/pixel */
509 v->BitsPerPixel = bits_per_pixel(v);
510 assert(v->BitsPerPixel > 0);
511
512 if (rgb_flag == GL_FALSE) {
513 /* COLOR-INDEXED WINDOW: not supported*/
514 return GL_FALSE;
515 }
516 else {
517 /* RGB WINDOW:
518 * We support RGB rendering into almost any kind of visual.
519 */
520 const int xclass = v->mesa_visual.visualType;
521 if (xclass != GLX_TRUE_COLOR && xclass == !GLX_DIRECT_COLOR) {
522 _mesa_warning(NULL,
523 "XMesa: RGB mode rendering not supported in given visual.\n");
524 return GL_FALSE;
525 }
526 v->mesa_visual.indexBits = 0;
527
528 if (v->BitsPerPixel == 32) {
529 /* We use XImages for all front/back buffers. If an X Window or
530 * X Pixmap is 32bpp, there's no guarantee that the alpha channel
531 * will be preserved. For XImages we're in luck.
532 */
533 v->mesa_visual.alphaBits = 8;
534 }
535 }
536
537 /*
538 * If MESA_INFO env var is set print out some debugging info
539 * which can help Brian figure out what's going on when a user
540 * reports bugs.
541 */
542 if (_mesa_getenv("MESA_INFO")) {
543 _mesa_printf("X/Mesa visual = %p\n", (void *) v);
544 _mesa_printf("X/Mesa level = %d\n", v->mesa_visual.level);
545 _mesa_printf("X/Mesa depth = %d\n", GET_VISUAL_DEPTH(v));
546 _mesa_printf("X/Mesa bits per pixel = %d\n", v->BitsPerPixel);
547 }
548
549 if (b && window) {
550 /* these should have been set in create_xmesa_buffer */
551 ASSERT(b->drawable == window);
552
553 /* Setup for single/double buffering */
554 if (v->mesa_visual.doubleBufferMode) {
555 /* Double buffered */
556 b->shm = xmesa_check_for_xshm( v->display );
557 }
558
559 /* X11 graphics context */
560 #ifdef XFree86Server
561 b->gc = CreateScratchGC(v->display, window->depth);
562 #else
563 b->gc = XCreateGC( v->display, window, 0, NULL );
564 #endif
565 XMesaSetFunction( v->display, b->gc, GXcopy );
566 }
567
568 return GL_TRUE;
569 }
570
571
572
573 #define NUM_VISUAL_TYPES 6
574
575 /**
576 * Convert an X visual type to a GLX visual type.
577 *
578 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
579 * to be converted.
580 * \return If \c visualType is a valid X visual type, a GLX visual type will
581 * be returned. Otherwise \c GLX_NONE will be returned.
582 *
583 * \note
584 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
585 * DRI CVS tree.
586 */
587 static GLint
588 xmesa_convert_from_x_visual_type( int visualType )
589 {
590 static const int glx_visual_types[ NUM_VISUAL_TYPES ] = {
591 GLX_STATIC_GRAY, GLX_GRAY_SCALE,
592 GLX_STATIC_COLOR, GLX_PSEUDO_COLOR,
593 GLX_TRUE_COLOR, GLX_DIRECT_COLOR
594 };
595
596 return ( (unsigned) visualType < NUM_VISUAL_TYPES )
597 ? glx_visual_types[ visualType ] : GLX_NONE;
598 }
599
600
601 /**********************************************************************/
602 /***** Public Functions *****/
603 /**********************************************************************/
604
605
606 /*
607 * Create a new X/Mesa visual.
608 * Input: display - X11 display
609 * visinfo - an XVisualInfo pointer
610 * rgb_flag - GL_TRUE = RGB mode,
611 * GL_FALSE = color index mode
612 * alpha_flag - alpha buffer requested?
613 * db_flag - GL_TRUE = double-buffered,
614 * GL_FALSE = single buffered
615 * stereo_flag - stereo visual?
616 * ximage_flag - GL_TRUE = use an XImage for back buffer,
617 * GL_FALSE = use an off-screen pixmap for back buffer
618 * depth_size - requested bits/depth values, or zero
619 * stencil_size - requested bits/stencil values, or zero
620 * accum_red_size - requested bits/red accum values, or zero
621 * accum_green_size - requested bits/green accum values, or zero
622 * accum_blue_size - requested bits/blue accum values, or zero
623 * accum_alpha_size - requested bits/alpha accum values, or zero
624 * num_samples - number of samples/pixel if multisampling, or zero
625 * level - visual level, usually 0
626 * visualCaveat - ala the GLX extension, usually GLX_NONE
627 * Return; a new XMesaVisual or 0 if error.
628 */
629 PUBLIC
630 XMesaVisual XMesaCreateVisual( XMesaDisplay *display,
631 XMesaVisualInfo visinfo,
632 GLboolean rgb_flag,
633 GLboolean alpha_flag,
634 GLboolean db_flag,
635 GLboolean stereo_flag,
636 GLboolean ximage_flag,
637 GLint depth_size,
638 GLint stencil_size,
639 GLint accum_red_size,
640 GLint accum_green_size,
641 GLint accum_blue_size,
642 GLint accum_alpha_size,
643 GLint num_samples,
644 GLint level,
645 GLint visualCaveat )
646 {
647 XMesaVisual v;
648 GLint red_bits, green_bits, blue_bits, alpha_bits;
649
650 #ifndef XFree86Server
651 /* For debugging only */
652 if (_mesa_getenv("MESA_XSYNC")) {
653 /* This makes debugging X easier.
654 * In your debugger, set a breakpoint on _XError to stop when an
655 * X protocol error is generated.
656 */
657 XSynchronize( display, 1 );
658 }
659 #endif
660
661 v = (XMesaVisual) CALLOC_STRUCT(xmesa_visual);
662 if (!v) {
663 return NULL;
664 }
665
666 v->display = display;
667
668 /* Save a copy of the XVisualInfo struct because the user may X_mesa_free()
669 * the struct but we may need some of the information contained in it
670 * at a later time.
671 */
672 #ifndef XFree86Server
673 v->visinfo = (XVisualInfo *) MALLOC(sizeof(*visinfo));
674 if(!v->visinfo) {
675 _mesa_free(v);
676 return NULL;
677 }
678 MEMCPY(v->visinfo, visinfo, sizeof(*visinfo));
679 #endif
680
681 v->ximage_flag = ximage_flag;
682
683 #ifdef XFree86Server
684 /* We could calculate these values by ourselves. nplanes is either the sum
685 * of the red, green, and blue bits or the number index bits.
686 * ColormapEntries is either (1U << index_bits) or
687 * (1U << max(redBits, greenBits, blueBits)).
688 */
689 assert(visinfo->nplanes > 0);
690 v->nplanes = visinfo->nplanes;
691 v->ColormapEntries = visinfo->ColormapEntries;
692
693 v->mesa_visual.redMask = visinfo->redMask;
694 v->mesa_visual.greenMask = visinfo->greenMask;
695 v->mesa_visual.blueMask = visinfo->blueMask;
696 v->mesa_visual.visualID = visinfo->vid;
697 v->mesa_visual.screen = 0; /* FIXME: What should be done here? */
698 #else
699 v->mesa_visual.redMask = visinfo->red_mask;
700 v->mesa_visual.greenMask = visinfo->green_mask;
701 v->mesa_visual.blueMask = visinfo->blue_mask;
702 v->mesa_visual.visualID = visinfo->visualid;
703 v->mesa_visual.screen = visinfo->screen;
704 #endif
705
706 #if defined(XFree86Server) || !(defined(__cplusplus) || defined(c_plusplus))
707 v->mesa_visual.visualType = xmesa_convert_from_x_visual_type(visinfo->class);
708 #else
709 v->mesa_visual.visualType = xmesa_convert_from_x_visual_type(visinfo->c_class);
710 #endif
711
712 v->mesa_visual.visualRating = visualCaveat;
713
714 if (alpha_flag)
715 v->mesa_visual.alphaBits = 8;
716
717 (void) initialize_visual_and_buffer( v, NULL, rgb_flag, 0, 0 );
718
719 {
720 const int xclass = v->mesa_visual.visualType;
721 if (xclass == GLX_TRUE_COLOR || xclass == GLX_DIRECT_COLOR) {
722 red_bits = _mesa_bitcount(GET_REDMASK(v));
723 green_bits = _mesa_bitcount(GET_GREENMASK(v));
724 blue_bits = _mesa_bitcount(GET_BLUEMASK(v));
725 }
726 else {
727 /* this is an approximation */
728 int depth;
729 depth = GET_VISUAL_DEPTH(v);
730 red_bits = depth / 3;
731 depth -= red_bits;
732 green_bits = depth / 2;
733 depth -= green_bits;
734 blue_bits = depth;
735 alpha_bits = 0;
736 assert( red_bits + green_bits + blue_bits == GET_VISUAL_DEPTH(v) );
737 }
738 alpha_bits = v->mesa_visual.alphaBits;
739 }
740
741 _mesa_initialize_visual( &v->mesa_visual,
742 rgb_flag, db_flag, stereo_flag,
743 red_bits, green_bits,
744 blue_bits, alpha_bits,
745 v->mesa_visual.indexBits,
746 depth_size,
747 stencil_size,
748 accum_red_size, accum_green_size,
749 accum_blue_size, accum_alpha_size,
750 0 );
751
752 /* XXX minor hack */
753 v->mesa_visual.level = level;
754 return v;
755 }
756
757
758 PUBLIC
759 void XMesaDestroyVisual( XMesaVisual v )
760 {
761 #ifndef XFree86Server
762 _mesa_free(v->visinfo);
763 #endif
764 _mesa_free(v);
765 }
766
767
768
769 /**
770 * Create a new XMesaContext.
771 * \param v the XMesaVisual
772 * \param share_list another XMesaContext with which to share display
773 * lists or NULL if no sharing is wanted.
774 * \return an XMesaContext or NULL if error.
775 */
776 PUBLIC
777 XMesaContext XMesaCreateContext( XMesaVisual v, XMesaContext share_list )
778 {
779 static GLboolean firstTime = GL_TRUE;
780 struct pipe_context *pipe;
781 XMesaContext c;
782 GLcontext *mesaCtx;
783 uint pf;
784
785 if (firstTime) {
786 pipe_mutex_init(_xmesa_lock);
787 firstTime = GL_FALSE;
788 }
789
790 /* Note: the XMesaContext contains a Mesa GLcontext struct (inheritance) */
791 c = (XMesaContext) CALLOC_STRUCT(xmesa_context);
792 if (!c)
793 return NULL;
794
795 pf = choose_pixel_format(v);
796 assert(pf);
797
798 c->xm_visual = v;
799 c->xm_buffer = NULL; /* set later by XMesaMakeCurrent */
800
801 if (!getenv("XM_AUB")) {
802 xmesa_mode = XMESA_SOFTPIPE;
803 pipe = xmesa_create_pipe_context( c, pf );
804 }
805 else {
806 xmesa_mode = XMESA_AUB;
807 pipe = xmesa_create_i965simple(xmesa_get_pipe_winsys_aub(v));
808 }
809
810 if (pipe == NULL)
811 goto fail;
812
813 c->st = st_create_context(pipe, &v->mesa_visual,
814 share_list ? share_list->st : NULL);
815 if (c->st == NULL)
816 goto fail;
817
818 mesaCtx = c->st->ctx;
819 c->st->ctx->DriverCtx = c;
820
821 #if 00
822 _mesa_enable_sw_extensions(mesaCtx);
823 _mesa_enable_1_3_extensions(mesaCtx);
824 _mesa_enable_1_4_extensions(mesaCtx);
825 _mesa_enable_1_5_extensions(mesaCtx);
826 _mesa_enable_2_0_extensions(mesaCtx);
827 #endif
828
829 #ifdef XFree86Server
830 /* If we're running in the X server, do bounds checking to prevent
831 * segfaults and server crashes!
832 */
833 mesaCtx->Const.CheckArrayBounds = GL_TRUE;
834 #endif
835
836 return c;
837
838 fail:
839 if (c->st)
840 st_destroy_context(c->st);
841 else if (pipe)
842 pipe->destroy(pipe);
843 FREE(c);
844 return NULL;
845 }
846
847
848
849 PUBLIC
850 void XMesaDestroyContext( XMesaContext c )
851 {
852 struct pipe_screen *screen = c->st->pipe->screen;
853 st_destroy_context(c->st);
854 /* FIXME: We should destroy the screen here, but if we do so, surfaces may
855 * outlive it, causing segfaults
856 screen->destroy(screen);
857 */
858 _mesa_free(c);
859 }
860
861
862
863 /**
864 * Private function for creating an XMesaBuffer which corresponds to an
865 * X window or pixmap.
866 * \param v the window's XMesaVisual
867 * \param w the window we're wrapping
868 * \return new XMesaBuffer or NULL if error
869 */
870 PUBLIC XMesaBuffer
871 XMesaCreateWindowBuffer(XMesaVisual v, XMesaWindow w)
872 {
873 #ifndef XFree86Server
874 XWindowAttributes attr;
875 #endif
876 XMesaBuffer b;
877 XMesaColormap cmap;
878 int depth;
879
880 assert(v);
881 assert(w);
882
883 /* Check that window depth matches visual depth */
884 #ifdef XFree86Server
885 depth = ((XMesaDrawable)w)->depth;
886 #else
887 XGetWindowAttributes( v->display, w, &attr );
888 depth = attr.depth;
889 #endif
890 if (GET_VISUAL_DEPTH(v) != depth) {
891 _mesa_warning(NULL, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
892 GET_VISUAL_DEPTH(v), depth);
893 return NULL;
894 }
895
896 /* Find colormap */
897 #ifdef XFree86Server
898 cmap = (ColormapPtr)LookupIDByType(wColormap(w), RT_COLORMAP);
899 #else
900 if (attr.colormap) {
901 cmap = attr.colormap;
902 }
903 else {
904 _mesa_warning(NULL, "Window %u has no colormap!\n", (unsigned int) w);
905 /* this is weird, a window w/out a colormap!? */
906 /* OK, let's just allocate a new one and hope for the best */
907 cmap = XCreateColormap(v->display, w, attr.visual, AllocNone);
908 }
909 #endif
910
911 b = create_xmesa_buffer((XMesaDrawable) w, WINDOW, v, cmap);
912 if (!b)
913 return NULL;
914
915 if (!initialize_visual_and_buffer( v, b, v->mesa_visual.rgbMode,
916 (XMesaDrawable) w, cmap )) {
917 xmesa_free_buffer(b);
918 return NULL;
919 }
920
921 return b;
922 }
923
924
925
926 /**
927 * Create a new XMesaBuffer from an X pixmap.
928 *
929 * \param v the XMesaVisual
930 * \param p the pixmap
931 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
932 * \c GLX_DIRECT_COLOR visual for the pixmap
933 * \returns new XMesaBuffer or NULL if error
934 */
935 PUBLIC XMesaBuffer
936 XMesaCreatePixmapBuffer(XMesaVisual v, XMesaPixmap p, XMesaColormap cmap)
937 {
938 XMesaBuffer b;
939
940 assert(v);
941
942 b = create_xmesa_buffer((XMesaDrawable) p, PIXMAP, v, cmap);
943 if (!b)
944 return NULL;
945
946 if (!initialize_visual_and_buffer(v, b, v->mesa_visual.rgbMode,
947 (XMesaDrawable) p, cmap)) {
948 xmesa_free_buffer(b);
949 return NULL;
950 }
951
952 return b;
953 }
954
955
956 /**
957 * For GLX_EXT_texture_from_pixmap
958 */
959 XMesaBuffer
960 XMesaCreatePixmapTextureBuffer(XMesaVisual v, XMesaPixmap p,
961 XMesaColormap cmap,
962 int format, int target, int mipmap)
963 {
964 GET_CURRENT_CONTEXT(ctx);
965 XMesaBuffer b;
966 GLuint width, height;
967
968 assert(v);
969
970 b = create_xmesa_buffer((XMesaDrawable) p, PIXMAP, v, cmap);
971 if (!b)
972 return NULL;
973
974 /* get pixmap size, update framebuffer/renderbuffer dims */
975 xmesa_get_window_size(v->display, b, &width, &height);
976 _mesa_resize_framebuffer(NULL, &(b->stfb->Base), width, height);
977
978 if (target == 0) {
979 /* examine dims */
980 if (ctx->Extensions.ARB_texture_non_power_of_two) {
981 target = GLX_TEXTURE_2D_EXT;
982 }
983 else if ( _mesa_bitcount(width) == 1
984 && _mesa_bitcount(height) == 1) {
985 /* power of two size */
986 if (height == 1) {
987 target = GLX_TEXTURE_1D_EXT;
988 }
989 else {
990 target = GLX_TEXTURE_2D_EXT;
991 }
992 }
993 else if (ctx->Extensions.NV_texture_rectangle) {
994 target = GLX_TEXTURE_RECTANGLE_EXT;
995 }
996 else {
997 /* non power of two textures not supported */
998 XMesaDestroyBuffer(b);
999 return 0;
1000 }
1001 }
1002
1003 b->TextureTarget = target;
1004 b->TextureFormat = format;
1005 b->TextureMipmap = mipmap;
1006
1007 if (!initialize_visual_and_buffer(v, b, v->mesa_visual.rgbMode,
1008 (XMesaDrawable) p, cmap)) {
1009 xmesa_free_buffer(b);
1010 return NULL;
1011 }
1012
1013 return b;
1014 }
1015
1016
1017
1018 XMesaBuffer
1019 XMesaCreatePBuffer(XMesaVisual v, XMesaColormap cmap,
1020 unsigned int width, unsigned int height)
1021 {
1022 #ifndef XFree86Server
1023 XMesaWindow root;
1024 XMesaDrawable drawable; /* X Pixmap Drawable */
1025 XMesaBuffer b;
1026
1027 /* allocate pixmap for front buffer */
1028 root = RootWindow( v->display, v->visinfo->screen );
1029 drawable = XCreatePixmap(v->display, root, width, height,
1030 v->visinfo->depth);
1031 if (!drawable)
1032 return NULL;
1033
1034 b = create_xmesa_buffer(drawable, PBUFFER, v, cmap);
1035 if (!b)
1036 return NULL;
1037
1038 if (!initialize_visual_and_buffer(v, b, v->mesa_visual.rgbMode,
1039 drawable, cmap)) {
1040 xmesa_free_buffer(b);
1041 return NULL;
1042 }
1043
1044 return b;
1045 #else
1046 return 0;
1047 #endif
1048 }
1049
1050
1051
1052 /*
1053 * Deallocate an XMesaBuffer structure and all related info.
1054 */
1055 PUBLIC void
1056 XMesaDestroyBuffer(XMesaBuffer b)
1057 {
1058 xmesa_free_buffer(b);
1059 }
1060
1061
1062 /**
1063 * Query the current window size and update the corresponding GLframebuffer
1064 * and all attached renderbuffers.
1065 * Called when:
1066 * 1. the first time a buffer is bound to a context.
1067 * 2. from the XMesaResizeBuffers() API function.
1068 * 3. SwapBuffers. XXX probabaly from xm_flush_frontbuffer() too...
1069 * Note: it's possible (and legal) for xmctx to be NULL. That can happen
1070 * when resizing a buffer when no rendering context is bound.
1071 */
1072 void
1073 xmesa_check_and_update_buffer_size(XMesaContext xmctx, XMesaBuffer drawBuffer)
1074 {
1075 GLuint width, height;
1076 xmesa_get_window_size(drawBuffer->xm_visual->display, drawBuffer, &width, &height);
1077 st_resize_framebuffer(drawBuffer->stfb, width, height);
1078 }
1079
1080
1081 /*
1082 * Bind buffer b to context c and make c the current rendering context.
1083 */
1084 GLboolean XMesaMakeCurrent( XMesaContext c, XMesaBuffer b )
1085 {
1086 return XMesaMakeCurrent2( c, b, b );
1087 }
1088
1089
1090 /*
1091 * Bind buffer b to context c and make c the current rendering context.
1092 */
1093 PUBLIC
1094 GLboolean XMesaMakeCurrent2( XMesaContext c, XMesaBuffer drawBuffer,
1095 XMesaBuffer readBuffer )
1096 {
1097 if (c) {
1098 if (!drawBuffer || !readBuffer)
1099 return GL_FALSE; /* must specify buffers! */
1100
1101 #if 0
1102 /* XXX restore this optimization */
1103 if (&(c->mesa) == _mesa_get_current_context()
1104 && c->mesa.DrawBuffer == &drawBuffer->mesa_buffer
1105 && c->mesa.ReadBuffer == &readBuffer->mesa_buffer
1106 && xmesa_buffer(c->mesa.DrawBuffer)->wasCurrent) {
1107 /* same context and buffer, do nothing */
1108 return GL_TRUE;
1109 }
1110 #endif
1111
1112 c->xm_buffer = drawBuffer;
1113
1114 /* Call this periodically to detect when the user has begun using
1115 * GL rendering from multiple threads.
1116 */
1117 _glapi_check_multithread();
1118
1119 st_make_current(c->st, drawBuffer->stfb, readBuffer->stfb);
1120
1121 xmesa_check_and_update_buffer_size(c, drawBuffer);
1122 if (readBuffer != drawBuffer)
1123 xmesa_check_and_update_buffer_size(c, readBuffer);
1124
1125 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1126 drawBuffer->wasCurrent = GL_TRUE;
1127 }
1128 else {
1129 /* Detach */
1130 st_make_current( NULL, NULL, NULL );
1131 }
1132 return GL_TRUE;
1133 }
1134
1135
1136 /*
1137 * Unbind the context c from its buffer.
1138 */
1139 GLboolean XMesaUnbindContext( XMesaContext c )
1140 {
1141 /* A no-op for XFree86 integration purposes */
1142 return GL_TRUE;
1143 }
1144
1145
1146 XMesaContext XMesaGetCurrentContext( void )
1147 {
1148 GET_CURRENT_CONTEXT(ctx);
1149 if (ctx) {
1150 XMesaContext xmesa = xmesa_context(ctx);
1151 return xmesa;
1152 }
1153 else {
1154 return 0;
1155 }
1156 }
1157
1158
1159 XMesaBuffer XMesaGetCurrentBuffer( void )
1160 {
1161 GET_CURRENT_CONTEXT(ctx);
1162 if (ctx) {
1163 XMesaBuffer xmbuf = xmesa_buffer(ctx->DrawBuffer);
1164 return xmbuf;
1165 }
1166 else {
1167 return 0;
1168 }
1169 }
1170
1171
1172 /* New in Mesa 3.1 */
1173 XMesaBuffer XMesaGetCurrentReadBuffer( void )
1174 {
1175 GET_CURRENT_CONTEXT(ctx);
1176 if (ctx) {
1177 return xmesa_buffer(ctx->ReadBuffer);
1178 }
1179 else {
1180 return 0;
1181 }
1182 }
1183
1184
1185 #ifdef XFree86Server
1186 PUBLIC
1187 GLboolean XMesaForceCurrent(XMesaContext c)
1188 {
1189 if (c) {
1190 _glapi_set_dispatch(c->mesa.CurrentDispatch);
1191
1192 if (&(c->mesa) != _mesa_get_current_context()) {
1193 _mesa_make_current(&c->mesa, c->mesa.DrawBuffer, c->mesa.ReadBuffer);
1194 }
1195 }
1196 else {
1197 _mesa_make_current(NULL, NULL, NULL);
1198 }
1199 return GL_TRUE;
1200 }
1201
1202
1203 PUBLIC
1204 GLboolean XMesaLoseCurrent(XMesaContext c)
1205 {
1206 (void) c;
1207 _mesa_make_current(NULL, NULL, NULL);
1208 return GL_TRUE;
1209 }
1210
1211
1212 PUBLIC
1213 GLboolean XMesaCopyContext( XMesaContext xm_src, XMesaContext xm_dst, GLuint mask )
1214 {
1215 _mesa_copy_context(&xm_src->mesa, &xm_dst->mesa, mask);
1216 return GL_TRUE;
1217 }
1218 #endif /* XFree86Server */
1219
1220
1221 #ifndef FX
1222 GLboolean XMesaSetFXmode( GLint mode )
1223 {
1224 (void) mode;
1225 return GL_FALSE;
1226 }
1227 #endif
1228
1229
1230
1231 /*
1232 * Copy the back buffer to the front buffer. If there's no back buffer
1233 * this is a no-op.
1234 */
1235 PUBLIC
1236 void XMesaSwapBuffers( XMesaBuffer b )
1237 {
1238 struct pipe_surface *surf;
1239
1240 /* If we're swapping the buffer associated with the current context
1241 * we have to flush any pending rendering commands first.
1242 */
1243 st_notify_swapbuffers(b->stfb);
1244
1245 surf = st_get_framebuffer_surface(b->stfb, ST_SURFACE_BACK_LEFT);
1246 if (surf) {
1247 if (xmesa_mode == XMESA_AUB)
1248 xmesa_display_aub( surf );
1249 else
1250 xmesa_display_surface(b, surf);
1251 }
1252
1253 xmesa_check_and_update_buffer_size(NULL, b);
1254 }
1255
1256
1257
1258 /*
1259 * Copy sub-region of back buffer to front buffer
1260 */
1261 void XMesaCopySubBuffer( XMesaBuffer b, int x, int y, int width, int height )
1262 {
1263 struct pipe_surface *surf_front
1264 = st_get_framebuffer_surface(b->stfb, ST_SURFACE_FRONT_LEFT);
1265 struct pipe_surface *surf_back
1266 = st_get_framebuffer_surface(b->stfb, ST_SURFACE_BACK_LEFT);
1267 struct pipe_context *pipe = NULL; /* XXX fix */
1268
1269 if (!surf_front || !surf_back)
1270 return;
1271
1272 pipe->surface_copy(pipe,
1273 FALSE,
1274 surf_front, x, y, /* dest */
1275 surf_back, x, y, /* src */
1276 width, height);
1277 }
1278
1279
1280
1281 /*
1282 * Return the depth buffer associated with an XMesaBuffer.
1283 * Input: b - the XMesa buffer handle
1284 * Output: width, height - size of buffer in pixels
1285 * bytesPerValue - bytes per depth value (2 or 4)
1286 * buffer - pointer to depth buffer values
1287 * Return: GL_TRUE or GL_FALSE to indicate success or failure.
1288 */
1289 GLboolean XMesaGetDepthBuffer( XMesaBuffer b, GLint *width, GLint *height,
1290 GLint *bytesPerValue, void **buffer )
1291 {
1292 *width = 0;
1293 *height = 0;
1294 *bytesPerValue = 0;
1295 *buffer = 0;
1296 return GL_FALSE;
1297 }
1298
1299
1300 void XMesaFlush( XMesaContext c )
1301 {
1302 if (c && c->xm_visual->display) {
1303 #ifdef XFree86Server
1304 /* NOT_NEEDED */
1305 #else
1306 st_finish(c->st);
1307 XSync( c->xm_visual->display, False );
1308 #endif
1309 }
1310 }
1311
1312
1313
1314 const char *XMesaGetString( XMesaContext c, int name )
1315 {
1316 (void) c;
1317 if (name==XMESA_VERSION) {
1318 return "5.0";
1319 }
1320 else if (name==XMESA_EXTENSIONS) {
1321 return "";
1322 }
1323 else {
1324 return NULL;
1325 }
1326 }
1327
1328
1329
1330 XMesaBuffer XMesaFindBuffer( XMesaDisplay *dpy, XMesaDrawable d )
1331 {
1332 XMesaBuffer b;
1333 for (b=XMesaBufferList; b; b=b->Next) {
1334 if (b->drawable == d && b->xm_visual->display == dpy) {
1335 return b;
1336 }
1337 }
1338 return NULL;
1339 }
1340
1341
1342 /**
1343 * Free/destroy all XMesaBuffers associated with given display.
1344 */
1345 void xmesa_destroy_buffers_on_display(XMesaDisplay *dpy)
1346 {
1347 XMesaBuffer b, next;
1348 for (b = XMesaBufferList; b; b = next) {
1349 next = b->Next;
1350 if (b->xm_visual->display == dpy) {
1351 xmesa_free_buffer(b);
1352 }
1353 }
1354 }
1355
1356
1357 /*
1358 * Look for XMesaBuffers whose X window has been destroyed.
1359 * Deallocate any such XMesaBuffers.
1360 */
1361 void XMesaGarbageCollect( void )
1362 {
1363 XMesaBuffer b, next;
1364 for (b=XMesaBufferList; b; b=next) {
1365 next = b->Next;
1366 if (b->xm_visual &&
1367 b->xm_visual->display &&
1368 b->drawable &&
1369 b->type == WINDOW) {
1370 #ifdef XFree86Server
1371 /* NOT_NEEDED */
1372 #else
1373 XSync(b->xm_visual->display, False);
1374 if (!window_exists( b->xm_visual->display, b->drawable )) {
1375 /* found a dead window, free the ancillary info */
1376 XMesaDestroyBuffer( b );
1377 }
1378 #endif
1379 }
1380 }
1381 }
1382
1383
1384 unsigned long XMesaDitherColor( XMesaContext xmesa, GLint x, GLint y,
1385 GLfloat red, GLfloat green,
1386 GLfloat blue, GLfloat alpha )
1387 {
1388 /* no longer supported */
1389 return 0;
1390 }
1391
1392
1393 /*
1394 * This is typically called when the window size changes and we need
1395 * to reallocate the buffer's back/depth/stencil/accum buffers.
1396 */
1397 PUBLIC void
1398 XMesaResizeBuffers( XMesaBuffer b )
1399 {
1400 GET_CURRENT_CONTEXT(ctx);
1401 XMesaContext xmctx = xmesa_context(ctx);
1402 if (!xmctx)
1403 return;
1404 xmesa_check_and_update_buffer_size(xmctx, b);
1405 }
1406
1407
1408
1409
1410 PUBLIC void
1411 XMesaBindTexImage(XMesaDisplay *dpy, XMesaBuffer drawable, int buffer,
1412 const int *attrib_list)
1413 {
1414 }
1415
1416
1417
1418 PUBLIC void
1419 XMesaReleaseTexImage(XMesaDisplay *dpy, XMesaBuffer drawable, int buffer)
1420 {
1421 }
1422