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

Commit c165c186 authored by Mike Schneider's avatar Mike Schneider
Browse files

Use `directMapped` velocity to prime new springs.

See b/379612686#comment3 for a visualization of this change.

Bug: 379612686
Test: Unit tests
Flag: com.android.systemui.scene_container
Change-Id: Ic83de9a340699a0fa6a240fa8e70ba8d2cb1e002
parent 7d629e95
Loading
Loading
Loading
Loading
+32 −2
Original line number Diff line number Diff line
@@ -198,6 +198,13 @@ class MotionValue(

            try {
                debugIsAnimating = true

                // indicates whether withFrameNanos is called continuously (as opposed to being
                // suspended for an undetermined amount of time in between withFrameNanos).
                // This is essential after `withFrameNanos` returned: if true at this point,
                // currentAnimationTimeNanos - lastFrameNanos is the duration of the last frame.
                var isAnimatingUninterrupted = false

                while (continueRunning.invoke(this@MotionValue)) {

                    withFrameNanos { frameTimeNanos ->
@@ -224,6 +231,19 @@ class MotionValue(
                    // cause a re-computation if the current state is being read before the next
                    // frame).

                    if (isAnimatingUninterrupted) {
                        val currentDirectMapped = currentDirectMapped
                        val lastDirectMapped =
                            lastSegment.mapping.map(lastInput) - lastAnimation.targetValue

                        val frameDuration =
                            (currentAnimationTimeNanos - lastFrameTimeNanos) / 1_000_000_000.0
                        val staticDelta = (currentDirectMapped - lastDirectMapped)
                        directMappedVelocity = (staticDelta / frameDuration).toFloat()
                    } else {
                        directMappedVelocity = 0f
                    }

                    var scheduleNextFrame = !isStable
                    if (capturedSegment != currentSegment) {
                        capturedSegment = currentSegment
@@ -275,6 +295,7 @@ class MotionValue(
                            )
                    }

                    isAnimatingUninterrupted = scheduleNextFrame
                    if (scheduleNextFrame) {
                        continue
                    }
@@ -376,6 +397,11 @@ class MotionValue(
    private var lastAnimation: DiscontinuityAnimation by
        mutableStateOf(DiscontinuityAnimation.None, referentialEqualityPolicy())

    // The change velocity of the `currentDirectMapped`, in `units/sec`. Only non-zero if the
    // animation loop is processing every frame (while animating or while the input changes
    // continuously).
    private var directMappedVelocity: Float = 0f

    // ---- Last frame's input and output ----------------------------------------------------------

    // The state below captures relevant input values (including frame time) and the computed spring
@@ -702,7 +728,7 @@ class MotionValue(
                    val newTarget = delta - lastSpringState.displacement
                    DiscontinuityAnimation(
                        newTarget,
                        SpringState(-newTarget, lastSpringState.velocity),
                        SpringState(-newTarget, lastSpringState.velocity + directMappedVelocity),
                        springParameters,
                        lastFrameTimeNanos,
                    )
@@ -800,7 +826,7 @@ class MotionValue(

                        if (deltaIsFinite) {
                            springTarget += delta
                            springState = springState.addDisplacement(-delta)
                            springState = springState.nudge(displacementDelta = -delta)
                        }
                        segmentIndex += directionOffset
                        lastBreakpoint = nextBreakpoint
@@ -822,6 +848,10 @@ class MotionValue(
                            }
                    }

                    if (springState.displacement != 0f) {
                        springState = springState.nudge(velocityDelta = directMappedVelocity)
                    }

                    val tightened =
                        currentGuaranteeState.updatedSpringParameters(
                            currentSegment.entryBreakpoint
+3 −2
Original line number Diff line number Diff line
@@ -51,8 +51,9 @@ value class SpringState(val packedValue: Long) {
        return currentEnergy <= maxStableEnergy
    }

    fun addDisplacement(displacementDelta: Float): SpringState {
        return SpringState(displacement + displacementDelta, velocity)
    /** Adds the specified [displacementDelta] and [velocityDelta] to the returned state. */
    fun nudge(displacementDelta: Float = 0f, velocityDelta: Float = 0f): SpringState {
        return SpringState(displacement + displacementDelta, velocity + velocityDelta)
    }

    override fun toString(): String {
+282 −0
Original line number Diff line number Diff line
{
  "frame_ids": [
    0,
    16,
    32,
    48,
    64,
    80,
    96,
    112,
    128,
    144,
    160,
    176,
    192,
    208,
    224,
    240,
    256,
    272,
    288,
    304,
    320,
    336,
    352,
    368
  ],
  "features": [
    {
      "name": "input",
      "type": "float",
      "data_points": [
        0,
        3,
        6,
        11,
        11,
        11,
        11,
        11,
        11,
        11,
        11,
        11,
        11,
        11,
        11,
        11,
        11,
        11,
        11,
        11,
        11,
        11,
        11,
        11
      ]
    },
    {
      "name": "gestureDirection",
      "type": "string",
      "data_points": [
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max"
      ]
    },
    {
      "name": "output",
      "type": "float",
      "data_points": [
        0,
        3,
        6,
        10.58992,
        13.213701,
        15.27689,
        16.823486,
        17.93786,
        18.712797,
        19.23355,
        19.571312,
        19.781933,
        19.907185,
        19.977114,
        20.01258,
        20.02758,
        20.031174,
        20.029,
        20.024399,
        20.019238,
        20.014452,
        20.010433,
        20,
        20
      ]
    },
    {
      "name": "outputTarget",
      "type": "float",
      "data_points": [
        0,
        3,
        6,
        20,
        20,
        20,
        20,
        20,
        20,
        20,
        20,
        20,
        20,
        20,
        20,
        20,
        20,
        20,
        20,
        20,
        20,
        20,
        20,
        20
      ]
    },
    {
      "name": "outputSpring",
      "type": "springParameters",
      "data_points": [
        {
          "stiffness": 100000,
          "dampingRatio": 1
        },
        {
          "stiffness": 100000,
          "dampingRatio": 1
        },
        {
          "stiffness": 100000,
          "dampingRatio": 1
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        }
      ]
    },
    {
      "name": "isStable",
      "type": "boolean",
      "data_points": [
        true,
        true,
        true,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        true,
        true
      ]
    }
  ]
}
 No newline at end of file
+322 −0
Original line number Diff line number Diff line
{
  "frame_ids": [
    0,
    16,
    32,
    48,
    64,
    80,
    96,
    112,
    128,
    144,
    160,
    176,
    192,
    208,
    224,
    240,
    256,
    272,
    288,
    304,
    320,
    336,
    352,
    368,
    384,
    400,
    416,
    432
  ],
  "features": [
    {
      "name": "input",
      "type": "float",
      "data_points": [
        0,
        3,
        6,
        9,
        12,
        15,
        18,
        21,
        21,
        21,
        21,
        21,
        21,
        21,
        21,
        21,
        21,
        21,
        21,
        21,
        21,
        21,
        21,
        21,
        21,
        21,
        21,
        21
      ]
    },
    {
      "name": "gestureDirection",
      "type": "string",
      "data_points": [
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max",
        "Max"
      ]
    },
    {
      "name": "output",
      "type": "float",
      "data_points": [
        0,
        3,
        6,
        9,
        14.22027,
        20.881996,
        27.33559,
        33.145477,
        36.971085,
        39.13093,
        40.246414,
        40.73661,
        40.874382,
        40.830643,
        40.70759,
        40.56272,
        40.42557,
        40.30897,
        40.21637,
        40.146416,
        40.095673,
        40.060165,
        40.036156,
        40.020485,
        40.01064,
        40.00473,
        40,
        40
      ]
    },
    {
      "name": "outputTarget",
      "type": "float",
      "data_points": [
        0,
        3,
        6,
        9,
        32,
        35,
        38,
        40,
        40,
        40,
        40,
        40,
        40,
        40,
        40,
        40,
        40,
        40,
        40,
        40,
        40,
        40,
        40,
        40,
        40,
        40,
        40,
        40
      ]
    },
    {
      "name": "outputSpring",
      "type": "springParameters",
      "data_points": [
        {
          "stiffness": 100000,
          "dampingRatio": 1
        },
        {
          "stiffness": 100000,
          "dampingRatio": 1
        },
        {
          "stiffness": 100000,
          "dampingRatio": 1
        },
        {
          "stiffness": 100000,
          "dampingRatio": 1
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        },
        {
          "stiffness": 700,
          "dampingRatio": 0.9
        }
      ]
    },
    {
      "name": "isStable",
      "type": "boolean",
      "data_points": [
        true,
        true,
        true,
        true,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        true,
        true
      ]
    }
  ]
}
 No newline at end of file
+10 −10
Original line number Diff line number Diff line
@@ -66,16 +66,16 @@
        1.5,
        1.7,
        1.9,
        1.6615533,
        1.226438,
        0.817063,
        0.5036553,
        0.28967857,
        0.15513074,
        0.07628298,
        0.033192396,
        0.011453152,
        0.0016130209,
        1.7769231,
        1.356505,
        0.92442614,
        0.5804193,
        0.33967388,
        0.18536365,
        0.093348265,
        0.042140007,
        0.015731335,
        0.0033904314,
        0,
        0
      ]
Loading