1
0
Эх сурвалжийг харах

libobs: Add gs_create_texture_file_data function

Allows loading the file to memory first so the graphics subsystem
doesn't necessarily have to be locked when loading image files.
jp9000 9 жил өмнө
parent
commit
9f63554e69

+ 14 - 8
libobs/graphics/graphics-ffmpeg.c

@@ -201,21 +201,27 @@ static inline enum gs_color_format convert_format(enum AVPixelFormat format)
 	return GS_BGRX;
 }
 
-gs_texture_t *gs_texture_create_from_file(const char *file)
+uint8_t *gs_create_texture_file_data(const char *file,
+		enum gs_color_format *format,
+		uint32_t *cx_out, uint32_t *cy_out)
 {
 	struct ffmpeg_image image;
-	gs_texture_t           *tex = NULL;
+	uint8_t *data = NULL;
 
 	if (ffmpeg_image_init(&image, file)) {
-		uint8_t *data = malloc(image.cx * image.cy * 4);
+		data = bmalloc(image.cx * image.cy * 4);
+
 		if (ffmpeg_image_decode(&image, data, image.cx * 4)) {
-			tex = gs_texture_create(image.cx, image.cy,
-					convert_format(image.format),
-					1, (const uint8_t**)&data, 0);
+			*format = convert_format(image.format);
+			*cx_out = (uint32_t)image.cx;
+			*cy_out = (uint32_t)image.cy;
+		} else {
+			bfree(data);
+			data = NULL;
 		}
 
 		ffmpeg_image_free(&image);
-		free(data);
 	}
-	return tex;
+
+	return data;
 }

+ 13 - 9
libobs/graphics/graphics-magick.c

@@ -14,9 +14,11 @@ void gs_free_image_deps()
 	MagickCoreTerminus();
 }
 
-gs_texture_t *gs_texture_create_from_file(const char *file)
+uint8_t *gs_create_texture_file_data(const char *file,
+		enum gs_color_format *format,
+		uint32_t *cx_out, uint32_t *cy_out)
 {
-	gs_texture_t  *tex = NULL;
+	uint8_t       *data = NULL;
 	ImageInfo     *info;
 	ExceptionInfo *exception;
 	Image         *image;
@@ -32,19 +34,21 @@ gs_texture_t *gs_texture_create_from_file(const char *file)
 	if (image) {
 		size_t  cx    = image->magick_columns;
 		size_t  cy    = image->magick_rows;
-		uint8_t *data = malloc(cx * cy * 4);
+		data          = bmalloc(cx * cy * 4);
 
 		ExportImagePixels(image, 0, 0, cx, cy, "BGRA", CharPixel,
 				data, exception);
-		if (exception->severity == UndefinedException)
-			tex = gs_texture_create(cx, cy, GS_BGRA, 1,
-					(const uint8_t**)&data, 0);
-		else
+		if (exception->severity != UndefinedException) {
 			blog(LOG_WARNING, "magickcore warning/error getting "
 			                  "pixels from file '%s': %s", file,
 			                  exception->reason);
+			bfree(data);
+			data = NULL;
+		}
 
-		free(data);
+		*format = GS_BGRA;
+		*cx_out = (uint32_t)cx;
+		*cy_out = (uint32_t)cy;
 		DestroyImage(image);
 
 	} else if (exception->severity != UndefinedException) {
@@ -55,5 +59,5 @@ gs_texture_t *gs_texture_create_from_file(const char *file)
 	DestroyImageInfo(info);
 	DestroyExceptionInfo(exception);
 
-	return tex;
+	return data;
 }

+ 17 - 0
libobs/graphics/graphics.c

@@ -896,6 +896,23 @@ gs_shader_t *gs_pixelshader_create_from_file(const char *file,
 	return shader;
 }
 
+gs_texture_t *gs_texture_create_from_file(const char *file)
+{
+	enum gs_color_format format;
+	uint32_t cx;
+	uint32_t cy;
+	uint8_t *data = gs_create_texture_file_data(file, &format, &cx, &cy);
+	gs_texture_t *tex = NULL;
+
+	if (data) {
+		tex = gs_texture_create(cx, cy, format, 1,
+				(const uint8_t**)&data, 0);
+		bfree(data);
+	}
+
+	return tex;
+}
+
 static inline void assign_sprite_rect(float *start, float *end, float size,
 		bool flip)
 {

+ 2 - 0
libobs/graphics/graphics.h

@@ -502,6 +502,8 @@ EXPORT gs_shader_t *gs_pixelshader_create_from_file(const char *file,
 		char **error_string);
 
 EXPORT gs_texture_t *gs_texture_create_from_file(const char *file);
+EXPORT uint8_t *gs_create_texture_file_data(const char *file,
+		enum gs_color_format *format, uint32_t *cx, uint32_t *cy);
 
 #define GS_FLIP_U (1<<0)
 #define GS_FLIP_V (1<<1)