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