Skip to content

Commit

Permalink
Pass drawParameter in rendering context callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
maicki committed May 6, 2017
1 parent 850aeb4 commit de8967b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
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

0 comments on commit de8967b

Please sign in to comment.