2019-06-24 13:40:38 +03:00
|
|
|
/**
|
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
|
CocoaPods frameworks compatibility: Step 2 (#25619)
Summary:
This is my proposal for fixing `use_frameworks!` compatibility without breaking all `<React/*>` imports I outlined in https://github.com/facebook/react-native/pull/25393#issuecomment-508457700. If accepted, it will fix https://github.com/facebook/react-native/issues/25349.
It builds on the changes I made in https://github.com/facebook/react-native/pull/25496 by ensuring each podspec has a unique value for `header_dir` so that framework imports do not conflict. Every podspec which should be included in the `<React/*>` namespace now includes it's headers from `React-Core.podspec`.
The following pods can still be imported with `<React/*>` and so should not have breaking changes: `React-ART`,`React-DevSupport`, `React-CoreModules`, `React-RCTActionSheet`, `React-RCTAnimation`, `React-RCTBlob`, `React-RCTImage`, `React-RCTLinking`, `React-RCTNetwork`, `React-RCTPushNotification`, `React-RCTSettings`, `React-RCTText`, `React-RCTSettings`, `React-RCTVibration`, `React-RCTWebSocket` .
There are still a few breaking changes which I hope will be acceptable:
- `React-Core.podspec` has been moved to the root of the project. Any `Podfile` that references it will need to update the path.
- ~~`React-turbomodule-core`'s headers now live under `<turbomodule/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511091823.
- ~~`React-turbomodulesamples`'s headers now live under `<turbomodulesamples/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511091823.
- ~~`React-TypeSaferty`'s headers now live under `<TypeSafety/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511040967.
- ~~`React-jscallinvoker`'s headers now live under `<jscallinvoker/*>`~~ Replaced by https://github.com/facebook/react-native/pull/25619#issuecomment-511091823.
- Each podspec now uses `s.static_framework = true`. This means that a minimum of CocoaPods 1.5 ([released in April 2018](http://blog.cocoapods.org/CocoaPods-1.5.0/)) is now required. This is needed so that the ` __has_include` conditions can still work when frameworks are enabled.
Still to do:
- ~~Including `React-turbomodule-core` with `use_frameworks!` enabled causes the C++ import failures we saw in https://github.com/facebook/react-native/issues/25349. I'm sure it will be possible to fix this but I need to dig deeper (perhaps a custom modulemap would be needed).~~ Addressed by https://github.com/facebook/react-native/pull/25619/commits/33573511f02f3502a28bad48e085e9a4b8608302.
- I haven't got Fabric working yet. I wonder if it would be acceptable to move Fabric out of the `<React/*>` namespace since it is new? �
## Changelog
[iOS] [Fixed] - Fixed compatibility with CocoaPods frameworks.
Pull Request resolved: https://github.com/facebook/react-native/pull/25619
Test Plan:
### FB
```
buck build catalyst
```
### Sample Project
Everything should work exactly as before, where `use_frameworks!` is not in `Podfile`s. I have a branch on my [sample project](https://github.com/jtreanor/react-native-cocoapods-frameworks) here which has `use_frameworks!` in its `Podfile` to demonstrate this is fixed.
You can see that it works with these steps:
1. `git clone git@github.com:jtreanor/react-native-cocoapods-frameworks.git`
2. `git checkout fix-frameworks-subspecs`
3. `cd ios && pod install`
4. `cd .. && react-native run-ios`
The sample app will build and run successfully. To see that it still works without frameworks, remove `use_frameworks!` from the `Podfile` and do steps 3 and 4 again.
### RNTesterPods
`RNTesterPodsPods` can now work with or without `use_frameworks!`.
1. Go to the `RNTester` directory and run `pod install`.
2. Run the tests in `RNTesterPods.xcworkspace` to see that everything still works fine.
3. Uncomment the `use_frameworks!` line at the top of `RNTester/Podfile` and run `pod install` again.
4. Run the tests again and see that it still works with frameworks enabled.
Reviewed By: PeteTheHeat
Differential Revision: D16465247
Pulled By: PeteTheHeat
fbshipit-source-id: cad837e9cced06d30cc5b372af1c65c7780b9e7a
2019-07-25 08:26:42 +03:00
|
|
|
#import <React/RCTUIImageViewAnimated.h>
|
2019-08-22 20:14:58 +03:00
|
|
|
#import <React/RCTWeakProxy.h>
|
2019-06-24 13:40:38 +03:00
|
|
|
|
|
|
|
#import <mach/mach.h>
|
|
|
|
#import <objc/runtime.h>
|
|
|
|
|
2019-09-08 05:02:57 +03:00
|
|
|
static BOOL weakProxyEnabled = YES;
|
|
|
|
void RCTUIImageViewEnableWeakProxy(BOOL enabled) {
|
|
|
|
weakProxyEnabled = enabled;
|
|
|
|
}
|
|
|
|
|
2019-06-24 13:40:38 +03:00
|
|
|
static NSUInteger RCTDeviceTotalMemory() {
|
|
|
|
return (NSUInteger)[[NSProcessInfo processInfo] physicalMemory];
|
|
|
|
}
|
|
|
|
|
|
|
|
static NSUInteger RCTDeviceFreeMemory() {
|
|
|
|
mach_port_t host_port = mach_host_self();
|
|
|
|
mach_msg_type_number_t host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
|
|
|
|
vm_size_t page_size;
|
|
|
|
vm_statistics_data_t vm_stat;
|
|
|
|
kern_return_t kern;
|
|
|
|
|
|
|
|
kern = host_page_size(host_port, &page_size);
|
|
|
|
if (kern != KERN_SUCCESS) return 0;
|
|
|
|
kern = host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size);
|
|
|
|
if (kern != KERN_SUCCESS) return 0;
|
2019-10-01 22:04:13 +03:00
|
|
|
return (vm_stat.free_count - vm_stat.speculative_count) * page_size;
|
2019-06-24 13:40:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@interface RCTUIImageViewAnimated () <CALayerDelegate>
|
|
|
|
|
|
|
|
@property (nonatomic, assign) NSUInteger maxBufferSize;
|
|
|
|
@property (nonatomic, strong, readwrite) UIImage *currentFrame;
|
|
|
|
@property (nonatomic, assign, readwrite) NSUInteger currentFrameIndex;
|
|
|
|
@property (nonatomic, assign, readwrite) NSUInteger currentLoopCount;
|
|
|
|
@property (nonatomic, assign) NSUInteger totalFrameCount;
|
|
|
|
@property (nonatomic, assign) NSUInteger totalLoopCount;
|
|
|
|
@property (nonatomic, strong) UIImage<RCTAnimatedImage> *animatedImage;
|
|
|
|
@property (nonatomic, strong) NSMutableDictionary<NSNumber *, UIImage *> *frameBuffer;
|
|
|
|
@property (nonatomic, assign) NSTimeInterval currentTime;
|
|
|
|
@property (nonatomic, assign) BOOL bufferMiss;
|
|
|
|
@property (nonatomic, assign) NSUInteger maxBufferCount;
|
|
|
|
@property (nonatomic, strong) NSOperationQueue *fetchQueue;
|
|
|
|
@property (nonatomic, strong) dispatch_semaphore_t lock;
|
|
|
|
@property (nonatomic, assign) CGFloat animatedImageScale;
|
|
|
|
@property (nonatomic, strong) CADisplayLink *displayLink;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTUIImageViewAnimated
|
|
|
|
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
|
|
|
{
|
|
|
|
if (self = [super initWithFrame:frame]) {
|
|
|
|
self.lock = dispatch_semaphore_create(1);
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
|
|
|
|
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)resetAnimatedImage
|
|
|
|
{
|
|
|
|
self.animatedImage = nil;
|
|
|
|
self.totalFrameCount = 0;
|
|
|
|
self.totalLoopCount = 0;
|
|
|
|
self.currentFrame = nil;
|
|
|
|
self.currentFrameIndex = 0;
|
|
|
|
self.currentLoopCount = 0;
|
|
|
|
self.currentTime = 0;
|
|
|
|
self.bufferMiss = NO;
|
|
|
|
self.maxBufferCount = 0;
|
|
|
|
self.animatedImageScale = 1;
|
|
|
|
[_fetchQueue cancelAllOperations];
|
|
|
|
_fetchQueue = nil;
|
|
|
|
dispatch_semaphore_wait(self.lock, DISPATCH_TIME_FOREVER);
|
|
|
|
[_frameBuffer removeAllObjects];
|
|
|
|
_frameBuffer = nil;
|
|
|
|
dispatch_semaphore_signal(self.lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setImage:(UIImage *)image
|
|
|
|
{
|
|
|
|
if (self.image == image) {
|
|
|
|
return;
|
|
|
|
}
|
2019-07-23 17:04:34 +03:00
|
|
|
|
|
|
|
[self stop];
|
|
|
|
[self resetAnimatedImage];
|
2019-06-24 13:40:38 +03:00
|
|
|
|
|
|
|
if ([image respondsToSelector:@selector(animatedImageFrameAtIndex:)]) {
|
|
|
|
NSUInteger animatedImageFrameCount = ((UIImage<RCTAnimatedImage> *)image).animatedImageFrameCount;
|
|
|
|
|
2019-07-18 17:17:25 +03:00
|
|
|
// In case frame count is 0, there is no reason to continue.
|
|
|
|
if (animatedImageFrameCount == 0) {
|
2019-06-24 13:40:38 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.animatedImage = (UIImage<RCTAnimatedImage> *)image;
|
|
|
|
self.totalFrameCount = animatedImageFrameCount;
|
|
|
|
|
|
|
|
// Get the current frame and loop count.
|
|
|
|
self.totalLoopCount = self.animatedImage.animatedImageLoopCount;
|
|
|
|
|
|
|
|
self.animatedImageScale = image.scale;
|
|
|
|
|
|
|
|
self.currentFrame = image;
|
|
|
|
|
|
|
|
dispatch_semaphore_wait(self.lock, DISPATCH_TIME_FOREVER);
|
|
|
|
self.frameBuffer[@(self.currentFrameIndex)] = self.currentFrame;
|
|
|
|
dispatch_semaphore_signal(self.lock);
|
|
|
|
|
|
|
|
// Calculate max buffer size
|
|
|
|
[self calculateMaxBufferCount];
|
|
|
|
|
|
|
|
if ([self paused]) {
|
|
|
|
[self start];
|
|
|
|
}
|
|
|
|
|
|
|
|
[self.layer setNeedsDisplay];
|
|
|
|
} else {
|
|
|
|
super.image = image;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Private
|
|
|
|
|
|
|
|
- (NSOperationQueue *)fetchQueue
|
|
|
|
{
|
|
|
|
if (!_fetchQueue) {
|
|
|
|
_fetchQueue = [[NSOperationQueue alloc] init];
|
|
|
|
_fetchQueue.maxConcurrentOperationCount = 1;
|
|
|
|
}
|
|
|
|
return _fetchQueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSMutableDictionary<NSNumber *,UIImage *> *)frameBuffer
|
|
|
|
{
|
|
|
|
if (!_frameBuffer) {
|
|
|
|
_frameBuffer = [NSMutableDictionary dictionary];
|
|
|
|
}
|
|
|
|
return _frameBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CADisplayLink *)displayLink
|
|
|
|
{
|
2019-09-24 01:18:00 +03:00
|
|
|
// TODO (T53783620): This logic should always be enabled.
|
|
|
|
if (weakProxyEnabled) {
|
|
|
|
// We only need a displayLink in the case of animated images, so short-circuit this code and don't create one for most of the use cases.
|
|
|
|
// Since this class is used for all RCTImageView's, this is especially important.
|
|
|
|
if (!_animatedImage) {
|
|
|
|
return nil;
|
|
|
|
}
|
2019-08-22 20:35:07 +03:00
|
|
|
}
|
|
|
|
|
2019-06-24 13:40:38 +03:00
|
|
|
if (!_displayLink) {
|
2019-09-08 05:02:57 +03:00
|
|
|
__weak typeof(self) weakSelf = self;
|
2019-09-24 01:18:00 +03:00
|
|
|
// TODO (T53783620): This logic should always be enabled.
|
2019-09-08 05:02:57 +03:00
|
|
|
id target = weakProxyEnabled ? [RCTWeakProxy weakProxyWithTarget:self] : weakSelf;
|
|
|
|
_displayLink = [CADisplayLink displayLinkWithTarget:target selector:@selector(displayDidRefresh:)];
|
2019-06-24 13:40:38 +03:00
|
|
|
NSString *runLoopMode = [NSProcessInfo processInfo].activeProcessorCount > 1 ? NSRunLoopCommonModes : NSDefaultRunLoopMode;
|
|
|
|
[_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:runLoopMode];
|
|
|
|
}
|
|
|
|
return _displayLink;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Animation
|
|
|
|
|
|
|
|
- (void)start
|
|
|
|
{
|
|
|
|
self.displayLink.paused = NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)stop
|
|
|
|
{
|
|
|
|
self.displayLink.paused = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)paused
|
|
|
|
{
|
|
|
|
return self.displayLink.isPaused;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)displayDidRefresh:(CADisplayLink *)displayLink
|
|
|
|
{
|
2019-07-04 20:27:03 +03:00
|
|
|
#if TARGET_OS_UIKITFORMAC
|
|
|
|
// TODO: `displayLink.frameInterval` is not available on UIKitForMac
|
|
|
|
NSTimeInterval duration = displayLink.duration;
|
|
|
|
#else
|
2019-06-24 13:40:38 +03:00
|
|
|
NSTimeInterval duration = displayLink.duration * displayLink.frameInterval;
|
2019-07-04 20:27:03 +03:00
|
|
|
#endif
|
2019-06-24 13:40:38 +03:00
|
|
|
NSUInteger totalFrameCount = self.totalFrameCount;
|
|
|
|
NSUInteger currentFrameIndex = self.currentFrameIndex;
|
|
|
|
NSUInteger nextFrameIndex = (currentFrameIndex + 1) % totalFrameCount;
|
|
|
|
|
|
|
|
// Check if we have the frame buffer firstly to improve performance
|
|
|
|
if (!self.bufferMiss) {
|
|
|
|
// Then check if timestamp is reached
|
|
|
|
self.currentTime += duration;
|
|
|
|
NSTimeInterval currentDuration = [self.animatedImage animatedImageDurationAtIndex:currentFrameIndex];
|
|
|
|
if (self.currentTime < currentDuration) {
|
|
|
|
// Current frame timestamp not reached, return
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.currentTime -= currentDuration;
|
|
|
|
NSTimeInterval nextDuration = [self.animatedImage animatedImageDurationAtIndex:nextFrameIndex];
|
|
|
|
if (self.currentTime > nextDuration) {
|
|
|
|
// Do not skip frame
|
|
|
|
self.currentTime = nextDuration;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the current frame
|
|
|
|
UIImage *currentFrame;
|
|
|
|
UIImage *fetchFrame;
|
|
|
|
dispatch_semaphore_wait(self.lock, DISPATCH_TIME_FOREVER);
|
|
|
|
currentFrame = self.frameBuffer[@(currentFrameIndex)];
|
|
|
|
fetchFrame = currentFrame ? self.frameBuffer[@(nextFrameIndex)] : nil;
|
|
|
|
dispatch_semaphore_signal(self.lock);
|
|
|
|
BOOL bufferFull = NO;
|
|
|
|
if (currentFrame) {
|
|
|
|
dispatch_semaphore_wait(self.lock, DISPATCH_TIME_FOREVER);
|
|
|
|
// Remove the frame buffer if need
|
|
|
|
if (self.frameBuffer.count > self.maxBufferCount) {
|
|
|
|
self.frameBuffer[@(currentFrameIndex)] = nil;
|
|
|
|
}
|
|
|
|
// Check whether we can stop fetch
|
|
|
|
if (self.frameBuffer.count == totalFrameCount) {
|
|
|
|
bufferFull = YES;
|
|
|
|
}
|
|
|
|
dispatch_semaphore_signal(self.lock);
|
|
|
|
self.currentFrame = currentFrame;
|
|
|
|
self.currentFrameIndex = nextFrameIndex;
|
|
|
|
self.bufferMiss = NO;
|
|
|
|
[self.layer setNeedsDisplay];
|
|
|
|
} else {
|
|
|
|
self.bufferMiss = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the loop count when last frame rendered
|
|
|
|
if (nextFrameIndex == 0 && !self.bufferMiss) {
|
|
|
|
// Update the loop count
|
|
|
|
self.currentLoopCount++;
|
|
|
|
// if reached the max loop count, stop animating, 0 means loop indefinitely
|
|
|
|
NSUInteger maxLoopCount = self.totalLoopCount;
|
|
|
|
if (maxLoopCount != 0 && (self.currentLoopCount >= maxLoopCount)) {
|
|
|
|
[self stop];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we should prefetch next frame or current frame
|
|
|
|
NSUInteger fetchFrameIndex;
|
|
|
|
if (self.bufferMiss) {
|
|
|
|
// When buffer miss, means the decode speed is slower than render speed, we fetch current miss frame
|
|
|
|
fetchFrameIndex = currentFrameIndex;
|
|
|
|
} else {
|
|
|
|
// Or, most cases, the decode speed is faster than render speed, we fetch next frame
|
|
|
|
fetchFrameIndex = nextFrameIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fetchFrame && !bufferFull && self.fetchQueue.operationCount == 0) {
|
|
|
|
// Prefetch next frame in background queue
|
|
|
|
UIImage<RCTAnimatedImage> *animatedImage = self.animatedImage;
|
|
|
|
NSOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
|
|
|
|
UIImage *frame = [animatedImage animatedImageFrameAtIndex:fetchFrameIndex];
|
|
|
|
dispatch_semaphore_wait(self.lock, DISPATCH_TIME_FOREVER);
|
|
|
|
self.frameBuffer[@(fetchFrameIndex)] = frame;
|
|
|
|
dispatch_semaphore_signal(self.lock);
|
|
|
|
}];
|
|
|
|
[self.fetchQueue addOperation:operation];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - CALayerDelegate
|
|
|
|
|
|
|
|
- (void)displayLayer:(CALayer *)layer
|
|
|
|
{
|
|
|
|
if (_currentFrame) {
|
|
|
|
layer.contentsScale = self.animatedImageScale;
|
|
|
|
layer.contents = (__bridge id)_currentFrame.CGImage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Util
|
|
|
|
|
|
|
|
- (void)calculateMaxBufferCount
|
|
|
|
{
|
|
|
|
NSUInteger bytes = CGImageGetBytesPerRow(self.currentFrame.CGImage) * CGImageGetHeight(self.currentFrame.CGImage);
|
|
|
|
if (bytes == 0) bytes = 1024;
|
|
|
|
|
|
|
|
NSUInteger max = 0;
|
|
|
|
if (self.maxBufferSize > 0) {
|
|
|
|
max = self.maxBufferSize;
|
|
|
|
} else {
|
|
|
|
// Calculate based on current memory, these factors are by experience
|
|
|
|
NSUInteger total = RCTDeviceTotalMemory();
|
|
|
|
NSUInteger free = RCTDeviceFreeMemory();
|
|
|
|
max = MIN(total * 0.2, free * 0.6);
|
|
|
|
}
|
|
|
|
|
|
|
|
NSUInteger maxBufferCount = (double)max / (double)bytes;
|
|
|
|
if (!maxBufferCount) {
|
|
|
|
// At least 1 frame
|
|
|
|
maxBufferCount = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.maxBufferCount = maxBufferCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Lifecycle
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
// Removes the display link from all run loop modes.
|
|
|
|
[_displayLink invalidate];
|
|
|
|
_displayLink = nil;
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)didReceiveMemoryWarning:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
[_fetchQueue cancelAllOperations];
|
|
|
|
[_fetchQueue addOperationWithBlock:^{
|
|
|
|
NSNumber *currentFrameIndex = @(self.currentFrameIndex);
|
|
|
|
dispatch_semaphore_wait(self.lock, DISPATCH_TIME_FOREVER);
|
|
|
|
NSArray *keys = self.frameBuffer.allKeys;
|
|
|
|
// only keep the next frame for later rendering
|
|
|
|
for (NSNumber * key in keys) {
|
|
|
|
if (![key isEqualToNumber:currentFrameIndex]) {
|
|
|
|
[self.frameBuffer removeObjectForKey:key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dispatch_semaphore_signal(self.lock);
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|