1 #include "evas_common_private.h"
2 #include "evas_xcb_buffer.h"
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);
11 evas_software_xcb_write_mask_line(Outbuf *buf, Xcb_Output_Buffer *xcbob, DATA32 *src, int w, int y)
18 dst_ptr = evas_software_xcb_output_buffer_data(xcbob, &bpl);
19 dst_ptr = dst_ptr + (bpl * y);
21 if (buf->priv.x11.xcb.bit_swap)
23 for (x = 0; x < w; x += 8)
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);
40 for (x = 0; x < w; x += 8)
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);
58 xcb_image_put_pixel(xcbob->xim, x, y, A_VAL(src_ptr) >> 7);
64 evas_software_xcb_write_mask_line_rev(Outbuf *buf, Xcb_Output_Buffer *xcbob, DATA32 *src, int w, int y)
70 src_ptr = src + w - 1;
71 dst_ptr = evas_software_xcb_output_buffer_data(xcbob, &bpl);
72 dst_ptr = dst_ptr + (bpl * y);
74 if (buf->priv.x11.xcb.bit_swap)
76 for (x = 0; x < w; x += 8)
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);
93 for (x = 0; x < w; x += 8)
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);
111 xcb_image_put_pixel(xcbob->xim, x, y, A_VAL(src_ptr) >> 7);
117 evas_software_xcb_write_mask_line_vert(Outbuf *buf, Xcb_Output_Buffer *xcbob, DATA32 *src, int h, int y, int w)
124 dst_ptr = evas_software_xcb_output_buffer_data(xcbob, &bpl);
125 dst_ptr = dst_ptr + (bpl * y);
127 if (buf->priv.x11.xcb.bit_swap)
129 for (yy = 0; yy < h; yy += 8)
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);
146 for (yy = 0; yy < h; yy += 8)
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);
162 for (; yy < h; yy ++)
164 xcb_image_put_pixel(xcbob->xim, yy, y, A_VAL(src_ptr) >> 7);
170 evas_software_xcb_write_mask_line_vert_rev(Outbuf *buf, Xcb_Output_Buffer *xcbob, DATA32 *src, int h, int y, int w)
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);
180 if (buf->priv.x11.xcb.bit_swap)
182 for (yy = 0; yy < h; yy += 8)
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);
199 for (yy = 0; yy < h; yy += 8)
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);
215 for (; yy < h; yy ++)
217 xcb_image_put_pixel(xcbob->xim, yy, y, A_VAL(src_ptr) >> 7);
223 evas_software_xcb_can_do_shm(xcb_connection_t *conn, xcb_screen_t *screen)
225 const xcb_query_extension_reply_t *reply;
226 static xcb_connection_t *cached_conn = NULL;
227 static int cached_result = 0;
229 if (conn == cached_conn) return cached_result;
232 reply = xcb_get_extension_data(conn, &xcb_shm_id);
233 if ((reply) && (reply->present))
235 xcb_visualtype_t *visual;
236 Xcb_Output_Buffer *xcbob = NULL;
238 visual = _xcbob_find_visual_by_id(screen, screen->root_visual);
240 evas_software_xcb_output_buffer_new(conn, visual, screen->root_depth,
246 evas_software_xcb_output_buffer_unref(xcbob, EINA_TRUE);
253 return cached_result;
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)
259 Xcb_Output_Buffer *xcbob = NULL;
261 if (!(xcbob = calloc(1, sizeof(Xcb_Output_Buffer))))
264 xcbob->connection = conn;
267 xcbob->shm_info = NULL;
274 xcbob->shm_info = malloc(sizeof(xcb_shm_segment_info_t));
277 xcbob->shm_info->shmseg = xcb_generate_id(conn);
279 _xcbob_create_native(conn, w, h, XCB_IMAGE_FORMAT_Z_PIXMAP,
280 depth, NULL, ~0, NULL);
283 xcbob->shm_info->shmid =
285 xcbob->xim->stride * xcbob->xim->height,
287 if (xcbob->shm_info->shmid == (uint32_t)-1)
289 xcb_image_destroy(xcbob->xim);
290 free(xcbob->shm_info);
294 xcbob->shm_info->shmaddr = xcbob->xim->data =
295 shmat(xcbob->shm_info->shmid, 0, 0);
296 if (xcbob->shm_info->shmaddr != ((void *)-1))
298 /* Sync only needed for testing */
299 if (try_shm == 2) _xcbob_sync(conn);
301 #if defined(LIBXEXT_VERSION_LOW)
302 if (evas_common_frameq_enabled())
303 xcb_grab_server(conn);
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);
311 if (try_shm == 2) _xcbob_sync(conn);
313 xcbob->bpl = xcbob->xim->stride;
314 xcbob->psize = (xcbob->bpl * xcbob->h);
317 shmdt(xcbob->shm_info->shmaddr);
318 shmctl(xcbob->shm_info->shmid, IPC_RMID, 0);
320 if (xcbob->xim) xcb_image_destroy(xcbob->xim);
323 if (xcbob->shm_info) free(xcbob->shm_info);
324 xcbob->shm_info = NULL;
327 if (try_shm > 1) return NULL;
331 _xcbob_create_native(conn, w, h, XCB_IMAGE_FORMAT_Z_PIXMAP,
332 depth, NULL, ~0, NULL);
341 if (!xcbob->xim->data)
343 xcbob->xim->data = malloc(xcbob->xim->stride * xcbob->xim->height);
344 if (!xcbob->xim->data)
346 xcb_image_destroy(xcbob->xim);
351 xcbob->bpl = xcbob->xim->stride;
352 xcbob->psize = (xcbob->bpl * xcbob->h);
357 evas_software_xcb_output_buffer_ref(Xcb_Output_Buffer *xcbob)
359 if (xcbob->refcount == UINT_MAX)
366 evas_software_xcb_output_buffer_unref(Xcb_Output_Buffer *xcbob, Eina_Bool sync)
368 if (!xcbob->refcount)
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);
384 if (xcbob->data) xcbob->xim->data = NULL;
385 // free(xcbob->xim->data);
386 xcb_image_destroy(xcbob->xim);
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)
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);
401 xcb_image_put(xcbob->connection, drawable, gc, xcbob->xim, x, y, 0);
405 evas_software_xcb_output_buffer_data(Xcb_Output_Buffer *xcbob, int *bpl_ret)
407 if (bpl_ret) *bpl_ret = xcbob->xim->stride;
408 return (DATA8 *)xcbob->xim->data;
412 evas_software_xcb_output_buffer_depth(Xcb_Output_Buffer *xcbob)
414 return xcbob->xim->bpp;
418 evas_software_xcb_output_buffer_byte_order(Xcb_Output_Buffer *xcbob)
420 return xcbob->xim->byte_order;
424 evas_software_xcb_output_buffer_bit_order(Xcb_Output_Buffer *xcbob)
426 return xcbob->xim->bit_order;
429 /* local functions */
431 _xcbob_sync(xcb_connection_t *conn)
433 free(xcb_get_input_focus_reply(conn,
434 xcb_get_input_focus_unchecked(conn), NULL));
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)
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;
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);
451 if ((xif == XCB_IMAGE_FORMAT_Z_PIXMAP) && (depth == 1))
452 xif = XCB_IMAGE_FORMAT_XY_PIXMAP;
457 fmt = _xcbob_find_format(setup, depth);
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,
469 fmt->depth, fmt->bits_per_pixel,
470 setup->bitmap_format_scanline_unit,
471 setup->image_byte_order,
472 setup->bitmap_format_bit_order,
481 static xcb_format_t *
482 _xcbob_find_format(const xcb_setup_t *setup, uint8_t depth)
484 xcb_format_t *fmt, *fmtend;
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)
495 static xcb_visualtype_t *
496 _xcbob_find_visual_by_id(xcb_screen_t *screen, xcb_visualid_t id)
498 xcb_depth_iterator_t diter;
499 xcb_visualtype_iterator_t viter;
501 diter = xcb_screen_allowed_depths_iterator(screen);
502 for (; diter.rem; xcb_depth_next(&diter))
504 viter = xcb_depth_visuals_iterator(diter.data);
505 for (; viter.rem; xcb_visualtype_next(&viter))
507 if (viter.data->visual_id == id)