Split out RootRenderNode

- Deleted unused OnFinishedEvent, InvokeAnimationListeners
- Hid FinishAndInvokeListener and AnimationContextBridge in the
implmentation

Bug: 117921091
Test: all tests should pass
Change-Id: I98af836b27d06907a24696fcae03f24808c1993a
diff --git a/core/jni/android_view_ThreadedRenderer.cpp b/core/jni/android_view_ThreadedRenderer.cpp
index 84c3b3a..7dd0e93 100644
--- a/core/jni/android_view_ThreadedRenderer.cpp
+++ b/core/jni/android_view_ThreadedRenderer.cpp
@@ -25,7 +25,6 @@
 #include <nativehelper/JNIHelp.h>
 #include "core_jni_helpers.h"
 #include <GraphicsJNI.h>
-#include <nativehelper/ScopedPrimitiveArray.h>
 
 #include <gui/BufferItemConsumer.h>
 #include <gui/BufferQueue.h>
@@ -42,15 +41,12 @@
 
 #include "android_os_MessageQueue.h"
 
-#include <Animator.h>
-#include <AnimationContext.h>
 #include <FrameInfo.h>
 #include <FrameMetricsObserver.h>
 #include <IContextFactory.h>
 #include <Picture.h>
 #include <Properties.h>
-#include <PropertyValuesAnimatorSet.h>
-#include <RenderNode.h>
+#include <RootRenderNode.h>
 #include <renderthread/CanvasContext.h>
 #include <renderthread/RenderProxy.h>
 #include <renderthread/RenderTask.h>
@@ -91,56 +87,18 @@
     return env;
 }
 
