From 32fa136cc7db029fbe4656a89d51d2b97b4bc199 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Fri, 25 Jan 2019 13:39:43 -0300 Subject: [PATCH] Ability to keep images in ImageTexture cached while using editor, fixes #25243 --- editor/editor_node.cpp | 2 ++ scene/resources/texture.cpp | 20 +++++++++++++++++++- scene/resources/texture.h | 5 +++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index 719130621e1..75f01ef055b 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -4760,6 +4760,8 @@ EditorNode::EditorNode() { ResourceLoader::clear_translation_remaps(); //no remaps using during editor ResourceLoader::clear_path_remaps(); + ImageTexture::set_keep_images_cached(true); + InputDefault *id = Object::cast_to(Input::get_singleton()); if (id) { diff --git a/scene/resources/texture.cpp b/scene/resources/texture.cpp index 38709167799..26036c08a97 100644 --- a/scene/resources/texture.cpp +++ b/scene/resources/texture.cpp @@ -178,6 +178,12 @@ void ImageTexture::_reload_hook(const RID &p_hook) { _change_notify(); } +bool ImageTexture::keep_images_cached = false; + +void ImageTexture::set_keep_images_cached(bool p_enable) { + keep_images_cached = p_enable; +} + void ImageTexture::create(int p_width, int p_height, Image::Format p_format, uint32_t p_flags) { flags = p_flags; @@ -198,6 +204,10 @@ void ImageTexture::create_from_image(const Ref &p_image, uint32_t p_flags VisualServer::get_singleton()->texture_allocate(texture, p_image->get_width(), p_image->get_height(), 0, p_image->get_format(), VS::TEXTURE_TYPE_2D, p_flags); VisualServer::get_singleton()->texture_set_data(texture, p_image); _change_notify(); + + if (keep_images_cached) { + image_cache = p_image; + } } void ImageTexture::set_flags(uint32_t p_flags) { @@ -245,6 +255,10 @@ void ImageTexture::set_data(const Ref &p_image) { _change_notify(); alpha_cache.unref(); + + if (keep_images_cached) { + image_cache = p_image; + } } void ImageTexture::_resource_path_changed() { @@ -254,7 +268,11 @@ void ImageTexture::_resource_path_changed() { Ref ImageTexture::get_data() const { - return VisualServer::get_singleton()->texture_get_data(texture); + if (image_cache.is_valid()) { + return image_cache; + } else { + return VisualServer::get_singleton()->texture_get_data(texture); + } } int ImageTexture::get_width() const { diff --git a/scene/resources/texture.h b/scene/resources/texture.h index 2b67ebec62f..a6b4c763e92 100644 --- a/scene/resources/texture.h +++ b/scene/resources/texture.h @@ -111,6 +111,7 @@ private: Size2 size_override; float lossy_storage_quality; mutable Ref alpha_cache; + Ref image_cache; protected: virtual void reload_from_file(); @@ -125,7 +126,11 @@ protected: void _set_data(Dictionary p_data); + static bool keep_images_cached; + public: + static void set_keep_images_cached(bool p_enable); + void create(int p_width, int p_height, Image::Format p_format, uint32_t p_flags = FLAGS_DEFAULT); void create_from_image(const Ref &p_image, uint32_t p_flags = FLAGS_DEFAULT);