Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions packages/react-native/Libraries/Image/RCTImageLoader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#import <mach/mach_time.h>
#import <objc/runtime.h>
#import <atomic>
#import <future>

#import <ImageIO/ImageIO.h>

Expand All @@ -18,6 +19,7 @@
#import <React/RCTImageLoader.h>
#import <React/RCTImageLoaderWithAttributionProtocol.h>
#import <React/RCTImageUtils.h>
#import <React/RCTInitializing.h>
#import <React/RCTLog.h>
#import <React/RCTNetworking.h>
#import <React/RCTUtils.h>
Expand Down Expand Up @@ -45,6 +47,29 @@ static NSInteger RCTImageBytesForImage(UIImage *image)
return error;
}

template <typename T>
using Block = T (^)(void);

template <typename T>
std::shared_future<T> runOnMainQueue(Block<T> block)
{
__block std::promise<T> promise;
RCTExecuteOnMainQueue(^{
@try {
try {
T result = block();
promise.set_value(result);
} catch (...) {
promise.set_exception(std::current_exception());
}
} @catch (NSException *exception) {
auto cppException = std::runtime_error([exception.description UTF8String]);
promise.set_exception(std::make_exception_ptr(cppException));
}
});
return promise.get_future().share();
}

@interface RCTImageLoader () <NativeImageLoaderIOSSpec, RCTImageLoaderWithAttributionProtocol>

@end
Expand Down Expand Up @@ -84,6 +109,7 @@ @implementation RCTImageLoader {
NSUInteger _activeBytes;
std::mutex _loadersMutex;
__weak id<RCTImageRedirectProtocol> _redirectDelegate;
std::shared_future<CGSize> _screenSize;
}

