Merge commit 'origin/gallium-0.2' into gallium-xlib-rework
[mesa.git] / src / gallium / winsys / xlib / xlib_softpipe.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
36 #include "xm_api.h"
37
38 #undef ASSERT
39 #undef Elements
40
41 #include "pipe/p_winsys.h"
42 #include "pipe/p_format.h"
43 #include "pipe/p_context.h"
44 #include "pipe/p_inlines.h"
45 #include "util/u_math.h"
46 #include "util/u_memory.h"
47 #include "softpipe/sp_winsys.h"
48
49 #include "xlib.h"
50
51 /**
52 * Subclass of pipe_buffer for Xlib winsys.
53 * Low-level OS/window system memory buffer
54 */
55 struct xm_buffer
56 {
57 struct pipe_buffer base;
58 boolean userBuffer; /** Is this a user-space buffer? */
59 void *data;
60 void *mapped;
61
62 XImage *tempImage;
63 int shm;
64 XShmSegmentInfo shminfo;
65 };
66
67
68 /**
69 * Subclass of pipe_winsys for Xlib winsys
70 */
71 struct xmesa_pipe_winsys
72 {
73 struct pipe_winsys base;
74 /* struct xmesa_visual *xm_visual; */
75 int shm;
76 };
77
78
79
80 /** Cast wrapper */
81 static INLINE struct xm_buffer *
82 xm_buffer( struct pipe_buffer *buf )
83 {
84 return (struct xm_buffer *)buf;
85 }
86
87
88 /**
89 * X Shared Memory Image extension code
90 */
91 #define XSHM_ENABLED(b) ((b)->shm)
92
93 static volatile int mesaXErrorFlag = 0;
94
95 /**
96 * Catches potential Xlib errors.
97 */
98 static int
99 mesaHandleXError(Display *dpy, XErrorEvent *event)
100 {
101 (void) dpy;
102 (void) event;
103 mesaXErrorFlag = 1;
104 return 0;
105 }
106
107
108 static GLboolean alloc_shm(struct xm_buffer *buf, unsigned size)
109 {
110 XShmSegmentInfo *const shminfo = & buf->shminfo;
111
112 shminfo->shmid = shmget(IPC_PRIVATE, size, IPC_CREAT|0777);
113 if (shminfo->shmid < 0) {
114 return GL_FALSE;
115 }
116
117 shminfo->shmaddr = (char *) shmat(shminfo->shmid, 0, 0);
118 if (shminfo->shmaddr == (char *) -1) {
119 shmctl(shminfo->shmid, IPC_RMID, 0);
120 return GL_FALSE;
121 }
122
123 shminfo->readOnly = False;
124 return GL_TRUE;
125 }
126
127
128 /**
129 * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
130 */
131 static void
132 alloc_shm_ximage(struct xm_buffer *b, struct xmesa_buffer *xmb,
133 unsigned width, unsigned height)
134 {
135 /*
136 * We have to do a _lot_ of error checking here to be sure we can
137 * really use the XSHM extension. It seems different servers trigger
138 * errors at different points if the extension won't work. Therefore
139 * we have to be very careful...
140 */
141 int (*old_handler)(Display *, XErrorEvent *);
142
143 b->tempImage = XShmCreateImage(xmb->xm_visual->display,
144 xmb->xm_visual->visinfo->visual,
145 xmb->xm_visual->visinfo->depth,
146 ZPixmap,
147 NULL,
148 &b->shminfo,
149 width, height);
150 if (b->tempImage == NULL) {
151 b->shm = 0;
152 return;
153 }
154
155
156 mesaXErrorFlag = 0;
157 old_handler = XSetErrorHandler(mesaHandleXError);
158 /* This may trigger the X protocol error we're ready to catch: */
159 XShmAttach(xmb->xm_visual->display, &b->shminfo);
160 XSync(xmb->xm_visual->display, False);
161
162 if (mesaXErrorFlag) {
163 /* we are on a remote display, this error is normal, don't print it */
164 XFlush(xmb->xm_visual->display);
165 mesaXErrorFlag = 0;
166 XDestroyImage(b->tempImage);
167 b->tempImage = NULL;
168 b->shm = 0;
169 (void) XSetErrorHandler(old_handler);
170 return;
171 }
172 }
173
174
175
176 /* Most callbacks map direcly onto dri_bufmgr operations:
177 */
178 static void *
179 xm_buffer_map(struct pipe_winsys *pws, struct pipe_buffer *buf,
180 unsigned flags)
181 {
182 struct xm_buffer *xm_buf = xm_buffer(buf);
183 xm_buf->mapped = xm_buf->data;
184 return xm_buf->mapped;
185 }
186
187 static void
188 xm_buffer_unmap(struct pipe_winsys *pws, struct pipe_buffer *buf)
189 {
190 struct xm_buffer *xm_buf = xm_buffer(buf);
191 xm_buf->mapped = NULL;
192 }
193
194 static void
195 xm_buffer_destroy(struct pipe_winsys *pws,
196 struct pipe_buffer *buf)
197 {
198 struct xm_buffer *oldBuf = xm_buffer(buf);
199
200 if (oldBuf->data) {
201 if (oldBuf->shminfo.shmid >= 0) {
202 shmdt(oldBuf->shminfo.shmaddr);
203 shmctl(oldBuf->shminfo.shmid, IPC_RMID, 0);
204
205 oldBuf->shminfo.shmid = -1;
206 oldBuf->shminfo.shmaddr = (char *) -1;
207 }
208 else
209 {
210 if (!oldBuf->userBuffer) {
211 align_free(oldBuf->data);
212 }
213 }
214
215 oldBuf->data = NULL;
216 }
217
218 free(oldBuf);
219 }
220
221
222
223 /**
224 * Display/copy the image in the surface into the X window specified
225 * by the XMesaBuffer.
226 */
227 static void
228 xlib_softpipe_display_surface(struct xmesa_buffer *b,
229 struct pipe_surface *surf)
230 {
231 XImage *ximage;
232 struct xm_buffer *xm_buf = xm_buffer(surf->buffer);
233 static boolean no_swap = 0;
234 static boolean firsttime = 1;
235
236 if (firsttime) {
237 no_swap = getenv("SP_NO_RAST") != NULL;
238 firsttime = 0;
239 }
240
241 if (no_swap)
242 return;
243
244 if (XSHM_ENABLED(xm_buf) && (xm_buf->tempImage == NULL)) {
245 assert(surf->block.width == 1);
246 assert(surf->block.height == 1);
247 alloc_shm_ximage(xm_buf, b, surf->stride/surf->block.size, surf->height);
248 }
249
250 ximage = (XSHM_ENABLED(xm_buf)) ? xm_buf->tempImage : b->tempImage;
251 ximage->data = xm_buf->data;
252
253 /* display image in Window */
254 if (XSHM_ENABLED(xm_buf)) {
255 XShmPutImage(b->xm_visual->display, b->drawable, b->gc,
256 ximage, 0, 0, 0, 0, surf->width, surf->height, False);
257 } else {
258 /* check that the XImage has been previously initialized */
259 assert(ximage->format);
260 assert(ximage->bitmap_unit);
261
262 /* update XImage's fields */
263 ximage->width = surf->width;
264 ximage->height = surf->height;
265 ximage->bytes_per_line = surf->stride;
266
267 XPutImage(b->xm_visual->display, b->drawable, b->gc,
268 ximage, 0, 0, 0, 0, surf->width, surf->height);
269 }
270 }
271
272
273 static void
274 xm_flush_frontbuffer(struct pipe_winsys *pws,
275 struct pipe_surface *surf,
276 void *context_private)
277 {
278 /*
279 * The front color buffer is actually just another XImage buffer.
280 * This function copies that XImage to the actual X Window.
281 */
282 XMesaContext xmctx = (XMesaContext) context_private;
283 xlib_softpipe_display_surface(xmctx->xm_buffer, surf);
284 }
285
286
287
288 static const char *
289 xm_get_name(struct pipe_winsys *pws)
290 {
291 return "Xlib";
292 }
293
294
295 static struct pipe_buffer *
296 xm_buffer_create(struct pipe_winsys *pws,
297 unsigned alignment,
298 unsigned usage,
299 unsigned size)
300 {
301 struct xm_buffer *buffer = CALLOC_STRUCT(xm_buffer);
302 struct xmesa_pipe_winsys *xpws = (struct xmesa_pipe_winsys *) pws;
303
304 buffer->base.refcount = 1;
305 buffer->base.alignment = alignment;
306 buffer->base.usage = usage;
307 buffer->base.size = size;
308 buffer->shminfo.shmid = -1;
309 buffer->shminfo.shmaddr = (char *) -1;
310
311 if (xpws->shm && (usage & PIPE_BUFFER_USAGE_PIXEL) != 0) {
312 buffer->shm = xpws->shm;
313
314 if (alloc_shm(buffer, size)) {
315 buffer->data = buffer->shminfo.shmaddr;
316 }
317 }
318
319 if (buffer->data == NULL) {
320 buffer->shm = 0;
321
322 /* align to 16-byte multiple for Cell */
323 buffer->data = align_malloc(size, max(alignment, 16));
324 }
325
326 return &buffer->base;
327 }
328
329
330 /**
331 * Create buffer which wraps user-space data.
332 */
333 static struct pipe_buffer *
334 xm_user_buffer_create(struct pipe_winsys *pws, void *ptr, unsigned bytes)
335 {
336 struct xm_buffer *buffer = CALLOC_STRUCT(xm_buffer);
337 buffer->base.refcount = 1;
338 buffer->base.size = bytes;
339 buffer->userBuffer = TRUE;
340 buffer->data = ptr;
341 buffer->shm = 0;
342
343 return &buffer->base;
344 }
345
346
347
348 /**
349 * Round n up to next multiple.
350 */
351 static INLINE unsigned
352 round_up(unsigned n, unsigned multiple)
353 {
354 return (n + multiple - 1) & ~(multiple - 1);
355 }
356
357 static int
358 xm_surface_alloc_storage(struct pipe_winsys *winsys,
359 struct pipe_surface *surf,
360 unsigned width, unsigned height,
361 enum pipe_format format,
362 unsigned flags,
363 unsigned tex_usage)
364 {
365 const unsigned alignment = 64;
366
367 surf->width = width;
368 surf->height = height;
369 surf->format = format;
370 pf_get_block(format, &surf->block);
371 surf->nblocksx = pf_get_nblocksx(&surf->block, width);
372 surf->nblocksy = pf_get_nblocksy(&surf->block, height);
373 surf->stride = round_up(surf->nblocksx * surf->block.size, alignment);
374 surf->usage = flags;
375
376 assert(!surf->buffer);
377 surf->buffer = winsys->buffer_create(winsys, alignment,
378 PIPE_BUFFER_USAGE_PIXEL,
379 surf->stride * surf->nblocksy);
380
381 if(!surf->buffer)
382 return -1;
383
384 return 0;
385 }
386
387
388 /**
389 * Called via winsys->surface_alloc() to create new surfaces.
390 */
391 static struct pipe_surface *
392 xm_surface_alloc(struct pipe_winsys *ws)
393 {
394 struct pipe_surface *surface = CALLOC_STRUCT(pipe_surface);
395
396 assert(ws);
397
398 surface->refcount = 1;
399 surface->winsys = ws;
400
401 return surface;
402 }
403
404
405
406 static void
407 xm_surface_release(struct pipe_winsys *winsys, struct pipe_surface **s)
408 {
409 struct pipe_surface *surf = *s;
410 assert(!surf->texture);
411 surf->refcount--;
412 if (surf->refcount == 0) {
413 if (surf->buffer)
414 winsys_buffer_reference(winsys, &surf->buffer, NULL);
415 free(surf);
416 }
417 *s = NULL;
418 }
419
420
421 /*
422 * Fence functions - basically nothing to do, as we don't create any actual
423 * fence objects.
424 */
425
426 static void
427 xm_fence_reference(struct pipe_winsys *sws, struct pipe_fence_handle **ptr,
428 struct pipe_fence_handle *fence)
429 {
430 }
431
432
433 static int
434 xm_fence_signalled(struct pipe_winsys *sws, struct pipe_fence_handle *fence,
435 unsigned flag)
436 {
437 return 0;
438 }
439
440
441 static int
442 xm_fence_finish(struct pipe_winsys *sws, struct pipe_fence_handle *fence,
443 unsigned flag)
444 {
445 return 0;
446 }
447
448
449
450 static struct pipe_winsys *
451 xlib_create_softpipe_winsys( void )
452 {
453 static struct xmesa_pipe_winsys *ws = NULL;
454
455 if (!ws) {
456 ws = CALLOC_STRUCT(xmesa_pipe_winsys);
457
458 /* Fill in this struct with callbacks that pipe will need to
459 * communicate with the window system, buffer manager, etc.
460 */
461 ws->base.buffer_create = xm_buffer_create;
462 ws->base.user_buffer_create = xm_user_buffer_create;
463 ws->base.buffer_map = xm_buffer_map;
464 ws->base.buffer_unmap = xm_buffer_unmap;
465 ws->base.buffer_destroy = xm_buffer_destroy;
466
467 ws->base.surface_alloc = xm_surface_alloc;
468 ws->base.surface_alloc_storage = xm_surface_alloc_storage;
469 ws->base.surface_release = xm_surface_release;
470
471 ws->base.fence_reference = xm_fence_reference;
472 ws->base.fence_signalled = xm_fence_signalled;
473 ws->base.fence_finish = xm_fence_finish;
474
475 ws->base.flush_frontbuffer = xm_flush_frontbuffer;
476 ws->base.get_name = xm_get_name;
477 }
478
479 return &ws->base;
480 }
481
482
483 static struct pipe_screen *
484 xlib_create_softpipe_screen( void )
485 {
486 struct pipe_winsys *winsys;
487 struct pipe_screen *screen;
488
489 winsys = xlib_create_softpipe_winsys();
490 if (winsys == NULL)
491 return NULL;
492
493 screen = softpipe_create_screen(winsys);
494 if (screen == NULL)
495 goto fail;
496
497 return screen;
498
499 fail:
500 if (winsys)
501 winsys->destroy( winsys );
502
503 return NULL;
504 }
505
506
507 static struct pipe_context *
508 xlib_create_softpipe_context( struct pipe_screen *screen,
509 void *context_private )
510 {
511 struct pipe_context *pipe;
512
513 pipe = softpipe_create(screen, screen->winsys, NULL);
514 if (pipe == NULL)
515 goto fail;
516
517 pipe->priv = context_private;
518 return pipe;
519
520 fail:
521 /* Free stuff here */
522 return NULL;
523 }
524
525 struct xm_driver xlib_softpipe_driver =
526 {
527 .create_pipe_screen = xlib_create_softpipe_screen,
528 .create_pipe_context = xlib_create_softpipe_context,
529 .display_surface = xlib_softpipe_display_surface
530 };
531
532
533