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
;
78 #if defined(USE_XSHM) && !defined(XFree86Server)
79 # define XSHM_ENABLED(b) ((b)->shm)
81 # define XSHM_ENABLED(b) 0
86 * Subclass of pipe_surface for Xlib winsys
90 struct pipe_surface surface
;
98 * Subclass of pipe_winsys for Xlib winsys
100 struct xmesa_pipe_winsys
102 struct pipe_winsys base
;
103 struct xmesa_visual
*xm_visual
;
108 static void alloc_shm_ximage(struct xm_buffer
*b
, struct xmesa_buffer
*xmb
,
109 unsigned width
, unsigned height
);
113 static INLINE
struct xmesa_surface
*
114 xmesa_surface(struct pipe_surface
*ps
)
116 return (struct xmesa_surface
*) ps
;
122 * Turn the softpipe opaque buffer pointer into a dri_bufmgr opaque
125 static INLINE
struct xm_buffer
*
126 xm_buffer( struct pipe_buffer
*buf
)
128 return (struct xm_buffer
*)buf
;
133 /* Most callbacks map direcly onto dri_bufmgr operations:
136 xm_buffer_map(struct pipe_winsys
*pws
, struct pipe_buffer
*buf
,
139 struct xm_buffer
*xm_buf
= xm_buffer(buf
);
140 xm_buf
->mapped
= xm_buf
->data
;
141 return xm_buf
->mapped
;
145 xm_buffer_unmap(struct pipe_winsys
*pws
, struct pipe_buffer
*buf
)
147 struct xm_buffer
*xm_buf
= xm_buffer(buf
);
148 xm_buf
->mapped
= NULL
;
152 xm_buffer_destroy(struct pipe_winsys
*pws
,
153 struct pipe_buffer
*buf
)
155 struct xm_buffer
*oldBuf
= xm_buffer(buf
);
158 #if defined(USE_XSHM) && !defined(XFree86Server)
159 if (oldBuf
->shminfo
.shmid
>= 0) {
160 shmdt(oldBuf
->shminfo
.shmaddr
);
161 shmctl(oldBuf
->shminfo
.shmid
, IPC_RMID
, 0);
163 oldBuf
->shminfo
.shmid
= -1;
164 oldBuf
->shminfo
.shmaddr
= (char *) -1;
169 if (!oldBuf
->userBuffer
) {
170 align_free(oldBuf
->data
);
182 * Display a surface that's in a tiled configuration. That is, all the
183 * pixels for a TILE_SIZExTILE_SIZE block are contiguous in memory.
186 xmesa_display_surface_tiled(XMesaBuffer b
, const struct pipe_surface
*surf
)
189 struct xm_buffer
*xm_buf
= xm_buffer(surf
->buffer
);
190 const uint tilesPerRow
= (surf
->width
+ TILE_SIZE
- 1) / TILE_SIZE
;
193 /* check that the XImage has been previously initialized */
194 assert(ximage
->format
);
195 assert(ximage
->bitmap_unit
);
197 if (XSHM_ENABLED(xm_buf
) && (xm_buf
->tempImage
== NULL
)) {
198 alloc_shm_ximage(xm_buf
, b
, TILE_SIZE
, TILE_SIZE
);
201 ximage
= (XSHM_ENABLED(xm_buf
)) ? xm_buf
->tempImage
: b
->tempImage
;
203 if (!XSHM_ENABLED(xm_buf
)) {
204 /* update XImage's fields */
205 ximage
->width
= TILE_SIZE
;
206 ximage
->height
= TILE_SIZE
;
207 ximage
->bytes_per_line
= TILE_SIZE
* 4;
210 for (y
= 0; y
< surf
->height
; y
+= TILE_SIZE
) {
211 for (x
= 0; x
< surf
->width
; x
+= TILE_SIZE
) {
214 int tx
= x
/ TILE_SIZE
;
215 int ty
= y
/ TILE_SIZE
;
216 int offset
= ty
* tilesPerRow
+ tx
;
218 offset
*= 4 * TILE_SIZE
* TILE_SIZE
;
220 ximage
->data
= (char *) xm_buf
->data
+ offset
;
222 if (XSHM_ENABLED(xm_buf
)) {
223 #if defined(USE_XSHM) && !defined(XFree86Server)
224 XShmPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
225 ximage
, 0, 0, x
, y
, TILE_SIZE
, TILE_SIZE
, False
);
228 XPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
229 ximage
, 0, 0, dx
, dy
, TILE_SIZE
, TILE_SIZE
);
237 * Display/copy the image in the surface into the X window specified
238 * by the XMesaBuffer.
241 xmesa_display_surface(XMesaBuffer b
, const struct pipe_surface
*surf
)
244 struct xm_buffer
*xm_buf
= xm_buffer(surf
->buffer
);
245 const struct xmesa_surface
*xm_surf
246 = xmesa_surface((struct pipe_surface
*) surf
);
248 if (xm_surf
->no_swap
)
251 if (xm_surf
->tileSize
) {
252 xmesa_display_surface_tiled(b
, surf
);
257 if (XSHM_ENABLED(xm_buf
) && (xm_buf
->tempImage
== NULL
)) {
258 alloc_shm_ximage(xm_buf
, b
, surf
->pitch
, surf
->height
);
261 ximage
= (XSHM_ENABLED(xm_buf
)) ? xm_buf
->tempImage
: b
->tempImage
;
262 ximage
->data
= xm_buf
->data
;
264 /* display image in Window */
265 if (XSHM_ENABLED(xm_buf
)) {
266 #if defined(USE_XSHM) && !defined(XFree86Server)
267 XShmPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
268 ximage
, 0, 0, 0, 0, surf
->width
, surf
->height
, False
);
271 /* check that the XImage has been previously initialized */
272 assert(ximage
->format
);
273 assert(ximage
->bitmap_unit
);
275 /* update XImage's fields */
276 ximage
->width
= surf
->width
;
277 ximage
->height
= surf
->height
;
278 ximage
->bytes_per_line
= surf
->pitch
* surf
->cpp
;
280 XPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
281 ximage
, 0, 0, 0, 0, surf
->width
, surf
->height
);
287 xm_flush_frontbuffer(struct pipe_winsys
*pws
,
288 struct pipe_surface
*surf
,
289 void *context_private
)
292 * The front color buffer is actually just another XImage buffer.
293 * This function copies that XImage to the actual X Window.
295 XMesaContext xmctx
= (XMesaContext
) context_private
;
296 xmesa_display_surface(xmctx
->xm_buffer
, surf
);
302 xm_get_name(struct pipe_winsys
*pws
)
308 #if defined(USE_XSHM) && !defined(XFree86Server)
309 static volatile int mesaXErrorFlag
= 0;
312 * Catches potential Xlib errors.
315 mesaHandleXError(XMesaDisplay
*dpy
, XErrorEvent
*event
)
324 static GLboolean
alloc_shm(struct xm_buffer
*buf
, unsigned size
)
326 XShmSegmentInfo
*const shminfo
= & buf
->shminfo
;
328 shminfo
->shmid
= shmget(IPC_PRIVATE
, size
, IPC_CREAT
|0777);
329 if (shminfo
->shmid
< 0) {
333 shminfo
->shmaddr
= (char *) shmat(shminfo
->shmid
, 0, 0);
334 if (shminfo
->shmaddr
== (char *) -1) {
335 shmctl(shminfo
->shmid
, IPC_RMID
, 0);
339 shminfo
->readOnly
= False
;
345 * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
348 alloc_shm_ximage(struct xm_buffer
*b
, struct xmesa_buffer
*xmb
,
349 unsigned width
, unsigned height
)
352 * We have to do a _lot_ of error checking here to be sure we can
353 * really use the XSHM extension. It seems different servers trigger
354 * errors at different points if the extension won't work. Therefore
355 * we have to be very careful...
360 int (*old_handler
)(XMesaDisplay
*, XErrorEvent
*);
362 b
->tempImage
= XShmCreateImage(xmb
->xm_visual
->display
,
363 xmb
->xm_visual
->visinfo
->visual
,
364 xmb
->xm_visual
->visinfo
->depth
,
369 if (b
->tempImage
== NULL
) {
376 old_handler
= XSetErrorHandler(mesaHandleXError
);
377 /* This may trigger the X protocol error we're ready to catch: */
378 XShmAttach(xmb
->xm_visual
->display
, &b
->shminfo
);
379 XSync(xmb
->xm_visual
->display
, False
);
381 if (mesaXErrorFlag
) {
382 /* we are on a remote display, this error is normal, don't print it */
383 XFlush(xmb
->xm_visual
->display
);
385 XDestroyImage(b
->tempImage
);
388 (void) XSetErrorHandler(old_handler
);
393 /* Finally, try an XShmPutImage to be really sure the extension works */
395 gc
= XCreateGC(xmb
->xm_visual
->display
, xmb
->drawable
, 0, NULL
);
396 XShmPutImage(xmb
->xm_visual
->display
, xmb
->drawable
, gc
,
397 b
->tempImage
, 0, 0, 0, 0, 1, 1 /*one pixel*/, False
);
398 XSync(xmb
->xm_visual
->display
, False
);
399 XFreeGC(xmb
->xm_visual
->display
, gc
);
400 (void) XSetErrorHandler(old_handler
);
401 if (mesaXErrorFlag
) {
402 XFlush(xmb
->xm_visual
->display
);
404 XDestroyImage(b
->tempImage
);
413 alloc_shm_ximage(struct xm_buffer
*b
, struct xmesa_buffer
*xmb
,
414 unsigned width
, unsigned height
)
421 static struct pipe_buffer
*
422 xm_buffer_create(struct pipe_winsys
*pws
,
427 struct xm_buffer
*buffer
= CALLOC_STRUCT(xm_buffer
);
428 #if defined(USE_XSHM) && !defined(XFree86Server)
429 struct xmesa_pipe_winsys
*xpws
= (struct xmesa_pipe_winsys
*) pws
;
432 buffer
->base
.refcount
= 1;
433 buffer
->base
.alignment
= alignment
;
434 buffer
->base
.usage
= usage
;
435 buffer
->base
.size
= size
;
438 #if defined(USE_XSHM) && !defined(XFree86Server)
439 buffer
->shminfo
.shmid
= -1;
440 buffer
->shminfo
.shmaddr
= (char *) -1;
442 if (xpws
->shm
&& (usage
& PIPE_BUFFER_USAGE_PIXEL
) != 0) {
443 buffer
->shm
= xpws
->shm
;
445 if (alloc_shm(buffer
, size
)) {
446 buffer
->data
= buffer
->shminfo
.shmaddr
;
451 if (buffer
->data
== NULL
) {
454 /* align to 16-byte multiple for Cell */
455 buffer
->data
= align_malloc(size
, max(alignment
, 16));
458 return &buffer
->base
;
463 * Create buffer which wraps user-space data.
465 static struct pipe_buffer
*
466 xm_user_buffer_create(struct pipe_winsys
*pws
, void *ptr
, unsigned bytes
)
468 struct xm_buffer
*buffer
= CALLOC_STRUCT(xm_buffer
);
469 buffer
->base
.refcount
= 1;
470 buffer
->base
.size
= bytes
;
471 buffer
->userBuffer
= TRUE
;
475 return &buffer
->base
;
481 * Round n up to next multiple.
483 static INLINE
unsigned
484 round_up(unsigned n
, unsigned multiple
)
486 return (n
+ multiple
- 1) & ~(multiple
- 1);
490 xm_surface_alloc_storage(struct pipe_winsys
*winsys
,
491 struct pipe_surface
*surf
,
492 unsigned width
, unsigned height
,
493 enum pipe_format format
,
496 const unsigned alignment
= 64;
499 surf
->height
= height
;
500 surf
->format
= format
;
501 surf
->cpp
= pf_get_size(format
);
502 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
;
557 if (surf
->refcount
== 0) {
559 pipe_buffer_reference(winsys
, &surf
->buffer
, NULL
);
567 * Fence functions - basically nothing to do, as we don't create any actual
572 xm_fence_reference(struct pipe_winsys
*sws
, struct pipe_fence_handle
**ptr
,
573 struct pipe_fence_handle
*fence
)
579 xm_fence_signalled(struct pipe_winsys
*sws
, struct pipe_fence_handle
*fence
,
587 xm_fence_finish(struct pipe_winsys
*sws
, struct pipe_fence_handle
*fence
,
595 * Return pointer to a pipe_winsys object.
596 * For Xlib, this is a singleton object.
597 * Nothing special for the Xlib driver so no subclassing or anything.
600 xmesa_get_pipe_winsys_aub(struct xmesa_visual
*xm_vis
)
602 static struct xmesa_pipe_winsys
*ws
= NULL
;
605 ws
= (struct xmesa_pipe_winsys
*) xmesa_create_pipe_winsys_aub();
612 xmesa_get_pipe_winsys(struct xmesa_visual
*xm_vis
)
614 static struct xmesa_pipe_winsys
*ws
= NULL
;
617 ws
= CALLOC_STRUCT(xmesa_pipe_winsys
);
619 ws
->xm_visual
= xm_vis
;
620 ws
->shm
= xmesa_check_for_xshm(xm_vis
->display
);
622 /* Fill in this struct with callbacks that pipe will need to
623 * communicate with the window system, buffer manager, etc.
625 ws
->base
.buffer_create
= xm_buffer_create
;
626 ws
->base
.user_buffer_create
= xm_user_buffer_create
;
627 ws
->base
.buffer_map
= xm_buffer_map
;
628 ws
->base
.buffer_unmap
= xm_buffer_unmap
;
629 ws
->base
.buffer_destroy
= xm_buffer_destroy
;
631 ws
->base
.surface_alloc
= xm_surface_alloc
;
632 ws
->base
.surface_alloc_storage
= xm_surface_alloc_storage
;
633 ws
->base
.surface_release
= xm_surface_release
;
635 ws
->base
.fence_reference
= xm_fence_reference
;
636 ws
->base
.fence_signalled
= xm_fence_signalled
;
637 ws
->base
.fence_finish
= xm_fence_finish
;
639 ws
->base
.flush_frontbuffer
= xm_flush_frontbuffer
;
640 ws
->base
.get_name
= xm_get_name
;
647 struct pipe_context
*
648 xmesa_create_pipe_context(XMesaContext xmesa
, uint pixelformat
)
650 struct pipe_winsys
*pws
;
651 struct pipe_context
*pipe
;
653 if (getenv("XM_AUB")) {
654 pws
= xmesa_get_pipe_winsys_aub(xmesa
->xm_visual
);
657 pws
= xmesa_get_pipe_winsys(xmesa
->xm_visual
);
661 if (!getenv("GALLIUM_NOCELL")) {
662 struct cell_winsys
*cws
= cell_get_winsys(pixelformat
);
663 struct pipe_screen
*screen
= cell_create_screen(pws
);
665 pipe
= cell_create_context(screen
, cws
);
670 struct pipe_screen
*screen
= softpipe_create_screen(pws
);
672 pipe
= softpipe_create(screen
, pws
, NULL
);