summaryrefslogtreecommitdiff
path: root/src/gallium/winsys/sw/xlib/xlib_sw_winsys.c
blob: 0eea71b752383fe86397e8c6e77ed7168a984bf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/**************************************************************************
 * 
 * Copyright 2007 VMware, Inc., Bismarck, ND., USA
 * All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * 
 **************************************************************************/

/*
 * Authors:
 *   Keith Whitwell
 *   Brian Paul
 */

#include "pipe/p_format.h"
#include "pipe/p_context.h"
#include "util/u_inlines.h"
#include "util/format/u_format.h"
#include "util/u_math.h"
#include "util/u_memory.h"

#include "frontend/xlibsw_api.h"
#include "xlib_sw_winsys.h"

#include <X11/Xlib.h>
#include <X11/Xlibint.h>
#include <X11/Xutil.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/extensions/XShm.h>

DEBUG_GET_ONCE_BOOL_OPTION(xlib_no_shm, "XLIB_NO_SHM", false)

/**
 * Display target for Xlib winsys.
 * Low-level OS/window system memory buffer
 */
struct xlib_displaytarget
{
   enum pipe_format format;
   unsigned width;
   unsigned height;
   unsigned stride;

   void *data;
   void *mapped;

   Display *display;
   Visual *visual;
   XImage *tempImage;
   GC gc;

   /* This is the last drawable that this display target was presented
    * against.  May need to recreate gc, tempImage when this changes??
    */
   Drawable drawable;

   XShmSegmentInfo shminfo;
   Bool shm;  /** Using shared memory images? */
};


/**
 * Subclass of sw_winsys for Xlib winsys
 */
struct xlib_sw_winsys
{
   struct sw_winsys base;
   Display *display;
};



/** Cast wrapper */
static inline struct xlib_displaytarget *
xlib_displaytarget(struct sw_displaytarget *dt)
{
   return (struct xlib_displaytarget *) dt;
}


/**
 * X Shared Memory Image extension code
 */

static volatile int XErrorFlag = 0;

/**
 * Catches potential Xlib errors.
 */
static int
handle_xerror(Display *dpy, XErrorEvent *event)
{
   (void) dpy;
   (void) event;
   XErrorFlag = 1;
   return 0;
}


static char *
alloc_shm(struct xlib_displaytarget *buf, unsigned size)
{
   XShmSegmentInfo *const shminfo = & buf->shminfo;

   shminfo->shmid = -1;
   shminfo->shmaddr = (char *) -1;

   /* 0600 = user read+write */
   shminfo->shmid = shmget(IPC_PRIVATE, size, IPC_CREAT | 0600);
   if (shminfo->shmid < 0) {
      return NULL;
   }

   shminfo->shmaddr = (char *) shmat(shminfo->shmid, 0, 0);
   if (shminfo->shmaddr == (char *) -1) {
      shmctl(shminfo->shmid, IPC_RMID, 0);
      return NULL;
   }

   shminfo->readOnly = False;
   return shminfo->shmaddr;
}


/**
 * Allocate a shared memory XImage back buffer for the given display target.
 */
static void
alloc_shm_ximage(struct xlib_displaytarget *xlib_dt,
                 struct xlib_drawable *xmb,
                 unsigned width, unsigned height)
{
   /*
    * We have to do a _lot_ of error checking here to be sure we can
    * really use the XSHM extension.  It seems different servers trigger
    * errors at different points if the extension won't work.  Therefore
    * we have to be very careful...
    */
   int (*old_handler)(Display *, XErrorEvent *);

   xlib_dt->tempImage = XShmCreateImage(xlib_dt->display,
                                      xmb->visual,
                                      xmb->depth,
                                      ZPixmap,
                                      NULL,
                                      &xlib_dt->shminfo,
                                      width, height);
   if (xlib_dt->tempImage == NULL) {
      shmctl(xlib_dt->shminfo.shmid, IPC_RMID, 0);
      xlib_dt->shm = False;
      return;
   }


   XErrorFlag = 0;
   old_handler = XSetErrorHandler(handle_xerror);
   /* This may trigger the X protocol error we're ready to catch: */
   XShmAttach(xlib_dt->display, &xlib_dt->shminfo);
   XSync(xlib_dt->display, False);

   /* Mark the segment to be destroyed, so that it is automatically destroyed
    * when this process dies.  Needs to be after XShmAttach() for *BSD.
    */
   shmctl(xlib_dt->shminfo.shmid, IPC_RMID, 0);

   if (XErrorFlag) {
      /* we are on a remote display, this error is normal, don't print it */
      XFlush(xlib_dt->display);
      XErrorFlag = 0;
      XDestroyImage(xlib_dt->tempImage);
      xlib_dt->tempImage = NULL;
      xlib_dt->shm = False;
      (void) XSetErrorHandler(old_handler);
      return;
   }

   xlib_dt->shm = True;
}


