Fix slow importation when window is unfocused

This commit is contained in:
Hilderin 2024-07-04 15:30:18 -04:00
parent db24ed4ead
commit ce42d9dcfe
3 changed files with 21 additions and 7 deletions

View File

@ -1988,7 +1988,7 @@ void EditorFileSystem::_update_scene_groups() {
} }
if (ep) { if (ep) {
ep->step(efd->files[index]->file, step_count++); ep->step(efd->files[index]->file, step_count++, false);
} }
} }
@ -2706,6 +2706,16 @@ void EditorFileSystem::reimport_files(const Vector<String> &p_files) {
EditorProgress *ep = memnew(EditorProgress("reimport", TTR("(Re)Importing Assets"), p_files.size())); EditorProgress *ep = memnew(EditorProgress("reimport", TTR("(Re)Importing Assets"), p_files.size()));
// The method reimport_files runs on the main thread, and if VSync is enabled
// or Update Continuously is disabled, Main::Iteration takes longer each frame.
// Each EditorProgress::step can trigger a redraw, and when there are many files to import,
// this could lead to a slow import process, especially when the editor is unfocused.
// Temporarily disabling VSync and low_processor_usage_mode while reimporting fixes this.
const bool old_low_processor_usage_mode = OS::get_singleton()->is_in_low_processor_usage_mode();
const DisplayServer::VSyncMode old_vsync_mode = DisplayServer::get_singleton()->window_get_vsync_mode(DisplayServer::MAIN_WINDOW_ID);
OS::get_singleton()->set_low_processor_usage_mode(false);
DisplayServer::get_singleton()->window_set_vsync_mode(DisplayServer::VSyncMode::VSYNC_DISABLED);
Vector<ImportFile> reimport_files; Vector<ImportFile> reimport_files;
HashSet<String> groups_to_reimport; HashSet<String> groups_to_reimport;
@ -2836,6 +2846,7 @@ void EditorFileSystem::reimport_files(const Vector<String> &p_files) {
} }
} }
} }
ep->step(TTR("Finalizing Asset Import..."), p_files.size());
ResourceUID::get_singleton()->update_cache(); // After reimporting, update the cache. ResourceUID::get_singleton()->update_cache(); // After reimporting, update the cache.
_save_filesystem_cache(); _save_filesystem_cache();
@ -2843,6 +2854,11 @@ void EditorFileSystem::reimport_files(const Vector<String> &p_files) {
memdelete_notnull(ep); memdelete_notnull(ep);
_process_update_pending(); _process_update_pending();
// Revert to previous values to restore editor settings for VSync and Update Continuously.
OS::get_singleton()->set_low_processor_usage_mode(old_low_processor_usage_mode);
DisplayServer::get_singleton()->window_set_vsync_mode(old_vsync_mode);
importing = false; importing = false;
ep = memnew(EditorProgress("reimport", TTR("(Re)Importing Assets"), p_files.size())); ep = memnew(EditorProgress("reimport", TTR("(Re)Importing Assets"), p_files.size()));

View File

@ -202,14 +202,13 @@ void ProgressDialog::add_task(const String &p_task, const String &p_label, int p
bool ProgressDialog::task_step(const String &p_task, const String &p_state, int p_step, bool p_force_redraw) { bool ProgressDialog::task_step(const String &p_task, const String &p_state, int p_step, bool p_force_redraw) {
ERR_FAIL_COND_V(!tasks.has(p_task), canceled); ERR_FAIL_COND_V(!tasks.has(p_task), canceled);
Task &t = tasks[p_task];
if (!p_force_redraw) { if (!p_force_redraw) {
uint64_t tus = OS::get_singleton()->get_ticks_usec(); uint64_t tus = OS::get_singleton()->get_ticks_usec();
if (tus - last_progress_tick < 200000) { //200ms if (tus - t.last_progress_tick < 200000) { //200ms
return canceled; return canceled;
} }
} }
Task &t = tasks[p_task];
if (p_step < 0) { if (p_step < 0) {
t.progress->set_value(t.progress->get_value() + 1); t.progress->set_value(t.progress->get_value() + 1);
} else { } else {
@ -217,7 +216,7 @@ bool ProgressDialog::task_step(const String &p_task, const String &p_state, int
} }
t.state->set_text(p_state); t.state->set_text(p_state);
last_progress_tick = OS::get_singleton()->get_ticks_usec(); t.last_progress_tick = OS::get_singleton()->get_ticks_usec();
_update_ui(); _update_ui();
return canceled; return canceled;
@ -252,7 +251,6 @@ ProgressDialog::ProgressDialog() {
main->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); main->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
set_exclusive(true); set_exclusive(true);
set_flag(Window::FLAG_POPUP, false); set_flag(Window::FLAG_POPUP, false);
last_progress_tick = 0;
singleton = this; singleton = this;
cancel_hb = memnew(HBoxContainer); cancel_hb = memnew(HBoxContainer);
main->add_child(cancel_hb); main->add_child(cancel_hb);

View File

@ -71,13 +71,13 @@ class ProgressDialog : public PopupPanel {
VBoxContainer *vb = nullptr; VBoxContainer *vb = nullptr;
ProgressBar *progress = nullptr; ProgressBar *progress = nullptr;
Label *state = nullptr; Label *state = nullptr;
uint64_t last_progress_tick = 0;
}; };
HBoxContainer *cancel_hb = nullptr; HBoxContainer *cancel_hb = nullptr;
Button *cancel = nullptr; Button *cancel = nullptr;
HashMap<String, Task> tasks; HashMap<String, Task> tasks;
VBoxContainer *main = nullptr; VBoxContainer *main = nullptr;
uint64_t last_progress_tick;
LocalVector<Window *> host_windows; LocalVector<Window *> host_windows;