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,unsigned nboxes,struct pipe_box * box)298 xlib_sw_display(struct xlib_drawable *xlib_drawable,
299 struct sw_displaytarget *dt,
300 unsigned nboxes,
301 struct pipe_box *box)
302 {
303 static bool no_swap = false;
304 static bool firsttime = true;
305 struct xlib_displaytarget *xlib_dt = xlib_displaytarget(dt);
306 Display *display = xlib_dt->display;
307 XImage *ximage;
308 struct pipe_box _box = {};
309
310 if (firsttime) {
311 no_swap = getenv("SP_NO_RAST") != NULL;
312 firsttime = 0;
313 }
314
315 if (no_swap)
316 return;
317
318 if (!nboxes) {
319 nboxes = 1;
320 _box.width = xlib_dt->width;
321 _box.height = xlib_dt->height;
322 box = &_box;
323 }
324
325 if (xlib_dt->drawable != xlib_drawable->drawable) {
326 if (xlib_dt->gc) {
327 XFreeGC(display, xlib_dt->gc);
328 xlib_dt->gc = NULL;
329 }
330
331 if (xlib_dt->tempImage) {
332 XDestroyImage(xlib_dt->tempImage);
333 xlib_dt->tempImage = NULL;
334 }
335
336 xlib_dt->drawable = xlib_drawable->drawable;
337 }
338
339 if (xlib_dt->tempImage == NULL) {
340 assert(util_format_get_blockwidth(xlib_dt->format) == 1);
341 assert(util_format_get_blockheight(xlib_dt->format) == 1);
342 alloc_ximage(xlib_dt, xlib_drawable,
343 xlib_dt->stride / util_format_get_blocksize(xlib_dt->format),
344 xlib_dt->height);
345 if (!xlib_dt->tempImage)
346 return;
347 }
348
349 if (xlib_dt->gc == NULL) {
350 xlib_dt->gc = XCreateGC(display, xlib_drawable->drawable, 0, NULL);
351 XSetFunction(display, xlib_dt->gc, GXcopy);
352 }
353
354 for (unsigned i = 0; i < nboxes; i++) {
355 if (xlib_dt->shm) {
356 ximage = xlib_dt->tempImage;
357 ximage->data = xlib_dt->data;
358
359 /* _debug_printf("XSHM\n"); */
360 XShmPutImage(xlib_dt->display, xlib_drawable->drawable, xlib_dt->gc,
361 ximage, box[i].x, box[i].y, box[i].x, box[i].y,
362 box[i].width, box[i].height, False);
363 }
364 else {
365 /* display image in Window */
366 ximage = xlib_dt->tempImage;
367 ximage->data = xlib_dt->data;
368
369 /* check that the XImage has been previously initialized */
370 assert(ximage->format);
371 assert(ximage->bitmap_unit);
372
373 /* update XImage's fields */
374 ximage->width = xlib_dt->width;
375 ximage->height = xlib_dt->height;
376 ximage->bytes_per_line = xlib_dt->stride;
377
378 /* _debug_printf("XPUT\n"); */
379 XPutImage(xlib_dt->display, xlib_drawable->drawable, xlib_dt->gc,
380 ximage, box[i].x, box[i].y, box[i].x, box[i].y,
381 box[i].width, box[i].height);
382 }
383 }
384
385 XFlush(xlib_dt->display);
386 }
387
388
389 /**
390 * Display/copy the image in the surface into the X window specified
391 * by the display target.
392 */
393 static void
xlib_displaytarget_display(struct sw_winsys * ws,struct sw_displaytarget * dt,void * context_private,unsigned nboxes,struct pipe_box * box)394 xlib_displaytarget_display(struct sw_winsys *ws,
395 struct sw_displaytarget *dt,
396 void *context_private,
397 unsigned nboxes,
398 struct pipe_box *box)
399 {
400 struct xlib_drawable *xlib_drawable = (struct xlib_drawable *)context_private;
401 xlib_sw_display(xlib_drawable, dt, nboxes, box);
402 }
403
404
405 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)406 xlib_displaytarget_create(struct sw_winsys *winsys,
407 unsigned tex_usage,
408 enum pipe_format format,
409 unsigned width, unsigned height,
410 unsigned alignment,
411 const void *front_private,
412 unsigned *stride)
413 {
414 struct xlib_displaytarget *xlib_dt;
415 unsigned nblocksy, size;
416 int ignore;
417
418 xlib_dt = CALLOC_STRUCT(xlib_displaytarget);
419 if (!xlib_dt)
420 goto no_xlib_dt;
421
422 xlib_dt->display = ((struct xlib_sw_winsys *)winsys)->display;
423 xlib_dt->format = format;
424 xlib_dt->width = width;
425 xlib_dt->height = height;
426
427 nblocksy = util_format_get_nblocksy(format, height);
428 xlib_dt->stride = align(util_format_get_stride(format, width), alignment);
429 size = xlib_dt->stride * nblocksy;
430
431 if (!debug_get_option_xlib_no_shm() &&
432 XQueryExtension(xlib_dt->display, "MIT-SHM", &ignore, &ignore, &ignore)) {
433 xlib_dt->data = alloc_shm(xlib_dt, size);
434 if (xlib_dt->data) {
435 xlib_dt->shm = True;
436 }
437 }
438
439 if (!xlib_dt->data) {
440 xlib_dt->data = align_malloc(size, alignment);
441 if (!xlib_dt->data)
442 goto no_data;
443 }
444
445 *stride = xlib_dt->stride;
446 return (struct sw_displaytarget *)xlib_dt;
447
448 no_data:
449 FREE(xlib_dt);
450 no_xlib_dt:
451 return NULL;
452 }
453
454
455 static struct sw_displaytarget *
xlib_displaytarget_from_handle(struct sw_winsys * winsys,const struct pipe_resource * templet,struct winsys_handle * whandle,unsigned * stride)456 xlib_displaytarget_from_handle(struct sw_winsys *winsys,
457 const struct pipe_resource *templet,
458 struct winsys_handle *whandle,
459 unsigned *stride)
460 {
461 assert(0);
462 return NULL;
463 }
464
465
466 static bool
xlib_displaytarget_get_handle(struct sw_winsys * winsys,struct sw_displaytarget * dt,struct winsys_handle * whandle)467 xlib_displaytarget_get_handle(struct sw_winsys *winsys,
468 struct sw_displaytarget *dt,
469 struct winsys_handle *whandle)
470 {
471 assert(0);
472 return false;
473 }
474
475
476 static void
xlib_destroy(struct sw_winsys * ws)477 xlib_destroy(struct sw_winsys *ws)
478 {
479 FREE(ws);
480 }
481
482
483 struct sw_winsys *
xlib_create_sw_winsys(Display * display)484 xlib_create_sw_winsys(Display *display)
485 {
486 struct xlib_sw_winsys *ws;
487
488 ws = CALLOC_STRUCT(xlib_sw_winsys);
489 if (!ws)
490 return NULL;
491
492 ws->display = display;
493 ws->base.destroy = xlib_destroy;
494
495 ws->base.is_displaytarget_format_supported = xlib_is_displaytarget_format_supported;
496
497 ws->base.displaytarget_create = xlib_displaytarget_create;
498 ws->base.displaytarget_from_handle = xlib_displaytarget_from_handle;
499 ws->base.displaytarget_get_handle = xlib_displaytarget_get_handle;
500 ws->base.displaytarget_map = xlib_displaytarget_map;
501 ws->base.displaytarget_unmap = xlib_displaytarget_unmap;
502 ws->base.displaytarget_destroy = xlib_displaytarget_destroy;
503
504 ws->base.displaytarget_display = xlib_displaytarget_display;
505
506 return &ws->base;
507 }
508