Flutter Engine
The Flutter Engine
Public Member Functions | List of all members
flutter::AndroidShellHolder Class Reference

This is the Android owner of the core engine Shell. More...

#include <android_shell_holder.h>

Public Member Functions

 AndroidShellHolder (const flutter::Settings &settings, std::shared_ptr< PlatformViewAndroidJNI > jni_facade)
 
 ~AndroidShellHolder ()
 
bool IsValid () const
 
std::unique_ptr< AndroidShellHolderSpawn (std::shared_ptr< PlatformViewAndroidJNI > jni_facade, const std::string &entrypoint, const std::string &libraryUrl, const std::string &initial_route, const std::vector< std::string > &entrypoint_args) const
 This is a factory for a derived AndroidShellHolder from an existing AndroidShellHolder. More...
 
void Launch (std::unique_ptr< APKAssetProvider > apk_asset_provider, const std::string &entrypoint, const std::string &libraryUrl, const std::vector< std::string > &entrypoint_args)
 
const flutter::SettingsGetSettings () const
 
fml::WeakPtr< PlatformViewAndroidGetPlatformView ()
 
Rasterizer::Screenshot Screenshot (Rasterizer::ScreenshotType type, bool base64_encode)
 
void NotifyLowMemoryWarning ()
 
const std::shared_ptr< PlatformMessageHandler > & GetPlatformMessageHandler () const
 
void UpdateDisplayMetrics ()
 

Detailed Description

This is the Android owner of the core engine Shell.

This is the top orchestrator class on the C++ side for the Android embedding. It corresponds to a FlutterEngine on the Java side. This class is in C++ because the Shell is in C++ and an Android orchestrator needs to exist to compose it with other Android specific C++ components such as the PlatformViewAndroid. This composition of many-to-one C++ components would be difficult to do through JNI whereas a FlutterEngine and AndroidShellHolder has a 1:1 relationship.

Technically, the FlutterJNI class owns this AndroidShellHolder class instance, but the FlutterJNI class is meant to be mostly static and has minimal state to perform the C++ pointer <-> Java class instance translation.

Definition at line 42 of file android_shell_holder.h.

Constructor & Destructor Documentation

◆ AndroidShellHolder()

flutter::AndroidShellHolder::AndroidShellHolder ( const flutter::Settings settings,
std::shared_ptr< PlatformViewAndroidJNI jni_facade 
)

Definition at line 85 of file android_shell_holder.cc.

