Commit: 3e271f460a52bdb5fe3f53085e832755b5b82c15
Author: Bastien Montagne
Date:   Sat Apr 18 11:09:49 2015 +0200
Branches: ui-preview-buttons
https://developer.blender.org/rB3e271f460a52bdb5fe3f53085e832755b5b82c15

Previews: use a ghash to store previews of every path.

Simpler, sanier, and avoids Icons to have to own ImagePreview data even!

===================================================================

M       source/blender/blenkernel/BKE_icons.h
M       source/blender/blenkernel/intern/icons.c
M       source/blender/makesrna/intern/rna_main_api.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_icons.h 
b/source/blender/blenkernel/BKE_icons.h
index ff3157e..58f3edf 100644
--- a/source/blender/blenkernel/BKE_icons.h
+++ b/source/blender/blenkernel/BKE_icons.h
@@ -90,7 +90,7 @@ void BKE_previewimg_free_id(struct ID *id);
 /* create a new preview image */
 struct PreviewImage *BKE_previewimg_create(void);
 
-struct PreviewImage *BKE_previewimg_thumbnail_create(const char *path, int 
source);
+struct PreviewImage *BKE_previewimg_thumbnail_create(const char *path, const 
int source, bool force_update);
 
 /* create a copy of the preview image */
 struct PreviewImage *BKE_previewimg_copy(struct PreviewImage *prv);
diff --git a/source/blender/blenkernel/intern/icons.c 
b/source/blender/blenkernel/intern/icons.c
index dd85565..75a1f61 100644
--- a/source/blender/blenkernel/intern/icons.c
+++ b/source/blender/blenkernel/intern/icons.c
@@ -65,6 +65,7 @@ static int gNextIconId = 1;
 
 static int gFirstIconId = 1;
 
+static GHash *gFilePreviews = NULL;
 
 static void icon_free(void *val)
 {
@@ -77,10 +78,6 @@ static void icon_free(void *val)
                else if (icon->drawinfo) {
                        MEM_freeN(icon->drawinfo);
                }
-               if (!icon->type && icon->obj) {
-                       /* 'preview' icons own preview data too. */
-                       BKE_previewimg_freefunc(icon->obj);
-               }
                MEM_freeN(icon);
        }
 }
@@ -113,14 +110,24 @@ void BKE_icons_init(int first_dyn_id)
        gFirstIconId = first_dyn_id;
 
        if (!gIcons)
-               gIcons = BLI_ghash_int_new("icons_init gh");
+               gIcons = BLI_ghash_int_new(__func__);
+
+       if (!gFilePreviews) {
+               gFilePreviews = BLI_ghash_str_new(__func__);
+       }
 }
 
 void BKE_icons_free(void)
 {
-       if (gIcons)
+       if (gIcons) {
                BLI_ghash_free(gIcons, NULL, icon_free);
-       gIcons = NULL;
+               gIcons = NULL;
+       }
+
+       if (gFilePreviews) {
+               BLI_ghash_free(gFilePreviews, NULL, BKE_previewimg_freefunc);
+               gFilePreviews = NULL;
+       }
 }
 
 PreviewImage *BKE_previewimg_create(void)
@@ -140,39 +147,66 @@ PreviewImage *BKE_previewimg_create(void)
 /**
  * Generate a PreviewImage from given file path, using thumbnails management.
  */
-PreviewImage *BKE_previewimg_thumbnail_create(const char *path, int source)
+PreviewImage *BKE_previewimg_thumbnail_create(const char *path, const int 
source, bool force_update)
 {
-       PreviewImage *prv = BKE_previewimg_create();
+       PreviewImage *prv = NULL;
+       void **prv_v;
        int icon_w, icon_h;
 
-       ImBuf *thumb = IMB_thumb_manage(path, THB_NORMAL, source);
+       prv_v = BLI_ghash_lookup_p(gFilePreviews, path);
+
+       if (prv_v) {
+               prv = *prv_v;
+               BLI_assert(prv);
+       }
+
+       if (prv && force_update) {
+               BKE_previewimg_clear(prv, ICON_SIZE_ICON);
+               BKE_previewimg_clear(prv, ICON_SIZE_PREVIEW);
+       }
+       else if (!prv) {
+               prv = BKE_previewimg_create();
+               force_update = true;
+       }
+
+       if (force_update) {
+               ImBuf *thumb = IMB_thumb_manage(path, THB_NORMAL, source);
+
+               if (thumb) {
+                       prv->w[ICON_SIZE_PREVIEW] = thumb->x;
+                       prv->h[ICON_SIZE_PREVIEW] = thumb->y;
+                       prv->rect[ICON_SIZE_PREVIEW] = 
MEM_dupallocN(thumb->rect);
+                       prv->flag[ICON_SIZE_PREVIEW] &= ~(CHANGED | 
USER_EDITED);
 
-       if (thumb) {
-               prv->w[ICON_SIZE_PREVIEW] = thumb->x;
-               prv->h[ICON_SIZE_PREVIEW] = thumb->y;
-               prv->rect[ICON_SIZE_PREVIEW] = MEM_dupallocN(thumb->rect);
-               prv->flag[ICON_SIZE_PREVIEW] &= ~CHANGED;
+                       if (thumb->x > thumb->y) {
+                               icon_w = ICON_RENDER_DEFAULT_HEIGHT;
+                               icon_h = (thumb->y * icon_w) / thumb->x + 1;
+                       }
+                       else if (thumb->x < thumb->y) {
+                               icon_h = ICON_RENDER_DEFAULT_HEIGHT;
+                               icon_w = (thumb->x * icon_h) / thumb->y + 1;
+                       }
+                       else {
+                               icon_w = icon_h = ICON_RENDER_DEFAULT_HEIGHT;
+                       }
 
-               if (thumb->x > thumb->y) {
-                       icon_w = ICON_RENDER_DEFAULT_HEIGHT;
-                       icon_h = (thumb->y * icon_w) / thumb->x + 1;
+                       IMB_scaleImBuf(thumb, icon_w, icon_h);
+                       prv->w[ICON_SIZE_ICON] = icon_w;
+                       prv->h[ICON_SIZE_ICON] = icon_h;
+                       prv->rect[ICON_SIZE_ICON] = MEM_dupallocN(thumb->rect);
+                       prv->flag[ICON_SIZE_ICON] &= ~(CHANGED | USER_EDITED);
+
+                       IMB_freeImBuf(thumb);
                }
-               else if (thumb->x < thumb->y) {
-                       icon_h = ICON_RENDER_DEFAULT_HEIGHT;
-                       icon_w = (thumb->x * icon_h) / thumb->y + 1;
+
+               if (prv_v) {
+                       *prv_v = prv;
                }
                else {
-                       icon_w = icon_h = ICON_RENDER_DEFAULT_HEIGHT;
+                       BLI_ghash_insert(gFilePreviews, (void *)path, prv);
                }
-
-               IMB_scaleImBuf(thumb, icon_w, icon_h);
-               prv->w[ICON_SIZE_ICON] = icon_w;
-               prv->h[ICON_SIZE_ICON] = icon_h;
-               prv->rect[ICON_SIZE_ICON] = MEM_dupallocN(thumb->rect);
-               prv->flag[ICON_SIZE_ICON] &= ~CHANGED;
-
-               IMB_freeImBuf(thumb);
        }
+
        return prv;
 }
 
@@ -358,7 +392,6 @@ int BKE_icon_id_get(struct ID *id)
 
 /**
  * Return icon id of given preview, or create new icon if not found.
- * Note it takes ownership of given peview data!
  */
 int BKE_icon_preview_get(PreviewImage *preview)
 {
diff --git a/source/blender/makesrna/intern/rna_main_api.c 
b/source/blender/makesrna/intern/rna_main_api.c
index 9d1237e..3b8932e 100644
--- a/source/blender/makesrna/intern/rna_main_api.c
+++ b/source/blender/makesrna/intern/rna_main_api.c
@@ -123,13 +123,12 @@ static int rna_Main_thumbnail_ensure(const char *path, 
int source)
        return thumb != NULL;
 }
 
-static PreviewImage *rna_Main_thumbnail_preview(const char *path, int source)
+static PreviewImage *rna_Main_thumbnail_preview(const char *path, int source, 
int force_update)
 {
-       PreviewImage *prv = BKE_previewimg_thumbnail_create(path, source);
+       PreviewImage *prv = BKE_previewimg_thumbnail_create(path, source, 
(force_update != 0));
+
+       BKE_icon_preview_get(prv);
 
-       if (!BKE_icon_preview_get(prv)) {
-               BKE_previewimg_free(&prv);
-       }
        return prv;
 }
 
@@ -852,6 +851,8 @@ void RNA_api_main(StructRNA *srna)
        RNA_def_function_ui_description(func, "Generate a Preview with icon_id 
from thumbnail of given file");
        RNA_def_string_file_path(func, "path", NULL, FILE_MAX_LIBEXTRA, "", 
"Path of the file to ensure thumbnail from");
        RNA_def_enum(func, "type", main_thumbtypes, THB_SOURCE_IMAGE, "", "Type 
of file to generate thumbnail from");
+       RNA_def_boolean(func, "force_update", false, "",
+                       "Force to re-run thumbnail manager on this path, even 
if its preview is already cached");
        parm = RNA_def_pointer(func, "preview", "Preview", "", "Preview 
generated (None if failure)");
        RNA_def_function_return(func, parm);

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to