blob: 8381925521272b95e68033f9e04898014f23c36d [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
Chris Craik91eff222016-02-22 13:39:33 -0800126#if HWUI_NEW_OPS
127 ANDROID_API void output(uint32_t level = 0, const char* label = "Root");
128#else
John Reck113e0822014-03-18 09:22:59 -0700129 ANDROID_API void output(uint32_t level = 1);
Chris Craik91eff222016-02-22 13:39:33 -0800130#endif
John Reckfe5e7b72014-05-23 17:42:28 -0700131 ANDROID_API int getDebugSize();
John Recke248bd12015-08-05 13:53:53 -0700132 void copyTo(proto::RenderNode* node);
John Reck113e0822014-03-18 09:22:59 -0700133
134 bool isRenderable() const {
Chris Craik003cc3d2015-10-16 10:24:55 -0700135 return mDisplayList && !mDisplayList->isEmpty();
John Reck113e0822014-03-18 09:22:59 -0700136 }
137
John Recka447d292014-06-11 18:39:44 -0700138 bool hasProjectionReceiver() const {
Chris Craik003cc3d2015-10-16 10:24:55 -0700139 return mDisplayList && mDisplayList->projectionReceiveIndex >= 0;
John Recka447d292014-06-11 18:39:44 -0700140 }
141
Chris Craikdefb7f32014-04-08 18:17:07 -0700142 const char* getName() const {
143 return mName.string();
144 }
145
John Reck113e0822014-03-18 09:22:59 -0700146 void setName(const char* name) {
147 if (name) {
148 char* lastPeriod = strrchr(name, '.');
149 if (lastPeriod) {
150 mName.setTo(lastPeriod + 1);
151 } else {
152 mName.setTo(name);
153 }
154 }
155 }
156
John Reckff941dc2014-05-14 16:34:14 -0700157 bool isPropertyFieldDirty(DirtyPropertyMask field) const {
158 return mDirtyPropertyFields & field;
159 }
160
161 void setPropertyFieldsDirty(uint32_t fields) {
162 mDirtyPropertyFields |= fields;
163 }
164
John Recke4267ea2014-06-03 15:53:15 -0700165 const RenderProperties& properties() const {
John Reck113e0822014-03-18 09:22:59 -0700166 return mProperties;
167 }
168
John Reck52244ff2014-05-01 21:27:37 -0700169 RenderProperties& animatorProperties() {
170 return mProperties;
171 }
172
John Reckd0a0b2a2014-03-20 16:28:56 -0700173 const RenderProperties& stagingProperties() {
174 return mStagingProperties;
175 }
176
177 RenderProperties& mutateStagingProperties() {
John Reckd0a0b2a2014-03-20 16:28:56 -0700178 return mStagingProperties;
179 }
180
Chris Craik76caecf2015-11-02 19:17:45 -0800181 int getWidth() const {
John Reck113e0822014-03-18 09:22:59 -0700182 return properties().getWidth();
183 }
184
Chris Craik76caecf2015-11-02 19:17:45 -0800185 int getHeight() const {
John Reck113e0822014-03-18 09:22:59 -0700186 return properties().getHeight();
187 }
188
John Recke45b1fd2014-04-15 09:50:16 -0700189 ANDROID_API virtual void prepareTree(TreeInfo& info);
John Reckdcba6722014-07-08 13:59:49 -0700190 void destroyHardwareResources();
John Recke45b1fd2014-04-15 09:50:16 -0700191
192 // UI thread only!
John Reck68bfe0a2014-06-24 15:34:58 -0700193 ANDROID_API void addAnimator(const sp<BaseRenderNodeAnimator>& animator);
Doris Liu8b083202016-02-19 21:46:06 +0000194 void removeAnimator(const sp<BaseRenderNodeAnimator>& animator);
195
196 // This can only happen during pushStaging()
197 void onAnimatorTargetChanged(BaseRenderNodeAnimator* animator) {
198 mAnimatorManager.onAnimatorTargetChanged(animator);
199 }
John Reck668f0e32014-03-26 15:10:40 -0700200
John Reck119907c2014-08-14 09:02:01 -0700201 AnimatorManager& animators() { return mAnimatorManager; }
202
Chris Craik69e5adf2014-08-14 13:34:01 -0700203 void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false) const;
204
Chris Craikb565df12015-10-05 13:00:52 -0700205 bool nothingToDraw() const {
206 const Outline& outline = properties().getOutline();
Chris Craik003cc3d2015-10-16 10:24:55 -0700207 return mDisplayList == nullptr
Chris Craikb565df12015-10-05 13:00:52 -0700208 || properties().getAlpha() <= 0
209 || (outline.getShouldClip() && outline.isEmpty())
210 || properties().getScaleX() == 0
211 || properties().getScaleY() == 0;
212 }
213
Chris Craik0b7e8242015-10-28 16:50:44 -0700214 const DisplayList* getDisplayList() const {
215 return mDisplayList;
Chris Craikb565df12015-10-05 13:00:52 -0700216 }
Chris Craik0b7e8242015-10-28 16:50:44 -0700217#if HWUI_NEW_OPS
218 OffscreenBuffer* getLayer() const { return mLayer; }
219 OffscreenBuffer** getLayerHandle() { return &mLayer; } // ugh...
220#endif
Chris Craikb565df12015-10-05 13:00:52 -0700221
John Reckf6481082016-02-02 15:18:23 -0800222 class ANDROID_API PositionListener {
223 public:
224 virtual ~PositionListener() {}
225 virtual void onPositionUpdated(RenderNode& node, const TreeInfo& info) = 0;
226 };
227
228 // Note this is not thread safe, this needs to be called
229 // before the RenderNode is used for drawing.
230 // RenderNode takes ownership of the pointer
231 ANDROID_API void setPositionListener(PositionListener* listener) {
232 mPositionListener.reset(listener);
233 }
234
John Reck113e0822014-03-18 09:22:59 -0700235private:
Chris Craika7090e02014-06-20 16:01:00 -0700236 typedef key_value_pair_t<float, DrawRenderNodeOp*> ZDrawRenderNodeOpPair;
John Reck113e0822014-03-18 09:22:59 -0700237
John Reck272a6852015-07-29 16:48:58 -0700238 static size_t findNonNegativeIndex(const std::vector<ZDrawRenderNodeOpPair>& nodes) {
John Reck113e0822014-03-18 09:22:59 -0700239 for (size_t i = 0; i < nodes.size(); i++) {
240 if (nodes[i].key >= 0.0f) return i;
241 }
242 return nodes.size();
243 }
244
Chris Craikb9ce116d2015-08-20 15:14:06 -0700245 enum class ChildrenSelectMode {
246 NegativeZChildren,
247 PositiveZChildren
John Reck113e0822014-03-18 09:22:59 -0700248 };
249
Chris Craik8d1f2122015-11-24 16:40:09 -0800250 void computeOrderingImpl(renderNodeOp_t* opState,
251 std::vector<renderNodeOp_t*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700252 const mat4* transformFromProjectionSurface);
253
254 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700255 inline void setViewProperties(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700256
Chris Craik003cc3d2015-10-16 10:24:55 -0700257 void buildZSortedChildList(const DisplayList::Chunk& chunk,
John Reck272a6852015-07-29 16:48:58 -0700258 std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes);
John Reck113e0822014-03-18 09:22:59 -0700259
Chris Craikb265e2c2014-03-27 15:50:09 -0700260 template<class T>
261 inline void issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler);
262
John Reck113e0822014-03-18 09:22:59 -0700263 template <class T>
Chris Craikc3e75f92014-08-27 15:34:52 -0700264 inline void issueOperationsOf3dChildren(ChildrenSelectMode mode,
John Reck272a6852015-07-29 16:48:58 -0700265 const Matrix4& initialTransform, const std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craik80d49022014-06-20 15:03:43 -0700266 OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700267
268 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700269 inline void issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700270
Chris Craikb265e2c2014-03-27 15:50:09 -0700271 /**
272 * Issue the RenderNode's operations into a handler, recursing for subtrees through
Chris Craika7090e02014-06-20 16:01:00 -0700273 * DrawRenderNodeOp's defer() or replay() methods
Chris Craikb265e2c2014-03-27 15:50:09 -0700274 */
John Reck113e0822014-03-18 09:22:59 -0700275 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700276 inline void issueOperations(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700277
278 class TextContainer {
279 public:
280 size_t length() const {
281 return mByteLength;
282 }
283
284 const char* text() const {
285 return (const char*) mText;
286 }
287
288 size_t mByteLength;
289 const char* mText;
290 };
291
Chris Craikb565df12015-10-05 13:00:52 -0700292
293 void syncProperties();
294 void syncDisplayList();
295
Chris Craika766cb22015-06-08 16:49:43 -0700296 void prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer);
John Reck25fbb3f2014-06-12 13:46:45 -0700297 void pushStagingPropertiesChanges(TreeInfo& info);
298 void pushStagingDisplayListChanges(TreeInfo& info);
Chris Craik003cc3d2015-10-16 10:24:55 -0700299 void prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayList* subtree);
Chris Craik0b7e8242015-10-28 16:50:44 -0700300#if !HWUI_NEW_OPS
John Reck25fbb3f2014-06-12 13:46:45 -0700301 void applyLayerPropertiesToLayer(TreeInfo& info);
Chris Craik0b7e8242015-10-28 16:50:44 -0700302#endif
John Recka7c2ea22014-08-08 13:21:00 -0700303 void prepareLayer(TreeInfo& info, uint32_t dirtyMask);
John Reck25fbb3f2014-06-12 13:46:45 -0700304 void pushLayerUpdate(TreeInfo& info);
Chris Craik003cc3d2015-10-16 10:24:55 -0700305 void deleteDisplayList();
John Reck0a973302014-07-16 13:29:45 -0700306 void damageSelf(TreeInfo& info);
John Reckdcba6722014-07-08 13:59:49 -0700307
308 void incParentRefCount() { mParentCount++; }
309 void decParentRefCount();
John Reck8de65a82014-04-09 15:23:38 -0700310
John Reck113e0822014-03-18 09:22:59 -0700311 String8 mName;
John Reck113e0822014-03-18 09:22:59 -0700312
John Reckff941dc2014-05-14 16:34:14 -0700313 uint32_t mDirtyPropertyFields;
John Reck113e0822014-03-18 09:22:59 -0700314 RenderProperties mProperties;
John Reckd0a0b2a2014-03-20 16:28:56 -0700315 RenderProperties mStagingProperties;
316
Chris Craik003cc3d2015-10-16 10:24:55 -0700317 bool mNeedsDisplayListSync;
318 // WARNING: Do not delete this directly, you must go through deleteDisplayList()!
319 DisplayList* mDisplayList;
320 DisplayList* mStagingDisplayList;
John Reck113e0822014-03-18 09:22:59 -0700321
John Reck68bfe0a2014-06-24 15:34:58 -0700322 friend class AnimatorManager;
323 AnimatorManager mAnimatorManager;
John Recke45b1fd2014-04-15 09:50:16 -0700324
John Reck25fbb3f2014-06-12 13:46:45 -0700325 // Owned by RT. Lifecycle is managed by prepareTree(), with the exception
326 // being in ~RenderNode() which may happen on any thread.
Chris Craik0b7e8242015-10-28 16:50:44 -0700327 layer_t* mLayer = nullptr;
John Reck25fbb3f2014-06-12 13:46:45 -0700328
John Reck113e0822014-03-18 09:22:59 -0700329 /**
330 * Draw time state - these properties are only set and used during rendering
331 */
332
333 // for projection surfaces, contains a list of all children items
Chris Craik8d1f2122015-11-24 16:40:09 -0800334 std::vector<renderNodeOp_t*> mProjectedNodes;
John Reckdcba6722014-07-08 13:59:49 -0700335
336 // How many references our parent(s) have to us. Typically this should alternate
337 // between 2 and 1 (when a staging push happens we inc first then dec)
338 // When this hits 0 we are no longer in the tree, so any hardware resources
339 // (specifically Layers) should be released.
340 // This is *NOT* thread-safe, and should therefore only be tracking
Chris Craik003cc3d2015-10-16 10:24:55 -0700341 // mDisplayList, not mStagingDisplayList.
John Reckdcba6722014-07-08 13:59:49 -0700342 uint32_t mParentCount;
John Reckf6481082016-02-02 15:18:23 -0800343
344 std::unique_ptr<PositionListener> mPositionListener;
John Reck113e0822014-03-18 09:22:59 -0700345}; // class RenderNode
346
347} /* namespace uirenderer */
348} /* namespace android */
349
350#endif /* RENDERNODE_H */