Merge commit 'origin/gallium-master-merge'
[mesa.git] / src / gallium / winsys / xlib / xlib_cell.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Bismarck, ND., USA
4 * All Rights Reserved.
5 *
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:
13 *
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.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 *
27 **************************************************************************/
28
29 /*
30 * Authors:
31 * Keith Whitwell
32 * Brian Paul
33 */
34
35 #include "xlib.h"
36
37 #ifdef GALLIUM_CELL
38
39 #include "xm_api.h"
40
41 #undef ASSERT
42 #undef Elements
43
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"
50
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"
55
56
57 /**
58 * Subclass of pipe_buffer for Xlib winsys.
59 * Low-level OS/window system memory buffer
60 */
61 struct xm_buffer
62 {
63 struct pipe_buffer base;
64 boolean userBuffer; /** Is this a user-space buffer? */
65 void *data;
66 void *mapped;
67
68 XImage *tempImage;
69 int shm;
70 };
71
72
73 /**
74 * Subclass of pipe_winsys for Xlib winsys
75 */
76 struct xmesa_pipe_winsys
77 {
78 struct pipe_winsys base;
79 };
80
81
82
83 /** Cast wrapper */
84 static INLINE struct xm_buffer *
85 xm_buffer( struct pipe_buffer *buf )
86 {
87 return (struct xm_buffer *)buf;
88 }
89
90
91 /* Most callbacks map direcly onto dri_bufmgr operations:
92 */
93 static void *
94 xm_buffer_map(struct pipe_winsys *pws, struct pipe_buffer *buf,
95 unsigned flags)
96 {
97 struct xm_buffer *xm_buf = xm_buffer(buf);
98 xm_buf->mapped = xm_buf->data;
99 return xm_buf->mapped;
100 }
101
102 static void
103 xm_buffer_unmap(struct pipe_winsys *pws, struct pipe_buffer *buf)
104 {
105 struct xm_buffer *xm_buf = xm_buffer(buf);
106 xm_buf->mapped = NULL;
107 }
108
109 static void
110 xm_buffer_destroy(struct pipe_winsys *pws,
111 struct pipe_buffer *buf)
112 {
113 struct xm_buffer *oldBuf = xm_buffer(buf);
114
115 if (oldBuf->data) {
116 {
117 if (!oldBuf->userBuffer) {
118 align_free(oldBuf->data);
119 }
120 }
121
122 oldBuf->data = NULL;
123 }
124
125 free(oldBuf);
126 }
127
128
129 /**
130 * For Cell. Basically, rearrange the pixels/quads from this layout:
131 * +--+--+--+--+
132 * |p0|p1|p2|p3|....
133 * +--+--+--+--+
134 *
135 * to this layout:
136 * +--+--+
137 * |p0|p1|....
138 * +--+--+
139 * |p2|p3|
140 * +--+--+
141 */
142 static void
143 twiddle_tile(const uint *tileIn, uint *tileOut)
144 {
145 int y, x;
146
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];
154 }
155 }
156 }
157
158
159
160 /**
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.
163 */
164 static void
165 xlib_cell_display_surface(struct xmesa_buffer *b, struct pipe_surface *surf)
166 {
167 XImage *ximage;
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;
171 uint x, y;
172
173 ximage = b->tempImage;
174
175 /* check that the XImage has been previously initialized */
176 assert(ximage->format);
177 assert(ximage->bitmap_unit);
178
179 /* update XImage's fields */
180 ximage->width = TILE_SIZE;
181 ximage->height = TILE_SIZE;
182 ximage->bytes_per_line = TILE_SIZE * 4;
183
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;
190 int w = TILE_SIZE;
191 int h = TILE_SIZE;
192
193 if (y + h > surf->height)
194 h = surf->height - y;
195 if (x + w > surf->width)
196 w = surf->width - x;
197
198 /* offset in pixels */
199 offset *= TILE_SIZE * TILE_SIZE;
200
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);
207 }
208 }
209 }
210
211
212
213
214
215 static void
216 xm_flush_frontbuffer(struct pipe_winsys *pws,
217 struct pipe_surface *surf,
218 void *context_private)
219 {
220 /*
221 * The front color buffer is actually just another XImage buffer.
222 * This function copies that XImage to the actual X Window.
223 */
224 XMesaContext xmctx = (XMesaContext) context_private;
225 xlib_cell_display_surface(xmctx->xm_buffer, surf);
226 }
227
228
229
230 static const char *
231 xm_get_name(struct pipe_winsys *pws)
232 {
233 return "Xlib/Cell";
234 }
235
236
237 static struct pipe_buffer *
238 xm_buffer_create(struct pipe_winsys *pws,
239 unsigned alignment,
240 unsigned usage,
241 unsigned size)
242 {
243 struct xm_buffer *buffer = CALLOC_STRUCT(xm_buffer);
244
245 buffer->base.refcount = 1;
246 buffer->base.alignment = alignment;
247 buffer->base.usage = usage;
248 buffer->base.size = size;
249
250
251 if (buffer->data == NULL) {
252 buffer->shm = 0;
253
254 /* align to 16-byte multiple for Cell */
255 buffer->data = align_malloc(size, max(alignment, 16));
256 }
257
258 return &buffer->base;
259 }
260
261
262 /**
263 * Create buffer which wraps user-space data.
264 */
265 static struct pipe_buffer *
266 xm_user_buffer_create(struct pipe_winsys *pws, void *ptr, unsigned bytes)
267 {
268 struct xm_buffer *buffer = CALLOC_STRUCT(xm_buffer);
269 buffer->base.refcount = 1;
270 buffer->base.size = bytes;
271 buffer->userBuffer = TRUE;
272 buffer->data = ptr;
273 buffer->shm = 0;
274
275 return &buffer->base;
276 }
277
278
279
280 /**
281 * Round n up to next multiple.
282 */
283 static INLINE unsigned
284 round_up(unsigned n, unsigned multiple)
285 {
286 return (n + multiple - 1) & ~(multiple - 1);
287 }
288
289 static struct pipe_buffer *
290 xm_surface_buffer_create(struct pipe_winsys *winsys,
291 unsigned width, unsigned height,
292 enum pipe_format format,
293 unsigned usage,
294 unsigned *stride)
295 {
296 const unsigned alignment = 64;
297 struct pipe_format_block block;
298 unsigned nblocksx, nblocksy;
299
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);
304
305 return winsys->buffer_create(winsys, alignment,
306 usage,
307 /* XXX a bit of a hack */
308 *stride * round_up(nblocksy, TILE_SIZE));
309 }
310
311
312 /*
313 * Fence functions - basically nothing to do, as we don't create any actual
314 * fence objects.
315 */
316
317 static void
318 xm_fence_reference(struct pipe_winsys *sws, struct pipe_fence_handle **ptr,
319 struct pipe_fence_handle *fence)
320 {
321 }
322
323
324 static int
325 xm_fence_signalled(struct pipe_winsys *sws, struct pipe_fence_handle *fence,
326 unsigned flag)
327 {
328 return 0;
329 }
330
331
332 static int
333 xm_fence_finish(struct pipe_winsys *sws, struct pipe_fence_handle *fence,
334 unsigned flag)
335 {
336 return 0;
337 }
338
339
340
341 static struct pipe_winsys *
342 xlib_create_cell_winsys( void )
343 {
344 static struct xmesa_pipe_winsys *ws = NULL;
345
346 if (!ws) {
347 ws = CALLOC_STRUCT(xmesa_pipe_winsys);
348
349 /* Fill in this struct with callbacks that pipe will need to
350 * communicate with the window system, buffer manager, etc.
351 */
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;
357
358 ws->base.surface_buffer_create = xm_surface_buffer_create;
359
360 ws->base.fence_reference = xm_fence_reference;
361 ws->base.fence_signalled = xm_fence_signalled;
362 ws->base.fence_finish = xm_fence_finish;
363
364 ws->base.flush_frontbuffer = xm_flush_frontbuffer;
365 ws->base.get_name = xm_get_name;
366 }
367
368 return &ws->base;
369 }
370
371
372 static struct pipe_screen *
373 xlib_create_cell_screen( struct pipe_winsys *pws )
374 {
375 struct pipe_winsys *winsys;
376 struct pipe_screen *screen;
377
378 winsys = xlib_create_cell_winsys();
379 if (winsys == NULL)
380 return NULL;
381
382 screen = cell_create_screen(winsys);
383 if (screen == NULL)
384 goto fail;
385
386 return screen;
387
388 fail:
389 if (winsys)
390 winsys->destroy( winsys );
391
392 return NULL;
393 }
394
395
396 static struct pipe_context *
397 xlib_create_cell_context( struct pipe_screen *screen,
398 void *priv )
399 {
400 struct pipe_context *pipe;
401
402
403 /* This takes a cell_winsys pointer, but probably that should be
404 * created and stored at screen creation, not context creation.
405 *
406 * The actual cell_winsys value isn't used for anything, so just
407 * passing NULL for now.
408 */
409 pipe = cell_create_context( screen, NULL);
410 if (pipe == NULL)
411 goto fail;
412
413 pipe->priv = priv;
414
415 return pipe;
416
417 fail:
418 return NULL;
419 }
420
421 struct xm_driver xlib_cell_driver =
422 {
423 .create_pipe_screen = xlib_create_cell_screen,
424 .create_pipe_context = xlib_create_cell_context,
425 .display_surface = xlib_cell_display_surface,
426 };
427
428 #else
429
430 struct xm_driver xlib_cell_driver =
431 {
432 .create_pipe_screen = NULL,
433 .create_pipe_context = NULL,
434 .display_surface = NULL,
435 };
436
437 #endif