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/internal/p_winsys_screen.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"
54 #include "cell/ppu/cell_texture.h"
58 * Subclass of pipe_buffer for Xlib winsys.
59 * Low-level OS/window system memory buffer
63 struct pipe_buffer base
;
64 boolean userBuffer
; /** Is this a user-space buffer? */
74 * Subclass of pipe_winsys for Xlib winsys
76 struct xmesa_pipe_winsys
78 struct pipe_winsys base
;
84 static INLINE
struct xm_buffer
*
85 xm_buffer( struct pipe_buffer
*buf
)
87 return (struct xm_buffer
*)buf
;
91 /* Most callbacks map direcly onto dri_bufmgr operations:
94 xm_buffer_map(struct pipe_winsys
*pws
, struct pipe_buffer
*buf
,
97 struct xm_buffer
*xm_buf
= xm_buffer(buf
);
98 xm_buf
->mapped
= xm_buf
->data
;
99 return xm_buf
->mapped
;
103 xm_buffer_unmap(struct pipe_winsys
*pws
, struct pipe_buffer
*buf
)
105 struct xm_buffer
*xm_buf
= xm_buffer(buf
);
106 xm_buf
->mapped
= NULL
;
110 xm_buffer_destroy(struct pipe_winsys
*pws
,
111 struct pipe_buffer
*buf
)
113 struct xm_buffer
*oldBuf
= xm_buffer(buf
);
117 if (!oldBuf
->userBuffer
) {
118 align_free(oldBuf
->data
);
130 * For Cell. Basically, rearrange the pixels/quads from this layout:
143 twiddle_tile(const uint
*tileIn
, uint
*tileOut
)
147 for (y
= 0; y
< TILE_SIZE
; y
+=2) {
148 for (x
= 0; x
< TILE_SIZE
; x
+=2) {
149 int k
= 4 * (y
/2 * TILE_SIZE
/2 + x
/2);
150 tileOut
[y
* TILE_SIZE
+ (x
+ 0)] = tileIn
[k
];
151 tileOut
[y
* TILE_SIZE
+ (x
+ 1)] = tileIn
[k
+1];
152 tileOut
[(y
+ 1) * TILE_SIZE
+ (x
+ 0)] = tileIn
[k
+2];
153 tileOut
[(y
+ 1) * TILE_SIZE
+ (x
+ 1)] = tileIn
[k
+3];
161 * Display a surface that's in a tiled configuration. That is, all the
162 * pixels for a TILE_SIZExTILE_SIZE block are contiguous in memory.
165 xlib_cell_display_surface(struct xmesa_buffer
*b
, struct pipe_surface
*surf
)
168 struct xm_buffer
*xm_buf
= xm_buffer(
169 cell_texture(surf
->texture
)->buffer
);
170 const uint tilesPerRow
= (surf
->width
+ TILE_SIZE
- 1) / TILE_SIZE
;
173 ximage
= b
->tempImage
;
175 /* check that the XImage has been previously initialized */
176 assert(ximage
->format
);
177 assert(ximage
->bitmap_unit
);
179 /* update XImage's fields */
180 ximage
->width
= TILE_SIZE
;
181 ximage
->height
= TILE_SIZE
;
182 ximage
->bytes_per_line
= TILE_SIZE
* 4;
184 for (y
= 0; y
< surf
->height
; y
+= TILE_SIZE
) {
185 for (x
= 0; x
< surf
->width
; x
+= TILE_SIZE
) {
186 uint tmpTile
[TILE_SIZE
* TILE_SIZE
];
187 int tx
= x
/ TILE_SIZE
;
188 int ty
= y
/ TILE_SIZE
;
189 int offset
= ty
* tilesPerRow
+ tx
;
193 if (y
+ h
> surf
->height
)
194 h
= surf
->height
- y
;
195 if (x
+ w
> surf
->width
)
198 /* offset in pixels */
199 offset
*= TILE_SIZE
* TILE_SIZE
;
201 /* twiddle from ximage buffer to temp tile */
202 twiddle_tile((uint
*) xm_buf
->data
+ offset
, tmpTile
);
203 /* display temp tile data */
204 ximage
->data
= (char *) tmpTile
;
205 XPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
206 ximage
, 0, 0, x
, y
, w
, h
);
216 xm_flush_frontbuffer(struct pipe_winsys
*pws
,
217 struct pipe_surface
*surf
,
218 void *context_private
)
221 * The front color buffer is actually just another XImage buffer.
222 * This function copies that XImage to the actual X Window.
224 XMesaContext xmctx
= (XMesaContext
) context_private
;
225 xlib_cell_display_surface(xmctx
->xm_buffer
, surf
);
231 xm_get_name(struct pipe_winsys
*pws
)
237 static struct pipe_buffer
*
238 xm_buffer_create(struct pipe_winsys
*pws
,
243 struct xm_buffer
*buffer
= CALLOC_STRUCT(xm_buffer
);
245 buffer
->base
.refcount
= 1;
246 buffer
->base
.alignment
= alignment
;
247 buffer
->base
.usage
= usage
;
248 buffer
->base
.size
= size
;
251 if (buffer
->data
== NULL
) {
254 /* align to 16-byte multiple for Cell */
255 buffer
->data
= align_malloc(size
, max(alignment
, 16));
258 return &buffer
->base
;
263 * Create buffer which wraps user-space data.
265 static struct pipe_buffer
*
266 xm_user_buffer_create(struct pipe_winsys
*pws
, void *ptr
, unsigned bytes
)
268 struct xm_buffer
*buffer
= CALLOC_STRUCT(xm_buffer
);
269 buffer
->base
.refcount
= 1;
270 buffer
->base
.size
= bytes
;
271 buffer
->userBuffer
= TRUE
;
275 return &buffer
->base
;
281 * Round n up to next multiple.
283 static INLINE
unsigned
284 round_up(unsigned n
, unsigned multiple
)
286 return (n
+ multiple
- 1) & ~(multiple
- 1);
289 static struct pipe_buffer
*
290 xm_surface_buffer_create(struct pipe_winsys
*winsys
,
291 unsigned width
, unsigned height
,
292 enum pipe_format format
,
296 const unsigned alignment
= 64;
297 struct pipe_format_block block
;
298 unsigned nblocksx
, nblocksy
;
300 pf_get_block(format
, &block
);
301 nblocksx
= pf_get_nblocksx(&block
, width
);
302 nblocksy
= pf_get_nblocksy(&block
, height
);
303 *stride
= round_up(nblocksx
* block
.size
, alignment
);
305 return winsys
->buffer_create(winsys
, alignment
,
307 /* XXX a bit of a hack */
308 *stride
* round_up(nblocksy
, TILE_SIZE
));
313 * Fence functions - basically nothing to do, as we don't create any actual
318 xm_fence_reference(struct pipe_winsys
*sws
, struct pipe_fence_handle
**ptr
,
319 struct pipe_fence_handle
*fence
)
325 xm_fence_signalled(struct pipe_winsys
*sws
, struct pipe_fence_handle
*fence
,
333 xm_fence_finish(struct pipe_winsys
*sws
, struct pipe_fence_handle
*fence
,
341 static struct pipe_winsys
*
342 xlib_create_cell_winsys( void )
344 static struct xmesa_pipe_winsys
*ws
= NULL
;
347 ws
= CALLOC_STRUCT(xmesa_pipe_winsys
);
349 /* Fill in this struct with callbacks that pipe will need to
350 * communicate with the window system, buffer manager, etc.
352 ws
->base
.buffer_create
= xm_buffer_create
;
353 ws
->base
.user_buffer_create
= xm_user_buffer_create
;
354 ws
->base
.buffer_map
= xm_buffer_map
;
355 ws
->base
.buffer_unmap
= xm_buffer_unmap
;
356 ws
->base
.buffer_destroy
= xm_buffer_destroy
;
358 ws
->base
.surface_buffer_create
= xm_surface_buffer_create
;
360 ws
->base
.fence_reference
= xm_fence_reference
;
361 ws
->base
.fence_signalled
= xm_fence_signalled
;
362 ws
->base
.fence_finish
= xm_fence_finish
;
364 ws
->base
.flush_frontbuffer
= xm_flush_frontbuffer
;
365 ws
->base
.get_name
= xm_get_name
;
372 static struct pipe_screen
*
373 xlib_create_cell_screen( struct pipe_winsys
*pws
)
375 struct pipe_winsys
*winsys
;
376 struct pipe_screen
*screen
;
378 winsys
= xlib_create_cell_winsys();
382 screen
= cell_create_screen(winsys
);
390 winsys
->destroy( winsys
);
396 static struct pipe_context
*
397 xlib_create_cell_context( struct pipe_screen
*screen
,
400 struct pipe_context
*pipe
;
403 /* This takes a cell_winsys pointer, but probably that should be
404 * created and stored at screen creation, not context creation.
406 * The actual cell_winsys value isn't used for anything, so just
407 * passing NULL for now.
409 pipe
= cell_create_context( screen
, NULL
);
421 struct xm_driver xlib_cell_driver
=
423 .create_pipe_screen
= xlib_create_cell_screen
,
424 .create_pipe_context
= xlib_create_cell_context
,
425 .display_surface
= xlib_cell_display_surface
,
430 struct xm_driver xlib_cell_driver
=
432 .create_pipe_screen
= NULL
,
433 .create_pipe_context
= NULL
,
434 .display_surface
= NULL
,