diff --git a/engine/src/flutter/shell/platform/android/android_context_gl.cc b/engine/src/flutter/shell/platform/android/android_context_gl.cc index 1d0f83cafa..42ab9cd656 100644 --- a/engine/src/flutter/shell/platform/android/android_context_gl.cc +++ b/engine/src/flutter/shell/platform/android/android_context_gl.cc @@ -148,10 +148,12 @@ SkISize AndroidEGLSurface::GetSize() const { AndroidContextGL::AndroidContextGL( AndroidRenderingAPI rendering_api, - fml::RefPtr environment) + fml::RefPtr environment, + const TaskRunners& task_runners) : AndroidContext(AndroidRenderingAPI::kOpenGLES), environment_(environment), - config_(nullptr) { + config_(nullptr), + task_runners_(task_runners) { if (!environment_->IsValid()) { FML_LOG(ERROR) << "Could not create an Android GL environment."; return; @@ -189,6 +191,26 @@ AndroidContextGL::AndroidContextGL( } AndroidContextGL::~AndroidContextGL() { + FML_DCHECK(task_runners_.GetPlatformTaskRunner()->RunsTasksOnCurrentThread()); + sk_sp main_context = GetMainSkiaContext(); + SetMainSkiaContext(nullptr); + fml::AutoResetWaitableEvent latch; + // This context needs to be deallocated from the raster thread in order to + // keep a coherent usage of egl from a single thread. + fml::TaskRunner::RunNowOrPostTask(task_runners_.GetRasterTaskRunner(), [&] { + if (main_context) { + std::unique_ptr pbuffer_surface = + CreatePbufferSurface(); + if (pbuffer_surface->MakeCurrent()) { + main_context->releaseResourcesAndAbandonContext(); + main_context.reset(); + ClearCurrent(); + } + } + latch.Signal(); + }); + latch.Wait(); + if (!TeardownContext(environment_->Display(), context_)) { FML_LOG(ERROR) << "Could not tear down the EGL context. Possible resource leak."; diff --git a/engine/src/flutter/shell/platform/android/android_context_gl.h b/engine/src/flutter/shell/platform/android/android_context_gl.h index b9e34ecf6f..08668e488c 100644 --- a/engine/src/flutter/shell/platform/android/android_context_gl.h +++ b/engine/src/flutter/shell/platform/android/android_context_gl.h @@ -72,7 +72,8 @@ class AndroidEGLSurface { class AndroidContextGL : public AndroidContext { public: AndroidContextGL(AndroidRenderingAPI rendering_api, - fml::RefPtr environment); + fml::RefPtr environment, + const TaskRunners& taskRunners); ~AndroidContextGL(); @@ -131,6 +132,7 @@ class AndroidContextGL : public AndroidContext { EGLContext context_; EGLContext resource_context_; bool valid_ = false; + TaskRunners task_runners_; FML_DISALLOW_COPY_AND_ASSIGN(AndroidContextGL); }; 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 f31b023643..2278acd6b3 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 @@ -1,11 +1,67 @@ +#define FML_USED_ON_EMBEDDER + #include +#include "flutter/shell/common/thread_host.h" #include "flutter/shell/platform/android/android_context_gl.h" #include "flutter/shell/platform/android/android_environment_gl.h" #include "gtest/gtest.h" -TEST(AndroidContextGl, Create) { - auto environment = fml::MakeRefCounted(); - auto context = std::make_unique( - flutter::AndroidRenderingAPI::kOpenGLES, environment); - EXPECT_NE(context.get(), nullptr); +namespace flutter { +namespace testing { +namespace android { +namespace { +TaskRunners MakeTaskRunners(const std::string& thread_label, + const ThreadHost& thread_host) { + fml::MessageLoop::EnsureInitializedForCurrentThread(); + fml::RefPtr platform_runner = + fml::MessageLoop::GetCurrent().GetTaskRunner(); + + return TaskRunners(thread_label, platform_runner, + thread_host.raster_thread->GetTaskRunner(), + thread_host.ui_thread->GetTaskRunner(), + thread_host.io_thread->GetTaskRunner()); } +} // namespace + +TEST(AndroidContextGl, Create) { + GrMockOptions main_context_options; + sk_sp main_context = + GrDirectContext::MakeMock(&main_context_options); + auto environment = fml::MakeRefCounted(); + std::string thread_label = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + ThreadHost thread_host(thread_label, ThreadHost::Type::UI | + ThreadHost::Type::RASTER | + ThreadHost::Type::IO); + TaskRunners task_runners = MakeTaskRunners(thread_label, thread_host); + auto context = std::make_unique( + AndroidRenderingAPI::kOpenGLES, environment, task_runners); + context->SetMainSkiaContext(main_context); + EXPECT_NE(context.get(), nullptr); + context.reset(); + EXPECT_TRUE(main_context->abandoned()); +} + +TEST(AndroidContextGl, CreateSingleThread) { + GrMockOptions main_context_options; + sk_sp main_context = + GrDirectContext::MakeMock(&main_context_options); + auto environment = fml::MakeRefCounted(); + std::string thread_label = + ::testing::UnitTest::GetInstance()->current_test_info()->name(); + fml::MessageLoop::EnsureInitializedForCurrentThread(); + fml::RefPtr platform_runner = + fml::MessageLoop::GetCurrent().GetTaskRunner(); + TaskRunners task_runners = + TaskRunners(thread_label, platform_runner, platform_runner, + platform_runner, platform_runner); + auto context = std::make_unique( + AndroidRenderingAPI::kOpenGLES, environment, task_runners); + context->SetMainSkiaContext(main_context); + EXPECT_NE(context.get(), nullptr); + context.reset(); + EXPECT_TRUE(main_context->abandoned()); +} +} // namespace android +} // namespace testing +} // namespace flutter 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 ca3e24e8cd..224b2280d2 100644 --- a/engine/src/flutter/shell/platform/android/android_surface_gl.cc +++ b/engine/src/flutter/shell/platform/android/android_surface_gl.cc @@ -52,7 +52,13 @@ std::unique_ptr AndroidSurfaceGL::CreateGPUSurface( if (gr_context) { return std::make_unique(sk_ref_sp(gr_context), this, true); } else { - return std::make_unique(this, true); + sk_sp main_skia_context = + GLContextPtr()->GetMainSkiaContext(); + if (!main_skia_context) { + main_skia_context = GPUSurfaceGL::MakeGLContext(this); + GLContextPtr()->SetMainSkiaContext(main_skia_context); + } + return std::make_unique(main_skia_context, this, true); } } @@ -171,7 +177,14 @@ AndroidContextGL* AndroidSurfaceGL::GLContextPtr() const { std::unique_ptr AndroidSurfaceGL::CreatePbufferSurface() { onscreen_surface_ = GLContextPtr()->CreatePbufferSurface(); - return std::make_unique(this, true); + sk_sp main_skia_context = + GLContextPtr()->GetMainSkiaContext(); + if (!main_skia_context) { + main_skia_context = GPUSurfaceGL::MakeGLContext(this); + GLContextPtr()->SetMainSkiaContext(main_skia_context); + } + + return std::make_unique(main_skia_context, this, true); } } // namespace flutter 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 4ff6550c15..1d4badf954 100644 --- a/engine/src/flutter/shell/platform/android/context/android_context.cc +++ b/engine/src/flutter/shell/platform/android/context/android_context.cc @@ -9,7 +9,11 @@ namespace flutter { AndroidContext::AndroidContext(AndroidRenderingAPI rendering_api) : rendering_api_(rendering_api) {} -AndroidContext::~AndroidContext() = default; +AndroidContext::~AndroidContext() { + if (main_context_) { + main_context_->releaseResourcesAndAbandonContext(); + } +}; AndroidRenderingAPI AndroidContext::RenderingApi() const { return rendering_api_; @@ -19,4 +23,13 @@ bool AndroidContext::IsValid() const { return true; } +void AndroidContext::SetMainSkiaContext( + const sk_sp& main_context) { + main_context_ = main_context; +} + +sk_sp AndroidContext::GetMainSkiaContext() const { + return main_context_; +} + } // namespace flutter diff --git a/engine/src/flutter/shell/platform/android/context/android_context.h b/engine/src/flutter/shell/platform/android/context/android_context.h index 103340f7e5..0bed23e398 100644 --- a/engine/src/flutter/shell/platform/android/context/android_context.h +++ b/engine/src/flutter/shell/platform/android/context/android_context.h @@ -6,6 +6,7 @@ #define FLUTTER_SHELL_PLATFORM_ANDROID_ANDROID_CONTEXT_H_ #include "flutter/fml/macros.h" +#include "flutter/fml/task_runner.h" #include "third_party/skia/include/gpu/GrDirectContext.h" namespace flutter { @@ -28,9 +29,37 @@ class AndroidContext { virtual bool IsValid() const; + //---------------------------------------------------------------------------- + /// @brief Setter for the Skia context to be used by subsequent + /// AndroidSurfaces. + /// @details This is useful to reduce memory consumption when creating + /// multiple AndroidSurfaces for the same AndroidContext. + /// + /// The first AndroidSurface should set this for the + /// AndroidContext if the AndroidContext does not yet have a + /// Skia context to share via GetMainSkiaContext. + /// + void SetMainSkiaContext(const sk_sp& main_context); + + //---------------------------------------------------------------------------- + /// @brief Accessor for the Skia context associated with AndroidSurfaces + /// and the raster thread. + /// @details This context is created lazily by the AndroidSurface based + /// on their respective rendering backend and set on this + /// AndroidContext to share via SetMainSkiaContext. + /// @returns `nullptr` when no Skia context has been set yet by its + /// AndroidSurface via SetMainSkiaContext. + /// @attention The software context doesn't have a Skia context, so this + /// value will be nullptr. + /// + sk_sp GetMainSkiaContext() const; + private: const AndroidRenderingAPI rendering_api_; + // This is the Skia context used for on-screen rendering. + sk_sp main_context_; + FML_DISALLOW_COPY_AND_ASSIGN(AndroidContext); }; diff --git a/engine/src/flutter/shell/platform/android/platform_view_android.cc b/engine/src/flutter/shell/platform/android/platform_view_android.cc index 064bbf92ae..69a6bece9c 100644 --- a/engine/src/flutter/shell/platform/android/platform_view_android.cc +++ b/engine/src/flutter/shell/platform/android/platform_view_android.cc @@ -46,13 +46,14 @@ std::unique_ptr AndroidSurfaceFactoryImpl::CreateSurface() { } static std::shared_ptr CreateAndroidContext( - bool use_software_rendering) { + bool use_software_rendering, + const flutter::TaskRunners task_runners) { if (use_software_rendering) { return std::make_shared(AndroidRenderingAPI::kSoftware); } return std::make_unique( AndroidRenderingAPI::kOpenGLES, - fml::MakeRefCounted()); + fml::MakeRefCounted(), task_runners); } PlatformViewAndroid::PlatformViewAndroid( @@ -60,10 +61,11 @@ PlatformViewAndroid::PlatformViewAndroid( flutter::TaskRunners task_runners, std::shared_ptr jni_facade, bool use_software_rendering) - : PlatformViewAndroid(delegate, - std::move(task_runners), - std::move(jni_facade), - CreateAndroidContext(use_software_rendering)) {} + : PlatformViewAndroid( + delegate, + std::move(task_runners), + std::move(jni_facade), + CreateAndroidContext(use_software_rendering, task_runners)) {} PlatformViewAndroid::PlatformViewAndroid( PlatformView::Delegate& delegate, @@ -247,7 +249,8 @@ std::unique_ptr PlatformViewAndroid::CreateRenderingSurface() { if (!android_surface_) { return nullptr; } - return android_surface_->CreateGPUSurface(); + return android_surface_->CreateGPUSurface( + android_context_->GetMainSkiaContext().get()); } // |PlatformView|