[Bf-blender-cvs] [59b2acc] master: Make .blend file thumbnail reading simpler and more coherent, read/store them when reading in background mode.

Bastien Montagne noreply at git.blender.org
Thu Aug 27 16:00:59 CEST 2015


Commit: 59b2acc71b996ebf30ce643a188317605d5a971f
Author: Bastien Montagne
Date:   Thu Aug 27 15:53:23 2015 +0200
Branches: master
https://developer.blender.org/rB59b2acc71b996ebf30ce643a188317605d5a971f

Make .blend file thumbnail reading simpler and more coherent, read/store them when reading in background mode.

Primary goal of this commit is to fix an annoying issue - when processing and saving .blend
files in background mode you lose their thumbnails, since it can only be generated with
an OpenGL context.

Solution to that is to read .blend thumbnail while reading .blend file (only done in background
mode currently), and store it in Main struct.

Also, this lead to removing .blend file reading code from thumb_blend (no need to have doublons).
We now have a small interface in regular reading code area, which keeps it reasonbaly light
by only reading/parsing header info, and first few BHead blocks.

This makes code reading .blend thumbnail about 3 to 4 times slower than previous highly specialized
one in blend_thumb.c, but overall thumbnail generation of a big .blend files folder only grows
of about 1%, think we can bare with it.

Finally, since thumbnail is now optionally stored in Main struct, it makes it easy to allow user
to define their own custom one (instead of auto-generated one). RNA API for this was not added though,
accessing that kind of .blend meta-data has to be rethought a bit on a bigger level first.

Reviewers: sergey, campbellbarton

Subscribers: Severin, psy-fi

Differential Revision: https://developer.blender.org/D1469

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

M	source/blender/blenkernel/BKE_library.h
M	source/blender/blenkernel/BKE_main.h
M	source/blender/blenkernel/intern/library.c
M	source/blender/blenloader/BLO_blend_defs.h
M	source/blender/blenloader/BLO_readfile.h
M	source/blender/blenloader/BLO_writefile.h
M	source/blender/blenloader/intern/readfile.c
M	source/blender/blenloader/intern/writefile.c
M	source/blender/imbuf/intern/thumbs_blend.c
M	source/blender/windowmanager/intern/wm_files.c

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

