blob: 18402b2bed1cf831967112b1ac1b41d6e442d755 [file] [log] [blame]
John Reck113e0822014-03-18 09:22:59 -07001/*
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 RENDERNODE_H
17#define RENDERNODE_H
18
19#ifndef LOG_TAG
20 #define LOG_TAG "OpenGLRenderer"
21#endif
22
23#include <SkCamera.h>
24#include <SkMatrix.h>
25
John Reck113e0822014-03-18 09:22:59 -070026#include <utils/LinearAllocator.h>
27#include <utils/RefBase.h>
John Reck113e0822014-03-18 09:22:59 -070028#include <utils/String8.h>
29#include <utils/Vector.h>
30
31#include <cutils/compiler.h>
32
33#include <androidfw/ResourceTypes.h>
34
John Reck68bfe0a2014-06-24 15:34:58 -070035#include "AnimatorManager.h"
John Recka447d292014-06-11 18:39:44 -070036#include "DamageAccumulator.h"
John Reck113e0822014-03-18 09:22:59 -070037#include "Debug.h"
38#include "Matrix.h"
39#include "DeferredDisplayList.h"
40#include "DisplayList.h"
41#include "RenderProperties.h"
John Recke45b1fd2014-04-15 09:50:16 -070042#include "TreeInfo.h"
John Reck113e0822014-03-18 09:22:59 -070043
44class SkBitmap;
45class SkPaint;
46class SkPath;
47class SkRegion;
48
49namespace android {
50namespace uirenderer {
51
John Reck113e0822014-03-18 09:22:59 -070052class DisplayListOp;
53class DisplayListRenderer;
54class OpenGLRenderer;
55class Rect;
56class Layer;
57class SkiaShader;
58
59class ClipRectOp;
60class SaveLayerOp;
61class SaveOp;
62class RestoreToCountOp;
Chris Craika7090e02014-06-20 16:01:00 -070063class DrawRenderNodeOp;
John Reck113e0822014-03-18 09:22:59 -070064
65/**
66 * Primary class for storing recorded canvas commands, as well as per-View/ViewGroup display properties.
67 *
68 * Recording of canvas commands is somewhat similar to SkPicture, except the canvas-recording
69 * functionality is split between DisplayListRenderer (which manages the recording), DisplayListData
70 * (which holds the actual data), and DisplayList (which holds properties and performs playback onto
71 * a renderer).
72 *
73 * Note that DisplayListData is swapped out from beneath an individual DisplayList when a view's
74 * recorded stream of canvas operations is refreshed. The DisplayList (and its properties) stay
75 * attached.
76 */
John Reck087bc0c2014-04-04 16:20:08 -070077class RenderNode : public VirtualLightRefBase {
John Reck113e0822014-03-18 09:22:59 -070078public:
John Reckff941dc2014-05-14 16:34:14 -070079 enum DirtyPropertyMask {
80 GENERIC = 1 << 1,
81 TRANSLATION_X = 1 << 2,
82 TRANSLATION_Y = 1 << 3,
83 TRANSLATION_Z = 1 << 4,
84 SCALE_X = 1 << 5,
85 SCALE_Y = 1 << 6,
86 ROTATION = 1 << 7,
87 ROTATION_X = 1 << 8,
88 ROTATION_Y = 1 << 9,
89 X = 1 << 10,
90 Y = 1 << 11,
91 Z = 1 << 12,
92 ALPHA = 1 << 13,
John Recka7c2ea22014-08-08 13:21:00 -070093 DISPLAY_LIST = 1 << 14,
John Reckff941dc2014-05-14 16:34:14 -070094 };
95
John Reck113e0822014-03-18 09:22:59 -070096 ANDROID_API RenderNode();
John Recke45b1fd2014-04-15 09:50:16 -070097 ANDROID_API virtual ~RenderNode();
John Reck113e0822014-03-18 09:22:59 -070098
99 // See flags defined in DisplayList.java
100 enum ReplayFlag {
101 kReplayFlag_ClipChildren = 0x1
102 };
103
John Reck113e0822014-03-18 09:22:59 -0700104 ANDROID_API static void outputLogBuffer(int fd);
105
John Reck8de65a82014-04-09 15:23:38 -0700106 ANDROID_API void setStagingDisplayList(DisplayListData* newData);
John Reck113e0822014-03-18 09:22:59 -0700107
108 void computeOrdering();
Chris Craikb265e2c2014-03-27 15:50:09 -0700109
Chris Craik80d49022014-06-20 15:03:43 -0700110 void defer(DeferStateStruct& deferStruct, const int level);
111 void replay(ReplayStateStruct& replayStruct, const int level);
John Reck113e0822014-03-18 09:22:59 -0700112
113 ANDROID_API void output(uint32_t level = 1);
John Reckfe5e7b72014-05-23 17:42:28 -0700114 ANDROID_API int getDebugSize();
John Reck113e0822014-03-18 09:22:59 -0700115
116 bool isRenderable() const {
Chris Craik8afd0f22014-08-21 17:41:57 -0700117 return mDisplayListData && !mDisplayListData->isEmpty();
John Reck113e0822014-03-18 09:22:59 -0700118 }
119
John Recka447d292014-06-11 18:39:44 -0700120 bool hasProjectionReceiver() const {
121 return mDisplayListData && mDisplayListData->projectionReceiveIndex >= 0;
122 }
123
Chris Craikdefb7f32014-04-08 18:17:07 -0700124 const char* getName() const {
125 return mName.string();
126 }
127
John Reck113e0822014-03-18 09:22:59 -0700128 void setName(const char* name) {
129 if (name) {
130 char* lastPeriod = strrchr(name, '.');
131 if (lastPeriod) {
132 mName.setTo(lastPeriod + 1);
133 } else {
134 mName.setTo(name);
135 }
136 }
137 }
138
John Reckff941dc2014-05-14 16:34:14 -0700139 bool isPropertyFieldDirty(DirtyPropertyMask field) const {
140 return mDirtyPropertyFields & field;
141 }
142
143 void setPropertyFieldsDirty(uint32_t fields) {
144 mDirtyPropertyFields |= fields;
145 }
146
John Recke4267ea2014-06-03 15:53:15 -0700147 const RenderProperties& properties() const {
John Reck113e0822014-03-18 09:22:59 -0700148 return mProperties;
149 }
150
John Reck52244ff2014-05-01 21:27:37 -0700151 RenderProperties& animatorProperties() {
152 return mProperties;
153 }
154
John Reckd0a0b2a2014-03-20 16:28:56 -0700155 const RenderProperties& stagingProperties() {
156 return mStagingProperties;
157 }
158
159 RenderProperties& mutateStagingProperties() {
John Reckd0a0b2a2014-03-20 16:28:56 -0700160 return mStagingProperties;
161 }
162
John Reck113e0822014-03-18 09:22:59 -0700163 int getWidth() {
164 return properties().getWidth();
165 }
166
167 int getHeight() {
168 return properties().getHeight();
169 }
170
John Recke45b1fd2014-04-15 09:50:16 -0700171 ANDROID_API virtual void prepareTree(TreeInfo& info);
John Reckdcba6722014-07-08 13:59:49 -0700172 void destroyHardwareResources();
John Recke45b1fd2014-04-15 09:50:16 -0700173
174 // UI thread only!
John Reck68bfe0a2014-06-24 15:34:58 -0700175 ANDROID_API void addAnimator(const sp<BaseRenderNodeAnimator>& animator);
John Reck668f0e32014-03-26 15:10:40 -0700176
Chris Craik69e5adf2014-08-14 13:34:01 -0700177 void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false) const;
178
John Reck113e0822014-03-18 09:22:59 -0700179private:
Chris Craika7090e02014-06-20 16:01:00 -0700180 typedef key_value_pair_t<float, DrawRenderNodeOp*> ZDrawRenderNodeOpPair;
John Reck113e0822014-03-18 09:22:59 -0700181
Chris Craika7090e02014-06-20 16:01:00 -0700182 static size_t findNonNegativeIndex(const Vector<ZDrawRenderNodeOpPair>& nodes) {
John Reck113e0822014-03-18 09:22:59 -0700183 for (size_t i = 0; i < nodes.size(); i++) {
184 if (nodes[i].key >= 0.0f) return i;
185 }
186 return nodes.size();
187 }
188
189 enum ChildrenSelectMode {
190 kNegativeZChildren,
191 kPositiveZChildren
192 };
193
Chris Craika7090e02014-06-20 16:01:00 -0700194 void computeOrderingImpl(DrawRenderNodeOp* opState,
Chris Craik3f0854292014-04-15 16:18:08 -0700195 const SkPath* outlineOfProjectionSurface,
Chris Craika7090e02014-06-20 16:01:00 -0700196 Vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700197 const mat4* transformFromProjectionSurface);
198
199 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700200 inline void setViewProperties(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700201
Chris Craik8afd0f22014-08-21 17:41:57 -0700202 void buildZSortedChildList(const DisplayListData::Chunk& chunk,
203 Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes);
John Reck113e0822014-03-18 09:22:59 -0700204
Chris Craikb265e2c2014-03-27 15:50:09 -0700205 template<class T>
206 inline void issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler);
207
John Reck113e0822014-03-18 09:22:59 -0700208 template <class T>
Chris Craik80d49022014-06-20 15:03:43 -0700209 inline int issueOperationsOfNegZChildren(
Chris Craika7090e02014-06-20 16:01:00 -0700210 const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craik80d49022014-06-20 15:03:43 -0700211 OpenGLRenderer& renderer, T& handler);
212 template <class T>
213 inline void issueOperationsOfPosZChildren(int shadowRestoreTo,
Chris Craika7090e02014-06-20 16:01:00 -0700214 const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craik80d49022014-06-20 15:03:43 -0700215 OpenGLRenderer& renderer, T& handler);
216 template <class T>
Chris Craika7090e02014-06-20 16:01:00 -0700217 inline void issueOperationsOf3dChildren(const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
John Reck113e0822014-03-18 09:22:59 -0700218 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler);
219
220 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700221 inline void issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700222
Chris Craikb265e2c2014-03-27 15:50:09 -0700223 /**
224 * Issue the RenderNode's operations into a handler, recursing for subtrees through
Chris Craika7090e02014-06-20 16:01:00 -0700225 * DrawRenderNodeOp's defer() or replay() methods
Chris Craikb265e2c2014-03-27 15:50:09 -0700226 */
John Reck113e0822014-03-18 09:22:59 -0700227 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700228 inline void issueOperations(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700229
230 class TextContainer {
231 public:
232 size_t length() const {
233 return mByteLength;
234 }
235
236 const char* text() const {
237 return (const char*) mText;
238 }
239
240 size_t mByteLength;
241 const char* mText;
242 };
243
John Reckf4198b72014-04-09 17:00:04 -0700244 void prepareTreeImpl(TreeInfo& info);
John Reck25fbb3f2014-06-12 13:46:45 -0700245 void pushStagingPropertiesChanges(TreeInfo& info);
246 void pushStagingDisplayListChanges(TreeInfo& info);
John Reckf4198b72014-04-09 17:00:04 -0700247 void prepareSubTree(TreeInfo& info, DisplayListData* subtree);
John Reck25fbb3f2014-06-12 13:46:45 -0700248 void applyLayerPropertiesToLayer(TreeInfo& info);
John Recka7c2ea22014-08-08 13:21:00 -0700249 void prepareLayer(TreeInfo& info, uint32_t dirtyMask);
John Reck25fbb3f2014-06-12 13:46:45 -0700250 void pushLayerUpdate(TreeInfo& info);
John Reckdcba6722014-07-08 13:59:49 -0700251 void deleteDisplayListData();
John Reck0a973302014-07-16 13:29:45 -0700252 void damageSelf(TreeInfo& info);
John Reckdcba6722014-07-08 13:59:49 -0700253
254 void incParentRefCount() { mParentCount++; }
255 void decParentRefCount();
John Reck8de65a82014-04-09 15:23:38 -0700256
John Reck113e0822014-03-18 09:22:59 -0700257 String8 mName;
John Reck113e0822014-03-18 09:22:59 -0700258
John Reckff941dc2014-05-14 16:34:14 -0700259 uint32_t mDirtyPropertyFields;
John Reck113e0822014-03-18 09:22:59 -0700260 RenderProperties mProperties;
John Reckd0a0b2a2014-03-20 16:28:56 -0700261 RenderProperties mStagingProperties;
262
John Reck8de65a82014-04-09 15:23:38 -0700263 bool mNeedsDisplayListDataSync;
John Reckdcba6722014-07-08 13:59:49 -0700264 // WARNING: Do not delete this directly, you must go through deleteDisplayListData()!
John Reck113e0822014-03-18 09:22:59 -0700265 DisplayListData* mDisplayListData;
John Reck8de65a82014-04-09 15:23:38 -0700266 DisplayListData* mStagingDisplayListData;
John Reck113e0822014-03-18 09:22:59 -0700267
John Reck68bfe0a2014-06-24 15:34:58 -0700268 friend class AnimatorManager;
269 AnimatorManager mAnimatorManager;
John Recke45b1fd2014-04-15 09:50:16 -0700270
John Reck25fbb3f2014-06-12 13:46:45 -0700271 // Owned by RT. Lifecycle is managed by prepareTree(), with the exception
272 // being in ~RenderNode() which may happen on any thread.
273 Layer* mLayer;
274
John Reck113e0822014-03-18 09:22:59 -0700275 /**
276 * Draw time state - these properties are only set and used during rendering
277 */
278
279 // for projection surfaces, contains a list of all children items
Chris Craika7090e02014-06-20 16:01:00 -0700280 Vector<DrawRenderNodeOp*> mProjectedNodes;
John Reckdcba6722014-07-08 13:59:49 -0700281
282 // How many references our parent(s) have to us. Typically this should alternate
283 // between 2 and 1 (when a staging push happens we inc first then dec)
284 // When this hits 0 we are no longer in the tree, so any hardware resources
285 // (specifically Layers) should be released.
286 // This is *NOT* thread-safe, and should therefore only be tracking
287 // mDisplayListData, not mStagingDisplayListData.
288 uint32_t mParentCount;
John Reck113e0822014-03-18 09:22:59 -0700289}; // class RenderNode
290
291} /* namespace uirenderer */
292} /* namespace android */
293
294#endif /* RENDERNODE_H */