From 607ec8f4a5a5fa7058023bdc189a13425ab2df1e Mon Sep 17 00:00:00 2001 From: Adlai Holler Date: Mon, 5 Sep 2016 20:52:06 -0700 Subject: [PATCH] [ASImageNode] Use kCGBlendModeCopy when Possible (#2197) * [ASImageNode] Use kCGBlendModeCopy when possible * Make the blend mode logic more readable --- AsyncDisplayKit/ASImageNode.mm | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/AsyncDisplayKit/ASImageNode.mm b/AsyncDisplayKit/ASImageNode.mm index cdd428b313..c5986f955d 100644 --- a/AsyncDisplayKit/ASImageNode.mm +++ b/AsyncDisplayKit/ASImageNode.mm @@ -437,15 +437,19 @@ static ASDN::Mutex cacheLock; // will do its rounding on pixel instead of point boundaries UIGraphicsBeginImageContextWithOptions(key.backingSize, key.isOpaque, 1.0); + BOOL contextIsClean = YES; + CGContextRef context = UIGraphicsGetCurrentContext(); if (context && key.preContextBlock) { key.preContextBlock(context); + contextIsClean = NO; } // if view is opaque, fill the context with background color if (key.isOpaque && key.backgroundColor) { [key.backgroundColor setFill]; UIRectFill({ .size = key.backingSize }); + contextIsClean = NO; } // iOS 9 appears to contain a thread safety regression when drawing the same CGImageRef on @@ -460,8 +464,12 @@ static ASDN::Mutex cacheLock; // Another option is to have ASDisplayNode+AsyncDisplay coordinate these cases, and share the decoded buffer. // Details tracked in https://github.com/facebook/AsyncDisplayKit/issues/1068 - @synchronized(key.image) { - [key.image drawInRect:key.imageDrawRect]; + UIImage *image = key.image; + BOOL canUseCopy = (contextIsClean || CGImageGetAlphaInfo(image.CGImage) == kCGImageAlphaNone); + CGBlendMode blendMode = canUseCopy ? kCGBlendModeCopy : kCGBlendModeNormal; + + @synchronized(image) { + [image drawInRect:key.imageDrawRect blendMode:blendMode alpha:1]; } if (context && key.postContextBlock) {