blob: eee93c153b9a2fc00ee41e7cace240a2fb833557 [file] [log] [blame]
John Reck113e0822014-03-18 09:22:59 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
John Reck113e0822014-03-18 09:22:59 -070017#include "RenderNode.h"
18
John Recke4267ea2014-06-03 15:53:15 -070019#include "DamageAccumulator.h"
John Reck113e0822014-03-18 09:22:59 -070020#include "Debug.h"
Huihong Luoec68b7c2021-06-25 21:54:16 -070021#include "Properties.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040022#include "TreeInfo.h"
John Reck1bcacfd2017-11-03 10:12:19 -070023#include "VectorDrawable.h"
Derek Sollenberger5d0ca632019-07-19 16:17:12 -040024#include "private/hwui/WebViewFunctor.h"
Fedor Kudasov86bd2142019-06-18 15:51:57 +010025#ifdef __ANDROID__
John Reck1bcacfd2017-11-03 10:12:19 -070026#include "renderthread/CanvasContext.h"
Fedor Kudasov86bd2142019-06-18 15:51:57 +010027#else
28#include "DamageAccumulator.h"
29#include "pipeline/skia/SkiaDisplayList.h"
30#endif
rnleece9762b2021-05-21 15:40:53 -070031#include <gui/TraceUtils.h>
Chris Craike0bb87d2014-04-22 17:55:41 -070032#include "utils/MathUtils.h"
sergeyvc3849aa2016-08-08 13:22:06 -070033#include "utils/StringUtils.h"
John Reck113e0822014-03-18 09:22:59 -070034
Derek Sollenberger579317d42017-08-29 16:33:49 -040035#include <SkPathOps.h>
John Reck77c40102015-10-26 15:49:47 -070036#include <algorithm>
John Reckf96b2842018-11-29 09:44:10 -080037#include <atomic>
John Reck77c40102015-10-26 15:49:47 -070038#include <sstream>
39#include <string>
Jagadeesh Pakaravoorb624af32020-05-01 00:01:40 +000040#include <ui/FatVector.h>
John Reck77c40102015-10-26 15:49:47 -070041
John Reck113e0822014-03-18 09:22:59 -070042namespace android {
43namespace uirenderer {
44
John Reck2de950d2017-01-25 10:58:30 -080045// Used for tree mutations that are purely destructive.
46// Generic tree mutations should use MarkAndSweepObserver instead
47class ImmediateRemoved : public TreeObserver {
48public:
49 explicit ImmediateRemoved(TreeInfo* info) : mTreeInfo(info) {}
50
John Reck1bcacfd2017-11-03 10:12:19 -070051 void onMaybeRemovedFromTree(RenderNode* node) override { node->onRemovedFromTree(mTreeInfo); }
John Reck2de950d2017-01-25 10:58:30 -080052
53private:
54 TreeInfo* mTreeInfo;
55};
56
John Reckf96b2842018-11-29 09:44:10 -080057static int64_t generateId() {
58 static std::atomic<int64_t> sNextId{1};
59 return sNextId++;
60}
61
John Reck8de65a82014-04-09 15:23:38 -070062RenderNode::RenderNode()
John Reckf96b2842018-11-29 09:44:10 -080063 : mUniqueId(generateId())
64 , mDirtyPropertyFields(0)
Chris Craik003cc3d2015-10-16 10:24:55 -070065 , mNeedsDisplayListSync(false)
66 , mDisplayList(nullptr)
67 , mStagingDisplayList(nullptr)
John Reck68bfe0a2014-06-24 15:34:58 -070068 , mAnimatorManager(*this)
John Reck1bcacfd2017-11-03 10:12:19 -070069 , mParentCount(0) {}
John Reck113e0822014-03-18 09:22:59 -070070
71RenderNode::~RenderNode() {
John Reck2de950d2017-01-25 10:58:30 -080072 ImmediateRemoved observer(nullptr);
73 deleteDisplayList(observer);
Derek Sollenberger0df62092016-09-27 16:04:42 -040074 LOG_ALWAYS_FATAL_IF(hasLayer(), "layer missed detachment!");
John Reck113e0822014-03-18 09:22:59 -070075}
76
John Reckbe671952021-01-13 22:39:32 -050077void RenderNode::setStagingDisplayList(DisplayList&& newData) {
78 mValid = newData.isValid();
Chris Craik003cc3d2015-10-16 10:24:55 -070079 mNeedsDisplayListSync = true;
John Reckbe671952021-01-13 22:39:32 -050080 mStagingDisplayList = std::move(newData);
John Reck113e0822014-03-18 09:22:59 -070081}
82
John Reckdc1a6962021-01-12 13:56:07 -050083void RenderNode::discardStagingDisplayList() {
John Reckbe671952021-01-13 22:39:32 -050084 setStagingDisplayList(DisplayList());
John Reckdc1a6962021-01-12 13:56:07 -050085}
86
John Reck113e0822014-03-18 09:22:59 -070087/**
88 * This function is a simplified version of replay(), where we simply retrieve and log the
89 * display list. This function should remain in sync with the replay() function.
90 */
sergeyvc3849aa2016-08-08 13:22:06 -070091void RenderNode::output() {
92 LogcatStream strout;
93 strout << "Root";
94 output(strout, 0);
95}
96
97void RenderNode::output(std::ostream& output, uint32_t level) {
98 output << " (" << getName() << " " << this
John Reck1bcacfd2017-11-03 10:12:19 -070099 << (MathUtils::isZero(properties().getAlpha()) ? ", zero alpha" : "")
100 << (properties().hasShadow() ? ", casting shadow" : "")
101 << (isRenderable() ? "" : ", empty")
102 << (properties().getProjectBackwards() ? ", projected" : "")
103 << (hasLayer() ? ", on HW Layer" : "") << ")" << std::endl;
sergeyvc3849aa2016-08-08 13:22:06 -0700104
105 properties().debugOutputProperties(output, level + 1);
Chris Craik91eff222016-02-22 13:39:33 -0800106
John Reckbe671952021-01-13 22:39:32 -0500107 mDisplayList.output(output, level);
sergeyvc3849aa2016-08-08 13:22:06 -0700108 output << std::string(level * 2, ' ') << "/RenderNode(" << getName() << " " << this << ")";
109 output << std::endl;
Chris Craik91eff222016-02-22 13:39:33 -0800110}
John Reck113e0822014-03-18 09:22:59 -0700111
John Reck07f3d912023-08-17 12:46:44 -0400112void RenderNode::visit(std::function<void(const RenderNode&)> func) const {
113 func(*this);
114 if (mDisplayList) {
115 mDisplayList.visit(func);
116 }
117}
118
John Reck183e1382019-10-09 13:41:18 -0700119int RenderNode::getUsageSize() {
John Reckfe5e7b72014-05-23 17:42:28 -0700120 int size = sizeof(RenderNode);
John Reckbe671952021-01-13 22:39:32 -0500121 size += mStagingDisplayList.getUsedSize();
122 size += mDisplayList.getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700123 return size;
124}
125
John Reck183e1382019-10-09 13:41:18 -0700126int RenderNode::getAllocatedSize() {
127 int size = sizeof(RenderNode);
John Reckbe671952021-01-13 22:39:32 -0500128 size += mStagingDisplayList.getAllocatedSize();
129 size += mDisplayList.getAllocatedSize();
John Reck183e1382019-10-09 13:41:18 -0700130 return size;
131}
132
133
John Reckf4198b72014-04-09 17:00:04 -0700134void RenderNode::prepareTree(TreeInfo& info) {
135 ATRACE_CALL();
Chris Craik69e5adf2014-08-14 13:34:01 -0700136 LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
John Reck2de950d2017-01-25 10:58:30 -0800137 MarkAndSweepRemoved observer(&info);
John Reckf4198b72014-04-09 17:00:04 -0700138
John Reck1423e132018-09-21 14:30:19 -0700139 const int before = info.disableForceDark;
John Reck1072fff2018-04-12 15:20:09 -0700140 prepareTreeImpl(observer, info, false);
John Reck1423e132018-09-21 14:30:19 -0700141 LOG_ALWAYS_FATAL_IF(before != info.disableForceDark, "Mis-matched force dark");
John Reckf4198b72014-04-09 17:00:04 -0700142}
143
John Reck68bfe0a2014-06-24 15:34:58 -0700144void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
145 mAnimatorManager.addAnimator(animator);
146}
147
Doris Liu8b083202016-02-19 21:46:06 +0000148void RenderNode::removeAnimator(const sp<BaseRenderNodeAnimator>& animator) {
149 mAnimatorManager.removeAnimator(animator);
150}
151
John Recke4267ea2014-06-03 15:53:15 -0700152void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700153 if (isRenderable()) {
John Reckf1aa7909e2019-03-07 17:01:08 -0800154 mDamageGenerationId = info.damageGenerationId;
John Reck293e8682014-06-17 10:34:02 -0700155 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700156 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
157 } else {
158 // Hope this is big enough?
159 // TODO: Get this from the display list ops or something
John Reckc1288232015-08-12 13:39:11 -0700160 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
John Recka447d292014-06-11 18:39:44 -0700161 }
Ady Abrahamcab4afe2023-05-09 11:25:22 -0700162 if (!mIsTextureView) {
163 info.out.solelyTextureViewUpdates = false;
164 }
John Recke4267ea2014-06-03 15:53:15 -0700165 }
166}
167
John Recka7c2ea22014-08-08 13:21:00 -0700168void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700169 LayerType layerType = properties().effectiveLayerType();
Chris Craik182952f2015-03-09 14:17:29 -0700170 if (CC_UNLIKELY(layerType == LayerType::RenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700171 // Damage applied so far needs to affect our parent, but does not require
172 // the layer to be updated. So we pop/push here to clear out the current
173 // damage and get a clean state for display list or children updates to
174 // affect, which will require the layer to be updated
175 info.damageAccumulator->popTransform();
176 info.damageAccumulator->pushTransform(this);
177 if (dirtyMask & DISPLAY_LIST) {
178 damageSelf(info);
179 }
John Reck25fbb3f2014-06-12 13:46:45 -0700180 }
181}
182
183void RenderNode::pushLayerUpdate(TreeInfo& info) {
Fedor Kudasov86bd2142019-06-18 15:51:57 +0100184#ifdef __ANDROID__ // Layoutlib does not support CanvasContext and Layers
Chris Craik856f0cc2015-04-21 15:13:29 -0700185 LayerType layerType = properties().effectiveLayerType();
John Reck25fbb3f2014-06-12 13:46:45 -0700186 // If we are not a layer OR we cannot be rendered (eg, view was detached)
187 // we need to destroy any Layers we may have had previously
John Reck1bcacfd2017-11-03 10:12:19 -0700188 if (CC_LIKELY(layerType != LayerType::RenderLayer) || CC_UNLIKELY(!isRenderable()) ||
189 CC_UNLIKELY(properties().getWidth() == 0) || CC_UNLIKELY(properties().getHeight() == 0) ||
190 CC_UNLIKELY(!properties().fitsOnLayer())) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400191 if (CC_UNLIKELY(hasLayer())) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400192 this->setLayerSurface(nullptr);
John Reck25fbb3f2014-06-12 13:46:45 -0700193 }
194 return;
195 }
196
Stan Iliev216b1572018-03-26 14:29:50 -0400197 if (info.canvasContext.createOrUpdateLayer(this, *info.damageAccumulator, info.errorHandler)) {
Chris Craik9fded232015-11-11 16:42:34 -0800198 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700199 }
200
Derek Sollenberger0df62092016-09-27 16:04:42 -0400201 if (!hasLayer()) {
John Reckc25e5062014-06-18 14:21:29 -0700202 return;
203 }
204
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400205 SkRect dirty;
206 info.damageAccumulator->peekAtDirty(&dirty);
Chris Craik0b7e8242015-10-28 16:50:44 -0700207 info.layerUpdateQueue->enqueueLayerWithDamage(this, dirty);
Nader Jawad197743f2021-04-19 19:45:13 -0700208 if (!dirty.isEmpty()) {
209 mStretchMask.markDirty();
210 }
John Reck998a6d82014-08-28 15:35:53 -0700211
Chris Craike2e53a72015-10-28 15:55:40 -0700212 // There might be prefetched layers that need to be accounted for.
213 // That might be us, so tell CanvasContext that this layer is in the
214 // tree and should not be destroyed.
215 info.canvasContext.markLayerInUse(this);
Fedor Kudasov86bd2142019-06-18 15:51:57 +0100216#endif
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) {
John Reckf1aa7909e2019-03-07 17:01:08 -0800228 if (mDamageGenerationId == info.damageGenerationId) {
229 // 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
373 mSnapshotResult.snapshot =
374 snapshot->makeWithFilter(context,
375 imageFilter,
376 subset,
377 clipBounds,
378 &mSnapshotResult.outSubset,
379 &mSnapshotResult.outOffset);
380 mTargetImageFilter = sk_ref_sp(imageFilter);
381 mImageFilterClipBounds = clipBounds;
Nader Jawadf49068f2021-09-08 22:17:15 -0700382 mTargetImageFilterLayerSurfaceGenerationId = layerSurfaceGenerationId;
Nader Jawad6aff4812021-06-09 10:14:43 -0700383 }
384
385 return mSnapshotResult;
386}
387
John Reck2de950d2017-01-25 10:58:30 -0800388void RenderNode::syncDisplayList(TreeObserver& observer, TreeInfo* info) {
Chris Craikb565df12015-10-05 13:00:52 -0700389 // Make sure we inc first so that we don't fluctuate between 0 and 1,
390 // which would thrash the layer cache
Chris Craik003cc3d2015-10-16 10:24:55 -0700391 if (mStagingDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500392 mStagingDisplayList.updateChildren([](RenderNode* child) { child->incParentRefCount(); });
Chris Craikb565df12015-10-05 13:00:52 -0700393 }
John Reck2de950d2017-01-25 10:58:30 -0800394 deleteDisplayList(observer, info);
John Reckbe671952021-01-13 22:39:32 -0500395 mDisplayList = std::move(mStagingDisplayList);
Chris Craik003cc3d2015-10-16 10:24:55 -0700396 if (mDisplayList) {
John Reck283bb462018-12-13 16:40:14 -0800397 WebViewSyncData syncData {
398 .applyForceDark = info && !info->disableForceDark
399 };
John Reckbe671952021-01-13 22:39:32 -0500400 mDisplayList.syncContents(syncData);
John Reck3b4510cd2018-09-27 17:39:45 -0700401 handleForceDark(info);
402 }
403}
John Reckf3c724f2018-09-20 13:00:04 -0700404
John Reck3b4510cd2018-09-27 17:39:45 -0700405void RenderNode::handleForceDark(android::uirenderer::TreeInfo *info) {
406 if (CC_LIKELY(!info || info->disableForceDark)) {
407 return;
408 }
409 auto usage = usageHint();
John Reckbe671952021-01-13 22:39:32 -0500410 FatVector<RenderNode*, 6> children;
411 mDisplayList.updateChildren([&children](RenderNode* node) {
412 children.push_back(node);
413 });
414 if (mDisplayList.hasText()) {
John Reck3b4510cd2018-09-27 17:39:45 -0700415 usage = UsageHint::Foreground;
416 }
417 if (usage == UsageHint::Unknown) {
418 if (children.size() > 1) {
419 usage = UsageHint::Background;
420 } else if (children.size() == 1 &&
John Reckbe671952021-01-13 22:39:32 -0500421 children.front()->usageHint() !=
John Reck3b4510cd2018-09-27 17:39:45 -0700422 UsageHint::Background) {
423 usage = UsageHint::Background;
John Reck8f45d4a2018-08-15 10:17:12 -0700424 }
Chris Craikb565df12015-10-05 13:00:52 -0700425 }
John Reck3b4510cd2018-09-27 17:39:45 -0700426 if (children.size() > 1) {
427 // Crude overlap check
428 SkRect drawn = SkRect::MakeEmpty();
429 for (auto iter = children.rbegin(); iter != children.rend(); ++iter) {
John Reckbe671952021-01-13 22:39:32 -0500430 const auto& child = *iter;
John Reck3b4510cd2018-09-27 17:39:45 -0700431 // We use stagingProperties here because we haven't yet sync'd the children
432 SkRect bounds = SkRect::MakeXYWH(child->stagingProperties().getX(), child->stagingProperties().getY(),
433 child->stagingProperties().getWidth(), child->stagingProperties().getHeight());
434 if (bounds.contains(drawn)) {
435 // This contains everything drawn after it, so make it a background
436 child->setUsageHint(UsageHint::Background);
437 }
438 drawn.join(bounds);
439 }
440 }
John Reckbe671952021-01-13 22:39:32 -0500441 mDisplayList.applyColorTransform(
John Reck3b4510cd2018-09-27 17:39:45 -0700442 usage == UsageHint::Background ? ColorTransform::Dark : ColorTransform::Light);
Chris Craikb565df12015-10-05 13:00:52 -0700443}
444
John Reck2de950d2017-01-25 10:58:30 -0800445void RenderNode::pushStagingDisplayListChanges(TreeObserver& observer, TreeInfo& info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700446 if (mNeedsDisplayListSync) {
447 mNeedsDisplayListSync = false;
John Reck5c9d7172014-10-22 11:32:27 -0700448 // Damage with the old display list first then the new one to catch any
449 // changes in isRenderable or, in the future, bounds
450 damageSelf(info);
John Reck2de950d2017-01-25 10:58:30 -0800451 syncDisplayList(observer, &info);
John Recke4267ea2014-06-03 15:53:15 -0700452 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700453 }
John Reck8de65a82014-04-09 15:23:38 -0700454}
455
John Reck2de950d2017-01-25 10:58:30 -0800456void RenderNode::deleteDisplayList(TreeObserver& observer, TreeInfo* info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700457 if (mDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500458 mDisplayList.updateChildren(
John Reck1bcacfd2017-11-03 10:12:19 -0700459 [&observer, info](RenderNode* child) { child->decParentRefCount(observer, info); });
John Reckbe671952021-01-13 22:39:32 -0500460 mDisplayList.clear(this);
John Reckdcba6722014-07-08 13:59:49 -0700461 }
John Reckdcba6722014-07-08 13:59:49 -0700462}
463
John Reck2de950d2017-01-25 10:58:30 -0800464void RenderNode::destroyHardwareResources(TreeInfo* info) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400465 if (hasLayer()) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400466 this->setLayerSurface(nullptr);
John Reckdcba6722014-07-08 13:59:49 -0700467 }
John Reckbe671952021-01-13 22:39:32 -0500468 discardStagingDisplayList();
John Reck3afd6372017-01-30 10:15:48 -0800469
470 ImmediateRemoved observer(info);
471 deleteDisplayList(observer, info);
John Reck2de950d2017-01-25 10:58:30 -0800472}
473
474void RenderNode::destroyLayers() {
475 if (hasLayer()) {
Derek Sollenberger28a4d992018-09-20 13:37:24 -0400476 this->setLayerSurface(nullptr);
John Reck2de950d2017-01-25 10:58:30 -0800477 }
Nader Jawadb502ad72021-06-17 14:10:04 -0700478
John Reck2de950d2017-01-25 10:58:30 -0800479 if (mDisplayList) {
John Reckbe671952021-01-13 22:39:32 -0500480 mDisplayList.updateChildren([](RenderNode* child) { child->destroyLayers(); });
John Reck2de950d2017-01-25 10:58:30 -0800481 }
482}
483
484void RenderNode::decParentRefCount(TreeObserver& observer, TreeInfo* info) {
485 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
486 mParentCount--;
487 if (!mParentCount) {
488 observer.onMaybeRemovedFromTree(this);
489 if (CC_UNLIKELY(mPositionListener.get())) {
490 mPositionListener->onPositionLost(*this, info);
John Reckdcba6722014-07-08 13:59:49 -0700491 }
492 }
493}
494
John Reck2de950d2017-01-25 10:58:30 -0800495void RenderNode::onRemovedFromTree(TreeInfo* info) {
Huihong Luoec68b7c2021-06-25 21:54:16 -0700496 if (Properties::enableWebViewOverlays && mDisplayList) {
497 mDisplayList.onRemovedFromTree();
498 }
John Reck2de950d2017-01-25 10:58:30 -0800499 destroyHardwareResources(info);
500}
501
502void RenderNode::clearRoot() {
503 ImmediateRemoved observer(nullptr);
504 decParentRefCount(observer);
John Reckdcba6722014-07-08 13:59:49 -0700505}
506
John Reck113e0822014-03-18 09:22:59 -0700507/**
508 * Apply property-based transformations to input matrix
509 *
510 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
511 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
512 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700513void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700514 if (properties().getLeft() != 0 || properties().getTop() != 0) {
515 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700516 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700517 if (properties().getStaticMatrix()) {
518 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700519 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700520 } else if (properties().getAnimationMatrix()) {
521 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700522 matrix.multiply(anim);
523 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700524
Chris Craikcc39e162014-04-25 18:34:11 -0700525 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700526 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700527 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700528 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
John Reck1bcacfd2017-11-03 10:12:19 -0700529 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700530 } else {
531 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700532 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700533 } else {
534 mat4 true3dMat;
John Reck1bcacfd2017-11-03 10:12:19 -0700535 true3dMat.loadTranslate(properties().getPivotX() + properties().getTranslationX(),
536 properties().getPivotY() + properties().getTranslationY(),
537 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700538 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
539 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
540 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
541 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
542 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700543
544 matrix.multiply(true3dMat);
545 }
546 }
547 }
John Reck8ed00dc2021-05-10 13:09:27 -0400548
Nader Jawad93d6e242021-05-17 18:12:29 -0700549 if (Properties::getStretchEffectBehavior() == StretchEffectBehavior::UniformScale) {
John Reck8ed00dc2021-05-10 13:09:27 -0400550 const StretchEffect& stretch = properties().layerProperties().getStretchEffect();
551 if (!stretch.isEmpty()) {
552 matrix.multiply(
553 stretch.makeLinearStretch(properties().getWidth(), properties().getHeight()));
554 }
555 }
John Reck113e0822014-03-18 09:22:59 -0700556}
557
Derek Sollenberger579317d42017-08-29 16:33:49 -0400558const SkPath* RenderNode::getClippedOutline(const SkRect& clipRect) const {
559 const SkPath* outlinePath = properties().getOutline().getPath();
560 const uint32_t outlineID = outlinePath->getGenerationID();
561
562 if (outlineID != mClippedOutlineCache.outlineID || clipRect != mClippedOutlineCache.clipRect) {
563 // update the cache keys
564 mClippedOutlineCache.outlineID = outlineID;
565 mClippedOutlineCache.clipRect = clipRect;
566
567 // update the cache value by recomputing a new path
568 SkPath clipPath;
569 clipPath.addRect(clipRect);
570 Op(*outlinePath, clipPath, kIntersect_SkPathOp, &mClippedOutlineCache.clippedOutline);
Derek Sollenberger579317d42017-08-29 16:33:49 -0400571 }
572 return &mClippedOutlineCache.clippedOutline;
573}
574
John Reckf96b2842018-11-29 09:44:10 -0800575using StringBuffer = FatVector<char, 128>;
576
577template <typename... T>
John Reck7af5b2c2018-12-05 13:36:20 -0800578// TODO:__printflike(2, 3)
579// Doesn't work because the warning doesn't understand string_view and doesn't like that
580// it's not a C-style variadic function.
John Reckf96b2842018-11-29 09:44:10 -0800581static void format(StringBuffer& buffer, const std::string_view& format, T... args) {
582 buffer.resize(buffer.capacity());
583 while (1) {
584 int needed = snprintf(buffer.data(), buffer.size(),
585 format.data(), std::forward<T>(args)...);
586 if (needed < 0) {
587 buffer[0] = '\0';
588 buffer.resize(1);
589 return;
590 }
591 if (needed < buffer.size()) {
592 buffer.resize(needed + 1);
593 return;
594 }
John Reck7af5b2c2018-12-05 13:36:20 -0800595 // If we're doing a heap alloc anyway might as well give it some slop
596 buffer.resize(needed + 100);
John Reckf96b2842018-11-29 09:44:10 -0800597 }
598}
599
600void RenderNode::markDrawStart(SkCanvas& canvas) {
601 StringBuffer buffer;
John Reck7af5b2c2018-12-05 13:36:20 -0800602 format(buffer, "RenderNode(id=%" PRId64 ", name='%s')", uniqueId(), getName());
John Reckf96b2842018-11-29 09:44:10 -0800603 canvas.drawAnnotation(SkRect::MakeWH(getWidth(), getHeight()), buffer.data(), nullptr);
604}
605
606void RenderNode::markDrawEnd(SkCanvas& canvas) {
607 StringBuffer buffer;
John Reck7af5b2c2018-12-05 13:36:20 -0800608 format(buffer, "/RenderNode(id=%" PRId64 ", name='%s')", uniqueId(), getName());
John Reckf96b2842018-11-29 09:44:10 -0800609 canvas.drawAnnotation(SkRect::MakeWH(getWidth(), getHeight()), buffer.data(), nullptr);
610}
611
John Reck113e0822014-03-18 09:22:59 -0700612} /* namespace uirenderer */
613} /* namespace android */