blob: f1c8232760e1b5ed7dfabd824f4aca498924001e [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 "OpDumper.h"
23#include "RecordedOp.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040024#include "TreeInfo.h"
John Reck2de950d2017-01-25 10:58:30 -080025#include "utils/FatVector.h"
Chris Craike0bb87d2014-04-22 17:55:41 -070026#include "utils/MathUtils.h"
sergeyvc3849aa2016-08-08 13:22:06 -070027#include "utils/StringUtils.h"
Chris Craik70850ea2014-11-18 10:49:23 -080028#include "utils/TraceUtils.h"
Chris Craik5e00c7c2016-07-06 16:10:09 -070029#include "VectorDrawable.h"
30#include "renderstate/RenderState.h"
John Reck998a6d82014-08-28 15:35:53 -070031#include "renderthread/CanvasContext.h"
John Reck113e0822014-03-18 09:22:59 -070032
John Recke248bd12015-08-05 13:53:53 -070033#include "protos/hwui.pb.h"
34#include "protos/ProtoHelpers.h"
35
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
49 void onMaybeRemovedFromTree(RenderNode* node) override {
50 node->onRemovedFromTree(mTreeInfo);
51 }
52
53private:
54 TreeInfo* mTreeInfo;
55};
56
John Reck8de65a82014-04-09 15:23:38 -070057RenderNode::RenderNode()
John Reckff941dc2014-05-14 16:34:14 -070058 : mDirtyPropertyFields(0)
Chris Craik003cc3d2015-10-16 10:24:55 -070059 , mNeedsDisplayListSync(false)
60 , mDisplayList(nullptr)
61 , mStagingDisplayList(nullptr)
John Reck68bfe0a2014-06-24 15:34:58 -070062 , mAnimatorManager(*this)
John Reckdcba6722014-07-08 13:59:49 -070063 , mParentCount(0) {
John Reck113e0822014-03-18 09:22:59 -070064}
65
66RenderNode::~RenderNode() {
John Reck2de950d2017-01-25 10:58:30 -080067 ImmediateRemoved observer(nullptr);
68 deleteDisplayList(observer);
Chris Craik003cc3d2015-10-16 10:24:55 -070069 delete mStagingDisplayList;
Derek Sollenberger0df62092016-09-27 16:04:42 -040070 LOG_ALWAYS_FATAL_IF(hasLayer(), "layer missed detachment!");
John Reck113e0822014-03-18 09:22:59 -070071}
72
John Reck2de950d2017-01-25 10:58:30 -080073void RenderNode::setStagingDisplayList(DisplayList* displayList) {
74 mValid = (displayList != nullptr);
Chris Craik003cc3d2015-10-16 10:24:55 -070075 mNeedsDisplayListSync = true;
76 delete mStagingDisplayList;
77 mStagingDisplayList = displayList;
John Reck113e0822014-03-18 09:22:59 -070078}
79
80/**
81 * This function is a simplified version of replay(), where we simply retrieve and log the
82 * display list. This function should remain in sync with the replay() function.
83 */
sergeyvc3849aa2016-08-08 13:22:06 -070084void RenderNode::output() {
85 LogcatStream strout;
86 strout << "Root";
87 output(strout, 0);
88}
89
90void RenderNode::output(std::ostream& output, uint32_t level) {
91 output << " (" << getName() << " " << this
92 << (MathUtils::isZero(properties().getAlpha()) ? ", zero alpha" : "")
93 << (properties().hasShadow() ? ", casting shadow" : "")
94 << (isRenderable() ? "" : ", empty")
95 << (properties().getProjectBackwards() ? ", projected" : "")
Derek Sollenberger0df62092016-09-27 16:04:42 -040096 << (hasLayer() ? ", on HW Layer" : "")
sergeyvc3849aa2016-08-08 13:22:06 -070097 << ")" << std::endl;
98
99 properties().debugOutputProperties(output, level + 1);
Chris Craik91eff222016-02-22 13:39:33 -0800100
101 if (mDisplayList) {
102 for (auto&& op : mDisplayList->getOps()) {
sergeyvc3849aa2016-08-08 13:22:06 -0700103 OpDumper::dump(*op, output, level + 1);
Chris Craik91eff222016-02-22 13:39:33 -0800104 if (op->opId == RecordedOpId::RenderNodeOp) {
105 auto rnOp = reinterpret_cast<const RenderNodeOp*>(op);
sergeyvc3849aa2016-08-08 13:22:06 -0700106 rnOp->renderNode->output(output, level + 1);
Chris Craik91eff222016-02-22 13:39:33 -0800107 } else {
sergeyvc3849aa2016-08-08 13:22:06 -0700108 output << std::endl;
Chris Craik91eff222016-02-22 13:39:33 -0800109 }
110 }
111 }
sergeyvc3849aa2016-08-08 13:22:06 -0700112 output << std::string(level * 2, ' ') << "/RenderNode(" << getName() << " " << this << ")";
113 output << std::endl;
Chris Craik91eff222016-02-22 13:39:33 -0800114}
John Reck113e0822014-03-18 09:22:59 -0700115
John Recke248bd12015-08-05 13:53:53 -0700116void RenderNode::copyTo(proto::RenderNode *pnode) {
117 pnode->set_id(static_cast<uint64_t>(
118 reinterpret_cast<uintptr_t>(this)));
119 pnode->set_name(mName.string(), mName.length());
120
121 proto::RenderProperties* pprops = pnode->mutable_properties();
122 pprops->set_left(properties().getLeft());
123 pprops->set_top(properties().getTop());
124 pprops->set_right(properties().getRight());
125 pprops->set_bottom(properties().getBottom());
126 pprops->set_clip_flags(properties().getClippingFlags());
127 pprops->set_alpha(properties().getAlpha());
128 pprops->set_translation_x(properties().getTranslationX());
129 pprops->set_translation_y(properties().getTranslationY());
130 pprops->set_translation_z(properties().getTranslationZ());
131 pprops->set_elevation(properties().getElevation());
132 pprops->set_rotation(properties().getRotation());
133 pprops->set_rotation_x(properties().getRotationX());
134 pprops->set_rotation_y(properties().getRotationY());
135 pprops->set_scale_x(properties().getScaleX());
136 pprops->set_scale_y(properties().getScaleY());
137 pprops->set_pivot_x(properties().getPivotX());
138 pprops->set_pivot_y(properties().getPivotY());
139 pprops->set_has_overlapping_rendering(properties().getHasOverlappingRendering());
140 pprops->set_pivot_explicitly_set(properties().isPivotExplicitlySet());
141 pprops->set_project_backwards(properties().getProjectBackwards());
142 pprops->set_projection_receiver(properties().isProjectionReceiver());
143 set(pprops->mutable_clip_bounds(), properties().getClipBounds());
144
145 const Outline& outline = properties().getOutline();
146 if (outline.getType() != Outline::Type::None) {
147 proto::Outline* poutline = pprops->mutable_outline();
148 poutline->clear_path();
149 if (outline.getType() == Outline::Type::Empty) {
150 poutline->set_type(proto::Outline_Type_Empty);
151 } else if (outline.getType() == Outline::Type::ConvexPath) {
152 poutline->set_type(proto::Outline_Type_ConvexPath);
153 if (const SkPath* path = outline.getPath()) {
154 set(poutline->mutable_path(), *path);
155 }
156 } else if (outline.getType() == Outline::Type::RoundRect) {
157 poutline->set_type(proto::Outline_Type_RoundRect);
158 } else {
159 ALOGW("Uknown outline type! %d", static_cast<int>(outline.getType()));
160 poutline->set_type(proto::Outline_Type_None);
161 }
162 poutline->set_should_clip(outline.getShouldClip());
163 poutline->set_alpha(outline.getAlpha());
164 poutline->set_radius(outline.getRadius());
165 set(poutline->mutable_bounds(), outline.getBounds());
166 } else {
167 pprops->clear_outline();
168 }
169
170 const RevealClip& revealClip = properties().getRevealClip();
171 if (revealClip.willClip()) {
172 proto::RevealClip* prevealClip = pprops->mutable_reveal_clip();
173 prevealClip->set_x(revealClip.getX());
174 prevealClip->set_y(revealClip.getY());
175 prevealClip->set_radius(revealClip.getRadius());
176 } else {
177 pprops->clear_reveal_clip();
178 }
179
180 pnode->clear_children();
Chris Craik003cc3d2015-10-16 10:24:55 -0700181 if (mDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700182 for (auto&& child : mDisplayList->getChildren()) {
Chris Craikb565df12015-10-05 13:00:52 -0700183 child->renderNode->copyTo(pnode->add_children());
John Recke248bd12015-08-05 13:53:53 -0700184 }
185 }
186}
187
John Reckfe5e7b72014-05-23 17:42:28 -0700188int RenderNode::getDebugSize() {
189 int size = sizeof(RenderNode);
Chris Craik003cc3d2015-10-16 10:24:55 -0700190 if (mStagingDisplayList) {
191 size += mStagingDisplayList->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700192 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700193 if (mDisplayList && mDisplayList != mStagingDisplayList) {
194 size += mDisplayList->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700195 }
196 return size;
197}
198
John Reckf4198b72014-04-09 17:00:04 -0700199void RenderNode::prepareTree(TreeInfo& info) {
200 ATRACE_CALL();
Chris Craik69e5adf2014-08-14 13:34:01 -0700201 LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
John Reck2de950d2017-01-25 10:58:30 -0800202 MarkAndSweepRemoved observer(&info);
John Reckf4198b72014-04-09 17:00:04 -0700203
Matt Sarettf58cc922016-11-14 18:33:38 -0500204 // The OpenGL renderer reserves the stencil buffer for overdraw debugging. Functors
205 // will need to be drawn in a layer.
206 bool functorsNeedLayer = Properties::debugOverdraw && !Properties::isSkiaEnabled();
Chris Craika766cb22015-06-08 16:49:43 -0700207
John Reck2de950d2017-01-25 10:58:30 -0800208 prepareTreeImpl(observer, info, functorsNeedLayer);
John Reckf4198b72014-04-09 17:00:04 -0700209}
210
John Reck68bfe0a2014-06-24 15:34:58 -0700211void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
212 mAnimatorManager.addAnimator(animator);
213}
214
Doris Liu8b083202016-02-19 21:46:06 +0000215void RenderNode::removeAnimator(const sp<BaseRenderNodeAnimator>& animator) {
216 mAnimatorManager.removeAnimator(animator);
217}
218
John Recke4267ea2014-06-03 15:53:15 -0700219void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700220 if (isRenderable()) {
John Reck293e8682014-06-17 10:34:02 -0700221 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700222 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
223 } else {
224 // Hope this is big enough?
225 // TODO: Get this from the display list ops or something
John Reckc1288232015-08-12 13:39:11 -0700226 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
John Recka447d292014-06-11 18:39:44 -0700227 }
John Recke4267ea2014-06-03 15:53:15 -0700228 }
229}
230
John Recka7c2ea22014-08-08 13:21:00 -0700231void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700232 LayerType layerType = properties().effectiveLayerType();
Chris Craik182952f2015-03-09 14:17:29 -0700233 if (CC_UNLIKELY(layerType == LayerType::RenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700234 // Damage applied so far needs to affect our parent, but does not require
235 // the layer to be updated. So we pop/push here to clear out the current
236 // damage and get a clean state for display list or children updates to
237 // affect, which will require the layer to be updated
238 info.damageAccumulator->popTransform();
239 info.damageAccumulator->pushTransform(this);
240 if (dirtyMask & DISPLAY_LIST) {
241 damageSelf(info);
242 }
John Reck25fbb3f2014-06-12 13:46:45 -0700243 }
244}
245
246void RenderNode::pushLayerUpdate(TreeInfo& info) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700247 LayerType layerType = properties().effectiveLayerType();
John Reck25fbb3f2014-06-12 13:46:45 -0700248 // If we are not a layer OR we cannot be rendered (eg, view was detached)
249 // we need to destroy any Layers we may have had previously
Chris Craike3e481d2016-07-11 12:20:51 -0700250 if (CC_LIKELY(layerType != LayerType::RenderLayer)
251 || CC_UNLIKELY(!isRenderable())
252 || CC_UNLIKELY(properties().getWidth() == 0)
253 || CC_UNLIKELY(properties().getHeight() == 0)) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400254 if (CC_UNLIKELY(hasLayer())) {
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400255 renderthread::CanvasContext::destroyLayer(this);
John Reck25fbb3f2014-06-12 13:46:45 -0700256 }
257 return;
258 }
259
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400260 if(info.canvasContext.createOrUpdateLayer(this, *info.damageAccumulator)) {
Chris Craik9fded232015-11-11 16:42:34 -0800261 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700262 }
263
Derek Sollenberger0df62092016-09-27 16:04:42 -0400264 if (!hasLayer()) {
John Reck0e89e2b2014-10-31 14:49:06 -0700265 Caches::getInstance().dumpMemoryUsage();
John Reckc25e5062014-06-18 14:21:29 -0700266 if (info.errorHandler) {
John Reck77c40102015-10-26 15:49:47 -0700267 std::ostringstream err;
268 err << "Unable to create layer for " << getName();
Chris Craik76caecf2015-11-02 19:17:45 -0800269 const int maxTextureSize = Caches::getInstance().maxTextureSize;
John Reck77c40102015-10-26 15:49:47 -0700270 if (getWidth() > maxTextureSize || getHeight() > maxTextureSize) {
271 err << ", size " << getWidth() << "x" << getHeight()
272 << " exceeds max size " << maxTextureSize;
273 } else {
274 err << ", see logcat for more info";
275 }
276 info.errorHandler->onError(err.str());
John Reckc25e5062014-06-18 14:21:29 -0700277 }
278 return;
279 }
280
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400281 SkRect dirty;
282 info.damageAccumulator->peekAtDirty(&dirty);
Chris Craik0b7e8242015-10-28 16:50:44 -0700283 info.layerUpdateQueue->enqueueLayerWithDamage(this, dirty);
John Reck998a6d82014-08-28 15:35:53 -0700284
Chris Craike2e53a72015-10-28 15:55:40 -0700285 // There might be prefetched layers that need to be accounted for.
286 // That might be us, so tell CanvasContext that this layer is in the
287 // tree and should not be destroyed.
288 info.canvasContext.markLayerInUse(this);
John Reck25fbb3f2014-06-12 13:46:45 -0700289}
290
Chris Craika766cb22015-06-08 16:49:43 -0700291/**
292 * Traverse down the the draw tree to prepare for a frame.
293 *
294 * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven
295 *
296 * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the
297 * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer.
298 */
John Reck2de950d2017-01-25 10:58:30 -0800299void RenderNode::prepareTreeImpl(TreeObserver& observer, TreeInfo& info, bool functorsNeedLayer) {
John Recka447d292014-06-11 18:39:44 -0700300 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700301
John Reckdcba6722014-07-08 13:59:49 -0700302 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700303 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700304 }
John Reck9eb9f6f2014-08-21 11:23:05 -0700305 uint32_t animatorDirtyMask = 0;
306 if (CC_LIKELY(info.runAnimations)) {
307 animatorDirtyMask = mAnimatorManager.animate(info);
308 }
Chris Craika766cb22015-06-08 16:49:43 -0700309
John Reck3f725f02015-06-16 10:29:31 -0700310 bool willHaveFunctor = false;
Chris Craik003cc3d2015-10-16 10:24:55 -0700311 if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400312 willHaveFunctor = mStagingDisplayList->hasFunctor();
Chris Craik003cc3d2015-10-16 10:24:55 -0700313 } else if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400314 willHaveFunctor = mDisplayList->hasFunctor();
John Reck3f725f02015-06-16 10:29:31 -0700315 }
Chris Craika766cb22015-06-08 16:49:43 -0700316 bool childFunctorsNeedLayer = mProperties.prepareForFunctorPresence(
317 willHaveFunctor, functorsNeedLayer);
318
John Reckf6481082016-02-02 15:18:23 -0800319 if (CC_UNLIKELY(mPositionListener.get())) {
320 mPositionListener->onPositionUpdated(*this, info);
321 }
322
John Recka7c2ea22014-08-08 13:21:00 -0700323 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700324 if (info.mode == TreeInfo::MODE_FULL) {
John Reck2de950d2017-01-25 10:58:30 -0800325 pushStagingDisplayListChanges(observer, info);
John Reck25fbb3f2014-06-12 13:46:45 -0700326 }
John Reck25fbb3f2014-06-12 13:46:45 -0700327
Doris Liu07c056d2016-06-13 12:52:44 -0700328 if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400329 info.out.hasFunctors |= mDisplayList->hasFunctor();
John Reck2de950d2017-01-25 10:58:30 -0800330 bool isDirty = mDisplayList->prepareListAndChildren(observer, info, childFunctorsNeedLayer,
331 [](RenderNode* child, TreeObserver& observer, TreeInfo& info, bool functorsNeedLayer) {
332 child->prepareTreeImpl(observer, info, functorsNeedLayer);
Derek Sollenberger0df62092016-09-27 16:04:42 -0400333 });
334 if (isDirty) {
335 damageSelf(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700336 }
Doris Liu718cd3e2016-05-17 16:50:31 -0700337 }
Doris Liub51b2862016-08-01 19:56:47 -0700338 pushLayerUpdate(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700339
John Recka447d292014-06-11 18:39:44 -0700340 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700341}
342
Chris Craikb565df12015-10-05 13:00:52 -0700343void RenderNode::syncProperties() {
344 mProperties = mStagingProperties;
345}
346
John Reck25fbb3f2014-06-12 13:46:45 -0700347void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reckff941dc2014-05-14 16:34:14 -0700348 // Push the animators first so that setupStartValueIfNecessary() is called
349 // before properties() is trampled by stagingProperties(), as they are
350 // required by some animators.
John Reck9eb9f6f2014-08-21 11:23:05 -0700351 if (CC_LIKELY(info.runAnimations)) {
John Reck119907c2014-08-14 09:02:01 -0700352 mAnimatorManager.pushStaging();
John Reck9eb9f6f2014-08-21 11:23:05 -0700353 }
John Reckff941dc2014-05-14 16:34:14 -0700354 if (mDirtyPropertyFields) {
355 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700356 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700357 info.damageAccumulator->popTransform();
Chris Craikb565df12015-10-05 13:00:52 -0700358 syncProperties();
John Recke4267ea2014-06-03 15:53:15 -0700359 // We could try to be clever and only re-damage if the matrix changed.
360 // However, we don't need to worry about that. The cost of over-damaging
361 // here is only going to be a single additional map rect of this node
362 // plus a rect join(). The parent's transform (and up) will only be
363 // performed once.
John Recka447d292014-06-11 18:39:44 -0700364 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700365 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700366 }
John Reck25fbb3f2014-06-12 13:46:45 -0700367}
368
John Reck2de950d2017-01-25 10:58:30 -0800369void RenderNode::syncDisplayList(TreeObserver& observer, TreeInfo* info) {
Chris Craikb565df12015-10-05 13:00:52 -0700370 // Make sure we inc first so that we don't fluctuate between 0 and 1,
371 // which would thrash the layer cache
Chris Craik003cc3d2015-10-16 10:24:55 -0700372 if (mStagingDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400373 mStagingDisplayList->updateChildren([](RenderNode* child) {
374 child->incParentRefCount();
375 });
Chris Craikb565df12015-10-05 13:00:52 -0700376 }
John Reck2de950d2017-01-25 10:58:30 -0800377 deleteDisplayList(observer, info);
Chris Craik003cc3d2015-10-16 10:24:55 -0700378 mDisplayList = mStagingDisplayList;
379 mStagingDisplayList = nullptr;
380 if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400381 mDisplayList->syncContents();
Chris Craikb565df12015-10-05 13:00:52 -0700382 }
383}
384
John Reck2de950d2017-01-25 10:58:30 -0800385void RenderNode::pushStagingDisplayListChanges(TreeObserver& observer, TreeInfo& info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700386 if (mNeedsDisplayListSync) {
387 mNeedsDisplayListSync = false;
John Reck5c9d7172014-10-22 11:32:27 -0700388 // Damage with the old display list first then the new one to catch any
389 // changes in isRenderable or, in the future, bounds
390 damageSelf(info);
John Reck2de950d2017-01-25 10:58:30 -0800391 syncDisplayList(observer, &info);
John Recke4267ea2014-06-03 15:53:15 -0700392 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700393 }
John Reck8de65a82014-04-09 15:23:38 -0700394}
395
John Reck2de950d2017-01-25 10:58:30 -0800396void RenderNode::deleteDisplayList(TreeObserver& observer, TreeInfo* info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700397 if (mDisplayList) {
John Reck2de950d2017-01-25 10:58:30 -0800398 mDisplayList->updateChildren([&observer, info](RenderNode* child) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400399 child->decParentRefCount(observer, info);
400 });
401 if (!mDisplayList->reuseDisplayList(this, info ? &info->canvasContext : nullptr)) {
402 delete mDisplayList;
John Reckdcba6722014-07-08 13:59:49 -0700403 }
404 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700405 mDisplayList = nullptr;
John Reckdcba6722014-07-08 13:59:49 -0700406}
407
John Reck2de950d2017-01-25 10:58:30 -0800408void RenderNode::destroyHardwareResources(TreeInfo* info) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400409 if (hasLayer()) {
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400410 renderthread::CanvasContext::destroyLayer(this);
John Reckdcba6722014-07-08 13:59:49 -0700411 }
John Reck3afd6372017-01-30 10:15:48 -0800412 setStagingDisplayList(nullptr);
413
414 ImmediateRemoved observer(info);
415 deleteDisplayList(observer, info);
John Reck2de950d2017-01-25 10:58:30 -0800416}
417
418void RenderNode::destroyLayers() {
419 if (hasLayer()) {
420 renderthread::CanvasContext::destroyLayer(this);
421 }
422 if (mDisplayList) {
423 mDisplayList->updateChildren([](RenderNode* child) {
424 child->destroyLayers();
425 });
426 }
427}
428
429void RenderNode::decParentRefCount(TreeObserver& observer, TreeInfo* info) {
430 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
431 mParentCount--;
432 if (!mParentCount) {
433 observer.onMaybeRemovedFromTree(this);
434 if (CC_UNLIKELY(mPositionListener.get())) {
435 mPositionListener->onPositionLost(*this, info);
John Reckdcba6722014-07-08 13:59:49 -0700436 }
437 }
438}
439
John Reck2de950d2017-01-25 10:58:30 -0800440void RenderNode::onRemovedFromTree(TreeInfo* info) {
441 destroyHardwareResources(info);
442}
443
444void RenderNode::clearRoot() {
445 ImmediateRemoved observer(nullptr);
446 decParentRefCount(observer);
John Reckdcba6722014-07-08 13:59:49 -0700447}
448
John Reck113e0822014-03-18 09:22:59 -0700449/**
450 * Apply property-based transformations to input matrix
451 *
452 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
453 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
454 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700455void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700456 if (properties().getLeft() != 0 || properties().getTop() != 0) {
457 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700458 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700459 if (properties().getStaticMatrix()) {
460 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700461 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700462 } else if (properties().getAnimationMatrix()) {
463 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700464 matrix.multiply(anim);
465 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700466
Chris Craikcc39e162014-04-25 18:34:11 -0700467 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700468 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700469 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700470 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700471 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700472 } else {
473 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700474 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700475 } else {
476 mat4 true3dMat;
477 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700478 properties().getPivotX() + properties().getTranslationX(),
479 properties().getPivotY() + properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700480 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700481 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
482 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
483 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
484 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
485 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700486
487 matrix.multiply(true3dMat);
488 }
489 }
490 }
491}
492
493/**
494 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
495 *
496 * This should be called before a call to defer() or drawDisplayList()
497 *
498 * Each DisplayList that serves as a 3d root builds its list of composited children,
499 * which are flagged to not draw in the standard draw loop.
500 */
501void RenderNode::computeOrdering() {
502 ATRACE_CALL();
503 mProjectedNodes.clear();
504
505 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
506 // transform properties are applied correctly to top level children
Chris Craik003cc3d2015-10-16 10:24:55 -0700507 if (mDisplayList == nullptr) return;
Chris Craikb36af872015-10-16 14:23:12 -0700508 for (unsigned int i = 0; i < mDisplayList->getChildren().size(); i++) {
Chris Craik5e00c7c2016-07-06 16:10:09 -0700509 RenderNodeOp* childOp = mDisplayList->getChildren()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700510 childOp->renderNode->computeOrderingImpl(childOp, &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700511 }
512}
513
514void RenderNode::computeOrderingImpl(
Chris Craik5e00c7c2016-07-06 16:10:09 -0700515 RenderNodeOp* opState,
516 std::vector<RenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700517 const mat4* transformFromProjectionSurface) {
518 mProjectedNodes.clear();
Chris Craik003cc3d2015-10-16 10:24:55 -0700519 if (mDisplayList == nullptr || mDisplayList->isEmpty()) return;
John Reck113e0822014-03-18 09:22:59 -0700520
521 // TODO: should avoid this calculation in most cases
522 // TODO: just calculate single matrix, down to all leaf composited elements
523 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
Chris Craik8d1f2122015-11-24 16:40:09 -0800524 localTransformFromProjectionSurface.multiply(opState->localMatrix);
John Reck113e0822014-03-18 09:22:59 -0700525
John Reckd0a0b2a2014-03-20 16:28:56 -0700526 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700527 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
Chris Craik8d1f2122015-11-24 16:40:09 -0800528 opState->skipInOrderDraw = true;
529 opState->transformFromCompositingAncestor = localTransformFromProjectionSurface;
John Reck272a6852015-07-29 16:48:58 -0700530 compositedChildrenOfProjectionSurface->push_back(opState);
John Reck113e0822014-03-18 09:22:59 -0700531 } else {
532 // standard in order draw
Chris Craik8d1f2122015-11-24 16:40:09 -0800533 opState->skipInOrderDraw = false;
John Reck113e0822014-03-18 09:22:59 -0700534 }
535
Chris Craikb36af872015-10-16 14:23:12 -0700536 if (mDisplayList->getChildren().size() > 0) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700537 const bool isProjectionReceiver = mDisplayList->projectionReceiveIndex >= 0;
John Reck113e0822014-03-18 09:22:59 -0700538 bool haveAppliedPropertiesToProjection = false;
Chris Craikb36af872015-10-16 14:23:12 -0700539 for (unsigned int i = 0; i < mDisplayList->getChildren().size(); i++) {
Chris Craik5e00c7c2016-07-06 16:10:09 -0700540 RenderNodeOp* childOp = mDisplayList->getChildren()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700541 RenderNode* child = childOp->renderNode;
John Reck113e0822014-03-18 09:22:59 -0700542
Chris Craik5e00c7c2016-07-06 16:10:09 -0700543 std::vector<RenderNodeOp*>* projectionChildren = nullptr;
Chris Craikd41c4d82015-01-05 15:51:13 -0800544 const mat4* projectionTransform = nullptr;
John Reckd0a0b2a2014-03-20 16:28:56 -0700545 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
Chris Craikbf72eb82015-06-08 11:30:44 -0700546 // if receiving projections, collect projecting descendant
John Reck113e0822014-03-18 09:22:59 -0700547
Chris Craikbf72eb82015-06-08 11:30:44 -0700548 // Note that if a direct descendant is projecting backwards, we pass its
549 // grandparent projection collection, since it shouldn't project onto its
John Reck113e0822014-03-18 09:22:59 -0700550 // parent, where it will already be drawing.
551 projectionChildren = &mProjectedNodes;
552 projectionTransform = &mat4::identity();
553 } else {
554 if (!haveAppliedPropertiesToProjection) {
555 applyViewPropertyTransforms(localTransformFromProjectionSurface);
556 haveAppliedPropertiesToProjection = true;
557 }
558 projectionChildren = compositedChildrenOfProjectionSurface;
559 projectionTransform = &localTransformFromProjectionSurface;
560 }
Derek Sollenbergerf2932592015-08-13 14:59:33 -0400561 child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700562 }
563 }
John Reck113e0822014-03-18 09:22:59 -0700564}
565
John Reck113e0822014-03-18 09:22:59 -0700566} /* namespace uirenderer */
567} /* namespace android */