forked from facebook/litho
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce frame identifier calculation to the rendering pipeline
Summary: The earlier attempt to measure frame drops during animations was naive and did not take into account stalls on the background work (bind, resolve, layout) that we do during rendering. This diff stack attempts to fix that. There are two parts to this change: **1. Frame Id (this diff)** We introduce the concept of a "frame identifier", which is an integer that represents the frame in which the last update was requested. The frame identifier is held by `RenderState` and updated when a new render is requested. This frame id is then plumbed through to the pipeline to the point where we commit, so that at the point we commit, we know which update we are committing. Commit listeners receive this. **2. Animation perf (next diff)** Each animation registers a commit listener when started. This commit listener uses the frameId provided to the commit listener callback to then compute how many frames were lost since the last commit happens. Differential Revision: D54906022 fbshipit-source-id: 7512e0c9ccd8237d1e7e14c95807203a7c79570c
- Loading branch information
1 parent
20ef996
commit c34c655
Showing
9 changed files
with
99 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
litho-rendercore/src/main/java/com/facebook/rendercore/utils/VSyncUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.facebook.rendercore.utils | ||
|
||
import android.content.Context | ||
import android.view.WindowManager | ||
import java.util.concurrent.TimeUnit | ||
import java.util.concurrent.atomic.AtomicInteger | ||
import kotlin.math.roundToInt | ||
|
||
object VSyncUtils { | ||
private const val DEFAULT_REFRESH_RATE = 60.0 | ||
private const val REFRESH_RATE_MIN = 30.0 | ||
private const val REFRESH_RATE_MAX = 240.0 | ||
private val ONE_SECOND_IN_NS = TimeUnit.SECONDS.toNanos(1) | ||
|
||
private val vsyncTimeNs: AtomicInteger = AtomicInteger(-1) | ||
|
||
@JvmStatic | ||
fun getNormalVsyncTime(context: Context): Int { | ||
var value = vsyncTimeNs.get() | ||
if (value != -1) { | ||
return value | ||
} | ||
|
||
// Initialize the value. | ||
val display = (context.getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay | ||
var refreshRate = display.refreshRate.toDouble() | ||
|
||
// If refresh rate is lower than 0, it means the OS is not reporting a correct value. We | ||
// will | ||
// assume it is 60. | ||
refreshRate = | ||
if (refreshRate < 0) { | ||
DEFAULT_REFRESH_RATE | ||
} else { | ||
// Cap refresh rates between 30 and 240. Anything else is unreasonable. | ||
refreshRate.coerceIn(REFRESH_RATE_MIN, REFRESH_RATE_MAX) | ||
} | ||
|
||
value = (ONE_SECOND_IN_NS / refreshRate).roundToInt() | ||
vsyncTimeNs.compareAndSet(-1, value) | ||
|
||
return value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters