blob: a05c744c4a562c32a5220077ba7d0b44b047a749 [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"
Derek Sollenberger0df62092016-09-27 16:04:42 -040035#include "SkiaDisplayList.h"
John Reck113e0822014-03-18 09:22:59 -070036
John Reck272a6852015-07-29 16:48:58 -070037#include <vector>
38
John Reck113e0822014-03-18 09:22:59 -070039class SkBitmap;
40class SkPaint;
41class SkPath;
42class SkRegion;
Derek Sollenberger0df62092016-09-27 16:04:42 -040043class SkSurface;
John Reck113e0822014-03-18 09:22:59 -070044
45namespace android {
46namespace uirenderer {
47
Chris Craikb565df12015-10-05 13:00:52 -070048class CanvasState;
Chris Craik0b7e8242015-10-28 16:50:44 -070049class DisplayListOp;
Chris Craikf158b492016-01-12 14:45:08 -080050class FrameBuilder;
Chris Craik0b7e8242015-10-28 16:50:44 -070051class OffscreenBuffer;
Chris Craik5e00c7c2016-07-06 16:10:09 -070052class Rect;
Derek Sollenberger0df62092016-09-27 16:04:42 -040053class SkiaDisplayList;
Chris Craik5e00c7c2016-07-06 16:10:09 -070054class SkiaShader;
Chris Craik8d1f2122015-11-24 16:40:09 -080055struct RenderNodeOp;
Chris Craik0b7e8242015-10-28 16:50:44 -070056
Tom Hudson2dc236b2014-10-15 15:46:42 -040057class TreeInfo;
John Reck44b49f02016-03-25 14:29:48 -070058class TreeObserver;
John Reck113e0822014-03-18 09:22:59 -070059
John Recke248bd12015-08-05 13:53:53 -070060namespace proto {
61class RenderNode;
62}
63
John Reck113e0822014-03-18 09:22:59 -070064/**
65 * Primary class for storing recorded canvas commands, as well as per-View/ViewGroup display properties.
66 *
67 * Recording of canvas commands is somewhat similar to SkPicture, except the canvas-recording
Chris Craik5e00c7c2016-07-06 16:10:09 -070068 * functionality is split between RecordingCanvas (which manages the recording), DisplayList
69 * (which holds the actual data), and RenderNode (which holds properties used for render playback).
John Reck113e0822014-03-18 09:22:59 -070070 *
Chris Craik003cc3d2015-10-16 10:24:55 -070071 * Note that DisplayList is swapped out from beneath an individual RenderNode when a view's
72 * recorded stream of canvas operations is refreshed. The RenderNode (and its properties) stay
John Reck113e0822014-03-18 09:22:59 -070073 * attached.
74 */
John Reck087bc0c2014-04-04 16:20:08 -070075class RenderNode : public VirtualLightRefBase {
Chris Craikb565df12015-10-05 13:00:52 -070076friend class TestUtils; // allow TestUtils to access syncDisplayList / syncProperties
Chris Craikf158b492016-01-12 14:45:08 -080077friend class FrameBuilder;
John Reck113e0822014-03-18 09:22:59 -070078public:
John Reckff941dc2014-05-14 16:34:14 -070079 enum DirtyPropertyMask {
80 GENERIC = 1 << 1,
81 TRANSLATION_X = 1 << 2,
82 TRANSLATION_Y = 1 << 3,
83 TRANSLATION_Z = 1 << 4,
84 SCALE_X = 1 << 5,
85 SCALE_Y = 1 << 6,
86 ROTATION = 1 << 7,
87 ROTATION_X = 1 << 8,
88 ROTATION_Y = 1 << 9,
89 X = 1 << 10,
90 Y = 1 << 11,
91 Z = 1 << 12,
92 ALPHA = 1 << 13,
John Recka7c2ea22014-08-08 13:21:00 -070093 DISPLAY_LIST = 1 << 14,
John Reckff941dc2014-05-14 16:34:14 -070094 };
95
John Reck113e0822014-03-18 09:22:59 -070096 ANDROID_API RenderNode();
John Recke45b1fd2014-04-15 09:50:16 -070097 ANDROID_API virtual ~RenderNode();
John Reck113e0822014-03-18 09:22:59 -070098
99 // See flags defined in DisplayList.java
100 enum ReplayFlag {
101 kReplayFlag_ClipChildren = 0x1
102 };
103
John Reck51f2d602016-04-06 07:50:47 -0700104 ANDROID_API void setStagingDisplayList(DisplayList* newData, TreeObserver* observer);
John Reck113e0822014-03-18 09:22:59 -0700105
106 void computeOrdering();
Chris Craikb265e2c2014-03-27 15:50:09 -0700107
sergeyvc3849aa2016-08-08 13:22:06 -0700108 ANDROID_API void output();
John Reckfe5e7b72014-05-23 17:42:28 -0700109 ANDROID_API int getDebugSize();
John Recke248bd12015-08-05 13:53:53 -0700110 void copyTo(proto::RenderNode* node);
John Reck113e0822014-03-18 09:22:59 -0700111
112 bool isRenderable() const {
Chris Craik003cc3d2015-10-16 10:24:55 -0700113 return mDisplayList && !mDisplayList->isEmpty();
John Reck113e0822014-03-18 09:22:59 -0700114 }
115
John Recka447d292014-06-11 18:39:44 -0700116 bool hasProjectionReceiver() const {
Chris Craik003cc3d2015-10-16 10:24:55 -0700117 return mDisplayList && mDisplayList->projectionReceiveIndex >= 0;
John Recka447d292014-06-11 18:39:44 -0700118 }
119
Chris Craikdefb7f32014-04-08 18:17:07 -0700120 const char* getName() const {
121 return mName.string();
122 }
123
John Reck113e0822014-03-18 09:22:59 -0700124 void setName(const char* name) {
125 if (name) {
Dan Austin71831a62016-03-24 12:03:42 -0700126 const char* lastPeriod = strrchr(name, '.');
John Reck113e0822014-03-18 09:22:59 -0700127 if (lastPeriod) {
128 mName.setTo(lastPeriod + 1);
129 } else {
130 mName.setTo(name);
131 }
132 }
133 }
134
John Reck44b49f02016-03-25 14:29:48 -0700135 VirtualLightRefBase* getUserContext() const {
136 return mUserContext.get();
137 }
138
139 void setUserContext(VirtualLightRefBase* context) {
140 mUserContext = context;
141 }
142
John Reckff941dc2014-05-14 16:34:14 -0700143 bool isPropertyFieldDirty(DirtyPropertyMask field) const {
144 return mDirtyPropertyFields & field;
145 }
146
147 void setPropertyFieldsDirty(uint32_t fields) {
148 mDirtyPropertyFields |= fields;
149 }
150
John Recke4267ea2014-06-03 15:53:15 -0700151 const RenderProperties& properties() const {
John Reck113e0822014-03-18 09:22:59 -0700152 return mProperties;
153 }
154
John Reck52244ff2014-05-01 21:27:37 -0700155 RenderProperties& animatorProperties() {
156 return mProperties;
157 }
158
John Reckd0a0b2a2014-03-20 16:28:56 -0700159 const RenderProperties& stagingProperties() {
160 return mStagingProperties;
161 }
162
163 RenderProperties& mutateStagingProperties() {
John Reckd0a0b2a2014-03-20 16:28:56 -0700164 return mStagingProperties;
165 }
166
Chris Craik76caecf2015-11-02 19:17:45 -0800167 int getWidth() const {
John Reck113e0822014-03-18 09:22:59 -0700168 return properties().getWidth();
169 }
170
Chris Craik76caecf2015-11-02 19:17:45 -0800171 int getHeight() const {
John Reck113e0822014-03-18 09:22:59 -0700172 return properties().getHeight();
173 }
174
John Recke45b1fd2014-04-15 09:50:16 -0700175 ANDROID_API virtual void prepareTree(TreeInfo& info);
John Reckaa6e84f2016-06-16 15:36:13 -0700176 void destroyHardwareResources(TreeObserver* observer, TreeInfo* info = nullptr);
John Recke45b1fd2014-04-15 09:50:16 -0700177
178 // UI thread only!
John Reck68bfe0a2014-06-24 15:34:58 -0700179 ANDROID_API void addAnimator(const sp<BaseRenderNodeAnimator>& animator);
Doris Liu8b083202016-02-19 21:46:06 +0000180 void removeAnimator(const sp<BaseRenderNodeAnimator>& animator);
181
182 // This can only happen during pushStaging()
183 void onAnimatorTargetChanged(BaseRenderNodeAnimator* animator) {
184 mAnimatorManager.onAnimatorTargetChanged(animator);
185 }
John Reck668f0e32014-03-26 15:10:40 -0700186
John Reck119907c2014-08-14 09:02:01 -0700187 AnimatorManager& animators() { return mAnimatorManager; }
188
Chris Craik69e5adf2014-08-14 13:34:01 -0700189 void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false) const;
190
Chris Craikb565df12015-10-05 13:00:52 -0700191 bool nothingToDraw() const {
192 const Outline& outline = properties().getOutline();
Chris Craik003cc3d2015-10-16 10:24:55 -0700193 return mDisplayList == nullptr
Chris Craikb565df12015-10-05 13:00:52 -0700194 || properties().getAlpha() <= 0
195 || (outline.getShouldClip() && outline.isEmpty())
196 || properties().getScaleX() == 0
197 || properties().getScaleY() == 0;
198 }
199
Chris Craik0b7e8242015-10-28 16:50:44 -0700200 const DisplayList* getDisplayList() const {
201 return mDisplayList;
Chris Craikb565df12015-10-05 13:00:52 -0700202 }
Chris Craik0b7e8242015-10-28 16:50:44 -0700203 OffscreenBuffer* getLayer() const { return mLayer; }
204 OffscreenBuffer** getLayerHandle() { return &mLayer; } // ugh...
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400205 void setLayer(OffscreenBuffer* layer) { mLayer = layer; }
Chris Craikb565df12015-10-05 13:00:52 -0700206
John Reckaa6e84f2016-06-16 15:36:13 -0700207 // Note: The position callbacks are relying on the listener using
208 // the frameNumber to appropriately batch/synchronize these transactions.
209 // There is no other filtering/batching to ensure that only the "final"
210 // state called once per frame.
John Reck7b570de2016-06-27 13:27:23 -0700211 class ANDROID_API PositionListener : public VirtualLightRefBase {
John Reckf6481082016-02-02 15:18:23 -0800212 public:
213 virtual ~PositionListener() {}
John Reckaa6e84f2016-06-16 15:36:13 -0700214 // Called when the RenderNode's position changes
John Reckf6481082016-02-02 15:18:23 -0800215 virtual void onPositionUpdated(RenderNode& node, const TreeInfo& info) = 0;
John Reckaa6e84f2016-06-16 15:36:13 -0700216 // Called when the RenderNode no longer has a position. As in, it's
217 // no longer being drawn.
218 // Note, tree info might be null
219 virtual void onPositionLost(RenderNode& node, const TreeInfo* info) = 0;
John Reckf6481082016-02-02 15:18:23 -0800220 };
221
222 // Note this is not thread safe, this needs to be called
223 // before the RenderNode is used for drawing.
224 // RenderNode takes ownership of the pointer
225 ANDROID_API void setPositionListener(PositionListener* listener) {
John Reck7b570de2016-06-27 13:27:23 -0700226 mPositionListener = listener;
John Reckf6481082016-02-02 15:18:23 -0800227 }
228
John Reck44b49f02016-03-25 14:29:48 -0700229 // This is only modified in MODE_FULL, so it can be safely accessed
230 // on the UI thread.
231 ANDROID_API bool hasParents() {
232 return mParentCount;
233 }
234
John Reck113e0822014-03-18 09:22:59 -0700235private:
Chris Craik5e00c7c2016-07-06 16:10:09 -0700236 void computeOrderingImpl(RenderNodeOp* opState,
237 std::vector<RenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700238 const mat4* transformFromProjectionSurface);
239
Chris Craikb565df12015-10-05 13:00:52 -0700240 void syncProperties();
John Reckaa6e84f2016-06-16 15:36:13 -0700241 void syncDisplayList(TreeInfo* info);
Chris Craikb565df12015-10-05 13:00:52 -0700242
Chris Craika766cb22015-06-08 16:49:43 -0700243 void prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer);
John Reck25fbb3f2014-06-12 13:46:45 -0700244 void pushStagingPropertiesChanges(TreeInfo& info);
245 void pushStagingDisplayListChanges(TreeInfo& info);
John Recka7c2ea22014-08-08 13:21:00 -0700246 void prepareLayer(TreeInfo& info, uint32_t dirtyMask);
John Reck25fbb3f2014-06-12 13:46:45 -0700247 void pushLayerUpdate(TreeInfo& info);
John Reckaa6e84f2016-06-16 15:36:13 -0700248 void deleteDisplayList(TreeObserver* observer, TreeInfo* info = nullptr);
John Reck0a973302014-07-16 13:29:45 -0700249 void damageSelf(TreeInfo& info);
John Reckdcba6722014-07-08 13:59:49 -0700250
251 void incParentRefCount() { mParentCount++; }
John Reckaa6e84f2016-06-16 15:36:13 -0700252 void decParentRefCount(TreeObserver* observer, TreeInfo* info = nullptr);
sergeyvc3849aa2016-08-08 13:22:06 -0700253 void output(std::ostream& output, uint32_t level);
John Reck8de65a82014-04-09 15:23:38 -0700254
John Reck113e0822014-03-18 09:22:59 -0700255 String8 mName;
John Reck44b49f02016-03-25 14:29:48 -0700256 sp<VirtualLightRefBase> mUserContext;
John Reck113e0822014-03-18 09:22:59 -0700257
John Reckff941dc2014-05-14 16:34:14 -0700258 uint32_t mDirtyPropertyFields;
John Reck113e0822014-03-18 09:22:59 -0700259 RenderProperties mProperties;
John Reckd0a0b2a2014-03-20 16:28:56 -0700260 RenderProperties mStagingProperties;
261
Chris Craik003cc3d2015-10-16 10:24:55 -0700262 bool mNeedsDisplayListSync;
263 // WARNING: Do not delete this directly, you must go through deleteDisplayList()!
264 DisplayList* mDisplayList;
265 DisplayList* mStagingDisplayList;
John Reck113e0822014-03-18 09:22:59 -0700266
John Reck68bfe0a2014-06-24 15:34:58 -0700267 friend class AnimatorManager;
268 AnimatorManager mAnimatorManager;
John Recke45b1fd2014-04-15 09:50:16 -0700269
John Reck25fbb3f2014-06-12 13:46:45 -0700270 // Owned by RT. Lifecycle is managed by prepareTree(), with the exception
271 // being in ~RenderNode() which may happen on any thread.
Chris Craik5e00c7c2016-07-06 16:10:09 -0700272 OffscreenBuffer* mLayer = nullptr;
John Reck25fbb3f2014-06-12 13:46:45 -0700273
John Reck113e0822014-03-18 09:22:59 -0700274 /**
275 * Draw time state - these properties are only set and used during rendering
276 */
277
278 // for projection surfaces, contains a list of all children items
Chris Craik5e00c7c2016-07-06 16:10:09 -0700279 std::vector<RenderNodeOp*> mProjectedNodes;
John Reckdcba6722014-07-08 13:59:49 -0700280
281 // How many references our parent(s) have to us. Typically this should alternate
282 // between 2 and 1 (when a staging push happens we inc first then dec)
283 // When this hits 0 we are no longer in the tree, so any hardware resources
284 // (specifically Layers) should be released.
285 // This is *NOT* thread-safe, and should therefore only be tracking
Chris Craik003cc3d2015-10-16 10:24:55 -0700286 // mDisplayList, not mStagingDisplayList.
John Reckdcba6722014-07-08 13:59:49 -0700287 uint32_t mParentCount;
John Reckf6481082016-02-02 15:18:23 -0800288
John Reck7b570de2016-06-27 13:27:23 -0700289 sp<PositionListener> mPositionListener;
Derek Sollenberger0df62092016-09-27 16:04:42 -0400290
291// METHODS & FIELDS ONLY USED BY THE SKIA RENDERER
292public:
293 /**
294 * Detach and transfer ownership of an already allocated displayList for use
295 * in recording updated content for this renderNode
296 */
297 std::unique_ptr<SkiaDisplayList> detachAvailableList() {
298 return std::move(mAvailableDisplayList);
299 }
300
301 /**
302 * Attach unused displayList to this node for potential future reuse.
303 */
304 void attachAvailableList(SkiaDisplayList* skiaDisplayList) {
305 mAvailableDisplayList.reset(skiaDisplayList);
306 }
307
308 /**
309 * Returns true if an offscreen layer from any renderPipeline is attached
310 * to this node.
311 */
312 bool hasLayer() const { return mLayer || mLayerSurface.get(); }
313
314 /**
315 * Used by the RenderPipeline to attach an offscreen surface to the RenderNode.
316 * The surface is then will be used to store the contents of a layer.
317 */
318 void setLayerSurface(sk_sp<SkSurface> layer) { mLayerSurface = layer; }
319
320
321 /**
322 * If the RenderNode is of type LayerType::RenderLayer then this method will
323 * return the an offscreen rendering surface that is used to both render into
324 * the layer and composite the layer into its parent. If the type is not
325 * LayerType::RenderLayer then it will return a nullptr.
326 *
327 * NOTE: this function is only guaranteed to return accurate results after
328 * prepareTree has been run for this RenderNode
329 */
330 SkSurface* getLayerSurface() const { return mLayerSurface.get(); }
331
332private:
333 /**
334 * If this RenderNode has been used in a previous frame then the SkiaDisplayList
335 * from that frame is cached here until one of the following conditions is met:
336 * 1) The RenderNode is deleted (causing this to be deleted)
337 * 2) It is replaced with the displayList from the next completed frame
338 * 3) It is detached and used to to record a new displayList for a later frame
339 */
340 std::unique_ptr<SkiaDisplayList> mAvailableDisplayList;
341
342 /**
343 * An offscreen rendering target used to contain the contents this RenderNode
344 * when it has been set to draw as a LayerType::RenderLayer.
345 */
346 sk_sp<SkSurface> mLayerSurface;
John Reck113e0822014-03-18 09:22:59 -0700347}; // class RenderNode
348
349} /* namespace uirenderer */
350} /* namespace android */