blob: 46c9da2ac6b8f751c7b1b0e0bfed48e43741bdc3 [file] [log] [blame]
John Reck119907c2014-08-14 09:02:01 -07001/*
2 * Copyright (C) 2014 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#include "AnimationContext.h"
17
18#include "Animator.h"
19#include "RenderNode.h"
John Reck119907c2014-08-14 09:02:01 -070020#include "renderthread/TimeLord.h"
21
22namespace android {
23namespace uirenderer {
24
25AnimationContext::AnimationContext(renderthread::TimeLord& clock)
26 : mClock(clock)
27 , mCurrentFrameAnimations(*this)
28 , mNextFrameAnimations(*this)
John Reck1bcacfd2017-11-03 10:12:19 -070029 , mFrameTimeMs(0) {}
John Reck119907c2014-08-14 09:02:01 -070030
John Reck1bcacfd2017-11-03 10:12:19 -070031AnimationContext::~AnimationContext() {}
John Recke2478d42014-09-03 16:46:05 -070032
33void AnimationContext::destroy() {
John Reckec845a22014-09-05 15:23:38 -070034 startFrame(TreeInfo::MODE_RT_ONLY);
John Reckd0cd9db2014-08-28 08:43:39 -070035 while (mCurrentFrameAnimations.mNextHandle) {
36 AnimationHandle* current = mCurrentFrameAnimations.mNextHandle;
37 AnimatorManager& animators = current->mRenderNode->animators();
John Recke2478d42014-09-03 16:46:05 -070038 animators.endAllActiveAnimators();
John Reckd0cd9db2014-08-28 08:43:39 -070039 LOG_ALWAYS_FATAL_IF(mCurrentFrameAnimations.mNextHandle == current,
John Reck1bcacfd2017-11-03 10:12:19 -070040 "endAllAnimators failed to remove from current frame list!");
John Reckd0cd9db2014-08-28 08:43:39 -070041 }
John Reck119907c2014-08-14 09:02:01 -070042}
43
44void AnimationContext::addAnimatingRenderNode(RenderNode& node) {
45 if (!node.animators().hasAnimationHandle()) {
46 AnimationHandle* handle = new AnimationHandle(node, *this);
47 addAnimationHandle(handle);
48 }
49}
50
51void AnimationContext::addAnimationHandle(AnimationHandle* handle) {
52 handle->insertAfter(&mNextFrameAnimations);
53}
54
Andreas Gampe64bb4132014-11-22 00:35:09 +000055void AnimationContext::startFrame(TreeInfo::TraversalMode mode) {
John Reck119907c2014-08-14 09:02:01 -070056 LOG_ALWAYS_FATAL_IF(mCurrentFrameAnimations.mNextHandle,
John Reck1bcacfd2017-11-03 10:12:19 -070057 "Missed running animations last frame!");
John Reck119907c2014-08-14 09:02:01 -070058 AnimationHandle* head = mNextFrameAnimations.mNextHandle;
59 if (head) {
Chris Craikd41c4d82015-01-05 15:51:13 -080060 mNextFrameAnimations.mNextHandle = nullptr;
John Reck119907c2014-08-14 09:02:01 -070061 mCurrentFrameAnimations.mNextHandle = head;
62 head->mPreviousHandle = &mCurrentFrameAnimations;
63 }
John Reck501ff9a2016-06-17 12:57:12 -070064 mFrameTimeMs = ns2ms(mClock.latestVsync());
John Reck119907c2014-08-14 09:02:01 -070065}
66
67void AnimationContext::runRemainingAnimations(TreeInfo& info) {
68 while (mCurrentFrameAnimations.mNextHandle) {
69 AnimationHandle* current = mCurrentFrameAnimations.mNextHandle;
70 AnimatorManager& animators = current->mRenderNode->animators();
71 animators.pushStaging();
72 animators.animateNoDamage(info);
73 LOG_ALWAYS_FATAL_IF(mCurrentFrameAnimations.mNextHandle == current,
John Reck1bcacfd2017-11-03 10:12:19 -070074 "Animate failed to remove from current frame list!");
John Reck119907c2014-08-14 09:02:01 -070075 }
76}
77
78void AnimationContext::callOnFinished(BaseRenderNodeAnimator* animator,
John Reck1bcacfd2017-11-03 10:12:19 -070079 AnimationListener* listener) {
John Reck119907c2014-08-14 09:02:01 -070080 listener->onAnimationFinished(animator);
81}
82
83AnimationHandle::AnimationHandle(AnimationContext& context)
John Reck1bcacfd2017-11-03 10:12:19 -070084 : mContext(context), mPreviousHandle(nullptr), mNextHandle(nullptr) {}
John Reck119907c2014-08-14 09:02:01 -070085
86AnimationHandle::AnimationHandle(RenderNode& animatingNode, AnimationContext& context)
87 : mRenderNode(&animatingNode)
88 , mContext(context)
Chris Craikd41c4d82015-01-05 15:51:13 -080089 , mPreviousHandle(nullptr)
90 , mNextHandle(nullptr) {
John Reck119907c2014-08-14 09:02:01 -070091 mRenderNode->animators().setAnimationHandle(this);
92}
93
94AnimationHandle::~AnimationHandle() {
95 LOG_ALWAYS_FATAL_IF(mPreviousHandle || mNextHandle,
John Reck1bcacfd2017-11-03 10:12:19 -070096 "AnimationHandle destroyed while still animating!");
John Reck119907c2014-08-14 09:02:01 -070097}
98
99void AnimationHandle::notifyAnimationsRan() {
100 removeFromList();
101 if (mRenderNode->animators().hasAnimators()) {
102 mContext.addAnimationHandle(this);
103 } else {
John Reckd0cd9db2014-08-28 08:43:39 -0700104 release();
John Reck119907c2014-08-14 09:02:01 -0700105 }
106}
107
John Reckd0cd9db2014-08-28 08:43:39 -0700108void AnimationHandle::release() {
109 LOG_ALWAYS_FATAL_IF(mRenderNode->animators().hasAnimators(),
John Reck1bcacfd2017-11-03 10:12:19 -0700110 "Releasing the handle for an RenderNode with outstanding animators!");
John Reckd0cd9db2014-08-28 08:43:39 -0700111 removeFromList();
Chris Craikd41c4d82015-01-05 15:51:13 -0800112 mRenderNode->animators().setAnimationHandle(nullptr);
John Reckd0cd9db2014-08-28 08:43:39 -0700113 delete this;
114}
115
John Reck119907c2014-08-14 09:02:01 -0700116void AnimationHandle::insertAfter(AnimationHandle* prev) {
117 removeFromList();
118 mNextHandle = prev->mNextHandle;
119 if (mNextHandle) {
120 mNextHandle->mPreviousHandle = this;
121 }
122 prev->mNextHandle = this;
123 mPreviousHandle = prev;
124}
125
126void AnimationHandle::removeFromList() {
127 if (mPreviousHandle) {
128 mPreviousHandle->mNextHandle = mNextHandle;
129 }
130 if (mNextHandle) {
131 mNextHandle->mPreviousHandle = mPreviousHandle;
132 }
Chris Craikd41c4d82015-01-05 15:51:13 -0800133 mPreviousHandle = nullptr;
134 mNextHandle = nullptr;
John Reck119907c2014-08-14 09:02:01 -0700135}
136
137} /* namespace uirenderer */
138} /* namespace android */