-class OnFinishedEvent {
+class JvmErrorReporter : public ErrorHandler {
 public:
-    OnFinishedEvent(BaseRenderNodeAnimator* animator, AnimationListener* listener)
-            : animator(animator), listener(listener) {}
-    sp<BaseRenderNodeAnimator> animator;
-    sp<AnimationListener> listener;
-};
-
-class InvokeAnimationListeners : public MessageHandler {
-public:
-    explicit InvokeAnimationListeners(std::vector<OnFinishedEvent>& events) {
-        mOnFinishedEvents.swap(events);
+    JvmErrorReporter(JNIEnv* env) {
+        env->GetJavaVM(&mVm);
     }
 
-    static void callOnFinished(OnFinishedEvent& event) {
-        event.listener->onAnimationFinished(event.animator.get());
-    }
-
-    virtual void handleMessage(const Message& message) {
-        std::for_each(mOnFinishedEvents.begin(), mOnFinishedEvents.end(), callOnFinished);
-        mOnFinishedEvents.clear();
-    }
-
-private:
-    std::vector<OnFinishedEvent> mOnFinishedEvents;
-};
-
-class FinishAndInvokeListener : public MessageHandler {
-public:
-    explicit FinishAndInvokeListener(PropertyValuesAnimatorSet* anim)
-            : mAnimator(anim) {
-        mListener = anim->getOneShotListener();
-        mRequestId = anim->getRequestId();
-    }
-
-    virtual void handleMessage(const Message& message) {
-        if (mAnimator->getRequestId() == mRequestId) {
-            // Request Id has not changed, meaning there's no animation lifecyle change since the
-            // message is posted, so go ahead and call finish to make sure the PlayState is properly
-            // updated. This is needed because before the next frame comes in from UI thread to
-            // trigger an animation update, there could be reverse/cancel etc. So we need to update
-            // the playstate in time to ensure all the subsequent events get chained properly.
-            mAnimator->end();
-        }
-        mListener->onAnimationFinished(nullptr);
+    virtual void onError(const std::string& message) override {
+        JNIEnv* env = getenv(mVm);
+        jniThrowException(env, "java/lang/IllegalStateException", message.c_str());
     }
 private:
-    sp<PropertyValuesAnimatorSet> mAnimator;
-    sp<AnimationListener> mListener;
-    uint32_t mRequestId;
+    JavaVM* mVm;
 };
 
 class FrameCompleteWrapper : public LightRefBase<FrameCompleteWrapper> {
@@ -175,283 +133,6 @@
     }
 };
 
-class RootRenderNode : public RenderNode, ErrorHandler {
-public:
-    explicit RootRenderNode(JNIEnv* env) : RenderNode() {
-        env->GetJavaVM(&mVm);
-    }
-
-    virtual ~RootRenderNode() {}
-
-    virtual void onError(const std::string& message) override {
-        JNIEnv* env = getenv(mVm);
-        jniThrowException(env, "java/lang/IllegalStateException", message.c_str());
-    }
-
-    virtual void prepareTree(TreeInfo& info) override {
-        info.errorHandler = this;
-
-        for (auto& anim : mRunningVDAnimators) {
-            // Assume that the property change in VD from the animators will not be consumed. Mark
-            // otherwise if the VDs are found in the display list tree. For VDs that are not in
-            // the display list tree, we stop providing animation pulses by 1) removing them from
-            // the animation list, 2) post a delayed message to end them at end time so their
-            // listeners can receive the corresponding callbacks.
-            anim->getVectorDrawable()->setPropertyChangeWillBeConsumed(false);
-            // Mark the VD dirty so it will damage itself during prepareTree.
-            anim->getVectorDrawable()->markDirty();
-        }
-        if (info.mode == TreeInfo::MODE_FULL) {
-            for (auto &anim : mPausedVDAnimators) {
-                anim->getVectorDrawable()->setPropertyChangeWillBeConsumed(false);
-                anim->getVectorDrawable()->markDirty();
-            }
-        }
-        // TODO: This is hacky
-        info.updateWindowPositions = true;
-        RenderNode::prepareTree(info);
-        info.updateWindowPositions = false;
-        info.errorHandler = nullptr;
-    }
-
-    void attachAnimatingNode(RenderNode* animatingNode) {
-        mPendingAnimatingRenderNodes.push_back(animatingNode);
-    }
-
-    void attachPendingVectorDrawableAnimators() {
-        mRunningVDAnimators.insert(mPendingVectorDrawableAnimators.begin(),
-                mPendingVectorDrawableAnimators.end());
-        mPendingVectorDrawableAnimators.clear();
-    }
-
-    void detachAnimators() {
-        // Remove animators from the list and post a delayed message in future to end the animator
-        // For infinite animators, remove the listener so we no longer hold a global ref to the AVD
-        // java object, and therefore the AVD objects in both native and Java can be properly
-        // released.
-        for (auto& anim : mRunningVDAnimators) {
-            detachVectorDrawableAnimator(anim.get());
-            anim->clearOneShotListener();
-        }
-        for (auto& anim : mPausedVDAnimators) {
-            anim->clearOneShotListener();
-        }
-        mRunningVDAnimators.clear();
-        mPausedVDAnimators.clear();
-    }
-
-    // Move all the animators to the paused list, and send a delayed message to notify the finished
-    // listener.
-    void pauseAnimators() {
-        mPausedVDAnimators.insert(mRunningVDAnimators.begin(), mRunningVDAnimators.end());
-        for (auto& anim : mRunningVDAnimators) {
-            detachVectorDrawableAnimator(anim.get());
-        }
-        mRunningVDAnimators.clear();
-    }
-
-    void doAttachAnimatingNodes(AnimationContext* context) {
-        for (size_t i = 0; i < mPendingAnimatingRenderNodes.size(); i++) {
-            RenderNode* node = mPendingAnimatingRenderNodes[i].get();
-            context->addAnimatingRenderNode(*node);
-        }
-        mPendingAnimatingRenderNodes.clear();
-    }
-
-    // Run VectorDrawable animators after prepareTree.
-    void runVectorDrawableAnimators(AnimationContext* context, TreeInfo& info) {
-        // Push staging.
-        if (info.mode == TreeInfo::MODE_FULL) {
-            pushStagingVectorDrawableAnimators(context);
-        }
-
-        // Run the animators in the running list.
-        for (auto it = mRunningVDAnimators.begin(); it != mRunningVDAnimators.end();) {
-            if ((*it)->animate(*context)) {
-                it = mRunningVDAnimators.erase(it);
-            } else {
-                it++;
-            }
-        }
-
-        // Run the animators in paused list during full sync.
-        if (info.mode == TreeInfo::MODE_FULL) {
-            // During full sync we also need to pulse paused animators, in case their targets
-            // have been added back to the display list. All the animators that passed the
-            // scheduled finish time will be removed from the paused list.
-            for (auto it = mPausedVDAnimators.begin(); it != mPausedVDAnimators.end();) {
-                if ((*it)->animate(*context)) {
-                    // Animator has finished, remove from the list.
-                    it = mPausedVDAnimators.erase(it);
-                } else {
-                    it++;
-                }
-            }
-        }
-
-        // Move the animators with a target not in DisplayList to paused list.
-        for (auto it = mRunningVDAnimators.begin(); it != mRunningVDAnimators.end();) {
-            if (!(*it)->getVectorDrawable()->getPropertyChangeWillBeConsumed()) {
-                // Vector Drawable is not in the display list, we should remove this animator from
-                // the list, put it in the paused list, and post a delayed message to end the
-                // animator.
-                detachVectorDrawableAnimator(it->get());
-                mPausedVDAnimators.insert(*it);
-                it = mRunningVDAnimators.erase(it);
-            } else {
-                it++;
-            }
-        }
-
-        // Move the animators with a target in DisplayList from paused list to running list, and
-        // trim paused list.
-        if (info.mode == TreeInfo::MODE_FULL) {
-            // Check whether any paused animator's target is back in Display List. If so, put the
-            // animator back in the running list.
-            for (auto it = mPausedVDAnimators.begin(); it != mPausedVDAnimators.end();) {
-                if ((*it)->getVectorDrawable()->getPropertyChangeWillBeConsumed()) {
-                    mRunningVDAnimators.insert(*it);
-                    it = mPausedVDAnimators.erase(it);
-                } else {
-                    it++;
-                }
-            }
-            // Trim paused VD animators at full sync, so that when Java loses reference to an
-            // animator, we know we won't be requested to animate it any more, then we remove such
-            // animators from the paused list so they can be properly freed. We also remove the
-            // animators from paused list when the time elapsed since start has exceeded duration.
-            trimPausedVDAnimators(context);
-        }
-
-        info.out.hasAnimations |= !mRunningVDAnimators.empty();
-    }
-
-    void trimPausedVDAnimators(AnimationContext* context) {
-        // Trim paused vector drawable animator list.
-        for (auto it = mPausedVDAnimators.begin(); it != mPausedVDAnimators.end();) {
-            // Remove paused VD animator if no one else is referencing it. Note that animators that
-            // have passed scheduled finish time are removed from list when they are being pulsed
-            // before prepare tree.
-            // TODO: this is a bit hacky, need to figure out a better way to track when the paused
-            // animators should be freed.
-            if ((*it)->getStrongCount() == 1) {
-                it = mPausedVDAnimators.erase(it);
-            } else {
-                it++;
-            }
-        }
-    }
-
-    void pushStagingVectorDrawableAnimators(AnimationContext* context) {
-        for (auto& anim : mRunningVDAnimators) {
-            anim->pushStaging(*context);
-        }
-    }
-
-    void destroy() {
-        for (auto& renderNode : mPendingAnimatingRenderNodes) {
-            renderNode->animators().endAllStagingAnimators();
-        }
-        mPendingAnimatingRenderNodes.clear();
-        mPendingVectorDrawableAnimators.clear();
-    }
-
-    void addVectorDrawableAnimator(PropertyValuesAnimatorSet* anim) {
-        mPendingVectorDrawableAnimators.insert(anim);
-    }
-
-private:
-    JavaVM* mVm;
-    std::vector< sp<RenderNode> > mPendingAnimatingRenderNodes;
-    std::set< sp<PropertyValuesAnimatorSet> > mPendingVectorDrawableAnimators;
-    std::set< sp<PropertyValuesAnimatorSet> > mRunningVDAnimators;
-    // mPausedVDAnimators stores a list of animators that have not yet passed the finish time, but
-    // their VectorDrawable targets are no longer in the DisplayList. We skip these animators when
-    // render thread runs animators independent of UI thread (i.e. RT_ONLY mode). These animators
-    // need to be re-activated once their VD target is added back into DisplayList. Since that could
-    // only happen when we do a full sync, we need to make sure to pulse these paused animators at
-    // full sync. If any animator's VD target is found in DisplayList during a full sync, we move
-    // the animator back to the running list.
-    std::set< sp<PropertyValuesAnimatorSet> > mPausedVDAnimators;
-    void detachVectorDrawableAnimator(PropertyValuesAnimatorSet* anim) {
-        if (anim->isInfinite() || !anim->isRunning()) {
-            // Do not need to post anything if the animation is infinite (i.e. no meaningful
-            // end listener action), or if the animation has already ended.
-            return;
-        }
-        nsecs_t remainingTimeInMs = anim->getRemainingPlayTime();
-        // Post a delayed onFinished event that is scheduled to be handled when the animator ends.
-        if (anim->getOneShotListener()) {
-            // VectorDrawable's oneshot listener is updated when there are user triggered animation
-            // lifecycle changes, such as start(), end(), etc. By using checking and clearing
-            // one shot listener, we ensure the same end listener event gets posted only once.
-            // Therefore no duplicates. Another benefit of using one shot listener is that no
-            // removal is necessary: the end time of animation will not change unless triggered by
-            // user events, in which case the already posted listener's id will become stale, and
-            // the onFinished callback will then be ignored.
-            sp<FinishAndInvokeListener> message
-                    = new FinishAndInvokeListener(anim);
-            auto looper = Looper::getForThread();
-            LOG_ALWAYS_FATAL_IF(looper == nullptr, "Not on a looper thread?");
-            looper->sendMessageDelayed(ms2ns(remainingTimeInMs), message, 0);
-            anim->clearOneShotListener();
-        }
-    }
-};
-
-class AnimationContextBridge : public AnimationContext {
-public:
-    AnimationContextBridge(renderthread::TimeLord& clock, RootRenderNode* rootNode)
-            : AnimationContext(clock), mRootNode(rootNode) {
-    }
-
-    virtual ~AnimationContextBridge() {}
-
-    // Marks the start of a frame, which will update the frame time and move all
-    // next frame animations into the current frame
-    virtual void startFrame(TreeInfo::TraversalMode mode) {
-        if (mode == TreeInfo::MODE_FULL) {
-            mRootNode->doAttachAnimatingNodes(this);
-            mRootNode->attachPendingVectorDrawableAnimators();
-        }
-        AnimationContext::startFrame(mode);
-    }
-
-    // Runs any animations still left in mCurrentFrameAnimations
-    virtual void runRemainingAnimations(TreeInfo& info) {
-        AnimationContext::runRemainingAnimations(info);
-        mRootNode->runVectorDrawableAnimators(this, info);
-    }
-
-    virtual void pauseAnimators() override {
-        mRootNode->pauseAnimators();
-    }
-
-    virtual void callOnFinished(BaseRenderNodeAnimator* animator, AnimationListener* listener) {
-        listener->onAnimationFinished(animator);
-    }
-
-    virtual void destroy() {
-        AnimationContext::destroy();
-        mRootNode->detachAnimators();
-    }
-
-private:
-    sp<RootRenderNode> mRootNode;
-};
-
-class ContextFactoryImpl : public IContextFactory {
-public:
-    explicit ContextFactoryImpl(RootRenderNode* rootNode) : mRootNode(rootNode) {}
-
-    virtual AnimationContext* createAnimationContext(renderthread::TimeLord& clock) {
-        return new AnimationContextBridge(clock, mRootNode);
-    }
-
-private:
-    RootRenderNode* mRootNode;
-};
-
 class ObserverProxy;
 
 class NotifyHandler : public MessageHandler {
@@ -604,7 +285,7 @@
 }
 
 static jlong android_view_ThreadedRenderer_createRootRenderNode(JNIEnv* env, jobject clazz) {
-    RootRenderNode* node = new RootRenderNode(env);
+    RootRenderNode* node = new RootRenderNode(std::make_unique<JvmErrorReporter>(env));
     node->incStrong(0);
     node->setName("RootRenderNode");
     return reinterpret_cast<jlong>(node);
diff --git a/libs/hwui/Android.bp b/libs/hwui/Android.bp
index 5814bb7..bfead1a 100644
--- a/libs/hwui/Android.bp
+++ b/libs/hwui/Android.bp
@@ -250,6 +250,7 @@
                 "ProfileData.cpp",
                 "ProfileDataContainer.cpp",
                 "Readback.cpp",
+                "RootRenderNode.cpp",
                 "TreeInfo.cpp",
                 "WebViewFunctorManager.cpp",
                 "protos/graphicsstats.proto",
diff --git a/libs/hwui/RootRenderNode.cpp b/libs/hwui/RootRenderNode.cpp
new file mode 100644
index 0000000..d8c1b57e2
--- /dev/null
+++ b/libs/hwui/RootRenderNode.cpp
@@ -0,0 +1,284 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "RootRenderNode.h"
+
+namespace android::uirenderer {
+
+class FinishAndInvokeListener : public MessageHandler {
+public:
+    explicit FinishAndInvokeListener(PropertyValuesAnimatorSet* anim) : mAnimator(anim) {
+        mListener = anim->getOneShotListener();
+        mRequestId = anim->getRequestId();
+    }
+
+    virtual void handleMessage(const Message& message) {
+        if (mAnimator->getRequestId() == mRequestId) {
+            // Request Id has not changed, meaning there's no animation lifecyle change since the
+            // message is posted, so go ahead and call finish to make sure the PlayState is properly
+            // updated. This is needed because before the next frame comes in from UI thread to
+            // trigger an animation update, there could be reverse/cancel etc. So we need to update
+            // the playstate in time to ensure all the subsequent events get chained properly.
+            mAnimator->end();
+        }
+        mListener->onAnimationFinished(nullptr);
+    }
+
+private:
+    sp<PropertyValuesAnimatorSet> mAnimator;
+    sp<AnimationListener> mListener;
+    uint32_t mRequestId;
+};
+
+void RootRenderNode::prepareTree(TreeInfo& info) {
+    info.errorHandler = mErrorHandler.get();
+
+    for (auto& anim : mRunningVDAnimators) {
+        // Assume that the property change in VD from the animators will not be consumed. Mark
+        // otherwise if the VDs are found in the display list tree. For VDs that are not in
+        // the display list tree, we stop providing animation pulses by 1) removing them from
+        // the animation list, 2) post a delayed message to end them at end time so their
+        // listeners can receive the corresponding callbacks.
+        anim->getVectorDrawable()->setPropertyChangeWillBeConsumed(false);
+        // Mark the VD dirty so it will damage itself during prepareTree.
+        anim->getVectorDrawable()->markDirty();
+    }
+    if (info.mode == TreeInfo::MODE_FULL) {
+        for (auto& anim : mPausedVDAnimators) {
+            anim->getVectorDrawable()->setPropertyChangeWillBeConsumed(false);
+            anim->getVectorDrawable()->markDirty();
+        }
+    }
+    // TODO: This is hacky
+    info.updateWindowPositions = true;
+    RenderNode::prepareTree(info);
+    info.updateWindowPositions = false;
+    info.errorHandler = nullptr;
+}
+
+void RootRenderNode::attachAnimatingNode(RenderNode* animatingNode) {
+    mPendingAnimatingRenderNodes.push_back(animatingNode);
+}
+
+void RootRenderNode::attachPendingVectorDrawableAnimators() {
+    mRunningVDAnimators.insert(mPendingVectorDrawableAnimators.begin(),
+                               mPendingVectorDrawableAnimators.end());
+    mPendingVectorDrawableAnimators.clear();
+}
+
+void RootRenderNode::detachAnimators() {
+    // Remove animators from the list and post a delayed message in future to end the animator
+    // For infinite animators, remove the listener so we no longer hold a global ref to the AVD
+    // java object, and therefore the AVD objects in both native and Java can be properly
+    // released.
+    for (auto& anim : mRunningVDAnimators) {
+        detachVectorDrawableAnimator(anim.get());
+        anim->clearOneShotListener();
+    }
+    for (auto& anim : mPausedVDAnimators) {
+        anim->clearOneShotListener();
+    }
+    mRunningVDAnimators.clear();
+    mPausedVDAnimators.clear();
+}
+
+// Move all the animators to the paused list, and send a delayed message to notify the finished
+// listener.
+void RootRenderNode::pauseAnimators() {
+    mPausedVDAnimators.insert(mRunningVDAnimators.begin(), mRunningVDAnimators.end());
+    for (auto& anim : mRunningVDAnimators) {
+        detachVectorDrawableAnimator(anim.get());
+    }
+    mRunningVDAnimators.clear();
+}
+
+void RootRenderNode::doAttachAnimatingNodes(AnimationContext* context) {
+    for (size_t i = 0; i < mPendingAnimatingRenderNodes.size(); i++) {
+        RenderNode* node = mPendingAnimatingRenderNodes[i].get();
+        context->addAnimatingRenderNode(*node);
+    }
+    mPendingAnimatingRenderNodes.clear();
+}
+
+// Run VectorDrawable animators after prepareTree.
+void RootRenderNode::runVectorDrawableAnimators(AnimationContext* context, TreeInfo& info) {
+    // Push staging.
+    if (info.mode == TreeInfo::MODE_FULL) {
+        pushStagingVectorDrawableAnimators(context);
+    }
+
+    // Run the animators in the running list.
+    for (auto it = mRunningVDAnimators.begin(); it != mRunningVDAnimators.end();) {
+        if ((*it)->animate(*context)) {
+            it = mRunningVDAnimators.erase(it);
+        } else {
+            it++;
+        }
+    }
+
+    // Run the animators in paused list during full sync.
+    if (info.mode == TreeInfo::MODE_FULL) {
+        // During full sync we also need to pulse paused animators, in case their targets
+        // have been added back to the display list. All the animators that passed the
+        // scheduled finish time will be removed from the paused list.
+        for (auto it = mPausedVDAnimators.begin(); it != mPausedVDAnimators.end();) {
+            if ((*it)->animate(*context)) {
+                // Animator has finished, remove from the list.
+                it = mPausedVDAnimators.erase(it);
+            } else {
+                it++;
+            }
+        }
+    }
+
+    // Move the animators with a target not in DisplayList to paused list.
+    for (auto it = mRunningVDAnimators.begin(); it != mRunningVDAnimators.end();) {
+        if (!(*it)->getVectorDrawable()->getPropertyChangeWillBeConsumed()) {
+            // Vector Drawable is not in the display list, we should remove this animator from
+            // the list, put it in the paused list, and post a delayed message to end the
+            // animator.
+            detachVectorDrawableAnimator(it->get());
+            mPausedVDAnimators.insert(*it);
+            it = mRunningVDAnimators.erase(it);
+        } else {
+            it++;
+        }
+    }
+
+    // Move the animators with a target in DisplayList from paused list to running list, and
+    // trim paused list.
+    if (info.mode == TreeInfo::MODE_FULL) {
+        // Check whether any paused animator's target is back in Display List. If so, put the
+        // animator back in the running list.
+        for (auto it = mPausedVDAnimators.begin(); it != mPausedVDAnimators.end();) {
+            if ((*it)->getVectorDrawable()->getPropertyChangeWillBeConsumed()) {
+                mRunningVDAnimators.insert(*it);
+                it = mPausedVDAnimators.erase(it);
+            } else {
+                it++;
+            }
+        }
+        // Trim paused VD animators at full sync, so that when Java loses reference to an
+        // animator, we know we won't be requested to animate it any more, then we remove such
+        // animators from the paused list so they can be properly freed. We also remove the
+        // animators from paused list when the time elapsed since start has exceeded duration.
+        trimPausedVDAnimators(context);
+    }
+
+    info.out.hasAnimations |= !mRunningVDAnimators.empty();
+}
+
+void RootRenderNode::trimPausedVDAnimators(AnimationContext* context) {
+    // Trim paused vector drawable animator list.
+    for (auto it = mPausedVDAnimators.begin(); it != mPausedVDAnimators.end();) {
+        // Remove paused VD animator if no one else is referencing it. Note that animators that
+        // have passed scheduled finish time are removed from list when they are being pulsed
+        // before prepare tree.
+        // TODO: this is a bit hacky, need to figure out a better way to track when the paused
+        // animators should be freed.
+        if ((*it)->getStrongCount() == 1) {
+            it = mPausedVDAnimators.erase(it);
+        } else {
+            it++;
+        }
+    }
+}
+
+void RootRenderNode::pushStagingVectorDrawableAnimators(AnimationContext* context) {
+    for (auto& anim : mRunningVDAnimators) {
+        anim->pushStaging(*context);
+    }
+}
+
+void RootRenderNode::destroy() {
+    for (auto& renderNode : mPendingAnimatingRenderNodes) {
+        renderNode->animators().endAllStagingAnimators();
+    }
+    mPendingAnimatingRenderNodes.clear();
+    mPendingVectorDrawableAnimators.clear();
+}
+
+void RootRenderNode::addVectorDrawableAnimator(PropertyValuesAnimatorSet* anim) {
+    mPendingVectorDrawableAnimators.insert(anim);
+}
+
+void RootRenderNode::detachVectorDrawableAnimator(PropertyValuesAnimatorSet* anim) {
+    if (anim->isInfinite() || !anim->isRunning()) {
+        // Do not need to post anything if the animation is infinite (i.e. no meaningful
+        // end listener action), or if the animation has already ended.
+        return;
+    }
+    nsecs_t remainingTimeInMs = anim->getRemainingPlayTime();
+    // Post a delayed onFinished event that is scheduled to be handled when the animator ends.
+    if (anim->getOneShotListener()) {
+        // VectorDrawable's oneshot listener is updated when there are user triggered animation
+        // lifecycle changes, such as start(), end(), etc. By using checking and clearing
+        // one shot listener, we ensure the same end listener event gets posted only once.
+        // Therefore no duplicates. Another benefit of using one shot listener is that no
+        // removal is necessary: the end time of animation will not change unless triggered by
+        // user events, in which case the already posted listener's id will become stale, and
+        // the onFinished callback will then be ignored.
+        sp<FinishAndInvokeListener> message = new FinishAndInvokeListener(anim);
+        auto looper = Looper::getForThread();
+        LOG_ALWAYS_FATAL_IF(looper == nullptr, "Not on a looper thread?");
+        looper->sendMessageDelayed(ms2ns(remainingTimeInMs), message, 0);
+        anim->clearOneShotListener();
+    }
+}
+
+class AnimationContextBridge : public AnimationContext {
+public:
+    AnimationContextBridge(renderthread::TimeLord& clock, RootRenderNode* rootNode)
+            : AnimationContext(clock), mRootNode(rootNode) {}
+
+    virtual ~AnimationContextBridge() {}
+
+    // Marks the start of a frame, which will update the frame time and move all
+    // next frame animations into the current frame
+    virtual void startFrame(TreeInfo::TraversalMode mode) {
+        if (mode == TreeInfo::MODE_FULL) {
+            mRootNode->doAttachAnimatingNodes(this);
+            mRootNode->attachPendingVectorDrawableAnimators();
+        }
+        AnimationContext::startFrame(mode);
+    }
+
+    // Runs any animations still left in mCurrentFrameAnimations
+    virtual void runRemainingAnimations(TreeInfo& info) {
+        AnimationContext::runRemainingAnimations(info);
+        mRootNode->runVectorDrawableAnimators(this, info);
+    }
+
+    virtual void pauseAnimators() override { mRootNode->pauseAnimators(); }
+
+    virtual void callOnFinished(BaseRenderNodeAnimator* animator, AnimationListener* listener) {
+        listener->onAnimationFinished(animator);
+    }
+
+    virtual void destroy() {
+        AnimationContext::destroy();
+        mRootNode->detachAnimators();
+    }
+
+private:
+    sp<RootRenderNode> mRootNode;
+};
+
+AnimationContext* ContextFactoryImpl::createAnimationContext(renderthread::TimeLord& clock) {
+    return new AnimationContextBridge(clock, mRootNode);
+}
+
+}  // namespace android::uirenderer
diff --git a/libs/hwui/RootRenderNode.h b/libs/hwui/RootRenderNode.h
new file mode 100644
index 0000000..ea10921
--- /dev/null
+++ b/libs/hwui/RootRenderNode.h
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <utils/Looper.h>
+
+#include <set>
+#include <vector>
+
+#include "AnimationContext.h"
+#include "Animator.h"
+#include "PropertyValuesAnimatorSet.h"
+#include "RenderNode.h"
+
+namespace android::uirenderer {
+
+class ANDROID_API RootRenderNode : public RenderNode {
+public:
+    ANDROID_API explicit RootRenderNode(std::unique_ptr<ErrorHandler> errorHandler)
+            : RenderNode(), mErrorHandler(std::move(errorHandler)) {}
+
+    ANDROID_API virtual ~RootRenderNode() {}
+
+    virtual void prepareTree(TreeInfo& info) override;
+
+    ANDROID_API void attachAnimatingNode(RenderNode* animatingNode);
+
+    void attachPendingVectorDrawableAnimators();
+
+    void detachAnimators();
+
+    void pauseAnimators();
+
+    void doAttachAnimatingNodes(AnimationContext* context);
+
+    // Run VectorDrawable animators after prepareTree.
+    void runVectorDrawableAnimators(AnimationContext* context, TreeInfo& info);
+
+    void trimPausedVDAnimators(AnimationContext* context);
+
+    void pushStagingVectorDrawableAnimators(AnimationContext* context);
+
+    ANDROID_API void destroy();
+
+    ANDROID_API void addVectorDrawableAnimator(PropertyValuesAnimatorSet* anim);
+
+private:
+    const std::unique_ptr<ErrorHandler> mErrorHandler;
+    std::vector<sp<RenderNode> > mPendingAnimatingRenderNodes;
+    std::set<sp<PropertyValuesAnimatorSet> > mPendingVectorDrawableAnimators;
+    std::set<sp<PropertyValuesAnimatorSet> > mRunningVDAnimators;
+    // mPausedVDAnimators stores a list of animators that have not yet passed the finish time, but
+    // their VectorDrawable targets are no longer in the DisplayList. We skip these animators when
+    // render thread runs animators independent of UI thread (i.e. RT_ONLY mode). These animators
+    // need to be re-activated once their VD target is added back into DisplayList. Since that could
+    // only happen when we do a full sync, we need to make sure to pulse these paused animators at
+    // full sync. If any animator's VD target is found in DisplayList during a full sync, we move
+    // the animator back to the running list.
+    std::set<sp<PropertyValuesAnimatorSet> > mPausedVDAnimators;
+
+    void detachVectorDrawableAnimator(PropertyValuesAnimatorSet* anim);
+};
+
+class ANDROID_API ContextFactoryImpl : public IContextFactory {
+public:
+    ANDROID_API explicit ContextFactoryImpl(RootRenderNode* rootNode) : mRootNode(rootNode) {}
+
+    ANDROID_API virtual AnimationContext* createAnimationContext(
+            renderthread::TimeLord& clock) override;
+
+private:
+    RootRenderNode* mRootNode;
+};
+
+}  // namespace android::uirenderer
diff --git a/libs/hwui/TreeInfo.h b/libs/hwui/TreeInfo.h
index 7e8d12f..f2481f8 100644
--- a/libs/hwui/TreeInfo.h
+++ b/libs/hwui/TreeInfo.h
@@ -40,7 +40,6 @@
 public:
     virtual void onError(const std::string& message) = 0;
 
-protected:
     virtual ~ErrorHandler() = default;
 };