blob: 88fc60856feb7c8e5e5ae2fa9a511c0eb3b1d464 [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
John Reck113e0822014-03-18 09:22:59 -070019#include <SkCamera.h>
20#include <SkMatrix.h>
21
John Reck113e0822014-03-18 09:22:59 -070022#include <utils/LinearAllocator.h>
23#include <utils/RefBase.h>
John Reck113e0822014-03-18 09:22:59 -070024#include <utils/String8.h>
John Reck113e0822014-03-18 09:22:59 -070025
26#include <cutils/compiler.h>
27
28#include <androidfw/ResourceTypes.h>
29
John Reck68bfe0a2014-06-24 15:34:58 -070030#include "AnimatorManager.h"
John Reck113e0822014-03-18 09:22:59 -070031#include "Debug.h"
32#include "Matrix.h"
John Reck113e0822014-03-18 09:22:59 -070033#include "DisplayList.h"
34#include "RenderProperties.h"
35
John Reck272a6852015-07-29 16:48:58 -070036#include <vector>
37
John Reck113e0822014-03-18 09:22:59 -070038class SkBitmap;
39class SkPaint;
40class SkPath;
41class SkRegion;
42
43namespace android {
44namespace uirenderer {
45
John Reck113e0822014-03-18 09:22:59 -070046class DisplayListOp;
Chris Craikdb663fe2015-04-20 13:34:45 -070047class DisplayListCanvas;
John Reck113e0822014-03-18 09:22:59 -070048class OpenGLRenderer;
49class Rect;
50class Layer;
51class SkiaShader;
52
53class ClipRectOp;
54class SaveLayerOp;
55class SaveOp;
56class RestoreToCountOp;
Chris Craika7090e02014-06-20 16:01:00 -070057class DrawRenderNodeOp;
Tom Hudson2dc236b2014-10-15 15:46:42 -040058class TreeInfo;
John Reck113e0822014-03-18 09:22:59 -070059
John Recke248bd12015-08-05 13:53:53 -070060namespace proto {
61class RenderNode;
62}
63
John Reck113e0822014-03-18 09:22:59 -070064/**
65 * Primary class for storing recorded canvas commands, as well as per-View/ViewGroup display properties.
66 *
67 * Recording of canvas commands is somewhat similar to SkPicture, except the canvas-recording
Chris Craikdb663fe2015-04-20 13:34:45 -070068 * functionality is split between DisplayListCanvas (which manages the recording), DisplayListData
John Reck113e0822014-03-18 09:22:59 -070069 * (which holds the actual data), and DisplayList (which holds properties and performs playback onto
70 * a renderer).
71 *
72 * Note that DisplayListData is swapped out from beneath an individual DisplayList when a view's
73 * recorded stream of canvas operations is refreshed. The DisplayList (and its properties) stay
74 * attached.
75 */
John Reck087bc0c2014-04-04 16:20:08 -070076class RenderNode : public VirtualLightRefBase {
John Reck113e0822014-03-18 09:22:59 -070077public:
John Reckff941dc2014-05-14 16:34:14 -070078 enum DirtyPropertyMask {
79 GENERIC = 1 << 1,
80 TRANSLATION_X = 1 << 2,
81 TRANSLATION_Y = 1 << 3,
82 TRANSLATION_Z = 1 << 4,
83 SCALE_X = 1 << 5,
84 SCALE_Y = 1 << 6,
85 ROTATION = 1 << 7,
86 ROTATION_X = 1 << 8,
87 ROTATION_Y = 1 << 9,
88 X = 1 << 10,
89 Y = 1 << 11,
90 Z = 1 << 12,
91 ALPHA = 1 << 13,
John Recka7c2ea22014-08-08 13:21:00 -070092 DISPLAY_LIST = 1 << 14,
John Reckff941dc2014-05-14 16:34:14 -070093 };
94
John Reck113e0822014-03-18 09:22:59 -070095 ANDROID_API RenderNode();
John Recke45b1fd2014-04-15 09:50:16 -070096 ANDROID_API virtual ~RenderNode();
John Reck113e0822014-03-18 09:22:59 -070097
98 // See flags defined in DisplayList.java
99 enum ReplayFlag {
100 kReplayFlag_ClipChildren = 0x1
101 };
102
John Reck443a7142014-09-04 17:40:05 -0700103 void debugDumpLayers(const char* prefix);
John Reck113e0822014-03-18 09:22:59 -0700104
John Reck8de65a82014-04-09 15:23:38 -0700105 ANDROID_API void setStagingDisplayList(DisplayListData* newData);
John Reck113e0822014-03-18 09:22:59 -0700106
107 void computeOrdering();
Chris Craikb265e2c2014-03-27 15:50:09 -0700108
Chris Craik80d49022014-06-20 15:03:43 -0700109 void defer(DeferStateStruct& deferStruct, const int level);
110 void replay(ReplayStateStruct& replayStruct, const int level);
John Reck113e0822014-03-18 09:22:59 -0700111
112 ANDROID_API void output(uint32_t level = 1);
John Reckfe5e7b72014-05-23 17:42:28 -0700113 ANDROID_API int getDebugSize();
John Recke248bd12015-08-05 13:53:53 -0700114 void copyTo(proto::RenderNode* node);
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
John Reck119907c2014-08-14 09:02:01 -0700177 AnimatorManager& animators() { return mAnimatorManager; }
178
Chris Craik69e5adf2014-08-14 13:34:01 -0700179 void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false) const;
180
John Reck113e0822014-03-18 09:22:59 -0700181private:
Chris Craika7090e02014-06-20 16:01:00 -0700182 typedef key_value_pair_t<float, DrawRenderNodeOp*> ZDrawRenderNodeOpPair;
John Reck113e0822014-03-18 09:22:59 -0700183
John Reck272a6852015-07-29 16:48:58 -0700184 static size_t findNonNegativeIndex(const std::vector<ZDrawRenderNodeOpPair>& nodes) {
John Reck113e0822014-03-18 09:22:59 -0700185 for (size_t i = 0; i < nodes.size(); i++) {
186 if (nodes[i].key >= 0.0f) return i;
187 }
188 return nodes.size();
189 }
190
Chris Craikb9ce116d2015-08-20 15:14:06 -0700191 enum class ChildrenSelectMode {
192 NegativeZChildren,
193 PositiveZChildren
John Reck113e0822014-03-18 09:22:59 -0700194 };
195
Chris Craika7090e02014-06-20 16:01:00 -0700196 void computeOrderingImpl(DrawRenderNodeOp* opState,
John Reck272a6852015-07-29 16:48:58 -0700197 std::vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700198 const mat4* transformFromProjectionSurface);
199
200 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700201 inline void setViewProperties(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700202
Chris Craik8afd0f22014-08-21 17:41:57 -0700203 void buildZSortedChildList(const DisplayListData::Chunk& chunk,
John Reck272a6852015-07-29 16:48:58 -0700204 std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes);
John Reck113e0822014-03-18 09:22:59 -0700205
Chris Craikb265e2c2014-03-27 15:50:09 -0700206 template<class T>
207 inline void issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler);
208
John Reck113e0822014-03-18 09:22:59 -0700209 template <class T>
Chris Craikc3e75f92014-08-27 15:34:52 -0700210 inline void issueOperationsOf3dChildren(ChildrenSelectMode mode,
John Reck272a6852015-07-29 16:48:58 -0700211 const Matrix4& initialTransform, const std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craik80d49022014-06-20 15:03:43 -0700212 OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700213
214 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700215 inline void issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700216
Chris Craikb265e2c2014-03-27 15:50:09 -0700217 /**
218 * Issue the RenderNode's operations into a handler, recursing for subtrees through
Chris Craika7090e02014-06-20 16:01:00 -0700219 * DrawRenderNodeOp's defer() or replay() methods
Chris Craikb265e2c2014-03-27 15:50:09 -0700220 */
John Reck113e0822014-03-18 09:22:59 -0700221 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700222 inline void issueOperations(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700223
224 class TextContainer {
225 public:
226 size_t length() const {
227 return mByteLength;
228 }
229
230 const char* text() const {
231 return (const char*) mText;
232 }
233
234 size_t mByteLength;
235 const char* mText;
236 };
237
Chris Craika766cb22015-06-08 16:49:43 -0700238 void prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer);
John Reck25fbb3f2014-06-12 13:46:45 -0700239 void pushStagingPropertiesChanges(TreeInfo& info);
240 void pushStagingDisplayListChanges(TreeInfo& info);
Chris Craika766cb22015-06-08 16:49:43 -0700241 void prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayListData* subtree);
John Reck25fbb3f2014-06-12 13:46:45 -0700242 void applyLayerPropertiesToLayer(TreeInfo& info);
John Recka7c2ea22014-08-08 13:21:00 -0700243 void prepareLayer(TreeInfo& info, uint32_t dirtyMask);
John Reck25fbb3f2014-06-12 13:46:45 -0700244 void pushLayerUpdate(TreeInfo& info);
John Reckdcba6722014-07-08 13:59:49 -0700245 void deleteDisplayListData();
John Reck0a973302014-07-16 13:29:45 -0700246 void damageSelf(TreeInfo& info);
John Reckdcba6722014-07-08 13:59:49 -0700247
248 void incParentRefCount() { mParentCount++; }
249 void decParentRefCount();
John Reck8de65a82014-04-09 15:23:38 -0700250
John Reck113e0822014-03-18 09:22:59 -0700251 String8 mName;
John Reck113e0822014-03-18 09:22:59 -0700252
John Reckff941dc2014-05-14 16:34:14 -0700253 uint32_t mDirtyPropertyFields;
John Reck113e0822014-03-18 09:22:59 -0700254 RenderProperties mProperties;
John Reckd0a0b2a2014-03-20 16:28:56 -0700255 RenderProperties mStagingProperties;
256
John Reck8de65a82014-04-09 15:23:38 -0700257 bool mNeedsDisplayListDataSync;
John Reckdcba6722014-07-08 13:59:49 -0700258 // WARNING: Do not delete this directly, you must go through deleteDisplayListData()!
John Reck113e0822014-03-18 09:22:59 -0700259 DisplayListData* mDisplayListData;
John Reck8de65a82014-04-09 15:23:38 -0700260 DisplayListData* mStagingDisplayListData;
John Reck113e0822014-03-18 09:22:59 -0700261
John Reck68bfe0a2014-06-24 15:34:58 -0700262 friend class AnimatorManager;
263 AnimatorManager mAnimatorManager;
John Recke45b1fd2014-04-15 09:50:16 -0700264
John Reck25fbb3f2014-06-12 13:46:45 -0700265 // Owned by RT. Lifecycle is managed by prepareTree(), with the exception
266 // being in ~RenderNode() which may happen on any thread.
267 Layer* mLayer;
268
John Reck113e0822014-03-18 09:22:59 -0700269 /**
270 * Draw time state - these properties are only set and used during rendering
271 */
272
273 // for projection surfaces, contains a list of all children items
John Reck272a6852015-07-29 16:48:58 -0700274 std::vector<DrawRenderNodeOp*> mProjectedNodes;
John Reckdcba6722014-07-08 13:59:49 -0700275
276 // How many references our parent(s) have to us. Typically this should alternate
277 // between 2 and 1 (when a staging push happens we inc first then dec)
278 // When this hits 0 we are no longer in the tree, so any hardware resources
279 // (specifically Layers) should be released.
280 // This is *NOT* thread-safe, and should therefore only be tracking
281 // mDisplayListData, not mStagingDisplayListData.
282 uint32_t mParentCount;
John Reck113e0822014-03-18 09:22:59 -0700283}; // class RenderNode
284
285} /* namespace uirenderer */
286} /* namespace android */
287
288#endif /* RENDERNODE_H */