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