Move compiler.h and imports.h/c from src/mesa/main into src/util
[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 "util/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 window the window/pixmap we're rendering into
640 * \param cmap the colormap associated with the window/pixmap
641 * \return GL_TRUE=success, GL_FALSE=failure
642 */
643 static GLboolean
644 initialize_visual_and_buffer(XMesaVisual v, XMesaBuffer b,
645 Drawable window, Colormap cmap)
646 {
647 assert(!b || b->xm_visual == v);
648
649 /* Save true bits/pixel */
650 v->BitsPerPixel = bits_per_pixel(v);
651 assert(v->BitsPerPixel > 0);
652
653 /* RGB WINDOW:
654 * We support RGB rendering into almost any kind of visual.
655 */
656 const int xclass = v->visualType;
657 if (xclass != GLX_TRUE_COLOR && xclass != GLX_DIRECT_COLOR) {
658 _mesa_warning(NULL,
659 "XMesa: RGB mode rendering not supported in given visual.\n");
660 return GL_FALSE;
661 }
662
663 if (v->BitsPerPixel == 32) {
664 /* We use XImages for all front/back buffers. If an X Window or
665 * X Pixmap is 32bpp, there's no guarantee that the alpha channel
666 * will be preserved. For XImages we're in luck.
667 */
668 v->mesa_visual.alphaBits = 8;
669 }
670
671 /*
672 * If MESA_INFO env var is set print out some debugging info
673 * which can help Brian figure out what's going on when a user
674 * reports bugs.
675 */
676 if (getenv("MESA_INFO")) {
677 printf("X/Mesa visual = %p\n", (void *) v);
678 printf("X/Mesa level = %d\n", v->mesa_visual.level);
679 printf("X/Mesa depth = %d\n", v->visinfo->depth);
680 printf("X/Mesa bits per pixel = %d\n", v->BitsPerPixel);
681 }
682
683 return GL_TRUE;
684 }
685
686
687
688 #define NUM_VISUAL_TYPES 6
689
690 /**
691 * Convert an X visual type to a GLX visual type.
692 *
693 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
694 * to be converted.
695 * \return If \c visualType is a valid X visual type, a GLX visual type will
696 * be returned. Otherwise \c GLX_NONE will be returned.
697 *
698 * \note
699 * This code was lifted directly from lib/GL/glx/glcontextmodes.c in the
700 * DRI CVS tree.
701 */
702 static GLint
703 xmesa_convert_from_x_visual_type( int visualType )
704 {
705 static const int glx_visual_types[ NUM_VISUAL_TYPES ] = {
706 GLX_STATIC_GRAY, GLX_GRAY_SCALE,
707 GLX_STATIC_COLOR, GLX_PSEUDO_COLOR,
708 GLX_TRUE_COLOR, GLX_DIRECT_COLOR
709 };
710
711 return ( (unsigned) visualType < NUM_VISUAL_TYPES )
712 ? glx_visual_types[ visualType ] : GLX_NONE;
713 }
714
715
716 /**********************************************************************/
717 /***** Public Functions *****/
718 /**********************************************************************/
719
720
721 /*
722 * Create a new X/Mesa visual.
723 * Input: display - X11 display
724 * visinfo - an XVisualInfo pointer
725 * rgb_flag - GL_TRUE = RGB mode,
726 * GL_FALSE = color index mode
727 * alpha_flag - alpha buffer requested?
728 * db_flag - GL_TRUE = double-buffered,
729 * GL_FALSE = single buffered
730 * stereo_flag - stereo visual?
731 * ximage_flag - GL_TRUE = use an XImage for back buffer,
732 * GL_FALSE = use an off-screen pixmap for back buffer
733 * depth_size - requested bits/depth values, or zero
734 * stencil_size - requested bits/stencil values, or zero
735 * accum_red_size - requested bits/red accum values, or zero
736 * accum_green_size - requested bits/green accum values, or zero
737 * accum_blue_size - requested bits/blue accum values, or zero
738 * accum_alpha_size - requested bits/alpha accum values, or zero
739 * num_samples - number of samples/pixel if multisampling, or zero
740 * level - visual level, usually 0
741 * visualCaveat - ala the GLX extension, usually GLX_NONE
742 * Return; a new XMesaVisual or 0 if error.
743 */
744 PUBLIC
745 XMesaVisual XMesaCreateVisual( Display *display,
746 XVisualInfo * visinfo,
747 GLboolean rgb_flag,
748 GLboolean alpha_flag,
749 GLboolean db_flag,
750 GLboolean stereo_flag,
751 GLboolean ximage_flag,
752 GLint depth_size,
753 GLint stencil_size,
754 GLint accum_red_size,
755 GLint accum_green_size,
756 GLint accum_blue_size,
757 GLint accum_alpha_size,
758 GLint num_samples,
759 GLint level,
760 GLint visualCaveat )
761 {
762 XMesaDisplay xmdpy = xmesa_init_display(display);
763 XMesaVisual v;
764 GLint red_bits, green_bits, blue_bits, alpha_bits;
765
766 if (!xmdpy)
767 return NULL;
768
769 if (!rgb_flag)
770 return NULL;
771
772 /* For debugging only */
773 if (getenv("MESA_XSYNC")) {
774 /* This makes debugging X easier.
775 * In your debugger, set a breakpoint on _XError to stop when an
776 * X protocol error is generated.
777 */
778 XSynchronize( display, 1 );
779 }
780
781 v = (XMesaVisual) CALLOC_STRUCT(xmesa_visual);
782 if (!v) {
783 return NULL;
784 }
785
786 v->display = display;
787
788 /* Save a copy of the XVisualInfo struct because the user may Xfree()
789 * the struct but we may need some of the information contained in it
790 * at a later time.
791 */
792 v->visinfo = malloc(sizeof(*visinfo));
793 if (!v->visinfo) {
794 free(v);
795 return NULL;
796 }
797 memcpy(v->visinfo, visinfo, sizeof(*visinfo));
798
799 v->ximage_flag = ximage_flag;
800
801 v->mesa_visual.redMask = visinfo->red_mask;
802 v->mesa_visual.greenMask = visinfo->green_mask;
803 v->mesa_visual.blueMask = visinfo->blue_mask;
804 v->visualID = visinfo->visualid;
805 v->screen = visinfo->screen;
806
807 #if !(defined(__cplusplus) || defined(c_plusplus))
808 v->visualType = xmesa_convert_from_x_visual_type(visinfo->class);
809 #else
810 v->visualType = xmesa_convert_from_x_visual_type(visinfo->c_class);
811 #endif
812
813 v->mesa_visual.visualRating = visualCaveat;
814
815 if (alpha_flag)
816 v->mesa_visual.alphaBits = 8;
817
818 (void) initialize_visual_and_buffer( v, NULL, 0, 0 );
819
820 {
821 const int xclass = v->visualType;
822 if (xclass == GLX_TRUE_COLOR || xclass == GLX_DIRECT_COLOR) {
823 red_bits = util_bitcount(GET_REDMASK(v));
824 green_bits = util_bitcount(GET_GREENMASK(v));
825 blue_bits = util_bitcount(GET_BLUEMASK(v));
826 }
827 else {
828 /* this is an approximation */
829 int depth;
830 depth = v->visinfo->depth;
831 red_bits = depth / 3;
832 depth -= red_bits;
833 green_bits = depth / 2;
834 depth -= green_bits;
835 blue_bits = depth;
836 alpha_bits = 0;
837 assert( red_bits + green_bits + blue_bits == v->visinfo->depth );
838 }
839 alpha_bits = v->mesa_visual.alphaBits;
840 }
841
842 /* initialize visual */
843 {
844 struct gl_config *vis = &v->mesa_visual;
845
846 vis->doubleBufferMode = db_flag;
847 vis->stereoMode = stereo_flag;
848
849 vis->redBits = red_bits;
850 vis->greenBits = green_bits;
851 vis->blueBits = blue_bits;
852 vis->alphaBits = alpha_bits;
853 vis->rgbBits = red_bits + green_bits + blue_bits;
854
855 vis->depthBits = depth_size;
856 vis->stencilBits = stencil_size;
857
858 vis->accumRedBits = accum_red_size;
859 vis->accumGreenBits = accum_green_size;
860 vis->accumBlueBits = accum_blue_size;
861 vis->accumAlphaBits = accum_alpha_size;
862
863 vis->numAuxBuffers = 0;
864 vis->level = 0;
865 vis->sampleBuffers = num_samples > 1;
866 vis->samples = num_samples;
867 }
868
869 v->stvis.buffer_mask = ST_ATTACHMENT_FRONT_LEFT_MASK;
870 if (db_flag)
871 v->stvis.buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK;
872 if (stereo_flag) {
873 v->stvis.buffer_mask |= ST_ATTACHMENT_FRONT_RIGHT_MASK;
874 if (db_flag)
875 v->stvis.buffer_mask |= ST_ATTACHMENT_BACK_RIGHT_MASK;
876 }
877
878 v->stvis.color_format = choose_pixel_format(v);
879
880 /* Check format support at requested num_samples (for multisample) */
881 if (!xmdpy->screen->is_format_supported(xmdpy->screen,
882 v->stvis.color_format,
883 PIPE_TEXTURE_2D, num_samples,
884 num_samples,
885 PIPE_BIND_RENDER_TARGET))
886 v->stvis.color_format = PIPE_FORMAT_NONE;
887
888 if (v->stvis.color_format == PIPE_FORMAT_NONE) {
889 free(v->visinfo);
890 free(v);
891 return NULL;
892 }
893
894 v->stvis.depth_stencil_format =
895 choose_depth_stencil_format(xmdpy, depth_size, stencil_size,
896 num_samples);
897
898 v->stvis.accum_format = (accum_red_size +
899 accum_green_size + accum_blue_size + accum_alpha_size) ?
900 PIPE_FORMAT_R16G16B16A16_SNORM : PIPE_FORMAT_NONE;
901
902 v->stvis.samples = num_samples;
903 v->stvis.render_buffer = ST_ATTACHMENT_INVALID;
904
905 /* XXX minor hack */
906 v->mesa_visual.level = level;
907 return v;
908 }
909
910
911 PUBLIC
912 void XMesaDestroyVisual( XMesaVisual v )
913 {
914 free(v->visinfo);
915 free(v);
916 }
917
918
919 /**
920 * Return the informative name.
921 */
922 const char *
923 xmesa_get_name(void)
924 {
925 return stapi->name;
926 }
927
928
929 /**
930 * Do per-display initializations.
931 */
932 int
933 xmesa_init( Display *display )
934 {
935 return xmesa_init_display(display) ? 0 : 1;
936 }
937
938
939 /**
940 * Create a new XMesaContext.
941 * \param v the XMesaVisual
942 * \param share_list another XMesaContext with which to share display
943 * lists or NULL if no sharing is wanted.
944 * \return an XMesaContext or NULL if error.
945 */
946 PUBLIC
947 XMesaContext XMesaCreateContext( XMesaVisual v, XMesaContext share_list,
948 GLuint major, GLuint minor,
949 GLuint profileMask, GLuint contextFlags)
950 {
951 XMesaDisplay xmdpy = xmesa_init_display(v->display);
952 struct st_context_attribs attribs;
953 enum st_context_error ctx_err = 0;
954 XMesaContext c;
955
956 if (!xmdpy)
957 goto no_xmesa_context;
958
959 /* Note: the XMesaContext contains a Mesa struct gl_context struct (inheritance) */
960 c = (XMesaContext) CALLOC_STRUCT(xmesa_context);
961 if (!c)
962 goto no_xmesa_context;
963
964 c->xm_visual = v;
965 c->xm_buffer = NULL; /* set later by XMesaMakeCurrent */
966 c->xm_read_buffer = NULL;
967
968 memset(&attribs, 0, sizeof(attribs));
969 attribs.visual = v->stvis;
970 attribs.major = major;
971 attribs.minor = minor;
972 if (contextFlags & GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB)
973 attribs.flags |= ST_CONTEXT_FLAG_FORWARD_COMPATIBLE;
974 if (contextFlags & GLX_CONTEXT_DEBUG_BIT_ARB)
975 attribs.flags |= ST_CONTEXT_FLAG_DEBUG;
976 if (contextFlags & GLX_CONTEXT_ROBUST_ACCESS_BIT_ARB)
977 attribs.flags |= ST_CONTEXT_FLAG_ROBUST_ACCESS;
978
979 switch (profileMask) {
980 case GLX_CONTEXT_CORE_PROFILE_BIT_ARB:
981 /* There are no profiles before OpenGL 3.2. The
982 * GLX_ARB_create_context_profile spec says:
983 *
984 * "If the requested OpenGL version is less than 3.2,
985 * GLX_CONTEXT_PROFILE_MASK_ARB is ignored and the functionality
986 * of the context is determined solely by the requested version."
987 */
988 if (major > 3 || (major == 3 && minor >= 2)) {
989 attribs.profile = ST_PROFILE_OPENGL_CORE;
990 break;
991 }
992 /* fall-through */
993 case GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB:
994 /*
995 * The spec also says:
996 *
997 * "If version 3.1 is requested, the context returned may implement
998 * any of the following versions:
999 *
1000 * * Version 3.1. The GL_ARB_compatibility extension may or may not
1001 * be implemented, as determined by the implementation.
1002 * * The core profile of version 3.2 or greater."
1003 *
1004 * and because Mesa doesn't support GL_ARB_compatibility, the only chance to
1005 * honour a 3.1 context is through core profile.
1006 */
1007 if (major == 3 && minor == 1) {
1008 attribs.profile = ST_PROFILE_OPENGL_CORE;
1009 } else {
1010 attribs.profile = ST_PROFILE_DEFAULT;
1011 }
1012 break;
1013 case GLX_CONTEXT_ES_PROFILE_BIT_EXT:
1014 if (major >= 2) {
1015 attribs.profile = ST_PROFILE_OPENGL_ES2;
1016 } else {
1017 attribs.profile = ST_PROFILE_OPENGL_ES1;
1018 }
1019 break;
1020 default:
1021 assert(0);
1022 goto no_st;
1023 }
1024
1025 c->st = stapi->create_context(stapi, xmdpy->smapi, &attribs,
1026 &ctx_err, (share_list) ? share_list->st : NULL);
1027 if (c->st == NULL)
1028 goto no_st;
1029
1030 c->st->st_manager_private = (void *) c;
1031
1032 c->hud = hud_create(c->st->cso_context, NULL);
1033
1034 return c;
1035
1036 no_st:
1037 free(c);
1038 no_xmesa_context:
1039 return NULL;
1040 }
1041
1042
1043
1044 PUBLIC
1045 void XMesaDestroyContext( XMesaContext c )
1046 {
1047 if (c->hud) {
1048 hud_destroy(c->hud, NULL);
1049 }
1050
1051 c->st->destroy(c->st);
1052
1053 /* FIXME: We should destroy the screen here, but if we do so, surfaces may
1054 * outlive it, causing segfaults
1055 struct pipe_screen *screen = c->st->pipe->screen;
1056 screen->destroy(screen);
1057 */
1058
1059 free(c);
1060 }
1061
1062
1063
1064 /**
1065 * Private function for creating an XMesaBuffer which corresponds to an
1066 * X window or pixmap.
1067 * \param v the window's XMesaVisual
1068 * \param w the window we're wrapping
1069 * \return new XMesaBuffer or NULL if error
1070 */
1071 PUBLIC XMesaBuffer
1072 XMesaCreateWindowBuffer(XMesaVisual v, Window w)
1073 {
1074 XWindowAttributes attr;
1075 XMesaBuffer b;
1076 Colormap cmap;
1077 int depth;
1078
1079 assert(v);
1080 assert(w);
1081
1082 /* Check that window depth matches visual depth */
1083 XGetWindowAttributes( v->display, w, &attr );
1084 depth = attr.depth;
1085 if (v->visinfo->depth != depth) {
1086 _mesa_warning(NULL, "XMesaCreateWindowBuffer: depth mismatch between visual (%d) and window (%d)!\n",
1087 v->visinfo->depth, depth);
1088 return NULL;
1089 }
1090
1091 /* Find colormap */
1092 if (attr.colormap) {
1093 cmap = attr.colormap;
1094 }
1095 else {
1096 _mesa_warning(NULL, "Window %u has no colormap!\n", (unsigned int) w);
1097 /* this is weird, a window w/out a colormap!? */
1098 /* OK, let's just allocate a new one and hope for the best */
1099 cmap = XCreateColormap(v->display, w, attr.visual, AllocNone);
1100 }
1101
1102 b = create_xmesa_buffer((Drawable) w, WINDOW, v, cmap);
1103 if (!b)
1104 return NULL;
1105
1106 if (!initialize_visual_and_buffer( v, b, (Drawable) w, cmap )) {
1107 xmesa_free_buffer(b);
1108 return NULL;
1109 }
1110
1111 return b;
1112 }
1113
1114
1115
1116 /**
1117 * Create a new XMesaBuffer from an X pixmap.
1118 *
1119 * \param v the XMesaVisual
1120 * \param p the pixmap
1121 * \param cmap the colormap, may be 0 if using a \c GLX_TRUE_COLOR or
1122 * \c GLX_DIRECT_COLOR visual for the pixmap
1123 * \returns new XMesaBuffer or NULL if error
1124 */
1125 PUBLIC XMesaBuffer
1126 XMesaCreatePixmapBuffer(XMesaVisual v, Pixmap p, Colormap cmap)
1127 {
1128 XMesaBuffer b;
1129
1130 assert(v);
1131
1132 b = create_xmesa_buffer((Drawable) p, PIXMAP, v, cmap);
1133 if (!b)
1134 return NULL;
1135
1136 if (!initialize_visual_and_buffer(v, b, (Drawable) p, cmap)) {
1137 xmesa_free_buffer(b);
1138 return NULL;
1139 }
1140
1141 return b;
1142 }
1143
1144
1145 /**
1146 * For GLX_EXT_texture_from_pixmap
1147 */
1148 XMesaBuffer
1149 XMesaCreatePixmapTextureBuffer(XMesaVisual v, Pixmap p,
1150 Colormap cmap,
1151 int format, int target, int mipmap)
1152 {
1153 GET_CURRENT_CONTEXT(ctx);
1154 XMesaBuffer b;
1155
1156 assert(v);
1157
1158 b = create_xmesa_buffer((Drawable) p, PIXMAP, v, cmap);
1159 if (!b)
1160 return NULL;
1161
1162 /* get pixmap size */
1163 xmesa_get_window_size(v->display, b, &b->width, &b->height);
1164
1165 if (target == 0) {
1166 /* examine dims */
1167 if (ctx->Extensions.ARB_texture_non_power_of_two) {
1168 target = GLX_TEXTURE_2D_EXT;
1169 }
1170 else if ( util_bitcount(b->width) == 1
1171 && util_bitcount(b->height) == 1) {
1172 /* power of two size */
1173 if (b->height == 1) {
1174 target = GLX_TEXTURE_1D_EXT;
1175 }
1176 else {
1177 target = GLX_TEXTURE_2D_EXT;
1178 }
1179 }
1180 else if (ctx->Extensions.NV_texture_rectangle) {
1181 target = GLX_TEXTURE_RECTANGLE_EXT;
1182 }
1183 else {
1184 /* non power of two textures not supported */
1185 XMesaDestroyBuffer(b);
1186 return 0;
1187 }
1188 }
1189
1190 b->TextureTarget = target;
1191 b->TextureFormat = format;
1192 b->TextureMipmap = mipmap;
1193
1194 if (!initialize_visual_and_buffer(v, b, (Drawable) p, cmap)) {
1195 xmesa_free_buffer(b);
1196 return NULL;
1197 }
1198
1199 return b;
1200 }
1201
1202
1203
1204 XMesaBuffer
1205 XMesaCreatePBuffer(XMesaVisual v, Colormap cmap,
1206 unsigned int width, unsigned int height)
1207 {
1208 Window root;
1209 Drawable drawable; /* X Pixmap Drawable */
1210 XMesaBuffer b;
1211
1212 /* allocate pixmap for front buffer */
1213 root = RootWindow( v->display, v->visinfo->screen );
1214 drawable = XCreatePixmap(v->display, root, width, height,
1215 v->visinfo->depth);
1216 if (!drawable)
1217 return NULL;
1218
1219 b = create_xmesa_buffer(drawable, PBUFFER, v, cmap);
1220 if (!b)
1221 return NULL;
1222
1223 if (!initialize_visual_and_buffer(v, b, drawable, cmap)) {
1224 xmesa_free_buffer(b);
1225 return NULL;
1226 }
1227
1228 return b;
1229 }
1230
1231
1232
1233 /*
1234 * Deallocate an XMesaBuffer structure and all related info.
1235 */
1236 PUBLIC void
1237 XMesaDestroyBuffer(XMesaBuffer b)
1238 {
1239 xmesa_free_buffer(b);
1240 }
1241
1242
1243 /**
1244 * Notify the binding context to validate the buffer.
1245 */
1246 void
1247 xmesa_notify_invalid_buffer(XMesaBuffer b)
1248 {
1249 p_atomic_inc(&b->stfb->stamp);
1250 }
1251
1252
1253 /**
1254 * Query the current drawable size and notify the binding context.
1255 */
1256 void
1257 xmesa_check_buffer_size(XMesaBuffer b)
1258 {
1259 GLuint old_width, old_height;
1260
1261 if (!b)
1262 return;
1263
1264 if (b->type == PBUFFER)
1265 return;
1266
1267 old_width = b->width;
1268 old_height = b->height;
1269
1270 xmesa_get_window_size(b->xm_visual->display, b, &b->width, &b->height);
1271
1272 if (b->width != old_width || b->height != old_height)
1273 xmesa_notify_invalid_buffer(b);
1274 }
1275
1276
1277 /*
1278 * Bind buffer b to context c and make c the current rendering context.
1279 */
1280 PUBLIC
1281 GLboolean XMesaMakeCurrent2( XMesaContext c, XMesaBuffer drawBuffer,
1282 XMesaBuffer readBuffer )
1283 {
1284 XMesaContext old_ctx = XMesaGetCurrentContext();
1285
1286 if (old_ctx && old_ctx != c) {
1287 XMesaFlush(old_ctx);
1288 old_ctx->xm_buffer = NULL;
1289 old_ctx->xm_read_buffer = NULL;
1290 }
1291
1292 if (c) {
1293 if (!drawBuffer != !readBuffer) {
1294 return GL_FALSE; /* must specify zero or two buffers! */
1295 }
1296
1297 if (c == old_ctx &&
1298 c->xm_buffer == drawBuffer &&
1299 c->xm_read_buffer == readBuffer)
1300 return GL_TRUE;
1301
1302 xmesa_check_buffer_size(drawBuffer);
1303 if (readBuffer != drawBuffer)
1304 xmesa_check_buffer_size(readBuffer);
1305
1306 c->xm_buffer = drawBuffer;
1307 c->xm_read_buffer = readBuffer;
1308
1309 stapi->make_current(stapi, c->st,
1310 drawBuffer ? drawBuffer->stfb : NULL,
1311 readBuffer ? readBuffer->stfb : NULL);
1312
1313 /* Solution to Stephane Rehel's problem with glXReleaseBuffersMESA(): */
1314 if (drawBuffer)
1315 drawBuffer->wasCurrent = GL_TRUE;
1316 }
1317 else {
1318 /* Detach */
1319 stapi->make_current(stapi, NULL, NULL, NULL);
1320
1321 }
1322 return GL_TRUE;
1323 }
1324
1325
1326 /*
1327 * Unbind the context c from its buffer.
1328 */
1329 GLboolean XMesaUnbindContext( XMesaContext c )
1330 {
1331 /* A no-op for XFree86 integration purposes */
1332 return GL_TRUE;
1333 }
1334
1335
1336 XMesaContext XMesaGetCurrentContext( void )
1337 {
1338 struct st_context_iface *st = stapi->get_current(stapi);
1339 return (XMesaContext) (st) ? st->st_manager_private : NULL;
1340 }
1341
1342
1343
1344 /**
1345 * Swap front and back color buffers and have winsys display front buffer.
1346 * If there's no front color buffer no swap actually occurs.
1347 */
1348 PUBLIC
1349 void XMesaSwapBuffers( XMesaBuffer b )
1350 {
1351 XMesaContext xmctx = XMesaGetCurrentContext();
1352
1353 /* Need to draw HUD before flushing */
1354 if (xmctx && xmctx->hud) {
1355 struct pipe_resource *back =
1356 xmesa_get_framebuffer_resource(b->stfb, ST_ATTACHMENT_BACK_LEFT);
1357 hud_run(xmctx->hud, NULL, back);
1358 }
1359
1360 if (xmctx && xmctx->xm_buffer == b) {
1361 xmctx->st->flush( xmctx->st, ST_FLUSH_FRONT, NULL, NULL, NULL);
1362 }
1363
1364 xmesa_swap_st_framebuffer(b->stfb);
1365 }
1366
1367
1368
1369 /*
1370 * Copy sub-region of back buffer to front buffer
1371 */
1372 void XMesaCopySubBuffer( XMesaBuffer b, int x, int y, int width, int height )
1373 {
1374 XMesaContext xmctx = XMesaGetCurrentContext();
1375
1376 xmctx->st->flush( xmctx->st, ST_FLUSH_FRONT, NULL, NULL, NULL);
1377
1378 xmesa_copy_st_framebuffer(b->stfb,
1379 ST_ATTACHMENT_BACK_LEFT, ST_ATTACHMENT_FRONT_LEFT,
1380 x, b->height - y - height, width, height);
1381 }
1382
1383
1384
1385 void XMesaFlush( XMesaContext c )
1386 {
1387 if (c && c->xm_visual->display) {
1388 XMesaDisplay xmdpy = xmesa_init_display(c->xm_visual->display);
1389 struct pipe_fence_handle *fence = NULL;
1390
1391 c->st->flush(c->st, ST_FLUSH_FRONT, &fence, NULL, NULL);
1392 if (fence) {
1393 xmdpy->screen->fence_finish(xmdpy->screen, NULL, fence,
1394 PIPE_TIMEOUT_INFINITE);
1395 xmdpy->screen->fence_reference(xmdpy->screen, &fence, NULL);
1396 }
1397 XFlush( c->xm_visual->display );
1398 }
1399 }
1400
1401
1402
1403
1404
1405 XMesaBuffer XMesaFindBuffer( Display *dpy, Drawable d )
1406 {
1407 XMesaBuffer b;
1408 for (b = XMesaBufferList; b; b = b->Next) {
1409 if (b->ws.drawable == d && b->xm_visual->display == dpy) {
1410 return b;
1411 }
1412 }
1413 return NULL;
1414 }
1415
1416
1417 /**
1418 * Free/destroy all XMesaBuffers associated with given display.
1419 */
1420 void xmesa_destroy_buffers_on_display(Display *dpy)
1421 {
1422 XMesaBuffer b, next;
1423 for (b = XMesaBufferList; b; b = next) {
1424 next = b->Next;
1425 if (b->xm_visual->display == dpy) {
1426 xmesa_free_buffer(b);
1427 /* delete head of list? */
1428 if (XMesaBufferList == b) {
1429 XMesaBufferList = next;
1430 }
1431 }
1432 }
1433 }
1434
1435
1436 /*
1437 * Look for XMesaBuffers whose X window has been destroyed.
1438 * Deallocate any such XMesaBuffers.
1439 */
1440 void XMesaGarbageCollect( void )
1441 {
1442 XMesaBuffer b, next;
1443 for (b=XMesaBufferList; b; b=next) {
1444 next = b->Next;
1445 if (b->xm_visual &&
1446 b->xm_visual->display &&
1447 b->ws.drawable &&
1448 b->type == WINDOW) {
1449 XSync(b->xm_visual->display, False);
1450 if (!window_exists( b->xm_visual->display, b->ws.drawable )) {
1451 /* found a dead window, free the ancillary info */
1452 XMesaDestroyBuffer( b );
1453 }
1454 }
1455 }
1456 }
1457
1458
1459 static enum st_attachment_type xmesa_attachment_type(int glx_attachment)
1460 {
1461 switch(glx_attachment) {
1462 case GLX_FRONT_LEFT_EXT:
1463 return ST_ATTACHMENT_FRONT_LEFT;
1464 case GLX_FRONT_RIGHT_EXT:
1465 return ST_ATTACHMENT_FRONT_RIGHT;
1466 case GLX_BACK_LEFT_EXT:
1467 return ST_ATTACHMENT_BACK_LEFT;
1468 case GLX_BACK_RIGHT_EXT:
1469 return ST_ATTACHMENT_BACK_RIGHT;
1470 default:
1471 assert(0);
1472 return ST_ATTACHMENT_FRONT_LEFT;
1473 }
1474 }
1475
1476
1477 PUBLIC void
1478 XMesaBindTexImage(Display *dpy, XMesaBuffer drawable, int buffer,
1479 const int *attrib_list)
1480 {
1481 struct st_context_iface *st = stapi->get_current(stapi);
1482 struct st_framebuffer_iface* stfbi = drawable->stfb;
1483 struct pipe_resource *res;
1484 int x, y, w, h;
1485 enum st_attachment_type st_attachment = xmesa_attachment_type(buffer);
1486
1487 x = 0;
1488 y = 0;
1489 w = drawable->width;
1490 h = drawable->height;
1491
1492 /* We need to validate our attachments before using them,
1493 * in case the texture doesn't exist yet. */
1494 xmesa_st_framebuffer_validate_textures(stfbi, w, h, 1 << st_attachment);
1495 res = xmesa_get_attachment(stfbi, st_attachment);
1496
1497 if (res) {
1498 struct pipe_context* pipe = xmesa_get_context(stfbi);
1499 enum pipe_format internal_format = res->format;
1500 struct pipe_transfer *tex_xfer;
1501 char *map;
1502 int line, byte_width;
1503 XImage *img;
1504
1505 internal_format = choose_pixel_format(drawable->xm_visual);
1506
1507 map = pipe_transfer_map(pipe, res,
1508 0, 0, /* level, layer */
1509 PIPE_TRANSFER_WRITE,
1510 x, y,
1511 w, h, &tex_xfer);
1512 if (!map)
1513 return;
1514
1515 /* Grab the XImage that we want to turn into a texture. */
1516 img = XGetImage(dpy,
1517 drawable->ws.drawable,
1518 x, y,
1519 w, h,
1520 AllPlanes,
1521 ZPixmap);
1522
1523 if (!img) {
1524 pipe_transfer_unmap(pipe, tex_xfer);
1525 return;
1526 }
1527
1528 /* The pipe transfer has a pitch rounded up to the nearest 64 pixels. */
1529 byte_width = w * ((img->bits_per_pixel + 7) / 8);
1530
1531 for (line = 0; line < h; line++)
1532 memcpy(&map[line * tex_xfer->stride],
1533 &img->data[line * img->bytes_per_line],
1534 byte_width);
1535
1536 pipe_transfer_unmap(pipe, tex_xfer);
1537
1538 st->teximage(st,
1539 ST_TEXTURE_2D,
1540 0, /* level */
1541 internal_format,
1542 res,
1543 FALSE /* no mipmap */);
1544
1545 }
1546 }
1547
1548
1549
1550 PUBLIC void
1551 XMesaReleaseTexImage(Display *dpy, XMesaBuffer drawable, int buffer)
1552 {
1553 }
1554
1555
1556 void
1557 XMesaCopyContext(XMesaContext src, XMesaContext dst, unsigned long mask)
1558 {
1559 if (dst->st->copy)
1560 dst->st->copy(dst->st, src->st, mask);
1561 }