static void
alloc_ximage(struct xlib_displaytarget *xlib_dt,
             struct xlib_drawable *xmb,
             unsigned width, unsigned height)
{
   /* try allocating a shared memory image first */
   if (xlib_dt->shm) {
      alloc_shm_ximage(xlib_dt, xmb, width, height);
      if (xlib_dt->tempImage)
         return; /* success */
   }

   /* try regular (non-shared memory) image */
   xlib_dt->tempImage = XCreateImage(xlib_dt->display,
                                   xmb->visual,
                                   xmb->depth,
                                   ZPixmap, 0,
                                   NULL, width, height,
                                   8, 0);
}

static bool
xlib_is_displaytarget_format_supported(struct sw_winsys *ws,
                                       unsigned tex_usage,
                                       enum pipe_format format)
{
   /* TODO: check visuals or other sensible thing here */
   return true;
}


static void *
xlib_displaytarget_map(struct sw_winsys *ws,
                       struct sw_displaytarget *dt,
                       unsigned flags)
{
   struct xlib_displaytarget *xlib_dt = xlib_displaytarget(dt);
   xlib_dt->mapped = xlib_dt->data;
   return xlib_dt->mapped;
}


static void
xlib_displaytarget_unmap(struct sw_winsys *ws,
                         struct sw_displaytarget *dt)
{
   struct xlib_displaytarget *xlib_dt = xlib_displaytarget(dt);
   xlib_dt->mapped = NULL;
}


static void
xlib_displaytarget_destroy(struct sw_winsys *ws,
                           struct sw_displaytarget *dt)
{
   struct xlib_displaytarget *xlib_dt = xlib_displaytarget(dt);

   if (xlib_dt->data) {
      if (xlib_dt->shminfo.shmid >= 0) {
         shmdt(xlib_dt->shminfo.shmaddr);
         shmctl(xlib_dt->shminfo.shmid, IPC_RMID, 0);
         
         xlib_dt->shminfo.shmid = -1;
         xlib_dt->shminfo.shmaddr = (char *) -1;

         xlib_dt->data = NULL;
         if (xlib_dt->tempImage)
            xlib_dt->tempImage->data = NULL;
      }
      else {
         align_free(xlib_dt->data);
         if (xlib_dt->tempImage && xlib_dt->tempImage->data == xlib_dt->data) {
            xlib_dt->tempImage->data = NULL;
         }
         xlib_dt->data = NULL;
      }
   }

   if (xlib_dt->tempImage) {
      XDestroyImage(xlib_dt->tempImage);
      xlib_dt->tempImage = NULL;
   }

   if (xlib_dt->gc)
      XFreeGC(xlib_dt->display, xlib_dt->gc);

   FREE(xlib_dt);
}


/**
 * Display/copy the image in the surface into the X window specified
 * by the display target.
 */
static void
xlib_sw_display(struct xlib_drawable *xlib_drawable,
                struct sw_displaytarget *dt)
{
   static bool no_swap = false;
   static bool firsttime = true;
   struct xlib_displaytarget *xlib_dt = xlib_displaytarget(dt);
   Display *display = xlib_dt->display;
   XImage *ximage;

   if (firsttime) {
      no_swap = getenv("SP_NO_RAST") != NULL;
      firsttime = 0;
   }

   if (no_swap)
      return;

   if (xlib_dt->drawable != xlib_drawable->drawable) {
      if (xlib_dt->gc) {
         XFreeGC(display, xlib_dt->gc);
         xlib_dt->gc = NULL;
      }

      if (xlib_dt->tempImage) {
         XDestroyImage(xlib_dt->tempImage);
         xlib_dt->tempImage = NULL;
      }

      xlib_dt->drawable = xlib_drawable->drawable;
   }

   if (xlib_dt->tempImage == NULL) {
      assert(util_format_get_blockwidth(xlib_dt->format) == 1);
      assert(util_format_get_blockheight(xlib_dt->format) == 1);
      alloc_ximage(xlib_dt, xlib_drawable,
                   xlib_dt->stride / util_format_get_blocksize(xlib_dt->format),
                   xlib_dt->height);
      if (!xlib_dt->tempImage)
         return;
   }

