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/internal/p_winsys_screen.h"
42 #include "pipe/p_format.h"
43 #include "pipe/p_context.h"
44 #include "pipe/p_inlines.h"
45 #include "util/u_math.h"
46 #include "util/u_memory.h"
47 #include "softpipe/sp_winsys.h"
48 #include "softpipe/sp_texture.h"
53 * Subclass of pipe_buffer for Xlib winsys.
54 * Low-level OS/window system memory buffer
58 struct pipe_buffer base
;
59 boolean userBuffer
; /** Is this a user-space buffer? */
65 XShmSegmentInfo shminfo
;
70 * Subclass of pipe_winsys for Xlib winsys
72 struct xmesa_pipe_winsys
74 struct pipe_winsys base
;
75 /* struct xmesa_visual *xm_visual; */
82 static INLINE
struct xm_buffer
*
83 xm_buffer( struct pipe_buffer
*buf
)
85 return (struct xm_buffer
*)buf
;
90 * X Shared Memory Image extension code
92 #define XSHM_ENABLED(b) ((b)->shm)
94 static volatile int mesaXErrorFlag
= 0;
97 * Catches potential Xlib errors.
100 mesaHandleXError(Display
*dpy
, XErrorEvent
*event
)
109 static GLboolean
alloc_shm(struct xm_buffer
*buf
, unsigned size
)
111 XShmSegmentInfo
*const shminfo
= & buf
->shminfo
;
113 shminfo
->shmid
= shmget(IPC_PRIVATE
, size
, IPC_CREAT
|0777);
114 if (shminfo
->shmid
< 0) {
118 shminfo
->shmaddr
= (char *) shmat(shminfo
->shmid
, 0, 0);
119 if (shminfo
->shmaddr
== (char *) -1) {
120 shmctl(shminfo
->shmid
, IPC_RMID
, 0);
124 shminfo
->readOnly
= False
;
130 * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
133 alloc_shm_ximage(struct xm_buffer
*b
, struct xmesa_buffer
*xmb
,
134 unsigned width
, unsigned height
)
137 * We have to do a _lot_ of error checking here to be sure we can
138 * really use the XSHM extension. It seems different servers trigger
139 * errors at different points if the extension won't work. Therefore
140 * we have to be very careful...
142 int (*old_handler
)(Display
*, XErrorEvent
*);
144 b
->tempImage
= XShmCreateImage(xmb
->xm_visual
->display
,
145 xmb
->xm_visual
->visinfo
->visual
,
146 xmb
->xm_visual
->visinfo
->depth
,
151 if (b
->tempImage
== NULL
) {
158 old_handler
= XSetErrorHandler(mesaHandleXError
);
159 /* This may trigger the X protocol error we're ready to catch: */
160 XShmAttach(xmb
->xm_visual
->display
, &b
->shminfo
);
161 XSync(xmb
->xm_visual
->display
, False
);
163 if (mesaXErrorFlag
) {
164 /* we are on a remote display, this error is normal, don't print it */
165 XFlush(xmb
->xm_visual
->display
);
167 XDestroyImage(b
->tempImage
);
170 (void) XSetErrorHandler(old_handler
);
177 /* Most callbacks map direcly onto dri_bufmgr operations:
180 xm_buffer_map(struct pipe_winsys
*pws
, struct pipe_buffer
*buf
,
183 struct xm_buffer
*xm_buf
= xm_buffer(buf
);
184 xm_buf
->mapped
= xm_buf
->data
;
185 return xm_buf
->mapped
;
189 xm_buffer_unmap(struct pipe_winsys
*pws
, struct pipe_buffer
*buf
)
191 struct xm_buffer
*xm_buf
= xm_buffer(buf
);
192 xm_buf
->mapped
= NULL
;
196 xm_buffer_destroy(struct pipe_winsys
*pws
,
197 struct pipe_buffer
*buf
)
199 struct xm_buffer
*oldBuf
= xm_buffer(buf
);
202 if (oldBuf
->shminfo
.shmid
>= 0) {
203 shmdt(oldBuf
->shminfo
.shmaddr
);
204 shmctl(oldBuf
->shminfo
.shmid
, IPC_RMID
, 0);
206 oldBuf
->shminfo
.shmid
= -1;
207 oldBuf
->shminfo
.shmaddr
= (char *) -1;
211 if (!oldBuf
->userBuffer
) {
212 align_free(oldBuf
->data
);
225 * Display/copy the image in the surface into the X window specified
226 * by the XMesaBuffer.
229 xlib_softpipe_display_surface(struct xmesa_buffer
*b
,
230 struct pipe_surface
*surf
)
233 struct xm_buffer
*xm_buf
= xm_buffer(
234 softpipe_texture(surf
->texture
)->buffer
);
235 static boolean no_swap
= 0;
236 static boolean firsttime
= 1;
239 no_swap
= getenv("SP_NO_RAST") != NULL
;
246 if (XSHM_ENABLED(xm_buf
) && (xm_buf
->tempImage
== NULL
)) {
247 assert(surf
->block
.width
== 1);
248 assert(surf
->block
.height
== 1);
249 alloc_shm_ximage(xm_buf
, b
, surf
->stride
/surf
->block
.size
, surf
->height
);
252 ximage
= (XSHM_ENABLED(xm_buf
)) ? xm_buf
->tempImage
: b
->tempImage
;
253 ximage
->data
= xm_buf
->data
;
255 /* display image in Window */
256 if (XSHM_ENABLED(xm_buf
)) {
257 XShmPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
258 ximage
, 0, 0, 0, 0, surf
->width
, surf
->height
, False
);
260 /* check that the XImage has been previously initialized */
261 assert(ximage
->format
);
262 assert(ximage
->bitmap_unit
);
264 /* update XImage's fields */
265 ximage
->width
= surf
->width
;
266 ximage
->height
= surf
->height
;
267 ximage
->bytes_per_line
= surf
->stride
;
269 XPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
270 ximage
, 0, 0, 0, 0, surf
->width
, surf
->height
);
276 xm_flush_frontbuffer(struct pipe_winsys
*pws
,
277 struct pipe_surface
*surf
,
278 void *context_private
)
281 * The front color buffer is actually just another XImage buffer.
282 * This function copies that XImage to the actual X Window.
284 XMesaContext xmctx
= (XMesaContext
) context_private
;
285 xlib_softpipe_display_surface(xmctx
->xm_buffer
, surf
);
291 xm_get_name(struct pipe_winsys
*pws
)
297 static struct pipe_buffer
*
298 xm_buffer_create(struct pipe_winsys
*pws
,
303 struct xm_buffer
*buffer
= CALLOC_STRUCT(xm_buffer
);
304 struct xmesa_pipe_winsys
*xpws
= (struct xmesa_pipe_winsys
*) pws
;
306 buffer
->base
.refcount
= 1;
307 buffer
->base
.alignment
= alignment
;
308 buffer
->base
.usage
= usage
;
309 buffer
->base
.size
= size
;
310 buffer
->shminfo
.shmid
= -1;
311 buffer
->shminfo
.shmaddr
= (char *) -1;
313 if (xpws
->shm
&& (usage
& PIPE_BUFFER_USAGE_PIXEL
) != 0) {
314 buffer
->shm
= xpws
->shm
;
316 if (alloc_shm(buffer
, size
)) {
317 buffer
->data
= buffer
->shminfo
.shmaddr
;
321 if (buffer
->data
== NULL
) {
324 /* align to 16-byte multiple for Cell */
325 buffer
->data
= align_malloc(size
, max(alignment
, 16));
328 return &buffer
->base
;
333 * Create buffer which wraps user-space data.
335 static struct pipe_buffer
*
336 xm_user_buffer_create(struct pipe_winsys
*pws
, void *ptr
, unsigned bytes
)
338 struct xm_buffer
*buffer
= CALLOC_STRUCT(xm_buffer
);
339 buffer
->base
.refcount
= 1;
340 buffer
->base
.size
= bytes
;
341 buffer
->userBuffer
= TRUE
;
345 return &buffer
->base
;
349 static struct pipe_buffer
*
350 xm_surface_buffer_create(struct pipe_winsys
*winsys
,
351 unsigned width
, unsigned height
,
352 enum pipe_format format
,
356 const unsigned alignment
= 64;
357 struct pipe_format_block block
;
358 unsigned nblocksx
, nblocksy
;
360 pf_get_block(format
, &block
);
361 nblocksx
= pf_get_nblocksx(&block
, width
);
362 nblocksy
= pf_get_nblocksy(&block
, height
);
363 *stride
= align(nblocksx
* block
.size
, alignment
);
365 return winsys
->buffer_create(winsys
, alignment
,
372 * Fence functions - basically nothing to do, as we don't create any actual
377 xm_fence_reference(struct pipe_winsys
*sws
, struct pipe_fence_handle
**ptr
,
378 struct pipe_fence_handle
*fence
)
384 xm_fence_signalled(struct pipe_winsys
*sws
, struct pipe_fence_handle
*fence
,
392 xm_fence_finish(struct pipe_winsys
*sws
, struct pipe_fence_handle
*fence
,
400 static struct pipe_winsys
*
401 xlib_create_softpipe_winsys( void )
403 static struct xmesa_pipe_winsys
*ws
= NULL
;
406 ws
= CALLOC_STRUCT(xmesa_pipe_winsys
);
408 /* Fill in this struct with callbacks that pipe will need to
409 * communicate with the window system, buffer manager, etc.
411 ws
->base
.buffer_create
= xm_buffer_create
;
412 ws
->base
.user_buffer_create
= xm_user_buffer_create
;
413 ws
->base
.buffer_map
= xm_buffer_map
;
414 ws
->base
.buffer_unmap
= xm_buffer_unmap
;
415 ws
->base
.buffer_destroy
= xm_buffer_destroy
;
417 ws
->base
.surface_buffer_create
= xm_surface_buffer_create
;
419 ws
->base
.fence_reference
= xm_fence_reference
;
420 ws
->base
.fence_signalled
= xm_fence_signalled
;
421 ws
->base
.fence_finish
= xm_fence_finish
;
423 ws
->base
.flush_frontbuffer
= xm_flush_frontbuffer
;
424 ws
->base
.get_name
= xm_get_name
;
431 static struct pipe_screen
*
432 xlib_create_softpipe_screen( void )
434 struct pipe_winsys
*winsys
;
435 struct pipe_screen
*screen
;
437 winsys
= xlib_create_softpipe_winsys();
441 screen
= softpipe_create_screen(winsys
);
449 winsys
->destroy( winsys
);
455 static struct pipe_context
*
456 xlib_create_softpipe_context( struct pipe_screen
*screen
,
457 void *context_private
)
459 struct pipe_context
*pipe
;
461 pipe
= softpipe_create(screen
, screen
->winsys
, NULL
);
465 pipe
->priv
= context_private
;
469 /* Free stuff here */
473 struct xm_driver xlib_softpipe_driver
=
475 .create_pipe_screen
= xlib_create_softpipe_screen
,
476 .create_pipe_context
= xlib_create_softpipe_context
,
477 .display_surface
= xlib_softpipe_display_surface