What does CADisplayLink's timestamp property mean?

Hardware: iPhone 6


Program:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  ...
  globalDisplayLink = [[myWindow screen] displayLinkWithTarget:self selector:@selector(doFrame:)];
  [globalDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}

- (void)doFrame:(CADisplayLink *)displayLink
{
  static u32 frameNum = 0;

  static CFTimeInterval tPrev = (CFTimeInterval)0.0f;
  CFTimeInterval tNow = [displayLink timestamp];
  f32 dt = (f32)(tNow - tPrev);
  tPrev = tNow;

  static u64 machTPrev = 0;
  u64 machTNow = mach_absolute_time();
  u64 machDt = machTNow - machTPrev;
  machTPrev = machTNow;
  f32 machDtF32 = machTimeToSeconds(machDt);

  if(frameNum++ > 0)
  {
      NSLog(@"(%d)\tDisplayLink: %f\tMach: %f", frameNum, dt, machDtF32);
      if(frameNum == 3)
      {
            NSLog(@"sleeping..");
            [NSThread sleepForTimeInterval:WAIT_INTERVAL];
      }
  }
  ...
  [myEAGLContext presentRenderbuffer:GL_RENDERBUFFER];
}


Output (WAIT_INTERVAL = 5.0f):

(2) DisplayLink: 0.016675 Mach: 0.010766

(3) DisplayLink: 0.016674 Mach: 0.016691

sleeping..

(4) DisplayLink: 0.266828 Mach: 5.012117

(5) DisplayLink: 4.752381 Mach: 0.007276

(6) DisplayLink: 0.016714 Mach: 0.018646

Output (WAIT_INTERVAL = 0.02f):

(2) DisplayLink: 0.016700 Mach: 0.016742

(3) DisplayLink: 0.016662 Mach: 0.016679

sleeping..

(4) DisplayLink: 0.016658 Mach: 0.022046

(5) DisplayLink: 0.016705 Mach: 0.011410


Discussion:

The docs say about timestamp: "The time value associated with the last frame that was displayed". Additionally, it reads "The target can read the display link’s

timestamp
property to retrieve the time that the previous frame was displayed"
. But how does this make sense with our results?
  • WAIT_INTERVAL == 5.0f: Not sure how or why the 5s timestep was broken up into 0.26s + 4.75s over 2 frames. There is no way this can represent the time when the last frame was shown.
  • WAIT_INTERVAL == 0.02f: How does this not even register?
  • Overall (without the sleeping), [CADisplayLink duration] seems to cover much closer to 16.6ms, where dt computated via mach_absolute_time() has much more variance, regularilly dipping close to 14ms, and going above 20ms. I thought mach_absolute_time() was highest res clock available on iOS?

Replies

Good question. Anyone know how to explain this?