@synthesize bridge = _bridge;
Expand All @@ -107,6 +133,9 @@ + (BOOL)requiresMainQueueSetup
- (instancetype)initWithRedirectDelegate:(id<RCTImageRedirectProtocol>)redirectDelegate
{
if (self = [super init]) {
_screenSize = runOnMainQueue(^{
return RCTScreenSize();
});
_redirectDelegate = redirectDelegate;
_isLoaderSetup = NO;
}
Expand Down Expand Up @@ -956,15 +985,19 @@ - (RCTImageLoaderCancellationBlock)decodeImageData:(NSData *)data
// Mark these bytes as in-use
self->_activeBytes += decodedImageBytes;

__block std::shared_future<CGFloat> screenScale = runOnMainQueue(^{
return RCTScreenScale();
});

// Do actual decompression on a concurrent background queue
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if (!std::atomic_load(cancelled.get())) {
// Decompress the image data (this may be CPU and memory intensive)
UIImage *image = RCTDecodeImageWithData(data, size, scale, resizeMode);
UIImage *image = RCTDecodeImageWithData(data, size, scale, resizeMode, screenScale.get());

#if RCT_DEV
CGSize imagePixelSize = RCTSizeInPixels(image.size, image.scale);
CGSize screenPixelSize = RCTSizeInPixels(RCTScreenSize(), RCTScreenScale());
CGSize screenPixelSize = RCTSizeInPixels(self->_screenSize.get(), screenScale.get());
if (imagePixelSize.width * imagePixelSize.height > screenPixelSize.width * screenPixelSize.height) {
RCTLogInfo(
@"[PERF ASSETS] Loading image at size %@, which is larger "
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/Libraries/Image/RCTImageUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ RCT_EXTERN BOOL RCTUpscalingRequired(
* Pass a destSize of CGSizeZero to decode the image at its original size.
*/
RCT_EXTERN UIImage *__nullable
RCTDecodeImageWithData(NSData *data, CGSize destSize, CGFloat destScale, RCTResizeMode resizeMode);
RCTDecodeImageWithData(NSData *data, CGSize destSize, CGFloat destScale, RCTResizeMode resizeMode, CGFloat screenScale);

/**
* This function takes the source data for an image and decodes just the
Expand Down
5 changes: 3 additions & 2 deletions packages/react-native/Libraries/Image/RCTImageUtils.mm
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ BOOL RCTUpscalingRequired(
}
}

UIImage *__nullable RCTDecodeImageWithData(NSData *data, CGSize destSize, CGFloat destScale, RCTResizeMode resizeMode)
UIImage *__nullable
RCTDecodeImageWithData(NSData *data, CGSize destSize, CGFloat destScale, RCTResizeMode resizeMode, CGFloat screenScale)
{
CGImageSourceRef sourceRef = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
if (!sourceRef) {
Expand All @@ -280,7 +281,7 @@ BOOL RCTUpscalingRequired(
destScale = 1;
}
} else if (!destScale) {
destScale = RCTScreenScale();
destScale = screenScale;
}

if (resizeMode == RCTResizeModeStretch) {
Expand Down
19 changes: 12 additions & 7 deletions packages/react-native/React/Fabric/Surface/RCTFabricSurface.mm
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,14 @@ - (instancetype)initWithSurfacePresenter:(RCTSurfacePresenter *)surfacePresenter

[_surfacePresenter registerSurface:self];

[self setMinimumSize:CGSizeZero maximumSize:RCTViewportSize()];

[self _updateLayoutContext];
/**
* Viewport size, and screen scale are only available on the main thread.
* Therefore, we just set the constraints on the main thread.
*/
RCTExecuteOnMainQueue(^{
[self setMinimumSize:CGSizeZero maximumSize:RCTViewportSize()];
[self _updateLayoutContextWithScreenScale:RCTScreenScale()];
});

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleContentSizeCategoryDidChangeNotification:)
Expand Down Expand Up @@ -143,7 +148,7 @@ - (RCTSurfaceView *)view

if (!_view) {
_view = [[RCTSurfaceView alloc] initWithSurface:(RCTSurface *)self];
[self _updateLayoutContext];
[self _updateLayoutContextWithScreenScale:RCTScreenScale()];
_touchHandler = [RCTSurfaceTouchHandler new];
[_touchHandler attachToView:_view];
}
Expand All @@ -170,14 +175,14 @@ - (void)_propagateStageChange
}
}

- (void)_updateLayoutContext
- (void)_updateLayoutContextWithScreenScale:(CGFloat)screenScale
{
auto layoutConstraints = _surfaceHandler->getLayoutConstraints();
layoutConstraints.layoutDirection = RCTLayoutDirection([[RCTI18nUtil sharedInstance] isRTL]);

auto layoutContext = _surfaceHandler->getLayoutContext();

layoutContext.pointScaleFactor = RCTScreenScale();
layoutContext.pointScaleFactor = screenScale;
layoutContext.swapLeftAndRightInRTL =
[[RCTI18nUtil sharedInstance] isRTL] && [[RCTI18nUtil sharedInstance] doLeftAndRightSwapInRTL];
layoutContext.fontSizeMultiplier = RCTFontSizeMultiplier();
Expand Down Expand Up @@ -271,7 +276,7 @@ - (BOOL)synchronouslyWaitFor:(NSTimeInterval)timeout

- (void)handleContentSizeCategoryDidChangeNotification:(NSNotification *)notification
{
[self _updateLayoutContext];
[self _updateLayoutContextWithScreenScale:RCTScreenScale()];
}

#pragma mark - Private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import <react/renderer/attributedstring/AttributedString.h>
#import <react/renderer/attributedstring/ParagraphAttributes.h>
#import <react/renderer/core/LayoutConstraints.h>
#import <react/renderer/textlayoutmanager/TextLayoutContext.h>
#import <react/renderer/textlayoutmanager/TextMeasureCache.h>

NS_ASSUME_NONNULL_BEGIN
Expand All @@ -28,11 +29,13 @@ using RCTTextLayoutFragmentEnumerationBlock =

- (facebook::react::TextMeasurement)measureAttributedString:(facebook::react::AttributedString)attributedString
paragraphAttributes:(facebook::react::ParagraphAttributes)paragraphAttributes
layoutConstraints:(facebook::react::LayoutConstraints)layoutConstraints;
layoutConstraints:(facebook::react::LayoutConstraints)layoutConstraints
layoutContext:(facebook::react::TextLayoutContext)layoutContext;

- (facebook::react::TextMeasurement)measureNSAttributedString:(NSAttributedString *)attributedString
paragraphAttributes:(facebook::react::ParagraphAttributes)paragraphAttributes
layoutConstraints:(facebook::react::LayoutConstraints)layoutConstraints;
layoutConstraints:(facebook::react::LayoutConstraints)layoutConstraints
layoutContext:(facebook::react::TextLayoutContext)layoutContext;

- (void)drawAttributedString:(facebook::react::AttributedString)attributedString
paragraphAttributes:(facebook::react::ParagraphAttributes)paragraphAttributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ static NSLineBreakMode RCTNSLineBreakModeFromEllipsizeMode(EllipsizeMode ellipsi
- (TextMeasurement)measureNSAttributedString:(NSAttributedString *)attributedString
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
layoutConstraints:(LayoutConstraints)layoutConstraints
layoutContext:(TextLayoutContext)layoutContext
{
if (attributedString.length == 0) {
// This is not really an optimization because that should be checked much earlier on the call stack.
Expand All @@ -51,16 +52,18 @@ - (TextMeasurement)measureNSAttributedString:(NSAttributedString *)attributedStr
paragraphAttributes:paragraphAttributes
size:maximumSize];

return [self _measureTextStorage:textStorage paragraphAttributes:paragraphAttributes];
return [self _measureTextStorage:textStorage paragraphAttributes:paragraphAttributes layoutContext:layoutContext];
}

- (TextMeasurement)measureAttributedString:(AttributedString)attributedString
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
layoutConstraints:(LayoutConstraints)layoutConstraints
layoutContext:(TextLayoutContext)layoutContext
{
return [self measureNSAttributedString:[self _nsAttributedStringFromAttributedString:attributedString]
paragraphAttributes:paragraphAttributes
layoutConstraints:layoutConstraints];
layoutConstraints:layoutConstraints
layoutContext:layoutContext];
}

- (void)drawAttributedString:(AttributedString)attributedString
Expand Down Expand Up @@ -329,6 +332,7 @@ - (NSAttributedString *)_nsAttributedStringFromAttributedString:(AttributedStrin

- (TextMeasurement)_measureTextStorage:(NSTextStorage *)textStorage
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
layoutContext:(TextLayoutContext)layoutContext
{
NSLayoutManager *layoutManager = textStorage.layoutManagers.firstObject;
NSTextContainer *textContainer = layoutManager.textContainers.firstObject;
Expand Down Expand Up @@ -382,7 +386,9 @@ - (TextMeasurement)_measureTextStorage:(NSTextStorage *)textStorage
size.height = enumeratedLinesHeight;
}

size = (CGSize){RCTCeilPixelValue(size.width), RCTCeilPixelValue(size.height)};
size = (CGSize){
ceil(size.width * layoutContext.pointScaleFactor) / layoutContext.pointScaleFactor,
ceil(size.height * layoutContext.pointScaleFactor) / layoutContext.pointScaleFactor};

__block auto attachments = TextMeasurement::Attachments{};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@

auto measurement = [textLayoutManager measureAttributedString:attributedString
paragraphAttributes:paragraphAttributes
layoutConstraints:layoutConstraints];
layoutConstraints:layoutConstraints
layoutContext:layoutContext];

if (telemetry) {
telemetry->didMeasureText();
Expand All @@ -69,7 +70,8 @@

measurement = [textLayoutManager measureNSAttributedString:nsAttributedString
paragraphAttributes:paragraphAttributes
layoutConstraints:layoutConstraints];
layoutConstraints:layoutConstraints
layoutContext:layoutContext];

if (telemetry) {
telemetry->didMeasureText();
Expand Down