diff --git a/common/settings.h b/common/settings.h index 52abe1994bd60..bf615379637e5 100644 --- a/common/settings.h +++ b/common/settings.h @@ -213,6 +213,15 @@ struct Settings { /// to log a timeline event that tracks the latency of engine startup. std::chrono::microseconds engine_start_timestamp = {}; + /// Whether the application claims that it uses the android embedded view for + /// platform views. + /// + /// A `true` value will result the raster task runner always run on the + /// platform thread. + // TODO(cyanlaz): Remove this when dynamic thread merging is done. + // https://github.com/flutter/flutter/issues/59930 + bool use_embedded_view = false; + std::string ToString() const; }; diff --git a/shell/common/switches.cc b/shell/common/switches.cc index e3f6e985d4d74..34c4c4ee56f46 100644 --- a/shell/common/switches.cc +++ b/shell/common/switches.cc @@ -227,6 +227,9 @@ Settings SettingsFromCommandLine(const fml::CommandLine& command_line) { : "127.0.0.1"; } + settings.use_embedded_view = + command_line.HasOption(FlagForSwitch(Switch::UseEmbeddedView)); + // Set Observatory Port if (command_line.HasOption(FlagForSwitch(Switch::DeviceObservatoryPort))) { if (!GetSwitchValue(command_line, Switch::DeviceObservatoryPort, diff --git a/shell/common/switches.h b/shell/common/switches.h index 3bef7d645dde9..8506bc7e72496 100644 --- a/shell/common/switches.h +++ b/shell/common/switches.h @@ -189,6 +189,15 @@ DEF_SWITCH( "Uses separate threads for the platform, UI, GPU and IO task runners. " "By default, a single thread is used for all task runners. Only available " "in the flutter_tester.") +// TODO(cyanlaz): Remove this when dynamic thread merging is done. +// https://github.com/flutter/flutter/issues/59930 +DEF_SWITCH(UseEmbeddedView, + "use-embedded-view", + "Whether an android application uses embedded views." + "This is a temporary flag to make the raster task runner runs on " + "the platform thread." + "This flag should be removed once the dynamic thread merging is " + "enabled on android.") DEF_SWITCHES_END void PrintUsage(const std::string& executable_name); diff --git a/shell/platform/android/android_shell_holder.cc b/shell/platform/android/android_shell_holder.cc index 62f8acf6fc8fd..c9667dcb7edf3 100644 --- a/shell/platform/android/android_shell_holder.cc +++ b/shell/platform/android/android_shell_holder.cc @@ -28,6 +28,8 @@ static WindowData GetDefaultWindowData() { return window_data; } +bool AndroidShellHolder::use_embedded_view; + AndroidShellHolder::AndroidShellHolder( flutter::Settings settings, std::shared_ptr jni_facade, @@ -101,20 +103,47 @@ AndroidShellHolder::AndroidShellHolder( ui_runner = thread_host_.ui_thread->GetTaskRunner(); io_runner = thread_host_.io_thread->GetTaskRunner(); } - flutter::TaskRunners task_runners(thread_label, // label - platform_runner, // platform - gpu_runner, // raster - ui_runner, // ui - io_runner // io - ); - - shell_ = - Shell::Create(task_runners, // task runners - GetDefaultWindowData(), // window data - settings_, // settings - on_create_platform_view, // platform view create callback - on_create_rasterizer // rasterizer create callback - ); + if (settings.use_embedded_view) { + use_embedded_view = true; + // Embedded views requires the gpu and the platform views to be the same. + // The plan is to eventually dynamically merge the threads when there's a + // platform view in the layer tree. + // For now we use a fixed thread configuration with the same thread used as + // the gpu and platform task runner. + // TODO(amirh/chinmaygarde): remove this, and dynamically change the thread + // configuration. https://github.com/flutter/flutter/issues/23975 + // https://github.com/flutter/flutter/issues/59930 + flutter::TaskRunners task_runners(thread_label, // label + platform_runner, // platform + platform_runner, // raster + ui_runner, // ui + io_runner // io + ); + + shell_ = + Shell::Create(task_runners, // task runners + GetDefaultWindowData(), // window data + settings_, // settings + on_create_platform_view, // platform view create callback + on_create_rasterizer // rasterizer create callback + ); + } else { + use_embedded_view = false; + flutter::TaskRunners task_runners(thread_label, // label + platform_runner, // platform + gpu_runner, // raster + ui_runner, // ui + io_runner // io + ); + + shell_ = + Shell::Create(task_runners, // task runners + GetDefaultWindowData(), // window data + settings_, // settings + on_create_platform_view, // platform view create callback + on_create_rasterizer // rasterizer create callback + ); + } platform_view_ = weak_platform_view; FML_DCHECK(platform_view_); @@ -122,7 +151,7 @@ AndroidShellHolder::AndroidShellHolder( is_valid_ = shell_ != nullptr; if (is_valid_) { - task_runners.GetRasterTaskRunner()->PostTask([]() { + 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. @@ -134,7 +163,7 @@ AndroidShellHolder::AndroidShellHolder( } } }); - task_runners.GetUITaskRunner()->PostTask([]() { + shell_->GetTaskRunners().GetUITaskRunner()->PostTask([]() { if (::setpriority(PRIO_PROCESS, gettid(), -1) != 0) { FML_LOG(ERROR) << "Failed to set UI task runner priority"; } diff --git a/shell/platform/android/android_shell_holder.h b/shell/platform/android/android_shell_holder.h index 107e93f18672a..6fb6695801733 100644 --- a/shell/platform/android/android_shell_holder.h +++ b/shell/platform/android/android_shell_holder.h @@ -21,6 +21,14 @@ namespace flutter { class AndroidShellHolder { public: + // Whether the application sets to use embedded_view view + // `io.flutter.embedded_views_preview` flag. This can be static because it is + // determined by the application and it is safe when there are multiple + // `AndroidSurface`s. + // TODO(cyanglaz): remove this when dynamic thread merging is enabled on + // android. https://github.com/flutter/flutter/issues/59930 + static bool use_embedded_view; + AndroidShellHolder(flutter::Settings settings, std::shared_ptr jni_facade, bool is_background_view); diff --git a/shell/platform/android/android_surface_gl.cc b/shell/platform/android/android_surface_gl.cc index f4bd9e6747907..c8cc3d2d1183e 100644 --- a/shell/platform/android/android_surface_gl.cc +++ b/shell/platform/android/android_surface_gl.cc @@ -8,6 +8,7 @@ #include "flutter/fml/logging.h" #include "flutter/fml/memory/ref_ptr.h" +#include "flutter/shell/platform/android/android_shell_holder.h" namespace flutter { @@ -122,6 +123,9 @@ intptr_t AndroidSurfaceGL::GLContextFBO() const { // |GPUSurfaceGLDelegate| ExternalViewEmbedder* AndroidSurfaceGL::GetExternalViewEmbedder() { + if (!AndroidShellHolder::use_embedded_view) { + return nullptr; + } return external_view_embedder_.get(); } diff --git a/shell/platform/android/android_surface_software.cc b/shell/platform/android/android_surface_software.cc index 7face67038d0a..3bc52e317279a 100644 --- a/shell/platform/android/android_surface_software.cc +++ b/shell/platform/android/android_surface_software.cc @@ -11,6 +11,7 @@ #include "flutter/fml/platform/android/jni_weak_ref.h" #include "flutter/fml/platform/android/scoped_java_ref.h" #include "flutter/fml/trace_event.h" +#include "flutter/shell/platform/android/android_shell_holder.h" #include "flutter/shell/platform/android/jni/platform_view_android_jni.h" namespace flutter { @@ -145,6 +146,9 @@ bool AndroidSurfaceSoftware::PresentBackingStore( // |GPUSurfaceSoftwareDelegate| ExternalViewEmbedder* AndroidSurfaceSoftware::GetExternalViewEmbedder() { + if (!AndroidShellHolder::use_embedded_view) { + return nullptr; + } return external_view_embedder_.get(); } diff --git a/shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java b/shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java index c57d8c86b8374..9156a64ef061e 100644 --- a/shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java +++ b/shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java @@ -201,7 +201,6 @@ public void ensureInitializationComplete( + applicationInfo.nativeLibraryDir + File.separator + DEFAULT_LIBRARY); - if (args != null) { Collections.addAll(shellArgs, args); } @@ -234,6 +233,17 @@ public void ensureInitializationComplete( } long initTimeMillis = SystemClock.uptimeMillis() - initStartTimestampMillis; + + // TODO(cyanlaz): Remove this when dynamic thread merging is done. + // https://github.com/flutter/flutter/issues/59930 + Bundle bundle = applicationInfo.metaData; + if (bundle != null) { + boolean use_embedded_view = bundle.getBoolean("io.flutter.embedded_views_preview"); + if (use_embedded_view) { + shellArgs.add("--use-embedded-view"); + } + } + FlutterJNI.nativeInit( applicationContext, shellArgs.toArray(new String[0]), diff --git a/shell/platform/android/platform_view_android.h b/shell/platform/android/platform_view_android.h index c0b6a1d79a4d8..c7b22fb17070e 100644 --- a/shell/platform/android/platform_view_android.h +++ b/shell/platform/android/platform_view_android.h @@ -117,7 +117,6 @@ class PlatformViewAndroid final : public PlatformView { FML_DISALLOW_COPY_AND_ASSIGN(PlatformViewAndroid); }; - } // namespace flutter #endif // SHELL_PLATFORM_ANDROID_PLATFORM_VIEW_ANDROID_H_ diff --git a/testing/scenario_app/android/app/src/main/AndroidManifest.xml b/testing/scenario_app/android/app/src/main/AndroidManifest.xml index 65ff9e909329b..a7416851ccd0d 100644 --- a/testing/scenario_app/android/app/src/main/AndroidManifest.xml +++ b/testing/scenario_app/android/app/src/main/AndroidManifest.xml @@ -28,6 +28,9 @@ +