eolian: rename is_ref API to is_ptr to match syntax
[platform/upstream/efl.git] / src / modules / evas / engines / software_x11 / evas_xcb_buffer.c
1 #include "evas_common_private.h"
2 #include "evas_xcb_buffer.h"
3
4 /* local function prototypes */
5 static void _xcbob_sync(xcb_connection_t *conn);
6 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);
7 static xcb_format_t *_xcbob_find_format(const xcb_setup_t *setup, uint8_t depth);
8 static xcb_visualtype_t *_xcbob_find_visual_by_id(xcb_screen_t *screen, xcb_visualid_t id);
9
10 void 
11 evas_software_xcb_write_mask_line(Outbuf *buf, Xcb_Output_Buffer *xcbob, DATA32 *src, int w, int y) 
12 {
13    int x, bpl = 0;
14    DATA32 *src_ptr;
15    DATA8 *dst_ptr;
16
17    src_ptr = src;
18    dst_ptr = evas_software_xcb_output_buffer_data(xcbob, &bpl);
19    dst_ptr = dst_ptr + (bpl * y);
20    w -= 7;
21    if (buf->priv.x11.xcb.bit_swap)
22      {
23         for (x = 0; x < w; x += 8)
24           {
25              *dst_ptr =
26                ((A_VAL(&(src_ptr[0])) >> 7) << 7) |
27                ((A_VAL(&(src_ptr[1])) >> 7) << 6) |
28                ((A_VAL(&(src_ptr[2])) >> 7) << 5) |
29                ((A_VAL(&(src_ptr[3])) >> 7) << 4) |
30                ((A_VAL(&(src_ptr[4])) >> 7) << 3) |
31                ((A_VAL(&(src_ptr[5])) >> 7) << 2) |
32                ((A_VAL(&(src_ptr[6])) >> 7) << 1) |
33                ((A_VAL(&(src_ptr[7])) >> 7) << 0);
34              src_ptr += 8;
35              dst_ptr++;
36           }
37      }
38    else
39      {
40         for (x = 0; x < w; x += 8)
41           {
42              *dst_ptr =
43                ((A_VAL(&(src_ptr[0])) >> 7) << 0) |
44                ((A_VAL(&(src_ptr[1])) >> 7) << 1) |
45                ((A_VAL(&(src_ptr[2])) >> 7) << 2) |
46                ((A_VAL(&(src_ptr[3])) >> 7) << 3) |
47                ((A_VAL(&(src_ptr[4])) >> 7) << 4) |
48                ((A_VAL(&(src_ptr[5])) >> 7) << 5) |
49                ((A_VAL(&(src_ptr[6])) >> 7) << 6) |
50                ((A_VAL(&(src_ptr[7])) >> 7) << 7);
51              src_ptr += 8;
52              dst_ptr++;
53           }
54      }
55    w += 7;
56    for (; x < w; x ++)
57      {
58         xcb_image_put_pixel(xcbob->xim, x, y, A_VAL(src_ptr) >> 7);
59         src_ptr++;
60      }
61 }
62
63 void 
64 evas_software_xcb_write_mask_line_rev(Outbuf *buf, Xcb_Output_Buffer *xcbob, DATA32 *src, int w, int y) 
65 {
66    int x, bpl = 0;
67    DATA32 *src_ptr;
68    DATA8 *dst_ptr;
69
70    src_ptr = src + w - 1;
71    dst_ptr = evas_software_xcb_output_buffer_data(xcbob, &bpl);
72    dst_ptr = dst_ptr + (bpl * y);
73    w -= 7;
74    if (buf->priv.x11.xcb.bit_swap)
75      {
76         for (x = 0; x < w; x += 8)
77           {
78              *dst_ptr =
79                ((A_VAL(&(src_ptr[ 0])) >> 7) << 7) |
80                ((A_VAL(&(src_ptr[-1])) >> 7) << 6) |
81                ((A_VAL(&(src_ptr[-2])) >> 7) << 5) |
82                ((A_VAL(&(src_ptr[-3])) >> 7) << 4) |
83                ((A_VAL(&(src_ptr[-4])) >> 7) << 3) |
84                ((A_VAL(&(src_ptr[-5])) >> 7) << 2) |
85                ((A_VAL(&(src_ptr[-6])) >> 7) << 1) |
86                ((A_VAL(&(src_ptr[-7])) >> 7) << 0);
87              src_ptr -= 8;
88              dst_ptr++;
89           }
90      }
91    else
92      {
93         for (x = 0; x < w; x += 8)
94           {
95              *dst_ptr =
96                ((A_VAL(&(src_ptr[ 0])) >> 7) << 0) |
97                ((A_VAL(&(src_ptr[-1])) >> 7) << 1) |
98                ((A_VAL(&(src_ptr[-2])) >> 7) << 2) |
99                ((A_VAL(&(src_ptr[-3])) >> 7) << 3) |
100                ((A_VAL(&(src_ptr[-4])) >> 7) << 4) |
101                ((A_VAL(&(src_ptr[-5])) >> 7) << 5) |
102                ((A_VAL(&(src_ptr[-6])) >> 7) << 6) |
103                ((A_VAL(&(src_ptr[-7])) >> 7) << 7);
104              src_ptr -= 8;
105              dst_ptr++;
106           }
107      }
108    w += 7;
109    for (; x < w; x ++)
110      {
111         xcb_image_put_pixel(xcbob->xim, x, y, A_VAL(src_ptr) >> 7);
112         src_ptr--;
113      }
114 }
115
116 void 
117 evas_software_xcb_write_mask_line_vert(Outbuf *buf, Xcb_Output_Buffer *xcbob, DATA32 *src, int h, int y, int w) 
118 {
119    int yy, bpl = 0;
120    DATA32 *src_ptr;
121    DATA8 *dst_ptr;
122
123    src_ptr = src;
124    dst_ptr = evas_software_xcb_output_buffer_data(xcbob, &bpl);
125    dst_ptr = dst_ptr + (bpl * y);
126    h -= 7;
127    if (buf->priv.x11.xcb.bit_swap)
128      {
129         for (yy = 0; yy < h; yy += 8)
130           {
131              *dst_ptr =
132                ((A_VAL(&(src_ptr[0 * w])) >> 7) << 7) |
133                ((A_VAL(&(src_ptr[1 * w])) >> 7) << 6) |
134                ((A_VAL(&(src_ptr[2 * w])) >> 7) << 5) |
135                ((A_VAL(&(src_ptr[3 * w])) >> 7) << 4) |
136                ((A_VAL(&(src_ptr[4 * w])) >> 7) << 3) |
137                ((A_VAL(&(src_ptr[5 * w])) >> 7) << 2) |
138                ((A_VAL(&(src_ptr[6 * w])) >> 7) << 1) |
139                ((A_VAL(&(src_ptr[7 * w])) >> 7) << 0);
140              src_ptr += 8 * w;
141              dst_ptr++;
142           }
143      }
144    else
145      {
146         for (yy = 0; yy < h; yy += 8)
147           {
148              *dst_ptr =
149                ((A_VAL(&(src_ptr[0 * w])) >> 7) << 0) |
150                ((A_VAL(&(src_ptr[1 * w])) >> 7) << 1) |
151                ((A_VAL(&(src_ptr[2 * w])) >> 7) << 2) |
152                ((A_VAL(&(src_ptr[3 * w])) >> 7) << 3) |
153                ((A_VAL(&(src_ptr[4 * w])) >> 7) << 4) |
154                ((A_VAL(&(src_ptr[5 * w])) >> 7) << 5) |
155                ((A_VAL(&(src_ptr[6 * w])) >> 7) << 6) |
156                ((A_VAL(&(src_ptr[7 * w])) >> 7) << 7);
157              src_ptr += 8 * w;
158              dst_ptr++;
159           }
160      }
161    h += 7;
162    for (; yy < h; yy ++)
163      {
164         xcb_image_put_pixel(xcbob->xim, yy, y, A_VAL(src_ptr) >> 7);
165         src_ptr += w;
166      }
167 }
168
169 void 
170 evas_software_xcb_write_mask_line_vert_rev(Outbuf *buf, Xcb_Output_Buffer *xcbob, DATA32 *src, int h, int y, int w) 
171 {
172    int yy, bpl = 0;
173    DATA32 *src_ptr;
174    DATA8 *dst_ptr;
175
176    src_ptr = src + ((h - 1) * w);
177    dst_ptr = evas_software_xcb_output_buffer_data(xcbob, &bpl);
178    dst_ptr = dst_ptr + (bpl * y);
179    h -= 7;
180    if (buf->priv.x11.xcb.bit_swap)
181      {
182         for (yy = 0; yy < h; yy += 8)
183           {
184              *dst_ptr =
185                ((A_VAL(&(src_ptr[ 0 * w])) >> 7) << 7) |
186                ((A_VAL(&(src_ptr[-1 * w])) >> 7) << 6) |
187                ((A_VAL(&(src_ptr[-2 * w])) >> 7) << 5) |
188                ((A_VAL(&(src_ptr[-3 * w])) >> 7) << 4) |
189                ((A_VAL(&(src_ptr[-4 * w])) >> 7) << 3) |
190                ((A_VAL(&(src_ptr[-5 * w])) >> 7) << 2) |
191                ((A_VAL(&(src_ptr[-6 * w])) >> 7) << 1) |
192                ((A_VAL(&(src_ptr[-7 * w])) >> 7) << 0);
193              src_ptr -= 8 * w;
194              dst_ptr++;
195           }
196      }
197    else
198      {
199         for (yy = 0; yy < h; yy += 8)
200           {
201              *dst_ptr =
202                ((A_VAL(&(src_ptr[ 0 * w])) >> 7) << 0) |
203                ((A_VAL(&(src_ptr[-1 * w])) >> 7) << 1) |
204                ((A_VAL(&(src_ptr[-2 * w])) >> 7) << 2) |
205                ((A_VAL(&(src_ptr[-3 * w])) >> 7) << 3) |
206                ((A_VAL(&(src_ptr[-4 * w])) >> 7) << 4) |
207                ((A_VAL(&(src_ptr[-5 * w])) >> 7) << 5) |
208                ((A_VAL(&(src_ptr[-6 * w])) >> 7) << 6) |
209                ((A_VAL(&(src_ptr[-7 * w])) >> 7) << 7);
210              src_ptr -= 8 * w;
211              dst_ptr++;
212           }
213      }
214    h += 7;
215    for (; yy < h; yy ++)
216      {
217         xcb_image_put_pixel(xcbob->xim, yy, y, A_VAL(src_ptr) >> 7);
218         src_ptr -= w;
219      }
220 }
221
222 Eina_Bool 
223 evas_software_xcb_can_do_shm(xcb_connection_t *conn, xcb_screen_t *screen) 
224 {
225    const xcb_query_extension_reply_t *reply;
226    static xcb_connection_t *cached_conn = NULL;
227    static int cached_result = 0;
228
229    if (conn == cached_conn) return cached_result;
230    cached_conn = conn;
231
232    reply = xcb_get_extension_data(conn, &xcb_shm_id);
233    if ((reply) && (reply->present)) 
234      {
235         xcb_visualtype_t *visual;
236         Xcb_Output_Buffer *xcbob = NULL;
237
238         visual = _xcbob_find_visual_by_id(screen, screen->root_visual);
239         xcbob = 
240           evas_software_xcb_output_buffer_new(conn, visual, screen->root_depth,
241                                               16, 16, 2, NULL);
242         if (!xcbob)
243           cached_result = 0;
244         else 
245           {
246              evas_software_xcb_output_buffer_unref(xcbob, EINA_TRUE);
247              cached_result = 1;
248           }
249      }
250    else
251      cached_result = 0;
252
253    return cached_result;
254 }
255
256 Xcb_Output_Buffer *
257 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) 
258 {
259    Xcb_Output_Buffer *xcbob = NULL;
260
261    if (!(xcbob = calloc(1, sizeof(Xcb_Output_Buffer))))
262      return NULL;
263
264    xcbob->connection = conn;
265    xcbob->visual = vis;
266    xcbob->xim = NULL;
267    xcbob->shm_info = NULL;
268    xcbob->w = w;
269    xcbob->h = h;
270    xcbob->refcount = 1;
271
272    if (try_shm > 0) 
273      {
274         xcbob->shm_info = malloc(sizeof(xcb_shm_segment_info_t));
275         if (xcbob->shm_info) 
276           {
277              xcbob->shm_info->shmseg = xcb_generate_id(conn);
278              xcbob->xim = 
279                _xcbob_create_native(conn, w, h, XCB_IMAGE_FORMAT_Z_PIXMAP, 
280                                     depth, NULL, ~0, NULL);
281              if (xcbob->xim) 
282                {
283                   xcbob->shm_info->shmid = 
284                     shmget(IPC_PRIVATE, 
285                            xcbob->xim->stride * xcbob->xim->height, 
286                            (IPC_CREAT | 0600));
287                   if (xcbob->shm_info->shmid == (uint32_t)-1) 
288                     {
289                        xcb_image_destroy(xcbob->xim);
290                        free(xcbob->shm_info);
291                        free(xcbob);
292                        return NULL;
293                     }
294                   xcbob->shm_info->shmaddr = xcbob->xim->data = 
295                     shmat(xcbob->shm_info->shmid, 0, 0);
296                   if (xcbob->shm_info->shmaddr != ((void *)-1))
297                     {
298                        /* Sync only needed for testing */
299                        if (try_shm == 2) _xcbob_sync(conn);
300
301 #if defined(LIBXEXT_VERSION_LOW)
302                        if (evas_common_frameq_enabled())
303                          xcb_grab_server(conn);
304 #endif
305                        xcb_shm_attach(conn, xcbob->shm_info->shmseg, 
306                                       xcbob->shm_info->shmid, 0);
307 #if defined(LIBXEXT_VERSION_LOW)
308                        if (evas_common_frameq_enabled()) 
309                          xcb_ungrab_server(conn);
310 #endif
311                        if (try_shm == 2) _xcbob_sync(conn);
312
313                        xcbob->bpl = xcbob->xim->stride;
314                        xcbob->psize = (xcbob->bpl * xcbob->h);
315                        return xcbob;
316                     }
317                   shmdt(xcbob->shm_info->shmaddr);
318                   shmctl(xcbob->shm_info->shmid, IPC_RMID, 0);
319                }
320              if (xcbob->xim) xcb_image_destroy(xcbob->xim);
321              xcbob->xim = NULL;
322           }
323         if (xcbob->shm_info) free(xcbob->shm_info);
324         xcbob->shm_info = NULL;
325      }
326
327    if (try_shm > 1) return NULL;
328
329    /* no shm */
330    xcbob->xim = 
331      _xcbob_create_native(conn, w, h, XCB_IMAGE_FORMAT_Z_PIXMAP, 
332                           depth, NULL, ~0, NULL);
333    if (!xcbob->xim) 
334      {
335         free(xcbob);
336         return NULL;
337      }
338
339    xcbob->data = data;
340
341    if (!xcbob->xim->data) 
342      {
343         xcbob->xim->data = malloc(xcbob->xim->stride * xcbob->xim->height);
344         if (!xcbob->xim->data) 
345           {
346              xcb_image_destroy(xcbob->xim);
347              free(xcbob);
348              return NULL;
349           }
350      }
351    xcbob->bpl = xcbob->xim->stride;
352    xcbob->psize = (xcbob->bpl * xcbob->h);
353    return xcbob;
354 }
355
356 Xcb_Output_Buffer *
357 evas_software_xcb_output_buffer_ref(Xcb_Output_Buffer *xcbob)
358 {
359    if (xcbob->refcount == UINT_MAX)
360      return NULL;
361    xcbob->refcount++;
362    return xcbob;
363 }
364
365 void 
366 evas_software_xcb_output_buffer_unref(Xcb_Output_Buffer *xcbob, Eina_Bool sync) 
367 {
368    if (!xcbob->refcount)
369      return;
370    xcbob->refcount--;
371    if (xcbob->refcount)
372      return;
373    if (xcbob->shm_info) 
374      {
375         if (sync) _xcbob_sync(xcbob->connection);
376         xcb_shm_detach(xcbob->connection, xcbob->shm_info->shmseg);
377         xcb_image_destroy(xcbob->xim);
378         shmdt(xcbob->shm_info->shmaddr);
379         shmctl(xcbob->shm_info->shmid, IPC_RMID, 0);
380         free(xcbob->shm_info);
381      }
382    else 
383      {
384         if (xcbob->data) xcbob->xim->data = NULL;
385 //        free(xcbob->xim->data);
386         xcb_image_destroy(xcbob->xim);
387      }
388    free(xcbob);
389 }
390
391 void 
392 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) 
393 {
394    if (xcbob->shm_info) 
395      {
396         xcb_image_shm_put(xcbob->connection, drawable, gc, xcbob->xim, 
397                           *xcbob->shm_info, 0, 0, x, y, xcbob->w, xcbob->h, 0);
398         if (sync) _xcbob_sync(xcbob->connection);
399      }
400    else 
401      xcb_image_put(xcbob->connection, drawable, gc, xcbob->xim, x, y, 0);
402 }
403
404 DATA8 *
405 evas_software_xcb_output_buffer_data(Xcb_Output_Buffer *xcbob, int *bpl_ret) 
406 {
407    if (bpl_ret) *bpl_ret = xcbob->xim->stride;
408    return (DATA8 *)xcbob->xim->data;
409 }
410
411 int 
412 evas_software_xcb_output_buffer_depth(Xcb_Output_Buffer *xcbob) 
413 {
414    return xcbob->xim->bpp;
415 }
416
417 int 
418 evas_software_xcb_output_buffer_byte_order(Xcb_Output_Buffer *xcbob) 
419 {
420    return xcbob->xim->byte_order;
421 }
422
423 int 
424 evas_software_xcb_output_buffer_bit_order(Xcb_Output_Buffer *xcbob) 
425 {
426    return xcbob->xim->bit_order;
427 }
428
429 /* local functions */
430 static void 
431 _xcbob_sync(xcb_connection_t *conn) 
432 {
433    free(xcb_get_input_focus_reply(conn, 
434                                   xcb_get_input_focus_unchecked(conn), NULL));
435 }
436
437 static xcb_image_t *
438 _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) 
439 {
440    static uint8_t dpth = 0;
441    static xcb_format_t *fmt = NULL;
442    const xcb_setup_t *setup;
443    xcb_image_format_t xif;
444
445    /* NB: We cannot use xcb_image_create_native as it only creates images 
446     * using MSB_FIRST, so this routine recreates that function and uses 
447     * the endian-ness of the server setup */
448    setup = xcb_get_setup(conn);
449    xif = format;
450
451    if ((xif == XCB_IMAGE_FORMAT_Z_PIXMAP) && (depth == 1))
452      xif = XCB_IMAGE_FORMAT_XY_PIXMAP;
453
454    if (dpth != depth) 
455      {
456         dpth = depth;
457         fmt = _xcbob_find_format(setup, depth);
458         if (!fmt) return 0;
459      }
460
461    switch (xif) 
462      {
463       case XCB_IMAGE_FORMAT_XY_BITMAP:
464         if (depth != 1) return 0;
465       case XCB_IMAGE_FORMAT_XY_PIXMAP:
466       case XCB_IMAGE_FORMAT_Z_PIXMAP:
467         return xcb_image_create(w, h, xif, 
468                                 fmt->scanline_pad, 
469                                 fmt->depth, fmt->bits_per_pixel, 
470                                 setup->bitmap_format_scanline_unit, 
471                                 setup->image_byte_order, 
472                                 setup->bitmap_format_bit_order, 
473                                 base, bytes, data);
474       default:
475         break;
476      }
477
478    return 0;
479 }
480
481 static xcb_format_t *
482 _xcbob_find_format(const xcb_setup_t *setup, uint8_t depth) 
483 {
484    xcb_format_t *fmt, *fmtend;
485
486    fmt = xcb_setup_pixmap_formats(setup);
487    fmtend = fmt + xcb_setup_pixmap_formats_length(setup);
488    for (; fmt != fmtend; ++fmt)
489      if (fmt->depth == depth) 
490        return fmt;
491
492    return 0;
493 }
494
495 static xcb_visualtype_t *
496 _xcbob_find_visual_by_id(xcb_screen_t *screen, xcb_visualid_t id) 
497 {
498    xcb_depth_iterator_t diter;
499    xcb_visualtype_iterator_t viter;
500
501    diter = xcb_screen_allowed_depths_iterator(screen);
502    for (; diter.rem; xcb_depth_next(&diter)) 
503      {
504         viter = xcb_depth_visuals_iterator(diter.data);
505         for (; viter.rem; xcb_visualtype_next(&viter)) 
506           {
507              if (viter.data->visual_id == id)
508                return viter.data;
509           }
510      }
511
512    return 0;
513 }