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

[ASDisplayNode] Pass drawParameter in rendering context callbacks #248

Merged
merged 3 commits into from
May 8, 2017
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
- Move more properties from ASTableView, ASCollectionView to their respective node classes. [Adlai Holler](https://github.com/Adlai-Holler)
- Remove finalLayoutElement [Michael Schneider] (https://github.com/maicki)[#96](https://github.com/TextureGroup/Texture/pull/96)
- Add ASPageTable - A map table for fast retrieval of objects within a certain page [Huy Nguyen](https://github.com/nguyenhuy)
- [ASDisplayNode] Pass drawParameter in rendering context callbacks [Michael Schneider](https://github.com/maicki)[#248](https://github.com/TextureGroup/Texture/pull/248)
2 changes: 1 addition & 1 deletion Source/ASDisplayNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ typedef void (^ASDisplayNodeDidLoadBlock)(__kindof ASDisplayNode * node);
/**
* ASDisplayNode will / did render node content in context.
*/
typedef void (^ASDisplayNodeContextModifier)(CGContextRef context);
typedef void (^ASDisplayNodeContextModifier)(CGContextRef context, id _Nullable drawParameters);

/**
* ASDisplayNode layout spec block. This block can be used instead of implementing layoutSpecThatFits: in subclass
Expand Down
19 changes: 11 additions & 8 deletions Source/ASImageNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,11 @@ - (UIImage *)displayWithParameters:(id<NSObject>)parameter isCancelled:(asdispla
return nil;
}

ASWeakMapEntry<UIImage *> *entry = [self.class contentsForkey:contentsKey isCancelled:(asdisplaynode_iscancelled_block_t)isCancelled];
if (entry == nil) { // If nil, we were cancelled.
ASWeakMapEntry<UIImage *> *entry = [self.class contentsForkey:contentsKey
drawParameters:parameter
isCancelled:isCancelled];
// If nil, we were cancelled.
if (entry == nil) {
return nil;
}

Expand All @@ -428,7 +431,7 @@ - (UIImage *)displayWithParameters:(id<NSObject>)parameter isCancelled:(asdispla
static ASWeakMap<ASImageNodeContentsKey *, UIImage *> *cache = nil;
static ASDN::Mutex cacheLock;

+ (ASWeakMapEntry *)contentsForkey:(ASImageNodeContentsKey *)key isCancelled:(asdisplaynode_iscancelled_block_t)isCancelled
+ (ASWeakMapEntry *)contentsForkey:(ASImageNodeContentsKey *)key drawParameters:(id)drawParameters isCancelled:(asdisplaynode_iscancelled_block_t)isCancelled
{
{
ASDN::MutexLocker l(cacheLock);
Expand All @@ -443,7 +446,7 @@ + (ASWeakMapEntry *)contentsForkey:(ASImageNodeContentsKey *)key isCancelled:(as
}

// cache miss
UIImage *contents = [self createContentsForkey:key isCancelled:isCancelled];
UIImage *contents = [self createContentsForkey:key drawParameters:drawParameters isCancelled:isCancelled];
if (contents == nil) { // If nil, we were cancelled
return nil;
}
Expand All @@ -454,7 +457,7 @@ + (ASWeakMapEntry *)contentsForkey:(ASImageNodeContentsKey *)key isCancelled:(as
}
}

+ (UIImage *)createContentsForkey:(ASImageNodeContentsKey *)key isCancelled:(asdisplaynode_iscancelled_block_t)isCancelled
+ (UIImage *)createContentsForkey:(ASImageNodeContentsKey *)key drawParameters:(id)drawParameters isCancelled:(asdisplaynode_iscancelled_block_t)isCancelled
{
// The following `UIGraphicsBeginImageContextWithOptions` call will sometimes take take longer than 5ms on an
// A5 processor for a 400x800 backingSize.
Expand All @@ -471,7 +474,7 @@ + (UIImage *)createContentsForkey:(ASImageNodeContentsKey *)key isCancelled:(asd

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

Expand Down Expand Up @@ -503,7 +506,7 @@ + (UIImage *)createContentsForkey:(ASImageNodeContentsKey *)key isCancelled:(asd
}

if (context && key.didDisplayNodeContentWithRenderingContext) {
key.didDisplayNodeContentWithRenderingContext(context);
key.didDisplayNodeContentWithRenderingContext(context, drawParameters);
}

// The following `UIGraphicsGetImageFromCurrentImageContext` call will commonly take more than 20ms on an
Expand All @@ -517,7 +520,7 @@ + (UIImage *)createContentsForkey:(ASImageNodeContentsKey *)key isCancelled:(asd

UIGraphicsEndImageContext();

if (key.imageModificationBlock != NULL) {
if (key.imageModificationBlock) {
result = key.imageModificationBlock(result);
}

Expand Down
4 changes: 2 additions & 2 deletions Source/Private/ASDisplayNode+AsyncDisplay.mm
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ - (asyncdisplaykit_async_transaction_operation_block_t)_displayBlockWithAsynchro
// For -display methods, we don't have a context, and thus will not call the _willDisplayNodeContentWithRenderingContext or
// _didDisplayNodeContentWithRenderingContext blocks. It's up to the implementation of -display... to do what it needs.
if (willDisplayNodeContentWithRenderingContext != nil) {
willDisplayNodeContentWithRenderingContext(currentContext);
willDisplayNodeContentWithRenderingContext(currentContext, drawParameters);
}

// Decide if we use a class or instance method to draw or display.
Expand All @@ -266,7 +266,7 @@ - (asyncdisplaykit_async_transaction_operation_block_t)_displayBlockWithAsynchro
}

if (didDisplayNodeContentWithRenderingContext != nil) {
didDisplayNodeContentWithRenderingContext(currentContext);
didDisplayNodeContentWithRenderingContext(currentContext, drawParameters);
}

if (shouldCreateGraphicsContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class PhotoWithInsetTextOverlay : LayoutExampleNode {
backgroundColor = .clear

photoNode.url = URL(string: "http://asyncdisplaykit.org/static/images/layout-examples-photo-with-inset-text-overlay-photo.png")
photoNode.willDisplayNodeContentWithRenderingContext = { context in
photoNode.willDisplayNodeContentWithRenderingContext = { context, drawParameters in
let bounds = context.boundingBoxOfClipPath
UIBezierPath(roundedRect: bounds, cornerRadius: 10).addClip()
}
Expand Down
2 changes: 1 addition & 1 deletion examples/LayoutSpecExamples/Sample/LayoutExampleNodes.m
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ - (instancetype)init

_photoNode = [[ASNetworkImageNode alloc] init];
_photoNode.URL = [NSURL URLWithString:@"http://asyncdisplaykit.org/static/images/layout-examples-photo-with-inset-text-overlay-photo.png"];
_photoNode.willDisplayNodeContentWithRenderingContext = ^(CGContextRef context) {
_photoNode.willDisplayNodeContentWithRenderingContext = ^(CGContextRef context, id drawParameters) {
CGRect bounds = CGContextGetClipBoundingBox(context);
[[UIBezierPath bezierPathWithRoundedRect:bounds cornerRadius:10] addClip];
};
Expand Down