|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and 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 | + |
| 8 | +#include "AppleEventBeat.h" |
| 9 | + |
| 10 | +#import <QuartzCore/QuartzCore.h> |
| 11 | +#import <React/RCTUtils.h> |
| 12 | + |
| 13 | +#include <react/debug/react_native_assert.h> |
| 14 | + |
| 15 | +/* |
| 16 | + * A zero-sized layer whose only purpose is to run a callback during the |
| 17 | + * display phase of a Core Animation commit. Core Animation processes a commit |
| 18 | + * as layout → display → (repeat until stable) → commit, so a layer marked as |
| 19 | + * needing display during the layout phase has its `display` called after the |
| 20 | + * whole layout pass but before the transaction is committed. |
| 21 | + */ |
| 22 | +@interface RCTEventBeatFlusherLayer : CALayer |
| 23 | +- (instancetype)initWithOnDisplay:(void (^)(void))onDisplay; |
| 24 | +@end |
| 25 | + |
| 26 | +@implementation RCTEventBeatFlusherLayer { |
| 27 | + void (^_onDisplay)(void); |
| 28 | +} |
| 29 | + |
| 30 | +- (instancetype)initWithOnDisplay:(void (^)(void))onDisplay |
| 31 | +{ |
| 32 | + if (self = [super init]) { |
| 33 | + _onDisplay = [onDisplay copy]; |
| 34 | + self.frame = CGRectZero; |
| 35 | + } |
| 36 | + return self; |
| 37 | +} |
| 38 | + |
| 39 | +- (void)display |
| 40 | +{ |
| 41 | + _onDisplay(); |
| 42 | +} |
| 43 | + |
| 44 | +// The layer is not a visual element; never participate in animations. |
| 45 | +- (id<CAAction>)actionForKey:(NSString *)event |
| 46 | +{ |
| 47 | + return nil; |
| 48 | +} |
| 49 | + |
| 50 | +@end |
| 51 | + |
| 52 | +namespace facebook::react { |
| 53 | + |
| 54 | +AppleEventBeat::AppleEventBeat(std::shared_ptr<OwnerBox> ownerBox, |
| 55 | + std::unique_ptr<const RunLoopObserver> uiRunLoopObserver, |
| 56 | + RuntimeScheduler &runtimeScheduler, |
| 57 | + WindowLayerResolver windowLayerResolver) |
| 58 | + : EventBeat(std::move(ownerBox), runtimeScheduler), |
| 59 | + uiRunLoopObserver_(std::move(uiRunLoopObserver)), |
| 60 | + windowLayerResolver_(std::move(windowLayerResolver)), |
| 61 | + layers_([NSMapTable weakToStrongObjectsMapTable]) |
| 62 | +{ |
| 63 | + std::weak_ptr<const void> weakOwner = ownerBox_->owner; |
| 64 | + onDisplay_ = ^{ |
| 65 | + // The owner (indirectly) retains the event beat; if it is gone, so is |
| 66 | + // the beat this induces. |
| 67 | + auto owner = weakOwner.lock(); |
| 68 | + if (!owner) { |
| 69 | + return; |
| 70 | + } |
| 71 | + this->induce(); |
| 72 | + }; |
| 73 | + |
| 74 | + uiRunLoopObserver_->setDelegate(this); |
| 75 | + uiRunLoopObserver_->enable(); |
| 76 | +} |
| 77 | + |
| 78 | +AppleEventBeat::~AppleEventBeat() |
| 79 | +{ |
| 80 | + // The beat can be destroyed on any thread; layer mutations belong on the |
| 81 | + // main thread. The block only retains the layers, and a display happening |
| 82 | + // before it executes is made safe by the owner check above. |
| 83 | + NSMapTable<CALayer *, RCTEventBeatFlusherLayer *> *layers = layers_; |
| 84 | + RCTExecuteOnMainQueue(^{ |
| 85 | + for (RCTEventBeatFlusherLayer *layer in layers.objectEnumerator) { |
| 86 | + [layer removeFromSuperlayer]; |
| 87 | + } |
| 88 | + [layers removeAllObjects]; |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +void AppleEventBeat::requestSynchronous(Tag tag) const |
| 93 | +{ |
| 94 | + EventBeat::requestSynchronous(tag); |
| 95 | + |
| 96 | + if (tag == kNoTag || !RCTIsMainQueue()) { |
| 97 | + return; |
| 98 | + } |
| 99 | + CALayer *hostLayer = windowLayerResolver_ ? windowLayerResolver_(tag) : nil; |
| 100 | + if (hostLayer == nil) { |
| 101 | + return; |
| 102 | + } |
| 103 | + RCTEventBeatFlusherLayer *layer = [layers_ objectForKey:hostLayer]; |
| 104 | + if (layer == nil) { |
| 105 | + layer = [[RCTEventBeatFlusherLayer alloc] initWithOnDisplay:onDisplay_]; |
| 106 | + [layers_ setObject:layer forKey:hostLayer]; |
| 107 | + } |
| 108 | + if (layer.superlayer != hostLayer) { |
| 109 | + [layer removeFromSuperlayer]; |
| 110 | + [hostLayer addSublayer:layer]; |
| 111 | + } |
| 112 | + [layer setNeedsDisplay]; |
| 113 | +} |
| 114 | + |
| 115 | +void AppleEventBeat::activityDidChange(const RunLoopObserver::Delegate *delegate, |
| 116 | + RunLoopObserver::Activity /*activity*/) const noexcept |
| 117 | +{ |
| 118 | + react_native_assert(delegate == this); |
| 119 | + induce(); |
| 120 | +} |
| 121 | + |
| 122 | +} // namespace facebook::react |
0 commit comments