Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
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, AndroidRenderingAPI android_rendering_api)
 
 ~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, int64_t engine_id) const
 This is a factory for a derived AndroidShellHolder from an existing AndroidShellHolder.
 
void Launch (std::unique_ptr< APKAssetProvider > apk_asset_provider, const std::string &entrypoint, const std::string &libraryUrl, const std::vector< std::string > &entrypoint_args, int64_t engine_id)
 
const flutter::SettingsGetSettings () const
 
fml::WeakPtr< PlatformViewAndroidGetPlatformView ()
 
bool IsSurfaceControlEnabled ()
 
Rasterizer::Screenshot Screenshot (Rasterizer::ScreenshotType type, bool base64_encode)
 
void NotifyLowMemoryWarning ()
 
const std::shared_ptr< PlatformMessageHandler > & GetPlatformMessageHandler () const
 
void UpdateDisplayMetrics ()
 
const std::unique_ptr< Shell > & GetShellForTesting () const
 

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 38 of file android_shell_holder.h.

Constructor & Destructor Documentation

◆ AndroidShellHolder()

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

Definition at line 82 of file android_shell_holder.cc.

86 : settings_(settings),
87 jni_facade_(jni_facade),
88 android_rendering_api_(android_rendering_api) {
89 static size_t thread_host_count = 1;
90 auto thread_label = std::to_string(thread_host_count++);
91
93 if (settings.merged_platform_ui_thread !=
96 }
97
99 thread_label, mask, AndroidPlatformThreadConfigSetter);
100 host_config.ui_config = fml::Thread::ThreadConfig(
102 flutter::ThreadHost::Type::kUi, thread_label),
104 host_config.raster_config = fml::Thread::ThreadConfig(
108 host_config.io_config = fml::Thread::ThreadConfig(
110 flutter::ThreadHost::Type::kIo, thread_label),
112
113 thread_host_ = std::make_shared<ThreadHost>(host_config);
114
115 fml::WeakPtr<PlatformViewAndroid> weak_platform_view;
116 AndroidRenderingAPI rendering_api = android_rendering_api_;
117 Shell::CreateCallback<PlatformView> on_create_platform_view =
118 [&jni_facade, &weak_platform_view, rendering_api](Shell& shell) {
119 std::unique_ptr<PlatformViewAndroid> platform_view_android;
120 platform_view_android = std::make_unique<PlatformViewAndroid>(
121 shell, // delegate
122 shell.GetTaskRunners(), // task runners
123 jni_facade, // JNI interop
124 rendering_api // rendering API
125 );
126 weak_platform_view = platform_view_android->GetWeakPtr();
127 return platform_view_android;
128 };
129
130 Shell::CreateCallback<Rasterizer> on_create_rasterizer = [](Shell& shell) {
131 return std::make_unique<Rasterizer>(shell);
132 };
133
134 // The current thread will be used as the platform thread. Ensure that the
135 // message loop is initialized.
137 fml::RefPtr<fml::TaskRunner> raster_runner;
140 fml::RefPtr<fml::TaskRunner> platform_runner =
142 raster_runner = thread_host_->raster_thread->GetTaskRunner();
143 if (settings.merged_platform_ui_thread ==
145 ui_runner = platform_runner;
146 } else {
147 ui_runner = thread_host_->ui_thread->GetTaskRunner();
148 }
149 io_runner = thread_host_->io_thread->GetTaskRunner();
150
151 flutter::TaskRunners task_runners(thread_label, // label
152 platform_runner, // platform
153 raster_runner, // raster
154 ui_runner, // ui
155 io_runner // io
156 );
157
158 shell_ =
159 Shell::Create(GetDefaultPlatformData(), // window data
160 task_runners, // task runners
161 settings_, // settings
162 on_create_platform_view, // platform view create callback
163 on_create_rasterizer // rasterizer create callback
164 );
165
166 if (shell_) {
167 shell_->GetDartVM()->GetConcurrentMessageLoop()->PostTaskToAllWorkers([]() {
168 if (::setpriority(PRIO_PROCESS, gettid(), 1) != 0) {
169 FML_LOG(ERROR) << "Failed to set Workers task runner priority";
170 }
171 });
172
173 shell_->RegisterImageDecoder(
174 [runner = task_runners.GetIOTaskRunner()](sk_sp<SkData> buffer) {
175 return AndroidImageGenerator::MakeFromData(std::move(buffer), runner);
176 },
177 -1);
178 FML_DLOG(INFO) << "Registered Android SDK image decoder (API level 28+)";
179 }
180
181 platform_view_ = weak_platform_view;
182 FML_DCHECK(platform_view_);
183 is_valid_ = shell_ != nullptr;
184}
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:221
static void EnsureInitializedForCurrentThread()
fml::RefPtr< fml::TaskRunner > GetTaskRunner() const
static FML_EMBEDDER_ONLY MessageLoop & GetCurrent()
@ kNormal
Default priority level.
@ kRaster
Suitable for thread which raster data.
@ kDisplay
Suitable for threads which generate data for the display.
#define FML_DLOG(severity)
Definition logging.h:121
#define FML_LOG(severity)
Definition logging.h:101
#define FML_DCHECK(condition)
Definition logging.h:122
static PlatformData GetDefaultPlatformData()
static void AndroidPlatformThreadConfigSetter(const fml::Thread::ThreadConfig &config)
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 disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set profile Make the profiler discard new samples once the profiler sample buffer is full When this flag is not the profiler sample buffer is used as a ring buffer
Definition switch_defs.h:98
MergedPlatformUIThread merged_platform_ui_thread
Definition settings.h:379
static std::string MakeThreadName(Type type, const std::string &prefix)
Use the prefix and thread type to generator a thread name.
The ThreadConfig is the thread info include thread name, thread priority.
Definition thread.h:35

