From 46fc9fc756d28ff45e30d962eb98fb1b8d80586c Mon Sep 17 00:00:00 2001 From: Cyril Hrubis Date: Sat, 9 Jun 2012 00:27:14 +0200 Subject: [PATCH] spiv: Split the caches for resized and original images. --- demos/spiv/image_cache.c | 6 +++--- demos/spiv/image_cache.h | 10 ++++++---- demos/spiv/spiv.c | 45 ++++++++++++++++++++++++++++++++++----------- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/demos/spiv/image_cache.c b/demos/spiv/image_cache.c index 4a8c2b73..4cb194f5 100644 --- a/demos/spiv/image_cache.c +++ b/demos/spiv/image_cache.c @@ -47,7 +47,7 @@ struct image_cache { struct image *end; }; -static size_t read_total_memory(void) +size_t image_cache_get_ram_size(void) { FILE *f; size_t ret; @@ -84,7 +84,7 @@ static size_t image_size(struct image *img) return image_size2(img->ctx, img->path); } -struct image_cache *image_cache_create(unsigned int max_size) +struct image_cache *image_cache_create(unsigned int max_size_bytes) { struct image_cache *self; @@ -93,7 +93,7 @@ struct image_cache *image_cache_create(unsigned int max_size) if (self == NULL) return NULL; - self->max_size = max_size ? max_size : read_total_memory(); + self->max_size = max_size_bytes; self->cur_size = sizeof(struct image_cache); self->root = NULL; diff --git a/demos/spiv/image_cache.h b/demos/spiv/image_cache.h index e9467093..023ccd85 100644 --- a/demos/spiv/image_cache.h +++ b/demos/spiv/image_cache.h @@ -32,12 +32,14 @@ struct image_cache; /* + * Returns size of the ram in kbytes. + */ +size_t image_cache_get_ram_size(void); + +/* * Creates an image cache with maximal memory size. - * - * When memory size is set to zero it's set to 10% of total machine memory - * (as reported by /proc/meminfo). */ -struct image_cache *image_cache_create(unsigned int max_size); +struct image_cache *image_cache_create(unsigned int max_size_bytes); /* * Returns cached image, or NULL. diff --git a/demos/spiv/spiv.c b/demos/spiv/spiv.c index ec5a6569..cec4ab65 100644 --- a/demos/spiv/spiv.c +++ b/demos/spiv/spiv.c @@ -85,12 +85,14 @@ static int image_loader_callback(GP_ProgressCallback *self) struct loader_params { const char *img_path; + int show_progress; int show_progress_once; int show_info; /* cached loaded images */ - struct image_cache *image_cache; + struct image_cache *img_resized_cache; + struct image_cache *img_orig_cache; }; static float calc_img_size(uint32_t img_w, uint32_t img_h, @@ -134,7 +136,7 @@ GP_Context *load_image(struct loader_params *params, int elevate) GP_ProgressCallback callback = {.callback = image_loader_callback, .priv = "Loading image"}; - img = image_cache_get(params->image_cache, params->img_path, 0, 0, elevate); + img = image_cache_get(params->img_orig_cache, params->img_path, 0, 0, elevate); /* Image not cached, load it */ if (img == NULL) { @@ -160,7 +162,7 @@ GP_Context *load_image(struct loader_params *params, int elevate) img = tmp; } - image_cache_put(params->image_cache, img, params->img_path, 0, 0); + image_cache_put(params->img_orig_cache, img, params->img_path, 0, 0); cpu_timer_stop(&timer); } @@ -176,7 +178,7 @@ GP_Context *load_resized_image(struct loader_params *params, GP_Size w, GP_Size GP_ProgressCallback callback = {.callback = image_loader_callback}; /* Try to get resized cached image */ - img = image_cache_get(params->image_cache, params->img_path, cookie, resampling_method, 1); + img = image_cache_get(params->img_resized_cache, params->img_path, cookie, resampling_method, 1); if (img != NULL) return img; @@ -211,7 +213,7 @@ GP_Context *load_resized_image(struct loader_params *params, GP_Size w, GP_Size if (img == NULL) return NULL; - image_cache_put(params->image_cache, img, params->img_path, cookie, resampling_method); + image_cache_put(params->img_resized_cache, img, params->img_path, cookie, resampling_method); return img; } @@ -268,12 +270,13 @@ static void *image_loader(void *ptr) w = img->w; h = img->h; - img = load_resized_image(params, img->w * rat + 0.5, img->h * rat + 0.5, rat); + img = load_resized_image(params, w * rat + 0.5, h * rat + 0.5, rat); if (img == NULL) return NULL; - image_cache_print(params->image_cache); + image_cache_print(params->img_resized_cache); + image_cache_print(params->img_orig_cache); switch (rotate) { case 0: @@ -475,17 +478,34 @@ static int wait_for_event(int sleep_msec) } } +static void init_caches(struct loader_params *params) +{ + size_t size = image_cache_get_ram_size(); + unsigned int resized_size = (1024 * size)/10; + unsigned int orig_size = (1024 * size)/50; + + if (resized_size > 100 * 1024 * 1024) + resized_size = 100 * 1024 * 1024; + + if (orig_size > 20 * 1024 * 1024) + orig_size = 20 * 1024 * 1024; + + GP_DEBUG(1, "Cache sizes original = %u, resized = %u", + orig_size, resized_size); + + params->img_resized_cache = image_cache_create(resized_size); + params->img_orig_cache = image_cache_create(orig_size); +} + int main(int argc, char *argv[]) { GP_Context *context = NULL; const char *backend_opts = "X11"; int sleep_sec = -1; - struct loader_params params = {NULL, 0, 0, 0, NULL}; + struct loader_params params = {NULL, 0, 0, 0, NULL, NULL}; int opt, debug_level = 0; GP_PixelType emul_type = GP_PIXEL_UNKNOWN; - params.image_cache = image_cache_create(0); - while ((opt = getopt(argc, argv, "b:cd:e:fIPs:r:")) != -1) { switch (opt) { case 'I': @@ -535,6 +555,8 @@ int main(int argc, char *argv[]) signal(SIGSEGV, sighandler); signal(SIGBUS, sighandler); signal(SIGABRT, sighandler); + + init_caches(¶ms); init_backend(backend_opts); @@ -598,7 +620,8 @@ int main(int argc, char *argv[]) show_image(¶ms, NULL); break; case GP_KEY_D: - image_cache_drop(params.image_cache); + image_cache_drop(params.img_resized_cache); + image_cache_drop(params.img_orig_cache); break; case GP_KEY_ESC: case GP_KEY_ENTER: -- 2.11.4.GIT