Merge commit 'origin/master' into gallium-map-range
[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 if (xmctx)
226 xlib_cell_display_surface(xmctx->xm_buffer, surf);
227 }
228
229
230
231 static const char *
232 xm_get_name(struct pipe_winsys *pws)
233 {
234 return "Xlib/Cell";
235 }
236
237
238 static struct pipe_buffer *
239 xm_buffer_create(struct pipe_winsys *pws,
240 unsigned alignment,
241 unsigned usage,
242 unsigned size)
243 {
244 struct xm_buffer *buffer = CALLOC_STRUCT(xm_buffer);
245
246 buffer->base.refcount = 1;
247 buffer->base.alignment = alignment;
248 buffer->base.usage = usage;
249 buffer->base.size = size;
250
251
252 if (buffer->data == NULL) {
253 buffer->shm = 0;
254
255 /* align to 16-byte multiple for Cell */
256 buffer->data = align_malloc(size, max(alignment, 16));
257 }
258
259 return &buffer->base;
260 }
261
262
263 /**
264 * Create buffer which wraps user-space data.
265 */
266 static struct pipe_buffer *
267 xm_user_buffer_create(struct pipe_winsys *pws, void *ptr, unsigned bytes)
268 {
269 struct xm_buffer *buffer = CALLOC_STRUCT(xm_buffer);
270 buffer->base.refcount = 1;
271 buffer->base.size = bytes;
272 buffer->userBuffer = TRUE;
273 buffer->data = ptr;
274 buffer->shm = 0;
275
276 return &buffer->base;
277 }
278
279
280
281 /**
282 * Round n up to next multiple.
283 */
284 static INLINE unsigned
285 round_up(unsigned n, unsigned multiple)
286 {
287 return (n + multiple - 1) & ~(multiple - 1);
288 }
289
290 static struct pipe_buffer *
291 xm_surface_buffer_create(struct pipe_winsys *winsys,
292 unsigned width, unsigned height,
293 enum pipe_format format,
294 unsigned usage,
295 unsigned *stride)
296 {
297 const unsigned alignment = 64;
298 struct pipe_format_block block;
299 unsigned nblocksx, nblocksy;
300
301 pf_get_block(format, &block);
302 nblocksx = pf_get_nblocksx(&block, width);
303 nblocksy = pf_get_nblocksy(&block, height);
304 *stride = round_up(nblocksx * block.size, alignment);
305
306 return winsys->buffer_create(winsys, alignment,
307 usage,
308 /* XXX a bit of a hack */
309 *stride * round_up(nblocksy, TILE_SIZE));
310 }
311
312
313 /*
314 * Fence functions - basically nothing to do, as we don't create any actual
315 * fence objects.
316 */
317
318 static void
319 xm_fence_reference(struct pipe_winsys *sws, struct pipe_fence_handle **ptr,
320 struct pipe_fence_handle *fence)
321 {
322 }
323
324
325 static int
326 xm_fence_signalled(struct pipe_winsys *sws, struct pipe_fence_handle *fence,
327 unsigned flag)
328 {
329 return 0;
330 }
331
332
333 static int
334 xm_fence_finish(struct pipe_winsys *sws, struct pipe_fence_handle *fence,
335 unsigned flag)
336 {
337 return 0;
338 }
339
340
341
342 static struct pipe_winsys *
343 xlib_create_cell_winsys( void )
344 {
345 static struct xmesa_pipe_winsys *ws = NULL;
346
347 if (!ws) {
348 ws = CALLOC_STRUCT(xmesa_pipe_winsys);
349
350 /* Fill in this struct with callbacks that pipe will need to
351 * communicate with the window system, buffer manager, etc.
352 */
353 ws->base.buffer_create = xm_buffer_create;
354 ws->base.user_buffer_create = xm_user_buffer_create;
355 ws->base.buffer_map = xm_buffer_map;
356 ws->base.buffer_unmap = xm_buffer_unmap;
357 ws->base.buffer_destroy = xm_buffer_destroy;
358
359 ws->base.surface_buffer_create = xm_surface_buffer_create;
360
361 ws->base.fence_reference = xm_fence_reference;
362 ws->base.fence_signalled = xm_fence_signalled;
363 ws->base.fence_finish = xm_fence_finish;
364
365 ws->base.flush_frontbuffer = xm_flush_frontbuffer;
366 ws->base.get_name = xm_get_name;
367 }
368
369 return &ws->base;
370 }
371
372
373 static struct pipe_screen *
374 xlib_create_cell_screen( struct pipe_winsys *pws )
375 {
376 struct pipe_winsys *winsys;
377 struct pipe_screen *screen;
378
379 winsys = xlib_create_cell_winsys();
380 if (winsys == NULL)
381 return NULL;
382
383 screen = cell_create_screen(winsys);
384 if (screen == NULL)
385 goto fail;
386
387 return screen;
388
389 fail:
390 if (winsys)
391 winsys->destroy( winsys );
392
393 return NULL;
394 }
395
396
397 static struct pipe_context *
398 xlib_create_cell_context( struct pipe_screen *screen,
399 void *priv )
400 {
401 struct pipe_context *pipe;
402
403
404 /* This takes a cell_winsys pointer, but probably that should be
405 * created and stored at screen creation, not context creation.
406 *
407 * The actual cell_winsys value isn't used for anything, so just
408 * passing NULL for now.
409 */
410 pipe = cell_create_context( screen, NULL);
411 if (pipe == NULL)
412 goto fail;
413
414 pipe->priv = priv;
415
416 return pipe;
417
418 fail:
419 return NULL;
420 }
421
422 struct xm_driver xlib_cell_driver =
423 {
424 .create_pipe_screen = xlib_create_cell_screen,
425 .create_pipe_context = xlib_create_cell_context,
426 .display_surface = xlib_cell_display_surface,
427 };
428
429 #else
430
431 struct xm_driver xlib_cell_driver =
432 {
433 .create_pipe_screen = NULL,
434 .create_pipe_context = NULL,
435 .display_surface = NULL,
436 };
437
438 #endif