blob: 8aee8f5b1848a132e772533148403985f3191b26 [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"
Fedor Kudasov86bd2142019-06-18 15:51:57 +010023#ifdef __ANDROID__
John Reck1bcacfd2017-11-03 10:12:19 -070024#include "renderthread/CanvasContext.h"
Fedor Kudasov86bd2142019-06-18 15:51:57 +010025#else
26#include "DamageAccumulator.h"
27#include "pipeline/skia/SkiaDisplayList.h"
28#endif
John Reck2de950d2017-01-25 10:58:30 -080029#include "utils/FatVector.h"
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>
39
John Reck113e0822014-03-18 09:22:59 -070040namespace android {
41namespace uirenderer {
42
John Reck2de950d2017-01-25 10:58:30 -080043// Used for tree mutations that are purely destructive.
44// Generic tree mutations should use MarkAndSweepObserver instead
45class ImmediateRemoved : public TreeObserver {
46public:
47 explicit ImmediateRemoved(TreeInfo* info) : mTreeInfo(info) {}
48
John Reck1bcacfd2017-11-03 10:12:19 -070049 void onMaybeRemovedFromTree(RenderNode* node) override { node->onRemovedFromTree(mTreeInfo); }
John Reck2de950d2017-01-25 10:58:30 -080050
51private:
52 TreeInfo* mTreeInfo;
53};
54
John Reckf96b2842018-11-29 09:44:10 -080055static int64_t generateId() {
56 static std::atomic<int64_t> sNextId{1};
57 return sNextId++;
58}
59
John Reck8de65a82014-04-09 15:23:38 -070060RenderNode::RenderNode()
John Reckf96b2842018-11-29 09:44:10 -080061 : mUniqueId(generateId())
62 , mDirtyPropertyFields(0)
Chris Craik003cc3d2015-10-16 10:24:55 -070063 , mNeedsDisplayListSync(false)
64 , mDisplayList(nullptr)
65 , mStagingDisplayList(nullptr)
John Reck68bfe0a2014-06-24 15:34:58 -070066 , mAnimatorManager(*this)
John Reck1bcacfd2017-11-03 10:12:19 -070067 , mParentCount(0) {}
John Reck113e0822014-03-18 09:22:59 -070068
69RenderNode::~RenderNode() {
John Reck2de950d2017-01-25 10:58:30 -080070 ImmediateRemoved observer(nullptr);
71 deleteDisplayList(observer);
Chris Craik003cc3d2015-10-16 10:24:55 -070072 delete mStagingDisplayList;
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 Reck2de950d2017-01-25 10:58:30 -080076void RenderNode::setStagingDisplayList(DisplayList* displayList) {
77 mValid = (displayList != nullptr);
Chris Craik003cc3d2015-10-16 10:24:55 -070078 mNeedsDisplayListSync = true;
79 delete mStagingDisplayList;
80 mStagingDisplayList = displayList;
John Reck113e0822014-03-18 09:22:59 -070081}
82
83/**
84 * This function is a simplified version of replay(), where we simply retrieve and log the
85 * display list. This function should remain in sync with the replay() function.
86 */
sergeyvc3849aa2016-08-08 13:22:06 -070087void RenderNode::output() {
88 LogcatStream strout;
89 strout << "Root";
90 output(strout, 0);
91}
92
93void RenderNode::output(std::ostream& output, uint32_t level) {
94 output << " (" << getName() << " " << this
John Reck1bcacfd2017-11-03 10:12:19 -070095 << (MathUtils::isZero(properties().getAlpha()) ? ", zero alpha" : "")
96 << (properties().hasShadow() ? ", casting shadow" : "")
97 << (isRenderable() ? "" : ", empty")
98 << (properties().getProjectBackwards() ? ", projected" : "")
99 << (hasLayer() ? ", on HW Layer" : "") << ")" << std::endl;
sergeyvc3849aa2016-08-08 13:22:06 -0700100
101 properties().debugOutputProperties(output, level + 1);
Chris Craik91eff222016-02-22 13:39:33 -0800102
103 if (mDisplayList) {
Stan Ilievd2172372017-02-09 16:59:27 -0500104 mDisplayList->output(output, level);
Chris Craik91eff222016-02-22 13:39:33 -0800105 }
sergeyvc3849aa2016-08-08 13:22:06 -0700106 output << std::string(level * 2, ' ') << "/RenderNode(" << getName() << " " << this << ")";
107 output << std::endl;
Chris Craik91eff222016-02-22 13:39:33 -0800108}
John Reck113e0822014-03-18 09:22:59 -0700109
John Reckfe5e7b72014-05-23 17:42:28 -0700110int RenderNode::getDebugSize() {
111 int size = sizeof(RenderNode);
Chris Craik003cc3d2015-10-16 10:24:55 -0700112 if (mStagingDisplayList) {
113 size += mStagingDisplayList->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700114 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700115 if (mDisplayList && mDisplayList != mStagingDisplayList) {
116 size += mDisplayList->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700117 }
118 return size;
119}
120
John Reckf4198b72014-04-09 17:00:04 -0700121void RenderNode::prepareTree(TreeInfo& info) {
122 ATRACE_CALL();
Chris Craik69e5adf2014-08-14 13:34:01 -0700123 LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
John Reck2de950d2017-01-25 10:58:30 -0800124 MarkAndSweepRemoved observer(&info);
John Reckf4198b72014-04-09 17:00:04 -0700125
John Reck1423e132018-09-21 14:30:19 -0700126 const int before = info.disableForceDark;
John Reck1072fff2018-04-12 15:20:09 -0700127 prepareTreeImpl(observer, info, false);
John Reck1423e132018-09-21 14:30:19 -0700128 LOG_ALWAYS_FATAL_IF(before != info.disableForceDark, "Mis-matched force dark");
John Reckf4198b72014-04-09 17:00:04 -0700129}
130
John Reck68bfe0a2014-06-24 15:34:58 -0700131void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
132 mAnimatorManager.addAnimator(animator);
133}
134
Doris Liu8b083202016-02-19 21:46:06 +0000135void RenderNode::removeAnimator(const sp<BaseRenderNodeAnimator>& animator) {
136 mAnimatorManager.removeAnimator(animator);
137}
138
John Recke4267ea2014-06-03 15:53:15 -0700139void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700140 if (isRenderable()) {
John Reckf1aa7909e2019-03-07 17:01:08 -0800141 mDamageGenerationId = info.damageGenerationId;
John Reck293e8682014-06-17 10:34:02 -0700142 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700143 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
144 } else {
145 // Hope this is big enough?
146 // TODO: Get this from the display list ops or something
John Reckc1288232015-08-12 13:39:11 -0700147 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
John Recka447d292014-06-11 18:39:44 -0700148 }
John Recke4267ea2014-06-03 15:53:15 -0700149 }
150}
151
John Recka7c2ea22014-08-08 13:21:00 -0700152void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700153 LayerType layerType = properties().effectiveLayerType();
Chris Craik182952f2015-03-09 14:17:29 -0700154 if (CC_UNLIKELY(layerType == LayerType::RenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700155 // Damage applied so far needs to affect our parent, but does not require
156 // the layer to be updated. So we pop/push here to clear out the current
157 // damage and get a clean state for display list or children updates to
158 // affect, which will require the layer to be updated
159 info.damageAccumulator->popTransform();
160 info.damageAccumulator->pushTransform(this);
161 if (dirtyMask & DISPLAY_LIST) {
162 damageSelf(info);
163 }
John Reck25fbb3f2014-06-12 13:46:45 -0700164 }
165}
166
167void RenderNode::pushLayerUpdate(TreeInfo& info) {
Fedor Kudasov86bd2142019-06-18 15:51:57 +0100168#ifdef __ANDROID__ // Layoutlib does not support CanvasContext and Layers
Chris Craik856f0cc2015-04-21 15:13:29 -0700169 LayerType layerType = properties().effectiveLayerType();
John Reck25fbb3f2014-06-12 13:46:45 -0700170 // If we are not a layer OR we cannot be rendered (eg, view was detached)
171 // we need to destroy any Layers we may have had previously
John Reck1bcacfd2017-11-03 10:12:19 -0700172 if (CC_LIKELY(layerType != LayerType::RenderLayer) || CC_UNLIKELY(!isRenderable()) ||
173 CC_UNLIKELY(properties().getWidth() == 0) || CC_UNLIKELY(properties().getHeight() == 0) ||
174 CC_UNLIKELY(!properties().fitsOnLayer())) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400175 if (CC_UNLIKELY(hasLayer())) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400176 this->setLayerSurface(nullptr);
John Reck25fbb3f2014-06-12 13:46:45 -0700177 }
178 return;
179 }
180
Stan Iliev216b1572018-03-26 14:29:50 -0400181 if (info.canvasContext.createOrUpdateLayer(this, *info.damageAccumulator, info.errorHandler)) {
Chris Craik9fded232015-11-11 16:42:34 -0800182 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700183 }
184
Derek Sollenberger0df62092016-09-27 16:04:42 -0400185 if (!hasLayer()) {
John Reckc25e5062014-06-18 14:21:29 -0700186 return;
187 }
188
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400189 SkRect dirty;
190 info.damageAccumulator->peekAtDirty(&dirty);
Chris Craik0b7e8242015-10-28 16:50:44 -0700191 info.layerUpdateQueue->enqueueLayerWithDamage(this, dirty);
John Reck998a6d82014-08-28 15:35:53 -0700192
Chris Craike2e53a72015-10-28 15:55:40 -0700193 // There might be prefetched layers that need to be accounted for.
194 // That might be us, so tell CanvasContext that this layer is in the
195 // tree and should not be destroyed.
196 info.canvasContext.markLayerInUse(this);
Fedor Kudasov86bd2142019-06-18 15:51:57 +0100197#endif
John Reck25fbb3f2014-06-12 13:46:45 -0700198}
199
Chris Craika766cb22015-06-08 16:49:43 -0700200/**
201 * Traverse down the the draw tree to prepare for a frame.
202 *
203 * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven
204 *
205 * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the
206 * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer.
207 */
John Reck2de950d2017-01-25 10:58:30 -0800208void RenderNode::prepareTreeImpl(TreeObserver& observer, TreeInfo& info, bool functorsNeedLayer) {
John Reckf1aa7909e2019-03-07 17:01:08 -0800209 if (mDamageGenerationId == info.damageGenerationId) {
210 // We hit the same node a second time in the same tree. We don't know the minimal
211 // damage rect anymore, so just push the biggest we can onto our parent's transform
212 // We push directly onto parent in case we are clipped to bounds but have moved position.
213 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
214 }
John Recka447d292014-06-11 18:39:44 -0700215 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700216
John Reckdcba6722014-07-08 13:59:49 -0700217 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700218 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700219 }
John Reck1423e132018-09-21 14:30:19 -0700220
221 if (!mProperties.getAllowForceDark()) {
222 info.disableForceDark++;
223 }
224
John Reck9eb9f6f2014-08-21 11:23:05 -0700225 uint32_t animatorDirtyMask = 0;
226 if (CC_LIKELY(info.runAnimations)) {
227 animatorDirtyMask = mAnimatorManager.animate(info);
228 }
Chris Craika766cb22015-06-08 16:49:43 -0700229
John Reck3f725f02015-06-16 10:29:31 -0700230 bool willHaveFunctor = false;
Chris Craik003cc3d2015-10-16 10:24:55 -0700231 if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400232 willHaveFunctor = mStagingDisplayList->hasFunctor();
Chris Craik003cc3d2015-10-16 10:24:55 -0700233 } else if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400234 willHaveFunctor = mDisplayList->hasFunctor();
John Reck3f725f02015-06-16 10:29:31 -0700235 }
John Reck1bcacfd2017-11-03 10:12:19 -0700236 bool childFunctorsNeedLayer =
237 mProperties.prepareForFunctorPresence(willHaveFunctor, functorsNeedLayer);
Chris Craika766cb22015-06-08 16:49:43 -0700238
John Reckf6481082016-02-02 15:18:23 -0800239 if (CC_UNLIKELY(mPositionListener.get())) {
240 mPositionListener->onPositionUpdated(*this, info);
241 }
242
John Recka7c2ea22014-08-08 13:21:00 -0700243 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700244 if (info.mode == TreeInfo::MODE_FULL) {
John Reck2de950d2017-01-25 10:58:30 -0800245 pushStagingDisplayListChanges(observer, info);
John Reck25fbb3f2014-06-12 13:46:45 -0700246 }
John Reck25fbb3f2014-06-12 13:46:45 -0700247
Doris Liu07c056d2016-06-13 12:52:44 -0700248 if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400249 info.out.hasFunctors |= mDisplayList->hasFunctor();
John Reck1bcacfd2017-11-03 10:12:19 -0700250 bool isDirty = mDisplayList->prepareListAndChildren(
251 observer, info, childFunctorsNeedLayer,
252 [](RenderNode* child, TreeObserver& observer, TreeInfo& info,
253 bool functorsNeedLayer) {
254 child->prepareTreeImpl(observer, info, functorsNeedLayer);
255 });
Derek Sollenberger0df62092016-09-27 16:04:42 -0400256 if (isDirty) {
257 damageSelf(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700258 }
Doris Liu718cd3e2016-05-17 16:50:31 -0700259 }
Doris Liub51b2862016-08-01 19:56:47 -0700260 pushLayerUpdate(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700261
John Reck1423e132018-09-21 14:30:19 -0700262 if (!mProperties.getAllowForceDark()) {
263 info.disableForceDark--;
264 }
John Recka447d292014-06-11 18:39:44 -0700265 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700266}
267
Chris Craikb565df12015-10-05 13:00:52 -0700268void RenderNode::syncProperties() {
269 mProperties = mStagingProperties;
270}
271
John Reck25fbb3f2014-06-12 13:46:45 -0700272void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reck097e1d32019-06-12 15:01:51 -0700273 if (mPositionListenerDirty) {
274 mPositionListener = std::move(mStagingPositionListener);
275 mStagingPositionListener = nullptr;
276 mPositionListenerDirty = false;
277 }
278
John Reckff941dc2014-05-14 16:34:14 -0700279 // Push the animators first so that setupStartValueIfNecessary() is called
280 // before properties() is trampled by stagingProperties(), as they are
281 // required by some animators.
John Reck9eb9f6f2014-08-21 11:23:05 -0700282 if (CC_LIKELY(info.runAnimations)) {
John Reck119907c2014-08-14 09:02:01 -0700283 mAnimatorManager.pushStaging();
John Reck9eb9f6f2014-08-21 11:23:05 -0700284 }
John Reckff941dc2014-05-14 16:34:14 -0700285 if (mDirtyPropertyFields) {
286 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700287 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700288 info.damageAccumulator->popTransform();
Chris Craikb565df12015-10-05 13:00:52 -0700289 syncProperties();
John Recke4267ea2014-06-03 15:53:15 -0700290 // We could try to be clever and only re-damage if the matrix changed.
291 // However, we don't need to worry about that. The cost of over-damaging
292 // here is only going to be a single additional map rect of this node
293 // plus a rect join(). The parent's transform (and up) will only be
294 // performed once.
John Recka447d292014-06-11 18:39:44 -0700295 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700296 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700297 }
John Reck25fbb3f2014-06-12 13:46:45 -0700298}
299
John Reck2de950d2017-01-25 10:58:30 -0800300void RenderNode::syncDisplayList(TreeObserver& observer, TreeInfo* info) {
Chris Craikb565df12015-10-05 13:00:52 -0700301 // Make sure we inc first so that we don't fluctuate between 0 and 1,
302 // which would thrash the layer cache
Chris Craik003cc3d2015-10-16 10:24:55 -0700303 if (mStagingDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -0700304 mStagingDisplayList->updateChildren([](RenderNode* child) { child->incParentRefCount(); });
Chris Craikb565df12015-10-05 13:00:52 -0700305 }
John Reck2de950d2017-01-25 10:58:30 -0800306 deleteDisplayList(observer, info);
Chris Craik003cc3d2015-10-16 10:24:55 -0700307 mDisplayList = mStagingDisplayList;
308 mStagingDisplayList = nullptr;
309 if (mDisplayList) {
John Reck283bb462018-12-13 16:40:14 -0800310 WebViewSyncData syncData {
311 .applyForceDark = info && !info->disableForceDark
312 };
313 mDisplayList->syncContents(syncData);
John Reck3b4510cd2018-09-27 17:39:45 -0700314 handleForceDark(info);
315 }
316}
John Reckf3c724f2018-09-20 13:00:04 -0700317
John Reck3b4510cd2018-09-27 17:39:45 -0700318void RenderNode::handleForceDark(android::uirenderer::TreeInfo *info) {
319 if (CC_LIKELY(!info || info->disableForceDark)) {
320 return;
321 }
322 auto usage = usageHint();
323 const auto& children = mDisplayList->mChildNodes;
324 if (mDisplayList->hasText()) {
325 usage = UsageHint::Foreground;
326 }
327 if (usage == UsageHint::Unknown) {
328 if (children.size() > 1) {
329 usage = UsageHint::Background;
330 } else if (children.size() == 1 &&
331 children.front().getRenderNode()->usageHint() !=
332 UsageHint::Background) {
333 usage = UsageHint::Background;
John Reck8f45d4a2018-08-15 10:17:12 -0700334 }
Chris Craikb565df12015-10-05 13:00:52 -0700335 }
John Reck3b4510cd2018-09-27 17:39:45 -0700336 if (children.size() > 1) {
337 // Crude overlap check
338 SkRect drawn = SkRect::MakeEmpty();
339 for (auto iter = children.rbegin(); iter != children.rend(); ++iter) {
340 const auto& child = iter->getRenderNode();
341 // We use stagingProperties here because we haven't yet sync'd the children
342 SkRect bounds = SkRect::MakeXYWH(child->stagingProperties().getX(), child->stagingProperties().getY(),
343 child->stagingProperties().getWidth(), child->stagingProperties().getHeight());
344 if (bounds.contains(drawn)) {
345 // This contains everything drawn after it, so make it a background
346 child->setUsageHint(UsageHint::Background);
347 }
348 drawn.join(bounds);
349 }
350 }
351 mDisplayList->mDisplayList.applyColorTransform(
352 usage == UsageHint::Background ? ColorTransform::Dark : ColorTransform::Light);
Chris Craikb565df12015-10-05 13:00:52 -0700353}
354
John Reck2de950d2017-01-25 10:58:30 -0800355void RenderNode::pushStagingDisplayListChanges(TreeObserver& observer, TreeInfo& info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700356 if (mNeedsDisplayListSync) {
357 mNeedsDisplayListSync = false;
John Reck5c9d7172014-10-22 11:32:27 -0700358 // Damage with the old display list first then the new one to catch any
359 // changes in isRenderable or, in the future, bounds
360 damageSelf(info);
John Reck2de950d2017-01-25 10:58:30 -0800361 syncDisplayList(observer, &info);
John Recke4267ea2014-06-03 15:53:15 -0700362 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700363 }
John Reck8de65a82014-04-09 15:23:38 -0700364}
365
John Reck2de950d2017-01-25 10:58:30 -0800366void RenderNode::deleteDisplayList(TreeObserver& observer, TreeInfo* info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700367 if (mDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -0700368 mDisplayList->updateChildren(
369 [&observer, info](RenderNode* child) { child->decParentRefCount(observer, info); });
Derek Sollenberger0df62092016-09-27 16:04:42 -0400370 if (!mDisplayList->reuseDisplayList(this, info ? &info->canvasContext : nullptr)) {
371 delete mDisplayList;
John Reckdcba6722014-07-08 13:59:49 -0700372 }
373 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700374 mDisplayList = nullptr;
John Reckdcba6722014-07-08 13:59:49 -0700375}
376
John Reck2de950d2017-01-25 10:58:30 -0800377void RenderNode::destroyHardwareResources(TreeInfo* info) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400378 if (hasLayer()) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400379 this->setLayerSurface(nullptr);
John Reckdcba6722014-07-08 13:59:49 -0700380 }
John Reck3afd6372017-01-30 10:15:48 -0800381 setStagingDisplayList(nullptr);
382
383 ImmediateRemoved observer(info);
384 deleteDisplayList(observer, info);
John Reck2de950d2017-01-25 10:58:30 -0800385}
386
387void RenderNode::destroyLayers() {
388 if (hasLayer()) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400389 this->setLayerSurface(nullptr);
John Reck2de950d2017-01-25 10:58:30 -0800390 }
391 if (mDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -0700392 mDisplayList->updateChildren([](RenderNode* child) { child->destroyLayers(); });
John Reck2de950d2017-01-25 10:58:30 -0800393 }
394}
395
396void RenderNode::decParentRefCount(TreeObserver& observer, TreeInfo* info) {
397 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
398 mParentCount--;
399 if (!mParentCount) {
400 observer.onMaybeRemovedFromTree(this);
401 if (CC_UNLIKELY(mPositionListener.get())) {
402 mPositionListener->onPositionLost(*this, info);
John Reckdcba6722014-07-08 13:59:49 -0700403 }
404 }
405}
406
John Reck2de950d2017-01-25 10:58:30 -0800407void RenderNode::onRemovedFromTree(TreeInfo* info) {
408 destroyHardwareResources(info);
409}
410
411void RenderNode::clearRoot() {
412 ImmediateRemoved observer(nullptr);
413 decParentRefCount(observer);
John Reckdcba6722014-07-08 13:59:49 -0700414}
415
John Reck113e0822014-03-18 09:22:59 -0700416/**
417 * Apply property-based transformations to input matrix
418 *
419 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
420 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
421 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700422void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700423 if (properties().getLeft() != 0 || properties().getTop() != 0) {
424 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700425 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700426 if (properties().getStaticMatrix()) {
427 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700428 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700429 } else if (properties().getAnimationMatrix()) {
430 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700431 matrix.multiply(anim);
432 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700433
Chris Craikcc39e162014-04-25 18:34:11 -0700434 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700435 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700436 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700437 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
John Reck1bcacfd2017-11-03 10:12:19 -0700438 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700439 } else {
440 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700441 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700442 } else {
443 mat4 true3dMat;
John Reck1bcacfd2017-11-03 10:12:19 -0700444 true3dMat.loadTranslate(properties().getPivotX() + properties().getTranslationX(),
445 properties().getPivotY() + properties().getTranslationY(),
446 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700447 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
448 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
449 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
450 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
451 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700452
453 matrix.multiply(true3dMat);
454 }
455 }
456 }
457}
458
Derek Sollenberger579317d42017-08-29 16:33:49 -0400459const SkPath* RenderNode::getClippedOutline(const SkRect& clipRect) const {
460 const SkPath* outlinePath = properties().getOutline().getPath();
461 const uint32_t outlineID = outlinePath->getGenerationID();
462
463 if (outlineID != mClippedOutlineCache.outlineID || clipRect != mClippedOutlineCache.clipRect) {
464 // update the cache keys
465 mClippedOutlineCache.outlineID = outlineID;
466 mClippedOutlineCache.clipRect = clipRect;
467
468 // update the cache value by recomputing a new path
469 SkPath clipPath;
470 clipPath.addRect(clipRect);
471 Op(*outlinePath, clipPath, kIntersect_SkPathOp, &mClippedOutlineCache.clippedOutline);
Derek Sollenberger579317d42017-08-29 16:33:49 -0400472 }
473 return &mClippedOutlineCache.clippedOutline;
474}
475
John Reckf96b2842018-11-29 09:44:10 -0800476using StringBuffer = FatVector<char, 128>;
477
478template <typename... T>
John Reck7af5b2c2018-12-05 13:36:20 -0800479// TODO:__printflike(2, 3)
480// Doesn't work because the warning doesn't understand string_view and doesn't like that
481// it's not a C-style variadic function.
John Reckf96b2842018-11-29 09:44:10 -0800482static void format(StringBuffer& buffer, const std::string_view& format, T... args) {
483 buffer.resize(buffer.capacity());
484 while (1) {
485 int needed = snprintf(buffer.data(), buffer.size(),
486 format.data(), std::forward<T>(args)...);
487 if (needed < 0) {
488 buffer[0] = '\0';
489 buffer.resize(1);
490 return;
491 }
492 if (needed < buffer.size()) {
493 buffer.resize(needed + 1);
494 return;
495 }
John Reck7af5b2c2018-12-05 13:36:20 -0800496 // If we're doing a heap alloc anyway might as well give it some slop
497 buffer.resize(needed + 100);
John Reckf96b2842018-11-29 09:44:10 -0800498 }
499}
500
501void RenderNode::markDrawStart(SkCanvas& canvas) {
502 StringBuffer buffer;
John Reck7af5b2c2018-12-05 13:36:20 -0800503 format(buffer, "RenderNode(id=%" PRId64 ", name='%s')", uniqueId(), getName());
John Reckf96b2842018-11-29 09:44:10 -0800504 canvas.drawAnnotation(SkRect::MakeWH(getWidth(), getHeight()), buffer.data(), nullptr);
505}
506
507void RenderNode::markDrawEnd(SkCanvas& canvas) {
508 StringBuffer buffer;
John Reck7af5b2c2018-12-05 13:36:20 -0800509 format(buffer, "/RenderNode(id=%" PRId64 ", name='%s')", uniqueId(), getName());
John Reckf96b2842018-11-29 09:44:10 -0800510 canvas.drawAnnotation(SkRect::MakeWH(getWidth(), getHeight()), buffer.data(), nullptr);
511}
512
John Reck113e0822014-03-18 09:22:59 -0700513} /* namespace uirenderer */
514} /* namespace android */