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 **************************************************************************/
44 #include "pipe/p_winsys.h"
45 #include "pipe/p_format.h"
46 #include "pipe/p_context.h"
47 #include "pipe/p_inlines.h"
48 #include "util/u_math.h"
49 #include "util/u_memory.h"
51 #include "cell/ppu/cell_context.h"
52 #include "cell/ppu/cell_screen.h"
53 #include "cell/ppu/cell_winsys.h"
57 * Subclass of pipe_buffer for Xlib winsys.
58 * Low-level OS/window system memory buffer
62 struct pipe_buffer base
;
63 boolean userBuffer
; /** Is this a user-space buffer? */
73 * Subclass of pipe_winsys for Xlib winsys
75 struct xmesa_pipe_winsys
77 struct pipe_winsys base
;
83 static INLINE
struct xm_buffer
*
84 xm_buffer( struct pipe_buffer
*buf
)
86 return (struct xm_buffer
*)buf
;
90 /* Most callbacks map direcly onto dri_bufmgr operations:
93 xm_buffer_map(struct pipe_winsys
*pws
, struct pipe_buffer
*buf
,
96 struct xm_buffer
*xm_buf
= xm_buffer(buf
);
97 xm_buf
->mapped
= xm_buf
->data
;
98 return xm_buf
->mapped
;
102 xm_buffer_unmap(struct pipe_winsys
*pws
, struct pipe_buffer
*buf
)
104 struct xm_buffer
*xm_buf
= xm_buffer(buf
);
105 xm_buf
->mapped
= NULL
;
109 xm_buffer_destroy(struct pipe_winsys
*pws
,
110 struct pipe_buffer
*buf
)
112 struct xm_buffer
*oldBuf
= xm_buffer(buf
);
116 if (!oldBuf
->userBuffer
) {
117 align_free(oldBuf
->data
);
129 * For Cell. Basically, rearrange the pixels/quads from this layout:
142 twiddle_tile(const uint
*tileIn
, uint
*tileOut
)
146 for (y
= 0; y
< TILE_SIZE
; y
+=2) {
147 for (x
= 0; x
< TILE_SIZE
; x
+=2) {
148 int k
= 4 * (y
/2 * TILE_SIZE
/2 + x
/2);
149 tileOut
[y
* TILE_SIZE
+ (x
+ 0)] = tileIn
[k
];
150 tileOut
[y
* TILE_SIZE
+ (x
+ 1)] = tileIn
[k
+1];
151 tileOut
[(y
+ 1) * TILE_SIZE
+ (x
+ 0)] = tileIn
[k
+2];
152 tileOut
[(y
+ 1) * TILE_SIZE
+ (x
+ 1)] = tileIn
[k
+3];
160 * Display a surface that's in a tiled configuration. That is, all the
161 * pixels for a TILE_SIZExTILE_SIZE block are contiguous in memory.
164 xlib_cell_display_surface(struct xmesa_buffer
*b
, struct pipe_surface
*surf
)
167 struct xm_buffer
*xm_buf
= xm_buffer(surf
->buffer
);
168 const uint tilesPerRow
= (surf
->width
+ TILE_SIZE
- 1) / TILE_SIZE
;
171 ximage
= b
->tempImage
;
173 /* check that the XImage has been previously initialized */
174 assert(ximage
->format
);
175 assert(ximage
->bitmap_unit
);
177 /* update XImage's fields */
178 ximage
->width
= TILE_SIZE
;
179 ximage
->height
= TILE_SIZE
;
180 ximage
->bytes_per_line
= TILE_SIZE
* 4;
182 for (y
= 0; y
< surf
->height
; y
+= TILE_SIZE
) {
183 for (x
= 0; x
< surf
->width
; x
+= TILE_SIZE
) {
184 uint tmpTile
[TILE_SIZE
* TILE_SIZE
];
185 int tx
= x
/ TILE_SIZE
;
186 int ty
= y
/ TILE_SIZE
;
187 int offset
= ty
* tilesPerRow
+ tx
;
191 if (y
+ h
> surf
->height
)
192 h
= surf
->height
- y
;
193 if (x
+ w
> surf
->width
)
196 /* offset in pixels */
197 offset
*= TILE_SIZE
* TILE_SIZE
;
199 /* twiddle from ximage buffer to temp tile */
200 twiddle_tile((uint
*) xm_buf
->data
+ offset
, tmpTile
);
201 /* display temp tile data */
202 ximage
->data
= (char *) tmpTile
;
203 XPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
204 ximage
, 0, 0, x
, y
, w
, h
);
214 xm_flush_frontbuffer(struct pipe_winsys
*pws
,
215 struct pipe_surface
*surf
,
216 void *context_private
)
219 * The front color buffer is actually just another XImage buffer.
220 * This function copies that XImage to the actual X Window.
222 XMesaContext xmctx
= (XMesaContext
) context_private
;
223 xlib_cell_display_surface(xmctx
->xm_buffer
, surf
);
229 xm_get_name(struct pipe_winsys
*pws
)
235 static struct pipe_buffer
*
236 xm_buffer_create(struct pipe_winsys
*pws
,
241 struct xm_buffer
*buffer
= CALLOC_STRUCT(xm_buffer
);
243 buffer
->base
.refcount
= 1;
244 buffer
->base
.alignment
= alignment
;
245 buffer
->base
.usage
= usage
;
246 buffer
->base
.size
= size
;
249 if (buffer
->data
== NULL
) {
252 /* align to 16-byte multiple for Cell */
253 buffer
->data
= align_malloc(size
, max(alignment
, 16));
256 return &buffer
->base
;
261 * Create buffer which wraps user-space data.
263 static struct pipe_buffer
*
264 xm_user_buffer_create(struct pipe_winsys
*pws
, void *ptr
, unsigned bytes
)
266 struct xm_buffer
*buffer
= CALLOC_STRUCT(xm_buffer
);
267 buffer
->base
.refcount
= 1;
268 buffer
->base
.size
= bytes
;
269 buffer
->userBuffer
= TRUE
;
273 return &buffer
->base
;
279 * Round n up to next multiple.
281 static INLINE
unsigned
282 round_up(unsigned n
, unsigned multiple
)
284 return (n
+ multiple
- 1) & ~(multiple
- 1);
288 xm_surface_alloc_storage(struct pipe_winsys
*winsys
,
289 struct pipe_surface
*surf
,
290 unsigned width
, unsigned height
,
291 enum pipe_format format
,
295 const unsigned alignment
= 64;
298 surf
->height
= height
;
299 surf
->format
= format
;
300 pf_get_block(format
, &surf
->block
);
301 surf
->nblocksx
= pf_get_nblocksx(&surf
->block
, width
);
302 surf
->nblocksy
= pf_get_nblocksy(&surf
->block
, height
);
303 surf
->stride
= round_up(surf
->nblocksx
* surf
->block
.size
, alignment
);
306 assert(!surf
->buffer
);
307 surf
->buffer
= winsys
->buffer_create(winsys
, alignment
,
308 PIPE_BUFFER_USAGE_PIXEL
,
309 /* XXX a bit of a hack */
310 surf
->stride
* round_up(surf
->nblocksy
, TILE_SIZE
));
320 * Called via winsys->surface_alloc() to create new surfaces.
322 static struct pipe_surface
*
323 xm_surface_alloc(struct pipe_winsys
*ws
)
325 struct pipe_surface
*surface
= CALLOC_STRUCT(pipe_surface
);
329 surface
->refcount
= 1;
330 surface
->winsys
= ws
;
338 xm_surface_release(struct pipe_winsys
*winsys
, struct pipe_surface
**s
)
340 struct pipe_surface
*surf
= *s
;
341 assert(!surf
->texture
);
343 if (surf
->refcount
== 0) {
345 winsys_buffer_reference(winsys
, &surf
->buffer
, NULL
);
353 * Fence functions - basically nothing to do, as we don't create any actual
358 xm_fence_reference(struct pipe_winsys
*sws
, struct pipe_fence_handle
**ptr
,
359 struct pipe_fence_handle
*fence
)
365 xm_fence_signalled(struct pipe_winsys
*sws
, struct pipe_fence_handle
*fence
,
373 xm_fence_finish(struct pipe_winsys
*sws
, struct pipe_fence_handle
*fence
,
381 static struct pipe_winsys
*
382 xlib_create_cell_winsys( void )
384 static struct xmesa_pipe_winsys
*ws
= NULL
;
387 ws
= CALLOC_STRUCT(xmesa_pipe_winsys
);
389 /* Fill in this struct with callbacks that pipe will need to
390 * communicate with the window system, buffer manager, etc.
392 ws
->base
.buffer_create
= xm_buffer_create
;
393 ws
->base
.user_buffer_create
= xm_user_buffer_create
;
394 ws
->base
.buffer_map
= xm_buffer_map
;
395 ws
->base
.buffer_unmap
= xm_buffer_unmap
;
396 ws
->base
.buffer_destroy
= xm_buffer_destroy
;
398 ws
->base
.surface_alloc
= xm_surface_alloc
;
399 ws
->base
.surface_alloc_storage
= xm_surface_alloc_storage
;
400 ws
->base
.surface_release
= xm_surface_release
;
402 ws
->base
.fence_reference
= xm_fence_reference
;
403 ws
->base
.fence_signalled
= xm_fence_signalled
;
404 ws
->base
.fence_finish
= xm_fence_finish
;
406 ws
->base
.flush_frontbuffer
= xm_flush_frontbuffer
;
407 ws
->base
.get_name
= xm_get_name
;
414 static struct pipe_screen
*
415 xlib_create_cell_screen( struct pipe_winsys
*pws
)
417 struct pipe_winsys
*winsys
;
418 struct pipe_screen
*screen
;
420 winsys
= xlib_create_cell_winsys();
424 screen
= cell_create_screen(winsys
);
432 winsys
->destroy( winsys
);
438 static struct pipe_context
*
439 xlib_create_cell_context( struct pipe_screen
*screen
,
442 struct pipe_context
*pipe
;
445 /* This takes a cell_winsys pointer, but probably that should be
446 * created and stored at screen creation, not context creation.
448 * The actual cell_winsys value isn't used for anything, so just
449 * passing NULL for now.
451 pipe
= cell_create_context( screen
, NULL
);
463 struct xm_driver xlib_cell_driver
=
465 .create_pipe_screen
= xlib_create_cell_screen
,
466 .create_pipe_context
= xlib_create_cell_context
,
467 .display_surface
= xlib_cell_display_surface
,
472 struct xm_driver xlib_cell_driver
=
474 .create_pipe_screen
= NULL
,
475 .create_pipe_context
= NULL
,
476 .display_surface
= NULL
,