Skip to content

Commit 3440b7c

Browse files
committed
Update test case for display link and revert the protect code
1 parent 271e8d8 commit 3440b7c

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

SDWebImage/Private/SDDisplayLink.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
@property (readonly, nonatomic, weak, nullable) id target;
1717
@property (readonly, nonatomic, assign, nonnull) SEL selector;
18-
@property (readonly, nonatomic) NSTimeInterval duration; // elapsed time in seconds of previous callback. (or it's first callback, use the time between `start` and callback)
18+
@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
1919
@property (readonly, nonatomic) BOOL isRunning;
2020

2121
+ (nonnull instancetype)displayLinkWithTarget:(nonnull id)target selector:(nonnull SEL)sel;

SDWebImage/Private/SDDisplayLink.m

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#import "SDWeakProxy.h"
1111
#if SD_MAC
1212
#import <CoreVideo/CoreVideo.h>
13+
#elif SD_IOS || SD_TV
14+
#import <QuartzCore/QuartzCore.h>
1315
#endif
1416
#include <mach/mach_time.h>
1517

@@ -92,14 +94,14 @@ + (instancetype)displayLinkWithTarget:(id)target selector:(SEL)sel {
9294
}
9395

9496
- (NSTimeInterval)duration {
95-
NSTimeInterval duration;
97+
NSTimeInterval duration = 0;
9698
#if SD_MAC
9799
CVTimeStamp outputTime = self.outputTime;
98100
double periodPerSecond = (double)outputTime.videoTimeScale * outputTime.rateScalar;
99-
duration = (double)outputTime.videoRefreshPeriod / periodPerSecond;
101+
if (periodPerSecond > 0) {
102+
duration = (double)outputTime.videoRefreshPeriod / periodPerSecond;
103+
}
100104
#else
101-
#pragma clang diagnostic push
102-
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
103105
// iOS 10+/watchOS use `nextTime`
104106
if (@available(iOS 10.0, tvOS 10.0, watchOS 2.0, *)) {
105107
duration = self.nextFireTime - CACurrentMediaTime();
@@ -108,8 +110,25 @@ - (NSTimeInterval)duration {
108110
duration = CACurrentMediaTime() - self.previousFireTime;
109111
}
110112
#endif
113+
// When system sleep, the targetTimestamp will mass up, fallback refresh rate
111114
if (duration < 0) {
115+
#if SD_MAC
116+
// Supports Pro display 120Hz
117+
CGDirectDisplayID display = CVDisplayLinkGetCurrentCGDisplay(_displayLink);
118+
CGDisplayModeRef mode = CGDisplayCopyDisplayMode(display);
119+
double refreshRate = CGDisplayModeGetRefreshRate(mode);
120+
if (refreshRate > 0) {
121+
duration = 1.0 / refreshRate;
122+
} else {
123+
duration = kSDDisplayLinkInterval;
124+
}
125+
#elif SD_IOS || SD_TV
126+
// Fallback
127+
duration = self.displayLink.duration;
128+
#else
129+
// Watch always 60Hz
112130
duration = kSDDisplayLinkInterval;
131+
#endif
113132
}
114133
return duration;
115134
}

Tests/Tests/SDUtilsTests.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ - (void)testSDDisplayLink {
4848
XCTestExpectation *expectation1 = [self expectationWithDescription:@"Display Link Stop"];
4949
XCTestExpectation *expectation2 = [self expectationWithDescription:@"Display Link Start"];
5050
SDDisplayLink *displayLink = [SDDisplayLink displayLinkWithTarget:self selector:@selector(displayLinkDidRefresh:)];
51-
NSTimeInterval duration = displayLink.duration; // Initial value
52-
expect(duration).equal(1.0 / 60);
5351
[displayLink addToRunLoop:NSRunLoop.mainRunLoop forMode:NSRunLoopCommonModes];
5452
[displayLink start];
5553
expect(displayLink.isRunning).beTruthy();

0 commit comments

Comments
 (0)