Reputation: 2009
I've been playing with functionality of CADisplayLink
to programmatically monitor rendering performance in an iOS app. Also spent quite some time in Instruments, of course, but curious of what it looks like in the field and for specific screens in the app.
The documentation says that a CADisplayLink
timer object
allows your app to synchronize its drawing to the refresh rate of the display.
which makes me expect that the provided selector is called somewhat close to vsync
events emitted by the underlying hardware. Also, the timestamp
property is defined as follows:
The time interval that represents when the last frame displayed.
So I just use the two successive callbacks to figure out the on-screen duration of the frame and decide if there was a hitch or not, like so:
@objc private func handleDisplayUpdate(_ displayLink: CADisplayLink) {
defer { cachedTimestamp = displayLink.timestamp }
guard let cachedTimestamp = cachedTimestamp else { return }
let frameTime = displayLink.timestamp - cachedTimestamp
if frameTime > aThreshold {
// do some lightweight logging
}
}
By using CACurrentMediaTime()
call I can see that the handler is called pretty close to what the latest timestamp
holds, so it is 1ms-ish behind the recent frame update. However the frameTime
intervals are not always a multiple of 16.67ms (assuming the device renders @60fps): while scrolling I can observe the numbers being e.g. 25ms or 43ms.
Given that frames are swapped onto the display at vsync
points in time only, does the above mean that the timestamp
property actually represents something else in the whole render loop?
Upvotes: 2
Views: 415