blob: 6761435a8171adce43a21189679811bd7104ea3f [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
Tim Murray894f1322020-02-24 21:30:20 +000030#include "utils/FatVector.h"
Chris Craike0bb87d2014-04-22 17:55:41 -070031#include "utils/MathUtils.h"
sergeyvc3849aa2016-08-08 13:22:06 -070032#include "utils/StringUtils.h"
Chris Craik70850ea2014-11-18 10:49:23 -080033#include "utils/TraceUtils.h"
John Reck113e0822014-03-18 09:22:59 -070034
Derek Sollenberger579317d2017-08-29 16:33:49 -040035#include <SkPathOps.h>
John Reck77c40102015-10-26 15:49:47 -070036#include <algorithm>
John Reckf96b2842018-11-29 09:44:10 -080037#include <atomic>
John Reck77c40102015-10-26 15:49:47 -070038#include <sstream>
39#include <string>
40
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);
Chris Craik003cc3d2015-10-16 10:24:55 -070073 delete mStagingDisplayList;
Derek Sollenberger0df62092016-09-27 16:04:42 -040074 LOG_ALWAYS_FATAL_IF(hasLayer(), "layer missed detachment!");
John Reck113e0822014-03-18 09:22:59 -070075}
76
John Reck2de950d2017-01-25 10:58:30 -080077void RenderNode::setStagingDisplayList(DisplayList* displayList) {
78 mValid = (displayList != nullptr);
Chris Craik003cc3d2015-10-16 10:24:55 -070079 mNeedsDisplayListSync = true;
80 delete mStagingDisplayList;
81 mStagingDisplayList = displayList;
John Reck113e0822014-03-18 09:22:59 -070082}
83
84/**
85 * This function is a simplified version of replay(), where we simply retrieve and log the
86 * display list. This function should remain in sync with the replay() function.
87 */
sergeyvc3849aa2016-08-08 13:22:06 -070088void RenderNode::output() {
89 LogcatStream strout;
90 strout << "Root";
91 output(strout, 0);
92}
93
94void RenderNode::output(std::ostream& output, uint32_t level) {
95 output << " (" << getName() << " " << this
John Reck1bcacfd2017-11-03 10:12:19 -070096 << (MathUtils::isZero(properties().getAlpha()) ? ", zero alpha" : "")
97 << (properties().hasShadow() ? ", casting shadow" : "")
98 << (isRenderable() ? "" : ", empty")
99 << (properties().getProjectBackwards() ? ", projected" : "")
100 << (hasLayer() ? ", on HW Layer" : "") << ")" << std::endl;
sergeyvc3849aa2016-08-08 13:22:06 -0700101
102 properties().debugOutputProperties(output, level + 1);
Chris Craik91eff222016-02-22 13:39:33 -0800103
104 if (mDisplayList) {
Stan Ilievd2172372017-02-09 16:59:27 -0500105 mDisplayList->output(output, level);
Chris Craik91eff222016-02-22 13:39:33 -0800106 }
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);
Chris Craik003cc3d2015-10-16 10:24:55 -0700113 if (mStagingDisplayList) {
114 size += mStagingDisplayList->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700115 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700116 if (mDisplayList && mDisplayList != mStagingDisplayList) {
117 size += mDisplayList->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700118 }
119 return size;
120}
121
John Reck183e1382019-10-09 13:41:18 -0700122int RenderNode::getAllocatedSize() {
123 int size = sizeof(RenderNode);
124 if (mStagingDisplayList) {
125 size += mStagingDisplayList->getAllocatedSize();
126 }
127 if (mDisplayList && mDisplayList != mStagingDisplayList) {
128 size += mDisplayList->getAllocatedSize();
129 }
130 return size;
131}
132
133
John Reckf4198b72014-04-09 17:00:04 -0700134void RenderNode::prepareTree(TreeInfo& info) {
135 ATRACE_CALL();
Chris Craik69e5adf2014-08-14 13:34:01 -0700136 LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
John Reck2de950d2017-01-25 10:58:30 -0800137 MarkAndSweepRemoved observer(&info);
John Reckf4198b72014-04-09 17:00:04 -0700138
John Reck1423e132018-09-21 14:30:19 -0700139 const int before = info.disableForceDark;
John Reck1072fff2018-04-12 15:20:09 -0700140 prepareTreeImpl(observer, info, false);
John Reck1423e132018-09-21 14:30:19 -0700141 LOG_ALWAYS_FATAL_IF(before != info.disableForceDark, "Mis-matched force dark");
John Reckf4198b72014-04-09 17:00:04 -0700142}
143
John Reck68bfe0a2014-06-24 15:34:58 -0700144void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
145 mAnimatorManager.addAnimator(animator);
146}
147
Doris Liu8b083202016-02-19 21:46:06 +0000148void RenderNode::removeAnimator(const sp<BaseRenderNodeAnimator>& animator) {
149 mAnimatorManager.removeAnimator(animator);
150}
151
John Recke4267ea2014-06-03 15:53:15 -0700152void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700153 if (isRenderable()) {
John Reckf1aa7909e2019-03-07 17:01:08 -0800154 mDamageGenerationId = info.damageGenerationId;
John Reck293e8682014-06-17 10:34:02 -0700155 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700156 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
157 } else {
158 // Hope this is big enough?
159 // TODO: Get this from the display list ops or something
John Reckc1288232015-08-12 13:39:11 -0700160 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
John Recka447d292014-06-11 18:39:44 -0700161 }
John Recke4267ea2014-06-03 15:53:15 -0700162 }
163}
164
John Recka7c2ea22014-08-08 13:21:00 -0700165void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700166 LayerType layerType = properties().effectiveLayerType();
Chris Craik182952f2015-03-09 14:17:29 -0700167 if (CC_UNLIKELY(layerType == LayerType::RenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700168 // Damage applied so far needs to affect our parent, but does not require
169 // the layer to be updated. So we pop/push here to clear out the current
170 // damage and get a clean state for display list or children updates to
171 // affect, which will require the layer to be updated
172 info.damageAccumulator->popTransform();
173 info.damageAccumulator->pushTransform(this);
174 if (dirtyMask & DISPLAY_LIST) {
175 damageSelf(info);
176 }
John Reck25fbb3f2014-06-12 13:46:45 -0700177 }
178}
179
180void RenderNode::pushLayerUpdate(TreeInfo& info) {
Fedor Kudasov86bd2142019-06-18 15:51:57 +0100181#ifdef __ANDROID__ // Layoutlib does not support CanvasContext and Layers
Chris Craik856f0cc2015-04-21 15:13:29 -0700182 LayerType layerType = properties().effectiveLayerType();
John Reck25fbb3f2014-06-12 13:46:45 -0700183 // If we are not a layer OR we cannot be rendered (eg, view was detached)
184 // we need to destroy any Layers we may have had previously
John Reck1bcacfd2017-11-03 10:12:19 -0700185 if (CC_LIKELY(layerType != LayerType::RenderLayer) || CC_UNLIKELY(!isRenderable()) ||
186 CC_UNLIKELY(properties().getWidth() == 0) || CC_UNLIKELY(properties().getHeight() == 0) ||
187 CC_UNLIKELY(!properties().fitsOnLayer())) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400188 if (CC_UNLIKELY(hasLayer())) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400189 this->setLayerSurface(nullptr);
John Reck25fbb3f2014-06-12 13:46:45 -0700190 }
191 return;
192 }
193
Stan Iliev216b1572018-03-26 14:29:50 -0400194 if (info.canvasContext.createOrUpdateLayer(this, *info.damageAccumulator, info.errorHandler)) {
Chris Craik9fded232015-11-11 16:42:34 -0800195 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700196 }
197
Derek Sollenberger0df62092016-09-27 16:04:42 -0400198 if (!hasLayer()) {
John Reckc25e5062014-06-18 14:21:29 -0700199 return;
200 }
201
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400202 SkRect dirty;
203 info.damageAccumulator->peekAtDirty(&dirty);
Chris Craik0b7e8242015-10-28 16:50:44 -0700204 info.layerUpdateQueue->enqueueLayerWithDamage(this, dirty);
John Reck998a6d82014-08-28 15:35:53 -0700205
Chris Craike2e53a72015-10-28 15:55:40 -0700206 // There might be prefetched layers that need to be accounted for.
207 // That might be us, so tell CanvasContext that this layer is in the
208 // tree and should not be destroyed.
209 info.canvasContext.markLayerInUse(this);
Fedor Kudasov86bd2142019-06-18 15:51:57 +0100210#endif
John Reck25fbb3f2014-06-12 13:46:45 -0700211}
212
Chris Craika766cb22015-06-08 16:49:43 -0700213/**
214 * Traverse down the the draw tree to prepare for a frame.
215 *
216 * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven
217 *
218 * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the
219 * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer.
220 */
John Reck2de950d2017-01-25 10:58:30 -0800221void RenderNode::prepareTreeImpl(TreeObserver& observer, TreeInfo& info, bool functorsNeedLayer) {
John Reckf1aa7909e2019-03-07 17:01:08 -0800222 if (mDamageGenerationId == info.damageGenerationId) {
223 // We hit the same node a second time in the same tree. We don't know the minimal
224 // damage rect anymore, so just push the biggest we can onto our parent's transform
225 // We push directly onto parent in case we are clipped to bounds but have moved position.
226 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
227 }
John Recka447d292014-06-11 18:39:44 -0700228 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700229
John Reckdcba6722014-07-08 13:59:49 -0700230 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700231 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700232 }
John Reck1423e132018-09-21 14:30:19 -0700233
234 if (!mProperties.getAllowForceDark()) {
235 info.disableForceDark++;
236 }
237
John Reck9eb9f6f2014-08-21 11:23:05 -0700238 uint32_t animatorDirtyMask = 0;
239 if (CC_LIKELY(info.runAnimations)) {
240 animatorDirtyMask = mAnimatorManager.animate(info);
241 }
Chris Craika766cb22015-06-08 16:49:43 -0700242
John Reck3f725f02015-06-16 10:29:31 -0700243 bool willHaveFunctor = false;
Chris Craik003cc3d2015-10-16 10:24:55 -0700244 if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400245 willHaveFunctor = mStagingDisplayList->hasFunctor();
Chris Craik003cc3d2015-10-16 10:24:55 -0700246 } else if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400247 willHaveFunctor = mDisplayList->hasFunctor();
John Reck3f725f02015-06-16 10:29:31 -0700248 }
John Reck1bcacfd2017-11-03 10:12:19 -0700249 bool childFunctorsNeedLayer =
250 mProperties.prepareForFunctorPresence(willHaveFunctor, functorsNeedLayer);
Chris Craika766cb22015-06-08 16:49:43 -0700251
John Reckf6481082016-02-02 15:18:23 -0800252 if (CC_UNLIKELY(mPositionListener.get())) {
253 mPositionListener->onPositionUpdated(*this, info);
254 }
255
John Recka7c2ea22014-08-08 13:21:00 -0700256 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700257 if (info.mode == TreeInfo::MODE_FULL) {
John Reck2de950d2017-01-25 10:58:30 -0800258 pushStagingDisplayListChanges(observer, info);
John Reck25fbb3f2014-06-12 13:46:45 -0700259 }
John Reck25fbb3f2014-06-12 13:46:45 -0700260
Doris Liu07c056d2016-06-13 12:52:44 -0700261 if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400262 info.out.hasFunctors |= mDisplayList->hasFunctor();
John Reck1bcacfd2017-11-03 10:12:19 -0700263 bool isDirty = mDisplayList->prepareListAndChildren(
264 observer, info, childFunctorsNeedLayer,
265 [](RenderNode* child, TreeObserver& observer, TreeInfo& info,
266 bool functorsNeedLayer) {
267 child->prepareTreeImpl(observer, info, functorsNeedLayer);
268 });
Derek Sollenberger0df62092016-09-27 16:04:42 -0400269 if (isDirty) {
270 damageSelf(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700271 }
Doris Liu718cd3e2016-05-17 16:50:31 -0700272 }
Doris Liub51b2862016-08-01 19:56:47 -0700273 pushLayerUpdate(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700274
John Reck1423e132018-09-21 14:30:19 -0700275 if (!mProperties.getAllowForceDark()) {
276 info.disableForceDark--;
277 }
John Recka447d292014-06-11 18:39:44 -0700278 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700279}
280
Chris Craikb565df12015-10-05 13:00:52 -0700281void RenderNode::syncProperties() {
282 mProperties = mStagingProperties;
283}
284
John Reck25fbb3f2014-06-12 13:46:45 -0700285void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reck097e1d32019-06-12 15:01:51 -0700286 if (mPositionListenerDirty) {
287 mPositionListener = std::move(mStagingPositionListener);
288 mStagingPositionListener = nullptr;
289 mPositionListenerDirty = false;
290 }
291
John Reckff941dc2014-05-14 16:34:14 -0700292 // Push the animators first so that setupStartValueIfNecessary() is called
293 // before properties() is trampled by stagingProperties(), as they are
294 // required by some animators.
John Reck9eb9f6f2014-08-21 11:23:05 -0700295 if (CC_LIKELY(info.runAnimations)) {
John Reck119907c2014-08-14 09:02:01 -0700296 mAnimatorManager.pushStaging();
John Reck9eb9f6f2014-08-21 11:23:05 -0700297 }
John Reckff941dc2014-05-14 16:34:14 -0700298 if (mDirtyPropertyFields) {
299 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700300 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700301 info.damageAccumulator->popTransform();
Chris Craikb565df12015-10-05 13:00:52 -0700302 syncProperties();
John Recke4267ea2014-06-03 15:53:15 -0700303 // We could try to be clever and only re-damage if the matrix changed.
304 // However, we don't need to worry about that. The cost of over-damaging
305 // here is only going to be a single additional map rect of this node
306 // plus a rect join(). The parent's transform (and up) will only be
307 // performed once.
John Recka447d292014-06-11 18:39:44 -0700308 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700309 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700310 }
John Reck25fbb3f2014-06-12 13:46:45 -0700311}
312
John Reck2de950d2017-01-25 10:58:30 -0800313void RenderNode::syncDisplayList(TreeObserver& observer, TreeInfo* info) {
Chris Craikb565df12015-10-05 13:00:52 -0700314 // Make sure we inc first so that we don't fluctuate between 0 and 1,
315 // which would thrash the layer cache
Chris Craik003cc3d2015-10-16 10:24:55 -0700316 if (mStagingDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -0700317 mStagingDisplayList->updateChildren([](RenderNode* child) { child->incParentRefCount(); });
Chris Craikb565df12015-10-05 13:00:52 -0700318 }
John Reck2de950d2017-01-25 10:58:30 -0800319 deleteDisplayList(observer, info);
Chris Craik003cc3d2015-10-16 10:24:55 -0700320 mDisplayList = mStagingDisplayList;
321 mStagingDisplayList = nullptr;
322 if (mDisplayList) {
John Reck283bb462018-12-13 16:40:14 -0800323 WebViewSyncData syncData {
324 .applyForceDark = info && !info->disableForceDark
325 };
326 mDisplayList->syncContents(syncData);
John Reck3b4510cd2018-09-27 17:39:45 -0700327 handleForceDark(info);
328 }
329}
John Reckf3c724f2018-09-20 13:00:04 -0700330
John Reck3b4510cd2018-09-27 17:39:45 -0700331void RenderNode::handleForceDark(android::uirenderer::TreeInfo *info) {
332 if (CC_LIKELY(!info || info->disableForceDark)) {
333 return;
334 }
335 auto usage = usageHint();
336 const auto& children = mDisplayList->mChildNodes;
337 if (mDisplayList->hasText()) {
338 usage = UsageHint::Foreground;
339 }
340 if (usage == UsageHint::Unknown) {
341 if (children.size() > 1) {
342 usage = UsageHint::Background;
343 } else if (children.size() == 1 &&
344 children.front().getRenderNode()->usageHint() !=
345 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) {
353 const auto& child = iter->getRenderNode();
354 // 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 }
364 mDisplayList->mDisplayList.applyColorTransform(
365 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 Reck1bcacfd2017-11-03 10:12:19 -0700381 mDisplayList->updateChildren(
382 [&observer, info](RenderNode* child) { child->decParentRefCount(observer, info); });
Derek Sollenberger0df62092016-09-27 16:04:42 -0400383 if (!mDisplayList->reuseDisplayList(this, info ? &info->canvasContext : nullptr)) {
384 delete mDisplayList;
John Reckdcba6722014-07-08 13:59:49 -0700385 }
386 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700387 mDisplayList = nullptr;
John Reckdcba6722014-07-08 13:59:49 -0700388}
389
John Reck2de950d2017-01-25 10:58:30 -0800390void RenderNode::destroyHardwareResources(TreeInfo* info) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400391 if (hasLayer()) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400392 this->setLayerSurface(nullptr);
John Reckdcba6722014-07-08 13:59:49 -0700393 }
John Reck3afd6372017-01-30 10:15:48 -0800394 setStagingDisplayList(nullptr);
395
396 ImmediateRemoved observer(info);
397 deleteDisplayList(observer, info);
John Reck2de950d2017-01-25 10:58:30 -0800398}
399
400void RenderNode::destroyLayers() {
401 if (hasLayer()) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400402 this->setLayerSurface(nullptr);
John Reck2de950d2017-01-25 10:58:30 -0800403 }
404 if (mDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -0700405 mDisplayList->updateChildren([](RenderNode* child) { child->destroyLayers(); });
John Reck2de950d2017-01-25 10:58:30 -0800406 }
407}
408
409void RenderNode::decParentRefCount(TreeObserver& observer, TreeInfo* info) {
410 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
411 mParentCount--;
412 if (!mParentCount) {
413 observer.onMaybeRemovedFromTree(this);
414 if (CC_UNLIKELY(mPositionListener.get())) {
415 mPositionListener->onPositionLost(*this, info);
John Reckdcba6722014-07-08 13:59:49 -0700416 }
417 }
418}
419
John Reck2de950d2017-01-25 10:58:30 -0800420void RenderNode::onRemovedFromTree(TreeInfo* info) {
421 destroyHardwareResources(info);
422}
423
424void RenderNode::clearRoot() {
425 ImmediateRemoved observer(nullptr);
426 decParentRefCount(observer);
John Reckdcba6722014-07-08 13:59:49 -0700427}
428
John Reck113e0822014-03-18 09:22:59 -0700429/**
430 * Apply property-based transformations to input matrix
431 *
432 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
433 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
434 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700435void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700436 if (properties().getLeft() != 0 || properties().getTop() != 0) {
437 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700438 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700439 if (properties().getStaticMatrix()) {
440 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700441 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700442 } else if (properties().getAnimationMatrix()) {
443 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700444 matrix.multiply(anim);
445 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700446
Chris Craikcc39e162014-04-25 18:34:11 -0700447 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700448 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700449 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700450 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
John Reck1bcacfd2017-11-03 10:12:19 -0700451 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700452 } else {
453 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700454 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700455 } else {
456 mat4 true3dMat;
John Reck1bcacfd2017-11-03 10:12:19 -0700457 true3dMat.loadTranslate(properties().getPivotX() + properties().getTranslationX(),
458 properties().getPivotY() + properties().getTranslationY(),
459 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700460 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
461 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
462 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
463 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
464 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700465
466 matrix.multiply(true3dMat);
467 }
468 }
469 }
470}
471
Derek Sollenberger579317d2017-08-29 16:33:49 -0400472const SkPath* RenderNode::getClippedOutline(const SkRect& clipRect) const {
473 const SkPath* outlinePath = properties().getOutline().getPath();
474 const uint32_t outlineID = outlinePath->getGenerationID();
475
476 if (outlineID != mClippedOutlineCache.outlineID || clipRect != mClippedOutlineCache.clipRect) {
477 // update the cache keys
478 mClippedOutlineCache.outlineID = outlineID;
479 mClippedOutlineCache.clipRect = clipRect;
480
481 // update the cache value by recomputing a new path
482 SkPath clipPath;
483 clipPath.addRect(clipRect);
484 Op(*outlinePath, clipPath, kIntersect_SkPathOp, &mClippedOutlineCache.clippedOutline);
Derek Sollenberger579317d2017-08-29 16:33:49 -0400485 }
486 return &mClippedOutlineCache.clippedOutline;
487}
488
John Reckf96b2842018-11-29 09:44:10 -0800489using StringBuffer = FatVector<char, 128>;
490
491template <typename... T>
John Reck7af5b2c2018-12-05 13:36:20 -0800492// TODO:__printflike(2, 3)
493// Doesn't work because the warning doesn't understand string_view and doesn't like that
494// it's not a C-style variadic function.
John Reckf96b2842018-11-29 09:44:10 -0800495static void format(StringBuffer& buffer, const std::string_view& format, T... args) {
496 buffer.resize(buffer.capacity());
497 while (1) {
498 int needed = snprintf(buffer.data(), buffer.size(),
499 format.data(), std::forward<T>(args)...);
500 if (needed < 0) {
501 buffer[0] = '\0';
502 buffer.resize(1);
503 return;
504 }
505 if (needed < buffer.size()) {
506 buffer.resize(needed + 1);
507 return;
508 }
John Reck7af5b2c2018-12-05 13:36:20 -0800509 // If we're doing a heap alloc anyway might as well give it some slop
510 buffer.resize(needed + 100);
John Reckf96b2842018-11-29 09:44:10 -0800511 }
512}
513
514void RenderNode::markDrawStart(SkCanvas& canvas) {
515 StringBuffer buffer;
John Reck7af5b2c2018-12-05 13:36:20 -0800516 format(buffer, "RenderNode(id=%" PRId64 ", name='%s')", uniqueId(), getName());
John Reckf96b2842018-11-29 09:44:10 -0800517 canvas.drawAnnotation(SkRect::MakeWH(getWidth(), getHeight()), buffer.data(), nullptr);
518}
519
520void RenderNode::markDrawEnd(SkCanvas& canvas) {
521 StringBuffer buffer;
John Reck7af5b2c2018-12-05 13:36:20 -0800522 format(buffer, "/RenderNode(id=%" PRId64 ", name='%s')", uniqueId(), getName());
John Reckf96b2842018-11-29 09:44:10 -0800523 canvas.drawAnnotation(SkRect::MakeWH(getWidth(), getHeight()), buffer.data(), nullptr);
524}
525
John Reck113e0822014-03-18 09:22:59 -0700526} /* namespace uirenderer */
527} /* namespace android */