3232
3333namespace flutter {
3434
35+ // / Inheriting ThreadConfigurer and use Android platform thread API to configure
36+ // / the thread priorities
37+ class PlatformAndroidThreadConfig : public fml ::Thread::ThreadConfig {
38+ public:
39+ using fml::Thread::ThreadConfig::ThreadConfig;
40+
41+ void SetCurrentThreadPriority () override {
42+ switch (thread_priority ()) {
43+ case fml::Thread::ThreadPriority::BACKGROUND:
44+ if (::setpriority (PRIO_PROCESS, 0 , 10 ) != 0 ) {
45+ FML_LOG (ERROR) << " Failed to set IO task runner priority" ;
46+ }
47+ break ;
48+ case fml::Thread::ThreadPriority::DISPLAY: {
49+ if (::setpriority (PRIO_PROCESS, 0 , -1 ) != 0 ) {
50+ FML_LOG (ERROR) << " Failed to set UI task runner priority" ;
51+ }
52+ break ;
53+ }
54+ case fml::Thread::ThreadPriority::RASTER: {
55+ // Android describes -8 as "most important display threads, for
56+ // compositing the screen and retrieving input events". Conservatively
57+ // set the raster thread to slightly lower priority than it.
58+ if (::setpriority (PRIO_PROCESS, 0 , -5 ) != 0 ) {
59+ // Defensive fallback. Depending on the OEM, it may not be possible
60+ // to set priority to -5.
61+ if (::setpriority (PRIO_PROCESS, 0 , -2 ) != 0 ) {
62+ FML_LOG (ERROR) << " Failed to set raster task runner priority" ;
63+ }
64+ }
65+ }
66+ default :
67+ if (::setpriority (PRIO_PROCESS, 0 , 0 ) != 0 ) {
68+ FML_LOG (ERROR) << " Failed to set priority" ;
69+ }
70+ }
71+ }
72+ };
73+
3574static PlatformData GetDefaultPlatformData () {
3675 PlatformData platform_data;
3776 platform_data.lifecycle_state = " AppLifecycleState.detached" ;
@@ -45,10 +84,23 @@ AndroidShellHolder::AndroidShellHolder(
4584 static size_t thread_host_count = 1 ;
4685 auto thread_label = std::to_string (thread_host_count++);
4786
48- thread_host_ = std::make_shared<ThreadHost>();
49- *thread_host_ = {thread_label, ThreadHost::Type::UI |
50- ThreadHost::Type::RASTER |
51- ThreadHost::Type::IO};
87+ auto ui_thread_name = flutter::ThreadHost::ThreadName (
88+ flutter::ThreadHost::Type::UI, thread_label);
89+ auto raster_thread_name = flutter::ThreadHost::ThreadName (
90+ flutter::ThreadHost::Type::UI, thread_label);
91+ auto io_thread_name = flutter::ThreadHost::ThreadName (
92+ flutter::ThreadHost::Type::UI, thread_label);
93+
94+ thread_host_ = std::make_shared<ThreadHost>(
95+ thread_label,
96+ ThreadHost::Type::UI | ThreadHost::Type::RASTER | ThreadHost::Type::IO,
97+ (flutter::ThreadHost::ThreadHostConfig){
98+ .ui_configure = std::make_unique<PlatformAndroidThreadConfig>(
99+ ui_thread_name, fml::Thread::ThreadPriority::DISPLAY),
100+ .raster_configure = std::make_unique<PlatformAndroidThreadConfig>(
101+ raster_thread_name, fml::Thread::ThreadPriority::RASTER),
102+ .io_configure = std::make_unique<PlatformAndroidThreadConfig>(
103+ io_thread_name, fml::Thread::ThreadPriority::BACKGROUND)});
52104
53105 fml::WeakPtr<PlatformViewAndroid> weak_platform_view;
54106 Shell::CreateCallback<PlatformView> on_create_platform_view =
@@ -91,28 +143,6 @@ AndroidShellHolder::AndroidShellHolder(
91143 ui_runner, // ui
92144 io_runner // io
93145 );
94- task_runners.GetRasterTaskRunner ()->PostTask ([]() {
95- // Android describes -8 as "most important display threads, for
96- // compositing the screen and retrieving input events". Conservatively
97- // set the raster thread to slightly lower priority than it.
98- if (::setpriority (PRIO_PROCESS, gettid (), -5 ) != 0 ) {
99- // Defensive fallback. Depending on the OEM, it may not be possible
100- // to set priority to -5.
101- if (::setpriority (PRIO_PROCESS, gettid (), -2 ) != 0 ) {
102- FML_LOG (ERROR) << " Failed to set raster task runner priority" ;
103- }
104- }
105- });
106- task_runners.GetUITaskRunner ()->PostTask ([]() {
107- if (::setpriority (PRIO_PROCESS, gettid (), -1 ) != 0 ) {
108- FML_LOG (ERROR) << " Failed to set UI task runner priority" ;
109- }
110- });
111- task_runners.GetIOTaskRunner ()->PostTask ([]() {
112- if (::setpriority (PRIO_PROCESS, gettid (), 1 ) != 0 ) {
113- FML_LOG (ERROR) << " Failed to set IO task runner priority" ;
114- }
115- });
116146
117147 shell_ =
118148 Shell::Create (GetDefaultPlatformData (), // window data
0 commit comments