Update test case for display link and revert the protect code

This commit is contained in:
DreamPiggy 2023-02-18 17:56:34 +08:00
parent 271e8d8b12
commit 3440b7c0fb
3 changed files with 24 additions and 7 deletions

View File

@ -15,7 +15,7 @@
@property (readonly, nonatomic, weak, nullable) id target;
@property (readonly, nonatomic, assign, nonnull) SEL selector;
@property (readonly, nonatomic) NSTimeInterval duration; // elapsed time in seconds of previous callback. (or it's first callback, use the time between `start` and callback)
@property (readonly, nonatomic) NSTimeInterval duration; // elapsed time in seconds of previous callback. (or it's first callback, use the time between `start` and callback). Always zero when display link not running
@property (readonly, nonatomic) BOOL isRunning;
+ (nonnull instancetype)displayLinkWithTarget:(nonnull id)target selector:(nonnull SEL)sel;

View File

@ -10,6 +10,8 @@
#import "SDWeakProxy.h"
#if SD_MAC
#import <CoreVideo/CoreVideo.h>
#elif SD_IOS || SD_TV
#import <QuartzCore/QuartzCore.h>
#endif
#include <mach/mach_time.h>
@ -92,14 +94,14 @@ static CFTimeInterval CACurrentMediaTime(void)
}
- (NSTimeInterval)duration {
NSTimeInterval duration;
NSTimeInterval duration = 0;
#if SD_MAC
CVTimeStamp outputTime = self.outputTime;
double periodPerSecond = (double)outputTime.videoTimeScale * outputTime.rateScalar;
duration = (double)outputTime.videoRefreshPeriod / periodPerSecond;
if (periodPerSecond > 0) {
duration = (double)outputTime.videoRefreshPeriod / periodPerSecond;
}
#else
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
// iOS 10+/watchOS use `nextTime`
if (@available(iOS 10.0, tvOS 10.0, watchOS 2.0, *)) {
duration = self.nextFireTime - CACurrentMediaTime();
@ -108,8 +110,25 @@ static CFTimeInterval CACurrentMediaTime(void)
duration = CACurrentMediaTime() - self.previousFireTime;
}
#endif
// When system sleep, the targetTimestamp will mass up, fallback refresh rate
if (duration < 0) {
#if SD_MAC
// Supports Pro display 120Hz
CGDirectDisplayID display = CVDisplayLinkGetCurrentCGDisplay(_displayLink);
CGDisplayModeRef mode = CGDisplayCopyDisplayMode(display);
double refreshRate = CGDisplayModeGetRefreshRate(mode);
if (refreshRate > 0) {
duration = 1.0 / refreshRate;
} else {
duration = kSDDisplayLinkInterval;
}
#elif SD_IOS || SD_TV
// Fallback
duration = self.displayLink.duration;
#else
// Watch always 60Hz
duration = kSDDisplayLinkInterval;
#endif
}
return duration;
}

View File

@ -48,8 +48,6 @@
XCTestExpectation *expectation1 = [self expectationWithDescription:@"Display Link Stop"];
XCTestExpectation *expectation2 = [self expectationWithDescription:@"Display Link Start"];
SDDisplayLink *displayLink = [SDDisplayLink displayLinkWithTarget:self selector:@selector(displayLinkDidRefresh:)];
NSTimeInterval duration = displayLink.duration; // Initial value
expect(duration).equal(1.0 / 60);
[displayLink addToRunLoop:NSRunLoop.mainRunLoop forMode:NSRunLoopCommonModes];
[displayLink start];
expect(displayLink.isRunning).beTruthy();