From 6e425044710f16c342be16ec6a067061e0ba8f74 Mon Sep 17 00:00:00 2001 From: Christoph Purrer Date: Fri, 3 Jan 2025 16:53:22 -0800 Subject: [PATCH] Stop all surfaces on SurfaceManager destruction (#48481) Summary: [Changelog] [Internal] - Stop all surfaces on SurfaceManager destruction Reviewed By: zeyap Differential Revision: D67809574 --- .../react/renderer/scheduler/SurfaceManager.cpp | 17 +++++++++++++++++ .../react/renderer/scheduler/SurfaceManager.h | 3 +++ 2 files changed, 20 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..0dd2759062262f 100644 --- a/packages/react-native/ReactCommon/react/renderer/scheduler/SurfaceManager.cpp +++ b/packages/react-native/ReactCommon/react/renderer/scheduler/SurfaceManager.cpp @@ -14,6 +14,10 @@ namespace facebook::react { SurfaceManager::SurfaceManager(const Scheduler& scheduler) noexcept : scheduler_(scheduler) {} +SurfaceManager::~SurfaceManager() noexcept { + stopAllSurfaces(); +} + void SurfaceManager::startSurface( SurfaceId surfaceId, const std::string& moduleName, @@ -58,6 +62,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,