References flutter::AndroidPlatformThreadConfigSetter(), flutter::buffer, flutter::Shell::Create(), fml::MessageLoop::EnsureInitializedForCurrentThread(), FML_DCHECK, FML_DLOG, FML_LOG, fml::MessageLoop::GetCurrent(), flutter::GetDefaultPlatformData(), flutter::TaskRunners::GetIOTaskRunner(), fml::MessageLoop::GetTaskRunner(), flutter::ThreadHost::ThreadHostConfig::io_config, fml::Thread::kDisplay, flutter::Settings::kEnabled, flutter::ThreadHost::kIo, fml::Thread::kNormal, fml::Thread::kRaster, flutter::ThreadHost::kRaster, flutter::ThreadHost::kUi, flutter::AndroidImageGenerator::MakeFromData(), flutter::ThreadHost::ThreadHostConfig::MakeThreadName(), flutter::Settings::merged_platform_ui_thread, flutter::ThreadHost::ThreadHostConfig::raster_config, and flutter::ThreadHost::ThreadHostConfig::ui_config.

◆ ~AndroidShellHolder()

flutter::AndroidShellHolder::~AndroidShellHolder ( )

Definition at line 209 of file android_shell_holder.cc.

209 {
210 shell_.reset();
211 thread_host_.reset();
212}

Member Function Documentation

◆ GetPlatformMessageHandler()

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

Definition at line 102 of file android_shell_holder.h.

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

◆ GetPlatformView()

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

Definition at line 316 of file android_shell_holder.cc.

316 {
317 FML_DCHECK(platform_view_);
318 return platform_view_;
319}

References FML_DCHECK.

Referenced by IsSurfaceControlEnabled().

◆ GetSettings()

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

Definition at line 218 of file android_shell_holder.cc.

218 {
219 return settings_;
220}

Referenced by Spawn().

◆ GetShellForTesting()

const std::unique_ptr< Shell > & flutter::AndroidShellHolder::GetShellForTesting ( ) const
inline

Definition at line 110 of file android_shell_holder.h.

110{ return shell_; }

◆ IsSurfaceControlEnabled()

bool flutter::AndroidShellHolder::IsSurfaceControlEnabled ( )

Definition at line 367 of file android_shell_holder.cc.

367 {
368 return GetPlatformView()->IsSurfaceControlEnabled();
369}
fml::WeakPtr< PlatformViewAndroid > GetPlatformView()

References GetPlatformView().

◆ IsValid()

bool flutter::AndroidShellHolder::IsValid ( ) const

Definition at line 214 of file android_shell_holder.cc.

214 {
215 return is_valid_;
216}

Referenced by Launch(), and Screenshot().

◆ 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,
int64_t  engine_id 
)

Definition at line 287 of file android_shell_holder.cc.

292 {
293 if (!IsValid()) {
294 return;
295 }
296
297 apk_asset_provider_ = std::move(apk_asset_provider);
298 auto config = BuildRunConfiguration(entrypoint, libraryUrl, entrypoint_args);
299 if (!config) {
300 return;
301 }
302 config->SetEngineId(engine_id);
304 shell_->RunEngine(std::move(config.value()));
305}

References IsValid(), and UpdateDisplayMetrics().

◆ NotifyLowMemoryWarning()

void flutter::AndroidShellHolder::NotifyLowMemoryWarning ( )

Definition at line 321 of file android_shell_holder.cc.

321 {
322 FML_DCHECK(shell_);
323 shell_->NotifyLowMemoryWarning();
324}

References FML_DCHECK.

◆ Screenshot()

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

Definition at line 307 of file android_shell_holder.cc.

309 {
310 if (!IsValid()) {
311 return {nullptr, DlISize(), "", Rasterizer::ScreenshotFormat::kUnknown};
312 }
313 return shell_->Screenshot(type, base64_encode);
314}
GLenum type
impeller::ISize32 DlISize

References IsValid(), flutter::Rasterizer::kUnknown, and type.

◆ 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,
int64_t  engine_id 
) 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 222 of file android_shell_holder.cc.

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

References FML_DCHECK, flutter::PlatformViewAndroid::GetAndroidContext(), and GetSettings().

◆ UpdateDisplayMetrics()

void flutter::AndroidShellHolder::UpdateDisplayMetrics ( )

Definition at line 361 of file android_shell_holder.cc.

361 {
362 std::vector<std::unique_ptr<Display>> displays;
363 displays.push_back(std::make_unique<AndroidDisplay>(jni_facade_));
364 shell_->OnDisplayUpdates(std::move(displays));
365}
std::vector< FlutterEngineDisplay > * displays

References displays.

Referenced by Launch().


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