1 #include "evas_common.h"
2 #include "evas_engine.h"
6 typedef struct _Convert_Pal_Priv Convert_Pal_Priv;
8 struct _Convert_Pal_Priv
10 xcb_connection_t *conn;
12 xcb_visualtype_t *vis;
15 typedef DATA8 * (*Xcb_Func_Alloc_Colors) (xcb_connection_t *conn, xcb_colormap_t cmap, xcb_visualtype_t *v);
17 static Xcb_Func_Alloc_Colors x_color_alloc[PAL_MODE_LAST + 1];
18 static int x_color_count[PAL_MODE_LAST + 1];
19 static Evas_List *palettes = NULL;
21 static DATA8 * x_color_alloc_rgb(int nr, int ng, int nb, xcb_connection_t *conn, xcb_colormap_t cmap, xcb_visualtype_t *v);
22 static DATA8 * x_color_alloc_gray(int ng, xcb_connection_t *conn, xcb_colormap_t cmap, xcb_visualtype_t *v);
24 static DATA8 * x_color_alloc_rgb_332 (xcb_connection_t *conn, xcb_colormap_t cmap, xcb_visualtype_t *v);
25 static DATA8 * x_color_alloc_rgb_666 (xcb_connection_t *conn, xcb_colormap_t cmap, xcb_visualtype_t *v);
26 static DATA8 * x_color_alloc_rgb_232 (xcb_connection_t *conn, xcb_colormap_t cmap, xcb_visualtype_t *v);
27 static DATA8 * x_color_alloc_rgb_222 (xcb_connection_t *conn, xcb_colormap_t cmap, xcb_visualtype_t *v);
28 static DATA8 * x_color_alloc_rgb_221 (xcb_connection_t *conn, xcb_colormap_t cmap, xcb_visualtype_t *v);
29 static DATA8 * x_color_alloc_rgb_121 (xcb_connection_t *conn, xcb_colormap_t cmap, xcb_visualtype_t *v);
30 static DATA8 * x_color_alloc_rgb_111 (xcb_connection_t *conn, xcb_colormap_t cmap, xcb_visualtype_t *v);
31 static DATA8 * x_color_alloc_gray_256 (xcb_connection_t *conn, xcb_colormap_t cmap, xcb_visualtype_t *v);
32 static DATA8 * x_color_alloc_gray_64 (xcb_connection_t *conn, xcb_colormap_t cmap, xcb_visualtype_t *v);
33 static DATA8 * x_color_alloc_gray_16 (xcb_connection_t *conn, xcb_colormap_t cmap, xcb_visualtype_t *v);
34 static DATA8 * x_color_alloc_gray_4 (xcb_connection_t *conn, xcb_colormap_t cmap, xcb_visualtype_t *v);
35 static DATA8 * x_color_alloc_mono (xcb_connection_t *conn, xcb_colormap_t cmap, xcb_visualtype_t *v);
38 x_color_alloc_rgb(int nr,
41 xcb_connection_t *conn,
50 for (i = 0; i < v->bits_per_rgb_value; i++) sig_mask |= (0x1 << i);
51 sig_mask <<= (16 - v->bits_per_rgb_value);
53 color_lut = malloc((nr) * (ng) * (nb));
54 if (!color_lut) return NULL;
56 /* FIXME: remove the round-trip ? */
57 for (r = 0; r < (nr); r++)
59 for (g = 0; g < (ng); g++)
61 for (b = 0; b < (nb); b++)
64 xcb_coloritem_t xcl_in;
65 xcb_alloc_color_reply_t *rep;
69 val = (int)((((double)r) / ((nr) - 1)) * 255);
70 val = (val << 8) | val;
71 xcl.red = (uint16_t)(val);
72 val = (int)((((double)g) / ((ng) - 1)) * 255);
73 val = (val << 8) | val;
74 xcl.green = (uint16_t)(val);
75 val = (int)((((double)b) / ((nb) - 1)) * 255);
76 val = (val << 8) | val;
77 xcl.blue = (uint16_t)(val);
79 rep = xcb_alloc_color_reply(conn,
80 xcb_alloc_color_unchecked(conn,
86 dr = (int)xcl_in.red - (int)xcl.red;
88 dg = (int)xcl_in.green - (int)xcl.green;
90 db = (int)xcl_in.blue - (int)xcl.blue;
93 printf("ASK [%i]: %04x %04x %04x = %04x %04x %04x | dif = %04x / %04x\n",
95 xcl_in.red, xcl_in.green, xcl_in.blue,
96 xcl.red, xcl.green, xcl.blue,
100 /* TODO: XAllocColor tries to approach the color */
101 /* in case the allocation fails */
102 /* XCB does not that (i think). It should be done */
103 /* So if rep == NULL, the other following tests */
104 /* should be always satisfied */
106 ((dr + dg + db) > delt)
108 ((xcl_in.red & sig_mask) != (xcl.red & sig_mask)) ||
109 ((xcl_in.green & sig_mask) != (xcl.green & sig_mask)) ||
110 ((xcl_in.blue & sig_mask) != (xcl.blue & sig_mask))
114 uint32_t pixels[256];
119 for (j = 0; j < i; j++)
120 pixels[j] = (uint32_t)color_lut[j];
121 xcb_free_colors(conn, cmap, 0, i, pixels);
126 color_lut[i] = rep->pixel;
136 x_color_alloc_gray(int ng,
137 xcb_connection_t *conn,
145 for (i = 0; i < v->bits_per_rgb_value; i++) sig_mask |= (0x1 << i);
146 sig_mask <<= (16 - v->bits_per_rgb_value);
148 color_lut = malloc(ng);
149 if (!color_lut) return NULL;
150 /* FIXME: remove the round-trip ? */
151 for (g = 0; g < (ng); g++)
154 xcb_coloritem_t xcl_in;
156 xcb_alloc_color_reply_t *rep;
158 val = (int)((((double)g) / ((ng) - 1)) * 255);
159 val = (val << 8) | val;
160 xcl.red = (uint16_t)(val);
161 xcl.green = (uint16_t)(val);
162 xcl.blue = (uint16_t)(val);
164 rep = xcb_alloc_color_reply(conn,
165 xcb_alloc_color_unchecked(conn,
171 /* FIXME: XAllocColor tries to approach the color */
172 /* in case the allocation fails */
173 /* XCB does not that (i think). It should be done */
174 /* So if rep == NULL, the other following tests */
175 /* should be always satisfied */
177 ((xcl_in.red & sig_mask) != (xcl.red & sig_mask)) ||
178 ((xcl_in.green & sig_mask) != (xcl.green & sig_mask)) ||
179 ((xcl_in.blue & sig_mask) != (xcl.blue & sig_mask)))
181 uint32_t pixels[256];
186 for (j = 0; j < i; j++)
187 pixels[j] = (uint32_t) color_lut[j];
188 xcb_free_colors(conn, cmap, 0, i, pixels);
193 color_lut[i] = rep->pixel;
201 x_color_alloc_rgb_332(xcb_connection_t *conn,
205 return x_color_alloc_rgb(8, 8, 4, conn, cmap, v);
209 x_color_alloc_rgb_666(xcb_connection_t *conn,
213 return x_color_alloc_rgb(6, 6, 6, conn, cmap, v);
217 x_color_alloc_rgb_232(xcb_connection_t *conn,
221 return x_color_alloc_rgb(4, 8, 4, conn, cmap, v);
225 x_color_alloc_rgb_222(xcb_connection_t *conn,
229 return x_color_alloc_rgb(4, 4, 4, conn, cmap, v);
233 x_color_alloc_rgb_221(xcb_connection_t *conn,
237 return x_color_alloc_rgb(4, 4, 2, conn, cmap, v);
241 x_color_alloc_rgb_121(xcb_connection_t *conn,
245 return x_color_alloc_rgb(2, 4, 2, conn, cmap, v);
249 x_color_alloc_rgb_111(xcb_connection_t *conn,
253 return x_color_alloc_rgb(2, 2, 2, conn, cmap, v);
257 x_color_alloc_gray_256(xcb_connection_t *conn,
261 return x_color_alloc_gray(256, conn, cmap, v);
265 x_color_alloc_gray_64(xcb_connection_t *conn,
269 return x_color_alloc_gray(64, conn, cmap, v);
273 x_color_alloc_gray_16(xcb_connection_t *conn,
277 return x_color_alloc_gray(32, conn, cmap, v);
281 x_color_alloc_gray_4(xcb_connection_t *conn,
285 return x_color_alloc_gray(16, conn, cmap, v);
289 x_color_alloc_mono(xcb_connection_t *conn,
293 return x_color_alloc_gray(2, conn, cmap, v);
297 evas_software_xcb_x_color_init(void)
299 static int initialised = 0;
301 if (initialised) return;
302 x_color_alloc[PAL_MODE_NONE] = NULL;
303 x_color_count[PAL_MODE_NONE] = 0;
305 x_color_alloc[PAL_MODE_MONO] = x_color_alloc_mono;
306 x_color_count[PAL_MODE_MONO] = 2;
308 x_color_alloc[PAL_MODE_GRAY4] = x_color_alloc_gray_4;
309 x_color_count[PAL_MODE_GRAY4] = 4;
311 x_color_alloc[PAL_MODE_GRAY16] = x_color_alloc_gray_16;
312 x_color_count[PAL_MODE_GRAY16] = 16;
314 x_color_alloc[PAL_MODE_GRAY64] = x_color_alloc_gray_64;
315 x_color_count[PAL_MODE_GRAY64] = 64;
317 x_color_alloc[PAL_MODE_GRAY256] = x_color_alloc_gray_256;
318 x_color_count[PAL_MODE_GRAY256] = 256;
320 x_color_alloc[PAL_MODE_RGB111] = x_color_alloc_rgb_111;
321 x_color_count[PAL_MODE_RGB111] = 2 * 2 * 2;
323 x_color_alloc[PAL_MODE_RGB121] = x_color_alloc_rgb_121;
324 x_color_count[PAL_MODE_RGB121] = 2 * 4 * 2;
326 x_color_alloc[PAL_MODE_RGB221] = x_color_alloc_rgb_221;
327 x_color_count[PAL_MODE_RGB221] = 4 * 4 * 2;
329 x_color_alloc[PAL_MODE_RGB222] = x_color_alloc_rgb_222;
330 x_color_count[PAL_MODE_RGB222] = 4 * 4 * 4;
332 x_color_alloc[PAL_MODE_RGB232] = x_color_alloc_rgb_232;
333 x_color_count[PAL_MODE_RGB232] = 4 * 8 * 4;
335 x_color_alloc[PAL_MODE_RGB666] = x_color_alloc_rgb_666;
336 x_color_count[PAL_MODE_RGB666] = 6 * 6 * 6;
338 x_color_alloc[PAL_MODE_RGB332] = x_color_alloc_rgb_332;
339 x_color_count[PAL_MODE_RGB332] = 8 * 8 * 4;
341 x_color_alloc[PAL_MODE_LAST] = NULL;
342 x_color_count[PAL_MODE_LAST] = 0;
347 evas_software_xcb_x_color_allocate(xcb_connection_t *conn,
349 xcb_visualtype_t *vis,
350 Convert_Pal_Mode colors)
352 Convert_Pal_Priv *palpriv;
357 /* printf("ALLOC cmap=%i vis=%p\n", cmap, vis);*/
358 for (l = palettes; l; l = l->next)
362 if ((conn == palpriv->conn) &&
363 (vis == palpriv->vis) &&
364 (cmap == palpriv->cmap))
370 pal = calloc(1, sizeof(struct _Convert_Pal));
371 if (!pal) return NULL;
372 for (c = colors; c > PAL_MODE_NONE; c--)
374 if (x_color_alloc[c])
376 /* printf("TRY PAL %i\n", c);*/
377 pal->lookup = (x_color_alloc[c])(conn, cmap, vis);
378 if (pal->lookup) break;
383 pal->count = x_color_count[c];
384 palpriv = calloc(1, sizeof(Convert_Pal_Priv));
388 if (pal->lookup) free(pal->lookup);
392 palpriv->conn = conn;
394 palpriv->cmap = cmap;
395 if (pal->colors == PAL_MODE_NONE)
397 if (pal->lookup) free(pal->lookup);
401 palettes = evas_list_append(palettes, pal);
406 evas_software_xcb_x_color_deallocate(xcb_connection_t *conn,
408 xcb_visualtype_t *vis,
411 uint32_t pixels[256];
415 if (pal->references > 0) return;
418 for(j = 0; j < pal->count; j++)
419 pixels[j] = (uint32_t) pal->lookup[j];
420 xcb_free_colors(conn, cmap, 0, pal->count, pixels);
424 palettes = evas_list_remove(palettes, pal);