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