blob: 3e131bc44d393c4510082f82c3f8eea518209c43 [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
Derek Sollenberger579317d42017-08-29 16:33:49 -040031#include <SkPathOps.h>
Robert Phillips74fe4712023-09-06 15:32:34 -040032#include <gui/TraceUtils.h>
33#include <ui/FatVector.h>
34
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>
Robert Phillips74fe4712023-09-06 15:32:34 -040039
40#ifdef __ANDROID__
41#include "include/gpu/ganesh/SkImageGanesh.h"
42#endif
Tyler Freeman33567742023-10-31 01:55:51 +000043#include "utils/ForceDark.h"
Robert Phillips74fe4712023-09-06 15:32:34 -040044#include "utils/MathUtils.h"
45#include "utils/StringUtils.h"
John Reck77c40102015-10-26 15:49:47 -070046
John Reck113e0822014-03-18 09:22:59 -070047namespace android {
48namespace uirenderer {
49
John Reck2de950d2017-01-25 10:58:30 -080050// Used for tree mutations that are purely destructive.
51// Generic tree mutations should use MarkAndSweepObserver instead
52class ImmediateRemoved : public TreeObserver {
53public:
54 explicit ImmediateRemoved(TreeInfo* info) : mTreeInfo(info) {}
55
John Reck1bcacfd2017-11-03 10:12:19 -070056 void onMaybeRemovedFromTree(RenderNode* node) override { node->onRemovedFromTree(mTreeInfo); }
John Reck2de950d2017-01-25 10:58:30 -080057
58private:
59 TreeInfo* mTreeInfo;
60};
61
John Reckf96b2842018-11-29 09:44:10 -080062static int64_t generateId() {
63 static std::atomic<int64_t> sNextId{1};
64 return sNextId++;
65}
66
John Reck8de65a82014-04-09 15:23:38 -070067RenderNode::RenderNode()
John Reckf96b2842018-11-29 09:44:10 -080068 : mUniqueId(generateId())
69 , mDirtyPropertyFields(0)
Chris Craik003cc3d2015-10-16 10:24:55 -070070 , mNeedsDisplayListSync(false)
71 , mDisplayList(nullptr)
72 , mStagingDisplayList(nullptr)
John Reck68bfe0a2014-06-24 15:34:58 -070073 , mAnimatorManager(*this)
John Reck1bcacfd2017-11-03 10:12:19 -070074 , mParentCount(0) {}
John Reck113e0822014-03-18 09:22:59 -070075
76RenderNode::~RenderNode() {
John Reck2de950d2017-01-25 10:58:30 -080077 ImmediateRemoved observer(nullptr);
78 deleteDisplayList(observer);
Derek Sollenberger0df62092016-09-27 16:04:42 -040079 LOG_ALWAYS_FATAL_IF(hasLayer(), "layer missed detachment!");
John Reck113e0822014-03-18 09:22:59 -070080}
81
John Reckbe671952021-01-13 22:39:32 -050082void RenderNode::setStagingDisplayList(DisplayList&& newData) {
83 mValid = newData.isValid();
Chris Craik003cc3d2015-10-16 10:24:55 -070084 mNeedsDisplayListSync = true;
John Reckbe671952021-01-13 22:39:32 -050085 mStagingDisplayList = std::move(newData);
John Reck113e0822014-03-18 09:22:59 -070086}
87
John Reckdc1a6962021-01-12 13:56:07 -050088void RenderNode::discardStagingDisplayList() {
John Reckbe671952021-01-13 22:39:32 -050089 setStagingDisplayList(DisplayList());
John Reckdc1a6962021-01-12 13:56:07 -050090}
91
John Reck113e0822014-03-18 09:22:59 -070092/**
93 * This function is a simplified version of replay(), where we simply retrieve and log the
94 * display list. This function should remain in sync with the replay() function.
95 */
sergeyvc3849aa2016-08-08 13:22:06 -070096void RenderNode::output() {
97 LogcatStream strout;
98 strout << "Root";
99 output(strout, 0);
100}
101
102void RenderNode::output(std::ostream& output, uint32_t level) {
103 output << " (" << getName() << " " << this
John Reck1bcacfd2017-11-03 10:12:19 -0700104 << (MathUtils::isZero(properties().getAlpha()) ? ", zero alpha" : "")
105 << (properties().hasShadow() ? ", casting shadow" : "")
106 << (isRenderable() ? "" : ", empty")
107 << (properties().getProjectBackwards() ? ", projected" : "")
108 << (hasLayer() ? ", on HW Layer" : "") << ")" << std::endl;
sergeyvc3849aa2016-08-08 13:22:06 -0700109
110 properties().debugOutputProperties(output, level + 1);
Chris Craik91eff222016-02-22 13:39:33 -0800111
John Reckbe671952021-01-13 22:39:32 -0500112 mDisplayList.output(output, level);
sergeyvc3849aa2016-08-08 13:22:06 -0700113 output << std::string(level * 2, ' ') << "/RenderNode(" << getName() << " " << this << ")";
114 output << std::endl;
Chris Craik91eff222016-02-22 13:39:33 -0800115}
John Reck113e0822014-03-18 09:22:59 -0700116
John Reck07f3d912023-08-17 12:46:44 -0400117void RenderNode::visit(std::function<void(const RenderNode&)> func) const {
118 func(*this);
119 if (mDisplayList) {
120 mDisplayList.visit(func);
121 }
122}
123
John Reck183e1382019-10-09 13:41:18 -0700124int RenderNode::getUsageSize() {
John Reckfe5e7b72014-05-23 17:42:28 -0700125 int size = sizeof(RenderNode);
John Reckbe671952021-01-13 22:39:32 -0500126 size += mStagingDisplayList.getUsedSize();
127 size += mDisplayList.getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700128 return size;
129}
130
John Reck183e1382019-10-09 13:41:18 -0700131int RenderNode::getAllocatedSize() {
132 int size = sizeof(RenderNode);
John Reckbe671952021-01-13 22:39:32 -0500133 size += mStagingDisplayList.getAllocatedSize();
134 size += mDisplayList.getAllocatedSize();
John Reck183e1382019-10-09 13:41:18 -0700135 return size;
136}
137
138
John Reckf4198b72014-04-09 17:00:04 -0700139void RenderNode::prepareTree(TreeInfo& info) {
140 ATRACE_CALL();
Chris Craik69e5adf2014-08-14 13:34:01 -0700141 LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
John Reck2de950d2017-01-25 10:58:30 -0800142 MarkAndSweepRemoved observer(&info);
John Reckf4198b72014-04-09 17:00:04 -0700143
John Reck1423e132018-09-21 14:30:19 -0700144 const int before = info.disableForceDark;
John Reck1072fff2018-04-12 15:20:09 -0700145 prepareTreeImpl(observer, info, false);
John Reck1423e132018-09-21 14:30:19 -0700146 LOG_ALWAYS_FATAL_IF(before != info.disableForceDark, "Mis-matched force dark");
John Reckf4198b72014-04-09 17:00:04 -0700147}
148
John Reck68bfe0a2014-06-24 15:34:58 -0700149void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
150 mAnimatorManager.addAnimator(animator);
151}
152
Doris Liu8b083202016-02-19 21:46:06 +0000153void RenderNode::removeAnimator(const sp<BaseRenderNodeAnimator>& animator) {
154 mAnimatorManager.removeAnimator(animator);
155}
156
John Recke4267ea2014-06-03 15:53:15 -0700157void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700158 if (isRenderable()) {
John Reckf1aa7909e2019-03-07 17:01:08 -0800159 mDamageGenerationId = info.damageGenerationId;
John Reck293e8682014-06-17 10:34:02 -0700160 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700161 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
162 } else {
163 // Hope this is big enough?
164 // TODO: Get this from the display list ops or something
John Reckc1288232015-08-12 13:39:11 -0700165 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
John Recka447d292014-06-11 18:39:44 -0700166 }
Ady Abrahamcab4afe2023-05-09 11:25:22 -0700167 if (!mIsTextureView) {
168 info.out.solelyTextureViewUpdates = false;
169 }
John Recke4267ea2014-06-03 15:53:15 -0700170 }
171}
172
John Recka7c2ea22014-08-08 13:21:00 -0700173void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700174 LayerType layerType = properties().effectiveLayerType();
Chris Craik182952f2015-03-09 14:17:29 -0700175 if (CC_UNLIKELY(layerType == LayerType::RenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700176 // Damage applied so far needs to affect our parent, but does not require
177 // the layer to be updated. So we pop/push here to clear out the current
178 // damage and get a clean state for display list or children updates to
179 // affect, which will require the layer to be updated
180 info.damageAccumulator->popTransform();
181 info.damageAccumulator->pushTransform(this);
182 if (dirtyMask & DISPLAY_LIST) {
183 damageSelf(info);
184 }
John Reck25fbb3f2014-06-12 13:46:45 -0700185 }
186}
187
188void RenderNode::pushLayerUpdate(TreeInfo& info) {
Fedor Kudasov86bd2142019-06-18 15:51:57 +0100189#ifdef __ANDROID__ // Layoutlib does not support CanvasContext and Layers
Chris Craik856f0cc2015-04-21 15:13:29 -0700190 LayerType layerType = properties().effectiveLayerType();
John Reck25fbb3f2014-06-12 13:46:45 -0700191 // If we are not a layer OR we cannot be rendered (eg, view was detached)
192 // we need to destroy any Layers we may have had previously
John Reck1bcacfd2017-11-03 10:12:19 -0700193 if (CC_LIKELY(layerType != LayerType::RenderLayer) || CC_UNLIKELY(!isRenderable()) ||
194 CC_UNLIKELY(properties().getWidth() == 0) || CC_UNLIKELY(properties().getHeight() == 0) ||
195 CC_UNLIKELY(!properties().fitsOnLayer())) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400196 if (CC_UNLIKELY(hasLayer())) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400197 this->setLayerSurface(nullptr);
John Reck25fbb3f2014-06-12 13:46:45 -0700198 }
199 return;
200 }
201
Stan Iliev216b1572018-03-26 14:29:50 -0400202 if (info.canvasContext.createOrUpdateLayer(this, *info.damageAccumulator, info.errorHandler)) {
Chris Craik9fded232015-11-11 16:42:34 -0800203 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700204 }
205
Derek Sollenberger0df62092016-09-27 16:04:42 -0400206 if (!hasLayer()) {
John Reckc25e5062014-06-18 14:21:29 -0700207 return;
208 }
209
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400210 SkRect dirty;
211 info.damageAccumulator->peekAtDirty(&dirty);
Chris Craik0b7e8242015-10-28 16:50:44 -0700212 info.layerUpdateQueue->enqueueLayerWithDamage(this, dirty);
Nader Jawad197743f2021-04-19 19:45:13 -0700213 if (!dirty.isEmpty()) {
214 mStretchMask.markDirty();
215 }
John Reck998a6d82014-08-28 15:35:53 -0700216
Chris Craike2e53a72015-10-28 15:55:40 -0700217 // There might be prefetched layers that need to be accounted for.
218 // That might be us, so tell CanvasContext that this layer is in the
219 // tree and should not be destroyed.
220 info.canvasContext.markLayerInUse(this);
Fedor Kudasov86bd2142019-06-18 15:51:57 +0100221#endif
John Reck25fbb3f2014-06-12 13:46:45 -0700222}
223
Chris Craika766cb22015-06-08 16:49:43 -0700224/**
225 * Traverse down the the draw tree to prepare for a frame.
226 *
227 * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven
228 *
229 * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the
230 * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer.
231 */
John Reck2de950d2017-01-25 10:58:30 -0800232void RenderNode::prepareTreeImpl(TreeObserver& observer, TreeInfo& info, bool functorsNeedLayer) {
John Reckf1aa7909e2019-03-07 17:01:08 -0800233 if (mDamageGenerationId == info.damageGenerationId) {
234 // We hit the same node a second time in the same tree. We don't know the minimal
235 // damage rect anymore, so just push the biggest we can onto our parent's transform
236 // We push directly onto parent in case we are clipped to bounds but have moved position.
237 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
238 }
John Recka447d292014-06-11 18:39:44 -0700239 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700240
John Reckdcba6722014-07-08 13:59:49 -0700241 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700242 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700243 }
John Reck1423e132018-09-21 14:30:19 -0700244
245 if (!mProperties.getAllowForceDark()) {
246 info.disableForceDark++;
247 }
John Reck5d53a752021-02-08 19:22:59 -0500248 if (!mProperties.layerProperties().getStretchEffect().isEmpty()) {
249 info.stretchEffectCount++;
250 }
John Reck1423e132018-09-21 14:30:19 -0700251
John Reck9eb9f6f2014-08-21 11:23:05 -0700252 uint32_t animatorDirtyMask = 0;
253 if (CC_LIKELY(info.runAnimations)) {
254 animatorDirtyMask = mAnimatorManager.animate(info);
255 }
Chris Craika766cb22015-06-08 16:49:43 -0700256
John Reck3f725f02015-06-16 10:29:31 -0700257 bool willHaveFunctor = false;
Chris Craik003cc3d2015-10-16 10:24:55 -0700258 if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500259 willHaveFunctor = mStagingDisplayList.hasFunctor();
Chris Craik003cc3d2015-10-16 10:24:55 -0700260 } else if (mDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500261 willHaveFunctor = mDisplayList.hasFunctor();
John Reck3f725f02015-06-16 10:29:31 -0700262 }
John Reck1bcacfd2017-11-03 10:12:19 -0700263 bool childFunctorsNeedLayer =
264 mProperties.prepareForFunctorPresence(willHaveFunctor, functorsNeedLayer);
Chris Craika766cb22015-06-08 16:49:43 -0700265
John Reckf6481082016-02-02 15:18:23 -0800266 if (CC_UNLIKELY(mPositionListener.get())) {
267 mPositionListener->onPositionUpdated(*this, info);
268 }
269
John Recka7c2ea22014-08-08 13:21:00 -0700270 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700271 if (info.mode == TreeInfo::MODE_FULL) {
John Reck2de950d2017-01-25 10:58:30 -0800272 pushStagingDisplayListChanges(observer, info);
John Reck25fbb3f2014-06-12 13:46:45 -0700273 }
John Reck25fbb3f2014-06-12 13:46:45 -0700274
Dongya Jiange2b99382022-02-28 21:35:57 +0800275 // always damageSelf when filtering backdrop content, or else the BackdropFilterDrawable will
276 // get a wrong snapshot of previous content.
277 if (mProperties.layerProperties().getBackdropImageFilter()) {
278 damageSelf(info);
279 }
280
Doris Liu07c056d2016-06-13 12:52:44 -0700281 if (mDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500282 info.out.hasFunctors |= mDisplayList.hasFunctor();
Nader Jawad2dc632a2021-03-29 18:51:29 -0700283 mHasHolePunches = mDisplayList.hasHolePunches();
John Reckbe671952021-01-13 22:39:32 -0500284 bool isDirty = mDisplayList.prepareListAndChildren(
John Reck1bcacfd2017-11-03 10:12:19 -0700285 observer, info, childFunctorsNeedLayer,
Nader Jawad2dc632a2021-03-29 18:51:29 -0700286 [this](RenderNode* child, TreeObserver& observer, TreeInfo& info,
287 bool functorsNeedLayer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700288 child->prepareTreeImpl(observer, info, functorsNeedLayer);
Nader Jawad2dc632a2021-03-29 18:51:29 -0700289 mHasHolePunches |= child->hasHolePunches();
John Reck1bcacfd2017-11-03 10:12:19 -0700290 });
Derek Sollenberger0df62092016-09-27 16:04:42 -0400291 if (isDirty) {
292 damageSelf(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700293 }
Nader Jawad2dc632a2021-03-29 18:51:29 -0700294 } else {
295 mHasHolePunches = false;
Doris Liu718cd3e2016-05-17 16:50:31 -0700296 }
Doris Liub51b2862016-08-01 19:56:47 -0700297 pushLayerUpdate(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700298
John Reck1423e132018-09-21 14:30:19 -0700299 if (!mProperties.getAllowForceDark()) {
300 info.disableForceDark--;
301 }
John Reck5d53a752021-02-08 19:22:59 -0500302 if (!mProperties.layerProperties().getStretchEffect().isEmpty()) {
303 info.stretchEffectCount--;
304 }
John Recka447d292014-06-11 18:39:44 -0700305 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700306}
307
Chris Craikb565df12015-10-05 13:00:52 -0700308void RenderNode::syncProperties() {
309 mProperties = mStagingProperties;
310}
311
John Reck25fbb3f2014-06-12 13:46:45 -0700312void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reck097e1d32019-06-12 15:01:51 -0700313 if (mPositionListenerDirty) {
314 mPositionListener = std::move(mStagingPositionListener);
315 mStagingPositionListener = nullptr;
316 mPositionListenerDirty = false;
317 }
318
John Reckff941dc2014-05-14 16:34:14 -0700319 // Push the animators first so that setupStartValueIfNecessary() is called
320 // before properties() is trampled by stagingProperties(), as they are
321 // required by some animators.
John Reck9eb9f6f2014-08-21 11:23:05 -0700322 if (CC_LIKELY(info.runAnimations)) {
John Reck119907c2014-08-14 09:02:01 -0700323 mAnimatorManager.pushStaging();
John Reck9eb9f6f2014-08-21 11:23:05 -0700324 }
John Reckff941dc2014-05-14 16:34:14 -0700325 if (mDirtyPropertyFields) {
326 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700327 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700328 info.damageAccumulator->popTransform();
Chris Craikb565df12015-10-05 13:00:52 -0700329 syncProperties();
Nader Jawad197743f2021-04-19 19:45:13 -0700330
Nader Jawad6aff4812021-06-09 10:14:43 -0700331 auto& layerProperties = mProperties.layerProperties();
332 const StretchEffect& stagingStretch = layerProperties.getStretchEffect();
Nader Jawad197743f2021-04-19 19:45:13 -0700333 if (stagingStretch.isEmpty()) {
334 mStretchMask.clear();
335 }
Nader Jawad6aff4812021-06-09 10:14:43 -0700336
337 if (layerProperties.getImageFilter() == nullptr) {
338 mSnapshotResult.snapshot = nullptr;
339 mTargetImageFilter = nullptr;
340 }
341
John Recke4267ea2014-06-03 15:53:15 -0700342 // We could try to be clever and only re-damage if the matrix changed.
343 // However, we don't need to worry about that. The cost of over-damaging
344 // here is only going to be a single additional map rect of this node
345 // plus a rect join(). The parent's transform (and up) will only be
346 // performed once.
John Recka447d292014-06-11 18:39:44 -0700347 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700348 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700349 }
John Reck25fbb3f2014-06-12 13:46:45 -0700350}
351
Nader Jawad6aff4812021-06-09 10:14:43 -0700352std::optional<RenderNode::SnapshotResult> RenderNode::updateSnapshotIfRequired(
353 GrRecordingContext* context,
354 const SkImageFilter* imageFilter,
355 const SkIRect& clipBounds
356) {
357 auto* layerSurface = getLayerSurface();
358 if (layerSurface == nullptr) {
359 return std::nullopt;
360 }
361
362 sk_sp<SkImage> snapshot = layerSurface->makeImageSnapshot();
363 const auto subset = SkIRect::MakeWH(properties().getWidth(),
364 properties().getHeight());
Nader Jawadf49068f2021-09-08 22:17:15 -0700365 uint32_t layerSurfaceGenerationId = layerSurface->generationID();
Nader Jawad6aff4812021-06-09 10:14:43 -0700366 // If we don't have an ImageFilter just return the snapshot
367 if (imageFilter == nullptr) {
368 mSnapshotResult.snapshot = snapshot;
369 mSnapshotResult.outSubset = subset;
370 mSnapshotResult.outOffset = SkIPoint::Make(0.0f, 0.0f);
371 mImageFilterClipBounds = clipBounds;
372 mTargetImageFilter = nullptr;
Nader Jawadf49068f2021-09-08 22:17:15 -0700373 mTargetImageFilterLayerSurfaceGenerationId = 0;
374 } else if (mSnapshotResult.snapshot == nullptr || imageFilter != mTargetImageFilter.get() ||
375 mImageFilterClipBounds != clipBounds ||
376 mTargetImageFilterLayerSurfaceGenerationId != layerSurfaceGenerationId) {
Nader Jawad6aff4812021-06-09 10:14:43 -0700377 // Otherwise create a new snapshot with the given filter and snapshot
Robert Phillips74fe4712023-09-06 15:32:34 -0400378#ifdef __ANDROID__
379 if (context) {
380 mSnapshotResult.snapshot = SkImages::MakeWithFilter(
381 context, snapshot, imageFilter, subset, clipBounds, &mSnapshotResult.outSubset,
382 &mSnapshotResult.outOffset);
383 } else
384#endif
385 {
386 mSnapshotResult.snapshot = SkImages::MakeWithFilter(
387 snapshot, imageFilter, subset, clipBounds, &mSnapshotResult.outSubset,
388 &mSnapshotResult.outOffset);
389 }
Nader Jawad6aff4812021-06-09 10:14:43 -0700390 mTargetImageFilter = sk_ref_sp(imageFilter);
391 mImageFilterClipBounds = clipBounds;
Nader Jawadf49068f2021-09-08 22:17:15 -0700392 mTargetImageFilterLayerSurfaceGenerationId = layerSurfaceGenerationId;
Nader Jawad6aff4812021-06-09 10:14:43 -0700393 }
394
395 return mSnapshotResult;
396}
397
John Reck2de950d2017-01-25 10:58:30 -0800398void RenderNode::syncDisplayList(TreeObserver& observer, TreeInfo* info) {
Chris Craikb565df12015-10-05 13:00:52 -0700399 // Make sure we inc first so that we don't fluctuate between 0 and 1,
400 // which would thrash the layer cache
Chris Craik003cc3d2015-10-16 10:24:55 -0700401 if (mStagingDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500402 mStagingDisplayList.updateChildren([](RenderNode* child) { child->incParentRefCount(); });
Chris Craikb565df12015-10-05 13:00:52 -0700403 }
John Reck2de950d2017-01-25 10:58:30 -0800404 deleteDisplayList(observer, info);
John Reckbe671952021-01-13 22:39:32 -0500405 mDisplayList = std::move(mStagingDisplayList);
Chris Craik003cc3d2015-10-16 10:24:55 -0700406 if (mDisplayList) {
Tyler Freeman33567742023-10-31 01:55:51 +0000407 WebViewSyncData syncData{.applyForceDark = shouldEnableForceDark(info)};
John Reckbe671952021-01-13 22:39:32 -0500408 mDisplayList.syncContents(syncData);
John Reck3b4510cd2018-09-27 17:39:45 -0700409 handleForceDark(info);
410 }
411}
John Reckf3c724f2018-09-20 13:00:04 -0700412
Tyler Freeman33567742023-10-31 01:55:51 +0000413inline bool RenderNode::shouldEnableForceDark(TreeInfo* info) {
414 return CC_UNLIKELY(
415 info &&
416 (!info->disableForceDark ||
417 info->forceDarkType == android::uirenderer::ForceDarkType::FORCE_INVERT_COLOR_DARK));
418}
419
John Reck3b4510cd2018-09-27 17:39:45 -0700420void RenderNode::handleForceDark(android::uirenderer::TreeInfo *info) {
Tyler Freeman33567742023-10-31 01:55:51 +0000421 if (!shouldEnableForceDark(info)) {
John Reck3b4510cd2018-09-27 17:39:45 -0700422 return;
423 }
424 auto usage = usageHint();
John Reckbe671952021-01-13 22:39:32 -0500425 FatVector<RenderNode*, 6> children;
426 mDisplayList.updateChildren([&children](RenderNode* node) {
427 children.push_back(node);
428 });
429 if (mDisplayList.hasText()) {
John Reck3b4510cd2018-09-27 17:39:45 -0700430 usage = UsageHint::Foreground;
431 }
432 if (usage == UsageHint::Unknown) {
433 if (children.size() > 1) {
434 usage = UsageHint::Background;
435 } else if (children.size() == 1 &&
John Reckbe671952021-01-13 22:39:32 -0500436 children.front()->usageHint() !=
John Reck3b4510cd2018-09-27 17:39:45 -0700437 UsageHint::Background) {
438 usage = UsageHint::Background;
John Reck8f45d4a2018-08-15 10:17:12 -0700439 }
Chris Craikb565df12015-10-05 13:00:52 -0700440 }
John Reck3b4510cd2018-09-27 17:39:45 -0700441 if (children.size() > 1) {
442 // Crude overlap check
443 SkRect drawn = SkRect::MakeEmpty();
444 for (auto iter = children.rbegin(); iter != children.rend(); ++iter) {
John Reckbe671952021-01-13 22:39:32 -0500445 const auto& child = *iter;
John Reck3b4510cd2018-09-27 17:39:45 -0700446 // We use stagingProperties here because we haven't yet sync'd the children
447 SkRect bounds = SkRect::MakeXYWH(child->stagingProperties().getX(), child->stagingProperties().getY(),
448 child->stagingProperties().getWidth(), child->stagingProperties().getHeight());
449 if (bounds.contains(drawn)) {
450 // This contains everything drawn after it, so make it a background
451 child->setUsageHint(UsageHint::Background);
452 }
453 drawn.join(bounds);
454 }
455 }
John Reckbe671952021-01-13 22:39:32 -0500456 mDisplayList.applyColorTransform(
John Reck3b4510cd2018-09-27 17:39:45 -0700457 usage == UsageHint::Background ? ColorTransform::Dark : ColorTransform::Light);
Chris Craikb565df12015-10-05 13:00:52 -0700458}
459
John Reck2de950d2017-01-25 10:58:30 -0800460void RenderNode::pushStagingDisplayListChanges(TreeObserver& observer, TreeInfo& info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700461 if (mNeedsDisplayListSync) {
462 mNeedsDisplayListSync = false;
John Reck5c9d7172014-10-22 11:32:27 -0700463 // Damage with the old display list first then the new one to catch any
464 // changes in isRenderable or, in the future, bounds
465 damageSelf(info);
John Reck2de950d2017-01-25 10:58:30 -0800466 syncDisplayList(observer, &info);
John Recke4267ea2014-06-03 15:53:15 -0700467 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700468 }
John Reck8de65a82014-04-09 15:23:38 -0700469}
470
John Reck2de950d2017-01-25 10:58:30 -0800471void RenderNode::deleteDisplayList(TreeObserver& observer, TreeInfo* info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700472 if (mDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500473 mDisplayList.updateChildren(
John Reck1bcacfd2017-11-03 10:12:19 -0700474 [&observer, info](RenderNode* child) { child->decParentRefCount(observer, info); });
John Reckbe671952021-01-13 22:39:32 -0500475 mDisplayList.clear(this);
John Reckdcba6722014-07-08 13:59:49 -0700476 }
John Reckdcba6722014-07-08 13:59:49 -0700477}
478
John Reck2de950d2017-01-25 10:58:30 -0800479void RenderNode::destroyHardwareResources(TreeInfo* info) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400480 if (hasLayer()) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400481 this->setLayerSurface(nullptr);
John Reckdcba6722014-07-08 13:59:49 -0700482 }
John Reckbe671952021-01-13 22:39:32 -0500483 discardStagingDisplayList();
John Reck3afd6372017-01-30 10:15:48 -0800484
485 ImmediateRemoved observer(info);
486 deleteDisplayList(observer, info);
John Reck2de950d2017-01-25 10:58:30 -0800487}
488
489void RenderNode::destroyLayers() {
490 if (hasLayer()) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400491 this->setLayerSurface(nullptr);
John Reck2de950d2017-01-25 10:58:30 -0800492 }
Nader Jawadb502ad72021-06-17 14:10:04 -0700493
John Reck2de950d2017-01-25 10:58:30 -0800494 if (mDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500495 mDisplayList.updateChildren([](RenderNode* child) { child->destroyLayers(); });
John Reck2de950d2017-01-25 10:58:30 -0800496 }
497}
498
499void RenderNode::decParentRefCount(TreeObserver& observer, TreeInfo* info) {
500 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
501 mParentCount--;
502 if (!mParentCount) {
503 observer.onMaybeRemovedFromTree(this);
504 if (CC_UNLIKELY(mPositionListener.get())) {
505 mPositionListener->onPositionLost(*this, info);
John Reckdcba6722014-07-08 13:59:49 -0700506 }
507 }
508}
509
John Reck2de950d2017-01-25 10:58:30 -0800510void RenderNode::onRemovedFromTree(TreeInfo* info) {
Huihong Luoec68b7c2021-06-25 21:54:16 -0700511 if (Properties::enableWebViewOverlays && mDisplayList) {
512 mDisplayList.onRemovedFromTree();
513 }
John Reck2de950d2017-01-25 10:58:30 -0800514 destroyHardwareResources(info);
515}
516
517void RenderNode::clearRoot() {
518 ImmediateRemoved observer(nullptr);
519 decParentRefCount(observer);
John Reckdcba6722014-07-08 13:59:49 -0700520}
521
John Reck113e0822014-03-18 09:22:59 -0700522/**
523 * Apply property-based transformations to input matrix
524 *
525 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
526 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
527 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700528void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700529 if (properties().getLeft() != 0 || properties().getTop() != 0) {
530 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700531 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700532 if (properties().getStaticMatrix()) {
533 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700534 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700535 } else if (properties().getAnimationMatrix()) {
536 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700537 matrix.multiply(anim);
538 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700539
Chris Craikcc39e162014-04-25 18:34:11 -0700540 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700541 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700542 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700543 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
John Reck1bcacfd2017-11-03 10:12:19 -0700544 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700545 } else {
546 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700547 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700548 } else {
549 mat4 true3dMat;
John Reck1bcacfd2017-11-03 10:12:19 -0700550 true3dMat.loadTranslate(properties().getPivotX() + properties().getTranslationX(),
551 properties().getPivotY() + properties().getTranslationY(),
552 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700553 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
554 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
555 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
556 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
557 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700558
559 matrix.multiply(true3dMat);
560 }
561 }
562 }
John Reck8ed00dc2021-05-10 13:09:27 -0400563
Nader Jawad93d6e242021-05-17 18:12:29 -0700564 if (Properties::getStretchEffectBehavior() == StretchEffectBehavior::UniformScale) {
John Reck8ed00dc2021-05-10 13:09:27 -0400565 const StretchEffect& stretch = properties().layerProperties().getStretchEffect();
566 if (!stretch.isEmpty()) {
567 matrix.multiply(
568 stretch.makeLinearStretch(properties().getWidth(), properties().getHeight()));
569 }
570 }
John Reck113e0822014-03-18 09:22:59 -0700571}
572
Derek Sollenberger579317d42017-08-29 16:33:49 -0400573const SkPath* RenderNode::getClippedOutline(const SkRect& clipRect) const {
574 const SkPath* outlinePath = properties().getOutline().getPath();
575 const uint32_t outlineID = outlinePath->getGenerationID();
576
577 if (outlineID != mClippedOutlineCache.outlineID || clipRect != mClippedOutlineCache.clipRect) {
578 // update the cache keys
579 mClippedOutlineCache.outlineID = outlineID;
580 mClippedOutlineCache.clipRect = clipRect;
581
582 // update the cache value by recomputing a new path
583 SkPath clipPath;
584 clipPath.addRect(clipRect);
585 Op(*outlinePath, clipPath, kIntersect_SkPathOp, &mClippedOutlineCache.clippedOutline);
Derek Sollenberger579317d42017-08-29 16:33:49 -0400586 }
587 return &mClippedOutlineCache.clippedOutline;
588}
589
John Reckf96b2842018-11-29 09:44:10 -0800590using StringBuffer = FatVector<char, 128>;
591
592template <typename... T>
John Reck7af5b2c2018-12-05 13:36:20 -0800593// TODO:__printflike(2, 3)
594// Doesn't work because the warning doesn't understand string_view and doesn't like that
595// it's not a C-style variadic function.
John Reckf96b2842018-11-29 09:44:10 -0800596static void format(StringBuffer& buffer, const std::string_view& format, T... args) {
597 buffer.resize(buffer.capacity());
598 while (1) {
599 int needed = snprintf(buffer.data(), buffer.size(),
600 format.data(), std::forward<T>(args)...);
601 if (needed < 0) {
602 buffer[0] = '\0';
603 buffer.resize(1);
604 return;
605 }
606 if (needed < buffer.size()) {
607 buffer.resize(needed + 1);
608 return;
609 }
John Reck7af5b2c2018-12-05 13:36:20 -0800610 // If we're doing a heap alloc anyway might as well give it some slop
611 buffer.resize(needed + 100);
John Reckf96b2842018-11-29 09:44:10 -0800612 }
613}
614
615void RenderNode::markDrawStart(SkCanvas& canvas) {
616 StringBuffer buffer;
John Reck7af5b2c2018-12-05 13:36:20 -0800617 format(buffer, "RenderNode(id=%" PRId64 ", name='%s')", uniqueId(), getName());
John Reckf96b2842018-11-29 09:44:10 -0800618 canvas.drawAnnotation(SkRect::MakeWH(getWidth(), getHeight()), buffer.data(), nullptr);
619}
620
621void RenderNode::markDrawEnd(SkCanvas& canvas) {
622 StringBuffer buffer;
John Reck7af5b2c2018-12-05 13:36:20 -0800623 format(buffer, "/RenderNode(id=%" PRId64 ", name='%s')", uniqueId(), getName());
John Reckf96b2842018-11-29 09:44:10 -0800624 canvas.drawAnnotation(SkRect::MakeWH(getWidth(), getHeight()), buffer.data(), nullptr);
625}
626
John Reck113e0822014-03-18 09:22:59 -0700627} /* namespace uirenderer */
628} /* namespace android */