5#define FML_USED_ON_EMBEDDER
8#include <sys/resource.h>
43 if (::setpriority(PRIO_PROCESS, 0, 10) != 0) {
44 FML_LOG(ERROR) <<
"Failed to set IO task runner priority";
50 if (::setpriority(PRIO_PROCESS, 0, -1) != 0) {
51 FML_LOG(ERROR) <<
"Failed to set UI task runner priority";
60 if (::setpriority(PRIO_PROCESS, 0, -5) != 0) {
63 if (::setpriority(PRIO_PROCESS, 0, -2) != 0) {
64 FML_LOG(ERROR) <<
"Failed to set raster task runner priority";
71 if (::setpriority(PRIO_PROCESS, 0, 0) != 0) {
72 FML_LOG(ERROR) <<
"Failed to set priority";
84 std::shared_ptr<PlatformViewAndroidJNI> jni_facade,
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++);
113 thread_host_ = std::make_shared<ThreadHost>(host_config);
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>(
122 shell.GetTaskRunners(),
126 weak_platform_view = platform_view_android->GetWeakPtr();
127 return platform_view_android;
131 return std::make_unique<Rasterizer>(shell);
142 raster_runner = thread_host_->raster_thread->GetTaskRunner();
145 ui_runner = platform_runner;
147 ui_runner = thread_host_->ui_thread->GetTaskRunner();
149 io_runner = thread_host_->io_thread->GetTaskRunner();
162 on_create_platform_view,
167 shell_->GetDartVM()->GetConcurrentMessageLoop()->PostTaskToAllWorkers([]() {
168 if (::setpriority(PRIO_PROCESS, gettid(), 1) != 0) {
169 FML_LOG(ERROR) <<
"Failed to set Workers task runner priority";
173 shell_->RegisterImageDecoder(
178 FML_DLOG(INFO) <<
"Registered Android SDK image decoder (API level 28+)";
181 platform_view_ = weak_platform_view;
183 is_valid_ = shell_ !=
nullptr;
188 const std::shared_ptr<PlatformViewAndroidJNI>& jni_facade,
189 const std::shared_ptr<ThreadHost>& thread_host,
190 std::unique_ptr<Shell> shell,
191 std::unique_ptr<APKAssetProvider> apk_asset_provider,
195 jni_facade_(jni_facade),
198 shell_(
std::move(shell)),
199 apk_asset_provider_(
std::move(apk_asset_provider)),
200 android_rendering_api_(rendering_api) {
206 is_valid_ = shell_ !=
nullptr;
211 thread_host_.reset();
223 std::shared_ptr<PlatformViewAndroidJNI> jni_facade,
224 const std::string& entrypoint,
225 const std::string& libraryUrl,
226 const std::string& initial_route,
227 const std::vector<std::string>& entrypoint_args,
228 int64_t engine_id)
const {
230 <<
"A new Shell can only be spawned "
231 "if the current Shell is properly constructed";
247 std::shared_ptr<flutter::AndroidContext> android_context =
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>(
257 shell.GetTaskRunners(),
261 weak_platform_view = platform_view_android->GetWeakPtr();
262 return platform_view_android;
266 return std::make_unique<Rasterizer>(shell);
269 auto config = BuildRunConfiguration(entrypoint, libraryUrl, entrypoint_args);
275 config->SetEngineId(engine_id);
277 std::unique_ptr<flutter::Shell> shell =
278 shell_->Spawn(std::move(config.value()), initial_route,
279 on_create_platform_view, on_create_rasterizer);
282 GetSettings(), jni_facade, thread_host_, std::move(shell),
283 apk_asset_provider_->Clone(), weak_platform_view,
284 android_context->RenderingApi()));
288 std::unique_ptr<APKAssetProvider> apk_asset_provider,
289 const std::string& entrypoint,
290 const std::string& libraryUrl,
291 const std::vector<std::string>& entrypoint_args,
297 apk_asset_provider_ = std::move(apk_asset_provider);
298 auto config = BuildRunConfiguration(entrypoint, libraryUrl, entrypoint_args);
302 config->SetEngineId(engine_id);
304 shell_->RunEngine(std::move(config.value()));
309 bool base64_encode) {
313 return shell_->Screenshot(
type, base64_encode);
318 return platform_view_;
323 shell_->NotifyLowMemoryWarning();
326std::optional<RunConfiguration> AndroidShellHolder::BuildRunConfiguration(
327 const std::string& entrypoint,
328 const std::string& libraryUrl,
329 const std::vector<std::string>& entrypoint_args)
const {
330 std::unique_ptr<IsolateConfiguration> isolate_configuration;
334 std::unique_ptr<fml::Mapping> kernel_blob =
338 FML_DLOG(ERROR) <<
"Unable to load the kernel blob asset.";
341 isolate_configuration =
345 RunConfiguration config(std::move(isolate_configuration));
346 config.AddAssetResolver(apk_asset_provider_->Clone());
349 if (!entrypoint.empty() && !libraryUrl.empty()) {
350 config.SetEntrypointAndLibrary(entrypoint, libraryUrl);
351 }
else if (!entrypoint.empty()) {
352 config.SetEntrypoint(entrypoint);
354 if (!entrypoint_args.empty()) {
355 config.SetEntrypointArgs(entrypoint_args);
362 std::vector<std::unique_ptr<Display>>
displays;
363 displays.push_back(std::make_unique<AndroidDisplay>(jni_facade_));
364 shell_->OnDisplayUpdates(std::move(
displays));
static std::shared_ptr< ImageGenerator > MakeFromData(sk_sp< SkData > data, const fml::RefPtr< fml::TaskRunner > &task_runner)
This is the Android owner of the core engine Shell.
const flutter::Settings & GetSettings() const
void NotifyLowMemoryWarning()
void UpdateDisplayMetrics()
AndroidShellHolder(const flutter::Settings &settings, std::shared_ptr< PlatformViewAndroidJNI > jni_facade, AndroidRenderingAPI android_rendering_api)
std::unique_ptr< 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.
fml::WeakPtr< PlatformViewAndroid > GetPlatformView()
bool IsSurfaceControlEnabled()
Rasterizer::Screenshot Screenshot(Rasterizer::ScreenshotType type, bool base64_encode)
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)
static bool IsRunningPrecompiledCode()
Checks if VM instances in the process can run precompiled code. This call can be made at any time and...
static std::unique_ptr< IsolateConfiguration > CreateForAppSnapshot()
Creates an AOT isolate configuration using snapshot symbols present in the currently loaded process....
static std::unique_ptr< IsolateConfiguration > CreateForKernel(std::unique_ptr< const fml::Mapping > kernel)
Creates a JIT isolate configuration using the specified snapshot. This is a convenience method for th...
ScreenshotType
The type of the screenshot to obtain of the previously rendered layer tree.
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...
std::function< std::unique_ptr< T >(Shell &)> CreateCallback
fml::RefPtr< fml::TaskRunner > GetIOTaskRunner() const
static std::unique_ptr< FileMapping > CreateReadOnly(const std::string &path)
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.
@ kBackground
Suitable for threads that shouldn't disrupt high priority work.
@ kDisplay
Suitable for threads which generate data for the display.
static void SetCurrentThreadName(const ThreadConfig &config)
#define FML_DLOG(severity)
#define FML_LOG(severity)
#define FML_DCHECK(condition)
impeller::ISize32 DlISize
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
@ kNotEfficiency
Request affinity for all non-efficiency cores.
@ kEfficiency
Request CPU affinity for the efficiency cores.
@ kNotPerformance
Request affinity for all non-performance cores.
bool RequestAffinity(CpuAffinity affinity)
Request the given affinity for the current thread.
A POD type used to return the screenshot data along with the size of the frame.
MergedPlatformUIThread merged_platform_ui_thread
std::optional< ThreadConfig > io_config
std::optional< ThreadConfig > ui_config
std::optional< ThreadConfig > raster_config
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.