import AppKit import Darwin import QuartzCore import os struct FrameRecord: Codable, Equatable { enum Kind: String, Codable { case write, vsync, mark } let timestampNanoseconds: UInt64 let x: Double let y: Double let w: Double let h: Double let kind: Kind var label: String? var timestamp: Double? var targetTimestamp: Double? private enum CodingKeys: String, CodingKey { case timestampNanoseconds = "t_ns" case x, y, w, h, kind, label, timestamp, targetTimestamp } } struct FrameRecording: Codable { let window: Int let records: [FrameRecord] let refreshIntervalNanoseconds: UInt64 let overflow: Bool let clockSource: String private enum CodingKeys: String, CodingKey { case window, records, overflow, clockSource case refreshIntervalNanoseconds = "refreshInterval_ns" } } struct FrameRecordRing { static let capacity = 4_096 private var storage = [FrameRecord?](repeating: nil, count: capacity) private var head = 0 private(set) var count = 0 private(set) var overflow = false mutating func append(_ record: FrameRecord) { storage[(head + count) % Self.capacity] = record if count == Self.capacity { head = (head + 1) % Self.capacity overflow = true } else { count += 1 } } var records: [FrameRecord] { (0.. UInt64 { clock_gettime_nsec_np(CLOCK_UPTIME_RAW) } func start(window: NSWindow) throws { guard !isRecording else { throw RecorderError(description: "Recording already active") } let before = Double(Self.now()) let media = CACurrentMediaTime() * 1_000_000_000 let after = Double(Self.now()) guard media.isFinite, media >= before - 100_000, media <= after + 100_000 else { throw RecorderError(description: "Display and raw uptime clocks disagree by more than 100 microseconds") } ring = FrameRecordRing() refreshInterval = 0 self.window = window isRecording = true let link = window.displayLink(target: self, selector: #selector(tick(_:))) self.link = link link.add(to: .main, forMode: .common) } func stop(index: Int) -> FrameRecording { link?.invalidate() link = nil isRecording = false window = nil return FrameRecording( window: index, records: ring.records, refreshIntervalNanoseconds: refreshInterval, overflow: ring.overflow, clockSource: "CLOCK_UPTIME_RAW") } func write(frame: NSRect) { guard isRecording else { return } append(frame: frame, kind: .write) if signposter.isEnabled { signposter.emitEvent("Write") } } @discardableResult func mark(label: String, at timestamp: UInt64) throws -> UInt64 { guard isRecording, let window else { throw RecorderError(description: "Mark needs an active recording") } guard !label.isEmpty, label.utf8.count <= 128 else { throw RecorderError(description: "Mark label must contain 1 through 128 UTF-8 bytes") } append(frame: window.frame, kind: .mark, at: timestamp, label: label) return timestamp } @objc private func tick(_ link: CADisplayLink) { guard isRecording, link === self.link, let window else { return } let interval = (link.targetTimestamp - link.timestamp) * 1_000_000_000 if interval.isFinite, interval > 0 { refreshInterval = UInt64(interval.rounded()) } append( frame: window.frame, kind: .vsync, timestamp: link.timestamp, targetTimestamp: link.targetTimestamp) } private func append( frame: NSRect, kind: FrameRecord.Kind, at time: UInt64 = FrameRecorder.now(), label: String? = nil, timestamp: Double? = nil, targetTimestamp: Double? = nil ) { ring.append( FrameRecord( timestampNanoseconds: time, x: frame.minX, y: frame.minY, w: frame.width, h: frame.height, kind: kind, label: label, timestamp: timestamp, targetTimestamp: targetTimestamp)) } } private struct RecorderError: Error, CustomStringConvertible { let description: String }