Skip to content

Commit

Permalink
Make the ios FPSCounterView more stable
Browse files Browse the repository at this point in the history
* Calculate the accumulated frame rate over the entire update interval.

* Update FPS when we actually draw, instead of when we advance the animation.

Diffs=
1ec31ffba Make the ios FPSCounterView more stable
  • Loading branch information
csmartdalton committed Jan 31, 2023
1 parent caa83d5 commit e88b16b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
331ad0d55068e5059f3e9c85af5382836e278e4e
1ec31ffba21a7e07c72c6062d0a522a383ffc7e4
5 changes: 4 additions & 1 deletion Source/RiveView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ open class RiveView: RiveRendererView {

// Calculate the time elapsed between ticks
let elapsedTime = timestamp - lastTime
fpsCounter?.elapsed(time: elapsedTime)
lastTime = timestamp
advance(delta: elapsedTime)
if !isPlaying {
Expand Down Expand Up @@ -195,6 +194,10 @@ open class RiveView: RiveRendererView {
let newFrame = CGRect(origin: rect.origin, size: size)
align(with: newFrame, contentRect: riveModel.artboard.bounds(), alignment: alignment, fit: fit)
draw(with: riveModel.artboard)

if let displayLink = displayLinkProxy?.displayLink {
fpsCounter?.didDrawFrame(timestamp:displayLink.timestamp)
}
}

// MARK: - UIResponder
Expand Down
27 changes: 17 additions & 10 deletions Source/Utils/FPSCounterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import UIKit

class FPSCounterView: UILabel {
private let fpsFormatter = NumberFormatter()
private let updateInterval: Double = 0.5
private var timeSinceUpdate: Double = 0
private let updateInterval: Double = 1.0
private var timestampOnLastUpdate: CFTimeInterval = 0
private var framesDrawnSinceLastUpdate: Int = 0

internal convenience init() {
self.init(frame: CGRect(x: 1, y: 1, width: 70, height: 20))
Expand All @@ -29,14 +30,20 @@ class FPSCounterView: UILabel {
fpsFormatter.roundingMode = .down
}

internal func elapsed(time elapsedTime: Double) {
if elapsedTime != 0 {
timeSinceUpdate += elapsedTime

if timeSinceUpdate >= updateInterval {
timeSinceUpdate = 0
text = fpsFormatter.string(from: NSNumber(value: 1 / elapsedTime))! + "fps"
}
internal func didDrawFrame(timestamp: CFTimeInterval) {
if (timestampOnLastUpdate == 0) {
timestampOnLastUpdate = timestamp
framesDrawnSinceLastUpdate = 0
return
}

framesDrawnSinceLastUpdate += 1

let elapsedSinceLastUpdate = timestamp - timestampOnLastUpdate
if elapsedSinceLastUpdate >= updateInterval {
text = fpsFormatter.string(from: NSNumber(value: Double(framesDrawnSinceLastUpdate) / elapsedSinceLastUpdate))! + "fps"
timestampOnLastUpdate = timestamp
framesDrawnSinceLastUpdate = 0
}
}

Expand Down

0 comments on commit e88b16b

Please sign in to comment.