Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2c0ce439 authored by Mike Schneider's avatar Mike Schneider
Browse files

Replace `coerce*` calls with `fastCoerce*`

Applying the suggestion from ag/31525429 to the complete mechanics
codebase.

Bug: 384402395
Test: Existing tests
Flag: EXEMPT refactor
Change-Id: I4d4fa36d07984b58dd97e52b264bf185821c159e
parent 206793ff
Loading
Loading
Loading
Loading
+5 −3
Original line number Diff line number Diff line
@@ -26,6 +26,8 @@ import androidx.compose.runtime.referentialEqualityPolicy
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.runtime.withFrameNanos
import androidx.compose.ui.util.fastCoerceAtLeast
import androidx.compose.ui.util.fastCoerceIn
import androidx.compose.ui.util.lerp
import androidx.compose.ui.util.packFloats
import androidx.compose.ui.util.unpackFloat1
@@ -548,7 +550,7 @@ class MotionValue(
     * last frame, but that is likely good enough.
     */
    private fun lastFrameFractionOfPosition(position: Float): Float {
        return ((position - lastInput) / (currentInput() - lastInput)).coerceIn(0f, 1f)
        return ((position - lastInput) / (currentInput() - lastInput)).fastCoerceIn(0f, 1f)
    }

    /**
@@ -715,7 +717,7 @@ class MotionValue(
                    var segmentIndex = sourceIndex
                    while (segmentIndex != targetIndex) {
                        val nextBreakpoint =
                            breakpoints[segmentIndex + directionOffset.coerceAtLeast(0)]
                            breakpoints[segmentIndex + directionOffset.fastCoerceAtLeast(0)]

                        val nextBreakpointFrameFraction =
                            lastFrameFractionOfPosition(nextBreakpoint.position)
@@ -931,7 +933,7 @@ internal value class GuaranteeState(val packedValue: Long) {
    fun withCurrentValue(value: Float, direction: InputDirection): GuaranteeState {
        if (isInactive) return Inactive

        val delta = ((value - start) * direction.sign).coerceAtLeast(0f)
        val delta = ((value - start) * direction.sign).fastCoerceAtLeast(0f)
        return GuaranteeState(start, max(delta, maxDelta))
    }

+4 −2
Original line number Diff line number Diff line
@@ -43,6 +43,8 @@ import androidx.compose.ui.node.ObserverModifierNode
import androidx.compose.ui.node.observeReads
import androidx.compose.ui.platform.InspectorInfo
import androidx.compose.ui.unit.dp
import androidx.compose.ui.util.fastCoerceAtLeast
import androidx.compose.ui.util.fastCoerceAtMost
import androidx.compose.ui.util.fastForEachIndexed
import com.android.mechanics.MotionValue
import com.android.mechanics.spec.DirectionalMotionSpec
@@ -352,10 +354,10 @@ private fun DrawScope.drawDirectionalSpec(
        val mapping = spec.mappings[segmentIndex]
        val startBreakpoint = spec.breakpoints[segmentIndex]
        val segmentStart = startBreakpoint.position
        val fromInput = segmentStart.coerceAtLeast(inputRange.start)
        val fromInput = segmentStart.fastCoerceAtLeast(inputRange.start)
        val endBreakpoint = spec.breakpoints[segmentIndex + 1]
        val segmentEnd = endBreakpoint.position
        val toInput = segmentEnd.coerceAtMost(inputRange.endInclusive)
        val toInput = segmentEnd.fastCoerceAtMost(inputRange.endInclusive)

        // TODO add support for functions that are not linear
        val fromY = mapPointInOutputToY(mapping.map(fromInput), outputRange)
+2 −1
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.mechanics.spring

import androidx.compose.ui.util.fastCoerceIn
import androidx.compose.ui.util.lerp
import androidx.compose.ui.util.packFloats
import androidx.compose.ui.util.unpackFloat1
@@ -71,7 +72,7 @@ fun SpringParameters(stiffness: Float, dampingRatio: Float): SpringParameters {
 * The [fraction] is clamped to a `0..1` range.
 */
fun lerp(start: SpringParameters, stop: SpringParameters, fraction: Float): SpringParameters {
    val f = fraction.coerceIn(0f, 1f)
    val f = fraction.fastCoerceIn(0f, 1f)
    val stiffness = start.stiffness.pow(1 - f) * stop.stiffness.pow(f)
    val dampingRatio = lerp(start.dampingRatio, stop.dampingRatio, f)
    return SpringParameters(packFloats(stiffness, dampingRatio))