Skip to content

Commit

Permalink
[ASImageNode] Move debug label and will- / didDisplayNodeContentWithR…
Browse files Browse the repository at this point in the history
…enderingContext out of drawing method #trivial (#235)

* Move configuring the debug label from the drawing method in displayDidFinish

* Move will- and didDisplayNodeContentWithRenderingContext in drawing parameters
  • Loading branch information
maicki authored and garrettmoon committed May 4, 2017
1 parent 588f30c commit 17d9564
Showing 1 changed file with 40 additions and 31 deletions.
71 changes: 40 additions & 31 deletions Source/ASImageNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
CGRect cropRect;
CGRect cropDisplayBounds;
asimagenode_modification_block_t imageModificationBlock;
ASDisplayNodeContextModifier willDisplayNodeContentWithRenderingContext;
ASDisplayNodeContextModifier didDisplayNodeContentWithRenderingContext;
};

/**
Expand All @@ -65,8 +67,8 @@ @interface ASImageNodeContentsKey : NSObject {}
@property CGRect imageDrawRect;
@property BOOL isOpaque;
@property (nonatomic, strong) UIColor *backgroundColor;
@property (nonatomic, copy) ASDisplayNodeContextModifier preContextBlock;
@property (nonatomic, copy) ASDisplayNodeContextModifier postContextBlock;
@property (nonatomic, copy) ASDisplayNodeContextModifier willDisplayNodeContentWithRenderingContext;
@property (nonatomic, copy) ASDisplayNodeContextModifier didDisplayNodeContentWithRenderingContext;
@property (nonatomic, copy) asimagenode_modification_block_t imageModificationBlock;

@end
Expand All @@ -90,8 +92,8 @@ - (BOOL)isEqual:(id)object
&& CGRectEqualToRect(_imageDrawRect, other.imageDrawRect)
&& _isOpaque == other.isOpaque
&& [_backgroundColor isEqual:other.backgroundColor]
&& _preContextBlock == other.preContextBlock
&& _postContextBlock == other.postContextBlock
&& _willDisplayNodeContentWithRenderingContext == other.willDisplayNodeContentWithRenderingContext
&& _didDisplayNodeContentWithRenderingContext == other.didDisplayNodeContentWithRenderingContext
&& _imageModificationBlock == other.imageModificationBlock;
} else {
return NO;
Expand All @@ -106,17 +108,17 @@ - (NSUInteger)hash
CGRect imageDrawRect;
BOOL isOpaque;
NSUInteger backgroundColorHash;
void *preContextBlock;
void *postContextBlock;
void *willDisplayNodeContentWithRenderingContext;
void *didDisplayNodeContentWithRenderingContext;
void *imageModificationBlock;
} data = {
_image.hash,
_backingSize,
_imageDrawRect,
_isOpaque,
_backgroundColor.hash,
(void *)_preContextBlock,
(void *)_postContextBlock,
(void *)_willDisplayNodeContentWithRenderingContext,
(void *)_didDisplayNodeContentWithRenderingContext,
(void *)_imageModificationBlock
};
return ASHashBytes(&data, sizeof(data));
Expand Down Expand Up @@ -257,7 +259,7 @@ - (void)_locked_setImage:(UIImage *)image
[self addSubnode:_debugLabelNode];
});
}

} else {
self.contents = nil;
}
Expand Down Expand Up @@ -299,7 +301,9 @@ - (NSObject *)drawParametersForAsyncLayer:(_ASDisplayLayer *)layer
.forcedSize = _forcedSize,
.cropRect = _cropRect,
.cropDisplayBounds = _cropDisplayBounds,
.imageModificationBlock = _imageModificationBlock
.imageModificationBlock = _imageModificationBlock,
.willDisplayNodeContentWithRenderingContext = _willDisplayNodeContentWithRenderingContext,
.didDisplayNodeContentWithRenderingContext = _didDisplayNodeContentWithRenderingContext
};

return nil;
Expand Down Expand Up @@ -335,12 +339,12 @@ - (UIImage *)displayWithParameters:(id<NSObject>)parameter isCancelled:(asdispla
CGRect cropDisplayBounds = drawParameter.cropDisplayBounds;
CGRect cropRect = drawParameter.cropRect;
asimagenode_modification_block_t imageModificationBlock = drawParameter.imageModificationBlock;
ASDisplayNodeContextModifier willDisplayNodeContentWithRenderingContext = drawParameter.willDisplayNodeContentWithRenderingContext;
ASDisplayNodeContextModifier didDisplayNodeContentWithRenderingContext = drawParameter.didDisplayNodeContentWithRenderingContext;

BOOL hasValidCropBounds = cropEnabled && !CGRectIsEmpty(cropDisplayBounds);
CGRect bounds = (hasValidCropBounds ? cropDisplayBounds : drawParameterBounds);

ASDisplayNodeContextModifier preContextBlock = self.willDisplayNodeContentWithRenderingContext;
ASDisplayNodeContextModifier postContextBlock = self.didDisplayNodeContentWithRenderingContext;

ASDisplayNodeAssert(contentsScale > 0, @"invalid contentsScale at display time");

Expand All @@ -357,19 +361,6 @@ - (UIImage *)displayWithParameters:(id<NSObject>)parameter isCancelled:(asdispla
CGSize imageSizeInPixels = CGSizeMake(imageSize.width * image.scale, imageSize.height * image.scale);
CGSize boundsSizeInPixels = CGSizeMake(std::floor(bounds.size.width * contentsScale), std::floor(bounds.size.height * contentsScale));

if (_debugLabelNode) {
CGFloat pixelCountRatio = (imageSizeInPixels.width * imageSizeInPixels.height) / (boundsSizeInPixels.width * boundsSizeInPixels.height);
if (pixelCountRatio != 1.0) {
NSString *scaleString = [NSString stringWithFormat:@"%.2fx", pixelCountRatio];
_debugLabelNode.attributedText = [[NSAttributedString alloc] initWithString:scaleString attributes:[self debugLabelAttributes]];
_debugLabelNode.hidden = NO;
[self setNeedsLayout];
} else {
_debugLabelNode.hidden = YES;
_debugLabelNode.attributedText = nil;
}
}

BOOL contentModeSupported = contentMode == UIViewContentModeScaleAspectFill ||
contentMode == UIViewContentModeScaleAspectFit ||
contentMode == UIViewContentModeCenter;
Expand Down Expand Up @@ -414,8 +405,8 @@ - (UIImage *)displayWithParameters:(id<NSObject>)parameter isCancelled:(asdispla
contentsKey.imageDrawRect = imageDrawRect;
contentsKey.isOpaque = isOpaque;
contentsKey.backgroundColor = backgroundColor;
contentsKey.preContextBlock = preContextBlock;
contentsKey.postContextBlock = postContextBlock;
contentsKey.willDisplayNodeContentWithRenderingContext = willDisplayNodeContentWithRenderingContext;
contentsKey.didDisplayNodeContentWithRenderingContext = didDisplayNodeContentWithRenderingContext;
contentsKey.imageModificationBlock = imageModificationBlock;

if (isCancelled()) {
Expand Down Expand Up @@ -479,8 +470,8 @@ + (UIImage *)createContentsForkey:(ASImageNodeContentsKey *)key isCancelled:(asd
BOOL contextIsClean = YES;

CGContextRef context = UIGraphicsGetCurrentContext();
if (context && key.preContextBlock) {
key.preContextBlock(context);
if (context && key.willDisplayNodeContentWithRenderingContext) {
key.willDisplayNodeContentWithRenderingContext(context);
contextIsClean = NO;
}

Expand Down Expand Up @@ -511,8 +502,8 @@ + (UIImage *)createContentsForkey:(ASImageNodeContentsKey *)key isCancelled:(asd
[image drawInRect:key.imageDrawRect blendMode:blendMode alpha:1];
}

if (context && key.postContextBlock) {
key.postContextBlock(context);
if (context && key.didDisplayNodeContentWithRenderingContext) {
key.didDisplayNodeContentWithRenderingContext(context);
}

// The following `UIGraphicsGetImageFromCurrentImageContext` call will commonly take more than 20ms on an
Expand Down Expand Up @@ -540,7 +531,25 @@ - (void)displayDidFinish
__instanceLock__.lock();
void (^displayCompletionBlock)(BOOL canceled) = _displayCompletionBlock;
UIImage *image = _image;
BOOL hasDebugLabel = (_debugLabelNode != nil);
__instanceLock__.unlock();

// Update the debug label if necessary
if (hasDebugLabel) {
// For debugging purposes we don't care about locking for now
CGSize imageSize = image.size;
CGSize imageSizeInPixels = CGSizeMake(imageSize.width * image.scale, imageSize.height * image.scale);
CGSize boundsSizeInPixels = CGSizeMake(std::floor(self.bounds.size.width * self.contentsScale), std::floor(self.bounds.size.height * self.contentsScale));
CGFloat pixelCountRatio = (imageSizeInPixels.width * imageSizeInPixels.height) / (boundsSizeInPixels.width * boundsSizeInPixels.height);
if (pixelCountRatio != 1.0) {
NSString *scaleString = [NSString stringWithFormat:@"%.2fx", pixelCountRatio];
_debugLabelNode.attributedText = [[NSAttributedString alloc] initWithString:scaleString attributes:[self debugLabelAttributes]];
_debugLabelNode.hidden = NO;
} else {
_debugLabelNode.hidden = YES;
_debugLabelNode.attributedText = nil;
}
}

// If we've got a block to perform after displaying, do it.
if (image && displayCompletionBlock) {
Expand Down

0 comments on commit 17d9564

Please sign in to comment.