diff options
Diffstat (limited to 'editor/editor_resource_preview.cpp')
-rw-r--r-- | editor/editor_resource_preview.cpp | 83 |
1 files changed, 23 insertions, 60 deletions
diff --git a/editor/editor_resource_preview.cpp b/editor/editor_resource_preview.cpp index 2a4300f833..77288be614 100644 --- a/editor/editor_resource_preview.cpp +++ b/editor/editor_resource_preview.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -30,19 +30,16 @@ #include "editor_resource_preview.h" -#include "core/method_bind_ext.gen.inc" - +#include "core/config/project_settings.h" #include "core/io/resource_loader.h" #include "core/io/resource_saver.h" -#include "core/message_queue.h" +#include "core/object/message_queue.h" #include "core/os/file_access.h" -#include "core/project_settings.h" #include "editor_node.h" #include "editor_scale.h" #include "editor_settings.h" bool EditorResourcePreviewGenerator::handles(const String &p_type) const { - if (get_script_instance() && get_script_instance()->has_method("handles")) { return get_script_instance()->call("handles", p_type); } @@ -50,7 +47,6 @@ bool EditorResourcePreviewGenerator::handles(const String &p_type) const { } Ref<Texture2D> EditorResourcePreviewGenerator::generate(const RES &p_from, const Size2 &p_size) const { - if (get_script_instance() && get_script_instance()->has_method("generate")) { return get_script_instance()->call("generate", p_from, p_size); } @@ -58,19 +54,18 @@ Ref<Texture2D> EditorResourcePreviewGenerator::generate(const RES &p_from, const } Ref<Texture2D> EditorResourcePreviewGenerator::generate_from_path(const String &p_path, const Size2 &p_size) const { - if (get_script_instance() && get_script_instance()->has_method("generate_from_path")) { return get_script_instance()->call("generate_from_path", p_path, p_size); } RES res = ResourceLoader::load(p_path); - if (!res.is_valid()) + if (!res.is_valid()) { return res; + } return generate(res, p_size); } bool EditorResourcePreviewGenerator::generate_small_preview_automatically() const { - if (get_script_instance() && get_script_instance()->has_method("generate_small_preview_automatically")) { return get_script_instance()->call("generate_small_preview_automatically"); } @@ -79,7 +74,6 @@ bool EditorResourcePreviewGenerator::generate_small_preview_automatically() cons } bool EditorResourcePreviewGenerator::can_generate_small_preview() const { - if (get_script_instance() && get_script_instance()->has_method("can_generate_small_preview")) { return get_script_instance()->call("can_generate_small_preview"); } @@ -88,7 +82,6 @@ bool EditorResourcePreviewGenerator::can_generate_small_preview() const { } void EditorResourcePreviewGenerator::_bind_methods() { - ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "handles", PropertyInfo(Variant::STRING, "type"))); ClassDB::add_virtual_method(get_class_static(), MethodInfo(CLASS_INFO(Texture2D), "generate", PropertyInfo(Variant::OBJECT, "from", PROPERTY_HINT_RESOURCE_TYPE, "Resource"), PropertyInfo(Variant::VECTOR2, "size"))); ClassDB::add_virtual_method(get_class_static(), MethodInfo(CLASS_INFO(Texture2D), "generate_from_path", PropertyInfo(Variant::STRING, "path", PROPERTY_HINT_FILE), PropertyInfo(Variant::VECTOR2, "size"))); @@ -102,13 +95,11 @@ EditorResourcePreviewGenerator::EditorResourcePreviewGenerator() { EditorResourcePreview *EditorResourcePreview::singleton = nullptr; void EditorResourcePreview::_thread_func(void *ud) { - EditorResourcePreview *erp = (EditorResourcePreview *)ud; erp->_thread(); } void EditorResourcePreview::_preview_ready(const String &p_str, const Ref<Texture2D> &p_texture, const Ref<Texture2D> &p_small_texture, ObjectID id, const StringName &p_func, const Variant &p_ud) { - String path = p_str; { MutexLock lock(preview_mutex); @@ -117,7 +108,7 @@ void EditorResourcePreview::_preview_ready(const String &p_str, const Ref<Textur uint64_t modified_time = 0; if (p_str.begins_with("ID:")) { - hash = uint32_t(p_str.get_slicec(':', 2).to_int64()); + hash = uint32_t(p_str.get_slicec(':', 2).to_int()); path = "ID:" + p_str.get_slicec(':', 1); } else { modified_time = FileAccess::get_modified_time(path); @@ -139,10 +130,11 @@ void EditorResourcePreview::_preview_ready(const String &p_str, const Ref<Textur void EditorResourcePreview::_generate_preview(Ref<ImageTexture> &r_texture, Ref<ImageTexture> &r_small_texture, const QueueItem &p_item, const String &cache_base) { String type; - if (p_item.resource.is_valid()) + if (p_item.resource.is_valid()) { type = p_item.resource->get_class(); - else + } else { type = ResourceLoader::get_resource_type(p_item.path); + } if (type == "") { r_texture = Ref<ImageTexture>(); @@ -157,8 +149,9 @@ void EditorResourcePreview::_generate_preview(Ref<ImageTexture> &r_texture, Ref< r_small_texture = Ref<ImageTexture>(); for (int i = 0; i < preview_generators.size(); i++) { - if (!preview_generators[i]->handles(type)) + if (!preview_generators[i]->handles(type)) { continue; + } Ref<Texture2D> generated; if (p_item.resource.is_valid()) { @@ -169,7 +162,6 @@ void EditorResourcePreview::_generate_preview(Ref<ImageTexture> &r_texture, Ref< r_texture = generated; int small_thumbnail_size = EditorNode::get_singleton()->get_theme_base()->get_theme_icon("Object", "EditorIcons")->get_width(); // Kind of a workaround to retrieve the default icon size - small_thumbnail_size *= EDSCALE; if (preview_generators[i]->can_generate_small_preview()) { Ref<Texture2D> generated_small; @@ -214,15 +206,12 @@ void EditorResourcePreview::_generate_preview(Ref<ImageTexture> &r_texture, Ref< } void EditorResourcePreview::_thread() { - - exited = false; - while (!exit) { - + exited.clear(); + while (!exit.is_set()) { preview_sem.wait(); preview_mutex.lock(); if (queue.size()) { - QueueItem item = queue.front()->get(); queue.pop_front(); @@ -237,7 +226,6 @@ void EditorResourcePreview::_thread() { preview_mutex.unlock(); } else { - preview_mutex.unlock(); Ref<ImageTexture> texture; @@ -247,14 +235,12 @@ void EditorResourcePreview::_thread() { thumbnail_size *= EDSCALE; if (item.resource.is_valid()) { - _generate_preview(texture, small_texture, item, String()); //adding hash to the end of path (should be ID:<objid>:<hash>) because of 5 argument limit to call_deferred _preview_ready(item.path + ":" + itos(item.resource->hash_edited_version()), texture, small_texture, item.id, item.function, item.userdata); } else { - String temp_path = EditorSettings::get_singleton()->get_cache_dir(); String cache_base = ProjectSettings::get_singleton()->globalize_path(item.path).md5_text(); cache_base = temp_path.plus_file("resthumb-" + cache_base); @@ -264,30 +250,25 @@ void EditorResourcePreview::_thread() { String file = cache_base + ".txt"; FileAccess *f = FileAccess::open(file, FileAccess::READ); if (!f) { - // No cache found, generate _generate_preview(texture, small_texture, item, cache_base); } else { - uint64_t modtime = FileAccess::get_modified_time(item.path); - int tsize = f->get_line().to_int64(); + int tsize = f->get_line().to_int(); bool has_small_texture = f->get_line().to_int(); - uint64_t last_modtime = f->get_line().to_int64(); + uint64_t last_modtime = f->get_line().to_int(); bool cache_valid = true; if (tsize != thumbnail_size) { - cache_valid = false; memdelete(f); } else if (last_modtime != modtime) { - String last_md5 = f->get_line(); String md5 = FileAccess::get_md5(item.path); memdelete(f); if (last_md5 != md5) { - cache_valid = false; } else { @@ -311,7 +292,6 @@ void EditorResourcePreview::_thread() { } if (cache_valid) { - Ref<Image> img; img.instance(); Ref<Image> small_img; @@ -320,7 +300,6 @@ void EditorResourcePreview::_thread() { if (img->load(cache_base + ".png") != OK) { cache_valid = false; } else { - texture.instance(); texture->create_from_image(img); @@ -336,7 +315,6 @@ void EditorResourcePreview::_thread() { } if (!cache_valid) { - _generate_preview(texture, small_texture, item, cache_base); } } @@ -348,11 +326,10 @@ void EditorResourcePreview::_thread() { preview_mutex.unlock(); } } - exited = true; + exited.set(); } void EditorResourcePreview::queue_edited_resource_preview(const Ref<Resource> &p_res, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata) { - ERR_FAIL_NULL(p_receiver); ERR_FAIL_COND(!p_res.is_valid()); @@ -362,7 +339,6 @@ void EditorResourcePreview::queue_edited_resource_preview(const Ref<Resource> &p String path_id = "ID:" + itos(p_res->get_instance_id()); if (cache.has(path_id) && cache[path_id].last_hash == p_res->hash_edited_version()) { - cache[path_id].order = order++; p_receiver->call(p_receiver_func, path_id, cache[path_id].preview, cache[path_id].small_preview, p_userdata); return; @@ -383,7 +359,6 @@ void EditorResourcePreview::queue_edited_resource_preview(const Ref<Resource> &p } void EditorResourcePreview::queue_resource_preview(const String &p_path, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata) { - ERR_FAIL_NULL(p_receiver); { MutexLock lock(preview_mutex); @@ -406,22 +381,18 @@ void EditorResourcePreview::queue_resource_preview(const String &p_path, Object } void EditorResourcePreview::add_preview_generator(const Ref<EditorResourcePreviewGenerator> &p_generator) { - preview_generators.push_back(p_generator); } void EditorResourcePreview::remove_preview_generator(const Ref<EditorResourcePreviewGenerator> &p_generator) { - preview_generators.erase(p_generator); } EditorResourcePreview *EditorResourcePreview::get_singleton() { - return singleton; } void EditorResourcePreview::_bind_methods() { - ClassDB::bind_method("_preview_ready", &EditorResourcePreview::_preview_ready); ClassDB::bind_method(D_METHOD("queue_resource_preview", "path", "receiver", "receiver_func", "userdata"), &EditorResourcePreview::queue_resource_preview); @@ -434,13 +405,11 @@ void EditorResourcePreview::_bind_methods() { } void EditorResourcePreview::check_for_invalidation(const String &p_path) { - bool call_invalidated = false; { MutexLock lock(preview_mutex); if (cache.has(p_path)) { - uint64_t modified_time = FileAccess::get_modified_time(p_path); if (modified_time != cache[p_path].modified_time) { cache.erase(p_path); @@ -455,33 +424,27 @@ void EditorResourcePreview::check_for_invalidation(const String &p_path) { } void EditorResourcePreview::start() { - ERR_FAIL_COND_MSG(thread, "Thread already started."); - thread = Thread::create(_thread_func, this); + ERR_FAIL_COND_MSG(thread.is_started(), "Thread already started."); + thread.start(_thread_func, this); } void EditorResourcePreview::stop() { - if (thread) { - exit = true; + if (thread.is_started()) { + exit.set(); preview_sem.post(); - while (!exited) { + while (!exited.is_set()) { OS::get_singleton()->delay_usec(10000); RenderingServer::get_singleton()->sync(); //sync pending stuff, as thread may be blocked on visual server } - Thread::wait_to_finish(thread); - memdelete(thread); - thread = nullptr; + thread.wait_to_finish(); } } EditorResourcePreview::EditorResourcePreview() { - thread = nullptr; singleton = this; order = 0; - exit = false; - exited = false; } EditorResourcePreview::~EditorResourcePreview() { - stop(); } |