2016-06-18 12:46:12 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* editor_resource_preview.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2015-05-31 04:59:42 +00:00
|
|
|
#include "editor_resource_preview.h"
|
2017-01-16 07:04:19 +00:00
|
|
|
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/config/project_settings.h"
|
2021-06-11 12:51:48 +00:00
|
|
|
#include "core/io/file_access.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/io/resource_loader.h"
|
|
|
|
#include "core/io/resource_saver.h"
|
2023-06-11 06:04:15 +00:00
|
|
|
#include "core/variant/variant_utility.h"
|
2022-02-12 01:46:22 +00:00
|
|
|
#include "editor/editor_node.h"
|
2022-02-14 13:00:03 +00:00
|
|
|
#include "editor/editor_paths.h"
|
2022-02-12 01:46:22 +00:00
|
|
|
#include "editor/editor_settings.h"
|
2023-09-06 14:11:05 +00:00
|
|
|
#include "editor/editor_string_names.h"
|
2024-01-15 12:14:55 +00:00
|
|
|
#include "editor/themes/editor_scale.h"
|
2024-07-22 12:44:14 +00:00
|
|
|
#include "scene/main/window.h"
|
2023-07-11 20:29:09 +00:00
|
|
|
#include "scene/resources/image_texture.h"
|
2024-01-15 18:34:06 +00:00
|
|
|
#include "servers/rendering/rendering_server_default.h"
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2016-09-10 18:32:17 +00:00
|
|
|
bool EditorResourcePreviewGenerator::handles(const String &p_type) const {
|
2022-09-29 09:53:28 +00:00
|
|
|
bool success = false;
|
2021-08-22 01:52:44 +00:00
|
|
|
if (GDVIRTUAL_CALL(_handles, p_type, success)) {
|
|
|
|
return success;
|
2016-09-10 18:32:17 +00:00
|
|
|
}
|
2021-05-15 21:48:59 +00:00
|
|
|
ERR_FAIL_V_MSG(false, "EditorResourcePreviewGenerator::_handles needs to be overridden.");
|
2016-09-10 18:32:17 +00:00
|
|
|
}
|
2018-09-12 11:10:49 +00:00
|
|
|
|
2022-08-19 16:14:57 +00:00
|
|
|
Ref<Texture2D> EditorResourcePreviewGenerator::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {
|
2021-08-22 01:52:44 +00:00
|
|
|
Ref<Texture2D> preview;
|
2022-08-19 16:14:57 +00:00
|
|
|
if (GDVIRTUAL_CALL(_generate, p_from, p_size, p_metadata, preview)) {
|
2021-08-22 01:52:44 +00:00
|
|
|
return preview;
|
2016-09-10 18:32:17 +00:00
|
|
|
}
|
2021-05-15 21:48:59 +00:00
|
|
|
ERR_FAIL_V_MSG(Ref<Texture2D>(), "EditorResourcePreviewGenerator::_generate needs to be overridden.");
|
2016-09-10 18:32:17 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 16:14:57 +00:00
|
|
|
Ref<Texture2D> EditorResourcePreviewGenerator::generate_from_path(const String &p_path, const Size2 &p_size, Dictionary &p_metadata) const {
|
2021-08-22 01:52:44 +00:00
|
|
|
Ref<Texture2D> preview;
|
2022-08-19 16:14:57 +00:00
|
|
|
if (GDVIRTUAL_CALL(_generate_from_path, p_path, p_size, p_metadata, preview)) {
|
2021-08-22 01:52:44 +00:00
|
|
|
return preview;
|
2016-09-10 18:32:17 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 23:43:50 +00:00
|
|
|
Ref<Resource> res = ResourceLoader::load(p_path);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!res.is_valid()) {
|
2015-05-31 04:59:42 +00:00
|
|
|
return res;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2022-08-19 16:14:57 +00:00
|
|
|
return generate(res, p_size, p_metadata);
|
2018-09-12 11:10:49 +00:00
|
|
|
}
|
|
|
|
|
2019-05-20 08:45:12 +00:00
|
|
|
bool EditorResourcePreviewGenerator::generate_small_preview_automatically() const {
|
2022-09-29 09:53:28 +00:00
|
|
|
bool success = false;
|
2022-10-18 16:47:44 +00:00
|
|
|
GDVIRTUAL_CALL(_generate_small_preview_automatically, success);
|
|
|
|
return success;
|
2019-05-20 08:45:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool EditorResourcePreviewGenerator::can_generate_small_preview() const {
|
2022-09-29 09:53:28 +00:00
|
|
|
bool success = false;
|
2022-10-18 16:47:44 +00:00
|
|
|
GDVIRTUAL_CALL(_can_generate_small_preview, success);
|
|
|
|
return success;
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
|
2016-09-10 18:32:17 +00:00
|
|
|
void EditorResourcePreviewGenerator::_bind_methods() {
|
2021-08-22 01:52:44 +00:00
|
|
|
GDVIRTUAL_BIND(_handles, "type");
|
2022-08-19 16:14:57 +00:00
|
|
|
GDVIRTUAL_BIND(_generate, "resource", "size", "metadata");
|
|
|
|
GDVIRTUAL_BIND(_generate_from_path, "path", "size", "metadata");
|
2021-08-22 01:52:44 +00:00
|
|
|
GDVIRTUAL_BIND(_generate_small_preview_automatically);
|
|
|
|
GDVIRTUAL_BIND(_can_generate_small_preview);
|
2016-09-10 18:32:17 +00:00
|
|
|
}
|
|
|
|
|
2015-05-31 04:59:42 +00:00
|
|
|
EditorResourcePreviewGenerator::EditorResourcePreviewGenerator() {
|
|
|
|
}
|
|
|
|
|
2024-02-21 09:44:21 +00:00
|
|
|
void EditorResourcePreviewGenerator::DrawRequester::request_and_wait(RID p_viewport) const {
|
2024-07-22 12:44:14 +00:00
|
|
|
Callable request_vp_update_once = callable_mp(RS::get_singleton(), &RS::viewport_set_update_mode).bind(p_viewport, RS::VIEWPORT_UPDATE_ONCE);
|
|
|
|
|
2024-02-21 09:44:21 +00:00
|
|
|
if (EditorResourcePreview::get_singleton()->is_threaded()) {
|
|
|
|
RS::get_singleton()->connect(SNAME("frame_pre_draw"), request_vp_update_once, Object::CONNECT_ONE_SHOT);
|
|
|
|
RS::get_singleton()->request_frame_drawn_callback(callable_mp(const_cast<EditorResourcePreviewGenerator::DrawRequester *>(this), &EditorResourcePreviewGenerator::DrawRequester::_post_semaphore));
|
|
|
|
|
|
|
|
semaphore.wait();
|
|
|
|
} else {
|
2024-07-22 12:44:14 +00:00
|
|
|
// Avoid the main viewport and children being redrawn.
|
|
|
|
SceneTree *st = Object::cast_to<SceneTree>(OS::get_singleton()->get_main_loop());
|
|
|
|
ERR_FAIL_NULL_MSG(st, "Editor's MainLoop is not a SceneTree. This is a bug.");
|
|
|
|
RID root_vp = st->get_root()->get_viewport_rid();
|
|
|
|
RenderingServer::get_singleton()->viewport_set_active(root_vp, false);
|
|
|
|
|
|
|
|
request_vp_update_once.call();
|
2024-02-21 09:44:21 +00:00
|
|
|
RS::get_singleton()->draw(false);
|
2024-07-22 12:44:14 +00:00
|
|
|
|
|
|
|
// Let main viewport and children be drawn again.
|
|
|
|
RenderingServer::get_singleton()->viewport_set_active(root_vp, true);
|
2024-02-21 09:44:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorResourcePreviewGenerator::DrawRequester::abort() const {
|
|
|
|
if (EditorResourcePreview::get_singleton()->is_threaded()) {
|
|
|
|
semaphore.post();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Variant EditorResourcePreviewGenerator::DrawRequester::_post_semaphore() const {
|
|
|
|
semaphore.post();
|
|
|
|
return Variant(); // Needed because of how the callback is used.
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditorResourcePreview::is_threaded() const {
|
2024-03-15 17:13:31 +00:00
|
|
|
return RSG::rasterizer->can_create_resources_async();
|
2024-02-21 09:44:21 +00:00
|
|
|
}
|
|
|
|
|
2015-05-31 04:59:42 +00:00
|
|
|
void EditorResourcePreview::_thread_func(void *ud) {
|
|
|
|
EditorResourcePreview *erp = (EditorResourcePreview *)ud;
|
|
|
|
erp->_thread();
|
|
|
|
}
|
|
|
|
|
2022-08-19 16:14:57 +00:00
|
|
|
void EditorResourcePreview::_preview_ready(const String &p_path, int p_hash, const Ref<Texture2D> &p_texture, const Ref<Texture2D> &p_small_texture, ObjectID id, const StringName &p_func, const Variant &p_ud, const Dictionary &p_metadata) {
|
2020-02-26 10:28:13 +00:00
|
|
|
{
|
2020-03-13 10:13:58 +00:00
|
|
|
MutexLock lock(preview_mutex);
|
|
|
|
|
2020-02-26 10:28:13 +00:00
|
|
|
uint64_t modified_time = 0;
|
2016-05-27 17:18:40 +00:00
|
|
|
|
2022-08-19 16:14:57 +00:00
|
|
|
if (!p_path.begins_with("ID:")) {
|
|
|
|
modified_time = FileAccess::get_modified_time(p_path);
|
2020-02-26 10:28:13 +00:00
|
|
|
}
|
2016-05-27 17:18:40 +00:00
|
|
|
|
2020-02-26 10:28:13 +00:00
|
|
|
Item item;
|
|
|
|
item.preview = p_texture;
|
|
|
|
item.small_preview = p_small_texture;
|
2022-08-19 16:14:57 +00:00
|
|
|
item.last_hash = p_hash;
|
2020-02-26 10:28:13 +00:00
|
|
|
item.modified_time = modified_time;
|
2022-08-19 16:14:57 +00:00
|
|
|
item.preview_metadata = p_metadata;
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2022-08-19 16:14:57 +00:00
|
|
|
cache[p_path] = item;
|
2020-02-26 10:28:13 +00:00
|
|
|
}
|
2016-07-09 14:19:49 +00:00
|
|
|
|
2023-12-18 14:46:56 +00:00
|
|
|
Callable(id, p_func).call_deferred(p_path, p_texture, p_small_texture, p_ud);
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 16:14:57 +00:00
|
|
|
void EditorResourcePreview::_generate_preview(Ref<ImageTexture> &r_texture, Ref<ImageTexture> &r_small_texture, const QueueItem &p_item, const String &cache_base, Dictionary &p_metadata) {
|
2016-05-27 17:18:40 +00:00
|
|
|
String type;
|
|
|
|
|
2024-01-26 17:55:27 +00:00
|
|
|
uint64_t started_at = OS::get_singleton()->get_ticks_usec();
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_item.resource.is_valid()) {
|
2017-01-03 02:03:46 +00:00
|
|
|
type = p_item.resource->get_class();
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2016-05-27 17:18:40 +00:00
|
|
|
type = ResourceLoader::get_resource_type(p_item.path);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2021-12-09 09:42:46 +00:00
|
|
|
if (type.is_empty()) {
|
2018-09-12 11:10:49 +00:00
|
|
|
r_texture = Ref<ImageTexture>();
|
|
|
|
r_small_texture = Ref<ImageTexture>();
|
2024-01-26 17:55:27 +00:00
|
|
|
|
|
|
|
if (is_print_verbose_enabled()) {
|
|
|
|
print_line(vformat("Generated '%s' preview in %d usec", p_item.path, OS::get_singleton()->get_ticks_usec() - started_at));
|
|
|
|
}
|
2018-09-12 11:10:49 +00:00
|
|
|
return; //could not guess type
|
|
|
|
}
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2022-10-18 14:43:37 +00:00
|
|
|
int thumbnail_size = EDITOR_GET("filesystem/file_dialog/thumbnail_size");
|
2018-09-12 11:10:49 +00:00
|
|
|
thumbnail_size *= EDSCALE;
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2018-09-12 11:10:49 +00:00
|
|
|
r_texture = Ref<ImageTexture>();
|
|
|
|
r_small_texture = Ref<ImageTexture>();
|
2016-07-09 14:19:49 +00:00
|
|
|
|
2018-09-12 11:10:49 +00:00
|
|
|
for (int i = 0; i < preview_generators.size(); i++) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!preview_generators[i]->handles(type)) {
|
2015-05-31 04:59:42 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-09-12 11:10:49 +00:00
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
Ref<Texture2D> generated;
|
2016-05-27 17:18:40 +00:00
|
|
|
if (p_item.resource.is_valid()) {
|
2022-08-19 16:14:57 +00:00
|
|
|
generated = preview_generators.write[i]->generate(p_item.resource, Vector2(thumbnail_size, thumbnail_size), p_metadata);
|
2016-05-27 17:18:40 +00:00
|
|
|
} else {
|
2022-08-19 16:14:57 +00:00
|
|
|
generated = preview_generators.write[i]->generate_from_path(p_item.path, Vector2(thumbnail_size, thumbnail_size), p_metadata);
|
2016-05-27 17:18:40 +00:00
|
|
|
}
|
2018-09-12 11:10:49 +00:00
|
|
|
r_texture = generated;
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2019-05-20 08:45:12 +00:00
|
|
|
if (preview_generators[i]->can_generate_small_preview()) {
|
2019-06-11 18:43:37 +00:00
|
|
|
Ref<Texture2D> generated_small;
|
2022-08-19 16:14:57 +00:00
|
|
|
Dictionary d;
|
2019-05-20 08:45:12 +00:00
|
|
|
if (p_item.resource.is_valid()) {
|
2022-08-19 16:14:57 +00:00
|
|
|
generated_small = preview_generators.write[i]->generate(p_item.resource, Vector2(small_thumbnail_size, small_thumbnail_size), d);
|
2019-05-20 08:45:12 +00:00
|
|
|
} else {
|
2022-08-19 16:14:57 +00:00
|
|
|
generated_small = preview_generators.write[i]->generate_from_path(p_item.path, Vector2(small_thumbnail_size, small_thumbnail_size), d);
|
2019-05-20 08:45:12 +00:00
|
|
|
}
|
|
|
|
r_small_texture = generated_small;
|
|
|
|
}
|
2018-09-12 11:10:49 +00:00
|
|
|
|
2019-05-20 08:45:12 +00:00
|
|
|
if (!r_small_texture.is_valid() && r_texture.is_valid() && preview_generators[i]->generate_small_preview_automatically()) {
|
2021-03-28 11:32:17 +00:00
|
|
|
Ref<Image> small_image = r_texture->get_image();
|
2019-01-27 16:39:16 +00:00
|
|
|
small_image = small_image->duplicate();
|
2018-09-12 11:10:49 +00:00
|
|
|
small_image->resize(small_thumbnail_size, small_thumbnail_size, Image::INTERPOLATE_CUBIC);
|
2021-06-17 22:03:09 +00:00
|
|
|
r_small_texture.instantiate();
|
2022-05-03 23:49:20 +00:00
|
|
|
r_small_texture->set_image(small_image);
|
2018-09-12 11:10:49 +00:00
|
|
|
}
|
2019-05-20 08:45:12 +00:00
|
|
|
|
2024-08-29 03:34:46 +00:00
|
|
|
if (generated.is_valid()) {
|
|
|
|
break;
|
|
|
|
}
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 17:18:40 +00:00
|
|
|
if (!p_item.resource.is_valid()) {
|
2022-08-19 16:14:57 +00:00
|
|
|
// Cache the preview in case it's a resource on disk.
|
2018-09-12 11:10:49 +00:00
|
|
|
if (r_texture.is_valid()) {
|
2022-08-19 16:14:57 +00:00
|
|
|
// Wow it generated a preview... save cache.
|
2018-09-12 11:10:49 +00:00
|
|
|
bool has_small_texture = r_small_texture.is_valid();
|
2022-06-02 23:33:42 +00:00
|
|
|
ResourceSaver::save(r_texture, cache_base + ".png");
|
2018-09-12 11:10:49 +00:00
|
|
|
if (has_small_texture) {
|
2022-06-02 23:33:42 +00:00
|
|
|
ResourceSaver::save(r_small_texture, cache_base + "_small.png");
|
2018-09-12 11:10:49 +00:00
|
|
|
}
|
2022-03-23 09:08:58 +00:00
|
|
|
Ref<FileAccess> f = FileAccess::open(cache_base + ".txt", FileAccess::WRITE);
|
|
|
|
ERR_FAIL_COND_MSG(f.is_null(), "Cannot create file '" + cache_base + ".txt'. Check user write permissions.");
|
2022-08-19 16:14:57 +00:00
|
|
|
_write_preview_cache(f, thumbnail_size, has_small_texture, FileAccess::get_modified_time(p_item.path), FileAccess::get_md5(p_item.path), p_metadata);
|
2016-05-27 17:18:40 +00:00
|
|
|
}
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
2024-01-26 17:55:27 +00:00
|
|
|
|
|
|
|
if (is_print_verbose_enabled()) {
|
|
|
|
print_line(vformat("Generated '%s' preview in %d usec", p_item.path, OS::get_singleton()->get_ticks_usec() - started_at));
|
|
|
|
}
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
|
2022-07-20 21:44:06 +00:00
|
|
|
const Dictionary EditorResourcePreview::get_preview_metadata(const String &p_path) const {
|
|
|
|
ERR_FAIL_COND_V(!cache.has(p_path), Dictionary());
|
|
|
|
return cache[p_path].preview_metadata;
|
2022-08-19 16:14:57 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 18:11:30 +00:00
|
|
|
void EditorResourcePreview::_iterate() {
|
|
|
|
preview_mutex.lock();
|
|
|
|
|
2023-11-10 14:27:51 +00:00
|
|
|
if (queue.size() == 0) {
|
|
|
|
preview_mutex.unlock();
|
|
|
|
return;
|
|
|
|
}
|
2020-11-18 18:11:30 +00:00
|
|
|
|
2023-11-10 14:27:51 +00:00
|
|
|
QueueItem item = queue.front()->get();
|
|
|
|
queue.pop_front();
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2023-11-10 14:27:51 +00:00
|
|
|
if (cache.has(item.path)) {
|
|
|
|
Item cached_item = cache[item.path];
|
|
|
|
// Already has it because someone loaded it, just let it know it's ready.
|
|
|
|
_preview_ready(item.path, cached_item.last_hash, cached_item.preview, cached_item.small_preview, item.id, item.function, item.userdata, cached_item.preview_metadata);
|
|
|
|
preview_mutex.unlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
preview_mutex.unlock();
|
|
|
|
|
|
|
|
Ref<ImageTexture> texture;
|
|
|
|
Ref<ImageTexture> small_texture;
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2023-11-10 14:27:51 +00:00
|
|
|
int thumbnail_size = EDITOR_GET("filesystem/file_dialog/thumbnail_size");
|
|
|
|
thumbnail_size *= EDSCALE;
|
2018-09-12 11:10:49 +00:00
|
|
|
|
2023-11-10 14:27:51 +00:00
|
|
|
if (item.resource.is_valid()) {
|
|
|
|
Dictionary preview_metadata;
|
|
|
|
_generate_preview(texture, small_texture, item, String(), preview_metadata);
|
2024-03-07 16:45:13 +00:00
|
|
|
_preview_ready(item.path, item.resource->hash_edited_version_for_preview(), texture, small_texture, item.id, item.function, item.userdata, preview_metadata);
|
2023-11-10 14:27:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2023-11-10 14:27:51 +00:00
|
|
|
Dictionary preview_metadata;
|
|
|
|
String temp_path = EditorPaths::get_singleton()->get_cache_dir();
|
|
|
|
String cache_base = ProjectSettings::get_singleton()->globalize_path(item.path).md5_text();
|
|
|
|
cache_base = temp_path.path_join("resthumb-" + cache_base);
|
2016-07-09 14:19:49 +00:00
|
|
|
|
2023-11-10 14:27:51 +00:00
|
|
|
// Does not have it, try to load a cached thumbnail.
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2023-11-10 14:27:51 +00:00
|
|
|
String file = cache_base + ".txt";
|
|
|
|
Ref<FileAccess> f = FileAccess::open(file, FileAccess::READ);
|
|
|
|
if (f.is_null()) {
|
|
|
|
// No cache found, generate.
|
|
|
|
_generate_preview(texture, small_texture, item, cache_base, preview_metadata);
|
|
|
|
} else {
|
|
|
|
uint64_t modtime = FileAccess::get_modified_time(item.path);
|
|
|
|
int tsize;
|
|
|
|
bool has_small_texture;
|
|
|
|
uint64_t last_modtime;
|
|
|
|
String hash;
|
2023-05-31 14:52:50 +00:00
|
|
|
bool outdated;
|
|
|
|
_read_preview_cache(f, &tsize, &has_small_texture, &last_modtime, &hash, &preview_metadata, &outdated);
|
2023-11-10 14:27:51 +00:00
|
|
|
|
|
|
|
bool cache_valid = true;
|
|
|
|
|
|
|
|
if (tsize != thumbnail_size) {
|
|
|
|
cache_valid = false;
|
|
|
|
f.unref();
|
2023-05-31 14:52:50 +00:00
|
|
|
} else if (outdated) {
|
|
|
|
cache_valid = false;
|
|
|
|
f.unref();
|
2023-11-10 14:27:51 +00:00
|
|
|
} else if (last_modtime != modtime) {
|
|
|
|
String last_md5 = f->get_line();
|
|
|
|
String md5 = FileAccess::get_md5(item.path);
|
|
|
|
f.unref();
|
|
|
|
|
|
|
|
if (last_md5 != md5) {
|
|
|
|
cache_valid = false;
|
2020-11-18 18:11:30 +00:00
|
|
|
} else {
|
2023-11-10 14:27:51 +00:00
|
|
|
// Update modified time.
|
|
|
|
|
|
|
|
Ref<FileAccess> f2 = FileAccess::open(file, FileAccess::WRITE);
|
|
|
|
if (f2.is_null()) {
|
|
|
|
// Not returning as this would leave the thread hanging and would require
|
|
|
|
// some proper cleanup/disabling of resource preview generation.
|
|
|
|
ERR_PRINT("Cannot create file '" + file + "'. Check user write permissions.");
|
2020-11-18 18:11:30 +00:00
|
|
|
} else {
|
2023-11-10 14:27:51 +00:00
|
|
|
_write_preview_cache(f2, thumbnail_size, has_small_texture, modtime, md5, preview_metadata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
f.unref();
|
|
|
|
}
|
2020-11-18 18:11:30 +00:00
|
|
|
|
2023-11-10 14:27:51 +00:00
|
|
|
if (cache_valid) {
|
|
|
|
Ref<Image> img;
|
|
|
|
img.instantiate();
|
|
|
|
Ref<Image> small_img;
|
|
|
|
small_img.instantiate();
|
2020-11-18 18:11:30 +00:00
|
|
|
|
2023-11-10 14:27:51 +00:00
|
|
|
if (img->load(cache_base + ".png") != OK) {
|
|
|
|
cache_valid = false;
|
|
|
|
} else {
|
|
|
|
texture.instantiate();
|
|
|
|
texture->set_image(img);
|
|
|
|
|
|
|
|
if (has_small_texture) {
|
|
|
|
if (small_img->load(cache_base + "_small.png") != OK) {
|
2020-11-18 18:11:30 +00:00
|
|
|
cache_valid = false;
|
2022-04-12 07:12:40 +00:00
|
|
|
} else {
|
2023-11-10 14:27:51 +00:00
|
|
|
small_texture.instantiate();
|
|
|
|
small_texture->set_image(small_img);
|
2016-07-09 14:19:49 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-18 18:11:30 +00:00
|
|
|
|
2023-11-10 14:27:51 +00:00
|
|
|
if (!cache_valid) {
|
|
|
|
_generate_preview(texture, small_texture, item, cache_base, preview_metadata);
|
|
|
|
}
|
2020-11-18 18:11:30 +00:00
|
|
|
}
|
2023-11-10 14:27:51 +00:00
|
|
|
_preview_ready(item.path, 0, texture, small_texture, item.id, item.function, item.userdata, preview_metadata);
|
2020-11-18 18:11:30 +00:00
|
|
|
}
|
|
|
|
|
2024-02-15 16:25:58 +00:00
|
|
|
void EditorResourcePreview::_write_preview_cache(Ref<FileAccess> p_file, int p_thumbnail_size, bool p_has_small_texture, uint64_t p_modified_time, const String &p_hash, const Dictionary &p_metadata) {
|
2022-08-19 16:14:57 +00:00
|
|
|
p_file->store_line(itos(p_thumbnail_size));
|
|
|
|
p_file->store_line(itos(p_has_small_texture));
|
|
|
|
p_file->store_line(itos(p_modified_time));
|
|
|
|
p_file->store_line(p_hash);
|
|
|
|
p_file->store_line(VariantUtilityFunctions::var_to_str(p_metadata).replace("\n", " "));
|
2023-05-31 14:52:50 +00:00
|
|
|
p_file->store_line(itos(CURRENT_METADATA_VERSION));
|
2022-08-19 16:14:57 +00:00
|
|
|
}
|
|
|
|
|
2023-05-31 14:52:50 +00:00
|
|
|
void EditorResourcePreview::_read_preview_cache(Ref<FileAccess> p_file, int *r_thumbnail_size, bool *r_has_small_texture, uint64_t *r_modified_time, String *r_hash, Dictionary *r_metadata, bool *r_outdated) {
|
2022-08-19 16:14:57 +00:00
|
|
|
*r_thumbnail_size = p_file->get_line().to_int();
|
|
|
|
*r_has_small_texture = p_file->get_line().to_int();
|
|
|
|
*r_modified_time = p_file->get_line().to_int();
|
|
|
|
*r_hash = p_file->get_line();
|
|
|
|
*r_metadata = VariantUtilityFunctions::str_to_var(p_file->get_line());
|
2023-05-31 14:52:50 +00:00
|
|
|
*r_outdated = p_file->get_line().to_int() < CURRENT_METADATA_VERSION;
|
2022-08-19 16:14:57 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 18:11:30 +00:00
|
|
|
void EditorResourcePreview::_thread() {
|
|
|
|
exited.clear();
|
2023-11-10 14:27:51 +00:00
|
|
|
while (!exiting.is_set()) {
|
2020-11-18 18:11:30 +00:00
|
|
|
preview_sem.wait();
|
|
|
|
_iterate();
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
2021-02-10 18:22:13 +00:00
|
|
|
exited.set();
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 18:34:06 +00:00
|
|
|
void EditorResourcePreview::_idle_callback() {
|
|
|
|
if (!singleton) {
|
|
|
|
// Just in case the shutdown of the editor involves the deletion of the singleton
|
|
|
|
// happening while additional idle callbacks can happen.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process preview tasks, trying to leave a little bit of responsiveness worst case.
|
|
|
|
uint64_t start = OS::get_singleton()->get_ticks_msec();
|
|
|
|
while (!singleton->queue.is_empty() && OS::get_singleton()->get_ticks_msec() - start < 100) {
|
|
|
|
singleton->_iterate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-12 11:53:15 +00:00
|
|
|
void EditorResourcePreview::_update_thumbnail_sizes() {
|
|
|
|
if (small_thumbnail_size == -1) {
|
2023-09-06 14:11:05 +00:00
|
|
|
// Kind of a workaround to retrieve the default icon size.
|
|
|
|
small_thumbnail_size = EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("Object"), EditorStringName(EditorIcons))->get_width();
|
2023-05-12 11:53:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-02 20:33:38 +00:00
|
|
|
EditorResourcePreview::PreviewItem EditorResourcePreview::get_resource_preview_if_available(const String &p_path) {
|
|
|
|
PreviewItem item;
|
|
|
|
{
|
|
|
|
MutexLock lock(preview_mutex);
|
|
|
|
|
|
|
|
HashMap<String, EditorResourcePreview::Item>::Iterator I = cache.find(p_path);
|
|
|
|
if (!I) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorResourcePreview::Item &cached_item = I->value;
|
|
|
|
item.preview = cached_item.preview;
|
|
|
|
item.small_preview = cached_item.small_preview;
|
|
|
|
}
|
|
|
|
preview_sem.post();
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2016-05-27 17:18:40 +00:00
|
|
|
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());
|
2023-05-12 11:53:15 +00:00
|
|
|
_update_thumbnail_sizes();
|
2016-05-27 17:18:40 +00:00
|
|
|
|
2020-02-26 10:28:13 +00:00
|
|
|
{
|
|
|
|
MutexLock lock(preview_mutex);
|
2016-05-27 17:18:40 +00:00
|
|
|
|
2020-02-26 10:28:13 +00:00
|
|
|
String path_id = "ID:" + itos(p_res->get_instance_id());
|
2016-07-03 16:15:15 +00:00
|
|
|
|
2024-03-07 16:45:13 +00:00
|
|
|
if (cache.has(path_id) && cache[path_id].last_hash == p_res->hash_edited_version_for_preview()) {
|
2020-02-26 10:28:13 +00:00
|
|
|
p_receiver->call(p_receiver_func, path_id, cache[path_id].preview, cache[path_id].small_preview, p_userdata);
|
|
|
|
return;
|
|
|
|
}
|
2016-05-27 17:18:40 +00:00
|
|
|
|
2020-02-26 10:28:13 +00:00
|
|
|
cache.erase(path_id); //erase if exists, since it will be regen
|
2016-07-03 16:15:15 +00:00
|
|
|
|
2020-02-26 10:28:13 +00:00
|
|
|
QueueItem item;
|
|
|
|
item.function = p_receiver_func;
|
|
|
|
item.id = p_receiver->get_instance_id();
|
|
|
|
item.resource = p_res;
|
|
|
|
item.path = path_id;
|
|
|
|
item.userdata = p_userdata;
|
2016-05-27 17:18:40 +00:00
|
|
|
|
2020-02-26 10:28:13 +00:00
|
|
|
queue.push_back(item);
|
|
|
|
}
|
2020-03-03 08:26:42 +00:00
|
|
|
preview_sem.post();
|
2016-05-27 17:18:40 +00:00
|
|
|
}
|
2015-05-31 04:59:42 +00:00
|
|
|
|
|
|
|
void EditorResourcePreview::queue_resource_preview(const String &p_path, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata) {
|
2023-11-10 14:27:51 +00:00
|
|
|
ERR_FAIL_NULL(p_receiver);
|
2023-05-12 11:53:15 +00:00
|
|
|
_update_thumbnail_sizes();
|
|
|
|
|
2020-02-26 10:28:13 +00:00
|
|
|
{
|
|
|
|
MutexLock lock(preview_mutex);
|
|
|
|
|
|
|
|
if (cache.has(p_path)) {
|
|
|
|
p_receiver->call(p_receiver_func, p_path, cache[p_path].preview, cache[p_path].small_preview, p_userdata);
|
|
|
|
return;
|
|
|
|
}
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2020-02-26 10:28:13 +00:00
|
|
|
QueueItem item;
|
|
|
|
item.function = p_receiver_func;
|
|
|
|
item.id = p_receiver->get_instance_id();
|
|
|
|
item.path = p_path;
|
|
|
|
item.userdata = p_userdata;
|
2015-05-31 04:59:42 +00:00
|
|
|
|
2020-02-26 10:28:13 +00:00
|
|
|
queue.push_back(item);
|
|
|
|
}
|
2020-03-03 08:26:42 +00:00
|
|
|
preview_sem.post();
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorResourcePreview::add_preview_generator(const Ref<EditorResourcePreviewGenerator> &p_generator) {
|
|
|
|
preview_generators.push_back(p_generator);
|
|
|
|
}
|
|
|
|
|
2016-09-10 18:32:17 +00:00
|
|
|
void EditorResourcePreview::remove_preview_generator(const Ref<EditorResourcePreviewGenerator> &p_generator) {
|
|
|
|
preview_generators.erase(p_generator);
|
|
|
|
}
|
|
|
|
|
2015-05-31 04:59:42 +00:00
|
|
|
EditorResourcePreview *EditorResourcePreview::get_singleton() {
|
|
|
|
return singleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorResourcePreview::_bind_methods() {
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("queue_resource_preview", "path", "receiver", "receiver_func", "userdata"), &EditorResourcePreview::queue_resource_preview);
|
|
|
|
ClassDB::bind_method(D_METHOD("queue_edited_resource_preview", "resource", "receiver", "receiver_func", "userdata"), &EditorResourcePreview::queue_edited_resource_preview);
|
|
|
|
ClassDB::bind_method(D_METHOD("add_preview_generator", "generator"), &EditorResourcePreview::add_preview_generator);
|
|
|
|
ClassDB::bind_method(D_METHOD("remove_preview_generator", "generator"), &EditorResourcePreview::remove_preview_generator);
|
2017-02-13 11:47:24 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("check_for_invalidation", "path"), &EditorResourcePreview::check_for_invalidation);
|
2016-07-03 16:15:15 +00:00
|
|
|
|
|
|
|
ADD_SIGNAL(MethodInfo("preview_invalidated", PropertyInfo(Variant::STRING, "path")));
|
|
|
|
}
|
|
|
|
|
2016-07-07 02:46:04 +00:00
|
|
|
void EditorResourcePreview::check_for_invalidation(const String &p_path) {
|
2016-07-03 16:15:15 +00:00
|
|
|
bool call_invalidated = false;
|
2020-02-26 10:28:13 +00:00
|
|
|
{
|
2020-03-13 10:13:58 +00:00
|
|
|
MutexLock lock(preview_mutex);
|
|
|
|
|
2020-02-26 10:28:13 +00:00
|
|
|
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);
|
|
|
|
call_invalidated = true;
|
|
|
|
}
|
2016-07-03 16:15:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (call_invalidated) { //do outside mutex
|
2021-07-17 21:22:52 +00:00
|
|
|
call_deferred(SNAME("emit_signal"), "preview_invalidated", p_path);
|
2016-07-03 16:15:15 +00:00
|
|
|
}
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 16:31:11 +00:00
|
|
|
void EditorResourcePreview::start() {
|
2024-01-15 18:34:06 +00:00
|
|
|
if (DisplayServer::get_singleton()->get_name() == "headless") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-21 09:44:21 +00:00
|
|
|
if (is_threaded()) {
|
2023-02-23 20:45:41 +00:00
|
|
|
ERR_FAIL_COND_MSG(thread.is_started(), "Thread already started.");
|
|
|
|
thread.start(_thread_func, this);
|
2024-01-15 18:34:06 +00:00
|
|
|
} else {
|
|
|
|
SceneTree *st = Object::cast_to<SceneTree>(OS::get_singleton()->get_main_loop());
|
|
|
|
ERR_FAIL_NULL_MSG(st, "Editor's MainLoop is not a SceneTree. This is a bug.");
|
2024-02-16 07:27:03 +00:00
|
|
|
st->add_idle_callback(&_idle_callback);
|
2023-02-23 20:45:41 +00:00
|
|
|
}
|
2019-02-27 16:31:11 +00:00
|
|
|
}
|
2019-05-31 06:05:44 +00:00
|
|
|
|
2019-01-17 12:09:01 +00:00
|
|
|
void EditorResourcePreview::stop() {
|
2024-02-21 09:44:21 +00:00
|
|
|
if (is_threaded()) {
|
2024-01-15 18:34:06 +00:00
|
|
|
if (thread.is_started()) {
|
|
|
|
exiting.set();
|
|
|
|
preview_sem.post();
|
2023-11-10 14:27:51 +00:00
|
|
|
|
2024-01-15 18:34:06 +00:00
|
|
|
for (int i = 0; i < preview_generators.size(); i++) {
|
|
|
|
preview_generators.write[i]->abort();
|
|
|
|
}
|
2023-11-10 14:27:51 +00:00
|
|
|
|
2024-01-15 18:34:06 +00:00
|
|
|
while (!exited.is_set()) {
|
2024-09-13 10:19:06 +00:00
|
|
|
// Sync pending work.
|
2024-01-15 18:34:06 +00:00
|
|
|
OS::get_singleton()->delay_usec(10000);
|
2024-09-13 10:19:06 +00:00
|
|
|
RenderingServer::get_singleton()->sync();
|
|
|
|
MessageQueue::get_singleton()->flush();
|
2024-01-15 18:34:06 +00:00
|
|
|
}
|
2023-11-10 14:27:51 +00:00
|
|
|
|
2024-01-15 18:34:06 +00:00
|
|
|
thread.wait_to_finish();
|
|
|
|
}
|
2019-01-17 12:09:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-31 04:59:42 +00:00
|
|
|
EditorResourcePreview::EditorResourcePreview() {
|
|
|
|
singleton = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorResourcePreview::~EditorResourcePreview() {
|
2019-01-17 12:09:01 +00:00
|
|
|
stop();
|
2015-05-31 04:59:42 +00:00
|
|
|
}
|