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_winsys.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"
52 * Subclass of pipe_buffer for Xlib winsys.
53 * Low-level OS/window system memory buffer
57 struct pipe_buffer base
;
58 boolean userBuffer
; /** Is this a user-space buffer? */
64 XShmSegmentInfo shminfo
;
69 * Subclass of pipe_winsys for Xlib winsys
71 struct xmesa_pipe_winsys
73 struct pipe_winsys base
;
74 /* struct xmesa_visual *xm_visual; */
81 static INLINE
struct xm_buffer
*
82 xm_buffer( struct pipe_buffer
*buf
)
84 return (struct xm_buffer
*)buf
;
89 * X Shared Memory Image extension code
91 #define XSHM_ENABLED(b) ((b)->shm)
93 static volatile int mesaXErrorFlag
= 0;
96 * Catches potential Xlib errors.
99 mesaHandleXError(Display
*dpy
, XErrorEvent
*event
)
108 static GLboolean
alloc_shm(struct xm_buffer
*buf
, unsigned size
)
110 XShmSegmentInfo
*const shminfo
= & buf
->shminfo
;
112 shminfo
->shmid
= shmget(IPC_PRIVATE
, size
, IPC_CREAT
|0777);
113 if (shminfo
->shmid
< 0) {
117 shminfo
->shmaddr
= (char *) shmat(shminfo
->shmid
, 0, 0);
118 if (shminfo
->shmaddr
== (char *) -1) {
119 shmctl(shminfo
->shmid
, IPC_RMID
, 0);
123 shminfo
->readOnly
= False
;
129 * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
132 alloc_shm_ximage(struct xm_buffer
*b
, struct xmesa_buffer
*xmb
,
133 unsigned width
, unsigned height
)
136 * We have to do a _lot_ of error checking here to be sure we can
137 * really use the XSHM extension. It seems different servers trigger
138 * errors at different points if the extension won't work. Therefore
139 * we have to be very careful...
141 int (*old_handler
)(Display
*, XErrorEvent
*);
143 b
->tempImage
= XShmCreateImage(xmb
->xm_visual
->display
,
144 xmb
->xm_visual
->visinfo
->visual
,
145 xmb
->xm_visual
->visinfo
->depth
,
150 if (b
->tempImage
== NULL
) {
157 old_handler
= XSetErrorHandler(mesaHandleXError
);
158 /* This may trigger the X protocol error we're ready to catch: */
159 XShmAttach(xmb
->xm_visual
->display
, &b
->shminfo
);
160 XSync(xmb
->xm_visual
->display
, False
);
162 if (mesaXErrorFlag
) {
163 /* we are on a remote display, this error is normal, don't print it */
164 XFlush(xmb
->xm_visual
->display
);
166 XDestroyImage(b
->tempImage
);
169 (void) XSetErrorHandler(old_handler
);
176 /* Most callbacks map direcly onto dri_bufmgr operations:
179 xm_buffer_map(struct pipe_winsys
*pws
, struct pipe_buffer
*buf
,
182 struct xm_buffer
*xm_buf
= xm_buffer(buf
);
183 xm_buf
->mapped
= xm_buf
->data
;
184 return xm_buf
->mapped
;
188 xm_buffer_unmap(struct pipe_winsys
*pws
, struct pipe_buffer
*buf
)
190 struct xm_buffer
*xm_buf
= xm_buffer(buf
);
191 xm_buf
->mapped
= NULL
;
195 xm_buffer_destroy(struct pipe_winsys
*pws
,
196 struct pipe_buffer
*buf
)
198 struct xm_buffer
*oldBuf
= xm_buffer(buf
);
201 if (oldBuf
->shminfo
.shmid
>= 0) {
202 shmdt(oldBuf
->shminfo
.shmaddr
);
203 shmctl(oldBuf
->shminfo
.shmid
, IPC_RMID
, 0);
205 oldBuf
->shminfo
.shmid
= -1;
206 oldBuf
->shminfo
.shmaddr
= (char *) -1;
210 if (!oldBuf
->userBuffer
) {
211 align_free(oldBuf
->data
);
224 * Display/copy the image in the surface into the X window specified
225 * by the XMesaBuffer.
228 xlib_softpipe_display_surface(struct xmesa_buffer
*b
,
229 struct pipe_surface
*surf
)
232 struct xm_buffer
*xm_buf
= xm_buffer(surf
->buffer
);
233 static boolean no_swap
= 0;
234 static boolean firsttime
= 1;
237 no_swap
= getenv("SP_NO_RAST") != NULL
;
244 if (XSHM_ENABLED(xm_buf
) && (xm_buf
->tempImage
== NULL
)) {
245 assert(surf
->block
.width
== 1);
246 assert(surf
->block
.height
== 1);
247 alloc_shm_ximage(xm_buf
, b
, surf
->stride
/surf
->block
.size
, surf
->height
);
250 ximage
= (XSHM_ENABLED(xm_buf
)) ? xm_buf
->tempImage
: b
->tempImage
;
251 ximage
->data
= xm_buf
->data
;
253 /* display image in Window */
254 if (XSHM_ENABLED(xm_buf
)) {
255 XShmPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
256 ximage
, 0, 0, 0, 0, surf
->width
, surf
->height
, False
);
258 /* check that the XImage has been previously initialized */
259 assert(ximage
->format
);
260 assert(ximage
->bitmap_unit
);
262 /* update XImage's fields */
263 ximage
->width
= surf
->width
;
264 ximage
->height
= surf
->height
;
265 ximage
->bytes_per_line
= surf
->stride
;
267 XPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
268 ximage
, 0, 0, 0, 0, surf
->width
, surf
->height
);
274 xm_flush_frontbuffer(struct pipe_winsys
*pws
,
275 struct pipe_surface
*surf
,
276 void *context_private
)
279 * The front color buffer is actually just another XImage buffer.
280 * This function copies that XImage to the actual X Window.
282 XMesaContext xmctx
= (XMesaContext
) context_private
;
283 xlib_softpipe_display_surface(xmctx
->xm_buffer
, surf
);
289 xm_get_name(struct pipe_winsys
*pws
)
295 static struct pipe_buffer
*
296 xm_buffer_create(struct pipe_winsys
*pws
,
301 struct xm_buffer
*buffer
= CALLOC_STRUCT(xm_buffer
);
302 struct xmesa_pipe_winsys
*xpws
= (struct xmesa_pipe_winsys
*) pws
;
304 buffer
->base
.refcount
= 1;
305 buffer
->base
.alignment
= alignment
;
306 buffer
->base
.usage
= usage
;
307 buffer
->base
.size
= size
;
308 buffer
->shminfo
.shmid
= -1;
309 buffer
->shminfo
.shmaddr
= (char *) -1;
311 if (xpws
->shm
&& (usage
& PIPE_BUFFER_USAGE_PIXEL
) != 0) {
312 buffer
->shm
= xpws
->shm
;
314 if (alloc_shm(buffer
, size
)) {
315 buffer
->data
= buffer
->shminfo
.shmaddr
;
319 if (buffer
->data
== NULL
) {
322 /* align to 16-byte multiple for Cell */
323 buffer
->data
= align_malloc(size
, max(alignment
, 16));
326 return &buffer
->base
;
331 * Create buffer which wraps user-space data.
333 static struct pipe_buffer
*
334 xm_user_buffer_create(struct pipe_winsys
*pws
, void *ptr
, unsigned bytes
)
336 struct xm_buffer
*buffer
= CALLOC_STRUCT(xm_buffer
);
337 buffer
->base
.refcount
= 1;
338 buffer
->base
.size
= bytes
;
339 buffer
->userBuffer
= TRUE
;
343 return &buffer
->base
;
349 * Round n up to next multiple.
351 static INLINE
unsigned
352 round_up(unsigned n
, unsigned multiple
)
354 return (n
+ multiple
- 1) & ~(multiple
- 1);
358 xm_surface_alloc_storage(struct pipe_winsys
*winsys
,
359 struct pipe_surface
*surf
,
360 unsigned width
, unsigned height
,
361 enum pipe_format format
,
365 const unsigned alignment
= 64;
368 surf
->height
= height
;
369 surf
->format
= format
;
370 pf_get_block(format
, &surf
->block
);
371 surf
->nblocksx
= pf_get_nblocksx(&surf
->block
, width
);
372 surf
->nblocksy
= pf_get_nblocksy(&surf
->block
, height
);
373 surf
->stride
= round_up(surf
->nblocksx
* surf
->block
.size
, alignment
);
376 assert(!surf
->buffer
);
377 surf
->buffer
= winsys
->buffer_create(winsys
, alignment
,
378 PIPE_BUFFER_USAGE_PIXEL
,
379 surf
->stride
* surf
->nblocksy
);
389 * Called via winsys->surface_alloc() to create new surfaces.
391 static struct pipe_surface
*
392 xm_surface_alloc(struct pipe_winsys
*ws
)
394 struct pipe_surface
*surface
= CALLOC_STRUCT(pipe_surface
);
398 surface
->refcount
= 1;
399 surface
->winsys
= ws
;
407 xm_surface_release(struct pipe_winsys
*winsys
, struct pipe_surface
**s
)
409 struct pipe_surface
*surf
= *s
;
410 assert(!surf
->texture
);
412 if (surf
->refcount
== 0) {
414 winsys_buffer_reference(winsys
, &surf
->buffer
, NULL
);
422 * Fence functions - basically nothing to do, as we don't create any actual
427 xm_fence_reference(struct pipe_winsys
*sws
, struct pipe_fence_handle
**ptr
,
428 struct pipe_fence_handle
*fence
)
434 xm_fence_signalled(struct pipe_winsys
*sws
, struct pipe_fence_handle
*fence
,
442 xm_fence_finish(struct pipe_winsys
*sws
, struct pipe_fence_handle
*fence
,
450 static struct pipe_winsys
*
451 xlib_create_softpipe_winsys( void )
453 static struct xmesa_pipe_winsys
*ws
= NULL
;
456 ws
= CALLOC_STRUCT(xmesa_pipe_winsys
);
458 /* Fill in this struct with callbacks that pipe will need to
459 * communicate with the window system, buffer manager, etc.
461 ws
->base
.buffer_create
= xm_buffer_create
;
462 ws
->base
.user_buffer_create
= xm_user_buffer_create
;
463 ws
->base
.buffer_map
= xm_buffer_map
;
464 ws
->base
.buffer_unmap
= xm_buffer_unmap
;
465 ws
->base
.buffer_destroy
= xm_buffer_destroy
;
467 ws
->base
.surface_alloc
= xm_surface_alloc
;
468 ws
->base
.surface_alloc_storage
= xm_surface_alloc_storage
;
469 ws
->base
.surface_release
= xm_surface_release
;
471 ws
->base
.fence_reference
= xm_fence_reference
;
472 ws
->base
.fence_signalled
= xm_fence_signalled
;
473 ws
->base
.fence_finish
= xm_fence_finish
;
475 ws
->base
.flush_frontbuffer
= xm_flush_frontbuffer
;
476 ws
->base
.get_name
= xm_get_name
;
483 static struct pipe_screen
*
484 xlib_create_softpipe_screen( void )
486 struct pipe_winsys
*winsys
;
487 struct pipe_screen
*screen
;
489 winsys
= xlib_create_softpipe_winsys();
493 screen
= softpipe_create_screen(winsys
);
501 winsys
->destroy( winsys
);
507 static struct pipe_context
*
508 xlib_create_softpipe_context( struct pipe_screen
*screen
,
509 void *context_private
)
511 struct pipe_context
*pipe
;
513 pipe
= softpipe_create(screen
, screen
->winsys
, NULL
);
517 pipe
->priv
= context_private
;
521 /* Free stuff here */
525 struct xm_driver xlib_softpipe_driver
=
527 .create_pipe_screen
= xlib_create_softpipe_screen
,
528 .create_pipe_context
= xlib_create_softpipe_context
,
529 .display_surface
= xlib_softpipe_display_surface