RCTDisplayLink.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import "RCTDisplayLink.h"
  8. #import <Foundation/Foundation.h>
  9. #import <QuartzCore/CADisplayLink.h>
  10. #import "RCTAssert.h"
  11. #import "RCTBridgeModule.h"
  12. #import "RCTFrameUpdate.h"
  13. #import "RCTModuleData.h"
  14. #import "RCTProfile.h"
  15. #define RCTAssertRunLoop() \
  16. RCTAssert(_runLoop == [NSRunLoop currentRunLoop], \
  17. @"This method must be called on the CADisplayLink run loop")
  18. @implementation RCTDisplayLink
  19. {
  20. CADisplayLink *_jsDisplayLink;
  21. NSMutableSet<RCTModuleData *> *_frameUpdateObservers;
  22. NSRunLoop *_runLoop;
  23. }
  24. - (instancetype)init
  25. {
  26. if ((self = [super init])) {
  27. _frameUpdateObservers = [NSMutableSet new];
  28. _jsDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(_jsThreadUpdate:)];
  29. }
  30. return self;
  31. }
  32. - (void)registerModuleForFrameUpdates:(id<RCTBridgeModule>)module
  33. withModuleData:(RCTModuleData *)moduleData
  34. {
  35. if (![moduleData.moduleClass conformsToProtocol:@protocol(RCTFrameUpdateObserver)] ||
  36. [_frameUpdateObservers containsObject:moduleData]) {
  37. return;
  38. }
  39. [_frameUpdateObservers addObject:moduleData];
  40. // Don't access the module instance via moduleData, as this will cause deadlock
  41. id<RCTFrameUpdateObserver> observer = (id<RCTFrameUpdateObserver>)module;
  42. __weak typeof(self) weakSelf = self;
  43. observer.pauseCallback = ^{
  44. typeof(self) strongSelf = weakSelf;
  45. if (!strongSelf) {
  46. return;
  47. }
  48. CFRunLoopRef cfRunLoop = [strongSelf->_runLoop getCFRunLoop];
  49. if (!cfRunLoop) {
  50. return;
  51. }
  52. if ([NSRunLoop currentRunLoop] == strongSelf->_runLoop) {
  53. [weakSelf updateJSDisplayLinkState];
  54. } else {
  55. CFRunLoopPerformBlock(cfRunLoop, kCFRunLoopDefaultMode, ^{
  56. [weakSelf updateJSDisplayLinkState];
  57. });
  58. CFRunLoopWakeUp(cfRunLoop);
  59. }
  60. };
  61. // Assuming we're paused right now, we only need to update the display link's state
  62. // when the new observer is not paused. If it not paused, the observer will immediately
  63. // start receiving updates anyway.
  64. if (![observer isPaused] && _runLoop) {
  65. CFRunLoopPerformBlock([_runLoop getCFRunLoop], kCFRunLoopDefaultMode, ^{
  66. [self updateJSDisplayLinkState];
  67. });
  68. }
  69. }
  70. - (void)addToRunLoop:(NSRunLoop *)runLoop
  71. {
  72. _runLoop = runLoop;
  73. [_jsDisplayLink addToRunLoop:runLoop forMode:NSRunLoopCommonModes];
  74. }
  75. - (void)invalidate
  76. {
  77. [_jsDisplayLink invalidate];
  78. }
  79. - (void)dispatchBlock:(dispatch_block_t)block
  80. queue:(dispatch_queue_t)queue
  81. {
  82. if (queue == RCTJSThread) {
  83. block();
  84. } else if (queue) {
  85. dispatch_async(queue, block);
  86. }
  87. }
  88. - (void)_jsThreadUpdate:(CADisplayLink *)displayLink
  89. {
  90. RCTAssertRunLoop();
  91. RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"-[RCTDisplayLink _jsThreadUpdate:]", nil);
  92. RCTFrameUpdate *frameUpdate = [[RCTFrameUpdate alloc] initWithDisplayLink:displayLink];
  93. for (RCTModuleData *moduleData in _frameUpdateObservers) {
  94. id<RCTFrameUpdateObserver> observer = (id<RCTFrameUpdateObserver>)moduleData.instance;
  95. if (!observer.paused) {
  96. RCTProfileBeginFlowEvent();
  97. [self dispatchBlock:^{
  98. RCTProfileEndFlowEvent();
  99. [observer didUpdateFrame:frameUpdate];
  100. } queue:moduleData.methodQueue];
  101. }
  102. }
  103. [self updateJSDisplayLinkState];
  104. RCTProfileImmediateEvent(RCTProfileTagAlways, @"JS Thread Tick", displayLink.timestamp, 'g');
  105. RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"objc_call");
  106. }
  107. - (void)updateJSDisplayLinkState
  108. {
  109. RCTAssertRunLoop();
  110. BOOL pauseDisplayLink = YES;
  111. for (RCTModuleData *moduleData in _frameUpdateObservers) {
  112. id<RCTFrameUpdateObserver> observer = (id<RCTFrameUpdateObserver>)moduleData.instance;
  113. if (!observer.paused) {
  114. pauseDisplayLink = NO;
  115. break;
  116. }
  117. }
  118. _jsDisplayLink.paused = pauseDisplayLink;
  119. }
  120. @end