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 **************************************************************************/
35 #include "pipe/p_format.h"
36 #include "pipe/p_context.h"
37 #include "util/u_inlines.h"
38 #include "util/u_format.h"
39 #include "util/u_math.h"
40 #include "util/u_memory.h"
42 #include "state_tracker/xlib_sw_winsys.h"
45 #include <X11/Xlibint.h>
46 #include <X11/Xutil.h>
49 #include <X11/extensions/XShm.h>
51 DEBUG_GET_ONCE_BOOL_OPTION(xlib_no_shm, "XLIB_NO_SHM", FALSE)
54 * Display target for Xlib winsys.
55 * Low-level OS/window system memory buffer
57 struct xlib_displaytarget
59 enum pipe_format format;
72 /* This is the last drawable that this display target was presented
73 * against. May need to recreate gc, tempImage when this changes??
77 XShmSegmentInfo shminfo;
78 Bool shm; /** Using shared memory images? */
83 * Subclass of sw_winsys for Xlib winsys
87 struct sw_winsys base;
94 static INLINE struct xlib_displaytarget *
95 xlib_displaytarget(struct sw_displaytarget *dt)
97 return (struct xlib_displaytarget *) dt;
102 * X Shared Memory Image extension code
105 static volatile int XErrorFlag = 0;
108 * Catches potential Xlib errors.
111 handle_xerror(Display *dpy, XErrorEvent *event)
121 alloc_shm(struct xlib_displaytarget *buf, unsigned size)
123 XShmSegmentInfo *const shminfo = & buf->shminfo;
126 shminfo->shmaddr = (char *) -1;
128 shminfo->shmid = shmget(IPC_PRIVATE, size, IPC_CREAT|0777);
129 if (shminfo->shmid < 0) {
133 shminfo->shmaddr = (char *) shmat(shminfo->shmid, 0, 0);
134 if (shminfo->shmaddr == (char *) -1) {
135 shmctl(shminfo->shmid, IPC_RMID, 0);
139 shminfo->readOnly = False;
140 return shminfo->shmaddr;
145 * Allocate a shared memory XImage back buffer for the given display target.
148 alloc_shm_ximage(struct xlib_displaytarget *xlib_dt,
149 struct xlib_drawable *xmb,
150 unsigned width, unsigned height)
153 * We have to do a _lot_ of error checking here to be sure we can
154 * really use the XSHM extension. It seems different servers trigger
155 * errors at different points if the extension won't work. Therefore
156 * we have to be very careful...
158 int (*old_handler)(Display *, XErrorEvent *);
160 xlib_dt->tempImage = XShmCreateImage(xlib_dt->display,
167 if (xlib_dt->tempImage == NULL) {
168 xlib_dt->shm = False;
174 old_handler = XSetErrorHandler(handle_xerror);
175 /* This may trigger the X protocol error we're ready to catch: */
176 XShmAttach(xlib_dt->display, &xlib_dt->shminfo);
177 XSync(xlib_dt->display, False);
180 /* we are on a remote display, this error is normal, don't print it */
181 XFlush(xlib_dt->display);
183 XDestroyImage(xlib_dt->tempImage);
184 xlib_dt->tempImage = NULL;
185 xlib_dt->shm = False;
186 (void) XSetErrorHandler(old_handler);
195 alloc_ximage(struct xlib_displaytarget *xlib_dt,
196 struct xlib_drawable *xmb,
197 unsigned width, unsigned height)
199 /* try allocating a shared memory image first */
201 alloc_shm_ximage(xlib_dt, xmb, width, height);
202 if (xlib_dt->tempImage)
203 return; /* success */
206 /* try regular (non-shared memory) image */
207 xlib_dt->tempImage = XCreateImage(xlib_dt->display,
216 xlib_is_displaytarget_format_supported(struct sw_winsys *ws,
218 enum pipe_format format)
220 /* TODO: check visuals or other sensible thing here */
226 xlib_displaytarget_map(struct sw_winsys *ws,
227 struct sw_displaytarget *dt,
230 struct xlib_displaytarget *xlib_dt = xlib_displaytarget(dt);
231 xlib_dt->mapped = xlib_dt->data;
232 return xlib_dt->mapped;
237 xlib_displaytarget_unmap(struct sw_winsys *ws,
238 struct sw_displaytarget *dt)
240 struct xlib_displaytarget *xlib_dt = xlib_displaytarget(dt);
241 xlib_dt->mapped = NULL;
246 xlib_displaytarget_destroy(struct sw_winsys *ws,
247 struct sw_displaytarget *dt)
249 struct xlib_displaytarget *xlib_dt = xlib_displaytarget(dt);
252 if (xlib_dt->shminfo.shmid >= 0) {
253 shmdt(xlib_dt->shminfo.shmaddr);
254 shmctl(xlib_dt->shminfo.shmid, IPC_RMID, 0);
256 xlib_dt->shminfo.shmid = -1;
257 xlib_dt->shminfo.shmaddr = (char *) -1;
259 xlib_dt->data = NULL;
260 if (xlib_dt->tempImage)
261 xlib_dt->tempImage->data = NULL;
265 if (xlib_dt->tempImage && xlib_dt->tempImage->data == xlib_dt->data) {
266 xlib_dt->tempImage->data = NULL;
268 xlib_dt->data = NULL;
272 if (xlib_dt->tempImage) {
273 XDestroyImage(xlib_dt->tempImage);
274 xlib_dt->tempImage = NULL;
278 XFreeGC(xlib_dt->display, xlib_dt->gc);
285 * Display/copy the image in the surface into the X window specified
286 * by the display target.
289 xlib_sw_display(struct xlib_drawable *xlib_drawable,
290 struct sw_displaytarget *dt)
292 static boolean no_swap = 0;
293 static boolean firsttime = 1;
294 struct xlib_displaytarget *xlib_dt = xlib_displaytarget(dt);
295 Display *display = xlib_dt->display;
299 no_swap = getenv("SP_NO_RAST") != NULL;
306 if (xlib_dt->drawable != xlib_drawable->drawable) {
308 XFreeGC(display, xlib_dt->gc);
312 if (xlib_dt->tempImage) {
313 XDestroyImage(xlib_dt->tempImage);
314 xlib_dt->tempImage = NULL;
317 xlib_dt->drawable = xlib_drawable->drawable;
320 if (xlib_dt->tempImage == NULL) {
321 assert(util_format_get_blockwidth(xlib_dt->format) == 1);
322 assert(util_format_get_blockheight(xlib_dt->format) == 1);
323 alloc_ximage(xlib_dt, xlib_drawable,
324 xlib_dt->stride / util_format_get_blocksize(xlib_dt->format),
326 if (!xlib_dt->tempImage)
330 if (xlib_dt->gc == NULL) {
331 xlib_dt->gc = XCreateGC(display, xlib_drawable->drawable, 0, NULL);
332 XSetFunction(display, xlib_dt->gc, GXcopy);
336 ximage = xlib_dt->tempImage;
337 ximage->data = xlib_dt->data;
339 /* _debug_printf("XSHM\n"); */
340 XShmPutImage(xlib_dt->display, xlib_drawable->drawable, xlib_dt->gc,
341 ximage, 0, 0, 0, 0, xlib_dt->width, xlib_dt->height, False);
344 /* display image in Window */
345 ximage = xlib_dt->tempImage;
346 ximage->data = xlib_dt->data;
348 /* check that the XImage has been previously initialized */
349 assert(ximage->format);
350 assert(ximage->bitmap_unit);
352 /* update XImage's fields */
353 ximage->width = xlib_dt->width;
354 ximage->height = xlib_dt->height;
355 ximage->bytes_per_line = xlib_dt->stride;
357 /* _debug_printf("XPUT\n"); */
358 XPutImage(xlib_dt->display, xlib_drawable->drawable, xlib_dt->gc,
359 ximage, 0, 0, 0, 0, xlib_dt->width, xlib_dt->height);
362 XFlush(xlib_dt->display);
367 * Display/copy the image in the surface into the X window specified
368 * by the display target.
371 xlib_displaytarget_display(struct sw_winsys *ws,
372 struct sw_displaytarget *dt,
373 void *context_private)
375 struct xlib_drawable *xlib_drawable = (struct xlib_drawable *)context_private;
376 xlib_sw_display(xlib_drawable, dt);
380 static struct sw_displaytarget *
381 xlib_displaytarget_create(struct sw_winsys *winsys,
383 enum pipe_format format,
384 unsigned width, unsigned height,
388 struct xlib_displaytarget *xlib_dt;
389 unsigned nblocksy, size;
391 xlib_dt = CALLOC_STRUCT(xlib_displaytarget);
395 xlib_dt->display = ((struct xlib_sw_winsys *)winsys)->display;
396 xlib_dt->format = format;
397 xlib_dt->width = width;
398 xlib_dt->height = height;
400 nblocksy = util_format_get_nblocksy(format, height);
401 xlib_dt->stride = align(util_format_get_stride(format, width), alignment);
402 size = xlib_dt->stride * nblocksy;
404 if (!debug_get_option_xlib_no_shm()) {
405 xlib_dt->data = alloc_shm(xlib_dt, size);
411 if (!xlib_dt->data) {
412 xlib_dt->data = align_malloc(size, alignment);
417 *stride = xlib_dt->stride;
418 return (struct sw_displaytarget *)xlib_dt;
427 static struct sw_displaytarget *
428 xlib_displaytarget_from_handle(struct sw_winsys *winsys,
429 const struct pipe_resource *templet,
430 struct winsys_handle *whandle,
439 xlib_displaytarget_get_handle(struct sw_winsys *winsys,
440 struct sw_displaytarget *dt,
441 struct winsys_handle *whandle)
449 xlib_destroy(struct sw_winsys *ws)
456 xlib_create_sw_winsys(Display *display)
458 struct xlib_sw_winsys *ws;
460 ws = CALLOC_STRUCT(xlib_sw_winsys);
464 ws->display = display;
465 ws->base.destroy = xlib_destroy;
467 ws->base.is_displaytarget_format_supported = xlib_is_displaytarget_format_supported;
469 ws->base.displaytarget_create = xlib_displaytarget_create;
470 ws->base.displaytarget_from_handle = xlib_displaytarget_from_handle;
471 ws->base.displaytarget_get_handle = xlib_displaytarget_get_handle;
472 ws->base.displaytarget_map = xlib_displaytarget_map;
473 ws->base.displaytarget_unmap = xlib_displaytarget_unmap;
474 ws->base.displaytarget_destroy = xlib_displaytarget_destroy;
476 ws->base.displaytarget_display = xlib_displaytarget_display;