From 14fe2f8c2718fea7909c203bd4d8506d630a60dd Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Thu, 25 Jan 2024 09:02:34 -0800 Subject: [PATCH] [Impeller] call shutdown method in Android context. (flutter/engine#49992) The Impeller context requires us to call Shutdown to ensure that all of its spawned worker threads are joined, ensuring that the raster thread retains the last reference. Without this, we may either destroy parts of the context to early, or attempt to join into a worker thread which will fail. --- .../android/android_context_gl_unittests.cc | 72 +++++++++++++++++++ .../android/context/android_context.cc | 3 + 2 files changed, 75 insertions(+) diff --git a/engine/src/flutter/shell/platform/android/android_context_gl_unittests.cc b/engine/src/flutter/shell/platform/android/android_context_gl_unittests.cc index e1ee5ec89e..859a1d3100 100644 --- a/engine/src/flutter/shell/platform/android/android_context_gl_unittests.cc +++ b/engine/src/flutter/shell/platform/android/android_context_gl_unittests.cc @@ -6,8 +6,10 @@ #include "flutter/shell/platform/android/android_egl_surface.h" #include "flutter/shell/platform/android/android_environment_gl.h" #include "flutter/shell/platform/android/android_surface_gl_skia.h" +#include "fml/logging.h" #include "gmock/gmock.h" #include "gtest/gtest.h" +#include "shell/platform/android/context/android_context.h" namespace flutter { namespace testing { @@ -27,6 +29,65 @@ TaskRunners MakeTaskRunners(const std::string& thread_label, } } // namespace +class TestImpellerContext : public impeller::Context { + public: + TestImpellerContext() {} + + ~TestImpellerContext() {} + + impeller::Context::BackendType GetBackendType() const override { + return impeller::Context::BackendType::kOpenGLES; + } + + std::string DescribeGpuModel() const override { return ""; } + + bool IsValid() const override { return true; } + + const std::shared_ptr& GetCapabilities() + const override { + FML_UNREACHABLE(); + } + + bool UpdateOffscreenLayerPixelFormat(impeller::PixelFormat format) override { + FML_UNREACHABLE(); + } + + std::shared_ptr GetResourceAllocator() const override { + FML_UNREACHABLE(); + } + + std::shared_ptr GetShaderLibrary() const override { + FML_UNREACHABLE(); + } + + std::shared_ptr GetSamplerLibrary() const override { + FML_UNREACHABLE(); + } + + std::shared_ptr GetPipelineLibrary() + const override { + FML_UNREACHABLE(); + } + + std::shared_ptr CreateCommandBuffer() + const override { + FML_UNREACHABLE(); + } + + void Shutdown() override { did_shutdown = true; } + + bool did_shutdown = false; +}; + +class TestAndroidContext : public AndroidContext { + public: + TestAndroidContext(const std::shared_ptr& impeller_context, + AndroidRenderingAPI rendering_api) + : AndroidContext(rendering_api) { + SetImpellerContext(impeller_context); + } +}; + TEST(AndroidContextGl, Create) { GrMockOptions main_context_options; sk_sp main_context = @@ -47,6 +108,17 @@ TEST(AndroidContextGl, Create) { EXPECT_TRUE(main_context->abandoned()); } +TEST(AndroidContextGl, CreateImpeller) { + auto impeller_context = std::make_shared(); + auto android_context = std::make_unique( + impeller_context, AndroidRenderingAPI::kOpenGLES); + EXPECT_FALSE(impeller_context->did_shutdown); + + android_context.reset(); + + EXPECT_TRUE(impeller_context->did_shutdown); +} + TEST(AndroidContextGl, CreateSingleThread) { GrMockOptions main_context_options; sk_sp main_context = diff --git a/engine/src/flutter/shell/platform/android/context/android_context.cc b/engine/src/flutter/shell/platform/android/context/android_context.cc index 3d7c4e619d..ed0970cb73 100644 --- a/engine/src/flutter/shell/platform/android/context/android_context.cc +++ b/engine/src/flutter/shell/platform/android/context/android_context.cc @@ -13,6 +13,9 @@ AndroidContext::~AndroidContext() { if (main_context_) { main_context_->releaseResourcesAndAbandonContext(); } + if (impeller_context_) { + impeller_context_->Shutdown(); + } }; AndroidRenderingAPI AndroidContext::RenderingApi() const {