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