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_util.h"
46 #include "pipe/p_inlines.h"
47 #include "softpipe/sp_winsys.h"
50 #include "cell/ppu/cell_context.h"
51 #include "cell/ppu/cell_screen.h"
52 #include "cell/ppu/cell_winsys.h"
54 #define TILE_SIZE 32 /* avoid compilation errors */
57 #include "xm_winsys_aub.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? */
73 #if defined(USE_XSHM) && !defined(XFree86Server)
74 XShmSegmentInfo shminfo
;
80 * Subclass of pipe_surface for Xlib winsys
84 struct pipe_surface surface
;
92 * Subclass of pipe_winsys for Xlib winsys
94 struct xmesa_pipe_winsys
96 struct pipe_winsys base
;
97 struct xmesa_visual
*xm_visual
;
104 static INLINE
struct xmesa_surface
*
105 xmesa_surface(struct pipe_surface
*ps
)
107 return (struct xmesa_surface
*) ps
;
112 static INLINE
struct xm_buffer
*
113 xm_buffer( struct pipe_buffer
*buf
)
115 return (struct xm_buffer
*)buf
;
120 * X Shared Memory Image extension code
122 #if defined(USE_XSHM) && !defined(XFree86Server)
124 #define XSHM_ENABLED(b) ((b)->shm)
126 static volatile int mesaXErrorFlag
= 0;
129 * Catches potential Xlib errors.
132 mesaHandleXError(XMesaDisplay
*dpy
, XErrorEvent
*event
)
141 static GLboolean
alloc_shm(struct xm_buffer
*buf
, unsigned size
)
143 XShmSegmentInfo
*const shminfo
= & buf
->shminfo
;
145 shminfo
->shmid
= shmget(IPC_PRIVATE
, size
, IPC_CREAT
|0777);
146 if (shminfo
->shmid
< 0) {
150 shminfo
->shmaddr
= (char *) shmat(shminfo
->shmid
, 0, 0);
151 if (shminfo
->shmaddr
== (char *) -1) {
152 shmctl(shminfo
->shmid
, IPC_RMID
, 0);
156 shminfo
->readOnly
= False
;
162 * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
165 alloc_shm_ximage(struct xm_buffer
*b
, struct xmesa_buffer
*xmb
,
166 unsigned width
, unsigned height
)
169 * We have to do a _lot_ of error checking here to be sure we can
170 * really use the XSHM extension. It seems different servers trigger
171 * errors at different points if the extension won't work. Therefore
172 * we have to be very careful...
177 int (*old_handler
)(XMesaDisplay
*, XErrorEvent
*);
179 b
->tempImage
= XShmCreateImage(xmb
->xm_visual
->display
,
180 xmb
->xm_visual
->visinfo
->visual
,
181 xmb
->xm_visual
->visinfo
->depth
,
186 if (b
->tempImage
== NULL
) {
193 old_handler
= XSetErrorHandler(mesaHandleXError
);
194 /* This may trigger the X protocol error we're ready to catch: */
195 XShmAttach(xmb
->xm_visual
->display
, &b
->shminfo
);
196 XSync(xmb
->xm_visual
->display
, False
);
198 if (mesaXErrorFlag
) {
199 /* we are on a remote display, this error is normal, don't print it */
200 XFlush(xmb
->xm_visual
->display
);
202 XDestroyImage(b
->tempImage
);
205 (void) XSetErrorHandler(old_handler
);
210 /* Finally, try an XShmPutImage to be really sure the extension works */
212 gc
= XCreateGC(xmb
->xm_visual
->display
, xmb
->drawable
, 0, NULL
);
213 XShmPutImage(xmb
->xm_visual
->display
, xmb
->drawable
, gc
,
214 b
->tempImage
, 0, 0, 0, 0, 1, 1 /*one pixel*/, False
);
215 XSync(xmb
->xm_visual
->display
, False
);
216 XFreeGC(xmb
->xm_visual
->display
, gc
);
217 (void) XSetErrorHandler(old_handler
);
218 if (mesaXErrorFlag
) {
219 XFlush(xmb
->xm_visual
->display
);
221 XDestroyImage(b
->tempImage
);
231 #define XSHM_ENABLED(b) 0
234 alloc_shm_ximage(struct xm_buffer
*b
, struct xmesa_buffer
*xmb
,
235 unsigned width
, unsigned height
)
239 #endif /* USE_XSHM */
244 /* Most callbacks map direcly onto dri_bufmgr operations:
247 xm_buffer_map(struct pipe_winsys
*pws
, struct pipe_buffer
*buf
,
250 struct xm_buffer
*xm_buf
= xm_buffer(buf
);
251 xm_buf
->mapped
= xm_buf
->data
;
252 return xm_buf
->mapped
;
256 xm_buffer_unmap(struct pipe_winsys
*pws
, struct pipe_buffer
*buf
)
258 struct xm_buffer
*xm_buf
= xm_buffer(buf
);
259 xm_buf
->mapped
= NULL
;
263 xm_buffer_destroy(struct pipe_winsys
*pws
,
264 struct pipe_buffer
*buf
)
266 struct xm_buffer
*oldBuf
= xm_buffer(buf
);
269 #if defined(USE_XSHM) && !defined(XFree86Server)
270 if (oldBuf
->shminfo
.shmid
>= 0) {
271 shmdt(oldBuf
->shminfo
.shmaddr
);
272 shmctl(oldBuf
->shminfo
.shmid
, IPC_RMID
, 0);
274 oldBuf
->shminfo
.shmid
= -1;
275 oldBuf
->shminfo
.shmaddr
= (char *) -1;
280 if (!oldBuf
->userBuffer
) {
281 align_free(oldBuf
->data
);
293 * Display a surface that's in a tiled configuration. That is, all the
294 * pixels for a TILE_SIZExTILE_SIZE block are contiguous in memory.
297 xmesa_display_surface_tiled(XMesaBuffer b
, const struct pipe_surface
*surf
)
300 struct xm_buffer
*xm_buf
= xm_buffer(surf
->buffer
);
301 const uint tilesPerRow
= (surf
->width
+ TILE_SIZE
- 1) / TILE_SIZE
;
304 /* check that the XImage has been previously initialized */
305 assert(ximage
->format
);
306 assert(ximage
->bitmap_unit
);
308 if (XSHM_ENABLED(xm_buf
) && (xm_buf
->tempImage
== NULL
)) {
309 alloc_shm_ximage(xm_buf
, b
, TILE_SIZE
, TILE_SIZE
);
312 ximage
= (XSHM_ENABLED(xm_buf
)) ? xm_buf
->tempImage
: b
->tempImage
;
314 if (!XSHM_ENABLED(xm_buf
)) {
315 /* update XImage's fields */
316 ximage
->width
= TILE_SIZE
;
317 ximage
->height
= TILE_SIZE
;
318 ximage
->bytes_per_line
= TILE_SIZE
* 4;
321 for (y
= 0; y
< surf
->height
; y
+= TILE_SIZE
) {
322 for (x
= 0; x
< surf
->width
; x
+= TILE_SIZE
) {
325 int tx
= x
/ TILE_SIZE
;
326 int ty
= y
/ TILE_SIZE
;
327 int offset
= ty
* tilesPerRow
+ tx
;
329 offset
*= 4 * TILE_SIZE
* TILE_SIZE
;
331 ximage
->data
= (char *) xm_buf
->data
+ offset
;
333 if (XSHM_ENABLED(xm_buf
)) {
334 #if defined(USE_XSHM) && !defined(XFree86Server)
335 XShmPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
336 ximage
, 0, 0, x
, y
, TILE_SIZE
, TILE_SIZE
, False
);
339 XPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
340 ximage
, 0, 0, dx
, dy
, TILE_SIZE
, TILE_SIZE
);
348 * Display/copy the image in the surface into the X window specified
349 * by the XMesaBuffer.
352 xmesa_display_surface(XMesaBuffer b
, const struct pipe_surface
*surf
)
355 struct xm_buffer
*xm_buf
= xm_buffer(surf
->buffer
);
356 const struct xmesa_surface
*xm_surf
357 = xmesa_surface((struct pipe_surface
*) surf
);
359 if (xm_surf
->no_swap
)
362 if (xm_surf
->tileSize
) {
363 xmesa_display_surface_tiled(b
, surf
);
368 if (XSHM_ENABLED(xm_buf
) && (xm_buf
->tempImage
== NULL
)) {
369 alloc_shm_ximage(xm_buf
, b
, surf
->pitch
, surf
->height
);
372 ximage
= (XSHM_ENABLED(xm_buf
)) ? xm_buf
->tempImage
: b
->tempImage
;
373 ximage
->data
= xm_buf
->data
;
375 /* display image in Window */
376 if (XSHM_ENABLED(xm_buf
)) {
377 #if defined(USE_XSHM) && !defined(XFree86Server)
378 XShmPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
379 ximage
, 0, 0, 0, 0, surf
->width
, surf
->height
, False
);
382 /* check that the XImage has been previously initialized */
383 assert(ximage
->format
);
384 assert(ximage
->bitmap_unit
);
386 /* update XImage's fields */
387 ximage
->width
= surf
->width
;
388 ximage
->height
= surf
->height
;
389 ximage
->bytes_per_line
= surf
->pitch
* surf
->cpp
;
391 XPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
392 ximage
, 0, 0, 0, 0, surf
->width
, surf
->height
);
398 xm_flush_frontbuffer(struct pipe_winsys
*pws
,
399 struct pipe_surface
*surf
,
400 void *context_private
)
403 * The front color buffer is actually just another XImage buffer.
404 * This function copies that XImage to the actual X Window.
406 XMesaContext xmctx
= (XMesaContext
) context_private
;
407 xmesa_display_surface(xmctx
->xm_buffer
, surf
);
413 xm_get_name(struct pipe_winsys
*pws
)
419 static struct pipe_buffer
*
420 xm_buffer_create(struct pipe_winsys
*pws
,
425 struct xm_buffer
*buffer
= CALLOC_STRUCT(xm_buffer
);
426 #if defined(USE_XSHM) && !defined(XFree86Server)
427 struct xmesa_pipe_winsys
*xpws
= (struct xmesa_pipe_winsys
*) pws
;
430 buffer
->base
.refcount
= 1;
431 buffer
->base
.alignment
= alignment
;
432 buffer
->base
.usage
= usage
;
433 buffer
->base
.size
= size
;
436 #if defined(USE_XSHM) && !defined(XFree86Server)
437 buffer
->shminfo
.shmid
= -1;
438 buffer
->shminfo
.shmaddr
= (char *) -1;
440 if (xpws
->shm
&& (usage
& PIPE_BUFFER_USAGE_PIXEL
) != 0) {
441 buffer
->shm
= xpws
->shm
;
443 if (alloc_shm(buffer
, size
)) {
444 buffer
->data
= buffer
->shminfo
.shmaddr
;
449 if (buffer
->data
== NULL
) {
452 /* align to 16-byte multiple for Cell */
453 buffer
->data
= align_malloc(size
, max(alignment
, 16));
456 return &buffer
->base
;
461 * Create buffer which wraps user-space data.
463 static struct pipe_buffer
*
464 xm_user_buffer_create(struct pipe_winsys
*pws
, void *ptr
, unsigned bytes
)
466 struct xm_buffer
*buffer
= CALLOC_STRUCT(xm_buffer
);
467 buffer
->base
.refcount
= 1;
468 buffer
->base
.size
= bytes
;
469 buffer
->userBuffer
= TRUE
;
473 return &buffer
->base
;
479 * Round n up to next multiple.
481 static INLINE
unsigned
482 round_up(unsigned n
, unsigned multiple
)
484 return (n
+ multiple
- 1) & ~(multiple
- 1);
488 xm_surface_alloc_storage(struct pipe_winsys
*winsys
,
489 struct pipe_surface
*surf
,
490 unsigned width
, unsigned height
,
491 enum pipe_format format
,
495 const unsigned alignment
= 64;
498 surf
->height
= height
;
499 surf
->format
= format
;
500 surf
->cpp
= pf_get_size(format
);
501 surf
->pitch
= round_up(width
, alignment
/ surf
->cpp
);
504 #ifdef GALLIUM_CELL /* XXX a bit of a hack */
505 height
= round_up(height
, TILE_SIZE
);
508 assert(!surf
->buffer
);
509 surf
->buffer
= winsys
->buffer_create(winsys
, alignment
,
510 PIPE_BUFFER_USAGE_PIXEL
,
511 surf
->pitch
* surf
->cpp
* height
);
520 * Called via winsys->surface_alloc() to create new surfaces.
522 static struct pipe_surface
*
523 xm_surface_alloc(struct pipe_winsys
*ws
)
525 struct xmesa_surface
*xms
= CALLOC_STRUCT(xmesa_surface
);
526 static boolean no_swap
= 0;
527 static boolean firsttime
= 1;
530 no_swap
= getenv("SP_NO_RAST") != NULL
;
536 xms
->surface
.refcount
= 1;
537 xms
->surface
.winsys
= ws
;
540 if (!getenv("GALLIUM_NOCELL")) {
541 xms
->tileSize
= 32; /** probably temporary */
545 xms
->no_swap
= no_swap
;
547 return &xms
->surface
;
553 xm_surface_release(struct pipe_winsys
*winsys
, struct pipe_surface
**s
)
555 struct pipe_surface
*surf
= *s
;
556 assert(!surf
->texture
);
558 if (surf
->refcount
== 0) {
560 pipe_buffer_reference(winsys
, &surf
->buffer
, NULL
);
568 * Fence functions - basically nothing to do, as we don't create any actual
573 xm_fence_reference(struct pipe_winsys
*sws
, struct pipe_fence_handle
**ptr
,
574 struct pipe_fence_handle
*fence
)
580 xm_fence_signalled(struct pipe_winsys
*sws
, struct pipe_fence_handle
*fence
,
588 xm_fence_finish(struct pipe_winsys
*sws
, struct pipe_fence_handle
*fence
,
596 * Return pointer to a pipe_winsys object.
597 * For Xlib, this is a singleton object.
598 * Nothing special for the Xlib driver so no subclassing or anything.
601 xmesa_get_pipe_winsys_aub(struct xmesa_visual
*xm_vis
)
603 static struct xmesa_pipe_winsys
*ws
= NULL
;
606 ws
= (struct xmesa_pipe_winsys
*) xmesa_create_pipe_winsys_aub();
612 static struct pipe_winsys
*
613 xmesa_get_pipe_winsys(struct xmesa_visual
*xm_vis
)
615 static struct xmesa_pipe_winsys
*ws
= NULL
;
618 ws
= CALLOC_STRUCT(xmesa_pipe_winsys
);
620 ws
->xm_visual
= xm_vis
;
621 ws
->shm
= xmesa_check_for_xshm(xm_vis
->display
);
623 /* Fill in this struct with callbacks that pipe will need to
624 * communicate with the window system, buffer manager, etc.
626 ws
->base
.buffer_create
= xm_buffer_create
;
627 ws
->base
.user_buffer_create
= xm_user_buffer_create
;
628 ws
->base
.buffer_map
= xm_buffer_map
;
629 ws
->base
.buffer_unmap
= xm_buffer_unmap
;
630 ws
->base
.buffer_destroy
= xm_buffer_destroy
;
632 ws
->base
.surface_alloc
= xm_surface_alloc
;
633 ws
->base
.surface_alloc_storage
= xm_surface_alloc_storage
;
634 ws
->base
.surface_release
= xm_surface_release
;
636 ws
->base
.fence_reference
= xm_fence_reference
;
637 ws
->base
.fence_signalled
= xm_fence_signalled
;
638 ws
->base
.fence_finish
= xm_fence_finish
;
640 ws
->base
.flush_frontbuffer
= xm_flush_frontbuffer
;
641 ws
->base
.get_name
= xm_get_name
;
648 struct pipe_context
*
649 xmesa_create_pipe_context(XMesaContext xmesa
, uint pixelformat
)
651 struct pipe_winsys
*pws
;
652 struct pipe_context
*pipe
;
654 if (getenv("XM_AUB")) {
655 pws
= xmesa_get_pipe_winsys_aub(xmesa
->xm_visual
);
658 pws
= xmesa_get_pipe_winsys(xmesa
->xm_visual
);
662 if (!getenv("GALLIUM_NOCELL")) {
663 struct cell_winsys
*cws
= cell_get_winsys(pixelformat
);
664 struct pipe_screen
*screen
= cell_create_screen(pws
);
666 pipe
= cell_create_context(screen
, cws
);
671 struct pipe_screen
*screen
= softpipe_create_screen(pws
);
673 pipe
= softpipe_create(screen
, pws
, NULL
);