From 2188aa84423ee188762a92535351f7e07504cc23 Mon Sep 17 00:00:00 2001 From: Vlad Solomenchuk Date: Wed, 9 Oct 2019 12:21:27 -0700 Subject: [PATCH] deprecate ASGraphicsCreateImageWithOptions --- Source/ASDisplayNode.mm | 2 +- Source/ASImageNode.mm | 22 +++++---- Source/ASMapNode.mm | 2 +- Source/ASTextNode.mm | 8 +++- Source/Debug/AsyncDisplayKit+Debug.mm | 2 +- Source/Details/ASGraphicsContext.h | 2 +- Source/Details/ASGraphicsContext.mm | 48 ++++++++++---------- Source/Private/ASDisplayNode+AsyncDisplay.mm | 4 +- 8 files changed, 48 insertions(+), 42 deletions(-) diff --git a/Source/ASDisplayNode.mm b/Source/ASDisplayNode.mm index 93798051d..ccad49b33 100644 --- a/Source/ASDisplayNode.mm +++ b/Source/ASDisplayNode.mm @@ -1499,7 +1499,7 @@ - (void)_updateClipCornerLayerContentsWithRadius:(CGFloat)radius backgroundColor BOOL isRight = (idx == 1 || idx == 3); CGSize size = CGSizeMake(radius + 1, radius + 1); - UIImage *newContents = ASGraphicsCreateImageWithOptions(size, NO, self.contentsScaleForDisplay, nil, nil, ^{ + UIImage *newContents = ASGraphicsCreateImageWithTraitCollectionAndOptions(self.primitiveTraitCollection, size, NO, self.contentsScaleForDisplay, nil, ^{ CGContextRef ctx = UIGraphicsGetCurrentContext(); if (isRight == YES) { CGContextTranslateCTM(ctx, -radius + 1, 0); diff --git a/Source/ASImageNode.mm b/Source/ASImageNode.mm index 114eda324..0a5a4e59c 100644 --- a/Source/ASImageNode.mm +++ b/Source/ASImageNode.mm @@ -52,7 +52,7 @@ @interface ASImageNodeDrawParameters : NSObject { ASDisplayNodeContextModifier _willDisplayNodeContentWithRenderingContext; ASDisplayNodeContextModifier _didDisplayNodeContentWithRenderingContext; ASImageNodeDrawParametersBlock _didDrawBlock; - UIUserInterfaceStyle _userInterfaceStyle API_AVAILABLE(tvos(10.0), ios(12.0)); + ASPrimitiveTraitCollection _traitCollection; } @end @@ -209,7 +209,10 @@ - (UIImage *)placeholderImage return nil; } - return ASGraphicsCreateImageWithOptions(size, NO, 1, nil, nil, ^{ + __instanceLock__.lock(); + ASPrimitiveTraitCollection tc = _primitiveTraitCollection; + __instanceLock__.unlock(); + return ASGraphicsCreateImageWithTraitCollectionAndOptions(tc, size, NO, 1, nil, ^{ AS::MutexLocker l(__instanceLock__); [_placeholderColor setFill]; UIRectFill(CGRectMake(0, 0, size.width, size.height)); @@ -329,9 +332,7 @@ - (NSObject *)drawParametersForAsyncLayer:(_ASDisplayLayer *)layer drawParameters->_imageModificationBlock = _imageModificationBlock; drawParameters->_willDisplayNodeContentWithRenderingContext = _willDisplayNodeContentWithRenderingContext; drawParameters->_didDisplayNodeContentWithRenderingContext = _didDisplayNodeContentWithRenderingContext; - if (AS_AVAILABLE_IOS_TVOS(12, 10)) { - drawParameters->_userInterfaceStyle = self.primitiveTraitCollection.userInterfaceStyle; - } + drawParameters->_traitCollection = _primitiveTraitCollection; // Hack for now to retain the weak entry that was created while this drawing happened @@ -436,8 +437,7 @@ + (UIImage *)displayWithParameters:(id)parameter isCancelled:(NS_NOESC contentsKey.imageModificationBlock = imageModificationBlock; if (AS_AVAILABLE_IOS_TVOS(12, 10)) { - UIUserInterfaceStyle userInterfaceStyle = drawParameter->_userInterfaceStyle; - contentsKey.userInterfaceStyle = userInterfaceStyle; + contentsKey.userInterfaceStyle = drawParameter->_traitCollection.userInterfaceStyle; } if (isCancelled()) { @@ -492,18 +492,20 @@ + (ASWeakMapEntry *)contentsForkey:(ASImageNodeContentsKey *)key drawParameters: } } -+ (UIImage *)createContentsForkey:(ASImageNodeContentsKey *)key drawParameters:(id)drawParameters isCancelled:(asdisplaynode_iscancelled_block_t)isCancelled ++ (UIImage *)createContentsForkey:(ASImageNodeContentsKey *)key drawParameters:(id)parameter isCancelled:(asdisplaynode_iscancelled_block_t)isCancelled { - // The following `ASGraphicsCreateImageWithOptions` call will sometimes take take longer than 5ms on an + // The following `ASGraphicsCreateImageWithTraitCollectionAndOptions` call will sometimes take take longer than 5ms on an // A5 processor for a 400x800 backingSize. // Check for cancellation before we call it. if (isCancelled()) { return nil; } + + ASImageNodeDrawParameters *drawParameters = (ASImageNodeDrawParameters *)parameter; // Use contentsScale of 1.0 and do the contentsScale handling in boundsSizeInPixels so ASCroppedImageBackingSizeAndDrawRectInBounds // will do its rounding on pixel instead of point boundaries - UIImage *result = ASGraphicsCreateImageWithOptions(key.backingSize, key.isOpaque, 1.0, key.image, isCancelled, ^{ + UIImage *result = ASGraphicsCreateImageWithTraitCollectionAndOptions(drawParameters->_traitCollection, key.backingSize, key.isOpaque, 1.0, key.image, ^{ BOOL contextIsClean = YES; CGContextRef context = UIGraphicsGetCurrentContext(); diff --git a/Source/ASMapNode.mm b/Source/ASMapNode.mm index c41141c0c..074c1a5a9 100644 --- a/Source/ASMapNode.mm +++ b/Source/ASMapNode.mm @@ -221,7 +221,7 @@ - (void)takeSnapshot CGRect finalImageRect = CGRectMake(0, 0, image.size.width, image.size.height); - image = ASGraphicsCreateImageWithOptions(image.size, YES, image.scale, image, nil, ^{ + image = ASGraphicsCreateImageWithTraitCollectionAndOptions(strongSelf.primitiveTraitCollection, image.size, YES, image.scale, image, ^{ [image drawAtPoint:CGPointZero]; UIImage *pinImage; diff --git a/Source/ASTextNode.mm b/Source/ASTextNode.mm index 71263a873..c8f3d9fad 100644 --- a/Source/ASTextNode.mm +++ b/Source/ASTextNode.mm @@ -140,6 +140,7 @@ @interface ASTextNodeDrawParameter : NSObject { CGFloat _contentScale; BOOL _opaque; CGRect _bounds; + ASPrimitiveTraitCollection _traitCollection; } @end @@ -151,6 +152,7 @@ - (instancetype)initWithRendererAttributes:(ASTextKitAttributes)rendererAttribut contentScale:(CGFloat)contentScale opaque:(BOOL)opaque bounds:(CGRect)bounds + traitCollection: (ASPrimitiveTraitCollection)traitCollection { self = [super init]; if (self != nil) { @@ -160,6 +162,7 @@ - (instancetype)initWithRendererAttributes:(ASTextKitAttributes)rendererAttribut _contentScale = contentScale; _opaque = opaque; _bounds = bounds; + _traitCollection = traitCollection; } return self; } @@ -554,7 +557,8 @@ - (NSObject *)drawParametersForAsyncLayer:(_ASDisplayLayer *)layer textContainerInsets:_textContainerInset contentScale:_contentsScaleForDisplay opaque:self.isOpaque - bounds:[self threadSafeBounds]]; + bounds:[self threadSafeBounds] + traitCollection:self.primitiveTraitCollection]; } + (UIImage *)displayWithParameters:(id)parameters isCancelled:(NS_NOESCAPE asdisplaynode_iscancelled_block_t)isCancelled @@ -569,7 +573,7 @@ + (UIImage *)displayWithParameters:(id)parameters isCancelled:(NS_NOES UIEdgeInsets textContainerInsets = drawParameter ? drawParameter->_textContainerInsets : UIEdgeInsetsZero; ASTextKitRenderer *renderer = [drawParameter rendererForBounds:drawParameter->_bounds]; - UIImage *result = ASGraphicsCreateImageWithOptions(CGSizeMake(drawParameter->_bounds.size.width, drawParameter->_bounds.size.height), drawParameter->_opaque, drawParameter->_contentScale, nil, nil, ^{ + UIImage *result = ASGraphicsCreateImageWithTraitCollectionAndOptions(drawParameter->_traitCollection, CGSizeMake(drawParameter->_bounds.size.width, drawParameter->_bounds.size.height), drawParameter->_opaque, drawParameter->_contentScale, nil, ^{ CGContextRef context = UIGraphicsGetCurrentContext(); ASDisplayNodeAssert(context, @"This is no good without a context."); diff --git a/Source/Debug/AsyncDisplayKit+Debug.mm b/Source/Debug/AsyncDisplayKit+Debug.mm index 0e834e4e8..05fa4a0be 100644 --- a/Source/Debug/AsyncDisplayKit+Debug.mm +++ b/Source/Debug/AsyncDisplayKit+Debug.mm @@ -140,7 +140,7 @@ - (void)layout UIColor *clipsBorderColor = [UIColor colorWithRed:30/255.0 green:90/255.0 blue:50/255.0 alpha:0.7]; CGRect imgRect = CGRectMake(0, 0, 2.0 * borderWidth + 1.0, 2.0 * borderWidth + 1.0); - UIImage *debugHighlightImage = ASGraphicsCreateImageWithOptions(imgRect.size, NO, 1, nil, nil, ^{ + UIImage *debugHighlightImage = ASGraphicsCreateImageWithTraitCollectionAndOptions(self.primitiveTraitCollection, imgRect.size, NO, 1, nil, ^{ [fillColor setFill]; UIRectFill(imgRect); diff --git a/Source/Details/ASGraphicsContext.h b/Source/Details/ASGraphicsContext.h index da1349cf0..ea39bf3b5 100644 --- a/Source/Details/ASGraphicsContext.h +++ b/Source/Details/ASGraphicsContext.h @@ -28,7 +28,7 @@ NS_ASSUME_NONNULL_BEGIN * * @return The rendered image. You can also render intermediary images using UIGraphicsGetImageFromCurrentImageContext. */ -AS_EXTERN UIImage *ASGraphicsCreateImageWithOptions(CGSize size, BOOL opaque, CGFloat scale, UIImage * _Nullable sourceImage, asdisplaynode_iscancelled_block_t NS_NOESCAPE _Nullable isCancelled, void (NS_NOESCAPE ^work)(void)); +AS_EXTERN UIImage *ASGraphicsCreateImageWithOptions(CGSize size, BOOL opaque, CGFloat scale, UIImage * _Nullable sourceImage, asdisplaynode_iscancelled_block_t NS_NOESCAPE _Nullable isCancelled, void (NS_NOESCAPE ^work)(void)) ASDISPLAYNODE_DEPRECATED_MSG("Use ASGraphicsCreateImageWithTraitCollectionAndOptions instead"); /** * A wrapper for the UIKit drawing APIs. diff --git a/Source/Details/ASGraphicsContext.mm b/Source/Details/ASGraphicsContext.mm index 89882681c..15fcee0bc 100644 --- a/Source/Details/ASGraphicsContext.mm +++ b/Source/Details/ASGraphicsContext.mm @@ -12,6 +12,22 @@ #import #import + +#if AS_AT_LEAST_IOS13 +#define PERFORM_WORK_WITH_TRAIT_COLLECTION(work, traitCollection) \ + if (@available(iOS 13.0, *)) { \ + UITraitCollection *uiTraitCollection = ASPrimitiveTraitCollectionToUITraitCollection(traitCollection); \ + [uiTraitCollection performAsCurrentTraitCollection:^{ \ + work(); \ + }];\ + } else { \ + work(); \ + } +#else +#define PERFORM_WORK_WITH_TRAIT_COLLECTION(work, traitCollection) work(); +#endif + + NS_AVAILABLE_IOS(10) NS_INLINE void ASConfigureExtendedRange(UIGraphicsImageRendererFormat *format) { @@ -27,6 +43,10 @@ NS_INLINE void ASConfigureExtendedRange(UIGraphicsImageRendererFormat *format) asdisplaynode_iscancelled_block_t NS_NOESCAPE isCancelled, void (^NS_NOESCAPE work)()) { + return ASGraphicsCreateImageWithTraitCollectionAndOptions(ASPrimitiveTraitCollectionMakeDefault(), size, opaque, scale, sourceImage, work); +} + +UIImage *ASGraphicsCreateImageWithTraitCollectionAndOptions(ASPrimitiveTraitCollection traitCollection, CGSize size, BOOL opaque, CGFloat scale, UIImage * sourceImage, void (NS_NOESCAPE ^work)()) { if (AS_AVAILABLE_IOS_TVOS(10, 10)) { if (ASActivateExperimentalFeature(ASExperimentalDrawingGlobal)) { // If they used default scale, reuse one of two preferred formats. @@ -77,38 +97,18 @@ NS_INLINE void ASConfigureExtendedRange(UIGraphicsImageRendererFormat *format) format.scale = scale; ASConfigureExtendedRange(format); } - + return [[[UIGraphicsImageRenderer alloc] initWithSize:size format:format] imageWithActions:^(UIGraphicsImageRendererContext *rendererContext) { ASDisplayNodeCAssert(UIGraphicsGetCurrentContext(), @"Should have a context!"); - work(); + PERFORM_WORK_WITH_TRAIT_COLLECTION(work, traitCollection) }]; } } // Bad OS or experiment flag. Use UIGraphics* API. UIGraphicsBeginImageContextWithOptions(size, opaque, scale); - work(); - UIImage *image = nil; - if (isCancelled == nil || !isCancelled()) { - image = UIGraphicsGetImageFromCurrentImageContext(); - } + PERFORM_WORK_WITH_TRAIT_COLLECTION(work, traitCollection) + UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } - -UIImage *ASGraphicsCreateImageWithTraitCollectionAndOptions(ASPrimitiveTraitCollection traitCollection, CGSize size, BOOL opaque, CGFloat scale, UIImage * sourceImage, void (NS_NOESCAPE ^work)()) { -#if AS_AT_LEAST_IOS13 - if (@available(iOS 13.0, *)) { - UITraitCollection *uiTraitCollection = ASPrimitiveTraitCollectionToUITraitCollection(traitCollection); - return ASGraphicsCreateImageWithOptions(size, opaque, scale, sourceImage, nil, ^{ - [uiTraitCollection performAsCurrentTraitCollection:^{ - work(); - }]; - }); - } else { - return ASGraphicsCreateImageWithOptions(size, opaque, scale, sourceImage, nil, work); - } -#else - return ASGraphicsCreateImageWithOptions(size, opaque, scale, sourceImage, nil, work); -#endif -} diff --git a/Source/Private/ASDisplayNode+AsyncDisplay.mm b/Source/Private/ASDisplayNode+AsyncDisplay.mm index 983f6f88c..e7a875532 100644 --- a/Source/Private/ASDisplayNode+AsyncDisplay.mm +++ b/Source/Private/ASDisplayNode+AsyncDisplay.mm @@ -209,7 +209,7 @@ - (asyncdisplaykit_async_transaction_operation_block_t)_displayBlockWithAsynchro displayBlock = ^id{ CHECK_CANCELLED_AND_RETURN_NIL(); - UIImage *image = ASGraphicsCreateImageWithOptions(bounds.size, opaque, contentsScaleForDisplay, nil, isCancelledBlock, ^{ + UIImage *image = ASGraphicsCreateImageWithTraitCollectionAndOptions(self.primitiveTraitCollection, bounds.size, opaque, contentsScaleForDisplay, nil, ^{ for (dispatch_block_t block in displayBlocks) { if (isCancelledBlock()) return; block(); @@ -247,7 +247,7 @@ - (asyncdisplaykit_async_transaction_operation_block_t)_displayBlockWithAsynchro }; if (shouldCreateGraphicsContext) { - return ASGraphicsCreateImageWithOptions(bounds.size, opaque, contentsScaleForDisplay, nil, isCancelledBlock, workWithContext); + return ASGraphicsCreateImageWithTraitCollectionAndOptions(self.primitiveTraitCollection, bounds.size, opaque, contentsScaleForDisplay, nil, workWithContext); } else { workWithContext(); return image;