   if (xlib_dt->gc == NULL) {
      xlib_dt->gc = XCreateGC(display, xlib_drawable->drawable, 0, NULL);
      XSetFunction(display, xlib_dt->gc, GXcopy);
   }

   if (xlib_dt->shm) {
      ximage = xlib_dt->tempImage;
      ximage->data = xlib_dt->data;

      /* _debug_printf("XSHM\n"); */
      XShmPutImage(xlib_dt->display, xlib_drawable->drawable, xlib_dt->gc,
                   ximage, 0, 0, 0, 0, xlib_dt->width, xlib_dt->height, False);
   }
   else {
      /* display image in Window */
      ximage = xlib_dt->tempImage;
      ximage->data = xlib_dt->data;

      /* check that the XImage has been previously initialized */
      assert(ximage->format);
      assert(ximage->bitmap_unit);

      /* update XImage's fields */
      ximage->width = xlib_dt->width;
      ximage->height = xlib_dt->height;
      ximage->bytes_per_line = xlib_dt->stride;

      /* _debug_printf("XPUT\n"); */
      XPutImage(xlib_dt->display, xlib_drawable->drawable, xlib_dt->gc,
                ximage, 0, 0, 0, 0, xlib_dt->width, xlib_dt->height);
   }

   XFlush(xlib_dt->display);
}


/**
 * Display/copy the image in the surface into the X window specified
 * by the display target.
 */
static void
xlib_displaytarget_display(struct sw_winsys *ws,
                           struct sw_displaytarget *dt,
                           void *context_private,
                           struct pipe_box *box)
{
   struct xlib_drawable *xlib_drawable = (struct xlib_drawable *)context_private;
   xlib_sw_display(xlib_drawable, dt);
}


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)
{
   struct xlib_displaytarget *xlib_dt;
   unsigned nblocksy, size;
   int ignore;

   xlib_dt = CALLOC_STRUCT(xlib_displaytarget);
   if (!xlib_dt)
      goto no_xlib_dt;

   xlib_dt->display = ((struct xlib_sw_winsys *)winsys)->display;
   xlib_dt->format = format;
   xlib_dt->width = width;
   xlib_dt->height = height;

   nblocksy = util_format_get_nblocksy(format, height);
   xlib_dt->stride = align(util_format_get_stride(format, width), alignment);
   size = xlib_dt->stride * nblocksy;

   if (!debug_get_option_xlib_no_shm() &&
       XQueryExtension(xlib_dt->display, "MIT-SHM", &ignore, &ignore, &ignore)) {
      xlib_dt->data = alloc_shm(xlib_dt, size);
      if (xlib_dt->data) {
         xlib_dt->shm = True;
      }
   }

   if (!xlib_dt->data) {
      xlib_dt->data = align_malloc(size, alignment);
      if (!xlib_dt->data)
         goto no_data;
   }

   *stride = xlib_dt->stride;
   return (struct sw_displaytarget *)xlib_dt;

no_data:
   FREE(xlib_dt);
no_xlib_dt:
   return NULL;
}


static struct sw_displaytarget *
xlib_displaytarget_from_handle(struct sw_winsys *winsys,
                               const struct pipe_resource *templet,
                               struct winsys_handle *whandle,
                               unsigned *stride)
{
   assert(0);
   return NULL;
}


static bool
xlib_displaytarget_get_handle(struct sw_winsys *winsys,
                              struct sw_displaytarget *dt,
                              struct winsys_handle *whandle)
{
   assert(0);
   return false;
}


static void
xlib_destroy(struct sw_winsys *ws)
{
   FREE(ws);
}


struct sw_winsys *
xlib_create_sw_winsys(Display *display)
{
   struct xlib_sw_winsys *ws;

   ws = CALLOC_STRUCT(xlib_sw_winsys);
   if (!ws)
      return NULL;

   ws->display = display;
   ws->base.destroy = xlib_destroy;

   ws->base.is_displaytarget_format_supported = xlib_is_displaytarget_format_supported;

   ws->base.displaytarget_create = xlib_displaytarget_create;
   ws->base.displaytarget_from_handle = xlib_displaytarget_from_handle;
   ws->base.displaytarget_get_handle = xlib_displaytarget_get_handle;
   ws->base.displaytarget_map = xlib_displaytarget_map;
   ws->base.displaytarget_unmap = xlib_displaytarget_unmap;
   ws->base.displaytarget_destroy = xlib_displaytarget_destroy;

   ws->base.displaytarget_display = xlib_displaytarget_display;

   return &ws->base;
}