blob: ff673ba32343aa83c8a53b9de850c21b93dacd34 [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"
John Reck113e0822014-03-18 09:22:59 -070032#include "DisplayList.h"
Chris Craikb565df12015-10-05 13:00:52 -070033#include "Matrix.h"
John Reck113e0822014-03-18 09:22:59 -070034#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
Chris Craikb565df12015-10-05 13:00:52 -070046class CanvasState;
John Reck113e0822014-03-18 09:22:59 -070047class DisplayListOp;
Chris Craikdb663fe2015-04-20 13:34:45 -070048class DisplayListCanvas;
John Reck113e0822014-03-18 09:22:59 -070049class OpenGLRenderer;
50class Rect;
51class Layer;
52class SkiaShader;
53
54class ClipRectOp;
55class SaveLayerOp;
56class SaveOp;
57class RestoreToCountOp;
Chris Craika7090e02014-06-20 16:01:00 -070058class DrawRenderNodeOp;
Tom Hudson2dc236b2014-10-15 15:46:42 -040059class TreeInfo;
John Reck113e0822014-03-18 09:22:59 -070060
John Recke248bd12015-08-05 13:53:53 -070061namespace proto {
62class RenderNode;
63}
64
John Reck113e0822014-03-18 09:22:59 -070065/**
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
Chris Craikdb663fe2015-04-20 13:34:45 -070069 * functionality is split between DisplayListCanvas (which manages the recording), DisplayListData
John Reck113e0822014-03-18 09:22:59 -070070 * (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 {
Chris Craikb565df12015-10-05 13:00:52 -070078friend class TestUtils; // allow TestUtils to access syncDisplayList / syncProperties
John Reck113e0822014-03-18 09:22:59 -070079public:
John Reckff941dc2014-05-14 16:34:14 -070080 enum DirtyPropertyMask {
81 GENERIC = 1 << 1,
82 TRANSLATION_X = 1 << 2,
83 TRANSLATION_Y = 1 << 3,
84 TRANSLATION_Z = 1 << 4,
85 SCALE_X = 1 << 5,
86 SCALE_Y = 1 << 6,
87 ROTATION = 1 << 7,
88 ROTATION_X = 1 << 8,
89 ROTATION_Y = 1 << 9,
90 X = 1 << 10,
91 Y = 1 << 11,
92 Z = 1 << 12,
93 ALPHA = 1 << 13,
John Recka7c2ea22014-08-08 13:21:00 -070094 DISPLAY_LIST = 1 << 14,
John Reckff941dc2014-05-14 16:34:14 -070095 };
96
John Reck113e0822014-03-18 09:22:59 -070097 ANDROID_API RenderNode();
John Recke45b1fd2014-04-15 09:50:16 -070098 ANDROID_API virtual ~RenderNode();
John Reck113e0822014-03-18 09:22:59 -070099
100 // See flags defined in DisplayList.java
101 enum ReplayFlag {
102 kReplayFlag_ClipChildren = 0x1
103 };
104
John Reck443a7142014-09-04 17:40:05 -0700105 void debugDumpLayers(const char* prefix);
John Reck113e0822014-03-18 09:22:59 -0700106
John Reck8de65a82014-04-09 15:23:38 -0700107 ANDROID_API void setStagingDisplayList(DisplayListData* newData);
John Reck113e0822014-03-18 09:22:59 -0700108
109 void computeOrdering();
Chris Craikb265e2c2014-03-27 15:50:09 -0700110
Chris Craik80d49022014-06-20 15:03:43 -0700111 void defer(DeferStateStruct& deferStruct, const int level);
112 void replay(ReplayStateStruct& replayStruct, const int level);
John Reck113e0822014-03-18 09:22:59 -0700113
114 ANDROID_API void output(uint32_t level = 1);
John Reckfe5e7b72014-05-23 17:42:28 -0700115 ANDROID_API int getDebugSize();
John Recke248bd12015-08-05 13:53:53 -0700116 void copyTo(proto::RenderNode* node);
John Reck113e0822014-03-18 09:22:59 -0700117
118 bool isRenderable() const {
Chris Craik8afd0f22014-08-21 17:41:57 -0700119 return mDisplayListData && !mDisplayListData->isEmpty();
John Reck113e0822014-03-18 09:22:59 -0700120 }
121
John Recka447d292014-06-11 18:39:44 -0700122 bool hasProjectionReceiver() const {
123 return mDisplayListData && mDisplayListData->projectionReceiveIndex >= 0;
124 }
125
Chris Craikdefb7f32014-04-08 18:17:07 -0700126 const char* getName() const {
127 return mName.string();
128 }
129
John Reck113e0822014-03-18 09:22:59 -0700130 void setName(const char* name) {
131 if (name) {
132 char* lastPeriod = strrchr(name, '.');
133 if (lastPeriod) {
134 mName.setTo(lastPeriod + 1);
135 } else {
136 mName.setTo(name);
137 }
138 }
139 }
140
John Reckff941dc2014-05-14 16:34:14 -0700141 bool isPropertyFieldDirty(DirtyPropertyMask field) const {
142 return mDirtyPropertyFields & field;
143 }
144
145 void setPropertyFieldsDirty(uint32_t fields) {
146 mDirtyPropertyFields |= fields;
147 }
148
John Recke4267ea2014-06-03 15:53:15 -0700149 const RenderProperties& properties() const {
John Reck113e0822014-03-18 09:22:59 -0700150 return mProperties;
151 }
152
John Reck52244ff2014-05-01 21:27:37 -0700153 RenderProperties& animatorProperties() {
154 return mProperties;
155 }
156
John Reckd0a0b2a2014-03-20 16:28:56 -0700157 const RenderProperties& stagingProperties() {
158 return mStagingProperties;
159 }
160
161 RenderProperties& mutateStagingProperties() {
John Reckd0a0b2a2014-03-20 16:28:56 -0700162 return mStagingProperties;
163 }
164
John Reck113e0822014-03-18 09:22:59 -0700165 int getWidth() {
166 return properties().getWidth();
167 }
168
169 int getHeight() {
170 return properties().getHeight();
171 }
172
John Recke45b1fd2014-04-15 09:50:16 -0700173 ANDROID_API virtual void prepareTree(TreeInfo& info);
John Reckdcba6722014-07-08 13:59:49 -0700174 void destroyHardwareResources();
John Recke45b1fd2014-04-15 09:50:16 -0700175
176 // UI thread only!
John Reck68bfe0a2014-06-24 15:34:58 -0700177 ANDROID_API void addAnimator(const sp<BaseRenderNodeAnimator>& animator);
John Reck668f0e32014-03-26 15:10:40 -0700178
John Reck119907c2014-08-14 09:02:01 -0700179 AnimatorManager& animators() { return mAnimatorManager; }
180
Chris Craikb565df12015-10-05 13:00:52 -0700181 // Returns false if the properties dictate the subtree contained in this RenderNode won't render
182 bool applyViewProperties(CanvasState& canvasState) const;
183
Chris Craik69e5adf2014-08-14 13:34:01 -0700184 void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false) const;
185
Chris Craikb565df12015-10-05 13:00:52 -0700186 bool nothingToDraw() const {
187 const Outline& outline = properties().getOutline();
188 return mDisplayListData == nullptr
189 || properties().getAlpha() <= 0
190 || (outline.getShouldClip() && outline.isEmpty())
191 || properties().getScaleX() == 0
192 || properties().getScaleY() == 0;
193 }
194
195 // Only call if RenderNode has DisplayListData...
196 const DisplayListData& getDisplayListData() const {
197 return *mDisplayListData;
198 }
199
John Reck113e0822014-03-18 09:22:59 -0700200private:
Chris Craika7090e02014-06-20 16:01:00 -0700201 typedef key_value_pair_t<float, DrawRenderNodeOp*> ZDrawRenderNodeOpPair;
John Reck113e0822014-03-18 09:22:59 -0700202
John Reck272a6852015-07-29 16:48:58 -0700203 static size_t findNonNegativeIndex(const std::vector<ZDrawRenderNodeOpPair>& nodes) {
John Reck113e0822014-03-18 09:22:59 -0700204 for (size_t i = 0; i < nodes.size(); i++) {
205 if (nodes[i].key >= 0.0f) return i;
206 }
207 return nodes.size();
208 }
209
Chris Craikb9ce116d2015-08-20 15:14:06 -0700210 enum class ChildrenSelectMode {
211 NegativeZChildren,
212 PositiveZChildren
John Reck113e0822014-03-18 09:22:59 -0700213 };
214
Chris Craika7090e02014-06-20 16:01:00 -0700215 void computeOrderingImpl(DrawRenderNodeOp* opState,
John Reck272a6852015-07-29 16:48:58 -0700216 std::vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700217 const mat4* transformFromProjectionSurface);
218
219 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700220 inline void setViewProperties(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700221
Chris Craik8afd0f22014-08-21 17:41:57 -0700222 void buildZSortedChildList(const DisplayListData::Chunk& chunk,
John Reck272a6852015-07-29 16:48:58 -0700223 std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes);
John Reck113e0822014-03-18 09:22:59 -0700224
Chris Craikb265e2c2014-03-27 15:50:09 -0700225 template<class T>
226 inline void issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler);
227
John Reck113e0822014-03-18 09:22:59 -0700228 template <class T>
Chris Craikc3e75f92014-08-27 15:34:52 -0700229 inline void issueOperationsOf3dChildren(ChildrenSelectMode mode,
John Reck272a6852015-07-29 16:48:58 -0700230 const Matrix4& initialTransform, const std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craik80d49022014-06-20 15:03:43 -0700231 OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700232
233 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700234 inline void issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700235
Chris Craikb265e2c2014-03-27 15:50:09 -0700236 /**
237 * Issue the RenderNode's operations into a handler, recursing for subtrees through
Chris Craika7090e02014-06-20 16:01:00 -0700238 * DrawRenderNodeOp's defer() or replay() methods
Chris Craikb265e2c2014-03-27 15:50:09 -0700239 */
John Reck113e0822014-03-18 09:22:59 -0700240 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700241 inline void issueOperations(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700242
243 class TextContainer {
244 public:
245 size_t length() const {
246 return mByteLength;
247 }
248
249 const char* text() const {
250 return (const char*) mText;
251 }
252
253 size_t mByteLength;
254 const char* mText;
255 };
256
Chris Craikb565df12015-10-05 13:00:52 -0700257
258 void syncProperties();
259 void syncDisplayList();
260
Chris Craika766cb22015-06-08 16:49:43 -0700261 void prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer);
John Reck25fbb3f2014-06-12 13:46:45 -0700262 void pushStagingPropertiesChanges(TreeInfo& info);
263 void pushStagingDisplayListChanges(TreeInfo& info);
Chris Craika766cb22015-06-08 16:49:43 -0700264 void prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayListData* subtree);
John Reck25fbb3f2014-06-12 13:46:45 -0700265 void applyLayerPropertiesToLayer(TreeInfo& info);
John Recka7c2ea22014-08-08 13:21:00 -0700266 void prepareLayer(TreeInfo& info, uint32_t dirtyMask);
John Reck25fbb3f2014-06-12 13:46:45 -0700267 void pushLayerUpdate(TreeInfo& info);
John Reckdcba6722014-07-08 13:59:49 -0700268 void deleteDisplayListData();
John Reck0a973302014-07-16 13:29:45 -0700269 void damageSelf(TreeInfo& info);
John Reckdcba6722014-07-08 13:59:49 -0700270
271 void incParentRefCount() { mParentCount++; }
272 void decParentRefCount();
John Reck8de65a82014-04-09 15:23:38 -0700273
John Reck113e0822014-03-18 09:22:59 -0700274 String8 mName;
John Reck113e0822014-03-18 09:22:59 -0700275
John Reckff941dc2014-05-14 16:34:14 -0700276 uint32_t mDirtyPropertyFields;
John Reck113e0822014-03-18 09:22:59 -0700277 RenderProperties mProperties;
John Reckd0a0b2a2014-03-20 16:28:56 -0700278 RenderProperties mStagingProperties;
279
John Reck8de65a82014-04-09 15:23:38 -0700280 bool mNeedsDisplayListDataSync;
John Reckdcba6722014-07-08 13:59:49 -0700281 // WARNING: Do not delete this directly, you must go through deleteDisplayListData()!
John Reck113e0822014-03-18 09:22:59 -0700282 DisplayListData* mDisplayListData;
John Reck8de65a82014-04-09 15:23:38 -0700283 DisplayListData* mStagingDisplayListData;
John Reck113e0822014-03-18 09:22:59 -0700284
John Reck68bfe0a2014-06-24 15:34:58 -0700285 friend class AnimatorManager;
286 AnimatorManager mAnimatorManager;
John Recke45b1fd2014-04-15 09:50:16 -0700287
John Reck25fbb3f2014-06-12 13:46:45 -0700288 // Owned by RT. Lifecycle is managed by prepareTree(), with the exception
289 // being in ~RenderNode() which may happen on any thread.
290 Layer* mLayer;
291
John Reck113e0822014-03-18 09:22:59 -0700292 /**
293 * Draw time state - these properties are only set and used during rendering
294 */
295
296 // for projection surfaces, contains a list of all children items
John Reck272a6852015-07-29 16:48:58 -0700297 std::vector<DrawRenderNodeOp*> mProjectedNodes;
John Reckdcba6722014-07-08 13:59:49 -0700298
299 // How many references our parent(s) have to us. Typically this should alternate
300 // between 2 and 1 (when a staging push happens we inc first then dec)
301 // When this hits 0 we are no longer in the tree, so any hardware resources
302 // (specifically Layers) should be released.
303 // This is *NOT* thread-safe, and should therefore only be tracking
304 // mDisplayListData, not mStagingDisplayListData.
305 uint32_t mParentCount;
John Reck113e0822014-03-18 09:22:59 -0700306}; // class RenderNode
307
308} /* namespace uirenderer */
309} /* namespace android */
310
311#endif /* RENDERNODE_H */