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