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