2016-03-30 02:51:51 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTDisplayLink.h"
|
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
#import <QuartzCore/CADisplayLink.h>
|
|
|
|
|
|
|
|
#import "RCTAssert.h"
|
|
|
|
#import "RCTBridgeModule.h"
|
|
|
|
#import "RCTFrameUpdate.h"
|
|
|
|
#import "RCTModuleData.h"
|
|
|
|
#import "RCTProfile.h"
|
|
|
|
|
2016-07-11 15:25:43 +00:00
|
|
|
#define RCTAssertRunLoop() \
|
|
|
|
RCTAssert(_runLoop == [NSRunLoop currentRunLoop], \
|
|
|
|
@"This method must be called on the CADisplayLink run loop")
|
|
|
|
|
2016-03-30 02:51:51 +00:00
|
|
|
@implementation RCTDisplayLink
|
|
|
|
{
|
|
|
|
CADisplayLink *_jsDisplayLink;
|
|
|
|
NSMutableSet<RCTModuleData *> *_frameUpdateObservers;
|
|
|
|
NSRunLoop *_runLoop;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)init
|
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
|
|
|
_frameUpdateObservers = [NSMutableSet new];
|
|
|
|
_jsDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(_jsThreadUpdate:)];
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)registerModuleForFrameUpdates:(id<RCTBridgeModule>)module
|
|
|
|
withModuleData:(RCTModuleData *)moduleData
|
|
|
|
{
|
2016-07-11 15:25:43 +00:00
|
|
|
if (![moduleData.moduleClass conformsToProtocol:@protocol(RCTFrameUpdateObserver)] ||
|
|
|
|
[_frameUpdateObservers containsObject:moduleData]) {
|
2016-03-30 02:51:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
[_frameUpdateObservers addObject:moduleData];
|
2016-07-11 15:25:43 +00:00
|
|
|
|
2016-03-30 02:51:51 +00:00
|
|
|
// Don't access the module instance via moduleData, as this will cause deadlock
|
|
|
|
id<RCTFrameUpdateObserver> observer = (id<RCTFrameUpdateObserver>)module;
|
|
|
|
__weak typeof(self) weakSelf = self;
|
|
|
|
observer.pauseCallback = ^{
|
|
|
|
typeof(self) strongSelf = weakSelf;
|
|
|
|
if (!strongSelf) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CFRunLoopRef cfRunLoop = [strongSelf->_runLoop getCFRunLoop];
|
2016-07-11 15:25:43 +00:00
|
|
|
if (!cfRunLoop) {
|
2016-03-30 02:51:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-11 15:25:43 +00:00
|
|
|
if ([NSRunLoop currentRunLoop] == strongSelf->_runLoop) {
|
|
|
|
[weakSelf updateJSDisplayLinkState];
|
|
|
|
} else {
|
|
|
|
CFRunLoopPerformBlock(cfRunLoop, kCFRunLoopDefaultMode, ^{
|
|
|
|
[weakSelf updateJSDisplayLinkState];
|
|
|
|
});
|
|
|
|
CFRunLoopWakeUp(cfRunLoop);
|
|
|
|
}
|
2016-03-30 02:51:51 +00:00
|
|
|
};
|
2016-07-11 15:25:43 +00:00
|
|
|
|
|
|
|
// Assuming we're paused right now, we only need to update the display link's state
|
|
|
|
// when the new observer is not paused. If it not paused, the observer will immediately
|
|
|
|
// start receiving updates anyway.
|
|
|
|
if (![observer isPaused] && _runLoop) {
|
|
|
|
CFRunLoopPerformBlock([_runLoop getCFRunLoop], kCFRunLoopDefaultMode, ^{
|
|
|
|
[self updateJSDisplayLinkState];
|
|
|
|
});
|
|
|
|
}
|
2016-03-30 02:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)addToRunLoop:(NSRunLoop *)runLoop
|
|
|
|
{
|
|
|
|
_runLoop = runLoop;
|
|
|
|
[_jsDisplayLink addToRunLoop:runLoop forMode:NSRunLoopCommonModes];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)invalidate
|
|
|
|
{
|
|
|
|
[_jsDisplayLink invalidate];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dispatchBlock:(dispatch_block_t)block
|
|
|
|
queue:(dispatch_queue_t)queue
|
|
|
|
{
|
|
|
|
if (queue == RCTJSThread) {
|
|
|
|
block();
|
|
|
|
} else if (queue) {
|
|
|
|
dispatch_async(queue, block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)_jsThreadUpdate:(CADisplayLink *)displayLink
|
|
|
|
{
|
2016-07-11 15:25:43 +00:00
|
|
|
RCTAssertRunLoop();
|
2016-03-30 02:51:51 +00:00
|
|
|
|
2016-05-14 00:14:59 +00:00
|
|
|
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"-[RCTDisplayLink _jsThreadUpdate:]", nil);
|
2016-03-30 02:51:51 +00:00
|
|
|
|
|
|
|
RCTFrameUpdate *frameUpdate = [[RCTFrameUpdate alloc] initWithDisplayLink:displayLink];
|
|
|
|
for (RCTModuleData *moduleData in _frameUpdateObservers) {
|
|
|
|
id<RCTFrameUpdateObserver> observer = (id<RCTFrameUpdateObserver>)moduleData.instance;
|
|
|
|
if (!observer.paused) {
|
|
|
|
RCTProfileBeginFlowEvent();
|
|
|
|
|
|
|
|
[self dispatchBlock:^{
|
|
|
|
RCTProfileEndFlowEvent();
|
|
|
|
[observer didUpdateFrame:frameUpdate];
|
|
|
|
} queue:moduleData.methodQueue];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[self updateJSDisplayLinkState];
|
|
|
|
|
2016-05-14 00:14:59 +00:00
|
|
|
RCTProfileImmediateEvent(RCTProfileTagAlways, @"JS Thread Tick", displayLink.timestamp, 'g');
|
2016-03-30 02:51:51 +00:00
|
|
|
|
2016-09-05 18:11:37 +00:00
|
|
|
RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"objc_call");
|
2016-03-30 02:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateJSDisplayLinkState
|
|
|
|
{
|
2016-07-11 15:25:43 +00:00
|
|
|
RCTAssertRunLoop();
|
2016-03-30 02:51:51 +00:00
|
|
|
|
|
|
|
BOOL pauseDisplayLink = YES;
|
|
|
|
for (RCTModuleData *moduleData in _frameUpdateObservers) {
|
|
|
|
id<RCTFrameUpdateObserver> observer = (id<RCTFrameUpdateObserver>)moduleData.instance;
|
|
|
|
if (!observer.paused) {
|
|
|
|
pauseDisplayLink = NO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-07-11 15:25:43 +00:00
|
|
|
|
2016-03-30 02:51:51 +00:00
|
|
|
_jsDisplayLink.paused = pauseDisplayLink;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|