[Bf-blender-cvs] [6e8cc0b] asset-experiments: Move handling of ID blocks previews in thumbnail handler.

Bastien Montagne noreply at git.blender.org
Tue Apr 7 15:05:39 CEST 2015


Commit: 6e8cc0bc053590be4f4bde543b54f7e25c15302a
Author: Bastien Montagne
Date:   Tue Apr 7 15:00:40 2015 +0200
Branches: asset-experiments
https://developer.blender.org/rB6e8cc0bc053590be4f4bde543b54f7e25c15302a

Move handling of ID blocks previews in thumbnail handler.

this is mandatory, since we do not keep previews for all listed items anymore
(with 10k datablocks would start to take too much mem). So we now generate (read)
them from thumbnailing threaded task as any other image, has several advantages:
* Quicker initial listing of data from .blend files.
* Since thumbnails are cached on disk, re-generating them is rather quick.
* General consistency!

Note thumbnail generation of datablocks could (should!) be enhanced, right now
it reads all previews for a given datatype in a file each time it has to generate
a single datablock thumbnail, we should generate all of those previews in a single
run. But this means handling cache dirty things (TBH_FAIL, outdating, etc.) on another
level so need some more work.

Also, still some memleaks issues with thumbnails, have to track those dose!

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

M	source/blender/editors/space_file/filelist.c
M	source/blender/imbuf/IMB_thumbs.h
M	source/blender/imbuf/intern/thumbs.c
M	source/blender/imbuf/intern/thumbs_blend.c

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

diff --git a/source/blender/editors/space_file/filelist.c b/source/blender/editors/space_file/filelist.c
index c4389a2..13ac100 100644
--- a/source/blender/editors/space_file/filelist.c
+++ b/source/blender/editors/space_file/filelist.c
@@ -1140,6 +1140,9 @@ static void filelist_cache_previewf(TaskPool *pool, void *taskdata, int threadid
 				preview->flags |= FILE_TYPE_MOVIE_ICON;
 			}
 		}
+		else if (preview->flags & FILE_TYPE_BLENDERLIB) {
+			preview->img = IMB_thumb_manage(preview->path, THB_NORMAL, THB_SOURCE_BLEND);
+		}
 		BLI_thread_queue_push(cache->previews_done, preview);
 	}
 
@@ -1188,7 +1191,8 @@ static void filelist_cache_previews_push(FileList *filelist, FileDirEntry *entry
 	FileListEntryCache *cache = &filelist->filelist_cache;
 
 	if (!entry->image &&
-		(entry->typeflag & (FILE_TYPE_IMAGE | FILE_TYPE_MOVIE | FILE_TYPE_BLENDER | FILE_TYPE_BLENDER_BACKUP)))
+		(entry->typeflag & (FILE_TYPE_IMAGE | FILE_TYPE_MOVIE |
+	                        FILE_TYPE_BLENDER | FILE_TYPE_BLENDER_BACKUP | FILE_TYPE_BLENDERLIB)))
 	{
 		FileListEntryPreview *preview = MEM_mallocN(sizeof(*preview), __func__);
 		BLI_join_dirfile(preview->path, sizeof(preview->path), filelist->filelist.root, entry->relpath);
@@ -1333,7 +1337,7 @@ static const char *fileentry_uiname(const char *root, const char *relpath, const
 		}
 	}
 	else if (typeflag & FILE_TYPE_DIR) {
-		name = relpath;
+		name = (char *)relpath;
 	}
 	else {
 		name = (char *)BLI_path_basename(relpath);
diff --git a/source/blender/imbuf/IMB_thumbs.h b/source/blender/imbuf/IMB_thumbs.h
index 9fc075e..f9045a3 100644
--- a/source/blender/imbuf/IMB_thumbs.h
+++ b/source/blender/imbuf/IMB_thumbs.h
@@ -78,7 +78,7 @@ ImBuf *IMB_thumb_manage(const char *path, ThumbSize size, ThumbSource source);
 void IMB_thumb_makedirs(void);
 
 /* special function for loading a thumbnail embedded into a blend file */
-ImBuf *IMB_loadblend_thumb(const char *path);
+ImBuf *IMB_loadblend_thumb(const char *blen_path, const char *blen_group, const char *blen_id);
 void IMB_overlayblend_thumb(unsigned int *thumb, int width, int height, float aspect);
 
 #ifdef __cplusplus
diff --git a/source/blender/imbuf/intern/thumbs.c b/source/blender/imbuf/intern/thumbs.c
index 848fbe7..b927aef 100644
--- a/source/blender/imbuf/intern/thumbs.c
+++ b/source/blender/imbuf/intern/thumbs.c
@@ -40,6 +40,8 @@
 #include "BLI_system.h"
 #include BLI_SYSTEM_PID_H
 
+#include "BLO_readfile.h"  /* XXX Hope this is not badlevel! */
+
 #include "IMB_imbuf_types.h"
 #include "IMB_imbuf.h"
 #include "IMB_thumbs.h"
@@ -241,30 +243,45 @@ static int uri_from_filename(const char *path, char *uri)
 	return 1;
 }
 
