diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 2317b1ea8b..4074fe5715 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -640,6 +640,8 @@ FILE: ../../../flutter/shell/platform/android/android_environment_gl.h FILE: ../../../flutter/shell/platform/android/android_exports.lst FILE: ../../../flutter/shell/platform/android/android_external_texture_gl.cc FILE: ../../../flutter/shell/platform/android/android_external_texture_gl.h +FILE: ../../../flutter/shell/platform/android/android_external_view_embedder.cc +FILE: ../../../flutter/shell/platform/android/android_external_view_embedder.h FILE: ../../../flutter/shell/platform/android/android_native_window.cc FILE: ../../../flutter/shell/platform/android/android_native_window.h FILE: ../../../flutter/shell/platform/android/android_shell_holder.cc diff --git a/engine/src/flutter/flow/embedded_views.h b/engine/src/flutter/flow/embedded_views.h index 14dd00cf5e..5888288bf9 100644 --- a/engine/src/flutter/flow/embedded_views.h +++ b/engine/src/flutter/flow/embedded_views.h @@ -206,8 +206,8 @@ enum class PostPrerollResult { kResubmitFrame, kSuccess }; // Facilitates embedding of platform views within the flow layer tree. // -// Used on iOS and on embedded platforms that provide a system compositor -// as part of the project arguments. +// Used on iOS, Android (hybrid composite mode), and on embedded platforms +// that provide a system compositor as part of the project arguments. class ExternalViewEmbedder { // TODO(cyanglaz): Make embedder own the `EmbeddedViewParams`. diff --git a/engine/src/flutter/shell/platform/android/BUILD.gn b/engine/src/flutter/shell/platform/android/BUILD.gn index cc5d0f8b59..d88e77e735 100644 --- a/engine/src/flutter/shell/platform/android/BUILD.gn +++ b/engine/src/flutter/shell/platform/android/BUILD.gn @@ -29,6 +29,8 @@ shared_library("flutter_shell_native") { "android_environment_gl.h", "android_external_texture_gl.cc", "android_external_texture_gl.h", + "android_external_view_embedder.cc", + "android_external_view_embedder.h", "android_native_window.cc", "android_native_window.h", "android_shell_holder.cc", diff --git a/engine/src/flutter/shell/platform/android/android_external_view_embedder.cc b/engine/src/flutter/shell/platform/android/android_external_view_embedder.cc new file mode 100644 index 0000000000..eae1c1f34c --- /dev/null +++ b/engine/src/flutter/shell/platform/android/android_external_view_embedder.cc @@ -0,0 +1,95 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/android/android_external_view_embedder.h" + +#include "fml/trace_event.h" + +namespace flutter { + +// |ExternalViewEmbedder| +void AndroidExternalViewEmbedder::PrerollCompositeEmbeddedView( + int view_id, + std::unique_ptr params) { + // TODO(egarciad): Implement hybrid composition. + // https://github.com/flutter/flutter/issues/55270 + TRACE_EVENT0("flutter", + "AndroidExternalViewEmbedder::PrerollCompositeEmbeddedView"); + picture_recorders_[view_id] = std::make_unique(); + picture_recorders_[view_id]->beginRecording(SkRect::Make(frame_size_)); + + composition_order_.push_back(view_id); +} + +// |ExternalViewEmbedder| +SkCanvas* AndroidExternalViewEmbedder::CompositeEmbeddedView(int view_id) { + return picture_recorders_[view_id]->getRecordingCanvas(); +} + +// |ExternalViewEmbedder| +std::vector AndroidExternalViewEmbedder::GetCurrentCanvases() { + // TODO(egarciad): Implement hybrid composition. + // https://github.com/flutter/flutter/issues/55270 + std::vector canvases; + for (size_t i = 0; i < composition_order_.size(); i++) { + int64_t view_id = composition_order_[i]; + canvases.push_back(picture_recorders_[view_id]->getRecordingCanvas()); + } + return canvases; +} + +// |ExternalViewEmbedder| +bool AndroidExternalViewEmbedder::SubmitFrame(GrContext* context, + SkCanvas* background_canvas) { + // TODO(egarciad): Implement hybrid composition. + // https://github.com/flutter/flutter/issues/55270 + TRACE_EVENT0("flutter", "AndroidExternalViewEmbedder::SubmitFrame"); + for (size_t i = 0; i < composition_order_.size(); i++) { + int64_t view_id = composition_order_[i]; + background_canvas->drawPicture( + picture_recorders_[view_id]->finishRecordingAsPicture()); + } + return true; +} + +// |ExternalViewEmbedder| +PostPrerollResult AndroidExternalViewEmbedder::PostPrerollAction( + fml::RefPtr raster_thread_merger) { + return PostPrerollResult::kSuccess; +} + +// |ExternalViewEmbedder| +SkCanvas* AndroidExternalViewEmbedder::GetRootCanvas() { + // On Android, the root surface is created from the on-screen render target. + return nullptr; +} + +// |ExternalViewEmbedder| +void AndroidExternalViewEmbedder::BeginFrame(SkISize frame_size, + GrContext* context, + double device_pixel_ratio) { + frame_size_ = frame_size; +} + +void AndroidExternalViewEmbedder::ClearFrame() { + composition_order_.clear(); + picture_recorders_.clear(); + frame_size_ = SkISize::MakeEmpty(); +} + +// |ExternalViewEmbedder| +void AndroidExternalViewEmbedder::CancelFrame() { + ClearFrame(); +} + +// |ExternalViewEmbedder| +void AndroidExternalViewEmbedder::FinishFrame() { + ClearFrame(); +} + +// |ExternalViewEmbedder| +void AndroidExternalViewEmbedder::EndFrame( + fml::RefPtr raster_thread_merger) {} + +} // namespace flutter diff --git a/engine/src/flutter/shell/platform/android/android_external_view_embedder.h b/engine/src/flutter/shell/platform/android/android_external_view_embedder.h new file mode 100644 index 0000000000..75bbf55af8 --- /dev/null +++ b/engine/src/flutter/shell/platform/android/android_external_view_embedder.h @@ -0,0 +1,71 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_ANDROID_EXTERNAL_VIEW_EMBEDDER_H_ +#define FLUTTER_SHELL_PLATFORM_ANDROID_EXTERNAL_VIEW_EMBEDDER_H_ + +#include "flutter/flow/embedded_views.h" + +#include "third_party/skia/include/core/SkPictureRecorder.h" + +namespace flutter { + +class AndroidExternalViewEmbedder : public ExternalViewEmbedder { + public: + // |ExternalViewEmbedder| + void PrerollCompositeEmbeddedView( + int view_id, + std::unique_ptr params) override; + + // |ExternalViewEmbedder| + SkCanvas* CompositeEmbeddedView(int view_id) override; + + // |ExternalViewEmbedder| + std::vector GetCurrentCanvases() override; + + // |ExternalViewEmbedder| + bool SubmitFrame(GrContext* context, SkCanvas* background_canvas) override; + + // |ExternalViewEmbedder| + PostPrerollResult PostPrerollAction( + fml::RefPtr raster_thread_merger) override; + + // |ExternalViewEmbedder| + SkCanvas* GetRootCanvas() override; + + // |ExternalViewEmbedder| + void BeginFrame(SkISize frame_size, + GrContext* context, + double device_pixel_ratio) override; + + // |ExternalViewEmbedder| + void CancelFrame() override; + + // |ExternalViewEmbedder| + void FinishFrame() override; + + // |ExternalViewEmbedder| + void EndFrame( + fml::RefPtr raster_thread_merger) override; + + private: + // The size of the background canvas. + SkISize frame_size_; + + // The order of composition. Each entry contains a unique id for the platform + // view. + std::vector composition_order_; + + // The platform view's picture recorder keyed off the platform view id, which + // contains any subsequent operation until the next platform view or the end + // of the last leaf node in the layer tree. + std::map> picture_recorders_; + + /// Resets the state. + void ClearFrame(); +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_ANDROID_EXTERNAL_VIEW_EMBEDDER_H_ diff --git a/engine/src/flutter/shell/platform/android/android_surface_gl.cc b/engine/src/flutter/shell/platform/android/android_surface_gl.cc index 737d9f293a..e27756c3b6 100644 --- a/engine/src/flutter/shell/platform/android/android_surface_gl.cc +++ b/engine/src/flutter/shell/platform/android/android_surface_gl.cc @@ -32,6 +32,8 @@ AndroidSurfaceGL::AndroidSurfaceGL() { if (!offscreen_context_ || !offscreen_context_->IsValid()) { offscreen_context_ = nullptr; } + + external_view_embedder_ = std::make_unique(); } AndroidSurfaceGL::~AndroidSurfaceGL() = default; @@ -127,7 +129,7 @@ intptr_t AndroidSurfaceGL::GLContextFBO() const { // |GPUSurfaceGLDelegate| ExternalViewEmbedder* AndroidSurfaceGL::GetExternalViewEmbedder() { - return nullptr; + return external_view_embedder_.get(); } } // namespace flutter diff --git a/engine/src/flutter/shell/platform/android/android_surface_gl.h b/engine/src/flutter/shell/platform/android/android_surface_gl.h index d59302ad66..045929ef67 100644 --- a/engine/src/flutter/shell/platform/android/android_surface_gl.h +++ b/engine/src/flutter/shell/platform/android/android_surface_gl.h @@ -12,6 +12,7 @@ #include "flutter/shell/gpu/gpu_surface_gl.h" #include "flutter/shell/platform/android/android_context_gl.h" #include "flutter/shell/platform/android/android_environment_gl.h" +#include "flutter/shell/platform/android/android_external_view_embedder.h" #include "flutter/shell/platform/android/android_surface.h" namespace flutter { @@ -64,6 +65,7 @@ class AndroidSurfaceGL final : public GPUSurfaceGLDelegate, private: fml::RefPtr onscreen_context_; fml::RefPtr offscreen_context_; + std::unique_ptr external_view_embedder_; FML_DISALLOW_COPY_AND_ASSIGN(AndroidSurfaceGL); }; diff --git a/engine/src/flutter/shell/platform/android/android_surface_software.cc b/engine/src/flutter/shell/platform/android/android_surface_software.cc index 620e15301a..fa1399cf0a 100644 --- a/engine/src/flutter/shell/platform/android/android_surface_software.cc +++ b/engine/src/flutter/shell/platform/android/android_surface_software.cc @@ -39,6 +39,7 @@ bool GetSkColorType(int32_t buffer_format, AndroidSurfaceSoftware::AndroidSurfaceSoftware() { GetSkColorType(WINDOW_FORMAT_RGBA_8888, &target_color_type_, &target_alpha_type_); + external_view_embedder_ = std::make_unique(); } AndroidSurfaceSoftware::~AndroidSurfaceSoftware() = default; @@ -137,7 +138,7 @@ bool AndroidSurfaceSoftware::PresentBackingStore( // |GPUSurfaceSoftwareDelegate| ExternalViewEmbedder* AndroidSurfaceSoftware::GetExternalViewEmbedder() { - return nullptr; + return external_view_embedder_.get(); } void AndroidSurfaceSoftware::TeardownOnScreenContext() {} diff --git a/engine/src/flutter/shell/platform/android/android_surface_software.h b/engine/src/flutter/shell/platform/android/android_surface_software.h index 30888091fe..3ae108c488 100644 --- a/engine/src/flutter/shell/platform/android/android_surface_software.h +++ b/engine/src/flutter/shell/platform/android/android_surface_software.h @@ -9,6 +9,7 @@ #include "flutter/fml/platform/android/jni_weak_ref.h" #include "flutter/fml/platform/android/scoped_java_ref.h" #include "flutter/shell/gpu/gpu_surface_software.h" +#include "flutter/shell/platform/android/android_external_view_embedder.h" #include "flutter/shell/platform/android/android_surface.h" namespace flutter { @@ -55,6 +56,7 @@ class AndroidSurfaceSoftware final : public AndroidSurface, fml::RefPtr native_window_; SkColorType target_color_type_; SkAlphaType target_alpha_type_; + std::unique_ptr external_view_embedder_; FML_DISALLOW_COPY_AND_ASSIGN(AndroidSurfaceSoftware); }; diff --git a/engine/src/flutter/shell/platform/android/android_surface_vulkan.cc b/engine/src/flutter/shell/platform/android/android_surface_vulkan.cc index ffcf52ac3a..601ae4298d 100644 --- a/engine/src/flutter/shell/platform/android/android_surface_vulkan.cc +++ b/engine/src/flutter/shell/platform/android/android_surface_vulkan.cc @@ -13,7 +13,9 @@ namespace flutter { AndroidSurfaceVulkan::AndroidSurfaceVulkan() - : proc_table_(fml::MakeRefCounted()) {} + : proc_table_(fml::MakeRefCounted()) { + external_view_embedder_ = std::make_unique(); +} AndroidSurfaceVulkan::~AndroidSurfaceVulkan() = default; @@ -73,7 +75,7 @@ bool AndroidSurfaceVulkan::SetNativeWindow( } ExternalViewEmbedder* AndroidSurfaceVulkan::GetExternalViewEmbedder() { - return nullptr; + return external_view_embedder_.get(); } fml::RefPtr AndroidSurfaceVulkan::vk() { diff --git a/engine/src/flutter/shell/platform/android/android_surface_vulkan.h b/engine/src/flutter/shell/platform/android/android_surface_vulkan.h index d97973002e..188296565f 100644 --- a/engine/src/flutter/shell/platform/android/android_surface_vulkan.h +++ b/engine/src/flutter/shell/platform/android/android_surface_vulkan.h @@ -9,6 +9,7 @@ #include #include "flutter/fml/macros.h" #include "flutter/shell/gpu/gpu_surface_vulkan_delegate.h" +#include "flutter/shell/platform/android/android_external_view_embedder.h" #include "flutter/shell/platform/android/android_native_window.h" #include "flutter/shell/platform/android/android_surface.h" #include "flutter/vulkan/vulkan_window.h" @@ -52,6 +53,7 @@ class AndroidSurfaceVulkan : public AndroidSurface, private: fml::RefPtr proc_table_; fml::RefPtr native_window_; + std::unique_ptr external_view_embedder_; FML_DISALLOW_COPY_AND_ASSIGN(AndroidSurfaceVulkan); };