Skip to content

Commit 9e2fa9c

Browse files
Keep pre-mounting within half of a 30 Hz frame
A display the system slows below 60 Hz can spend more of each frame pre-mounting, so the ceiling is now half of a 30 Hz frame (16.67 ms) rather than half of a 60 Hz one: from 30 Hz up the budget is half of the display's frame, and below 30 Hz it is 16.67 ms. A display that reports no refresh rate is still budgeted as a 60 Hz one.
1 parent 3d780fb commit 9e2fa9c

2 files changed

Lines changed: 30 additions & 14 deletions

File tree

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountItemDispatcher.kt‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,12 @@ internal class MountItemDispatcher(
341341
dispatchPreMountItemsImpl(deadline)
342342
}
343343

344-
/** Half of the display's current frame, and never longer than [LONGEST_PRE_MOUNT_NANOS]. */
344+
/**
345+
* Half of the display's current frame, and never longer than [LONGEST_PRE_MOUNT_NANOS]. A display
346+
* that reports no refresh rate is budgeted as a 60 Hz one.
347+
*/
345348
private fun computePreMountBudgetNanos(): Long {
346-
val frameIntervalNanos =
347-
frameIntervalSource.frameIntervalNanos() ?: return LONGEST_PRE_MOUNT_NANOS
349+
val frameIntervalNanos = frameIntervalSource.frameIntervalNanos() ?: FRAME_TIME_NS
348350
return minOf(frameIntervalNanos / 2, LONGEST_PRE_MOUNT_NANOS)
349351
}
350352

@@ -435,11 +437,13 @@ internal class MountItemDispatcher(
435437
private companion object {
436438
private const val TAG: String = "MountItemDispatcher"
437439

440+
private const val FRAME_TIME_NS: Long = (1000000000 / 60).toLong()
441+
438442
/**
439-
* The longest a frame pre-mounts for: half of a 60 Hz frame, so that below 60 Hz pre-mounting
440-
* holds the UI thread no longer than it does at 60 Hz.
443+
* The longest a frame pre-mounts for: half of a 30 Hz frame, so that a display below 30 Hz,
444+
* such as an adaptive panel idling at 1 Hz, holds the UI thread no longer than at 30 Hz.
441445
*/
442-
private const val LONGEST_PRE_MOUNT_NANOS: Long = 1_000_000_000L / 60 / 2
446+
private const val LONGEST_PRE_MOUNT_NANOS: Long = 1_000_000_000L / 30 / 2
443447

444448
private fun <E> drainConcurrentItemQueue(queue: Queue<E>): List<E>? {
445449
if (queue.isEmpty()) {

‎packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/mounting/MountItemDispatcherTest.kt‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,30 @@ class MountItemDispatcherTest {
8989
}
9090

9191
@Test
92-
fun preMountsForHalfOfTheDisplaysFrameFromSixtyHertzUp() {
92+
fun preMountsForHalfOfTheDisplaysFrameFromThirtyHertzUp() {
9393
// Items start at 0, 1, 2, ... ms, and the first one starting past half of the frame waits for
94-
// the next frame: half of a 60 Hz frame is 8.33 ms, of a 185 Hz frame 2.70 ms.
95-
assertThat(preMountFrameAtEachRefreshRate(listOf(60, 90, 120, 144, 165, 185, 240)))
96-
.isEqualTo(mapOf(60 to 9, 90 to 6, 120 to 5, 144 to 4, 165 to 4, 185 to 3, 240 to 3))
94+
// the next frame: half of a 30 Hz frame is 16.67 ms, of a 185 Hz frame 2.70 ms.
95+
assertThat(preMountFrameAtEachRefreshRate(listOf(30, 48, 60, 90, 120, 144, 165, 185, 240)))
96+
.isEqualTo(
97+
mapOf(
98+
30 to 17,
99+
48 to 11,
100+
60 to 9,
101+
90 to 6,
102+
120 to 5,
103+
144 to 4,
104+
165 to 4,
105+
185 to 3,
106+
240 to 3,
107+
)
108+
)
97109
}
98110

99111
@Test
100-
fun neverPreMountsLongerThanHalfOfASixtyHertzFrameBelowSixtyHertz() {
101-
// Half of a 1 Hz frame would be 500 ms; every rate below 60 Hz keeps 8.33 ms.
102-
assertThat(preMountFrameAtEachRefreshRate(listOf(1, 10, 24, 30, 48)))
103-
.isEqualTo(mapOf(1 to 9, 10 to 9, 24 to 9, 30 to 9, 48 to 9))
112+
fun neverPreMountsLongerThanHalfOfAThirtyHertzFrameBelowThirtyHertz() {
113+
// Half of a 1 Hz frame would be 500 ms; every rate below 30 Hz keeps 16.67 ms.
114+
assertThat(preMountFrameAtEachRefreshRate(listOf(1, 10, 24)))
115+
.isEqualTo(mapOf(1 to 17, 10 to 17, 24 to 17))
104116
}
105117

106118
@Test

0 commit comments

Comments
 (0)