Abort threaded preview generators on exit

This commit is contained in:
Yuri Sizov 2023-11-10 15:27:51 +01:00
parent 2b987d1c54
commit e90ea87b42
5 changed files with 114 additions and 110 deletions

View File

@ -206,16 +206,21 @@ const Dictionary EditorResourcePreview::get_preview_metadata(const String &p_pat
void EditorResourcePreview::_iterate() {
preview_mutex.lock();
if (queue.size()) {
if (queue.size() == 0) {
preview_mutex.unlock();
return;
}
QueueItem item = queue.front()->get();
queue.pop_front();
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, cache[item.path].last_hash, cache[item.path].preview, cache[item.path].small_preview, item.id, item.function, item.userdata, cache[item.path].preview_metadata);
_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();
} else {
return;
}
preview_mutex.unlock();
Ref<ImageTexture> texture;
@ -227,10 +232,10 @@ void EditorResourcePreview::_iterate() {
if (item.resource.is_valid()) {
Dictionary preview_metadata;
_generate_preview(texture, small_texture, item, String(), preview_metadata);
_preview_ready(item.path, item.resource->hash_edited_version(), texture, small_texture, item.id, item.function, item.userdata, preview_metadata);
return;
}
} else {
Dictionary preview_metadata;
String temp_path = EditorPaths::get_singleton()->get_cache_dir();
String cache_base = ProjectSettings::get_singleton()->globalize_path(item.path).md5_text();
@ -308,12 +313,6 @@ void EditorResourcePreview::_iterate() {
}
_preview_ready(item.path, 0, texture, small_texture, item.id, item.function, item.userdata, preview_metadata);
}
}
} else {
preview_mutex.unlock();
}
}
void EditorResourcePreview::_write_preview_cache(Ref<FileAccess> p_file, int p_thumbnail_size, bool p_has_small_texture, uint64_t p_modified_time, String p_hash, const Dictionary &p_metadata) {
p_file->store_line(itos(p_thumbnail_size));
@ -333,7 +332,7 @@ void EditorResourcePreview::_read_preview_cache(Ref<FileAccess> p_file, int *r_t
void EditorResourcePreview::_thread() {
exited.clear();
while (!exit.is_set()) {
while (!exiting.is_set()) {
preview_sem.wait();
_iterate();
}
@ -378,9 +377,9 @@ 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);
_update_thumbnail_sizes();
ERR_FAIL_NULL(p_receiver);
{
MutexLock lock(preview_mutex);
@ -414,8 +413,6 @@ EditorResourcePreview *EditorResourcePreview::get_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);
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);
@ -453,12 +450,18 @@ void EditorResourcePreview::start() {
void EditorResourcePreview::stop() {
if (thread.is_started()) {
exit.set();
exiting.set();
preview_sem.post();
for (int i = 0; i < preview_generators.size(); i++) {
preview_generators.write[i]->abort();
}
while (!exited.is_set()) {
OS::get_singleton()->delay_usec(10000);
RenderingServer::get_singleton()->sync(); //sync pending stuff, as thread may be blocked on rendering server
}
thread.wait_to_finish();
}
}

View File

@ -55,6 +55,7 @@ public:
virtual bool handles(const String &p_type) const;
virtual Ref<Texture2D> generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const;
virtual Ref<Texture2D> generate_from_path(const String &p_path, const Size2 &p_size, Dictionary &p_metadata) const;
virtual void abort(){};
virtual bool generate_small_preview_automatically() const;
virtual bool can_generate_small_preview() const;
@ -80,7 +81,7 @@ class EditorResourcePreview : public Node {
Mutex preview_mutex;
Semaphore preview_sem;
Thread thread;
SafeFlag exit;
SafeFlag exiting;
SafeFlag exited;
struct Item {

View File

@ -295,6 +295,10 @@ void EditorMaterialPreviewPlugin::_preview_done() {
preview_done.post();
}
void EditorMaterialPreviewPlugin::abort() {
preview_done.post();
}
bool EditorMaterialPreviewPlugin::handles(const String &p_type) const {
return ClassDB::is_parent_class(p_type, "Material"); // Any material.
}
@ -690,6 +694,10 @@ void EditorMeshPreviewPlugin::_preview_done() {
preview_done.post();
}
void EditorMeshPreviewPlugin::abort() {
preview_done.post();
}
bool EditorMeshPreviewPlugin::handles(const String &p_type) const {
return ClassDB::is_parent_class(p_type, "Mesh"); // Any mesh.
}
@ -807,6 +815,10 @@ void EditorFontPreviewPlugin::_preview_done() {
preview_done.post();
}
void EditorFontPreviewPlugin::abort() {
preview_done.post();
}
bool EditorFontPreviewPlugin::handles(const String &p_type) const {
return ClassDB::is_parent_class(p_type, "Font");
}

View File

@ -103,6 +103,7 @@ public:
virtual bool handles(const String &p_type) const override;
virtual bool generate_small_preview_automatically() const override;
virtual Ref<Texture2D> generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const override;
virtual void abort() override;
EditorMaterialPreviewPlugin();
~EditorMaterialPreviewPlugin();
@ -149,6 +150,7 @@ class EditorMeshPreviewPlugin : public EditorResourcePreviewGenerator {
public:
virtual bool handles(const String &p_type) const override;
virtual Ref<Texture2D> generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const override;
virtual void abort() override;
EditorMeshPreviewPlugin();
~EditorMeshPreviewPlugin();
@ -170,27 +172,12 @@ public:
virtual bool handles(const String &p_type) const override;
virtual Ref<Texture2D> generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const override;
virtual Ref<Texture2D> generate_from_path(const String &p_path, const Size2 &p_size, Dictionary &p_metadata) const override;
virtual void abort() override;
EditorFontPreviewPlugin();
~EditorFontPreviewPlugin();
};
class EditorTileMapPatternPreviewPlugin : public EditorResourcePreviewGenerator {
GDCLASS(EditorTileMapPatternPreviewPlugin, EditorResourcePreviewGenerator);
Semaphore preview_done;
void _generate_frame_started();
void _preview_done();
public:
virtual bool handles(const String &p_type) const override;
virtual Ref<Texture2D> generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const override;
EditorTileMapPatternPreviewPlugin();
~EditorTileMapPatternPreviewPlugin();
};
class EditorGradientPreviewPlugin : public EditorResourcePreviewGenerator {
GDCLASS(EditorGradientPreviewPlugin, EditorResourcePreviewGenerator);

View File

@ -92,7 +92,8 @@ void SurfaceUpgradeTool::_show_popup() {
EditorSettings::get_singleton()->set_project_metadata("surface_upgrade_tool", "reimport_paths", reimport_paths);
EditorSettings::get_singleton()->set_project_metadata("surface_upgrade_tool", "resave_paths", resave_paths);
EditorNode::get_singleton()->restart_editor();
// Delay to avoid deadlocks, since this dialog can be triggered by loading a scene.
MessageQueue::get_singleton()->push_callable(callable_mp(EditorNode::get_singleton(), &EditorNode::restart_editor));
} else {
RS::get_singleton()->set_warn_on_surface_upgrade(true);
}