From a776a6cf071f71868fa464dbd4020d0fef82acd5 Mon Sep 17 00:00:00 2001 From: jp9000 Date: Wed, 13 Mar 2019 01:09:57 -0700 Subject: [PATCH] libobs/graphics: Add memory usage member to image file Allows estimating the required memory usage of an image. --- libobs/graphics/image-file.c | 16 ++++++++++++++-- libobs/graphics/image-file.h | 1 + 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/libobs/graphics/image-file.c b/libobs/graphics/image-file.c index 4262c0b11..27328c213 100644 --- a/libobs/graphics/image-file.c +++ b/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); diff --git a/libobs/graphics/image-file.h b/libobs/graphics/image-file.h index 46fec44c1..d344fad71 100644 --- a/libobs/graphics/image-file.h +++ b/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;