blob: 4063d4bd53ceeec6f90a7176e8b94d1c92b0a770 [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>
25#include <utils/Vector.h>
26
27#include <cutils/compiler.h>
28
29#include <androidfw/ResourceTypes.h>
30
John Reck68bfe0a2014-06-24 15:34:58 -070031#include "AnimatorManager.h"
John Reck113e0822014-03-18 09:22:59 -070032#include "Debug.h"
33#include "Matrix.h"
John Reck113e0822014-03-18 09:22:59 -070034#include "DisplayList.h"
35#include "RenderProperties.h"
36
37class SkBitmap;
38class SkPaint;
39class SkPath;
40class SkRegion;
41
42namespace android {
43namespace uirenderer {
44
John Reck113e0822014-03-18 09:22:59 -070045class DisplayListOp;
Chris Craikdb663fe2015-04-20 13:34:45 -070046class DisplayListCanvas;
John Reck113e0822014-03-18 09:22:59 -070047class OpenGLRenderer;
48class Rect;
49class Layer;
50class SkiaShader;
51
52class ClipRectOp;
53class SaveLayerOp;
54class SaveOp;
55class RestoreToCountOp;
Chris Craika7090e02014-06-20 16:01:00 -070056class DrawRenderNodeOp;
Tom Hudson2dc236b2014-10-15 15:46:42 -040057class TreeInfo;
John Reck113e0822014-03-18 09:22:59 -070058
59/**
60 * Primary class for storing recorded canvas commands, as well as per-View/ViewGroup display properties.
61 *
62 * Recording of canvas commands is somewhat similar to SkPicture, except the canvas-recording
Chris Craikdb663fe2015-04-20 13:34:45 -070063 * functionality is split between DisplayListCanvas (which manages the recording), DisplayListData
John Reck113e0822014-03-18 09:22:59 -070064 * (which holds the actual data), and DisplayList (which holds properties and performs playback onto
65 * a renderer).
66 *
67 * Note that DisplayListData is swapped out from beneath an individual DisplayList when a view's
68 * recorded stream of canvas operations is refreshed. The DisplayList (and its properties) stay
69 * attached.
70 */
John Reck087bc0c2014-04-04 16:20:08 -070071class RenderNode : public VirtualLightRefBase {
John Reck113e0822014-03-18 09:22:59 -070072public:
John Reckff941dc2014-05-14 16:34:14 -070073 enum DirtyPropertyMask {
74 GENERIC = 1 << 1,
75 TRANSLATION_X = 1 << 2,
76 TRANSLATION_Y = 1 << 3,
77 TRANSLATION_Z = 1 << 4,
78 SCALE_X = 1 << 5,
79 SCALE_Y = 1 << 6,
80 ROTATION = 1 << 7,
81 ROTATION_X = 1 << 8,
82 ROTATION_Y = 1 << 9,
83 X = 1 << 10,
84 Y = 1 << 11,
85 Z = 1 << 12,
86 ALPHA = 1 << 13,
John Recka7c2ea22014-08-08 13:21:00 -070087 DISPLAY_LIST = 1 << 14,
John Reckff941dc2014-05-14 16:34:14 -070088 };
89
John Reck113e0822014-03-18 09:22:59 -070090 ANDROID_API RenderNode();
John Recke45b1fd2014-04-15 09:50:16 -070091 ANDROID_API virtual ~RenderNode();
John Reck113e0822014-03-18 09:22:59 -070092
93 // See flags defined in DisplayList.java
94 enum ReplayFlag {
95 kReplayFlag_ClipChildren = 0x1
96 };
97
John Reck0e89e2b2014-10-31 14:49:06 -070098 static void outputLogBuffer(int fd);
John Reck443a7142014-09-04 17:40:05 -070099 void debugDumpLayers(const char* prefix);
John Reck113e0822014-03-18 09:22:59 -0700100
John Reck8de65a82014-04-09 15:23:38 -0700101 ANDROID_API void setStagingDisplayList(DisplayListData* newData);
John Reck113e0822014-03-18 09:22:59 -0700102
103 void computeOrdering();
Chris Craikb265e2c2014-03-27 15:50:09 -0700104
Chris Craik80d49022014-06-20 15:03:43 -0700105 void defer(DeferStateStruct& deferStruct, const int level);
106 void replay(ReplayStateStruct& replayStruct, const int level);
John Reck113e0822014-03-18 09:22:59 -0700107
108 ANDROID_API void output(uint32_t level = 1);
John Reckfe5e7b72014-05-23 17:42:28 -0700109 ANDROID_API int getDebugSize();
John Reck113e0822014-03-18 09:22:59 -0700110
111 bool isRenderable() const {
Chris Craik8afd0f22014-08-21 17:41:57 -0700112 return mDisplayListData && !mDisplayListData->isEmpty();
John Reck113e0822014-03-18 09:22:59 -0700113 }
114
John Recka447d292014-06-11 18:39:44 -0700115 bool hasProjectionReceiver() const {
116 return mDisplayListData && mDisplayListData->projectionReceiveIndex >= 0;
117 }
118
Chris Craikdefb7f32014-04-08 18:17:07 -0700119 const char* getName() const {
120 return mName.string();
121 }
122
John Reck113e0822014-03-18 09:22:59 -0700123 void setName(const char* name) {
124 if (name) {
125 char* lastPeriod = strrchr(name, '.');
126 if (lastPeriod) {
127 mName.setTo(lastPeriod + 1);
128 } else {
129 mName.setTo(name);
130 }
131 }
132 }
133
John Reckff941dc2014-05-14 16:34:14 -0700134 bool isPropertyFieldDirty(DirtyPropertyMask field) const {
135 return mDirtyPropertyFields & field;
136 }
137
138 void setPropertyFieldsDirty(uint32_t fields) {
139 mDirtyPropertyFields |= fields;
140 }
141
John Recke4267ea2014-06-03 15:53:15 -0700142 const RenderProperties& properties() const {
John Reck113e0822014-03-18 09:22:59 -0700143 return mProperties;
144 }
145
John Reck52244ff2014-05-01 21:27:37 -0700146 RenderProperties& animatorProperties() {
147 return mProperties;
148 }
149
John Reckd0a0b2a2014-03-20 16:28:56 -0700150 const RenderProperties& stagingProperties() {
151 return mStagingProperties;
152 }
153
154 RenderProperties& mutateStagingProperties() {
John Reckd0a0b2a2014-03-20 16:28:56 -0700155 return mStagingProperties;
156 }
157
John Reck113e0822014-03-18 09:22:59 -0700158 int getWidth() {
159 return properties().getWidth();
160 }
161
162 int getHeight() {
163 return properties().getHeight();
164 }
165
John Recke45b1fd2014-04-15 09:50:16 -0700166 ANDROID_API virtual void prepareTree(TreeInfo& info);
John Reckdcba6722014-07-08 13:59:49 -0700167 void destroyHardwareResources();
John Recke45b1fd2014-04-15 09:50:16 -0700168
169 // UI thread only!
John Reck68bfe0a2014-06-24 15:34:58 -0700170 ANDROID_API void addAnimator(const sp<BaseRenderNodeAnimator>& animator);
John Reck668f0e32014-03-26 15:10:40 -0700171
John Reck119907c2014-08-14 09:02:01 -0700172 AnimatorManager& animators() { return mAnimatorManager; }
173
Chris Craik69e5adf2014-08-14 13:34:01 -0700174 void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false) const;
175
John Reck113e0822014-03-18 09:22:59 -0700176private:
Chris Craika7090e02014-06-20 16:01:00 -0700177 typedef key_value_pair_t<float, DrawRenderNodeOp*> ZDrawRenderNodeOpPair;
John Reck113e0822014-03-18 09:22:59 -0700178
Chris Craika7090e02014-06-20 16:01:00 -0700179 static size_t findNonNegativeIndex(const Vector<ZDrawRenderNodeOpPair>& nodes) {
John Reck113e0822014-03-18 09:22:59 -0700180 for (size_t i = 0; i < nodes.size(); i++) {
181 if (nodes[i].key >= 0.0f) return i;
182 }
183 return nodes.size();
184 }
185
186 enum ChildrenSelectMode {
187 kNegativeZChildren,
188 kPositiveZChildren
189 };
190
Chris Craika7090e02014-06-20 16:01:00 -0700191 void computeOrderingImpl(DrawRenderNodeOp* opState,
Chris Craik3f0854292014-04-15 16:18:08 -0700192 const SkPath* outlineOfProjectionSurface,
Chris Craika7090e02014-06-20 16:01:00 -0700193 Vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700194 const mat4* transformFromProjectionSurface);
195
196 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700197 inline void setViewProperties(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700198
Chris Craik8afd0f22014-08-21 17:41:57 -0700199 void buildZSortedChildList(const DisplayListData::Chunk& chunk,
200 Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes);
John Reck113e0822014-03-18 09:22:59 -0700201
Chris Craikb265e2c2014-03-27 15:50:09 -0700202 template<class T>
203 inline void issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler);
204
John Reck113e0822014-03-18 09:22:59 -0700205 template <class T>
Chris Craikc3e75f92014-08-27 15:34:52 -0700206 inline void issueOperationsOf3dChildren(ChildrenSelectMode mode,
207 const Matrix4& initialTransform, const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craik80d49022014-06-20 15:03:43 -0700208 OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700209
210 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700211 inline void issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700212
Chris Craikb265e2c2014-03-27 15:50:09 -0700213 /**
214 * Issue the RenderNode's operations into a handler, recursing for subtrees through
Chris Craika7090e02014-06-20 16:01:00 -0700215 * DrawRenderNodeOp's defer() or replay() methods
Chris Craikb265e2c2014-03-27 15:50:09 -0700216 */
John Reck113e0822014-03-18 09:22:59 -0700217 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700218 inline void issueOperations(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700219
220 class TextContainer {
221 public:
222 size_t length() const {
223 return mByteLength;
224 }
225
226 const char* text() const {
227 return (const char*) mText;
228 }
229
230 size_t mByteLength;
231 const char* mText;
232 };
233
Chris Craika766cb22015-06-08 16:49:43 -0700234 void prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer);
John Reck25fbb3f2014-06-12 13:46:45 -0700235 void pushStagingPropertiesChanges(TreeInfo& info);
236 void pushStagingDisplayListChanges(TreeInfo& info);
Chris Craika766cb22015-06-08 16:49:43 -0700237 void prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayListData* subtree);
John Reck25fbb3f2014-06-12 13:46:45 -0700238 void applyLayerPropertiesToLayer(TreeInfo& info);
John Recka7c2ea22014-08-08 13:21:00 -0700239 void prepareLayer(TreeInfo& info, uint32_t dirtyMask);
John Reck25fbb3f2014-06-12 13:46:45 -0700240 void pushLayerUpdate(TreeInfo& info);
John Reckdcba6722014-07-08 13:59:49 -0700241 void deleteDisplayListData();
John Reck0a973302014-07-16 13:29:45 -0700242 void damageSelf(TreeInfo& info);
John Reckdcba6722014-07-08 13:59:49 -0700243
244 void incParentRefCount() { mParentCount++; }
245 void decParentRefCount();
John Reck8de65a82014-04-09 15:23:38 -0700246
John Reck113e0822014-03-18 09:22:59 -0700247 String8 mName;
John Reck113e0822014-03-18 09:22:59 -0700248
John Reckff941dc2014-05-14 16:34:14 -0700249 uint32_t mDirtyPropertyFields;
John Reck113e0822014-03-18 09:22:59 -0700250 RenderProperties mProperties;
John Reckd0a0b2a2014-03-20 16:28:56 -0700251 RenderProperties mStagingProperties;
252
John Reck8de65a82014-04-09 15:23:38 -0700253 bool mNeedsDisplayListDataSync;
John Reckdcba6722014-07-08 13:59:49 -0700254 // WARNING: Do not delete this directly, you must go through deleteDisplayListData()!
John Reck113e0822014-03-18 09:22:59 -0700255 DisplayListData* mDisplayListData;
John Reck8de65a82014-04-09 15:23:38 -0700256 DisplayListData* mStagingDisplayListData;
John Reck113e0822014-03-18 09:22:59 -0700257
John Reck68bfe0a2014-06-24 15:34:58 -0700258 friend class AnimatorManager;
259 AnimatorManager mAnimatorManager;
John Recke45b1fd2014-04-15 09:50:16 -0700260
John Reck25fbb3f2014-06-12 13:46:45 -0700261 // Owned by RT. Lifecycle is managed by prepareTree(), with the exception
262 // being in ~RenderNode() which may happen on any thread.
263 Layer* mLayer;
264
John Reck113e0822014-03-18 09:22:59 -0700265 /**
266 * Draw time state - these properties are only set and used during rendering
267 */
268
269 // for projection surfaces, contains a list of all children items
Chris Craika7090e02014-06-20 16:01:00 -0700270 Vector<DrawRenderNodeOp*> mProjectedNodes;
John Reckdcba6722014-07-08 13:59:49 -0700271
272 // How many references our parent(s) have to us. Typically this should alternate
273 // between 2 and 1 (when a staging push happens we inc first then dec)
274 // When this hits 0 we are no longer in the tree, so any hardware resources
275 // (specifically Layers) should be released.
276 // This is *NOT* thread-safe, and should therefore only be tracking
277 // mDisplayListData, not mStagingDisplayListData.
278 uint32_t mParentCount;
John Reck113e0822014-03-18 09:22:59 -0700279}; // class RenderNode
280
281} /* namespace uirenderer */
282} /* namespace android */
283
284#endif /* RENDERNODE_H */