From 6abcca837461cb14d1c83e939dccf31d87d99f6b Mon Sep 17 00:00:00 2001 From: Christoph Purrer Date: Fri, 3 Jan 2025 19:58:06 -0800 Subject: [PATCH] Stop all surfaces on SurfaceManager destruction (#48481) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/48481 [Changelog] [Internal] - Stop all surfaces on SurfaceManager destruction Reviewed By: zeyap Differential Revision: D67809574 fbshipit-source-id: 150ddd091e6822c3a40f8ea413aa65da50445ede --- .../renderer/scheduler/SurfaceManager.cpp | 20 +++++++++++++++++++ .../react/renderer/scheduler/SurfaceManager.h | 3 +++ 2 files changed, 23 insertions(+) diff --git a/packages/react-native/ReactCommon/react/renderer/scheduler/SurfaceManager.cpp b/packages/react-native/ReactCommon/react/renderer/scheduler/SurfaceManager.cpp index 65e3548ccb2410..5e280a68349c07 100644 --- a/packages/react-native/ReactCommon/react/renderer/scheduler/SurfaceManager.cpp +++ b/packages/react-native/ReactCommon/react/renderer/scheduler/SurfaceManager.cpp @@ -7,6 +7,7 @@ #include "SurfaceManager.h" +#include #include namespace facebook::react { @@ -14,6 +15,12 @@ namespace facebook::react { SurfaceManager::SurfaceManager(const Scheduler& scheduler) noexcept : scheduler_(scheduler) {} +SurfaceManager::~SurfaceManager() noexcept { + LOG(WARNING) << "SurfaceManager::~SurfaceManager() was called (address: " + << this << ")."; + stopAllSurfaces(); +} + void SurfaceManager::startSurface( SurfaceId surfaceId, const std::string& moduleName, @@ -58,6 +65,19 @@ void SurfaceManager::stopSurface(SurfaceId surfaceId) const noexcept { } } +void SurfaceManager::stopAllSurfaces() const noexcept { + std::unordered_set surfaceIds; + { + std::shared_lock lock(mutex_); + for (const auto& [surfaceId, _] : registry_) { + surfaceIds.insert(surfaceId); + } + } + for (const auto& surfaceId : surfaceIds) { + stopSurface(surfaceId); + } +} + Size SurfaceManager::measureSurface( SurfaceId surfaceId, const LayoutConstraints& layoutConstraints, diff --git a/packages/react-native/ReactCommon/react/renderer/scheduler/SurfaceManager.h b/packages/react-native/ReactCommon/react/renderer/scheduler/SurfaceManager.h index db6a7499243293..fd60f788cc0127 100644 --- a/packages/react-native/ReactCommon/react/renderer/scheduler/SurfaceManager.h +++ b/packages/react-native/ReactCommon/react/renderer/scheduler/SurfaceManager.h @@ -27,6 +27,7 @@ namespace facebook::react { class SurfaceManager final { public: explicit SurfaceManager(const Scheduler& scheduler) noexcept; + ~SurfaceManager() noexcept; #pragma mark - Surface Management @@ -44,6 +45,8 @@ class SurfaceManager final { void stopSurface(SurfaceId surfaceId) const noexcept; + void stopAllSurfaces() const noexcept; + Size measureSurface( SurfaceId surfaceId, const LayoutConstraints& layoutConstraints,