Merge commit 'origin/master' into gallium-sampler-view
[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 * Choose a depth/stencil format that is "better" than the given depth and
314 * stencil sizes.
315 */
316 static enum pipe_format
317 choose_depth_stencil_format(XMesaDisplay xmdpy, int depth, int stencil)
318 {
319 const enum pipe_texture_target target = PIPE_TEXTURE_2D;
320 const unsigned tex_usage = PIPE_TEXTURE_USAGE_DEPTH_STENCIL;
321 const unsigned geom_flags = (PIPE_TEXTURE_GEOM_NON_SQUARE |
322 PIPE_TEXTURE_GEOM_NON_POWER_OF_TWO);
323 enum pipe_format formats[8], fmt;
324 int count, i;
325
326 count = 0;
327 if (depth <= 24 && stencil <= 8) {
328 formats[count++] = PIPE_FORMAT_S8Z24_UNORM;
329 formats[count++] = PIPE_FORMAT_Z24S8_UNORM;
330 }
331
332 if (!stencil) {
333 if (depth <= 16)
334 formats[count++] = PIPE_FORMAT_Z16_UNORM;
335 if (depth <= 32)
336 formats[count++] = PIPE_FORMAT_Z32_UNORM;
337 }
338
339 fmt = PIPE_FORMAT_NONE;
340 for (i = 0; i < count; i++) {
341 if (xmdpy->screen->is_format_supported(xmdpy->screen, formats[i],
342 target, tex_usage, geom_flags)) {
343 fmt = formats[i];
344 break;
345 }
346 }
347
348 return fmt;
349 }
350
351
352
353 /**********************************************************************/
354 /***** Linked list of XMesaBuffers *****/
355 /**********************************************************************/
356
357 static XMesaBuffer XMesaBufferList = NULL;
358
359
360 /**
361 * Allocate a new XMesaBuffer object which corresponds to the given drawable.
362 * Note that XMesaBuffer is derived from GLframebuffer.
363 * The new XMesaBuffer will not have any size (Width=Height=0).
364 *
365 * \param d the corresponding X drawable (window or pixmap)
366 * \param type either WINDOW, PIXMAP or PBUFFER, describing d
367 * \param vis the buffer's visual
368 * \param cmap the window's colormap, if known.
369 * \return new XMesaBuffer or NULL if any problem
370 */
371 static XMesaBuffer
372 create_xmesa_buffer(Drawable d, BufferType type,
373 XMesaVisual vis, Colormap cmap)
374 {
375 XMesaDisplay xmdpy = xmesa_init_display(vis->display);
376 XMesaBuffer b;
377 uint width, height;
378
379 ASSERT(type == WINDOW || type == PIXMAP || type == PBUFFER);
380
381 if (!xmdpy)
382 return NULL;
383
384 b = (XMesaBuffer) CALLOC_STRUCT(xmesa_buffer);
385 if (!b)
386 return NULL;
387
388 b->ws.drawable = d;
389 b->ws.visual = vis->visinfo->visual;
390 b->ws.depth = vis->visinfo->depth;
391
392 b->xm_visual = vis;
393 b->type = type;
394 b->cmap = cmap;
395
396 get_drawable_size(vis->display, d, &width, &height);
397
398 /*
399 * Create framebuffer, but we'll plug in our own renderbuffers below.
400 */
401 b->stfb = xmesa_create_st_framebuffer(xmdpy, b);
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(Display *dpy, Colormap 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 /* unlink buffer from list */
446 if (prev)
447 prev->Next = buffer->Next;
448 else
449 XMesaBufferList = buffer->Next;
450
451 /* Since the X window for the XMesaBuffer is going away, we don't
452 * want to dereference this pointer in the future.
453 */
454 b->ws.drawable = 0;
455
456 /* XXX we should move the buffer to a delete-pending list and destroy
457 * the buffer until it is no longer current.
458 */
459 xmesa_destroy_st_framebuffer(buffer->stfb);
460
461 free(buffer);
462
463 return;
464 }
465 /* continue search */
466 prev = b;
467 }
468 /* buffer not found in XMesaBufferList */
469 _mesa_problem(NULL,"xmesa_free_buffer() - buffer not found\n");
470 }
471
472
473
474 /**********************************************************************/
475 /***** Misc Private Functions *****/
476 /**********************************************************************/
477
478
479 /**
480 * When a context is bound for the first time, we can finally finish
481 * initializing the context's visual and buffer information.
482 * \param v the XMesaVisual to initialize
483 * \param b the XMesaBuffer to initialize (may be NULL)
484 * \param rgb_flag TRUE = RGBA mode, FALSE = color index mode
485 * \param window the window/pixmap we're rendering into
486 * \param cmap the colormap associated with the window/pixmap
487 * \return GL_TRUE=success, GL_FALSE=failure
488 */
489 static GLboolean
490 initialize_visual_and_buffer(XMesaVisual v, XMesaBuffer b,
491 GLboolean rgb_flag, Drawable window,
492 Colormap cmap)
493 {
494 ASSERT(!b || b->xm_visual == v);
495
496 /* Save true bits/pixel */
497 v->BitsPerPixel = bits_per_pixel(v);
498 assert(v->BitsPerPixel > 0);
499
500 if (rgb_flag == GL_FALSE) {
501 /* COLOR-INDEXED WINDOW: not supported*/
502 return GL_FALSE;
503 }
504 else {
505 /* RGB WINDOW:
506 * We support RGB rendering into almost any kind of visual.
507 */
508 const int xclass = v->mesa_visual.visualType;
509 if (xclass != GLX_TRUE_COLOR && xclass == !GLX_DIRECT_COLOR) {
510 _mesa_warning(NULL,
511 "XMesa: RGB mode rendering not supported in given visual.\n");
512 return GL_FALSE;
513 }
514 v->mesa_visual.indexBits = 0;
515
516 if (v->BitsPerPixel == 32) {
517 /* We use XImages for all front/back buffers. If an X Window or
518 * X Pixmap is 32bpp, there's no guarantee that the alpha channel
519 * will be preserved. For XImages we're in luck.
520 */
521 v->mesa_visual.alphaBits = 8;
522 }
523 }
524
525 /*
526 * If MESA_INFO env var is set print out some debugging info
527 * which can help Brian figure out what's going on when a user
528 * reports bugs.
529 */
530 if (_mesa_getenv("MESA_INFO")) {
531 printf("X/Mesa visual = %p\n", (void *) v);
532 printf("X/Mesa level = %d\n", v->mesa_visual.level);
533 printf("X/Mesa depth = %d\n", v->visinfo->depth);
534 printf("X/Mesa bits per pixel = %d\n", v->BitsPerPixel);
535 }
536
537 return GL_TRUE;
538 }
539
540
541
542 #define NUM_VISUAL_TYPES 6
543
544 /**
545 * Convert an X visual type to a GLX visual type.
546 *
547 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
548 * to be converted.
549 * \return If \c visualType is a valid X visual type, a GLX visual type will
550 * be returned. Otherwise \c GLX_NONE will be returned.
551 *
552 * \note
553 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
554 * DRI CVS tree.
555 */
556 static GLint
557 xmesa_convert_from_x_visual_type( int visualType )
558 {
559 static const int glx_visual_types[ NUM_VISUAL_TYPES ] = {
560 GLX_STATIC_GRAY, GLX_GRAY_SCALE,
561 GLX_STATIC_COLOR, GLX_PSEUDO_COLOR,
562 GLX_TRUE_COLOR, GLX_DIRECT_COLOR
563 };
564
565 return ( (unsigned) visualType < NUM_VISUAL_TYPES )
566 ? glx_visual_types[ visualType ] : GLX_NONE;
567 }
568
569
570 /**********************************************************************/
571 /***** Public Functions *****/
572 /**********************************************************************/
573
574
575 /*
576 * Create a new X/Mesa visual.
577 * Input: display - X11 display
578 * visinfo - an XVisualInfo pointer
579 * rgb_flag - GL_TRUE = RGB mode,
580 * GL_FALSE = color index mode
581 * alpha_flag - alpha buffer requested?
582 * db_flag - GL_TRUE = double-buffered,
583 * GL_FALSE = single buffered
584 * stereo_flag - stereo visual?
585 * ximage_flag - GL_TRUE = use an XImage for back buffer,
586 * GL_FALSE = use an off-screen pixmap for back buffer
587 * depth_size - requested bits/depth values, or zero
588 * stencil_size - requested bits/stencil values, or zero
589 * accum_red_size - requested bits/red accum values, or zero
590 * accum_green_size - requested bits/green accum values, or zero
591 * accum_blue_size - requested bits/blue accum values, or zero
592 * accum_alpha_size - requested bits/alpha accum values, or zero
593 * num_samples - number of samples/pixel if multisampling, or zero
594 * level - visual level, usually 0
595 * visualCaveat - ala the GLX extension, usually GLX_NONE
596 * Return; a new XMesaVisual or 0 if error.
597 */
598 PUBLIC
599 XMesaVisual XMesaCreateVisual( Display *display,
600 XVisualInfo * visinfo,
601 GLboolean rgb_flag,
602 GLboolean alpha_flag,
603 GLboolean db_flag,
604 GLboolean stereo_flag,
605 GLboolean ximage_flag,
606 GLint depth_size,
607 GLint stencil_size,
608 GLint accum_red_size,
609 GLint accum_green_size,
610 GLint accum_blue_size,
611 GLint accum_alpha_size,
612 GLint num_samples,
613 GLint level,
614 GLint visualCaveat )
615 {
616 XMesaDisplay xmdpy = xmesa_init_display(display);
617 XMesaVisual v;
618 GLint red_bits, green_bits, blue_bits, alpha_bits;
619
620 if (!xmdpy)
621 return NULL;
622
623 /* For debugging only */
624 if (_mesa_getenv("MESA_XSYNC")) {
625 /* This makes debugging X easier.
626 * In your debugger, set a breakpoint on _XError to stop when an
627 * X protocol error is generated.
628 */
629 XSynchronize( display, 1 );
630 }
631
632 v = (XMesaVisual) CALLOC_STRUCT(xmesa_visual);
633 if (!v) {
634 return NULL;
635 }
636
637 v->display = display;
638
639 /* Save a copy of the XVisualInfo struct because the user may Xfree()
640 * the struct but we may need some of the information contained in it
641 * at a later time.
642 */
643 v->visinfo = (XVisualInfo *) MALLOC(sizeof(*visinfo));
644 if (!v->visinfo) {
645 free(v);
646 return NULL;
647 }
648 memcpy(v->visinfo, visinfo, sizeof(*visinfo));
649
650 v->ximage_flag = ximage_flag;
651
652 v->mesa_visual.redMask = visinfo->red_mask;
653 v->mesa_visual.greenMask = visinfo->green_mask;
654 v->mesa_visual.blueMask = visinfo->blue_mask;
655 v->mesa_visual.visualID = visinfo->visualid;
656 v->mesa_visual.screen = visinfo->screen;
657
658 #if !(defined(__cplusplus) || defined(c_plusplus))
659 v->mesa_visual.visualType = xmesa_convert_from_x_visual_type(visinfo->class);
660 #else
661 v->mesa_visual.visualType = xmesa_convert_from_x_visual_type(visinfo->c_class);
662 #endif
663
664 v->mesa_visual.visualRating = visualCaveat;
665
666 if (alpha_flag)
667 v->mesa_visual.alphaBits = 8;
668
669 (void) initialize_visual_and_buffer( v, NULL, rgb_flag, 0, 0 );
670
671 {
672 const int xclass = v->mesa_visual.visualType;
673 if (xclass == GLX_TRUE_COLOR || xclass == GLX_DIRECT_COLOR) {
674 red_bits = _mesa_bitcount(GET_REDMASK(v));
675 green_bits = _mesa_bitcount(GET_GREENMASK(v));
676 blue_bits = _mesa_bitcount(GET_BLUEMASK(v));
677 }
678 else {
679 /* this is an approximation */
680 int depth;
681 depth = v->visinfo->depth;
682 red_bits = depth / 3;
683 depth -= red_bits;
684 green_bits = depth / 2;
685 depth -= green_bits;
686 blue_bits = depth;
687 alpha_bits = 0;
688 assert( red_bits + green_bits + blue_bits == v->visinfo->depth );
689 }
690 alpha_bits = v->mesa_visual.alphaBits;
691 }
692
693 _mesa_initialize_visual( &v->mesa_visual,
694 db_flag, stereo_flag,
695 red_bits, green_bits,
696 blue_bits, alpha_bits,
697 depth_size,
698 stencil_size,
699 accum_red_size, accum_green_size,
700 accum_blue_size, accum_alpha_size,
701 0 );
702
703 v->stvis.buffer_mask = ST_ATTACHMENT_FRONT_LEFT_MASK;
704 if (db_flag)
705 v->stvis.buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK;
706 if (stereo_flag) {
707 v->stvis.buffer_mask |= ST_ATTACHMENT_FRONT_RIGHT_MASK;
708 if (db_flag)
709 v->stvis.buffer_mask |= ST_ATTACHMENT_BACK_RIGHT_MASK;
710 }
711
712 v->stvis.color_format = choose_pixel_format(v);
713 v->stvis.depth_stencil_format =
714 choose_depth_stencil_format(xmdpy, depth_size, stencil_size);
715
716 v->stvis.accum_format = (accum_red_size +
717 accum_green_size + accum_blue_size + accum_alpha_size) ?
718 PIPE_FORMAT_R16G16B16A16_SNORM : PIPE_FORMAT_NONE;
719
720 v->stvis.samples = num_samples;
721 v->stvis.render_buffer = ST_ATTACHMENT_INVALID;
722
723 /* XXX minor hack */
724 v->mesa_visual.level = level;
725 return v;
726 }
727
728
729 PUBLIC
730 void XMesaDestroyVisual( XMesaVisual v )
731 {
732 free(v->visinfo);
733 free(v);
734 }
735
736
737 /**
738 * Do per-display initializations.
739 */
740 void
741 xmesa_init( Display *display )
742 {
743 xmesa_init_display(display);
744 }
745
746
747 /**
748 * Create a new XMesaContext.
749 * \param v the XMesaVisual
750 * \param share_list another XMesaContext with which to share display
751 * lists or NULL if no sharing is wanted.
752 * \return an XMesaContext or NULL if error.
753 */
754 PUBLIC
755 XMesaContext XMesaCreateContext( XMesaVisual v, XMesaContext share_list )
756 {
757 XMesaDisplay xmdpy = xmesa_init_display(v->display);
758 XMesaContext c;
759
760 if (!xmdpy)
761 return NULL;
762
763 /* Note: the XMesaContext contains a Mesa GLcontext struct (inheritance) */
764 c = (XMesaContext) CALLOC_STRUCT(xmesa_context);
765 if (!c)
766 return NULL;
767
768 c->xm_visual = v;
769 c->xm_buffer = NULL; /* set later by XMesaMakeCurrent */
770 c->xm_read_buffer = NULL;
771
772 c->st = stapi->create_context(stapi, xmdpy->smapi,
773 &v->stvis, (share_list) ? share_list->st : NULL);
774 if (c->st == NULL)
775 goto fail;
776
777 c->st->st_manager_private = (void *) c;
778
779 return c;
780
781 fail:
782 if (c->st)
783 c->st->destroy(c->st);
784
785 free(c);
786 return NULL;
787 }
788
789
790
791 PUBLIC
792 void XMesaDestroyContext( XMesaContext c )
793 {
794 c->st->destroy(c->st);
795
796 /* FIXME: We should destroy the screen here, but if we do so, surfaces may
797 * outlive it, causing segfaults
798 struct pipe_screen *screen = c->st->pipe->screen;
799 screen->destroy(screen);
800 */
801
802 free(c);
803 }
804
805
806
807 /**
808 * Private function for creating an XMesaBuffer which corresponds to an
809 * X window or pixmap.
810 * \param v the window's XMesaVisual
811 * \param w the window we're wrapping
812 * \return new XMesaBuffer or NULL if error
813 */
814 PUBLIC XMesaBuffer
815 XMesaCreateWindowBuffer(XMesaVisual v, Window w)
816 {
817 XWindowAttributes attr;
818 XMesaBuffer b;
819 Colormap cmap;
820 int depth;
821
822 assert(v);
823 assert(w);
824
825 /* Check that window depth matches visual depth */
826 XGetWindowAttributes( v->display, w, &attr );
827 depth = attr.depth;
828 if (v->visinfo->depth != depth) {
829 _mesa_warning(NULL, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
830 v->visinfo->depth, depth);
831 return NULL;
832 }
833
834 /* Find colormap */
835 if (attr.colormap) {
836 cmap = attr.colormap;
837 }
838 else {
839 _mesa_warning(NULL, "Window %u has no colormap!\n", (unsigned int) w);
840 /* this is weird, a window w/out a colormap!? */
841 /* OK, let's just allocate a new one and hope for the best */
842 cmap = XCreateColormap(v->display, w, attr.visual, AllocNone);
843 }
844
845 b = create_xmesa_buffer((Drawable) w, WINDOW, v, cmap);
846 if (!b)
847 return NULL;
848
849 if (!initialize_visual_and_buffer( v, b, v->mesa_visual.rgbMode,
850 (Drawable) w, cmap )) {
851 xmesa_free_buffer(b);
852 return NULL;
853 }
854
855 return b;
856 }
857
858
859
860 /**
861 * Create a new XMesaBuffer from an X pixmap.
862 *
863 * \param v the XMesaVisual
864 * \param p the pixmap
865 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
866 * \c GLX_DIRECT_COLOR visual for the pixmap
867 * \returns new XMesaBuffer or NULL if error
868 */
869 PUBLIC XMesaBuffer
870 XMesaCreatePixmapBuffer(XMesaVisual v, Pixmap p, Colormap cmap)
871 {
872 XMesaBuffer b;
873
874 assert(v);
875
876 b = create_xmesa_buffer((Drawable) p, PIXMAP, v, cmap);
877 if (!b)
878 return NULL;
879
880 if (!initialize_visual_and_buffer(v, b, v->mesa_visual.rgbMode,
881 (Drawable) p, cmap)) {
882 xmesa_free_buffer(b);
883 return NULL;
884 }
885
886 return b;
887 }
888
889
890 /**
891 * For GLX_EXT_texture_from_pixmap
892 */
893 XMesaBuffer
894 XMesaCreatePixmapTextureBuffer(XMesaVisual v, Pixmap p,
895 Colormap cmap,
896 int format, int target, int mipmap)
897 {
898 GET_CURRENT_CONTEXT(ctx);
899 XMesaBuffer b;
900
901 assert(v);
902
903 b = create_xmesa_buffer((Drawable) p, PIXMAP, v, cmap);
904 if (!b)
905 return NULL;
906
907 /* get pixmap size */
908 xmesa_get_window_size(v->display, b, &b->width, &b->height);
909
910 if (target == 0) {
911 /* examine dims */
912 if (ctx->Extensions.ARB_texture_non_power_of_two) {
913 target = GLX_TEXTURE_2D_EXT;
914 }
915 else if ( _mesa_bitcount(b->width) == 1
916 && _mesa_bitcount(b->height) == 1) {
917 /* power of two size */
918 if (b->height == 1) {
919 target = GLX_TEXTURE_1D_EXT;
920 }
921 else {
922 target = GLX_TEXTURE_2D_EXT;
923 }
924 }
925 else if (ctx->Extensions.NV_texture_rectangle) {
926 target = GLX_TEXTURE_RECTANGLE_EXT;
927 }
928 else {
929 /* non power of two textures not supported */
930 XMesaDestroyBuffer(b);
931 return 0;
932 }
933 }
934
935 b->TextureTarget = target;
936 b->TextureFormat = format;
937 b->TextureMipmap = mipmap;
938
939 if (!initialize_visual_and_buffer(v, b, v->mesa_visual.rgbMode,
940 (Drawable) p, cmap)) {
941 xmesa_free_buffer(b);
942 return NULL;
943 }
944
945 return b;
946 }
947
948
949
950 XMesaBuffer
951 XMesaCreatePBuffer(XMesaVisual v, Colormap cmap,
952 unsigned int width, unsigned int height)
953 {
954 Window root;
955 Drawable drawable; /* X Pixmap Drawable */
956 XMesaBuffer b;
957
958 /* allocate pixmap for front buffer */
959 root = RootWindow( v->display, v->visinfo->screen );
960 drawable = XCreatePixmap(v->display, root, width, height,
961 v->visinfo->depth);
962 if (!drawable)
963 return NULL;
964
965 b = create_xmesa_buffer(drawable, PBUFFER, v, cmap);
966 if (!b)
967 return NULL;
968
969 if (!initialize_visual_and_buffer(v, b, v->mesa_visual.rgbMode,
970 drawable, cmap)) {
971 xmesa_free_buffer(b);
972 return NULL;
973 }
974
975 return b;
976 }
977
978
979
980 /*
981 * Deallocate an XMesaBuffer structure and all related info.
982 */
983 PUBLIC void
984 XMesaDestroyBuffer(XMesaBuffer b)
985 {
986 xmesa_free_buffer(b);
987 }
988
989
990 /**
991 * Query the current drawable size and notify the binding context.
992 */
993 void
994 xmesa_check_buffer_size(XMesaBuffer b)
995 {
996 XMesaContext xmctx = XMesaGetCurrentContext();
997
998 if (b->type == PBUFFER)
999 return;
1000
1001 xmesa_get_window_size(b->xm_visual->display, b, &b->width, &b->height);
1002 if (xmctx && xmctx->xm_buffer == b)
1003 xmctx->st->notify_invalid_framebuffer(xmctx->st, b->stfb);
1004 }
1005
1006
1007 /*
1008 * Bind buffer b to context c and make c the current rendering context.
1009 */
1010 PUBLIC
1011 GLboolean XMesaMakeCurrent2( XMesaContext c, XMesaBuffer drawBuffer,
1012 XMesaBuffer readBuffer )
1013 {
1014 XMesaContext old_ctx = XMesaGetCurrentContext();
1015
1016 if (old_ctx && old_ctx != c) {
1017 XMesaFlush(old_ctx);
1018 old_ctx->xm_buffer = NULL;
1019 old_ctx->xm_read_buffer = NULL;
1020 }
1021
1022 if (c) {
1023 if (!drawBuffer || !readBuffer)
1024 return GL_FALSE; /* must specify buffers! */
1025
1026 if (c == old_ctx &&
1027 c->xm_buffer == drawBuffer &&
1028 c->xm_read_buffer == readBuffer)
1029 return GL_TRUE;
1030
1031 xmesa_check_buffer_size(drawBuffer);
1032 if (readBuffer != drawBuffer)
1033 xmesa_check_buffer_size(readBuffer);
1034
1035 c->xm_buffer = drawBuffer;
1036 c->xm_read_buffer = readBuffer;
1037
1038 stapi->make_current(stapi, c->st, drawBuffer->stfb, readBuffer->stfb);
1039
1040 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1041 drawBuffer->wasCurrent = GL_TRUE;
1042 }
1043 else {
1044 /* Detach */
1045 stapi->make_current(stapi, NULL, NULL, NULL);
1046
1047 }
1048 return GL_TRUE;
1049 }
1050
1051
1052 /*
1053 * Unbind the context c from its buffer.
1054 */
1055 GLboolean XMesaUnbindContext( XMesaContext c )
1056 {
1057 /* A no-op for XFree86 integration purposes */
1058 return GL_TRUE;
1059 }
1060
1061
1062 XMesaContext XMesaGetCurrentContext( void )
1063 {
1064 struct st_context_iface *st = stapi->get_current(stapi);
1065 return (XMesaContext) (st) ? st->st_manager_private : NULL;
1066 }
1067
1068
1069
1070 /**
1071 * Swap front and back color buffers and have winsys display front buffer.
1072 * If there's no front color buffer no swap actually occurs.
1073 */
1074 PUBLIC
1075 void XMesaSwapBuffers( XMesaBuffer b )
1076 {
1077 XMesaContext xmctx = XMesaGetCurrentContext();
1078
1079 if (xmctx && xmctx->xm_buffer == b) {
1080 xmctx->st->flush( xmctx->st,
1081 PIPE_FLUSH_RENDER_CACHE |
1082 PIPE_FLUSH_SWAPBUFFERS |
1083 PIPE_FLUSH_FRAME,
1084 NULL);
1085 }
1086
1087 xmesa_swap_st_framebuffer(b->stfb);
1088 }
1089
1090
1091
1092 /*
1093 * Copy sub-region of back buffer to front buffer
1094 */
1095 void XMesaCopySubBuffer( XMesaBuffer b, int x, int y, int width, int height )
1096 {
1097 xmesa_copy_st_framebuffer(b->stfb,
1098 ST_ATTACHMENT_BACK_LEFT, ST_ATTACHMENT_FRONT_LEFT,
1099 x, y, width, height);
1100 }
1101
1102
1103
1104 void XMesaFlush( XMesaContext c )
1105 {
1106 if (c && c->xm_visual->display) {
1107 XMesaDisplay xmdpy = xmesa_init_display(c->xm_visual->display);
1108 struct pipe_fence_handle *fence = NULL;
1109
1110 c->st->flush(c->st, PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, &fence);
1111 if (fence) {
1112 xmdpy->screen->fence_finish(xmdpy->screen, fence, 0);
1113 xmdpy->screen->fence_reference(xmdpy->screen, &fence, NULL);
1114 }
1115 XSync( c->xm_visual->display, False );
1116 }
1117 }
1118
1119
1120
1121
1122
1123 XMesaBuffer XMesaFindBuffer( Display *dpy, Drawable d )
1124 {
1125 XMesaBuffer b;
1126 for (b = XMesaBufferList; b; b = b->Next) {
1127 if (b->ws.drawable == d && b->xm_visual->display == dpy) {
1128 return b;
1129 }
1130 }
1131 return NULL;
1132 }
1133
1134
1135 /**
1136 * Free/destroy all XMesaBuffers associated with given display.
1137 */
1138 void xmesa_destroy_buffers_on_display(Display *dpy)
1139 {
1140 XMesaBuffer b, next;
1141 for (b = XMesaBufferList; b; b = next) {
1142 next = b->Next;
1143 if (b->xm_visual->display == dpy) {
1144 xmesa_free_buffer(b);
1145 }
1146 }
1147 }
1148
1149
1150 /*
1151 * Look for XMesaBuffers whose X window has been destroyed.
1152 * Deallocate any such XMesaBuffers.
1153 */
1154 void XMesaGarbageCollect( void )
1155 {
1156 XMesaBuffer b, next;
1157 for (b=XMesaBufferList; b; b=next) {
1158 next = b->Next;
1159 if (b->xm_visual &&
1160 b->xm_visual->display &&
1161 b->ws.drawable &&
1162 b->type == WINDOW) {
1163 XSync(b->xm_visual->display, False);
1164 if (!window_exists( b->xm_visual->display, b->ws.drawable )) {
1165 /* found a dead window, free the ancillary info */
1166 XMesaDestroyBuffer( b );
1167 }
1168 }
1169 }
1170 }
1171
1172
1173
1174
1175 PUBLIC void
1176 XMesaBindTexImage(Display *dpy, XMesaBuffer drawable, int buffer,
1177 const int *attrib_list)
1178 {
1179 }
1180
1181
1182
1183 PUBLIC void
1184 XMesaReleaseTexImage(Display *dpy, XMesaBuffer drawable, int buffer)
1185 {
1186 }
1187
1188
1189 void
1190 XMesaCopyContext(XMesaContext src, XMesaContext dst, unsigned long mask)
1191 {
1192 if (dst->st->copy)
1193 dst->st->copy(dst->st, src->st, mask);
1194 }