blob: dc962f307903a8167238c044e7b5f9f063d1a8d3 [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"
Stan Iliev021693b2016-10-17 16:26:15 -040035#include "pipeline/skia/SkiaDisplayList.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040036#include "pipeline/skia/SkiaLayer.h"
John Reck2de950d2017-01-25 10:58:30 -080037#include "utils/FatVector.h"
John Reck113e0822014-03-18 09:22:59 -070038
John Reck272a6852015-07-29 16:48:58 -070039#include <vector>
40
John Reck113e0822014-03-18 09:22:59 -070041class SkBitmap;
42class SkPaint;
43class SkPath;
44class SkRegion;
Derek Sollenberger0df62092016-09-27 16:04:42 -040045class SkSurface;
John Reck113e0822014-03-18 09:22:59 -070046
47namespace android {
48namespace uirenderer {
49
Chris Craikb565df12015-10-05 13:00:52 -070050class CanvasState;
Chris Craik5e00c7c2016-07-06 16:10:09 -070051class Rect;
52class SkiaShader;
Chris Craik8d1f2122015-11-24 16:40:09 -080053struct RenderNodeOp;
Chris Craik0b7e8242015-10-28 16:50:44 -070054
Tom Hudson2dc236b2014-10-15 15:46:42 -040055class TreeInfo;
John Reck44b49f02016-03-25 14:29:48 -070056class TreeObserver;
John Reck113e0822014-03-18 09:22:59 -070057
John Recke248bd12015-08-05 13:53:53 -070058namespace proto {
59class RenderNode;
60}
61
John Reck113e0822014-03-18 09:22:59 -070062/**
John Reck1bcacfd2017-11-03 10:12:19 -070063 * Primary class for storing recorded canvas commands, as well as per-View/ViewGroup display
64 * properties.
John Reck113e0822014-03-18 09:22:59 -070065 *
66 * Recording of canvas commands is somewhat similar to SkPicture, except the canvas-recording
Chris Craik5e00c7c2016-07-06 16:10:09 -070067 * functionality is split between RecordingCanvas (which manages the recording), DisplayList
68 * (which holds the actual data), and RenderNode (which holds properties used for render playback).
John Reck113e0822014-03-18 09:22:59 -070069 *
Chris Craik003cc3d2015-10-16 10:24:55 -070070 * Note that DisplayList is swapped out from beneath an individual RenderNode when a view's
71 * recorded stream of canvas operations is refreshed. The RenderNode (and its properties) stay
John Reck113e0822014-03-18 09:22:59 -070072 * attached.
73 */
John Reck087bc0c2014-04-04 16:20:08 -070074class RenderNode : public VirtualLightRefBase {
John Reck1bcacfd2017-11-03 10:12:19 -070075 friend class TestUtils; // allow TestUtils to access syncDisplayList / syncProperties
John Reck1bcacfd2017-11-03 10:12:19 -070076
John Reck113e0822014-03-18 09:22:59 -070077public:
John Reckff941dc2014-05-14 16:34:14 -070078 enum DirtyPropertyMask {
John Reck1bcacfd2017-11-03 10:12:19 -070079 GENERIC = 1 << 1,
80 TRANSLATION_X = 1 << 2,
81 TRANSLATION_Y = 1 << 3,
82 TRANSLATION_Z = 1 << 4,
83 SCALE_X = 1 << 5,
84 SCALE_Y = 1 << 6,
85 ROTATION = 1 << 7,
86 ROTATION_X = 1 << 8,
87 ROTATION_Y = 1 << 9,
88 X = 1 << 10,
89 Y = 1 << 11,
90 Z = 1 << 12,
91 ALPHA = 1 << 13,
92 DISPLAY_LIST = 1 << 14,
John Reckff941dc2014-05-14 16:34:14 -070093 };
94
John Reck113e0822014-03-18 09:22:59 -070095 ANDROID_API RenderNode();
John Recke45b1fd2014-04-15 09:50:16 -070096 ANDROID_API virtual ~RenderNode();
John Reck113e0822014-03-18 09:22:59 -070097
98 // See flags defined in DisplayList.java
John Reck1bcacfd2017-11-03 10:12:19 -070099 enum ReplayFlag { kReplayFlag_ClipChildren = 0x1 };
John Reck113e0822014-03-18 09:22:59 -0700100
John Reck2de950d2017-01-25 10:58:30 -0800101 ANDROID_API void setStagingDisplayList(DisplayList* newData);
John Reck113e0822014-03-18 09:22:59 -0700102
sergeyvc3849aa2016-08-08 13:22:06 -0700103 ANDROID_API void output();
John Reckfe5e7b72014-05-23 17:42:28 -0700104 ANDROID_API int getDebugSize();
John Reck113e0822014-03-18 09:22:59 -0700105
John Reck1bcacfd2017-11-03 10:12:19 -0700106 bool isRenderable() const { return mDisplayList && !mDisplayList->isEmpty(); }
John Reck113e0822014-03-18 09:22:59 -0700107
John Recka447d292014-06-11 18:39:44 -0700108 bool hasProjectionReceiver() const {
Chris Craik003cc3d2015-10-16 10:24:55 -0700109 return mDisplayList && mDisplayList->projectionReceiveIndex >= 0;
John Recka447d292014-06-11 18:39:44 -0700110 }
111
John Reck1bcacfd2017-11-03 10:12:19 -0700112 const char* getName() const { return mName.string(); }
Chris Craikdefb7f32014-04-08 18:17:07 -0700113
John Reck113e0822014-03-18 09:22:59 -0700114 void setName(const char* name) {
115 if (name) {
Dan Austin71831a62016-03-24 12:03:42 -0700116 const char* lastPeriod = strrchr(name, '.');
John Reck113e0822014-03-18 09:22:59 -0700117 if (lastPeriod) {
118 mName.setTo(lastPeriod + 1);
119 } else {
120 mName.setTo(name);
121 }
122 }
123 }
124
John Reck1bcacfd2017-11-03 10:12:19 -0700125 VirtualLightRefBase* getUserContext() const { return mUserContext.get(); }
John Reck44b49f02016-03-25 14:29:48 -0700126
John Reck1bcacfd2017-11-03 10:12:19 -0700127 void setUserContext(VirtualLightRefBase* context) { mUserContext = context; }
John Reck44b49f02016-03-25 14:29:48 -0700128
John Reckff941dc2014-05-14 16:34:14 -0700129 bool isPropertyFieldDirty(DirtyPropertyMask field) const {
130 return mDirtyPropertyFields & field;
131 }
132
John Reck1bcacfd2017-11-03 10:12:19 -0700133 void setPropertyFieldsDirty(uint32_t fields) { mDirtyPropertyFields |= fields; }
John Reckff941dc2014-05-14 16:34:14 -0700134
John Reck1bcacfd2017-11-03 10:12:19 -0700135 const RenderProperties& properties() const { return mProperties; }
John Reck113e0822014-03-18 09:22:59 -0700136
John Reck1bcacfd2017-11-03 10:12:19 -0700137 RenderProperties& animatorProperties() { return mProperties; }
John Reck52244ff2014-05-01 21:27:37 -0700138
John Reck1bcacfd2017-11-03 10:12:19 -0700139 const RenderProperties& stagingProperties() { return mStagingProperties; }
John Reckd0a0b2a2014-03-20 16:28:56 -0700140
John Reck1bcacfd2017-11-03 10:12:19 -0700141 RenderProperties& mutateStagingProperties() { return mStagingProperties; }
John Reckd0a0b2a2014-03-20 16:28:56 -0700142
John Reck1bcacfd2017-11-03 10:12:19 -0700143 bool isValid() { return mValid; }
John Reck2de950d2017-01-25 10:58:30 -0800144
John Reck1bcacfd2017-11-03 10:12:19 -0700145 int getWidth() const { return properties().getWidth(); }
John Reck113e0822014-03-18 09:22:59 -0700146
John Reck1bcacfd2017-11-03 10:12:19 -0700147 int getHeight() const { return properties().getHeight(); }
John Reck113e0822014-03-18 09:22:59 -0700148
John Recke45b1fd2014-04-15 09:50:16 -0700149 ANDROID_API virtual void prepareTree(TreeInfo& info);
John Reck2de950d2017-01-25 10:58:30 -0800150 void destroyHardwareResources(TreeInfo* info = nullptr);
151 void destroyLayers();
John Recke45b1fd2014-04-15 09:50:16 -0700152
153 // UI thread only!
John Reck68bfe0a2014-06-24 15:34:58 -0700154 ANDROID_API void addAnimator(const sp<BaseRenderNodeAnimator>& animator);
Doris Liu8b083202016-02-19 21:46:06 +0000155 void removeAnimator(const sp<BaseRenderNodeAnimator>& animator);
156
157 // This can only happen during pushStaging()
158 void onAnimatorTargetChanged(BaseRenderNodeAnimator* animator) {
159 mAnimatorManager.onAnimatorTargetChanged(animator);
160 }
John Reck668f0e32014-03-26 15:10:40 -0700161
John Reck119907c2014-08-14 09:02:01 -0700162 AnimatorManager& animators() { return mAnimatorManager; }
163
Chris Craik69e5adf2014-08-14 13:34:01 -0700164 void applyViewPropertyTransforms(mat4& matrix, bool true3dTransform = false) const;
165
Chris Craikb565df12015-10-05 13:00:52 -0700166 bool nothingToDraw() const {
167 const Outline& outline = properties().getOutline();
John Reck1bcacfd2017-11-03 10:12:19 -0700168 return mDisplayList == nullptr || properties().getAlpha() <= 0 ||
169 (outline.getShouldClip() && outline.isEmpty()) || properties().getScaleX() == 0 ||
170 properties().getScaleY() == 0;
Chris Craikb565df12015-10-05 13:00:52 -0700171 }
172
John Reck1bcacfd2017-11-03 10:12:19 -0700173 const DisplayList* getDisplayList() const { return mDisplayList; }
Chris Craikb565df12015-10-05 13:00:52 -0700174
John Reckaa6e84f2016-06-16 15:36:13 -0700175 // Note: The position callbacks are relying on the listener using
176 // the frameNumber to appropriately batch/synchronize these transactions.
177 // There is no other filtering/batching to ensure that only the "final"
178 // state called once per frame.
John Reck7b570de2016-06-27 13:27:23 -0700179 class ANDROID_API PositionListener : public VirtualLightRefBase {
John Reckf6481082016-02-02 15:18:23 -0800180 public:
181 virtual ~PositionListener() {}
John Reckaa6e84f2016-06-16 15:36:13 -0700182 // Called when the RenderNode's position changes
John Reckf6481082016-02-02 15:18:23 -0800183 virtual void onPositionUpdated(RenderNode& node, const TreeInfo& info) = 0;
John Reckaa6e84f2016-06-16 15:36:13 -0700184 // Called when the RenderNode no longer has a position. As in, it's
185 // no longer being drawn.
186 // Note, tree info might be null
187 virtual void onPositionLost(RenderNode& node, const TreeInfo* info) = 0;
John Reckf6481082016-02-02 15:18:23 -0800188 };
189
190 // Note this is not thread safe, this needs to be called
191 // before the RenderNode is used for drawing.
192 // RenderNode takes ownership of the pointer
193 ANDROID_API void setPositionListener(PositionListener* listener) {
John Reck7b570de2016-06-27 13:27:23 -0700194 mPositionListener = listener;
John Reckf6481082016-02-02 15:18:23 -0800195 }
196
John Reck44b49f02016-03-25 14:29:48 -0700197 // This is only modified in MODE_FULL, so it can be safely accessed
198 // on the UI thread.
John Reck1bcacfd2017-11-03 10:12:19 -0700199 ANDROID_API bool hasParents() { return mParentCount; }
John Reck44b49f02016-03-25 14:29:48 -0700200
John Reck2de950d2017-01-25 10:58:30 -0800201 void onRemovedFromTree(TreeInfo* info);
202
203 // Called by CanvasContext to promote a RenderNode to be a root node
John Reck1bcacfd2017-11-03 10:12:19 -0700204 void makeRoot() { incParentRefCount(); }
John Reck2de950d2017-01-25 10:58:30 -0800205
206 // Called by CanvasContext when it drops a RenderNode from being a root node
207 void clearRoot();
208
Stan Ilievd2172372017-02-09 16:59:27 -0500209 void output(std::ostream& output, uint32_t level);
210
John Reck113e0822014-03-18 09:22:59 -0700211private:
Chris Craik5e00c7c2016-07-06 16:10:09 -0700212 void computeOrderingImpl(RenderNodeOp* opState,
John Reck1bcacfd2017-11-03 10:12:19 -0700213 std::vector<RenderNodeOp*>* compositedChildrenOfProjectionSurface,
214 const mat4* transformFromProjectionSurface);
John Reck113e0822014-03-18 09:22:59 -0700215
Chris Craikb565df12015-10-05 13:00:52 -0700216 void syncProperties();
John Reck2de950d2017-01-25 10:58:30 -0800217 void syncDisplayList(TreeObserver& observer, TreeInfo* info);
Chris Craikb565df12015-10-05 13:00:52 -0700218
John Reck2de950d2017-01-25 10:58:30 -0800219 void prepareTreeImpl(TreeObserver& observer, TreeInfo& info, bool functorsNeedLayer);
John Reck25fbb3f2014-06-12 13:46:45 -0700220 void pushStagingPropertiesChanges(TreeInfo& info);
John Reck2de950d2017-01-25 10:58:30 -0800221 void pushStagingDisplayListChanges(TreeObserver& observer, TreeInfo& info);
John Recka7c2ea22014-08-08 13:21:00 -0700222 void prepareLayer(TreeInfo& info, uint32_t dirtyMask);
John Reck25fbb3f2014-06-12 13:46:45 -0700223 void pushLayerUpdate(TreeInfo& info);
John Reck2de950d2017-01-25 10:58:30 -0800224 void deleteDisplayList(TreeObserver& observer, TreeInfo* info = nullptr);
John Reck0a973302014-07-16 13:29:45 -0700225 void damageSelf(TreeInfo& info);
John Reckdcba6722014-07-08 13:59:49 -0700226
227 void incParentRefCount() { mParentCount++; }
John Reck2de950d2017-01-25 10:58:30 -0800228 void decParentRefCount(TreeObserver& observer, TreeInfo* info = nullptr);
John Reck8de65a82014-04-09 15:23:38 -0700229
John Reck113e0822014-03-18 09:22:59 -0700230 String8 mName;
John Reck44b49f02016-03-25 14:29:48 -0700231 sp<VirtualLightRefBase> mUserContext;
John Reck113e0822014-03-18 09:22:59 -0700232
John Reckff941dc2014-05-14 16:34:14 -0700233 uint32_t mDirtyPropertyFields;
John Reck113e0822014-03-18 09:22:59 -0700234 RenderProperties mProperties;
John Reckd0a0b2a2014-03-20 16:28:56 -0700235 RenderProperties mStagingProperties;
236
John Reck2de950d2017-01-25 10:58:30 -0800237 // Owned by UI. Set when DL is set, cleared when DL cleared or when node detached
238 // (likely by parent re-record/removal)
239 bool mValid = false;
240
Chris Craik003cc3d2015-10-16 10:24:55 -0700241 bool mNeedsDisplayListSync;
242 // WARNING: Do not delete this directly, you must go through deleteDisplayList()!
243 DisplayList* mDisplayList;
244 DisplayList* mStagingDisplayList;
John Reck113e0822014-03-18 09:22:59 -0700245
John Reck68bfe0a2014-06-24 15:34:58 -0700246 friend class AnimatorManager;
247 AnimatorManager mAnimatorManager;
John Recke45b1fd2014-04-15 09:50:16 -0700248
John Reck113e0822014-03-18 09:22:59 -0700249 /**
250 * Draw time state - these properties are only set and used during rendering
251 */
252
253 // for projection surfaces, contains a list of all children items
Chris Craik5e00c7c2016-07-06 16:10:09 -0700254 std::vector<RenderNodeOp*> mProjectedNodes;
John Reckdcba6722014-07-08 13:59:49 -0700255
256 // How many references our parent(s) have to us. Typically this should alternate
257 // between 2 and 1 (when a staging push happens we inc first then dec)
258 // When this hits 0 we are no longer in the tree, so any hardware resources
259 // (specifically Layers) should be released.
260 // This is *NOT* thread-safe, and should therefore only be tracking
Chris Craik003cc3d2015-10-16 10:24:55 -0700261 // mDisplayList, not mStagingDisplayList.
John Reckdcba6722014-07-08 13:59:49 -0700262 uint32_t mParentCount;
John Reckf6481082016-02-02 15:18:23 -0800263
John Reck7b570de2016-06-27 13:27:23 -0700264 sp<PositionListener> mPositionListener;
Derek Sollenberger0df62092016-09-27 16:04:42 -0400265
John Reck1bcacfd2017-11-03 10:12:19 -0700266 // METHODS & FIELDS ONLY USED BY THE SKIA RENDERER
Derek Sollenberger0df62092016-09-27 16:04:42 -0400267public:
268 /**
269 * Detach and transfer ownership of an already allocated displayList for use
270 * in recording updated content for this renderNode
271 */
Stan Iliev021693b2016-10-17 16:26:15 -0400272 std::unique_ptr<skiapipeline::SkiaDisplayList> detachAvailableList() {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400273 return std::move(mAvailableDisplayList);
274 }
275
276 /**
277 * Attach unused displayList to this node for potential future reuse.
278 */
Stan Iliev021693b2016-10-17 16:26:15 -0400279 void attachAvailableList(skiapipeline::SkiaDisplayList* skiaDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400280 mAvailableDisplayList.reset(skiaDisplayList);
281 }
282
283 /**
284 * Returns true if an offscreen layer from any renderPipeline is attached
285 * to this node.
286 */
John Recke4c1e6c2018-05-24 16:27:35 -0700287 bool hasLayer() const { return mSkiaLayer.get(); }
Derek Sollenberger0df62092016-09-27 16:04:42 -0400288
289 /**
290 * Used by the RenderPipeline to attach an offscreen surface to the RenderNode.
291 * The surface is then will be used to store the contents of a layer.
292 */
Stan Iliev500a0c32016-10-26 10:30:09 -0400293 void setLayerSurface(sk_sp<SkSurface> layer) {
294 if (layer.get()) {
295 if (!mSkiaLayer.get()) {
296 mSkiaLayer = std::make_unique<skiapipeline::SkiaLayer>();
297 }
298 mSkiaLayer->layerSurface = std::move(layer);
299 mSkiaLayer->inverseTransformInWindow.loadIdentity();
300 } else {
301 mSkiaLayer.reset();
302 }
303 }
Derek Sollenberger0df62092016-09-27 16:04:42 -0400304
305 /**
306 * If the RenderNode is of type LayerType::RenderLayer then this method will
307 * return the an offscreen rendering surface that is used to both render into
308 * the layer and composite the layer into its parent. If the type is not
309 * LayerType::RenderLayer then it will return a nullptr.
310 *
311 * NOTE: this function is only guaranteed to return accurate results after
312 * prepareTree has been run for this RenderNode
313 */
Stan Iliev500a0c32016-10-26 10:30:09 -0400314 SkSurface* getLayerSurface() const {
315 return mSkiaLayer.get() ? mSkiaLayer->layerSurface.get() : nullptr;
316 }
317
John Reck1bcacfd2017-11-03 10:12:19 -0700318 skiapipeline::SkiaLayer* getSkiaLayer() const { return mSkiaLayer.get(); }
Derek Sollenberger0df62092016-09-27 16:04:42 -0400319
Derek Sollenberger579317d42017-08-29 16:33:49 -0400320 /**
321 * Returns the path that represents the outline of RenderNode intersected with
322 * the provided rect. This call will internally cache the resulting path in
323 * order to potentially return that path for subsequent calls to this method.
324 * By reusing the same path we get better performance on the GPU backends since
325 * those resources are cached in the hardware based on the path's genID.
326 *
327 * The returned path is only guaranteed to be valid until this function is called
328 * again or the RenderNode's outline is mutated.
329 */
330 const SkPath* getClippedOutline(const SkRect& clipRect) const;
John Reck1bcacfd2017-11-03 10:12:19 -0700331
Derek Sollenberger0df62092016-09-27 16:04:42 -0400332private:
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 */
Stan Iliev021693b2016-10-17 16:26:15 -0400340 std::unique_ptr<skiapipeline::SkiaDisplayList> mAvailableDisplayList;
Derek Sollenberger0df62092016-09-27 16:04:42 -0400341
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 */
Stan Iliev500a0c32016-10-26 10:30:09 -0400346 std::unique_ptr<skiapipeline::SkiaLayer> mSkiaLayer;
Derek Sollenberger579317d42017-08-29 16:33:49 -0400347
348 struct ClippedOutlineCache {
349 // keys
350 uint32_t outlineID = 0;
351 SkRect clipRect;
352
353 // value
354 SkPath clippedOutline;
355 };
356 mutable ClippedOutlineCache mClippedOutlineCache;
John Reck1bcacfd2017-11-03 10:12:19 -0700357}; // class RenderNode
John Reck113e0822014-03-18 09:22:59 -0700358
John Reck2de950d2017-01-25 10:58:30 -0800359class MarkAndSweepRemoved : public TreeObserver {
John Reck1bcacfd2017-11-03 10:12:19 -0700360 PREVENT_COPY_AND_ASSIGN(MarkAndSweepRemoved);
John Reck2de950d2017-01-25 10:58:30 -0800361
362public:
363 explicit MarkAndSweepRemoved(TreeInfo* info) : mTreeInfo(info) {}
364
John Reck1bcacfd2017-11-03 10:12:19 -0700365 void onMaybeRemovedFromTree(RenderNode* node) override { mMarked.emplace_back(node); }
John Reck2de950d2017-01-25 10:58:30 -0800366
367 ~MarkAndSweepRemoved() {
368 for (auto& node : mMarked) {
369 if (!node->hasParents()) {
370 node->onRemovedFromTree(mTreeInfo);
371 }
372 }
373 }
374
375private:
376 FatVector<sp<RenderNode>, 10> mMarked;
377 TreeInfo* mTreeInfo;
378};
379
John Reck113e0822014-03-18 09:22:59 -0700380} /* namespace uirenderer */
381} /* namespace android */