blob: f5b2675c7fe6b2421374008398107d9cb87023e4 [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
John Reck113e0822014-03-18 09:22:59 -070017#include "RenderNode.h"
18
John Recke4267ea2014-06-03 15:53:15 -070019#include "DamageAccumulator.h"
John Reck113e0822014-03-18 09:22:59 -070020#include "Debug.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040021#include "TreeInfo.h"
John Reck1bcacfd2017-11-03 10:12:19 -070022#include "VectorDrawable.h"
Derek Sollenberger5d0ca632019-07-19 16:17:12 -040023#include "private/hwui/WebViewFunctor.h"
Fedor Kudasov86bd2142019-06-18 15:51:57 +010024#ifdef __ANDROID__
John Reck1bcacfd2017-11-03 10:12:19 -070025#include "renderthread/CanvasContext.h"
Fedor Kudasov86bd2142019-06-18 15:51:57 +010026#else
27#include "DamageAccumulator.h"
28#include "pipeline/skia/SkiaDisplayList.h"
29#endif
Chris Craike0bb87d2014-04-22 17:55:41 -070030#include "utils/MathUtils.h"
sergeyvc3849aa2016-08-08 13:22:06 -070031#include "utils/StringUtils.h"
Chris Craik70850ea2014-11-18 10:49:23 -080032#include "utils/TraceUtils.h"
John Reck113e0822014-03-18 09:22:59 -070033
Derek Sollenberger579317d42017-08-29 16:33:49 -040034#include <SkPathOps.h>
John Reck77c40102015-10-26 15:49:47 -070035#include <algorithm>
John Reckf96b2842018-11-29 09:44:10 -080036#include <atomic>
John Reck77c40102015-10-26 15:49:47 -070037#include <sstream>
38#include <string>
Jagadeesh Pakaravoorb624af32020-05-01 00:01:40 +000039#include <ui/FatVector.h>
John Reck77c40102015-10-26 15:49:47 -070040
John Reck113e0822014-03-18 09:22:59 -070041namespace android {
42namespace uirenderer {
43
John Reck2de950d2017-01-25 10:58:30 -080044// Used for tree mutations that are purely destructive.
45// Generic tree mutations should use MarkAndSweepObserver instead
46class ImmediateRemoved : public TreeObserver {
47public:
48 explicit ImmediateRemoved(TreeInfo* info) : mTreeInfo(info) {}
49
John Reck1bcacfd2017-11-03 10:12:19 -070050 void onMaybeRemovedFromTree(RenderNode* node) override { node->onRemovedFromTree(mTreeInfo); }
John Reck2de950d2017-01-25 10:58:30 -080051
52private:
53 TreeInfo* mTreeInfo;
54};
55
John Reckf96b2842018-11-29 09:44:10 -080056static int64_t generateId() {
57 static std::atomic<int64_t> sNextId{1};
58 return sNextId++;
59}
60
John Reck8de65a82014-04-09 15:23:38 -070061RenderNode::RenderNode()
John Reckf96b2842018-11-29 09:44:10 -080062 : mUniqueId(generateId())
63 , mDirtyPropertyFields(0)
Chris Craik003cc3d2015-10-16 10:24:55 -070064 , mNeedsDisplayListSync(false)
65 , mDisplayList(nullptr)
66 , mStagingDisplayList(nullptr)
John Reck68bfe0a2014-06-24 15:34:58 -070067 , mAnimatorManager(*this)
John Reck1bcacfd2017-11-03 10:12:19 -070068 , mParentCount(0) {}
John Reck113e0822014-03-18 09:22:59 -070069
70RenderNode::~RenderNode() {
John Reck2de950d2017-01-25 10:58:30 -080071 ImmediateRemoved observer(nullptr);
72 deleteDisplayList(observer);
Derek Sollenberger0df62092016-09-27 16:04:42 -040073 LOG_ALWAYS_FATAL_IF(hasLayer(), "layer missed detachment!");
John Reck113e0822014-03-18 09:22:59 -070074}
75
John Reckbe671952021-01-13 22:39:32 -050076void RenderNode::setStagingDisplayList(DisplayList&& newData) {
77 mValid = newData.isValid();
Chris Craik003cc3d2015-10-16 10:24:55 -070078 mNeedsDisplayListSync = true;
John Reckbe671952021-01-13 22:39:32 -050079 mStagingDisplayList = std::move(newData);
John Reck113e0822014-03-18 09:22:59 -070080}
81
John Reckdc1a6962021-01-12 13:56:07 -050082void RenderNode::discardStagingDisplayList() {
John Reckbe671952021-01-13 22:39:32 -050083 setStagingDisplayList(DisplayList());
John Reckdc1a6962021-01-12 13:56:07 -050084}
85
John Reck113e0822014-03-18 09:22:59 -070086/**
87 * This function is a simplified version of replay(), where we simply retrieve and log the
88 * display list. This function should remain in sync with the replay() function.
89 */
sergeyvc3849aa2016-08-08 13:22:06 -070090void RenderNode::output() {
91 LogcatStream strout;
92 strout << "Root";
93 output(strout, 0);
94}
95
96void RenderNode::output(std::ostream& output, uint32_t level) {
97 output << " (" << getName() << " " << this
John Reck1bcacfd2017-11-03 10:12:19 -070098 << (MathUtils::isZero(properties().getAlpha()) ? ", zero alpha" : "")
99 << (properties().hasShadow() ? ", casting shadow" : "")
100 << (isRenderable() ? "" : ", empty")
101 << (properties().getProjectBackwards() ? ", projected" : "")
102 << (hasLayer() ? ", on HW Layer" : "") << ")" << std::endl;
sergeyvc3849aa2016-08-08 13:22:06 -0700103
104 properties().debugOutputProperties(output, level + 1);
Chris Craik91eff222016-02-22 13:39:33 -0800105
John Reckbe671952021-01-13 22:39:32 -0500106 mDisplayList.output(output, level);
sergeyvc3849aa2016-08-08 13:22:06 -0700107 output << std::string(level * 2, ' ') << "/RenderNode(" << getName() << " " << this << ")";
108 output << std::endl;
Chris Craik91eff222016-02-22 13:39:33 -0800109}
John Reck113e0822014-03-18 09:22:59 -0700110
John Reck183e1382019-10-09 13:41:18 -0700111int RenderNode::getUsageSize() {
John Reckfe5e7b72014-05-23 17:42:28 -0700112 int size = sizeof(RenderNode);
John Reckbe671952021-01-13 22:39:32 -0500113 size += mStagingDisplayList.getUsedSize();
114 size += mDisplayList.getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700115 return size;
116}
117
John Reck183e1382019-10-09 13:41:18 -0700118int RenderNode::getAllocatedSize() {
119 int size = sizeof(RenderNode);
John Reckbe671952021-01-13 22:39:32 -0500120 size += mStagingDisplayList.getAllocatedSize();
121 size += mDisplayList.getAllocatedSize();
John Reck183e1382019-10-09 13:41:18 -0700122 return size;
123}
124
125
John Reckf4198b72014-04-09 17:00:04 -0700126void RenderNode::prepareTree(TreeInfo& info) {
127 ATRACE_CALL();
Chris Craik69e5adf2014-08-14 13:34:01 -0700128 LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
John Reck2de950d2017-01-25 10:58:30 -0800129 MarkAndSweepRemoved observer(&info);
John Reckf4198b72014-04-09 17:00:04 -0700130
John Reck1423e132018-09-21 14:30:19 -0700131 const int before = info.disableForceDark;
John Reck1072fff2018-04-12 15:20:09 -0700132 prepareTreeImpl(observer, info, false);
John Reck1423e132018-09-21 14:30:19 -0700133 LOG_ALWAYS_FATAL_IF(before != info.disableForceDark, "Mis-matched force dark");
John Reckf4198b72014-04-09 17:00:04 -0700134}
135
John Reck68bfe0a2014-06-24 15:34:58 -0700136void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
137 mAnimatorManager.addAnimator(animator);
138}
139
Doris Liu8b083202016-02-19 21:46:06 +0000140void RenderNode::removeAnimator(const sp<BaseRenderNodeAnimator>& animator) {
141 mAnimatorManager.removeAnimator(animator);
142}
143
John Recke4267ea2014-06-03 15:53:15 -0700144void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700145 if (isRenderable()) {
John Reckf1aa7909e2019-03-07 17:01:08 -0800146 mDamageGenerationId = info.damageGenerationId;
John Reck293e8682014-06-17 10:34:02 -0700147 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700148 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
149 } else {
150 // Hope this is big enough?
151 // TODO: Get this from the display list ops or something
John Reckc1288232015-08-12 13:39:11 -0700152 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
John Recka447d292014-06-11 18:39:44 -0700153 }
John Recke4267ea2014-06-03 15:53:15 -0700154 }
155}
156
John Recka7c2ea22014-08-08 13:21:00 -0700157void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700158 LayerType layerType = properties().effectiveLayerType();
Chris Craik182952f2015-03-09 14:17:29 -0700159 if (CC_UNLIKELY(layerType == LayerType::RenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700160 // Damage applied so far needs to affect our parent, but does not require
161 // the layer to be updated. So we pop/push here to clear out the current
162 // damage and get a clean state for display list or children updates to
163 // affect, which will require the layer to be updated
164 info.damageAccumulator->popTransform();
165 info.damageAccumulator->pushTransform(this);
166 if (dirtyMask & DISPLAY_LIST) {
167 damageSelf(info);
168 }
John Reck25fbb3f2014-06-12 13:46:45 -0700169 }
170}
171
172void RenderNode::pushLayerUpdate(TreeInfo& info) {
Fedor Kudasov86bd2142019-06-18 15:51:57 +0100173#ifdef __ANDROID__ // Layoutlib does not support CanvasContext and Layers
Chris Craik856f0cc2015-04-21 15:13:29 -0700174 LayerType layerType = properties().effectiveLayerType();
John Reck25fbb3f2014-06-12 13:46:45 -0700175 // If we are not a layer OR we cannot be rendered (eg, view was detached)
176 // we need to destroy any Layers we may have had previously
John Reck1bcacfd2017-11-03 10:12:19 -0700177 if (CC_LIKELY(layerType != LayerType::RenderLayer) || CC_UNLIKELY(!isRenderable()) ||
178 CC_UNLIKELY(properties().getWidth() == 0) || CC_UNLIKELY(properties().getHeight() == 0) ||
179 CC_UNLIKELY(!properties().fitsOnLayer())) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400180 if (CC_UNLIKELY(hasLayer())) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400181 this->setLayerSurface(nullptr);
John Reck25fbb3f2014-06-12 13:46:45 -0700182 }
183 return;
184 }
185
Stan Iliev216b1572018-03-26 14:29:50 -0400186 if (info.canvasContext.createOrUpdateLayer(this, *info.damageAccumulator, info.errorHandler)) {
Chris Craik9fded232015-11-11 16:42:34 -0800187 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700188 }
189
Derek Sollenberger0df62092016-09-27 16:04:42 -0400190 if (!hasLayer()) {
John Reckc25e5062014-06-18 14:21:29 -0700191 return;
192 }
193
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400194 SkRect dirty;
195 info.damageAccumulator->peekAtDirty(&dirty);
Chris Craik0b7e8242015-10-28 16:50:44 -0700196 info.layerUpdateQueue->enqueueLayerWithDamage(this, dirty);
John Reck998a6d82014-08-28 15:35:53 -0700197
Chris Craike2e53a72015-10-28 15:55:40 -0700198 // There might be prefetched layers that need to be accounted for.
199 // That might be us, so tell CanvasContext that this layer is in the
200 // tree and should not be destroyed.
201 info.canvasContext.markLayerInUse(this);
Fedor Kudasov86bd2142019-06-18 15:51:57 +0100202#endif
John Reck25fbb3f2014-06-12 13:46:45 -0700203}
204
Chris Craika766cb22015-06-08 16:49:43 -0700205/**
206 * Traverse down the the draw tree to prepare for a frame.
207 *
208 * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven
209 *
210 * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the
211 * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer.
212 */
John Reck2de950d2017-01-25 10:58:30 -0800213void RenderNode::prepareTreeImpl(TreeObserver& observer, TreeInfo& info, bool functorsNeedLayer) {
John Reckf1aa7909e2019-03-07 17:01:08 -0800214 if (mDamageGenerationId == info.damageGenerationId) {
215 // We hit the same node a second time in the same tree. We don't know the minimal
216 // damage rect anymore, so just push the biggest we can onto our parent's transform
217 // We push directly onto parent in case we are clipped to bounds but have moved position.
218 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
219 }
John Recka447d292014-06-11 18:39:44 -0700220 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700221
John Reckdcba6722014-07-08 13:59:49 -0700222 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700223 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700224 }
John Reck1423e132018-09-21 14:30:19 -0700225
226 if (!mProperties.getAllowForceDark()) {
227 info.disableForceDark++;
228 }
John Reck5d53a752021-02-08 19:22:59 -0500229 if (!mProperties.layerProperties().getStretchEffect().isEmpty()) {
230 info.stretchEffectCount++;
231 }
John Reck1423e132018-09-21 14:30:19 -0700232
John Reck9eb9f6f2014-08-21 11:23:05 -0700233 uint32_t animatorDirtyMask = 0;
234 if (CC_LIKELY(info.runAnimations)) {
235 animatorDirtyMask = mAnimatorManager.animate(info);
236 }
Chris Craika766cb22015-06-08 16:49:43 -0700237
John Reck3f725f02015-06-16 10:29:31 -0700238 bool willHaveFunctor = false;
Chris Craik003cc3d2015-10-16 10:24:55 -0700239 if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500240 willHaveFunctor = mStagingDisplayList.hasFunctor();
Chris Craik003cc3d2015-10-16 10:24:55 -0700241 } else if (mDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500242 willHaveFunctor = mDisplayList.hasFunctor();
John Reck3f725f02015-06-16 10:29:31 -0700243 }
John Reck1bcacfd2017-11-03 10:12:19 -0700244 bool childFunctorsNeedLayer =
245 mProperties.prepareForFunctorPresence(willHaveFunctor, functorsNeedLayer);
Chris Craika766cb22015-06-08 16:49:43 -0700246
John Reckf6481082016-02-02 15:18:23 -0800247 if (CC_UNLIKELY(mPositionListener.get())) {
248 mPositionListener->onPositionUpdated(*this, info);
249 }
250
John Recka7c2ea22014-08-08 13:21:00 -0700251 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700252 if (info.mode == TreeInfo::MODE_FULL) {
John Reck2de950d2017-01-25 10:58:30 -0800253 pushStagingDisplayListChanges(observer, info);
John Reck25fbb3f2014-06-12 13:46:45 -0700254 }
John Reck25fbb3f2014-06-12 13:46:45 -0700255
Doris Liu07c056d2016-06-13 12:52:44 -0700256 if (mDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500257 info.out.hasFunctors |= mDisplayList.hasFunctor();
258 bool isDirty = mDisplayList.prepareListAndChildren(
John Reck1bcacfd2017-11-03 10:12:19 -0700259 observer, info, childFunctorsNeedLayer,
260 [](RenderNode* child, TreeObserver& observer, TreeInfo& info,
261 bool functorsNeedLayer) {
262 child->prepareTreeImpl(observer, info, functorsNeedLayer);
263 });
Derek Sollenberger0df62092016-09-27 16:04:42 -0400264 if (isDirty) {
265 damageSelf(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700266 }
Doris Liu718cd3e2016-05-17 16:50:31 -0700267 }
Doris Liub51b2862016-08-01 19:56:47 -0700268 pushLayerUpdate(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700269
John Reck1423e132018-09-21 14:30:19 -0700270 if (!mProperties.getAllowForceDark()) {
271 info.disableForceDark--;
272 }
John Reck5d53a752021-02-08 19:22:59 -0500273 if (!mProperties.layerProperties().getStretchEffect().isEmpty()) {
274 info.stretchEffectCount--;
275 }
John Recka447d292014-06-11 18:39:44 -0700276 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700277}
278
Chris Craikb565df12015-10-05 13:00:52 -0700279void RenderNode::syncProperties() {
280 mProperties = mStagingProperties;
281}
282
John Reck25fbb3f2014-06-12 13:46:45 -0700283void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reck097e1d32019-06-12 15:01:51 -0700284 if (mPositionListenerDirty) {
285 mPositionListener = std::move(mStagingPositionListener);
286 mStagingPositionListener = nullptr;
287 mPositionListenerDirty = false;
288 }
289
John Reckff941dc2014-05-14 16:34:14 -0700290 // Push the animators first so that setupStartValueIfNecessary() is called
291 // before properties() is trampled by stagingProperties(), as they are
292 // required by some animators.
John Reck9eb9f6f2014-08-21 11:23:05 -0700293 if (CC_LIKELY(info.runAnimations)) {
John Reck119907c2014-08-14 09:02:01 -0700294 mAnimatorManager.pushStaging();
John Reck9eb9f6f2014-08-21 11:23:05 -0700295 }
John Reckff941dc2014-05-14 16:34:14 -0700296 if (mDirtyPropertyFields) {
297 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700298 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700299 info.damageAccumulator->popTransform();
Chris Craikb565df12015-10-05 13:00:52 -0700300 syncProperties();
John Recke4267ea2014-06-03 15:53:15 -0700301 // We could try to be clever and only re-damage if the matrix changed.
302 // However, we don't need to worry about that. The cost of over-damaging
303 // here is only going to be a single additional map rect of this node
304 // plus a rect join(). The parent's transform (and up) will only be
305 // performed once.
John Recka447d292014-06-11 18:39:44 -0700306 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700307 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700308 }
John Reck25fbb3f2014-06-12 13:46:45 -0700309}
310
John Reck2de950d2017-01-25 10:58:30 -0800311void RenderNode::syncDisplayList(TreeObserver& observer, TreeInfo* info) {
Chris Craikb565df12015-10-05 13:00:52 -0700312 // Make sure we inc first so that we don't fluctuate between 0 and 1,
313 // which would thrash the layer cache
Chris Craik003cc3d2015-10-16 10:24:55 -0700314 if (mStagingDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500315 mStagingDisplayList.updateChildren([](RenderNode* child) { child->incParentRefCount(); });
Chris Craikb565df12015-10-05 13:00:52 -0700316 }
John Reck2de950d2017-01-25 10:58:30 -0800317 deleteDisplayList(observer, info);
John Reckbe671952021-01-13 22:39:32 -0500318 mDisplayList = std::move(mStagingDisplayList);
Chris Craik003cc3d2015-10-16 10:24:55 -0700319 if (mDisplayList) {
John Reck283bb462018-12-13 16:40:14 -0800320 WebViewSyncData syncData {
321 .applyForceDark = info && !info->disableForceDark
322 };
John Reckbe671952021-01-13 22:39:32 -0500323 mDisplayList.syncContents(syncData);
John Reck3b4510cd2018-09-27 17:39:45 -0700324 handleForceDark(info);
325 }
326}
John Reckf3c724f2018-09-20 13:00:04 -0700327
John Reck3b4510cd2018-09-27 17:39:45 -0700328void RenderNode::handleForceDark(android::uirenderer::TreeInfo *info) {
329 if (CC_LIKELY(!info || info->disableForceDark)) {
330 return;
331 }
332 auto usage = usageHint();
John Reckbe671952021-01-13 22:39:32 -0500333 FatVector<RenderNode*, 6> children;
334 mDisplayList.updateChildren([&children](RenderNode* node) {
335 children.push_back(node);
336 });
337 if (mDisplayList.hasText()) {
John Reck3b4510cd2018-09-27 17:39:45 -0700338 usage = UsageHint::Foreground;
339 }
340 if (usage == UsageHint::Unknown) {
341 if (children.size() > 1) {
342 usage = UsageHint::Background;
343 } else if (children.size() == 1 &&
John Reckbe671952021-01-13 22:39:32 -0500344 children.front()->usageHint() !=
John Reck3b4510cd2018-09-27 17:39:45 -0700345 UsageHint::Background) {
346 usage = UsageHint::Background;
John Reck8f45d4a2018-08-15 10:17:12 -0700347 }
Chris Craikb565df12015-10-05 13:00:52 -0700348 }
John Reck3b4510cd2018-09-27 17:39:45 -0700349 if (children.size() > 1) {
350 // Crude overlap check
351 SkRect drawn = SkRect::MakeEmpty();
352 for (auto iter = children.rbegin(); iter != children.rend(); ++iter) {
John Reckbe671952021-01-13 22:39:32 -0500353 const auto& child = *iter;
John Reck3b4510cd2018-09-27 17:39:45 -0700354 // We use stagingProperties here because we haven't yet sync'd the children
355 SkRect bounds = SkRect::MakeXYWH(child->stagingProperties().getX(), child->stagingProperties().getY(),
356 child->stagingProperties().getWidth(), child->stagingProperties().getHeight());
357 if (bounds.contains(drawn)) {
358 // This contains everything drawn after it, so make it a background
359 child->setUsageHint(UsageHint::Background);
360 }
361 drawn.join(bounds);
362 }
363 }
John Reckbe671952021-01-13 22:39:32 -0500364 mDisplayList.applyColorTransform(
John Reck3b4510cd2018-09-27 17:39:45 -0700365 usage == UsageHint::Background ? ColorTransform::Dark : ColorTransform::Light);
Chris Craikb565df12015-10-05 13:00:52 -0700366}
367
John Reck2de950d2017-01-25 10:58:30 -0800368void RenderNode::pushStagingDisplayListChanges(TreeObserver& observer, TreeInfo& info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700369 if (mNeedsDisplayListSync) {
370 mNeedsDisplayListSync = false;
John Reck5c9d7172014-10-22 11:32:27 -0700371 // Damage with the old display list first then the new one to catch any
372 // changes in isRenderable or, in the future, bounds
373 damageSelf(info);
John Reck2de950d2017-01-25 10:58:30 -0800374 syncDisplayList(observer, &info);
John Recke4267ea2014-06-03 15:53:15 -0700375 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700376 }
John Reck8de65a82014-04-09 15:23:38 -0700377}
378
John Reck2de950d2017-01-25 10:58:30 -0800379void RenderNode::deleteDisplayList(TreeObserver& observer, TreeInfo* info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700380 if (mDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500381 mDisplayList.updateChildren(
John Reck1bcacfd2017-11-03 10:12:19 -0700382 [&observer, info](RenderNode* child) { child->decParentRefCount(observer, info); });
John Reckbe671952021-01-13 22:39:32 -0500383 mDisplayList.clear(this);
John Reckdcba6722014-07-08 13:59:49 -0700384 }
John Reckdcba6722014-07-08 13:59:49 -0700385}
386
John Reck2de950d2017-01-25 10:58:30 -0800387void RenderNode::destroyHardwareResources(TreeInfo* info) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400388 if (hasLayer()) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400389 this->setLayerSurface(nullptr);
John Reckdcba6722014-07-08 13:59:49 -0700390 }
John Reckbe671952021-01-13 22:39:32 -0500391 discardStagingDisplayList();
John Reck3afd6372017-01-30 10:15:48 -0800392
393 ImmediateRemoved observer(info);
394 deleteDisplayList(observer, info);
John Reck2de950d2017-01-25 10:58:30 -0800395}
396
397void RenderNode::destroyLayers() {
398 if (hasLayer()) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400399 this->setLayerSurface(nullptr);
John Reck2de950d2017-01-25 10:58:30 -0800400 }
401 if (mDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500402 mDisplayList.updateChildren([](RenderNode* child) { child->destroyLayers(); });
John Reck2de950d2017-01-25 10:58:30 -0800403 }
404}
405
406void RenderNode::decParentRefCount(TreeObserver& observer, TreeInfo* info) {
407 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
408 mParentCount--;
409 if (!mParentCount) {
410 observer.onMaybeRemovedFromTree(this);
411 if (CC_UNLIKELY(mPositionListener.get())) {
412 mPositionListener->onPositionLost(*this, info);
John Reckdcba6722014-07-08 13:59:49 -0700413 }
414 }
415}
416
John Reck2de950d2017-01-25 10:58:30 -0800417void RenderNode::onRemovedFromTree(TreeInfo* info) {
418 destroyHardwareResources(info);
419}
420
421void RenderNode::clearRoot() {
422 ImmediateRemoved observer(nullptr);
423 decParentRefCount(observer);
John Reckdcba6722014-07-08 13:59:49 -0700424}
425
John Reck113e0822014-03-18 09:22:59 -0700426/**
427 * Apply property-based transformations to input matrix
428 *
429 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
430 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
431 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700432void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700433 if (properties().getLeft() != 0 || properties().getTop() != 0) {
434 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700435 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700436 if (properties().getStaticMatrix()) {
437 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700438 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700439 } else if (properties().getAnimationMatrix()) {
440 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700441 matrix.multiply(anim);
442 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700443
Chris Craikcc39e162014-04-25 18:34:11 -0700444 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700445 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700446 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700447 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
John Reck1bcacfd2017-11-03 10:12:19 -0700448 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700449 } else {
450 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700451 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700452 } else {
453 mat4 true3dMat;
John Reck1bcacfd2017-11-03 10:12:19 -0700454 true3dMat.loadTranslate(properties().getPivotX() + properties().getTranslationX(),
455 properties().getPivotY() + properties().getTranslationY(),
456 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700457 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
458 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
459 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
460 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
461 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700462
463 matrix.multiply(true3dMat);
464 }
465 }
466 }
467}
468
Derek Sollenberger579317d42017-08-29 16:33:49 -0400469const SkPath* RenderNode::getClippedOutline(const SkRect& clipRect) const {
470 const SkPath* outlinePath = properties().getOutline().getPath();
471 const uint32_t outlineID = outlinePath->getGenerationID();
472
473 if (outlineID != mClippedOutlineCache.outlineID || clipRect != mClippedOutlineCache.clipRect) {
474 // update the cache keys
475 mClippedOutlineCache.outlineID = outlineID;
476 mClippedOutlineCache.clipRect = clipRect;
477
478 // update the cache value by recomputing a new path
479 SkPath clipPath;
480 clipPath.addRect(clipRect);
481 Op(*outlinePath, clipPath, kIntersect_SkPathOp, &mClippedOutlineCache.clippedOutline);
Derek Sollenberger579317d42017-08-29 16:33:49 -0400482 }
483 return &mClippedOutlineCache.clippedOutline;
484}
485
John Reckf96b2842018-11-29 09:44:10 -0800486using StringBuffer = FatVector<char, 128>;
487
488template <typename... T>
John Reck7af5b2c2018-12-05 13:36:20 -0800489// TODO:__printflike(2, 3)
490// Doesn't work because the warning doesn't understand string_view and doesn't like that
491// it's not a C-style variadic function.
John Reckf96b2842018-11-29 09:44:10 -0800492static void format(StringBuffer& buffer, const std::string_view& format, T... args) {
493 buffer.resize(buffer.capacity());
494 while (1) {
495 int needed = snprintf(buffer.data(), buffer.size(),
496 format.data(), std::forward<T>(args)...);
497 if (needed < 0) {
498 buffer[0] = '\0';
499 buffer.resize(1);
500 return;
501 }
502 if (needed < buffer.size()) {
503 buffer.resize(needed + 1);
504 return;
505 }
John Reck7af5b2c2018-12-05 13:36:20 -0800506 // If we're doing a heap alloc anyway might as well give it some slop
507 buffer.resize(needed + 100);
John Reckf96b2842018-11-29 09:44:10 -0800508 }
509}
510
511void RenderNode::markDrawStart(SkCanvas& canvas) {
512 StringBuffer buffer;
John Reck7af5b2c2018-12-05 13:36:20 -0800513 format(buffer, "RenderNode(id=%" PRId64 ", name='%s')", uniqueId(), getName());
John Reckf96b2842018-11-29 09:44:10 -0800514 canvas.drawAnnotation(SkRect::MakeWH(getWidth(), getHeight()), buffer.data(), nullptr);
515}
516
517void RenderNode::markDrawEnd(SkCanvas& canvas) {
518 StringBuffer buffer;
John Reck7af5b2c2018-12-05 13:36:20 -0800519 format(buffer, "/RenderNode(id=%" PRId64 ", name='%s')", uniqueId(), getName());
John Reckf96b2842018-11-29 09:44:10 -0800520 canvas.drawAnnotation(SkRect::MakeWH(getWidth(), getHeight()), buffer.data(), nullptr);
521}
522
John Reck113e0822014-03-18 09:22:59 -0700523} /* namespace uirenderer */
524} /* namespace android */