blob: d28bb499c907da36d0bf3cb4a1a441054e6bc225 [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
43#include "utils/MathUtils.h"
44#include "utils/StringUtils.h"
John Reck77c40102015-10-26 15:49:47 -070045
John Reck113e0822014-03-18 09:22:59 -070046namespace android {
47namespace uirenderer {
48
John Reck2de950d2017-01-25 10:58:30 -080049// Used for tree mutations that are purely destructive.
50// Generic tree mutations should use MarkAndSweepObserver instead
51class ImmediateRemoved : public TreeObserver {
52public:
53 explicit ImmediateRemoved(TreeInfo* info) : mTreeInfo(info) {}
54
John Reck1bcacfd2017-11-03 10:12:19 -070055 void onMaybeRemovedFromTree(RenderNode* node) override { node->onRemovedFromTree(mTreeInfo); }
John Reck2de950d2017-01-25 10:58:30 -080056
57private:
58 TreeInfo* mTreeInfo;
59};
60
John Reckf96b2842018-11-29 09:44:10 -080061static int64_t generateId() {
62 static std::atomic<int64_t> sNextId{1};
63 return sNextId++;
64}
65
John Reck8de65a82014-04-09 15:23:38 -070066RenderNode::RenderNode()
John Reckf96b2842018-11-29 09:44:10 -080067 : mUniqueId(generateId())
68 , mDirtyPropertyFields(0)
Chris Craik003cc3d2015-10-16 10:24:55 -070069 , mNeedsDisplayListSync(false)
70 , mDisplayList(nullptr)
71 , mStagingDisplayList(nullptr)
John Reck68bfe0a2014-06-24 15:34:58 -070072 , mAnimatorManager(*this)
John Reck1bcacfd2017-11-03 10:12:19 -070073 , mParentCount(0) {}
John Reck113e0822014-03-18 09:22:59 -070074
75RenderNode::~RenderNode() {
John Reck2de950d2017-01-25 10:58:30 -080076 ImmediateRemoved observer(nullptr);
77 deleteDisplayList(observer);
Derek Sollenberger0df62092016-09-27 16:04:42 -040078 LOG_ALWAYS_FATAL_IF(hasLayer(), "layer missed detachment!");
John Reck113e0822014-03-18 09:22:59 -070079}
80
John Reckbe671952021-01-13 22:39:32 -050081void RenderNode::setStagingDisplayList(DisplayList&& newData) {
82 mValid = newData.isValid();
Chris Craik003cc3d2015-10-16 10:24:55 -070083 mNeedsDisplayListSync = true;
John Reckbe671952021-01-13 22:39:32 -050084 mStagingDisplayList = std::move(newData);
John Reck113e0822014-03-18 09:22:59 -070085}
86
John Reckdc1a6962021-01-12 13:56:07 -050087void RenderNode::discardStagingDisplayList() {
John Reckbe671952021-01-13 22:39:32 -050088 setStagingDisplayList(DisplayList());
John Reckdc1a6962021-01-12 13:56:07 -050089}
90
John Reck113e0822014-03-18 09:22:59 -070091/**
92 * This function is a simplified version of replay(), where we simply retrieve and log the
93 * display list. This function should remain in sync with the replay() function.
94 */
sergeyvc3849aa2016-08-08 13:22:06 -070095void RenderNode::output() {
96 LogcatStream strout;
97 strout << "Root";
98 output(strout, 0);
99}
100
101void RenderNode::output(std::ostream& output, uint32_t level) {
102 output << " (" << getName() << " " << this
John Reck1bcacfd2017-11-03 10:12:19 -0700103 << (MathUtils::isZero(properties().getAlpha()) ? ", zero alpha" : "")
104 << (properties().hasShadow() ? ", casting shadow" : "")
105 << (isRenderable() ? "" : ", empty")
106 << (properties().getProjectBackwards() ? ", projected" : "")
107 << (hasLayer() ? ", on HW Layer" : "") << ")" << std::endl;
sergeyvc3849aa2016-08-08 13:22:06 -0700108
109 properties().debugOutputProperties(output, level + 1);
Chris Craik91eff222016-02-22 13:39:33 -0800110
John Reckbe671952021-01-13 22:39:32 -0500111 mDisplayList.output(output, level);
sergeyvc3849aa2016-08-08 13:22:06 -0700112 output << std::string(level * 2, ' ') << "/RenderNode(" << getName() << " " << this << ")";
113 output << std::endl;
Chris Craik91eff222016-02-22 13:39:33 -0800114}
John Reck113e0822014-03-18 09:22:59 -0700115
John Reck07f3d912023-08-17 12:46:44 -0400116void RenderNode::visit(std::function<void(const RenderNode&)> func) const {
117 func(*this);
118 if (mDisplayList) {
119 mDisplayList.visit(func);
120 }
121}
122
John Reck183e1382019-10-09 13:41:18 -0700123int RenderNode::getUsageSize() {
John Reckfe5e7b72014-05-23 17:42:28 -0700124 int size = sizeof(RenderNode);
John Reckbe671952021-01-13 22:39:32 -0500125 size += mStagingDisplayList.getUsedSize();
126 size += mDisplayList.getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700127 return size;
128}
129
John Reck183e1382019-10-09 13:41:18 -0700130int RenderNode::getAllocatedSize() {
131 int size = sizeof(RenderNode);
John Reckbe671952021-01-13 22:39:32 -0500132 size += mStagingDisplayList.getAllocatedSize();
133 size += mDisplayList.getAllocatedSize();
John Reck183e1382019-10-09 13:41:18 -0700134 return size;
135}
136
137
John Reckf4198b72014-04-09 17:00:04 -0700138void RenderNode::prepareTree(TreeInfo& info) {
139 ATRACE_CALL();
Chris Craik69e5adf2014-08-14 13:34:01 -0700140 LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
John Reck2de950d2017-01-25 10:58:30 -0800141 MarkAndSweepRemoved observer(&info);
John Reckf4198b72014-04-09 17:00:04 -0700142
John Reck1423e132018-09-21 14:30:19 -0700143 const int before = info.disableForceDark;
John Reck1072fff2018-04-12 15:20:09 -0700144 prepareTreeImpl(observer, info, false);
John Reck1423e132018-09-21 14:30:19 -0700145 LOG_ALWAYS_FATAL_IF(before != info.disableForceDark, "Mis-matched force dark");
John Reckf4198b72014-04-09 17:00:04 -0700146}
147
John Reck68bfe0a2014-06-24 15:34:58 -0700148void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
149 mAnimatorManager.addAnimator(animator);
150}
151
Doris Liu8b083202016-02-19 21:46:06 +0000152void RenderNode::removeAnimator(const sp<BaseRenderNodeAnimator>& animator) {
153 mAnimatorManager.removeAnimator(animator);
154}
155
John Recke4267ea2014-06-03 15:53:15 -0700156void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700157 if (isRenderable()) {
John Reckf1aa7909e2019-03-07 17:01:08 -0800158 mDamageGenerationId = info.damageGenerationId;
John Reck293e8682014-06-17 10:34:02 -0700159 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700160 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
161 } else {
162 // Hope this is big enough?
163 // TODO: Get this from the display list ops or something
John Reckc1288232015-08-12 13:39:11 -0700164 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
John Recka447d292014-06-11 18:39:44 -0700165 }
Ady Abrahamcab4afe2023-05-09 11:25:22 -0700166 if (!mIsTextureView) {
167 info.out.solelyTextureViewUpdates = false;
168 }
John Recke4267ea2014-06-03 15:53:15 -0700169 }
170}
171
John Recka7c2ea22014-08-08 13:21:00 -0700172void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700173 LayerType layerType = properties().effectiveLayerType();
Chris Craik182952f2015-03-09 14:17:29 -0700174 if (CC_UNLIKELY(layerType == LayerType::RenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700175 // Damage applied so far needs to affect our parent, but does not require
176 // the layer to be updated. So we pop/push here to clear out the current
177 // damage and get a clean state for display list or children updates to
178 // affect, which will require the layer to be updated
179 info.damageAccumulator->popTransform();
180 info.damageAccumulator->pushTransform(this);
181 if (dirtyMask & DISPLAY_LIST) {
182 damageSelf(info);
183 }
John Reck25fbb3f2014-06-12 13:46:45 -0700184 }
185}
186
187void RenderNode::pushLayerUpdate(TreeInfo& info) {
Fedor Kudasov86bd2142019-06-18 15:51:57 +0100188#ifdef __ANDROID__ // Layoutlib does not support CanvasContext and Layers
Chris Craik856f0cc2015-04-21 15:13:29 -0700189 LayerType layerType = properties().effectiveLayerType();
John Reck25fbb3f2014-06-12 13:46:45 -0700190 // If we are not a layer OR we cannot be rendered (eg, view was detached)
191 // we need to destroy any Layers we may have had previously
John Reck1bcacfd2017-11-03 10:12:19 -0700192 if (CC_LIKELY(layerType != LayerType::RenderLayer) || CC_UNLIKELY(!isRenderable()) ||
193 CC_UNLIKELY(properties().getWidth() == 0) || CC_UNLIKELY(properties().getHeight() == 0) ||
194 CC_UNLIKELY(!properties().fitsOnLayer())) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400195 if (CC_UNLIKELY(hasLayer())) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400196 this->setLayerSurface(nullptr);
John Reck25fbb3f2014-06-12 13:46:45 -0700197 }
198 return;
199 }
200
Stan Iliev216b1572018-03-26 14:29:50 -0400201 if (info.canvasContext.createOrUpdateLayer(this, *info.damageAccumulator, info.errorHandler)) {
Chris Craik9fded232015-11-11 16:42:34 -0800202 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700203 }
204
Derek Sollenberger0df62092016-09-27 16:04:42 -0400205 if (!hasLayer()) {
John Reckc25e5062014-06-18 14:21:29 -0700206 return;
207 }
208
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400209 SkRect dirty;
210 info.damageAccumulator->peekAtDirty(&dirty);
Chris Craik0b7e8242015-10-28 16:50:44 -0700211 info.layerUpdateQueue->enqueueLayerWithDamage(this, dirty);
Nader Jawad197743f2021-04-19 19:45:13 -0700212 if (!dirty.isEmpty()) {
213 mStretchMask.markDirty();
214 }
John Reck998a6d82014-08-28 15:35:53 -0700215
Chris Craike2e53a72015-10-28 15:55:40 -0700216 // There might be prefetched layers that need to be accounted for.
217 // That might be us, so tell CanvasContext that this layer is in the
218 // tree and should not be destroyed.
219 info.canvasContext.markLayerInUse(this);
Fedor Kudasov86bd2142019-06-18 15:51:57 +0100220#endif
John Reck25fbb3f2014-06-12 13:46:45 -0700221}
222
Chris Craika766cb22015-06-08 16:49:43 -0700223/**
224 * Traverse down the the draw tree to prepare for a frame.
225 *
226 * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven
227 *
228 * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the
229 * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer.
230 */
John Reck2de950d2017-01-25 10:58:30 -0800231void RenderNode::prepareTreeImpl(TreeObserver& observer, TreeInfo& info, bool functorsNeedLayer) {
John Reckf1aa7909e2019-03-07 17:01:08 -0800232 if (mDamageGenerationId == info.damageGenerationId) {
233 // We hit the same node a second time in the same tree. We don't know the minimal
234 // damage rect anymore, so just push the biggest we can onto our parent's transform
235 // We push directly onto parent in case we are clipped to bounds but have moved position.
236 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
237 }
John Recka447d292014-06-11 18:39:44 -0700238 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700239
John Reckdcba6722014-07-08 13:59:49 -0700240 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700241 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700242 }
John Reck1423e132018-09-21 14:30:19 -0700243
244 if (!mProperties.getAllowForceDark()) {
245 info.disableForceDark++;
246 }
John Reck5d53a752021-02-08 19:22:59 -0500247 if (!mProperties.layerProperties().getStretchEffect().isEmpty()) {
248 info.stretchEffectCount++;
249 }
John Reck1423e132018-09-21 14:30:19 -0700250
John Reck9eb9f6f2014-08-21 11:23:05 -0700251 uint32_t animatorDirtyMask = 0;
252 if (CC_LIKELY(info.runAnimations)) {
253 animatorDirtyMask = mAnimatorManager.animate(info);
254 }
Chris Craika766cb22015-06-08 16:49:43 -0700255
John Reck3f725f02015-06-16 10:29:31 -0700256 bool willHaveFunctor = false;
Chris Craik003cc3d2015-10-16 10:24:55 -0700257 if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500258 willHaveFunctor = mStagingDisplayList.hasFunctor();
Chris Craik003cc3d2015-10-16 10:24:55 -0700259 } else if (mDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500260 willHaveFunctor = mDisplayList.hasFunctor();
John Reck3f725f02015-06-16 10:29:31 -0700261 }
John Reck1bcacfd2017-11-03 10:12:19 -0700262 bool childFunctorsNeedLayer =
263 mProperties.prepareForFunctorPresence(willHaveFunctor, functorsNeedLayer);
Chris Craika766cb22015-06-08 16:49:43 -0700264
John Reckf6481082016-02-02 15:18:23 -0800265 if (CC_UNLIKELY(mPositionListener.get())) {
266 mPositionListener->onPositionUpdated(*this, info);
267 }
268
John Recka7c2ea22014-08-08 13:21:00 -0700269 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700270 if (info.mode == TreeInfo::MODE_FULL) {
John Reck2de950d2017-01-25 10:58:30 -0800271 pushStagingDisplayListChanges(observer, info);
John Reck25fbb3f2014-06-12 13:46:45 -0700272 }
John Reck25fbb3f2014-06-12 13:46:45 -0700273
Dongya Jiange2b99382022-02-28 21:35:57 +0800274 // always damageSelf when filtering backdrop content, or else the BackdropFilterDrawable will
275 // get a wrong snapshot of previous content.
276 if (mProperties.layerProperties().getBackdropImageFilter()) {
277 damageSelf(info);
278 }
279
Doris Liu07c056d2016-06-13 12:52:44 -0700280 if (mDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500281 info.out.hasFunctors |= mDisplayList.hasFunctor();
Nader Jawad2dc632a2021-03-29 18:51:29 -0700282 mHasHolePunches = mDisplayList.hasHolePunches();
John Reckbe671952021-01-13 22:39:32 -0500283 bool isDirty = mDisplayList.prepareListAndChildren(
John Reck1bcacfd2017-11-03 10:12:19 -0700284 observer, info, childFunctorsNeedLayer,
Nader Jawad2dc632a2021-03-29 18:51:29 -0700285 [this](RenderNode* child, TreeObserver& observer, TreeInfo& info,
286 bool functorsNeedLayer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700287 child->prepareTreeImpl(observer, info, functorsNeedLayer);
Nader Jawad2dc632a2021-03-29 18:51:29 -0700288 mHasHolePunches |= child->hasHolePunches();
John Reck1bcacfd2017-11-03 10:12:19 -0700289 });
Derek Sollenberger0df62092016-09-27 16:04:42 -0400290 if (isDirty) {
291 damageSelf(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700292 }
Nader Jawad2dc632a2021-03-29 18:51:29 -0700293 } else {
294 mHasHolePunches = false;
Doris Liu718cd3e2016-05-17 16:50:31 -0700295 }
Doris Liub51b2862016-08-01 19:56:47 -0700296 pushLayerUpdate(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700297
John Reck1423e132018-09-21 14:30:19 -0700298 if (!mProperties.getAllowForceDark()) {
299 info.disableForceDark--;
300 }
John Reck5d53a752021-02-08 19:22:59 -0500301 if (!mProperties.layerProperties().getStretchEffect().isEmpty()) {
302 info.stretchEffectCount--;
303 }
John Recka447d292014-06-11 18:39:44 -0700304 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700305}
306
Chris Craikb565df12015-10-05 13:00:52 -0700307void RenderNode::syncProperties() {
308 mProperties = mStagingProperties;
309}
310
John Reck25fbb3f2014-06-12 13:46:45 -0700311void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reck097e1d32019-06-12 15:01:51 -0700312 if (mPositionListenerDirty) {
313 mPositionListener = std::move(mStagingPositionListener);
314 mStagingPositionListener = nullptr;
315 mPositionListenerDirty = false;
316 }
317
John Reckff941dc2014-05-14 16:34:14 -0700318 // Push the animators first so that setupStartValueIfNecessary() is called
319 // before properties() is trampled by stagingProperties(), as they are
320 // required by some animators.
John Reck9eb9f6f2014-08-21 11:23:05 -0700321 if (CC_LIKELY(info.runAnimations)) {
John Reck119907c2014-08-14 09:02:01 -0700322 mAnimatorManager.pushStaging();
John Reck9eb9f6f2014-08-21 11:23:05 -0700323 }
John Reckff941dc2014-05-14 16:34:14 -0700324 if (mDirtyPropertyFields) {
325 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700326 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700327 info.damageAccumulator->popTransform();
Chris Craikb565df12015-10-05 13:00:52 -0700328 syncProperties();
Nader Jawad197743f2021-04-19 19:45:13 -0700329
Nader Jawad6aff4812021-06-09 10:14:43 -0700330 auto& layerProperties = mProperties.layerProperties();
331 const StretchEffect& stagingStretch = layerProperties.getStretchEffect();
Nader Jawad197743f2021-04-19 19:45:13 -0700332 if (stagingStretch.isEmpty()) {
333 mStretchMask.clear();
334 }
Nader Jawad6aff4812021-06-09 10:14:43 -0700335
336 if (layerProperties.getImageFilter() == nullptr) {
337 mSnapshotResult.snapshot = nullptr;
338 mTargetImageFilter = nullptr;
339 }
340
John Recke4267ea2014-06-03 15:53:15 -0700341 // We could try to be clever and only re-damage if the matrix changed.
342 // However, we don't need to worry about that. The cost of over-damaging
343 // here is only going to be a single additional map rect of this node
344 // plus a rect join(). The parent's transform (and up) will only be
345 // performed once.
John Recka447d292014-06-11 18:39:44 -0700346 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700347 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700348 }
John Reck25fbb3f2014-06-12 13:46:45 -0700349}
350
Nader Jawad6aff4812021-06-09 10:14:43 -0700351std::optional<RenderNode::SnapshotResult> RenderNode::updateSnapshotIfRequired(
352 GrRecordingContext* context,
353 const SkImageFilter* imageFilter,
354 const SkIRect& clipBounds
355) {
356 auto* layerSurface = getLayerSurface();
357 if (layerSurface == nullptr) {
358 return std::nullopt;
359 }
360
361 sk_sp<SkImage> snapshot = layerSurface->makeImageSnapshot();
362 const auto subset = SkIRect::MakeWH(properties().getWidth(),
363 properties().getHeight());
Nader Jawadf49068f2021-09-08 22:17:15 -0700364 uint32_t layerSurfaceGenerationId = layerSurface->generationID();
Nader Jawad6aff4812021-06-09 10:14:43 -0700365 // If we don't have an ImageFilter just return the snapshot
366 if (imageFilter == nullptr) {
367 mSnapshotResult.snapshot = snapshot;
368 mSnapshotResult.outSubset = subset;
369 mSnapshotResult.outOffset = SkIPoint::Make(0.0f, 0.0f);
370 mImageFilterClipBounds = clipBounds;
371 mTargetImageFilter = nullptr;
Nader Jawadf49068f2021-09-08 22:17:15 -0700372 mTargetImageFilterLayerSurfaceGenerationId = 0;
373 } else if (mSnapshotResult.snapshot == nullptr || imageFilter != mTargetImageFilter.get() ||
374 mImageFilterClipBounds != clipBounds ||
375 mTargetImageFilterLayerSurfaceGenerationId != layerSurfaceGenerationId) {
Nader Jawad6aff4812021-06-09 10:14:43 -0700376 // Otherwise create a new snapshot with the given filter and snapshot
Robert Phillips74fe4712023-09-06 15:32:34 -0400377#ifdef __ANDROID__
378 if (context) {
379 mSnapshotResult.snapshot = SkImages::MakeWithFilter(
380 context, snapshot, imageFilter, subset, clipBounds, &mSnapshotResult.outSubset,
381 &mSnapshotResult.outOffset);
382 } else
383#endif
384 {
385 mSnapshotResult.snapshot = SkImages::MakeWithFilter(
386 snapshot, imageFilter, subset, clipBounds, &mSnapshotResult.outSubset,
387 &mSnapshotResult.outOffset);
388 }
Nader Jawad6aff4812021-06-09 10:14:43 -0700389 mTargetImageFilter = sk_ref_sp(imageFilter);
390 mImageFilterClipBounds = clipBounds;
Nader Jawadf49068f2021-09-08 22:17:15 -0700391 mTargetImageFilterLayerSurfaceGenerationId = layerSurfaceGenerationId;
Nader Jawad6aff4812021-06-09 10:14:43 -0700392 }
393
394 return mSnapshotResult;
395}
396
John Reck2de950d2017-01-25 10:58:30 -0800397void RenderNode::syncDisplayList(TreeObserver& observer, TreeInfo* info) {
Chris Craikb565df12015-10-05 13:00:52 -0700398 // Make sure we inc first so that we don't fluctuate between 0 and 1,
399 // which would thrash the layer cache
Chris Craik003cc3d2015-10-16 10:24:55 -0700400 if (mStagingDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500401 mStagingDisplayList.updateChildren([](RenderNode* child) { child->incParentRefCount(); });
Chris Craikb565df12015-10-05 13:00:52 -0700402 }
John Reck2de950d2017-01-25 10:58:30 -0800403 deleteDisplayList(observer, info);
John Reckbe671952021-01-13 22:39:32 -0500404 mDisplayList = std::move(mStagingDisplayList);
Chris Craik003cc3d2015-10-16 10:24:55 -0700405 if (mDisplayList) {
John Reck283bb462018-12-13 16:40:14 -0800406 WebViewSyncData syncData {
407 .applyForceDark = info && !info->disableForceDark
408 };
John Reckbe671952021-01-13 22:39:32 -0500409 mDisplayList.syncContents(syncData);
John Reck3b4510cd2018-09-27 17:39:45 -0700410 handleForceDark(info);
411 }
412}
John Reckf3c724f2018-09-20 13:00:04 -0700413
John Reck3b4510cd2018-09-27 17:39:45 -0700414void RenderNode::handleForceDark(android::uirenderer::TreeInfo *info) {
415 if (CC_LIKELY(!info || info->disableForceDark)) {
416 return;
417 }
418 auto usage = usageHint();
John Reckbe671952021-01-13 22:39:32 -0500419 FatVector<RenderNode*, 6> children;
420 mDisplayList.updateChildren([&children](RenderNode* node) {
421 children.push_back(node);
422 });
423 if (mDisplayList.hasText()) {
John Reck3b4510cd2018-09-27 17:39:45 -0700424 usage = UsageHint::Foreground;
425 }
426 if (usage == UsageHint::Unknown) {
427 if (children.size() > 1) {
428 usage = UsageHint::Background;
429 } else if (children.size() == 1 &&
John Reckbe671952021-01-13 22:39:32 -0500430 children.front()->usageHint() !=
John Reck3b4510cd2018-09-27 17:39:45 -0700431 UsageHint::Background) {
432 usage = UsageHint::Background;
John Reck8f45d4a2018-08-15 10:17:12 -0700433 }
Chris Craikb565df12015-10-05 13:00:52 -0700434 }
John Reck3b4510cd2018-09-27 17:39:45 -0700435 if (children.size() > 1) {
436 // Crude overlap check
437 SkRect drawn = SkRect::MakeEmpty();
438 for (auto iter = children.rbegin(); iter != children.rend(); ++iter) {
John Reckbe671952021-01-13 22:39:32 -0500439 const auto& child = *iter;
John Reck3b4510cd2018-09-27 17:39:45 -0700440 // We use stagingProperties here because we haven't yet sync'd the children
441 SkRect bounds = SkRect::MakeXYWH(child->stagingProperties().getX(), child->stagingProperties().getY(),
442 child->stagingProperties().getWidth(), child->stagingProperties().getHeight());
443 if (bounds.contains(drawn)) {
444 // This contains everything drawn after it, so make it a background
445 child->setUsageHint(UsageHint::Background);
446 }
447 drawn.join(bounds);
448 }
449 }
John Reckbe671952021-01-13 22:39:32 -0500450 mDisplayList.applyColorTransform(
John Reck3b4510cd2018-09-27 17:39:45 -0700451 usage == UsageHint::Background ? ColorTransform::Dark : ColorTransform::Light);
Chris Craikb565df12015-10-05 13:00:52 -0700452}
453
John Reck2de950d2017-01-25 10:58:30 -0800454void RenderNode::pushStagingDisplayListChanges(TreeObserver& observer, TreeInfo& info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700455 if (mNeedsDisplayListSync) {
456 mNeedsDisplayListSync = false;
John Reck5c9d7172014-10-22 11:32:27 -0700457 // Damage with the old display list first then the new one to catch any
458 // changes in isRenderable or, in the future, bounds
459 damageSelf(info);
John Reck2de950d2017-01-25 10:58:30 -0800460 syncDisplayList(observer, &info);
John Recke4267ea2014-06-03 15:53:15 -0700461 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700462 }
John Reck8de65a82014-04-09 15:23:38 -0700463}
464
John Reck2de950d2017-01-25 10:58:30 -0800465void RenderNode::deleteDisplayList(TreeObserver& observer, TreeInfo* info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700466 if (mDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500467 mDisplayList.updateChildren(
John Reck1bcacfd2017-11-03 10:12:19 -0700468 [&observer, info](RenderNode* child) { child->decParentRefCount(observer, info); });
John Reckbe671952021-01-13 22:39:32 -0500469 mDisplayList.clear(this);
John Reckdcba6722014-07-08 13:59:49 -0700470 }
John Reckdcba6722014-07-08 13:59:49 -0700471}
472
John Reck2de950d2017-01-25 10:58:30 -0800473void RenderNode::destroyHardwareResources(TreeInfo* info) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400474 if (hasLayer()) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400475 this->setLayerSurface(nullptr);
John Reckdcba6722014-07-08 13:59:49 -0700476 }
John Reckbe671952021-01-13 22:39:32 -0500477 discardStagingDisplayList();
John Reck3afd6372017-01-30 10:15:48 -0800478
479 ImmediateRemoved observer(info);
480 deleteDisplayList(observer, info);
John Reck2de950d2017-01-25 10:58:30 -0800481}
482
483void RenderNode::destroyLayers() {
484 if (hasLayer()) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400485 this->setLayerSurface(nullptr);
John Reck2de950d2017-01-25 10:58:30 -0800486 }
Nader Jawadb502ad72021-06-17 14:10:04 -0700487
John Reck2de950d2017-01-25 10:58:30 -0800488 if (mDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500489 mDisplayList.updateChildren([](RenderNode* child) { child->destroyLayers(); });
John Reck2de950d2017-01-25 10:58:30 -0800490 }
491}
492
493void RenderNode::decParentRefCount(TreeObserver& observer, TreeInfo* info) {
494 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
495 mParentCount--;
496 if (!mParentCount) {
497 observer.onMaybeRemovedFromTree(this);
498 if (CC_UNLIKELY(mPositionListener.get())) {
499 mPositionListener->onPositionLost(*this, info);
John Reckdcba6722014-07-08 13:59:49 -0700500 }
501 }
502}
503
John Reck2de950d2017-01-25 10:58:30 -0800504void RenderNode::onRemovedFromTree(TreeInfo* info) {
Huihong Luoec68b7c2021-06-25 21:54:16 -0700505 if (Properties::enableWebViewOverlays && mDisplayList) {
506 mDisplayList.onRemovedFromTree();
507 }
John Reck2de950d2017-01-25 10:58:30 -0800508 destroyHardwareResources(info);
509}
510
511void RenderNode::clearRoot() {
512 ImmediateRemoved observer(nullptr);
513 decParentRefCount(observer);
John Reckdcba6722014-07-08 13:59:49 -0700514}
515
John Reck113e0822014-03-18 09:22:59 -0700516/**
517 * Apply property-based transformations to input matrix
518 *
519 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
520 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
521 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700522void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700523 if (properties().getLeft() != 0 || properties().getTop() != 0) {
524 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700525 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700526 if (properties().getStaticMatrix()) {
527 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700528 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700529 } else if (properties().getAnimationMatrix()) {
530 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700531 matrix.multiply(anim);
532 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700533
Chris Craikcc39e162014-04-25 18:34:11 -0700534 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700535 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700536 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700537 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
John Reck1bcacfd2017-11-03 10:12:19 -0700538 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700539 } else {
540 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700541 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700542 } else {
543 mat4 true3dMat;
John Reck1bcacfd2017-11-03 10:12:19 -0700544 true3dMat.loadTranslate(properties().getPivotX() + properties().getTranslationX(),
545 properties().getPivotY() + properties().getTranslationY(),
546 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700547 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
548 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
549 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
550 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
551 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700552
553 matrix.multiply(true3dMat);
554 }
555 }
556 }
John Reck8ed00dc2021-05-10 13:09:27 -0400557
Nader Jawad93d6e242021-05-17 18:12:29 -0700558 if (Properties::getStretchEffectBehavior() == StretchEffectBehavior::UniformScale) {
John Reck8ed00dc2021-05-10 13:09:27 -0400559 const StretchEffect& stretch = properties().layerProperties().getStretchEffect();
560 if (!stretch.isEmpty()) {
561 matrix.multiply(
562 stretch.makeLinearStretch(properties().getWidth(), properties().getHeight()));
563 }
564 }
John Reck113e0822014-03-18 09:22:59 -0700565}
566
Derek Sollenberger579317d42017-08-29 16:33:49 -0400567const SkPath* RenderNode::getClippedOutline(const SkRect& clipRect) const {
568 const SkPath* outlinePath = properties().getOutline().getPath();
569 const uint32_t outlineID = outlinePath->getGenerationID();
570
571 if (outlineID != mClippedOutlineCache.outlineID || clipRect != mClippedOutlineCache.clipRect) {
572 // update the cache keys
573 mClippedOutlineCache.outlineID = outlineID;
574 mClippedOutlineCache.clipRect = clipRect;
575
576 // update the cache value by recomputing a new path
577 SkPath clipPath;
578 clipPath.addRect(clipRect);
579 Op(*outlinePath, clipPath, kIntersect_SkPathOp, &mClippedOutlineCache.clippedOutline);
Derek Sollenberger579317d42017-08-29 16:33:49 -0400580 }
581 return &mClippedOutlineCache.clippedOutline;
582}
583
John Reckf96b2842018-11-29 09:44:10 -0800584using StringBuffer = FatVector<char, 128>;
585
586template <typename... T>
John Reck7af5b2c2018-12-05 13:36:20 -0800587// TODO:__printflike(2, 3)
588// Doesn't work because the warning doesn't understand string_view and doesn't like that
589// it's not a C-style variadic function.
John Reckf96b2842018-11-29 09:44:10 -0800590static void format(StringBuffer& buffer, const std::string_view& format, T... args) {
591 buffer.resize(buffer.capacity());
592 while (1) {
593 int needed = snprintf(buffer.data(), buffer.size(),
594 format.data(), std::forward<T>(args)...);
595 if (needed < 0) {
596 buffer[0] = '\0';
597 buffer.resize(1);
598 return;
599 }
600 if (needed < buffer.size()) {
601 buffer.resize(needed + 1);
602 return;
603 }
John Reck7af5b2c2018-12-05 13:36:20 -0800604 // If we're doing a heap alloc anyway might as well give it some slop
605 buffer.resize(needed + 100);
John Reckf96b2842018-11-29 09:44:10 -0800606 }
607}
608
609void RenderNode::markDrawStart(SkCanvas& canvas) {
610 StringBuffer buffer;
John Reck7af5b2c2018-12-05 13:36:20 -0800611 format(buffer, "RenderNode(id=%" PRId64 ", name='%s')", uniqueId(), getName());
John Reckf96b2842018-11-29 09:44:10 -0800612 canvas.drawAnnotation(SkRect::MakeWH(getWidth(), getHeight()), buffer.data(), nullptr);
613}
614
615void RenderNode::markDrawEnd(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
John Reck113e0822014-03-18 09:22:59 -0700621} /* namespace uirenderer */
622} /* namespace android */