blob: 3500cb200a5132822167eb825be3cf3341e4f706 [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;
Chris Craikdb663fe2015-04-20 13:34:45 -070047class DisplayListCanvas;
Chris Craik0b7e8242015-10-28 16:50:44 -070048class DisplayListOp;
John Reck113e0822014-03-18 09:22:59 -070049class OpenGLRenderer;
Chris Craik0b7e8242015-10-28 16:50:44 -070050class OpReorderer;
John Reck113e0822014-03-18 09:22:59 -070051class Rect;
John Reck113e0822014-03-18 09:22:59 -070052class SkiaShader;
53
Chris Craik0b7e8242015-10-28 16:50:44 -070054
55#if HWUI_NEW_OPS
56class OffscreenBuffer;
57typedef OffscreenBuffer layer_t;
58#else
59class Layer;
60typedef Layer layer_t;
61#endif
62
John Reck113e0822014-03-18 09:22:59 -070063class ClipRectOp;
64class SaveLayerOp;
65class SaveOp;
66class RestoreToCountOp;
Chris Craika7090e02014-06-20 16:01:00 -070067class DrawRenderNodeOp;
Tom Hudson2dc236b2014-10-15 15:46:42 -040068class TreeInfo;
John Reck113e0822014-03-18 09:22:59 -070069
John Recke248bd12015-08-05 13:53:53 -070070namespace proto {
71class RenderNode;
72}
73
John Reck113e0822014-03-18 09:22:59 -070074/**
75 * Primary class for storing recorded canvas commands, as well as per-View/ViewGroup display properties.
76 *
77 * Recording of canvas commands is somewhat similar to SkPicture, except the canvas-recording
Chris Craik003cc3d2015-10-16 10:24:55 -070078 * functionality is split between DisplayListCanvas (which manages the recording), DisplayList
John Reck113e0822014-03-18 09:22:59 -070079 * (which holds the actual data), and DisplayList (which holds properties and performs playback onto
80 * a renderer).
81 *
Chris Craik003cc3d2015-10-16 10:24:55 -070082 * Note that DisplayList is swapped out from beneath an individual RenderNode when a view's
83 * recorded stream of canvas operations is refreshed. The RenderNode (and its properties) stay
John Reck113e0822014-03-18 09:22:59 -070084 * attached.
85 */
John Reck087bc0c2014-04-04 16:20:08 -070086class RenderNode : public VirtualLightRefBase {
Chris Craikb565df12015-10-05 13:00:52 -070087friend class TestUtils; // allow TestUtils to access syncDisplayList / syncProperties
John Reck113e0822014-03-18 09:22:59 -070088public:
John Reckff941dc2014-05-14 16:34:14 -070089 enum DirtyPropertyMask {
90 GENERIC = 1 << 1,
91 TRANSLATION_X = 1 << 2,
92 TRANSLATION_Y = 1 << 3,
93 TRANSLATION_Z = 1 << 4,
94 SCALE_X = 1 << 5,
95 SCALE_Y = 1 << 6,
96 ROTATION = 1 << 7,
97 ROTATION_X = 1 << 8,
98 ROTATION_Y = 1 << 9,
99 X = 1 << 10,
100 Y = 1 << 11,
101 Z = 1 << 12,
102 ALPHA = 1 << 13,
John Recka7c2ea22014-08-08 13:21:00 -0700103 DISPLAY_LIST = 1 << 14,
John Reckff941dc2014-05-14 16:34:14 -0700104 };
105
John Reck113e0822014-03-18 09:22:59 -0700106 ANDROID_API RenderNode();
John Recke45b1fd2014-04-15 09:50:16 -0700107 ANDROID_API virtual ~RenderNode();
John Reck113e0822014-03-18 09:22:59 -0700108
109 // See flags defined in DisplayList.java
110 enum ReplayFlag {
111 kReplayFlag_ClipChildren = 0x1
112 };
113
John Reck443a7142014-09-04 17:40:05 -0700114 void debugDumpLayers(const char* prefix);
John Reck113e0822014-03-18 09:22:59 -0700115
Chris Craik003cc3d2015-10-16 10:24:55 -0700116 ANDROID_API void setStagingDisplayList(DisplayList* newData);
John Reck113e0822014-03-18 09:22:59 -0700117
118 void computeOrdering();
Chris Craikb265e2c2014-03-27 15:50:09 -0700119
Chris Craik80d49022014-06-20 15:03:43 -0700120 void defer(DeferStateStruct& deferStruct, const int level);
121 void replay(ReplayStateStruct& replayStruct, const int level);
John Reck113e0822014-03-18 09:22:59 -0700122
123 ANDROID_API void output(uint32_t level = 1);
John Reckfe5e7b72014-05-23 17:42:28 -0700124 ANDROID_API int getDebugSize();
John Recke248bd12015-08-05 13:53:53 -0700125 void copyTo(proto::RenderNode* node);
John Reck113e0822014-03-18 09:22:59 -0700126
127 bool isRenderable() const {
Chris Craik003cc3d2015-10-16 10:24:55 -0700128 return mDisplayList && !mDisplayList->isEmpty();
John Reck113e0822014-03-18 09:22:59 -0700129 }
130
John Recka447d292014-06-11 18:39:44 -0700131 bool hasProjectionReceiver() const {
Chris Craik003cc3d2015-10-16 10:24:55 -0700132 return mDisplayList && mDisplayList->projectionReceiveIndex >= 0;
John Recka447d292014-06-11 18:39:44 -0700133 }
134
Chris Craikdefb7f32014-04-08 18:17:07 -0700135 const char* getName() const {
136 return mName.string();
137 }
138
John Reck113e0822014-03-18 09:22:59 -0700139 void setName(const char* name) {
140 if (name) {
141 char* lastPeriod = strrchr(name, '.');
142 if (lastPeriod) {
143 mName.setTo(lastPeriod + 1);
144 } else {
145 mName.setTo(name);
146 }
147 }
148 }
149
John Reckff941dc2014-05-14 16:34:14 -0700150 bool isPropertyFieldDirty(DirtyPropertyMask field) const {
151 return mDirtyPropertyFields & field;
152 }
153
154 void setPropertyFieldsDirty(uint32_t fields) {
155 mDirtyPropertyFields |= fields;
156 }
157
John Recke4267ea2014-06-03 15:53:15 -0700158 const RenderProperties& properties() const {
John Reck113e0822014-03-18 09:22:59 -0700159 return mProperties;
160 }
161
John Reck52244ff2014-05-01 21:27:37 -0700162 RenderProperties& animatorProperties() {
163 return mProperties;
164 }
165
John Reckd0a0b2a2014-03-20 16:28:56 -0700166 const RenderProperties& stagingProperties() {
167 return mStagingProperties;
168 }
169
170 RenderProperties& mutateStagingProperties() {
John Reckd0a0b2a2014-03-20 16:28:56 -0700171 return mStagingProperties;
172 }
173
Chris Craik0b7e8242015-10-28 16:50:44 -0700174 uint32_t getWidth() {
John Reck113e0822014-03-18 09:22:59 -0700175 return properties().getWidth();
176 }
177
Chris Craik0b7e8242015-10-28 16:50:44 -0700178 uint32_t getHeight() {
John Reck113e0822014-03-18 09:22:59 -0700179 return properties().getHeight();
180 }
181
John Recke45b1fd2014-04-15 09:50:16 -0700182 ANDROID_API virtual void prepareTree(TreeInfo& info);
John Reckdcba6722014-07-08 13:59:49 -0700183 void destroyHardwareResources();
John Recke45b1fd2014-04-15 09:50:16 -0700184
185 // UI thread only!
John Reck68bfe0a2014-06-24 15:34:58 -0700186 ANDROID_API void addAnimator(const sp<BaseRenderNodeAnimator>& animator);
John Reck668f0e32014-03-26 15:10:40 -0700187
John Reck119907c2014-08-14 09:02:01 -0700188 AnimatorManager& animators() { return mAnimatorManager; }
189
Chris Craikb565df12015-10-05 13:00:52 -0700190 // Returns false if the properties dictate the subtree contained in this RenderNode won't render
191 bool applyViewProperties(CanvasState& canvasState) const;
192
Chris Craik69e5adf2014-08-14 13:34:01 -0700193 void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false) const;
194
Chris Craikb565df12015-10-05 13:00:52 -0700195 bool nothingToDraw() const {
196 const Outline& outline = properties().getOutline();
Chris Craik003cc3d2015-10-16 10:24:55 -0700197 return mDisplayList == nullptr
Chris Craikb565df12015-10-05 13:00:52 -0700198 || properties().getAlpha() <= 0
199 || (outline.getShouldClip() && outline.isEmpty())
200 || properties().getScaleX() == 0
201 || properties().getScaleY() == 0;
202 }
203
Chris Craik003cc3d2015-10-16 10:24:55 -0700204 // Only call if RenderNode has DisplayList...
Chris Craik0b7e8242015-10-28 16:50:44 -0700205 const DisplayList* getDisplayList() const {
206 return mDisplayList;
Chris Craikb565df12015-10-05 13:00:52 -0700207 }
Chris Craik0b7e8242015-10-28 16:50:44 -0700208#if HWUI_NEW_OPS
209 OffscreenBuffer* getLayer() const { return mLayer; }
210 OffscreenBuffer** getLayerHandle() { return &mLayer; } // ugh...
211#endif
Chris Craikb565df12015-10-05 13:00:52 -0700212
John Reck113e0822014-03-18 09:22:59 -0700213private:
Chris Craika7090e02014-06-20 16:01:00 -0700214 typedef key_value_pair_t<float, DrawRenderNodeOp*> ZDrawRenderNodeOpPair;
John Reck113e0822014-03-18 09:22:59 -0700215
John Reck272a6852015-07-29 16:48:58 -0700216 static size_t findNonNegativeIndex(const std::vector<ZDrawRenderNodeOpPair>& nodes) {
John Reck113e0822014-03-18 09:22:59 -0700217 for (size_t i = 0; i < nodes.size(); i++) {
218 if (nodes[i].key >= 0.0f) return i;
219 }
220 return nodes.size();
221 }
222
Chris Craikb9ce116d2015-08-20 15:14:06 -0700223 enum class ChildrenSelectMode {
224 NegativeZChildren,
225 PositiveZChildren
John Reck113e0822014-03-18 09:22:59 -0700226 };
227
Chris Craika7090e02014-06-20 16:01:00 -0700228 void computeOrderingImpl(DrawRenderNodeOp* opState,
John Reck272a6852015-07-29 16:48:58 -0700229 std::vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700230 const mat4* transformFromProjectionSurface);
231
232 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700233 inline void setViewProperties(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700234
Chris Craik003cc3d2015-10-16 10:24:55 -0700235 void buildZSortedChildList(const DisplayList::Chunk& chunk,
John Reck272a6852015-07-29 16:48:58 -0700236 std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes);
John Reck113e0822014-03-18 09:22:59 -0700237
Chris Craikb265e2c2014-03-27 15:50:09 -0700238 template<class T>
239 inline void issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler);
240
John Reck113e0822014-03-18 09:22:59 -0700241 template <class T>
Chris Craikc3e75f92014-08-27 15:34:52 -0700242 inline void issueOperationsOf3dChildren(ChildrenSelectMode mode,
John Reck272a6852015-07-29 16:48:58 -0700243 const Matrix4& initialTransform, const std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craik80d49022014-06-20 15:03:43 -0700244 OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700245
246 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700247 inline void issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700248
Chris Craikb265e2c2014-03-27 15:50:09 -0700249 /**
250 * Issue the RenderNode's operations into a handler, recursing for subtrees through
Chris Craika7090e02014-06-20 16:01:00 -0700251 * DrawRenderNodeOp's defer() or replay() methods
Chris Craikb265e2c2014-03-27 15:50:09 -0700252 */
John Reck113e0822014-03-18 09:22:59 -0700253 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700254 inline void issueOperations(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700255
256 class TextContainer {
257 public:
258 size_t length() const {
259 return mByteLength;
260 }
261
262 const char* text() const {
263 return (const char*) mText;
264 }
265
266 size_t mByteLength;
267 const char* mText;
268 };
269
Chris Craikb565df12015-10-05 13:00:52 -0700270
271 void syncProperties();
272 void syncDisplayList();
273
Chris Craika766cb22015-06-08 16:49:43 -0700274 void prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer);
John Reck25fbb3f2014-06-12 13:46:45 -0700275 void pushStagingPropertiesChanges(TreeInfo& info);
276 void pushStagingDisplayListChanges(TreeInfo& info);
Chris Craik003cc3d2015-10-16 10:24:55 -0700277 void prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayList* subtree);
Chris Craik0b7e8242015-10-28 16:50:44 -0700278#if !HWUI_NEW_OPS
John Reck25fbb3f2014-06-12 13:46:45 -0700279 void applyLayerPropertiesToLayer(TreeInfo& info);
Chris Craik0b7e8242015-10-28 16:50:44 -0700280#endif
John Recka7c2ea22014-08-08 13:21:00 -0700281 void prepareLayer(TreeInfo& info, uint32_t dirtyMask);
John Reck25fbb3f2014-06-12 13:46:45 -0700282 void pushLayerUpdate(TreeInfo& info);
Chris Craik003cc3d2015-10-16 10:24:55 -0700283 void deleteDisplayList();
John Reck0a973302014-07-16 13:29:45 -0700284 void damageSelf(TreeInfo& info);
John Reckdcba6722014-07-08 13:59:49 -0700285
286 void incParentRefCount() { mParentCount++; }
287 void decParentRefCount();
John Reck8de65a82014-04-09 15:23:38 -0700288
John Reck113e0822014-03-18 09:22:59 -0700289 String8 mName;
John Reck113e0822014-03-18 09:22:59 -0700290
John Reckff941dc2014-05-14 16:34:14 -0700291 uint32_t mDirtyPropertyFields;
John Reck113e0822014-03-18 09:22:59 -0700292 RenderProperties mProperties;
John Reckd0a0b2a2014-03-20 16:28:56 -0700293 RenderProperties mStagingProperties;
294
Chris Craik003cc3d2015-10-16 10:24:55 -0700295 bool mNeedsDisplayListSync;
296 // WARNING: Do not delete this directly, you must go through deleteDisplayList()!
297 DisplayList* mDisplayList;
298 DisplayList* mStagingDisplayList;
John Reck113e0822014-03-18 09:22:59 -0700299
John Reck68bfe0a2014-06-24 15:34:58 -0700300 friend class AnimatorManager;
301 AnimatorManager mAnimatorManager;
John Recke45b1fd2014-04-15 09:50:16 -0700302
John Reck25fbb3f2014-06-12 13:46:45 -0700303 // Owned by RT. Lifecycle is managed by prepareTree(), with the exception
304 // being in ~RenderNode() which may happen on any thread.
Chris Craik0b7e8242015-10-28 16:50:44 -0700305 layer_t* mLayer = nullptr;
John Reck25fbb3f2014-06-12 13:46:45 -0700306
John Reck113e0822014-03-18 09:22:59 -0700307 /**
308 * Draw time state - these properties are only set and used during rendering
309 */
310
311 // for projection surfaces, contains a list of all children items
John Reck272a6852015-07-29 16:48:58 -0700312 std::vector<DrawRenderNodeOp*> mProjectedNodes;
John Reckdcba6722014-07-08 13:59:49 -0700313
314 // How many references our parent(s) have to us. Typically this should alternate
315 // between 2 and 1 (when a staging push happens we inc first then dec)
316 // When this hits 0 we are no longer in the tree, so any hardware resources
317 // (specifically Layers) should be released.
318 // This is *NOT* thread-safe, and should therefore only be tracking
Chris Craik003cc3d2015-10-16 10:24:55 -0700319 // mDisplayList, not mStagingDisplayList.
John Reckdcba6722014-07-08 13:59:49 -0700320 uint32_t mParentCount;
John Reck113e0822014-03-18 09:22:59 -0700321}; // class RenderNode
322
323} /* namespace uirenderer */
324} /* namespace android */
325
326#endif /* RENDERNODE_H */