aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/evas/src/modules/engines/software_x11/evas_xcb_buffer.c
blob: 49ce72182333fc76ac1d2578fd13aa7560977831 (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
492
493
494
495
496
497
498
#include "evas_common.h"
#include "evas_xcb_buffer.h"

/* local function prototypes */
static void _xcbob_sync(xcb_connection_t *conn);
static xcb_image_t *_xcbob_create_native(xcb_connection_t *conn, int w, int h, xcb_image_format_t format, uint8_t depth, void *base, uint32_t bytes, uint8_t *data);
static xcb_format_t *_xcbob_find_format(const xcb_setup_t *setup, uint8_t depth);
static xcb_visualtype_t *_xcbob_find_visual_by_id(xcb_screen_t *screen, xcb_visualid_t id);

void 
evas_software_xcb_write_mask_line(Outbuf *buf, Xcb_Output_Buffer *xcbob, DATA32 *src, int w, int y) 
{
   int x, bpl = 0;
   DATA32 *src_ptr;
   DATA8 *dst_ptr;

   src_ptr = src;
   dst_ptr = evas_software_xcb_output_buffer_data(xcbob, &bpl);
   dst_ptr = dst_ptr + (bpl * y);
   w -= 7;
   if (buf->priv.x11.xcb.bit_swap)
     {
	for (x = 0; x < w; x += 8)
	  {
	     *dst_ptr =
	       ((A_VAL(&(src_ptr[0])) >> 7) << 7) |
	       ((A_VAL(&(src_ptr[1])) >> 7) << 6) |
	       ((A_VAL(&(src_ptr[2])) >> 7) << 5) |
	       ((A_VAL(&(src_ptr[3])) >> 7) << 4) |
	       ((A_VAL(&(src_ptr[4])) >> 7) << 3) |
	       ((A_VAL(&(src_ptr[5])) >> 7) << 2) |
	       ((A_VAL(&(src_ptr[6])) >> 7) << 1) |
	       ((A_VAL(&(src_ptr[7])) >> 7) << 0);
	     src_ptr += 8;
	     dst_ptr++;
	  }
     }
   else
     {
	for (x = 0; x < w; x += 8)
	  {
	     *dst_ptr =
	       ((A_VAL(&(src_ptr[0])) >> 7) << 0) |
	       ((A_VAL(&(src_ptr[1])) >> 7) << 1) |
	       ((A_VAL(&(src_ptr[2])) >> 7) << 2) |
	       ((A_VAL(&(src_ptr[3])) >> 7) << 3) |
	       ((A_VAL(&(src_ptr[4])) >> 7) << 4) |
	       ((A_VAL(&(src_ptr[5])) >> 7) << 5) |
	       ((A_VAL(&(src_ptr[6])) >> 7) << 6) |
	       ((A_VAL(&(src_ptr[7])) >> 7) << 7);
	     src_ptr += 8;
	     dst_ptr++;
	  }
     }
   w += 7;
   for (; x < w; x ++)
     {
        xcb_image_put_pixel(xcbob->xim, x, y, A_VAL(src_ptr) >> 7);
        src_ptr++;
     }
}

void 
evas_software_xcb_write_mask_line_rev(Outbuf *buf, Xcb_Output_Buffer *xcbob, DATA32 *src, int w, int y) 
{
   int x, bpl = 0;
   DATA32 *src_ptr;
   DATA8 *dst_ptr;

   src_ptr = src + w - 1;
   dst_ptr = evas_software_xcb_output_buffer_data(xcbob, &bpl);
   dst_ptr = dst_ptr + (bpl * y);
   w -= 7;
   if (buf->priv.x11.xcb.bit_swap)
     {
	for (x = 0; x < w; x += 8)
	  {
	     *dst_ptr =
	       ((A_VAL(&(src_ptr[ 0])) >> 7) << 7) |
	       ((A_VAL(&(src_ptr[-1])) >> 7) << 6) |
	       ((A_VAL(&(src_ptr[-2])) >> 7) << 5) |
	       ((A_VAL(&(src_ptr[-3])) >> 7) << 4) |
	       ((A_VAL(&(src_ptr[-4])) >> 7) << 3) |
	       ((A_VAL(&(src_ptr[-5])) >> 7) << 2) |
	       ((A_VAL(&(src_ptr[-6])) >> 7) << 1) |
	       ((A_VAL(&(src_ptr[-7])) >> 7) << 0);
	     src_ptr -= 8;
	     dst_ptr++;
	  }
     }
   else
     {
	for (x = 0; x < w; x += 8)
	  {
	     *dst_ptr =
	       ((A_VAL(&(src_ptr[ 0])) >> 7) << 0) |
	       ((A_VAL(&(src_ptr[-1])) >> 7) << 1) |
	       ((A_VAL(&(src_ptr[-2])) >> 7) << 2) |
	       ((A_VAL(&(src_ptr[-3])) >> 7) << 3) |
	       ((A_VAL(&(src_ptr[-4])) >> 7) << 4) |
	       ((A_VAL(&(src_ptr[-5])) >> 7) << 5) |
	       ((A_VAL(&(src_ptr[-6])) >> 7) << 6) |
	       ((A_VAL(&(src_ptr[-7])) >> 7) << 7);
	     src_ptr -= 8;
	     dst_ptr++;
	  }
     }
   w += 7;
   for (; x < w; x ++)
     {
	xcb_image_put_pixel(xcbob->xim, x, y, A_VAL(src_ptr) >> 7);
	src_ptr--;
     }
}

void 
evas_software_xcb_write_mask_line_vert(Outbuf *buf, Xcb_Output_Buffer *xcbob, DATA32 *src, int h, int y, int w) 
{
   int yy, bpl = 0;
   DATA32 *src_ptr;
   DATA8 *dst_ptr;

   src_ptr = src;
   dst_ptr = evas_software_xcb_output_buffer_data(xcbob, &bpl);
   dst_ptr = dst_ptr + (bpl * y);
   h -= 7;
   if (buf->priv.x11.xcb.bit_swap)
     {
	for (yy = 0; yy < h; yy += 8)
	  {
	     *dst_ptr =
	       ((A_VAL(&(src_ptr[0 * w])) >> 7) << 7) |
	       ((A_VAL(&(src_ptr[1 * w])) >> 7) << 6) |
	       ((A_VAL(&(src_ptr[2 * w])) >> 7) << 5) |
	       ((A_VAL(&(src_ptr[3 * w])) >> 7) << 4) |
	       ((A_VAL(&(src_ptr[4 * w])) >> 7) << 3) |
	       ((A_VAL(&(src_ptr[5 * w])) >> 7) << 2) |
	       ((A_VAL(&(src_ptr[6 * w])) >> 7) << 1) |
	       ((A_VAL(&(src_ptr[7 * w])) >> 7) << 0);
	     src_ptr += 8 * w;
	     dst_ptr++;
	  }
     }
   else
     {
	for (yy = 0; yy < h; yy += 8)
	  {
	     *dst_ptr =
	       ((A_VAL(&(src_ptr[0 * w])) >> 7) << 0) |
	       ((A_VAL(&(src_ptr[1 * w])) >> 7) << 1) |
	       ((A_VAL(&(src_ptr[2 * w])) >> 7) << 2) |
	       ((A_VAL(&(src_ptr[3 * w])) >> 7) << 3) |
	       ((A_VAL(&(src_ptr[4 * w])) >> 7) << 4) |
	       ((A_VAL(&(src_ptr[5 * w])) >> 7) << 5) |
	       ((A_VAL(&(src_ptr[6 * w])) >> 7) << 6) |
	       ((A_VAL(&(src_ptr[7 * w])) >> 7) << 7);
	     src_ptr += 8 * w;
	     dst_ptr++;
	  }
     }
   h += 7;
   for (; yy < h; yy ++)
     {
	xcb_image_put_pixel(xcbob->xim, yy, y, A_VAL(src_ptr) >> 7);
	src_ptr += w;
     }
}

void 
evas_software_xcb_write_mask_line_vert_rev(Outbuf *buf, Xcb_Output_Buffer *xcbob, DATA32 *src, int h, int y, int w) 
{
   int yy, bpl = 0;
   DATA32 *src_ptr;
   DATA8 *dst_ptr;

   src_ptr = src + ((h - 1) * w);
   dst_ptr = evas_software_xcb_output_buffer_data(xcbob, &bpl);
   dst_ptr = dst_ptr + (bpl * y);
   h -= 7;
   if (buf->priv.x11.xcb.bit_swap)
     {
	for (yy = 0; yy < h; yy += 8)
	  {
	     *dst_ptr =
	       ((A_VAL(&(src_ptr[ 0 * w])) >> 7) << 7) |
	       ((A_VAL(&(src_ptr[-1 * w])) >> 7) << 6) |
	       ((A_VAL(&(src_ptr[-2 * w])) >> 7) << 5) |
	       ((A_VAL(&(src_ptr[-3 * w])) >> 7) << 4) |
	       ((A_VAL(&(src_ptr[-4 * w])) >> 7) << 3) |
	       ((A_VAL(&(src_ptr[-5 * w])) >> 7) << 2) |
	       ((A_VAL(&(src_ptr[-6 * w])) >> 7) << 1) |
	       ((A_VAL(&(src_ptr[-7 * w])) >> 7) << 0);
	     src_ptr -= 8 * w;
	     dst_ptr++;
	  }
     }
   else
     {
	for (yy = 0; yy < h; yy += 8)
	  {
	     *dst_ptr =
	       ((A_VAL(&(src_ptr[ 0 * w])) >> 7) << 0) |
	       ((A_VAL(&(src_ptr[-1 * w])) >> 7) << 1) |
	       ((A_VAL(&(src_ptr[-2 * w])) >> 7) << 2) |
	       ((A_VAL(&(src_ptr[-3 * w])) >> 7) << 3) |
	       ((A_VAL(&(src_ptr[-4 * w])) >> 7) << 4) |
	       ((A_VAL(&(src_ptr[-5 * w])) >> 7) << 5) |
	       ((A_VAL(&(src_ptr[-6 * w])) >> 7) << 6) |
	       ((A_VAL(&(src_ptr[-7 * w])) >> 7) << 7);
	     src_ptr -= 8 * w;
	     dst_ptr++;
	  }
     }
   h += 7;
   for (; yy < h; yy ++)
     {
	xcb_image_put_pixel(xcbob->xim, yy, y, A_VAL(src_ptr) >> 7);
	src_ptr -= w;
     }
}

Eina_Bool 
evas_software_xcb_can_do_shm(xcb_connection_t *conn, xcb_screen_t *screen) 
{
   const xcb_query_extension_reply_t *reply;
   static xcb_connection_t *cached_conn = NULL;
   static int cached_result = 0;

   if (conn == cached_conn) return cached_result;
   cached_conn = conn;

   reply = xcb_get_extension_data(conn, &xcb_shm_id);
   if ((reply) && (reply->present)) 
     {
        xcb_visualtype_t *visual;
        Xcb_Output_Buffer *xcbob = NULL;

        visual = _xcbob_find_visual_by_id(screen, screen->root_visual);
        xcbob = 
          evas_software_xcb_output_buffer_new(conn, visual, screen->root_depth,
                                              16, 16, 2, NULL);
        if (!xcbob)
          cached_result = 0;
        else 
          {
             evas_software_xcb_output_buffer_free(xcbob, EINA_TRUE);
             cached_result = 1;
          }
     }
   else
     cached_result = 0;

   return cached_result;
}

Xcb_Output_Buffer *
evas_software_xcb_output_buffer_new(xcb_connection_t *conn, xcb_visualtype_t *vis, int depth, int w, int h, int try_shm, unsigned char *data) 
{
   Xcb_Output_Buffer *xcbob = NULL;

   if (!(xcbob = calloc(1, sizeof(Xcb_Output_Buffer))))
     return NULL;

   xcbob->connection = conn;
   xcbob->visual = vis;
   xcbob->xim = NULL;
   xcbob->shm_info = NULL;
   xcbob->w = w;
   xcbob->h = h;

   if (try_shm > 0) 
     {
        xcbob->shm_info = malloc(sizeof(xcb_shm_segment_info_t));
        if (xcbob->shm_info) 
          {
             xcbob->shm_info->shmseg = xcb_generate_id(conn);
             xcbob->xim = 
               _xcbob_create_native(conn, w, h, XCB_IMAGE_FORMAT_Z_PIXMAP, 
                                    depth, NULL, ~0, NULL);
             if (xcbob->xim) 
               {
                  xcbob->shm_info->shmid = 
                    shmget(IPC_PRIVATE, 
                           xcbob->xim->stride * xcbob->xim->height, 
                           (IPC_CREAT | 0777));
                  if (xcbob->shm_info->shmid == (uint32_t)-1) 
                    {
                       xcb_image_destroy(xcbob->xim);
                       free(xcbob->shm_info);
                       free(xcbob);
                       return NULL;
                    }
                  xcbob->shm_info->shmaddr = xcbob->xim->data = 
                    shmat(xcbob->shm_info->shmid, 0, 0);
                  if (xcbob->shm_info->shmaddr != ((void *)-1))
                    {
                       /* Sync only needed for testing */
                       if (try_shm == 2) _xcbob_sync(conn);

#if defined(EVAS_FRAME_QUEUING) && defined(LIBXEXT_VERSION_LOW)
                       if (evas_common_frameq_enabled())
                         xcb_grab_server(conn);
#endif
                       xcb_shm_attach(conn, xcbob->shm_info->shmseg, 
                                      xcbob->shm_info->shmid, 0);
#if defined(EVAS_FRAME_QUEUING) && defined(LIBXEXT_VERSION_LOW)
                       if (evas_common_frameq_enabled()) 
                         xcb_ungrab_server(conn);
#endif
                       if (try_shm == 2) _xcbob_sync(conn);

                       xcbob->bpl = xcbob->xim->stride;
                       xcbob->psize = (xcbob->bpl * xcbob->h);
                       return xcbob;
                    }
                  shmdt(xcbob->shm_info->shmaddr);
                  shmctl(xcbob->shm_info->shmid, IPC_RMID, 0);
               }
             if (xcbob->xim) xcb_image_destroy(xcbob->xim);
             xcbob->xim = NULL;
          }
        if (xcbob->shm_info) free(xcbob->shm_info);
        xcbob->shm_info = NULL;
     }

   if (try_shm > 1) return NULL;

   /* no shm */
   xcbob->xim = 
     _xcbob_create_native(conn, w, h, XCB_IMAGE_FORMAT_Z_PIXMAP, 
                          depth, NULL, ~0, NULL);
   if (!xcbob->xim) 
     {
        free(xcbob);
        return NULL;
     }

   xcbob->data = data;

   if (!xcbob->xim->data) 
     {
        xcbob->xim->data = malloc(xcbob->xim->stride * xcbob->xim->height);
        if (!xcbob->xim->data) 
          {
             xcb_image_destroy(xcbob->xim);
             free(xcbob);
             return NULL;
          }
     }
   xcbob->bpl = xcbob->xim->stride;
   xcbob->psize = (xcbob->bpl * xcbob->h);
   return xcbob;
}

void 
evas_software_xcb_output_buffer_free(Xcb_Output_Buffer *xcbob, Eina_Bool sync) 
{
   if (xcbob->shm_info) 
     {
        if (sync) _xcbob_sync(xcbob->connection);
        xcb_shm_detach(xcbob->connection, xcbob->shm_info->shmseg);
        xcb_image_destroy(xcbob->xim);
        shmdt(xcbob->shm_info->shmaddr);
        shmctl(xcbob->shm_info->shmid, IPC_RMID, 0);
        free(xcbob->shm_info);
     }
   else 
     {
        if (xcbob->data) xcbob->xim->data = NULL;
//        free(xcbob->xim->data);
        xcb_image_destroy(xcbob->xim);
     }
   free(xcbob);
}

void 
evas_software_xcb_output_buffer_paste(Xcb_Output_Buffer *xcbob, xcb_drawable_t drawable, xcb_gcontext_t gc, int x, int y, Eina_Bool sync) 
{
   if (xcbob->shm_info) 
     {
        xcb_image_shm_put(xcbob->connection, drawable, gc, xcbob->xim, 
                          *xcbob->shm_info, 0, 0, x, y, xcbob->w, xcbob->h, 0);
        if (sync) _xcbob_sync(xcbob->connection);
     }
   else 
     xcb_image_put(xcbob->connection, drawable, gc, xcbob->xim, x, y, 0);
}

DATA8 *
evas_software_xcb_output_buffer_data(Xcb_Output_Buffer *xcbob, int *bpl_ret) 
{
   if (bpl_ret) *bpl_ret = xcbob->xim->stride;
   return (DATA8 *)xcbob->xim->data;
}

int 
evas_software_xcb_output_buffer_depth(Xcb_Output_Buffer *xcbob) 
{
   return xcbob->xim->bpp;
}

int 
evas_software_xcb_output_buffer_byte_order(Xcb_Output_Buffer *xcbob) 
{
   return xcbob->xim->byte_order;
}

int 
evas_software_xcb_output_buffer_bit_order(Xcb_Output_Buffer *xcbob) 
{
   return xcbob->xim->bit_order;
}

/* local functions */
static void 
_xcbob_sync(xcb_connection_t *conn) 
{
   free(xcb_get_input_focus_reply(conn, 
                                  xcb_get_input_focus_unchecked(conn), NULL));
}

static xcb_image_t *
_xcbob_create_native(xcb_connection_t *conn, int w, int h, xcb_image_format_t format, uint8_t depth, void *base, uint32_t bytes, uint8_t *data) 
{
   static uint8_t dpth = 0;
   static xcb_format_t *fmt = NULL;
   const xcb_setup_t *setup;
   xcb_image_format_t xif;

   /* NB: We cannot use xcb_image_create_native as it only creates images 
    * using MSB_FIRST, so this routine recreates that function and uses 
    * the endian-ness of the server setup */
   setup = xcb_get_setup(conn);
   xif = format;

   if ((xif == XCB_IMAGE_FORMAT_Z_PIXMAP) && (depth == 1))
     xif = XCB_IMAGE_FORMAT_XY_PIXMAP;

   if (dpth != depth) 
     {
        dpth = depth;
        fmt = _xcbob_find_format(setup, depth);
        if (!fmt) return 0;
     }

   switch (xif) 
     {
      case XCB_IMAGE_FORMAT_XY_BITMAP:
        if (depth != 1) return 0;
      case XCB_IMAGE_FORMAT_XY_PIXMAP:
      case XCB_IMAGE_FORMAT_Z_PIXMAP:
        return xcb_image_create(w, h, xif, 
                                fmt->scanline_pad, 
                                fmt->depth, fmt->bits_per_pixel, 
                                setup->bitmap_format_scanline_unit, 
                                setup->image_byte_order, 
                                setup->bitmap_format_bit_order, 
                                base, bytes, data);
      default:
        break;
     }

   return 0;
}

static xcb_format_t *
_xcbob_find_format(const xcb_setup_t *setup, uint8_t depth) 
{
   xcb_format_t *fmt, *fmtend;

   fmt = xcb_setup_pixmap_formats(setup);
   fmtend = fmt + xcb_setup_pixmap_formats_length(setup);
   for (; fmt != fmtend; ++fmt)
     if (fmt->depth == depth) 
       return fmt;

   return 0;
}

static xcb_visualtype_t *
_xcbob_find_visual_by_id(xcb_screen_t *screen, xcb_visualid_t id) 
{
   xcb_depth_iterator_t diter;
   xcb_visualtype_iterator_t viter;

   diter = xcb_screen_allowed_depths_iterator(screen);
   for (; diter.rem; xcb_depth_next(&diter)) 
     {
        viter = xcb_depth_visuals_iterator(diter.data);
        for (; viter.rem; xcb_visualtype_next(&viter)) 
          {
             if (viter.data->visual_id == id)
               return viter.data;
          }
     }

   return 0;
}