Skip to content

Commit

Permalink
Fix slow importation when window is unfocused
Browse files Browse the repository at this point in the history
(cherry picked from commit ce42d9d)
  • Loading branch information
Hilderin authored and Spartan322 committed Oct 31, 2024
1 parent 59f0c7c commit 43d6862
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
20 changes: 18 additions & 2 deletions editor/editor_file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1940,7 +1940,7 @@ void EditorFileSystem::_update_scene_groups() {
}

if (ep) {
ep->step(TTR("Updating Scene Groups..."), step_count++);
ep->step(TTR("Updating Scene Groups..."), step_count++, false);
}
}

Expand Down Expand Up @@ -2656,7 +2656,17 @@ void EditorFileSystem::reimport_files(const Vector<String> &p_files) {

Vector<String> reloads;

EditorProgress pr("reimport", TTR("(Re)Importing Assets"), p_files.size());
EditorProgress pr(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;

Expand Down Expand Up @@ -2784,11 +2794,17 @@ void EditorFileSystem::reimport_files(const Vector<String> &p_files) {
}
}
}
pr.step(TTR("Finalizing Asset Import..."), p_files.size());

ResourceUID::get_singleton()->update_cache(); // After reimporting, update the cache.

_save_filesystem_cache();
_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;
if (!is_scanning()) {
emit_signal(SNAME("filesystem_changed"));
Expand Down
8 changes: 3 additions & 5 deletions editor/progress_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,22 +204,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 @@ -254,7 +253,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 @@ -73,13 +73,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 43d6862

Please sign in to comment.