From 7d1117fe4f594c9ef8c7faef3dea701db0526255 Mon Sep 17 00:00:00 2001 From: Miika Pekkarinen Date: Sun, 29 Jan 2006 14:16:30 +0000 Subject: Cache wps image files to allow really fast boot. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@8483 a1c6a512-1295-4272-9138-f99709370657 --- apps/gui/gwps-common.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 89 insertions(+), 3 deletions(-) (limited to 'apps/gui/gwps-common.c') diff --git a/apps/gui/gwps-common.c b/apps/gui/gwps-common.c index b32deba479..9b32b862ff 100644 --- a/apps/gui/gwps-common.c +++ b/apps/gui/gwps-common.c @@ -75,6 +75,15 @@ static char* skip_utf8_bom(char* buf) * a..z and A..Z */ #ifdef HAVE_LCD_BITMAP +struct bmp_cache_entry { + char filename[MAX_PATH]; + int width; + int height; + int size; +}; +static int bmp_cache_fd = -1; +static bool bmp_cache_write; + static int get_image_id(int c) { if(c >= 'a' && c <= 'z') @@ -83,7 +92,60 @@ static int get_image_id(int c) c = c - 'A' + 26; return c; } + +void wps_initialize_bmp_cache(const char *file) +{ + bmp_cache_fd = open(file, O_RDONLY); + bmp_cache_write = 0; + if (bmp_cache_fd < 0) + { + bmp_cache_fd = open(file, O_WRONLY | O_CREAT); + bmp_cache_write = 1; + } +} + +void wps_close_bmp_cache(const char *file) +{ + if (bmp_cache_fd >= 0) + { + close(bmp_cache_fd); + bmp_cache_fd = -1; + return ; + } + + /* Remove the file if cache read failed. */ + remove(file); +} + +static int read_bmp_from_cache(const char *filename, struct bitmap *bm, + int buflen) +{ + struct bmp_cache_entry c; + int rc; + + if (!bmp_cache_fd || bmp_cache_write) + return -1; + + rc = read(bmp_cache_fd, &c, sizeof(struct bmp_cache_entry)); + if (rc != sizeof(struct bmp_cache_entry)) + return -2; + + if (buflen < c.size) + return -3; + + if (strcasecmp(filename, c.filename)) + return -4; + + bm->width = c.width; + bm->height = c.height; + rc = read(bmp_cache_fd, bm->data, c.size); + if (rc != c.size) + return -4; + + return c.size; +} #endif + /* * parse the given buffer for following static tags: * %x - load image for always display @@ -217,11 +279,35 @@ bool wps_data_preload_tags(struct wps_data *data, char *buf, /* load the image */ data->img[n].bm.data = data->img_buf_ptr; - ret = read_bmp_file(imgname, &data->img[n].bm, - data->img_buf_free, - FORMAT_ANY|FORMAT_TRANSPARENT); + ret = read_bmp_from_cache(imgname, &data->img[n].bm, + data->img_buf_free); + + if (ret < 0) + { + if (!bmp_cache_write) + { + close(bmp_cache_fd); + bmp_cache_fd = -1; + } + + ret = read_bmp_file(imgname, &data->img[n].bm, + data->img_buf_free, + FORMAT_ANY|FORMAT_TRANSPARENT); + } + if (ret > 0) { + if (bmp_cache_write && bmp_cache_fd >= 0) + { + struct bmp_cache_entry c; + strncpy(c.filename, imgname, sizeof(c.filename)-1); + c.width = data->img[n].bm.width; + c.height = data->img[n].bm.height; + c.size = ret; + write(bmp_cache_fd, &c, sizeof(struct bmp_cache_entry)); + write(bmp_cache_fd, data->img_buf_ptr, ret); + } + data->img_buf_ptr += ret; data->img_buf_free -= ret; data->img[n].loaded = true; -- cgit v1.2.3