Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix slow import when window is unfocused #93953

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion editor/editor_file_system.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
}

@@ -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;
@@ -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()));
8 changes: 3 additions & 5 deletions editor/progress_dialog.cpp
Original file line number Diff line number Diff line change
@@ -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;
@@ -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);
2 changes: 1 addition & 1 deletion editor/progress_dialog.h
Original file line number Diff line number Diff line change
@@ -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;