blob: d93a7578cfd789c9a15b956fa51aea43fd8d7e79 [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
Chris Craik5e00c7c2016-07-06 16:10:09 -070019#include "BakedOpRenderer.h"
John Recke4267ea2014-06-03 15:53:15 -070020#include "DamageAccumulator.h"
John Reck113e0822014-03-18 09:22:59 -070021#include "Debug.h"
Chris Craik5e00c7c2016-07-06 16:10:09 -070022#include "RecordedOp.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040023#include "TreeInfo.h"
John Reck1bcacfd2017-11-03 10:12:19 -070024#include "VectorDrawable.h"
25#include "renderstate/RenderState.h"
26#include "renderthread/CanvasContext.h"
John Reck2de950d2017-01-25 10:58:30 -080027#include "utils/FatVector.h"
Chris Craike0bb87d2014-04-22 17:55:41 -070028#include "utils/MathUtils.h"
sergeyvc3849aa2016-08-08 13:22:06 -070029#include "utils/StringUtils.h"
Chris Craik70850ea2014-11-18 10:49:23 -080030#include "utils/TraceUtils.h"
John Reck113e0822014-03-18 09:22:59 -070031
John Recke248bd12015-08-05 13:53:53 -070032#include "protos/ProtoHelpers.h"
John Reck1bcacfd2017-11-03 10:12:19 -070033#include "protos/hwui.pb.h"
John Recke248bd12015-08-05 13:53:53 -070034
Derek Sollenberger579317d2017-08-29 16:33:49 -040035#include <SkPathOps.h>
John Reck77c40102015-10-26 15:49:47 -070036#include <algorithm>
37#include <sstream>
38#include <string>
39
John Reck113e0822014-03-18 09:22:59 -070040namespace android {
41namespace uirenderer {
42
John Reck2de950d2017-01-25 10:58:30 -080043// Used for tree mutations that are purely destructive.
44// Generic tree mutations should use MarkAndSweepObserver instead
45class ImmediateRemoved : public TreeObserver {
46public:
47 explicit ImmediateRemoved(TreeInfo* info) : mTreeInfo(info) {}
48
John Reck1bcacfd2017-11-03 10:12:19 -070049 void onMaybeRemovedFromTree(RenderNode* node) override { node->onRemovedFromTree(mTreeInfo); }
John Reck2de950d2017-01-25 10:58:30 -080050
51private:
52 TreeInfo* mTreeInfo;
53};
54
John Reck8de65a82014-04-09 15:23:38 -070055RenderNode::RenderNode()
John Reckff941dc2014-05-14 16:34:14 -070056 : mDirtyPropertyFields(0)
Chris Craik003cc3d2015-10-16 10:24:55 -070057 , mNeedsDisplayListSync(false)
58 , mDisplayList(nullptr)
59 , mStagingDisplayList(nullptr)
John Reck68bfe0a2014-06-24 15:34:58 -070060 , mAnimatorManager(*this)
John Reck1bcacfd2017-11-03 10:12:19 -070061 , mParentCount(0) {}
John Reck113e0822014-03-18 09:22:59 -070062
63RenderNode::~RenderNode() {
John Reck2de950d2017-01-25 10:58:30 -080064 ImmediateRemoved observer(nullptr);
65 deleteDisplayList(observer);
Chris Craik003cc3d2015-10-16 10:24:55 -070066 delete mStagingDisplayList;
Derek Sollenberger0df62092016-09-27 16:04:42 -040067 LOG_ALWAYS_FATAL_IF(hasLayer(), "layer missed detachment!");
John Reck113e0822014-03-18 09:22:59 -070068}
69
John Reck2de950d2017-01-25 10:58:30 -080070void RenderNode::setStagingDisplayList(DisplayList* displayList) {
71 mValid = (displayList != nullptr);
Chris Craik003cc3d2015-10-16 10:24:55 -070072 mNeedsDisplayListSync = true;
73 delete mStagingDisplayList;
74 mStagingDisplayList = displayList;
John Reck113e0822014-03-18 09:22:59 -070075}
76
77/**
78 * This function is a simplified version of replay(), where we simply retrieve and log the
79 * display list. This function should remain in sync with the replay() function.
80 */
sergeyvc3849aa2016-08-08 13:22:06 -070081void RenderNode::output() {
82 LogcatStream strout;
83 strout << "Root";
84 output(strout, 0);
85}
86
87void RenderNode::output(std::ostream& output, uint32_t level) {
88 output << " (" << getName() << " " << this
John Reck1bcacfd2017-11-03 10:12:19 -070089 << (MathUtils::isZero(properties().getAlpha()) ? ", zero alpha" : "")
90 << (properties().hasShadow() ? ", casting shadow" : "")
91 << (isRenderable() ? "" : ", empty")
92 << (properties().getProjectBackwards() ? ", projected" : "")
93 << (hasLayer() ? ", on HW Layer" : "") << ")" << std::endl;
sergeyvc3849aa2016-08-08 13:22:06 -070094
95 properties().debugOutputProperties(output, level + 1);
Chris Craik91eff222016-02-22 13:39:33 -080096
97 if (mDisplayList) {
Stan Ilievd2172372017-02-09 16:59:27 -050098 mDisplayList->output(output, level);
Chris Craik91eff222016-02-22 13:39:33 -080099 }
sergeyvc3849aa2016-08-08 13:22:06 -0700100 output << std::string(level * 2, ' ') << "/RenderNode(" << getName() << " " << this << ")";
101 output << std::endl;
Chris Craik91eff222016-02-22 13:39:33 -0800102}
John Reck113e0822014-03-18 09:22:59 -0700103
John Reck1bcacfd2017-11-03 10:12:19 -0700104void RenderNode::copyTo(proto::RenderNode* pnode) {
105 pnode->set_id(static_cast<uint64_t>(reinterpret_cast<uintptr_t>(this)));
John Recke248bd12015-08-05 13:53:53 -0700106 pnode->set_name(mName.string(), mName.length());
107
108 proto::RenderProperties* pprops = pnode->mutable_properties();
109 pprops->set_left(properties().getLeft());
110 pprops->set_top(properties().getTop());
111 pprops->set_right(properties().getRight());
112 pprops->set_bottom(properties().getBottom());
113 pprops->set_clip_flags(properties().getClippingFlags());
114 pprops->set_alpha(properties().getAlpha());
115 pprops->set_translation_x(properties().getTranslationX());
116 pprops->set_translation_y(properties().getTranslationY());
117 pprops->set_translation_z(properties().getTranslationZ());
118 pprops->set_elevation(properties().getElevation());
119 pprops->set_rotation(properties().getRotation());
120 pprops->set_rotation_x(properties().getRotationX());
121 pprops->set_rotation_y(properties().getRotationY());
122 pprops->set_scale_x(properties().getScaleX());
123 pprops->set_scale_y(properties().getScaleY());
124 pprops->set_pivot_x(properties().getPivotX());
125 pprops->set_pivot_y(properties().getPivotY());
126 pprops->set_has_overlapping_rendering(properties().getHasOverlappingRendering());
127 pprops->set_pivot_explicitly_set(properties().isPivotExplicitlySet());
128 pprops->set_project_backwards(properties().getProjectBackwards());
129 pprops->set_projection_receiver(properties().isProjectionReceiver());
130 set(pprops->mutable_clip_bounds(), properties().getClipBounds());
131
132 const Outline& outline = properties().getOutline();
133 if (outline.getType() != Outline::Type::None) {
134 proto::Outline* poutline = pprops->mutable_outline();
135 poutline->clear_path();
136 if (outline.getType() == Outline::Type::Empty) {
137 poutline->set_type(proto::Outline_Type_Empty);
138 } else if (outline.getType() == Outline::Type::ConvexPath) {
139 poutline->set_type(proto::Outline_Type_ConvexPath);
140 if (const SkPath* path = outline.getPath()) {
141 set(poutline->mutable_path(), *path);
142 }
143 } else if (outline.getType() == Outline::Type::RoundRect) {
144 poutline->set_type(proto::Outline_Type_RoundRect);
145 } else {
146 ALOGW("Uknown outline type! %d", static_cast<int>(outline.getType()));
147 poutline->set_type(proto::Outline_Type_None);
148 }
149 poutline->set_should_clip(outline.getShouldClip());
150 poutline->set_alpha(outline.getAlpha());
151 poutline->set_radius(outline.getRadius());
152 set(poutline->mutable_bounds(), outline.getBounds());
153 } else {
154 pprops->clear_outline();
155 }
156
157 const RevealClip& revealClip = properties().getRevealClip();
158 if (revealClip.willClip()) {
159 proto::RevealClip* prevealClip = pprops->mutable_reveal_clip();
160 prevealClip->set_x(revealClip.getX());
161 prevealClip->set_y(revealClip.getY());
162 prevealClip->set_radius(revealClip.getRadius());
163 } else {
164 pprops->clear_reveal_clip();
165 }
166
167 pnode->clear_children();
Chris Craik003cc3d2015-10-16 10:24:55 -0700168 if (mDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700169 for (auto&& child : mDisplayList->getChildren()) {
Chris Craikb565df12015-10-05 13:00:52 -0700170 child->renderNode->copyTo(pnode->add_children());
John Recke248bd12015-08-05 13:53:53 -0700171 }
172 }
173}
174
John Reckfe5e7b72014-05-23 17:42:28 -0700175int RenderNode::getDebugSize() {
176 int size = sizeof(RenderNode);
Chris Craik003cc3d2015-10-16 10:24:55 -0700177 if (mStagingDisplayList) {
178 size += mStagingDisplayList->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700179 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700180 if (mDisplayList && mDisplayList != mStagingDisplayList) {
181 size += mDisplayList->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700182 }
183 return size;
184}
185
John Reckf4198b72014-04-09 17:00:04 -0700186void RenderNode::prepareTree(TreeInfo& info) {
187 ATRACE_CALL();
Chris Craik69e5adf2014-08-14 13:34:01 -0700188 LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
John Reck2de950d2017-01-25 10:58:30 -0800189 MarkAndSweepRemoved observer(&info);
John Reckf4198b72014-04-09 17:00:04 -0700190
Matt Sarettf58cc922016-11-14 18:33:38 -0500191 // The OpenGL renderer reserves the stencil buffer for overdraw debugging. Functors
192 // will need to be drawn in a layer.
193 bool functorsNeedLayer = Properties::debugOverdraw && !Properties::isSkiaEnabled();
Chris Craika766cb22015-06-08 16:49:43 -0700194
John Reck2de950d2017-01-25 10:58:30 -0800195 prepareTreeImpl(observer, info, functorsNeedLayer);
John Reckf4198b72014-04-09 17:00:04 -0700196}
197
John Reck68bfe0a2014-06-24 15:34:58 -0700198void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
199 mAnimatorManager.addAnimator(animator);
200}
201
Doris Liu8b083202016-02-19 21:46:06 +0000202void RenderNode::removeAnimator(const sp<BaseRenderNodeAnimator>& animator) {
203 mAnimatorManager.removeAnimator(animator);
204}
205
John Recke4267ea2014-06-03 15:53:15 -0700206void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700207 if (isRenderable()) {
John Reck293e8682014-06-17 10:34:02 -0700208 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700209 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
210 } else {
211 // Hope this is big enough?
212 // TODO: Get this from the display list ops or something
John Reckc1288232015-08-12 13:39:11 -0700213 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
John Recka447d292014-06-11 18:39:44 -0700214 }
John Recke4267ea2014-06-03 15:53:15 -0700215 }
216}
217
John Recka7c2ea22014-08-08 13:21:00 -0700218void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700219 LayerType layerType = properties().effectiveLayerType();
Chris Craik182952f2015-03-09 14:17:29 -0700220 if (CC_UNLIKELY(layerType == LayerType::RenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700221 // Damage applied so far needs to affect our parent, but does not require
222 // the layer to be updated. So we pop/push here to clear out the current
223 // damage and get a clean state for display list or children updates to
224 // affect, which will require the layer to be updated
225 info.damageAccumulator->popTransform();
226 info.damageAccumulator->pushTransform(this);
227 if (dirtyMask & DISPLAY_LIST) {
228 damageSelf(info);
229 }
John Reck25fbb3f2014-06-12 13:46:45 -0700230 }
231}
232
233void RenderNode::pushLayerUpdate(TreeInfo& info) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700234 LayerType layerType = properties().effectiveLayerType();
John Reck25fbb3f2014-06-12 13:46:45 -0700235 // If we are not a layer OR we cannot be rendered (eg, view was detached)
236 // we need to destroy any Layers we may have had previously
John Reck1bcacfd2017-11-03 10:12:19 -0700237 if (CC_LIKELY(layerType != LayerType::RenderLayer) || CC_UNLIKELY(!isRenderable()) ||
238 CC_UNLIKELY(properties().getWidth() == 0) || CC_UNLIKELY(properties().getHeight() == 0) ||
239 CC_UNLIKELY(!properties().fitsOnLayer())) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400240 if (CC_UNLIKELY(hasLayer())) {
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400241 renderthread::CanvasContext::destroyLayer(this);
John Reck25fbb3f2014-06-12 13:46:45 -0700242 }
243 return;
244 }
245
Stan Iliev216b1572018-03-26 14:29:50 -0400246 if (info.canvasContext.createOrUpdateLayer(this, *info.damageAccumulator, info.errorHandler)) {
Chris Craik9fded232015-11-11 16:42:34 -0800247 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700248 }
249
Derek Sollenberger0df62092016-09-27 16:04:42 -0400250 if (!hasLayer()) {
John Reckc25e5062014-06-18 14:21:29 -0700251 return;
252 }
253
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400254 SkRect dirty;
255 info.damageAccumulator->peekAtDirty(&dirty);
Chris Craik0b7e8242015-10-28 16:50:44 -0700256 info.layerUpdateQueue->enqueueLayerWithDamage(this, dirty);
John Reck998a6d82014-08-28 15:35:53 -0700257
Chris Craike2e53a72015-10-28 15:55:40 -0700258 // There might be prefetched layers that need to be accounted for.
259 // That might be us, so tell CanvasContext that this layer is in the
260 // tree and should not be destroyed.
261 info.canvasContext.markLayerInUse(this);
John Reck25fbb3f2014-06-12 13:46:45 -0700262}
263
Chris Craika766cb22015-06-08 16:49:43 -0700264/**
265 * Traverse down the the draw tree to prepare for a frame.
266 *
267 * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven
268 *
269 * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the
270 * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer.
271 */
John Reck2de950d2017-01-25 10:58:30 -0800272void RenderNode::prepareTreeImpl(TreeObserver& observer, TreeInfo& info, bool functorsNeedLayer) {
John Recka447d292014-06-11 18:39:44 -0700273 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700274
John Reckdcba6722014-07-08 13:59:49 -0700275 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700276 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700277 }
John Reck9eb9f6f2014-08-21 11:23:05 -0700278 uint32_t animatorDirtyMask = 0;
279 if (CC_LIKELY(info.runAnimations)) {
280 animatorDirtyMask = mAnimatorManager.animate(info);
281 }
Chris Craika766cb22015-06-08 16:49:43 -0700282
John Reck3f725f02015-06-16 10:29:31 -0700283 bool willHaveFunctor = false;
Chris Craik003cc3d2015-10-16 10:24:55 -0700284 if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400285 willHaveFunctor = mStagingDisplayList->hasFunctor();
Chris Craik003cc3d2015-10-16 10:24:55 -0700286 } else if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400287 willHaveFunctor = mDisplayList->hasFunctor();
John Reck3f725f02015-06-16 10:29:31 -0700288 }
John Reck1bcacfd2017-11-03 10:12:19 -0700289 bool childFunctorsNeedLayer =
290 mProperties.prepareForFunctorPresence(willHaveFunctor, functorsNeedLayer);
Chris Craika766cb22015-06-08 16:49:43 -0700291
John Reckf6481082016-02-02 15:18:23 -0800292 if (CC_UNLIKELY(mPositionListener.get())) {
293 mPositionListener->onPositionUpdated(*this, info);
294 }
295
John Recka7c2ea22014-08-08 13:21:00 -0700296 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700297 if (info.mode == TreeInfo::MODE_FULL) {
John Reck2de950d2017-01-25 10:58:30 -0800298 pushStagingDisplayListChanges(observer, info);
John Reck25fbb3f2014-06-12 13:46:45 -0700299 }
John Reck25fbb3f2014-06-12 13:46:45 -0700300
Doris Liu07c056d2016-06-13 12:52:44 -0700301 if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400302 info.out.hasFunctors |= mDisplayList->hasFunctor();
John Reck1bcacfd2017-11-03 10:12:19 -0700303 bool isDirty = mDisplayList->prepareListAndChildren(
304 observer, info, childFunctorsNeedLayer,
305 [](RenderNode* child, TreeObserver& observer, TreeInfo& info,
306 bool functorsNeedLayer) {
307 child->prepareTreeImpl(observer, info, functorsNeedLayer);
308 });
Derek Sollenberger0df62092016-09-27 16:04:42 -0400309 if (isDirty) {
310 damageSelf(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700311 }
Doris Liu718cd3e2016-05-17 16:50:31 -0700312 }
Doris Liub51b2862016-08-01 19:56:47 -0700313 pushLayerUpdate(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700314
John Recka447d292014-06-11 18:39:44 -0700315 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700316}
317
Chris Craikb565df12015-10-05 13:00:52 -0700318void RenderNode::syncProperties() {
319 mProperties = mStagingProperties;
320}
321
John Reck25fbb3f2014-06-12 13:46:45 -0700322void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reckff941dc2014-05-14 16:34:14 -0700323 // Push the animators first so that setupStartValueIfNecessary() is called
324 // before properties() is trampled by stagingProperties(), as they are
325 // required by some animators.
John Reck9eb9f6f2014-08-21 11:23:05 -0700326 if (CC_LIKELY(info.runAnimations)) {
John Reck119907c2014-08-14 09:02:01 -0700327 mAnimatorManager.pushStaging();
John Reck9eb9f6f2014-08-21 11:23:05 -0700328 }
John Reckff941dc2014-05-14 16:34:14 -0700329 if (mDirtyPropertyFields) {
330 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700331 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700332 info.damageAccumulator->popTransform();
Chris Craikb565df12015-10-05 13:00:52 -0700333 syncProperties();
John Recke4267ea2014-06-03 15:53:15 -0700334 // We could try to be clever and only re-damage if the matrix changed.
335 // However, we don't need to worry about that. The cost of over-damaging
336 // here is only going to be a single additional map rect of this node
337 // plus a rect join(). The parent's transform (and up) will only be
338 // performed once.
John Recka447d292014-06-11 18:39:44 -0700339 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700340 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700341 }
John Reck25fbb3f2014-06-12 13:46:45 -0700342}
343
John Reck2de950d2017-01-25 10:58:30 -0800344void RenderNode::syncDisplayList(TreeObserver& observer, TreeInfo* info) {
Chris Craikb565df12015-10-05 13:00:52 -0700345 // Make sure we inc first so that we don't fluctuate between 0 and 1,
346 // which would thrash the layer cache
Chris Craik003cc3d2015-10-16 10:24:55 -0700347 if (mStagingDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -0700348 mStagingDisplayList->updateChildren([](RenderNode* child) { child->incParentRefCount(); });
Chris Craikb565df12015-10-05 13:00:52 -0700349 }
John Reck2de950d2017-01-25 10:58:30 -0800350 deleteDisplayList(observer, info);
Chris Craik003cc3d2015-10-16 10:24:55 -0700351 mDisplayList = mStagingDisplayList;
352 mStagingDisplayList = nullptr;
353 if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400354 mDisplayList->syncContents();
Chris Craikb565df12015-10-05 13:00:52 -0700355 }
356}
357
John Reck2de950d2017-01-25 10:58:30 -0800358void RenderNode::pushStagingDisplayListChanges(TreeObserver& observer, TreeInfo& info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700359 if (mNeedsDisplayListSync) {
360 mNeedsDisplayListSync = false;
John Reck5c9d7172014-10-22 11:32:27 -0700361 // Damage with the old display list first then the new one to catch any
362 // changes in isRenderable or, in the future, bounds
363 damageSelf(info);
John Reck2de950d2017-01-25 10:58:30 -0800364 syncDisplayList(observer, &info);
John Recke4267ea2014-06-03 15:53:15 -0700365 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700366 }
John Reck8de65a82014-04-09 15:23:38 -0700367}
368
John Reck2de950d2017-01-25 10:58:30 -0800369void RenderNode::deleteDisplayList(TreeObserver& observer, TreeInfo* info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700370 if (mDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -0700371 mDisplayList->updateChildren(
372 [&observer, info](RenderNode* child) { child->decParentRefCount(observer, info); });
Derek Sollenberger0df62092016-09-27 16:04:42 -0400373 if (!mDisplayList->reuseDisplayList(this, info ? &info->canvasContext : nullptr)) {
374 delete mDisplayList;
John Reckdcba6722014-07-08 13:59:49 -0700375 }
376 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700377 mDisplayList = nullptr;
John Reckdcba6722014-07-08 13:59:49 -0700378}
379
John Reck2de950d2017-01-25 10:58:30 -0800380void RenderNode::destroyHardwareResources(TreeInfo* info) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400381 if (hasLayer()) {
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400382 renderthread::CanvasContext::destroyLayer(this);
John Reckdcba6722014-07-08 13:59:49 -0700383 }
John Reck3afd6372017-01-30 10:15:48 -0800384 setStagingDisplayList(nullptr);
385
386 ImmediateRemoved observer(info);
387 deleteDisplayList(observer, info);
John Reck2de950d2017-01-25 10:58:30 -0800388}
389
390void RenderNode::destroyLayers() {
391 if (hasLayer()) {
392 renderthread::CanvasContext::destroyLayer(this);
393 }
394 if (mDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -0700395 mDisplayList->updateChildren([](RenderNode* child) { child->destroyLayers(); });
John Reck2de950d2017-01-25 10:58:30 -0800396 }
397}
398
399void RenderNode::decParentRefCount(TreeObserver& observer, TreeInfo* info) {
400 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
401 mParentCount--;
402 if (!mParentCount) {
403 observer.onMaybeRemovedFromTree(this);
404 if (CC_UNLIKELY(mPositionListener.get())) {
405 mPositionListener->onPositionLost(*this, info);
John Reckdcba6722014-07-08 13:59:49 -0700406 }
407 }
408}
409
John Reck2de950d2017-01-25 10:58:30 -0800410void RenderNode::onRemovedFromTree(TreeInfo* info) {
411 destroyHardwareResources(info);
412}
413
414void RenderNode::clearRoot() {
415 ImmediateRemoved observer(nullptr);
416 decParentRefCount(observer);
John Reckdcba6722014-07-08 13:59:49 -0700417}
418
John Reck113e0822014-03-18 09:22:59 -0700419/**
420 * Apply property-based transformations to input matrix
421 *
422 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
423 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
424 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700425void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700426 if (properties().getLeft() != 0 || properties().getTop() != 0) {
427 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700428 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700429 if (properties().getStaticMatrix()) {
430 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700431 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700432 } else if (properties().getAnimationMatrix()) {
433 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700434 matrix.multiply(anim);
435 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700436
Chris Craikcc39e162014-04-25 18:34:11 -0700437 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700438 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700439 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700440 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
John Reck1bcacfd2017-11-03 10:12:19 -0700441 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700442 } else {
443 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700444 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700445 } else {
446 mat4 true3dMat;
John Reck1bcacfd2017-11-03 10:12:19 -0700447 true3dMat.loadTranslate(properties().getPivotX() + properties().getTranslationX(),
448 properties().getPivotY() + properties().getTranslationY(),
449 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700450 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
451 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
452 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
453 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
454 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700455
456 matrix.multiply(true3dMat);
457 }
458 }
459 }
460}
461
462/**
463 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
464 *
465 * This should be called before a call to defer() or drawDisplayList()
466 *
467 * Each DisplayList that serves as a 3d root builds its list of composited children,
468 * which are flagged to not draw in the standard draw loop.
469 */
470void RenderNode::computeOrdering() {
471 ATRACE_CALL();
472 mProjectedNodes.clear();
473
474 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
475 // transform properties are applied correctly to top level children
Chris Craik003cc3d2015-10-16 10:24:55 -0700476 if (mDisplayList == nullptr) return;
Chris Craikb36af872015-10-16 14:23:12 -0700477 for (unsigned int i = 0; i < mDisplayList->getChildren().size(); i++) {
Chris Craik5e00c7c2016-07-06 16:10:09 -0700478 RenderNodeOp* childOp = mDisplayList->getChildren()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700479 childOp->renderNode->computeOrderingImpl(childOp, &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700480 }
481}
482
483void RenderNode::computeOrderingImpl(
John Reck1bcacfd2017-11-03 10:12:19 -0700484 RenderNodeOp* opState, std::vector<RenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700485 const mat4* transformFromProjectionSurface) {
486 mProjectedNodes.clear();
Chris Craik003cc3d2015-10-16 10:24:55 -0700487 if (mDisplayList == nullptr || mDisplayList->isEmpty()) return;
John Reck113e0822014-03-18 09:22:59 -0700488
489 // TODO: should avoid this calculation in most cases
490 // TODO: just calculate single matrix, down to all leaf composited elements
491 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
Chris Craik8d1f2122015-11-24 16:40:09 -0800492 localTransformFromProjectionSurface.multiply(opState->localMatrix);
John Reck113e0822014-03-18 09:22:59 -0700493
John Reckd0a0b2a2014-03-20 16:28:56 -0700494 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700495 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
Chris Craik8d1f2122015-11-24 16:40:09 -0800496 opState->skipInOrderDraw = true;
497 opState->transformFromCompositingAncestor = localTransformFromProjectionSurface;
John Reck272a6852015-07-29 16:48:58 -0700498 compositedChildrenOfProjectionSurface->push_back(opState);
John Reck113e0822014-03-18 09:22:59 -0700499 } else {
500 // standard in order draw
Chris Craik8d1f2122015-11-24 16:40:09 -0800501 opState->skipInOrderDraw = false;
John Reck113e0822014-03-18 09:22:59 -0700502 }
503
Chris Craikb36af872015-10-16 14:23:12 -0700504 if (mDisplayList->getChildren().size() > 0) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700505 const bool isProjectionReceiver = mDisplayList->projectionReceiveIndex >= 0;
John Reck113e0822014-03-18 09:22:59 -0700506 bool haveAppliedPropertiesToProjection = false;
Chris Craikb36af872015-10-16 14:23:12 -0700507 for (unsigned int i = 0; i < mDisplayList->getChildren().size(); i++) {
Chris Craik5e00c7c2016-07-06 16:10:09 -0700508 RenderNodeOp* childOp = mDisplayList->getChildren()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700509 RenderNode* child = childOp->renderNode;
John Reck113e0822014-03-18 09:22:59 -0700510
Chris Craik5e00c7c2016-07-06 16:10:09 -0700511 std::vector<RenderNodeOp*>* projectionChildren = nullptr;
Chris Craikd41c4d82015-01-05 15:51:13 -0800512 const mat4* projectionTransform = nullptr;
John Reckd0a0b2a2014-03-20 16:28:56 -0700513 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
Chris Craikbf72eb82015-06-08 11:30:44 -0700514 // if receiving projections, collect projecting descendant
John Reck113e0822014-03-18 09:22:59 -0700515
Chris Craikbf72eb82015-06-08 11:30:44 -0700516 // Note that if a direct descendant is projecting backwards, we pass its
517 // grandparent projection collection, since it shouldn't project onto its
John Reck113e0822014-03-18 09:22:59 -0700518 // parent, where it will already be drawing.
519 projectionChildren = &mProjectedNodes;
520 projectionTransform = &mat4::identity();
521 } else {
522 if (!haveAppliedPropertiesToProjection) {
523 applyViewPropertyTransforms(localTransformFromProjectionSurface);
524 haveAppliedPropertiesToProjection = true;
525 }
526 projectionChildren = compositedChildrenOfProjectionSurface;
527 projectionTransform = &localTransformFromProjectionSurface;
528 }
Derek Sollenbergerf2932592015-08-13 14:59:33 -0400529 child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700530 }
531 }
John Reck113e0822014-03-18 09:22:59 -0700532}
533
Derek Sollenberger579317d2017-08-29 16:33:49 -0400534const SkPath* RenderNode::getClippedOutline(const SkRect& clipRect) const {
535 const SkPath* outlinePath = properties().getOutline().getPath();
536 const uint32_t outlineID = outlinePath->getGenerationID();
537
538 if (outlineID != mClippedOutlineCache.outlineID || clipRect != mClippedOutlineCache.clipRect) {
539 // update the cache keys
540 mClippedOutlineCache.outlineID = outlineID;
541 mClippedOutlineCache.clipRect = clipRect;
542
543 // update the cache value by recomputing a new path
544 SkPath clipPath;
545 clipPath.addRect(clipRect);
546 Op(*outlinePath, clipPath, kIntersect_SkPathOp, &mClippedOutlineCache.clippedOutline);
Derek Sollenberger579317d2017-08-29 16:33:49 -0400547 }
548 return &mClippedOutlineCache.clippedOutline;
549}
550
John Reck113e0822014-03-18 09:22:59 -0700551} /* namespace uirenderer */
552} /* namespace android */