Skip to content

Commit

Permalink
fix(ios): potential deadlock in layout & unify all ScreenScale calls
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg committed Jun 12, 2024
1 parent ad31bfa commit 6384825
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 38 deletions.
6 changes: 1 addition & 5 deletions ios/sdk/base/HippyDeviceBaseInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ BOOL isHippyScreenInOSDarkMode(void) {
statusBarHeight = bridge.delegate.defaultStatusBarHeightNoMatterHiddenOrNot ?: 0.0;
}
}
static NSNumber *screenScale = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
screenScale = @([UIScreen mainScreen].scale);
});
NSNumber *screenScale = @(HippyScreenScale());
NSDictionary *dimensions = @{
@"window" : @{
@"width": @(windowSize.width),
Expand Down
3 changes: 2 additions & 1 deletion ios/sdk/component/image/HippyDefaultImageProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#import "HippyDefaultImageProvider.h"
#import "NSData+DataType.h"
#import <CoreServices/CoreServices.h>
#import "HippyUtils.h"

@interface HippyDefaultImageProvider () {
NSData *_data;
Expand Down Expand Up @@ -72,7 +73,7 @@ - (UIImage *)image {
CGFloat view_width = _imageViewSize.width;
CGFloat view_height = _imageViewSize.height;
if (_downSample && view_width > 0 && view_height > 0) {
CGFloat scale = [UIScreen mainScreen].scale;
CGFloat scale = HippyScreenScale();
NSDictionary *options = @{ (NSString *)kCGImageSourceShouldCache: @(NO) };
CGImageSourceRef ref = CGImageSourceCreateWithData((__bridge CFDataRef)_data, (__bridge CFDictionaryRef)options);
if (ref) {
Expand Down
8 changes: 4 additions & 4 deletions ios/sdk/component/view/HippyBackgroundImageCacheManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ - (void)imageWithUrl:(NSString *)uri completionHandler:(HippyBackgroundImageComp
BOOL fileExist = [[NSFileManager defaultManager] fileExistsAtPath:localPath isDirectory:&isDirectory];
if (fileExist && !isDirectory) {
NSData *imageData = [NSData dataWithContentsOfFile:localPath];
UIImage *image = [UIImage imageWithData:imageData scale:[UIScreen mainScreen].scale];
UIImage *image = [UIImage imageWithData:imageData scale:HippyScreenScale()];
completionHandler(image, nil);
}
else {
Expand All @@ -81,7 +81,7 @@ - (void)imageWithUrl:(NSString *)uri completionHandler:(HippyBackgroundImageComp
id<HippyImageViewCustomLoader> imageLoader = self.bridge.imageLoader;
if (imageLoader) {
[imageLoader loadImage:imageURL completed:^(NSData *imgData, NSURL *url, NSError *error, BOOL cached) {
UIImage *image = [UIImage imageWithData:imgData scale:[UIScreen mainScreen].scale];
UIImage *image = [UIImage imageWithData:imgData scale:HippyScreenScale()];
completionHandler(image, error);
}];
} else {
Expand All @@ -103,7 +103,7 @@ - (void)loadHTTPURL:(NSURL *)URL completionHandler:(HippyBackgroundImageCompleti
[session dataTaskWithRequest:request
completionHandler:^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {
if (data) {
CGFloat scale = [UIScreen mainScreen].scale;
CGFloat scale = HippyScreenScale();
UIImage *originImage = [UIImage imageWithData:data scale:scale];
if (originImage) {
completionHandler(originImage, nil);
Expand All @@ -122,7 +122,7 @@ - (void)loadHTTPURL:(NSURL *)URL completionHandler:(HippyBackgroundImageCompleti
- (void)loadBase64URL:(NSURL *)base64URL completionHandler:(HippyBackgroundImageCompletionHandler)completionHandler {
NSData *imgData = [NSData dataWithContentsOfURL:base64URL];
if (imgData) {
UIImage *image = [UIImage imageWithData:imgData scale:[UIScreen mainScreen].scale];
UIImage *image = [UIImage imageWithData:imgData scale:HippyScreenScale()];
if (image) {
completionHandler(image, nil);
} else {
Expand Down
2 changes: 1 addition & 1 deletion ios/sdk/component/view/HippyShadowView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ - (instancetype)init {

_hippySubviews = [NSMutableArray array];

_nodeRef = MTTNodeNewWithScaleFactor([UIScreen mainScreen].scale);
_nodeRef = MTTNodeNewWithScaleFactor(HippyScreenScale());
}
return self;
}
Expand Down
2 changes: 1 addition & 1 deletion ios/sdk/component/view/HippyViewManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ - (HippyViewManagerUIBlock)uiBlockToAmendWithShadowViewRegistry:(__unused NSDict
}
HIPPY_CUSTOM_VIEW_PROPERTY(shouldRasterizeIOS, BOOL, HippyView) {
view.layer.shouldRasterize = json ? [HippyConvert BOOL:json] : defaultView.layer.shouldRasterize;
view.layer.rasterizationScale = view.layer.shouldRasterize ? [UIScreen mainScreen].scale : defaultView.layer.rasterizationScale;
view.layer.rasterizationScale = view.layer.shouldRasterize ? HippyScreenScale() : defaultView.layer.rasterizationScale;
}

HIPPY_CUSTOM_VIEW_PROPERTY(transform, CATransform3D, HippyView) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#import "HippyCollectionViewWaterfallLayout.h"
#import "tgmath.h"
#import "HippyUtils.h"

NSString *const HippyCollectionElementKindSectionHeader = @"HippyCollectionElementKindSectionHeader";
NSString *const HippyCollectionElementKindSectionFooter = @"HippyCollectionElementKindSectionFooter";
Expand Down Expand Up @@ -49,7 +50,7 @@ @implementation HippyCollectionViewWaterfallLayout
static const NSInteger unionSize = 20;

static CGFloat HippyFloorCGFloat(CGFloat value) {
CGFloat scale = [UIScreen mainScreen].scale;
CGFloat scale = HippyScreenScale();
return floor(value * scale) / scale;
}

Expand Down
28 changes: 3 additions & 25 deletions ios/sdk/layout/x5LayoutUtil.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,14 @@
*/

#import "x5LayoutUtil.h"

static void x5ExecuteOnMainThread(dispatch_block_t block, BOOL sync) {
if (0 == strcmp(dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL), dispatch_queue_get_label(dispatch_get_main_queue()))) {
block();
} else if (sync) {
dispatch_sync(dispatch_get_main_queue(), block);
} else {
dispatch_async(dispatch_get_main_queue(), block);
}
}

static CGFloat x5ScreenScale(void) {
static CGFloat scale;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
x5ExecuteOnMainThread(
^{
scale = [UIScreen mainScreen].scale;
}, YES);
});

return scale;
}
#import "HippyUtils.h"

CGFloat x5CeilPixelValue(CGFloat value) {
CGFloat scale = x5ScreenScale();
CGFloat scale = HippyScreenScale();
return ceil(value * scale) / scale;
}

CGFloat x5RoundPixelValue(CGFloat value) {
CGFloat scale = x5ScreenScale();
CGFloat scale = HippyScreenScale();
return round(value * scale) / scale;
}

0 comments on commit 6384825

Please sign in to comment.