diff --git a/packages/react-native/Libraries/Image/RCTImageLoader.mm b/packages/react-native/Libraries/Image/RCTImageLoader.mm index 5cba225f016..4e03352a53a 100644 --- a/packages/react-native/Libraries/Image/RCTImageLoader.mm +++ b/packages/react-native/Libraries/Image/RCTImageLoader.mm @@ -68,8 +68,8 @@ static NSError *addResponseHeadersToError(NSError *originalError, NSHTTPURLRespo @end @implementation RCTImageLoader { - std::atomic _isLoaderSetup; - std::mutex _loaderSetupLock; + std::atomic _didSetup; + std::mutex _setupLock; NSArray> * (^_loadersProvider)(RCTModuleRegistry *); NSArray> * (^_decodersProvider)(RCTModuleRegistry *); NSArray> *_loaders; @@ -82,7 +82,6 @@ static NSError *addResponseHeadersToError(NSError *originalError, NSHTTPURLRespo NSMutableArray *_pendingDecodes; NSInteger _scheduledDecodes; NSUInteger _activeBytes; - std::mutex _loadersMutex; __weak id _redirectDelegate; } @@ -108,7 +107,7 @@ RCT_EXPORT_MODULE() { if (self = [super init]) { _redirectDelegate = redirectDelegate; - _isLoaderSetup = NO; + _didSetup = NO; } return self; } @@ -126,15 +125,56 @@ RCT_EXPORT_MODULE() - (void)setUp { - std::lock_guard guard(_loaderSetupLock); - if (!_isLoaderSetup) { + std::lock_guard guard(_setupLock); + if (!_didSetup) { // Set defaults _maxConcurrentLoadingTasks = _maxConcurrentLoadingTasks ?: 4; _maxConcurrentDecodingTasks = _maxConcurrentDecodingTasks ?: 2; _maxConcurrentDecodingBytes = _maxConcurrentDecodingBytes ?: 30 * 1024 * 1024; // 30MB _URLRequestQueue = dispatch_queue_create("com.facebook.react.ImageLoaderURLRequestQueue", DISPATCH_QUEUE_SERIAL); - _isLoaderSetup = YES; + + // Get loaders, sorted in reverse priority order (highest priority first) + if (_loadersProvider != nil) { + _loaders = _loadersProvider(self.moduleRegistry); + } else { + RCTAssert(_bridge, @"Trying to find RCTImageURLLoaders and bridge not set."); + _loaders = [_bridge modulesConformingToProtocol:@protocol(RCTImageURLLoader)]; + } + _loaders = + [_loaders sortedArrayUsingComparator:^NSComparisonResult(id a, id b) { + float priorityA = [a respondsToSelector:@selector(loaderPriority)] ? [a loaderPriority] : 0; + float priorityB = [b respondsToSelector:@selector(loaderPriority)] ? [b loaderPriority] : 0; + if (priorityA > priorityB) { + return NSOrderedAscending; + } else if (priorityA < priorityB) { + return NSOrderedDescending; + } else { + return NSOrderedSame; + } + }]; + + // Get decoders, sorted in reverse priority order (highest priority first) + if (_decodersProvider != nil) { + _decoders = _decodersProvider(self.moduleRegistry); + } else { + RCTAssert(_bridge, @"Trying to find RCTImageDataDecoders and bridge not set."); + _decoders = [_bridge modulesConformingToProtocol:@protocol(RCTImageDataDecoder)]; + } + _decoders = [_decoders + sortedArrayUsingComparator:^NSComparisonResult(id a, id b) { + float priorityA = [a respondsToSelector:@selector(decoderPriority)] ? [a decoderPriority] : 0; + float priorityB = [b respondsToSelector:@selector(decoderPriority)] ? [b decoderPriority] : 0; + if (priorityA > priorityB) { + return NSOrderedAscending; + } else if (priorityA < priorityB) { + return NSOrderedDescending; + } else { + return NSOrderedSame; + } + }]; + + _didSetup = YES; } } @@ -163,41 +203,17 @@ RCT_EXPORT_MODULE() - (id)imageURLLoaderForURL:(NSURL *)URL { - if (!_isLoaderSetup) { + if (!_didSetup) { [self setUp]; } - if (!_loaders) { - std::unique_lock guard(_loadersMutex); - if (!_loaders) { - // Get loaders, sorted in reverse priority order (highest priority first) - if (_loadersProvider) { - _loaders = _loadersProvider(self.moduleRegistry); - } else { - RCTAssert(_bridge, @"Trying to find RCTImageURLLoaders and bridge not set."); - _loaders = [_bridge modulesConformingToProtocol:@protocol(RCTImageURLLoader)]; - } - - _loaders = - [_loaders sortedArrayUsingComparator:^NSComparisonResult(id a, id b) { - float priorityA = [a respondsToSelector:@selector(loaderPriority)] ? [a loaderPriority] : 0; - float priorityB = [b respondsToSelector:@selector(loaderPriority)] ? [b loaderPriority] : 0; - if (priorityA > priorityB) { - return NSOrderedAscending; - } else if (priorityA < priorityB) { - return NSOrderedDescending; - } else { - return NSOrderedSame; - } - }]; - } - } + NSArray> *loaders = _loaders; if (RCT_DEBUG) { // Check for handler conflicts float previousPriority = 0; id previousLoader = nil; - for (id loader in _loaders) { + for (id loader in loaders) { float priority = [loader respondsToSelector:@selector(loaderPriority)] ? [loader loaderPriority] : 0; if (previousLoader && priority < previousPriority) { return previousLoader; @@ -224,7 +240,7 @@ RCT_EXPORT_MODULE() } // Normal code path - for (id loader in _loaders) { + for (id loader in loaders) { if ([loader canLoadImageURL:URL]) { return loader; } @@ -236,39 +252,17 @@ RCT_EXPORT_MODULE() - (id)imageDataDecoderForData:(NSData *)data { - if (!_isLoaderSetup) { + if (!_didSetup) { [self setUp]; } - if (!_decoders) { - // Get decoders, sorted in reverse priority order (highest priority first) - - if (_decodersProvider) { - _decoders = _decodersProvider(self.moduleRegistry); - } else { - RCTAssert(_bridge, @"Trying to find RCTImageDataDecoders and bridge not set."); - _decoders = [_bridge modulesConformingToProtocol:@protocol(RCTImageDataDecoder)]; - } - - _decoders = [_decoders - sortedArrayUsingComparator:^NSComparisonResult(id a, id b) { - float priorityA = [a respondsToSelector:@selector(decoderPriority)] ? [a decoderPriority] : 0; - float priorityB = [b respondsToSelector:@selector(decoderPriority)] ? [b decoderPriority] : 0; - if (priorityA > priorityB) { - return NSOrderedAscending; - } else if (priorityA < priorityB) { - return NSOrderedDescending; - } else { - return NSOrderedSame; - } - }]; - } + NSArray> *decoders = _decoders; if (RCT_DEBUG) { // Check for handler conflicts float previousPriority = 0; id previousDecoder = nil; - for (id decoder in _decoders) { + for (id decoder in decoders) { float priority = [decoder respondsToSelector:@selector(decoderPriority)] ? [decoder decoderPriority] : 0; if (previousDecoder && priority < previousPriority) { return previousDecoder; @@ -297,7 +291,7 @@ RCT_EXPORT_MODULE() } // Normal code path - for (id decoder in _decoders) { + for (id decoder in decoders) { if ([decoder canDecodeImageData:data]) { return decoder; } @@ -621,7 +615,7 @@ static RCTImageLoaderCancellationBlock RCTLoadImageURLFromLoader( } // All access to URL cache must be serialized - if (!_isLoaderSetup) { + if (!_didSetup) { [self setUp]; } @@ -1040,7 +1034,7 @@ static RCTImageLoaderCancellationBlock RCTLoadImageURLFromLoader( }); }; - if (!_isLoaderSetup) { + if (!_didSetup) { [self setUp]; } dispatch_async(_URLRequestQueue, ^{ @@ -1173,6 +1167,9 @@ static RCTImageLoaderCancellationBlock RCTLoadImageURLFromLoader( return NO; } + if (!_didSetup) { + [self setUp]; + } for (id loader in _loaders) { // Don't use RCTImageURLLoader protocol for modules that already conform to // RCTURLRequestHandler as it's inefficient to decode an image and then