blob: 1c39db3a31bbeb7f8c1df3f0195040a9a4b71af2 [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"
Huihong Luoec68b7c2021-06-25 21:54:16 -070021#include "Properties.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040022#include "TreeInfo.h"
John Reck1bcacfd2017-11-03 10:12:19 -070023#include "VectorDrawable.h"
Derek Sollenberger5d0ca632019-07-19 16:17:12 -040024#include "private/hwui/WebViewFunctor.h"
Fedor Kudasov86bd2142019-06-18 15:51:57 +010025#ifdef __ANDROID__
John Reck1bcacfd2017-11-03 10:12:19 -070026#include "renderthread/CanvasContext.h"
Fedor Kudasov86bd2142019-06-18 15:51:57 +010027#else
28#include "DamageAccumulator.h"
29#include "pipeline/skia/SkiaDisplayList.h"
30#endif
rnleece9762b2021-05-21 15:40:53 -070031#include <gui/TraceUtils.h>
Chris Craike0bb87d2014-04-22 17:55:41 -070032#include "utils/MathUtils.h"
sergeyvc3849aa2016-08-08 13:22:06 -070033#include "utils/StringUtils.h"
John Reck113e0822014-03-18 09:22:59 -070034
Derek Sollenberger579317d42017-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>
Jagadeesh Pakaravoorb624af32020-05-01 00:01:40 +000040#include <ui/FatVector.h>
John Reck77c40102015-10-26 15:49:47 -070041
John Reck113e0822014-03-18 09:22:59 -070042namespace android {
43namespace uirenderer {
44
John Reck2de950d2017-01-25 10:58:30 -080045// Used for tree mutations that are purely destructive.
46// Generic tree mutations should use MarkAndSweepObserver instead
47class ImmediateRemoved : public TreeObserver {
48public:
49 explicit ImmediateRemoved(TreeInfo* info) : mTreeInfo(info) {}
50
John Reck1bcacfd2017-11-03 10:12:19 -070051 void onMaybeRemovedFromTree(RenderNode* node) override { node->onRemovedFromTree(mTreeInfo); }
John Reck2de950d2017-01-25 10:58:30 -080052
53private:
54 TreeInfo* mTreeInfo;
55};
56
John Reckf96b2842018-11-29 09:44:10 -080057static int64_t generateId() {
58 static std::atomic<int64_t> sNextId{1};
59 return sNextId++;
60}
61
John Reck8de65a82014-04-09 15:23:38 -070062RenderNode::RenderNode()
John Reckf96b2842018-11-29 09:44:10 -080063 : mUniqueId(generateId())
64 , mDirtyPropertyFields(0)
Chris Craik003cc3d2015-10-16 10:24:55 -070065 , mNeedsDisplayListSync(false)
66 , mDisplayList(nullptr)
67 , mStagingDisplayList(nullptr)
John Reck68bfe0a2014-06-24 15:34:58 -070068 , mAnimatorManager(*this)
John Reck1bcacfd2017-11-03 10:12:19 -070069 , mParentCount(0) {}
John Reck113e0822014-03-18 09:22:59 -070070
71RenderNode::~RenderNode() {
John Reck2de950d2017-01-25 10:58:30 -080072 ImmediateRemoved observer(nullptr);
73 deleteDisplayList(observer);
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 Reckbe671952021-01-13 22:39:32 -050077void RenderNode::setStagingDisplayList(DisplayList&& newData) {
78 mValid = newData.isValid();
Chris Craik003cc3d2015-10-16 10:24:55 -070079 mNeedsDisplayListSync = true;
John Reckbe671952021-01-13 22:39:32 -050080 mStagingDisplayList = std::move(newData);
John Reck113e0822014-03-18 09:22:59 -070081}
82
John Reckdc1a6962021-01-12 13:56:07 -050083void RenderNode::discardStagingDisplayList() {
John Reckbe671952021-01-13 22:39:32 -050084 setStagingDisplayList(DisplayList());
John Reckdc1a6962021-01-12 13:56:07 -050085}
86
John Reck113e0822014-03-18 09:22:59 -070087/**
88 * This function is a simplified version of replay(), where we simply retrieve and log the
89 * display list. This function should remain in sync with the replay() function.
90 */
sergeyvc3849aa2016-08-08 13:22:06 -070091void RenderNode::output() {
92 LogcatStream strout;
93 strout << "Root";
94 output(strout, 0);
95}
96
97void RenderNode::output(std::ostream& output, uint32_t level) {
98 output << " (" << getName() << " " << this
John Reck1bcacfd2017-11-03 10:12:19 -070099 << (MathUtils::isZero(properties().getAlpha()) ? ", zero alpha" : "")
100 << (properties().hasShadow() ? ", casting shadow" : "")
101 << (isRenderable() ? "" : ", empty")
102 << (properties().getProjectBackwards() ? ", projected" : "")
103 << (hasLayer() ? ", on HW Layer" : "") << ")" << std::endl;
sergeyvc3849aa2016-08-08 13:22:06 -0700104
105 properties().debugOutputProperties(output, level + 1);
Chris Craik91eff222016-02-22 13:39:33 -0800106
John Reckbe671952021-01-13 22:39:32 -0500107 mDisplayList.output(output, level);
sergeyvc3849aa2016-08-08 13:22:06 -0700108 output << std::string(level * 2, ' ') << "/RenderNode(" << getName() << " " << this << ")";
109 output << std::endl;
Chris Craik91eff222016-02-22 13:39:33 -0800110}
John Reck113e0822014-03-18 09:22:59 -0700111
John Reck183e1382019-10-09 13:41:18 -0700112int RenderNode::getUsageSize() {
John Reckfe5e7b72014-05-23 17:42:28 -0700113 int size = sizeof(RenderNode);
John Reckbe671952021-01-13 22:39:32 -0500114 size += mStagingDisplayList.getUsedSize();
115 size += mDisplayList.getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700116 return size;
117}
118
John Reck183e1382019-10-09 13:41:18 -0700119int RenderNode::getAllocatedSize() {
120 int size = sizeof(RenderNode);
John Reckbe671952021-01-13 22:39:32 -0500121 size += mStagingDisplayList.getAllocatedSize();
122 size += mDisplayList.getAllocatedSize();
John Reck183e1382019-10-09 13:41:18 -0700123 return size;
124}
125
126
John Reckf4198b72014-04-09 17:00:04 -0700127void RenderNode::prepareTree(TreeInfo& info) {
128 ATRACE_CALL();
Chris Craik69e5adf2014-08-14 13:34:01 -0700129 LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
John Reck2de950d2017-01-25 10:58:30 -0800130 MarkAndSweepRemoved observer(&info);
John Reckf4198b72014-04-09 17:00:04 -0700131
John Reck1423e132018-09-21 14:30:19 -0700132 const int before = info.disableForceDark;
John Reck1072fff2018-04-12 15:20:09 -0700133 prepareTreeImpl(observer, info, false);
John Reck1423e132018-09-21 14:30:19 -0700134 LOG_ALWAYS_FATAL_IF(before != info.disableForceDark, "Mis-matched force dark");
John Reckf4198b72014-04-09 17:00:04 -0700135}
136
John Reck68bfe0a2014-06-24 15:34:58 -0700137void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
138 mAnimatorManager.addAnimator(animator);
139}
140
Doris Liu8b083202016-02-19 21:46:06 +0000141void RenderNode::removeAnimator(const sp<BaseRenderNodeAnimator>& animator) {
142 mAnimatorManager.removeAnimator(animator);
143}
144
John Recke4267ea2014-06-03 15:53:15 -0700145void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700146 if (isRenderable()) {
John Reckf1aa7909e2019-03-07 17:01:08 -0800147 mDamageGenerationId = info.damageGenerationId;
John Reck293e8682014-06-17 10:34:02 -0700148 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700149 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
150 } else {
151 // Hope this is big enough?
152 // TODO: Get this from the display list ops or something
John Reckc1288232015-08-12 13:39:11 -0700153 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
John Recka447d292014-06-11 18:39:44 -0700154 }
Ady Abrahamcab4afe2023-05-09 11:25:22 -0700155 if (!mIsTextureView) {
156 info.out.solelyTextureViewUpdates = false;
157 }
John Recke4267ea2014-06-03 15:53:15 -0700158 }
159}
160
John Recka7c2ea22014-08-08 13:21:00 -0700161void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700162 LayerType layerType = properties().effectiveLayerType();
Chris Craik182952f2015-03-09 14:17:29 -0700163 if (CC_UNLIKELY(layerType == LayerType::RenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700164 // Damage applied so far needs to affect our parent, but does not require
165 // the layer to be updated. So we pop/push here to clear out the current
166 // damage and get a clean state for display list or children updates to
167 // affect, which will require the layer to be updated
168 info.damageAccumulator->popTransform();
169 info.damageAccumulator->pushTransform(this);
170 if (dirtyMask & DISPLAY_LIST) {
171 damageSelf(info);
172 }
John Reck25fbb3f2014-06-12 13:46:45 -0700173 }
174}
175
176void RenderNode::pushLayerUpdate(TreeInfo& info) {
Fedor Kudasov86bd2142019-06-18 15:51:57 +0100177#ifdef __ANDROID__ // Layoutlib does not support CanvasContext and Layers
Chris Craik856f0cc2015-04-21 15:13:29 -0700178 LayerType layerType = properties().effectiveLayerType();
John Reck25fbb3f2014-06-12 13:46:45 -0700179 // If we are not a layer OR we cannot be rendered (eg, view was detached)
180 // we need to destroy any Layers we may have had previously
John Reck1bcacfd2017-11-03 10:12:19 -0700181 if (CC_LIKELY(layerType != LayerType::RenderLayer) || CC_UNLIKELY(!isRenderable()) ||
182 CC_UNLIKELY(properties().getWidth() == 0) || CC_UNLIKELY(properties().getHeight() == 0) ||
183 CC_UNLIKELY(!properties().fitsOnLayer())) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400184 if (CC_UNLIKELY(hasLayer())) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400185 this->setLayerSurface(nullptr);
John Reck25fbb3f2014-06-12 13:46:45 -0700186 }
187 return;
188 }
189
Stan Iliev216b1572018-03-26 14:29:50 -0400190 if (info.canvasContext.createOrUpdateLayer(this, *info.damageAccumulator, info.errorHandler)) {
Chris Craik9fded232015-11-11 16:42:34 -0800191 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700192 }
193
Derek Sollenberger0df62092016-09-27 16:04:42 -0400194 if (!hasLayer()) {
John Reckc25e5062014-06-18 14:21:29 -0700195 return;
196 }
197
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400198 SkRect dirty;
199 info.damageAccumulator->peekAtDirty(&dirty);
Chris Craik0b7e8242015-10-28 16:50:44 -0700200 info.layerUpdateQueue->enqueueLayerWithDamage(this, dirty);
Nader Jawad197743f2021-04-19 19:45:13 -0700201 if (!dirty.isEmpty()) {
202 mStretchMask.markDirty();
203 }
John Reck998a6d82014-08-28 15:35:53 -0700204
Chris Craike2e53a72015-10-28 15:55:40 -0700205 // There might be prefetched layers that need to be accounted for.
206 // That might be us, so tell CanvasContext that this layer is in the
207 // tree and should not be destroyed.
208 info.canvasContext.markLayerInUse(this);
Fedor Kudasov86bd2142019-06-18 15:51:57 +0100209#endif
John Reck25fbb3f2014-06-12 13:46:45 -0700210}
211
Chris Craika766cb22015-06-08 16:49:43 -0700212/**
213 * Traverse down the the draw tree to prepare for a frame.
214 *
215 * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven
216 *
217 * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the
218 * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer.
219 */
John Reck2de950d2017-01-25 10:58:30 -0800220void RenderNode::prepareTreeImpl(TreeObserver& observer, TreeInfo& info, bool functorsNeedLayer) {
John Reckf1aa7909e2019-03-07 17:01:08 -0800221 if (mDamageGenerationId == info.damageGenerationId) {
222 // We hit the same node a second time in the same tree. We don't know the minimal
223 // damage rect anymore, so just push the biggest we can onto our parent's transform
224 // We push directly onto parent in case we are clipped to bounds but have moved position.
225 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
226 }
John Recka447d292014-06-11 18:39:44 -0700227 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700228
John Reckdcba6722014-07-08 13:59:49 -0700229 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700230 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700231 }
John Reck1423e132018-09-21 14:30:19 -0700232
233 if (!mProperties.getAllowForceDark()) {
234 info.disableForceDark++;
235 }
John Reck5d53a752021-02-08 19:22:59 -0500236 if (!mProperties.layerProperties().getStretchEffect().isEmpty()) {
237 info.stretchEffectCount++;
238 }
John Reck1423e132018-09-21 14:30:19 -0700239
John Reck9eb9f6f2014-08-21 11:23:05 -0700240 uint32_t animatorDirtyMask = 0;
241 if (CC_LIKELY(info.runAnimations)) {
242 animatorDirtyMask = mAnimatorManager.animate(info);
243 }
Chris Craika766cb22015-06-08 16:49:43 -0700244
John Reck3f725f02015-06-16 10:29:31 -0700245 bool willHaveFunctor = false;
Chris Craik003cc3d2015-10-16 10:24:55 -0700246 if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500247 willHaveFunctor = mStagingDisplayList.hasFunctor();
Chris Craik003cc3d2015-10-16 10:24:55 -0700248 } else if (mDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500249 willHaveFunctor = mDisplayList.hasFunctor();
John Reck3f725f02015-06-16 10:29:31 -0700250 }
John Reck1bcacfd2017-11-03 10:12:19 -0700251 bool childFunctorsNeedLayer =
252 mProperties.prepareForFunctorPresence(willHaveFunctor, functorsNeedLayer);
Chris Craika766cb22015-06-08 16:49:43 -0700253
John Reckf6481082016-02-02 15:18:23 -0800254 if (CC_UNLIKELY(mPositionListener.get())) {
255 mPositionListener->onPositionUpdated(*this, info);
256 }
257
John Recka7c2ea22014-08-08 13:21:00 -0700258 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700259 if (info.mode == TreeInfo::MODE_FULL) {
John Reck2de950d2017-01-25 10:58:30 -0800260 pushStagingDisplayListChanges(observer, info);
John Reck25fbb3f2014-06-12 13:46:45 -0700261 }
John Reck25fbb3f2014-06-12 13:46:45 -0700262
Doris Liu07c056d2016-06-13 12:52:44 -0700263 if (mDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500264 info.out.hasFunctors |= mDisplayList.hasFunctor();
Nader Jawad2dc632a2021-03-29 18:51:29 -0700265 mHasHolePunches = mDisplayList.hasHolePunches();
John Reckbe671952021-01-13 22:39:32 -0500266 bool isDirty = mDisplayList.prepareListAndChildren(
John Reck1bcacfd2017-11-03 10:12:19 -0700267 observer, info, childFunctorsNeedLayer,
Nader Jawad2dc632a2021-03-29 18:51:29 -0700268 [this](RenderNode* child, TreeObserver& observer, TreeInfo& info,
269 bool functorsNeedLayer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700270 child->prepareTreeImpl(observer, info, functorsNeedLayer);
Nader Jawad2dc632a2021-03-29 18:51:29 -0700271 mHasHolePunches |= child->hasHolePunches();
John Reck1bcacfd2017-11-03 10:12:19 -0700272 });
Derek Sollenberger0df62092016-09-27 16:04:42 -0400273 if (isDirty) {
274 damageSelf(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700275 }
Nader Jawad2dc632a2021-03-29 18:51:29 -0700276 } else {
277 mHasHolePunches = false;
Doris Liu718cd3e2016-05-17 16:50:31 -0700278 }
Doris Liub51b2862016-08-01 19:56:47 -0700279 pushLayerUpdate(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700280
John Reck1423e132018-09-21 14:30:19 -0700281 if (!mProperties.getAllowForceDark()) {
282 info.disableForceDark--;
283 }
John Reck5d53a752021-02-08 19:22:59 -0500284 if (!mProperties.layerProperties().getStretchEffect().isEmpty()) {
285 info.stretchEffectCount--;
286 }
John Recka447d292014-06-11 18:39:44 -0700287 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700288}
289
Chris Craikb565df12015-10-05 13:00:52 -0700290void RenderNode::syncProperties() {
291 mProperties = mStagingProperties;
292}
293
John Reck25fbb3f2014-06-12 13:46:45 -0700294void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reck097e1d32019-06-12 15:01:51 -0700295 if (mPositionListenerDirty) {
296 mPositionListener = std::move(mStagingPositionListener);
297 mStagingPositionListener = nullptr;
298 mPositionListenerDirty = false;
299 }
300
John Reckff941dc2014-05-14 16:34:14 -0700301 // Push the animators first so that setupStartValueIfNecessary() is called
302 // before properties() is trampled by stagingProperties(), as they are
303 // required by some animators.
John Reck9eb9f6f2014-08-21 11:23:05 -0700304 if (CC_LIKELY(info.runAnimations)) {
John Reck119907c2014-08-14 09:02:01 -0700305 mAnimatorManager.pushStaging();
John Reck9eb9f6f2014-08-21 11:23:05 -0700306 }
John Reckff941dc2014-05-14 16:34:14 -0700307 if (mDirtyPropertyFields) {
308 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700309 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700310 info.damageAccumulator->popTransform();
Chris Craikb565df12015-10-05 13:00:52 -0700311 syncProperties();
Nader Jawad197743f2021-04-19 19:45:13 -0700312
Nader Jawad6aff4812021-06-09 10:14:43 -0700313 auto& layerProperties = mProperties.layerProperties();
314 const StretchEffect& stagingStretch = layerProperties.getStretchEffect();
Nader Jawad197743f2021-04-19 19:45:13 -0700315 if (stagingStretch.isEmpty()) {
316 mStretchMask.clear();
317 }
Nader Jawad6aff4812021-06-09 10:14:43 -0700318
319 if (layerProperties.getImageFilter() == nullptr) {
320 mSnapshotResult.snapshot = nullptr;
321 mTargetImageFilter = nullptr;
322 }
323
John Recke4267ea2014-06-03 15:53:15 -0700324 // We could try to be clever and only re-damage if the matrix changed.
325 // However, we don't need to worry about that. The cost of over-damaging
326 // here is only going to be a single additional map rect of this node
327 // plus a rect join(). The parent's transform (and up) will only be
328 // performed once.
John Recka447d292014-06-11 18:39:44 -0700329 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700330 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700331 }
John Reck25fbb3f2014-06-12 13:46:45 -0700332}
333
Nader Jawad6aff4812021-06-09 10:14:43 -0700334std::optional<RenderNode::SnapshotResult> RenderNode::updateSnapshotIfRequired(
335 GrRecordingContext* context,
336 const SkImageFilter* imageFilter,
337 const SkIRect& clipBounds
338) {
339 auto* layerSurface = getLayerSurface();
340 if (layerSurface == nullptr) {
341 return std::nullopt;
342 }
343
344 sk_sp<SkImage> snapshot = layerSurface->makeImageSnapshot();
345 const auto subset = SkIRect::MakeWH(properties().getWidth(),
346 properties().getHeight());
Nader Jawadf49068f2021-09-08 22:17:15 -0700347 uint32_t layerSurfaceGenerationId = layerSurface->generationID();
Nader Jawad6aff4812021-06-09 10:14:43 -0700348 // If we don't have an ImageFilter just return the snapshot
349 if (imageFilter == nullptr) {
350 mSnapshotResult.snapshot = snapshot;
351 mSnapshotResult.outSubset = subset;
352 mSnapshotResult.outOffset = SkIPoint::Make(0.0f, 0.0f);
353 mImageFilterClipBounds = clipBounds;
354 mTargetImageFilter = nullptr;
Nader Jawadf49068f2021-09-08 22:17:15 -0700355 mTargetImageFilterLayerSurfaceGenerationId = 0;
356 } else if (mSnapshotResult.snapshot == nullptr || imageFilter != mTargetImageFilter.get() ||
357 mImageFilterClipBounds != clipBounds ||
358 mTargetImageFilterLayerSurfaceGenerationId != layerSurfaceGenerationId) {
Nader Jawad6aff4812021-06-09 10:14:43 -0700359 // Otherwise create a new snapshot with the given filter and snapshot
360 mSnapshotResult.snapshot =
361 snapshot->makeWithFilter(context,
362 imageFilter,
363 subset,
364 clipBounds,
365 &mSnapshotResult.outSubset,
366 &mSnapshotResult.outOffset);
367 mTargetImageFilter = sk_ref_sp(imageFilter);
368 mImageFilterClipBounds = clipBounds;
Nader Jawadf49068f2021-09-08 22:17:15 -0700369 mTargetImageFilterLayerSurfaceGenerationId = layerSurfaceGenerationId;
Nader Jawad6aff4812021-06-09 10:14:43 -0700370 }
371
372 return mSnapshotResult;
373}
374
John Reck2de950d2017-01-25 10:58:30 -0800375void RenderNode::syncDisplayList(TreeObserver& observer, TreeInfo* info) {
Chris Craikb565df12015-10-05 13:00:52 -0700376 // Make sure we inc first so that we don't fluctuate between 0 and 1,
377 // which would thrash the layer cache
Chris Craik003cc3d2015-10-16 10:24:55 -0700378 if (mStagingDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500379 mStagingDisplayList.updateChildren([](RenderNode* child) { child->incParentRefCount(); });
Chris Craikb565df12015-10-05 13:00:52 -0700380 }
John Reck2de950d2017-01-25 10:58:30 -0800381 deleteDisplayList(observer, info);
John Reckbe671952021-01-13 22:39:32 -0500382 mDisplayList = std::move(mStagingDisplayList);
Chris Craik003cc3d2015-10-16 10:24:55 -0700383 if (mDisplayList) {
John Reck283bb462018-12-13 16:40:14 -0800384 WebViewSyncData syncData {
385 .applyForceDark = info && !info->disableForceDark
386 };
John Reckbe671952021-01-13 22:39:32 -0500387 mDisplayList.syncContents(syncData);
John Reck3b4510cd2018-09-27 17:39:45 -0700388 handleForceDark(info);
389 }
390}
John Reckf3c724f2018-09-20 13:00:04 -0700391
John Reck3b4510cd2018-09-27 17:39:45 -0700392void RenderNode::handleForceDark(android::uirenderer::TreeInfo *info) {
393 if (CC_LIKELY(!info || info->disableForceDark)) {
394 return;
395 }
396 auto usage = usageHint();
John Reckbe671952021-01-13 22:39:32 -0500397 FatVector<RenderNode*, 6> children;
398 mDisplayList.updateChildren([&children](RenderNode* node) {
399 children.push_back(node);
400 });
401 if (mDisplayList.hasText()) {
John Reck3b4510cd2018-09-27 17:39:45 -0700402 usage = UsageHint::Foreground;
403 }
404 if (usage == UsageHint::Unknown) {
405 if (children.size() > 1) {
406 usage = UsageHint::Background;
407 } else if (children.size() == 1 &&
John Reckbe671952021-01-13 22:39:32 -0500408 children.front()->usageHint() !=
John Reck3b4510cd2018-09-27 17:39:45 -0700409 UsageHint::Background) {
410 usage = UsageHint::Background;
John Reck8f45d4a2018-08-15 10:17:12 -0700411 }
Chris Craikb565df12015-10-05 13:00:52 -0700412 }
John Reck3b4510cd2018-09-27 17:39:45 -0700413 if (children.size() > 1) {
414 // Crude overlap check
415 SkRect drawn = SkRect::MakeEmpty();
416 for (auto iter = children.rbegin(); iter != children.rend(); ++iter) {
John Reckbe671952021-01-13 22:39:32 -0500417 const auto& child = *iter;
John Reck3b4510cd2018-09-27 17:39:45 -0700418 // We use stagingProperties here because we haven't yet sync'd the children
419 SkRect bounds = SkRect::MakeXYWH(child->stagingProperties().getX(), child->stagingProperties().getY(),
420 child->stagingProperties().getWidth(), child->stagingProperties().getHeight());
421 if (bounds.contains(drawn)) {
422 // This contains everything drawn after it, so make it a background
423 child->setUsageHint(UsageHint::Background);
424 }
425 drawn.join(bounds);
426 }
427 }
John Reckbe671952021-01-13 22:39:32 -0500428 mDisplayList.applyColorTransform(
John Reck3b4510cd2018-09-27 17:39:45 -0700429 usage == UsageHint::Background ? ColorTransform::Dark : ColorTransform::Light);
Chris Craikb565df12015-10-05 13:00:52 -0700430}
431
John Reck2de950d2017-01-25 10:58:30 -0800432void RenderNode::pushStagingDisplayListChanges(TreeObserver& observer, TreeInfo& info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700433 if (mNeedsDisplayListSync) {
434 mNeedsDisplayListSync = false;
John Reck5c9d7172014-10-22 11:32:27 -0700435 // Damage with the old display list first then the new one to catch any
436 // changes in isRenderable or, in the future, bounds
437 damageSelf(info);
John Reck2de950d2017-01-25 10:58:30 -0800438 syncDisplayList(observer, &info);
John Recke4267ea2014-06-03 15:53:15 -0700439 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700440 }
John Reck8de65a82014-04-09 15:23:38 -0700441}
442
John Reck2de950d2017-01-25 10:58:30 -0800443void RenderNode::deleteDisplayList(TreeObserver& observer, TreeInfo* info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700444 if (mDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500445 mDisplayList.updateChildren(
John Reck1bcacfd2017-11-03 10:12:19 -0700446 [&observer, info](RenderNode* child) { child->decParentRefCount(observer, info); });
John Reckbe671952021-01-13 22:39:32 -0500447 mDisplayList.clear(this);
John Reckdcba6722014-07-08 13:59:49 -0700448 }
John Reckdcba6722014-07-08 13:59:49 -0700449}
450
John Reck2de950d2017-01-25 10:58:30 -0800451void RenderNode::destroyHardwareResources(TreeInfo* info) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400452 if (hasLayer()) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400453 this->setLayerSurface(nullptr);
John Reckdcba6722014-07-08 13:59:49 -0700454 }
John Reckbe671952021-01-13 22:39:32 -0500455 discardStagingDisplayList();
John Reck3afd6372017-01-30 10:15:48 -0800456
457 ImmediateRemoved observer(info);
458 deleteDisplayList(observer, info);
John Reck2de950d2017-01-25 10:58:30 -0800459}
460
461void RenderNode::destroyLayers() {
462 if (hasLayer()) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400463 this->setLayerSurface(nullptr);
John Reck2de950d2017-01-25 10:58:30 -0800464 }
Nader Jawadb502ad72021-06-17 14:10:04 -0700465
John Reck2de950d2017-01-25 10:58:30 -0800466 if (mDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500467 mDisplayList.updateChildren([](RenderNode* child) { child->destroyLayers(); });
John Reck2de950d2017-01-25 10:58:30 -0800468 }
469}
470
471void RenderNode::decParentRefCount(TreeObserver& observer, TreeInfo* info) {
472 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
473 mParentCount--;
474 if (!mParentCount) {
475 observer.onMaybeRemovedFromTree(this);
476 if (CC_UNLIKELY(mPositionListener.get())) {
477 mPositionListener->onPositionLost(*this, info);
John Reckdcba6722014-07-08 13:59:49 -0700478 }
479 }
480}
481
John Reck2de950d2017-01-25 10:58:30 -0800482void RenderNode::onRemovedFromTree(TreeInfo* info) {
Huihong Luoec68b7c2021-06-25 21:54:16 -0700483 if (Properties::enableWebViewOverlays && mDisplayList) {
484 mDisplayList.onRemovedFromTree();
485 }
John Reck2de950d2017-01-25 10:58:30 -0800486 destroyHardwareResources(info);
487}
488
489void RenderNode::clearRoot() {
490 ImmediateRemoved observer(nullptr);
491 decParentRefCount(observer);
John Reckdcba6722014-07-08 13:59:49 -0700492}
493
John Reck113e0822014-03-18 09:22:59 -0700494/**
495 * Apply property-based transformations to input matrix
496 *
497 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
498 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
499 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700500void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700501 if (properties().getLeft() != 0 || properties().getTop() != 0) {
502 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700503 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700504 if (properties().getStaticMatrix()) {
505 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700506 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700507 } else if (properties().getAnimationMatrix()) {
508 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700509 matrix.multiply(anim);
510 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700511
Chris Craikcc39e162014-04-25 18:34:11 -0700512 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700513 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700514 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700515 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
John Reck1bcacfd2017-11-03 10:12:19 -0700516 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700517 } else {
518 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700519 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700520 } else {
521 mat4 true3dMat;
John Reck1bcacfd2017-11-03 10:12:19 -0700522 true3dMat.loadTranslate(properties().getPivotX() + properties().getTranslationX(),
523 properties().getPivotY() + properties().getTranslationY(),
524 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700525 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
526 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
527 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
528 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
529 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700530
531 matrix.multiply(true3dMat);
532 }
533 }
534 }
John Reck8ed00dc2021-05-10 13:09:27 -0400535
Nader Jawad93d6e242021-05-17 18:12:29 -0700536 if (Properties::getStretchEffectBehavior() == StretchEffectBehavior::UniformScale) {
John Reck8ed00dc2021-05-10 13:09:27 -0400537 const StretchEffect& stretch = properties().layerProperties().getStretchEffect();
538 if (!stretch.isEmpty()) {
539 matrix.multiply(
540 stretch.makeLinearStretch(properties().getWidth(), properties().getHeight()));
541 }
542 }
John Reck113e0822014-03-18 09:22:59 -0700543}
544
Derek Sollenberger579317d42017-08-29 16:33:49 -0400545const SkPath* RenderNode::getClippedOutline(const SkRect& clipRect) const {
546 const SkPath* outlinePath = properties().getOutline().getPath();
547 const uint32_t outlineID = outlinePath->getGenerationID();
548
549 if (outlineID != mClippedOutlineCache.outlineID || clipRect != mClippedOutlineCache.clipRect) {
550 // update the cache keys
551 mClippedOutlineCache.outlineID = outlineID;
552 mClippedOutlineCache.clipRect = clipRect;
553
554 // update the cache value by recomputing a new path
555 SkPath clipPath;
556 clipPath.addRect(clipRect);
557 Op(*outlinePath, clipPath, kIntersect_SkPathOp, &mClippedOutlineCache.clippedOutline);
Derek Sollenberger579317d42017-08-29 16:33:49 -0400558 }
559 return &mClippedOutlineCache.clippedOutline;
560}
561
John Reckf96b2842018-11-29 09:44:10 -0800562using StringBuffer = FatVector<char, 128>;
563
564template <typename... T>
John Reck7af5b2c2018-12-05 13:36:20 -0800565// TODO:__printflike(2, 3)
566// Doesn't work because the warning doesn't understand string_view and doesn't like that
567// it's not a C-style variadic function.
John Reckf96b2842018-11-29 09:44:10 -0800568static void format(StringBuffer& buffer, const std::string_view& format, T... args) {
569 buffer.resize(buffer.capacity());
570 while (1) {
571 int needed = snprintf(buffer.data(), buffer.size(),
572 format.data(), std::forward<T>(args)...);
573 if (needed < 0) {
574 buffer[0] = '\0';
575 buffer.resize(1);
576 return;
577 }
578 if (needed < buffer.size()) {
579 buffer.resize(needed + 1);
580 return;
581 }
John Reck7af5b2c2018-12-05 13:36:20 -0800582 // If we're doing a heap alloc anyway might as well give it some slop
583 buffer.resize(needed + 100);
John Reckf96b2842018-11-29 09:44:10 -0800584 }
585}
586
587void RenderNode::markDrawStart(SkCanvas& canvas) {
588 StringBuffer buffer;
John Reck7af5b2c2018-12-05 13:36:20 -0800589 format(buffer, "RenderNode(id=%" PRId64 ", name='%s')", uniqueId(), getName());
John Reckf96b2842018-11-29 09:44:10 -0800590 canvas.drawAnnotation(SkRect::MakeWH(getWidth(), getHeight()), buffer.data(), nullptr);
591}
592
593void RenderNode::markDrawEnd(SkCanvas& canvas) {
594 StringBuffer buffer;
John Reck7af5b2c2018-12-05 13:36:20 -0800595 format(buffer, "/RenderNode(id=%" PRId64 ", name='%s')", uniqueId(), getName());
John Reckf96b2842018-11-29 09:44:10 -0800596 canvas.drawAnnotation(SkRect::MakeWH(getWidth(), getHeight()), buffer.data(), nullptr);
597}
598
John Reck113e0822014-03-18 09:22:59 -0700599} /* namespace uirenderer */
600} /* namespace android */