blob: e0376450178f1b152f682a199334bd3ef237a38a [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);
Doris Liu8b083202016-02-19 21:46:06 +0000190 void removeAnimator(const sp<BaseRenderNodeAnimator>& animator);
191
192 // This can only happen during pushStaging()
193 void onAnimatorTargetChanged(BaseRenderNodeAnimator* animator) {
194 mAnimatorManager.onAnimatorTargetChanged(animator);
195 }
John Reck668f0e32014-03-26 15:10:40 -0700196
John Reck119907c2014-08-14 09:02:01 -0700197 AnimatorManager& animators() { return mAnimatorManager; }
198
Chris Craik69e5adf2014-08-14 13:34:01 -0700199 void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false) const;
200
Chris Craikb565df12015-10-05 13:00:52 -0700201 bool nothingToDraw() const {
202 const Outline& outline = properties().getOutline();
Chris Craik003cc3d2015-10-16 10:24:55 -0700203 return mDisplayList == nullptr
Chris Craikb565df12015-10-05 13:00:52 -0700204 || properties().getAlpha() <= 0
205 || (outline.getShouldClip() && outline.isEmpty())
206 || properties().getScaleX() == 0
207 || properties().getScaleY() == 0;
208 }
209
Chris Craik0b7e8242015-10-28 16:50:44 -0700210 const DisplayList* getDisplayList() const {
211 return mDisplayList;
Chris Craikb565df12015-10-05 13:00:52 -0700212 }
Chris Craik0b7e8242015-10-28 16:50:44 -0700213#if HWUI_NEW_OPS
214 OffscreenBuffer* getLayer() const { return mLayer; }
215 OffscreenBuffer** getLayerHandle() { return &mLayer; } // ugh...
216#endif
Chris Craikb565df12015-10-05 13:00:52 -0700217
John Reckf6481082016-02-02 15:18:23 -0800218 class ANDROID_API PositionListener {
219 public:
220 virtual ~PositionListener() {}
221 virtual void onPositionUpdated(RenderNode& node, const TreeInfo& info) = 0;
222 };
223
224 // Note this is not thread safe, this needs to be called
225 // before the RenderNode is used for drawing.
226 // RenderNode takes ownership of the pointer
227 ANDROID_API void setPositionListener(PositionListener* listener) {
228 mPositionListener.reset(listener);
229 }
230
John Reck113e0822014-03-18 09:22:59 -0700231private:
Chris Craika7090e02014-06-20 16:01:00 -0700232 typedef key_value_pair_t<float, DrawRenderNodeOp*> ZDrawRenderNodeOpPair;
John Reck113e0822014-03-18 09:22:59 -0700233
John Reck272a6852015-07-29 16:48:58 -0700234 static size_t findNonNegativeIndex(const std::vector<ZDrawRenderNodeOpPair>& nodes) {
John Reck113e0822014-03-18 09:22:59 -0700235 for (size_t i = 0; i < nodes.size(); i++) {
236 if (nodes[i].key >= 0.0f) return i;
237 }
238 return nodes.size();
239 }
240
Chris Craikb9ce116d2015-08-20 15:14:06 -0700241 enum class ChildrenSelectMode {
242 NegativeZChildren,
243 PositiveZChildren
John Reck113e0822014-03-18 09:22:59 -0700244 };
245
Chris Craik8d1f2122015-11-24 16:40:09 -0800246 void computeOrderingImpl(renderNodeOp_t* opState,
247 std::vector<renderNodeOp_t*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700248 const mat4* transformFromProjectionSurface);
249
250 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700251 inline void setViewProperties(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700252
Chris Craik003cc3d2015-10-16 10:24:55 -0700253 void buildZSortedChildList(const DisplayList::Chunk& chunk,
John Reck272a6852015-07-29 16:48:58 -0700254 std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes);
John Reck113e0822014-03-18 09:22:59 -0700255
Chris Craikb265e2c2014-03-27 15:50:09 -0700256 template<class T>
257 inline void issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler);
258
John Reck113e0822014-03-18 09:22:59 -0700259 template <class T>
Chris Craikc3e75f92014-08-27 15:34:52 -0700260 inline void issueOperationsOf3dChildren(ChildrenSelectMode mode,
John Reck272a6852015-07-29 16:48:58 -0700261 const Matrix4& initialTransform, const std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craik80d49022014-06-20 15:03:43 -0700262 OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700263
264 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700265 inline void issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700266
Chris Craikb265e2c2014-03-27 15:50:09 -0700267 /**
268 * Issue the RenderNode's operations into a handler, recursing for subtrees through
Chris Craika7090e02014-06-20 16:01:00 -0700269 * DrawRenderNodeOp's defer() or replay() methods
Chris Craikb265e2c2014-03-27 15:50:09 -0700270 */
John Reck113e0822014-03-18 09:22:59 -0700271 template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700272 inline void issueOperations(OpenGLRenderer& renderer, T& handler);
John Reck113e0822014-03-18 09:22:59 -0700273
274 class TextContainer {
275 public:
276 size_t length() const {
277 return mByteLength;
278 }
279
280 const char* text() const {
281 return (const char*) mText;
282 }
283
284 size_t mByteLength;
285 const char* mText;
286 };
287
Chris Craikb565df12015-10-05 13:00:52 -0700288
289 void syncProperties();
290 void syncDisplayList();
291
Chris Craika766cb22015-06-08 16:49:43 -0700292 void prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer);
John Reck25fbb3f2014-06-12 13:46:45 -0700293 void pushStagingPropertiesChanges(TreeInfo& info);
294 void pushStagingDisplayListChanges(TreeInfo& info);
Chris Craik003cc3d2015-10-16 10:24:55 -0700295 void prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayList* subtree);
Chris Craik0b7e8242015-10-28 16:50:44 -0700296#if !HWUI_NEW_OPS
John Reck25fbb3f2014-06-12 13:46:45 -0700297 void applyLayerPropertiesToLayer(TreeInfo& info);
Chris Craik0b7e8242015-10-28 16:50:44 -0700298#endif
John Recka7c2ea22014-08-08 13:21:00 -0700299 void prepareLayer(TreeInfo& info, uint32_t dirtyMask);
John Reck25fbb3f2014-06-12 13:46:45 -0700300 void pushLayerUpdate(TreeInfo& info);
Chris Craik003cc3d2015-10-16 10:24:55 -0700301 void deleteDisplayList();
John Reck0a973302014-07-16 13:29:45 -0700302 void damageSelf(TreeInfo& info);
John Reckdcba6722014-07-08 13:59:49 -0700303
304 void incParentRefCount() { mParentCount++; }
305 void decParentRefCount();
John Reck8de65a82014-04-09 15:23:38 -0700306
John Reck113e0822014-03-18 09:22:59 -0700307 String8 mName;
John Reck113e0822014-03-18 09:22:59 -0700308
John Reckff941dc2014-05-14 16:34:14 -0700309 uint32_t mDirtyPropertyFields;
John Reck113e0822014-03-18 09:22:59 -0700310 RenderProperties mProperties;
John Reckd0a0b2a2014-03-20 16:28:56 -0700311 RenderProperties mStagingProperties;
312
Chris Craik003cc3d2015-10-16 10:24:55 -0700313 bool mNeedsDisplayListSync;
314 // WARNING: Do not delete this directly, you must go through deleteDisplayList()!
315 DisplayList* mDisplayList;
316 DisplayList* mStagingDisplayList;
John Reck113e0822014-03-18 09:22:59 -0700317
John Reck68bfe0a2014-06-24 15:34:58 -0700318 friend class AnimatorManager;
319 AnimatorManager mAnimatorManager;
John Recke45b1fd2014-04-15 09:50:16 -0700320
John Reck25fbb3f2014-06-12 13:46:45 -0700321 // Owned by RT. Lifecycle is managed by prepareTree(), with the exception
322 // being in ~RenderNode() which may happen on any thread.
Chris Craik0b7e8242015-10-28 16:50:44 -0700323 layer_t* mLayer = nullptr;
John Reck25fbb3f2014-06-12 13:46:45 -0700324
John Reck113e0822014-03-18 09:22:59 -0700325 /**
326 * Draw time state - these properties are only set and used during rendering
327 */
328
329 // for projection surfaces, contains a list of all children items
Chris Craik8d1f2122015-11-24 16:40:09 -0800330 std::vector<renderNodeOp_t*> mProjectedNodes;
John Reckdcba6722014-07-08 13:59:49 -0700331
332 // How many references our parent(s) have to us. Typically this should alternate
333 // between 2 and 1 (when a staging push happens we inc first then dec)
334 // When this hits 0 we are no longer in the tree, so any hardware resources
335 // (specifically Layers) should be released.
336 // This is *NOT* thread-safe, and should therefore only be tracking
Chris Craik003cc3d2015-10-16 10:24:55 -0700337 // mDisplayList, not mStagingDisplayList.
John Reckdcba6722014-07-08 13:59:49 -0700338 uint32_t mParentCount;
John Reckf6481082016-02-02 15:18:23 -0800339
340 std::unique_ptr<PositionListener> mPositionListener;
John Reck113e0822014-03-18 09:22:59 -0700341}; // class RenderNode
342
343} /* namespace uirenderer */
344} /* namespace android */
345
346#endif /* RENDERNODE_H */