TreeInfo.h revision 9eb9f6f8cbbbd87d45da8071aa54cb066a797723
1/*
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#ifndef TREEINFO_H
17#define TREEINFO_H
18
19#include <string>
20
21#include <utils/Timers.h>
22
23#include "DamageAccumulator.h"
24#include "utils/Macros.h"
25
26namespace android {
27namespace uirenderer {
28
29class BaseRenderNodeAnimator;
30class AnimationListener;
31class OpenGLRenderer;
32class RenderState;
33
34class AnimationHook {
35public:
36    virtual void callOnFinished(BaseRenderNodeAnimator* animator, AnimationListener* listener) = 0;
37protected:
38    ~AnimationHook() {}
39};
40
41class ErrorHandler {
42public:
43    virtual void onError(const std::string& message) = 0;
44protected:
45    ~ErrorHandler() {}
46};
47
48// This would be a struct, but we want to PREVENT_COPY_AND_ASSIGN
49class TreeInfo {
50    PREVENT_COPY_AND_ASSIGN(TreeInfo);
51public:
52    enum TraversalMode {
53        // The full monty - sync, push, run animators, etc... Used by DrawFrameTask
54        // May only be used if both the UI thread and RT thread are blocked on the
55        // prepare
56        MODE_FULL,
57        // Run only what can be done safely on RT thread. Currently this only means
58        // animators, but potentially things like SurfaceTexture updates
59        // could be handled by this as well if there are no listeners
60        MODE_RT_ONLY,
61    };
62
63    explicit TreeInfo(TraversalMode mode, RenderState& renderState)
64        : mode(mode)
65        , frameTimeMs(0)
66        , animationHook(NULL)
67        , prepareTextures(mode == MODE_FULL)
68        , runAnimations(true)
69        , damageAccumulator(NULL)
70        , renderState(renderState)
71        , renderer(NULL)
72        , errorHandler(NULL)
73    {}
74
75    explicit TreeInfo(TraversalMode mode, const TreeInfo& clone)
76        : mode(mode)
77        , frameTimeMs(clone.frameTimeMs)
78        , animationHook(clone.animationHook)
79        , prepareTextures(mode == MODE_FULL)
80        , runAnimations(clone.runAnimations)
81        , damageAccumulator(clone.damageAccumulator)
82        , renderState(clone.renderState)
83        , renderer(clone.renderer)
84        , errorHandler(clone.errorHandler)
85    {}
86
87    const TraversalMode mode;
88    nsecs_t frameTimeMs;
89    AnimationHook* animationHook;
90    // TODO: Remove this? Currently this is used to signal to stop preparing
91    // textures if we run out of cache space.
92    bool prepareTextures;
93    // TODO: buildLayer uses this to suppress running any animations, but this
94    // should probably be refactored somehow. The reason this is done is
95    // because buildLayer is not setup for injecting the animationHook, as well
96    // as this being otherwise wasted work as all the animators will be
97    // re-evaluated when the frame is actually drawn
98    bool runAnimations;
99
100    // Must not be null during actual usage
101    DamageAccumulator* damageAccumulator;
102    RenderState& renderState;
103    // The renderer that will be drawing the next frame. Use this to push any
104    // layer updates or similar. May be NULL.
105    OpenGLRenderer* renderer;
106    ErrorHandler* errorHandler;
107
108    struct Out {
109        Out()
110            : hasFunctors(false)
111            , hasAnimations(false)
112            , requiresUiRedraw(false)
113            , canDrawThisFrame(true)
114        {}
115        bool hasFunctors;
116        // This is only updated if evaluateAnimations is true
117        bool hasAnimations;
118        // This is set to true if there is an animation that RenderThread cannot
119        // animate itself, such as if hasFunctors is true
120        // This is only set if hasAnimations is true
121        bool requiresUiRedraw;
122        // This is set to true if draw() can be called this frame
123        // false means that we must delay until the next vsync pulse as frame
124        // production is outrunning consumption
125        // NOTE that if this is false CanvasContext will set either requiresUiRedraw
126        // *OR* will post itself for the next vsync automatically, use this
127        // only to avoid calling draw()
128        bool canDrawThisFrame;
129    } out;
130
131    // TODO: Damage calculations
132};
133
134} /* namespace uirenderer */
135} /* namespace android */
136
137#endif /* TREEINFO_H */
138