blob: a5443d9ff6f17d7383a2f3580eed0755882e8fa7 [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"
Chris Craike0bb87d2014-04-22 17:55:41 -070025#include "utils/MathUtils.h"
sergeyvc3849aa2016-08-08 13:22:06 -070026#include "utils/StringUtils.h"
Chris Craik70850ea2014-11-18 10:49:23 -080027#include "utils/TraceUtils.h"
Chris Craik5e00c7c2016-07-06 16:10:09 -070028#include "VectorDrawable.h"
29#include "renderstate/RenderState.h"
John Reck998a6d82014-08-28 15:35:53 -070030#include "renderthread/CanvasContext.h"
John Reck113e0822014-03-18 09:22:59 -070031
John Recke248bd12015-08-05 13:53:53 -070032#include "protos/hwui.pb.h"
33#include "protos/ProtoHelpers.h"
34
John Reck77c40102015-10-26 15:49:47 -070035#include <algorithm>
36#include <sstream>
37#include <string>
38
John Reck113e0822014-03-18 09:22:59 -070039namespace android {
40namespace uirenderer {
41
John Reck8de65a82014-04-09 15:23:38 -070042RenderNode::RenderNode()
John Reckff941dc2014-05-14 16:34:14 -070043 : mDirtyPropertyFields(0)
Chris Craik003cc3d2015-10-16 10:24:55 -070044 , mNeedsDisplayListSync(false)
45 , mDisplayList(nullptr)
46 , mStagingDisplayList(nullptr)
John Reck68bfe0a2014-06-24 15:34:58 -070047 , mAnimatorManager(*this)
John Reckdcba6722014-07-08 13:59:49 -070048 , mParentCount(0) {
John Reck113e0822014-03-18 09:22:59 -070049}
50
51RenderNode::~RenderNode() {
John Reck44b49f02016-03-25 14:29:48 -070052 deleteDisplayList(nullptr);
Chris Craik003cc3d2015-10-16 10:24:55 -070053 delete mStagingDisplayList;
Derek Sollenberger0df62092016-09-27 16:04:42 -040054 LOG_ALWAYS_FATAL_IF(hasLayer(), "layer missed detachment!");
John Reck113e0822014-03-18 09:22:59 -070055}
56
John Reck51f2d602016-04-06 07:50:47 -070057void RenderNode::setStagingDisplayList(DisplayList* displayList, TreeObserver* observer) {
Chris Craik003cc3d2015-10-16 10:24:55 -070058 mNeedsDisplayListSync = true;
59 delete mStagingDisplayList;
60 mStagingDisplayList = displayList;
John Reck9dea0d52015-10-27 10:04:38 -070061 // If mParentCount == 0 we are the sole reference to this RenderNode,
62 // so immediately free the old display list
63 if (!mParentCount && !mStagingDisplayList) {
John Reck51f2d602016-04-06 07:50:47 -070064 deleteDisplayList(observer);
John Reck9dea0d52015-10-27 10:04:38 -070065 }
John Reck113e0822014-03-18 09:22:59 -070066}
67
68/**
69 * This function is a simplified version of replay(), where we simply retrieve and log the
70 * display list. This function should remain in sync with the replay() function.
71 */
sergeyvc3849aa2016-08-08 13:22:06 -070072void RenderNode::output() {
73 LogcatStream strout;
74 strout << "Root";
75 output(strout, 0);
76}
77
78void RenderNode::output(std::ostream& output, uint32_t level) {
79 output << " (" << getName() << " " << this
80 << (MathUtils::isZero(properties().getAlpha()) ? ", zero alpha" : "")
81 << (properties().hasShadow() ? ", casting shadow" : "")
82 << (isRenderable() ? "" : ", empty")
83 << (properties().getProjectBackwards() ? ", projected" : "")
Derek Sollenberger0df62092016-09-27 16:04:42 -040084 << (hasLayer() ? ", on HW Layer" : "")
sergeyvc3849aa2016-08-08 13:22:06 -070085 << ")" << std::endl;
86
87 properties().debugOutputProperties(output, level + 1);
Chris Craik91eff222016-02-22 13:39:33 -080088
89 if (mDisplayList) {
90 for (auto&& op : mDisplayList->getOps()) {
sergeyvc3849aa2016-08-08 13:22:06 -070091 OpDumper::dump(*op, output, level + 1);
Chris Craik91eff222016-02-22 13:39:33 -080092 if (op->opId == RecordedOpId::RenderNodeOp) {
93 auto rnOp = reinterpret_cast<const RenderNodeOp*>(op);
sergeyvc3849aa2016-08-08 13:22:06 -070094 rnOp->renderNode->output(output, level + 1);
Chris Craik91eff222016-02-22 13:39:33 -080095 } else {
sergeyvc3849aa2016-08-08 13:22:06 -070096 output << std::endl;
Chris Craik91eff222016-02-22 13:39:33 -080097 }
98 }
99 }
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 Recke248bd12015-08-05 13:53:53 -0700104void RenderNode::copyTo(proto::RenderNode *pnode) {
105 pnode->set_id(static_cast<uint64_t>(
106 reinterpret_cast<uintptr_t>(this)));
107 pnode->set_name(mName.string(), mName.length());
108
109 proto::RenderProperties* pprops = pnode->mutable_properties();
110 pprops->set_left(properties().getLeft());
111 pprops->set_top(properties().getTop());
112 pprops->set_right(properties().getRight());
113 pprops->set_bottom(properties().getBottom());
114 pprops->set_clip_flags(properties().getClippingFlags());
115 pprops->set_alpha(properties().getAlpha());
116 pprops->set_translation_x(properties().getTranslationX());
117 pprops->set_translation_y(properties().getTranslationY());
118 pprops->set_translation_z(properties().getTranslationZ());
119 pprops->set_elevation(properties().getElevation());
120 pprops->set_rotation(properties().getRotation());
121 pprops->set_rotation_x(properties().getRotationX());
122 pprops->set_rotation_y(properties().getRotationY());
123 pprops->set_scale_x(properties().getScaleX());
124 pprops->set_scale_y(properties().getScaleY());
125 pprops->set_pivot_x(properties().getPivotX());
126 pprops->set_pivot_y(properties().getPivotY());
127 pprops->set_has_overlapping_rendering(properties().getHasOverlappingRendering());
128 pprops->set_pivot_explicitly_set(properties().isPivotExplicitlySet());
129 pprops->set_project_backwards(properties().getProjectBackwards());
130 pprops->set_projection_receiver(properties().isProjectionReceiver());
131 set(pprops->mutable_clip_bounds(), properties().getClipBounds());
132
133 const Outline& outline = properties().getOutline();
134 if (outline.getType() != Outline::Type::None) {
135 proto::Outline* poutline = pprops->mutable_outline();
136 poutline->clear_path();
137 if (outline.getType() == Outline::Type::Empty) {
138 poutline->set_type(proto::Outline_Type_Empty);
139 } else if (outline.getType() == Outline::Type::ConvexPath) {
140 poutline->set_type(proto::Outline_Type_ConvexPath);
141 if (const SkPath* path = outline.getPath()) {
142 set(poutline->mutable_path(), *path);
143 }
144 } else if (outline.getType() == Outline::Type::RoundRect) {
145 poutline->set_type(proto::Outline_Type_RoundRect);
146 } else {
147 ALOGW("Uknown outline type! %d", static_cast<int>(outline.getType()));
148 poutline->set_type(proto::Outline_Type_None);
149 }
150 poutline->set_should_clip(outline.getShouldClip());
151 poutline->set_alpha(outline.getAlpha());
152 poutline->set_radius(outline.getRadius());
153 set(poutline->mutable_bounds(), outline.getBounds());
154 } else {
155 pprops->clear_outline();
156 }
157
158 const RevealClip& revealClip = properties().getRevealClip();
159 if (revealClip.willClip()) {
160 proto::RevealClip* prevealClip = pprops->mutable_reveal_clip();
161 prevealClip->set_x(revealClip.getX());
162 prevealClip->set_y(revealClip.getY());
163 prevealClip->set_radius(revealClip.getRadius());
164 } else {
165 pprops->clear_reveal_clip();
166 }
167
168 pnode->clear_children();
Chris Craik003cc3d2015-10-16 10:24:55 -0700169 if (mDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700170 for (auto&& child : mDisplayList->getChildren()) {
Chris Craikb565df12015-10-05 13:00:52 -0700171 child->renderNode->copyTo(pnode->add_children());
John Recke248bd12015-08-05 13:53:53 -0700172 }
173 }
174}
175
John Reckfe5e7b72014-05-23 17:42:28 -0700176int RenderNode::getDebugSize() {
177 int size = sizeof(RenderNode);
Chris Craik003cc3d2015-10-16 10:24:55 -0700178 if (mStagingDisplayList) {
179 size += mStagingDisplayList->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700180 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700181 if (mDisplayList && mDisplayList != mStagingDisplayList) {
182 size += mDisplayList->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700183 }
184 return size;
185}
186
John Reckf4198b72014-04-09 17:00:04 -0700187void RenderNode::prepareTree(TreeInfo& info) {
188 ATRACE_CALL();
Chris Craik69e5adf2014-08-14 13:34:01 -0700189 LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
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
195 prepareTreeImpl(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
Chris Craike3e481d2016-07-11 12:20:51 -0700237 if (CC_LIKELY(layerType != LayerType::RenderLayer)
238 || CC_UNLIKELY(!isRenderable())
239 || CC_UNLIKELY(properties().getWidth() == 0)
240 || CC_UNLIKELY(properties().getHeight() == 0)) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400241 if (CC_UNLIKELY(hasLayer())) {
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400242 renderthread::CanvasContext::destroyLayer(this);
John Reck25fbb3f2014-06-12 13:46:45 -0700243 }
244 return;
245 }
246
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400247 if(info.canvasContext.createOrUpdateLayer(this, *info.damageAccumulator)) {
Chris Craik9fded232015-11-11 16:42:34 -0800248 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700249 }
250
Derek Sollenberger0df62092016-09-27 16:04:42 -0400251 if (!hasLayer()) {
John Reck0e89e2b2014-10-31 14:49:06 -0700252 Caches::getInstance().dumpMemoryUsage();
John Reckc25e5062014-06-18 14:21:29 -0700253 if (info.errorHandler) {
John Reck77c40102015-10-26 15:49:47 -0700254 std::ostringstream err;
255 err << "Unable to create layer for " << getName();
Chris Craik76caecf2015-11-02 19:17:45 -0800256 const int maxTextureSize = Caches::getInstance().maxTextureSize;
John Reck77c40102015-10-26 15:49:47 -0700257 if (getWidth() > maxTextureSize || getHeight() > maxTextureSize) {
258 err << ", size " << getWidth() << "x" << getHeight()
259 << " exceeds max size " << maxTextureSize;
260 } else {
261 err << ", see logcat for more info";
262 }
263 info.errorHandler->onError(err.str());
John Reckc25e5062014-06-18 14:21:29 -0700264 }
265 return;
266 }
267
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400268 SkRect dirty;
269 info.damageAccumulator->peekAtDirty(&dirty);
Chris Craik0b7e8242015-10-28 16:50:44 -0700270 info.layerUpdateQueue->enqueueLayerWithDamage(this, dirty);
John Reck998a6d82014-08-28 15:35:53 -0700271
Chris Craike2e53a72015-10-28 15:55:40 -0700272 // There might be prefetched layers that need to be accounted for.
273 // That might be us, so tell CanvasContext that this layer is in the
274 // tree and should not be destroyed.
275 info.canvasContext.markLayerInUse(this);
John Reck25fbb3f2014-06-12 13:46:45 -0700276}
277
Chris Craika766cb22015-06-08 16:49:43 -0700278/**
279 * Traverse down the the draw tree to prepare for a frame.
280 *
281 * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven
282 *
283 * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the
284 * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer.
285 */
286void RenderNode::prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer) {
John Recka447d292014-06-11 18:39:44 -0700287 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700288
John Reckdcba6722014-07-08 13:59:49 -0700289 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700290 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700291 }
John Reck9eb9f6f2014-08-21 11:23:05 -0700292 uint32_t animatorDirtyMask = 0;
293 if (CC_LIKELY(info.runAnimations)) {
294 animatorDirtyMask = mAnimatorManager.animate(info);
295 }
Chris Craika766cb22015-06-08 16:49:43 -0700296
John Reck3f725f02015-06-16 10:29:31 -0700297 bool willHaveFunctor = false;
Chris Craik003cc3d2015-10-16 10:24:55 -0700298 if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400299 willHaveFunctor = mStagingDisplayList->hasFunctor();
Chris Craik003cc3d2015-10-16 10:24:55 -0700300 } else if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400301 willHaveFunctor = mDisplayList->hasFunctor();
John Reck3f725f02015-06-16 10:29:31 -0700302 }
Chris Craika766cb22015-06-08 16:49:43 -0700303 bool childFunctorsNeedLayer = mProperties.prepareForFunctorPresence(
304 willHaveFunctor, functorsNeedLayer);
305
John Reckf6481082016-02-02 15:18:23 -0800306 if (CC_UNLIKELY(mPositionListener.get())) {
307 mPositionListener->onPositionUpdated(*this, info);
308 }
309
John Recka7c2ea22014-08-08 13:21:00 -0700310 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700311 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700312 pushStagingDisplayListChanges(info);
313 }
John Reck25fbb3f2014-06-12 13:46:45 -0700314
Doris Liu07c056d2016-06-13 12:52:44 -0700315 if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400316 info.out.hasFunctors |= mDisplayList->hasFunctor();
317 bool isDirty = mDisplayList->prepareListAndChildren(info, childFunctorsNeedLayer,
318 [](RenderNode* child, TreeInfo& info, bool functorsNeedLayer) {
319 child->prepareTreeImpl(info, functorsNeedLayer);
320 });
321 if (isDirty) {
322 damageSelf(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700323 }
Doris Liu718cd3e2016-05-17 16:50:31 -0700324 }
Doris Liub51b2862016-08-01 19:56:47 -0700325 pushLayerUpdate(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700326
John Recka447d292014-06-11 18:39:44 -0700327 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700328}
329
Chris Craikb565df12015-10-05 13:00:52 -0700330void RenderNode::syncProperties() {
331 mProperties = mStagingProperties;
332}
333
John Reck25fbb3f2014-06-12 13:46:45 -0700334void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reckff941dc2014-05-14 16:34:14 -0700335 // Push the animators first so that setupStartValueIfNecessary() is called
336 // before properties() is trampled by stagingProperties(), as they are
337 // required by some animators.
John Reck9eb9f6f2014-08-21 11:23:05 -0700338 if (CC_LIKELY(info.runAnimations)) {
John Reck119907c2014-08-14 09:02:01 -0700339 mAnimatorManager.pushStaging();
John Reck9eb9f6f2014-08-21 11:23:05 -0700340 }
John Reckff941dc2014-05-14 16:34:14 -0700341 if (mDirtyPropertyFields) {
342 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700343 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700344 info.damageAccumulator->popTransform();
Chris Craikb565df12015-10-05 13:00:52 -0700345 syncProperties();
John Recke4267ea2014-06-03 15:53:15 -0700346 // We could try to be clever and only re-damage if the matrix changed.
347 // However, we don't need to worry about that. The cost of over-damaging
348 // here is only going to be a single additional map rect of this node
349 // plus a rect join(). The parent's transform (and up) will only be
350 // performed once.
John Recka447d292014-06-11 18:39:44 -0700351 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700352 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700353 }
John Reck25fbb3f2014-06-12 13:46:45 -0700354}
355
John Reckaa6e84f2016-06-16 15:36:13 -0700356void RenderNode::syncDisplayList(TreeInfo* info) {
Chris Craikb565df12015-10-05 13:00:52 -0700357 // Make sure we inc first so that we don't fluctuate between 0 and 1,
358 // which would thrash the layer cache
Chris Craik003cc3d2015-10-16 10:24:55 -0700359 if (mStagingDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400360 mStagingDisplayList->updateChildren([](RenderNode* child) {
361 child->incParentRefCount();
362 });
Chris Craikb565df12015-10-05 13:00:52 -0700363 }
John Reckaa6e84f2016-06-16 15:36:13 -0700364 deleteDisplayList(info ? info->observer : nullptr, info);
Chris Craik003cc3d2015-10-16 10:24:55 -0700365 mDisplayList = mStagingDisplayList;
366 mStagingDisplayList = nullptr;
367 if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400368 mDisplayList->syncContents();
Chris Craikb565df12015-10-05 13:00:52 -0700369 }
370}
371
John Reck25fbb3f2014-06-12 13:46:45 -0700372void RenderNode::pushStagingDisplayListChanges(TreeInfo& info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700373 if (mNeedsDisplayListSync) {
374 mNeedsDisplayListSync = false;
John Reck5c9d7172014-10-22 11:32:27 -0700375 // Damage with the old display list first then the new one to catch any
376 // changes in isRenderable or, in the future, bounds
377 damageSelf(info);
John Reckaa6e84f2016-06-16 15:36:13 -0700378 syncDisplayList(&info);
John Recke4267ea2014-06-03 15:53:15 -0700379 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700380 }
John Reck8de65a82014-04-09 15:23:38 -0700381}
382
John Reckaa6e84f2016-06-16 15:36:13 -0700383void RenderNode::deleteDisplayList(TreeObserver* observer, TreeInfo* info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700384 if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400385 mDisplayList->updateChildren([observer, info](RenderNode* child) {
386 child->decParentRefCount(observer, info);
387 });
388 if (!mDisplayList->reuseDisplayList(this, info ? &info->canvasContext : nullptr)) {
389 delete mDisplayList;
John Reckdcba6722014-07-08 13:59:49 -0700390 }
391 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700392 mDisplayList = nullptr;
John Reckdcba6722014-07-08 13:59:49 -0700393}
394
John Reckaa6e84f2016-06-16 15:36:13 -0700395void RenderNode::destroyHardwareResources(TreeObserver* observer, TreeInfo* info) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400396 if (hasLayer()) {
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400397 renderthread::CanvasContext::destroyLayer(this);
John Reckdcba6722014-07-08 13:59:49 -0700398 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700399 if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400400 mDisplayList->updateChildren([observer, info](RenderNode* child) {
401 child->destroyHardwareResources(observer, info);
402 });
Chris Craik003cc3d2015-10-16 10:24:55 -0700403 if (mNeedsDisplayListSync) {
John Reckdcba6722014-07-08 13:59:49 -0700404 // Next prepare tree we are going to push a new display list, so we can
405 // drop our current one now
John Reckaa6e84f2016-06-16 15:36:13 -0700406 deleteDisplayList(observer, info);
John Reckdcba6722014-07-08 13:59:49 -0700407 }
408 }
409}
410
John Reckaa6e84f2016-06-16 15:36:13 -0700411void RenderNode::decParentRefCount(TreeObserver* observer, TreeInfo* info) {
John Reckdcba6722014-07-08 13:59:49 -0700412 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
413 mParentCount--;
414 if (!mParentCount) {
John Reck44b49f02016-03-25 14:29:48 -0700415 if (observer) {
416 observer->onMaybeRemovedFromTree(this);
417 }
John Reckaa6e84f2016-06-16 15:36:13 -0700418 if (CC_UNLIKELY(mPositionListener.get())) {
419 mPositionListener->onPositionLost(*this, info);
420 }
John Reckdcba6722014-07-08 13:59:49 -0700421 // If a child of ours is being attached to our parent then this will incorrectly
422 // destroy its hardware resources. However, this situation is highly unlikely
423 // and the failure is "just" that the layer is re-created, so this should
424 // be safe enough
John Reckaa6e84f2016-06-16 15:36:13 -0700425 destroyHardwareResources(observer, info);
John Reckdcba6722014-07-08 13:59:49 -0700426 }
427}
428
John Reck113e0822014-03-18 09:22:59 -0700429/**
430 * Apply property-based transformations to input matrix
431 *
432 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
433 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
434 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700435void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700436 if (properties().getLeft() != 0 || properties().getTop() != 0) {
437 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700438 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700439 if (properties().getStaticMatrix()) {
440 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700441 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700442 } else if (properties().getAnimationMatrix()) {
443 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700444 matrix.multiply(anim);
445 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700446
Chris Craikcc39e162014-04-25 18:34:11 -0700447 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700448 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700449 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700450 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700451 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700452 } else {
453 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700454 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700455 } else {
456 mat4 true3dMat;
457 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700458 properties().getPivotX() + properties().getTranslationX(),
459 properties().getPivotY() + properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700460 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700461 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
462 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
463 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
464 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
465 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700466
467 matrix.multiply(true3dMat);
468 }
469 }
470 }
471}
472
473/**
474 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
475 *
476 * This should be called before a call to defer() or drawDisplayList()
477 *
478 * Each DisplayList that serves as a 3d root builds its list of composited children,
479 * which are flagged to not draw in the standard draw loop.
480 */
481void RenderNode::computeOrdering() {
482 ATRACE_CALL();
483 mProjectedNodes.clear();
484
485 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
486 // transform properties are applied correctly to top level children
Chris Craik003cc3d2015-10-16 10:24:55 -0700487 if (mDisplayList == nullptr) return;
Chris Craikb36af872015-10-16 14:23:12 -0700488 for (unsigned int i = 0; i < mDisplayList->getChildren().size(); i++) {
Chris Craik5e00c7c2016-07-06 16:10:09 -0700489 RenderNodeOp* childOp = mDisplayList->getChildren()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700490 childOp->renderNode->computeOrderingImpl(childOp, &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700491 }
492}
493
494void RenderNode::computeOrderingImpl(
Chris Craik5e00c7c2016-07-06 16:10:09 -0700495 RenderNodeOp* opState,
496 std::vector<RenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700497 const mat4* transformFromProjectionSurface) {
498 mProjectedNodes.clear();
Chris Craik003cc3d2015-10-16 10:24:55 -0700499 if (mDisplayList == nullptr || mDisplayList->isEmpty()) return;
John Reck113e0822014-03-18 09:22:59 -0700500
501 // TODO: should avoid this calculation in most cases
502 // TODO: just calculate single matrix, down to all leaf composited elements
503 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
Chris Craik8d1f2122015-11-24 16:40:09 -0800504 localTransformFromProjectionSurface.multiply(opState->localMatrix);
John Reck113e0822014-03-18 09:22:59 -0700505
John Reckd0a0b2a2014-03-20 16:28:56 -0700506 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700507 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
Chris Craik8d1f2122015-11-24 16:40:09 -0800508 opState->skipInOrderDraw = true;
509 opState->transformFromCompositingAncestor = localTransformFromProjectionSurface;
John Reck272a6852015-07-29 16:48:58 -0700510 compositedChildrenOfProjectionSurface->push_back(opState);
John Reck113e0822014-03-18 09:22:59 -0700511 } else {
512 // standard in order draw
Chris Craik8d1f2122015-11-24 16:40:09 -0800513 opState->skipInOrderDraw = false;
John Reck113e0822014-03-18 09:22:59 -0700514 }
515
Chris Craikb36af872015-10-16 14:23:12 -0700516 if (mDisplayList->getChildren().size() > 0) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700517 const bool isProjectionReceiver = mDisplayList->projectionReceiveIndex >= 0;
John Reck113e0822014-03-18 09:22:59 -0700518 bool haveAppliedPropertiesToProjection = false;
Chris Craikb36af872015-10-16 14:23:12 -0700519 for (unsigned int i = 0; i < mDisplayList->getChildren().size(); i++) {
Chris Craik5e00c7c2016-07-06 16:10:09 -0700520 RenderNodeOp* childOp = mDisplayList->getChildren()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700521 RenderNode* child = childOp->renderNode;
John Reck113e0822014-03-18 09:22:59 -0700522
Chris Craik5e00c7c2016-07-06 16:10:09 -0700523 std::vector<RenderNodeOp*>* projectionChildren = nullptr;
Chris Craikd41c4d82015-01-05 15:51:13 -0800524 const mat4* projectionTransform = nullptr;
John Reckd0a0b2a2014-03-20 16:28:56 -0700525 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
Chris Craikbf72eb82015-06-08 11:30:44 -0700526 // if receiving projections, collect projecting descendant
John Reck113e0822014-03-18 09:22:59 -0700527
Chris Craikbf72eb82015-06-08 11:30:44 -0700528 // Note that if a direct descendant is projecting backwards, we pass its
529 // grandparent projection collection, since it shouldn't project onto its
John Reck113e0822014-03-18 09:22:59 -0700530 // parent, where it will already be drawing.
531 projectionChildren = &mProjectedNodes;
532 projectionTransform = &mat4::identity();
533 } else {
534 if (!haveAppliedPropertiesToProjection) {
535 applyViewPropertyTransforms(localTransformFromProjectionSurface);
536 haveAppliedPropertiesToProjection = true;
537 }
538 projectionChildren = compositedChildrenOfProjectionSurface;
539 projectionTransform = &localTransformFromProjectionSurface;
540 }
Derek Sollenbergerf2932592015-08-13 14:59:33 -0400541 child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700542 }
543 }
John Reck113e0822014-03-18 09:22:59 -0700544}
545
John Reck113e0822014-03-18 09:22:59 -0700546} /* namespace uirenderer */
547} /* namespace android */