blob: da93c131bb5a89c32f02e73e8ed2765cafc3fc3e [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 */
Chris Craik5e00c7c2016-07-06 16:10:09 -070016
17#pragma once
John Reck113e0822014-03-18 09:22:59 -070018
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 Craik0b7e8242015-10-28 16:50:44 -070047class DisplayListOp;
Chris Craikf158b492016-01-12 14:45:08 -080048class FrameBuilder;
Chris Craik0b7e8242015-10-28 16:50:44 -070049class OffscreenBuffer;
Chris Craik5e00c7c2016-07-06 16:10:09 -070050class Rect;
51class SkiaShader;
Chris Craik8d1f2122015-11-24 16:40:09 -080052struct RenderNodeOp;
Chris Craik0b7e8242015-10-28 16:50:44 -070053
Tom Hudson2dc236b2014-10-15 15:46:42 -040054class TreeInfo;
John Reck44b49f02016-03-25 14:29:48 -070055class TreeObserver;
John Reck113e0822014-03-18 09:22:59 -070056
John Recke248bd12015-08-05 13:53:53 -070057namespace proto {
58class RenderNode;
59}
60
John Reck113e0822014-03-18 09:22:59 -070061/**
62 * Primary class for storing recorded canvas commands, as well as per-View/ViewGroup display properties.
63 *
64 * Recording of canvas commands is somewhat similar to SkPicture, except the canvas-recording
Chris Craik5e00c7c2016-07-06 16:10:09 -070065 * functionality is split between RecordingCanvas (which manages the recording), DisplayList
66 * (which holds the actual data), and RenderNode (which holds properties used for render playback).
John Reck113e0822014-03-18 09:22:59 -070067 *
Chris Craik003cc3d2015-10-16 10:24:55 -070068 * Note that DisplayList is swapped out from beneath an individual RenderNode when a view's
69 * recorded stream of canvas operations is refreshed. The RenderNode (and its properties) stay
John Reck113e0822014-03-18 09:22:59 -070070 * attached.
71 */
John Reck087bc0c2014-04-04 16:20:08 -070072class RenderNode : public VirtualLightRefBase {
Chris Craikb565df12015-10-05 13:00:52 -070073friend class TestUtils; // allow TestUtils to access syncDisplayList / syncProperties
Chris Craikf158b492016-01-12 14:45:08 -080074friend class FrameBuilder;
John Reck113e0822014-03-18 09:22:59 -070075public:
John Reckff941dc2014-05-14 16:34:14 -070076 enum DirtyPropertyMask {
77 GENERIC = 1 << 1,
78 TRANSLATION_X = 1 << 2,
79 TRANSLATION_Y = 1 << 3,
80 TRANSLATION_Z = 1 << 4,
81 SCALE_X = 1 << 5,
82 SCALE_Y = 1 << 6,
83 ROTATION = 1 << 7,
84 ROTATION_X = 1 << 8,
85 ROTATION_Y = 1 << 9,
86 X = 1 << 10,
87 Y = 1 << 11,
88 Z = 1 << 12,
89 ALPHA = 1 << 13,
John Recka7c2ea22014-08-08 13:21:00 -070090 DISPLAY_LIST = 1 << 14,
John Reckff941dc2014-05-14 16:34:14 -070091 };
92
John Reck113e0822014-03-18 09:22:59 -070093 ANDROID_API RenderNode();
John Recke45b1fd2014-04-15 09:50:16 -070094 ANDROID_API virtual ~RenderNode();
John Reck113e0822014-03-18 09:22:59 -070095
96 // See flags defined in DisplayList.java
97 enum ReplayFlag {
98 kReplayFlag_ClipChildren = 0x1
99 };
100
John Reck51f2d602016-04-06 07:50:47 -0700101 ANDROID_API void setStagingDisplayList(DisplayList* newData, TreeObserver* observer);
John Reck113e0822014-03-18 09:22:59 -0700102
103 void computeOrdering();
Chris Craikb265e2c2014-03-27 15:50:09 -0700104
sergeyvc3849aa2016-08-08 13:22:06 -0700105 ANDROID_API void output();
John Reckfe5e7b72014-05-23 17:42:28 -0700106 ANDROID_API int getDebugSize();
John Recke248bd12015-08-05 13:53:53 -0700107 void copyTo(proto::RenderNode* node);
John Reck113e0822014-03-18 09:22:59 -0700108
109 bool isRenderable() const {
Chris Craik003cc3d2015-10-16 10:24:55 -0700110 return mDisplayList && !mDisplayList->isEmpty();
John Reck113e0822014-03-18 09:22:59 -0700111 }
112
John Recka447d292014-06-11 18:39:44 -0700113 bool hasProjectionReceiver() const {
Chris Craik003cc3d2015-10-16 10:24:55 -0700114 return mDisplayList && mDisplayList->projectionReceiveIndex >= 0;
John Recka447d292014-06-11 18:39:44 -0700115 }
116
Chris Craikdefb7f32014-04-08 18:17:07 -0700117 const char* getName() const {
118 return mName.string();
119 }
120
John Reck113e0822014-03-18 09:22:59 -0700121 void setName(const char* name) {
122 if (name) {
Dan Austin71831a62016-03-24 12:03:42 -0700123 const char* lastPeriod = strrchr(name, '.');
John Reck113e0822014-03-18 09:22:59 -0700124 if (lastPeriod) {
125 mName.setTo(lastPeriod + 1);
126 } else {
127 mName.setTo(name);
128 }
129 }
130 }
131
John Reck44b49f02016-03-25 14:29:48 -0700132 VirtualLightRefBase* getUserContext() const {
133 return mUserContext.get();
134 }
135
136 void setUserContext(VirtualLightRefBase* context) {
137 mUserContext = context;
138 }
139
John Reckff941dc2014-05-14 16:34:14 -0700140 bool isPropertyFieldDirty(DirtyPropertyMask field) const {
141 return mDirtyPropertyFields & field;
142 }
143
144 void setPropertyFieldsDirty(uint32_t fields) {
145 mDirtyPropertyFields |= fields;
146 }
147
John Recke4267ea2014-06-03 15:53:15 -0700148 const RenderProperties& properties() const {
John Reck113e0822014-03-18 09:22:59 -0700149 return mProperties;
150 }
151
John Reck52244ff2014-05-01 21:27:37 -0700152 RenderProperties& animatorProperties() {
153 return mProperties;
154 }
155
John Reckd0a0b2a2014-03-20 16:28:56 -0700156 const RenderProperties& stagingProperties() {
157 return mStagingProperties;
158 }
159
160 RenderProperties& mutateStagingProperties() {
John Reckd0a0b2a2014-03-20 16:28:56 -0700161 return mStagingProperties;
162 }
163
Chris Craik76caecf2015-11-02 19:17:45 -0800164 int getWidth() const {
John Reck113e0822014-03-18 09:22:59 -0700165 return properties().getWidth();
166 }
167
Chris Craik76caecf2015-11-02 19:17:45 -0800168 int getHeight() const {
John Reck113e0822014-03-18 09:22:59 -0700169 return properties().getHeight();
170 }
171
John Recke45b1fd2014-04-15 09:50:16 -0700172 ANDROID_API virtual void prepareTree(TreeInfo& info);
John Reckaa6e84f2016-06-16 15:36:13 -0700173 void destroyHardwareResources(TreeObserver* observer, TreeInfo* info = nullptr);
John Recke45b1fd2014-04-15 09:50:16 -0700174
175 // UI thread only!
John Reck68bfe0a2014-06-24 15:34:58 -0700176 ANDROID_API void addAnimator(const sp<BaseRenderNodeAnimator>& animator);
Doris Liu8b083202016-02-19 21:46:06 +0000177 void removeAnimator(const sp<BaseRenderNodeAnimator>& animator);
178
179 // This can only happen during pushStaging()
180 void onAnimatorTargetChanged(BaseRenderNodeAnimator* animator) {
181 mAnimatorManager.onAnimatorTargetChanged(animator);
182 }
John Reck668f0e32014-03-26 15:10:40 -0700183
John Reck119907c2014-08-14 09:02:01 -0700184 AnimatorManager& animators() { return mAnimatorManager; }
185
Chris Craik69e5adf2014-08-14 13:34:01 -0700186 void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false) const;
187
Chris Craikb565df12015-10-05 13:00:52 -0700188 bool nothingToDraw() const {
189 const Outline& outline = properties().getOutline();
Chris Craik003cc3d2015-10-16 10:24:55 -0700190 return mDisplayList == nullptr
Chris Craikb565df12015-10-05 13:00:52 -0700191 || properties().getAlpha() <= 0
192 || (outline.getShouldClip() && outline.isEmpty())
193 || properties().getScaleX() == 0
194 || properties().getScaleY() == 0;
195 }
196
Chris Craik0b7e8242015-10-28 16:50:44 -0700197 const DisplayList* getDisplayList() const {
198 return mDisplayList;
Chris Craikb565df12015-10-05 13:00:52 -0700199 }
Chris Craik0b7e8242015-10-28 16:50:44 -0700200 OffscreenBuffer* getLayer() const { return mLayer; }
201 OffscreenBuffer** getLayerHandle() { return &mLayer; } // ugh...
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400202 void setLayer(OffscreenBuffer* layer) { mLayer = layer; }
Chris Craikb565df12015-10-05 13:00:52 -0700203
John Reckaa6e84f2016-06-16 15:36:13 -0700204 // Note: The position callbacks are relying on the listener using
205 // the frameNumber to appropriately batch/synchronize these transactions.
206 // There is no other filtering/batching to ensure that only the "final"
207 // state called once per frame.
John Reck7b570de2016-06-27 13:27:23 -0700208 class ANDROID_API PositionListener : public VirtualLightRefBase {
John Reckf6481082016-02-02 15:18:23 -0800209 public:
210 virtual ~PositionListener() {}
John Reckaa6e84f2016-06-16 15:36:13 -0700211 // Called when the RenderNode's position changes
John Reckf6481082016-02-02 15:18:23 -0800212 virtual void onPositionUpdated(RenderNode& node, const TreeInfo& info) = 0;
John Reckaa6e84f2016-06-16 15:36:13 -0700213 // Called when the RenderNode no longer has a position. As in, it's
214 // no longer being drawn.
215 // Note, tree info might be null
216 virtual void onPositionLost(RenderNode& node, const TreeInfo* info) = 0;
John Reckf6481082016-02-02 15:18:23 -0800217 };
218
219 // Note this is not thread safe, this needs to be called
220 // before the RenderNode is used for drawing.
221 // RenderNode takes ownership of the pointer
222 ANDROID_API void setPositionListener(PositionListener* listener) {
John Reck7b570de2016-06-27 13:27:23 -0700223 mPositionListener = listener;
John Reckf6481082016-02-02 15:18:23 -0800224 }
225
John Reck44b49f02016-03-25 14:29:48 -0700226 // This is only modified in MODE_FULL, so it can be safely accessed
227 // on the UI thread.
228 ANDROID_API bool hasParents() {
229 return mParentCount;
230 }
231
John Reck113e0822014-03-18 09:22:59 -0700232private:
Chris Craik5e00c7c2016-07-06 16:10:09 -0700233 void computeOrderingImpl(RenderNodeOp* opState,
234 std::vector<RenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700235 const mat4* transformFromProjectionSurface);
236
Chris Craikb565df12015-10-05 13:00:52 -0700237 void syncProperties();
John Reckaa6e84f2016-06-16 15:36:13 -0700238 void syncDisplayList(TreeInfo* info);
Chris Craikb565df12015-10-05 13:00:52 -0700239
Chris Craika766cb22015-06-08 16:49:43 -0700240 void prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer);
John Reck25fbb3f2014-06-12 13:46:45 -0700241 void pushStagingPropertiesChanges(TreeInfo& info);
242 void pushStagingDisplayListChanges(TreeInfo& info);
Chris Craik003cc3d2015-10-16 10:24:55 -0700243 void prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayList* subtree);
John Recka7c2ea22014-08-08 13:21:00 -0700244 void prepareLayer(TreeInfo& info, uint32_t dirtyMask);
John Reck25fbb3f2014-06-12 13:46:45 -0700245 void pushLayerUpdate(TreeInfo& info);
John Reckaa6e84f2016-06-16 15:36:13 -0700246 void deleteDisplayList(TreeObserver* observer, TreeInfo* info = nullptr);
John Reck0a973302014-07-16 13:29:45 -0700247 void damageSelf(TreeInfo& info);
John Reckdcba6722014-07-08 13:59:49 -0700248
249 void incParentRefCount() { mParentCount++; }
John Reckaa6e84f2016-06-16 15:36:13 -0700250 void decParentRefCount(TreeObserver* observer, TreeInfo* info = nullptr);
sergeyvc3849aa2016-08-08 13:22:06 -0700251 void output(std::ostream& output, uint32_t level);
John Reck8de65a82014-04-09 15:23:38 -0700252
John Reck113e0822014-03-18 09:22:59 -0700253 String8 mName;
John Reck44b49f02016-03-25 14:29:48 -0700254 sp<VirtualLightRefBase> mUserContext;
John Reck113e0822014-03-18 09:22:59 -0700255
John Reckff941dc2014-05-14 16:34:14 -0700256 uint32_t mDirtyPropertyFields;
John Reck113e0822014-03-18 09:22:59 -0700257 RenderProperties mProperties;
John Reckd0a0b2a2014-03-20 16:28:56 -0700258 RenderProperties mStagingProperties;
259
Chris Craik003cc3d2015-10-16 10:24:55 -0700260 bool mNeedsDisplayListSync;
261 // WARNING: Do not delete this directly, you must go through deleteDisplayList()!
262 DisplayList* mDisplayList;
263 DisplayList* mStagingDisplayList;
John Reck113e0822014-03-18 09:22:59 -0700264
John Reck68bfe0a2014-06-24 15:34:58 -0700265 friend class AnimatorManager;
266 AnimatorManager mAnimatorManager;
John Recke45b1fd2014-04-15 09:50:16 -0700267
John Reck25fbb3f2014-06-12 13:46:45 -0700268 // Owned by RT. Lifecycle is managed by prepareTree(), with the exception
269 // being in ~RenderNode() which may happen on any thread.
Chris Craik5e00c7c2016-07-06 16:10:09 -0700270 OffscreenBuffer* mLayer = nullptr;
John Reck25fbb3f2014-06-12 13:46:45 -0700271
John Reck113e0822014-03-18 09:22:59 -0700272 /**
273 * Draw time state - these properties are only set and used during rendering
274 */
275
276 // for projection surfaces, contains a list of all children items
Chris Craik5e00c7c2016-07-06 16:10:09 -0700277 std::vector<RenderNodeOp*> mProjectedNodes;
John Reckdcba6722014-07-08 13:59:49 -0700278
279 // How many references our parent(s) have to us. Typically this should alternate
280 // between 2 and 1 (when a staging push happens we inc first then dec)
281 // When this hits 0 we are no longer in the tree, so any hardware resources
282 // (specifically Layers) should be released.
283 // This is *NOT* thread-safe, and should therefore only be tracking
Chris Craik003cc3d2015-10-16 10:24:55 -0700284 // mDisplayList, not mStagingDisplayList.
John Reckdcba6722014-07-08 13:59:49 -0700285 uint32_t mParentCount;
John Reckf6481082016-02-02 15:18:23 -0800286
John Reck7b570de2016-06-27 13:27:23 -0700287 sp<PositionListener> mPositionListener;
John Reck113e0822014-03-18 09:22:59 -0700288}; // class RenderNode
289
290} /* namespace uirenderer */
291} /* namespace android */