Browse Source

libobs/graphics: Add memory usage member to image file

Allows estimating the required memory usage of an image.
jp9000 6 years ago
parent
commit
a776a6cf07
2 changed files with 15 additions and 2 deletions
  1. 14 2
      libobs/graphics/image-file.c
  2. 1 0
      libobs/graphics/image-file.h

+ 14 - 2
libobs/graphics/image-file.c

@@ -59,6 +59,12 @@ static inline int get_full_decoded_gif_size(gs_image_file_t *image)
 	return image->gif.width * image->gif.height * 4 * image->gif.frame_count;
 }
 
+static inline void *alloc_mem(gs_image_file_t *image, size_t size)
+{
+	image->mem_usage += size;
+	return bzalloc(size);
+}
+
 static bool init_animated_gif(gs_image_file_t *image, const char *path)
 {
 	bool is_animated_gif = true;
@@ -121,9 +127,9 @@ static bool init_animated_gif(gs_image_file_t *image, const char *path)
 	if (image->is_animated_gif) {
 		gif_decode_frame(&image->gif, 0);
 
-		image->animation_frame_cache = bzalloc(
+		image->animation_frame_cache = alloc_mem(image,
 				image->gif.frame_count * sizeof(uint8_t*));
-		image->animation_frame_data = bzalloc(
+		image->animation_frame_data = alloc_mem(image,
 				get_full_decoded_gif_size(image));
 
 		for (unsigned int i = 0; i < image->gif.frame_count; i++) {
@@ -137,6 +143,9 @@ static bool init_animated_gif(gs_image_file_t *image, const char *path)
 		image->cx = (uint32_t)image->gif.width;
 		image->cy = (uint32_t)image->gif.height;
 		image->format = GS_RGBA;
+
+		image->mem_usage += image->cx * image->cy * 4;
+		image->mem_usage += size;
 	} else {
 		gif_finalise(&image->gif);
 		bfree(image->gif_data);
@@ -179,6 +188,9 @@ void gs_image_file_init(gs_image_file_t *image, const char *file)
 	image->texture_data = gs_create_texture_file_data(file,
 			&image->format, &image->cx, &image->cy);
 
+	image->mem_usage += image->cx * image->cy *
+		gs_get_format_bpp(image->format) / 8;
+
 	image->loaded = !!image->texture_data;
 	if (!image->loaded) {
 		blog(LOG_WARNING, "Failed to load file '%s'", file);

+ 1 - 0
libobs/graphics/image-file.h

@@ -38,6 +38,7 @@ struct gs_image_file {
 	uint8_t **animation_frame_cache;
 	uint8_t *animation_frame_data;
 	uint64_t cur_time;
+	uint64_t mem_usage;
 	int cur_frame;
 	int cur_loop;
 	int last_decoded_frame;