st/glx: Fix compilation error
[mesa.git] / src / gallium / state_trackers / glx / 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 *
39 * Byte swapping: If the Mesa host and the X display use a different
40 * byte order then there's some trickiness to be aware of when using
41 * XImages. The byte ordering used for the XImage is that of the X
42 * display, not the Mesa host.
43 * The color-to-pixel encoding for True/DirectColor must be done
44 * according to the display's visual red_mask, green_mask, and blue_mask.
45 * If XPutPixel is used to put a pixel into an XImage then XPutPixel will
46 * do byte swapping if needed. If one wants to directly "poke" the pixel
47 * into the XImage's buffer then the pixel must be byte swapped first.
48 *
49 */
50
51 #ifdef __CYGWIN__
52 #undef WIN32
53 #undef __WIN32__
54 #endif
55
56 #include "xm_api.h"
57 #include "xm_st.h"
58
59 #include "pipe/p_defines.h"
60 #include "pipe/p_screen.h"
61 #include "pipe/p_context.h"
62 #include "util/u_atomic.h"
63
64 #include "xm_public.h"
65 #include <GL/glx.h>
66
67
68 /* Driver interface routines, set up by xlib backend on library
69 * _init(). These are global in the same way that function names are
70 * global.
71 */
72 static struct xm_driver driver;
73 static struct st_api *stapi;
74
75 /* Default strict invalidate to false. This means we will not call
76 * XGetGeometry after every swapbuffers, which allows swapbuffers to
77 * remain asynchronous. For apps running at 100fps with synchronous
78 * swapping, a 10% boost is typical. For gears, I see closer to 20%
79 * speedup.
80 *
81 * Note that the work of copying data on swapbuffers doesn't disappear
82 * - this change just allows the X server to execute the PutImage
83 * asynchronously without us effectively blocked until its completion.
84 *
85 * This speeds up even llvmpipe's threaded rasterization as the
86 * swapbuffers operation was a large part of the serial component of
87 * an llvmpipe frame.
88 *
89 * The downside of this is correctness - applications which don't call
90 * glViewport on window resizes will get incorrect rendering. A
91 * better solution would be to have per-frame but asynchronous
92 * invalidation. Xcb almost looks as if it could provide this, but
93 * the API doesn't seem to quite be there.
94 */
95 boolean xmesa_strict_invalidate = FALSE;
96
97 void xmesa_set_driver( const struct xm_driver *templ )
98 {
99 driver = *templ;
100 stapi = driver.create_st_api();
101
102 xmesa_strict_invalidate =
103 debug_get_bool_option("XMESA_STRICT_INVALIDATE", FALSE);
104 }
105
106
107 /*
108 * XXX replace this with a linked list, or better yet, try to attach the
109 * gallium/mesa extra bits to the X Display object with XAddExtension().
110 */
111 #define MAX_DISPLAYS 10
112 static struct xmesa_display Displays[MAX_DISPLAYS];
113 static int NumDisplays = 0;
114
115 static int
116 xmesa_get_param(struct st_manager *smapi,
117 enum st_manager_param param)
118 {
119 switch(param) {
120 case ST_MANAGER_BROKEN_INVALIDATE:
121 return !xmesa_strict_invalidate;
122 default:
123 return 0;
124 }
125 }
126
127 static XMesaDisplay
128 xmesa_init_display( Display *display )
129 {
130 pipe_static_mutex(init_mutex);
131 XMesaDisplay xmdpy;
132 int i;
133
134 pipe_mutex_lock(init_mutex);
135
136 /* Look for XMesaDisplay which corresponds to 'display' */
137 for (i = 0; i < NumDisplays; i++) {
138 if (Displays[i].display == display) {
139 /* Found it */
140 pipe_mutex_unlock(init_mutex);
141 return &Displays[i];
142 }
143 }
144
145 /* Create new XMesaDisplay */
146
147 assert(NumDisplays < MAX_DISPLAYS);
148 xmdpy = &Displays[NumDisplays];
149 NumDisplays++;
150
151 if (!xmdpy->display && display) {
152 xmdpy->display = display;
153 xmdpy->screen = driver.create_pipe_screen(display);
154 xmdpy->smapi = CALLOC_STRUCT(st_manager);
155 if (xmdpy->smapi) {
156 xmdpy->smapi->screen = xmdpy->screen;
157 xmdpy->smapi->get_param = xmesa_get_param;
158 }
159
160 if (xmdpy->screen && xmdpy->smapi) {
161 pipe_mutex_init(xmdpy->mutex);
162 }
163 else {
164 if (xmdpy->screen) {
165 xmdpy->screen->destroy(xmdpy->screen);
166 xmdpy->screen = NULL;
167 }
168 if (xmdpy->smapi) {
169 FREE(xmdpy->smapi);
170 xmdpy->smapi = NULL;
171 }
172
173 xmdpy->display = NULL;
174 }
175 }
176 if (!xmdpy->display || xmdpy->display != display)
177 xmdpy = NULL;
178
179 pipe_mutex_unlock(init_mutex);
180
181 return xmdpy;
182 }
183
184 /**********************************************************************/
185 /***** X Utility Functions *****/
186 /**********************************************************************/
187
188
189 /**
190 * Return the host's byte order as LSBFirst or MSBFirst ala X.
191 */
192 static int host_byte_order( void )
193 {
194 int i = 1;
195 char *cptr = (char *) &i;
196 return (*cptr==1) ? LSBFirst : MSBFirst;
197 }
198
199
200
201
202 /**
203 * Return the true number of bits per pixel for XImages.
204 * For example, if we request a 24-bit deep visual we may actually need/get
205 * 32bpp XImages. This function returns the appropriate bpp.
206 * Input: dpy - the X display
207 * visinfo - desribes the visual to be used for XImages
208 * Return: true number of bits per pixel for XImages
209 */
210 static int
211 bits_per_pixel( XMesaVisual xmv )
212 {
213 Display *dpy = xmv->display;
214 XVisualInfo * visinfo = xmv->visinfo;
215 XImage *img;
216 int bitsPerPixel;
217 /* Create a temporary XImage */
218 img = XCreateImage( dpy, visinfo->visual, visinfo->depth,
219 ZPixmap, 0, /*format, offset*/
220 (char*) MALLOC(8), /*data*/
221 1, 1, /*width, height*/
222 32, /*bitmap_pad*/
223 0 /*bytes_per_line*/
224 );
225 assert(img);
226 /* grab the bits/pixel value */
227 bitsPerPixel = img->bits_per_pixel;
228 /* free the XImage */
229 free( img->data );
230 img->data = NULL;
231 XDestroyImage( img );
232 return bitsPerPixel;
233 }
234
235
236
237 /*
238 * Determine if a given X window ID is valid (window exists).
239 * Do this by calling XGetWindowAttributes() for the window and
240 * checking if we catch an X error.
241 * Input: dpy - the display
242 * win - the window to check for existance
243 * Return: GL_TRUE - window exists
244 * GL_FALSE - window doesn't exist
245 */
246 static GLboolean WindowExistsFlag;
247
248 static int window_exists_err_handler( Display* dpy, XErrorEvent* xerr )
249 {
250 (void) dpy;
251 if (xerr->error_code == BadWindow) {
252 WindowExistsFlag = GL_FALSE;
253 }
254 return 0;
255 }
256
257 static GLboolean window_exists( Display *dpy, Window win )
258 {
259 XWindowAttributes wa;
260 int (*old_handler)( Display*, XErrorEvent* );
261 WindowExistsFlag = GL_TRUE;
262 old_handler = XSetErrorHandler(window_exists_err_handler);
263 XGetWindowAttributes( dpy, win, &wa ); /* dummy request */
264 XSetErrorHandler(old_handler);
265 return WindowExistsFlag;
266 }
267
268 static Status
269 get_drawable_size( Display *dpy, Drawable d, uint *width, uint *height )
270 {
271 Window root;
272 Status stat;
273 int xpos, ypos;
274 unsigned int w, h, bw, depth;
275 stat = XGetGeometry(dpy, d, &root, &xpos, &ypos, &w, &h, &bw, &depth);
276 *width = w;
277 *height = h;
278 return stat;
279 }
280
281
282 /**
283 * Return the size of the window (or pixmap) that corresponds to the
284 * given XMesaBuffer.
285 * \param width returns width in pixels
286 * \param height returns height in pixels
287 */
288 void
289 xmesa_get_window_size(Display *dpy, XMesaBuffer b,
290 GLuint *width, GLuint *height)
291 {
292 XMesaDisplay xmdpy = xmesa_init_display(dpy);
293 Status stat;
294
295 pipe_mutex_lock(xmdpy->mutex);
296 stat = get_drawable_size(dpy, b->ws.drawable, width, height);
297 pipe_mutex_unlock(xmdpy->mutex);
298
299 if (!stat) {
300 /* probably querying a window that's recently been destroyed */
301 _mesa_warning(NULL, "XGetGeometry failed!\n");
302 *width = *height = 1;
303 }
304 }
305
306 #define GET_REDMASK(__v) __v->mesa_visual.redMask
307 #define GET_GREENMASK(__v) __v->mesa_visual.greenMask
308 #define GET_BLUEMASK(__v) __v->mesa_visual.blueMask
309
310
311 /**
312 * Choose the pixel format for the given visual.
313 * This will tell the gallium driver how to pack pixel data into
314 * drawing surfaces.
315 */
316 static GLuint
317 choose_pixel_format(XMesaVisual v)
318 {
319 boolean native_byte_order = (host_byte_order() ==
320 ImageByteOrder(v->display));
321
322 if ( GET_REDMASK(v) == 0x0000ff
323 && GET_GREENMASK(v) == 0x00ff00
324 && GET_BLUEMASK(v) == 0xff0000
325 && v->BitsPerPixel == 32) {
326 if (native_byte_order) {
327 /* no byteswapping needed */
328 return PIPE_FORMAT_R8G8B8A8_UNORM;
329 }
330 else {
331 return PIPE_FORMAT_A8B8G8R8_UNORM;
332 }
333 }
334 else if ( GET_REDMASK(v) == 0xff0000
335 && GET_GREENMASK(v) == 0x00ff00
336 && GET_BLUEMASK(v) == 0x0000ff
337 && v->BitsPerPixel == 32) {
338 if (native_byte_order) {
339 /* no byteswapping needed */
340 return PIPE_FORMAT_B8G8R8A8_UNORM;
341 }
342 else {
343 return PIPE_FORMAT_A8R8G8B8_UNORM;
344 }
345 }
346 else if ( GET_REDMASK(v) == 0x0000ff00
347 && GET_GREENMASK(v) == 0x00ff0000
348 && GET_BLUEMASK(v) == 0xff000000
349 && v->BitsPerPixel == 32) {
350 if (native_byte_order) {
351 /* no byteswapping needed */
352 return PIPE_FORMAT_A8R8G8B8_UNORM;
353 }
354 else {
355 return PIPE_FORMAT_B8G8R8A8_UNORM;
356 }
357 }
358 else if ( GET_REDMASK(v) == 0xf800
359 && GET_GREENMASK(v) == 0x07e0
360 && GET_BLUEMASK(v) == 0x001f
361 && native_byte_order
362 && v->BitsPerPixel == 16) {
363 /* 5-6-5 RGB */
364 return PIPE_FORMAT_B5G6R5_UNORM;
365 }
366
367 return PIPE_FORMAT_NONE;
368 }
369
370
371 /**
372 * Choose a depth/stencil format that satisfies the given depth and
373 * stencil sizes.
374 */
375 static enum pipe_format
376 choose_depth_stencil_format(XMesaDisplay xmdpy, int depth, int stencil)
377 {
378 const enum pipe_texture_target target = PIPE_TEXTURE_2D;
379 const unsigned tex_usage = PIPE_BIND_DEPTH_STENCIL;
380 const unsigned sample_count = 0;
381 enum pipe_format formats[8], fmt;
382 int count, i;
383
384 count = 0;
385
386 if (depth <= 16 && stencil == 0) {
387 formats[count++] = PIPE_FORMAT_Z16_UNORM;
388 }
389 if (depth <= 24 && stencil == 0) {
390 formats[count++] = PIPE_FORMAT_X8Z24_UNORM;
391 formats[count++] = PIPE_FORMAT_Z24X8_UNORM;
392 }
393 if (depth <= 24 && stencil <= 8) {
394 formats[count++] = PIPE_FORMAT_S8_USCALED_Z24_UNORM;
395 formats[count++] = PIPE_FORMAT_Z24_UNORM_S8_USCALED;
396 }
397 if (depth <= 32 && stencil == 0) {
398 formats[count++] = PIPE_FORMAT_Z32_UNORM;
399 }
400
401 fmt = PIPE_FORMAT_NONE;
402 for (i = 0; i < count; i++) {
403 if (xmdpy->screen->is_format_supported(xmdpy->screen, formats[i],
404 target, sample_count,
405 tex_usage)) {
406 fmt = formats[i];
407 break;
408 }
409 }
410
411 return fmt;
412 }
413
414
415
416 /**********************************************************************/
417 /***** Linked list of XMesaBuffers *****/
418 /**********************************************************************/
419
420 static XMesaBuffer XMesaBufferList = NULL;
421
422
423 /**
424 * Allocate a new XMesaBuffer object which corresponds to the given drawable.
425 * Note that XMesaBuffer is derived from struct gl_framebuffer.
426 * The new XMesaBuffer will not have any size (Width=Height=0).
427 *
428 * \param d the corresponding X drawable (window or pixmap)
429 * \param type either WINDOW, PIXMAP or PBUFFER, describing d
430 * \param vis the buffer's visual
431 * \param cmap the window's colormap, if known.
432 * \return new XMesaBuffer or NULL if any problem
433 */
434 static XMesaBuffer
435 create_xmesa_buffer(Drawable d, BufferType type,
436 XMesaVisual vis, Colormap cmap)
437 {
438 XMesaDisplay xmdpy = xmesa_init_display(vis->display);
439 XMesaBuffer b;
440 uint width, height;
441
442 ASSERT(type == WINDOW || type == PIXMAP || type == PBUFFER);
443
444 if (!xmdpy)
445 return NULL;
446
447 b = (XMesaBuffer) CALLOC_STRUCT(xmesa_buffer);
448 if (!b)
449 return NULL;
450
451 b->ws.drawable = d;
452 b->ws.visual = vis->visinfo->visual;
453 b->ws.depth = vis->visinfo->depth;
454
455 b->xm_visual = vis;
456 b->type = type;
457 b->cmap = cmap;
458
459 get_drawable_size(vis->display, d, &width, &height);
460
461 /*
462 * Create framebuffer, but we'll plug in our own renderbuffers below.
463 */
464 b->stfb = xmesa_create_st_framebuffer(xmdpy, b);
465
466 /* GLX_EXT_texture_from_pixmap */
467 b->TextureTarget = 0;
468 b->TextureFormat = GLX_TEXTURE_FORMAT_NONE_EXT;
469 b->TextureMipmap = 0;
470
471 /* insert buffer into linked list */
472 b->Next = XMesaBufferList;
473 XMesaBufferList = b;
474
475 return b;
476 }
477
478
479 /**
480 * Find an XMesaBuffer by matching X display and colormap but NOT matching
481 * the notThis buffer.
482 */
483 XMesaBuffer
484 xmesa_find_buffer(Display *dpy, Colormap cmap, XMesaBuffer notThis)
485 {
486 XMesaBuffer b;
487 for (b = XMesaBufferList; b; b = b->Next) {
488 if (b->xm_visual->display == dpy &&
489 b->cmap == cmap &&
490 b != notThis) {
491 return b;
492 }
493 }
494 return NULL;
495 }
496
497
498 /**
499 * Remove buffer from linked list, delete if no longer referenced.
500 */
501 static void
502 xmesa_free_buffer(XMesaBuffer buffer)
503 {
504 XMesaBuffer prev = NULL, b;
505
506 for (b = XMesaBufferList; b; b = b->Next) {
507 if (b == buffer) {
508 /* unlink buffer from list */
509 if (prev)
510 prev->Next = buffer->Next;
511 else
512 XMesaBufferList = buffer->Next;
513
514 /* Since the X window for the XMesaBuffer is going away, we don't
515 * want to dereference this pointer in the future.
516 */
517 b->ws.drawable = 0;
518
519 /* XXX we should move the buffer to a delete-pending list and destroy
520 * the buffer until it is no longer current.
521 */
522 xmesa_destroy_st_framebuffer(buffer->stfb);
523
524 free(buffer);
525
526 return;
527 }
528 /* continue search */
529 prev = b;
530 }
531 /* buffer not found in XMesaBufferList */
532 _mesa_problem(NULL,"xmesa_free_buffer() - buffer not found\n");
533 }
534
535
536
537 /**********************************************************************/
538 /***** Misc Private Functions *****/
539 /**********************************************************************/
540
541
542 /**
543 * When a context is bound for the first time, we can finally finish
544 * initializing the context's visual and buffer information.
545 * \param v the XMesaVisual to initialize
546 * \param b the XMesaBuffer to initialize (may be NULL)
547 * \param rgb_flag TRUE = RGBA mode, FALSE = color index mode
548 * \param window the window/pixmap we're rendering into
549 * \param cmap the colormap associated with the window/pixmap
550 * \return GL_TRUE=success, GL_FALSE=failure
551 */
552 static GLboolean
553 initialize_visual_and_buffer(XMesaVisual v, XMesaBuffer b,
554 GLboolean rgb_flag, Drawable window,
555 Colormap cmap)
556 {
557 ASSERT(!b || b->xm_visual == v);
558
559 /* Save true bits/pixel */
560 v->BitsPerPixel = bits_per_pixel(v);
561 assert(v->BitsPerPixel > 0);
562
563 if (rgb_flag == GL_FALSE) {
564 /* COLOR-INDEXED WINDOW: not supported*/
565 return GL_FALSE;
566 }
567 else {
568 /* RGB WINDOW:
569 * We support RGB rendering into almost any kind of visual.
570 */
571 const int xclass = v->visualType;
572 if (xclass != GLX_TRUE_COLOR && xclass == !GLX_DIRECT_COLOR) {
573 _mesa_warning(NULL,
574 "XMesa: RGB mode rendering not supported in given visual.\n");
575 return GL_FALSE;
576 }
577 v->mesa_visual.indexBits = 0;
578
579 if (v->BitsPerPixel == 32) {
580 /* We use XImages for all front/back buffers. If an X Window or
581 * X Pixmap is 32bpp, there's no guarantee that the alpha channel
582 * will be preserved. For XImages we're in luck.
583 */
584 v->mesa_visual.alphaBits = 8;
585 }
586 }
587
588 /*
589 * If MESA_INFO env var is set print out some debugging info
590 * which can help Brian figure out what's going on when a user
591 * reports bugs.
592 */
593 if (_mesa_getenv("MESA_INFO")) {
594 printf("X/Mesa visual = %p\n", (void *) v);
595 printf("X/Mesa level = %d\n", v->mesa_visual.level);
596 printf("X/Mesa depth = %d\n", v->visinfo->depth);
597 printf("X/Mesa bits per pixel = %d\n", v->BitsPerPixel);
598 }
599
600 return GL_TRUE;
601 }
602
603
604
605 #define NUM_VISUAL_TYPES 6
606
607 /**
608 * Convert an X visual type to a GLX visual type.
609 *
610 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
611 * to be converted.
612 * \return If \c visualType is a valid X visual type, a GLX visual type will
613 * be returned. Otherwise \c GLX_NONE will be returned.
614 *
615 * \note
616 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
617 * DRI CVS tree.
618 */
619 static GLint
620 xmesa_convert_from_x_visual_type( int visualType )
621 {
622 static const int glx_visual_types[ NUM_VISUAL_TYPES ] = {
623 GLX_STATIC_GRAY, GLX_GRAY_SCALE,
624 GLX_STATIC_COLOR, GLX_PSEUDO_COLOR,
625 GLX_TRUE_COLOR, GLX_DIRECT_COLOR
626 };
627
628 return ( (unsigned) visualType < NUM_VISUAL_TYPES )
629 ? glx_visual_types[ visualType ] : GLX_NONE;
630 }
631
632
633 /**********************************************************************/
634 /***** Public Functions *****/
635 /**********************************************************************/
636
637
638 /*
639 * Create a new X/Mesa visual.
640 * Input: display - X11 display
641 * visinfo - an XVisualInfo pointer
642 * rgb_flag - GL_TRUE = RGB mode,
643 * GL_FALSE = color index mode
644 * alpha_flag - alpha buffer requested?
645 * db_flag - GL_TRUE = double-buffered,
646 * GL_FALSE = single buffered
647 * stereo_flag - stereo visual?
648 * ximage_flag - GL_TRUE = use an XImage for back buffer,
649 * GL_FALSE = use an off-screen pixmap for back buffer
650 * depth_size - requested bits/depth values, or zero
651 * stencil_size - requested bits/stencil values, or zero
652 * accum_red_size - requested bits/red accum values, or zero
653 * accum_green_size - requested bits/green accum values, or zero
654 * accum_blue_size - requested bits/blue accum values, or zero
655 * accum_alpha_size - requested bits/alpha accum values, or zero
656 * num_samples - number of samples/pixel if multisampling, or zero
657 * level - visual level, usually 0
658 * visualCaveat - ala the GLX extension, usually GLX_NONE
659 * Return; a new XMesaVisual or 0 if error.
660 */
661 PUBLIC
662 XMesaVisual XMesaCreateVisual( Display *display,
663 XVisualInfo * visinfo,
664 GLboolean rgb_flag,
665 GLboolean alpha_flag,
666 GLboolean db_flag,
667 GLboolean stereo_flag,
668 GLboolean ximage_flag,
669 GLint depth_size,
670 GLint stencil_size,
671 GLint accum_red_size,
672 GLint accum_green_size,
673 GLint accum_blue_size,
674 GLint accum_alpha_size,
675 GLint num_samples,
676 GLint level,
677 GLint visualCaveat )
678 {
679 XMesaDisplay xmdpy = xmesa_init_display(display);
680 XMesaVisual v;
681 GLint red_bits, green_bits, blue_bits, alpha_bits;
682
683 if (!xmdpy)
684 return NULL;
685
686 /* For debugging only */
687 if (_mesa_getenv("MESA_XSYNC")) {
688 /* This makes debugging X easier.
689 * In your debugger, set a breakpoint on _XError to stop when an
690 * X protocol error is generated.
691 */
692 XSynchronize( display, 1 );
693 }
694
695 v = (XMesaVisual) CALLOC_STRUCT(xmesa_visual);
696 if (!v) {
697 return NULL;
698 }
699
700 v->display = display;
701
702 /* Save a copy of the XVisualInfo struct because the user may Xfree()
703 * the struct but we may need some of the information contained in it
704 * at a later time.
705 */
706 v->visinfo = (XVisualInfo *) MALLOC(sizeof(*visinfo));
707 if (!v->visinfo) {
708 free(v);
709 return NULL;
710 }
711 memcpy(v->visinfo, visinfo, sizeof(*visinfo));
712
713 v->ximage_flag = ximage_flag;
714
715 v->mesa_visual.redMask = visinfo->red_mask;
716 v->mesa_visual.greenMask = visinfo->green_mask;
717 v->mesa_visual.blueMask = visinfo->blue_mask;
718 v->visualID = visinfo->visualid;
719 v->screen = visinfo->screen;
720
721 #if !(defined(__cplusplus) || defined(c_plusplus))
722 v->visualType = xmesa_convert_from_x_visual_type(visinfo->class);
723 #else
724 v->visualType = xmesa_convert_from_x_visual_type(visinfo->c_class);
725 #endif
726
727 v->mesa_visual.visualRating = visualCaveat;
728
729 if (alpha_flag)
730 v->mesa_visual.alphaBits = 8;
731
732 (void) initialize_visual_and_buffer( v, NULL, rgb_flag, 0, 0 );
733
734 {
735 const int xclass = v->visualType;
736 if (xclass == GLX_TRUE_COLOR || xclass == GLX_DIRECT_COLOR) {
737 red_bits = _mesa_bitcount(GET_REDMASK(v));
738 green_bits = _mesa_bitcount(GET_GREENMASK(v));
739 blue_bits = _mesa_bitcount(GET_BLUEMASK(v));
740 }
741 else {
742 /* this is an approximation */
743 int depth;
744 depth = v->visinfo->depth;
745 red_bits = depth / 3;
746 depth -= red_bits;
747 green_bits = depth / 2;
748 depth -= green_bits;
749 blue_bits = depth;
750 alpha_bits = 0;
751 assert( red_bits + green_bits + blue_bits == v->visinfo->depth );
752 }
753 alpha_bits = v->mesa_visual.alphaBits;
754 }
755
756 /* initialize visual */
757 {
758 struct gl_config *vis = &v->mesa_visual;
759
760 vis->rgbMode = GL_TRUE;
761 vis->doubleBufferMode = db_flag;
762 vis->stereoMode = stereo_flag;
763
764 vis->redBits = red_bits;
765 vis->greenBits = green_bits;
766 vis->blueBits = blue_bits;
767 vis->alphaBits = alpha_bits;
768 vis->rgbBits = red_bits + green_bits + blue_bits;
769
770 vis->indexBits = 0;
771 vis->depthBits = depth_size;
772 vis->stencilBits = stencil_size;
773
774 vis->accumRedBits = accum_red_size;
775 vis->accumGreenBits = accum_green_size;
776 vis->accumBlueBits = accum_blue_size;
777 vis->accumAlphaBits = accum_alpha_size;
778
779 vis->haveAccumBuffer = accum_red_size > 0;
780 vis->haveDepthBuffer = depth_size > 0;
781 vis->haveStencilBuffer = stencil_size > 0;
782
783 vis->numAuxBuffers = 0;
784 vis->level = 0;
785 vis->sampleBuffers = 0;
786 vis->samples = 0;
787 }
788
789 v->stvis.buffer_mask = ST_ATTACHMENT_FRONT_LEFT_MASK;
790 if (db_flag)
791 v->stvis.buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK;
792 if (stereo_flag) {
793 v->stvis.buffer_mask |= ST_ATTACHMENT_FRONT_RIGHT_MASK;
794 if (db_flag)
795 v->stvis.buffer_mask |= ST_ATTACHMENT_BACK_RIGHT_MASK;
796 }
797
798 v->stvis.color_format = choose_pixel_format(v);
799 if (v->stvis.color_format == PIPE_FORMAT_NONE) {
800 FREE(v->visinfo);
801 FREE(v);
802 return NULL;
803 }
804
805 v->stvis.depth_stencil_format =
806 choose_depth_stencil_format(xmdpy, depth_size, stencil_size);
807
808 v->stvis.accum_format = (accum_red_size +
809 accum_green_size + accum_blue_size + accum_alpha_size) ?
810 PIPE_FORMAT_R16G16B16A16_SNORM : PIPE_FORMAT_NONE;
811
812 v->stvis.samples = num_samples;
813 v->stvis.render_buffer = ST_ATTACHMENT_INVALID;
814
815 /* XXX minor hack */
816 v->mesa_visual.level = level;
817 return v;
818 }
819
820
821 PUBLIC
822 void XMesaDestroyVisual( XMesaVisual v )
823 {
824 free(v->visinfo);
825 free(v);
826 }
827
828
829 /**
830 * Return the informative name.
831 */
832 const char *
833 xmesa_get_name(void)
834 {
835 return stapi->name;
836 }
837
838
839 /**
840 * Do per-display initializations.
841 */
842 void
843 xmesa_init( Display *display )
844 {
845 xmesa_init_display(display);
846 }
847
848
849 /**
850 * Create a new XMesaContext.
851 * \param v the XMesaVisual
852 * \param share_list another XMesaContext with which to share display
853 * lists or NULL if no sharing is wanted.
854 * \return an XMesaContext or NULL if error.
855 */
856 PUBLIC
857 XMesaContext XMesaCreateContext( XMesaVisual v, XMesaContext share_list,
858 GLuint major, GLuint minor,
859 GLuint profileMask, GLuint contextFlags)
860 {
861 XMesaDisplay xmdpy = xmesa_init_display(v->display);
862 struct st_context_attribs attribs;
863 XMesaContext c;
864
865 if (!xmdpy)
866 return NULL;
867
868 /* Note: the XMesaContext contains a Mesa struct gl_context struct (inheritance) */
869 c = (XMesaContext) CALLOC_STRUCT(xmesa_context);
870 if (!c)
871 return NULL;
872
873 c->xm_visual = v;
874 c->xm_buffer = NULL; /* set later by XMesaMakeCurrent */
875 c->xm_read_buffer = NULL;
876
877 memset(&attribs, 0, sizeof(attribs));
878 attribs.profile = ST_PROFILE_DEFAULT;
879 attribs.visual = v->stvis;
880 attribs.major = major;
881 attribs.minor = minor;
882 if (contextFlags & GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB)
883 attribs.flags |= ST_CONTEXT_FLAG_FORWARD_COMPATIBLE;
884 if (contextFlags & GLX_CONTEXT_DEBUG_BIT_ARB)
885 attribs.flags |= ST_CONTEXT_FLAG_DEBUG;
886 if (contextFlags & GLX_CONTEXT_ROBUST_ACCESS_BIT_ARB)
887 attribs.flags |= ST_CONTEXT_FLAG_ROBUST_ACCESS;
888 if (profileMask & GLX_CONTEXT_CORE_PROFILE_BIT_ARB)
889 attribs.flags |= ST_CONTEXT_FLAG_CORE_PROFILE;
890 if (profileMask & GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB)
891 attribs.flags |= ST_CONTEXT_FLAG_COMPATIBLE_PROFILE;
892
893 c->st = stapi->create_context(stapi, xmdpy->smapi,
894 &attribs, (share_list) ? share_list->st : NULL);
895 if (c->st == NULL)
896 goto fail;
897
898 c->st->st_manager_private = (void *) c;
899
900 return c;
901
902 fail:
903 if (c->st)
904 c->st->destroy(c->st);
905
906 free(c);
907 return NULL;
908 }
909
910
911
912 PUBLIC
913 void XMesaDestroyContext( XMesaContext c )
914 {
915 c->st->destroy(c->st);
916
917 /* FIXME: We should destroy the screen here, but if we do so, surfaces may
918 * outlive it, causing segfaults
919 struct pipe_screen *screen = c->st->pipe->screen;
920 screen->destroy(screen);
921 */
922
923 free(c);
924 }
925
926
927
928 /**
929 * Private function for creating an XMesaBuffer which corresponds to an
930 * X window or pixmap.
931 * \param v the window's XMesaVisual
932 * \param w the window we're wrapping
933 * \return new XMesaBuffer or NULL if error
934 */
935 PUBLIC XMesaBuffer
936 XMesaCreateWindowBuffer(XMesaVisual v, Window w)
937 {
938 XWindowAttributes attr;
939 XMesaBuffer b;
940 Colormap cmap;
941 int depth;
942
943 assert(v);
944 assert(w);
945
946 /* Check that window depth matches visual depth */
947 XGetWindowAttributes( v->display, w, &attr );
948 depth = attr.depth;
949 if (v->visinfo->depth != depth) {
950 _mesa_warning(NULL, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
951 v->visinfo->depth, depth);
952 return NULL;
953 }
954
955 /* Find colormap */
956 if (attr.colormap) {
957 cmap = attr.colormap;
958 }
959 else {
960 _mesa_warning(NULL, "Window %u has no colormap!\n", (unsigned int) w);
961 /* this is weird, a window w/out a colormap!? */
962 /* OK, let's just allocate a new one and hope for the best */
963 cmap = XCreateColormap(v->display, w, attr.visual, AllocNone);
964 }
965
966 b = create_xmesa_buffer((Drawable) w, WINDOW, v, cmap);
967 if (!b)
968 return NULL;
969
970 if (!initialize_visual_and_buffer( v, b, v->mesa_visual.rgbMode,
971 (Drawable) w, cmap )) {
972 xmesa_free_buffer(b);
973 return NULL;
974 }
975
976 return b;
977 }
978
979
980
981 /**
982 * Create a new XMesaBuffer from an X pixmap.
983 *
984 * \param v the XMesaVisual
985 * \param p the pixmap
986 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
987 * \c GLX_DIRECT_COLOR visual for the pixmap
988 * \returns new XMesaBuffer or NULL if error
989 */
990 PUBLIC XMesaBuffer
991 XMesaCreatePixmapBuffer(XMesaVisual v, Pixmap p, Colormap cmap)
992 {
993 XMesaBuffer b;
994
995 assert(v);
996
997 b = create_xmesa_buffer((Drawable) p, PIXMAP, v, cmap);
998 if (!b)
999 return NULL;
1000
1001 if (!initialize_visual_and_buffer(v, b, v->mesa_visual.rgbMode,
1002 (Drawable) p, cmap)) {
1003 xmesa_free_buffer(b);
1004 return NULL;
1005 }
1006
1007 return b;
1008 }
1009
1010
1011 /**
1012 * For GLX_EXT_texture_from_pixmap
1013 */
1014 XMesaBuffer
1015 XMesaCreatePixmapTextureBuffer(XMesaVisual v, Pixmap p,
1016 Colormap cmap,
1017 int format, int target, int mipmap)
1018 {
1019 GET_CURRENT_CONTEXT(ctx);
1020 XMesaBuffer b;
1021
1022 assert(v);
1023
1024 b = create_xmesa_buffer((Drawable) p, PIXMAP, v, cmap);
1025 if (!b)
1026 return NULL;
1027
1028 /* get pixmap size */
1029 xmesa_get_window_size(v->display, b, &b->width, &b->height);
1030
1031 if (target == 0) {
1032 /* examine dims */
1033 if (ctx->Extensions.ARB_texture_non_power_of_two) {
1034 target = GLX_TEXTURE_2D_EXT;
1035 }
1036 else if ( _mesa_bitcount(b->width) == 1
1037 && _mesa_bitcount(b->height) == 1) {
1038 /* power of two size */
1039 if (b->height == 1) {
1040 target = GLX_TEXTURE_1D_EXT;
1041 }
1042 else {
1043 target = GLX_TEXTURE_2D_EXT;
1044 }
1045 }
1046 else if (ctx->Extensions.NV_texture_rectangle) {
1047 target = GLX_TEXTURE_RECTANGLE_EXT;
1048 }
1049 else {
1050 /* non power of two textures not supported */
1051 XMesaDestroyBuffer(b);
1052 return 0;
1053 }
1054 }
1055
1056 b->TextureTarget = target;
1057 b->TextureFormat = format;
1058 b->TextureMipmap = mipmap;
1059
1060 if (!initialize_visual_and_buffer(v, b, v->mesa_visual.rgbMode,
1061 (Drawable) p, cmap)) {
1062 xmesa_free_buffer(b);
1063 return NULL;
1064 }
1065
1066 return b;
1067 }
1068
1069
1070
1071 XMesaBuffer
1072 XMesaCreatePBuffer(XMesaVisual v, Colormap cmap,
1073 unsigned int width, unsigned int height)
1074 {
1075 Window root;
1076 Drawable drawable; /* X Pixmap Drawable */
1077 XMesaBuffer b;
1078
1079 /* allocate pixmap for front buffer */
1080 root = RootWindow( v->display, v->visinfo->screen );
1081 drawable = XCreatePixmap(v->display, root, width, height,
1082 v->visinfo->depth);
1083 if (!drawable)
1084 return NULL;
1085
1086 b = create_xmesa_buffer(drawable, PBUFFER, v, cmap);
1087 if (!b)
1088 return NULL;
1089
1090 if (!initialize_visual_and_buffer(v, b, v->mesa_visual.rgbMode,
1091 drawable, cmap)) {
1092 xmesa_free_buffer(b);
1093 return NULL;
1094 }
1095
1096 return b;
1097 }
1098
1099
1100
1101 /*
1102 * Deallocate an XMesaBuffer structure and all related info.
1103 */
1104 PUBLIC void
1105 XMesaDestroyBuffer(XMesaBuffer b)
1106 {
1107 xmesa_free_buffer(b);
1108 }
1109
1110
1111 /**
1112 * Notify the binding context to validate the buffer.
1113 */
1114 void
1115 xmesa_notify_invalid_buffer(XMesaBuffer b)
1116 {
1117 p_atomic_inc(&b->stfb->stamp);
1118 }
1119
1120
1121 /**
1122 * Query the current drawable size and notify the binding context.
1123 */
1124 void
1125 xmesa_check_buffer_size(XMesaBuffer b)
1126 {
1127 if (b->type == PBUFFER)
1128 return;
1129
1130 xmesa_get_window_size(b->xm_visual->display, b, &b->width, &b->height);
1131 xmesa_notify_invalid_buffer(b);
1132 }
1133
1134
1135 /*
1136 * Bind buffer b to context c and make c the current rendering context.
1137 */
1138 PUBLIC
1139 GLboolean XMesaMakeCurrent2( XMesaContext c, XMesaBuffer drawBuffer,
1140 XMesaBuffer readBuffer )
1141 {
1142 XMesaContext old_ctx = XMesaGetCurrentContext();
1143
1144 if (old_ctx && old_ctx != c) {
1145 XMesaFlush(old_ctx);
1146 old_ctx->xm_buffer = NULL;
1147 old_ctx->xm_read_buffer = NULL;
1148 }
1149
1150 if (c) {
1151 if (!drawBuffer || !readBuffer)
1152 return GL_FALSE; /* must specify buffers! */
1153
1154 if (c == old_ctx &&
1155 c->xm_buffer == drawBuffer &&
1156 c->xm_read_buffer == readBuffer)
1157 return GL_TRUE;
1158
1159 xmesa_check_buffer_size(drawBuffer);
1160 if (readBuffer != drawBuffer)
1161 xmesa_check_buffer_size(readBuffer);
1162
1163 c->xm_buffer = drawBuffer;
1164 c->xm_read_buffer = readBuffer;
1165
1166 stapi->make_current(stapi, c->st, drawBuffer->stfb, readBuffer->stfb);
1167
1168 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1169 drawBuffer->wasCurrent = GL_TRUE;
1170 }
1171 else {
1172 /* Detach */
1173 stapi->make_current(stapi, NULL, NULL, NULL);
1174
1175 }
1176 return GL_TRUE;
1177 }
1178
1179
1180 /*
1181 * Unbind the context c from its buffer.
1182 */
1183 GLboolean XMesaUnbindContext( XMesaContext c )
1184 {
1185 /* A no-op for XFree86 integration purposes */
1186 return GL_TRUE;
1187 }
1188
1189
1190 XMesaContext XMesaGetCurrentContext( void )
1191 {
1192 struct st_context_iface *st = stapi->get_current(stapi);
1193 return (XMesaContext) (st) ? st->st_manager_private : NULL;
1194 }
1195
1196
1197
1198 /**
1199 * Swap front and back color buffers and have winsys display front buffer.
1200 * If there's no front color buffer no swap actually occurs.
1201 */
1202 PUBLIC
1203 void XMesaSwapBuffers( XMesaBuffer b )
1204 {
1205 XMesaContext xmctx = XMesaGetCurrentContext();
1206
1207 if (xmctx && xmctx->xm_buffer == b) {
1208 xmctx->st->flush( xmctx->st, ST_FLUSH_FRONT, NULL);
1209 }
1210
1211 xmesa_swap_st_framebuffer(b->stfb);
1212 }
1213
1214
1215
1216 /*
1217 * Copy sub-region of back buffer to front buffer
1218 */
1219 void XMesaCopySubBuffer( XMesaBuffer b, int x, int y, int width, int height )
1220 {
1221 xmesa_copy_st_framebuffer(b->stfb,
1222 ST_ATTACHMENT_BACK_LEFT, ST_ATTACHMENT_FRONT_LEFT,
1223 x, y, width, height);
1224 }
1225
1226
1227
1228 void XMesaFlush( XMesaContext c )
1229 {
1230 if (c && c->xm_visual->display) {
1231 XMesaDisplay xmdpy = xmesa_init_display(c->xm_visual->display);
1232 struct pipe_fence_handle *fence = NULL;
1233
1234 c->st->flush(c->st, ST_FLUSH_FRONT, &fence);
1235 if (fence) {
1236 xmdpy->screen->fence_finish(xmdpy->screen, fence,
1237 PIPE_TIMEOUT_INFINITE);
1238 xmdpy->screen->fence_reference(xmdpy->screen, &fence, NULL);
1239 }
1240 XFlush( c->xm_visual->display );
1241 }
1242 }
1243
1244
1245
1246
1247
1248 XMesaBuffer XMesaFindBuffer( Display *dpy, Drawable d )
1249 {
1250 XMesaBuffer b;
1251 for (b = XMesaBufferList; b; b = b->Next) {
1252 if (b->ws.drawable == d && b->xm_visual->display == dpy) {
1253 return b;
1254 }
1255 }
1256 return NULL;
1257 }
1258
1259
1260 /**
1261 * Free/destroy all XMesaBuffers associated with given display.
1262 */
1263 void xmesa_destroy_buffers_on_display(Display *dpy)
1264 {
1265 XMesaBuffer b, next;
1266 for (b = XMesaBufferList; b; b = next) {
1267 next = b->Next;
1268 if (b->xm_visual->display == dpy) {
1269 xmesa_free_buffer(b);
1270 /* delete head of list? */
1271 if (XMesaBufferList == b) {
1272 XMesaBufferList = next;
1273 }
1274 }
1275 }
1276 }
1277
1278
1279 /*
1280 * Look for XMesaBuffers whose X window has been destroyed.
1281 * Deallocate any such XMesaBuffers.
1282 */
1283 void XMesaGarbageCollect( void )
1284 {
1285 XMesaBuffer b, next;
1286 for (b=XMesaBufferList; b; b=next) {
1287 next = b->Next;
1288 if (b->xm_visual &&
1289 b->xm_visual->display &&
1290 b->ws.drawable &&
1291 b->type == WINDOW) {
1292 XSync(b->xm_visual->display, False);
1293 if (!window_exists( b->xm_visual->display, b->ws.drawable )) {
1294 /* found a dead window, free the ancillary info */
1295 XMesaDestroyBuffer( b );
1296 }
1297 }
1298 }
1299 }
1300
1301
1302
1303
1304 PUBLIC void
1305 XMesaBindTexImage(Display *dpy, XMesaBuffer drawable, int buffer,
1306 const int *attrib_list)
1307 {
1308 }
1309
1310
1311
1312 PUBLIC void
1313 XMesaReleaseTexImage(Display *dpy, XMesaBuffer drawable, int buffer)
1314 {
1315 }
1316
1317
1318 void
1319 XMesaCopyContext(XMesaContext src, XMesaContext dst, unsigned long mask)
1320 {
1321 if (dst->st->copy)
1322 dst->st->copy(dst->st, src->st, mask);
1323 }