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? */
66 XShmSegmentInfo shminfo
;
72 * Subclass of pipe_winsys for Xlib winsys
74 struct xmesa_pipe_winsys
76 struct pipe_winsys base
;
77 /* struct xmesa_visual *xm_visual; */
83 static INLINE
struct xm_buffer
*
84 xm_buffer( struct pipe_buffer
*buf
)
86 return (struct xm_buffer
*)buf
;
91 * X Shared Memory Image extension code
96 static volatile int mesaXErrorFlag
= 0;
99 * Catches potential Xlib errors.
102 mesaHandleXError(Display
*dpy
, XErrorEvent
*event
)
111 static char *alloc_shm(struct xm_buffer
*buf
, unsigned size
)
113 XShmSegmentInfo
*const shminfo
= & buf
->shminfo
;
115 shminfo
->shmid
= shmget(IPC_PRIVATE
, size
, IPC_CREAT
|0777);
116 if (shminfo
->shmid
< 0) {
120 shminfo
->shmaddr
= (char *) shmat(shminfo
->shmid
, 0, 0);
121 if (shminfo
->shmaddr
== (char *) -1) {
122 shmctl(shminfo
->shmid
, IPC_RMID
, 0);
126 shminfo
->readOnly
= False
;
127 return shminfo
->shmaddr
;
132 * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
135 alloc_shm_ximage(struct xm_buffer
*b
, struct xmesa_buffer
*xmb
,
136 unsigned width
, unsigned height
)
139 * We have to do a _lot_ of error checking here to be sure we can
140 * really use the XSHM extension. It seems different servers trigger
141 * errors at different points if the extension won't work. Therefore
142 * we have to be very careful...
144 int (*old_handler
)(Display
*, XErrorEvent
*);
146 b
->tempImage
= XShmCreateImage(xmb
->xm_visual
->display
,
147 xmb
->xm_visual
->visinfo
->visual
,
148 xmb
->xm_visual
->visinfo
->depth
,
153 if (b
->tempImage
== NULL
) {
160 old_handler
= XSetErrorHandler(mesaHandleXError
);
161 /* This may trigger the X protocol error we're ready to catch: */
162 XShmAttach(xmb
->xm_visual
->display
, &b
->shminfo
);
163 XSync(xmb
->xm_visual
->display
, False
);
165 if (mesaXErrorFlag
) {
166 /* we are on a remote display, this error is normal, don't print it */
167 XFlush(xmb
->xm_visual
->display
);
169 XDestroyImage(b
->tempImage
);
172 (void) XSetErrorHandler(old_handler
);
179 #endif /* USE_XSHM */
183 /* Most callbacks map direcly onto dri_bufmgr operations:
186 xm_buffer_map(struct pipe_winsys
*pws
, struct pipe_buffer
*buf
,
189 struct xm_buffer
*xm_buf
= xm_buffer(buf
);
190 xm_buf
->mapped
= xm_buf
->data
;
191 return xm_buf
->mapped
;
195 xm_buffer_unmap(struct pipe_winsys
*pws
, struct pipe_buffer
*buf
)
197 struct xm_buffer
*xm_buf
= xm_buffer(buf
);
198 xm_buf
->mapped
= NULL
;
202 xm_buffer_destroy(struct pipe_buffer
*buf
)
204 struct xm_buffer
*oldBuf
= xm_buffer(buf
);
207 * Note oldBuf->data may point to one of three things:
208 * 1. XShm shared memory image data
209 * 2. User-provided (wrapped) memory, see xm_user_buffer_create()
210 * 3. Regular, malloc'd memory
211 * We need to be careful with freeing that data now.
216 if (oldBuf
->shminfo
.shmid
>= 0) {
217 shmdt(oldBuf
->shminfo
.shmaddr
);
218 shmctl(oldBuf
->shminfo
.shmid
, IPC_RMID
, 0);
220 oldBuf
->shminfo
.shmid
= -1;
221 oldBuf
->shminfo
.shmaddr
= (char *) -1;
224 if (oldBuf
->tempImage
) {
225 if (oldBuf
->data
== oldBuf
->tempImage
->data
) {
226 /* oldBuf->data points at the xshm memory which we'll now free */
229 XDestroyImage(oldBuf
->tempImage
);
233 if (oldBuf
->data
&& !oldBuf
->userBuffer
) {
234 /* this was regular malloc'd memory */
235 align_free(oldBuf
->data
);
246 * Display/copy the image in the surface into the X window specified
247 * by the XMesaBuffer.
250 xlib_softpipe_display_surface(struct xmesa_buffer
*b
,
251 struct pipe_surface
*surf
)
254 struct softpipe_texture
*spt
= softpipe_texture(surf
->texture
);
255 struct xm_buffer
*xm_buf
= xm_buffer(spt
->buffer
);
256 static boolean no_swap
= 0;
257 static boolean firsttime
= 1;
260 no_swap
= getenv("SP_NO_RAST") != NULL
;
270 if (xm_buf
->tempImage
== NULL
)
272 assert(surf
->texture
->block
.width
== 1);
273 assert(surf
->texture
->block
.height
== 1);
274 alloc_shm_ximage(xm_buf
, b
, spt
->stride
[surf
->level
] /
275 surf
->texture
->block
.size
, surf
->height
);
278 ximage
= xm_buf
->tempImage
;
279 ximage
->data
= xm_buf
->data
;
281 /* _debug_printf("XSHM\n"); */
282 XShmPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
283 ximage
, 0, 0, 0, 0, surf
->width
, surf
->height
, False
);
288 /* display image in Window */
289 ximage
= b
->tempImage
;
290 ximage
->data
= xm_buf
->data
;
292 /* check that the XImage has been previously initialized */
293 assert(ximage
->format
);
294 assert(ximage
->bitmap_unit
);
296 /* update XImage's fields */
297 ximage
->width
= surf
->width
;
298 ximage
->height
= surf
->height
;
299 ximage
->bytes_per_line
= spt
->stride
[surf
->level
];
301 /* _debug_printf("XPUT\n"); */
302 XPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
303 ximage
, 0, 0, 0, 0, surf
->width
, surf
->height
);
309 xm_flush_frontbuffer(struct pipe_winsys
*pws
,
310 struct pipe_surface
*surf
,
311 void *context_private
)
314 * The front color buffer is actually just another XImage buffer.
315 * This function copies that XImage to the actual X Window.
317 XMesaContext xmctx
= (XMesaContext
) context_private
;
318 xlib_softpipe_display_surface(xmctx
->xm_buffer
, surf
);
319 xmesa_check_and_update_buffer_size(xmctx
, xmctx
->xm_buffer
);
325 xm_get_name(struct pipe_winsys
*pws
)
331 static struct pipe_buffer
*
332 xm_buffer_create(struct pipe_winsys
*pws
,
337 struct xm_buffer
*buffer
= CALLOC_STRUCT(xm_buffer
);
339 pipe_reference_init(&buffer
->base
.reference
, 1);
340 buffer
->base
.alignment
= alignment
;
341 buffer
->base
.usage
= usage
;
342 buffer
->base
.size
= size
;
344 if (buffer
->data
== NULL
) {
345 /* align to 16-byte multiple for Cell */
346 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 struct pipe_format_block block
;
379 unsigned nblocksx
, nblocksy
, size
;
381 pf_get_block(format
, &block
);
382 nblocksx
= pf_get_nblocksx(&block
, width
);
383 nblocksy
= pf_get_nblocksy(&block
, height
);
384 *stride
= align(nblocksx
* block
.size
, alignment
);
385 size
= *stride
* nblocksy
;
388 if (!debug_get_bool_option("XLIB_NO_SHM", FALSE
))
390 struct xm_buffer
*buffer
= CALLOC_STRUCT(xm_buffer
);
392 pipe_reference_init(&buffer
->base
.reference
, 1);
393 buffer
->base
.alignment
= alignment
;
394 buffer
->base
.usage
= usage
;
395 buffer
->base
.size
= size
;
396 buffer
->userBuffer
= FALSE
;
397 buffer
->shminfo
.shmid
= -1;
398 buffer
->shminfo
.shmaddr
= (char *) -1;
401 buffer
->data
= alloc_shm(buffer
, size
);
405 return &buffer
->base
;
414 return winsys
->buffer_create(winsys
, alignment
,
421 * Fence functions - basically nothing to do, as we don't create any actual
426 xm_fence_reference(struct pipe_winsys
*sws
, struct pipe_fence_handle
**ptr
,
427 struct pipe_fence_handle
*fence
)
433 xm_fence_signalled(struct pipe_winsys
*sws
, struct pipe_fence_handle
*fence
,
441 xm_fence_finish(struct pipe_winsys
*sws
, struct pipe_fence_handle
*fence
,
449 static struct pipe_winsys
*
450 xlib_create_softpipe_winsys( void )
452 static struct xmesa_pipe_winsys
*ws
= NULL
;
455 ws
= CALLOC_STRUCT(xmesa_pipe_winsys
);
457 /* Fill in this struct with callbacks that pipe will need to
458 * communicate with the window system, buffer manager, etc.
460 ws
->base
.buffer_create
= xm_buffer_create
;
461 ws
->base
.user_buffer_create
= xm_user_buffer_create
;
462 ws
->base
.buffer_map
= xm_buffer_map
;
463 ws
->base
.buffer_unmap
= xm_buffer_unmap
;
464 ws
->base
.buffer_destroy
= xm_buffer_destroy
;
466 ws
->base
.surface_buffer_create
= xm_surface_buffer_create
;
468 ws
->base
.fence_reference
= xm_fence_reference
;
469 ws
->base
.fence_signalled
= xm_fence_signalled
;
470 ws
->base
.fence_finish
= xm_fence_finish
;
472 ws
->base
.flush_frontbuffer
= xm_flush_frontbuffer
;
473 ws
->base
.get_name
= xm_get_name
;
480 static struct pipe_screen
*
481 xlib_create_softpipe_screen( void )
483 struct pipe_winsys
*winsys
;
484 struct pipe_screen
*screen
;
486 winsys
= xlib_create_softpipe_winsys();
490 screen
= softpipe_create_screen(winsys
);
498 winsys
->destroy( winsys
);
504 static struct pipe_context
*
505 xlib_create_softpipe_context( struct pipe_screen
*screen
,
506 void *context_private
)
508 struct pipe_context
*pipe
;
510 pipe
= softpipe_create(screen
);
514 pipe
->priv
= context_private
;
518 /* Free stuff here */
522 struct xm_driver xlib_softpipe_driver
=
524 .create_pipe_screen
= xlib_create_softpipe_screen
,
525 .create_pipe_context
= xlib_create_softpipe_context
,
526 .display_surface
= xlib_softpipe_display_surface