Krolyxon
Commit 47af0dd

Branches

Refs →

nsxiv47af0dd

Cache out of view thumbnails in the background

Author
Bert Münnich
Date
2015-01-04 21:19 +0100
Committer
Bert Münnich · 2015-01-04 21:24 +0100
Parent
9b9294b
Commit
47af0dd7b5e154fb64d8b4d6c5302ba905055799

+45 −25

5 files changed

  1. M commands.c +1 −1
  2. M main.c +15 −9
  3. M thumbs.c +22 −10
  4. M thumbs.h +4 −3
  5. M types.h +3 −2
M commands.c View
@@ -126,7 +126,7 @@ bool cg_reload_image(arg_t a)
126126 load_image(fileidx);
127127 } else {
128128 win_set_cursor(&win, CURSOR_WATCH);
129− if (!tns_load(&tns, fileidx, true)) {
129+ if (!tns_load(&tns, fileidx, true, false)) {
130130 remove_file(fileidx, false);
131131 tns.dirty = true;
132132 }
M main.c View
@@ -385,7 +385,10 @@ void update_info(void)
385385 r->p = r->buf;
386386 if (mode == MODE_THUMB) {
387387 if (tns.loadnext < tns.end) {
388− bar_put(l, "Loading... %0*d", fw, MAX(tns.loadnext, 1));
388+ bar_put(l, "Loading... %0*d", fw, tns.loadnext + 1);
389+ ow_info = false;
390+ } else if (tns.initnext < filecnt) {
391+ bar_put(l, "Caching... %0*d", fw, tns.initnext + 1);
389392 ow_info = false;
390393 } else {
391394 ow_info = true;
@@ -453,7 +456,7 @@ void reset_cursor(void)
453456 }
454457 }
455458 } else {
456− if (tns.loadnext < tns.end)
459+ if (tns.loadnext < tns.end || tns.initnext < filecnt)
457460 cursor = CURSOR_WATCH;
458461 else
459462 cursor = CURSOR_ARROW;
@@ -694,26 +697,29 @@ void run(void)
694697 int xfd;
695698 fd_set fds;
696699 struct timeval timeout;
697− bool discard, load_thumb, to_set;
700+ bool discard, init_thumb, load_thumb, to_set;
698701 XEvent ev, nextev;
699702
700703 while (true) {
701704 to_set = check_timeouts(&timeout);
705+ init_thumb = mode == MODE_THUMB && tns.initnext < filecnt;
702706 load_thumb = mode == MODE_THUMB && tns.loadnext < tns.end;
703707
704− if ((load_thumb || to_set || info.fd != -1) &&
708+ if ((init_thumb || load_thumb || to_set || info.fd != -1) &&
705709 XPending(win.env.dpy) == 0)
706710 {
707711 if (load_thumb) {
708712 set_timeout(redraw, TO_REDRAW_THUMBS, false);
709− if (!tns_load(&tns, tns.loadnext, false)) {
713+ if (!tns_load(&tns, tns.loadnext, false, false)) {
710714 remove_file(tns.loadnext, false);
711715 tns.dirty = true;
712716 }
713− while (tns.loadnext < tns.end && tns.thumbs[tns.loadnext].im != NULL)
714− tns.loadnext++;
715717 if (tns.loadnext >= tns.end)
716718 redraw();
719+ } else if (init_thumb) {
720+ set_timeout(redraw, TO_REDRAW_THUMBS, false);
721+ if (!tns_load(&tns, tns.initnext, false, true))
722+ remove_file(tns.initnext, false);
717723 } else {
718724 xfd = ConnectionNumber(win.env.dpy);
719725 FD_ZERO(&fds);
@@ -899,8 +905,8 @@ int main(int argc, char **argv)
899905 if (options->thumb_mode) {
900906 mode = MODE_THUMB;
901907 tns_init(&tns, files, &filecnt, &fileidx, &win);
902− while (!tns_load(&tns, 0, false))
903− remove_file(0, false);
908+ while (!tns_load(&tns, fileidx, false, false))
909+ remove_file(fileidx, false);
904910 } else {
905911 mode = MODE_IMAGE;
906912 tns.thumbs = NULL;
M thumbs.c View
@@ -152,7 +152,7 @@ void tns_clean_cache(tns_t *tns)
152152 }
153153
154154
155−void tns_init(tns_t *tns, const fileinfo_t *files, const int *cnt, int *sel,
155+void tns_init(tns_t *tns, fileinfo_t *files, const int *cnt, int *sel,
156156 win_t *win)
157157 {
158158 int len;
@@ -169,7 +169,8 @@ void tns_init(tns_t *tns, const fileinfo_t *files, const int *cnt, int *sel,
169169 }
170170 tns->files = files;
171171 tns->cnt = cnt;
172− tns->loadnext = tns->first = tns->end = tns->r_first = tns->r_end = 0;
172+ tns->initnext = tns->loadnext = 0;
173+ tns->first = tns->end = tns->r_first = tns->r_end = 0;
173174 tns->sel = sel;
174175 tns->win = win;
175176 tns->dirty = false;
@@ -237,7 +238,7 @@ Imlib_Image tns_scale_down(Imlib_Image im, int dim)
237238 return im;
238239 }
239240
240−bool tns_load(tns_t *tns, int n, bool force)
241+bool tns_load(tns_t *tns, int n, bool force, bool cache_only)
241242 {
242243 int w, h;
243244 int maxwh = thumb_sizes[ARRLEN(thumb_sizes)-1];
@@ -245,8 +246,8 @@ bool tns_load(tns_t *tns, int n, bool force)
245246 char *cfile;
246247 float zw, zh;
247248 thumb_t *t;
249+ fileinfo_t *file;
248250 Imlib_Image im = NULL;
249− const fileinfo_t *file;
250251
251252 if (tns == NULL || tns->thumbs == NULL)
252253 return false;
@@ -261,6 +262,7 @@ bool tns_load(tns_t *tns, int n, bool force)
261262 if (t->im != NULL) {
262263 imlib_context_set_image(t->im);
263264 imlib_free_image();
265+ t->im = NULL;
264266 }
265267
266268 if (!force) {
@@ -345,10 +347,10 @@ bool tns_load(tns_t *tns, int n, bool force)
345347 warn("could not open image: %s", file->name);
346348 return false;
347349 }
350+ imlib_context_set_image(im);
348351
349352 if (!cache_hit) {
350353 #if HAVE_LIBEXIF
351− imlib_context_set_image(im);
352354 exif_auto_orientate(file);
353355 #endif
354356 im = tns_scale_down(im, maxwh);
@@ -357,12 +359,22 @@ bool tns_load(tns_t *tns, int n, bool force)
357359 tns_cache_write(im, file->path, true);
358360 }
359361
360− t->im = tns_scale_down(im, thumb_sizes[tns->zl]);
361− imlib_context_set_image(t->im);
362− t->w = imlib_image_get_width();
363− t->h = imlib_image_get_height();
362+ if (cache_only) {
363+ imlib_free_image_and_decache();
364+ } else {
365+ t->im = tns_scale_down(im, thumb_sizes[tns->zl]);
366+ imlib_context_set_image(t->im);
367+ t->w = imlib_image_get_width();
368+ t->h = imlib_image_get_height();
369+ tns->dirty = true;
370+ }
371+ file->flags |= FF_TN_INIT;
372+
373+ if (n == tns->initnext)
374+ while (++tns->initnext < *tns->cnt && ((++file)->flags & FF_TN_INIT));
375+ if (n == tns->loadnext && !cache_only)
376+ while (++tns->loadnext < tns->end && (++t)->im != NULL);
364377
365− tns->dirty = true;
366378 return true;
367379 }
368380
M thumbs.h View
@@ -34,10 +34,11 @@ typedef struct {
3434 } thumb_t;
3535
3636 typedef struct {
37− const fileinfo_t *files;
37+ fileinfo_t *files;
3838 thumb_t *thumbs;
3939 const int *cnt;
4040 int *sel;
41+ int initnext;
4142 int loadnext;
4243 int first, end;
4344 int r_first, r_end;
@@ -56,10 +57,10 @@ typedef struct {
5657
5758 void tns_clean_cache(tns_t*);
5859
59−void tns_init(tns_t*, const fileinfo_t*, const int*, int*, win_t*);
60+void tns_init(tns_t*, fileinfo_t*, const int*, int*, win_t*);
6061 void tns_free(tns_t*);
6162
62−bool tns_load(tns_t*, int, bool);
63+bool tns_load(tns_t*, int, bool, bool);
6364 void tns_unload(tns_t*, int);
6465
6566 void tns_render(tns_t*);
M types.h View
@@ -65,8 +65,9 @@ typedef enum {
6565 } cursor_t;
6666
6767 typedef enum {
68− FF_WARN = 1,
69− FF_MARK = 2
68+ FF_WARN = 1,
69+ FF_MARK = 2,
70+ FF_TN_INIT = 4
7071 } fileflags_t;
7172
7273 typedef struct {