[Bf-blender-cvs] [d089bfa] multi_previews_id: Don't pass pixels through RNA, but Image pointers.

Sybren A. Stüvel noreply at git.blender.org
Fri Nov 11 17:34:16 CET 2016


Commit: d089bfa9f0943a8c8852d5bb97cd6c1ad6b9757b
Author: Sybren A. Stüvel
Date:   Fri Nov 11 17:32:32 2016 +0100
Branches: multi_previews_id
https://developer.blender.org/rBd089bfa9f0943a8c8852d5bb97cd6c1ad6b9757b

Don't pass pixels through RNA, but Image pointers.

This makes setting preview images MUCH faster; passing pixels was
O(n^2) in the number of pixels. It also prevents us from having double
setter functions (floats/bytes).

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

M	source/blender/makesrna/intern/rna_ID.c

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

diff --git a/source/blender/makesrna/intern/rna_ID.c b/source/blender/makesrna/intern/rna_ID.c
index fff094e..be1537b 100644
--- a/source/blender/makesrna/intern/rna_ID.c
+++ b/source/blender/makesrna/intern/rna_ID.c
@@ -35,6 +35,9 @@
 #include "BLI_utildefines.h"
 
 #include "BKE_icons.h"
+#include "BKE_image.h"
+
+#include "IMB_imbuf_types.h"
 
 #include "RNA_access.h"
 #include "RNA_define.h"
@@ -748,23 +751,6 @@ static void rna_ImagePreview_frame_get(
 	memcpy(*r_data, frame, frame_size);
 }
 
-static void rna_ImagePreview_frame_set(
-        PreviewImage *prv, ReportList *reports,
-        int frame_idx, int data_len, int *data, int meta, enum eIconSizes size)
-{
-	if (frame_idx < 0 || frame_idx >= (prv->num_frames ? prv->num_frames : 1)) {
-		BKE_reportf(reports, RPT_ERROR, "Invalid frame index %d", frame_idx);
-		return;
-	}
-
-	if (data_len != prv->w[size] * prv->h[size]) {
-		BKE_reportf(reports, RPT_ERROR, "Expected an array of %u integers, not %d", prv->w[size] * prv->h[size], data_len);
-		return;
-	}
-	BKE_previewimg_frame_data_set(prv, (unsigned short) frame_idx, size, meta, (unsigned int *)data);
-	prv->flag[size] |= PRV_USER_EDITED;
-}
-
 static void rna_ImagePreview_frame_float_get(
         PreviewImage *prv, ReportList *reports,
         int frame_idx, int *r_data_len, float **r_data, int *r_meta, enum eIconSizes size)
@@ -792,30 +778,54 @@ static void rna_ImagePreview_frame_float_get(
 
 }
 
