From 564367b29781d1480ab11d8b2dc85bb0c82162a8 Mon Sep 17 00:00:00 2001 From: linxuebin Date: Thu, 10 Sep 2020 05:07:34 +0800 Subject: [PATCH] Increase thread priority before engine init (#20922) We get huge ANRs in not pure flutter app. We found that 0.15% of the engine initialization took more than 10 seconds, every 10 million starts. --- .../platform/android/android_shell_holder.cc | 38 +++++++++---------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/shell/platform/android/android_shell_holder.cc b/shell/platform/android/android_shell_holder.cc index d58ad703f6..8e1448c13d 100644 --- a/shell/platform/android/android_shell_holder.cc +++ b/shell/platform/android/android_shell_holder.cc @@ -107,6 +107,23 @@ AndroidShellHolder::AndroidShellHolder( ui_runner, // ui io_runner // io ); + task_runners.GetRasterTaskRunner()->PostTask([]() { + // Android describes -8 as "most important display threads, for + // compositing the screen and retrieving input events". Conservatively + // set the raster thread to slightly lower priority than it. + if (::setpriority(PRIO_PROCESS, gettid(), -5) != 0) { + // Defensive fallback. Depending on the OEM, it may not be possible + // to set priority to -5. + if (::setpriority(PRIO_PROCESS, gettid(), -2) != 0) { + FML_LOG(ERROR) << "Failed to set GPU task runner priority"; + } + } + }); + task_runners.GetUITaskRunner()->PostTask([]() { + if (::setpriority(PRIO_PROCESS, gettid(), -1) != 0) { + FML_LOG(ERROR) << "Failed to set UI task runner priority"; + } + }); shell_ = Shell::Create(task_runners, // task runners @@ -118,28 +135,7 @@ AndroidShellHolder::AndroidShellHolder( platform_view_ = weak_platform_view; FML_DCHECK(platform_view_); - is_valid_ = shell_ != nullptr; - - if (is_valid_) { - shell_->GetTaskRunners().GetRasterTaskRunner()->PostTask([]() { - // Android describes -8 as "most important display threads, for - // compositing the screen and retrieving input events". Conservatively - // set the raster thread to slightly lower priority than it. - if (::setpriority(PRIO_PROCESS, gettid(), -5) != 0) { - // Defensive fallback. Depending on the OEM, it may not be possible - // to set priority to -5. - if (::setpriority(PRIO_PROCESS, gettid(), -2) != 0) { - FML_LOG(ERROR) << "Failed to set GPU task runner priority"; - } - } - }); - shell_->GetTaskRunners().GetUITaskRunner()->PostTask([]() { - if (::setpriority(PRIO_PROCESS, gettid(), -1) != 0) { - FML_LOG(ERROR) << "Failed to set UI task runner priority"; - } - }); - } } AndroidShellHolder::~AndroidShellHolder() { -- GitLab