aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/evas/src/lib/cache/evas_cache_image.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--libraries/evas/src/lib/cache/evas_cache_image.c1428
1 files changed, 0 insertions, 1428 deletions
diff --git a/libraries/evas/src/lib/cache/evas_cache_image.c b/libraries/evas/src/lib/cache/evas_cache_image.c
deleted file mode 100644
index d5b72c5..0000000
--- a/libraries/evas/src/lib/cache/evas_cache_image.c
+++ /dev/null
@@ -1,1428 +0,0 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include <stdlib.h>
6#include <assert.h>
7#include <sys/types.h>
8#include <sys/stat.h>
9#include <errno.h>
10
11#ifdef HAVE_EVIL
12# include <Evil.h>
13#endif
14
15#include "evas_common.h"
16#include "evas_private.h"
17
18//#define CACHEDUMP 1
19
20#ifdef EVAS_CSERVE
21// FIXME: cache server and threaded preload clash badly atm - disable
22//#undef BUILD_ASYNC_PRELOAD
23#endif
24
25#ifdef BUILD_ASYNC_PRELOAD
26typedef struct _Evas_Cache_Preload Evas_Cache_Preload;
27
28struct _Evas_Cache_Preload
29{
30 EINA_INLIST;
31 Image_Entry *ie;
32};
33
34static LK(engine_lock);
35static LK(wakeup);
36static int _evas_cache_mutex_init = 0;
37
38static Eina_Condition cond_wakeup;
39
40static void _evas_cache_image_entry_preload_remove(Image_Entry *ie, const void *target);
41#endif
42
43#define FREESTRC(Var) \
44 if (Var) \
45{ \
46 eina_stringshare_del(Var); \
47 Var = NULL; \
48}
49
50static void _evas_cache_image_dirty_add(Image_Entry *im);
51static void _evas_cache_image_dirty_del(Image_Entry *im);
52static void _evas_cache_image_activ_add(Image_Entry *im);
53static void _evas_cache_image_activ_del(Image_Entry *im);
54static void _evas_cache_image_lru_add(Image_Entry *im);
55static void _evas_cache_image_lru_del(Image_Entry *im);
56static void _evas_cache_image_lru_nodata_add(Image_Entry *im);
57static void _evas_cache_image_lru_nodata_del(Image_Entry *im);
58
59static void
60_evas_cache_image_dirty_add(Image_Entry *im)
61{
62 if (im->flags.dirty) return;
63 _evas_cache_image_activ_del(im);
64 _evas_cache_image_lru_del(im);
65 _evas_cache_image_lru_nodata_del(im);
66 im->flags.dirty = 1;
67 im->flags.cached = 1;
68#ifdef EVAS_FRAME_QUEUING
69 LKL(im->cache->lock);
70#endif
71 im->cache->dirty = eina_inlist_prepend(im->cache->dirty, EINA_INLIST_GET(im));
72#ifdef EVAS_FRAME_QUEUING
73 LKU(im->cache->lock);
74#endif
75 if (im->cache_key)
76 {
77 eina_stringshare_del(im->cache_key);
78 im->cache_key = NULL;
79 }
80}
81
82static void
83_evas_cache_image_dirty_del(Image_Entry *im)
84{
85 if (!im->flags.dirty) return;
86 im->flags.dirty = 0;
87 im->flags.cached = 0;
88#ifdef EVAS_FRAME_QUEUING
89 LKL(im->cache->lock);
90#endif
91 im->cache->dirty = eina_inlist_remove(im->cache->dirty, EINA_INLIST_GET(im));
92#ifdef EVAS_FRAME_QUEUING
93 LKU(im->cache->lock);
94#endif
95}
96
97static void
98_evas_cache_image_activ_add(Image_Entry *im)
99{
100 if (im->flags.activ) return;
101 _evas_cache_image_dirty_del(im);
102 _evas_cache_image_lru_del(im);
103 _evas_cache_image_lru_nodata_del(im);
104 if (!im->cache_key) return;
105 im->flags.activ = 1;
106 im->flags.cached = 1;
107#ifdef EVAS_FRAME_QUEUING
108 LKL(im->cache->lock);
109#endif
110 eina_hash_direct_add(im->cache->activ, im->cache_key, im);
111#ifdef EVAS_FRAME_QUEUING
112 LKU(im->cache->lock);
113#endif
114}
115
116static void
117_evas_cache_image_activ_del(Image_Entry *im)
118{
119 if (!im->flags.activ) return;
120 if (!im->cache_key) return;
121 im->flags.activ = 0;
122 im->flags.cached = 0;
123#ifdef EVAS_FRAME_QUEUING
124 LKL(im->cache->lock);
125#endif
126 eina_hash_del(im->cache->activ, im->cache_key, im);
127#ifdef EVAS_FRAME_QUEUING
128 LKU(im->cache->lock);
129#endif
130}
131
132static void
133_evas_cache_image_lru_add(Image_Entry *im)
134{
135 if (im->flags.lru) return;
136 _evas_cache_image_dirty_del(im);
137 _evas_cache_image_activ_del(im);
138 _evas_cache_image_lru_nodata_del(im);
139 if (!im->cache_key) return;
140 im->flags.lru = 1;
141 im->flags.cached = 1;
142#ifdef EVAS_FRAME_QUEUING
143 LKL(im->cache->lock);
144#endif
145 eina_hash_direct_add(im->cache->inactiv, im->cache_key, im);
146 im->cache->lru = eina_inlist_prepend(im->cache->lru, EINA_INLIST_GET(im));
147 im->cache->usage += im->cache->func.mem_size_get(im);
148#ifdef EVAS_FRAME_QUEUING
149 LKU(im->cache->lock);
150#endif
151}
152
153static void
154_evas_cache_image_lru_del(Image_Entry *im)
155{
156 if (!im->flags.lru) return;
157 if (!im->cache_key) return;
158 im->flags.lru = 0;
159 im->flags.cached = 0;
160#ifdef EVAS_FRAME_QUEUING
161 LKL(im->cache->lock);
162#endif
163 eina_hash_del(im->cache->inactiv, im->cache_key, im);
164 im->cache->lru = eina_inlist_remove(im->cache->lru, EINA_INLIST_GET(im));
165 im->cache->usage -= im->cache->func.mem_size_get(im);
166#ifdef EVAS_FRAME_QUEUING
167 LKU(im->cache->lock);
168#endif
169}
170
171static void
172_evas_cache_image_lru_nodata_add(Image_Entry *im)
173{
174 if (im->flags.lru_nodata) return;
175 _evas_cache_image_dirty_del(im);
176 _evas_cache_image_activ_del(im);
177 _evas_cache_image_lru_del(im);
178 im->flags.lru = 1;
179 im->flags.cached = 1;
180#ifdef EVAS_FRAME_QUEUING
181 LKL(im->cache->lock);
182#endif
183 im->cache->lru_nodata = eina_inlist_prepend(im->cache->lru_nodata, EINA_INLIST_GET(im));
184#ifdef EVAS_FRAME_QUEUING
185 LKU(im->cache->lock);
186#endif
187}
188
189static void
190_evas_cache_image_lru_nodata_del(Image_Entry *im)
191{
192 if (!im->flags.lru_nodata) return;
193 im->flags.lru = 0;
194 im->flags.cached = 0;
195#ifdef EVAS_FRAME_QUEUING
196 LKL(im->cache->lock);
197#endif
198 im->cache->lru_nodata = eina_inlist_remove(im->cache->lru_nodata, EINA_INLIST_GET(im));
199#ifdef EVAS_FRAME_QUEUING
200 LKU(im->cache->lock);
201#endif
202}
203
204static void
205_evas_cache_image_entry_delete(Evas_Cache_Image *cache, Image_Entry *ie)
206{
207 if (!ie) return;
208 if (cache->func.debug) cache->func.debug("deleting", ie);
209#ifdef BUILD_ASYNC_PRELOAD
210 if (ie->flags.delete_me == 1) return;
211 if (ie->preload)
212 {
213 ie->flags.delete_me = 1;
214 _evas_cache_image_entry_preload_remove(ie, NULL);
215 return;
216 }
217#endif
218
219 _evas_cache_image_dirty_del(ie);
220 _evas_cache_image_activ_del(ie);
221 _evas_cache_image_lru_del(ie);
222 _evas_cache_image_lru_nodata_del(ie);
223
224 cache->func.destructor(ie);
225 FREESTRC(ie->cache_key);
226 FREESTRC(ie->file);
227 FREESTRC(ie->key);
228 ie->cache = NULL;
229 cache->func.surface_delete(ie);
230
231#ifdef BUILD_ASYNC_PRELOAD
232 LKD(ie->lock);
233 LKD(ie->lock_cancel);
234#endif
235#ifdef EVAS_FRAME_QUEUING
236 LKD(ie->lock_references);
237#endif
238 cache->func.dealloc(ie);
239}
240
241static Eina_Bool
242_timestamp_compare(Image_Timestamp *tstamp, struct stat *st)
243{
244 if (tstamp->mtime != st->st_mtime) return EINA_FALSE;
245 if (tstamp->size != st->st_size) return EINA_FALSE;
246 if (tstamp->ino != st->st_ino) return EINA_FALSE;
247#ifdef _STAT_VER_LINUX
248#if (defined __USE_MISC && defined st_mtime)
249 if (tstamp->mtime_nsec != (unsigned long int)st->st_mtim.tv_nsec)
250 return EINA_FALSE;
251#else
252 if (tstamp->mtime_nsec != (unsigned long int)st->st_mtimensec)
253 return EINA_FALSE;
254#endif
255#endif
256 return EINA_TRUE;
257}
258
259static void
260_timestamp_build(Image_Timestamp *tstamp, struct stat *st)
261{
262 tstamp->mtime = st->st_mtime;
263 tstamp->size = st->st_size;
264 tstamp->ino = st->st_ino;
265#ifdef _STAT_VER_LINUX
266#if (defined __USE_MISC && defined st_mtime)
267 tstamp->mtime_nsec = (unsigned long int)st->st_mtim.tv_nsec;
268#else
269 tstamp->mtime_nsec = (unsigned long int)st->st_mtimensec;
270#endif
271#endif
272}
273
274static Image_Entry *
275_evas_cache_image_entry_new(Evas_Cache_Image *cache,
276 const char *hkey,
277 Image_Timestamp *tstamp,
278 const char *file,
279 const char *key,
280 RGBA_Image_Loadopts *lo,
281 int *error)
282{
283 Image_Entry *ie;
284
285 ie = cache->func.alloc();
286 if (!ie)
287 {
288 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
289 return NULL;
290 }
291 ie->cache = cache;
292 if (hkey) ie->cache_key = eina_stringshare_add(hkey);
293 ie->flags.need_data = 1;
294 ie->space = EVAS_COLORSPACE_ARGB8888;
295 ie->w = -1;
296 ie->h = -1;
297 ie->scale = 1;
298 if (file) ie->file = eina_stringshare_add(file);
299 if (key) ie->key = eina_stringshare_add(key);
300 if (tstamp) ie->tstamp = *tstamp;
301 else memset(&ie->tstamp, 0, sizeof(Image_Timestamp));
302
303#ifdef EVAS_FRAME_QUEUING
304 LKI(ie->lock_references);
305#endif
306#ifdef BUILD_ASYNC_PRELOAD
307 LKI(ie->lock);
308 LKI(ie->lock_cancel);
309#endif
310
311 if (lo) ie->load_opts = *lo;
312 if (ie->file)
313 {
314 *error = cache->func.constructor(ie);
315 if (*error != EVAS_LOAD_ERROR_NONE)
316 {
317 _evas_cache_image_entry_delete(cache, ie);
318 return NULL;
319 }
320 }
321 if (cache->func.debug) cache->func.debug("build", ie);
322 if (ie->cache_key) _evas_cache_image_activ_add(ie);
323 else _evas_cache_image_dirty_add(ie);
324 return ie;
325}
326
327static void
328_evas_cache_image_entry_surface_alloc__locked(Evas_Cache_Image *cache,
329 Image_Entry *ie,
330 unsigned int wmin,
331 unsigned int hmin)
332{
333 if ((ie->allocated.w == wmin) && (ie->allocated.h == hmin)) return;
334 if (cache->func.surface_alloc(ie, wmin, hmin))
335 {
336 wmin = 0;
337 hmin = 0;
338 }
339 ie->w = wmin;
340 ie->h = hmin;
341 ie->allocated.w = wmin;
342 ie->allocated.h = hmin;
343}
344
345static void
346_evas_cache_image_entry_surface_alloc(Evas_Cache_Image *cache,
347 Image_Entry *ie, int w, int h)
348{
349 int wmin = w > 0 ? w : 1;
350 int hmin = h > 0 ? h : 1;
351#ifdef BUILD_ASYNC_PRELOAD
352 LKL(engine_lock);
353#endif
354 _evas_cache_image_entry_surface_alloc__locked(cache, ie, wmin, hmin);
355#ifdef BUILD_ASYNC_PRELOAD
356 LKU(engine_lock);
357#endif
358}
359
360#ifdef BUILD_ASYNC_PRELOAD
361static void
362_evas_cache_image_async_heavy(void *data)
363{
364 Evas_Cache_Image *cache;
365 Image_Entry *current;
366 int error;
367 int pchannel;
368
369 current = data;
370
371 LKL(current->lock);
372 pchannel = current->channel;
373 current->channel++;
374 cache = current->cache;
375
376 if ((!current->flags.loaded) &&
377 ((Evas_Image_Load_Func*) current->info.module)->threadable)
378 {
379 error = cache->func.load(current);
380 if (cache->func.debug) cache->func.debug("load", current);
381 current->load_error = error;
382 if (error != EVAS_LOAD_ERROR_NONE)
383 {
384 current->flags.loaded = 0;
385 _evas_cache_image_entry_surface_alloc(cache, current,
386 current->w, current->h);
387 }
388 else
389 {
390 current->flags.loaded = 1;
391 }
392 }
393 current->channel = pchannel;
394 // check the unload cancel flag
395 LKL(current->lock_cancel);
396 if (current->unload_cancel)
397 {
398 current->unload_cancel = EINA_FALSE;
399 cache->func.surface_delete(current);
400 current->flags.loaded = 0;
401 current->flags.preload_done = 0;
402 }
403 LKU(current->lock_cancel);
404 LKU(current->lock);
405}
406
407static void
408_evas_cache_image_async_end(void *data)
409{
410 Image_Entry *ie = (Image_Entry *)data;
411 Evas_Cache_Target *tmp;
412
413 ie->cache->preload = eina_list_remove(ie->cache->preload, ie);
414 ie->cache->pending = eina_list_remove(ie->cache->pending, ie);
415 ie->preload = NULL;
416 ie->flags.preload_done = ie->flags.loaded;
417 while ((tmp = ie->targets))
418 {
419 evas_object_inform_call_image_preloaded((Evas_Object*) tmp->target);
420 ie->targets = (Evas_Cache_Target *)
421 eina_inlist_remove(EINA_INLIST_GET(ie->targets),
422 EINA_INLIST_GET(ie->targets));
423 free(tmp);
424 }
425}
426
427static void
428_evas_cache_image_async_cancel(void *data)
429{
430 Evas_Cache_Image *cache = NULL;
431 Image_Entry *ie = (Image_Entry *)data;
432
433 ie->preload = NULL;
434 ie->cache->pending = eina_list_remove(ie->cache->pending, ie);
435 if ((ie->flags.delete_me) || (ie->flags.dirty))
436 {
437 ie->flags.delete_me = 0;
438 _evas_cache_image_entry_delete(ie->cache, ie);
439 return;
440 }
441 if (ie->flags.loaded) _evas_cache_image_async_end(ie);
442#ifdef EVAS_FRAME_QUEUING
443 LKL(ie->lock_references);
444#endif
445 if (ie->references == 0)
446 {
447 _evas_cache_image_lru_add(ie);
448 cache = ie->cache;
449 }
450#ifdef EVAS_FRAME_QUEUING
451 LKU(ie->lock_references);
452#endif
453 if (cache) evas_cache_image_flush(cache);
454}
455
456// note - preload_add assumes a target is ONLY added ONCE to the image
457// entry. make sure you only add once, or remove first, then add
458static int
459_evas_cache_image_entry_preload_add(Image_Entry *ie, const void *target)
460{
461 Evas_Cache_Target *tg;
462
463 if (ie->flags.preload_done) return 0;
464
465 tg = malloc(sizeof (Evas_Cache_Target));
466 if (!tg) return 0;
467
468 tg->target = target;
469 ie->targets = (Evas_Cache_Target *)
470 eina_inlist_append(EINA_INLIST_GET(ie->targets), EINA_INLIST_GET(tg));
471 if (!ie->preload)
472 {
473 ie->cache->preload = eina_list_append(ie->cache->preload, ie);
474 ie->flags.pending = 0;
475 ie->preload = evas_preload_thread_run(_evas_cache_image_async_heavy,
476 _evas_cache_image_async_end,
477 _evas_cache_image_async_cancel,
478 ie);
479 }
480 return 1;
481}
482
483static void
484_evas_cache_image_entry_preload_remove(Image_Entry *ie, const void *target)
485{
486 if (target)
487 {
488 Evas_Cache_Target *tg;
489
490 EINA_INLIST_FOREACH(ie->targets, tg)
491 {
492 if (tg->target == target)
493 {
494 // FIXME: No callback when we cancel only for one target ?
495 ie->targets = (Evas_Cache_Target *)
496 eina_inlist_remove(EINA_INLIST_GET(ie->targets),
497 EINA_INLIST_GET(tg));
498 free(tg);
499 break;
500 }
501 }
502 }
503 else
504 {
505 Evas_Cache_Target *tg;
506
507 while (ie->targets)
508 {
509 tg = ie->targets;
510 ie->targets = (Evas_Cache_Target *)
511 eina_inlist_remove(EINA_INLIST_GET(ie->targets),
512 EINA_INLIST_GET(tg));
513 free(tg);
514 }
515 }
516
517 if ((!ie->targets) && (ie->preload) && (!ie->flags.pending))
518 {
519 ie->cache->preload = eina_list_remove(ie->cache->preload, ie);
520 ie->cache->pending = eina_list_append(ie->cache->pending, ie);
521 ie->flags.pending = 1;
522 evas_preload_thread_cancel(ie->preload);
523 }
524}
525#endif
526
527EAPI int
528evas_cache_image_usage_get(Evas_Cache_Image *cache)
529{
530 return cache->usage;
531}
532
533EAPI int
534evas_cache_image_get(Evas_Cache_Image *cache)
535{
536 return cache->limit;
537}
538
539EAPI void
540evas_cache_image_set(Evas_Cache_Image *cache, unsigned int limit)
541{
542#ifdef EVAS_FRAME_QUEUING
543 LKL(cache->lock);
544#endif
545 if (cache->limit == limit)
546 {
547#ifdef EVAS_FRAME_QUEUING
548 LKU(cache->lock);
549#endif
550 return;
551 }
552 cache->limit = limit;
553#ifdef EVAS_FRAME_QUEUING
554 LKU(cache->lock);
555#endif
556 evas_cache_image_flush(cache);
557}
558
559EAPI Evas_Cache_Image *
560evas_cache_image_init(const Evas_Cache_Image_Func *cb)
561{
562 Evas_Cache_Image *cache;
563
564#ifdef BUILD_ASYNC_PRELOAD
565 if (_evas_cache_mutex_init++ == 0)
566 {
567 LKI(engine_lock);
568 LKI(wakeup);
569 eina_condition_new(&cond_wakeup, &wakeup);
570 }
571#endif
572
573 cache = calloc(1, sizeof(Evas_Cache_Image));
574 if (!cache) return NULL;
575 cache->func = *cb;
576 cache->inactiv = eina_hash_string_superfast_new(NULL);
577 cache->activ = eina_hash_string_superfast_new(NULL);
578 cache->references = 1;
579#ifdef EVAS_FRAME_QUEUING
580 LKI(cache->lock);
581#endif
582 return cache;
583}
584
585static Eina_Bool
586_evas_cache_image_free_cb(__UNUSED__ const Eina_Hash *hash, __UNUSED__ const void *key, void *data, void *fdata)
587{
588 Eina_List **delete_list = fdata;
589 *delete_list = eina_list_prepend(*delete_list, data);
590 return EINA_TRUE;
591}
592
593EAPI void
594evas_cache_image_shutdown(Evas_Cache_Image *cache)
595{
596 Eina_List *delete_list;
597 Image_Entry *im;
598
599#ifdef EVAS_FRAME_QUEUING
600 LKL(cache->lock);
601#endif
602 cache->references--;
603 if (cache->references != 0)
604 {
605#ifdef EVAS_FRAME_QUEUING
606 LKU(cache->lock);
607#endif
608 return;
609 }
610#ifdef EVAS_FRAME_QUEUING
611 /* Release and destroy lock early ! */
612 LKU(cache->lock);
613 LKD(cache->lock);
614#endif
615
616#ifdef BUILD_ASYNC_PRELOAD
617 EINA_LIST_FREE(cache->preload, im)
618 {
619 /* By doing that we are protecting us from destroying image when the cache is no longer available. */
620 im->flags.delete_me = 1;
621 _evas_cache_image_entry_preload_remove(im, NULL);
622 }
623 evas_async_events_process();
624#endif
625 while (cache->lru)
626 {
627 im = (Image_Entry *)cache->lru;
628 _evas_cache_image_entry_delete(cache, im);
629 }
630 while (cache->lru_nodata)
631 {
632 im = (Image_Entry *)cache->lru_nodata;
633 _evas_cache_image_entry_delete(cache, im);
634 }
635 /* This is mad, I am about to destroy image still alive, but we need to prevent leak. */
636 while (cache->dirty)
637 {
638 im = (Image_Entry *)cache->dirty;
639 _evas_cache_image_entry_delete(cache, im);
640 }
641 delete_list = NULL;
642 eina_hash_foreach(cache->activ, _evas_cache_image_free_cb, &delete_list);
643 while (delete_list)
644 {
645 _evas_cache_image_entry_delete(cache, eina_list_data_get(delete_list));
646 delete_list = eina_list_remove_list(delete_list, delete_list);
647 }
648
649#ifdef BUILD_ASYNC_PRELOAD
650 /* Now wait for all pending image to die */
651 while (cache->pending)
652 {
653 evas_async_events_process();
654 LKL(wakeup);
655 // the lazy bum who did eain threads and converted this code
656 // didn't bother to worry about Eina_Lock being a different type
657 // to a pthread mutex.
658 if (cache->pending) eina_condition_wait(&cond_wakeup);
659 LKU(wakeup);
660 }
661#endif
662 eina_hash_free(cache->activ);
663 eina_hash_free(cache->inactiv);
664 free(cache);
665
666#ifdef BUILD_ASYNC_PRELOAD
667 if (--_evas_cache_mutex_init == 0)
668 {
669 eina_condition_free(&cond_wakeup);
670 LKD(engine_lock);
671 LKD(wakeup);
672 }
673#endif
674}
675
676EAPI Image_Entry *
677evas_cache_image_request(Evas_Cache_Image *cache, const char *file,
678 const char *key, RGBA_Image_Loadopts *lo, int *error)
679{
680 const char *ckey = "(null)";
681 char *hkey;
682 Image_Entry *im;
683 Evas_Image_Load_Opts prevent = { 0, 0.0, 0, 0, 0, { 0, 0, 0, 0 }, EINA_FALSE };
684 size_t size;
685 int stat_done = 0, stat_failed = 0;
686 size_t file_length;
687 size_t key_length;
688 struct stat st;
689 Image_Timestamp tstamp;
690
691 if ((!file) || ((!file) && (!key)))
692 {
693 *error = EVAS_LOAD_ERROR_GENERIC;
694 return NULL;
695 }
696
697 /* generate hkey from file+key+load opts */
698 file_length = strlen(file);
699 key_length = key ? strlen(key) : 6;
700 size = file_length + key_length + 132;
701 hkey = alloca(sizeof (char) * size);
702 memcpy(hkey, file, file_length);
703 size = file_length;
704 memcpy(hkey + size, "//://", 5);
705 size += 5;
706 if (key) ckey = key;
707 memcpy(hkey + size, ckey, key_length);
708 size += key_length;
709 if ((!lo) ||
710 (lo &&
711 (lo->scale_down_by == 0) &&
712 (lo->dpi == 0.0) &&
713 ((lo->w == 0) || (lo->h == 0)) &&
714 ((lo->region.w == 0) || (lo->region.h == 0)) &&
715 (lo->orientation == 0)
716 ))
717 {
718 lo = &prevent;
719 }
720 else
721 {
722 memcpy(hkey + size, "//@/", 4);
723 size += 4;
724 size += eina_convert_xtoa(lo->scale_down_by, hkey + size);
725 hkey[size] = '/';
726 size += 1;
727 size += eina_convert_dtoa(lo->dpi, hkey + size);
728 hkey[size] = '/';
729 size += 1;
730 size += eina_convert_xtoa(lo->w, hkey + size);
731 hkey[size] = 'x';
732 size += 1;
733 size += eina_convert_xtoa(lo->h, hkey + size);
734 hkey[size] = '/';
735 size += 1;
736 size += eina_convert_xtoa(lo->region.x, hkey + size);
737 hkey[size] = '+';
738 size += 1;
739 size += eina_convert_xtoa(lo->region.y, hkey + size);
740 hkey[size] = '.';
741 size += 1;
742 size += eina_convert_xtoa(lo->region.w, hkey + size);
743 hkey[size] = 'x';
744 size += 1;
745 size += eina_convert_xtoa(lo->region.h, hkey + size);
746
747 if (lo->orientation)
748 {
749 hkey[size] = '/';
750 size += 1;
751 hkey[size] = 'o';
752 size += 1;
753 }
754 }
755 hkey[size] = '\0';
756
757 /* find image by key in active hash */
758#ifdef EVAS_FRAME_QUEUING
759 LKL(cache->lock);
760#endif
761 im = eina_hash_find(cache->activ, hkey);
762#ifdef EVAS_FRAME_QUEUING
763 LKU(cache->lock);
764#endif
765 if (im)
766 {
767 int ok = 1;
768
769 stat_done = 1;
770 if (stat(file, &st) < 0)
771 {
772 stat_failed = 1;
773 ok = 0;
774 }
775 else if (!_timestamp_compare(&(im->tstamp), &st)) ok = 0;
776 if (ok) goto on_ok;
777 /* image we found doesn't match what's on disk (stat info wise)
778 * so dirty the active cache entry so we never find it again. this
779 * also implicitly guarantees that we only have 1 active copy
780 * of an image at a given key. we wither find it and keep re-reffing
781 * it or we dirty it and get it out */
782 _evas_cache_image_dirty_add(im);
783 im = NULL;
784 }
785
786 /* find image by key in inactive/lru hash */
787#ifdef EVAS_FRAME_QUEUING
788 LKL(cache->lock);
789#endif
790 im = eina_hash_find(cache->inactiv, hkey);
791#ifdef EVAS_FRAME_QUEUING
792 LKU(cache->lock);
793#endif
794 if (im)
795 {
796 int ok = 1;
797
798 if (!stat_done)
799 {
800 stat_done = 1;
801 if (stat(file, &st) < 0)
802 {
803 stat_failed = 1;
804 ok = 0;
805 }
806 else if (!_timestamp_compare(&(im->tstamp), &st)) ok = 0;
807 }
808 else if (!_timestamp_compare(&(im->tstamp), &st)) ok = 0;
809
810 if (ok)
811 {
812 /* remove from lru and make it active again */
813 _evas_cache_image_lru_del(im);
814 _evas_cache_image_activ_add(im);
815 goto on_ok;
816 }
817 /* as avtive cache find - if we match in lru and its invalid, dirty */
818 _evas_cache_image_dirty_add(im);
819 /* this image never used, so it have to be deleted */
820 _evas_cache_image_entry_delete(cache, im);
821 im = NULL;
822 }
823 if (stat_failed) goto on_stat_error;
824
825 if (!stat_done)
826 {
827 if (stat(file, &st) < 0) goto on_stat_error;
828 }
829 _timestamp_build(&tstamp, &st);
830 im = _evas_cache_image_entry_new(cache, hkey, &tstamp, file, key,
831 lo, error);
832 if (!im) goto on_stat_error;
833 if (cache->func.debug) cache->func.debug("request", im);
834
835on_ok:
836 *error = EVAS_LOAD_ERROR_NONE;
837#ifdef EVAS_FRAME_QUEUING
838 LKL(im->lock_references);
839#endif
840 im->references++;
841#ifdef EVAS_FRAME_QUEUING
842 LKU(im->lock_references);
843#endif
844 return im;
845
846on_stat_error:
847#ifndef _WIN32
848 if ((errno == ENOENT) || (errno == ENOTDIR) ||
849 (errno == ENAMETOOLONG) || (errno == ELOOP))
850#else
851 if (errno == ENOENT)
852#endif
853 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
854#ifndef _WIN32
855 else if ((errno == ENOMEM) || (errno == EOVERFLOW))
856#else
857 else if (errno == ENOMEM)
858#endif
859 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
860 else if (errno == EACCES)
861 *error = EVAS_LOAD_ERROR_PERMISSION_DENIED;
862 else
863 *error = EVAS_LOAD_ERROR_GENERIC;
864
865 if (im) _evas_cache_image_entry_delete(cache, im);
866 return NULL;
867}
868
869EAPI void
870evas_cache_image_drop(Image_Entry *im)
871{
872 Evas_Cache_Image *cache;
873 int references;
874
875#ifdef EVAS_FRAME_QUEUING
876 LKL(im->lock_references);
877#endif
878 im->references--;
879 if (im->references < 0) im->references = 0;
880 references = im->references;
881#ifdef EVAS_FRAME_QUEUING
882 LKU(im->lock_references);
883#endif
884
885 cache = im->cache;
886
887 if (references == 0)
888 {
889#ifdef EVAS_FRAME_QUEUING
890 LKL(im->ref_fq_add);
891 LKL(im->ref_fq_del);
892 if (im->ref_fq[0] != im->ref_fq[1])
893 {
894 LKU(im->ref_fq_add);
895 LKU(im->ref_fq_del);
896 return;
897 }
898 LKU(im->ref_fq_add);
899 LKU(im->ref_fq_del);
900#endif
901
902#ifdef BUILD_ASYNC_PRELOAD
903 if (im->preload)
904 {
905 _evas_cache_image_entry_preload_remove(im, NULL);
906 return;
907 }
908#endif
909
910 if (im->flags.dirty)
911 {
912 _evas_cache_image_entry_delete(cache, im);
913 return;
914 }
915 _evas_cache_image_lru_add(im);
916 if (cache) evas_cache_image_flush(cache);
917 }
918}
919
920EAPI void
921evas_cache_image_data_not_needed(Image_Entry *im)
922{
923 int references;
924
925#ifdef EVAS_FRAME_QUEUING
926 LKL(im->lock_references);
927#endif
928 references = im->references;
929#ifdef EVAS_FRAME_QUEUING
930 LKU(im->lock_references);
931#endif
932 if (references > 1) return;
933 if ((im->flags.dirty) || (!im->flags.need_data)) return;
934 _evas_cache_image_lru_nodata_add(im);
935}
936
937EAPI Image_Entry *
938evas_cache_image_dirty(Image_Entry *im, unsigned int x, unsigned int y, unsigned int w, unsigned int h)
939{
940 Image_Entry *im_dirty = im;
941 Evas_Cache_Image *cache;
942
943 cache = im->cache;
944 if (!(im->flags.dirty))
945 {
946#ifndef EVAS_CSERVE
947 int references;
948#ifdef EVAS_FRAME_QUEUING
949 LKL(im->lock_references);
950#endif
951 references = im->references;
952#ifdef EVAS_FRAME_QUEUING
953 LKU(im->lock_references);
954#endif
955 // if ref 1 also copy if using shared cache as its read-only
956 if (references == 1) im_dirty = im;
957 else
958#endif
959 {
960 im_dirty =
961 evas_cache_image_copied_data(cache, im->w, im->h,
962 evas_cache_image_pixels(im),
963 im->flags.alpha, im->space);
964 if (!im_dirty) goto on_error;
965 if (cache->func.debug) cache->func.debug("dirty-src", im);
966 cache->func.dirty(im_dirty, im);
967 if (cache->func.debug) cache->func.debug("dirty-out", im_dirty);
968#ifdef EVAS_FRAME_QUEUING
969 LKL(im_dirty->lock_references);
970#endif
971 im_dirty->references = 1;
972#ifdef EVAS_FRAME_QUEUING
973 LKU(im_dirty->lock_references);
974#endif
975 evas_cache_image_drop(im);
976 }
977 _evas_cache_image_dirty_add(im_dirty);
978 }
979
980 if (cache->func.debug) cache->func.debug("dirty-region", im_dirty);
981 if (cache->func.dirty_region)
982 cache->func.dirty_region(im_dirty, x, y, w, h);
983 return im_dirty;
984
985on_error:
986 if (im_dirty) _evas_cache_image_entry_delete(cache, im_dirty);
987 evas_cache_image_drop(im);
988 return NULL;
989}
990
991EAPI Image_Entry *
992evas_cache_image_alone(Image_Entry *im)
993{
994 Evas_Cache_Image *cache;
995 Image_Entry *im_dirty = im;
996 int references;
997
998 cache = im->cache;
999#ifdef EVAS_FRAME_QUEUING
1000 LKL(im->lock_references);
1001#endif
1002 references = im->references;
1003#ifdef EVAS_FRAME_QUEUING
1004 LKU(im->lock_references);
1005#endif
1006
1007 if (references <= 1)
1008 {
1009 if (!im->flags.dirty) _evas_cache_image_dirty_add(im);
1010 }
1011 else
1012 {
1013 im_dirty = evas_cache_image_copied_data(cache, im->w, im->h,
1014 evas_cache_image_pixels(im),
1015 im->flags.alpha,
1016 im->space);
1017 if (!im_dirty) goto on_error;
1018 if (cache->func.debug) cache->func.debug("dirty-src", im);
1019 cache->func.dirty(im_dirty, im);
1020 if (cache->func.debug) cache->func.debug("dirty-out", im_dirty);
1021#ifdef EVAS_FRAME_QUEUING
1022 LKL(im_dirty->lock_references);
1023#endif
1024 im_dirty->references = 1;
1025#ifdef EVAS_FRAME_QUEUING
1026 LKU(im_dirty->lock_references);
1027#endif
1028 evas_cache_image_drop(im);
1029 }
1030 return im_dirty;
1031
1032on_error:
1033 if (im_dirty) _evas_cache_image_entry_delete(cache, im_dirty);
1034 evas_cache_image_drop(im);
1035 return NULL;
1036}
1037
1038EAPI Image_Entry *
1039evas_cache_image_copied_data(Evas_Cache_Image *cache,
1040 unsigned int w, unsigned int h,
1041 DATA32 *image_data, int alpha, int cspace)
1042{
1043 Image_Entry *im;
1044
1045 if ((cspace == EVAS_COLORSPACE_YCBCR422P601_PL) ||
1046 (cspace == EVAS_COLORSPACE_YCBCR422P709_PL) ||
1047 (cspace == EVAS_COLORSPACE_YCBCR422601_PL))
1048 w &= ~0x1;
1049
1050 im = _evas_cache_image_entry_new(cache, NULL, NULL, NULL, NULL, NULL, NULL);
1051 if (!im) return NULL;
1052 im->space = cspace;
1053 im->flags.alpha = alpha;
1054 _evas_cache_image_entry_surface_alloc(cache, im, w, h);
1055 if (cache->func.copied_data(im, w, h, image_data, alpha, cspace) != 0)
1056 {
1057 _evas_cache_image_entry_delete(cache, im);
1058 return NULL;
1059 }
1060#ifdef EVAS_FRAME_QUEUING
1061 LKL(im->lock_references);
1062#endif
1063 im->references = 1;
1064#ifdef EVAS_FRAME_QUEUING
1065 LKU(im->lock_references);
1066#endif
1067 if (cache->func.debug) cache->func.debug("copied-data", im);
1068 return im;
1069}
1070
1071EAPI Image_Entry *
1072evas_cache_image_data(Evas_Cache_Image *cache, unsigned int w, unsigned int h, DATA32 *image_data, int alpha, int cspace)
1073{
1074 Image_Entry *im;
1075
1076 if ((cspace == EVAS_COLORSPACE_YCBCR422P601_PL) ||
1077 (cspace == EVAS_COLORSPACE_YCBCR422P709_PL) ||
1078 (cspace == EVAS_COLORSPACE_YCBCR422601_PL))
1079 w &= ~0x1;
1080
1081 im = _evas_cache_image_entry_new(cache, NULL, NULL, NULL, NULL, NULL, NULL);
1082 if (!im) return NULL;
1083 im->w = w;
1084 im->h = h;
1085 im->flags.alpha = alpha;
1086 if (cache->func.data(im, w, h, image_data, alpha, cspace) != 0)
1087 {
1088 _evas_cache_image_entry_delete(cache, im);
1089 return NULL;
1090 }
1091#ifdef EVAS_FRAME_QUEUING
1092 LKL(im->lock_references);
1093#endif
1094 im->references = 1;
1095#ifdef EVAS_FRAME_QUEUING
1096 LKU(im->lock_references);
1097#endif
1098 if (cache->func.debug) cache->func.debug("data", im);
1099 return im;
1100}
1101
1102EAPI void
1103evas_cache_image_surface_alloc(Image_Entry *im, unsigned int w, unsigned int h)
1104{
1105 Evas_Cache_Image *cache = im->cache;
1106
1107 if ((im->space == EVAS_COLORSPACE_YCBCR422P601_PL) ||
1108 (im->space == EVAS_COLORSPACE_YCBCR422P709_PL) ||
1109 (im->space == EVAS_COLORSPACE_YCBCR422601_PL))
1110 w &= ~0x1;
1111
1112 _evas_cache_image_entry_surface_alloc(cache, im, w, h);
1113 if (cache->func.debug) cache->func.debug("surface-alloc", im);
1114}
1115
1116EAPI Image_Entry *
1117evas_cache_image_size_set(Image_Entry *im, unsigned int w, unsigned int h)
1118{
1119 Evas_Cache_Image *cache;
1120 Image_Entry *im2 = NULL;
1121 int error;
1122
1123 if ((im->space == EVAS_COLORSPACE_YCBCR422P601_PL) ||
1124 (im->space == EVAS_COLORSPACE_YCBCR422P709_PL) ||
1125 (im->space == EVAS_COLORSPACE_YCBCR422601_PL))
1126 w &= ~0x1;
1127 if ((im->w == w) && (im->h == h)) return im;
1128
1129 cache = im->cache;
1130 im2 = _evas_cache_image_entry_new(cache, NULL, NULL, NULL, NULL, NULL, &error);
1131 if (!im2) goto on_error;
1132
1133 im2->flags.alpha = im->flags.alpha;
1134 im2->space = im->space;
1135 im2->load_opts = im->load_opts;
1136 _evas_cache_image_entry_surface_alloc(cache, im2, w, h);
1137 error = cache->func.size_set(im2, im, w, h);
1138 if (error != 0) goto on_error;
1139#ifdef EVAS_FRAME_QUEUING
1140 LKL(im2->lock_references);
1141#endif
1142 im2->references = 1;
1143#ifdef EVAS_FRAME_QUEUING
1144 LKU(im2->lock_references);
1145#endif
1146 evas_cache_image_drop(im);
1147 if (cache->func.debug) cache->func.debug("size_set", im2);
1148 return im2;
1149
1150on_error:
1151 if (im2) _evas_cache_image_entry_delete(cache, im2);
1152 evas_cache_image_drop(im);
1153 return NULL;
1154}
1155
1156EAPI int
1157evas_cache_image_load_data(Image_Entry *im)
1158{
1159#ifdef BUILD_ASYNC_PRELOAD
1160 Eina_Bool preload = EINA_FALSE;
1161#endif
1162 int error = EVAS_LOAD_ERROR_NONE;
1163
1164 if ((im->flags.loaded) && (!im->flags.animated)) return error;
1165#ifdef BUILD_ASYNC_PRELOAD
1166 if (im->preload)
1167 {
1168 preload = EINA_TRUE;
1169 if (!im->flags.pending)
1170 {
1171 im->cache->preload = eina_list_remove(im->cache->preload, im);
1172 im->cache->pending = eina_list_append(im->cache->pending, im);
1173 im->flags.pending = 1;
1174 evas_preload_thread_cancel(im->preload);
1175 }
1176 evas_async_events_process();
1177 LKL(wakeup);
1178 while (im->preload)
1179 {
1180 eina_condition_wait(&cond_wakeup);
1181 LKU(wakeup);
1182 evas_async_events_process();
1183 LKL(wakeup);
1184 }
1185 LKU(wakeup);
1186 }
1187
1188 if ((im->flags.loaded) && (!im->flags.animated)) return error;
1189 LKL(im->lock);
1190#endif
1191 im->flags.in_progress = EINA_TRUE;
1192 error = im->cache->func.load(im);
1193 im->flags.in_progress = EINA_FALSE;
1194#ifdef BUILD_ASYNC_PRELOAD
1195 LKU(im->lock);
1196#endif
1197 im->flags.loaded = 1;
1198 if (im->cache->func.debug) im->cache->func.debug("load", im);
1199 if (error != EVAS_LOAD_ERROR_NONE)
1200 {
1201 _evas_cache_image_entry_surface_alloc(im->cache, im, im->w, im->h);
1202 im->flags.loaded = 0;
1203 }
1204#ifdef BUILD_ASYNC_PRELOAD
1205 if (preload) _evas_cache_image_async_end(im);
1206#endif
1207 return error;
1208}
1209
1210EAPI void
1211evas_cache_image_unload_data(Image_Entry *im)
1212{
1213 if (im->flags.in_progress) return;
1214 evas_cache_image_preload_cancel(im, NULL);
1215#ifdef BUILD_ASYNC_PRELOAD
1216 LKL(im->lock_cancel);
1217 if (LKT(im->lock) == EINA_FALSE) /* can't get image lock - busy async load */
1218 {
1219 im->unload_cancel = EINA_TRUE;
1220 LKU(im->lock_cancel);
1221 return;
1222 }
1223 LKU(im->lock_cancel);
1224#endif
1225 if ((!im->flags.loaded) || (!im->file) || (!im->info.module) ||
1226 (im->flags.dirty))
1227 {
1228#ifdef BUILD_ASYNC_PRELOAD
1229 LKU(im->lock);
1230#endif
1231 return;
1232 }
1233 im->cache->func.destructor(im);
1234#ifdef BUILD_ASYNC_PRELOAD
1235 LKU(im->lock);
1236#endif
1237 //FIXME: imagedataunload - inform owners
1238}
1239
1240static Eina_Bool
1241_evas_cache_image_unload_cb(__UNUSED__ const Eina_Hash *hash, __UNUSED__ const void *key, void *data, __UNUSED__ void *fdata)
1242{
1243 evas_cache_image_unload_data(data);
1244 return EINA_TRUE;
1245}
1246
1247EAPI void
1248evas_cache_image_unload_all(Evas_Cache_Image *cache)
1249{
1250 Image_Entry *im;
1251
1252 EINA_INLIST_FOREACH(cache->lru, im) evas_cache_image_unload_data(im);
1253 EINA_INLIST_FOREACH(cache->lru_nodata, im) evas_cache_image_unload_data(im);
1254 eina_hash_foreach(cache->activ, _evas_cache_image_unload_cb, NULL);
1255 eina_hash_foreach(cache->inactiv, _evas_cache_image_unload_cb, NULL);
1256}
1257
1258EAPI Eina_Bool
1259evas_cache_image_is_loaded(Image_Entry *im)
1260{
1261 if (im->flags.loaded) return EINA_TRUE;
1262 return EINA_FALSE;
1263}
1264
1265EAPI void
1266evas_cache_image_preload_data(Image_Entry *im, const void *target)
1267{
1268#ifdef BUILD_ASYNC_PRELOAD
1269 RGBA_Image *img = (RGBA_Image *)im;
1270
1271 if ((im->flags.loaded) && (img->image.data))
1272 {
1273 evas_object_inform_call_image_preloaded((Evas_Object *)target);
1274 return;
1275 }
1276 im->flags.loaded = 0;
1277 if (!_evas_cache_image_entry_preload_add(im, target))
1278 evas_object_inform_call_image_preloaded((Evas_Object *)target);
1279#else
1280 evas_cache_image_load_data(im);
1281 evas_object_inform_call_image_preloaded((Evas_Object *)target);
1282#endif
1283}
1284
1285EAPI void
1286evas_cache_image_preload_cancel(Image_Entry *im, const void *target)
1287{
1288#ifdef BUILD_ASYNC_PRELOAD
1289 if (!target) return;
1290 _evas_cache_image_entry_preload_remove(im, target);
1291#else
1292 (void)im;
1293 (void)target;
1294#endif
1295}
1296
1297#ifdef CACHEDUMP
1298static int total = 0;
1299
1300static void
1301_dump_img(Image_Entry *im, const char *type)
1302{
1303 total += im->cache->func.mem_size_get(im);
1304 printf("%s: %4i: %4ib, %4ix%4i alloc[%4ix%4i] [%s] [%s]\n",
1305 type,
1306 im->references,
1307 im->cache->func.mem_size_get(im),
1308 im->w, im->h, im->allocated.w, im->allocated.h,
1309 im->file, im->key);
1310}
1311
1312static Eina_Bool
1313_dump_cache_active(__UNUSED__ const Eina_Hash *hash, __UNUSED__ const void *key, void *data, void *fdata __UNUSED__)
1314{
1315 Image_Entry *im = data;
1316 _dump_img(im, "ACTIVE");
1317 return EINA_TRUE;
1318}
1319
1320static void
1321_dump_cache(Evas_Cache_Image *cache)
1322{
1323 Image_Entry *im;
1324
1325 printf("--CACHE DUMP----------------------------------------------------\n");
1326 printf("cache: %ikb / %ikb\n",
1327 cache->usage / 1024,
1328 cache->limit / 1024);
1329 printf("................................................................\n");
1330 total = 0;
1331 EINA_INLIST_FOREACH(cache->lru_nodata, im)
1332 _dump_img(im, "NODATA");
1333 EINA_INLIST_FOREACH(cache->lru, im)
1334 _dump_img(im, "DATA ");
1335 printf("tot: %i\n"
1336 "usg: %i\n",
1337 total,
1338 cache->usage);
1339 eina_hash_foreach(cache->activ, _dump_cache_active, NULL);
1340}
1341#endif
1342
1343EAPI int
1344evas_cache_image_flush(Evas_Cache_Image *cache)
1345{
1346#ifdef CACHEDUMP
1347 _dump_cache(cache);
1348#endif
1349 if (cache->limit == (unsigned int)-1) return -1;
1350
1351 while ((cache->lru) && (cache->limit < (unsigned int)cache->usage))
1352 {
1353 Image_Entry *im;
1354
1355 im = (Image_Entry *)cache->lru->last;
1356 _evas_cache_image_entry_delete(cache, im);
1357 }
1358
1359 while ((cache->lru_nodata) && (cache->limit < (unsigned int)cache->usage))
1360 {
1361 Image_Entry *im;
1362
1363 im = (Image_Entry *) cache->lru_nodata->last;
1364 _evas_cache_image_lru_nodata_del(im);
1365 cache->func.surface_delete(im);
1366 im->flags.loaded = 0;
1367 }
1368
1369 return cache->usage;
1370}
1371
1372EAPI Image_Entry *
1373evas_cache_image_empty(Evas_Cache_Image *cache)
1374{
1375 Image_Entry *im;
1376
1377 im = _evas_cache_image_entry_new(cache, NULL, NULL, NULL, NULL, NULL, NULL);
1378 if (!im) return NULL;
1379#ifdef EVAS_FRAME_QUEUING
1380 LKL(im->lock_references);
1381#endif
1382 im->references = 1;
1383#ifdef EVAS_FRAME_QUEUING
1384 LKU(im->lock_references);
1385#endif
1386 return im;
1387}
1388
1389EAPI void
1390evas_cache_image_colorspace(Image_Entry *im, int cspace)
1391{
1392 if (im->space == cspace) return;
1393 im->space = cspace;
1394 im->cache->func.color_space(im, cspace);
1395}
1396
1397EAPI void *
1398evas_cache_private_from_image_entry_get(Image_Entry *im)
1399{
1400 return (void *)im->cache->data;
1401}
1402
1403EAPI void *
1404evas_cache_private_get(Evas_Cache_Image *cache)
1405{
1406 return cache->data;
1407}
1408
1409EAPI void
1410evas_cache_private_set(Evas_Cache_Image *cache, const void *data)
1411{
1412 cache->data = (void *)data;
1413}
1414
1415EAPI DATA32 *
1416evas_cache_image_pixels(Image_Entry *im)
1417{
1418 return im->cache->func.surface_pixels(im);
1419}
1420
1421EAPI void
1422evas_cache_image_wakeup(void)
1423{
1424#ifdef BUILD_ASYNC_PRELOAD
1425 if (_evas_cache_mutex_init > 0)
1426 eina_condition_broadcast(&cond_wakeup);
1427#endif
1428}