blob: a833f7ef98c958d8c3538302e6d3deea0b8e1d72 [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"
John Reck1bcacfd2017-11-03 10:12:19 -070023#include "renderthread/CanvasContext.h"
John Reck2de950d2017-01-25 10:58:30 -080024#include "utils/FatVector.h"
Chris Craike0bb87d2014-04-22 17:55:41 -070025#include "utils/MathUtils.h"
sergeyvc3849aa2016-08-08 13:22:06 -070026#include "utils/StringUtils.h"
Chris Craik70850ea2014-11-18 10:49:23 -080027#include "utils/TraceUtils.h"
John Reck113e0822014-03-18 09:22:59 -070028
Derek Sollenberger579317d42017-08-29 16:33:49 -040029#include <SkPathOps.h>
John Reck77c40102015-10-26 15:49:47 -070030#include <algorithm>
John Reckf96b2842018-11-29 09:44:10 -080031#include <atomic>
John Reck77c40102015-10-26 15:49:47 -070032#include <sstream>
33#include <string>
34
John Reck113e0822014-03-18 09:22:59 -070035namespace android {
36namespace uirenderer {
37
John Reck2de950d2017-01-25 10:58:30 -080038// Used for tree mutations that are purely destructive.
39// Generic tree mutations should use MarkAndSweepObserver instead
40class ImmediateRemoved : public TreeObserver {
41public:
42 explicit ImmediateRemoved(TreeInfo* info) : mTreeInfo(info) {}
43
John Reck1bcacfd2017-11-03 10:12:19 -070044 void onMaybeRemovedFromTree(RenderNode* node) override { node->onRemovedFromTree(mTreeInfo); }
John Reck2de950d2017-01-25 10:58:30 -080045
46private:
47 TreeInfo* mTreeInfo;
48};
49
John Reckf96b2842018-11-29 09:44:10 -080050static int64_t generateId() {
51 static std::atomic<int64_t> sNextId{1};
52 return sNextId++;
53}
54
John Reck8de65a82014-04-09 15:23:38 -070055RenderNode::RenderNode()
John Reckf96b2842018-11-29 09:44:10 -080056 : mUniqueId(generateId())
57 , mDirtyPropertyFields(0)
Chris Craik003cc3d2015-10-16 10:24:55 -070058 , mNeedsDisplayListSync(false)
59 , mDisplayList(nullptr)
60 , mStagingDisplayList(nullptr)
John Reck68bfe0a2014-06-24 15:34:58 -070061 , mAnimatorManager(*this)
John Reck1bcacfd2017-11-03 10:12:19 -070062 , mParentCount(0) {}
John Reck113e0822014-03-18 09:22:59 -070063
64RenderNode::~RenderNode() {
John Reck2de950d2017-01-25 10:58:30 -080065 ImmediateRemoved observer(nullptr);
66 deleteDisplayList(observer);
Chris Craik003cc3d2015-10-16 10:24:55 -070067 delete mStagingDisplayList;
Derek Sollenberger0df62092016-09-27 16:04:42 -040068 LOG_ALWAYS_FATAL_IF(hasLayer(), "layer missed detachment!");
John Reck113e0822014-03-18 09:22:59 -070069}
70
John Reck2de950d2017-01-25 10:58:30 -080071void RenderNode::setStagingDisplayList(DisplayList* displayList) {
72 mValid = (displayList != nullptr);
Chris Craik003cc3d2015-10-16 10:24:55 -070073 mNeedsDisplayListSync = true;
74 delete mStagingDisplayList;
75 mStagingDisplayList = displayList;
John Reck113e0822014-03-18 09:22:59 -070076}
77
78/**
79 * This function is a simplified version of replay(), where we simply retrieve and log the
80 * display list. This function should remain in sync with the replay() function.
81 */
sergeyvc3849aa2016-08-08 13:22:06 -070082void RenderNode::output() {
83 LogcatStream strout;
84 strout << "Root";
85 output(strout, 0);
86}
87
88void RenderNode::output(std::ostream& output, uint32_t level) {
89 output << " (" << getName() << " " << this
John Reck1bcacfd2017-11-03 10:12:19 -070090 << (MathUtils::isZero(properties().getAlpha()) ? ", zero alpha" : "")
91 << (properties().hasShadow() ? ", casting shadow" : "")
92 << (isRenderable() ? "" : ", empty")
93 << (properties().getProjectBackwards() ? ", projected" : "")
94 << (hasLayer() ? ", on HW Layer" : "") << ")" << std::endl;
sergeyvc3849aa2016-08-08 13:22:06 -070095
96 properties().debugOutputProperties(output, level + 1);
Chris Craik91eff222016-02-22 13:39:33 -080097
98 if (mDisplayList) {
Stan Ilievd2172372017-02-09 16:59:27 -050099 mDisplayList->output(output, level);
Chris Craik91eff222016-02-22 13:39:33 -0800100 }
sergeyvc3849aa2016-08-08 13:22:06 -0700101 output << std::string(level * 2, ' ') << "/RenderNode(" << getName() << " " << this << ")";
102 output << std::endl;
Chris Craik91eff222016-02-22 13:39:33 -0800103}
John Reck113e0822014-03-18 09:22:59 -0700104
John Reckfe5e7b72014-05-23 17:42:28 -0700105int RenderNode::getDebugSize() {
106 int size = sizeof(RenderNode);
Chris Craik003cc3d2015-10-16 10:24:55 -0700107 if (mStagingDisplayList) {
108 size += mStagingDisplayList->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700109 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700110 if (mDisplayList && mDisplayList != mStagingDisplayList) {
111 size += mDisplayList->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700112 }
113 return size;
114}
115
John Reckf4198b72014-04-09 17:00:04 -0700116void RenderNode::prepareTree(TreeInfo& info) {
117 ATRACE_CALL();
Chris Craik69e5adf2014-08-14 13:34:01 -0700118 LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
John Reck2de950d2017-01-25 10:58:30 -0800119 MarkAndSweepRemoved observer(&info);
John Reckf4198b72014-04-09 17:00:04 -0700120
John Reck1423e132018-09-21 14:30:19 -0700121 const int before = info.disableForceDark;
John Reck1072fff2018-04-12 15:20:09 -0700122 prepareTreeImpl(observer, info, false);
John Reck1423e132018-09-21 14:30:19 -0700123 LOG_ALWAYS_FATAL_IF(before != info.disableForceDark, "Mis-matched force dark");
John Reckf4198b72014-04-09 17:00:04 -0700124}
125
John Reck68bfe0a2014-06-24 15:34:58 -0700126void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
127 mAnimatorManager.addAnimator(animator);
128}
129
Doris Liu8b083202016-02-19 21:46:06 +0000130void RenderNode::removeAnimator(const sp<BaseRenderNodeAnimator>& animator) {
131 mAnimatorManager.removeAnimator(animator);
132}
133
John Recke4267ea2014-06-03 15:53:15 -0700134void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700135 if (isRenderable()) {
John Reckf1aa7909e2019-03-07 17:01:08 -0800136 mDamageGenerationId = info.damageGenerationId;
John Reck293e8682014-06-17 10:34:02 -0700137 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700138 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
139 } else {
140 // Hope this is big enough?
141 // TODO: Get this from the display list ops or something
John Reckc1288232015-08-12 13:39:11 -0700142 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
John Recka447d292014-06-11 18:39:44 -0700143 }
John Recke4267ea2014-06-03 15:53:15 -0700144 }
145}
146
John Recka7c2ea22014-08-08 13:21:00 -0700147void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700148 LayerType layerType = properties().effectiveLayerType();
Chris Craik182952f2015-03-09 14:17:29 -0700149 if (CC_UNLIKELY(layerType == LayerType::RenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700150 // Damage applied so far needs to affect our parent, but does not require
151 // the layer to be updated. So we pop/push here to clear out the current
152 // damage and get a clean state for display list or children updates to
153 // affect, which will require the layer to be updated
154 info.damageAccumulator->popTransform();
155 info.damageAccumulator->pushTransform(this);
156 if (dirtyMask & DISPLAY_LIST) {
157 damageSelf(info);
158 }
John Reck25fbb3f2014-06-12 13:46:45 -0700159 }
160}
161
162void RenderNode::pushLayerUpdate(TreeInfo& info) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700163 LayerType layerType = properties().effectiveLayerType();
John Reck25fbb3f2014-06-12 13:46:45 -0700164 // If we are not a layer OR we cannot be rendered (eg, view was detached)
165 // we need to destroy any Layers we may have had previously
John Reck1bcacfd2017-11-03 10:12:19 -0700166 if (CC_LIKELY(layerType != LayerType::RenderLayer) || CC_UNLIKELY(!isRenderable()) ||
167 CC_UNLIKELY(properties().getWidth() == 0) || CC_UNLIKELY(properties().getHeight() == 0) ||
168 CC_UNLIKELY(!properties().fitsOnLayer())) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400169 if (CC_UNLIKELY(hasLayer())) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400170 this->setLayerSurface(nullptr);
John Reck25fbb3f2014-06-12 13:46:45 -0700171 }
172 return;
173 }
174
Stan Iliev216b1572018-03-26 14:29:50 -0400175 if (info.canvasContext.createOrUpdateLayer(this, *info.damageAccumulator, info.errorHandler)) {
Chris Craik9fded232015-11-11 16:42:34 -0800176 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700177 }
178
Derek Sollenberger0df62092016-09-27 16:04:42 -0400179 if (!hasLayer()) {
John Reckc25e5062014-06-18 14:21:29 -0700180 return;
181 }
182
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400183 SkRect dirty;
184 info.damageAccumulator->peekAtDirty(&dirty);
Chris Craik0b7e8242015-10-28 16:50:44 -0700185 info.layerUpdateQueue->enqueueLayerWithDamage(this, dirty);
John Reck998a6d82014-08-28 15:35:53 -0700186
Chris Craike2e53a72015-10-28 15:55:40 -0700187 // There might be prefetched layers that need to be accounted for.
188 // That might be us, so tell CanvasContext that this layer is in the
189 // tree and should not be destroyed.
190 info.canvasContext.markLayerInUse(this);
John Reck25fbb3f2014-06-12 13:46:45 -0700191}
192
Chris Craika766cb22015-06-08 16:49:43 -0700193/**
194 * Traverse down the the draw tree to prepare for a frame.
195 *
196 * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven
197 *
198 * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the
199 * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer.
200 */
John Reck2de950d2017-01-25 10:58:30 -0800201void RenderNode::prepareTreeImpl(TreeObserver& observer, TreeInfo& info, bool functorsNeedLayer) {
John Reckf1aa7909e2019-03-07 17:01:08 -0800202 if (mDamageGenerationId == info.damageGenerationId) {
203 // We hit the same node a second time in the same tree. We don't know the minimal
204 // damage rect anymore, so just push the biggest we can onto our parent's transform
205 // We push directly onto parent in case we are clipped to bounds but have moved position.
206 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
207 }
John Recka447d292014-06-11 18:39:44 -0700208 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700209
John Reckdcba6722014-07-08 13:59:49 -0700210 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700211 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700212 }
John Reck1423e132018-09-21 14:30:19 -0700213
214 if (!mProperties.getAllowForceDark()) {
215 info.disableForceDark++;
216 }
217
John Reck9eb9f6f2014-08-21 11:23:05 -0700218 uint32_t animatorDirtyMask = 0;
219 if (CC_LIKELY(info.runAnimations)) {
220 animatorDirtyMask = mAnimatorManager.animate(info);
221 }
Chris Craika766cb22015-06-08 16:49:43 -0700222
John Reck3f725f02015-06-16 10:29:31 -0700223 bool willHaveFunctor = false;
Chris Craik003cc3d2015-10-16 10:24:55 -0700224 if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400225 willHaveFunctor = mStagingDisplayList->hasFunctor();
Chris Craik003cc3d2015-10-16 10:24:55 -0700226 } else if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400227 willHaveFunctor = mDisplayList->hasFunctor();
John Reck3f725f02015-06-16 10:29:31 -0700228 }
John Reck1bcacfd2017-11-03 10:12:19 -0700229 bool childFunctorsNeedLayer =
230 mProperties.prepareForFunctorPresence(willHaveFunctor, functorsNeedLayer);
Chris Craika766cb22015-06-08 16:49:43 -0700231
John Reckf6481082016-02-02 15:18:23 -0800232 if (CC_UNLIKELY(mPositionListener.get())) {
233 mPositionListener->onPositionUpdated(*this, info);
234 }
235
John Recka7c2ea22014-08-08 13:21:00 -0700236 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700237 if (info.mode == TreeInfo::MODE_FULL) {
John Reck2de950d2017-01-25 10:58:30 -0800238 pushStagingDisplayListChanges(observer, info);
John Reck25fbb3f2014-06-12 13:46:45 -0700239 }
John Reck25fbb3f2014-06-12 13:46:45 -0700240
Doris Liu07c056d2016-06-13 12:52:44 -0700241 if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400242 info.out.hasFunctors |= mDisplayList->hasFunctor();
John Reck1bcacfd2017-11-03 10:12:19 -0700243 bool isDirty = mDisplayList->prepareListAndChildren(
244 observer, info, childFunctorsNeedLayer,
245 [](RenderNode* child, TreeObserver& observer, TreeInfo& info,
246 bool functorsNeedLayer) {
247 child->prepareTreeImpl(observer, info, functorsNeedLayer);
248 });
Derek Sollenberger0df62092016-09-27 16:04:42 -0400249 if (isDirty) {
250 damageSelf(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700251 }
Doris Liu718cd3e2016-05-17 16:50:31 -0700252 }
Doris Liub51b2862016-08-01 19:56:47 -0700253 pushLayerUpdate(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700254
John Reck1423e132018-09-21 14:30:19 -0700255 if (!mProperties.getAllowForceDark()) {
256 info.disableForceDark--;
257 }
John Recka447d292014-06-11 18:39:44 -0700258 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700259}
260
Chris Craikb565df12015-10-05 13:00:52 -0700261void RenderNode::syncProperties() {
262 mProperties = mStagingProperties;
263}
264
John Reck25fbb3f2014-06-12 13:46:45 -0700265void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reck097e1d32019-06-12 15:01:51 -0700266 if (mPositionListenerDirty) {
267 mPositionListener = std::move(mStagingPositionListener);
268 mStagingPositionListener = nullptr;
269 mPositionListenerDirty = false;
270 }
271
John Reckff941dc2014-05-14 16:34:14 -0700272 // Push the animators first so that setupStartValueIfNecessary() is called
273 // before properties() is trampled by stagingProperties(), as they are
274 // required by some animators.
John Reck9eb9f6f2014-08-21 11:23:05 -0700275 if (CC_LIKELY(info.runAnimations)) {
John Reck119907c2014-08-14 09:02:01 -0700276 mAnimatorManager.pushStaging();
John Reck9eb9f6f2014-08-21 11:23:05 -0700277 }
John Reckff941dc2014-05-14 16:34:14 -0700278 if (mDirtyPropertyFields) {
279 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700280 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700281 info.damageAccumulator->popTransform();
Chris Craikb565df12015-10-05 13:00:52 -0700282 syncProperties();
John Recke4267ea2014-06-03 15:53:15 -0700283 // We could try to be clever and only re-damage if the matrix changed.
284 // However, we don't need to worry about that. The cost of over-damaging
285 // here is only going to be a single additional map rect of this node
286 // plus a rect join(). The parent's transform (and up) will only be
287 // performed once.
John Recka447d292014-06-11 18:39:44 -0700288 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700289 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700290 }
John Reck25fbb3f2014-06-12 13:46:45 -0700291}
292
John Reck2de950d2017-01-25 10:58:30 -0800293void RenderNode::syncDisplayList(TreeObserver& observer, TreeInfo* info) {
Chris Craikb565df12015-10-05 13:00:52 -0700294 // Make sure we inc first so that we don't fluctuate between 0 and 1,
295 // which would thrash the layer cache
Chris Craik003cc3d2015-10-16 10:24:55 -0700296 if (mStagingDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -0700297 mStagingDisplayList->updateChildren([](RenderNode* child) { child->incParentRefCount(); });
Chris Craikb565df12015-10-05 13:00:52 -0700298 }
John Reck2de950d2017-01-25 10:58:30 -0800299 deleteDisplayList(observer, info);
Chris Craik003cc3d2015-10-16 10:24:55 -0700300 mDisplayList = mStagingDisplayList;
301 mStagingDisplayList = nullptr;
302 if (mDisplayList) {
John Reck283bb462018-12-13 16:40:14 -0800303 WebViewSyncData syncData {
304 .applyForceDark = info && !info->disableForceDark
305 };
306 mDisplayList->syncContents(syncData);
John Reck3b4510cd2018-09-27 17:39:45 -0700307 handleForceDark(info);
308 }
309}
John Reckf3c724f2018-09-20 13:00:04 -0700310
John Reck3b4510cd2018-09-27 17:39:45 -0700311void RenderNode::handleForceDark(android::uirenderer::TreeInfo *info) {
312 if (CC_LIKELY(!info || info->disableForceDark)) {
313 return;
314 }
315 auto usage = usageHint();
316 const auto& children = mDisplayList->mChildNodes;
317 if (mDisplayList->hasText()) {
318 usage = UsageHint::Foreground;
319 }
320 if (usage == UsageHint::Unknown) {
321 if (children.size() > 1) {
322 usage = UsageHint::Background;
323 } else if (children.size() == 1 &&
324 children.front().getRenderNode()->usageHint() !=
325 UsageHint::Background) {
326 usage = UsageHint::Background;
John Reck8f45d4a2018-08-15 10:17:12 -0700327 }
Chris Craikb565df12015-10-05 13:00:52 -0700328 }
John Reck3b4510cd2018-09-27 17:39:45 -0700329 if (children.size() > 1) {
330 // Crude overlap check
331 SkRect drawn = SkRect::MakeEmpty();
332 for (auto iter = children.rbegin(); iter != children.rend(); ++iter) {
333 const auto& child = iter->getRenderNode();
334 // We use stagingProperties here because we haven't yet sync'd the children
335 SkRect bounds = SkRect::MakeXYWH(child->stagingProperties().getX(), child->stagingProperties().getY(),
336 child->stagingProperties().getWidth(), child->stagingProperties().getHeight());
337 if (bounds.contains(drawn)) {
338 // This contains everything drawn after it, so make it a background
339 child->setUsageHint(UsageHint::Background);
340 }
341 drawn.join(bounds);
342 }
343 }
344 mDisplayList->mDisplayList.applyColorTransform(
345 usage == UsageHint::Background ? ColorTransform::Dark : ColorTransform::Light);
Chris Craikb565df12015-10-05 13:00:52 -0700346}
347
John Reck2de950d2017-01-25 10:58:30 -0800348void RenderNode::pushStagingDisplayListChanges(TreeObserver& observer, TreeInfo& info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700349 if (mNeedsDisplayListSync) {
350 mNeedsDisplayListSync = false;
John Reck5c9d7172014-10-22 11:32:27 -0700351 // Damage with the old display list first then the new one to catch any
352 // changes in isRenderable or, in the future, bounds
353 damageSelf(info);
John Reck2de950d2017-01-25 10:58:30 -0800354 syncDisplayList(observer, &info);
John Recke4267ea2014-06-03 15:53:15 -0700355 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700356 }
John Reck8de65a82014-04-09 15:23:38 -0700357}
358
John Reck2de950d2017-01-25 10:58:30 -0800359void RenderNode::deleteDisplayList(TreeObserver& observer, TreeInfo* info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700360 if (mDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -0700361 mDisplayList->updateChildren(
362 [&observer, info](RenderNode* child) { child->decParentRefCount(observer, info); });
Derek Sollenberger0df62092016-09-27 16:04:42 -0400363 if (!mDisplayList->reuseDisplayList(this, info ? &info->canvasContext : nullptr)) {
364 delete mDisplayList;
John Reckdcba6722014-07-08 13:59:49 -0700365 }
366 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700367 mDisplayList = nullptr;
John Reckdcba6722014-07-08 13:59:49 -0700368}
369
John Reck2de950d2017-01-25 10:58:30 -0800370void RenderNode::destroyHardwareResources(TreeInfo* info) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400371 if (hasLayer()) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400372 this->setLayerSurface(nullptr);
John Reckdcba6722014-07-08 13:59:49 -0700373 }
John Reck3afd6372017-01-30 10:15:48 -0800374 setStagingDisplayList(nullptr);
375
376 ImmediateRemoved observer(info);
377 deleteDisplayList(observer, info);
John Reck2de950d2017-01-25 10:58:30 -0800378}
379
380void RenderNode::destroyLayers() {
381 if (hasLayer()) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400382 this->setLayerSurface(nullptr);
John Reck2de950d2017-01-25 10:58:30 -0800383 }
384 if (mDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -0700385 mDisplayList->updateChildren([](RenderNode* child) { child->destroyLayers(); });
John Reck2de950d2017-01-25 10:58:30 -0800386 }
387}
388
389void RenderNode::decParentRefCount(TreeObserver& observer, TreeInfo* info) {
390 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
391 mParentCount--;
392 if (!mParentCount) {
393 observer.onMaybeRemovedFromTree(this);
394 if (CC_UNLIKELY(mPositionListener.get())) {
395 mPositionListener->onPositionLost(*this, info);
John Reckdcba6722014-07-08 13:59:49 -0700396 }
397 }
398}
399
John Reck2de950d2017-01-25 10:58:30 -0800400void RenderNode::onRemovedFromTree(TreeInfo* info) {
401 destroyHardwareResources(info);
402}
403
404void RenderNode::clearRoot() {
405 ImmediateRemoved observer(nullptr);
406 decParentRefCount(observer);
John Reckdcba6722014-07-08 13:59:49 -0700407}
408
John Reck113e0822014-03-18 09:22:59 -0700409/**
410 * Apply property-based transformations to input matrix
411 *
412 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
413 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
414 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700415void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700416 if (properties().getLeft() != 0 || properties().getTop() != 0) {
417 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700418 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700419 if (properties().getStaticMatrix()) {
420 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700421 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700422 } else if (properties().getAnimationMatrix()) {
423 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700424 matrix.multiply(anim);
425 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700426
Chris Craikcc39e162014-04-25 18:34:11 -0700427 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700428 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700429 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700430 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
John Reck1bcacfd2017-11-03 10:12:19 -0700431 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700432 } else {
433 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700434 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700435 } else {
436 mat4 true3dMat;
John Reck1bcacfd2017-11-03 10:12:19 -0700437 true3dMat.loadTranslate(properties().getPivotX() + properties().getTranslationX(),
438 properties().getPivotY() + properties().getTranslationY(),
439 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700440 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
441 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
442 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
443 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
444 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700445
446 matrix.multiply(true3dMat);
447 }
448 }
449 }
450}
451
Derek Sollenberger579317d42017-08-29 16:33:49 -0400452const SkPath* RenderNode::getClippedOutline(const SkRect& clipRect) const {
453 const SkPath* outlinePath = properties().getOutline().getPath();
454 const uint32_t outlineID = outlinePath->getGenerationID();
455
456 if (outlineID != mClippedOutlineCache.outlineID || clipRect != mClippedOutlineCache.clipRect) {
457 // update the cache keys
458 mClippedOutlineCache.outlineID = outlineID;
459 mClippedOutlineCache.clipRect = clipRect;
460
461 // update the cache value by recomputing a new path
462 SkPath clipPath;
463 clipPath.addRect(clipRect);
464 Op(*outlinePath, clipPath, kIntersect_SkPathOp, &mClippedOutlineCache.clippedOutline);
Derek Sollenberger579317d42017-08-29 16:33:49 -0400465 }
466 return &mClippedOutlineCache.clippedOutline;
467}
468
John Reckf96b2842018-11-29 09:44:10 -0800469using StringBuffer = FatVector<char, 128>;
470
471template <typename... T>
John Reck7af5b2c2018-12-05 13:36:20 -0800472// TODO:__printflike(2, 3)
473// Doesn't work because the warning doesn't understand string_view and doesn't like that
474// it's not a C-style variadic function.
John Reckf96b2842018-11-29 09:44:10 -0800475static void format(StringBuffer& buffer, const std::string_view& format, T... args) {
476 buffer.resize(buffer.capacity());
477 while (1) {
478 int needed = snprintf(buffer.data(), buffer.size(),
479 format.data(), std::forward<T>(args)...);
480 if (needed < 0) {
481 buffer[0] = '\0';
482 buffer.resize(1);
483 return;
484 }
485 if (needed < buffer.size()) {
486 buffer.resize(needed + 1);
487 return;
488 }
John Reck7af5b2c2018-12-05 13:36:20 -0800489 // If we're doing a heap alloc anyway might as well give it some slop
490 buffer.resize(needed + 100);
John Reckf96b2842018-11-29 09:44:10 -0800491 }
492}
493
494void RenderNode::markDrawStart(SkCanvas& canvas) {
495 StringBuffer buffer;
John Reck7af5b2c2018-12-05 13:36:20 -0800496 format(buffer, "RenderNode(id=%" PRId64 ", name='%s')", uniqueId(), getName());
John Reckf96b2842018-11-29 09:44:10 -0800497 canvas.drawAnnotation(SkRect::MakeWH(getWidth(), getHeight()), buffer.data(), nullptr);
498}
499
500void RenderNode::markDrawEnd(SkCanvas& canvas) {
501 StringBuffer buffer;
John Reck7af5b2c2018-12-05 13:36:20 -0800502 format(buffer, "/RenderNode(id=%" PRId64 ", name='%s')", uniqueId(), getName());
John Reckf96b2842018-11-29 09:44:10 -0800503 canvas.drawAnnotation(SkRect::MakeWH(getWidth(), getHeight()), buffer.data(), nullptr);
504}
505
John Reck113e0822014-03-18 09:22:59 -0700506} /* namespace uirenderer */
507} /* namespace android */