-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Clean Up ASDisplayLayer #trivial #315
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,64 +28,26 @@ | |
|
||
@implementation _ASDisplayLayer | ||
{ | ||
ASDN::Mutex _asyncDelegateLock; | ||
// We can take this lock when we're setting displaySuspended and in setNeedsDisplay, so to not deadlock, this is recursive | ||
ASDN::RecursiveMutex _displaySuspendedLock; | ||
BOOL _displaySuspended; | ||
BOOL _attemptedDisplayWhileZeroSized; | ||
|
||
struct { | ||
BOOL delegateDidChangeBounds:1; | ||
} _delegateFlags; | ||
|
||
id<_ASDisplayLayerDelegate> __weak _asyncDelegate; | ||
} | ||
|
||
@dynamic displaysAsynchronously; | ||
|
||
#pragma mark - | ||
#pragma mark Lifecycle | ||
|
||
- (instancetype)init | ||
{ | ||
if ((self = [super init])) { | ||
|
||
self.opaque = YES; | ||
} | ||
return self; | ||
} | ||
|
||
#pragma mark - | ||
#pragma mark Properties | ||
|
||
- (id<_ASDisplayLayerDelegate>)asyncDelegate | ||
{ | ||
ASDN::MutexLocker l(_asyncDelegateLock); | ||
return _asyncDelegate; | ||
} | ||
#pragma mark - Properties | ||
|
||
- (void)setDelegate:(id)delegate | ||
{ | ||
[super setDelegate:delegate]; | ||
_delegateFlags.delegateDidChangeBounds = [delegate respondsToSelector:@selector(layer:didChangeBoundsWithOldValue:newValue:)]; | ||
} | ||
|
||
- (void)setAsyncDelegate:(id<_ASDisplayLayerDelegate>)asyncDelegate | ||
{ | ||
ASDisplayNodeAssert(!asyncDelegate || [asyncDelegate isKindOfClass:[ASDisplayNode class]], @"_ASDisplayLayer is inherently coupled to ASDisplayNode and cannot be used with another asyncDelegate. Please rethink what you are trying to do."); | ||
ASDN::MutexLocker l(_asyncDelegateLock); | ||
_asyncDelegate = asyncDelegate; | ||
} | ||
|
||
- (BOOL)isDisplaySuspended | ||
{ | ||
ASDN::MutexLocker l(_displaySuspendedLock); | ||
return _displaySuspended; | ||
} | ||
|
||
- (void)setDisplaySuspended:(BOOL)displaySuspended | ||
{ | ||
ASDN::MutexLocker l(_displaySuspendedLock); | ||
ASDisplayNodeAssertMainThread(); | ||
if (_displaySuspended != displaySuspended) { | ||
_displaySuspended = displaySuspended; | ||
if (!displaySuspended) { | ||
|
@@ -146,8 +108,6 @@ - (void)layoutSublayers | |
- (void)setNeedsDisplay | ||
{ | ||
ASDisplayNodeAssertMainThread(); | ||
|
||
_displaySuspendedLock.lock(); | ||
|
||
// FIXME: Reconsider whether we should cancel a display in progress. | ||
// We should definitely cancel a display that is scheduled, but unstarted display. | ||
|
@@ -157,7 +117,6 @@ - (void)setNeedsDisplay | |
if (!_displaySuspended) { | ||
[super setNeedsDisplay]; | ||
} | ||
_displaySuspendedLock.unlock(); | ||
} | ||
|
||
#pragma mark - | ||
|
@@ -179,13 +138,14 @@ + (id)defaultValueForKey:(NSString *)key | |
{ | ||
if ([key isEqualToString:@"displaysAsynchronously"]) { | ||
return @YES; | ||
} else if ([key isEqualToString:@"opaque"]) { | ||
return @YES; | ||
} else { | ||
return [super defaultValueForKey:key]; | ||
} | ||
} | ||
|
||
#pragma mark - | ||
#pragma mark Display | ||
#pragma mark - Display | ||
|
||
- (void)displayImmediately | ||
{ | ||
|
@@ -209,7 +169,7 @@ - (void)display | |
ASDisplayNodeAssertMainThread(); | ||
[self _hackResetNeedsDisplay]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's time to rename this method. YYAsyncLayer does the same thing :). Maybe just _clearNeedsDisplayFlag There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed but too lazy to do it now 😴 |
||
|
||
if (self.isDisplaySuspended) { | ||
if (self.displaySuspended) { | ||
return; | ||
} | ||
|
||
|
@@ -221,29 +181,15 @@ - (void)display:(BOOL)asynchronously | |
if (CGRectIsEmpty(self.bounds)) { | ||
_attemptedDisplayWhileZeroSized = YES; | ||
} | ||
|
||
id<_ASDisplayLayerDelegate> NS_VALID_UNTIL_END_OF_SCOPE strongAsyncDelegate; | ||
{ | ||
_asyncDelegateLock.lock(); | ||
strongAsyncDelegate = _asyncDelegate; | ||
_asyncDelegateLock.unlock(); | ||
} | ||
|
||
[strongAsyncDelegate displayAsyncLayer:self asynchronously:asynchronously]; | ||
[self.asyncDelegate displayAsyncLayer:self asynchronously:asynchronously]; | ||
} | ||
|
||
- (void)cancelAsyncDisplay | ||
{ | ||
ASDisplayNodeAssertMainThread(); | ||
|
||
id<_ASDisplayLayerDelegate> NS_VALID_UNTIL_END_OF_SCOPE strongAsyncDelegate; | ||
{ | ||
_asyncDelegateLock.lock(); | ||
strongAsyncDelegate = _asyncDelegate; | ||
_asyncDelegateLock.unlock(); | ||
} | ||
|
||
[strongAsyncDelegate cancelDisplayAsyncLayer:self]; | ||
[self.asyncDelegate cancelDisplayAsyncLayer:self]; | ||
} | ||
|
||
// e.g. <MYTextNodeLayer: 0xFFFFFF; node = <MYTextNode: 0xFFFFFFE; name = "Username node for user 179">> | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not very familiar with the semantics of defaultValueForKey when used with standard layer properties like this. That said:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@appleguy In my testing, CA only queries this once per class and stores the result, and it also correctly handles overriding standard layer properties as far as I can tell. So the overall performance is probably better with this implementation, which for me overrides the point about a user understanding where an unexpected opacity came from. Thoughts?