blob: f248de54acba5a04e6af6144419daee41e7d4ac1 [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;
50class Rect;
John Reck113e0822014-03-18 09:22:59 -070051class SkiaShader;
52
Chris Craik0b7e8242015-10-28 16:50:44 -070053#if HWUI_NEW_OPS
Chris Craikf158b492016-01-12 14:45:08 -080054class FrameBuilder;
Chris Craik0b7e8242015-10-28 16:50:44 -070055class OffscreenBuffer;
Chris Craik8d1f2122015-11-24 16:40:09 -080056struct RenderNodeOp;
Chris Craik0b7e8242015-10-28 16:50:44 -070057typedef OffscreenBuffer layer_t;
Chris Craik8d1f2122015-11-24 16:40:09 -080058typedef RenderNodeOp renderNodeOp_t;
Chris Craik0b7e8242015-10-28 16:50:44 -070059#else
60class Layer;
61typedef Layer layer_t;
Chris Craik8d1f2122015-11-24 16:40:09 -080062typedef DrawRenderNodeOp renderNodeOp_t;
Chris Craik0b7e8242015-10-28 16:50:44 -070063#endif
64
John Reck113e0822014-03-18 09:22:59 -070065class ClipRectOp;
Chris Craik8d1f2122015-11-24 16:40:09 -080066class DrawRenderNodeOp;
John Reck113e0822014-03-18 09:22:59 -070067class SaveLayerOp;
68class SaveOp;
69class RestoreToCountOp;
Tom Hudson2dc236b2014-10-15 15:46:42 -040070class TreeInfo;
John Reck113e0822014-03-18 09:22:59 -070071
John Recke248bd12015-08-05 13:53:53 -070072namespace proto {
73class RenderNode;
74}
75
John Reck113e0822014-03-18 09:22:59 -070076/**
77 * Primary class for storing recorded canvas commands, as well as per-View/ViewGroup display properties.
78 *
79 * Recording of canvas commands is somewhat similar to SkPicture, except the canvas-recording
Chris Craik003cc3d2015-10-16 10:24:55 -070080 * functionality is split between DisplayListCanvas (which manages the recording), DisplayList
John Reck113e0822014-03-18 09:22:59 -070081 * (which holds the actual data), and DisplayList (which holds properties and performs playback onto
82 * a renderer).
83 *
Chris Craik003cc3d2015-10-16 10:24:55 -070084 * Note that DisplayList is swapped out from beneath an individual RenderNode when a view's
85 * recorded stream of canvas operations is refreshed. The RenderNode (and its properties) stay
John Reck113e0822014-03-18 09:22:59 -070086 * attached.
87 */
John Reck087bc0c2014-04-04 16:20:08 -070088class RenderNode : public VirtualLightRefBase {
Chris Craikb565df12015-10-05 13:00:52 -070089friend class TestUtils; // allow TestUtils to access syncDisplayList / syncProperties
Chris Craikf158b492016-01-12 14:45:08 -080090friend class FrameBuilder;
John Reck113e0822014-03-18 09:22:59 -070091public:
John Reckff941dc2014-05-14 16:34:14 -070092 enum DirtyPropertyMask {
93 GENERIC = 1 << 1,
94 TRANSLATION_X = 1 << 2,
95 TRANSLATION_Y = 1 << 3,
96 TRANSLATION_Z = 1 << 4,
97 SCALE_X = 1 << 5,
98 SCALE_Y = 1 << 6,
99 ROTATION = 1 << 7,
100 ROTATION_X = 1 << 8,
101 ROTATION_Y = 1 << 9,
102 X = 1 << 10,
103 Y = 1 << 11,
104 Z = 1 << 12,
105 ALPHA = 1 << 13,
John Recka7c2ea22014-08-08 13:21:00 -0700106 DISPLAY_LIST = 1 << 14,
John Reckff941dc2014-05-14 16:34:14 -0700107 };
108
John Reck113e0822014-03-18 09:22:59 -0700109 ANDROID_API RenderNode();
John Recke45b1fd2014-04-15 09:50:16 -0700110 ANDROID_API virtual ~RenderNode();
John Reck113e0822014-03-18 09:22:59 -0700111
112 // See flags defined in DisplayList.java
113 enum ReplayFlag {
114 kReplayFlag_ClipChildren = 0x1
115 };
116
John Reck443a7142014-09-04 17:40:05 -0700117 void debugDumpLayers(const char* prefix);
John Reck113e0822014-03-18 09:22:59 -0700118
Chris Craik003cc3d2015-10-16 10:24:55 -0700119 ANDROID_API void setStagingDisplayList(DisplayList* newData);
John Reck113e0822014-03-18 09:22:59 -0700120
121 void computeOrdering();
Chris Craikb265e2c2014-03-27 15:50:09 -0700122
Chris Craik80d49022014-06-20 15:03:43 -0700123 void defer(DeferStateStruct& deferStruct, const int level);
124 void replay(ReplayStateStruct& replayStruct, const int level);
John Reck113e0822014-03-18 09:22:59 -0700125
126 ANDROID_API void output(uint32_t level = 1);
John Reckfe5e7b72014-05-23 17:42:28 -0700127 ANDROID_API int getDebugSize();
John Recke248bd12015-08-05 13:53:53 -0700128 void copyTo(proto::RenderNode* node);
John Reck113e0822014-03-18 09:22:59 -0700129
130 bool isRenderable() const {
Chris Craik003cc3d2015-10-16 10:24:55 -0700131 return mDisplayList && !mDisplayList->isEmpty();
John Reck113e0822014-03-18 09:22:59 -0700132 }
133
John Recka447d292014-06-11 18:39:44 -0700134 bool hasProjectionReceiver() const {
Chris Craik003cc3d2015-10-16 10:24:55 -0700135 return mDisplayList && mDisplayList->projectionReceiveIndex >= 0;
John Recka447d292014-06-11 18:39:44 -0700136 }
137
Chris Craikdefb7f32014-04-08 18:17:07 -0700138 const char* getName() const {
139 return mName.string();
140 }
141
John Reck113e0822014-03-18 09:22:59 -0700142 void setName(const char* name) {
143 if (name) {
144 char* lastPeriod = strrchr(name, '.');
145 if (lastPeriod) {
146 mName.setTo(lastPeriod + 1);
147 } else {
148 mName.setTo(name);
149 }
150 }
151 }
152
John Reckff941dc2014-05-14 16:34:14 -0700153 bool isPropertyFieldDirty(DirtyPropertyMask field) const {
154 return mDirtyPropertyFields & field;
155 }
156
157 void setPropertyFieldsDirty(uint32_t fields) {
158 mDirtyPropertyFields |= fields;
159 }
160
John Recke4267ea2014-06-03 15:53:15 -0700161 const RenderProperties& properties() const {
John Reck113e0822014-03-18 09:22:59 -0700162 return mProperties;
163 }
164
John Reck52244ff2014-05-01 21:27:37 -0700165 RenderProperties& animatorProperties() {
166 return mProperties;
167 }
168
John Reckd0a0b2a2014-03-20 16:28:56 -0700169 const RenderProperties& stagingProperties() {
170 return mStagingProperties;
171 }
172
173 RenderProperties& mutateStagingProperties() {
John Reckd0a0b2a2014-03-20 16:28:56 -0700174 return mStagingProperties;
175 }
176
Chris Craik76caecf2015-11-02 19:17:45 -0800177 int getWidth() const {
John Reck113e0822014-03-18 09:22:59 -0700178 return properties().getWidth();
179 }
180
Chris Craik76caecf2015-11-02 19:17:45 -0800181 int getHeight() const {
John Reck113e0822014-03-18 09:22:59 -0700182 return properties().getHeight();
183 }
184
John Recke45b1fd2014-04-15 09:50:16 -0700185 ANDROID_API virtual void prepareTree(TreeInfo& info);
John Reckdcba6722014-07-08 13:59:49 -0700186 void destroyHardwareResources();
John Recke45b1fd2014-04-15 09:50:16 -0700187
188 // UI thread only!
John Reck68bfe0a2014-06-24 15:34:58 -0700189 ANDROID_API void addAnimator(const sp<BaseRenderNodeAnimator>& animator);
John Reck668f0e32014-03-26 15:10:40 -0700190
John Reck119907c2014-08-14 09:02:01 -0700191 AnimatorManager& animators() { return mAnimatorManager; }
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 Craik0b7e8242015-10-28 16:50:44 -0700204 const DisplayList* getDisplayList() const {
205 return mDisplayList;
Chris Craikb565df12015-10-05 13:00:52 -0700206 }
Chris Craik0b7e8242015-10-28 16:50:44 -0700207#if HWUI_NEW_OPS
208 OffscreenBuffer* getLayer() const { return mLayer; }
209 OffscreenBuffer** getLayerHandle() { return &mLayer; } // ugh...
210#endif
Chris Craikb565df12015-10-05 13:00:52 -0700211
John Reckf6481082016-02-02 15:18:23 -0800212 class ANDROID_API PositionListener {
213 public:
214 virtual ~PositionListener() {}
215 virtual void onPositionUpdated(RenderNode& node, const TreeInfo& info) = 0;
216 };
217
218 // Note this is not thread safe, this needs to be called
219 // before the RenderNode is used for drawing.
220 // RenderNode takes ownership of the pointer
221 ANDROID_API void setPositionListener(PositionListener* listener) {
222 mPositionListener.reset(listener);
223 }
224
John Reck113e0822014-03-18 09:22:59 -0700225private:
Chris Craika7090e02014-06-20 16:01:00 -0700226 typedef key_value_pair_t<float, DrawRenderNodeOp*> ZDrawRenderNodeOpPair;
John Reck113e0822014-03-18 09:22:59 -0700227
John Reck272a6852015-07-29 16:48:58 -0700228 static size_t findNonNegativeIndex(const std::vector<ZDrawRenderNodeOpPair>& nodes) {
John Reck113e0822014-03-18 09:22:59 -0700229 for (size_t i = 0; i < nodes.size(); i++) {
230 if (nodes[i].key >= 0.0f) return i;
231 }
232 return nodes.size();
233 }
234
Chris Craikb9ce116d2015-08-20 15:14:06 -0700235 enum class ChildrenSelectMode {
236 NegativeZChildren,
237 PositiveZChildren
John Reck113e0822014-03-18 09:22:59 -0700238 };
239
Chris Craik8d1f2122015-11-24 16:40:09 -0800240 void computeOrderingImpl(renderNodeOp_t* opState,
241 std::vector<renderNodeOp_t*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700242 const mat4* transformFromProjectionSurface);
243
244 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700245 inline void setViewProperties(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700246
Chris Craik003cc3d2015-10-16 10:24:55 -0700247 void buildZSortedChildList(const DisplayList::Chunk& chunk,
John Reck272a6852015-07-29 16:48:58 -0700248 std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes);
John Reck113e0822014-03-18 09:22:59 -0700249
Chris Craikb265e2c2014-03-27 15:50:09 -0700250 template<class T>
251 inline void issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler);
252
John Reck113e0822014-03-18 09:22:59 -0700253 template <class T>
Chris Craikc3e75f92014-08-27 15:34:52 -0700254 inline void issueOperationsOf3dChildren(ChildrenSelectMode mode,
John Reck272a6852015-07-29 16:48:58 -0700255 const Matrix4& initialTransform, const std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craik80d49022014-06-20 15:03:43 -0700256 OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700257
258 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700259 inline void issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700260
Chris Craikb265e2c2014-03-27 15:50:09 -0700261 /**
262 * Issue the RenderNode's operations into a handler, recursing for subtrees through
Chris Craika7090e02014-06-20 16:01:00 -0700263 * DrawRenderNodeOp's defer() or replay() methods
Chris Craikb265e2c2014-03-27 15:50:09 -0700264 */
John Reck113e0822014-03-18 09:22:59 -0700265 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700266 inline void issueOperations(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700267
268 class TextContainer {
269 public:
270 size_t length() const {
271 return mByteLength;
272 }
273
274 const char* text() const {
275 return (const char*) mText;
276 }
277
278 size_t mByteLength;
279 const char* mText;
280 };
281
Chris Craikb565df12015-10-05 13:00:52 -0700282
283 void syncProperties();
284 void syncDisplayList();
285
Chris Craika766cb22015-06-08 16:49:43 -0700286 void prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer);
John Reck25fbb3f2014-06-12 13:46:45 -0700287 void pushStagingPropertiesChanges(TreeInfo& info);
288 void pushStagingDisplayListChanges(TreeInfo& info);
Chris Craik003cc3d2015-10-16 10:24:55 -0700289 void prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayList* subtree);
Chris Craik0b7e8242015-10-28 16:50:44 -0700290#if !HWUI_NEW_OPS
John Reck25fbb3f2014-06-12 13:46:45 -0700291 void applyLayerPropertiesToLayer(TreeInfo& info);
Chris Craik0b7e8242015-10-28 16:50:44 -0700292#endif
John Recka7c2ea22014-08-08 13:21:00 -0700293 void prepareLayer(TreeInfo& info, uint32_t dirtyMask);
John Reck25fbb3f2014-06-12 13:46:45 -0700294 void pushLayerUpdate(TreeInfo& info);
Chris Craik003cc3d2015-10-16 10:24:55 -0700295 void deleteDisplayList();
John Reck0a973302014-07-16 13:29:45 -0700296 void damageSelf(TreeInfo& info);
John Reckdcba6722014-07-08 13:59:49 -0700297
298 void incParentRefCount() { mParentCount++; }
299 void decParentRefCount();
John Reck8de65a82014-04-09 15:23:38 -0700300
John Reck113e0822014-03-18 09:22:59 -0700301 String8 mName;
John Reck113e0822014-03-18 09:22:59 -0700302
John Reckff941dc2014-05-14 16:34:14 -0700303 uint32_t mDirtyPropertyFields;
John Reck113e0822014-03-18 09:22:59 -0700304 RenderProperties mProperties;
John Reckd0a0b2a2014-03-20 16:28:56 -0700305 RenderProperties mStagingProperties;
306
Chris Craik003cc3d2015-10-16 10:24:55 -0700307 bool mNeedsDisplayListSync;
308 // WARNING: Do not delete this directly, you must go through deleteDisplayList()!
309 DisplayList* mDisplayList;
310 DisplayList* mStagingDisplayList;
John Reck113e0822014-03-18 09:22:59 -0700311
John Reck68bfe0a2014-06-24 15:34:58 -0700312 friend class AnimatorManager;
313 AnimatorManager mAnimatorManager;
John Recke45b1fd2014-04-15 09:50:16 -0700314
John Reck25fbb3f2014-06-12 13:46:45 -0700315 // Owned by RT. Lifecycle is managed by prepareTree(), with the exception
316 // being in ~RenderNode() which may happen on any thread.
Chris Craik0b7e8242015-10-28 16:50:44 -0700317 layer_t* mLayer = nullptr;
John Reck25fbb3f2014-06-12 13:46:45 -0700318
John Reck113e0822014-03-18 09:22:59 -0700319 /**
320 * Draw time state - these properties are only set and used during rendering
321 */
322
323 // for projection surfaces, contains a list of all children items
Chris Craik8d1f2122015-11-24 16:40:09 -0800324 std::vector<renderNodeOp_t*> mProjectedNodes;
John Reckdcba6722014-07-08 13:59:49 -0700325
326 // How many references our parent(s) have to us. Typically this should alternate
327 // between 2 and 1 (when a staging push happens we inc first then dec)
328 // When this hits 0 we are no longer in the tree, so any hardware resources
329 // (specifically Layers) should be released.
330 // This is *NOT* thread-safe, and should therefore only be tracking
Chris Craik003cc3d2015-10-16 10:24:55 -0700331 // mDisplayList, not mStagingDisplayList.
John Reckdcba6722014-07-08 13:59:49 -0700332 uint32_t mParentCount;
John Reckf6481082016-02-02 15:18:23 -0800333
334 std::unique_ptr<PositionListener> mPositionListener;
John Reck113e0822014-03-18 09:22:59 -0700335}; // class RenderNode
336
337} /* namespace uirenderer */
338} /* namespace android */
339
340#endif /* RENDERNODE_H */