blob: 24801928121b9c94ba2d506359e1f02f2b30dc12 [file] [log] [blame]
Fedor Kudasov34a25762019-06-28 21:53:56 +01001/*
2 * Copyright (C) 2019 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#include "RootRenderNode.h"
18
Fedor Kudasovbc409cf2019-07-03 13:56:56 +010019#include <utils/Looper.h>
20
Fedor Kudasov34a25762019-06-28 21:53:56 +010021namespace android::uirenderer {
22
23class FinishAndInvokeListener : public MessageHandler {
24public:
25 explicit FinishAndInvokeListener(PropertyValuesAnimatorSet* anim) : mAnimator(anim) {
26 mListener = anim->getOneShotListener();
27 mRequestId = anim->getRequestId();
28 }
29
30 virtual void handleMessage(const Message& message) {
31 if (mAnimator->getRequestId() == mRequestId) {
32 // Request Id has not changed, meaning there's no animation lifecyle change since the
33 // message is posted, so go ahead and call finish to make sure the PlayState is properly
34 // updated. This is needed because before the next frame comes in from UI thread to
35 // trigger an animation update, there could be reverse/cancel etc. So we need to update
36 // the playstate in time to ensure all the subsequent events get chained properly.
37 mAnimator->end();
38 }
39 mListener->onAnimationFinished(nullptr);
40 }
41
42private:
43 sp<PropertyValuesAnimatorSet> mAnimator;
44 sp<AnimationListener> mListener;
45 uint32_t mRequestId;
46};
47
48void RootRenderNode::prepareTree(TreeInfo& info) {
49 info.errorHandler = mErrorHandler.get();
50
51 for (auto& anim : mRunningVDAnimators) {
52 // Assume that the property change in VD from the animators will not be consumed. Mark
53 // otherwise if the VDs are found in the display list tree. For VDs that are not in
54 // the display list tree, we stop providing animation pulses by 1) removing them from
55 // the animation list, 2) post a delayed message to end them at end time so their
56 // listeners can receive the corresponding callbacks.
57 anim->getVectorDrawable()->setPropertyChangeWillBeConsumed(false);
58 // Mark the VD dirty so it will damage itself during prepareTree.
59 anim->getVectorDrawable()->markDirty();
60 }
61 if (info.mode == TreeInfo::MODE_FULL) {
62 for (auto& anim : mPausedVDAnimators) {
63 anim->getVectorDrawable()->setPropertyChangeWillBeConsumed(false);
64 anim->getVectorDrawable()->markDirty();
65 }
66 }
67 // TODO: This is hacky
68 info.updateWindowPositions = true;
69 RenderNode::prepareTree(info);
70 info.updateWindowPositions = false;
71 info.errorHandler = nullptr;
72}
73
74void RootRenderNode::attachAnimatingNode(RenderNode* animatingNode) {
75 mPendingAnimatingRenderNodes.push_back(animatingNode);
76}
77
78void RootRenderNode::attachPendingVectorDrawableAnimators() {
79 mRunningVDAnimators.insert(mPendingVectorDrawableAnimators.begin(),
80 mPendingVectorDrawableAnimators.end());
81 mPendingVectorDrawableAnimators.clear();
82}
83
84void RootRenderNode::detachAnimators() {
85 // Remove animators from the list and post a delayed message in future to end the animator
86 // For infinite animators, remove the listener so we no longer hold a global ref to the AVD
87 // java object, and therefore the AVD objects in both native and Java can be properly
88 // released.
89 for (auto& anim : mRunningVDAnimators) {
90 detachVectorDrawableAnimator(anim.get());
91 anim->clearOneShotListener();
92 }
93 for (auto& anim : mPausedVDAnimators) {
94 anim->clearOneShotListener();
95 }
96 mRunningVDAnimators.clear();
97 mPausedVDAnimators.clear();
98}
99
100// Move all the animators to the paused list, and send a delayed message to notify the finished
101// listener.
102void RootRenderNode::pauseAnimators() {
103 mPausedVDAnimators.insert(mRunningVDAnimators.begin(), mRunningVDAnimators.end());
104 for (auto& anim : mRunningVDAnimators) {
105 detachVectorDrawableAnimator(anim.get());
106 }
107 mRunningVDAnimators.clear();
108}
109
110void RootRenderNode::doAttachAnimatingNodes(AnimationContext* context) {
111 for (size_t i = 0; i < mPendingAnimatingRenderNodes.size(); i++) {
112 RenderNode* node = mPendingAnimatingRenderNodes[i].get();
113 context->addAnimatingRenderNode(*node);
114 }
115 mPendingAnimatingRenderNodes.clear();
116}
117
118// Run VectorDrawable animators after prepareTree.
119void RootRenderNode::runVectorDrawableAnimators(AnimationContext* context, TreeInfo& info) {
120 // Push staging.
121 if (info.mode == TreeInfo::MODE_FULL) {
122 pushStagingVectorDrawableAnimators(context);
123 }
124
125 // Run the animators in the running list.
126 for (auto it = mRunningVDAnimators.begin(); it != mRunningVDAnimators.end();) {
127 if ((*it)->animate(*context)) {
128 it = mRunningVDAnimators.erase(it);
129 } else {
130 it++;
131 }
132 }
133
134 // Run the animators in paused list during full sync.
135 if (info.mode == TreeInfo::MODE_FULL) {
136 // During full sync we also need to pulse paused animators, in case their targets
137 // have been added back to the display list. All the animators that passed the
138 // scheduled finish time will be removed from the paused list.
139 for (auto it = mPausedVDAnimators.begin(); it != mPausedVDAnimators.end();) {
140 if ((*it)->animate(*context)) {
141 // Animator has finished, remove from the list.
142 it = mPausedVDAnimators.erase(it);
143 } else {
144 it++;
145 }
146 }
147 }
148
149 // Move the animators with a target not in DisplayList to paused list.
150 for (auto it = mRunningVDAnimators.begin(); it != mRunningVDAnimators.end();) {
151 if (!(*it)->getVectorDrawable()->getPropertyChangeWillBeConsumed()) {
152 // Vector Drawable is not in the display list, we should remove this animator from
153 // the list, put it in the paused list, and post a delayed message to end the
154 // animator.
155 detachVectorDrawableAnimator(it->get());
156 mPausedVDAnimators.insert(*it);
157 it = mRunningVDAnimators.erase(it);
158 } else {
159 it++;
160 }
161 }
162
163 // Move the animators with a target in DisplayList from paused list to running list, and
164 // trim paused list.
165 if (info.mode == TreeInfo::MODE_FULL) {
166 // Check whether any paused animator's target is back in Display List. If so, put the
167 // animator back in the running list.
168 for (auto it = mPausedVDAnimators.begin(); it != mPausedVDAnimators.end();) {
169 if ((*it)->getVectorDrawable()->getPropertyChangeWillBeConsumed()) {
170 mRunningVDAnimators.insert(*it);
171 it = mPausedVDAnimators.erase(it);
172 } else {
173 it++;
174 }
175 }
176 // Trim paused VD animators at full sync, so that when Java loses reference to an
177 // animator, we know we won't be requested to animate it any more, then we remove such
178 // animators from the paused list so they can be properly freed. We also remove the
179 // animators from paused list when the time elapsed since start has exceeded duration.
180 trimPausedVDAnimators(context);
181 }
182
183 info.out.hasAnimations |= !mRunningVDAnimators.empty();
184}
185
186void RootRenderNode::trimPausedVDAnimators(AnimationContext* context) {
187 // Trim paused vector drawable animator list.
188 for (auto it = mPausedVDAnimators.begin(); it != mPausedVDAnimators.end();) {
189 // Remove paused VD animator if no one else is referencing it. Note that animators that
190 // have passed scheduled finish time are removed from list when they are being pulsed
191 // before prepare tree.
192 // TODO: this is a bit hacky, need to figure out a better way to track when the paused
193 // animators should be freed.
194 if ((*it)->getStrongCount() == 1) {
195 it = mPausedVDAnimators.erase(it);
196 } else {
197 it++;
198 }
199 }
200}
201
202void RootRenderNode::pushStagingVectorDrawableAnimators(AnimationContext* context) {
203 for (auto& anim : mRunningVDAnimators) {
204 anim->pushStaging(*context);
205 }
206}
207
208void RootRenderNode::destroy() {
209 for (auto& renderNode : mPendingAnimatingRenderNodes) {
210 renderNode->animators().endAllStagingAnimators();
211 }
212 mPendingAnimatingRenderNodes.clear();
213 mPendingVectorDrawableAnimators.clear();
214}
215
216void RootRenderNode::addVectorDrawableAnimator(PropertyValuesAnimatorSet* anim) {
217 mPendingVectorDrawableAnimators.insert(anim);
218}
219
220void RootRenderNode::detachVectorDrawableAnimator(PropertyValuesAnimatorSet* anim) {
221 if (anim->isInfinite() || !anim->isRunning()) {
222 // Do not need to post anything if the animation is infinite (i.e. no meaningful
223 // end listener action), or if the animation has already ended.
224 return;
225 }
226 nsecs_t remainingTimeInMs = anim->getRemainingPlayTime();
227 // Post a delayed onFinished event that is scheduled to be handled when the animator ends.
228 if (anim->getOneShotListener()) {
229 // VectorDrawable's oneshot listener is updated when there are user triggered animation
230 // lifecycle changes, such as start(), end(), etc. By using checking and clearing
231 // one shot listener, we ensure the same end listener event gets posted only once.
232 // Therefore no duplicates. Another benefit of using one shot listener is that no
233 // removal is necessary: the end time of animation will not change unless triggered by
234 // user events, in which case the already posted listener's id will become stale, and
235 // the onFinished callback will then be ignored.
236 sp<FinishAndInvokeListener> message = new FinishAndInvokeListener(anim);
237 auto looper = Looper::getForThread();
238 LOG_ALWAYS_FATAL_IF(looper == nullptr, "Not on a looper thread?");
239 looper->sendMessageDelayed(ms2ns(remainingTimeInMs), message, 0);
240 anim->clearOneShotListener();
241 }
242}
243
244class AnimationContextBridge : public AnimationContext {
245public:
246 AnimationContextBridge(renderthread::TimeLord& clock, RootRenderNode* rootNode)
247 : AnimationContext(clock), mRootNode(rootNode) {}
248
249 virtual ~AnimationContextBridge() {}
250
251 // Marks the start of a frame, which will update the frame time and move all
252 // next frame animations into the current frame
253 virtual void startFrame(TreeInfo::TraversalMode mode) {
254 if (mode == TreeInfo::MODE_FULL) {
255 mRootNode->doAttachAnimatingNodes(this);
256 mRootNode->attachPendingVectorDrawableAnimators();
257 }
258 AnimationContext::startFrame(mode);
259 }
260
261 // Runs any animations still left in mCurrentFrameAnimations
262 virtual void runRemainingAnimations(TreeInfo& info) {
263 AnimationContext::runRemainingAnimations(info);
264 mRootNode->runVectorDrawableAnimators(this, info);
265 }
266
267 virtual void pauseAnimators() override { mRootNode->pauseAnimators(); }
268
269 virtual void callOnFinished(BaseRenderNodeAnimator* animator, AnimationListener* listener) {
270 listener->onAnimationFinished(animator);
271 }
272
273 virtual void destroy() {
274 AnimationContext::destroy();
275 mRootNode->detachAnimators();
276 }
277
278private:
279 sp<RootRenderNode> mRootNode;
280};
281
282AnimationContext* ContextFactoryImpl::createAnimationContext(renderthread::TimeLord& clock) {
283 return new AnimationContextBridge(clock, mRootNode);
284}
285
286} // namespace android::uirenderer