88 : settings_(settings), jni_facade_(jni_facade) {
89 static size_t thread_host_count = 1;
90 auto thread_label = std::to_string(thread_host_count++);
91
92 auto mask =
93 ThreadHost::Type::kUi | ThreadHost::Type::kRaster | ThreadHost::Type::kIo;
94
96 thread_label, mask, AndroidPlatformThreadConfigSetter);
97 host_config.ui_config = fml::Thread::ThreadConfig(
99 flutter::ThreadHost::Type::kUi, thread_label),
101 host_config.raster_config = fml::Thread::ThreadConfig(
105 host_config.io_config = fml::Thread::ThreadConfig(
107 flutter::ThreadHost::Type::kIo, thread_label),
109
110 thread_host_ = std::make_shared<ThreadHost>(host_config);
111
112 fml::WeakPtr<PlatformViewAndroid> weak_platform_view;
113 Shell::CreateCallback<PlatformView> on_create_platform_view =
114 [&jni_facade, &weak_platform_view](Shell& shell) {
115 std::unique_ptr<PlatformViewAndroid> platform_view_android;
116 platform_view_android = std::make_unique<PlatformViewAndroid>(
117 shell, // delegate
118 shell.GetTaskRunners(), // task runners
119 jni_facade, // JNI interop
120 shell.GetSettings()
121 .enable_software_rendering // use software rendering
122 );
123 weak_platform_view = platform_view_android->GetWeakPtr();
124 return platform_view_android;
125 };
126
127 Shell::CreateCallback<Rasterizer> on_create_rasterizer = [](Shell& shell) {
128 return std::make_unique<Rasterizer>(shell);
129 };
130
131 // The current thread will be used as the platform thread. Ensure that the
132 // message loop is initialized.
134 fml::RefPtr<fml::TaskRunner> raster_runner;
137 fml::RefPtr<fml::TaskRunner> platform_runner =
139 raster_runner = thread_host_->raster_thread->GetTaskRunner();
140 ui_runner = thread_host_->ui_thread->GetTaskRunner();
141 io_runner = thread_host_->io_thread->GetTaskRunner();
142
143 flutter::TaskRunners task_runners(thread_label, // label
144 platform_runner, // platform
145 raster_runner, // raster
146 ui_runner, // ui
147 io_runner // io
148 );
149
150 shell_ =
151 Shell::Create(GetDefaultPlatformData(), // window data
152 task_runners, // task runners
153 settings_, // settings
154 on_create_platform_view, // platform view create callback
155 on_create_rasterizer // rasterizer create callback
156 );
157
158 if (shell_) {
159 shell_->GetDartVM()->GetConcurrentMessageLoop()->PostTaskToAllWorkers([]() {
160 if (::setpriority(PRIO_PROCESS, gettid(), 1) != 0) {
161 FML_LOG(ERROR) << "Failed to set Workers task runner priority";
162 }
163 });
164
165 shell_->RegisterImageDecoder(
166 [runner = task_runners.GetIOTaskRunner()](sk_sp<SkData> buffer) {
167 return AndroidImageGenerator::MakeFromData(std::move(buffer), runner);
168 },
169 -1);
170 FML_DLOG(INFO) << "Registered Android SDK image decoder (API level 28+)";
171 }
172
173 platform_view_ = weak_platform_view;
174 FML_DCHECK(platform_view_);
175 is_valid_ = shell_ != nullptr;
176}
static std::shared_ptr< ImageGenerator > MakeFromData(sk_sp< SkData > data, const fml::RefPtr< fml::TaskRunner > &task_runner)
static std::unique_ptr< Shell > Create(const PlatformData &platform_data, const TaskRunners &task_runners, Settings settings, const CreateCallback< PlatformView > &on_create_platform_view, const CreateCallback< Rasterizer > &on_create_rasterizer, bool is_gpu_disabled=false)
Creates a shell instance using the provided settings. The callbacks to create the various shell subco...
Definition: shell.cc:169
static void EnsureInitializedForCurrentThread()
Definition: message_loop.cc:27
fml::RefPtr< fml::TaskRunner > GetTaskRunner() const
Definition: message_loop.cc:56
static FML_EMBEDDER_ONLY MessageLoop & GetCurrent()
Definition: message_loop.cc:19
@ kNormal
Default priority level.
@ kRaster
Suitable for thread which raster data.
@ kDisplay
Suitable for threads which generate data for the display.
@ kRaster
Suitable for thread which raster data.
Definition: embedder.h:266
#define FML_DLOG(severity)
Definition: logging.h:102
#define FML_LOG(severity)
Definition: logging.h:82
#define FML_DCHECK(condition)
Definition: logging.h:103
static PlatformData GetDefaultPlatformData()
fml::Thread::ThreadConfig ThreadConfig
Definition: thread_host.h:17
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
static void AndroidPlatformThreadConfigSetter(const fml::Thread::ThreadConfig &config)
static SkString to_string(int n)
Definition: nanobench.cpp:119
static std::string MakeThreadName(Type type, const std::string &prefix)
Use the prefix and thread type to generator a thread name.
Definition: thread_host.cc:15
#define ERROR(message)
Definition: elf_loader.cc:260

◆ ~AndroidShellHolder()

flutter::AndroidShellHolder::~AndroidShellHolder ( )

Definition at line 199 of file android_shell_holder.cc.

199 {
200 shell_.reset();
201 thread_host_.reset();
202}

Member Function Documentation

◆ GetPlatformMessageHandler()

const std::shared_ptr< PlatformMessageHandler > & flutter::AndroidShellHolder::GetPlatformMessageHandler ( ) const
inline

Definition at line 101 of file android_shell_holder.h.

102 {
103 return shell_->GetPlatformMessageHandler();
104 }

◆ GetPlatformView()

fml::WeakPtr< PlatformViewAndroid > flutter::AndroidShellHolder::GetPlatformView ( )

Definition at line 304 of file android_shell_holder.cc.

304 {
305 FML_DCHECK(platform_view_);
306 return platform_view_;
307}

◆ GetSettings()

const flutter::Settings & flutter::AndroidShellHolder::GetSettings ( ) const

Definition at line 208 of file android_shell_holder.cc.

208 {
209 return settings_;
210}

◆ IsValid()

bool flutter::AndroidShellHolder::IsValid ( ) const

Definition at line 204 of file android_shell_holder.cc.

204 {
205 return is_valid_;
206}

◆ Launch()

void flutter::AndroidShellHolder::Launch ( std::unique_ptr< APKAssetProvider apk_asset_provider,
const std::string &  entrypoint,
const std::string &  libraryUrl,
const std::vector< std::string > &  entrypoint_args 
)

Definition at line 276 of file android_shell_holder.cc.

280 {
281 if (!IsValid()) {
282 return;
283 }
284
285 apk_asset_provider_ = std::move(apk_asset_provider);
286 auto config = BuildRunConfiguration(entrypoint, libraryUrl, entrypoint_args);
287 if (!config) {
288 return;
289 }
291 shell_->RunEngine(std::move(config.value()));
292}

◆ NotifyLowMemoryWarning()

void flutter::AndroidShellHolder::NotifyLowMemoryWarning ( )

Definition at line 309 of file android_shell_holder.cc.

309 {
310 FML_DCHECK(shell_);
311 shell_->NotifyLowMemoryWarning();
312}

◆ Screenshot()

Rasterizer::Screenshot flutter::AndroidShellHolder::Screenshot ( Rasterizer::ScreenshotType  type,
bool  base64_encode 
)