-static void thumbname_from_uri(const char *uri, char *thumb, const int thumb_len)
+static bool thumbpathname_from_uri(
+        const char *uri, char *r_path, const int path_len, char *r_name, int name_len, ThumbSize size)
 {
-	char hexdigest[33];
-	unsigned char digest[16];
+	char name_buff[40];
 
-	BLI_hash_md5_buffer(uri, strlen(uri), digest);
-	hexdigest[0] = '\0';
-	BLI_snprintf(thumb, thumb_len, "%s.png", BLI_hash_md5_to_hexdigest(digest, hexdigest));
+	if (r_path && !r_name) {
+		r_name = name_buff;
+		name_len = sizeof(name_buff);
+	}
+
+	if (r_name) {
+		char hexdigest[33];
+		unsigned char digest[16];
+		BLI_hash_md5_buffer(uri, strlen(uri), digest);
+		hexdigest[0] = '\0';
+		BLI_snprintf(r_name, name_len, "%s.png", BLI_hash_md5_to_hexdigest(digest, hexdigest));
+//		printf("%s: '%s' --> '%s'\n", __func__, uri, r_name);
+	}
+
+	if (r_path) {
+		char tmppath[FILE_MAX];
+
+		if (get_thumb_dir(tmppath, size)) {
+			BLI_snprintf(r_path, path_len, "%s%s", tmppath, r_name);
+//			printf("%s: '%s' --> '%s'\n", __func__, uri, r_path);
+			return true;
+		}
+	}
+	return false;
+}
 
-	// printf("%s: '%s' --> '%s'\n", __func__, uri, thumb);
+static void thumbname_from_uri(const char *uri, char *thumb, const int thumb_len)
+{
+	thumbpathname_from_uri(uri, NULL, 0, thumb, thumb_len, THB_FAIL);
 }
 
-static int thumbpath_from_uri(const char *uri, char *path, const int path_len, ThumbSize size)
+static bool thumbpath_from_uri(const char *uri, char *path, const int path_len, ThumbSize size)
 {
-	char tmppath[FILE_MAX];
-	int rv = 0;
-
-	if (get_thumb_dir(tmppath, size)) {
-		char thumb[40];
-		thumbname_from_uri(uri, thumb, sizeof(thumb));
-		BLI_snprintf(path, path_len, "%s%s", tmppath, thumb);
-		rv = 1;
-	}
-	return rv;
+	return thumbpathname_from_uri(uri, path, path_len, NULL, 0, size);
 }
 
 void IMB_thumb_makedirs(void)
@@ -279,9 +296,11 @@ void IMB_thumb_makedirs(void)
 }
 
 /* create thumbnail for file and returns new imbuf for thumbnail */
