1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, 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 "util/format/u_formats.h"
36 #include "pipe/p_context.h"
37 #include "util/u_inlines.h"
38 #include "util/format/u_format.h"
39 #include "util/u_math.h"
40 #include "util/u_memory.h"
41
42 #include "frontend/xlibsw_api.h"
43 #include "xlib_sw_winsys.h"
44
45 #include <X11/Xlib.h>
46 #include <X11/Xlibint.h>
47 #include <X11/Xutil.h>
48 #include <sys/ipc.h>
49 #include <sys/shm.h>
50 #include <X11/extensions/XShm.h>
51
52 DEBUG_GET_ONCE_BOOL_OPTION(xlib_no_shm, "XLIB_NO_SHM", false)
53
54 /**
55 * Display target for Xlib winsys.
56 * Low-level OS/window system memory buffer
57 */
58 struct xlib_displaytarget
59 {
60 enum pipe_format format;
61 unsigned width;
62 unsigned height;
63 unsigned stride;
64
65 void *data;
66 void *mapped;
67
68 Display *display;
69 Visual *visual;
70 XImage *tempImage;
71 GC gc;
72
73 /* This is the last drawable that this display target was presented
74 * against. May need to recreate gc, tempImage when this changes??
75 */
76 Drawable drawable;
77
78 XShmSegmentInfo shminfo;
79 Bool shm; /** Using shared memory images? */
80 };
81
82
83 /**
84 * Subclass of sw_winsys for Xlib winsys
85 */
86 struct xlib_sw_winsys
87 {
88 struct sw_winsys base;
89 Display *display;
90 };
91
92
93
94 /** Cast wrapper */
95 static inline struct xlib_displaytarget *
xlib_displaytarget(struct sw_displaytarget * dt)96 xlib_displaytarget(struct sw_displaytarget *dt)
97 {
98 return (struct xlib_displaytarget *) dt;
99 }
100
101
102 /**
103 * X Shared Memory Image extension code
104 */
105
106 static volatile int XErrorFlag = 0;
107
108 /**
109 * Catches potential Xlib errors.
110 */
111 static int
handle_xerror(Display * dpy,XErrorEvent * event)112 handle_xerror(Display *dpy, XErrorEvent *event)
113 {
114 (void) dpy;
115 (void) event;
116 XErrorFlag = 1;
117 return 0;
118 }
119
120
121 static char *
alloc_shm(struct xlib_displaytarget * buf,unsigned size)122 alloc_shm(struct xlib_displaytarget *buf, unsigned size)
123 {
124 XShmSegmentInfo *const shminfo = & buf->shminfo;
125
126 shminfo->shmid = -1;
127 shminfo->shmaddr = (char *) -1;
128
129 /* 0600 = user read+write */
130 shminfo->shmid = shmget(IPC_PRIVATE, size, IPC_CREAT | 0600);
131 if (shminfo->shmid < 0) {
132 return NULL;
133 }
134
135 shminfo->shmaddr = (char *) shmat(shminfo->shmid, 0, 0);
136 if (shminfo->shmaddr == (char *) -1) {
137 shmctl(shminfo->shmid, IPC_RMID, 0);
138 return NULL;
139 }
140
141 shmctl(shminfo->shmid, IPC_RMID, 0);
142 shminfo->readOnly = False;
143 return shminfo->shmaddr;
144 }
145
146
147 /**
148 * Allocate a shared memory XImage back buffer for the given display target.
149 */
150 static void
alloc_shm_ximage(struct xlib_displaytarget * xlib_dt,struct xlib_drawable * xmb,unsigned width,unsigned height)151 alloc_shm_ximage(struct xlib_displaytarget *xlib_dt,
152 struct xlib_drawable *xmb,
153 unsigned width, unsigned height)
154 {
155 /*
156 * We have to do a _lot_ of error checking here to be sure we can
157 * really use the XSHM extension. It seems different servers trigger
158 * errors at different points if the extension won't work. Therefore
159 * we have to be very careful...
160 */
161 int (*old_handler)(Display *, XErrorEvent *);
162
163 xlib_dt->tempImage = XShmCreateImage(xlib_dt->display,
164 xmb->visual,
165 xmb->depth,
166 ZPixmap,
167 NULL,
168 &xlib_dt->shminfo,
169 width, height);
170 if (xlib_dt->tempImage == NULL) {
171 shmctl(xlib_dt->shminfo.shmid, IPC_RMID, 0);
172 xlib_dt->shm = False;
173 return;
174 }
175
176
177 XErrorFlag = 0;
178 old_handler = XSetErrorHandler(handle_xerror);
179 /* This may trigger the X protocol error we're ready to catch: */
180 XShmAttach(xlib_dt->display, &xlib_dt->shminfo);
181 XSync(xlib_dt->display, False);
182
183 /* Mark the segment to be destroyed, so that it is automatically destroyed
184 * when this process dies. Needs to be after XShmAttach() for *BSD.
185 */
186 shmctl(xlib_dt->shminfo.shmid, IPC_RMID, 0);
187
188 if (XErrorFlag) {
189 /* we are on a remote display, this error is normal, don't print it */
190 XFlush(xlib_dt->display);
191 XErrorFlag = 0;
192 XDestroyImage(xlib_dt->tempImage);
193 xlib_dt->tempImage = NULL;
194 xlib_dt->shm = False;
195 (void) XSetErrorHandler(old_handler);
196 return;
197 }
198
199 xlib_dt->shm = True;
200 }
201
202
203 static void
alloc_ximage(struct xlib_displaytarget * xlib_dt,struct xlib_drawable * xmb,unsigned width,unsigned height)204 alloc_ximage(struct xlib_displaytarget *xlib_dt,
205 struct xlib_drawable *xmb,
206 unsigned width, unsigned height)
207 {
208 /* try allocating a shared memory image first */
209 if (xlib_dt->shm) {
210 alloc_shm_ximage(xlib_dt, xmb, width, height);
211 if (xlib_dt->tempImage)
212 return; /* success */
213 }
214
215 /* try regular (non-shared memory) image */
216 xlib_dt->tempImage = XCreateImage(xlib_dt->display,
217 xmb->visual,
218 xmb->depth,
219 ZPixmap, 0,
220 NULL, width, height,
221 8, 0);
222 }
223
224 static bool
xlib_is_displaytarget_format_supported(struct sw_winsys * ws,unsigned tex_usage,enum pipe_format format)225 xlib_is_displaytarget_format_supported(struct sw_winsys *ws,
226 unsigned tex_usage,
227 enum pipe_format format)
228 {
229 /* TODO: check visuals or other sensible thing here */
230 return true;
231 }
232
233
234 static void *
xlib_displaytarget_map(struct sw_winsys * ws,struct sw_displaytarget * dt,unsigned flags)235 xlib_displaytarget_map(struct sw_winsys *ws,
236 struct sw_displaytarget *dt,
237 unsigned flags)
238 {
239 struct xlib_displaytarget *xlib_dt = xlib_displaytarget(dt);
240 xlib_dt->mapped = xlib_dt->data;
241 return xlib_dt->mapped;
242 }
243
244
245 static void
xlib_displaytarget_unmap(struct sw_winsys * ws,struct sw_displaytarget * dt)246 xlib_displaytarget_unmap(struct sw_winsys *ws,
247 struct sw_displaytarget *dt)
248 {
249 struct xlib_displaytarget *xlib_dt = xlib_displaytarget(dt);
250 xlib_dt->mapped = NULL;
251 }
252
253
254 static void
xlib_displaytarget_destroy(struct sw_winsys * ws,struct sw_displaytarget * dt)255 xlib_displaytarget_destroy(struct sw_winsys *ws,
256 struct sw_displaytarget *dt)
257 {
258 struct xlib_displaytarget *xlib_dt = xlib_displaytarget(dt);
259
260 if (xlib_dt->data) {
261 if (xlib_dt->shminfo.shmid >= 0) {
262 shmdt(xlib_dt->shminfo.shmaddr);
263 shmctl(xlib_dt->shminfo.shmid, IPC_RMID, 0);
264
265 xlib_dt->shminfo.shmid = -1;
266 xlib_dt->shminfo.shmaddr = (char *) -1;
267
268 xlib_dt->data = NULL;
269 if (xlib_dt->tempImage)
270 xlib_dt->tempImage->data = NULL;
271 }
272 else {
273 align_free(xlib_dt->data);
274 if (xlib_dt->tempImage && xlib_dt->tempImage->data == xlib_dt->data) {
275 xlib_dt->tempImage->data = NULL;
276 }
277 xlib_dt->data = NULL;
278 }
279 }
280
281 if (xlib_dt->tempImage) {
282 XDestroyImage(xlib_dt->tempImage);
283 xlib_dt->tempImage = NULL;
284 }
285
286 if (xlib_dt->gc)
287 XFreeGC(xlib_dt->display, xlib_dt->gc);
288
289 FREE(xlib_dt);
290 }
291
292
293 /**
294 * Display/copy the image in the surface into the X window specified
295 * by the display target.
296 */
297 static void
xlib_sw_display(struct xlib_drawable * xlib_drawable,struct sw_displaytarget * dt,struct pipe_box * box)298 xlib_sw_display(struct xlib_drawable *xlib_drawable,
299 struct sw_displaytarget *dt,
300 struct pipe_box *box)
301 {
302 static bool no_swap = false;
303 static bool firsttime = true;
304 struct xlib_displaytarget *xlib_dt = xlib_displaytarget(dt);
305 Display *display = xlib_dt->display;
306 XImage *ximage;
307 struct pipe_box _box = {};
308
309 if (firsttime) {
310 no_swap = getenv("SP_NO_RAST") != NULL;
311 firsttime = 0;
312 }
313
314 if (no_swap)
315 return;
316
317 if (!box) {
318 _box.width = xlib_dt->width;
319 _box.height = xlib_dt->height;
320 box = &_box;
321 }
322
323 if (xlib_dt->drawable != xlib_drawable->drawable) {
324 if (xlib_dt->gc) {
325 XFreeGC(display, xlib_dt->gc);
326 xlib_dt->gc = NULL;
327 }
328
329 if (xlib_dt->tempImage) {
330 XDestroyImage(xlib_dt->tempImage);
331 xlib_dt->tempImage = NULL;
332 }
333
334 xlib_dt->drawable = xlib_drawable->drawable;
335 }
336
337 if (xlib_dt->tempImage == NULL) {
338 assert(util_format_get_blockwidth(xlib_dt->format) == 1);
339 assert(util_format_get_blockheight(xlib_dt->format) == 1);
340 alloc_ximage(xlib_dt, xlib_drawable,
341 xlib_dt->stride / util_format_get_blocksize(xlib_dt->format),
342 xlib_dt->height);
343 if (!xlib_dt->tempImage)
344 return;
345 }
346
347 if (xlib_dt->gc == NULL) {
348 xlib_dt->gc = XCreateGC(display, xlib_drawable->drawable, 0, NULL);
349 XSetFunction(display, xlib_dt->gc, GXcopy);
350 }
351
352 if (xlib_dt->shm) {
353 ximage = xlib_dt->tempImage;
354 ximage->data = xlib_dt->data;
355
356 /* _debug_printf("XSHM\n"); */
357 XShmPutImage(xlib_dt->display, xlib_drawable->drawable, xlib_dt->gc,
358 ximage, box->x, box->y, box->x, box->y,
359 box->width, box->height, False);
360 }
361 else {
362 /* display image in Window */
363 ximage = xlib_dt->tempImage;
364 ximage->data = xlib_dt->data;
365
366 /* check that the XImage has been previously initialized */
367 assert(ximage->format);
368 assert(ximage->bitmap_unit);
369
370 /* update XImage's fields */
371 ximage->width = xlib_dt->width;
372 ximage->height = xlib_dt->height;
373 ximage->bytes_per_line = xlib_dt->stride;
374
375 /* _debug_printf("XPUT\n"); */
376 XPutImage(xlib_dt->display, xlib_drawable->drawable, xlib_dt->gc,
377 ximage, box->x, box->y, box->x, box->y,
378 box->width, box->height);
379 }
380
381 XFlush(xlib_dt->display);
382 }
383
384
385 /**
386 * Display/copy the image in the surface into the X window specified
387 * by the display target.
388 */
389 static void
xlib_displaytarget_display(struct sw_winsys * ws,struct sw_displaytarget * dt,void * context_private,struct pipe_box * box)390 xlib_displaytarget_display(struct sw_winsys *ws,
391 struct sw_displaytarget *dt,
392 void *context_private,
393 struct pipe_box *box)
394 {
395 struct xlib_drawable *xlib_drawable = (struct xlib_drawable *)context_private;
396 xlib_sw_display(xlib_drawable, dt, box);
397 }
398
399
400 static struct sw_displaytarget *
xlib_displaytarget_create(struct sw_winsys * winsys,unsigned tex_usage,enum pipe_format format,unsigned width,unsigned height,unsigned alignment,const void * front_private,unsigned * stride)401 xlib_displaytarget_create(struct sw_winsys *winsys,
402 unsigned tex_usage,
403 enum pipe_format format,
404 unsigned width, unsigned height,
405 unsigned alignment,
406 const void *front_private,
407 unsigned *stride)
408 {
409 struct xlib_displaytarget *xlib_dt;
410 unsigned nblocksy, size;
411 int ignore;
412
413 xlib_dt = CALLOC_STRUCT(xlib_displaytarget);
414 if (!xlib_dt)
415 goto no_xlib_dt;
416
417 xlib_dt->display = ((struct xlib_sw_winsys *)winsys)->display;
418 xlib_dt->format = format;
419 xlib_dt->width = width;
420 xlib_dt->height = height;
421
422 nblocksy = util_format_get_nblocksy(format, height);
423 xlib_dt->stride = align(util_format_get_stride(format, width), alignment);
424 size = xlib_dt->stride * nblocksy;
425
426 if (!debug_get_option_xlib_no_shm() &&
427 XQueryExtension(xlib_dt->display, "MIT-SHM", &ignore, &ignore, &ignore)) {
428 xlib_dt->data = alloc_shm(xlib_dt, size);
429 if (xlib_dt->data) {
430 xlib_dt->shm = True;
431 }
432 }
433
434 if (!xlib_dt->data) {
435 xlib_dt->data = align_malloc(size, alignment);
436 if (!xlib_dt->data)
437 goto no_data;
438 }
439
440 *stride = xlib_dt->stride;
441 return (struct sw_displaytarget *)xlib_dt;
442
443 no_data:
444 FREE(xlib_dt);
445 no_xlib_dt:
446 return NULL;
447 }
448
449
450 static struct sw_displaytarget *
xlib_displaytarget_from_handle(struct sw_winsys * winsys,const struct pipe_resource * templet,struct winsys_handle * whandle,unsigned * stride)451 xlib_displaytarget_from_handle(struct sw_winsys *winsys,
452 const struct pipe_resource *templet,
453 struct winsys_handle *whandle,
454 unsigned *stride)
455 {
456 assert(0);
457 return NULL;
458 }
459
460
461 static bool
xlib_displaytarget_get_handle(struct sw_winsys * winsys,struct sw_displaytarget * dt,struct winsys_handle * whandle)462 xlib_displaytarget_get_handle(struct sw_winsys *winsys,
463 struct sw_displaytarget *dt,
464 struct winsys_handle *whandle)
465 {
466 assert(0);
467 return false;
468 }
469
470
471 static void
xlib_destroy(struct sw_winsys * ws)472 xlib_destroy(struct sw_winsys *ws)
473 {
474 FREE(ws);
475 }
476
477
478 struct sw_winsys *
xlib_create_sw_winsys(Display * display)479 xlib_create_sw_winsys(Display *display)
480 {
481 struct xlib_sw_winsys *ws;
482
483 ws = CALLOC_STRUCT(xlib_sw_winsys);
484 if (!ws)
485 return NULL;
486
487 ws->display = display;
488 ws->base.destroy = xlib_destroy;
489
490 ws->base.is_displaytarget_format_supported = xlib_is_displaytarget_format_supported;
491
492 ws->base.displaytarget_create = xlib_displaytarget_create;
493 ws->base.displaytarget_from_handle = xlib_displaytarget_from_handle;
494 ws->base.displaytarget_get_handle = xlib_displaytarget_get_handle;
495 ws->base.displaytarget_map = xlib_displaytarget_map;
496 ws->base.displaytarget_unmap = xlib_displaytarget_unmap;
497 ws->base.displaytarget_destroy = xlib_displaytarget_destroy;
498
499 ws->base.displaytarget_display = xlib_displaytarget_display;
500
501 return &ws->base;
502 }
503