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"
41 #include "pipe/p_winsys.h"
42 #include "pipe/p_format.h"
43 #include "pipe/p_context.h"
44 #include "pipe/p_util.h"
45 #include "pipe/p_inlines.h"
46 #include "softpipe/sp_winsys.h"
49 #include "cell/ppu/cell_context.h"
50 #include "cell/ppu/cell_screen.h"
51 #include "cell/ppu/cell_winsys.h"
53 #define TILE_SIZE 32 /* avoid compilation errors */
56 #include "xm_winsys_aub.h"
60 * Low-level OS/window system memory buffer
64 struct pipe_buffer base
;
65 boolean userBuffer
; /** Is this a user-space buffer? */
71 #if defined(USE_XSHM) && !defined(XFree86Server)
72 XShmSegmentInfo shminfo
;
76 #if defined(USE_XSHM) && !defined(XFree86Server)
77 # define XSHM_ENABLED(b) ((b)->shm)
79 # define XSHM_ENABLED(b) 0
84 struct pipe_surface surface
;
92 * Derived from softpipe_winsys.
93 * We just need one extra field which indicates the pixel format to use for
94 * drawing surfaces so that we're compatible with the XVisual/window format.
96 struct xmesa_softpipe_winsys
98 struct softpipe_winsys spws
;
99 enum pipe_format pixelformat
;
103 struct xmesa_pipe_winsys
105 struct pipe_winsys base
;
106 struct xmesa_visual
*xm_visual
;
111 static void alloc_shm_ximage(struct xm_buffer
*b
, struct xmesa_buffer
*xmb
,
112 unsigned width
, unsigned height
);
115 static INLINE
struct xmesa_surface
*
116 xmesa_surface(struct pipe_surface
*ps
)
119 return (struct xmesa_surface
*) ps
;
123 static INLINE
struct xmesa_softpipe_winsys
*
124 xmesa_softpipe_winsys(struct softpipe_winsys
*spws
)
126 return (struct xmesa_softpipe_winsys
*) spws
;
130 * Turn the softpipe opaque buffer pointer into a dri_bufmgr opaque
133 static INLINE
struct xm_buffer
*
134 xm_buffer( struct pipe_buffer
*buf
)
136 return (struct xm_buffer
*)buf
;
141 /* Most callbacks map direcly onto dri_bufmgr operations:
144 xm_buffer_map(struct pipe_winsys
*pws
, struct pipe_buffer
*buf
,
147 struct xm_buffer
*xm_buf
= xm_buffer(buf
);
148 xm_buf
->mapped
= xm_buf
->data
;
149 return xm_buf
->mapped
;
153 xm_buffer_unmap(struct pipe_winsys
*pws
, struct pipe_buffer
*buf
)
155 struct xm_buffer
*xm_buf
= xm_buffer(buf
);
156 xm_buf
->mapped
= NULL
;
160 xm_buffer_destroy(struct pipe_winsys
*pws
,
161 struct pipe_buffer
*buf
)
163 struct xm_buffer
*oldBuf
= xm_buffer(buf
);
166 #if defined(USE_XSHM) && !defined(XFree86Server)
167 if (oldBuf
->shminfo
.shmid
>= 0) {
168 shmdt(oldBuf
->shminfo
.shmaddr
);
169 shmctl(oldBuf
->shminfo
.shmid
, IPC_RMID
, 0);
171 oldBuf
->shminfo
.shmid
= -1;
172 oldBuf
->shminfo
.shmaddr
= (char *) -1;
177 if (!oldBuf
->userBuffer
) {
178 align_free(oldBuf
->data
);
190 * Display a surface that's in a tiled configuration. That is, all the
191 * pixels for a TILE_SIZExTILE_SIZE block are contiguous in memory.
194 xmesa_display_surface_tiled(XMesaBuffer b
, const struct pipe_surface
*surf
)
197 struct xm_buffer
*xm_buf
= xm_buffer(surf
->buffer
);
198 const uint tilesPerRow
= (surf
->width
+ TILE_SIZE
- 1) / TILE_SIZE
;
201 /* check that the XImage has been previously initialized */
202 assert(ximage
->format
);
203 assert(ximage
->bitmap_unit
);
205 if (XSHM_ENABLED(xm_buf
) && (xm_buf
->tempImage
== NULL
)) {
206 alloc_shm_ximage(xm_buf
, b
, TILE_SIZE
, TILE_SIZE
);
209 ximage
= (XSHM_ENABLED(xm_buf
)) ? xm_buf
->tempImage
: b
->tempImage
;
211 if (!XSHM_ENABLED(xm_buf
)) {
212 /* update XImage's fields */
213 ximage
->width
= TILE_SIZE
;
214 ximage
->height
= TILE_SIZE
;
215 ximage
->bytes_per_line
= TILE_SIZE
* 4;
218 for (y
= 0; y
< surf
->height
; y
+= TILE_SIZE
) {
219 for (x
= 0; x
< surf
->width
; x
+= TILE_SIZE
) {
222 int tx
= x
/ TILE_SIZE
;
223 int ty
= y
/ TILE_SIZE
;
224 int offset
= ty
* tilesPerRow
+ tx
;
226 offset
*= 4 * TILE_SIZE
* TILE_SIZE
;
228 ximage
->data
= (char *) xm_buf
->data
+ offset
;
230 if (XSHM_ENABLED(xm_buf
)) {
231 #if defined(USE_XSHM) && !defined(XFree86Server)
232 XShmPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
233 ximage
, 0, 0, x
, y
, TILE_SIZE
, TILE_SIZE
, False
);
236 XPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
237 ximage
, 0, 0, dx
, dy
, TILE_SIZE
, TILE_SIZE
);
245 * Display/copy the image in the surface into the X window specified
246 * by the XMesaBuffer.
249 xmesa_display_surface(XMesaBuffer b
, const struct pipe_surface
*surf
)
252 struct xm_buffer
*xm_buf
= xm_buffer(surf
->buffer
);
253 const struct xmesa_surface
*xm_surf
254 = xmesa_surface((struct pipe_surface
*) surf
);
256 if (xm_surf
->no_swap
)
259 if (xm_surf
->tileSize
) {
260 xmesa_display_surface_tiled(b
, surf
);
265 if (XSHM_ENABLED(xm_buf
) && (xm_buf
->tempImage
== NULL
)) {
266 alloc_shm_ximage(xm_buf
, b
, surf
->pitch
, surf
->height
);
269 ximage
= (XSHM_ENABLED(xm_buf
)) ? xm_buf
->tempImage
: b
->tempImage
;
270 ximage
->data
= xm_buf
->data
;
272 /* display image in Window */
273 if (XSHM_ENABLED(xm_buf
)) {
274 #if defined(USE_XSHM) && !defined(XFree86Server)
275 XShmPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
276 ximage
, 0, 0, 0, 0, surf
->width
, surf
->height
, False
);
279 /* check that the XImage has been previously initialized */
280 assert(ximage
->format
);
281 assert(ximage
->bitmap_unit
);
283 /* update XImage's fields */
284 ximage
->width
= surf
->width
;
285 ximage
->height
= surf
->height
;
286 ximage
->bytes_per_line
= surf
->pitch
* surf
->cpp
;
288 XPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
289 ximage
, 0, 0, 0, 0, surf
->width
, surf
->height
);
295 xm_flush_frontbuffer(struct pipe_winsys
*pws
,
296 struct pipe_surface
*surf
,
297 void *context_private
)
299 /* The Xlib driver's front color surfaces are actually X Windows so
300 * this flush is a no-op.
301 * If we instead did front buffer rendering to a temporary XImage,
302 * this would be the place to copy the Ximage to the on-screen Window.
304 XMesaContext xmctx
= (XMesaContext
) context_private
;
305 xmesa_display_surface(xmctx
->xm_buffer
, surf
);
311 xm_get_name(struct pipe_winsys
*pws
)
317 #if defined(USE_XSHM) && !defined(XFree86Server)
318 static volatile int mesaXErrorFlag
= 0;
321 * Catches potential Xlib errors.
324 mesaHandleXError(XMesaDisplay
*dpy
, XErrorEvent
*event
)
333 static GLboolean
alloc_shm(struct xm_buffer
*buf
, unsigned size
)
335 XShmSegmentInfo
*const shminfo
= & buf
->shminfo
;
337 shminfo
->shmid
= shmget(IPC_PRIVATE
, size
, IPC_CREAT
|0777);
338 if (shminfo
->shmid
< 0) {
342 shminfo
->shmaddr
= (char *) shmat(shminfo
->shmid
, 0, 0);
343 if (shminfo
->shmaddr
== (char *) -1) {
344 shmctl(shminfo
->shmid
, IPC_RMID
, 0);
348 shminfo
->readOnly
= False
;
354 * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
357 alloc_shm_ximage(struct xm_buffer
*b
, struct xmesa_buffer
*xmb
,
358 unsigned width
, unsigned height
)
361 * We have to do a _lot_ of error checking here to be sure we can
362 * really use the XSHM extension. It seems different servers trigger
363 * errors at different points if the extension won't work. Therefore
364 * we have to be very careful...
369 int (*old_handler
)(XMesaDisplay
*, XErrorEvent
*);
371 b
->tempImage
= XShmCreateImage(xmb
->xm_visual
->display
,
372 xmb
->xm_visual
->visinfo
->visual
,
373 xmb
->xm_visual
->visinfo
->depth
,
378 if (b
->tempImage
== NULL
) {
385 old_handler
= XSetErrorHandler(mesaHandleXError
);
386 /* This may trigger the X protocol error we're ready to catch: */
387 XShmAttach(xmb
->xm_visual
->display
, &b
->shminfo
);
388 XSync(xmb
->xm_visual
->display
, False
);
390 if (mesaXErrorFlag
) {
391 /* we are on a remote display, this error is normal, don't print it */
392 XFlush(xmb
->xm_visual
->display
);
394 XDestroyImage(b
->tempImage
);
397 (void) XSetErrorHandler(old_handler
);
402 /* Finally, try an XShmPutImage to be really sure the extension works */
404 gc
= XCreateGC(xmb
->xm_visual
->display
, xmb
->drawable
, 0, NULL
);
405 XShmPutImage(xmb
->xm_visual
->display
, xmb
->drawable
, gc
,
406 b
->tempImage
, 0, 0, 0, 0, 1, 1 /*one pixel*/, False
);
407 XSync(xmb
->xm_visual
->display
, False
);
408 XFreeGC(xmb
->xm_visual
->display
, gc
);
409 (void) XSetErrorHandler(old_handler
);
410 if (mesaXErrorFlag
) {
411 XFlush(xmb
->xm_visual
->display
);
413 XDestroyImage(b
->tempImage
);
422 alloc_shm_ximage(struct xm_buffer
*b
, struct xmesa_buffer
*xmb
,
423 unsigned width
, unsigned height
)
430 static struct pipe_buffer
*
431 xm_buffer_create(struct pipe_winsys
*pws
,
436 struct xm_buffer
*buffer
= CALLOC_STRUCT(xm_buffer
);
437 #if defined(USE_XSHM) && !defined(XFree86Server)
438 struct xmesa_pipe_winsys
*xpws
= (struct xmesa_pipe_winsys
*) pws
;
441 buffer
->base
.refcount
= 1;
442 buffer
->base
.alignment
= alignment
;
443 buffer
->base
.usage
= usage
;
444 buffer
->base
.size
= size
;
447 #if defined(USE_XSHM) && !defined(XFree86Server)
448 buffer
->shminfo
.shmid
= -1;
449 buffer
->shminfo
.shmaddr
= (char *) -1;
451 if (xpws
->shm
&& (usage
& PIPE_BUFFER_USAGE_PIXEL
) != 0) {
452 buffer
->shm
= xpws
->shm
;
454 if (alloc_shm(buffer
, size
)) {
455 buffer
->data
= buffer
->shminfo
.shmaddr
;
460 if (buffer
->data
== NULL
) {
463 /* align to 16-byte multiple for Cell */
464 buffer
->data
= align_malloc(size
, max(alignment
, 16));
467 return &buffer
->base
;
472 * Create buffer which wraps user-space data.
474 static struct pipe_buffer
*
475 xm_user_buffer_create(struct pipe_winsys
*pws
, void *ptr
, unsigned bytes
)
477 struct xm_buffer
*buffer
= CALLOC_STRUCT(xm_buffer
);
478 buffer
->base
.refcount
= 1;
479 buffer
->base
.size
= bytes
;
480 buffer
->userBuffer
= TRUE
;
484 return &buffer
->base
;
490 * Round n up to next multiple.
492 static INLINE
unsigned
493 round_up(unsigned n
, unsigned multiple
)
495 return (n
+ multiple
- 1) & ~(multiple
- 1);
499 xm_surface_alloc_storage(struct pipe_winsys
*winsys
,
500 struct pipe_surface
*surf
,
501 unsigned width
, unsigned height
,
502 enum pipe_format format
,
505 const unsigned alignment
= 64;
508 surf
->height
= height
;
509 surf
->format
= format
;
510 surf
->cpp
= pf_get_size(format
);
511 surf
->pitch
= round_up(width
, alignment
/ surf
->cpp
);
513 #ifdef GALLIUM_CELL /* XXX a bit of a hack */
514 height
= round_up(height
, TILE_SIZE
);
517 assert(!surf
->buffer
);
518 surf
->buffer
= winsys
->buffer_create(winsys
, alignment
,
519 PIPE_BUFFER_USAGE_PIXEL
,
520 surf
->pitch
* surf
->cpp
* height
);
529 * Called via pipe->surface_alloc() to create new surfaces (textures,
530 * renderbuffers, etc.
532 static struct pipe_surface
*
533 xm_surface_alloc(struct pipe_winsys
*ws
)
535 struct xmesa_surface
*xms
= CALLOC_STRUCT(xmesa_surface
);
536 static boolean no_swap
= 0;
537 static boolean firsttime
= 1;
540 no_swap
= getenv("SP_NO_RAST") != NULL
;
546 xms
->surface
.refcount
= 1;
547 xms
->surface
.winsys
= ws
;
550 if (!getenv("GALLIUM_NOCELL")) {
551 xms
->tileSize
= 32; /** probably temporary */
555 xms
->no_swap
= no_swap
;
557 return &xms
->surface
;
563 xm_surface_release(struct pipe_winsys
*winsys
, struct pipe_surface
**s
)
565 struct pipe_surface
*surf
= *s
;
567 if (surf
->refcount
== 0) {
569 pipe_buffer_reference(winsys
, &surf
->buffer
, NULL
);
577 * Fence functions - basically nothing to do, as we don't create any actual
582 xm_fence_reference(struct pipe_winsys
*sws
, struct pipe_fence_handle
**ptr
,
583 struct pipe_fence_handle
*fence
)
589 xm_fence_signalled(struct pipe_winsys
*sws
, struct pipe_fence_handle
*fence
,
597 xm_fence_finish(struct pipe_winsys
*sws
, struct pipe_fence_handle
*fence
,
605 * Return pointer to a pipe_winsys object.
606 * For Xlib, this is a singleton object.
607 * Nothing special for the Xlib driver so no subclassing or anything.
610 xmesa_get_pipe_winsys_aub(struct xmesa_visual
*xm_vis
)
612 static struct xmesa_pipe_winsys
*ws
= NULL
;
614 if (!ws
&& getenv("XM_AUB")) {
615 ws
= (struct xmesa_pipe_winsys
*) xmesa_create_pipe_winsys_aub();
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
;
649 * Called via softpipe_winsys->is_format_supported().
650 * This function is only called to test formats for front/back color surfaces.
651 * The winsys being queried will have been created at glXCreateContext
652 * time, with a pixel format corresponding to the context's visual.
655 xmesa_is_format_supported(struct softpipe_winsys
*sws
,
656 enum pipe_format format
)
658 struct xmesa_softpipe_winsys
*xmws
= xmesa_softpipe_winsys(sws
);
659 return (format
== xmws
->pixelformat
);
664 * Return pointer to a softpipe_winsys object.
666 static struct softpipe_winsys
*
667 xmesa_get_softpipe_winsys(uint pixelformat
)
669 struct xmesa_softpipe_winsys
*xmws
670 = CALLOC_STRUCT(xmesa_softpipe_winsys
);
674 xmws
->spws
.is_format_supported
= xmesa_is_format_supported
;
675 xmws
->pixelformat
= pixelformat
;
681 struct pipe_context
*
682 xmesa_create_pipe_context(XMesaContext xmesa
, uint pixelformat
)
684 struct pipe_winsys
*pws
= xmesa_get_pipe_winsys_aub(xmesa
->xm_visual
);
685 struct pipe_context
*pipe
;
688 if (!getenv("GALLIUM_NOCELL")) {
689 struct cell_winsys
*cws
= cell_get_winsys(pixelformat
);
690 struct pipe_screen
*screen
= cell_create_screen(pws
);
692 pipe
= cell_create_context(screen
, cws
);
697 struct softpipe_winsys
*spws
= xmesa_get_softpipe_winsys(pixelformat
);
698 struct pipe_screen
*screen
= softpipe_create_screen(pws
);
700 pipe
= softpipe_create(screen
, pws
, spws
);