Skip to content

Commit

Permalink
Cleanup ASDisplayLayer (TextureGroup#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
Adlai-Holler authored and bernieperez committed Apr 25, 2018
1 parent b5119d8 commit 6be94dc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 64 deletions.
4 changes: 2 additions & 2 deletions Source/Details/_ASDisplayLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@
+ (dispatch_queue_t)displayQueue;

/**
@summary Delegate for asynchronous display of the layer.
@summary Delegate for asynchronous display of the layer. This should be the node (default) unless you REALLY know what you're doing.
@desc The asyncDelegate will have the opportunity to override the methods related to async display.
*/
@property (nonatomic, weak) id<_ASDisplayLayerDelegate> asyncDelegate;
@property (atomic, weak) id<_ASDisplayLayerDelegate> asyncDelegate;

/**
@summary Suspends both asynchronous and synchronous display of the receiver if YES.
Expand Down
70 changes: 8 additions & 62 deletions Source/Details/_ASDisplayLayer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand All @@ -157,7 +117,6 @@ - (void)setNeedsDisplay
if (!_displaySuspended) {
[super setNeedsDisplay];
}
_displaySuspendedLock.unlock();
}

#pragma mark -
Expand All @@ -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
{
Expand All @@ -209,7 +169,7 @@ - (void)display
ASDisplayNodeAssertMainThread();
[self _hackResetNeedsDisplay];

if (self.isDisplaySuspended) {
if (self.displaySuspended) {
return;
}

Expand All @@ -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">>
Expand Down

0 comments on commit 6be94dc

Please sign in to comment.