1e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck/*
2e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck * Copyright (C) 2014 The Android Open Source Project
3e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck *
4e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck * Licensed under the Apache License, Version 2.0 (the "License");
5e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck * you may not use this file except in compliance with the License.
6e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck * You may obtain a copy of the License at
7e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck *
8e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck *      http://www.apache.org/licenses/LICENSE-2.0
9e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck *
10e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck * Unless required by applicable law or agreed to in writing, software
11e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck * distributed under the License is distributed on an "AS IS" BASIS,
12e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck * See the License for the specific language governing permissions and
14e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck * limitations under the License.
15e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck */
16e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
17e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck#include "Animator.h"
18e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
1968bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck#include <inttypes.h>
20e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck#include <set>
21e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
22119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck#include "AnimationContext.h"
232dc236b2bae13b9a0ed9b3f7320502aecd7983b3Tom Hudson#include "Interpolator.h"
2452244fff29042926e21fa897ef5ab11148e35299John Reck#include "RenderNode.h"
25e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck#include "RenderProperties.h"
26e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
27e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Recknamespace android {
28e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Recknamespace uirenderer {
29e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
30e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck/************************************************************
31ff941dcd815021bb20d6504eb486acb1e50592c3John Reck *  BaseRenderNodeAnimator
32e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck ************************************************************/
33e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
34ff941dcd815021bb20d6504eb486acb1e50592c3John ReckBaseRenderNodeAnimator::BaseRenderNodeAnimator(float finalValue)
35d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        : mTarget(nullptr)
368b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu        , mStagingTarget(nullptr)
378d8af3c1b768d590754d657a7d1242dcb462454bJohn Reck        , mFinalValue(finalValue)
38ff941dcd815021bb20d6504eb486acb1e50592c3John Reck        , mDeltaValue(0)
39ff941dcd815021bb20d6504eb486acb1e50592c3John Reck        , mFromValue(0)
40b9ce116dac378b4cf4490f265dcbd5704a1dd43cChris Craik        , mStagingPlayState(PlayState::NotStarted)
41b9ce116dac378b4cf4490f265dcbd5704a1dd43cChris Craik        , mPlayState(PlayState::NotStarted)
4268bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck        , mHasStartValue(false)
43e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck        , mStartTime(0)
44ad2f8e334f3ef22d3e412b0660a2e1f996f94116Alan Viverette        , mDuration(300)
45572d9acd598517c20c7bf2feb189357e925fa879Chris Craik        , mStartDelay(0)
46c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        , mMayRunAsync(true)
47c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        , mPlayTime(0) {
48e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck}
49e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
50ff941dcd815021bb20d6504eb486acb1e50592c3John ReckBaseRenderNodeAnimator::~BaseRenderNodeAnimator() {
5168bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck}
5268bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck
5368bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reckvoid BaseRenderNodeAnimator::checkMutable() {
5468bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    // Should be impossible to hit as the Java-side also has guards for this
55b9ce116dac378b4cf4490f265dcbd5704a1dd43cChris Craik    LOG_ALWAYS_FATAL_IF(mStagingPlayState != PlayState::NotStarted,
5668bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck            "Animator has already been started!");
57e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck}
58e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
59ff941dcd815021bb20d6504eb486acb1e50592c3John Reckvoid BaseRenderNodeAnimator::setInterpolator(Interpolator* interpolator) {
6068bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    checkMutable();
6151d6a3db97bdd5315f1a17a4b447d10a92217b98Chris Craik    mInterpolator.reset(interpolator);
62e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck}
63e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
64ff941dcd815021bb20d6504eb486acb1e50592c3John Reckvoid BaseRenderNodeAnimator::setStartValue(float value) {
6568bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    checkMutable();
6668bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    doSetStartValue(value);
67ff941dcd815021bb20d6504eb486acb1e50592c3John Reck}
68ff941dcd815021bb20d6504eb486acb1e50592c3John Reck
6968bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reckvoid BaseRenderNodeAnimator::doSetStartValue(float value) {
7068bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    mFromValue = value;
7168bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    mDeltaValue = (mFinalValue - mFromValue);
7268bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    mHasStartValue = true;
73ff941dcd815021bb20d6504eb486acb1e50592c3John Reck}
74ff941dcd815021bb20d6504eb486acb1e50592c3John Reck
75ad2f8e334f3ef22d3e412b0660a2e1f996f94116Alan Viverettevoid BaseRenderNodeAnimator::setDuration(nsecs_t duration) {
7668bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    checkMutable();
77ad2f8e334f3ef22d3e412b0660a2e1f996f94116Alan Viverette    mDuration = duration;
78ad2f8e334f3ef22d3e412b0660a2e1f996f94116Alan Viverette}
79ad2f8e334f3ef22d3e412b0660a2e1f996f94116Alan Viverette
80ad2f8e334f3ef22d3e412b0660a2e1f996f94116Alan Viverettevoid BaseRenderNodeAnimator::setStartDelay(nsecs_t startDelay) {
8168bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    checkMutable();
82ad2f8e334f3ef22d3e412b0660a2e1f996f94116Alan Viverette    mStartDelay = startDelay;
83ad2f8e334f3ef22d3e412b0660a2e1f996f94116Alan Viverette}
84ad2f8e334f3ef22d3e412b0660a2e1f996f94116Alan Viverette
858d8af3c1b768d590754d657a7d1242dcb462454bJohn Reckvoid BaseRenderNodeAnimator::attach(RenderNode* target) {
868b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu    mStagingTarget = target;
878d8af3c1b768d590754d657a7d1242dcb462454bJohn Reck    onAttached();
888d8af3c1b768d590754d657a7d1242dcb462454bJohn Reck}
898d8af3c1b768d590754d657a7d1242dcb462454bJohn Reck
90c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liuvoid BaseRenderNodeAnimator::start() {
91c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    mStagingPlayState = PlayState::Running;
92c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    mStagingRequests.push_back(Request::Start);
93c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    onStagingPlayStateChanged();
94c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu}
95c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu
96c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liuvoid BaseRenderNodeAnimator::cancel() {
97c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    mStagingPlayState = PlayState::Finished;
98c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    mStagingRequests.push_back(Request::Cancel);
99c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    onStagingPlayStateChanged();
100c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu}
101c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu
102c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liuvoid BaseRenderNodeAnimator::reset() {
103c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    mStagingPlayState = PlayState::Finished;
104c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    mStagingRequests.push_back(Request::Reset);
105c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    onStagingPlayStateChanged();
106c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu}
107c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu
108c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liuvoid BaseRenderNodeAnimator::reverse() {
109c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    mStagingPlayState = PlayState::Reversing;
110c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    mStagingRequests.push_back(Request::Reverse);
111c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    onStagingPlayStateChanged();
112c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu}
113c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu
114c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liuvoid BaseRenderNodeAnimator::end() {
115c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    mStagingPlayState = PlayState::Finished;
116c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    mStagingRequests.push_back(Request::End);
117c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    onStagingPlayStateChanged();
118c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu}
119c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu
120c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liuvoid BaseRenderNodeAnimator::resolveStagingRequest(Request request) {
121c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    switch (request) {
122c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    case Request::Start:
123c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        mPlayTime = (mPlayState == PlayState::Running || mPlayState == PlayState::Reversing) ?
124c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu                        mPlayTime : 0;
125c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        mPlayState = PlayState::Running;
1266725d581eb3c13591a4ff276413dbfa0fc13e739Doris Liu        mPendingActionUponFinish = Action::None;
127c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        break;
128c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    case Request::Reverse:
129c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        mPlayTime = (mPlayState == PlayState::Running || mPlayState == PlayState::Reversing) ?
130c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu                        mPlayTime : mDuration;
131c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        mPlayState = PlayState::Reversing;
1326725d581eb3c13591a4ff276413dbfa0fc13e739Doris Liu        mPendingActionUponFinish = Action::None;
133c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        break;
134c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    case Request::Reset:
135c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        mPlayTime = 0;
136c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        mPlayState = PlayState::Finished;
1376725d581eb3c13591a4ff276413dbfa0fc13e739Doris Liu        mPendingActionUponFinish = Action::Reset;
138c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        break;
139c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    case Request::Cancel:
140c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        mPlayState = PlayState::Finished;
1416725d581eb3c13591a4ff276413dbfa0fc13e739Doris Liu        mPendingActionUponFinish = Action::None;
142c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        break;
143c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    case Request::End:
144c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        mPlayTime = mPlayState == PlayState::Reversing ? 0 : mDuration;
145c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        mPlayState = PlayState::Finished;
1466725d581eb3c13591a4ff276413dbfa0fc13e739Doris Liu        mPendingActionUponFinish = Action::End;
147c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        break;
148c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    default:
149c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        LOG_ALWAYS_FATAL("Invalid staging request: %d", static_cast<int>(request));
150c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    };
151c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu}
152c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu
153119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckvoid BaseRenderNodeAnimator::pushStaging(AnimationContext& context) {
1548b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu    if (mStagingTarget) {
1558b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu        RenderNode* oldTarget = mTarget;
1568b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu        mTarget = mStagingTarget;
1578b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu        mStagingTarget = nullptr;
1588b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu        if (oldTarget && oldTarget != mTarget) {
1598b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu            oldTarget->onAnimatorTargetChanged(this);
1608b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu        }
1618b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu    }
1628b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu
16368bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    if (!mHasStartValue) {
1648d8af3c1b768d590754d657a7d1242dcb462454bJohn Reck        doSetStartValue(getValue(mTarget));
165ad2f8e334f3ef22d3e412b0660a2e1f996f94116Alan Viverette    }
166c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu
167c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    if (!mStagingRequests.empty()) {
168148f57fc267b3b0121ff66e4ec4f1798f5b67d52Doris Liu        // No interpolator was set, use the default
169148f57fc267b3b0121ff66e4ec4f1798f5b67d52Doris Liu        if (mPlayState == PlayState::NotStarted && !mInterpolator) {
170148f57fc267b3b0121ff66e4ec4f1798f5b67d52Doris Liu            mInterpolator.reset(Interpolator::createDefaultInterpolator());
171148f57fc267b3b0121ff66e4ec4f1798f5b67d52Doris Liu        }
172c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        // Keep track of the play state and play time before they are changed when
173c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        // staging requests are resolved.
174c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        nsecs_t currentPlayTime = mPlayTime;
175c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        PlayState prevFramePlayState = mPlayState;
176c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu
177c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        // Resolve staging requests one by one.
178c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        for (Request request : mStagingRequests) {
179c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu            resolveStagingRequest(request);
180766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu        }
181c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        mStagingRequests.clear();
182c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu
183c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        if (mStagingPlayState == PlayState::Finished) {
1844d2c47206a8e1706e5f89ef73c0e50e7321bf862John Reck            callOnFinishedListener(context);
185c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        } else if (mStagingPlayState == PlayState::Running
186c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu                || mStagingPlayState == PlayState::Reversing) {
187c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu            bool changed = currentPlayTime != mPlayTime || prevFramePlayState != mStagingPlayState;
188c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu            if (prevFramePlayState != mStagingPlayState) {
189c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu                transitionToRunning(context);
190c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu            }
191c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu            if (changed) {
192c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu                // Now we need to seek to the stagingPlayTime (i.e. the animation progress that was
193c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu                // requested from UI thread). It is achieved by modifying mStartTime, such that
194c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu                // current time - mStartTime = stagingPlayTime (or mDuration -stagingPlayTime in the
195c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu                // case of reversing)
196c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu                nsecs_t currentFrameTime = context.frameTimeMs();
197c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu                if (mPlayState == PlayState::Reversing) {
198c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu                    // Reverse is not supported for animations with a start delay, so here we
199c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu                    // assume no start delay.
200c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu                    mStartTime = currentFrameTime  - (mDuration - mPlayTime);
201c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu                } else {
202c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu                    // Animation should play forward
203c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu                    if (mPlayTime == 0) {
204c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu                        // If the request is to start from the beginning, include start delay.
205c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu                        mStartTime = currentFrameTime + mStartDelay;
206c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu                    } else {
207c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu                        // If the request is to seek to a non-zero play time, then we skip start
208c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu                        // delay.
209c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu                        mStartTime = currentFrameTime - mPlayTime;
210c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu                    }
211c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu                }
212c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu            }
21368bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck        }
21468bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    }
2158b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu    onPushStaging();
21668bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck}
217ad2f8e334f3ef22d3e412b0660a2e1f996f94116Alan Viverette
218119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckvoid BaseRenderNodeAnimator::transitionToRunning(AnimationContext& context) {
219119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    nsecs_t frameTimeMs = context.frameTimeMs();
220119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    LOG_ALWAYS_FATAL_IF(frameTimeMs <= 0, "%" PRId64 " isn't a real frame time!", frameTimeMs);
22168bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    if (mStartDelay < 0 || mStartDelay > 50000) {
22268bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck        ALOGW("Your start delay is strange and confusing: %" PRId64, mStartDelay);
22368bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    }
224119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    mStartTime = frameTimeMs + mStartDelay;
22568bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    if (mStartTime < 0) {
22668bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck        ALOGW("Ended up with a really weird start time of %" PRId64
22768bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck                " with frame time %" PRId64 " and start delay %" PRId64,
228119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck                mStartTime, frameTimeMs, mStartDelay);
22968bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck        // Set to 0 so that the animate() basically instantly finishes
23068bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck        mStartTime = 0;
23168bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    }
232952670d9cf533ed3529b7960f6c88399a400a147Doris Liu    if (mDuration < 0) {
23368bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck        ALOGW("Your duration is strange and confusing: %" PRId64, mDuration);
23468bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    }
23568bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck}
23668bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck
237119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckbool BaseRenderNodeAnimator::animate(AnimationContext& context) {
238b9ce116dac378b4cf4490f265dcbd5704a1dd43cChris Craik    if (mPlayState < PlayState::Running) {
239ad2f8e334f3ef22d3e412b0660a2e1f996f94116Alan Viverette        return false;
240ad2f8e334f3ef22d3e412b0660a2e1f996f94116Alan Viverette    }
241b9ce116dac378b4cf4490f265dcbd5704a1dd43cChris Craik    if (mPlayState == PlayState::Finished) {
2426725d581eb3c13591a4ff276413dbfa0fc13e739Doris Liu        if (mPendingActionUponFinish == Action::Reset) {
2436725d581eb3c13591a4ff276413dbfa0fc13e739Doris Liu            // Skip to start.
2446725d581eb3c13591a4ff276413dbfa0fc13e739Doris Liu            updatePlayTime(0);
2456725d581eb3c13591a4ff276413dbfa0fc13e739Doris Liu        } else if (mPendingActionUponFinish == Action::End) {
2466725d581eb3c13591a4ff276413dbfa0fc13e739Doris Liu            // Skip to end.
2476725d581eb3c13591a4ff276413dbfa0fc13e739Doris Liu            updatePlayTime(mDuration);
2486725d581eb3c13591a4ff276413dbfa0fc13e739Doris Liu        }
2496725d581eb3c13591a4ff276413dbfa0fc13e739Doris Liu        // Reset pending action.
2506725d581eb3c13591a4ff276413dbfa0fc13e739Doris Liu        mPendingActionUponFinish = Action ::None;
25132fb6307de7c3ee9399a39dc6734f1c82ffd1dcbJohn Reck        return true;
25232fb6307de7c3ee9399a39dc6734f1c82ffd1dcbJohn Reck    }
253ad2f8e334f3ef22d3e412b0660a2e1f996f94116Alan Viverette
254766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu    // This should be set before setValue() so animators can query this time when setValue
255766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu    // is called.
256c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    nsecs_t currentPlayTime = context.frameTimeMs() - mStartTime;
257c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    bool finished = updatePlayTime(currentPlayTime);
258c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    if (finished && mPlayState != PlayState::Finished) {
259c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        mPlayState = PlayState::Finished;
260c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        callOnFinishedListener(context);
261c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    }
262c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    return finished;
263c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu}
264766431aa57c16ece8842287a92b2e7208e3b8ac3Doris Liu
265c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liubool BaseRenderNodeAnimator::updatePlayTime(nsecs_t playTime) {
266c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    mPlayTime = mPlayState == PlayState::Reversing ? mDuration - playTime : playTime;
267c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    onPlayTimeChanged(mPlayTime);
2688d8af3c1b768d590754d657a7d1242dcb462454bJohn Reck    // If BaseRenderNodeAnimator is handling the delay (not typical), then
2698d8af3c1b768d590754d657a7d1242dcb462454bJohn Reck    // because the staging properties reflect the final value, we always need
2708d8af3c1b768d590754d657a7d1242dcb462454bJohn Reck    // to call setValue even if the animation isn't yet running or is still
2718d8af3c1b768d590754d657a7d1242dcb462454bJohn Reck    // being delayed as we need to override the staging value
272c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    if (playTime < 0) {
2738d8af3c1b768d590754d657a7d1242dcb462454bJohn Reck        setValue(mTarget, mFromValue);
27468bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck        return false;
275e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck    }
276e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
277e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck    float fraction = 1.0f;
278c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    if ((mPlayState == PlayState::Running || mPlayState == PlayState::Reversing) && mDuration > 0) {
279c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu        fraction = mPlayTime / (float) mDuration;
28068bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    }
281c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    fraction = MathUtils::clamp(fraction, 0.0f, 1.0f);
28268bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck
283e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck    fraction = mInterpolator->interpolate(fraction);
2848d8af3c1b768d590754d657a7d1242dcb462454bJohn Reck    setValue(mTarget, mFromValue + (mDeltaValue * fraction));
285e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
286c4bb185d41cfb960ed9a3178a4f8974c351abdb0Doris Liu    return playTime >= mDuration;
287e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck}
288e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
289718cd3eb70703c43f29ca37907bbf0e153d8cca0Doris Liunsecs_t BaseRenderNodeAnimator::getRemainingPlayTime() {
290718cd3eb70703c43f29ca37907bbf0e153d8cca0Doris Liu    return mPlayState == PlayState::Reversing ? mPlayTime : mDuration - mPlayTime;
291718cd3eb70703c43f29ca37907bbf0e153d8cca0Doris Liu}
292718cd3eb70703c43f29ca37907bbf0e153d8cca0Doris Liu
293e2478d45ccbe5b6abb360ac9d44771b5f4a50bdeJohn Reckvoid BaseRenderNodeAnimator::forceEndNow(AnimationContext& context) {
294b9ce116dac378b4cf4490f265dcbd5704a1dd43cChris Craik    if (mPlayState < PlayState::Finished) {
295b9ce116dac378b4cf4490f265dcbd5704a1dd43cChris Craik        mPlayState = PlayState::Finished;
296e2478d45ccbe5b6abb360ac9d44771b5f4a50bdeJohn Reck        callOnFinishedListener(context);
297e2478d45ccbe5b6abb360ac9d44771b5f4a50bdeJohn Reck    }
298e2478d45ccbe5b6abb360ac9d44771b5f4a50bdeJohn Reck}
299e2478d45ccbe5b6abb360ac9d44771b5f4a50bdeJohn Reck
300119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckvoid BaseRenderNodeAnimator::callOnFinishedListener(AnimationContext& context) {
30152244fff29042926e21fa897ef5ab11148e35299John Reck    if (mListener.get()) {
302119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck        context.callOnFinished(this, mListener.get());
30352244fff29042926e21fa897ef5ab11148e35299John Reck    }
30452244fff29042926e21fa897ef5ab11148e35299John Reck}
30552244fff29042926e21fa897ef5ab11148e35299John Reck
306e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck/************************************************************
30752244fff29042926e21fa897ef5ab11148e35299John Reck *  RenderPropertyAnimator
30852244fff29042926e21fa897ef5ab11148e35299John Reck ************************************************************/
30952244fff29042926e21fa897ef5ab11148e35299John Reck
310ff941dcd815021bb20d6504eb486acb1e50592c3John Reckstruct RenderPropertyAnimator::PropertyAccessors {
311ff941dcd815021bb20d6504eb486acb1e50592c3John Reck   RenderNode::DirtyPropertyMask dirtyMask;
312ff941dcd815021bb20d6504eb486acb1e50592c3John Reck   GetFloatProperty getter;
313ff941dcd815021bb20d6504eb486acb1e50592c3John Reck   SetFloatProperty setter;
314ff941dcd815021bb20d6504eb486acb1e50592c3John Reck};
315ff941dcd815021bb20d6504eb486acb1e50592c3John Reck
31652244fff29042926e21fa897ef5ab11148e35299John Reck// Maps RenderProperty enum to accessors
31752244fff29042926e21fa897ef5ab11148e35299John Reckconst RenderPropertyAnimator::PropertyAccessors RenderPropertyAnimator::PROPERTY_ACCESSOR_LUT[] = {
318ff941dcd815021bb20d6504eb486acb1e50592c3John Reck    {RenderNode::TRANSLATION_X, &RenderProperties::getTranslationX, &RenderProperties::setTranslationX },
319ff941dcd815021bb20d6504eb486acb1e50592c3John Reck    {RenderNode::TRANSLATION_Y, &RenderProperties::getTranslationY, &RenderProperties::setTranslationY },
32041282b7629714b397b43c81c8c25e9d15e2bb935John Reck    {RenderNode::TRANSLATION_Z, &RenderProperties::getTranslationZ, &RenderProperties::setTranslationZ },
321ff941dcd815021bb20d6504eb486acb1e50592c3John Reck    {RenderNode::SCALE_X, &RenderProperties::getScaleX, &RenderProperties::setScaleX },
322ff941dcd815021bb20d6504eb486acb1e50592c3John Reck    {RenderNode::SCALE_Y, &RenderProperties::getScaleY, &RenderProperties::setScaleY },
323ff941dcd815021bb20d6504eb486acb1e50592c3John Reck    {RenderNode::ROTATION, &RenderProperties::getRotation, &RenderProperties::setRotation },
324ff941dcd815021bb20d6504eb486acb1e50592c3John Reck    {RenderNode::ROTATION_X, &RenderProperties::getRotationX, &RenderProperties::setRotationX },
325ff941dcd815021bb20d6504eb486acb1e50592c3John Reck    {RenderNode::ROTATION_Y, &RenderProperties::getRotationY, &RenderProperties::setRotationY },
326ff941dcd815021bb20d6504eb486acb1e50592c3John Reck    {RenderNode::X, &RenderProperties::getX, &RenderProperties::setX },
327ff941dcd815021bb20d6504eb486acb1e50592c3John Reck    {RenderNode::Y, &RenderProperties::getY, &RenderProperties::setY },
328ff941dcd815021bb20d6504eb486acb1e50592c3John Reck    {RenderNode::Z, &RenderProperties::getZ, &RenderProperties::setZ },
329ff941dcd815021bb20d6504eb486acb1e50592c3John Reck    {RenderNode::ALPHA, &RenderProperties::getAlpha, &RenderProperties::setAlpha },
33052244fff29042926e21fa897ef5ab11148e35299John Reck};
33152244fff29042926e21fa897ef5ab11148e35299John Reck
332ff941dcd815021bb20d6504eb486acb1e50592c3John ReckRenderPropertyAnimator::RenderPropertyAnimator(RenderProperty property, float finalValue)
333ff941dcd815021bb20d6504eb486acb1e50592c3John Reck        : BaseRenderNodeAnimator(finalValue)
334ff941dcd815021bb20d6504eb486acb1e50592c3John Reck        , mPropertyAccess(&(PROPERTY_ACCESSOR_LUT[property])) {
335ff941dcd815021bb20d6504eb486acb1e50592c3John Reck}
336ff941dcd815021bb20d6504eb486acb1e50592c3John Reck
3378d8af3c1b768d590754d657a7d1242dcb462454bJohn Reckvoid RenderPropertyAnimator::onAttached() {
33868bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    if (!mHasStartValue
3398b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu            && mStagingTarget->isPropertyFieldDirty(mPropertyAccess->dirtyMask)) {
3408b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu        setStartValue((mStagingTarget->stagingProperties().*mPropertyAccess->getter)());
3418d8af3c1b768d590754d657a7d1242dcb462454bJohn Reck    }
3428d8af3c1b768d590754d657a7d1242dcb462454bJohn Reck}
3438d8af3c1b768d590754d657a7d1242dcb462454bJohn Reck
3448d8af3c1b768d590754d657a7d1242dcb462454bJohn Reckvoid RenderPropertyAnimator::onStagingPlayStateChanged() {
345b9ce116dac378b4cf4490f265dcbd5704a1dd43cChris Craik    if (mStagingPlayState == PlayState::Running) {
3468b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu        if (mStagingTarget) {
3478b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu            (mStagingTarget->mutateStagingProperties().*mPropertyAccess->setter)(finalValue());
3488b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu        } else {
3498b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu            // In the case of start delay where stagingTarget has been sync'ed over and null'ed
3508b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu            // we delay the properties update to push staging.
3518b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu            mShouldUpdateStagingProperties = true;
3528b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu        }
353b9ce116dac378b4cf4490f265dcbd5704a1dd43cChris Craik    } else if (mStagingPlayState == PlayState::Finished) {
35432fb6307de7c3ee9399a39dc6734f1c82ffd1dcbJohn Reck        // We're being canceled, so make sure that whatever values the UI thread
35532fb6307de7c3ee9399a39dc6734f1c82ffd1dcbJohn Reck        // is observing for us is pushed over
3568b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu        mShouldSyncPropertyFields = true;
3578b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu    }
3588b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu}
3598b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu
3608b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liuvoid RenderPropertyAnimator::onPushStaging() {
3618b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu    if (mShouldUpdateStagingProperties) {
3628b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu        (mTarget->mutateStagingProperties().*mPropertyAccess->setter)(finalValue());
3638b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu        mShouldUpdateStagingProperties = false;
3648b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu    }
3658b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu
3668b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu    if (mShouldSyncPropertyFields) {
36732fb6307de7c3ee9399a39dc6734f1c82ffd1dcbJohn Reck        mTarget->setPropertyFieldsDirty(dirtyMask());
3688b083206aef627b6445a8c6be8bf5bb1d778a7f8Doris Liu        mShouldSyncPropertyFields = false;
369ff941dcd815021bb20d6504eb486acb1e50592c3John Reck    }
37052244fff29042926e21fa897ef5ab11148e35299John Reck}
37152244fff29042926e21fa897ef5ab11148e35299John Reck
3722218472d23483f09341bf655d55db21dcbabc1b6John Reckuint32_t RenderPropertyAnimator::dirtyMask() {
3732218472d23483f09341bf655d55db21dcbabc1b6John Reck    return mPropertyAccess->dirtyMask;
3742218472d23483f09341bf655d55db21dcbabc1b6John Reck}
3752218472d23483f09341bf655d55db21dcbabc1b6John Reck
376ff941dcd815021bb20d6504eb486acb1e50592c3John Reckfloat RenderPropertyAnimator::getValue(RenderNode* target) const {
377ff941dcd815021bb20d6504eb486acb1e50592c3John Reck    return (target->properties().*mPropertyAccess->getter)();
37852244fff29042926e21fa897ef5ab11148e35299John Reck}
37952244fff29042926e21fa897ef5ab11148e35299John Reck
380ff941dcd815021bb20d6504eb486acb1e50592c3John Reckvoid RenderPropertyAnimator::setValue(RenderNode* target, float value) {
381ff941dcd815021bb20d6504eb486acb1e50592c3John Reck    (target->animatorProperties().*mPropertyAccess->setter)(value);
38252244fff29042926e21fa897ef5ab11148e35299John Reck}
38352244fff29042926e21fa897ef5ab11148e35299John Reck
38452244fff29042926e21fa897ef5ab11148e35299John Reck/************************************************************
38552244fff29042926e21fa897ef5ab11148e35299John Reck *  CanvasPropertyPrimitiveAnimator
38652244fff29042926e21fa897ef5ab11148e35299John Reck ************************************************************/
38752244fff29042926e21fa897ef5ab11148e35299John Reck
38852244fff29042926e21fa897ef5ab11148e35299John ReckCanvasPropertyPrimitiveAnimator::CanvasPropertyPrimitiveAnimator(
389ff941dcd815021bb20d6504eb486acb1e50592c3John Reck                CanvasPropertyPrimitive* property, float finalValue)
390ff941dcd815021bb20d6504eb486acb1e50592c3John Reck        : BaseRenderNodeAnimator(finalValue)
39152244fff29042926e21fa897ef5ab11148e35299John Reck        , mProperty(property) {
39252244fff29042926e21fa897ef5ab11148e35299John Reck}
39352244fff29042926e21fa897ef5ab11148e35299John Reck
39464bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampefloat CanvasPropertyPrimitiveAnimator::getValue(RenderNode* target) const {
39552244fff29042926e21fa897ef5ab11148e35299John Reck    return mProperty->value;
39652244fff29042926e21fa897ef5ab11148e35299John Reck}
39752244fff29042926e21fa897ef5ab11148e35299John Reck
39864bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampevoid CanvasPropertyPrimitiveAnimator::setValue(RenderNode* target, float value) {
39952244fff29042926e21fa897ef5ab11148e35299John Reck    mProperty->value = value;
40052244fff29042926e21fa897ef5ab11148e35299John Reck}
40152244fff29042926e21fa897ef5ab11148e35299John Reck
402a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reckuint32_t CanvasPropertyPrimitiveAnimator::dirtyMask() {
403a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck    return RenderNode::DISPLAY_LIST;
404a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck}
405a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck
40652244fff29042926e21fa897ef5ab11148e35299John Reck/************************************************************
40752244fff29042926e21fa897ef5ab11148e35299John Reck *  CanvasPropertySkPaintAnimator
40852244fff29042926e21fa897ef5ab11148e35299John Reck ************************************************************/
40952244fff29042926e21fa897ef5ab11148e35299John Reck
41052244fff29042926e21fa897ef5ab11148e35299John ReckCanvasPropertyPaintAnimator::CanvasPropertyPaintAnimator(
411ff941dcd815021bb20d6504eb486acb1e50592c3John Reck                CanvasPropertyPaint* property, PaintField field, float finalValue)
412ff941dcd815021bb20d6504eb486acb1e50592c3John Reck        : BaseRenderNodeAnimator(finalValue)
41352244fff29042926e21fa897ef5ab11148e35299John Reck        , mProperty(property)
41452244fff29042926e21fa897ef5ab11148e35299John Reck        , mField(field) {
41552244fff29042926e21fa897ef5ab11148e35299John Reck}
41652244fff29042926e21fa897ef5ab11148e35299John Reck
41764bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampefloat CanvasPropertyPaintAnimator::getValue(RenderNode* target) const {
41852244fff29042926e21fa897ef5ab11148e35299John Reck    switch (mField) {
41952244fff29042926e21fa897ef5ab11148e35299John Reck    case STROKE_WIDTH:
42052244fff29042926e21fa897ef5ab11148e35299John Reck        return mProperty->value.getStrokeWidth();
42152244fff29042926e21fa897ef5ab11148e35299John Reck    case ALPHA:
42252244fff29042926e21fa897ef5ab11148e35299John Reck        return mProperty->value.getAlpha();
42352244fff29042926e21fa897ef5ab11148e35299John Reck    }
42452244fff29042926e21fa897ef5ab11148e35299John Reck    LOG_ALWAYS_FATAL("Unknown field %d", (int) mField);
42552244fff29042926e21fa897ef5ab11148e35299John Reck    return -1;
42652244fff29042926e21fa897ef5ab11148e35299John Reck}
42752244fff29042926e21fa897ef5ab11148e35299John Reck
428531ee701ddca2d1604fcce8e5d6d8837a3f651acJohn Reckstatic uint8_t to_uint8(float value) {
429531ee701ddca2d1604fcce8e5d6d8837a3f651acJohn Reck    int c = (int) (value + .5f);
430531ee701ddca2d1604fcce8e5d6d8837a3f651acJohn Reck    return static_cast<uint8_t>( c < 0 ? 0 : c > 255 ? 255 : c );
431531ee701ddca2d1604fcce8e5d6d8837a3f651acJohn Reck}
432531ee701ddca2d1604fcce8e5d6d8837a3f651acJohn Reck
43364bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampevoid CanvasPropertyPaintAnimator::setValue(RenderNode* target, float value) {
43452244fff29042926e21fa897ef5ab11148e35299John Reck    switch (mField) {
43552244fff29042926e21fa897ef5ab11148e35299John Reck    case STROKE_WIDTH:
43652244fff29042926e21fa897ef5ab11148e35299John Reck        mProperty->value.setStrokeWidth(value);
43752244fff29042926e21fa897ef5ab11148e35299John Reck        return;
43852244fff29042926e21fa897ef5ab11148e35299John Reck    case ALPHA:
439531ee701ddca2d1604fcce8e5d6d8837a3f651acJohn Reck        mProperty->value.setAlpha(to_uint8(value));
44052244fff29042926e21fa897ef5ab11148e35299John Reck        return;
44152244fff29042926e21fa897ef5ab11148e35299John Reck    }
44252244fff29042926e21fa897ef5ab11148e35299John Reck    LOG_ALWAYS_FATAL("Unknown field %d", (int) mField);
443e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck}
444e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
445a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reckuint32_t CanvasPropertyPaintAnimator::dirtyMask() {
446a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck    return RenderNode::DISPLAY_LIST;
447a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck}
448a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck
449af4d04cab6d48ae0d6a5e79bd30f679af87abaadChris CraikRevealAnimator::RevealAnimator(int centerX, int centerY,
450d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck        float startValue, float finalValue)
451d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck        : BaseRenderNodeAnimator(finalValue)
452d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck        , mCenterX(centerX)
453af4d04cab6d48ae0d6a5e79bd30f679af87abaadChris Craik        , mCenterY(centerY) {
454d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck    setStartValue(startValue);
455d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck}
456d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck
457d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reckfloat RevealAnimator::getValue(RenderNode* target) const {
458af4d04cab6d48ae0d6a5e79bd30f679af87abaadChris Craik    return target->properties().getRevealClip().getRadius();
459d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck}
460d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck
461d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reckvoid RevealAnimator::setValue(RenderNode* target, float value) {
462af4d04cab6d48ae0d6a5e79bd30f679af87abaadChris Craik    target->animatorProperties().mutableRevealClip().set(true,
463d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck            mCenterX, mCenterY, value);
464d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck}
465d3de42cae84fadfa1befd082a2cf1bf72f9ad82aJohn Reck
466a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reckuint32_t RevealAnimator::dirtyMask() {
467a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck    return RenderNode::GENERIC;
468a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck}
469a7c2ea20c43ab797bef5801530687e22e83def8fJohn Reck
470e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck} /* namespace uirenderer */
471e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck} /* namespace android */
472