1 #include "evas_common.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 static xcb_connection_t *cached_conn = NULL;
226 static int cached_result = 0;
227 Xcb_Output_Buffer *xcbob = NULL;
228 xcb_visualtype_t *visual;
230 if (conn == cached_conn) return cached_result;
233 visual = _xcbob_find_visual_by_id(screen, screen->root_visual);
236 evas_software_xcb_output_buffer_new(conn, visual, screen->root_depth,
237 1, 1, EINA_TRUE, NULL);
242 evas_software_xcb_output_buffer_free(xcbob, EINA_TRUE);
246 return cached_result;
250 evas_software_xcb_output_buffer_new(xcb_connection_t *conn, xcb_visualtype_t *vis, int depth, int w, int h, Eina_Bool try_shm, unsigned char *data)
252 Xcb_Output_Buffer *xcbob = NULL;
254 if (!(xcbob = calloc(1, sizeof(Xcb_Output_Buffer))))
257 xcbob->connection = conn;
260 xcbob->shm_info = NULL;
267 xcbob->shm_info = malloc(sizeof(xcb_shm_segment_info_t));
270 xcbob->shm_info->shmseg = xcb_generate_id(conn);
272 _xcbob_create_native(conn, w, h, XCB_IMAGE_FORMAT_Z_PIXMAP,
273 depth, NULL, ~0, NULL);
276 xcbob->shm_info->shmid =
277 shmget(IPC_PRIVATE, xcbob->xim->size, (IPC_CREAT | 0666));
278 if (xcbob->shm_info->shmid == (uint32_t)-1)
280 xcb_image_destroy(xcbob->xim);
281 free(xcbob->shm_info);
285 xcbob->shm_info->shmaddr =
286 shmat(xcbob->shm_info->shmid, 0, 0);
287 if (xcbob->shm_info->shmaddr != ((void *)-1))
289 xcbob->xim->data = xcbob->shm_info->shmaddr;
290 xcb_shm_attach(conn, xcbob->shm_info->shmseg,
291 xcbob->shm_info->shmid, 0);
292 xcbob->bpl = xcbob->xim->stride;
293 xcbob->psize = (xcbob->bpl * xcbob->h);
298 shmctl(xcbob->shm_info->shmid, IPC_RMID, 0);
299 xcb_image_destroy(xcbob->xim);
300 free(xcbob->shm_info);
307 free(xcbob->shm_info);
322 _xcbob_create_native(conn, w, h, XCB_IMAGE_FORMAT_Z_PIXMAP,
323 depth, NULL, ~0, NULL);
329 if (!xcbob->xim->data)
331 xcbob->xim->data = malloc(xcbob->xim->size);
332 if (!xcbob->xim->data)
334 xcb_image_destroy(xcbob->xim);
339 xcbob->bpl = xcbob->xim->stride;
340 xcbob->psize = xcbob->xim->size;
346 evas_software_xcb_output_buffer_free(Xcb_Output_Buffer *xcbob, Eina_Bool sync)
350 if (sync) _xcbob_sync(xcbob->connection);
351 xcb_shm_detach(xcbob->connection, xcbob->shm_info->shmseg);
352 xcb_image_destroy(xcbob->xim);
353 shmdt(xcbob->shm_info->shmaddr);
354 shmctl(xcbob->shm_info->shmid, IPC_RMID, 0);
355 free(xcbob->shm_info);
359 if (xcbob->data) xcbob->xim->data = NULL;
360 free(xcbob->xim->data);
361 xcb_image_destroy(xcbob->xim);
367 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)
371 xcb_image_shm_put(xcbob->connection, drawable, gc, xcbob->xim,
372 *xcbob->shm_info, 0, 0, x, y, xcbob->w, xcbob->h, 0);
373 if (sync) _xcbob_sync(xcbob->connection);
376 xcb_image_put(xcbob->connection, drawable, gc, xcbob->xim, x, y, 0);
380 evas_software_xcb_output_buffer_data(Xcb_Output_Buffer *xcbob, int *bpl_ret)
382 if (bpl_ret) *bpl_ret = xcbob->xim->stride;
383 return xcbob->xim->data;
387 evas_software_xcb_output_buffer_depth(Xcb_Output_Buffer *xcbob)
389 return xcbob->xim->bpp;
393 evas_software_xcb_output_buffer_byte_order(Xcb_Output_Buffer *xcbob)
395 return xcbob->xim->byte_order;
399 evas_software_xcb_output_buffer_bit_order(Xcb_Output_Buffer *xcbob)
401 return xcbob->xim->bit_order;
404 /* local functions */
406 _xcbob_sync(xcb_connection_t *conn)
408 free(xcb_get_input_focus_reply(conn,
409 xcb_get_input_focus_unchecked(conn), NULL));
413 _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)
415 const xcb_setup_t *setup;
416 xcb_format_t *fmt = NULL;
417 xcb_image_format_t xif;
419 /* NB: We cannot use xcb_image_create_native as it only creates images
420 * using MSB_FIRST, so this routine recreates that function and checks
421 * endian-ness correctly */
422 setup = xcb_get_setup(conn);
425 if ((xif == XCB_IMAGE_FORMAT_Z_PIXMAP) && (depth == 1))
426 xif = XCB_IMAGE_FORMAT_XY_PIXMAP;
428 fmt = _xcbob_find_format(setup, depth);
433 case XCB_IMAGE_FORMAT_XY_BITMAP:
434 if (depth != 1) return 0;
435 case XCB_IMAGE_FORMAT_XY_PIXMAP:
436 return xcb_image_create(w, h, xif,
438 fmt->depth, fmt->bits_per_pixel,
439 setup->bitmap_format_scanline_unit,
440 setup->image_byte_order,
441 setup->bitmap_format_bit_order,
443 case XCB_IMAGE_FORMAT_Z_PIXMAP:
444 return xcb_image_create(w, h, xif,
446 fmt->depth, fmt->bits_per_pixel,
447 setup->bitmap_format_scanline_unit,
448 setup->image_byte_order,
449 setup->bitmap_format_bit_order,
458 static xcb_format_t *
459 _xcbob_find_format(const xcb_setup_t *setup, uint8_t depth)
461 xcb_format_t *fmt, *fmtend;
463 fmt = xcb_setup_pixmap_formats(setup);
464 fmtend = fmt + xcb_setup_pixmap_formats_length(setup);
465 for (; fmt != fmtend; ++fmt)
466 if (fmt->depth == depth)
472 static xcb_visualtype_t *
473 _xcbob_find_visual_by_id(xcb_screen_t *screen, xcb_visualid_t id)
475 xcb_depth_iterator_t diter;
476 xcb_visualtype_iterator_t viter;
478 diter = xcb_screen_allowed_depths_iterator(screen);
479 for (; diter.rem; xcb_depth_next(&diter))
481 viter = xcb_depth_visuals_iterator(diter.data);
482 for (; viter.rem; xcb_visualtype_next(&viter))
484 if (viter.data->visual_id == id)