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