1 /* 2 * Copyright (C) 2025 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.mechanics.impl 18 19 import com.android.mechanics.MotionValue 20 import com.android.mechanics.spec.Breakpoint 21 import com.android.mechanics.spec.Guarantee 22 import com.android.mechanics.spec.InputDirection 23 import com.android.mechanics.spec.Mapping 24 import com.android.mechanics.spec.MotionSpec 25 import com.android.mechanics.spec.SegmentData 26 import com.android.mechanics.spring.SpringState 27 28 /** Static configuration that remains constant over a MotionValue's lifecycle. */ 29 internal interface StaticConfig { 30 /** 31 * A threshold value (in output units) that determines when the [MotionValue]'s internal spring 32 * animation is considered stable. 33 */ 34 val stableThreshold: Float 35 36 /** Optional label for identifying a MotionValue for debugging purposes. */ 37 val label: String? 38 } 39 40 /** The up-to-date [MotionValue] input, used by [Computations] to calculate the updated output. */ 41 internal interface CurrentFrameInput { 42 val spec: MotionSpec 43 val currentInput: Float 44 val currentAnimationTimeNanos: Long 45 val currentDirection: InputDirection 46 val currentGestureDragOffset: Float 47 } 48 49 /** 50 * The [MotionValue] state of the last completed frame. 51 * 52 * The values must be published at the start of the frame, together with the 53 * [CurrentFrameInput.currentAnimationTimeNanos]. 54 */ 55 internal interface LastFrameState { 56 /** 57 * The segment in use, defined by the min/max [Breakpoint]s and the [Mapping] in between. This 58 * implicitly also captures the [InputDirection] and [MotionSpec]. 59 */ 60 val lastSegment: SegmentData 61 /** 62 * State of the [Guarantee]. Its interpretation is defined by the [lastSegment]'s 63 * [SegmentData.entryBreakpoint]'s [Breakpoint.guarantee]. If that breakpoint has no guarantee, 64 * this value will be [GuaranteeState.Inactive]. 65 * 66 * This is the maximal guarantee value seen so far, as well as the guarantee's start value, and 67 * is used to compute the spring-tightening fraction. 68 */ 69 val lastGuaranteeState: GuaranteeState 70 /** 71 * The state of an ongoing animation of a discontinuity. 72 * 73 * The spring animation is described by the [DiscontinuityAnimation.springStartState], which 74 * tracks the oscillation of the spring until the displacement is guaranteed not to exceed 75 * [stableThreshold] anymore. The spring animation started at 76 * [DiscontinuityAnimation.springStartTimeNanos], and uses the 77 * [DiscontinuityAnimation.springParameters]. The displacement's origin is at 78 * [DiscontinuityAnimation.targetValue]. 79 * 80 * This state does not have to be updated every frame, even as an animation is ongoing: the 81 * spring animation can be computed with the same start parameters, and as time progresses, the 82 * [SpringState.calculateUpdatedState] is passed an ever larger `elapsedNanos` on each frame. 83 * 84 * The [DiscontinuityAnimation.targetValue] is a delta to the direct mapped output value from 85 * the [SegmentData.mapping]. It might accumulate the target value - it is not required to reset 86 * when the animation ends. 87 */ 88 val lastAnimation: DiscontinuityAnimation 89 /** 90 * Last frame's spring state, based on initial origin values in [lastAnimation], carried-forward 91 * to [lastFrameTimeNanos]. 92 */ 93 val lastSpringState: SpringState 94 /** The time of the last frame, in nanoseconds. */ 95 val lastFrameTimeNanos: Long 96 /** The [currentInput] of the last frame */ 97 val lastInput: Float 98 val lastGestureDragOffset: Float 99 100 val directMappedVelocity: Float 101 } 102