Definition at line 294 of file android_shell_holder.cc.

296 {
297 if (!IsValid()) {
298 return {nullptr, SkISize::MakeEmpty(), "",
300 }
301 return shell_->Screenshot(type, base64_encode);
302}
GLenum type
static constexpr SkISize MakeEmpty()
Definition: SkSize.h:22

◆ Spawn()

std::unique_ptr< AndroidShellHolder > flutter::AndroidShellHolder::Spawn ( std::shared_ptr< PlatformViewAndroidJNI jni_facade,
const std::string &  entrypoint,
const std::string &  libraryUrl,
const std::string &  initial_route,
const std::vector< std::string > &  entrypoint_args 
) const

This is a factory for a derived AndroidShellHolder from an existing AndroidShellHolder.

Creates one Shell from another Shell where the created Shell takes the opportunity to share any internal components it can. This results is a Shell that has a smaller startup time cost and a smaller memory footprint than an Shell created with a Create function.

The new Shell is returned in a new AndroidShellHolder instance.

The new Shell's flutter::Settings cannot be changed from that of the initial Shell. The RunConfiguration subcomponent can be changed however in the spawned Shell to run a different entrypoint than the existing shell.

Since the AndroidShellHolder both binds downwards to a Shell and also upwards to JNI callbacks that the PlatformViewAndroid makes, the JNI instance holding this AndroidShellHolder should be created first to supply the jni_facade callback.

Parameters
[in]jni_facadethis argument should be the JNI callback facade of a new JNI instance meant to hold this AndroidShellHolder.
Returns
A new AndroidShellHolder containing a new Shell. Returns nullptr when a new Shell can't be created.

Definition at line 212 of file android_shell_holder.cc.

217 {
218 FML_DCHECK(shell_ && shell_->IsSetup())
219 << "A new Shell can only be spawned "
220 "if the current Shell is properly constructed";
221
222 // Pull out the new PlatformViewAndroid from the new Shell to feed to it to
223 // the new AndroidShellHolder.
224 //
225 // It's a weak pointer because it's owned by the Shell (which we're also)
226 // making below. And the AndroidShellHolder then owns the Shell.
227 fml::WeakPtr<PlatformViewAndroid> weak_platform_view;
228
229 // Take out the old AndroidContext to reuse inside the PlatformViewAndroid
230 // of the new Shell.
231 PlatformViewAndroid* android_platform_view = platform_view_.get();
232 // There's some indirection with platform_view_ being a weak pointer but
233 // we just checked that the shell_ exists above and a valid shell is the
234 // owner of the platform view so this weak pointer always exists.
235 FML_DCHECK(android_platform_view);
236 std::shared_ptr<flutter::AndroidContext> android_context =
237 android_platform_view->GetAndroidContext();
238 FML_DCHECK(android_context);
239
240 // This is a synchronous call, so the captures don't have race checks.
241 Shell::CreateCallback<PlatformView> on_create_platform_view =
242 [&jni_facade, android_context, &weak_platform_view](Shell& shell) {
243 std::unique_ptr<PlatformViewAndroid> platform_view_android;
244 platform_view_android = std::make_unique<PlatformViewAndroid>(
245 shell, // delegate
246 shell.GetTaskRunners(), // task runners
247 jni_facade, // JNI interop
248 android_context // Android context
249 );
250 weak_platform_view = platform_view_android->GetWeakPtr();
251 return platform_view_android;
252 };
253
254 Shell::CreateCallback<Rasterizer> on_create_rasterizer = [](Shell& shell) {
255 return std::make_unique<Rasterizer>(shell);
256 };
257
258 // TODO(xster): could be worth tracing this to investigate whether
259 // the IsolateConfiguration could be cached somewhere.
260 auto config = BuildRunConfiguration(entrypoint, libraryUrl, entrypoint_args);
261 if (!config) {
262 // If the RunConfiguration was null, the kernel blob wasn't readable.
263 // Fail the whole thing.
264 return nullptr;
265 }
266
267 std::unique_ptr<flutter::Shell> shell =
268 shell_->Spawn(std::move(config.value()), initial_route,
269 on_create_platform_view, on_create_rasterizer);
270
271 return std::unique_ptr<AndroidShellHolder>(new AndroidShellHolder(
272 GetSettings(), jni_facade, thread_host_, std::move(shell),
273 apk_asset_provider_->Clone(), weak_platform_view));
274}
const flutter::Settings & GetSettings() const
AndroidShellHolder(const flutter::Settings &settings, std::shared_ptr< PlatformViewAndroidJNI > jni_facade)

◆ UpdateDisplayMetrics()

void flutter::AndroidShellHolder::UpdateDisplayMetrics ( )

Definition at line 349 of file android_shell_holder.cc.

349 {
350 std::vector<std::unique_ptr<Display>> displays;
351 displays.push_back(std::make_unique<AndroidDisplay>(jni_facade_));
352 shell_->OnDisplayUpdates(std::move(displays));
353}

The documentation for this class was generated from the following files: