From 80c707a6422e327182f09bc741149a695692b9f6 Mon Sep 17 00:00:00 2001 From: Chris Yang Date: Tue, 19 May 2020 16:36:17 -0700 Subject: [PATCH] Introduce `TaskRunnerAffineWeakPtrFactory` to generate `TaskRunnerAffineWeakPtr`s (flutter/engine#18346) --- engine/src/flutter/fml/memory/weak_ptr.h | 83 +++++++++++++------ .../flutter/fml/memory/weak_ptr_unittest.cc | 4 +- engine/src/flutter/shell/common/rasterizer.cc | 7 +- engine/src/flutter/shell/common/rasterizer.h | 4 +- engine/src/flutter/shell/common/shell.cc | 4 +- engine/src/flutter/shell/common/shell.h | 2 +- .../src/flutter/shell/gpu/gpu_surface_gl.cc | 4 +- engine/src/flutter/shell/gpu/gpu_surface_gl.h | 2 +- .../flutter/shell/gpu/gpu_surface_software.cc | 4 +- .../flutter/shell/gpu/gpu_surface_software.h | 2 +- 10 files changed, 74 insertions(+), 42 deletions(-) diff --git a/engine/src/flutter/fml/memory/weak_ptr.h b/engine/src/flutter/fml/memory/weak_ptr.h index 95fc964372..1f7293014c 100644 --- a/engine/src/flutter/fml/memory/weak_ptr.h +++ b/engine/src/flutter/fml/memory/weak_ptr.h @@ -30,17 +30,18 @@ struct DebugTaskRunnerChecker { template class WeakPtrFactory; -// Class for "weak pointers" that can be invalidated. Valid weak pointers can -// only originate from a |WeakPtrFactory| (see below), though weak pointers are -// copyable and movable. +// Class for "weak pointers" that can be invalidated. Valid weak pointers +// can only originate from a |WeakPtrFactory| (see below), though weak +// pointers are copyable and movable. // -// Weak pointers are not in general thread-safe. They may only be *used* on a -// single thread, namely the same thread as the "originating" |WeakPtrFactory| -// (which can invalidate the weak pointers that it generates). +// Weak pointers are not in general thread-safe. They may only be *used* on +// a single thread, namely the same thread as the "originating" +// |WeakPtrFactory| (which can invalidate the weak pointers that it +// generates). // // However, weak pointers may be passed to other threads, reset on other -// threads, or destroyed on other threads. They may also be reassigned on other -// threads (in which case they should then only be used on the thread +// threads, or destroyed on other threads. They may also be reassigned on +// other threads (in which case they should then only be used on the thread // corresponding to the new "originating" |WeakPtrFactory|). template class WeakPtr { @@ -117,9 +118,6 @@ class WeakPtr { explicit WeakPtr(T* ptr, fml::RefPtr&& flag) : ptr_(ptr), flag_(std::move(flag)) {} - T* ptr_; - fml::RefPtr flag_; - virtual void CheckThreadSafety() const { FML_DCHECK_CREATION_THREAD_IS_CURRENT(checker_.checker); } @@ -134,12 +132,17 @@ class WeakPtr { fml::RefPtr&& flag, DebugThreadChecker checker) : ptr_(ptr), flag_(std::move(flag)), checker_(checker) {} - + T* ptr_; + fml::RefPtr flag_; DebugThreadChecker checker_; // Copy/move construction/assignment supported. }; +// Forward declaration, so |TaskRunnerAffineWeakPtr| can friend it. +template +class TaskRunnerAffineWeakPtrFactory; + // A weak pointer that can be used in different threads as long as // the threads are belong to the same |TaskRunner|. // @@ -153,14 +156,13 @@ class TaskRunnerAffineWeakPtr : public WeakPtr { template TaskRunnerAffineWeakPtr(const TaskRunnerAffineWeakPtr& r) - : WeakPtr(static_cast(r.ptr_), r.flag_), checker_(r.checker_) {} + : WeakPtr(r), checker_(r.checker_) {} TaskRunnerAffineWeakPtr(TaskRunnerAffineWeakPtr&& r) = default; template TaskRunnerAffineWeakPtr(TaskRunnerAffineWeakPtr&& r) - : WeakPtr(static_cast(r.ptr_), std::move(r.flag_)), - checker_(r.checker_) {} + : WeakPtr(r), checker_(r.checker_) {} ~TaskRunnerAffineWeakPtr() = default; @@ -176,7 +178,9 @@ class TaskRunnerAffineWeakPtr : public WeakPtr { } private: - friend class WeakPtrFactory; + template + friend class TaskRunnerAffineWeakPtr; + friend class TaskRunnerAffineWeakPtrFactory; explicit TaskRunnerAffineWeakPtr( T* ptr, @@ -248,14 +252,7 @@ class WeakPtrFactory { return WeakPtr(ptr_, flag_.Clone(), checker_); } - // Gets a new weak pointer, which will be valid until either - // |InvalidateWeakPtrs()| is called or this object is destroyed. - TaskRunnerAffineWeakPtr GetTaskRunnerAffineWeakPtr() const { - return TaskRunnerAffineWeakPtr(ptr_, flag_.Clone(), - task_runner_checker_); - } - - protected: + private: // Note: See weak_ptr_internal.h for an explanation of why we store the // pointer here, instead of in the "flag". T* const ptr_; @@ -265,13 +262,47 @@ class WeakPtrFactory { FML_DCHECK_CREATION_THREAD_IS_CURRENT(checker_.checker); } - private: DebugThreadChecker checker_; - DebugTaskRunnerChecker task_runner_checker_; FML_DISALLOW_COPY_AND_ASSIGN(WeakPtrFactory); }; +// A type of |WeakPtrFactory| that produces |TaskRunnerAffineWeakPtr| instead of +// |WeakPtr|. +template +class TaskRunnerAffineWeakPtrFactory { + public: + explicit TaskRunnerAffineWeakPtrFactory(T* ptr) + : ptr_(ptr), flag_(fml::MakeRefCounted()) { + FML_DCHECK(ptr_); + } + + ~TaskRunnerAffineWeakPtrFactory() { + CheckThreadSafety(); + flag_->Invalidate(); + } + + // Gets a new weak pointer, which will be valid until either + // |InvalidateWeakPtrs()| is called or this object is destroyed. + TaskRunnerAffineWeakPtr GetWeakPtr() const { + return TaskRunnerAffineWeakPtr(ptr_, flag_.Clone(), checker_); + } + + private: + // Note: See weak_ptr_internal.h for an explanation of why we store the + // pointer here, instead of in the "flag". + T* const ptr_; + fml::RefPtr flag_; + + void CheckThreadSafety() const { + FML_DCHECK_TASK_RUNNER_IS_CURRENT(checker_.checker); + } + + DebugTaskRunnerChecker checker_; + + FML_DISALLOW_COPY_AND_ASSIGN(TaskRunnerAffineWeakPtrFactory); +}; + } // namespace fml #endif // FLUTTER_FML_MEMORY_WEAK_PTR_H_ diff --git a/engine/src/flutter/fml/memory/weak_ptr_unittest.cc b/engine/src/flutter/fml/memory/weak_ptr_unittest.cc index 42de850356..86604f1a60 100644 --- a/engine/src/flutter/fml/memory/weak_ptr_unittest.cc +++ b/engine/src/flutter/fml/memory/weak_ptr_unittest.cc @@ -188,13 +188,13 @@ TEST(TaskRunnerAffineWeakPtrTest, ShouldNotCrashIfRunningOnTheSameTaskRunner) { &loop2_task_start_latch]() { fml::MessageLoop::EnsureInitializedForCurrentThread(); int data = 0; - WeakPtrFactory factory(&data); + TaskRunnerAffineWeakPtrFactory factory(&data); loop2 = &fml::MessageLoop::GetCurrent(); loop2->GetTaskRunner()->PostTask([&]() { latch2.Signal(); loop2_task_start_latch.Wait(); - TaskRunnerAffineWeakPtr ptr = factory.GetTaskRunnerAffineWeakPtr(); + TaskRunnerAffineWeakPtr ptr = factory.GetWeakPtr(); EXPECT_EQ(*ptr, data); loop2_task_finish_latch.Signal(); }); diff --git a/engine/src/flutter/shell/common/rasterizer.cc b/engine/src/flutter/shell/common/rasterizer.cc index 74329c941b..4f619ad303 100644 --- a/engine/src/flutter/shell/common/rasterizer.cc +++ b/engine/src/flutter/shell/common/rasterizer.cc @@ -54,10 +54,11 @@ Rasterizer::Rasterizer( Rasterizer::~Rasterizer() = default; fml::TaskRunnerAffineWeakPtr Rasterizer::GetWeakPtr() const { - return weak_factory_.GetTaskRunnerAffineWeakPtr(); + return weak_factory_.GetWeakPtr(); } -fml::WeakPtr Rasterizer::GetSnapshotDelegate() const { +fml::TaskRunnerAffineWeakPtr Rasterizer::GetSnapshotDelegate() + const { return weak_factory_.GetWeakPtr(); } @@ -155,7 +156,7 @@ void Rasterizer::Draw(fml::RefPtr> pipeline) { switch (consume_result) { case PipelineConsumeResult::MoreAvailable: { task_runners_.GetRasterTaskRunner()->PostTask( - [weak_this = weak_factory_.GetTaskRunnerAffineWeakPtr(), pipeline]() { + [weak_this = weak_factory_.GetWeakPtr(), pipeline]() { if (weak_this) { weak_this->Draw(pipeline); } diff --git a/engine/src/flutter/shell/common/rasterizer.h b/engine/src/flutter/shell/common/rasterizer.h index 6de1a843bd..a254d84994 100644 --- a/engine/src/flutter/shell/common/rasterizer.h +++ b/engine/src/flutter/shell/common/rasterizer.h @@ -186,7 +186,7 @@ class Rasterizer final : public SnapshotDelegate { /// fml::TaskRunnerAffineWeakPtr GetWeakPtr() const; - fml::WeakPtr GetSnapshotDelegate() const; + fml::TaskRunnerAffineWeakPtr GetSnapshotDelegate() const; //---------------------------------------------------------------------------- /// @brief Sometimes, it may be necessary to render the same frame again @@ -430,7 +430,7 @@ class Rasterizer final : public SnapshotDelegate { fml::closure next_frame_callback_; bool user_override_resource_cache_bytes_; std::optional max_cache_bytes_; - fml::WeakPtrFactory weak_factory_; + fml::TaskRunnerAffineWeakPtrFactory weak_factory_; fml::RefPtr raster_thread_merger_; // |SnapshotDelegate| diff --git a/engine/src/flutter/shell/common/shell.cc b/engine/src/flutter/shell/common/shell.cc index d83fadbb4b..fd5a581647 100644 --- a/engine/src/flutter/shell/common/shell.cc +++ b/engine/src/flutter/shell/common/shell.cc @@ -338,7 +338,7 @@ Shell::Shell(DartVMRef vm, TaskRunners task_runners, Settings settings) fml::TaskRunner::RunNowOrPostTask( task_runners_.GetRasterTaskRunner(), fml::MakeCopyable([this]() mutable { this->weak_factory_gpu_ = - std::make_unique>(this); + std::make_unique>(this); })); // Install service protocol handlers. @@ -1156,7 +1156,7 @@ void Shell::OnFrameRasterized(const FrameTiming& timing) { // never be reported until the next animation starts. frame_timings_report_scheduled_ = true; task_runners_.GetRasterTaskRunner()->PostDelayedTask( - [self = weak_factory_gpu_->GetTaskRunnerAffineWeakPtr()]() { + [self = weak_factory_gpu_->GetWeakPtr()]() { if (!self.get()) { return; } diff --git a/engine/src/flutter/shell/common/shell.h b/engine/src/flutter/shell/common/shell.h index a39884a823..d959eb41c5 100644 --- a/engine/src/flutter/shell/common/shell.h +++ b/engine/src/flutter/shell/common/shell.h @@ -580,7 +580,7 @@ class Shell final : public PlatformView::Delegate, // For accessing the Shell via the raster thread, necessary for various // rasterizer callbacks. - std::unique_ptr> weak_factory_gpu_; + std::unique_ptr> weak_factory_gpu_; friend class testing::ShellTest; diff --git a/engine/src/flutter/shell/gpu/gpu_surface_gl.cc b/engine/src/flutter/shell/gpu/gpu_surface_gl.cc index 14d4f03b23..e5fbf00a24 100644 --- a/engine/src/flutter/shell/gpu/gpu_surface_gl.cc +++ b/engine/src/flutter/shell/gpu/gpu_surface_gl.cc @@ -257,8 +257,8 @@ std::unique_ptr GPUSurfaceGL::AcquireFrame(const SkISize& size) { surface->getCanvas()->setMatrix(root_surface_transformation); SurfaceFrame::SubmitCallback submit_callback = - [weak = weak_factory_.GetTaskRunnerAffineWeakPtr()]( - const SurfaceFrame& surface_frame, SkCanvas* canvas) { + [weak = weak_factory_.GetWeakPtr()](const SurfaceFrame& surface_frame, + SkCanvas* canvas) { return weak ? weak->PresentSurface(canvas) : false; }; diff --git a/engine/src/flutter/shell/gpu/gpu_surface_gl.h b/engine/src/flutter/shell/gpu/gpu_surface_gl.h index 0a0806040a..d299e572f4 100644 --- a/engine/src/flutter/shell/gpu/gpu_surface_gl.h +++ b/engine/src/flutter/shell/gpu/gpu_surface_gl.h @@ -58,7 +58,7 @@ class GPUSurfaceGL : public Surface { // external view embedder is present. const bool render_to_surface_; bool valid_ = false; - fml::WeakPtrFactory weak_factory_; + fml::TaskRunnerAffineWeakPtrFactory weak_factory_; bool CreateOrUpdateSurfaces(const SkISize& size); diff --git a/engine/src/flutter/shell/gpu/gpu_surface_software.cc b/engine/src/flutter/shell/gpu/gpu_surface_software.cc index f15a1fc0c3..8b2f664d18 100644 --- a/engine/src/flutter/shell/gpu/gpu_surface_software.cc +++ b/engine/src/flutter/shell/gpu/gpu_surface_software.cc @@ -57,8 +57,8 @@ std::unique_ptr GPUSurfaceSoftware::AcquireFrame( canvas->resetMatrix(); SurfaceFrame::SubmitCallback on_submit = - [self = weak_factory_.GetTaskRunnerAffineWeakPtr()]( - const SurfaceFrame& surface_frame, SkCanvas* canvas) -> bool { + [self = weak_factory_.GetWeakPtr()](const SurfaceFrame& surface_frame, + SkCanvas* canvas) -> bool { // If the surface itself went away, there is nothing more to do. if (!self || !self->IsValid() || canvas == nullptr) { return false; diff --git a/engine/src/flutter/shell/gpu/gpu_surface_software.h b/engine/src/flutter/shell/gpu/gpu_surface_software.h index af4d1cb3c1..a18f67ca8c 100644 --- a/engine/src/flutter/shell/gpu/gpu_surface_software.h +++ b/engine/src/flutter/shell/gpu/gpu_surface_software.h @@ -41,7 +41,7 @@ class GPUSurfaceSoftware : public Surface { // 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::TaskRunnerAffineWeakPtrFactory weak_factory_; FML_DISALLOW_COPY_AND_ASSIGN(GPUSurfaceSoftware); };