7#include <android/api-level.h>
8#include <sys/system_properties.h>
37#if IMPELLER_ENABLE_VULKAN
53static constexpr int kMinAPILevelHCPP = 34;
56AndroidContext::ContextSettings CreateContextSettings(
57 const Settings& p_settings) {
58 AndroidContext::ContextSettings settings;
59 settings.enable_gpu_tracing = p_settings.enable_vulkan_gpu_tracing;
60 settings.enable_validation = p_settings.enable_vulkan_validation;
61 settings.enable_surface_control = p_settings.enable_surface_control;
62 settings.impeller_flags.lazy_shader_mode =
63 p_settings.impeller_enable_lazy_shader_mode;
64 settings.impeller_flags.antialiased_lines =
65 p_settings.impeller_antialiased_lines;
71 const std::shared_ptr<AndroidContext>& context,
73 bool lazy_shader_mode)
74 : android_context_(context),
75 enable_impeller_(enable_impeller),
76 lazy_shader_mode_(lazy_shader_mode) {}
81 if (android_context_->IsDynamicSelection()) {
82 auto cast_ptr = std::static_pointer_cast<AndroidContextDynamicImpeller>(
84 return std::make_unique<AndroidSurfaceDynamicImpeller>(cast_ptr);
86 switch (android_context_->RenderingApi()) {
89 return std::make_unique<AndroidSurfaceSoftware>();
91 return std::make_unique<AndroidSurfaceGLSkia>(
92 std::static_pointer_cast<AndroidContextGLSkia>(android_context_));
95 return std::make_unique<AndroidSurfaceGLImpeller>(
96 std::static_pointer_cast<AndroidContextGLImpeller>(android_context_));
98 return std::make_unique<AndroidSurfaceVKImpeller>(
99 std::static_pointer_cast<AndroidContextVKImpeller>(android_context_));
101 auto cast_ptr = std::static_pointer_cast<AndroidContextDynamicImpeller>(
103 return std::make_unique<AndroidSurfaceDynamicImpeller>(cast_ptr);
112 bool enable_opengl_gpu_tracing,
114 switch (android_rendering_api) {
119 return std::make_unique<AndroidContextGLSkia>(
120 fml::MakeRefCounted<AndroidEnvironmentGL>(),
125 return std::make_unique<AndroidContextVKImpeller>(settings);
127 return std::make_unique<AndroidContextGLImpeller>(
128 std::make_unique<impeller::egl::Display>(),
129 enable_opengl_gpu_tracing);
132 return std::make_unique<AndroidContextDynamicImpeller>(settings);
140 const std::shared_ptr<PlatformViewAndroidJNI>& jni_facade,
149 delegate.OnPlatformViewGetSettings().enable_opengl_gpu_tracing,
150 CreateContextSettings(delegate.OnPlatformViewGetSettings()))) {}
155 const std::shared_ptr<PlatformViewAndroidJNI>& jni_facade,
156 const std::shared_ptr<flutter::AndroidContext>& android_context)
158 jni_facade_(jni_facade),
159 android_context_(android_context),
160 platform_view_android_delegate_(jni_facade),
162 if (android_context_) {
164 <<
"Could not create surface from invalid Android context.";
165 surface_factory_ = std::make_shared<AndroidSurfaceFactoryImpl>(
171 android_surface_ = surface_factory_->CreateSurface();
172 android_meets_hcpp_criteria_ =
174 android_get_device_api_level() >= kMinAPILevelHCPP &&
176 FML_CHECK(android_surface_ && android_surface_->IsValid())
177 <<
"Could not create an OpenGL, Vulkan or Software surface to set "
187 if (android_surface_) {
188 InstallFirstFrameCallback();
193 [&latch,
surface = android_surface_.get(),
194 native_window = std::move(native_window), jni_facade = jni_facade_]() {
195 surface->SetNativeWindow(native_window, jni_facade);
206 if (android_surface_) {
210 [&latch,
surface = android_surface_.get(),
211 native_window = std::move(native_window), jni_facade = jni_facade_]() {
212 surface->TeardownOnScreenContext();
213 surface->SetNativeWindow(native_window, jni_facade);
225 if (android_surface_) {
229 [&latch,
surface = android_surface_.get()]() {
230 surface->TeardownOnScreenContext();
238 if (!android_surface_) {
244 [&latch,
surface = android_surface_.get(),
size]() {
245 surface->OnScreenSurfaceResize(size);
253 jobject java_message_data,
254 jint java_message_position,
256 uint8_t* message_data =
257 static_cast<uint8_t*
>(env->GetDirectBufferAddress(java_message_data));
263 response = fml::MakeRefCounted<PlatformMessageResponseAndroid>(
268 std::make_unique<flutter::PlatformMessage>(
269 std::move(
name), std::move(
message), std::move(response)));
277 response = fml::MakeRefCounted<PlatformMessageResponseAndroid>(
282 std::make_unique<flutter::PlatformMessage>(std::move(
name),
283 std::move(response)));
287void PlatformViewAndroid::HandlePlatformMessage(
288 std::unique_ptr<flutter::PlatformMessage>
message) {
290 platform_message_handler_->HandlePlatformMessage(std::move(
message));
294void PlatformViewAndroid::OnPreEngineRestart()
const {
295 jni_facade_->FlutterViewOnPreEngineRestart();
302 jint args_position) {
305 if (env->IsSameObject(
args, NULL)) {
312 uint8_t* args_data =
static_cast<uint8_t*
>(env->GetDirectBufferAddress(
args));
317 std::move(args_vector));
321void PlatformViewAndroid::UpdateSemantics(
329void PlatformViewAndroid::SetApplicationLocale(std::string locale) {
330 jni_facade_->FlutterViewSetApplicationLocale(std::move(locale));
336 switch (android_context_->RenderingApi()) {
339 RegisterTexture(std::make_shared<SurfaceTextureExternalTextureGLImpeller>(
340 std::static_pointer_cast<impeller::ContextGLES>(
341 android_context_->GetImpellerContext()),
357 FML_LOG(INFO) <<
"Software rendering does not support external textures.";
362 <<
"Flutter recommends migrating plugins that create and "
363 "register surface textures to the new surface producer "
364 "API. See https://docs.flutter.dev/release/breaking-changes/"
365 "android-surface-plugins";
366 RegisterTexture(std::make_shared<SurfaceTextureExternalTextureVKImpeller>(
367 std::static_pointer_cast<impeller::ContextVK>(
368 android_context_->GetImpellerContext()),
385 switch (android_context_->RenderingApi()) {
390 std::static_pointer_cast<AndroidContextGLSkia>(android_context_),
391 texture_id, image_texture_entry, jni_facade_, lifecycle));
394 FML_LOG(INFO) <<
"Software rendering does not support external textures.";
400 std::static_pointer_cast<impeller::ContextGLES>(
401 android_context_->GetImpellerContext()),
402 texture_id, image_texture_entry, jni_facade_, lifecycle));
406 std::static_pointer_cast<impeller::ContextVK>(
407 android_context_->GetImpellerContext()),
408 texture_id, image_texture_entry, jni_facade_, lifecycle));
417std::unique_ptr<VsyncWaiter> PlatformViewAndroid::CreateVSyncWaiter() {
422std::unique_ptr<Surface> PlatformViewAndroid::CreateRenderingSurface() {
423 if (!android_surface_) {
426 return android_surface_->CreateGPUSurface(
427 android_context_->GetMainSkiaContext().get());
431std::shared_ptr<ExternalViewEmbedder>
432PlatformViewAndroid::CreateExternalViewEmbedder() {
433 return std::make_shared<AndroidExternalViewEmbedderWrapper>(
434 android_meets_hcpp_criteria_, *android_context_, jni_facade_,
439std::unique_ptr<SnapshotSurfaceProducer>
440PlatformViewAndroid::CreateSnapshotSurfaceProducer() {
441 if (!android_surface_) {
444 return std::make_unique<AndroidSnapshotSurfaceProducer>(*android_surface_);
448sk_sp<GrDirectContext> PlatformViewAndroid::CreateResourceContext()
const {
449 if (!android_surface_) {
453 sk_sp<GrDirectContext> resource_context;
454 if (android_surface_->ResourceContextMakeCurrent()) {
459 GrBackendApi::kOpenGL,
462 FML_DLOG(ERROR) <<
"Could not make the resource context current.";
464 return resource_context;
466 android_surface_->ResourceContextMakeCurrent();
472void PlatformViewAndroid::ReleaseResourceContext()
const {
473 if (android_surface_) {
474 android_surface_->ResourceContextClearCurrent();
479std::shared_ptr<impeller::Context> PlatformViewAndroid::GetImpellerContext()
481 if (android_surface_) {
482 return android_surface_->GetImpellerContext();
484 return android_context_->GetImpellerContext();
488std::unique_ptr<std::vector<std::string>>
489PlatformViewAndroid::ComputePlatformResolvedLocales(
490 const std::vector<std::string>& supported_locale_data) {
491 return jni_facade_->FlutterViewComputePlatformResolvedLocale(
492 supported_locale_data);
496void PlatformViewAndroid::RequestDartDeferredLibrary(intptr_t loading_unit_id) {
497 if (jni_facade_->RequestDartDeferredLibrary(loading_unit_id)) {
505 intptr_t loading_unit_id,
506 std::unique_ptr<const fml::Mapping> snapshot_data,
507 std::unique_ptr<const fml::Mapping> snapshot_instructions) {
509 std::move(snapshot_instructions));
514 intptr_t loading_unit_id,
515 const std::string error_message,
523 std::unique_ptr<AssetResolver> updated_asset_resolver,
528void PlatformViewAndroid::InstallFirstFrameCallback() {
534 platform_task_runner->PostTask([platform_view]() {
537 reinterpret_cast<PlatformViewAndroid*>(platform_view.get())
538 ->FireFirstFrameCallback();
544void PlatformViewAndroid::FireFirstFrameCallback() {
545 jni_facade_->FlutterViewOnFirstFrame();
548double PlatformViewAndroid::GetScaledFontSize(
double unscaled_font_size,
549 int configuration_id)
const {
550 return jni_facade_->FlutterViewGetScaledFontSize(unscaled_font_size,
554bool PlatformViewAndroid::IsSurfaceControlEnabled()
const {
556 return android_meets_hcpp_criteria_ &&
557 android_context_->RenderingApi() ==
558 AndroidRenderingAPI::kImpellerVulkan &&
563void PlatformViewAndroid::SetupImpellerContext() {
564 android_context_->SetupImpellerContext();
565 android_surface_->SetupImpellerSurface();
std::unique_ptr< AndroidSurface > CreateSurface() override
~AndroidSurfaceFactoryImpl() override
AndroidSurfaceFactoryImpl(const std::shared_ptr< AndroidContext > &context, bool enable_impeller, bool lazy_shader_mode)
AssetResolverType
Identifies the type of AssetResolver an instance is.
static sk_sp< const GrGLInterface > GetDefaultPlatformGLInterface()
ImageLifecycle
Whether the last image should be reset when the context is destroyed.
static sk_sp< GrDirectContext > CreateCompatibleResourceLoadingContext(GrBackendApi backend, const sk_sp< const GrGLInterface > &gl_interface)
fml::RefPtr< fml::TaskRunner > GetRasterTaskRunner() const
fml::RefPtr< fml::TaskRunner > GetPlatformTaskRunner() const
A Mapping like NonOwnedMapping, but uses Free as its release proc.
static MallocMapping Copy(const T *begin, const T *end)
static void RunNowOrPostTask(const fml::RefPtr< fml::TaskRunner > &runner, const fml::closure &task)
static ContextVK & Cast(Context &base)
bool GetShouldEnableSurfaceControlSwapchain() const
Whether the Android Surface control based swapchain should be enabled.
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
G_BEGIN_DECLS GBytes * message
G_BEGIN_DECLS FlutterViewId view_id
#define FML_DLOG(severity)
#define FML_LOG(severity)
#define FML_CHECK(condition)
#define FML_UNREACHABLE()
std::unordered_map< int32_t, SemanticsNode > SemanticsNodeUpdates
std::unordered_map< int32_t, CustomAccessibilityAction > CustomAccessibilityActionUpdates
it will be possible to load the file into Perfetto s trace viewer use test Running tests that layout and measure text will not yield consistent results across various platforms Enabling this option will make font resolution default to the Ahem test font on all disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
DEF_SWITCHES_START aot vmservice shared library name
constexpr FlutterViewId kImplicitViewId
static std::shared_ptr< flutter::AndroidContext > CreateAndroidContext(const flutter::TaskRunners &task_runners, AndroidRenderingAPI android_rendering_api, bool enable_opengl_gpu_tracing, const AndroidContext::ContextSettings &settings)
bool impeller_enable_lazy_shader_mode
bool enable_surface_control