Change the CVDisplayLink to use the better way to get duration, fix warnings

This commit is contained in:
DreamPiggy 2019-10-02 14:56:44 +08:00
parent eb67ece614
commit f97c7d4d87
1 changed files with 11 additions and 6 deletions

View File

@ -580,7 +580,7 @@ static NSUInteger SDDeviceFreeMemory() {
} }
#if SD_MAC #if SD_MAC
- (void)displayDidRefresh:(CVDisplayLinkRef)displayLink duration:(NSTimeInterval)duration - (void)displayDidRefresh:(CVDisplayLinkRef)displayLink
#else #else
- (void)displayDidRefresh:(CADisplayLink *)displayLink - (void)displayDidRefresh:(CADisplayLink *)displayLink
#endif #endif
@ -590,9 +590,16 @@ static NSUInteger SDDeviceFreeMemory() {
if (!self.shouldAnimate) { if (!self.shouldAnimate) {
return; return;
} }
// Calculate refresh duration
#if SD_UIKIT #if SD_MAC
CVTimeStamp nowTime;
CVDisplayLinkGetCurrentTime(displayLink, &nowTime);
NSTimeInterval duration = (double)nowTime.videoRefreshPeriod / ((double)nowTime.videoTimeScale * nowTime.rateScalar);
#else
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
NSTimeInterval duration = displayLink.duration * displayLink.frameInterval; NSTimeInterval duration = displayLink.duration * displayLink.frameInterval;
#pragma clang diagnostic pop
#endif #endif
NSUInteger totalFrameCount = self.totalFrameCount; NSUInteger totalFrameCount = self.totalFrameCount;
NSUInteger currentFrameIndex = self.currentFrameIndex; NSUInteger currentFrameIndex = self.currentFrameIndex;
@ -786,13 +793,11 @@ static NSUInteger SDDeviceFreeMemory() {
#if SD_MAC #if SD_MAC
static CVReturn renderCallback(CVDisplayLinkRef displayLink, const CVTimeStamp *inNow, const CVTimeStamp *inOutputTime, CVOptionFlags flagsIn, CVOptionFlags *flagsOut, void *displayLinkContext) { static CVReturn renderCallback(CVDisplayLinkRef displayLink, const CVTimeStamp *inNow, const CVTimeStamp *inOutputTime, CVOptionFlags flagsIn, CVOptionFlags *flagsOut, void *displayLinkContext) {
// Calculate refresh duration
NSTimeInterval duration = (double)inOutputTime->videoRefreshPeriod / ((double)inOutputTime->videoTimeScale * inOutputTime->rateScalar);
// CVDisplayLink callback is not on main queue // CVDisplayLink callback is not on main queue
SDAnimatedImageView *imageView = (__bridge SDAnimatedImageView *)displayLinkContext; SDAnimatedImageView *imageView = (__bridge SDAnimatedImageView *)displayLinkContext;
__weak SDAnimatedImageView *weakImageView = imageView; __weak SDAnimatedImageView *weakImageView = imageView;
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
[weakImageView displayDidRefresh:displayLink duration:duration]; [weakImageView displayDidRefresh:displayLink];
}); });
return kCVReturnSuccess; return kCVReturnSuccess;
} }