gallium: introduce target directory
[mesa.git] / src / gallium / winsys / xlib / xlib_sw_winsys.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
37
38 #undef ASSERT
39 #undef Elements
40
41 #include "pipe/p_format.h"
42 #include "pipe/p_context.h"
43 #include "util/u_inlines.h"
44 #include "util/u_format.h"
45 #include "util/u_math.h"
46 #include "util/u_memory.h"
47
48 #include "state_tracker/xlib_sw_winsys.h"
49
50 #include "xlib.h"
51
52 #include <X11/Xlib.h>
53 #include <X11/Xlibint.h>
54 #include <X11/Xutil.h>
55 #include <sys/ipc.h>
56 #include <sys/shm.h>
57 #include <X11/extensions/XShm.h>
58
59 /**
60 * Subclass of pipe_buffer for Xlib winsys.
61 * Low-level OS/window system memory buffer
62 */
63 struct xm_displaytarget
64 {
65 enum pipe_format format;
66 unsigned width;
67 unsigned height;
68 unsigned stride;
69
70 void *data;
71 void *mapped;
72
73 Display *display;
74 Visual *visual;
75 XImage *tempImage;
76
77 XShmSegmentInfo shminfo;
78 int shm;
79 };
80
81
82 /**
83 * Subclass of sw_winsys for Xlib winsys
84 */
85 struct xlib_sw_winsys
86 {
87 struct sw_winsys base;
88
89
90
91 Display *display;
92 };
93
94
95
96 /** Cast wrapper */
97 static INLINE struct xm_displaytarget *
98 xm_displaytarget( struct sw_displaytarget *dt )
99 {
100 return (struct xm_displaytarget *)dt;
101 }
102
103
104 /**
105 * X Shared Memory Image extension code
106 */
107
108 #ifdef USE_XSHM
109
110 static volatile int mesaXErrorFlag = 0;
111
112 /**
113 * Catches potential Xlib errors.
114 */
115 static int
116 mesaHandleXError(Display *dpy, XErrorEvent *event)
117 {
118 (void) dpy;
119 (void) event;
120 mesaXErrorFlag = 1;
121 return 0;
122 }
123
124
125 static char *alloc_shm(struct xm_displaytarget *buf, unsigned size)
126 {
127 XShmSegmentInfo *const shminfo = & buf->shminfo;
128
129 shminfo->shmid = shmget(IPC_PRIVATE, size, IPC_CREAT|0777);
130 if (shminfo->shmid < 0) {
131 return NULL;
132 }
133
134 shminfo->shmaddr = (char *) shmat(shminfo->shmid, 0, 0);
135 if (shminfo->shmaddr == (char *) -1) {
136 shmctl(shminfo->shmid, IPC_RMID, 0);
137 return NULL;
138 }
139
140 shminfo->readOnly = False;
141 return shminfo->shmaddr;
142 }
143
144
145 /**
146 * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
147 */
148 static void
149 alloc_shm_ximage(struct xm_displaytarget *xm_dt,
150 struct xlib_drawable *xmb,
151 unsigned width, unsigned height)
152 {
153 /*
154 * We have to do a _lot_ of error checking here to be sure we can
155 * really use the XSHM extension. It seems different servers trigger
156 * errors at different points if the extension won't work. Therefore
157 * we have to be very careful...
158 */
159 int (*old_handler)(Display *, XErrorEvent *);
160
161 xm_dt->tempImage = XShmCreateImage(xm_dt->display,
162 xmb->visual,
163 xmb->depth,
164 ZPixmap,
165 NULL,
166 &xm_dt->shminfo,
167 width, height);
168 if (xm_dt->tempImage == NULL) {
169 xm_dt->shm = 0;
170 return;
171 }
172
173
174 mesaXErrorFlag = 0;
175 old_handler = XSetErrorHandler(mesaHandleXError);
176 /* This may trigger the X protocol error we're ready to catch: */
177 XShmAttach(xm_dt->display, &xm_dt->shminfo);
178 XSync(xm_dt->display, False);
179
180 if (mesaXErrorFlag) {
181 /* we are on a remote display, this error is normal, don't print it */
182 XFlush(xm_dt->display);
183 mesaXErrorFlag = 0;
184 XDestroyImage(xm_dt->tempImage);
185 xm_dt->tempImage = NULL;
186 xm_dt->shm = 0;
187 (void) XSetErrorHandler(old_handler);
188 return;
189 }
190
191 xm_dt->shm = 1;
192 }
193
194 #endif /* USE_XSHM */
195
196 static boolean
197 xm_is_displaytarget_format_supported( struct sw_winsys *ws,
198 enum pipe_format format )
199 {
200 /* TODO: check visuals or other sensible thing here */
201 return TRUE;
202 }
203
204
205 static void *
206 xm_displaytarget_map(struct sw_winsys *ws,
207 struct sw_displaytarget *dt,
208 unsigned flags)
209 {
210 struct xm_displaytarget *xm_dt = xm_displaytarget(dt);
211 xm_dt->mapped = xm_dt->data;
212 return xm_dt->mapped;
213 }
214
215 static void
216 xm_displaytarget_unmap(struct sw_winsys *ws,
217 struct sw_displaytarget *dt)
218 {
219 struct xm_displaytarget *xm_dt = xm_displaytarget(dt);
220 xm_dt->mapped = NULL;
221 }
222
223 static void
224 xm_displaytarget_destroy(struct sw_winsys *ws,
225 struct sw_displaytarget *dt)
226 {
227 struct xm_displaytarget *xm_dt = xm_displaytarget(dt);
228
229 if (xm_dt->data) {
230 #ifdef USE_XSHM
231 if (xm_dt->shminfo.shmid >= 0) {
232 shmdt(xm_dt->shminfo.shmaddr);
233 shmctl(xm_dt->shminfo.shmid, IPC_RMID, 0);
234
235 xm_dt->shminfo.shmid = -1;
236 xm_dt->shminfo.shmaddr = (char *) -1;
237 }
238 else
239 #endif
240 FREE(xm_dt->data);
241 }
242
243 FREE(xm_dt);
244 }
245
246
247 /**
248 * Display/copy the image in the surface into the X window specified
249 * by the XMesaBuffer.
250 */
251 void
252 xlib_sw_display(struct xlib_drawable *xlib_drawable,
253 struct sw_displaytarget *dt)
254 {
255 XImage *ximage;
256 struct xm_displaytarget *xm_dt = xm_displaytarget(dt);
257 static boolean no_swap = 0;
258 static boolean firsttime = 1;
259
260 if (firsttime) {
261 no_swap = getenv("SP_NO_RAST") != NULL;
262 firsttime = 0;
263 }
264
265 if (no_swap)
266 return;
267
268 #ifdef USE_XSHM
269 if (xm_dt->shm)
270 {
271 if (xm_dt->tempImage == NULL)
272 {
273 assert(util_format_get_blockwidth(xm_dt->format) == 1);
274 assert(util_format_get_blockheight(xm_dt->format) == 1);
275 alloc_shm_ximage(xm_dt,
276 xlib_drawable,
277 xm_dt->stride / util_format_get_blocksize(xm_dt->format),
278 xm_dt->height);
279 }
280
281 ximage = xm_dt->tempImage;
282 ximage->data = xm_dt->data;
283
284 /* _debug_printf("XSHM\n"); */
285 XShmPutImage(xm_dt->display, xlib_drawable->drawable, xlib_drawable->gc,
286 ximage, 0, 0, 0, 0, xm_dt->width, xm_dt->height, False);
287 }
288 else
289 #endif
290 {
291 /* display image in Window */
292 ximage = xm_dt->tempImage;
293 ximage->data = xm_dt->data;
294
295 /* check that the XImage has been previously initialized */
296 assert(ximage->format);
297 assert(ximage->bitmap_unit);
298
299 /* update XImage's fields */
300 ximage->width = xm_dt->width;
301 ximage->height = xm_dt->height;
302 ximage->bytes_per_line = xm_dt->stride;
303
304 /* _debug_printf("XPUT\n"); */
305 XPutImage(xm_dt->display, xlib_drawable->drawable, xlib_drawable->gc,
306 ximage, 0, 0, 0, 0, xm_dt->width, xm_dt->height);
307 }
308 }
309
310 /**
311 * Display/copy the image in the surface into the X window specified
312 * by the XMesaBuffer.
313 */
314 static void
315 xm_displaytarget_display(struct sw_winsys *ws,
316 struct sw_displaytarget *dt,
317 void *context_private)
318 {
319 struct xlib_drawable *xlib_drawable = (struct xlib_drawable *)context_private;
320 xlib_sw_display(xlib_drawable, dt);
321 }
322
323
324 static struct sw_displaytarget *
325 xm_displaytarget_create(struct sw_winsys *winsys,
326 enum pipe_format format,
327 unsigned width, unsigned height,
328 unsigned alignment,
329 unsigned *stride)
330 {
331 struct xm_displaytarget *xm_dt = CALLOC_STRUCT(xm_displaytarget);
332 unsigned nblocksy, size;
333
334 xm_dt = CALLOC_STRUCT(xm_displaytarget);
335 if(!xm_dt)
336 goto no_xm_dt;
337
338 xm_dt->display = ((struct xlib_sw_winsys *)winsys)->display;
339 xm_dt->format = format;
340 xm_dt->width = width;
341 xm_dt->height = height;
342
343 nblocksy = util_format_get_nblocksy(format, height);
344 xm_dt->stride = align(util_format_get_stride(format, width), alignment);
345 size = xm_dt->stride * nblocksy;
346
347 #ifdef USE_XSHM
348 if (!debug_get_bool_option("XLIB_NO_SHM", FALSE))
349 {
350 xm_dt->shminfo.shmid = -1;
351 xm_dt->shminfo.shmaddr = (char *) -1;
352 xm_dt->shm = TRUE;
353
354 xm_dt->data = alloc_shm(xm_dt, size);
355 if(!xm_dt->data)
356 goto no_data;
357 }
358 #endif
359
360 if(!xm_dt->data) {
361 xm_dt->data = align_malloc(size, alignment);
362 if(!xm_dt->data)
363 goto no_data;
364 }
365
366 *stride = xm_dt->stride;
367 return (struct sw_displaytarget *)xm_dt;
368
369 no_data:
370 FREE(xm_dt);
371 no_xm_dt:
372 return NULL;
373 }
374
375
376 static void
377 xm_destroy( struct sw_winsys *ws )
378 {
379 FREE(ws);
380 }
381
382
383 struct sw_winsys *
384 xlib_create_sw_winsys( Display *display )
385 {
386 struct xlib_sw_winsys *ws;
387
388 ws = CALLOC_STRUCT(xlib_sw_winsys);
389 if (!ws)
390 return NULL;
391
392 ws->display = display;
393 ws->base.destroy = xm_destroy;
394
395 ws->base.is_displaytarget_format_supported = xm_is_displaytarget_format_supported;
396
397 ws->base.displaytarget_create = xm_displaytarget_create;
398 ws->base.displaytarget_map = xm_displaytarget_map;
399 ws->base.displaytarget_unmap = xm_displaytarget_unmap;
400 ws->base.displaytarget_destroy = xm_displaytarget_destroy;
401
402 ws->base.displaytarget_display = xm_displaytarget_display;
403
404 return &ws->base;
405 }
406