From 49424e1b4ffa6e0424ac8c96d4320d0d8f51aa7a Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Fri, 16 Dec 2016 15:36:33 -0800 Subject: [PATCH] Detach the EGL surface from the GPU thread and then destroy it during the FlutterView surfaceDestroyed callback (flutter/engine#3316) Fixes https://github.com/flutter/flutter/issues/7147 --- engine/src/flutter/shell/common/null_rasterizer.cc | 5 +++++ engine/src/flutter/shell/common/null_rasterizer.h | 1 + engine/src/flutter/shell/common/surface.h | 2 ++ engine/src/flutter/shell/gpu/gpu_rasterizer.cc | 5 ++++- engine/src/flutter/shell/gpu/gpu_surface_gl.cc | 4 ++++ engine/src/flutter/shell/gpu/gpu_surface_gl.h | 2 ++ .../src/flutter/shell/platform/android/android_surface_gl.cc | 4 ++++ .../src/flutter/shell/platform/android/android_surface_gl.h | 2 ++ .../flutter/shell/platform/android/platform_view_android.cc | 1 + 9 files changed, 25 insertions(+), 1 deletion(-) diff --git a/engine/src/flutter/shell/common/null_rasterizer.cc b/engine/src/flutter/shell/common/null_rasterizer.cc index f428bc891b..9f3dc88ef3 100644 --- a/engine/src/flutter/shell/common/null_rasterizer.cc +++ b/engine/src/flutter/shell/common/null_rasterizer.cc @@ -12,12 +12,17 @@ void NullRasterizer::Setup( std::unique_ptr surface_or_null, ftl::Closure rasterizer_continuation, ftl::AutoResetWaitableEvent* setup_completion_event) { + surface_ = std::move(surface_or_null); rasterizer_continuation(); setup_completion_event->Signal(); } void NullRasterizer::Teardown( ftl::AutoResetWaitableEvent* teardown_completion_event) { + if (surface_) { + surface_->Teardown(); + surface_.reset(); + } teardown_completion_event->Signal(); } diff --git a/engine/src/flutter/shell/common/null_rasterizer.h b/engine/src/flutter/shell/common/null_rasterizer.h index 99ff59b0e1..4b120a9d30 100644 --- a/engine/src/flutter/shell/common/null_rasterizer.h +++ b/engine/src/flutter/shell/common/null_rasterizer.h @@ -32,6 +32,7 @@ class NullRasterizer : public Rasterizer { private: ftl::WeakPtrFactory weak_factory_; + std::unique_ptr surface_; FTL_DISALLOW_COPY_AND_ASSIGN(NullRasterizer); }; diff --git a/engine/src/flutter/shell/common/surface.h b/engine/src/flutter/shell/common/surface.h index 15052911c8..0aaf1df7b7 100644 --- a/engine/src/flutter/shell/common/surface.h +++ b/engine/src/flutter/shell/common/surface.h @@ -39,6 +39,8 @@ class Surface { virtual bool Setup() = 0; + virtual void Teardown() = 0; + virtual bool IsValid() = 0; virtual std::unique_ptr AcquireFrame(const SkISize& size) = 0; diff --git a/engine/src/flutter/shell/gpu/gpu_rasterizer.cc b/engine/src/flutter/shell/gpu/gpu_rasterizer.cc index 6c683905ec..03c55e3853 100644 --- a/engine/src/flutter/shell/gpu/gpu_rasterizer.cc +++ b/engine/src/flutter/shell/gpu/gpu_rasterizer.cc @@ -65,7 +65,10 @@ void GPURasterizer::Clear(SkColor color, const SkISize& size) { void GPURasterizer::Teardown( ftl::AutoResetWaitableEvent* teardown_completion_event) { - surface_.reset(); + if (surface_) { + surface_->Teardown(); + surface_.reset(); + } last_layer_tree_.reset(); compositor_context_.OnGrContextDestroyed(); teardown_completion_event->Signal(); diff --git a/engine/src/flutter/shell/gpu/gpu_surface_gl.cc b/engine/src/flutter/shell/gpu/gpu_surface_gl.cc index 814b5296fb..50d6d1244c 100644 --- a/engine/src/flutter/shell/gpu/gpu_surface_gl.cc +++ b/engine/src/flutter/shell/gpu/gpu_surface_gl.cc @@ -94,6 +94,10 @@ bool GPUSurfaceGL::Setup() { return true; } +void GPUSurfaceGL::Teardown() { + delegate_->GLContextClearCurrent(); +} + bool GPUSurfaceGL::IsValid() { return context_ != nullptr; } diff --git a/engine/src/flutter/shell/gpu/gpu_surface_gl.h b/engine/src/flutter/shell/gpu/gpu_surface_gl.h index a29d0ff487..707cbf1498 100644 --- a/engine/src/flutter/shell/gpu/gpu_surface_gl.h +++ b/engine/src/flutter/shell/gpu/gpu_surface_gl.h @@ -31,6 +31,8 @@ class GPUSurfaceGL : public Surface { bool Setup() override; + void Teardown() override; + bool IsValid() override; std::unique_ptr AcquireFrame(const SkISize& size) override; 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 25e91a44ad..b5a7677b33 100644 --- a/engine/src/flutter/shell/platform/android/android_surface_gl.cc +++ b/engine/src/flutter/shell/platform/android/android_surface_gl.cc @@ -79,6 +79,10 @@ bool AndroidSurfaceGL::SetNativeWindowForOnScreenContext( return true; } +void AndroidSurfaceGL::TeardownOnScreenContext() { + onscreen_context_ = nullptr; +} + bool AndroidSurfaceGL::IsValid() const { if (!onscreen_context_ || !offscreen_context_) { return false; 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 e0f9f180fe..c349582462 100644 --- a/engine/src/flutter/shell/platform/android/android_surface_gl.h +++ b/engine/src/flutter/shell/platform/android/android_surface_gl.h @@ -24,6 +24,8 @@ class AndroidSurfaceGL : public GPUSurfaceGLDelegate { AndroidNativeWindow window, PlatformView::SurfaceConfig onscreen_config); + void TeardownOnScreenContext(); + bool IsValid() const; SkISize OnScreenSurfaceSize() const; 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 42403c1f0b..8dd350f5ef 100644 --- a/engine/src/flutter/shell/platform/android/platform_view_android.cc +++ b/engine/src/flutter/shell/platform/android/platform_view_android.cc @@ -319,6 +319,7 @@ void PlatformViewAndroid::SetSemanticsEnabled(JNIEnv* env, void PlatformViewAndroid::ReleaseSurface() { NotifyDestroyed(); + surface_gl_->TeardownOnScreenContext(); } VsyncWaiter* PlatformViewAndroid::GetVsyncWaiter() {