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