1 /**************************************************************************
3 * Copyright 2007 VMware, 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/xlibsw_api.h"
43 #include "xlib_sw_winsys.h"
46 #include <X11/Xlibint.h>
47 #include <X11/Xutil.h>
50 #include <X11/extensions/XShm.h>
52 DEBUG_GET_ONCE_BOOL_OPTION(xlib_no_shm
, "XLIB_NO_SHM", FALSE
)
55 * Display target for Xlib winsys.
56 * Low-level OS/window system memory buffer
58 struct xlib_displaytarget
60 enum pipe_format format
;
73 /* This is the last drawable that this display target was presented
74 * against. May need to recreate gc, tempImage when this changes??
78 XShmSegmentInfo shminfo
;
79 Bool shm
; /** Using shared memory images? */
84 * Subclass of sw_winsys for Xlib winsys
88 struct sw_winsys base
;
95 static inline struct xlib_displaytarget
*
96 xlib_displaytarget(struct sw_displaytarget
*dt
)
98 return (struct xlib_displaytarget
*) dt
;
103 * X Shared Memory Image extension code
106 static volatile int XErrorFlag
= 0;
109 * Catches potential Xlib errors.
112 handle_xerror(Display
*dpy
, XErrorEvent
*event
)
122 alloc_shm(struct xlib_displaytarget
*buf
, unsigned size
)
124 XShmSegmentInfo
*const shminfo
= & buf
->shminfo
;
127 shminfo
->shmaddr
= (char *) -1;
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 display target.
149 alloc_shm_ximage(struct xlib_displaytarget
*xlib_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 xlib_dt
->tempImage
= XShmCreateImage(xlib_dt
->display
,
168 if (xlib_dt
->tempImage
== NULL
) {
169 shmctl(xlib_dt
->shminfo
.shmid
, IPC_RMID
, 0);
170 xlib_dt
->shm
= False
;
176 old_handler
= XSetErrorHandler(handle_xerror
);
177 /* This may trigger the X protocol error we're ready to catch: */
178 XShmAttach(xlib_dt
->display
, &xlib_dt
->shminfo
);
179 XSync(xlib_dt
->display
, False
);
181 /* Mark the segment to be destroyed, so that it is automatically destroyed
182 * when this process dies. Needs to be after XShmAttach() for *BSD.
184 shmctl(xlib_dt
->shminfo
.shmid
, IPC_RMID
, 0);
187 /* we are on a remote display, this error is normal, don't print it */
188 XFlush(xlib_dt
->display
);
190 XDestroyImage(xlib_dt
->tempImage
);
191 xlib_dt
->tempImage
= NULL
;
192 xlib_dt
->shm
= False
;
193 (void) XSetErrorHandler(old_handler
);
202 alloc_ximage(struct xlib_displaytarget
*xlib_dt
,
203 struct xlib_drawable
*xmb
,
204 unsigned width
, unsigned height
)
206 /* try allocating a shared memory image first */
208 alloc_shm_ximage(xlib_dt
, xmb
, width
, height
);
209 if (xlib_dt
->tempImage
)
210 return; /* success */
213 /* try regular (non-shared memory) image */
214 xlib_dt
->tempImage
= XCreateImage(xlib_dt
->display
,
223 xlib_is_displaytarget_format_supported(struct sw_winsys
*ws
,
225 enum pipe_format format
)
227 /* TODO: check visuals or other sensible thing here */
233 xlib_displaytarget_map(struct sw_winsys
*ws
,
234 struct sw_displaytarget
*dt
,
237 struct xlib_displaytarget
*xlib_dt
= xlib_displaytarget(dt
);
238 xlib_dt
->mapped
= xlib_dt
->data
;
239 return xlib_dt
->mapped
;
244 xlib_displaytarget_unmap(struct sw_winsys
*ws
,
245 struct sw_displaytarget
*dt
)
247 struct xlib_displaytarget
*xlib_dt
= xlib_displaytarget(dt
);
248 xlib_dt
->mapped
= NULL
;
253 xlib_displaytarget_destroy(struct sw_winsys
*ws
,
254 struct sw_displaytarget
*dt
)
256 struct xlib_displaytarget
*xlib_dt
= xlib_displaytarget(dt
);
259 if (xlib_dt
->shminfo
.shmid
>= 0) {
260 shmdt(xlib_dt
->shminfo
.shmaddr
);
261 shmctl(xlib_dt
->shminfo
.shmid
, IPC_RMID
, 0);
263 xlib_dt
->shminfo
.shmid
= -1;
264 xlib_dt
->shminfo
.shmaddr
= (char *) -1;
266 xlib_dt
->data
= NULL
;
267 if (xlib_dt
->tempImage
)
268 xlib_dt
->tempImage
->data
= NULL
;
271 align_free(xlib_dt
->data
);
272 if (xlib_dt
->tempImage
&& xlib_dt
->tempImage
->data
== xlib_dt
->data
) {
273 xlib_dt
->tempImage
->data
= NULL
;
275 xlib_dt
->data
= NULL
;
279 if (xlib_dt
->tempImage
) {
280 XDestroyImage(xlib_dt
->tempImage
);
281 xlib_dt
->tempImage
= NULL
;
285 XFreeGC(xlib_dt
->display
, xlib_dt
->gc
);
292 * Display/copy the image in the surface into the X window specified
293 * by the display target.
296 xlib_sw_display(struct xlib_drawable
*xlib_drawable
,
297 struct sw_displaytarget
*dt
)
299 static boolean no_swap
= 0;
300 static boolean firsttime
= 1;
301 struct xlib_displaytarget
*xlib_dt
= xlib_displaytarget(dt
);
302 Display
*display
= xlib_dt
->display
;
306 no_swap
= getenv("SP_NO_RAST") != NULL
;
313 if (xlib_dt
->drawable
!= xlib_drawable
->drawable
) {
315 XFreeGC(display
, xlib_dt
->gc
);
319 if (xlib_dt
->tempImage
) {
320 XDestroyImage(xlib_dt
->tempImage
);
321 xlib_dt
->tempImage
= NULL
;
324 xlib_dt
->drawable
= xlib_drawable
->drawable
;
327 if (xlib_dt
->tempImage
== NULL
) {
328 assert(util_format_get_blockwidth(xlib_dt
->format
) == 1);
329 assert(util_format_get_blockheight(xlib_dt
->format
) == 1);
330 alloc_ximage(xlib_dt
, xlib_drawable
,
331 xlib_dt
->stride
/ util_format_get_blocksize(xlib_dt
->format
),
333 if (!xlib_dt
->tempImage
)
337 if (xlib_dt
->gc
== NULL
) {
338 xlib_dt
->gc
= XCreateGC(display
, xlib_drawable
->drawable
, 0, NULL
);
339 XSetFunction(display
, xlib_dt
->gc
, GXcopy
);
343 ximage
= xlib_dt
->tempImage
;
344 ximage
->data
= xlib_dt
->data
;
346 /* _debug_printf("XSHM\n"); */
347 XShmPutImage(xlib_dt
->display
, xlib_drawable
->drawable
, xlib_dt
->gc
,
348 ximage
, 0, 0, 0, 0, xlib_dt
->width
, xlib_dt
->height
, False
);
351 /* display image in Window */
352 ximage
= xlib_dt
->tempImage
;
353 ximage
->data
= xlib_dt
->data
;
355 /* check that the XImage has been previously initialized */
356 assert(ximage
->format
);
357 assert(ximage
->bitmap_unit
);
359 /* update XImage's fields */
360 ximage
->width
= xlib_dt
->width
;
361 ximage
->height
= xlib_dt
->height
;
362 ximage
->bytes_per_line
= xlib_dt
->stride
;
364 /* _debug_printf("XPUT\n"); */
365 XPutImage(xlib_dt
->display
, xlib_drawable
->drawable
, xlib_dt
->gc
,
366 ximage
, 0, 0, 0, 0, xlib_dt
->width
, xlib_dt
->height
);
369 XFlush(xlib_dt
->display
);
374 * Display/copy the image in the surface into the X window specified
375 * by the display target.
378 xlib_displaytarget_display(struct sw_winsys
*ws
,
379 struct sw_displaytarget
*dt
,
380 void *context_private
,
381 struct pipe_box
*box
)
383 struct xlib_drawable
*xlib_drawable
= (struct xlib_drawable
*)context_private
;
384 xlib_sw_display(xlib_drawable
, dt
);
388 static struct sw_displaytarget
*
389 xlib_displaytarget_create(struct sw_winsys
*winsys
,
391 enum pipe_format format
,
392 unsigned width
, unsigned height
,
394 const void *front_private
,
397 struct xlib_displaytarget
*xlib_dt
;
398 unsigned nblocksy
, size
;
401 xlib_dt
= CALLOC_STRUCT(xlib_displaytarget
);
405 xlib_dt
->display
= ((struct xlib_sw_winsys
*)winsys
)->display
;
406 xlib_dt
->format
= format
;
407 xlib_dt
->width
= width
;
408 xlib_dt
->height
= height
;
410 nblocksy
= util_format_get_nblocksy(format
, height
);
411 xlib_dt
->stride
= align(util_format_get_stride(format
, width
), alignment
);
412 size
= xlib_dt
->stride
* nblocksy
;
414 if (!debug_get_option_xlib_no_shm() &&
415 XQueryExtension(xlib_dt
->display
, "MIT-SHM", &ignore
, &ignore
, &ignore
)) {
416 xlib_dt
->data
= alloc_shm(xlib_dt
, size
);
422 if (!xlib_dt
->data
) {
423 xlib_dt
->data
= align_malloc(size
, alignment
);
428 *stride
= xlib_dt
->stride
;
429 return (struct sw_displaytarget
*)xlib_dt
;
438 static struct sw_displaytarget
*
439 xlib_displaytarget_from_handle(struct sw_winsys
*winsys
,
440 const struct pipe_resource
*templet
,
441 struct winsys_handle
*whandle
,
450 xlib_displaytarget_get_handle(struct sw_winsys
*winsys
,
451 struct sw_displaytarget
*dt
,
452 struct winsys_handle
*whandle
)
460 xlib_destroy(struct sw_winsys
*ws
)
467 xlib_create_sw_winsys(Display
*display
)
469 struct xlib_sw_winsys
*ws
;
471 ws
= CALLOC_STRUCT(xlib_sw_winsys
);
475 ws
->display
= display
;
476 ws
->base
.destroy
= xlib_destroy
;
478 ws
->base
.is_displaytarget_format_supported
= xlib_is_displaytarget_format_supported
;
480 ws
->base
.displaytarget_create
= xlib_displaytarget_create
;
481 ws
->base
.displaytarget_from_handle
= xlib_displaytarget_from_handle
;
482 ws
->base
.displaytarget_get_handle
= xlib_displaytarget_get_handle
;
483 ws
->base
.displaytarget_map
= xlib_displaytarget_map
;
484 ws
->base
.displaytarget_unmap
= xlib_displaytarget_unmap
;
485 ws
->base
.displaytarget_destroy
= xlib_displaytarget_destroy
;
487 ws
->base
.displaytarget_display
= xlib_displaytarget_display
;