1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Bismarck, ND., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
27 **************************************************************************/
41 #include "pipe/p_format.h"
42 #include "pipe/p_context.h"
43 #include "util/u_inlines.h"
44 #include "util/u_format.h"
45 #include "util/u_math.h"
46 #include "util/u_memory.h"
48 #include "state_tracker/xlib_sw_winsys.h"
53 #include <X11/Xlibint.h>
54 #include <X11/Xutil.h>
57 #include <X11/extensions/XShm.h>
60 * Subclass of pipe_buffer for Xlib winsys.
61 * Low-level OS/window system memory buffer
63 struct xm_displaytarget
65 enum pipe_format format
;
77 XShmSegmentInfo shminfo
;
83 * Subclass of sw_winsys for Xlib winsys
87 struct sw_winsys base
;
97 static INLINE
struct xm_displaytarget
*
98 xm_displaytarget( struct sw_displaytarget
*dt
)
100 return (struct xm_displaytarget
*)dt
;
105 * X Shared Memory Image extension code
110 static volatile int mesaXErrorFlag
= 0;
113 * Catches potential Xlib errors.
116 mesaHandleXError(Display
*dpy
, XErrorEvent
*event
)
125 static char *alloc_shm(struct xm_displaytarget
*buf
, unsigned size
)
127 XShmSegmentInfo
*const shminfo
= & buf
->shminfo
;
129 shminfo
->shmid
= shmget(IPC_PRIVATE
, size
, IPC_CREAT
|0777);
130 if (shminfo
->shmid
< 0) {
134 shminfo
->shmaddr
= (char *) shmat(shminfo
->shmid
, 0, 0);
135 if (shminfo
->shmaddr
== (char *) -1) {
136 shmctl(shminfo
->shmid
, IPC_RMID
, 0);
140 shminfo
->readOnly
= False
;
141 return shminfo
->shmaddr
;
146 * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
149 alloc_shm_ximage(struct xm_displaytarget
*xm_dt
,
150 struct xlib_drawable
*xmb
,
151 unsigned width
, unsigned height
)
154 * We have to do a _lot_ of error checking here to be sure we can
155 * really use the XSHM extension. It seems different servers trigger
156 * errors at different points if the extension won't work. Therefore
157 * we have to be very careful...
159 int (*old_handler
)(Display
*, XErrorEvent
*);
161 xm_dt
->tempImage
= XShmCreateImage(xm_dt
->display
,
168 if (xm_dt
->tempImage
== NULL
) {
175 old_handler
= XSetErrorHandler(mesaHandleXError
);
176 /* This may trigger the X protocol error we're ready to catch: */
177 XShmAttach(xm_dt
->display
, &xm_dt
->shminfo
);
178 XSync(xm_dt
->display
, False
);
180 if (mesaXErrorFlag
) {
181 /* we are on a remote display, this error is normal, don't print it */
182 XFlush(xm_dt
->display
);
184 XDestroyImage(xm_dt
->tempImage
);
185 xm_dt
->tempImage
= NULL
;
187 (void) XSetErrorHandler(old_handler
);
194 #endif /* USE_XSHM */
197 xm_is_displaytarget_format_supported( struct sw_winsys
*ws
,
198 enum pipe_format format
)
200 /* TODO: check visuals or other sensible thing here */
206 xm_displaytarget_map(struct sw_winsys
*ws
,
207 struct sw_displaytarget
*dt
,
210 struct xm_displaytarget
*xm_dt
= xm_displaytarget(dt
);
211 xm_dt
->mapped
= xm_dt
->data
;
212 return xm_dt
->mapped
;
216 xm_displaytarget_unmap(struct sw_winsys
*ws
,
217 struct sw_displaytarget
*dt
)
219 struct xm_displaytarget
*xm_dt
= xm_displaytarget(dt
);
220 xm_dt
->mapped
= NULL
;
224 xm_displaytarget_destroy(struct sw_winsys
*ws
,
225 struct sw_displaytarget
*dt
)
227 struct xm_displaytarget
*xm_dt
= xm_displaytarget(dt
);
231 if (xm_dt
->shminfo
.shmid
>= 0) {
232 shmdt(xm_dt
->shminfo
.shmaddr
);
233 shmctl(xm_dt
->shminfo
.shmid
, IPC_RMID
, 0);
235 xm_dt
->shminfo
.shmid
= -1;
236 xm_dt
->shminfo
.shmaddr
= (char *) -1;
248 * Display/copy the image in the surface into the X window specified
249 * by the XMesaBuffer.
252 xlib_sw_display(struct xlib_drawable
*xlib_drawable
,
253 struct sw_displaytarget
*dt
)
256 struct xm_displaytarget
*xm_dt
= xm_displaytarget(dt
);
257 static boolean no_swap
= 0;
258 static boolean firsttime
= 1;
261 no_swap
= getenv("SP_NO_RAST") != NULL
;
271 if (xm_dt
->tempImage
== NULL
)
273 assert(util_format_get_blockwidth(xm_dt
->format
) == 1);
274 assert(util_format_get_blockheight(xm_dt
->format
) == 1);
275 alloc_shm_ximage(xm_dt
,
277 xm_dt
->stride
/ util_format_get_blocksize(xm_dt
->format
),
281 ximage
= xm_dt
->tempImage
;
282 ximage
->data
= xm_dt
->data
;
284 /* _debug_printf("XSHM\n"); */
285 XShmPutImage(xm_dt
->display
, xlib_drawable
->drawable
, xlib_drawable
->gc
,
286 ximage
, 0, 0, 0, 0, xm_dt
->width
, xm_dt
->height
, False
);
291 /* display image in Window */
292 ximage
= xm_dt
->tempImage
;
293 ximage
->data
= xm_dt
->data
;
295 /* check that the XImage has been previously initialized */
296 assert(ximage
->format
);
297 assert(ximage
->bitmap_unit
);
299 /* update XImage's fields */
300 ximage
->width
= xm_dt
->width
;
301 ximage
->height
= xm_dt
->height
;
302 ximage
->bytes_per_line
= xm_dt
->stride
;
304 /* _debug_printf("XPUT\n"); */
305 XPutImage(xm_dt
->display
, xlib_drawable
->drawable
, xlib_drawable
->gc
,
306 ximage
, 0, 0, 0, 0, xm_dt
->width
, xm_dt
->height
);
311 * Display/copy the image in the surface into the X window specified
312 * by the XMesaBuffer.
315 xm_displaytarget_display(struct sw_winsys
*ws
,
316 struct sw_displaytarget
*dt
,
317 void *context_private
)
319 struct xlib_drawable
*xlib_drawable
= (struct xlib_drawable
*)context_private
;
320 xlib_sw_display(xlib_drawable
, dt
);
324 static struct sw_displaytarget
*
325 xm_displaytarget_create(struct sw_winsys
*winsys
,
326 enum pipe_format format
,
327 unsigned width
, unsigned height
,
331 struct xm_displaytarget
*xm_dt
= CALLOC_STRUCT(xm_displaytarget
);
332 unsigned nblocksy
, size
;
334 xm_dt
= CALLOC_STRUCT(xm_displaytarget
);
338 xm_dt
->display
= ((struct xlib_sw_winsys
*)winsys
)->display
;
339 xm_dt
->format
= format
;
340 xm_dt
->width
= width
;
341 xm_dt
->height
= height
;
343 nblocksy
= util_format_get_nblocksy(format
, height
);
344 xm_dt
->stride
= align(util_format_get_stride(format
, width
), alignment
);
345 size
= xm_dt
->stride
* nblocksy
;
348 if (!debug_get_bool_option("XLIB_NO_SHM", FALSE
))
350 xm_dt
->shminfo
.shmid
= -1;
351 xm_dt
->shminfo
.shmaddr
= (char *) -1;
354 xm_dt
->data
= alloc_shm(xm_dt
, size
);
361 xm_dt
->data
= align_malloc(size
, alignment
);
366 *stride
= xm_dt
->stride
;
367 return (struct sw_displaytarget
*)xm_dt
;
377 xm_destroy( struct sw_winsys
*ws
)
384 xlib_create_sw_winsys( Display
*display
)
386 struct xlib_sw_winsys
*ws
;
388 ws
= CALLOC_STRUCT(xlib_sw_winsys
);
392 ws
->display
= display
;
393 ws
->base
.destroy
= xm_destroy
;
395 ws
->base
.is_displaytarget_format_supported
= xm_is_displaytarget_format_supported
;
397 ws
->base
.displaytarget_create
= xm_displaytarget_create
;
398 ws
->base
.displaytarget_map
= xm_displaytarget_map
;
399 ws
->base
.displaytarget_unmap
= xm_displaytarget_unmap
;
400 ws
->base
.displaytarget_destroy
= xm_displaytarget_destroy
;
402 ws
->base
.displaytarget_display
= xm_displaytarget_display
;