evas cache image: compare with cached image file

As cache2 knows cached image could be not matched even though
hash key is not different.

Please refer to the following comment of evas_cache2_image_open.

   /* image we found doesn't match what's on disk (stat info wise)
    * so dirty the active cache entry so we never find it again. this
    * also implicitly guarantees that we only have 1 active copy
    * of an image at a given key. we wither find it and keep re-reffing
    * it or we dirty it and get it out */

The hash key is created base on the image file address.
If the image file address to find does not equal cached image file address
then it means that the cached image is no longer valid.

This case could happen with the following step.

(1) Call evas_object_image_memfile_set with content data A
(2) Call evas_object_image_memfile_set with content data B
(3) Add timer with short time (ex: 0.01 sec)
(4) Delete A image, and add A image in timer callback
(5) Delete B image, and add B image in timer callback

Sometimes you could see image of A from the B image, because newly created
image at step 5 has same address of setp 1.

Reviewed-by: Cedric BAIL <cedric.bail@free.fr>
Differential Revision: https://phab.enlightenment.org/D6870
This commit is contained in:
Shinwoo Kim 2018-08-23 05:56:58 +00:00 committed by Christopher Michael
parent 03cb1ebae5
commit 5a68a63ab7
2 changed files with 133 additions and 16 deletions

View File

@ -796,28 +796,42 @@ evas_cache_image_mmap_request(Evas_Cache_Image *cache,
/* find image by key in active mmap hash */
SLKL(engine_lock);
im = eina_hash_find(cache->mmap_activ, hkey);
if ((im) && (!im->load_failed)) goto on_ok;
else if ((im) && (im->load_failed))
if (im)
{
_evas_cache_image_dirty_add(im);
im = NULL;
if (im->f != f)
{
/* as active cache find - if we match in lru and its invalid, dirty */
_evas_cache_image_dirty_add(im);
/* this image never used, so it have to be deleted */
_evas_cache_image_entry_delete(cache, im);
im = NULL;
}
else if (!im->load_failed) goto on_ok;
else if (im->load_failed)
{
_evas_cache_image_dirty_add(im);
im = NULL;
}
}
/* find image by key in inactive/lru hash */
im = eina_hash_find(cache->mmap_inactiv, hkey);
if ((im) && (!im->load_failed))
if (im)
{
_evas_cache_image_lru_del(im);
_evas_cache_image_activ_add(im);
goto on_ok;
}
else if ((im) && (im->load_failed))
{
/* as active cache find - if we match in lru and its invalid, dirty */
_evas_cache_image_dirty_add(im);
/* this image never used, so it have to be deleted */
_evas_cache_image_entry_delete(cache, im);
im = NULL;
if (im->f != f)
{
/* as active cache find - if we match in lru and its invalid, dirty */
_evas_cache_image_dirty_add(im);
/* this image never used, so it have to be deleted */
_evas_cache_image_entry_delete(cache, im);
im = NULL;
}
else if (!im->load_failed)
{
_evas_cache_image_lru_del(im);
_evas_cache_image_activ_add(im);
goto on_ok;
}
}
im = _evas_cache_image_entry_new(cache, hkey, NULL, f, NULL, key, lo, error);

View File

@ -627,6 +627,108 @@ EFL_START_TEST(evas_object_image_partially_load_orientation)
}
EFL_END_TEST
static int
_file_to_memory(const char *filename, char **result)
{
int size;
FILE *f;
f = fopen(filename, "rb");
if (f == NULL)
{
*result = NULL;
return -1;
}
fseek(f, 0, SEEK_END);
size = ftell(f);
fseek(f, 0, SEEK_SET);
*result = (char *)malloc(size + 1);
if ((size_t)size != fread(*result, sizeof(char), size, f))
{
free(*result);
return -1;
}
fclose(f);
(*result)[size] = 0;
return size;
}
EFL_START_TEST(evas_object_image_cached_data_comparision)
{
int i;
int size, size2;
char *content, *content2;
int w, h, n_w, n_h;
int w2, h2, n_w2, n_h2;
const uint32_t *d, *n_d;
const uint32_t *d2, *n_d2;
const char *img_path, *img_path2;
Evas_Object *img, *img2;
Evas *e = _setup_evas();
img_path = TESTS_IMG_DIR "/Pic1.png";
size = _file_to_memory(img_path, &content);
fail_if(size < 0);
img = evas_object_image_add(e);
evas_object_image_memfile_set(img, content, size, "png", NULL);
evas_object_image_fill_set(img, 0, 0, 250, 250);
evas_object_resize(img, 250, 250);
evas_object_move(img, 0, 0);
evas_object_show(img);
evas_object_image_size_get(img, &w, &h);
d = evas_object_image_data_get(img, EINA_FALSE);
img_path2 = TESTS_IMG_DIR "/Pic4.png";
size2 = _file_to_memory(img_path2, &content2);
img2 = evas_object_image_add(e);
evas_object_image_memfile_set(img2, content2, size2, "png", NULL);
evas_object_image_fill_set(img2, 0, 0, 250, 250);
evas_object_resize(img2, 250, 250);
evas_object_move(img2, 250, 250);
evas_object_show(img2);
evas_object_image_size_get(img, &w2, &h2);
d2 = evas_object_image_data_get(img, EINA_FALSE);
for (i = 0; i < 100; i++)
{
evas_object_del(img);
evas_object_del(img2);
img = evas_object_image_add(e);
evas_object_image_memfile_set(img, content, size, "png", NULL);
evas_object_image_fill_set(img, 0, 0, 250, 250);
evas_object_resize(img, 250, 250);
evas_object_move(img, 0, 0);
evas_object_show(img);
evas_object_image_size_get(img, &n_w, &n_h);
n_d = evas_object_image_data_get(img, EINA_FALSE);
fail_if(w != n_w || h != n_h);
fail_if(memcmp(d, n_d, w * h * 4));
img2 = evas_object_image_add(e);
evas_object_image_memfile_set(img2, content2, size2, "png", NULL);
evas_object_image_fill_set(img2, 0, 0, 250, 250);
evas_object_resize(img2, 250, 250);
evas_object_move(img2, 250, 250);
evas_object_show(img2);
evas_object_image_size_get(img, &n_w2, &n_h2);
n_d2 = evas_object_image_data_get(img, EINA_FALSE);
fail_if(w2 != n_w2 || h2 != n_h2);
fail_if(memcmp(d2, n_d2, w2 * h2 * 4));
}
}
EFL_END_TEST
EFL_START_TEST(evas_object_image_defaults)
{
Evas *e = _setup_evas();
@ -891,6 +993,7 @@ void evas_test_image_object(TCase *tc)
tcase_add_test(tc, evas_object_image_map_unmap);
#endif
tcase_add_test(tc, evas_object_image_partially_load_orientation);
tcase_add_test(tc, evas_object_image_cached_data_comparision);
}