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 **************************************************************************/
36 #include "glxheader.h"
42 #include "pipe/p_winsys.h"
43 #include "pipe/p_format.h"
44 #include "pipe/p_context.h"
45 #include "pipe/p_inlines.h"
46 #include "util/u_math.h"
47 #include "util/u_memory.h"
48 #include "softpipe/sp_winsys.h"
51 #include "cell/ppu/cell_context.h"
52 #include "cell/ppu/cell_screen.h"
53 #include "cell/ppu/cell_winsys.h"
55 #define TILE_SIZE 32 /* avoid compilation errors */
58 #include "xlib_softpipe.h"
61 * Subclass of pipe_buffer for Xlib winsys.
62 * Low-level OS/window system memory buffer
66 struct pipe_buffer base
;
67 boolean userBuffer
; /** Is this a user-space buffer? */
74 XShmSegmentInfo shminfo
;
80 * Subclass of pipe_winsys for Xlib winsys
82 struct xmesa_pipe_winsys
84 struct pipe_winsys base
;
85 /* struct xmesa_visual *xm_visual; */
92 static INLINE
struct xm_buffer
*
93 xm_buffer( struct pipe_buffer
*buf
)
95 return (struct xm_buffer
*)buf
;
100 * X Shared Memory Image extension code
102 #if defined(USE_XSHM)
104 #define XSHM_ENABLED(b) ((b)->shm)
106 static volatile int mesaXErrorFlag
= 0;
109 * Catches potential Xlib errors.
112 mesaHandleXError(XMesaDisplay
*dpy
, XErrorEvent
*event
)
121 static GLboolean
alloc_shm(struct xm_buffer
*buf
, unsigned size
)
123 XShmSegmentInfo
*const shminfo
= & buf
->shminfo
;
125 shminfo
->shmid
= shmget(IPC_PRIVATE
, size
, IPC_CREAT
|0777);
126 if (shminfo
->shmid
< 0) {
130 shminfo
->shmaddr
= (char *) shmat(shminfo
->shmid
, 0, 0);
131 if (shminfo
->shmaddr
== (char *) -1) {
132 shmctl(shminfo
->shmid
, IPC_RMID
, 0);
136 shminfo
->readOnly
= False
;
142 * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
145 alloc_shm_ximage(struct xm_buffer
*b
, struct xmesa_buffer
*xmb
,
146 unsigned width
, unsigned height
)
149 * We have to do a _lot_ of error checking here to be sure we can
150 * really use the XSHM extension. It seems different servers trigger
151 * errors at different points if the extension won't work. Therefore
152 * we have to be very careful...
157 int (*old_handler
)(XMesaDisplay
*, XErrorEvent
*);
159 b
->tempImage
= XShmCreateImage(xmb
->xm_visual
->display
,
160 xmb
->xm_visual
->visinfo
->visual
,
161 xmb
->xm_visual
->visinfo
->depth
,
166 if (b
->tempImage
== NULL
) {
173 old_handler
= XSetErrorHandler(mesaHandleXError
);
174 /* This may trigger the X protocol error we're ready to catch: */
175 XShmAttach(xmb
->xm_visual
->display
, &b
->shminfo
);
176 XSync(xmb
->xm_visual
->display
, False
);
178 if (mesaXErrorFlag
) {
179 /* we are on a remote display, this error is normal, don't print it */
180 XFlush(xmb
->xm_visual
->display
);
182 XDestroyImage(b
->tempImage
);
185 (void) XSetErrorHandler(old_handler
);
190 /* Finally, try an XShmPutImage to be really sure the extension works */
192 gc
= XCreateGC(xmb
->xm_visual
->display
, xmb
->drawable
, 0, NULL
);
193 XShmPutImage(xmb
->xm_visual
->display
, xmb
->drawable
, gc
,
194 b
->tempImage
, 0, 0, 0, 0, 1, 1 /*one pixel*/, False
);
195 XSync(xmb
->xm_visual
->display
, False
);
196 XFreeGC(xmb
->xm_visual
->display
, gc
);
197 (void) XSetErrorHandler(old_handler
);
198 if (mesaXErrorFlag
) {
199 XFlush(xmb
->xm_visual
->display
);
201 XDestroyImage(b
->tempImage
);
211 #define XSHM_ENABLED(b) 0
214 alloc_shm_ximage(struct xm_buffer
*b
, struct xmesa_buffer
*xmb
,
215 unsigned width
, unsigned height
)
219 #endif /* USE_XSHM */
224 /* Most callbacks map direcly onto dri_bufmgr operations:
227 xm_buffer_map(struct pipe_winsys
*pws
, struct pipe_buffer
*buf
,
230 struct xm_buffer
*xm_buf
= xm_buffer(buf
);
231 xm_buf
->mapped
= xm_buf
->data
;
232 return xm_buf
->mapped
;
236 xm_buffer_unmap(struct pipe_winsys
*pws
, struct pipe_buffer
*buf
)
238 struct xm_buffer
*xm_buf
= xm_buffer(buf
);
239 xm_buf
->mapped
= NULL
;
243 xm_buffer_destroy(struct pipe_winsys
*pws
,
244 struct pipe_buffer
*buf
)
246 struct xm_buffer
*oldBuf
= xm_buffer(buf
);
249 #if defined(USE_XSHM)
250 if (oldBuf
->shminfo
.shmid
>= 0) {
251 shmdt(oldBuf
->shminfo
.shmaddr
);
252 shmctl(oldBuf
->shminfo
.shmid
, IPC_RMID
, 0);
254 oldBuf
->shminfo
.shmid
= -1;
255 oldBuf
->shminfo
.shmaddr
= (char *) -1;
260 if (!oldBuf
->userBuffer
) {
261 align_free(oldBuf
->data
);
273 * For Cell. Basically, rearrange the pixels/quads from this layout:
286 twiddle_tile(const uint
*tileIn
, uint
*tileOut
)
290 for (y
= 0; y
< TILE_SIZE
; y
+=2) {
291 for (x
= 0; x
< TILE_SIZE
; x
+=2) {
292 int k
= 4 * (y
/2 * TILE_SIZE
/2 + x
/2);
293 tileOut
[y
* TILE_SIZE
+ (x
+ 0)] = tileIn
[k
];
294 tileOut
[y
* TILE_SIZE
+ (x
+ 1)] = tileIn
[k
+1];
295 tileOut
[(y
+ 1) * TILE_SIZE
+ (x
+ 0)] = tileIn
[k
+2];
296 tileOut
[(y
+ 1) * TILE_SIZE
+ (x
+ 1)] = tileIn
[k
+3];
304 * Display a surface that's in a tiled configuration. That is, all the
305 * pixels for a TILE_SIZExTILE_SIZE block are contiguous in memory.
308 xlib_cell_display_surface(struct xmesa_buffer
*b
, struct pipe_surface
*surf
)
311 struct xm_buffer
*xm_buf
= xm_buffer(surf
->buffer
);
312 const uint tilesPerRow
= (surf
->width
+ TILE_SIZE
- 1) / TILE_SIZE
;
315 if (XSHM_ENABLED(xm_buf
) && (xm_buf
->tempImage
== NULL
)) {
316 alloc_shm_ximage(xm_buf
, b
, TILE_SIZE
, TILE_SIZE
);
319 ximage
= (XSHM_ENABLED(xm_buf
)) ? xm_buf
->tempImage
: b
->tempImage
;
321 /* check that the XImage has been previously initialized */
322 assert(ximage
->format
);
323 assert(ximage
->bitmap_unit
);
325 if (!XSHM_ENABLED(xm_buf
)) {
326 /* update XImage's fields */
327 ximage
->width
= TILE_SIZE
;
328 ximage
->height
= TILE_SIZE
;
329 ximage
->bytes_per_line
= TILE_SIZE
* 4;
332 for (y
= 0; y
< surf
->height
; y
+= TILE_SIZE
) {
333 for (x
= 0; x
< surf
->width
; x
+= TILE_SIZE
) {
334 uint tmpTile
[TILE_SIZE
* TILE_SIZE
];
335 int tx
= x
/ TILE_SIZE
;
336 int ty
= y
/ TILE_SIZE
;
337 int offset
= ty
* tilesPerRow
+ tx
;
341 if (y
+ h
> surf
->height
)
342 h
= surf
->height
- y
;
343 if (x
+ w
> surf
->width
)
346 /* offset in pixels */
347 offset
*= TILE_SIZE
* TILE_SIZE
;
349 if (0 && XSHM_ENABLED(xm_buf
)) {
350 ximage
->data
= (char *) xm_buf
->data
+ 4 * offset
;
351 /* make copy of tile data */
352 memcpy(tmpTile
, (uint
*) ximage
->data
, sizeof(tmpTile
));
353 /* twiddle from temp to ximage in shared memory */
354 twiddle_tile(tmpTile
, (uint
*) ximage
->data
);
355 /* display image in shared memory */
356 #if defined(USE_XSHM)
357 XShmPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
358 ximage
, 0, 0, x
, y
, w
, h
, False
);
362 /* twiddle from ximage buffer to temp tile */
363 twiddle_tile((uint
*) xm_buf
->data
+ offset
, tmpTile
);
364 /* display temp tile data */
365 ximage
->data
= (char *) tmpTile
;
366 XPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
367 ximage
, 0, 0, x
, y
, w
, h
);
375 * Display/copy the image in the surface into the X window specified
376 * by the XMesaBuffer.
379 xlib_softpipe_display_surface(struct xmesa_buffer
*b
,
380 struct pipe_surface
*surf
)
383 struct xm_buffer
*xm_buf
= xm_buffer(surf
->buffer
);
384 static boolean no_swap
= 0;
385 static boolean firsttime
= 1;
386 static int tileSize
= 0;
389 no_swap
= getenv("SP_NO_RAST") != NULL
;
391 if (!getenv("GALLIUM_NOCELL")) {
392 tileSize
= 32; /** probably temporary */
402 xlib_cell_display_surface(b
, surf
);
406 if (XSHM_ENABLED(xm_buf
) && (xm_buf
->tempImage
== NULL
)) {
407 assert(surf
->block
.width
== 1);
408 assert(surf
->block
.height
== 1);
409 alloc_shm_ximage(xm_buf
, b
, surf
->stride
/surf
->block
.size
, surf
->height
);
412 ximage
= (XSHM_ENABLED(xm_buf
)) ? xm_buf
->tempImage
: b
->tempImage
;
413 ximage
->data
= xm_buf
->data
;
415 /* display image in Window */
416 if (XSHM_ENABLED(xm_buf
)) {
417 #if defined(USE_XSHM)
418 XShmPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
419 ximage
, 0, 0, 0, 0, surf
->width
, surf
->height
, False
);
422 /* check that the XImage has been previously initialized */
423 assert(ximage
->format
);
424 assert(ximage
->bitmap_unit
);
426 /* update XImage's fields */
427 ximage
->width
= surf
->width
;
428 ximage
->height
= surf
->height
;
429 ximage
->bytes_per_line
= surf
->stride
;
431 XPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
432 ximage
, 0, 0, 0, 0, surf
->width
, surf
->height
);
438 xm_flush_frontbuffer(struct pipe_winsys
*pws
,
439 struct pipe_surface
*surf
,
440 void *context_private
)
443 * The front color buffer is actually just another XImage buffer.
444 * This function copies that XImage to the actual X Window.
446 XMesaContext xmctx
= (XMesaContext
) context_private
;
447 xlib_softpipe_display_surface(xmctx
->xm_buffer
, surf
);
453 xm_get_name(struct pipe_winsys
*pws
)
459 static struct pipe_buffer
*
460 xm_buffer_create(struct pipe_winsys
*pws
,
465 struct xm_buffer
*buffer
= CALLOC_STRUCT(xm_buffer
);
466 #if defined(USE_XSHM)
467 struct xmesa_pipe_winsys
*xpws
= (struct xmesa_pipe_winsys
*) pws
;
470 buffer
->base
.refcount
= 1;
471 buffer
->base
.alignment
= alignment
;
472 buffer
->base
.usage
= usage
;
473 buffer
->base
.size
= size
;
476 #if defined(USE_XSHM)
477 buffer
->shminfo
.shmid
= -1;
478 buffer
->shminfo
.shmaddr
= (char *) -1;
480 if (xpws
->shm
&& (usage
& PIPE_BUFFER_USAGE_PIXEL
) != 0) {
481 buffer
->shm
= xpws
->shm
;
483 if (alloc_shm(buffer
, size
)) {
484 buffer
->data
= buffer
->shminfo
.shmaddr
;
489 if (buffer
->data
== NULL
) {
492 /* align to 16-byte multiple for Cell */
493 buffer
->data
= align_malloc(size
, max(alignment
, 16));
496 return &buffer
->base
;
501 * Create buffer which wraps user-space data.
503 static struct pipe_buffer
*
504 xm_user_buffer_create(struct pipe_winsys
*pws
, void *ptr
, unsigned bytes
)
506 struct xm_buffer
*buffer
= CALLOC_STRUCT(xm_buffer
);
507 buffer
->base
.refcount
= 1;
508 buffer
->base
.size
= bytes
;
509 buffer
->userBuffer
= TRUE
;
513 return &buffer
->base
;
519 * Round n up to next multiple.
521 static INLINE
unsigned
522 round_up(unsigned n
, unsigned multiple
)
524 return (n
+ multiple
- 1) & ~(multiple
- 1);
528 xm_surface_alloc_storage(struct pipe_winsys
*winsys
,
529 struct pipe_surface
*surf
,
530 unsigned width
, unsigned height
,
531 enum pipe_format format
,
535 const unsigned alignment
= 64;
538 surf
->height
= height
;
539 surf
->format
= format
;
540 pf_get_block(format
, &surf
->block
);
541 surf
->nblocksx
= pf_get_nblocksx(&surf
->block
, width
);
542 surf
->nblocksy
= pf_get_nblocksy(&surf
->block
, height
);
543 surf
->stride
= round_up(surf
->nblocksx
* surf
->block
.size
, alignment
);
546 assert(!surf
->buffer
);
547 surf
->buffer
= winsys
->buffer_create(winsys
, alignment
,
548 PIPE_BUFFER_USAGE_PIXEL
,
549 #ifdef GALLIUM_CELL /* XXX a bit of a hack */
550 surf
->stride
* round_up(surf
->nblocksy
, TILE_SIZE
));
552 surf
->stride
* surf
->nblocksy
);
563 * Called via winsys->surface_alloc() to create new surfaces.
565 static struct pipe_surface
*
566 xm_surface_alloc(struct pipe_winsys
*ws
)
568 struct pipe_surface
*surface
= CALLOC_STRUCT(pipe_surface
);
572 surface
->refcount
= 1;
573 surface
->winsys
= ws
;
581 xm_surface_release(struct pipe_winsys
*winsys
, struct pipe_surface
**s
)
583 struct pipe_surface
*surf
= *s
;
584 assert(!surf
->texture
);
586 if (surf
->refcount
== 0) {
588 winsys_buffer_reference(winsys
, &surf
->buffer
, NULL
);
596 * Fence functions - basically nothing to do, as we don't create any actual
601 xm_fence_reference(struct pipe_winsys
*sws
, struct pipe_fence_handle
**ptr
,
602 struct pipe_fence_handle
*fence
)
608 xm_fence_signalled(struct pipe_winsys
*sws
, struct pipe_fence_handle
*fence
,
616 xm_fence_finish(struct pipe_winsys
*sws
, struct pipe_fence_handle
*fence
,
625 xlib_create_softpipe_winsys( void )
627 static struct xmesa_pipe_winsys
*ws
= NULL
;
630 ws
= CALLOC_STRUCT(xmesa_pipe_winsys
);
632 //ws->shm = xmesa_check_for_xshm( display );
634 /* Fill in this struct with callbacks that pipe will need to
635 * communicate with the window system, buffer manager, etc.
637 ws
->base
.buffer_create
= xm_buffer_create
;
638 ws
->base
.user_buffer_create
= xm_user_buffer_create
;
639 ws
->base
.buffer_map
= xm_buffer_map
;
640 ws
->base
.buffer_unmap
= xm_buffer_unmap
;
641 ws
->base
.buffer_destroy
= xm_buffer_destroy
;
643 ws
->base
.surface_alloc
= xm_surface_alloc
;
644 ws
->base
.surface_alloc_storage
= xm_surface_alloc_storage
;
645 ws
->base
.surface_release
= xm_surface_release
;
647 ws
->base
.fence_reference
= xm_fence_reference
;
648 ws
->base
.fence_signalled
= xm_fence_signalled
;
649 ws
->base
.fence_finish
= xm_fence_finish
;
651 ws
->base
.flush_frontbuffer
= xm_flush_frontbuffer
;
652 ws
->base
.get_name
= xm_get_name
;
660 xlib_create_softpipe_screen( struct pipe_winsys
*pws
)
662 struct pipe_screen
*screen
;
664 screen
= softpipe_create_screen(pws
);
675 struct pipe_context
*
676 xlib_create_softpipe_context( struct pipe_screen
*screen
,
677 void *context_private
)
679 struct pipe_context
*pipe
;
681 pipe
= softpipe_create(screen
, screen
->winsys
, NULL
);
685 pipe
->priv
= context_private
;
689 /* Free stuff here */
694 /***********************************************************************
695 * Cell piggybacks on softpipe code still.
697 * Should be untangled sufficiently to live in a separate file, at
698 * least. That would mean removing #ifdef GALLIUM_CELL's from above
699 * and creating cell-specific versions of either those functions or
703 xlib_create_cell_winsys( void )
705 return xlib_create_softpipe_winsys();
709 xlib_create_cell_screen( struct pipe_winsys
*pws
)
711 return cell_create_screen( pws
);
715 struct pipe_context
*
716 xlib_create_cell_context( struct pipe_screen
*screen
,
720 struct cell_winsys
*cws
;
721 struct pipe_context
*pipe
;
723 if (getenv("GALLIUM_NOCELL"))
724 return xlib_create_softpipe_context( screen
, priv
);
727 /* This takes a cell_winsys pointer, but probably that should be
728 * created and stored at screen creation, not context creation.
730 * The actual cell_winsys value isn't used for anything, so just
731 * passing NULL for now.
733 pipe
= cell_create_context( screen
, NULL
);