-static void rna_ImagePreview_frame_float_set(
+static void rna_ImagePreview_frame_set_image(
         PreviewImage *prv, ReportList *reports,
-        int frame_idx, int data_len, float *data, int meta, enum eIconSizes size)
+        int frame_idx, Image *ima, int meta, enum eIconSizes size)
 {
+	ImBuf *ibuf;
+	void *lock;
+	int i, img_memsize;
+
 	if (frame_idx < 0 || frame_idx >= (prv->num_frames ? prv->num_frames : 1)) {
 		BKE_reportf(reports, RPT_ERROR, "Invalid frame index %d", frame_idx);
 		return;
 	}
 
-	if (data_len != prv->w[size] * prv->h[size] * 4) {
-		BKE_reportf(reports, RPT_ERROR, "Expected an array of %u floats, not %d", prv->w[size] * prv->h[size] * 4, data_len);
+	/* get the image's pixels, checking for the correct size and channel count */
+	ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
+	if (!ibuf) {
+		BKE_reportf(reports, RPT_ERROR, "Error acquiring image buffer");
+		BKE_image_release_ibuf(ima, ibuf, lock);
+		return;
+	}
+	if (ibuf->x != prv->w[size] || ibuf->y != prv->h[size]) {
+		BKE_reportf(reports, RPT_ERROR, "Expected an %ux%u image, not %dx%d", prv->w[size], prv->h[size], ibuf->x, ibuf->y);
+		BKE_image_release_ibuf(ima, ibuf, lock);
+		return;
+	}
+	if (ibuf->channels != 4) {
+		BKE_reportf(reports, RPT_ERROR, "Expected an RGBA (4-channel) image, not %i channels", ibuf->channels);
+		BKE_image_release_ibuf(ima, ibuf, lock);
 		return;
 	}
-
-	unsigned char *frame = (unsigned char *)BKE_previewimg_frame_data_get(prv, (unsigned short) frame_idx, size, NULL);
-	int i;
 
 	BLI_assert(sizeof(unsigned int) == 4);
+	unsigned char *frame = (unsigned char *)BKE_previewimg_frame_data_get(prv, (unsigned short) frame_idx, size, NULL);
+	img_memsize = ibuf->x * ibuf->y * ibuf->channels;
 
-	for (i = 0; i < data_len; i++) {
-		frame[i] = FTOCHAR(data[i]);
+	if (ibuf->rect_float) {
+		/* We have floats in the image, but we store the preview as bytes */
+		for (i = 0; i < img_memsize; i++) {
+			frame[i] = FTOCHAR(ibuf->rect_float[i]);
+		}
+	}
+	else {
+		/* We have bytes in the image, so we can directly copy */
+		memcpy(frame, ibuf->rect, img_memsize);
 	}
-	*((int *)(&frame[i])) = meta;
+	BKE_image_release_ibuf(ima, ibuf, lock);
 
+	*((int *)(&frame[img_memsize])) = meta;
 	prv->flag[size] |= PRV_USER_EDITED;
 }
 
@@ -826,9 +836,9 @@ static void rna_ImagePreview_image_frame_get(
 }
 
 static void rna_ImagePreview_image_frame_set(
-        PreviewImage *prv, ReportList *reports, int frame_idx, int data_len, int *data, int meta)
+        PreviewImage *prv, ReportList *reports, int frame_idx, Image *image, int meta)
 {
-	rna_ImagePreview_frame_set(prv, reports, frame_idx, data_len, data, meta, ICON_SIZE_PREVIEW);
+	rna_ImagePreview_frame_set_image(prv, reports, frame_idx, image, meta, ICON_SIZE_PREVIEW);
 }
 
 static void rna_ImagePreview_image_frame_float_get(
@@ -837,12 +847,6 @@ static void rna_ImagePreview_image_frame_float_get(
 	rna_ImagePreview_frame_float_get(prv, reports, frame_idx, r_data_len, r_data, r_meta, ICON_SIZE_PREVIEW);
 }
 
-static void rna_ImagePreview_image_frame_float_set(
-        PreviewImage *prv, ReportList *reports, int frame_idx, int data_len, float *data, int meta)
-{
-	rna_ImagePreview_frame_float_set(prv, reports, frame_idx, data_len, data, meta, ICON_SIZE_PREVIEW);
-}
-
 static void rna_ImagePreview_icon_frame_get(
         PreviewImage *prv, ReportList *reports, int frame_idx, int *r_data_len, int **r_data, int *r_meta)
 {
@@ -850,9 +854,9 @@ static void rna_ImagePreview_icon_frame_get(
 }
 
 static void rna_ImagePreview_icon_frame_set(
-        PreviewImage *prv, ReportList *reports, int frame_idx, int data_len, int *data, int meta)
+        PreviewImage *prv, ReportList *reports, int frame_idx, Image *image, int meta)
 {
-	rna_ImagePreview_frame_set(prv, reports, frame_idx, data_len, data, meta, ICON_SIZE_ICON);
+	rna_ImagePreview_frame_set_image(prv, reports, frame_idx, image, meta, ICON_SIZE_ICON);
 }
 
 static void rna_ImagePreview_icon_frame_float_get(
@@ -861,12 +865,6 @@ static void rna_ImagePreview_icon_frame_float_get(
 	rna_ImagePreview_frame_float_get(prv, reports, frame_idx, r_data_len, r_data, r_meta, ICON_SIZE_ICON);
 }
 
-static void rna_ImagePreview_icon_frame_float_set(
-        PreviewImage *prv, ReportList *reports, int frame_idx, int data_len, float *data, int meta)
-{
-	rna_ImagePreview_frame_float_set(prv, reports, frame_idx, data_len, data, meta, ICON_SIZE_ICON);
-}
-
 static PointerRNA rna_IDPreview_get(PointerRNA *ptr)
 {
 	ID *id = (ID *)ptr->data;
@@ -1101,10 +1099,10 @@ static void rna_def_image_preview(BlenderRNA *brna)
 	RNA_def_function_flag(func, FUNC_USE_REPORTS);
 	RNA_def_function_ui_description(func, "Set a frame data and meta-data from this preview");
 	RNA_def_int(func, "index", 0, 0, USHRT_MAX, "", "Index of frame to set", 0, USHRT_MAX);
-	parm = RNA_def_property(func, "data", PROP_INT, PROP_NONE);
-	RNA_def_property_array(parm, 1);
-	RNA_def_property_flag(parm, PROP_DYNAMIC | PROP_REQUIRED);
-	RNA_def_property_ui_text(parm, "", "Array of integers, one per pixel (RGBA concatenated values)");
+	parm = RNA_def_property(func, "image", PROP_POINTER, PROP_NONE);
+	RNA_def_property_flag(parm, PROP_REQUIRED | PROP_NEVER_NULL);
+	RNA_def_property_struct_type(parm, "Image");
+	RNA_def_property_ui_text(parm, "", "Image whose pixels to copy and use for this preview");
 	parm = RNA_def_property(func, "meta", PROP_INT, PROP_NONE);
 	RNA_def_property_ui_text(parm, "", "Meta-data integer associated to the preview frame");
 
@@ -1121,17 +1119,6 @@ static void rna_def_image_preview(BlenderRNA *brna)
 	RNA_def_property_ui_text(parm, "", "Meta-data integer associated to the preview frame");
 	RNA_def_function_output(func, parm);
 
-	func = RNA_def_function(srna, "image_frame_float_set", "rna_ImagePreview_image_frame_float_set");
-	RNA_def_function_flag(func, FUNC_USE_REPORTS);
-	RNA_def_function_ui_description(func, "Set a frame data and meta-data from this preview");
-	RNA_def_int(func, "index", 0, 0, USHRT_MAX, "", "Index of frame to set", 0, USHRT_MAX);
-	parm = RNA_def_property(func, "data", PROP_FLOAT, PROP_NONE);
-	RNA_def_property_array(parm, 1);
-	RNA_def_property_flag(parm, PROP_DYNAMIC | PROP_REQUIRED);
-	RNA_def_property_ui_text(parm, "", "Array of floats, one per pixel component (RGBA values)");
-	parm = RNA_def_property(func, "meta", PROP_INT, PROP_NONE);
-	RNA_def_property_ui_text(parm, "", "Meta-data integer associated to the preview frame");
-
 	func = RNA_def_function(srna, "icon_frame_get", "rna_ImagePreview_icon_frame_get");
 	RNA_def_function_flag(func, FUNC_USE_REPORTS);
 	RNA_def_function_ui_description(func, "Get a frame data and meta-data from this icon");
@@ -1149,10 +1136,10 @@ static void rna_def_image_preview(BlenderRNA *brna)
 	RNA_def_function_flag(func, FUNC_USE_REPORTS);
 	RNA_def_function_ui_description(func, "Set a frame data and meta-data from this icon");
 	RNA_def_int(func, "index", 0, 0, USHRT_MAX, "", "Index of frame to set", 0, USHRT_MAX);
-	parm = RNA_def_property(func, "data", PROP_INT, PROP_NONE);
-	RNA_def_property_array(parm, 1);
-	RNA_def_property_flag(parm, PROP_DYNAMIC | PROP_REQUIRED);
-	RNA_def_property_ui_text(parm, "", "Array of integers, one per pixel (RGBA concatenated values)");
+	parm = RNA_def_property(func, "image", PROP_POINTER, PROP_NONE);
+	RNA_def_property_flag(parm, PROP_REQUIRED | PROP_NEVER_NULL);
+	RNA_def_property_struct_type(parm, "Image");
+	RNA_def_property_ui_text(parm, "", "Image whose pixels to copy and use for this preview");
 	parm = RNA_def_property(func, "meta", PROP_INT, PROP_NONE);
 	RNA_def_property_ui_text(parm, "", "Meta-data integer associated to the icon frame");
 
@@ -1168,17 +1155,6 @@ static void rna_def_image_preview(BlenderRNA *brna)
 	parm = RNA_def_property(func, "meta", PROP_INT, PROP_NONE);
 	RNA_def_property_ui_text(parm, "", "Meta-data integer associated to the icon frame");
 	RNA_def_function_output(func, parm);
-
-	func = RNA_def_function(srna, "icon_frame_float_set", "rna_ImagePreview_icon_frame_float_set");
-	RNA_def_function_flag(func, FUNC_USE_REPORTS);
-	RNA_def_function_ui_description(func, "Set a frame data and meta-data from this icon");
-	RNA_def_int(func, "index", 0, 0, USHRT_MAX, "", "Index of frame to set", 0, USHRT_MAX);
-	parm = RNA_def_property(func, "data", PROP_FLOAT, PROP_NONE);
-	RNA_def_property_array(parm, 1);
-	RNA_def_property_flag(parm, PROP_DYNAMIC | PROP_REQUIRED);
-	RNA_def_property_ui_text(parm, "", "Array of floats, one per pixel component (RGBA values)");
-	parm = RNA_def_property(func, "meta", PROP_INT, PROP_NONE);
-	RNA_def_property_ui_text(parm, "", "Meta-data integer associated to the icon frame");
 }
 
 static void rna_def_ID(BlenderRNA *brna)




More information about the Bf-blender-cvs mailing list