blob: f9735a231d7aaed4eca968a4818fcbb232e0f6d0 [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 Reck44b49f02016-03-25 14:29:48 -070071class TreeObserver;
John Reck113e0822014-03-18 09:22:59 -070072
John Recke248bd12015-08-05 13:53:53 -070073namespace proto {
74class RenderNode;
75}
76
John Reck113e0822014-03-18 09:22:59 -070077/**
78 * Primary class for storing recorded canvas commands, as well as per-View/ViewGroup display properties.
79 *
80 * Recording of canvas commands is somewhat similar to SkPicture, except the canvas-recording
Chris Craik003cc3d2015-10-16 10:24:55 -070081 * functionality is split between DisplayListCanvas (which manages the recording), DisplayList
John Reck113e0822014-03-18 09:22:59 -070082 * (which holds the actual data), and DisplayList (which holds properties and performs playback onto
83 * a renderer).
84 *
Chris Craik003cc3d2015-10-16 10:24:55 -070085 * Note that DisplayList is swapped out from beneath an individual RenderNode when a view's
86 * recorded stream of canvas operations is refreshed. The RenderNode (and its properties) stay
John Reck113e0822014-03-18 09:22:59 -070087 * attached.
88 */
John Reck087bc0c2014-04-04 16:20:08 -070089class RenderNode : public VirtualLightRefBase {
Chris Craikb565df12015-10-05 13:00:52 -070090friend class TestUtils; // allow TestUtils to access syncDisplayList / syncProperties
Chris Craikf158b492016-01-12 14:45:08 -080091friend class FrameBuilder;
John Reck113e0822014-03-18 09:22:59 -070092public:
John Reckff941dc2014-05-14 16:34:14 -070093 enum DirtyPropertyMask {
94 GENERIC = 1 << 1,
95 TRANSLATION_X = 1 << 2,
96 TRANSLATION_Y = 1 << 3,
97 TRANSLATION_Z = 1 << 4,
98 SCALE_X = 1 << 5,
99 SCALE_Y = 1 << 6,
100 ROTATION = 1 << 7,
101 ROTATION_X = 1 << 8,
102 ROTATION_Y = 1 << 9,
103 X = 1 << 10,
104 Y = 1 << 11,
105 Z = 1 << 12,
106 ALPHA = 1 << 13,
John Recka7c2ea22014-08-08 13:21:00 -0700107 DISPLAY_LIST = 1 << 14,
John Reckff941dc2014-05-14 16:34:14 -0700108 };
109
John Reck113e0822014-03-18 09:22:59 -0700110 ANDROID_API RenderNode();
John Recke45b1fd2014-04-15 09:50:16 -0700111 ANDROID_API virtual ~RenderNode();
John Reck113e0822014-03-18 09:22:59 -0700112
113 // See flags defined in DisplayList.java
114 enum ReplayFlag {
115 kReplayFlag_ClipChildren = 0x1
116 };
117
John Reck443a7142014-09-04 17:40:05 -0700118 void debugDumpLayers(const char* prefix);
John Reck113e0822014-03-18 09:22:59 -0700119
John Reck51f2d602016-04-06 07:50:47 -0700120 ANDROID_API void setStagingDisplayList(DisplayList* newData, TreeObserver* observer);
John Reck113e0822014-03-18 09:22:59 -0700121
122 void computeOrdering();
Chris Craikb265e2c2014-03-27 15:50:09 -0700123
Chris Craik80d49022014-06-20 15:03:43 -0700124 void defer(DeferStateStruct& deferStruct, const int level);
125 void replay(ReplayStateStruct& replayStruct, const int level);
John Reck113e0822014-03-18 09:22:59 -0700126
Chris Craik91eff222016-02-22 13:39:33 -0800127#if HWUI_NEW_OPS
128 ANDROID_API void output(uint32_t level = 0, const char* label = "Root");
129#else
John Reck113e0822014-03-18 09:22:59 -0700130 ANDROID_API void output(uint32_t level = 1);
Chris Craik91eff222016-02-22 13:39:33 -0800131#endif
John Reckfe5e7b72014-05-23 17:42:28 -0700132 ANDROID_API int getDebugSize();
John Recke248bd12015-08-05 13:53:53 -0700133 void copyTo(proto::RenderNode* node);
John Reck113e0822014-03-18 09:22:59 -0700134
135 bool isRenderable() const {
Chris Craik003cc3d2015-10-16 10:24:55 -0700136 return mDisplayList && !mDisplayList->isEmpty();
John Reck113e0822014-03-18 09:22:59 -0700137 }
138
John Recka447d292014-06-11 18:39:44 -0700139 bool hasProjectionReceiver() const {
Chris Craik003cc3d2015-10-16 10:24:55 -0700140 return mDisplayList && mDisplayList->projectionReceiveIndex >= 0;
John Recka447d292014-06-11 18:39:44 -0700141 }
142
Chris Craikdefb7f32014-04-08 18:17:07 -0700143 const char* getName() const {
144 return mName.string();
145 }
146
John Reck113e0822014-03-18 09:22:59 -0700147 void setName(const char* name) {
148 if (name) {
Dan Austin71831a62016-03-24 12:03:42 -0700149 const char* lastPeriod = strrchr(name, '.');
John Reck113e0822014-03-18 09:22:59 -0700150 if (lastPeriod) {
151 mName.setTo(lastPeriod + 1);
152 } else {
153 mName.setTo(name);
154 }
155 }
156 }
157
John Reck44b49f02016-03-25 14:29:48 -0700158 VirtualLightRefBase* getUserContext() const {
159 return mUserContext.get();
160 }
161
162 void setUserContext(VirtualLightRefBase* context) {
163 mUserContext = context;
164 }
165
John Reckff941dc2014-05-14 16:34:14 -0700166 bool isPropertyFieldDirty(DirtyPropertyMask field) const {
167 return mDirtyPropertyFields & field;
168 }
169
170 void setPropertyFieldsDirty(uint32_t fields) {
171 mDirtyPropertyFields |= fields;
172 }
173
John Recke4267ea2014-06-03 15:53:15 -0700174 const RenderProperties& properties() const {
John Reck113e0822014-03-18 09:22:59 -0700175 return mProperties;
176 }
177
John Reck52244ff2014-05-01 21:27:37 -0700178 RenderProperties& animatorProperties() {
179 return mProperties;
180 }
181
John Reckd0a0b2a2014-03-20 16:28:56 -0700182 const RenderProperties& stagingProperties() {
183 return mStagingProperties;
184 }
185
186 RenderProperties& mutateStagingProperties() {
John Reckd0a0b2a2014-03-20 16:28:56 -0700187 return mStagingProperties;
188 }
189
Chris Craik76caecf2015-11-02 19:17:45 -0800190 int getWidth() const {
John Reck113e0822014-03-18 09:22:59 -0700191 return properties().getWidth();
192 }
193
Chris Craik76caecf2015-11-02 19:17:45 -0800194 int getHeight() const {
John Reck113e0822014-03-18 09:22:59 -0700195 return properties().getHeight();
196 }
197
John Recke45b1fd2014-04-15 09:50:16 -0700198 ANDROID_API virtual void prepareTree(TreeInfo& info);
John Reckaa6e84f2016-06-16 15:36:13 -0700199 void destroyHardwareResources(TreeObserver* observer, TreeInfo* info = nullptr);
John Recke45b1fd2014-04-15 09:50:16 -0700200
201 // UI thread only!
John Reck68bfe0a2014-06-24 15:34:58 -0700202 ANDROID_API void addAnimator(const sp<BaseRenderNodeAnimator>& animator);
Doris Liu8b083202016-02-19 21:46:06 +0000203 void removeAnimator(const sp<BaseRenderNodeAnimator>& animator);
204
205 // This can only happen during pushStaging()
206 void onAnimatorTargetChanged(BaseRenderNodeAnimator* animator) {
207 mAnimatorManager.onAnimatorTargetChanged(animator);
208 }
John Reck668f0e32014-03-26 15:10:40 -0700209
John Reck119907c2014-08-14 09:02:01 -0700210 AnimatorManager& animators() { return mAnimatorManager; }
211
Chris Craik69e5adf2014-08-14 13:34:01 -0700212 void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false) const;
213
Chris Craikb565df12015-10-05 13:00:52 -0700214 bool nothingToDraw() const {
215 const Outline& outline = properties().getOutline();
Chris Craik003cc3d2015-10-16 10:24:55 -0700216 return mDisplayList == nullptr
Chris Craikb565df12015-10-05 13:00:52 -0700217 || properties().getAlpha() <= 0
218 || (outline.getShouldClip() && outline.isEmpty())
219 || properties().getScaleX() == 0
220 || properties().getScaleY() == 0;
221 }
222
Chris Craik0b7e8242015-10-28 16:50:44 -0700223 const DisplayList* getDisplayList() const {
224 return mDisplayList;
Chris Craikb565df12015-10-05 13:00:52 -0700225 }
Chris Craik0b7e8242015-10-28 16:50:44 -0700226#if HWUI_NEW_OPS
227 OffscreenBuffer* getLayer() const { return mLayer; }
228 OffscreenBuffer** getLayerHandle() { return &mLayer; } // ugh...
229#endif
Chris Craikb565df12015-10-05 13:00:52 -0700230
John Reckaa6e84f2016-06-16 15:36:13 -0700231 // Note: The position callbacks are relying on the listener using
232 // the frameNumber to appropriately batch/synchronize these transactions.
233 // There is no other filtering/batching to ensure that only the "final"
234 // state called once per frame.
John Reck7b570de2016-06-27 13:27:23 -0700235 class ANDROID_API PositionListener : public VirtualLightRefBase {
John Reckf6481082016-02-02 15:18:23 -0800236 public:
237 virtual ~PositionListener() {}
John Reckaa6e84f2016-06-16 15:36:13 -0700238 // Called when the RenderNode's position changes
John Reckf6481082016-02-02 15:18:23 -0800239 virtual void onPositionUpdated(RenderNode& node, const TreeInfo& info) = 0;
John Reckaa6e84f2016-06-16 15:36:13 -0700240 // Called when the RenderNode no longer has a position. As in, it's
241 // no longer being drawn.
242 // Note, tree info might be null
243 virtual void onPositionLost(RenderNode& node, const TreeInfo* info) = 0;
John Reckf6481082016-02-02 15:18:23 -0800244 };
245
246 // Note this is not thread safe, this needs to be called
247 // before the RenderNode is used for drawing.
248 // RenderNode takes ownership of the pointer
249 ANDROID_API void setPositionListener(PositionListener* listener) {
John Reck7b570de2016-06-27 13:27:23 -0700250 mPositionListener = listener;
John Reckf6481082016-02-02 15:18:23 -0800251 }
252
John Reck44b49f02016-03-25 14:29:48 -0700253 // This is only modified in MODE_FULL, so it can be safely accessed
254 // on the UI thread.
255 ANDROID_API bool hasParents() {
256 return mParentCount;
257 }
258
John Reck113e0822014-03-18 09:22:59 -0700259private:
Chris Craika7090e02014-06-20 16:01:00 -0700260 typedef key_value_pair_t<float, DrawRenderNodeOp*> ZDrawRenderNodeOpPair;
John Reck113e0822014-03-18 09:22:59 -0700261
John Reck272a6852015-07-29 16:48:58 -0700262 static size_t findNonNegativeIndex(const std::vector<ZDrawRenderNodeOpPair>& nodes) {
John Reck113e0822014-03-18 09:22:59 -0700263 for (size_t i = 0; i < nodes.size(); i++) {
264 if (nodes[i].key >= 0.0f) return i;
265 }
266 return nodes.size();
267 }
268
Chris Craikb9ce116d2015-08-20 15:14:06 -0700269 enum class ChildrenSelectMode {
270 NegativeZChildren,
271 PositiveZChildren
John Reck113e0822014-03-18 09:22:59 -0700272 };
273
Chris Craik8d1f2122015-11-24 16:40:09 -0800274 void computeOrderingImpl(renderNodeOp_t* opState,
275 std::vector<renderNodeOp_t*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700276 const mat4* transformFromProjectionSurface);
277
278 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700279 inline void setViewProperties(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700280
Chris Craik003cc3d2015-10-16 10:24:55 -0700281 void buildZSortedChildList(const DisplayList::Chunk& chunk,
John Reck272a6852015-07-29 16:48:58 -0700282 std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes);
John Reck113e0822014-03-18 09:22:59 -0700283
Chris Craikb265e2c2014-03-27 15:50:09 -0700284 template<class T>
285 inline void issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler);
286
John Reck113e0822014-03-18 09:22:59 -0700287 template <class T>
Chris Craikc3e75f92014-08-27 15:34:52 -0700288 inline void issueOperationsOf3dChildren(ChildrenSelectMode mode,
John Reck272a6852015-07-29 16:48:58 -0700289 const Matrix4& initialTransform, const std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craik80d49022014-06-20 15:03:43 -0700290 OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700291
292 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700293 inline void issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700294
Chris Craikb265e2c2014-03-27 15:50:09 -0700295 /**
296 * Issue the RenderNode's operations into a handler, recursing for subtrees through
Chris Craika7090e02014-06-20 16:01:00 -0700297 * DrawRenderNodeOp's defer() or replay() methods
Chris Craikb265e2c2014-03-27 15:50:09 -0700298 */
John Reck113e0822014-03-18 09:22:59 -0700299 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700300 inline void issueOperations(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700301
302 class TextContainer {
303 public:
304 size_t length() const {
305 return mByteLength;
306 }
307
308 const char* text() const {
309 return (const char*) mText;
310 }
311
312 size_t mByteLength;
313 const char* mText;
314 };
315
Chris Craikb565df12015-10-05 13:00:52 -0700316
317 void syncProperties();
John Reckaa6e84f2016-06-16 15:36:13 -0700318 void syncDisplayList(TreeInfo* info);
Chris Craikb565df12015-10-05 13:00:52 -0700319
Chris Craika766cb22015-06-08 16:49:43 -0700320 void prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer);
John Reck25fbb3f2014-06-12 13:46:45 -0700321 void pushStagingPropertiesChanges(TreeInfo& info);
322 void pushStagingDisplayListChanges(TreeInfo& info);
Chris Craik003cc3d2015-10-16 10:24:55 -0700323 void prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayList* subtree);
Chris Craik0b7e8242015-10-28 16:50:44 -0700324#if !HWUI_NEW_OPS
John Reck25fbb3f2014-06-12 13:46:45 -0700325 void applyLayerPropertiesToLayer(TreeInfo& info);
Chris Craik0b7e8242015-10-28 16:50:44 -0700326#endif
John Recka7c2ea22014-08-08 13:21:00 -0700327 void prepareLayer(TreeInfo& info, uint32_t dirtyMask);
John Reck25fbb3f2014-06-12 13:46:45 -0700328 void pushLayerUpdate(TreeInfo& info);
John Reckaa6e84f2016-06-16 15:36:13 -0700329 void deleteDisplayList(TreeObserver* observer, TreeInfo* info = nullptr);
John Reck0a973302014-07-16 13:29:45 -0700330 void damageSelf(TreeInfo& info);
John Reckdcba6722014-07-08 13:59:49 -0700331
332 void incParentRefCount() { mParentCount++; }
John Reckaa6e84f2016-06-16 15:36:13 -0700333 void decParentRefCount(TreeObserver* observer, TreeInfo* info = nullptr);
John Reck8de65a82014-04-09 15:23:38 -0700334
John Reck113e0822014-03-18 09:22:59 -0700335 String8 mName;
John Reck44b49f02016-03-25 14:29:48 -0700336 sp<VirtualLightRefBase> mUserContext;
John Reck113e0822014-03-18 09:22:59 -0700337
John Reckff941dc2014-05-14 16:34:14 -0700338 uint32_t mDirtyPropertyFields;
John Reck113e0822014-03-18 09:22:59 -0700339 RenderProperties mProperties;
John Reckd0a0b2a2014-03-20 16:28:56 -0700340 RenderProperties mStagingProperties;
341
Chris Craik003cc3d2015-10-16 10:24:55 -0700342 bool mNeedsDisplayListSync;
343 // WARNING: Do not delete this directly, you must go through deleteDisplayList()!
344 DisplayList* mDisplayList;
345 DisplayList* mStagingDisplayList;
John Reck113e0822014-03-18 09:22:59 -0700346
John Reck68bfe0a2014-06-24 15:34:58 -0700347 friend class AnimatorManager;
348 AnimatorManager mAnimatorManager;
John Recke45b1fd2014-04-15 09:50:16 -0700349
John Reck25fbb3f2014-06-12 13:46:45 -0700350 // Owned by RT. Lifecycle is managed by prepareTree(), with the exception
351 // being in ~RenderNode() which may happen on any thread.
Chris Craik0b7e8242015-10-28 16:50:44 -0700352 layer_t* mLayer = nullptr;
John Reck25fbb3f2014-06-12 13:46:45 -0700353
John Reck113e0822014-03-18 09:22:59 -0700354 /**
355 * Draw time state - these properties are only set and used during rendering
356 */
357
358 // for projection surfaces, contains a list of all children items
Chris Craik8d1f2122015-11-24 16:40:09 -0800359 std::vector<renderNodeOp_t*> mProjectedNodes;
John Reckdcba6722014-07-08 13:59:49 -0700360
361 // How many references our parent(s) have to us. Typically this should alternate
362 // between 2 and 1 (when a staging push happens we inc first then dec)
363 // When this hits 0 we are no longer in the tree, so any hardware resources
364 // (specifically Layers) should be released.
365 // This is *NOT* thread-safe, and should therefore only be tracking
Chris Craik003cc3d2015-10-16 10:24:55 -0700366 // mDisplayList, not mStagingDisplayList.
John Reckdcba6722014-07-08 13:59:49 -0700367 uint32_t mParentCount;
John Reckf6481082016-02-02 15:18:23 -0800368
John Reck7b570de2016-06-27 13:27:23 -0700369 sp<PositionListener> mPositionListener;
John Reck113e0822014-03-18 09:22:59 -0700370}; // class RenderNode
371
372} /* namespace uirenderer */
373} /* namespace android */
374
375#endif /* RENDERNODE_H */