Skip to content

Commit 28522ce

Browse files
authored
Have image nodes draw into opaque contexts automatically if possible (TextureGroup#1432)
* Have image nodes draw into opaque contexts if the image is opaque and it fills the context * Call backingSize once
1 parent 8231599 commit 28522ce

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

Source/ASImageNode.mm

+12-5
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,17 @@ + (UIImage *)createContentsForkey:(ASImageNodeContentsKey *)key drawParameters:(
473473
return nil;
474474
}
475475

476+
// If the image is opaque, and the draw rect contains the bounds rect, we can use an opaque context.
477+
UIImage *image = key.image;
478+
const CGRect imageDrawRect = key.imageDrawRect;
479+
const CGRect contextBounds = { CGPointZero, key.backingSize };
480+
const BOOL imageIsOpaque = ASImageAlphaInfoIsOpaque(CGImageGetAlphaInfo(image.CGImage));
481+
const BOOL imageFillsContext = CGRectContainsRect(imageDrawRect, contextBounds);
482+
const BOOL contextIsOpaque = (imageIsOpaque && imageFillsContext) || key.isOpaque;
483+
476484
// Use contentsScale of 1.0 and do the contentsScale handling in boundsSizeInPixels so ASCroppedImageBackingSizeAndDrawRectInBounds
477485
// will do its rounding on pixel instead of point boundaries
478-
ASGraphicsBeginImageContextWithOptions(key.backingSize, key.isOpaque, 1.0);
486+
ASGraphicsBeginImageContextWithOptions(contextBounds.size, contextIsOpaque, 1.0);
479487

480488
BOOL contextIsClean = YES;
481489

@@ -503,13 +511,12 @@ + (UIImage *)createContentsForkey:(ASImageNodeContentsKey *)key drawParameters:(
503511
// upon removal of the object from the set when the operation completes.
504512
// Another option is to have ASDisplayNode+AsyncDisplay coordinate these cases, and share the decoded buffer.
505513
// Details tracked in https://github.com/facebook/AsyncDisplayKit/issues/1068
506-
507-
UIImage *image = key.image;
508-
BOOL canUseCopy = (contextIsClean || ASImageAlphaInfoIsOpaque(CGImageGetAlphaInfo(image.CGImage)));
514+
515+
BOOL canUseCopy = (contextIsClean || imageIsOpaque);
509516
CGBlendMode blendMode = canUseCopy ? kCGBlendModeCopy : kCGBlendModeNormal;
510517

511518
@synchronized(image) {
512-
[image drawInRect:key.imageDrawRect blendMode:blendMode alpha:1];
519+
[image drawInRect:imageDrawRect blendMode:blendMode alpha:1];
513520
}
514521

515522
if (context && key.didDisplayNodeContentWithRenderingContext) {

0 commit comments

Comments
 (0)