|
5 | 5 | #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterDisplayLink.h" |
6 | 6 |
|
7 | 7 | #import <AppKit/AppKit.h> |
| 8 | +#include <numeric> |
8 | 9 |
|
9 | 10 | #include "flutter/fml/synchronization/waitable_event.h" |
10 | 11 | #include "flutter/testing/testing.h" |
@@ -148,3 +149,32 @@ - (void)onDisplayLink:(CFTimeInterval)timestamp targetTimestamp:(CFTimeInterval) |
148 | 149 |
|
149 | 150 | [displayLink invalidate]; |
150 | 151 | } |
| 152 | + |
| 153 | +TEST(FlutterDisplayLinkTest, CVDisplayLinkInterval) { |
| 154 | + CVDisplayLinkRef link; |
| 155 | + CVDisplayLinkCreateWithCGDisplay(CGMainDisplayID(), &link); |
| 156 | + __block CFTimeInterval last = 0; |
| 157 | + __block auto intervals = std::make_unique<std::vector<CFTimeInterval>>(); |
| 158 | + __block auto event = std::make_unique<fml::AutoResetWaitableEvent>(); |
| 159 | + CVDisplayLinkSetOutputHandler( |
| 160 | + link, ^(CVDisplayLinkRef displayLink, const CVTimeStamp* inNow, |
| 161 | + const CVTimeStamp* inOutputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut) { |
| 162 | + if (last != 0) { |
| 163 | + intervals->push_back(CACurrentMediaTime() - last); |
| 164 | + } |
| 165 | + last = CACurrentMediaTime(); |
| 166 | + if (intervals->size() == 10) { |
| 167 | + event->Signal(); |
| 168 | + } |
| 169 | + return 0; |
| 170 | + }); |
| 171 | + |
| 172 | + CVDisplayLinkStart(link); |
| 173 | + event->Wait(); |
| 174 | + CVDisplayLinkStop(link); |
| 175 | + CVDisplayLinkRelease(link); |
| 176 | + CFTimeInterval average = std::reduce(intervals->begin(), intervals->end()) / intervals->size(); |
| 177 | + CFTimeInterval max = *std::max_element(intervals->begin(), intervals->end()); |
| 178 | + CFTimeInterval min = *std::min_element(intervals->begin(), intervals->end()); |
| 179 | + NSLog(@"CVDisplayLink Interval: Average: %fs, Max: %fs, Min: %fs", average, max, min); |
| 180 | +} |
0 commit comments