From 7132a8a12dd61243c334cb04f3115076964812ea Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Fri, 23 Aug 2019 13:52:06 -0700 Subject: [PATCH] Avoid root surface acquisition during custom composition with software renderer. (flutter/engine#11394) Uses the same technique used during OpenGL composition to elide root surface access. The refactoring of this approach is tracked in https://github.com/flutter/flutter/issues/38466 Fixes https://github.com/flutter/flutter/issues/39009. --- .../flutter/shell/gpu/gpu_surface_software.cc | 16 ++++++++++++++-- .../flutter/shell/gpu/gpu_surface_software.h | 8 +++++++- .../android/android_surface_software.cc | 3 ++- .../platform/darwin/ios/ios_surface_software.mm | 2 +- .../platform/embedder/embedder_surface_gl.cc | 2 +- .../embedder/embedder_surface_software.cc | 4 ++-- .../embedder/tests/embedder_config_builder.cc | 17 ++++++++++++++++- .../embedder/tests/embedder_test_context.cc | 15 +++++++++++++++ .../embedder/tests/embedder_test_context.h | 9 +++++++++ .../embedder/tests/embedder_unittests.cc | 8 ++++++++ 10 files changed, 75 insertions(+), 9 deletions(-) diff --git a/engine/src/flutter/shell/gpu/gpu_surface_software.cc b/engine/src/flutter/shell/gpu/gpu_surface_software.cc index 443e7b704a..346857ac47 100644 --- a/engine/src/flutter/shell/gpu/gpu_surface_software.cc +++ b/engine/src/flutter/shell/gpu/gpu_surface_software.cc @@ -9,8 +9,11 @@ namespace flutter { -GPUSurfaceSoftware::GPUSurfaceSoftware(GPUSurfaceSoftwareDelegate* delegate) - : delegate_(delegate), weak_factory_(this) {} +GPUSurfaceSoftware::GPUSurfaceSoftware(GPUSurfaceSoftwareDelegate* delegate, + bool render_to_surface) + : delegate_(delegate), + render_to_surface_(render_to_surface), + weak_factory_(this) {} GPUSurfaceSoftware::~GPUSurfaceSoftware() = default; @@ -22,6 +25,15 @@ bool GPUSurfaceSoftware::IsValid() { // |Surface| std::unique_ptr GPUSurfaceSoftware::AcquireFrame( const SkISize& logical_size) { + // TODO(38466): Refactor GPU surface APIs take into account the fact that an + // external view embedder may want to render to the root surface. + if (!render_to_surface_) { + return std::make_unique( + nullptr, [](const SurfaceFrame& surface_frame, SkCanvas* canvas) { + return true; + }); + } + if (!IsValid()) { return nullptr; } diff --git a/engine/src/flutter/shell/gpu/gpu_surface_software.h b/engine/src/flutter/shell/gpu/gpu_surface_software.h index 84b38411ea..af4d1cb3c1 100644 --- a/engine/src/flutter/shell/gpu/gpu_surface_software.h +++ b/engine/src/flutter/shell/gpu/gpu_surface_software.h @@ -14,7 +14,8 @@ namespace flutter { class GPUSurfaceSoftware : public Surface { public: - GPUSurfaceSoftware(GPUSurfaceSoftwareDelegate* delegate); + GPUSurfaceSoftware(GPUSurfaceSoftwareDelegate* delegate, + bool render_to_surface); ~GPUSurfaceSoftware() override; @@ -35,6 +36,11 @@ class GPUSurfaceSoftware : public Surface { private: GPUSurfaceSoftwareDelegate* delegate_; + // TODO(38466): Refactor GPU surface APIs take into account the fact that an + // external view embedder may want to render to the root surface. This is a + // hack to make avoid allocating resources for the root surface when an + // external view embedder is present. + const bool render_to_surface_; fml::WeakPtrFactory weak_factory_; FML_DISALLOW_COPY_AND_ASSIGN(GPUSurfaceSoftware); 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 5262fa5b9a..620e15301a 100644 --- a/engine/src/flutter/shell/platform/android/android_surface_software.cc +++ b/engine/src/flutter/shell/platform/android/android_surface_software.cc @@ -61,7 +61,8 @@ std::unique_ptr AndroidSurfaceSoftware::CreateGPUSurface() { return nullptr; } - auto surface = std::make_unique(this); + auto surface = + std::make_unique(this, true /* render to surface */); if (!surface->IsValid()) { return nullptr; diff --git a/engine/src/flutter/shell/platform/darwin/ios/ios_surface_software.mm b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_software.mm index 4470fc25df..89d6de8f92 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/ios_surface_software.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_software.mm @@ -43,7 +43,7 @@ std::unique_ptr IOSSurfaceSoftware::CreateGPUSurface() { return nullptr; } - auto surface = std::make_unique(this); + auto surface = std::make_unique(this, true /* render to surface */); if (!surface->IsValid()) { return nullptr; diff --git a/engine/src/flutter/shell/platform/embedder/embedder_surface_gl.cc b/engine/src/flutter/shell/platform/embedder/embedder_surface_gl.cc index b5c320867a..d37b03aae8 100644 --- a/engine/src/flutter/shell/platform/embedder/embedder_surface_gl.cc +++ b/engine/src/flutter/shell/platform/embedder/embedder_surface_gl.cc @@ -81,7 +81,7 @@ EmbedderSurfaceGL::GLProcResolver EmbedderSurfaceGL::GetGLProcResolver() const { // |EmbedderSurface| std::unique_ptr EmbedderSurfaceGL::CreateGPUSurface() { - bool render_to_surface = !external_view_embedder_; + const bool render_to_surface = !external_view_embedder_; return std::make_unique(this, // GPU surface GL delegate render_to_surface // render to surface diff --git a/engine/src/flutter/shell/platform/embedder/embedder_surface_software.cc b/engine/src/flutter/shell/platform/embedder/embedder_surface_software.cc index 84a83ddb3a..988ed27d85 100644 --- a/engine/src/flutter/shell/platform/embedder/embedder_surface_software.cc +++ b/engine/src/flutter/shell/platform/embedder/embedder_surface_software.cc @@ -32,8 +32,8 @@ std::unique_ptr EmbedderSurfaceSoftware::CreateGPUSurface() { if (!IsValid()) { return nullptr; } - - auto surface = std::make_unique(this); + const bool render_to_surface = !external_view_embedder_; + auto surface = std::make_unique(this, render_to_surface); if (!surface->IsValid()) { return nullptr; diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_config_builder.cc b/engine/src/flutter/shell/platform/embedder/tests/embedder_config_builder.cc index 00831c6538..689cecdcea 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_config_builder.cc +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_config_builder.cc @@ -5,6 +5,7 @@ #include "flutter/shell/platform/embedder/tests/embedder_config_builder.h" #include "flutter/shell/platform/embedder/embedder.h" +#include "third_party/skia/include/core/SkBitmap.h" namespace flutter { namespace testing { @@ -48,7 +49,21 @@ EmbedderConfigBuilder::EmbedderConfigBuilder( software_renderer_config_.struct_size = sizeof(FlutterSoftwareRendererConfig); software_renderer_config_.surface_present_callback = - [](void*, const void*, size_t, size_t) { return true; }; + [](void* context, const void* allocation, size_t row_bytes, + size_t height) { + auto image_info = + SkImageInfo::MakeN32Premul(SkISize::Make(row_bytes / 4, height)); + SkBitmap bitmap; + if (!bitmap.installPixels(image_info, const_cast(allocation), + row_bytes)) { + FML_LOG(ERROR) << "Could not copy pixels for the software " + "composition from the engine."; + return false; + } + bitmap.setImmutable(); + return reinterpret_cast(context)->SofwarePresent( + SkImage::MakeFromBitmap(bitmap)); + }; if (preference == InitializationPreference::kInitialize) { SetSoftwareRendererConfig(); diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.cc b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.cc index 212dc34ffc..fd5867925b 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.cc +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.cc @@ -143,6 +143,7 @@ bool EmbedderTestContext::GLClearCurrent() { } bool EmbedderTestContext::GLPresent() { + gl_surface_present_count_++; FML_CHECK(gl_surface_) << "GL surface must be initialized."; if (next_scene_callback_) { @@ -200,5 +201,19 @@ void EmbedderTestContext::SetNextSceneCallback( next_scene_callback_ = next_scene_callback; } +bool EmbedderTestContext::SofwarePresent(sk_sp image) { + software_surface_present_count_++; + software_surface_ = std::move(image); + return software_surface_ != nullptr; +} + +size_t EmbedderTestContext::GetGLSurfacePresentCount() const { + return gl_surface_present_count_; +} + +size_t EmbedderTestContext::GetSoftwareSurfacePresentCount() const { + return software_surface_present_count_; +} + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.h b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.h index 9d78a03c03..164134c22e 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.h +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.h @@ -61,6 +61,10 @@ class EmbedderTestContext { using NextSceneCallback = std::function image)>; void SetNextSceneCallback(NextSceneCallback next_scene_callback); + size_t GetGLSurfacePresentCount() const; + + size_t GetSoftwareSurfacePresentCount() const; + private: // This allows the builder to access the hooks. friend class EmbedderConfigBuilder; @@ -76,8 +80,11 @@ class EmbedderTestContext { SemanticsActionCallback update_semantics_custom_action_callback_; std::function platform_message_callback_; std::unique_ptr gl_surface_; + sk_sp software_surface_; std::unique_ptr compositor_; NextSceneCallback next_scene_callback_; + size_t gl_surface_present_count_ = 0; + size_t software_surface_present_count_ = 0; static VoidCallback GetIsolateCreateCallbackHook(); @@ -107,6 +114,8 @@ class EmbedderTestContext { void PlatformMessageCallback(const FlutterPlatformMessage* message); + bool SofwarePresent(sk_sp image); + FML_DISALLOW_COPY_AND_ASSIGN(EmbedderTestContext); }; diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc b/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc index db1ee72cdc..7324538688 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc @@ -1105,6 +1105,10 @@ TEST_F(EmbedderTest, CompositorMustBeAbleToRenderKnownScene) { latch.Wait(); ASSERT_TRUE(ImageMatchesFixture("compositor.png", scene_image)); + + // There should no present calls on the root surface. + ASSERT_EQ(context.GetSoftwareSurfacePresentCount(), 0u); + ASSERT_EQ(context.GetGLSurfacePresentCount(), 0u); } //------------------------------------------------------------------------------ @@ -1279,6 +1283,10 @@ TEST_F(EmbedderTest, latch.Wait(); ASSERT_TRUE(ImageMatchesFixture("compositor_software.png", scene_image)); + + // There should no present calls on the root surface. + ASSERT_EQ(context.GetSoftwareSurfacePresentCount(), 0u); + ASSERT_EQ(context.GetGLSurfacePresentCount(), 0u); } //------------------------------------------------------------------------------