5#include "flutter/shell/platform/android/platform_view_android.h"
7#include <android/api-level.h>
11#include "flutter/common/graphics/texture.h"
12#include "flutter/fml/synchronization/waitable_event.h"
13#include "flutter/shell/common/shell_io_manager.h"
14#include "flutter/shell/gpu/gpu_surface_gl_delegate.h"
15#include "flutter/shell/platform/android/android_context_gl_impeller.h"
16#include "flutter/shell/platform/android/android_context_gl_skia.h"
17#include "flutter/shell/platform/android/android_context_vulkan_impeller.h"
18#include "flutter/shell/platform/android/android_surface_gl_impeller.h"
19#include "flutter/shell/platform/android/android_surface_gl_skia.h"
20#include "flutter/shell/platform/android/android_surface_software.h"
21#include "flutter/shell/platform/android/image_external_texture_gl.h"
22#include "flutter/shell/platform/android/surface_texture_external_texture_gl.h"
24#if IMPELLER_ENABLE_VULKAN
25#include "flutter/shell/platform/android/android_surface_vulkan_impeller.h"
26#include "flutter/shell/platform/android/image_external_texture_vk.h"
28#include "flutter/shell/platform/android/context/android_context.h"
29#include "flutter/shell/platform/android/external_view_embedder/external_view_embedder.h"
30#include "flutter/shell/platform/android/jni/platform_view_android_jni.h"
31#include "flutter/shell/platform/android/platform_message_response_android.h"
32#include "flutter/shell/platform/android/surface/android_surface.h"
33#include "flutter/shell/platform/android/surface/snapshot_surface_producer.h"
34#include "flutter/shell/platform/android/vsync_waiter_android.h"
39 const std::shared_ptr<AndroidContext>& context,
41 : android_context_(context), enable_impeller_(enable_impeller) {}
46 switch (android_context_->RenderingApi()) {
48 return std::make_unique<AndroidSurfaceSoftware>();
50 return std::make_unique<AndroidSurfaceGLImpeller>(
51 std::static_pointer_cast<AndroidContextGLImpeller>(android_context_));
53 return std::make_unique<AndroidSurfaceGLSkia>(
54 std::static_pointer_cast<AndroidContextGLSkia>(android_context_));
56 return std::make_unique<AndroidSurfaceVulkanImpeller>(
57 std::static_pointer_cast<AndroidContextVulkanImpeller>(
64 bool use_software_rendering,
67 bool enable_vulkan_validation,
68 bool enable_opengl_gpu_tracing,
69 bool enable_vulkan_gpu_tracing) {
70 switch (android_rendering_api) {
74 return std::make_unique<AndroidContextGLImpeller>(
75 std::make_unique<impeller::egl::Display>(),
76 enable_opengl_gpu_tracing);
78 return std::make_unique<AndroidContextVulkanImpeller>(
79 enable_vulkan_validation, enable_vulkan_gpu_tracing);
81 return std::make_unique<AndroidContextGLSkia>(
82 fml::MakeRefCounted<AndroidEnvironmentGL>(),
92 const std::shared_ptr<PlatformViewAndroidJNI>& jni_facade,
93 bool use_software_rendering)
99 use_software_rendering,
101 delegate.OnPlatformViewGetSettings().android_rendering_api,
102 delegate.OnPlatformViewGetSettings().enable_vulkan_validation,
103 delegate.OnPlatformViewGetSettings().enable_opengl_gpu_tracing,
104 delegate.OnPlatformViewGetSettings().enable_vulkan_gpu_tracing)) {
110 const std::shared_ptr<PlatformViewAndroidJNI>& jni_facade,
111 const std::shared_ptr<flutter::AndroidContext>& android_context)
113 jni_facade_(jni_facade),
114 android_context_(android_context),
115 platform_view_android_delegate_(jni_facade),
117 if (android_context_) {
119 <<
"Could not create surface from invalid Android context.";
120 surface_factory_ = std::make_shared<AndroidSurfaceFactoryImpl>(
124 android_surface_ = surface_factory_->CreateSurface();
125 FML_CHECK(android_surface_ && android_surface_->IsValid())
126 <<
"Could not create an OpenGL, Vulkan or Software surface to set up "
135 if (android_surface_) {
136 InstallFirstFrameCallback();
141 [&latch,
surface = android_surface_.get(),
142 native_window = std::move(native_window)]() {
143 surface->SetNativeWindow(native_window);
154 if (android_surface_) {
158 [&latch,
surface = android_surface_.get(),
159 native_window = std::move(native_window)]() {
160 surface->TeardownOnScreenContext();
161 surface->SetNativeWindow(native_window);
173 if (android_surface_) {
177 [&latch,
surface = android_surface_.get()]() {
178 surface->TeardownOnScreenContext();
186 if (!android_surface_) {
192 [&latch,
surface = android_surface_.get(),
size]() {
193 surface->OnScreenSurfaceResize(size);
201 jobject java_message_data,
202 jint java_message_position,
204 uint8_t* message_data =
205 static_cast<uint8_t*
>(
env->GetDirectBufferAddress(java_message_data));
211 response = fml::MakeRefCounted<PlatformMessageResponseAndroid>(
216 std::make_unique<flutter::PlatformMessage>(
217 std::move(
name), std::move(
message), std::move(response)));
225 response = fml::MakeRefCounted<PlatformMessageResponseAndroid>(
230 std::make_unique<flutter::PlatformMessage>(std::move(
name),
231 std::move(response)));
235void PlatformViewAndroid::HandlePlatformMessage(
236 std::unique_ptr<flutter::PlatformMessage>
message) {
238 platform_message_handler_->HandlePlatformMessage(std::move(
message));
242void PlatformViewAndroid::OnPreEngineRestart()
const {
243 jni_facade_->FlutterViewOnPreEngineRestart();
250 jint args_position) {
251 if (
env->IsSameObject(
args, NULL)) {
258 uint8_t* args_data =
static_cast<uint8_t*
>(
env->GetDirectBufferAddress(
args));
263 std::move(args_vector));
267void PlatformViewAndroid::UpdateSemantics(
276 switch (android_context_->RenderingApi()) {
279 RegisterTexture(std::make_shared<SurfaceTextureExternalTextureImpellerGL>(
280 std::static_pointer_cast<impeller::ContextGLES>(
281 android_context_->GetImpellerContext()),
290 FML_LOG(INFO) <<
"Software rendering does not support external textures.";
293 FML_LOG(
ERROR) <<
"Impeller requires migrating plugins that create and "
294 "register surface textures to the new surface producer "
296 "https://docs.flutter.dev/release/breaking-changes/"
297 "android-surface-plugins";
304 switch (android_context_->RenderingApi()) {
308 std::static_pointer_cast<impeller::ContextGLES>(
309 android_context_->GetImpellerContext()),
310 texture_id, image_texture_entry, jni_facade_));
315 std::static_pointer_cast<AndroidContextGLSkia>(android_context_),
316 texture_id, image_texture_entry, jni_facade_));
320 std::static_pointer_cast<impeller::ContextVK>(
321 android_context_->GetImpellerContext()),
322 texture_id, image_texture_entry, jni_facade_));
325 FML_LOG(INFO) <<
"Software rendering does not support external textures.";
331std::unique_ptr<VsyncWaiter> PlatformViewAndroid::CreateVSyncWaiter() {
336std::unique_ptr<Surface> PlatformViewAndroid::CreateRenderingSurface() {
337 if (!android_surface_) {
340 return android_surface_->CreateGPUSurface(
341 android_context_->GetMainSkiaContext().get());
345std::shared_ptr<ExternalViewEmbedder>
346PlatformViewAndroid::CreateExternalViewEmbedder() {
347 return std::make_shared<AndroidExternalViewEmbedder>(
348 *android_context_, jni_facade_, surface_factory_,
task_runners_);
352std::unique_ptr<SnapshotSurfaceProducer>
353PlatformViewAndroid::CreateSnapshotSurfaceProducer() {
354 if (!android_surface_) {
357 return std::make_unique<AndroidSnapshotSurfaceProducer>(*android_surface_);
362 if (!android_surface_) {
366 if (android_surface_->ResourceContextMakeCurrent()) {
374 FML_DLOG(
ERROR) <<
"Could not make the resource context current.";
377 return resource_context;
381void PlatformViewAndroid::ReleaseResourceContext()
const {
382 if (android_surface_) {
383 android_surface_->ResourceContextClearCurrent();
388std::shared_ptr<impeller::Context> PlatformViewAndroid::GetImpellerContext()
390 if (android_surface_) {
391 return android_surface_->GetImpellerContext();
397std::unique_ptr<std::vector<std::string>>
398PlatformViewAndroid::ComputePlatformResolvedLocales(
399 const std::vector<std::string>& supported_locale_data) {
400 return jni_facade_->FlutterViewComputePlatformResolvedLocale(
401 supported_locale_data);
405void PlatformViewAndroid::RequestDartDeferredLibrary(intptr_t loading_unit_id) {
406 if (jni_facade_->RequestDartDeferredLibrary(loading_unit_id)) {
414 intptr_t loading_unit_id,
415 std::unique_ptr<const fml::Mapping> snapshot_data,
416 std::unique_ptr<const fml::Mapping> snapshot_instructions) {
418 std::move(snapshot_instructions));
423 intptr_t loading_unit_id,
424 const std::string error_message,
432 std::unique_ptr<AssetResolver> updated_asset_resolver,
437void PlatformViewAndroid::InstallFirstFrameCallback() {
443 platform_task_runner->PostTask([platform_view]() {
446 reinterpret_cast<PlatformViewAndroid*>(platform_view.get())
447 ->FireFirstFrameCallback();
453void PlatformViewAndroid::FireFirstFrameCallback() {
454 jni_facade_->FlutterViewOnFirstFrame();
457double PlatformViewAndroid::GetScaledFontSize(
double unscaled_font_size,
458 int configuration_id)
const {
459 return jni_facade_->FlutterViewGetScaledFontSize(unscaled_font_size,
std::unique_ptr< AndroidSurface > CreateSurface() override
AndroidSurfaceFactoryImpl(const std::shared_ptr< AndroidContext > &context, bool enable_impeller)
~AndroidSurfaceFactoryImpl() override
AssetResolverType
Identifies the type of AssetResolver an instance is.
static sk_sp< const GrGLInterface > GetDefaultPlatformGLInterface()
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)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
#define FML_DLOG(severity)
#define FML_LOG(severity)
#define FML_CHECK(condition)
#define FML_UNREACHABLE()
static std::shared_ptr< flutter::AndroidContext > CreateAndroidContext(bool use_software_rendering, const flutter::TaskRunners &task_runners, AndroidRenderingAPI android_rendering_api, bool enable_vulkan_validation, bool enable_opengl_gpu_tracing, bool enable_vulkan_gpu_tracing)
std::unordered_map< int32_t, SemanticsNode > SemanticsNodeUpdates
std::unordered_map< int32_t, CustomAccessibilityAction > CustomAccessibilityActionUpdates
DEF_SWITCHES_START aot vmservice shared library name
it will be possible to load the file into Perfetto s trace viewer 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