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;
67 const std::shared_ptr<AndroidContext>&
context,
69 bool lazy_shader_mode)
71 enable_impeller_(enable_impeller),
72 lazy_shader_mode_(lazy_shader_mode) {}
77 if (android_context_->IsDynamicSelection()) {
78 auto cast_ptr = std::static_pointer_cast<AndroidContextDynamicImpeller>(
80 return std::make_unique<AndroidSurfaceDynamicImpeller>(cast_ptr);
82 switch (android_context_->RenderingApi()) {
85 return std::make_unique<AndroidSurfaceSoftware>();
87 return std::make_unique<AndroidSurfaceGLSkia>(
88 std::static_pointer_cast<AndroidContextGLSkia>(android_context_));
91 return std::make_unique<AndroidSurfaceGLImpeller>(
92 std::static_pointer_cast<AndroidContextGLImpeller>(android_context_));
94 return std::make_unique<AndroidSurfaceVKImpeller>(
95 std::static_pointer_cast<AndroidContextVKImpeller>(android_context_));
97 auto cast_ptr = std::static_pointer_cast<AndroidContextDynamicImpeller>(
99 return std::make_unique<AndroidSurfaceDynamicImpeller>(cast_ptr);
108 bool enable_opengl_gpu_tracing,
110 std::shared_ptr<fml::BasicTaskRunner> io_task_runner) {
111 switch (android_rendering_api) {
116 return std::make_unique<AndroidContextGLSkia>(
117 fml::MakeRefCounted<AndroidEnvironmentGL>(),
122 return std::make_unique<AndroidContextVKImpeller>(settings);
124 return std::make_unique<AndroidContextGLImpeller>(
125 std::make_unique<impeller::egl::Display>(), enable_opengl_gpu_tracing,
126 std::move(io_task_runner));
129 return std::make_unique<AndroidContextDynamicImpeller>(
130 settings, std::move(io_task_runner));
138 const std::shared_ptr<PlatformViewAndroidJNI>& jni_facade,
147 delegate.OnPlatformViewGetSettings().enable_opengl_gpu_tracing,
148 CreateContextSettings(delegate.OnPlatformViewGetSettings()),
149 delegate.OnPlatformViewGetShutdownSafeIOTaskRunner())) {}
154 const std::shared_ptr<PlatformViewAndroidJNI>& jni_facade,
155 const std::shared_ptr<flutter::AndroidContext>& android_context)
157 jni_facade_(jni_facade),
158 android_context_(android_context),
159 platform_view_android_delegate_(jni_facade),
161 if (android_context_) {
163 <<
"Could not create surface from invalid Android context.";
164 surface_factory_ = std::make_shared<AndroidSurfaceFactoryImpl>(
170 android_surface_ = surface_factory_->CreateSurface();
171 android_meets_hcpp_criteria_ =
173 android_get_device_api_level() >= kMinAPILevelHCPP &&
175 FML_CHECK(android_surface_ && android_surface_->IsValid())
176 <<
"Could not create an OpenGL, Vulkan or Software surface to set "
186 if (android_surface_) {
187 InstallFirstFrameCallback();
192 [&latch,
surface = android_surface_.get(),
193 native_window = std::move(native_window), jni_facade = jni_facade_]() {
194 surface->SetNativeWindow(native_window, jni_facade);
205 if (android_surface_) {
209 [&latch,
surface = android_surface_.get(),
210 native_window = std::move(native_window), jni_facade = jni_facade_]() {
211 surface->TeardownOnScreenContext();
212 surface->SetNativeWindow(native_window, jni_facade);
224 if (android_surface_) {
228 [&latch,
surface = android_surface_.get()]() {
229 surface->TeardownOnScreenContext();
237 if (!android_surface_) {
243 [&latch,
surface = android_surface_.get(),
size]() {
244 surface->OnScreenSurfaceResize(size);
252 jobject java_message_data,
253 jint java_message_position,
255 uint8_t* message_data =
256 static_cast<uint8_t*
>(env->GetDirectBufferAddress(java_message_data));
262 response = fml::MakeRefCounted<PlatformMessageResponseAndroid>(
267 std::make_unique<flutter::PlatformMessage>(
268 std::move(
name), std::move(
message), std::move(response)));
276 response = fml::MakeRefCounted<PlatformMessageResponseAndroid>(
281 std::make_unique<flutter::PlatformMessage>(std::move(
name),
282 std::move(response)));
286void PlatformViewAndroid::HandlePlatformMessage(
287 std::unique_ptr<flutter::PlatformMessage>
message) {
289 platform_message_handler_->HandlePlatformMessage(std::move(
message));
293void PlatformViewAndroid::OnPreEngineRestart()
const {
294 jni_facade_->FlutterViewOnPreEngineRestart();
301 jint args_position) {
304 if (env->IsSameObject(
args, NULL)) {
311 uint8_t* args_data =
static_cast<uint8_t*
>(env->GetDirectBufferAddress(
args));
316 std::move(args_vector));
320void PlatformViewAndroid::UpdateSemantics(
328void PlatformViewAndroid::SetApplicationLocale(std::string locale) {
329 jni_facade_->FlutterViewSetApplicationLocale(std::move(locale));
333void PlatformViewAndroid::SetSemanticsTreeEnabled(
bool enabled) {
334 jni_facade_->FlutterViewSetSemanticsTreeEnabled(enabled);
340 switch (android_context_->RenderingApi()) {
343 RegisterTexture(std::make_shared<SurfaceTextureExternalTextureGLImpeller>(
344 std::static_pointer_cast<impeller::ContextGLES>(
345 android_context_->GetImpellerContext()),
361 FML_LOG(INFO) <<
"Software rendering does not support external textures.";
366 <<
"Flutter recommends migrating plugins that create and "
367 "register surface textures to the new surface producer "
368 "API. See https://docs.flutter.dev/release/breaking-changes/"
369 "android-surface-plugins";
370 RegisterTexture(std::make_shared<SurfaceTextureExternalTextureVKImpeller>(
371 std::static_pointer_cast<impeller::ContextVK>(
372 android_context_->GetImpellerContext()),
389 switch (android_context_->RenderingApi()) {
394 std::static_pointer_cast<AndroidContextGLSkia>(android_context_),
395 texture_id, image_texture_entry, jni_facade_, lifecycle));
398 FML_LOG(INFO) <<
"Software rendering does not support external textures.";
404 std::static_pointer_cast<impeller::ContextGLES>(
405 android_context_->GetImpellerContext()),
406 texture_id, image_texture_entry, jni_facade_, lifecycle));
410 std::static_pointer_cast<impeller::ContextVK>(
411 android_context_->GetImpellerContext()),
412 texture_id, image_texture_entry, jni_facade_, lifecycle));
421std::unique_ptr<VsyncWaiter> PlatformViewAndroid::CreateVSyncWaiter() {
426std::unique_ptr<Surface> PlatformViewAndroid::CreateRenderingSurface() {
427 if (!android_surface_) {
430 return android_surface_->CreateGPUSurface(
431 android_context_->GetMainSkiaContext().get());
435std::shared_ptr<ExternalViewEmbedder>
436PlatformViewAndroid::CreateExternalViewEmbedder() {
437 return std::make_shared<AndroidExternalViewEmbedderWrapper>(
438 android_meets_hcpp_criteria_, *android_context_, jni_facade_,
443std::unique_ptr<SnapshotSurfaceProducer>
444PlatformViewAndroid::CreateSnapshotSurfaceProducer() {
445 if (!android_surface_) {
448 return std::make_unique<AndroidSnapshotSurfaceProducer>(*android_surface_);
452sk_sp<GrDirectContext> PlatformViewAndroid::CreateResourceContext()
const {
453 if (!android_surface_) {
457 sk_sp<GrDirectContext> resource_context;
458 if (android_surface_->ResourceContextMakeCurrent()) {
463 GrBackendApi::kOpenGL,
466 FML_DLOG(ERROR) <<
"Could not make the resource context current.";
468 return resource_context;
470 android_surface_->ResourceContextMakeCurrent();
476void PlatformViewAndroid::ReleaseResourceContext()
const {
477 if (android_surface_) {
478 android_surface_->ResourceContextClearCurrent();
483std::shared_ptr<impeller::Context> PlatformViewAndroid::GetImpellerContext()
485 if (android_surface_) {
486 return android_surface_->GetImpellerContext();
488 return android_context_->GetImpellerContext();
492std::unique_ptr<std::vector<std::string>>
493PlatformViewAndroid::ComputePlatformResolvedLocales(
494 const std::vector<std::string>& supported_locale_data) {
495 return jni_facade_->FlutterViewComputePlatformResolvedLocale(
496 supported_locale_data);
500void PlatformViewAndroid::RequestDartDeferredLibrary(intptr_t loading_unit_id) {
501 if (jni_facade_->RequestDartDeferredLibrary(loading_unit_id)) {
509 intptr_t loading_unit_id,
510 std::unique_ptr<const fml::Mapping> snapshot_data,
511 std::unique_ptr<const fml::Mapping> snapshot_instructions) {
513 std::move(snapshot_instructions));
518 intptr_t loading_unit_id,
519 const std::string error_message,
527 std::unique_ptr<AssetResolver> updated_asset_resolver,
532void PlatformViewAndroid::InstallFirstFrameCallback() {
538 platform_task_runner->PostTask([platform_view]() {
541 reinterpret_cast<PlatformViewAndroid*>(platform_view.get())
542 ->FireFirstFrameCallback();
548void PlatformViewAndroid::FireFirstFrameCallback() {
549 jni_facade_->FlutterViewOnFirstFrame();
552double PlatformViewAndroid::GetScaledFontSize(
double unscaled_font_size,
553 int configuration_id)
const {
554 return jni_facade_->FlutterViewGetScaledFontSize(unscaled_font_size,
558bool PlatformViewAndroid::IsSurfaceControlEnabled()
const {
560 return android_meets_hcpp_criteria_ &&
561 android_context_->RenderingApi() ==
562 AndroidRenderingAPI::kImpellerVulkan &&
567void PlatformViewAndroid::SetupImpellerContext() {
568 android_context_->SetupImpellerContext();
569 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 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, std::shared_ptr< fml::BasicTaskRunner > io_task_runner)
std::shared_ptr< ContextGLES > context
impeller::ShaderType type
bool impeller_enable_lazy_shader_mode
bool enable_surface_control