Skip to content

Commit

Permalink
Fix slow importation when window is unfocused
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilderin committed Aug 27, 2024
1 parent db24ed4 commit ce42d9d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
18 changes: 17 additions & 1 deletion editor/editor_file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1988,7 +1988,7 @@ void EditorFileSystem::_update_scene_groups() {
}

if (ep) {
ep->step(efd->files[index]->file, step_count++);
ep->step(efd->files[index]->file, step_count++, false);
}
}

Expand Down Expand Up @@ -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()));

// 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;

HashSet<String> groups_to_reimport;
Expand Down Expand Up @@ -2836,13 +2846,19 @@ 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.
_save_filesystem_cache();

memdelete_notnull(ep);

_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;

ep = memnew(EditorProgress("reimport", TTR("(Re)Importing Assets"), p_files.size()));
Expand Down
8 changes: 3 additions & 5 deletions editor/progress_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,22 +202,21 @@ 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) {
ERR_FAIL_COND_V(!tasks.has(p_task), canceled);

Task &t = tasks[p_task];
if (!p_force_redraw) {
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;
}
}

Task &t = tasks[p_task];
if (p_step < 0) {
t.progress->set_value(t.progress->get_value() + 1);
} else {
t.progress->set_value(p_step);
}

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();

return canceled;
Expand Down Expand Up @@ -252,7 +251,6 @@ ProgressDialog::ProgressDialog() {
main->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
set_exclusive(true);
set_flag(Window::FLAG_POPUP, false);
last_progress_tick = 0;
singleton = this;
cancel_hb = memnew(HBoxContainer);
main->add_child(cancel_hb);
Expand Down
2 changes: 1 addition & 1 deletion editor/progress_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ class ProgressDialog : public PopupPanel {
VBoxContainer *vb = nullptr;
ProgressBar *progress = nullptr;
Label *state = nullptr;
uint64_t last_progress_tick = 0;
};
HBoxContainer *cancel_hb = nullptr;
Button *cancel = nullptr;

HashMap<String, Task> tasks;
VBoxContainer *main = nullptr;
uint64_t last_progress_tick;

LocalVector<Window *> host_windows;

Expand Down

0 comments on commit ce42d9d

Please sign in to comment.