Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deprecate ASGraphicsCreateImageWithOptions #1704

Merged
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
2 changes: 1 addition & 1 deletion Source/ASDisplayNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 12 additions & 10 deletions Source/ASImageNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -436,8 +437,7 @@ + (UIImage *)displayWithParameters:(id<NSObject>)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()) {
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion Source/ASMapNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions Source/ASTextNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ @interface ASTextNodeDrawParameter : NSObject {
CGFloat _contentScale;
BOOL _opaque;
CGRect _bounds;
ASPrimitiveTraitCollection _traitCollection;
}
@end

Expand All @@ -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) {
Expand All @@ -160,6 +162,7 @@ - (instancetype)initWithRendererAttributes:(ASTextKitAttributes)rendererAttribut
_contentScale = contentScale;
_opaque = opaque;
_bounds = bounds;
_traitCollection = traitCollection;
}
return self;
}
Expand Down Expand Up @@ -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<NSObject>)parameters isCancelled:(NS_NOESCAPE asdisplaynode_iscancelled_block_t)isCancelled
Expand All @@ -569,7 +573,7 @@ + (UIImage *)displayWithParameters:(id<NSObject>)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.");

Expand Down
2 changes: 1 addition & 1 deletion Source/Debug/AsyncDisplayKit+Debug.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion Source/Details/ASGraphicsContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
48 changes: 24 additions & 24 deletions Source/Details/ASGraphicsContext.mm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@
#import <AsyncDisplayKit/ASInternalHelpers.h>
#import <AsyncDisplayKit/ASAvailability.h>


#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)
{
Expand All @@ -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.
Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions Source/Private/ASDisplayNode+AsyncDisplay.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down