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 
17 #pragma once
18 
19 #include "Properties.h"
20 #include "utils/Macros.h"
21 
22 #include <utils/Timers.h>
23 #include "SkSize.h"
24 
25 #include <string>
26 
27 namespace android {
28 namespace uirenderer {
29 
30 namespace renderthread {
31 class CanvasContext;
32 }
33 
34 class DamageAccumulator;
35 class LayerUpdateQueue;
36 class RenderNode;
37 class RenderState;
38 
39 class ErrorHandler {
40 public:
41     virtual void onError(const std::string& message) = 0;
42 
43 protected:
44     virtual ~ErrorHandler() = default;
45 };
46 
47 class TreeObserver {
48 public:
49     // Called when a RenderNode's parent count hits 0.
50     // Due to the unordered nature of tree pushes, once prepareTree
51     // is finished it is possible that the node was "resurrected" and has
52     // a non-zero parent count.
53     virtual void onMaybeRemovedFromTree(RenderNode* node) = 0;
54 
55 protected:
56     virtual ~TreeObserver() = default;
57 };
58 
59 // This would be a struct, but we want to PREVENT_COPY_AND_ASSIGN
60 class TreeInfo {
61     PREVENT_COPY_AND_ASSIGN(TreeInfo);
62 
63 public:
64     enum TraversalMode {
65         // The full monty - sync, push, run animators, etc... Used by DrawFrameTask
66         // May only be used if both the UI thread and RT thread are blocked on the
67         // prepare
68         MODE_FULL,
69         // Run only what can be done safely on RT thread. Currently this only means
70         // animators, but potentially things like SurfaceTexture updates
71         // could be handled by this as well if there are no listeners
72         MODE_RT_ONLY,
73     };
74 
75     TreeInfo(TraversalMode mode, renderthread::CanvasContext& canvasContext);
76 
77     TraversalMode mode;
78     // TODO: Remove this? Currently this is used to signal to stop preparing
79     // textures if we run out of cache space.
80     bool prepareTextures;
81     renderthread::CanvasContext& canvasContext;
82     // TODO: buildLayer uses this to suppress running any animations, but this
83     // should probably be refactored somehow. The reason this is done is
84     // because buildLayer is not setup for injecting the animationHook, as well
85     // as this being otherwise wasted work as all the animators will be
86     // re-evaluated when the frame is actually drawn
87     bool runAnimations = true;
88 
89     // Must not be null during actual usage
90     DamageAccumulator* damageAccumulator = nullptr;
91     int64_t damageGenerationId = 0;
92 
93     LayerUpdateQueue* layerUpdateQueue = nullptr;
94     ErrorHandler* errorHandler = nullptr;
95 
96     bool updateWindowPositions = false;
97 
98     int disableForceDark;
99 
100     const SkISize screenSize;
101 
102     struct Out {
103         bool hasFunctors = false;
104         // This is only updated if evaluateAnimations is true
105         bool hasAnimations = false;
106         // This is set to true if there is an animation that RenderThread cannot
107         // animate itself, such as if hasFunctors is true
108         // This is only set if hasAnimations is true
109         bool requiresUiRedraw = false;
110         // This is set to true if draw() can be called this frame
111         // false means that we must delay until the next vsync pulse as frame
112         // production is outrunning consumption
113         // NOTE that if this is false CanvasContext will set either requiresUiRedraw
114         // *OR* will post itself for the next vsync automatically, use this
115         // only to avoid calling draw()
116         bool canDrawThisFrame = true;
117         // Sentinel for animatedImageDelay meaning there is no need to post such
118         // a message.
119         static constexpr nsecs_t kNoAnimatedImageDelay = -1;
120         // This is used to post a message to redraw when it is time to draw the
121         // next frame of an AnimatedImageDrawable.
122         nsecs_t animatedImageDelay = kNoAnimatedImageDelay;
123     } out;
124 
125     // This flag helps to disable projection for receiver nodes that do not have any backward
126     // projected children.
127     bool hasBackwardProjectedNodes = false;
128     // TODO: Damage calculations
129 };
130 
131 } /* namespace uirenderer */
132 } /* namespace android */
133