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 "util/u_simple_screen.h"
42 #include "pipe/p_format.h"
43 #include "pipe/p_context.h"
44 #include "util/u_inlines.h"
45 #include "util/u_format.h"
46 #include "util/u_math.h"
47 #include "util/u_memory.h"
48 #include "softpipe/sp_winsys.h"
49 #include "softpipe/sp_texture.h"
54 * Subclass of pipe_buffer for Xlib winsys.
55 * Low-level OS/window system memory buffer
59 struct pipe_buffer base
;
60 boolean userBuffer
; /** Is this a user-space buffer? */
66 boolean shm
; /** Is this a shared memory buffer? */
67 XShmSegmentInfo shminfo
;
73 * Subclass of pipe_winsys for Xlib winsys
75 struct xmesa_pipe_winsys
77 struct pipe_winsys base
;
78 /* struct xmesa_visual *xm_visual; */
84 static INLINE
struct xm_buffer
*
85 xm_buffer( struct pipe_buffer
*buf
)
87 return (struct xm_buffer
*)buf
;
92 * X Shared Memory Image extension code
97 static volatile int mesaXErrorFlag
= 0;
100 * Catches potential Xlib errors.
103 mesaHandleXError(Display
*dpy
, XErrorEvent
*event
)
112 static char *alloc_shm(struct xm_buffer
*buf
, unsigned size
)
114 XShmSegmentInfo
*const shminfo
= & buf
->shminfo
;
116 shminfo
->shmid
= shmget(IPC_PRIVATE
, size
, IPC_CREAT
|0777);
117 if (shminfo
->shmid
< 0) {
121 shminfo
->shmaddr
= (char *) shmat(shminfo
->shmid
, 0, 0);
122 if (shminfo
->shmaddr
== (char *) -1) {
123 shmctl(shminfo
->shmid
, IPC_RMID
, 0);
127 shminfo
->readOnly
= False
;
128 return shminfo
->shmaddr
;
133 * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
136 alloc_shm_ximage(struct xm_buffer
*b
, struct xmesa_buffer
*xmb
,
137 unsigned width
, unsigned height
)
140 * We have to do a _lot_ of error checking here to be sure we can
141 * really use the XSHM extension. It seems different servers trigger
142 * errors at different points if the extension won't work. Therefore
143 * we have to be very careful...
145 int (*old_handler
)(Display
*, XErrorEvent
*);
147 b
->tempImage
= XShmCreateImage(xmb
->xm_visual
->display
,
148 xmb
->xm_visual
->visinfo
->visual
,
149 xmb
->xm_visual
->visinfo
->depth
,
154 if (b
->tempImage
== NULL
) {
161 old_handler
= XSetErrorHandler(mesaHandleXError
);
162 /* This may trigger the X protocol error we're ready to catch: */
163 XShmAttach(xmb
->xm_visual
->display
, &b
->shminfo
);
164 XSync(xmb
->xm_visual
->display
, False
);
166 if (mesaXErrorFlag
) {
167 /* we are on a remote display, this error is normal, don't print it */
168 XFlush(xmb
->xm_visual
->display
);
170 XDestroyImage(b
->tempImage
);
173 (void) XSetErrorHandler(old_handler
);
180 #endif /* USE_XSHM */
184 /* Most callbacks map direcly onto dri_bufmgr operations:
187 xm_buffer_map(struct pipe_winsys
*pws
, struct pipe_buffer
*buf
,
190 struct xm_buffer
*xm_buf
= xm_buffer(buf
);
191 xm_buf
->mapped
= xm_buf
->data
;
192 return xm_buf
->mapped
;
196 xm_buffer_unmap(struct pipe_winsys
*pws
, struct pipe_buffer
*buf
)
198 struct xm_buffer
*xm_buf
= xm_buffer(buf
);
199 xm_buf
->mapped
= NULL
;
203 xm_buffer_destroy(struct pipe_buffer
*buf
)
205 struct xm_buffer
*oldBuf
= xm_buffer(buf
);
208 * Note oldBuf->data may point to one of three things:
209 * 1. XShm shared memory image data
210 * 2. User-provided (wrapped) memory, see xm_user_buffer_create()
211 * 3. Regular, malloc'd memory
212 * We need to be careful with freeing that data now.
217 if (oldBuf
->shminfo
.shmid
>= 0) {
218 shmdt(oldBuf
->shminfo
.shmaddr
);
219 shmctl(oldBuf
->shminfo
.shmid
, IPC_RMID
, 0);
221 oldBuf
->shminfo
.shmid
= -1;
222 oldBuf
->shminfo
.shmaddr
= (char *) -1;
229 if (oldBuf
->tempImage
) {
230 XDestroyImage(oldBuf
->tempImage
);
231 oldBuf
->tempImage
= NULL
;
235 if (oldBuf
->data
&& !oldBuf
->userBuffer
) {
236 /* this was regular malloc'd memory */
237 align_free(oldBuf
->data
);
248 * Display/copy the image in the surface into the X window specified
249 * by the XMesaBuffer.
252 xlib_softpipe_display_surface(struct xmesa_buffer
*b
,
253 struct pipe_surface
*surf
)
256 struct softpipe_texture
*spt
= softpipe_texture(surf
->texture
);
257 struct xm_buffer
*xm_buf
= xm_buffer(spt
->buffer
);
258 static boolean no_swap
= 0;
259 static boolean firsttime
= 1;
262 no_swap
= getenv("SP_NO_RAST") != NULL
;
272 if (xm_buf
->tempImage
== NULL
)
274 assert(util_format_get_blockwidth(surf
->texture
->format
) == 1);
275 assert(util_format_get_blockheight(surf
->texture
->format
) == 1);
276 alloc_shm_ximage(xm_buf
, b
, spt
->stride
[surf
->level
] /
277 util_format_get_blocksize(surf
->texture
->format
), surf
->height
);
280 ximage
= xm_buf
->tempImage
;
281 ximage
->data
= xm_buf
->data
;
283 /* _debug_printf("XSHM\n"); */
284 XShmPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
285 ximage
, 0, 0, 0, 0, surf
->width
, surf
->height
, False
);
290 /* display image in Window */
291 ximage
= b
->tempImage
;
292 ximage
->data
= xm_buf
->data
;
294 /* check that the XImage has been previously initialized */
295 assert(ximage
->format
);
296 assert(ximage
->bitmap_unit
);
298 /* update XImage's fields */
299 ximage
->width
= surf
->width
;
300 ximage
->height
= surf
->height
;
301 ximage
->bytes_per_line
= spt
->stride
[surf
->level
];
303 /* _debug_printf("XPUT\n"); */
304 XPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
305 ximage
, 0, 0, 0, 0, surf
->width
, surf
->height
);
311 xm_flush_frontbuffer(struct pipe_winsys
*pws
,
312 struct pipe_surface
*surf
,
313 void *context_private
)
316 * The front color buffer is actually just another XImage buffer.
317 * This function copies that XImage to the actual X Window.
319 XMesaContext xmctx
= (XMesaContext
) context_private
;
320 xlib_softpipe_display_surface(xmctx
->xm_buffer
, surf
);
321 xmesa_check_and_update_buffer_size(xmctx
, xmctx
->xm_buffer
);
327 xm_get_name(struct pipe_winsys
*pws
)
333 static struct pipe_buffer
*
334 xm_buffer_create(struct pipe_winsys
*pws
,
339 struct xm_buffer
*buffer
= CALLOC_STRUCT(xm_buffer
);
341 pipe_reference_init(&buffer
->base
.reference
, 1);
342 buffer
->base
.alignment
= alignment
;
343 buffer
->base
.usage
= usage
;
344 buffer
->base
.size
= size
;
346 /* align to 16-byte multiple for Cell */
347 buffer
->data
= align_malloc(size
, max(alignment
, 16));
349 return &buffer
->base
;
354 * Create buffer which wraps user-space data.
356 static struct pipe_buffer
*
357 xm_user_buffer_create(struct pipe_winsys
*pws
, void *ptr
, unsigned bytes
)
359 struct xm_buffer
*buffer
= CALLOC_STRUCT(xm_buffer
);
360 pipe_reference_init(&buffer
->base
.reference
, 1);
361 buffer
->base
.size
= bytes
;
362 buffer
->userBuffer
= TRUE
;
365 return &buffer
->base
;
369 static struct pipe_buffer
*
370 xm_surface_buffer_create(struct pipe_winsys
*winsys
,
371 unsigned width
, unsigned height
,
372 enum pipe_format format
,
377 const unsigned alignment
= 64;
378 unsigned nblocksy
, size
;
380 nblocksy
= util_format_get_nblocksy(format
, height
);
381 *stride
= align(util_format_get_stride(format
, width
), alignment
);
382 size
= *stride
* nblocksy
;
385 if (!debug_get_bool_option("XLIB_NO_SHM", FALSE
))
387 struct xm_buffer
*buffer
= CALLOC_STRUCT(xm_buffer
);
389 pipe_reference_init(&buffer
->base
.reference
, 1);
390 buffer
->base
.alignment
= alignment
;
391 buffer
->base
.usage
= usage
;
392 buffer
->base
.size
= size
;
393 buffer
->userBuffer
= FALSE
;
394 buffer
->shminfo
.shmid
= -1;
395 buffer
->shminfo
.shmaddr
= (char *) -1;
398 buffer
->data
= alloc_shm(buffer
, size
);
402 return &buffer
->base
;
411 return winsys
->buffer_create(winsys
, alignment
,
418 * Fence functions - basically nothing to do, as we don't create any actual
423 xm_fence_reference(struct pipe_winsys
*sws
, struct pipe_fence_handle
**ptr
,
424 struct pipe_fence_handle
*fence
)
430 xm_fence_signalled(struct pipe_winsys
*sws
, struct pipe_fence_handle
*fence
,
438 xm_fence_finish(struct pipe_winsys
*sws
, struct pipe_fence_handle
*fence
,
446 static struct pipe_winsys
*
447 xlib_create_softpipe_winsys( void )
449 static struct xmesa_pipe_winsys
*ws
= NULL
;
452 ws
= CALLOC_STRUCT(xmesa_pipe_winsys
);
454 /* Fill in this struct with callbacks that pipe will need to
455 * communicate with the window system, buffer manager, etc.
457 ws
->base
.buffer_create
= xm_buffer_create
;
458 ws
->base
.user_buffer_create
= xm_user_buffer_create
;
459 ws
->base
.buffer_map
= xm_buffer_map
;
460 ws
->base
.buffer_unmap
= xm_buffer_unmap
;
461 ws
->base
.buffer_destroy
= xm_buffer_destroy
;
463 ws
->base
.surface_buffer_create
= xm_surface_buffer_create
;
465 ws
->base
.fence_reference
= xm_fence_reference
;
466 ws
->base
.fence_signalled
= xm_fence_signalled
;
467 ws
->base
.fence_finish
= xm_fence_finish
;
469 ws
->base
.flush_frontbuffer
= xm_flush_frontbuffer
;
470 ws
->base
.get_name
= xm_get_name
;
477 static struct pipe_screen
*
478 xlib_create_softpipe_screen( void )
480 struct pipe_winsys
*winsys
;
481 struct pipe_screen
*screen
;
483 winsys
= xlib_create_softpipe_winsys();
487 screen
= softpipe_create_screen(winsys
);
495 winsys
->destroy( winsys
);
501 struct xm_driver xlib_softpipe_driver
=
503 .create_pipe_screen
= xlib_create_softpipe_screen
,
504 .display_surface
= xlib_softpipe_display_surface