diff --git a/engine/src/flutter/fml/memory/weak_ptr.h b/engine/src/flutter/fml/memory/weak_ptr.h index 2b369952d7..946ab645e0 100644 --- a/engine/src/flutter/fml/memory/weak_ptr.h +++ b/engine/src/flutter/fml/memory/weak_ptr.h @@ -159,37 +159,22 @@ class WeakPtr { template class WeakPtrFactory { public: - explicit WeakPtrFactory(T* ptr) : ptr_(ptr) { FXL_DCHECK(ptr_); } + explicit WeakPtrFactory(T* ptr) + : ptr_(ptr), flag_(fxl::MakeRefCounted()) { + FXL_DCHECK(ptr_); + } - ~WeakPtrFactory() { InvalidateWeakPtrs(); } + ~WeakPtrFactory() { + FML_DCHECK_CREATION_THREAD_IS_CURRENT(checker_.checker); + flag_->Invalidate(); + } // Gets a new weak pointer, which will be valid until either // |InvalidateWeakPtrs()| is called or this object is destroyed. - WeakPtr GetWeakPtr() { - FML_DCHECK_CREATION_THREAD_IS_CURRENT(checker_.checker); - if (!flag_) - flag_ = fxl::MakeRefCounted(); + WeakPtr GetWeakPtr() const { return WeakPtr(ptr_, flag_.Clone(), checker_); } - // Call this method to invalidate all existing weak pointers. (Note that - // additional weak pointers can be produced even after this is called.) - void InvalidateWeakPtrs() { - FML_DCHECK_CREATION_THREAD_IS_CURRENT(checker_.checker); - if (!flag_) - return; - flag_->Invalidate(); - flag_ = nullptr; - } - - // Call this method to determine if any weak pointers exist. (Note that a - // "false" result is definitive, but a "true" result may not be if weak - // pointers are held/reset/destroyed/reassigned on other threads.) - bool HasWeakPtrs() const { - FML_DCHECK_CREATION_THREAD_IS_CURRENT(checker_.checker); - return flag_ && !flag_->HasOneRef(); - } - private: // Note: See weak_ptr_internal.h for an explanation of why we store the // pointer here, instead of in the "flag". diff --git a/engine/src/flutter/fml/memory/weak_ptr_unittest.cc b/engine/src/flutter/fml/memory/weak_ptr_unittest.cc index 2b6dbd035e..3e0ecdd834 100644 --- a/engine/src/flutter/fml/memory/weak_ptr_unittest.cc +++ b/engine/src/flutter/fml/memory/weak_ptr_unittest.cc @@ -162,36 +162,5 @@ TEST(WeakPtrTest, UpcastMoveAssignment) { EXPECT_EQ(&data, ptr2.get()); } -TEST(WeakPtrTest, InvalidateWeakPtrs) { - int data = 0; - WeakPtrFactory factory(&data); - WeakPtr ptr = factory.GetWeakPtr(); - EXPECT_EQ(&data, ptr.get()); - EXPECT_TRUE(factory.HasWeakPtrs()); - factory.InvalidateWeakPtrs(); - EXPECT_EQ(nullptr, ptr.get()); - EXPECT_FALSE(factory.HasWeakPtrs()); - - // Test that the factory can create new weak pointers after a - // |InvalidateWeakPtrs()| call, and that they remain valid until the next - // |InvalidateWeakPtrs()| call. - WeakPtr ptr2 = factory.GetWeakPtr(); - EXPECT_EQ(&data, ptr2.get()); - EXPECT_TRUE(factory.HasWeakPtrs()); - factory.InvalidateWeakPtrs(); - EXPECT_EQ(nullptr, ptr2.get()); - EXPECT_FALSE(factory.HasWeakPtrs()); -} - -TEST(WeakPtrTest, HasWeakPtrs) { - int data = 0; - WeakPtrFactory factory(&data); - { - WeakPtr ptr = factory.GetWeakPtr(); - EXPECT_TRUE(factory.HasWeakPtrs()); - } - EXPECT_FALSE(factory.HasWeakPtrs()); -} - } // namespace } // namespace fml diff --git a/engine/src/flutter/runtime/dart_isolate.cc b/engine/src/flutter/runtime/dart_isolate.cc index 6d69ab7fc5..59093da140 100644 --- a/engine/src/flutter/runtime/dart_isolate.cc +++ b/engine/src/flutter/runtime/dart_isolate.cc @@ -107,7 +107,6 @@ DartIsolate::DartIsolate(const DartVM* vm, isolate_snapshot_(std::move(isolate_snapshot)), weak_factory_(this) { FXL_DCHECK(isolate_snapshot_) << "Must contain a valid isolate snapshot."; - weak_prototype_ = weak_factory_.GetWeakPtr(); if (vm_ == nullptr) { return; @@ -679,7 +678,7 @@ fxl::RefPtr DartIsolate::GetIsolateSnapshot() const { } fml::WeakPtr DartIsolate::GetWeakIsolatePtr() const { - return weak_prototype_; + return weak_factory_.GetWeakPtr(); } void DartIsolate::AddIsolateShutdownCallback(fxl::Closure closure) { diff --git a/engine/src/flutter/runtime/dart_isolate.h b/engine/src/flutter/runtime/dart_isolate.h index 13d573ebe3..dd402b157c 100644 --- a/engine/src/flutter/runtime/dart_isolate.h +++ b/engine/src/flutter/runtime/dart_isolate.h @@ -103,7 +103,6 @@ class DartIsolate : public UIDartState { Phase phase_ = Phase::Unknown; const fxl::RefPtr isolate_snapshot_; std::vector> shutdown_callbacks_; - fml::WeakPtr weak_prototype_; fml::WeakPtrFactory weak_factory_; FXL_WARN_UNUSED_RESULT diff --git a/engine/src/flutter/shell/common/engine.cc b/engine/src/flutter/shell/common/engine.cc index d1231b0d09..43c2e4e5b7 100644 --- a/engine/src/flutter/shell/common/engine.cc +++ b/engine/src/flutter/shell/common/engine.cc @@ -56,8 +56,6 @@ Engine::Engine(Delegate& delegate, activity_running_(false), have_surface_(false), weak_factory_(this) { - weak_prototype_ = weak_factory_.GetWeakPtr(); - if (legacy_sky_platform_) { // TODO: Remove this legacy call along with the platform. This is what makes // the engine unable to run from multiple threads in the legacy @@ -84,7 +82,7 @@ Engine::~Engine() { } fml::WeakPtr Engine::GetWeakPtr() const { - return weak_prototype_; + return weak_factory_.GetWeakPtr(); } bool Engine::UpdateAssetManager( diff --git a/engine/src/flutter/shell/common/engine.h b/engine/src/flutter/shell/common/engine.h index ede442ee3e..dfb2ac1191 100644 --- a/engine/src/flutter/shell/common/engine.h +++ b/engine/src/flutter/shell/common/engine.h @@ -109,7 +109,6 @@ class Engine final : public blink::RuntimeDelegate { fxl::RefPtr asset_manager_; bool activity_running_; bool have_surface_; - fml::WeakPtr weak_prototype_; fml::WeakPtrFactory weak_factory_; // |blink::RuntimeDelegate| diff --git a/engine/src/flutter/shell/common/io_manager.cc b/engine/src/flutter/shell/common/io_manager.cc index 4161422978..2b4523a46f 100644 --- a/engine/src/flutter/shell/common/io_manager.cc +++ b/engine/src/flutter/shell/common/io_manager.cc @@ -49,11 +49,6 @@ IOManager::IOManager(sk_sp resource_context, "context. Async texture uploads will be disabled. " "Expect performance degradation."; } - - if (resource_context_weak_factory_) { - resource_context_weak_prototype_ = - resource_context_weak_factory_->GetWeakPtr(); - } } IOManager::~IOManager() { @@ -63,7 +58,9 @@ IOManager::~IOManager() { } fml::WeakPtr IOManager::GetResourceContext() const { - return resource_context_weak_prototype_; + return resource_context_weak_factory_ + ? resource_context_weak_factory_->GetWeakPtr() + : fml::WeakPtr(); } fxl::RefPtr IOManager::GetSkiaUnrefQueue() const { diff --git a/engine/src/flutter/shell/common/io_manager.h b/engine/src/flutter/shell/common/io_manager.h index 1077a28d0a..42ea80f10e 100644 --- a/engine/src/flutter/shell/common/io_manager.h +++ b/engine/src/flutter/shell/common/io_manager.h @@ -35,7 +35,6 @@ class IOManager { private: // Resource context management. sk_sp resource_context_; - fml::WeakPtr resource_context_weak_prototype_; std::unique_ptr> resource_context_weak_factory_; diff --git a/engine/src/flutter/shell/common/platform_view.cc b/engine/src/flutter/shell/common/platform_view.cc index b2bfce051a..53cf7b7366 100644 --- a/engine/src/flutter/shell/common/platform_view.cc +++ b/engine/src/flutter/shell/common/platform_view.cc @@ -20,9 +20,7 @@ PlatformView::PlatformView(Delegate& delegate, blink::TaskRunners task_runners) : delegate_(delegate), task_runners_(std::move(task_runners)), size_(SkISize::Make(0, 0)), - weak_factory_(this) { - weak_prototype_ = weak_factory_.GetWeakPtr(); -} + weak_factory_(this) {} PlatformView::~PlatformView() = default; @@ -73,7 +71,7 @@ sk_sp PlatformView::CreateResourceContext() const { } fml::WeakPtr PlatformView::GetWeakPtr() const { - return weak_prototype_; + return weak_factory_.GetWeakPtr(); } void PlatformView::UpdateSemantics(blink::SemanticsNodeUpdates update) {} diff --git a/engine/src/flutter/shell/common/platform_view.h b/engine/src/flutter/shell/common/platform_view.h index 56179a6114..3ae91b178c 100644 --- a/engine/src/flutter/shell/common/platform_view.h +++ b/engine/src/flutter/shell/common/platform_view.h @@ -120,7 +120,6 @@ class PlatformView { std::unique_ptr vsync_waiter_; SkISize size_; - fml::WeakPtr weak_prototype_; fml::WeakPtrFactory weak_factory_; virtual std::unique_ptr CreateRenderingSurface(); diff --git a/engine/src/flutter/shell/common/rasterizer.cc b/engine/src/flutter/shell/common/rasterizer.cc index 20fc882580..e4f6871568 100644 --- a/engine/src/flutter/shell/common/rasterizer.cc +++ b/engine/src/flutter/shell/common/rasterizer.cc @@ -16,14 +16,12 @@ namespace shell { Rasterizer::Rasterizer(blink::TaskRunners task_runners) - : task_runners_(std::move(task_runners)), weak_factory_(this) { - weak_prototype_ = weak_factory_.GetWeakPtr(); -} + : task_runners_(std::move(task_runners)), weak_factory_(this) {} Rasterizer::~Rasterizer() = default; fml::WeakPtr Rasterizer::GetWeakPtr() const { - return weak_prototype_; + return weak_factory_.GetWeakPtr(); } void Rasterizer::Setup(std::unique_ptr surface) { diff --git a/engine/src/flutter/shell/common/rasterizer.h b/engine/src/flutter/shell/common/rasterizer.h index e373b1138c..79dda4ac9c 100644 --- a/engine/src/flutter/shell/common/rasterizer.h +++ b/engine/src/flutter/shell/common/rasterizer.h @@ -65,7 +65,6 @@ class Rasterizer final { flow::CompositorContext compositor_context_; std::unique_ptr last_layer_tree_; fxl::Closure next_frame_callback_; - fml::WeakPtr weak_prototype_; fml::WeakPtrFactory weak_factory_; void DoDraw(std::unique_ptr layer_tree);