From c6008316e2ec841b07aaa7dbae5539fccb6f0061 Mon Sep 17 00:00:00 2001 From: Christoph Hohmann Date: Fri, 20 Feb 2015 15:31:49 +0100 Subject: [PATCH] image-source: Add option (unload when not showing) If this option is on, the image will unload when the image isn't displayed anywhere, and then reload it when it's displayed again to reduce the amount of memory images take up on VRAM at any given time. If this option is off, then it's always loaded in VRAM regardless of whether it's displayed or not. Closes Pull Request #380 (edited by Jim) --- plugins/image-source/data/locale/en-US.ini | 1 + plugins/image-source/image-source.c | 77 +++++++++++++++++++--- 2 files changed, 69 insertions(+), 9 deletions(-) diff --git a/plugins/image-source/data/locale/en-US.ini b/plugins/image-source/data/locale/en-US.ini index fc453ae82..6b0d15579 100644 --- a/plugins/image-source/data/locale/en-US.ini +++ b/plugins/image-source/data/locale/en-US.ini @@ -1,2 +1,3 @@ ImageInput="Image" File="Image File" +UnloadWhenNotShowing="Unload image when not showing" diff --git a/plugins/image-source/image-source.c b/plugins/image-source/image-source.c index 133a1dc0c..27bbab9fd 100644 --- a/plugins/image-source/image-source.c +++ b/plugins/image-source/image-source.c @@ -14,6 +14,9 @@ struct image_source { obs_source_t *source; + char *file; + bool persistent; + gs_texture_t *tex; uint32_t cx; uint32_t cy; @@ -24,19 +27,19 @@ static const char *image_source_get_name(void) return obs_module_text("ImageInput"); } -static void image_source_update(void *data, obs_data_t *settings) +static void image_source_load(struct image_source *context) { - struct image_source *context = data; - const char *file = obs_data_get_string(settings, "file"); + char *file = context->file; obs_enter_graphics(); - if (context->tex) { + if (context->tex) gs_texture_destroy(context->tex); - context->tex = NULL; - } + context->tex = NULL; if (file && *file) { + debug("loading texture '%s'", file); + context->tex = gs_texture_create_from_file(file); if (context->tex) { context->cx = gs_texture_get_width(context->tex); @@ -51,6 +54,56 @@ static void image_source_update(void *data, obs_data_t *settings) obs_leave_graphics(); } +static void image_source_unload(struct image_source *context) +{ + obs_enter_graphics(); + + if (context->tex) + gs_texture_destroy(context->tex); + context->tex = NULL; + + obs_leave_graphics(); +} + +static void image_source_update(void *data, obs_data_t *settings) +{ + struct image_source *context = data; + const char *file = obs_data_get_string(settings, "file"); + const bool unload = obs_data_get_bool(settings, "unload"); + + if (context->file) + bfree(context->file); + context->file = bstrdup(file); + context->persistent = !unload; + + /* Load the image if the source is persistent or showing */ + if (context->persistent || obs_source_showing(context->source)) + image_source_load(data); + else + image_source_unload(data); +} + +static void image_source_defaults(obs_data_t *settings) +{ + obs_data_set_default_bool(settings, "unload", true); +} + +static void image_source_show(void *data) +{ + struct image_source *context = data; + + if (!context->persistent) + image_source_load(context); +} + +static void image_source_hide(void *data) +{ + struct image_source *context = data; + + if (!context->persistent) + image_source_unload(context); +} + static void *image_source_create(obs_data_t *settings, obs_source_t *source) { struct image_source *context = bzalloc(sizeof(struct image_source)); @@ -64,10 +117,10 @@ static void image_source_destroy(void *data) { struct image_source *context = data; - obs_enter_graphics(); - gs_texture_destroy(context->tex); - obs_leave_graphics(); + image_source_unload(context); + if (context->file) + bfree(context->file); bfree(context); } @@ -86,6 +139,7 @@ static uint32_t image_source_getheight(void *data) static void image_source_render(void *data, gs_effect_t *effect) { struct image_source *context = data; + if (!context->tex) return; @@ -112,6 +166,8 @@ static obs_properties_t *image_source_properties(void *unused) obs_properties_add_path(props, "file", obs_module_text("File"), OBS_PATH_FILE, image_filter, NULL); + obs_properties_add_bool(props, + "unload", obs_module_text("UnloadWhenNotShowing")); return props; } @@ -124,6 +180,9 @@ static struct obs_source_info image_source_info = { .create = image_source_create, .destroy = image_source_destroy, .update = image_source_update, + .get_defaults = image_source_defaults, + .show = image_source_show, + .hide = image_source_hide, .get_width = image_source_getwidth, .get_height = image_source_getheight, .video_render = image_source_render,