blob: 8b4e59aa73e21d1cad96ae7e458a8a0db0b0ac26 [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
Derek Sollenberger579317d42017-08-29 16:33:49 -040019#include <SkPathOps.h>
Robert Phillips74fe4712023-09-06 15:32:34 -040020#include <gui/TraceUtils.h>
21#include <ui/FatVector.h>
22
John Reck77c40102015-10-26 15:49:47 -070023#include <algorithm>
John Reckf96b2842018-11-29 09:44:10 -080024#include <atomic>
John Reck77c40102015-10-26 15:49:47 -070025#include <sstream>
26#include <string>
Robert Phillips74fe4712023-09-06 15:32:34 -040027
Jerome Gaillarddd37c982024-04-08 15:02:33 +010028#include "DamageAccumulator.h"
29#include "Debug.h"
Daniel Normanfc89f1c2025-02-26 17:45:26 -080030#include "FeatureFlags.h"
Jerome Gaillarddd37c982024-04-08 15:02:33 +010031#include "Properties.h"
32#include "TreeInfo.h"
33#include "VectorDrawable.h"
34#include "private/hwui/WebViewFunctor.h"
35#include "renderthread/CanvasContext.h"
36
Robert Phillips74fe4712023-09-06 15:32:34 -040037#ifdef __ANDROID__
38#include "include/gpu/ganesh/SkImageGanesh.h"
39#endif
Tyler Freeman33567742023-10-31 01:55:51 +000040#include "utils/ForceDark.h"
Robert Phillips74fe4712023-09-06 15:32:34 -040041#include "utils/MathUtils.h"
42#include "utils/StringUtils.h"
John Reck77c40102015-10-26 15:49:47 -070043
John Reck113e0822014-03-18 09:22:59 -070044namespace android {
45namespace uirenderer {
46
John Reck2de950d2017-01-25 10:58:30 -080047// Used for tree mutations that are purely destructive.
48// Generic tree mutations should use MarkAndSweepObserver instead
49class ImmediateRemoved : public TreeObserver {
50public:
51 explicit ImmediateRemoved(TreeInfo* info) : mTreeInfo(info) {}
52
John Reck1bcacfd2017-11-03 10:12:19 -070053 void onMaybeRemovedFromTree(RenderNode* node) override { node->onRemovedFromTree(mTreeInfo); }
John Reck2de950d2017-01-25 10:58:30 -080054
55private:
56 TreeInfo* mTreeInfo;
57};
58
John Reckf96b2842018-11-29 09:44:10 -080059static int64_t generateId() {
60 static std::atomic<int64_t> sNextId{1};
61 return sNextId++;
62}
63
John Reck8de65a82014-04-09 15:23:38 -070064RenderNode::RenderNode()
John Reckf96b2842018-11-29 09:44:10 -080065 : mUniqueId(generateId())
66 , mDirtyPropertyFields(0)
Chris Craik003cc3d2015-10-16 10:24:55 -070067 , mNeedsDisplayListSync(false)
68 , mDisplayList(nullptr)
69 , mStagingDisplayList(nullptr)
John Reck68bfe0a2014-06-24 15:34:58 -070070 , mAnimatorManager(*this)
John Reck1bcacfd2017-11-03 10:12:19 -070071 , mParentCount(0) {}
John Reck113e0822014-03-18 09:22:59 -070072
73RenderNode::~RenderNode() {
John Reck2de950d2017-01-25 10:58:30 -080074 ImmediateRemoved observer(nullptr);
75 deleteDisplayList(observer);
Derek Sollenberger0df62092016-09-27 16:04:42 -040076 LOG_ALWAYS_FATAL_IF(hasLayer(), "layer missed detachment!");
John Reck113e0822014-03-18 09:22:59 -070077}
78
John Reckbe671952021-01-13 22:39:32 -050079void RenderNode::setStagingDisplayList(DisplayList&& newData) {
80 mValid = newData.isValid();
Chris Craik003cc3d2015-10-16 10:24:55 -070081 mNeedsDisplayListSync = true;
John Reckbe671952021-01-13 22:39:32 -050082 mStagingDisplayList = std::move(newData);
John Reck113e0822014-03-18 09:22:59 -070083}
84
John Reckdc1a6962021-01-12 13:56:07 -050085void RenderNode::discardStagingDisplayList() {
John Reckbe671952021-01-13 22:39:32 -050086 setStagingDisplayList(DisplayList());
John Reckdc1a6962021-01-12 13:56:07 -050087}
88
John Reck113e0822014-03-18 09:22:59 -070089/**
90 * This function is a simplified version of replay(), where we simply retrieve and log the
91 * display list. This function should remain in sync with the replay() function.
92 */
sergeyvc3849aa2016-08-08 13:22:06 -070093void RenderNode::output() {
94 LogcatStream strout;
95 strout << "Root";
96 output(strout, 0);
97}
98
99void RenderNode::output(std::ostream& output, uint32_t level) {
100 output << " (" << getName() << " " << this
John Reck1bcacfd2017-11-03 10:12:19 -0700101 << (MathUtils::isZero(properties().getAlpha()) ? ", zero alpha" : "")
102 << (properties().hasShadow() ? ", casting shadow" : "")
103 << (isRenderable() ? "" : ", empty")
104 << (properties().getProjectBackwards() ? ", projected" : "")
105 << (hasLayer() ? ", on HW Layer" : "") << ")" << std::endl;
sergeyvc3849aa2016-08-08 13:22:06 -0700106
107 properties().debugOutputProperties(output, level + 1);
Chris Craik91eff222016-02-22 13:39:33 -0800108
John Reckbe671952021-01-13 22:39:32 -0500109 mDisplayList.output(output, level);
sergeyvc3849aa2016-08-08 13:22:06 -0700110 output << std::string(level * 2, ' ') << "/RenderNode(" << getName() << " " << this << ")";
111 output << std::endl;
Chris Craik91eff222016-02-22 13:39:33 -0800112}
John Reck113e0822014-03-18 09:22:59 -0700113
John Reck07f3d912023-08-17 12:46:44 -0400114void RenderNode::visit(std::function<void(const RenderNode&)> func) const {
115 func(*this);
116 if (mDisplayList) {
117 mDisplayList.visit(func);
118 }
119}
120
John Reck183e1382019-10-09 13:41:18 -0700121int RenderNode::getUsageSize() {
John Reckfe5e7b72014-05-23 17:42:28 -0700122 int size = sizeof(RenderNode);
John Reckbe671952021-01-13 22:39:32 -0500123 size += mStagingDisplayList.getUsedSize();
124 size += mDisplayList.getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700125 return size;
126}
127
John Reck183e1382019-10-09 13:41:18 -0700128int RenderNode::getAllocatedSize() {
129 int size = sizeof(RenderNode);
John Reckbe671952021-01-13 22:39:32 -0500130 size += mStagingDisplayList.getAllocatedSize();
131 size += mDisplayList.getAllocatedSize();
John Reck183e1382019-10-09 13:41:18 -0700132 return size;
133}
134
135
John Reckf4198b72014-04-09 17:00:04 -0700136void RenderNode::prepareTree(TreeInfo& info) {
137 ATRACE_CALL();
Chris Craik69e5adf2014-08-14 13:34:01 -0700138 LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
John Reck2de950d2017-01-25 10:58:30 -0800139 MarkAndSweepRemoved observer(&info);
John Reckf4198b72014-04-09 17:00:04 -0700140
John Reck1423e132018-09-21 14:30:19 -0700141 const int before = info.disableForceDark;
John Reck1072fff2018-04-12 15:20:09 -0700142 prepareTreeImpl(observer, info, false);
John Reck1423e132018-09-21 14:30:19 -0700143 LOG_ALWAYS_FATAL_IF(before != info.disableForceDark, "Mis-matched force dark");
John Reckf4198b72014-04-09 17:00:04 -0700144}
145
John Reck68bfe0a2014-06-24 15:34:58 -0700146void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
147 mAnimatorManager.addAnimator(animator);
148}
149
Doris Liu8b083202016-02-19 21:46:06 +0000150void RenderNode::removeAnimator(const sp<BaseRenderNodeAnimator>& animator) {
151 mAnimatorManager.removeAnimator(animator);
152}
153
John Recke4267ea2014-06-03 15:53:15 -0700154void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700155 if (isRenderable()) {
John Reckf1aa7909e2019-03-07 17:01:08 -0800156 mDamageGenerationId = info.damageGenerationId;
John Reck293e8682014-06-17 10:34:02 -0700157 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700158 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
159 } else {
160 // Hope this is big enough?
161 // TODO: Get this from the display list ops or something
John Reckc1288232015-08-12 13:39:11 -0700162 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
John Recka447d292014-06-11 18:39:44 -0700163 }
Ady Abrahamcab4afe2023-05-09 11:25:22 -0700164 if (!mIsTextureView) {
165 info.out.solelyTextureViewUpdates = false;
166 }
John Recke4267ea2014-06-03 15:53:15 -0700167 }
168}
169
John Recka7c2ea22014-08-08 13:21:00 -0700170void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700171 LayerType layerType = properties().effectiveLayerType();
Chris Craik182952f2015-03-09 14:17:29 -0700172 if (CC_UNLIKELY(layerType == LayerType::RenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700173 // Damage applied so far needs to affect our parent, but does not require
174 // the layer to be updated. So we pop/push here to clear out the current
175 // damage and get a clean state for display list or children updates to
176 // affect, which will require the layer to be updated
177 info.damageAccumulator->popTransform();
178 info.damageAccumulator->pushTransform(this);
179 if (dirtyMask & DISPLAY_LIST) {
180 damageSelf(info);
181 }
John Reck25fbb3f2014-06-12 13:46:45 -0700182 }
183}
184
185void RenderNode::pushLayerUpdate(TreeInfo& info) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700186 LayerType layerType = properties().effectiveLayerType();
John Reck25fbb3f2014-06-12 13:46:45 -0700187 // If we are not a layer OR we cannot be rendered (eg, view was detached)
188 // we need to destroy any Layers we may have had previously
John Reck1bcacfd2017-11-03 10:12:19 -0700189 if (CC_LIKELY(layerType != LayerType::RenderLayer) || CC_UNLIKELY(!isRenderable()) ||
John Reck1895e2e2024-11-13 11:48:29 -0500190 CC_UNLIKELY(properties().getWidth() <= 0) || CC_UNLIKELY(properties().getHeight() <= 0) ||
John Reck1bcacfd2017-11-03 10:12:19 -0700191 CC_UNLIKELY(!properties().fitsOnLayer())) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400192 if (CC_UNLIKELY(hasLayer())) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400193 this->setLayerSurface(nullptr);
John Reck25fbb3f2014-06-12 13:46:45 -0700194 }
195 return;
196 }
197
Stan Iliev216b1572018-03-26 14:29:50 -0400198 if (info.canvasContext.createOrUpdateLayer(this, *info.damageAccumulator, info.errorHandler)) {
Chris Craik9fded232015-11-11 16:42:34 -0800199 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700200 }
201
Derek Sollenberger0df62092016-09-27 16:04:42 -0400202 if (!hasLayer()) {
John Reckc25e5062014-06-18 14:21:29 -0700203 return;
204 }
205
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400206 SkRect dirty;
207 info.damageAccumulator->peekAtDirty(&dirty);
Chris Craik0b7e8242015-10-28 16:50:44 -0700208 info.layerUpdateQueue->enqueueLayerWithDamage(this, dirty);
Nader Jawad197743f2021-04-19 19:45:13 -0700209 if (!dirty.isEmpty()) {
210 mStretchMask.markDirty();
211 }
John Reck998a6d82014-08-28 15:35:53 -0700212
Chris Craike2e53a72015-10-28 15:55:40 -0700213 // There might be prefetched layers that need to be accounted for.
214 // That might be us, so tell CanvasContext that this layer is in the
215 // tree and should not be destroyed.
216 info.canvasContext.markLayerInUse(this);
John Reck25fbb3f2014-06-12 13:46:45 -0700217}
218
Chris Craika766cb22015-06-08 16:49:43 -0700219/**
220 * Traverse down the the draw tree to prepare for a frame.
221 *
222 * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven
223 *
224 * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the
225 * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer.
226 */
John Reck2de950d2017-01-25 10:58:30 -0800227void RenderNode::prepareTreeImpl(TreeObserver& observer, TreeInfo& info, bool functorsNeedLayer) {
qiubowen13d879b2024-01-22 19:46:22 +0800228 if (mDamageGenerationId == info.damageGenerationId && mDamageGenerationId != 0) {
John Reckf1aa7909e2019-03-07 17:01:08 -0800229 // We hit the same node a second time in the same tree. We don't know the minimal
230 // damage rect anymore, so just push the biggest we can onto our parent's transform
231 // We push directly onto parent in case we are clipped to bounds but have moved position.
232 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
233 }
John Recka447d292014-06-11 18:39:44 -0700234 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700235
John Reckdcba6722014-07-08 13:59:49 -0700236 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700237 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700238 }
John Reck1423e132018-09-21 14:30:19 -0700239
240 if (!mProperties.getAllowForceDark()) {
241 info.disableForceDark++;
242 }
John Reck5d53a752021-02-08 19:22:59 -0500243 if (!mProperties.layerProperties().getStretchEffect().isEmpty()) {
244 info.stretchEffectCount++;
245 }
John Reck1423e132018-09-21 14:30:19 -0700246
John Reck9eb9f6f2014-08-21 11:23:05 -0700247 uint32_t animatorDirtyMask = 0;
248 if (CC_LIKELY(info.runAnimations)) {
249 animatorDirtyMask = mAnimatorManager.animate(info);
250 }
Chris Craika766cb22015-06-08 16:49:43 -0700251
John Reck3f725f02015-06-16 10:29:31 -0700252 bool willHaveFunctor = false;
Chris Craik003cc3d2015-10-16 10:24:55 -0700253 if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500254 willHaveFunctor = mStagingDisplayList.hasFunctor();
Chris Craik003cc3d2015-10-16 10:24:55 -0700255 } else if (mDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500256 willHaveFunctor = mDisplayList.hasFunctor();
John Reck3f725f02015-06-16 10:29:31 -0700257 }
John Reck1bcacfd2017-11-03 10:12:19 -0700258 bool childFunctorsNeedLayer =
259 mProperties.prepareForFunctorPresence(willHaveFunctor, functorsNeedLayer);
Chris Craika766cb22015-06-08 16:49:43 -0700260
John Reckf6481082016-02-02 15:18:23 -0800261 if (CC_UNLIKELY(mPositionListener.get())) {
262 mPositionListener->onPositionUpdated(*this, info);
263 }
264
John Recka7c2ea22014-08-08 13:21:00 -0700265 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700266 if (info.mode == TreeInfo::MODE_FULL) {
John Reck2de950d2017-01-25 10:58:30 -0800267 pushStagingDisplayListChanges(observer, info);
John Reck25fbb3f2014-06-12 13:46:45 -0700268 }
John Reck25fbb3f2014-06-12 13:46:45 -0700269
Dongya Jiange2b99382022-02-28 21:35:57 +0800270 // always damageSelf when filtering backdrop content, or else the BackdropFilterDrawable will
271 // get a wrong snapshot of previous content.
272 if (mProperties.layerProperties().getBackdropImageFilter()) {
273 damageSelf(info);
274 }
275
Doris Liu07c056d2016-06-13 12:52:44 -0700276 if (mDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500277 info.out.hasFunctors |= mDisplayList.hasFunctor();
Nader Jawad2dc632a2021-03-29 18:51:29 -0700278 mHasHolePunches = mDisplayList.hasHolePunches();
John Reckbe671952021-01-13 22:39:32 -0500279 bool isDirty = mDisplayList.prepareListAndChildren(
John Reck1bcacfd2017-11-03 10:12:19 -0700280 observer, info, childFunctorsNeedLayer,
Nader Jawad2dc632a2021-03-29 18:51:29 -0700281 [this](RenderNode* child, TreeObserver& observer, TreeInfo& info,
282 bool functorsNeedLayer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700283 child->prepareTreeImpl(observer, info, functorsNeedLayer);
Nader Jawad2dc632a2021-03-29 18:51:29 -0700284 mHasHolePunches |= child->hasHolePunches();
John Reck1bcacfd2017-11-03 10:12:19 -0700285 });
Derek Sollenberger0df62092016-09-27 16:04:42 -0400286 if (isDirty) {
287 damageSelf(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700288 }
Nader Jawad2dc632a2021-03-29 18:51:29 -0700289 } else {
290 mHasHolePunches = false;
Doris Liu718cd3e2016-05-17 16:50:31 -0700291 }
Doris Liub51b2862016-08-01 19:56:47 -0700292 pushLayerUpdate(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700293
John Reck1423e132018-09-21 14:30:19 -0700294 if (!mProperties.getAllowForceDark()) {
295 info.disableForceDark--;
296 }
John Reck5d53a752021-02-08 19:22:59 -0500297 if (!mProperties.layerProperties().getStretchEffect().isEmpty()) {
298 info.stretchEffectCount--;
299 }
John Recka447d292014-06-11 18:39:44 -0700300 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700301}
302
Chris Craikb565df12015-10-05 13:00:52 -0700303void RenderNode::syncProperties() {
304 mProperties = mStagingProperties;
305}
306
John Reck25fbb3f2014-06-12 13:46:45 -0700307void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reck097e1d32019-06-12 15:01:51 -0700308 if (mPositionListenerDirty) {
309 mPositionListener = std::move(mStagingPositionListener);
310 mStagingPositionListener = nullptr;
311 mPositionListenerDirty = false;
312 }
313
John Reckff941dc2014-05-14 16:34:14 -0700314 // Push the animators first so that setupStartValueIfNecessary() is called
315 // before properties() is trampled by stagingProperties(), as they are
316 // required by some animators.
John Reck9eb9f6f2014-08-21 11:23:05 -0700317 if (CC_LIKELY(info.runAnimations)) {
John Reck119907c2014-08-14 09:02:01 -0700318 mAnimatorManager.pushStaging();
John Reck9eb9f6f2014-08-21 11:23:05 -0700319 }
John Reckff941dc2014-05-14 16:34:14 -0700320 if (mDirtyPropertyFields) {
321 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700322 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700323 info.damageAccumulator->popTransform();
Chris Craikb565df12015-10-05 13:00:52 -0700324 syncProperties();
Nader Jawad197743f2021-04-19 19:45:13 -0700325
Nader Jawad6aff4812021-06-09 10:14:43 -0700326 auto& layerProperties = mProperties.layerProperties();
327 const StretchEffect& stagingStretch = layerProperties.getStretchEffect();
Nader Jawad197743f2021-04-19 19:45:13 -0700328 if (stagingStretch.isEmpty()) {
329 mStretchMask.clear();
330 }
Nader Jawad6aff4812021-06-09 10:14:43 -0700331
332 if (layerProperties.getImageFilter() == nullptr) {
333 mSnapshotResult.snapshot = nullptr;
334 mTargetImageFilter = nullptr;
335 }
336
John Recke4267ea2014-06-03 15:53:15 -0700337 // We could try to be clever and only re-damage if the matrix changed.
338 // However, we don't need to worry about that. The cost of over-damaging
339 // here is only going to be a single additional map rect of this node
340 // plus a rect join(). The parent's transform (and up) will only be
341 // performed once.
John Recka447d292014-06-11 18:39:44 -0700342 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700343 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700344 }
John Reck25fbb3f2014-06-12 13:46:45 -0700345}
346
Nader Jawad6aff4812021-06-09 10:14:43 -0700347std::optional<RenderNode::SnapshotResult> RenderNode::updateSnapshotIfRequired(
348 GrRecordingContext* context,
349 const SkImageFilter* imageFilter,
350 const SkIRect& clipBounds
351) {
352 auto* layerSurface = getLayerSurface();
353 if (layerSurface == nullptr) {
354 return std::nullopt;
355 }
356
357 sk_sp<SkImage> snapshot = layerSurface->makeImageSnapshot();
358 const auto subset = SkIRect::MakeWH(properties().getWidth(),
359 properties().getHeight());
Nader Jawadf49068f2021-09-08 22:17:15 -0700360 uint32_t layerSurfaceGenerationId = layerSurface->generationID();
Nader Jawad6aff4812021-06-09 10:14:43 -0700361 // If we don't have an ImageFilter just return the snapshot
362 if (imageFilter == nullptr) {
363 mSnapshotResult.snapshot = snapshot;
364 mSnapshotResult.outSubset = subset;
365 mSnapshotResult.outOffset = SkIPoint::Make(0.0f, 0.0f);
366 mImageFilterClipBounds = clipBounds;
367 mTargetImageFilter = nullptr;
Nader Jawadf49068f2021-09-08 22:17:15 -0700368 mTargetImageFilterLayerSurfaceGenerationId = 0;
369 } else if (mSnapshotResult.snapshot == nullptr || imageFilter != mTargetImageFilter.get() ||
370 mImageFilterClipBounds != clipBounds ||
371 mTargetImageFilterLayerSurfaceGenerationId != layerSurfaceGenerationId) {
Nader Jawad6aff4812021-06-09 10:14:43 -0700372 // Otherwise create a new snapshot with the given filter and snapshot
Robert Phillips74fe4712023-09-06 15:32:34 -0400373#ifdef __ANDROID__
374 if (context) {
375 mSnapshotResult.snapshot = SkImages::MakeWithFilter(
376 context, snapshot, imageFilter, subset, clipBounds, &mSnapshotResult.outSubset,
377 &mSnapshotResult.outOffset);
378 } else
379#endif
380 {
381 mSnapshotResult.snapshot = SkImages::MakeWithFilter(
382 snapshot, imageFilter, subset, clipBounds, &mSnapshotResult.outSubset,
383 &mSnapshotResult.outOffset);
384 }
Nader Jawad6aff4812021-06-09 10:14:43 -0700385 mTargetImageFilter = sk_ref_sp(imageFilter);
386 mImageFilterClipBounds = clipBounds;
Nader Jawadf49068f2021-09-08 22:17:15 -0700387 mTargetImageFilterLayerSurfaceGenerationId = layerSurfaceGenerationId;
Nader Jawad6aff4812021-06-09 10:14:43 -0700388 }
389
390 return mSnapshotResult;
391}
392
John Reck2de950d2017-01-25 10:58:30 -0800393void RenderNode::syncDisplayList(TreeObserver& observer, TreeInfo* info) {
Chris Craikb565df12015-10-05 13:00:52 -0700394 // Make sure we inc first so that we don't fluctuate between 0 and 1,
395 // which would thrash the layer cache
Chris Craik003cc3d2015-10-16 10:24:55 -0700396 if (mStagingDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500397 mStagingDisplayList.updateChildren([](RenderNode* child) { child->incParentRefCount(); });
Chris Craikb565df12015-10-05 13:00:52 -0700398 }
John Reck2de950d2017-01-25 10:58:30 -0800399 deleteDisplayList(observer, info);
John Reckbe671952021-01-13 22:39:32 -0500400 mDisplayList = std::move(mStagingDisplayList);
Chris Craik003cc3d2015-10-16 10:24:55 -0700401 if (mDisplayList) {
Daniel Normanfc89f1c2025-02-26 17:45:26 -0800402 WebViewSyncData syncData{.applyForceDark = shouldEnableForceDark(info) ||
403 (info && isForceInvertDark(*info))};
John Reckbe671952021-01-13 22:39:32 -0500404 mDisplayList.syncContents(syncData);
John Reck3b4510cd2018-09-27 17:39:45 -0700405 handleForceDark(info);
406 }
407}
John Reckf3c724f2018-09-20 13:00:04 -0700408
Daniel Normanfc89f1c2025-02-26 17:45:26 -0800409// Return true if the tree should use the force invert feature that inverts
410// the entire tree to darken it.
Tyler Freeman3455a212024-09-03 18:01:08 +0000411inline bool RenderNode::isForceInvertDark(TreeInfo& info) {
Daniel Normanfc89f1c2025-02-26 17:45:26 -0800412 return CC_UNLIKELY(info.forceDarkType ==
413 android::uirenderer::ForceDarkType::FORCE_INVERT_COLOR_DARK);
Tyler Freeman3455a212024-09-03 18:01:08 +0000414}
415
Daniel Normanfc89f1c2025-02-26 17:45:26 -0800416// Return true if the tree should use the force dark feature that selectively
417// darkens light nodes on the tree.
Tyler Freeman33567742023-10-31 01:55:51 +0000418inline bool RenderNode::shouldEnableForceDark(TreeInfo* info) {
Daniel Normanfc89f1c2025-02-26 17:45:26 -0800419 return CC_UNLIKELY(info && !info->disableForceDark);
Tyler Freeman33567742023-10-31 01:55:51 +0000420}
421
Daniel Normanfc89f1c2025-02-26 17:45:26 -0800422void RenderNode::handleForceDark(TreeInfo *info) {
423 if (CC_UNLIKELY(view_accessibility_flags::force_invert_color() && info &&
424 isForceInvertDark(*info))) {
425 mDisplayList.applyColorTransform(ColorTransform::Invert);
426 return;
427 }
Tyler Freeman33567742023-10-31 01:55:51 +0000428 if (!shouldEnableForceDark(info)) {
John Reck3b4510cd2018-09-27 17:39:45 -0700429 return;
430 }
431 auto usage = usageHint();
John Reckbe671952021-01-13 22:39:32 -0500432 FatVector<RenderNode*, 6> children;
433 mDisplayList.updateChildren([&children](RenderNode* node) {
434 children.push_back(node);
435 });
436 if (mDisplayList.hasText()) {
Daniel Normanfc89f1c2025-02-26 17:45:26 -0800437 usage = UsageHint::Foreground;
John Reck3b4510cd2018-09-27 17:39:45 -0700438 }
439 if (usage == UsageHint::Unknown) {
440 if (children.size() > 1) {
441 usage = UsageHint::Background;
442 } else if (children.size() == 1 &&
John Reckbe671952021-01-13 22:39:32 -0500443 children.front()->usageHint() !=
John Reck3b4510cd2018-09-27 17:39:45 -0700444 UsageHint::Background) {
445 usage = UsageHint::Background;
John Reck8f45d4a2018-08-15 10:17:12 -0700446 }
Chris Craikb565df12015-10-05 13:00:52 -0700447 }
John Reck3b4510cd2018-09-27 17:39:45 -0700448 if (children.size() > 1) {
449 // Crude overlap check
450 SkRect drawn = SkRect::MakeEmpty();
451 for (auto iter = children.rbegin(); iter != children.rend(); ++iter) {
John Reckbe671952021-01-13 22:39:32 -0500452 const auto& child = *iter;
John Reck3b4510cd2018-09-27 17:39:45 -0700453 // We use stagingProperties here because we haven't yet sync'd the children
454 SkRect bounds = SkRect::MakeXYWH(child->stagingProperties().getX(), child->stagingProperties().getY(),
455 child->stagingProperties().getWidth(), child->stagingProperties().getHeight());
456 if (bounds.contains(drawn)) {
457 // This contains everything drawn after it, so make it a background
458 child->setUsageHint(UsageHint::Background);
459 }
460 drawn.join(bounds);
461 }
462 }
Tyler Freemane0faa692023-11-16 00:48:54 +0000463
464 if (usage == UsageHint::Container) {
465 mDisplayList.applyColorTransform(ColorTransform::Invert);
466 } else {
467 mDisplayList.applyColorTransform(usage == UsageHint::Background ? ColorTransform::Dark
468 : ColorTransform::Light);
469 }
Chris Craikb565df12015-10-05 13:00:52 -0700470}
471
John Reck2de950d2017-01-25 10:58:30 -0800472void RenderNode::pushStagingDisplayListChanges(TreeObserver& observer, TreeInfo& info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700473 if (mNeedsDisplayListSync) {
474 mNeedsDisplayListSync = false;
John Reck5c9d7172014-10-22 11:32:27 -0700475 // Damage with the old display list first then the new one to catch any
476 // changes in isRenderable or, in the future, bounds
477 damageSelf(info);
John Reck2de950d2017-01-25 10:58:30 -0800478 syncDisplayList(observer, &info);
John Recke4267ea2014-06-03 15:53:15 -0700479 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700480 }
John Reck8de65a82014-04-09 15:23:38 -0700481}
482
John Reck2de950d2017-01-25 10:58:30 -0800483void RenderNode::deleteDisplayList(TreeObserver& observer, TreeInfo* info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700484 if (mDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500485 mDisplayList.updateChildren(
John Reck1bcacfd2017-11-03 10:12:19 -0700486 [&observer, info](RenderNode* child) { child->decParentRefCount(observer, info); });
John Reckbe671952021-01-13 22:39:32 -0500487 mDisplayList.clear(this);
John Reckdcba6722014-07-08 13:59:49 -0700488 }
John Reckdcba6722014-07-08 13:59:49 -0700489}
490
John Reck2de950d2017-01-25 10:58:30 -0800491void RenderNode::destroyHardwareResources(TreeInfo* info) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400492 if (hasLayer()) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400493 this->setLayerSurface(nullptr);
John Reckdcba6722014-07-08 13:59:49 -0700494 }
John Reckbe671952021-01-13 22:39:32 -0500495 discardStagingDisplayList();
John Reck3afd6372017-01-30 10:15:48 -0800496
497 ImmediateRemoved observer(info);
498 deleteDisplayList(observer, info);
John Reck2de950d2017-01-25 10:58:30 -0800499}
500
501void RenderNode::destroyLayers() {
502 if (hasLayer()) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400503 this->setLayerSurface(nullptr);
John Reck2de950d2017-01-25 10:58:30 -0800504 }
Nader Jawadb502ad72021-06-17 14:10:04 -0700505
John Reck2de950d2017-01-25 10:58:30 -0800506 if (mDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500507 mDisplayList.updateChildren([](RenderNode* child) { child->destroyLayers(); });
John Reck2de950d2017-01-25 10:58:30 -0800508 }
509}
510
511void RenderNode::decParentRefCount(TreeObserver& observer, TreeInfo* info) {
512 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
513 mParentCount--;
514 if (!mParentCount) {
515 observer.onMaybeRemovedFromTree(this);
516 if (CC_UNLIKELY(mPositionListener.get())) {
517 mPositionListener->onPositionLost(*this, info);
John Reckdcba6722014-07-08 13:59:49 -0700518 }
519 }
520}
521
John Reck2de950d2017-01-25 10:58:30 -0800522void RenderNode::onRemovedFromTree(TreeInfo* info) {
Huihong Luoec68b7c2021-06-25 21:54:16 -0700523 if (Properties::enableWebViewOverlays && mDisplayList) {
524 mDisplayList.onRemovedFromTree();
525 }
John Reck2de950d2017-01-25 10:58:30 -0800526 destroyHardwareResources(info);
527}
528
529void RenderNode::clearRoot() {
530 ImmediateRemoved observer(nullptr);
531 decParentRefCount(observer);
John Reckdcba6722014-07-08 13:59:49 -0700532}
533
John Reck113e0822014-03-18 09:22:59 -0700534/**
535 * Apply property-based transformations to input matrix
536 *
537 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
538 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
539 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700540void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700541 if (properties().getLeft() != 0 || properties().getTop() != 0) {
542 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700543 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700544 if (properties().getStaticMatrix()) {
545 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700546 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700547 } else if (properties().getAnimationMatrix()) {
548 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700549 matrix.multiply(anim);
550 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700551
Chris Craikcc39e162014-04-25 18:34:11 -0700552 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700553 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700554 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700555 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
John Reck1bcacfd2017-11-03 10:12:19 -0700556 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700557 } else {
558 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700559 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700560 } else {
561 mat4 true3dMat;
John Reck1bcacfd2017-11-03 10:12:19 -0700562 true3dMat.loadTranslate(properties().getPivotX() + properties().getTranslationX(),
563 properties().getPivotY() + properties().getTranslationY(),
564 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700565 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
566 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
567 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
568 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
569 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700570
571 matrix.multiply(true3dMat);
572 }
573 }
574 }
John Reck8ed00dc2021-05-10 13:09:27 -0400575
Nader Jawad93d6e242021-05-17 18:12:29 -0700576 if (Properties::getStretchEffectBehavior() == StretchEffectBehavior::UniformScale) {
John Reck8ed00dc2021-05-10 13:09:27 -0400577 const StretchEffect& stretch = properties().layerProperties().getStretchEffect();
578 if (!stretch.isEmpty()) {
579 matrix.multiply(
580 stretch.makeLinearStretch(properties().getWidth(), properties().getHeight()));
581 }
582 }
John Reck113e0822014-03-18 09:22:59 -0700583}
584
Derek Sollenberger579317d42017-08-29 16:33:49 -0400585const SkPath* RenderNode::getClippedOutline(const SkRect& clipRect) const {
586 const SkPath* outlinePath = properties().getOutline().getPath();
587 const uint32_t outlineID = outlinePath->getGenerationID();
588
589 if (outlineID != mClippedOutlineCache.outlineID || clipRect != mClippedOutlineCache.clipRect) {
590 // update the cache keys
591 mClippedOutlineCache.outlineID = outlineID;
592 mClippedOutlineCache.clipRect = clipRect;
593
594 // update the cache value by recomputing a new path
595 SkPath clipPath;
596 clipPath.addRect(clipRect);
597 Op(*outlinePath, clipPath, kIntersect_SkPathOp, &mClippedOutlineCache.clippedOutline);
Derek Sollenberger579317d42017-08-29 16:33:49 -0400598 }
599 return &mClippedOutlineCache.clippedOutline;
600}
601
John Reckf96b2842018-11-29 09:44:10 -0800602using StringBuffer = FatVector<char, 128>;
603
604template <typename... T>
John Reck7af5b2c2018-12-05 13:36:20 -0800605// TODO:__printflike(2, 3)
606// Doesn't work because the warning doesn't understand string_view and doesn't like that
607// it's not a C-style variadic function.
John Reckf96b2842018-11-29 09:44:10 -0800608static void format(StringBuffer& buffer, const std::string_view& format, T... args) {
609 buffer.resize(buffer.capacity());
610 while (1) {
611 int needed = snprintf(buffer.data(), buffer.size(),
612 format.data(), std::forward<T>(args)...);
613 if (needed < 0) {
614 buffer[0] = '\0';
615 buffer.resize(1);
616 return;
617 }
618 if (needed < buffer.size()) {
619 buffer.resize(needed + 1);
620 return;
621 }
John Reck7af5b2c2018-12-05 13:36:20 -0800622 // If we're doing a heap alloc anyway might as well give it some slop
623 buffer.resize(needed + 100);
John Reckf96b2842018-11-29 09:44:10 -0800624 }
625}
626
627void RenderNode::markDrawStart(SkCanvas& canvas) {
628 StringBuffer buffer;
John Reck7af5b2c2018-12-05 13:36:20 -0800629 format(buffer, "RenderNode(id=%" PRId64 ", name='%s')", uniqueId(), getName());
John Reckf96b2842018-11-29 09:44:10 -0800630 canvas.drawAnnotation(SkRect::MakeWH(getWidth(), getHeight()), buffer.data(), nullptr);
631}
632
633void RenderNode::markDrawEnd(SkCanvas& canvas) {
634 StringBuffer buffer;
John Reck7af5b2c2018-12-05 13:36:20 -0800635 format(buffer, "/RenderNode(id=%" PRId64 ", name='%s')", uniqueId(), getName());
John Reckf96b2842018-11-29 09:44:10 -0800636 canvas.drawAnnotation(SkRect::MakeWH(getWidth(), getHeight()), buffer.data(), nullptr);
637}
638
John Reck113e0822014-03-18 09:22:59 -0700639} /* namespace uirenderer */
640} /* namespace android */