diff --git a/source/blender/blenkernel/BKE_library.h b/source/blender/blenkernel/BKE_library.h
index 6ecc955..5b12858 100644
--- a/source/blender/blenkernel/BKE_library.h
+++ b/source/blender/blenkernel/BKE_library.h
@@ -38,8 +38,10 @@ extern "C" {
 
 #include "BLI_compiler_attrs.h"
 
+struct BlendThumbnail;
 struct ListBase;
 struct ID;
+struct ImBuf;
 struct Main;
 struct Library;
 struct wmWindowManager;
@@ -87,6 +89,10 @@ void BKE_main_free(struct Main *mainvar);
 void BKE_main_lock(struct Main *bmain);
 void BKE_main_unlock(struct Main *bmain);
 
+struct BlendThumbnail *BKE_main_thumbnail_from_imbuf(struct Main *bmain, struct ImBuf *img);
+struct ImBuf *BKE_main_thumbnail_to_imbuf(struct Main *bmain, struct BlendThumbnail *data);
+void BKE_main_thumbnail_create(struct Main *bmain);
+
 void BKE_main_id_tag_idcode(struct Main *mainvar, const short type, const bool tag);
 void BKE_main_id_tag_listbase(struct ListBase *lb, const bool tag);
 void BKE_main_id_tag_all(struct Main *mainvar, const bool tag);
diff --git a/source/blender/blenkernel/BKE_main.h b/source/blender/blenkernel/BKE_main.h
index ec654ea..6a00961 100644
--- a/source/blender/blenkernel/BKE_main.h
+++ b/source/blender/blenkernel/BKE_main.h
@@ -50,6 +50,13 @@ struct EvaluationContext;
 struct Library;
 struct MainLock;
 
+/* Blender thumbnail, as written on file (width, height, and data as char RGBA). */
+/* We pack pixel data after that struct. */
+typedef struct BlendThumbnail {
+	int width, height;
+	char rect[0];
+} BlendThumbnail;
+
 typedef struct Main {
 	struct Main *next, *prev;
 	char name[1024]; /* 1024 = FILE_MAX */
@@ -58,6 +65,8 @@ typedef struct Main {
 	uint64_t build_commit_timestamp; /* commit's timestamp from buildinfo */
 	char build_hash[16];  /* hash from buildinfo */
 	short recovered;	/* indicate the main->name (file) is the recovered one */
+
+	BlendThumbnail *blen_thumb;
 	
 	struct Library *curlib;
 	ListBase scene;
@@ -109,7 +118,10 @@ typedef struct Main {
 #define MAIN_VERSION_OLDER(main, ver, subver) \
 	((main)->versionfile < (ver) || (main->versionfile == (ver) && (main)->subversionfile < (subver)))
 
-	
+#define BLEN_THUMB_SIZE 128
+
+#define BLEN_THUMB_MEMSIZE(_x, _y) (sizeof(BlendThumbnail) + (size_t)((_x) * (_y)) * sizeof(int))
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/blenkernel/intern/library.c b/source/blender/blenkernel/intern/library.c
index 8bb2864..1466cdf 100644
--- a/source/blender/blenkernel/intern/library.c
+++ b/source/blender/blenkernel/intern/library.c
@@ -119,6 +119,9 @@
 
 #include "RNA_access.h"
 
+#include "IMB_imbuf.h"
+#include "IMB_imbuf_types.h"
+
 #ifdef WITH_PYTHON
 #include "BPY_extern.h"
 #endif
@@ -1097,6 +1100,8 @@ void BKE_main_free(Main *mainvar)
 	ListBase *lbarray[MAX_LIBARRAY];
 	int a;
 
+	MEM_SAFE_FREE(mainvar->blen_thumb);
+
 	a = set_listbasepointers(mainvar, lbarray);
 	while (a--) {
 		ListBase *lb = lbarray[a];
@@ -1166,6 +1171,74 @@ void BKE_main_unlock(struct Main *bmain)
 	BLI_spin_unlock((SpinLock *) bmain->lock);
 }
 
+/**
+ * Generates a raw .blend file thumbnail data from given image.
+ *
+ * \param bmain If not NULL, also store generated data in this Main.
+ * \param img ImBuf image to generate thumbnail data from.
+ * \return The generated .blend file raw thumbnail data.
+ */
+BlendThumbnail *BKE_main_thumbnail_from_imbuf(Main *bmain, ImBuf *img)
+{
+	BlendThumbnail *data = NULL;
+
+	if (bmain) {
+		MEM_SAFE_FREE(bmain->blen_thumb);
+	}
+
+	if (img) {
+		const size_t sz = BLEN_THUMB_MEMSIZE(img->x, img->y);
+		data = MEM_mallocN(sz, __func__);
+
+		IMB_rect_from_float(img);  /* Just in case... */
+		data->width = img->x;
+		data->height = img->y;
+		memcpy(data->rect, img->rect, sz - sizeof(*data));
+	}
+
+	if (bmain) {
+		bmain->blen_thumb = data;
+	}
+	return data;
+}
+
+/**
+ * Generates an image from raw .blend file thumbnail \a data.
+ *
+ * \param bmain Use this bmain->blen_thumb data if given \a data is NULL.
+ * \param data Raw .blend file thumbnail data.
+ * \return An ImBuf from given data, or NULL if invalid.
+ */
+ImBuf *BKE_main_thumbnail_to_imbuf(Main *bmain, BlendThumbnail *data)
+{
+	ImBuf *img = NULL;
+
+	if (!data && bmain) {
+		data = bmain->blen_thumb;
+	}
+
+	if (data) {
+		/* Note: we cannot use IMB_allocFromBuffer(), since it tries to dupalloc passed buffer, which will fail
+		 *       here (we do not want to pass the first two ints!). */
+		img = IMB_allocImBuf((unsigned int)data->width, (unsigned int)data->height, 32, IB_rect | IB_metadata);
+		memcpy(img->rect, data->rect, BLEN_THUMB_MEMSIZE(data->width, data->height) - sizeof(*data));
+	}
+
+	return img;
+}
+
+/**
+ * Generates an empty (black) thumbnail for given Main.
+ */
+void BKE_main_thumbnail_create(struct Main *bmain)
+{
+	MEM_SAFE_FREE(bmain->blen_thumb);
+
+	bmain->blen_thumb = MEM_callocN(BLEN_THUMB_MEMSIZE(BLEN_THUMB_SIZE, BLEN_THUMB_SIZE), __func__);
+	bmain->blen_thumb->width = BLEN_THUMB_SIZE;
+	bmain->blen_thumb->height = BLEN_THUMB_SIZE;
+}
+
 /* ***************** ID ************************ */
 ID *BKE_libblock_find_name_ex(struct Main *bmain, const short type, const char *name)
 {
diff --git a/source/blender/blenloader/BLO_blend_defs.h b/source/blender/blenloader/BLO_blend_defs.h
index 44f0fa9..a6b06a0 100644
--- a/source/blender/blenloader/BLO_blend_defs.h
+++ b/source/blender/blenloader/BLO_blend_defs.h
@@ -75,4 +75,6 @@ enum {
 	ENDB = BLEND_MAKE_ID('E', 'N', 'D', 'B'),
 };
 
+#define BLEN_THUMB_MEMSIZE_FILE(_x, _y) (sizeof(int) * (size_t)(2 + (_x) * (_y)))
+
 #endif  /* __BLO_BLEND_DEFS_H__ */
diff --git a/source/blender/blenloader/BLO_readfile.h b/source/blender/blenloader/BLO_readfile.h
index 5f881c0..77bdb99 100644
--- a/source/blender/blenloader/BLO_readfile.h
+++ b/source/blender/blenloader/BLO_readfile.h
@@ -36,6 +36,7 @@
 extern "C" {
 #endif
 
+struct BlendThumbnail;
 struct bScreen;
 struct LinkNode;
 struct Main;
@@ -278,6 +279,15 @@ void BLO_expand_main(void *fdhandle, struct Main *mainvar);
 void BLO_update_defaults_userpref_blend(void);
 void BLO_update_defaults_startup_blend(struct Main *mainvar);
 
+/**
+ * Does a very light reading of given .blend file to extract its stored thumbnail.
+ *
+ * \param filepath The path of the file to extract thumbnail from.
+ * \return The raw thumbnail
+ *         (MEM-allocated, as stored in file, use BKE_main_thumbnail_to_imbuf() to convert it to ImBuf image).
+ */
+struct BlendThumbnail *BLO_thumbnail_from_file(const char *filepath);
+
 #ifdef __cplusplus
 } 
 #endif
diff --git a/source/blender/blenloader/BLO_writefile.h b/source/blender/blenloader/BLO_writefile.h
index 7a8429a..0d66eb7 100644
--- a/source/blender/blenloader/BLO_writefile.h
+++ b/source/blender/blenloader/BLO_writefile.h
@@ -33,14 +33,14 @@
  *  \brief external writefile function prototypes.
  */
 
+struct BlendThumbnail;
 struct MemFile;
 struct Main;
 struct ReportList;
 
-extern int BLO_write_file(struct Main *mainvar, const char *filepath, int write_flags, struct ReportList *reports, const int *thumb);
+extern int BLO_write_file(struct Main *mainvar, const char *filepath, int write_flags,
+        struct ReportList *reports, const struct BlendThumbnail *thumb);
 extern int BLO_write_file_mem(struct Main *mainvar, struct MemFile *compare, struct MemFile *current, int write_flags);
 
-#define BLEN_THUMB_SIZE 128
-
 #endif
 
diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index 6581fb3..ee07f2b 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -917,6 +917,41 @@ static int read_file_dna(FileData *fd)
 	return 0;
 }
 
+static int *read_file_thumbnail(FileData *fd)
+{
+	BHead *bhead;
+	int *blend_thumb = NULL;
+
+	for (bhead = blo_firstbhead(fd); bhead; bhead = blo_nextbhead(fd, bhead)) {
+		if (bhead->code == TEST) {
+			const bool do_endian_swap = (fd->flags & FD_FLAGS_SWITCH_ENDIAN) != 0;
+			int *data = (int *)(bhead + 1);
+
+			if (bhead->len < (2 * sizeof(int))) {
+				break;
+			}
+
+			if (do_endian_swap) {
+				BLI_endian_switch_int32(&data[0]);
+				BLI_endian_switch_int32(&data[1]);
+			}
+
+			if (bhead->len < BLEN_THUMB_MEMSIZE_FILE(data[0], data[1])) {
+				break;
+			}
+
+			blend_thumb = data;
+			break;
+		}
+		else if (bhead->code != REND) {
+			/* Thumbnail is stored in TEST immediately after first REND... */
+			break;
+		}
+	}
+
+	return blend_thumb;
+}
+
 static int fd_read_from_file(FileData *filedata, void *buffer, unsigned int size)
 {
 	int readsize = read(filedata->filedes, buffer, size);
@@ -1080,6 +1115,33 @@ FileData *blo_openblenderfile(const char *filepath, ReportList *reports)
 	}
 }
 
+/**
+ * Same as blo_openblenderfile(), but does not reads DNA data, only header. Use it for light access
+ * (e.g. thumbnail reading).
+ */
+static FileData *blo_openblenderfile_minimal(const char *filepath)
+{
+	gzFile gzfile;
+	errno = 0;
+	gzfile = BLI_gzopen(filepath, "rb");
+
+	if (gzfile != (gzFile)Z_NULL) {
+		FileData *fd = filedata_new();
+		fd->gzfiledes = gzfile;
+		fd->read = fd_read_gzip_from_file;
+
+		decode_blender_header(fd);
+
+		if (fd->flags & FD_FLAGS_FILE_OK) {
+			return fd;
+		}
+
+		blo_freefiledata(fd);
+	}
+
+	return NULL;
+}
+
 static int fd_read_gzip_from_memory(FileData *filedata, void *buffer, unsigned int size)
 {
 	int err;
@@ -1290,6 +1352,33 @@ bool BLO_library_path_explode(const char *path, char *r_dir, char **r_group, cha
 	return true;
 }
 
+BlendThumbnail *BLO_thumbnail_from_file(const char *filepath)
+{
+	FileData *fd;
+	BlendThumbnail *data;
+	int *fd_data;
+
+	fd = blo_openblenderfile_minimal(filepath);
+	fd_data = fd ? read_file_thumbnail(fd) : NULL;
+
+	if (fd_data) {
+		const size_t sz = BLEN_THUMB_MEMSIZE(fd_data[0], fd_data[1]);
+		data = MEM_mallocN(sz, __func__);
+
+		BLI_assert((sz - sizeof(*data)) == (BLEN_THUMB_MEMSIZE_FILE(fd_data[0], fd_data[1]) - (sizeof(*fd_data) * 2)));
+		data->width = fd_data[0];
+		data->height = fd_data[1];
+		memcpy(data->rect, &fd_data[2], sz - sizeof(*data));
+	}
+	else {
+		data = NULL;
+	}
+
+	blo_freefiledata(fd);
+
+	return data;
+}
+
 /* ************** OLD POINTERS ******************* */
 
 static void *newdataadr(FileData *fd, void *adr)		/* only di

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list