15 #include "evas_common.h"
16 #include "evas_private.h"
21 // FIXME: cache server and threaded preload clash badly atm - disable
22 //#undef BUILD_ASYNC_PRELOAD
25 #ifdef BUILD_ASYNC_PRELOAD
26 typedef struct _Evas_Cache_Preload Evas_Cache_Preload;
28 struct _Evas_Cache_Preload
34 static LK(engine_lock);
36 static int _evas_cache_mutex_init = 0;
38 static Eina_Condition cond_wakeup;
40 static void _evas_cache_image_entry_preload_remove(Image_Entry *ie, const void *target);
43 #define FREESTRC(Var) \
46 eina_stringshare_del(Var); \
50 static void _evas_cache_image_dirty_add(Image_Entry *im);
51 static void _evas_cache_image_dirty_del(Image_Entry *im);
52 static void _evas_cache_image_activ_add(Image_Entry *im);
53 static void _evas_cache_image_activ_del(Image_Entry *im);
54 static void _evas_cache_image_lru_add(Image_Entry *im);
55 static void _evas_cache_image_lru_del(Image_Entry *im);
56 static void _evas_cache_image_lru_nodata_add(Image_Entry *im);
57 static void _evas_cache_image_lru_nodata_del(Image_Entry *im);
60 _evas_cache_image_dirty_add(Image_Entry *im)
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);
68 #ifdef EVAS_FRAME_QUEUING
71 im->cache->dirty = eina_inlist_prepend(im->cache->dirty, EINA_INLIST_GET(im));
72 #ifdef EVAS_FRAME_QUEUING
77 eina_stringshare_del(im->cache_key);
83 _evas_cache_image_dirty_del(Image_Entry *im)
85 if (!im->flags.dirty) return;
88 #ifdef EVAS_FRAME_QUEUING
91 im->cache->dirty = eina_inlist_remove(im->cache->dirty, EINA_INLIST_GET(im));
92 #ifdef EVAS_FRAME_QUEUING
98 _evas_cache_image_activ_add(Image_Entry *im)
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;
106 im->flags.cached = 1;
107 #ifdef EVAS_FRAME_QUEUING
108 LKL(im->cache->lock);
110 eina_hash_direct_add(im->cache->activ, im->cache_key, im);
111 #ifdef EVAS_FRAME_QUEUING
112 LKU(im->cache->lock);
117 _evas_cache_image_activ_del(Image_Entry *im)
119 if (!im->flags.activ) return;
120 if (!im->cache_key) return;
122 im->flags.cached = 0;
123 #ifdef EVAS_FRAME_QUEUING
124 LKL(im->cache->lock);
126 eina_hash_del(im->cache->activ, im->cache_key, im);
127 #ifdef EVAS_FRAME_QUEUING
128 LKU(im->cache->lock);
133 _evas_cache_image_lru_add(Image_Entry *im)
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;
141 im->flags.cached = 1;
142 #ifdef EVAS_FRAME_QUEUING
143 LKL(im->cache->lock);
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);
154 _evas_cache_image_lru_del(Image_Entry *im)
156 if (!im->flags.lru) return;
157 if (!im->cache_key) return;
159 im->flags.cached = 0;
160 #ifdef EVAS_FRAME_QUEUING
161 LKL(im->cache->lock);
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);
172 _evas_cache_image_lru_nodata_add(Image_Entry *im)
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);
179 im->flags.cached = 1;
180 #ifdef EVAS_FRAME_QUEUING
181 LKL(im->cache->lock);
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);
190 _evas_cache_image_lru_nodata_del(Image_Entry *im)
192 if (!im->flags.lru_nodata) return;
194 im->flags.cached = 0;
195 #ifdef EVAS_FRAME_QUEUING
196 LKL(im->cache->lock);
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);
205 _evas_cache_image_entry_delete(Evas_Cache_Image *cache, Image_Entry *ie)
208 if (cache->func.debug) cache->func.debug("deleting", ie);
209 #ifdef BUILD_ASYNC_PRELOAD
210 if (ie->flags.delete_me == 1) return;
213 ie->flags.delete_me = 1;
214 _evas_cache_image_entry_preload_remove(ie, NULL);
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);
224 cache->func.destructor(ie);
225 FREESTRC(ie->cache_key);
229 cache->func.surface_delete(ie);
231 #ifdef BUILD_ASYNC_PRELOAD
233 LKD(ie->lock_cancel);
235 #ifdef EVAS_FRAME_QUEUING
236 LKD(ie->lock_references);
238 cache->func.dealloc(ie);
242 _timestamp_compare(Image_Timestamp *tstamp, struct stat *st)
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)
252 if (tstamp->mtime_nsec != (unsigned long int)st->st_mtimensec)
260 _timestamp_build(Image_Timestamp *tstamp, struct stat *st)
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;
269 tstamp->mtime_nsec = (unsigned long int)st->st_mtimensec;
275 _evas_cache_image_entry_new(Evas_Cache_Image *cache,
277 Image_Timestamp *tstamp,
280 RGBA_Image_Loadopts *lo,
285 ie = cache->func.alloc();
288 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
292 if (hkey) ie->cache_key = eina_stringshare_add(hkey);
293 ie->flags.need_data = 1;
294 ie->space = EVAS_COLORSPACE_ARGB8888;
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));
303 #ifdef EVAS_FRAME_QUEUING
304 LKI(ie->lock_references);
306 #ifdef BUILD_ASYNC_PRELOAD
308 LKI(ie->lock_cancel);
311 if (lo) ie->load_opts = *lo;
314 *error = cache->func.constructor(ie);
315 if (*error != EVAS_LOAD_ERROR_NONE)
317 _evas_cache_image_entry_delete(cache, ie);
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);
328 _evas_cache_image_entry_surface_alloc__locked(Evas_Cache_Image *cache,
333 if ((ie->allocated.w == wmin) && (ie->allocated.h == hmin)) return;
334 if (cache->func.surface_alloc(ie, wmin, hmin))
341 ie->allocated.w = wmin;
342 ie->allocated.h = hmin;
346 _evas_cache_image_entry_surface_alloc(Evas_Cache_Image *cache,
347 Image_Entry *ie, int w, int h)
349 int wmin = w > 0 ? w : 1;
350 int hmin = h > 0 ? h : 1;
351 #ifdef BUILD_ASYNC_PRELOAD
354 _evas_cache_image_entry_surface_alloc__locked(cache, ie, wmin, hmin);
355 #ifdef BUILD_ASYNC_PRELOAD
360 #ifdef BUILD_ASYNC_PRELOAD
362 _evas_cache_image_async_heavy(void *data)
364 Evas_Cache_Image *cache;
365 Image_Entry *current;
372 pchannel = current->channel;
374 cache = current->cache;
376 if ((!current->flags.loaded) &&
377 ((Evas_Image_Load_Func*) current->info.module)->threadable)
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)
384 current->flags.loaded = 0;
385 _evas_cache_image_entry_surface_alloc(cache, current,
386 current->w, current->h);
390 current->flags.loaded = 1;
393 current->channel = pchannel;
394 // check the unload cancel flag
395 LKL(current->lock_cancel);
396 if (current->unload_cancel)
398 current->unload_cancel = EINA_FALSE;
399 cache->func.surface_delete(current);
400 current->flags.loaded = 0;
401 current->flags.preload_done = 0;
403 LKU(current->lock_cancel);
408 _evas_cache_image_async_end(void *data)
410 Image_Entry *ie = (Image_Entry *)data;
411 Evas_Cache_Target *tmp;
413 ie->cache->preload = eina_list_remove(ie->cache->preload, ie);
414 ie->cache->pending = eina_list_remove(ie->cache->pending, ie);
416 ie->flags.preload_done = ie->flags.loaded;
417 while ((tmp = ie->targets))
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));
428 _evas_cache_image_async_cancel(void *data)
430 Evas_Cache_Image *cache = NULL;
431 Image_Entry *ie = (Image_Entry *)data;
434 ie->cache->pending = eina_list_remove(ie->cache->pending, ie);
435 if ((ie->flags.delete_me) || (ie->flags.dirty))
437 ie->flags.delete_me = 0;
438 _evas_cache_image_entry_delete(ie->cache, ie);
441 if (ie->flags.loaded) _evas_cache_image_async_end(ie);
442 #ifdef EVAS_FRAME_QUEUING
443 LKL(ie->lock_references);
445 if (ie->references == 0)
447 _evas_cache_image_lru_add(ie);
450 #ifdef EVAS_FRAME_QUEUING
451 LKU(ie->lock_references);
453 if (cache) evas_cache_image_flush(cache);
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
459 _evas_cache_image_entry_preload_add(Image_Entry *ie, const void *target)
461 Evas_Cache_Target *tg;
463 if (ie->flags.preload_done) return 0;
465 tg = malloc(sizeof (Evas_Cache_Target));
469 ie->targets = (Evas_Cache_Target *)
470 eina_inlist_append(EINA_INLIST_GET(ie->targets), EINA_INLIST_GET(tg));
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,
484 _evas_cache_image_entry_preload_remove(Image_Entry *ie, const void *target)
488 Evas_Cache_Target *tg;
490 EINA_INLIST_FOREACH(ie->targets, tg)
492 if (tg->target == target)
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));
505 Evas_Cache_Target *tg;
510 ie->targets = (Evas_Cache_Target *)
511 eina_inlist_remove(EINA_INLIST_GET(ie->targets),
512 EINA_INLIST_GET(tg));
517 if ((!ie->targets) && (ie->preload) && (!ie->flags.pending))
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);
528 evas_cache_image_usage_get(Evas_Cache_Image *cache)
534 evas_cache_image_get(Evas_Cache_Image *cache)
540 evas_cache_image_set(Evas_Cache_Image *cache, unsigned int limit)
542 #ifdef EVAS_FRAME_QUEUING
545 if (cache->limit == limit)
547 #ifdef EVAS_FRAME_QUEUING
552 cache->limit = limit;
553 #ifdef EVAS_FRAME_QUEUING
556 evas_cache_image_flush(cache);
559 EAPI Evas_Cache_Image *
560 evas_cache_image_init(const Evas_Cache_Image_Func *cb)
562 Evas_Cache_Image *cache;
564 #ifdef BUILD_ASYNC_PRELOAD
565 if (_evas_cache_mutex_init++ == 0)
569 eina_condition_new(&cond_wakeup, &wakeup);
573 cache = calloc(1, sizeof(Evas_Cache_Image));
574 if (!cache) return NULL;
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
586 _evas_cache_image_free_cb(__UNUSED__ const Eina_Hash *hash, __UNUSED__ const void *key, void *data, void *fdata)
588 Eina_List **delete_list = fdata;
589 *delete_list = eina_list_prepend(*delete_list, data);
594 evas_cache_image_shutdown(Evas_Cache_Image *cache)
596 Eina_List *delete_list;
599 #ifdef EVAS_FRAME_QUEUING
603 if (cache->references != 0)
605 #ifdef EVAS_FRAME_QUEUING
610 #ifdef EVAS_FRAME_QUEUING
611 /* Release and destroy lock early ! */
616 #ifdef BUILD_ASYNC_PRELOAD
617 EINA_LIST_FREE(cache->preload, im)
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);
623 evas_async_events_process();
627 im = (Image_Entry *)cache->lru;
628 _evas_cache_image_entry_delete(cache, im);
630 while (cache->lru_nodata)
632 im = (Image_Entry *)cache->lru_nodata;
633 _evas_cache_image_entry_delete(cache, im);
635 /* This is mad, I am about to destroy image still alive, but we need to prevent leak. */
638 im = (Image_Entry *)cache->dirty;
639 _evas_cache_image_entry_delete(cache, im);
642 eina_hash_foreach(cache->activ, _evas_cache_image_free_cb, &delete_list);
645 _evas_cache_image_entry_delete(cache, eina_list_data_get(delete_list));
646 delete_list = eina_list_remove_list(delete_list, delete_list);
649 #ifdef BUILD_ASYNC_PRELOAD
650 /* Now wait for all pending image to die */
651 while (cache->pending)
653 evas_async_events_process();
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);
662 eina_hash_free(cache->activ);
663 eina_hash_free(cache->inactiv);
666 #ifdef BUILD_ASYNC_PRELOAD
667 if (--_evas_cache_mutex_init == 0)
669 eina_condition_free(&cond_wakeup);
677 evas_cache_image_request(Evas_Cache_Image *cache, const char *file,
678 const char *key, RGBA_Image_Loadopts *lo, int *error)
680 const char *ckey = "(null)";
683 Evas_Image_Load_Opts prevent = { 0, 0.0, 0, 0, 0, { 0, 0, 0, 0 }, EINA_FALSE };
685 int stat_done = 0, stat_failed = 0;
689 Image_Timestamp tstamp;
691 if ((!file) || ((!file) && (!key)))
693 *error = EVAS_LOAD_ERROR_GENERIC;
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);
704 memcpy(hkey + size, "//://", 5);
707 memcpy(hkey + size, ckey, key_length);
711 (lo->scale_down_by == 0) &&
713 ((lo->w == 0) || (lo->h == 0)) &&
714 ((lo->region.w == 0) || (lo->region.h == 0)) &&
715 (lo->orientation == 0)
722 memcpy(hkey + size, "//@/", 4);
724 size += eina_convert_xtoa(lo->scale_down_by, hkey + size);
727 size += eina_convert_dtoa(lo->dpi, hkey + size);
730 size += eina_convert_xtoa(lo->w, hkey + size);
733 size += eina_convert_xtoa(lo->h, hkey + size);
736 size += eina_convert_xtoa(lo->region.x, hkey + size);
739 size += eina_convert_xtoa(lo->region.y, hkey + size);
742 size += eina_convert_xtoa(lo->region.w, hkey + size);
745 size += eina_convert_xtoa(lo->region.h, hkey + size);
757 /* find image by key in active hash */
758 #ifdef EVAS_FRAME_QUEUING
761 im = eina_hash_find(cache->activ, hkey);
762 #ifdef EVAS_FRAME_QUEUING
770 if (stat(file, &st) < 0)
775 else if (!_timestamp_compare(&(im->tstamp), &st)) ok = 0;
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);
786 /* find image by key in inactive/lru hash */
787 #ifdef EVAS_FRAME_QUEUING
790 im = eina_hash_find(cache->inactiv, hkey);
791 #ifdef EVAS_FRAME_QUEUING
801 if (stat(file, &st) < 0)
806 else if (!_timestamp_compare(&(im->tstamp), &st)) ok = 0;
808 else if (!_timestamp_compare(&(im->tstamp), &st)) ok = 0;
812 /* remove from lru and make it active again */
813 _evas_cache_image_lru_del(im);
814 _evas_cache_image_activ_add(im);
817 /* as avtive cache find - if we match in lru and its invalid, dirty */
818 _evas_cache_image_dirty_add(im);
821 if (stat_failed) goto on_stat_error;
825 if (stat(file, &st) < 0) goto on_stat_error;
827 _timestamp_build(&tstamp, &st);
828 im = _evas_cache_image_entry_new(cache, hkey, &tstamp, file, key,
830 if (!im) goto on_stat_error;
831 if (cache->func.debug) cache->func.debug("request", im);
834 *error = EVAS_LOAD_ERROR_NONE;
835 #ifdef EVAS_FRAME_QUEUING
836 LKL(im->lock_references);
839 #ifdef EVAS_FRAME_QUEUING
840 LKU(im->lock_references);
846 if ((errno == ENOENT) || (errno == ENOTDIR) ||
847 (errno == ENAMETOOLONG) || (errno == ELOOP))
851 *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST;
853 else if ((errno == ENOMEM) || (errno == EOVERFLOW))
855 else if (errno == ENOMEM)
857 *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
858 else if (errno == EACCES)
859 *error = EVAS_LOAD_ERROR_PERMISSION_DENIED;
861 *error = EVAS_LOAD_ERROR_GENERIC;
863 if (im) _evas_cache_image_entry_delete(cache, im);
868 evas_cache_image_drop(Image_Entry *im)
870 Evas_Cache_Image *cache;
873 #ifdef EVAS_FRAME_QUEUING
874 LKL(im->lock_references);
877 if (im->references < 0) im->references = 0;
878 references = im->references;
879 #ifdef EVAS_FRAME_QUEUING
880 LKU(im->lock_references);
887 #ifdef EVAS_FRAME_QUEUING
890 if (im->ref_fq[0] != im->ref_fq[1])
900 #ifdef BUILD_ASYNC_PRELOAD
903 _evas_cache_image_entry_preload_remove(im, NULL);
910 _evas_cache_image_entry_delete(cache, im);
913 _evas_cache_image_lru_add(im);
914 if (cache) evas_cache_image_flush(cache);
919 evas_cache_image_data_not_needed(Image_Entry *im)
921 Evas_Cache_Image *cache;
924 /* FIXME: no one uses this api... well evas_cache_engine_parent_not_needed()
925 * does, but nothing uses that! */
927 #ifdef EVAS_FRAME_QUEUING
928 LKL(im->lock_references);
930 references = im->references;
931 #ifdef EVAS_FRAME_QUEUING
932 LKU(im->lock_references);
934 if (references > 1) return;
935 if ((im->flags.dirty) || (!im->flags.need_data)) return;
936 _evas_cache_image_lru_nodata_add(im);
940 evas_cache_image_dirty(Image_Entry *im, unsigned int x, unsigned int y, unsigned int w, unsigned int h)
942 Image_Entry *im_dirty = im;
943 Evas_Cache_Image *cache;
947 if (!(im->flags.dirty))
949 #ifdef EVAS_FRAME_QUEUING
950 LKL(im->lock_references);
952 references = im->references;
953 #ifdef EVAS_FRAME_QUEUING
954 LKU(im->lock_references);
957 // if ref 1 also copy if using shared cache as its read-only
958 if (references == 1) im_dirty = im;
965 evas_cache_image_copied_data(cache, im->w, im->h,
966 evas_cache_image_pixels(im),
967 im->flags.alpha, im->space);
968 if (!im_dirty) goto on_error;
969 if (cache->func.debug) cache->func.debug("dirty-src", im);
970 error = cache->func.dirty(im_dirty, im);
971 if (cache->func.debug) cache->func.debug("dirty-out", im_dirty);
972 #ifdef EVAS_FRAME_QUEUING
973 LKL(im_dirty->lock_references);
975 im_dirty->references = 1;
976 #ifdef EVAS_FRAME_QUEUING
977 LKU(im_dirty->lock_references);
979 evas_cache_image_drop(im);
981 _evas_cache_image_dirty_add(im_dirty);
984 if (cache->func.debug) cache->func.debug("dirty-region", im_dirty);
985 if (cache->func.dirty_region)
986 cache->func.dirty_region(im_dirty, x, y, w, h);
990 if (im_dirty) _evas_cache_image_entry_delete(cache, im_dirty);
991 evas_cache_image_drop(im);
996 evas_cache_image_alone(Image_Entry *im)
998 Evas_Cache_Image *cache;
999 Image_Entry *im_dirty = im;
1003 #ifdef EVAS_FRAME_QUEUING
1004 LKL(im->lock_references);
1006 references = im->references;
1007 #ifdef EVAS_FRAME_QUEUING
1008 LKU(im->lock_references);
1011 if (references <= 1)
1013 if (!im->flags.dirty) _evas_cache_image_dirty_add(im);
1019 im_dirty = evas_cache_image_copied_data(cache, im->w, im->h,
1020 evas_cache_image_pixels(im),
1023 if (!im_dirty) goto on_error;
1024 if (cache->func.debug) cache->func.debug("dirty-src", im);
1025 error = cache->func.dirty(im_dirty, im);
1026 if (cache->func.debug) cache->func.debug("dirty-out", im_dirty);
1027 #ifdef EVAS_FRAME_QUEUING
1028 LKL(im_dirty->lock_references);
1030 im_dirty->references = 1;
1031 #ifdef EVAS_FRAME_QUEUING
1032 LKU(im_dirty->lock_references);
1034 evas_cache_image_drop(im);
1039 if (im_dirty) _evas_cache_image_entry_delete(cache, im_dirty);
1040 evas_cache_image_drop(im);
1045 evas_cache_image_copied_data(Evas_Cache_Image *cache,
1046 unsigned int w, unsigned int h,
1047 DATA32 *image_data, int alpha, int cspace)
1051 if ((cspace == EVAS_COLORSPACE_YCBCR422P601_PL) ||
1052 (cspace == EVAS_COLORSPACE_YCBCR422P709_PL) ||
1053 (cspace == EVAS_COLORSPACE_YCBCR422601_PL))
1056 im = _evas_cache_image_entry_new(cache, NULL, NULL, NULL, NULL, NULL, NULL);
1057 if (!im) return NULL;
1059 im->flags.alpha = alpha;
1060 _evas_cache_image_entry_surface_alloc(cache, im, w, h);
1061 if (cache->func.copied_data(im, w, h, image_data, alpha, cspace) != 0)
1063 _evas_cache_image_entry_delete(cache, im);
1066 #ifdef EVAS_FRAME_QUEUING
1067 LKL(im->lock_references);
1070 #ifdef EVAS_FRAME_QUEUING
1071 LKU(im->lock_references);
1073 if (cache->func.debug) cache->func.debug("copied-data", im);
1078 evas_cache_image_data(Evas_Cache_Image *cache, unsigned int w, unsigned int h, DATA32 *image_data, int alpha, int cspace)
1082 if ((cspace == EVAS_COLORSPACE_YCBCR422P601_PL) ||
1083 (cspace == EVAS_COLORSPACE_YCBCR422P709_PL) ||
1084 (cspace == EVAS_COLORSPACE_YCBCR422601_PL))
1087 im = _evas_cache_image_entry_new(cache, NULL, NULL, NULL, NULL, NULL, NULL);
1088 if (!im) return NULL;
1091 im->flags.alpha = alpha;
1092 if (cache->func.data(im, w, h, image_data, alpha, cspace) != 0)
1094 _evas_cache_image_entry_delete(cache, im);
1097 #ifdef EVAS_FRAME_QUEUING
1098 LKL(im->lock_references);
1101 #ifdef EVAS_FRAME_QUEUING
1102 LKU(im->lock_references);
1104 if (cache->func.debug) cache->func.debug("data", im);
1109 evas_cache_image_surface_alloc(Image_Entry *im, unsigned int w, unsigned int h)
1111 Evas_Cache_Image *cache = im->cache;
1113 if ((im->space == EVAS_COLORSPACE_YCBCR422P601_PL) ||
1114 (im->space == EVAS_COLORSPACE_YCBCR422P709_PL) ||
1115 (im->space == EVAS_COLORSPACE_YCBCR422601_PL))
1118 _evas_cache_image_entry_surface_alloc(cache, im, w, h);
1119 if (cache->func.debug) cache->func.debug("surface-alloc", im);
1123 evas_cache_image_size_set(Image_Entry *im, unsigned int w, unsigned int h)
1125 Evas_Cache_Image *cache;
1126 Image_Entry *im2 = NULL;
1129 if ((im->space == EVAS_COLORSPACE_YCBCR422P601_PL) ||
1130 (im->space == EVAS_COLORSPACE_YCBCR422P709_PL) ||
1131 (im->space == EVAS_COLORSPACE_YCBCR422601_PL))
1133 if ((im->w == w) && (im->h == h)) return im;
1136 im2 = _evas_cache_image_entry_new(cache, NULL, NULL, NULL, NULL, NULL, &error);
1137 if (!im2) goto on_error;
1139 im2->flags.alpha = im->flags.alpha;
1140 im2->space = im->space;
1141 im2->load_opts = im->load_opts;
1142 _evas_cache_image_entry_surface_alloc(cache, im2, w, h);
1143 error = cache->func.size_set(im2, im, w, h);
1144 if (error != 0) goto on_error;
1145 #ifdef EVAS_FRAME_QUEUING
1146 LKL(im2->lock_references);
1148 im2->references = 1;
1149 #ifdef EVAS_FRAME_QUEUING
1150 LKU(im2->lock_references);
1152 evas_cache_image_drop(im);
1153 if (cache->func.debug) cache->func.debug("size_set", im2);
1157 if (im2) _evas_cache_image_entry_delete(cache, im2);
1158 evas_cache_image_drop(im);
1163 evas_cache_image_load_data(Image_Entry *im)
1165 #ifdef BUILD_ASYNC_PRELOAD
1166 Eina_Bool preload = EINA_FALSE;
1168 int error = EVAS_LOAD_ERROR_NONE;
1170 if ((im->flags.loaded) && (!im->flags.animated)) return error;
1171 #ifdef BUILD_ASYNC_PRELOAD
1174 preload = EINA_TRUE;
1175 if (!im->flags.pending)
1177 im->cache->preload = eina_list_remove(im->cache->preload, im);
1178 im->cache->pending = eina_list_append(im->cache->pending, im);
1179 im->flags.pending = 1;
1180 evas_preload_thread_cancel(im->preload);
1182 evas_async_events_process();
1186 eina_condition_wait(&cond_wakeup);
1188 evas_async_events_process();
1194 if ((im->flags.loaded) && (!im->flags.animated)) return error;
1197 im->flags.in_progress = EINA_TRUE;
1198 error = im->cache->func.load(im);
1199 im->flags.in_progress = EINA_FALSE;
1200 #ifdef BUILD_ASYNC_PRELOAD
1203 im->flags.loaded = 1;
1204 if (im->cache->func.debug) im->cache->func.debug("load", im);
1205 if (error != EVAS_LOAD_ERROR_NONE)
1207 _evas_cache_image_entry_surface_alloc(im->cache, im, im->w, im->h);
1208 im->flags.loaded = 0;
1210 #ifdef BUILD_ASYNC_PRELOAD
1211 if (preload) _evas_cache_image_async_end(im);
1217 evas_cache_image_unload_data(Image_Entry *im)
1219 if (im->flags.in_progress) return;
1220 evas_cache_image_preload_cancel(im, NULL);
1221 #ifdef BUILD_ASYNC_PRELOAD
1222 LKL(im->lock_cancel);
1223 if (LKT(im->lock) == EINA_FALSE) /* can't get image lock - busy async load */
1225 im->unload_cancel = EINA_TRUE;
1226 LKU(im->lock_cancel);
1229 LKU(im->lock_cancel);
1231 if ((!im->flags.loaded) || (!im->file) || (!im->info.module) ||
1234 #ifdef BUILD_ASYNC_PRELOAD
1239 im->cache->func.destructor(im);
1240 #ifdef BUILD_ASYNC_PRELOAD
1243 //FIXME: imagedataunload - inform owners
1247 _evas_cache_image_unload_cb(__UNUSED__ const Eina_Hash *hash, __UNUSED__ const void *key, void *data, __UNUSED__ void *fdata)
1249 evas_cache_image_unload_data(data);
1254 evas_cache_image_unload_all(Evas_Cache_Image *cache)
1258 EINA_INLIST_FOREACH(cache->lru, im) evas_cache_image_unload_data(im);
1259 EINA_INLIST_FOREACH(cache->lru_nodata, im) evas_cache_image_unload_data(im);
1260 eina_hash_foreach(cache->activ, _evas_cache_image_unload_cb, NULL);
1261 eina_hash_foreach(cache->inactiv, _evas_cache_image_unload_cb, NULL);
1265 evas_cache_image_is_loaded(Image_Entry *im)
1267 if (im->flags.loaded) return EINA_TRUE;
1272 evas_cache_image_preload_data(Image_Entry *im, const void *target)
1274 #ifdef BUILD_ASYNC_PRELOAD
1275 RGBA_Image *img = (RGBA_Image *)im;
1277 if ((im->flags.loaded) && (img->image.data))
1279 evas_object_inform_call_image_preloaded((Evas_Object *)target);
1282 im->flags.loaded = 0;
1283 if (!_evas_cache_image_entry_preload_add(im, target))
1284 evas_object_inform_call_image_preloaded((Evas_Object *)target);
1286 evas_cache_image_load_data(im);
1287 evas_object_inform_call_image_preloaded((Evas_Object *)target);
1292 evas_cache_image_preload_cancel(Image_Entry *im, const void *target)
1294 #ifdef BUILD_ASYNC_PRELOAD
1295 if (!target) return;
1296 _evas_cache_image_entry_preload_remove(im, target);
1303 static int total = 0;
1306 _dump_img(Image_Entry *im, const char *type)
1308 total += im->cache->func.mem_size_get(im);
1309 printf("%s: %4i: %4ib, %4ix%4i alloc[%4ix%4i] [%s] [%s]\n",
1312 im->cache->func.mem_size_get(im),
1313 im->w, im->h, im->allocated.w, im->allocated.h,
1318 _dump_cache_active(__UNUSED__ const Eina_Hash *hash, __UNUSED__ const void *key, void *data, void *fdata __UNUSED__)
1320 Image_Entry *im = data;
1321 _dump_img(im, "ACTIVE");
1326 _dump_cache(Evas_Cache_Image *cache)
1330 printf("--CACHE DUMP----------------------------------------------------\n");
1331 printf("cache: %ikb / %ikb\n",
1332 cache->usage / 1024,
1333 cache->limit / 1024);
1334 printf("................................................................\n");
1336 EINA_INLIST_FOREACH(cache->lru_nodata, im)
1337 _dump_img(im, "NODATA");
1338 EINA_INLIST_FOREACH(cache->lru, im)
1339 _dump_img(im, "DATA ");
1344 eina_hash_foreach(cache->activ, _dump_cache_active, NULL);
1349 evas_cache_image_flush(Evas_Cache_Image *cache)
1354 if (cache->limit == (unsigned int)-1) return -1;
1356 while ((cache->lru) && (cache->limit < (unsigned int)cache->usage))
1360 im = (Image_Entry *)cache->lru->last;
1361 _evas_cache_image_entry_delete(cache, im);
1364 while ((cache->lru_nodata) && (cache->limit < (unsigned int)cache->usage))
1368 im = (Image_Entry *) cache->lru_nodata->last;
1369 _evas_cache_image_lru_nodata_del(im);
1370 cache->func.surface_delete(im);
1371 im->flags.loaded = 0;
1374 return cache->usage;
1378 evas_cache_image_empty(Evas_Cache_Image *cache)
1382 im = _evas_cache_image_entry_new(cache, NULL, NULL, NULL, NULL, NULL, NULL);
1383 if (!im) return NULL;
1384 #ifdef EVAS_FRAME_QUEUING
1385 LKL(im->lock_references);
1388 #ifdef EVAS_FRAME_QUEUING
1389 LKU(im->lock_references);
1395 evas_cache_image_colorspace(Image_Entry *im, int cspace)
1397 if (im->space == cspace) return;
1399 im->cache->func.color_space(im, cspace);
1403 evas_cache_private_from_image_entry_get(Image_Entry *im)
1405 return (void *)im->cache->data;
1409 evas_cache_private_get(Evas_Cache_Image *cache)
1415 evas_cache_private_set(Evas_Cache_Image *cache, const void *data)
1417 cache->data = (void *)data;
1421 evas_cache_image_pixels(Image_Entry *im)
1423 return im->cache->func.surface_pixels(im);
1427 evas_cache_image_wakeup(void)
1429 #ifdef BUILD_ASYNC_PRELOAD
1430 if (_evas_cache_mutex_init > 0)
1431 eina_condition_broadcast(&cond_wakeup);