-ImBuf *IMB_thumb_create(const char *path, ThumbSize size, ThumbSource source, ImBuf *img)
+static ImBuf *thumb_create_ex(
+        const char *path, const char *uri, const char *thumb,
+        const char *file_path, const char *blen_group, const char *blen_id,
+        ThumbSize size, ThumbSource source, ImBuf *img)
 {
-	char uri[URI_MAX] = "";
 	char desc[URI_MAX + 22];
 	char tpath[FILE_MAX];
 	char tdir[FILE_MAX];
@@ -289,7 +308,6 @@ ImBuf *IMB_thumb_create(const char *path, ThumbSize size, ThumbSource source, Im
 	char mtime[40] = "0"; /* in case we can't stat the file */
 	char cwidth[40] = "0"; /* in case images have no data */
 	char cheight[40] = "0";
-	char thumb[40];
 	short tsize = 128;
 	short ex, ey;
 	float scaledx, scaledy;
@@ -318,11 +336,9 @@ ImBuf *IMB_thumb_create(const char *path, ThumbSize size, ThumbSource source, Im
 		}
 	}
 
-	uri_from_filename(path, uri);
-	thumbname_from_uri(uri, thumb, sizeof(thumb));
 	if (get_thumb_dir(tdir, size)) {
 		BLI_snprintf(tpath, FILE_MAX, "%s%s", tdir, thumb);
-		thumb[8] = '\0'; /* shorten for tempname, not needed anymore */
+//		thumb[8] = '\0'; /* shorten for tempname, not needed anymore */
 		BLI_snprintf(temp, FILE_MAX, "%sblender_%d_%s.png", tdir, abs(getpid()), thumb);
 		if (BLI_path_ncmp(path, tdir, sizeof(tdir)) == 0) {
 			return NULL;
@@ -332,12 +348,11 @@ ImBuf *IMB_thumb_create(const char *path, ThumbSize size, ThumbSource source, Im
 			if (!img) return NULL;
 		}
 		else {
-			if (THB_SOURCE_IMAGE == source || THB_SOURCE_BLEND == source) {
-				
+			if (ELEM(source, THB_SOURCE_IMAGE, THB_SOURCE_BLEND)) {
 				/* only load if we didnt give an image */
 				if (img == NULL) {
-					if (THB_SOURCE_BLEND == source) {
-						img = IMB_loadblend_thumb(path);
+					if (source == THB_SOURCE_BLEND) {
+						img = IMB_loadblend_thumb(file_path, blen_group, blen_id);
 					}
 					else {
 						img = IMB_loadiffname(path, IB_rect | IB_metadata | IB_thumbnail, NULL);
@@ -345,7 +360,7 @@ ImBuf *IMB_thumb_create(const char *path, ThumbSize size, ThumbSource source, Im
 				}
 
 				if (img != NULL) {
-					if (BLI_stat(path, &info) != -1) {
+					if (BLI_stat(file_path, &info) != -1) {
 						BLI_snprintf(mtime, sizeof(mtime), "%ld", (long int)info.st_mtime);
 					}
 					BLI_snprintf(cwidth, sizeof(cwidth), "%d", img->x);
@@ -366,7 +381,7 @@ ImBuf *IMB_thumb_create(const char *path, ThumbSize size, ThumbSource source, Im
 					}
 					IMB_free_anim(anim);
 				}
-				if (BLI_stat(path, &info) != -1) {
+				if (BLI_stat(file_path, &info) != -1) {
 					BLI_snprintf(mtime, sizeof(mtime), "%ld", (long int)info.st_mtime);
 				}
 			}
@@ -382,7 +397,7 @@ ImBuf *IMB_thumb_create(const char *path, ThumbSize size, ThumbSource source, Im
 			}
 			ex = (short)scaledx;
 			ey = (short)scaledy;
-			
+
 			/* save some time by only scaling byte buf */
 			if (img->rect_float) {
 				if (img->rect == NULL) {
@@ -399,7 +414,7 @@ ImBuf *IMB_thumb_create(const char *path, ThumbSize size, ThumbSource source, Im
 		IMB_metadata_change_field(img, "Software", "Blender");
 		IMB_metadata_change_field(img, "Thumb::URI", uri);
 		IMB_metadata_change_field(img, "Thumb::MTime", mtime);
-		if (THB_SOURCE_IMAGE == source) {
+		if (ELEM(source, THB_SOURCE_IMAGE, THB_SOURCE_BLEND)) {
 			IMB_metadata_change_field(img, "Thumb::Image::Width", cwidth);
 			IMB_metadata_change_field(img, "Thumb::Image::Height", cheight);
 		}
@@ -414,12 +429,21 @@ ImBuf *IMB_thumb_create(const char *path, ThumbSize size, ThumbSource source, Im
 
 			BLI_rename(temp, tpath);
 		}
-
-		return img;
 	}
 	return img;
 }
 
+ImBuf *IMB_thumb_create(const char *path, ThumbSize size, ThumbSource source, ImBuf *img)
+{
+	char uri[URI_MAX] = "";
+	char thumb_name[40];
+
+	uri_from_filename(path, uri);
+	thumbname_from_uri(uri, thumb_name, sizeof(thumb_name));
+
+	return thumb_create_ex(path, uri, thumb_name, path, NULL, NULL, size, source, img);
+}
+
 /* read thumbnail for file and returns new imbuf for thumbnail */
 ImBuf *IMB_thumb_read(const char *path, ThumbSize size)
 {
@@ -460,23 +484,40 @@ void IMB_thumb_delete(const char *path, ThumbSize size)
 /* create the thumb if necessary and manage failed and old thumbs */
 ImBuf *IMB_thumb_manage(const char *path, ThumbSize size, ThumbSource source)
 {
-	char thumb[FILE_MAX];
+	char thumb_path[FILE_MAX];
+	char thumb_name[40];
 	char uri[URI_MAX];
+	const char *file_path;
+	char path_buff[FILE_MAX];
 	BLI_stat_t st;
 	ImBuf *img = NULL;
-	
-	if (BLI_stat(path, &st) == -1) {
+	char *blen_group = NULL, *blen_id = NULL;
+
+	file_path = path;
+	if (source == THB_SOURCE_BLEND) {
+		if (BLO_library_path_explode(path, path_buff, &blen_group, &blen_id)) {
+			if (blen_group) {
+				if (!blen_id) {
+					/* No preview for blen groups */
+					return NULL;
+				}
+				file_path = path_buff;  /* path needs to be a valid file! */
+			}
+		}
+	}
+
+	if (BLI_stat(file_path, &st) == -1) {
 		return NULL;
 	}
 	if (!uri_from_filename(path, uri)) {
 		return NULL;
 	}
-	if (thumbpath_from_uri(uri, thumb, sizeof(thumb), THB_FAIL)) {
+	if (thumbpath_from_uri(uri, thumb_path, sizeof(thumb_path), THB_FAIL)) {
 		/* failure thumb exists, don't try recreating */
-		if (BLI_exists(thumb)) {
-			/* clear out of date fail case */
-			if (BLI_file_older(thumb, path)) {
-				BLI_delete(thumb, false, false);
+		if (BLI_exists(thumb_path)) {
+			/* clear out of date fail case (note for blen IDs we use blender file itself here) */
+			if (BLI_file_older(thumb_path, file_path)) {
+				BLI_delete(thumb_path, false, false);
 			}
 			else {
 				return NULL;
@@ -484,12 +525,12 @@ 

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list