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
|
/* -*- c-basic-offset:2; tab-width:2; indent-tabs-mode:nil -*- */
#include "ui_color_cache.h"
#include <stdio.h>
#include <pobl/bl_debug.h>
#include <pobl/bl_util.h> /* BL_ARRAY_SIZE */
#include <pobl/bl_mem.h>
/* --- static variables --- */
static ui_color_cache_t **color_caches;
static u_int num_caches;
/* --- static functions --- */
#ifndef USE_COMPACT_TRUECOLOR
static ui_color_t *get_cached_true_xcolor(ui_color_cache_t *color_cache, vt_color_t color) {
u_int8_t red;
u_int8_t green;
u_int8_t blue;
u_int8_t alpha;
if (!IS_TRUE_COLOR(color)) {
return NULL;
}
if (color_cache->cache_true == NULL &&
(color_cache->cache_true = calloc(1, sizeof(ui_color_cache_true_t))) == NULL) {
return NULL;
}
if (vt_get_color_rgba(color, &red, &green, &blue, &alpha)) {
int idx = color_cache->cache_true->next_idx;
if (color_cache->cache_true->need_unload) {
ui_unload_xcolor(color_cache->disp, color_cache->cache_true->xcolors + idx);
}
ui_load_rgb_xcolor(color_cache->disp, color_cache->cache_true->xcolors + idx,
red, green, blue, alpha);
if (++color_cache->cache_true->next_idx == BL_ARRAY_SIZE(color_cache->cache_true->xcolors)) {
color_cache->cache_true->next_idx = 0;
color_cache->cache_true->need_unload = 1;
}
return color_cache->cache_true->xcolors + idx;
}
return NULL;
}
#endif
static ui_color_cache_256ext_t *acquire_color_cache_256ext(ui_display_t *disp) {
u_int count;
ui_color_cache_256ext_t *cache;
for (count = 0; count < num_caches; count++) {
if (color_caches[count]->disp == disp && color_caches[count]->cache_256ext) {
color_caches[count]->cache_256ext->ref_count++;
return color_caches[count]->cache_256ext;
}
}
if ((cache = calloc(1, sizeof(ui_color_cache_256ext_t))) == NULL) {
return NULL;
}
cache->ref_count = 1;
return cache;
}
static ui_color_t *get_cached_256ext_xcolor(ui_color_cache_t *color_cache, vt_color_t color) {
u_int8_t red;
u_int8_t green;
u_int8_t blue;
u_int8_t alpha;
if (!IS_256EXT_COLOR(color)) {
return NULL;
}
if (!color_cache->cache_256ext &&
!(color_cache->cache_256ext = acquire_color_cache_256ext(color_cache->disp))) {
return NULL;
}
if (color_cache->cache_256ext->is_loaded[color - 16]) {
if (IS_256_COLOR(color) || !vt_ext_color_is_changed(color)) {
return &color_cache->cache_256ext->xcolors[color - 16];
} else {
u_int count;
for (count = 0; count < num_caches; count++) {
if (color_caches[count]->cache_256ext) {
ui_unload_xcolor(color_caches[count]->disp,
&color_caches[count]->cache_256ext->xcolors[color - 16]);
color_caches[count]->cache_256ext->is_loaded[color - 16] = 0;
}
}
}
}
if (!vt_get_color_rgba(color, &red, &green, &blue, &alpha) ||
!ui_load_rgb_xcolor(color_cache->disp, &color_cache->cache_256ext->xcolors[color - 16], red,
green, blue, alpha)) {
return NULL;
}
/*
* 16-479 colors ignore color_cache->fade_ratio.
*/
#ifdef DEBUG
bl_debug_printf(BL_DEBUG_TAG " new color %x %x\n", color,
color_cache->cache_256ext->xcolors[color - 16].pixel);
#endif
color_cache->cache_256ext->is_loaded[color - 16] = 1;
return &color_cache->cache_256ext->xcolors[color - 16];
}
static ui_color_t *get_cached_vtsys_xcolor(ui_color_cache_t *color_cache, vt_color_t color) {
u_int8_t red;
u_int8_t green;
u_int8_t blue;
u_int8_t alpha;
if (!IS_VTSYS_COLOR(color)) {
return NULL;
}
if (color_cache->is_loaded[color]) {
return &color_cache->xcolors[color];
}
if (!vt_get_color_rgba(color, &red, &green, &blue, &alpha) ||
!ui_load_rgb_xcolor(color_cache->disp, &color_cache->xcolors[color], red, green, blue,
alpha)) {
return NULL;
}
if (color_cache->fade_ratio < 100) {
if (!ui_xcolor_fade(color_cache->disp, &color_cache->xcolors[color], color_cache->fade_ratio)) {
return NULL;
}
}
#ifdef DEBUG
#ifdef UI_COLOR_HAS_RGB
bl_debug_printf(BL_DEBUG_TAG " new color %x red %x green %x blue %x\n", color,
color_cache->xcolors[color].red, color_cache->xcolors[color].green,
color_cache->xcolors[color].blue);
#endif
#endif
color_cache->is_loaded[color] = 1;
return &color_cache->xcolors[color];
}
/* --- global functions --- */
ui_color_cache_t *ui_acquire_color_cache(ui_display_t *disp, u_int8_t fade_ratio) {
u_int count;
ui_color_cache_t *color_cache;
void *p;
for (count = 0; count < num_caches; count++) {
if (color_caches[count]->disp == disp && color_caches[count]->fade_ratio == fade_ratio) {
color_caches[count]->ref_count++;
return color_caches[count];
}
}
if ((p = realloc(color_caches, sizeof(ui_color_cache_t*) * (num_caches + 1))) == NULL) {
return NULL;
}
color_caches = p;
if ((color_cache = calloc(1, sizeof(ui_color_cache_t))) == NULL) {
return NULL;
}
color_cache->disp = disp;
color_cache->fade_ratio = fade_ratio;
if (!ui_load_rgb_xcolor(color_cache->disp, &color_cache->black, 0, 0, 0, 0xff)) {
free(color_cache);
return NULL;
}
color_cache->ref_count = 1;
color_caches[num_caches++] = color_cache;
return color_cache;
}
void ui_release_color_cache(ui_color_cache_t *color_cache) {
u_int count;
if (--color_cache->ref_count > 0) {
return;
}
for (count = 0; count < num_caches; count++) {
if (color_caches[count] == color_cache) {
color_caches[count] = color_caches[--num_caches];
ui_color_cache_unload(color_cache);
ui_unload_xcolor(color_cache->disp, &color_cache->black);
free(color_cache);
if (num_caches == 0) {
free(color_caches);
color_caches = NULL;
}
return;
}
}
}
void ui_color_cache_unload(ui_color_cache_t *color_cache) {
vt_color_t color;
for (color = 0; color < BL_ARRAY_SIZE(color_cache->xcolors); color++) {
if (color_cache->is_loaded[color]) {
ui_unload_xcolor(color_cache->disp, &color_cache->xcolors[color]);
color_cache->is_loaded[color] = 0;
}
}
if (color_cache->cache_256ext && --color_cache->cache_256ext->ref_count == 0) {
ui_color_cache_256ext_t *cache_256ext;
cache_256ext = color_cache->cache_256ext;
for (color = 0; color < BL_ARRAY_SIZE(cache_256ext->xcolors);
color++) {
if (cache_256ext->is_loaded[color]) {
ui_unload_xcolor(color_cache->disp, &cache_256ext->xcolors[color]);
cache_256ext->is_loaded[color] = 0;
}
}
free(cache_256ext);
color_cache->cache_256ext = NULL;
}
}
void ui_color_cache_unload_all(void) {
u_int count;
for (count = 0; count < num_caches; count++) {
ui_color_cache_unload(color_caches[count]);
}
}
/* Not cached */
int ui_load_xcolor(ui_color_cache_t *color_cache, ui_color_t *xcolor, const char *name) {
if (!ui_load_named_xcolor(color_cache->disp, xcolor, name)) {
return 0;
}
#ifdef DEBUG
bl_debug_printf(BL_DEBUG_TAG " new color %x\n", xcolor->pixel);
#endif
if (color_cache->fade_ratio < 100) {
if (!ui_xcolor_fade(color_cache->disp, xcolor, color_cache->fade_ratio)) {
return 0;
}
}
#ifdef DEBUG
bl_debug_printf(BL_DEBUG_TAG " new color %s %x\n", name, xcolor->pixel);
#endif
return 1;
}
/* Always return non-null value. */
ui_color_t *ui_get_cached_xcolor(ui_color_cache_t *color_cache, vt_color_t color) {
ui_color_t *xcolor;
if ((xcolor = get_cached_vtsys_xcolor(color_cache, color))) {
return xcolor;
}
if ((xcolor = get_cached_256ext_xcolor(color_cache, color))) {
return xcolor;
}
#ifndef USE_COMPACT_TRUECOLOR
if ((xcolor = get_cached_true_xcolor(color_cache, color))) {
return xcolor;
}
#endif
bl_msg_printf("Loading color 0x%x failed. Using black color instead.\n", color);
return &color_cache->black;
}
|