-
Notifications
You must be signed in to change notification settings - Fork 512
Expand file tree
/
Copy pathPINDisplayLink.m
More file actions
142 lines (119 loc) · 3.21 KB
/
PINDisplayLink.m
File metadata and controls
142 lines (119 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// PINDisplayLink.m
// Pods
//
// Created by Garrett Moon on 4/23/18.
//
#import "PINDisplayLink.h"
#if PIN_TARGET_MAC
@interface PINDisplayLink ()
@property (nonatomic, readonly) id target;
@property (nonatomic, readonly) SEL selector;
@property (nonatomic, readonly) NSRunLoop *runloop;
@property (nonatomic, readonly) NSRunLoopMode mode;
- (void)displayLinkFiredWithDuration:(CFTimeInterval)duration;
@end
static CVReturn displayLinkFired (CVDisplayLinkRef displayLink,
const CVTimeStamp *inNow,
const CVTimeStamp *inOutputTime,
CVOptionFlags flagsIn,
CVOptionFlags *flagsOut,
void *displayLinkContext)
{
CFTimeInterval duration = inOutputTime->videoRefreshPeriod / (inOutputTime->videoTimeScale * inOutputTime->rateScalar);
PINDisplayLink *link = (__bridge PINDisplayLink *)displayLinkContext;
[link displayLinkFiredWithDuration:duration];
return kCVReturnSuccess;
}
@implementation PINDisplayLink
{
CVDisplayLinkRef _displayLinkRef;
BOOL _paused;
NSInteger _frameInterval;
CFTimeInterval _duration;
}
+ (PINDisplayLink *)displayLinkWithTarget:(id)target selector:(SEL)sel
{
return [[PINDisplayLink alloc] initWithTarget:target selector:sel];
}
- (PINDisplayLink *)initWithTarget:(id)target selector:(SEL)sel
{
if (self = [super init]) {
_target = target;
_selector = sel;
CVDisplayLinkCreateWithActiveCGDisplays(&_displayLinkRef);
CVDisplayLinkSetOutputCallback(_displayLinkRef, &displayLinkFired, (__bridge void * _Nullable)(self));
}
return self;
}
- (void)dealloc
{
if (_displayLinkRef) {
CVDisplayLinkRelease(_displayLinkRef);
}
}
- (void)displayLinkFiredWithDuration:(CFTimeInterval)duration
{
dispatch_async(dispatch_get_main_queue(), ^{
self.duration = duration;
[self.runloop performSelector:self.selector target:self.target argument:self order:NSUIntegerMax modes:@[self.mode]];
});
}
- (void)addToRunLoop:(NSRunLoop *)runloop forMode:(NSRunLoopMode)mode
{
PINAssertMain();
NSAssert(runloop && mode, @"Must set a runloop and a mode.");
_runloop = runloop;
_mode = mode;
if (_paused == NO) {
CVDisplayLinkStart(_displayLinkRef);
}
}
- (void)removeFromRunLoop:(NSRunLoop *)runloop forMode:(NSRunLoopMode)mode
{
_runloop = nil;
_mode = nil;
if (_paused == NO) {
CVDisplayLinkStop(_displayLinkRef);
}
}
- (BOOL)isPaused
{
PINAssertMain();
return _paused;
}
- (void)setPaused:(BOOL)paused
{
PINAssertMain();
if (_paused == paused) {
return;
}
_paused = paused;
if (paused) {
CVDisplayLinkStop(_displayLinkRef);
} else {
CVDisplayLinkStart(_displayLinkRef);
}
}
- (CFTimeInterval)duration
{
PINAssertMain();
return _duration;
}
- (void)setDuration:(CFTimeInterval)duration
{
PINAssertMain();
_duration = duration;
}
- (NSInteger)frameInterval
{
PINAssertMain();
return _frameInterval;
}
- (void)setFrameInterval:(NSInteger)frameInterval
{
PINAssertMain();
_frameInterval = frameInterval;
}
@end
#endif