diff --git a/packages/skia/ios/RNSkia-iOS/SkiaDomView.mm b/packages/skia/ios/RNSkia-iOS/SkiaDomView.mm index 1f1bdb56d3..bc0831a057 100644 --- a/packages/skia/ios/RNSkia-iOS/SkiaDomView.mm +++ b/packages/skia/ios/RNSkia-iOS/SkiaDomView.mm @@ -24,10 +24,10 @@ @implementation SkiaDomView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { - auto skManager = [[self skiaManager] skManager]; - // Pass SkManager as a raw pointer to avoid circular dependenciesr + // Pass SkManager as a raw pointer to avoid circular dependencies + auto skManager = [SkiaManager latestActiveSkManager].get(); [self - initCommon:skManager.get() + initCommon:skManager factory:[](std::shared_ptr context) { return std::make_shared>(context); }]; diff --git a/packages/skia/ios/RNSkia-iOS/SkiaDomViewManager.mm b/packages/skia/ios/RNSkia-iOS/SkiaDomViewManager.mm index 30f53eba76..d77f3b83d5 100644 --- a/packages/skia/ios/RNSkia-iOS/SkiaDomViewManager.mm +++ b/packages/skia/ios/RNSkia-iOS/SkiaDomViewManager.mm @@ -15,7 +15,8 @@ @implementation SkiaDomViewManager RCT_EXPORT_MODULE(SkiaDomView) - (SkiaManager *)skiaManager { - auto bridge = [RCTBridge currentBridge]; + auto bridge = self.bridge; + RCTAssert(bridge, @"Bridge must not be nil."); auto skiaModule = (RNSkiaModule *)[bridge moduleForName:@"RNSkiaModule"]; return [skiaModule manager]; } diff --git a/packages/skia/ios/RNSkia-iOS/SkiaManager.h b/packages/skia/ios/RNSkia-iOS/SkiaManager.h index 8f5457f7de..07d41dc18d 100644 --- a/packages/skia/ios/RNSkia-iOS/SkiaManager.h +++ b/packages/skia/ios/RNSkia-iOS/SkiaManager.h @@ -16,4 +16,10 @@ jsInvoker:(std::shared_ptr) jsInvoker; +#ifdef RCT_NEW_ARCH_ENABLED +// Fabric components do not have a better way to interact with TurboModules. +// Workaround to get the SkManager instance from singleton. ++ (std::shared_ptr)latestActiveSkManager; +#endif // RCT_NEW_ARCH_ENABLED + @end diff --git a/packages/skia/ios/RNSkia-iOS/SkiaManager.mm b/packages/skia/ios/RNSkia-iOS/SkiaManager.mm index a523260dde..b5d7ecc78b 100644 --- a/packages/skia/ios/RNSkia-iOS/SkiaManager.mm +++ b/packages/skia/ios/RNSkia-iOS/SkiaManager.mm @@ -8,6 +8,8 @@ #import "RNSkiOSPlatformContext.h" +static __weak SkiaManager *sharedInstance = nil; + @implementation SkiaManager { std::shared_ptr _skManager; __weak RCTBridge *weakBridge; @@ -29,6 +31,7 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge jsInvoker { self = [super init]; if (self) { + sharedInstance = self; RCTCxxBridge *cxxBridge = (RCTCxxBridge *)bridge; if (cxxBridge.runtime) { @@ -44,4 +47,18 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge return self; } +- (void)dealloc +{ + sharedInstance = nil; +} + +#ifdef RCT_NEW_ARCH_ENABLED ++ (std::shared_ptr)latestActiveSkManager +{ + if (sharedInstance != nil) { + return [sharedInstance skManager]; + } + return nullptr; +} +#endif // RCT_NEW_ARCH_ENABLED @end diff --git a/packages/skia/ios/RNSkia-iOS/SkiaPictureView.mm b/packages/skia/ios/RNSkia-iOS/SkiaPictureView.mm index 9bf2f61eeb..b0730f6238 100644 --- a/packages/skia/ios/RNSkia-iOS/SkiaPictureView.mm +++ b/packages/skia/ios/RNSkia-iOS/SkiaPictureView.mm @@ -24,9 +24,9 @@ @implementation SkiaPictureView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { - auto skManager = [[self skiaManager] skManager]; - // Pass SkManager as a raw pointer to avoid circular dependenciesr - [self initCommon:skManager.get() + // Pass SkManager as a raw pointer to avoid circular dependencies + auto skManager = [SkiaManager latestActiveSkManager].get(); + [self initCommon:skManager factory:[](std::shared_ptr context) { return std::make_shared>( context); diff --git a/packages/skia/ios/RNSkia-iOS/SkiaPictureViewManager.mm b/packages/skia/ios/RNSkia-iOS/SkiaPictureViewManager.mm index 8ebccb1cce..c123db1d22 100644 --- a/packages/skia/ios/RNSkia-iOS/SkiaPictureViewManager.mm +++ b/packages/skia/ios/RNSkia-iOS/SkiaPictureViewManager.mm @@ -15,7 +15,8 @@ @implementation SkiaPictureViewManager RCT_EXPORT_MODULE(SkiaPictureView) - (SkiaManager *)skiaManager { - auto bridge = [RCTBridge currentBridge]; + auto bridge = self.bridge; + RCTAssert(bridge, @"Bridge must not be nil."); auto skiaModule = (RNSkiaModule *)[bridge moduleForName:@"RNSkiaModule"]; return [skiaModule manager]; } diff --git a/packages/skia/ios/RNSkia-iOS/SkiaUIView.h b/packages/skia/ios/RNSkia-iOS/SkiaUIView.h index 8335d424fb..c3fb2e2daf 100644 --- a/packages/skia/ios/RNSkia-iOS/SkiaUIView.h +++ b/packages/skia/ios/RNSkia-iOS/SkiaUIView.h @@ -29,7 +29,6 @@ factory:(std::function( std::shared_ptr)>)factory; - (std::shared_ptr)impl; -- (SkiaManager *)skiaManager; - (void)setDebugMode:(bool)debugMode; - (void)setOpaque:(bool)opaque; diff --git a/packages/skia/ios/RNSkia-iOS/SkiaUIView.mm b/packages/skia/ios/RNSkia-iOS/SkiaUIView.mm index b257b425d4..22ab6f9d0f 100644 --- a/packages/skia/ios/RNSkia-iOS/SkiaUIView.mm +++ b/packages/skia/ios/RNSkia-iOS/SkiaUIView.mm @@ -48,12 +48,6 @@ - (void)initCommon:(RNSkia::RNSkManager *)manager _factory = factory; } -- (SkiaManager *)skiaManager { - auto bridge = [RCTBridge currentBridge]; - auto skiaModule = (RNSkiaModule *)[bridge moduleForName:@"RNSkiaModule"]; - return [skiaModule manager]; -} - - (void)willInvalidateModules { _impl = nullptr; _manager = nullptr; @@ -114,7 +108,7 @@ - (void)finalizeUpdates:(RNComponentViewUpdateMask)updateMask { // this flag is only set when the view is inserted and we want to set the // manager here since the view could be recycled or the app could be // refreshed and we would have a stale manager then - _manager = [[self skiaManager] skManager].get(); + _manager = [SkiaManager latestActiveSkManager].get(); } } #endif // RCT_NEW_ARCH_ENABLED