blob: a03ded6435213105373e7d373a5843a0a581253c [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
Chris Craika766cb22015-06-08 16:49:43 -0700191 // Functors don't correctly handle stencil usage of overdraw debugging - shove 'em in a layer.
192 bool functorsNeedLayer = Properties::debugOverdraw;
193
194 prepareTreeImpl(info, functorsNeedLayer);
John Reckf4198b72014-04-09 17:00:04 -0700195}
196
John Reck68bfe0a2014-06-24 15:34:58 -0700197void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
198 mAnimatorManager.addAnimator(animator);
199}
200
Doris Liu8b083202016-02-19 21:46:06 +0000201void RenderNode::removeAnimator(const sp<BaseRenderNodeAnimator>& animator) {
202 mAnimatorManager.removeAnimator(animator);
203}
204
John Recke4267ea2014-06-03 15:53:15 -0700205void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700206 if (isRenderable()) {
John Reck293e8682014-06-17 10:34:02 -0700207 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700208 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
209 } else {
210 // Hope this is big enough?
211 // TODO: Get this from the display list ops or something
John Reckc1288232015-08-12 13:39:11 -0700212 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
John Recka447d292014-06-11 18:39:44 -0700213 }
John Recke4267ea2014-06-03 15:53:15 -0700214 }
215}
216
John Recka7c2ea22014-08-08 13:21:00 -0700217void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700218 LayerType layerType = properties().effectiveLayerType();
Chris Craik182952f2015-03-09 14:17:29 -0700219 if (CC_UNLIKELY(layerType == LayerType::RenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700220 // Damage applied so far needs to affect our parent, but does not require
221 // the layer to be updated. So we pop/push here to clear out the current
222 // damage and get a clean state for display list or children updates to
223 // affect, which will require the layer to be updated
224 info.damageAccumulator->popTransform();
225 info.damageAccumulator->pushTransform(this);
226 if (dirtyMask & DISPLAY_LIST) {
227 damageSelf(info);
228 }
John Reck25fbb3f2014-06-12 13:46:45 -0700229 }
230}
231
232void RenderNode::pushLayerUpdate(TreeInfo& info) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700233 LayerType layerType = properties().effectiveLayerType();
John Reck25fbb3f2014-06-12 13:46:45 -0700234 // If we are not a layer OR we cannot be rendered (eg, view was detached)
235 // we need to destroy any Layers we may have had previously
Chris Craike3e481d2016-07-11 12:20:51 -0700236 if (CC_LIKELY(layerType != LayerType::RenderLayer)
237 || CC_UNLIKELY(!isRenderable())
238 || CC_UNLIKELY(properties().getWidth() == 0)
239 || CC_UNLIKELY(properties().getHeight() == 0)) {
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
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400246 if(info.canvasContext.createOrUpdateLayer(this, *info.damageAccumulator)) {
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 Reck0e89e2b2014-10-31 14:49:06 -0700251 Caches::getInstance().dumpMemoryUsage();
John Reckc25e5062014-06-18 14:21:29 -0700252 if (info.errorHandler) {
John Reck77c40102015-10-26 15:49:47 -0700253 std::ostringstream err;
254 err << "Unable to create layer for " << getName();
Chris Craik76caecf2015-11-02 19:17:45 -0800255 const int maxTextureSize = Caches::getInstance().maxTextureSize;
John Reck77c40102015-10-26 15:49:47 -0700256 if (getWidth() > maxTextureSize || getHeight() > maxTextureSize) {
257 err << ", size " << getWidth() << "x" << getHeight()
258 << " exceeds max size " << maxTextureSize;
259 } else {
260 err << ", see logcat for more info";
261 }
262 info.errorHandler->onError(err.str());
John Reckc25e5062014-06-18 14:21:29 -0700263 }
264 return;
265 }
266
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400267 SkRect dirty;
268 info.damageAccumulator->peekAtDirty(&dirty);
Chris Craik0b7e8242015-10-28 16:50:44 -0700269 info.layerUpdateQueue->enqueueLayerWithDamage(this, dirty);
John Reck998a6d82014-08-28 15:35:53 -0700270
Chris Craike2e53a72015-10-28 15:55:40 -0700271 // There might be prefetched layers that need to be accounted for.
272 // That might be us, so tell CanvasContext that this layer is in the
273 // tree and should not be destroyed.
274 info.canvasContext.markLayerInUse(this);
John Reck25fbb3f2014-06-12 13:46:45 -0700275}
276
Chris Craika766cb22015-06-08 16:49:43 -0700277/**
278 * Traverse down the the draw tree to prepare for a frame.
279 *
280 * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven
281 *
282 * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the
283 * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer.
284 */
285void RenderNode::prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer) {
John Recka447d292014-06-11 18:39:44 -0700286 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700287
John Reckdcba6722014-07-08 13:59:49 -0700288 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700289 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700290 }
John Reck9eb9f6f2014-08-21 11:23:05 -0700291 uint32_t animatorDirtyMask = 0;
292 if (CC_LIKELY(info.runAnimations)) {
293 animatorDirtyMask = mAnimatorManager.animate(info);
294 }
Chris Craika766cb22015-06-08 16:49:43 -0700295
John Reck3f725f02015-06-16 10:29:31 -0700296 bool willHaveFunctor = false;
Chris Craik003cc3d2015-10-16 10:24:55 -0700297 if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400298 willHaveFunctor = mStagingDisplayList->hasFunctor();
Chris Craik003cc3d2015-10-16 10:24:55 -0700299 } else if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400300 willHaveFunctor = mDisplayList->hasFunctor();
John Reck3f725f02015-06-16 10:29:31 -0700301 }
Chris Craika766cb22015-06-08 16:49:43 -0700302 bool childFunctorsNeedLayer = mProperties.prepareForFunctorPresence(
303 willHaveFunctor, functorsNeedLayer);
304
John Reckf6481082016-02-02 15:18:23 -0800305 if (CC_UNLIKELY(mPositionListener.get())) {
306 mPositionListener->onPositionUpdated(*this, info);
307 }
308
John Recka7c2ea22014-08-08 13:21:00 -0700309 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700310 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700311 pushStagingDisplayListChanges(info);
312 }
John Reck25fbb3f2014-06-12 13:46:45 -0700313
Doris Liu07c056d2016-06-13 12:52:44 -0700314 if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400315 info.out.hasFunctors |= mDisplayList->hasFunctor();
316 bool isDirty = mDisplayList->prepareListAndChildren(info, childFunctorsNeedLayer,
317 [](RenderNode* child, TreeInfo& info, bool functorsNeedLayer) {
318 child->prepareTreeImpl(info, functorsNeedLayer);
319 });
320 if (isDirty) {
321 damageSelf(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700322 }
Doris Liu718cd3e2016-05-17 16:50:31 -0700323 }
Doris Liub51b2862016-08-01 19:56:47 -0700324 pushLayerUpdate(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700325
John Recka447d292014-06-11 18:39:44 -0700326 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700327}
328
Chris Craikb565df12015-10-05 13:00:52 -0700329void RenderNode::syncProperties() {
330 mProperties = mStagingProperties;
331}
332
John Reck25fbb3f2014-06-12 13:46:45 -0700333void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reckff941dc2014-05-14 16:34:14 -0700334 // Push the animators first so that setupStartValueIfNecessary() is called
335 // before properties() is trampled by stagingProperties(), as they are
336 // required by some animators.
John Reck9eb9f6f2014-08-21 11:23:05 -0700337 if (CC_LIKELY(info.runAnimations)) {
John Reck119907c2014-08-14 09:02:01 -0700338 mAnimatorManager.pushStaging();
John Reck9eb9f6f2014-08-21 11:23:05 -0700339 }
John Reckff941dc2014-05-14 16:34:14 -0700340 if (mDirtyPropertyFields) {
341 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700342 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700343 info.damageAccumulator->popTransform();
Chris Craikb565df12015-10-05 13:00:52 -0700344 syncProperties();
John Recke4267ea2014-06-03 15:53:15 -0700345 // We could try to be clever and only re-damage if the matrix changed.
346 // However, we don't need to worry about that. The cost of over-damaging
347 // here is only going to be a single additional map rect of this node
348 // plus a rect join(). The parent's transform (and up) will only be
349 // performed once.
John Recka447d292014-06-11 18:39:44 -0700350 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700351 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700352 }
John Reck25fbb3f2014-06-12 13:46:45 -0700353}
354
John Reckaa6e84f2016-06-16 15:36:13 -0700355void RenderNode::syncDisplayList(TreeInfo* info) {
Chris Craikb565df12015-10-05 13:00:52 -0700356 // Make sure we inc first so that we don't fluctuate between 0 and 1,
357 // which would thrash the layer cache
Chris Craik003cc3d2015-10-16 10:24:55 -0700358 if (mStagingDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400359 mStagingDisplayList->updateChildren([](RenderNode* child) {
360 child->incParentRefCount();
361 });
Chris Craikb565df12015-10-05 13:00:52 -0700362 }
John Reckaa6e84f2016-06-16 15:36:13 -0700363 deleteDisplayList(info ? info->observer : nullptr, info);
Chris Craik003cc3d2015-10-16 10:24:55 -0700364 mDisplayList = mStagingDisplayList;
365 mStagingDisplayList = nullptr;
366 if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400367 mDisplayList->syncContents();
Chris Craikb565df12015-10-05 13:00:52 -0700368 }
369}
370
John Reck25fbb3f2014-06-12 13:46:45 -0700371void RenderNode::pushStagingDisplayListChanges(TreeInfo& info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700372 if (mNeedsDisplayListSync) {
373 mNeedsDisplayListSync = false;
John Reck5c9d7172014-10-22 11:32:27 -0700374 // Damage with the old display list first then the new one to catch any
375 // changes in isRenderable or, in the future, bounds
376 damageSelf(info);
John Reckaa6e84f2016-06-16 15:36:13 -0700377 syncDisplayList(&info);
John Recke4267ea2014-06-03 15:53:15 -0700378 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700379 }
John Reck8de65a82014-04-09 15:23:38 -0700380}
381
John Reckaa6e84f2016-06-16 15:36:13 -0700382void RenderNode::deleteDisplayList(TreeObserver* observer, TreeInfo* info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700383 if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400384 mDisplayList->updateChildren([observer, info](RenderNode* child) {
385 child->decParentRefCount(observer, info);
386 });
387 if (!mDisplayList->reuseDisplayList(this, info ? &info->canvasContext : nullptr)) {
388 delete mDisplayList;
John Reckdcba6722014-07-08 13:59:49 -0700389 }
390 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700391 mDisplayList = nullptr;
John Reckdcba6722014-07-08 13:59:49 -0700392}
393
John Reckaa6e84f2016-06-16 15:36:13 -0700394void RenderNode::destroyHardwareResources(TreeObserver* observer, TreeInfo* info) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400395 if (hasLayer()) {
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400396 renderthread::CanvasContext::destroyLayer(this);
John Reckdcba6722014-07-08 13:59:49 -0700397 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700398 if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400399 mDisplayList->updateChildren([observer, info](RenderNode* child) {
400 child->destroyHardwareResources(observer, info);
401 });
Chris Craik003cc3d2015-10-16 10:24:55 -0700402 if (mNeedsDisplayListSync) {
John Reckdcba6722014-07-08 13:59:49 -0700403 // Next prepare tree we are going to push a new display list, so we can
404 // drop our current one now
John Reckaa6e84f2016-06-16 15:36:13 -0700405 deleteDisplayList(observer, info);
John Reckdcba6722014-07-08 13:59:49 -0700406 }
407 }
408}
409
John Reckaa6e84f2016-06-16 15:36:13 -0700410void RenderNode::decParentRefCount(TreeObserver* observer, TreeInfo* info) {
John Reckdcba6722014-07-08 13:59:49 -0700411 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
412 mParentCount--;
413 if (!mParentCount) {
John Reck44b49f02016-03-25 14:29:48 -0700414 if (observer) {
415 observer->onMaybeRemovedFromTree(this);
416 }
John Reckaa6e84f2016-06-16 15:36:13 -0700417 if (CC_UNLIKELY(mPositionListener.get())) {
418 mPositionListener->onPositionLost(*this, info);
419 }
John Reckdcba6722014-07-08 13:59:49 -0700420 // If a child of ours is being attached to our parent then this will incorrectly
421 // destroy its hardware resources. However, this situation is highly unlikely
422 // and the failure is "just" that the layer is re-created, so this should
423 // be safe enough
John Reckaa6e84f2016-06-16 15:36:13 -0700424 destroyHardwareResources(observer, info);
John Reckdcba6722014-07-08 13:59:49 -0700425 }
426}
427
John Reck113e0822014-03-18 09:22:59 -0700428/**
429 * Apply property-based transformations to input matrix
430 *
431 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
432 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
433 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700434void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700435 if (properties().getLeft() != 0 || properties().getTop() != 0) {
436 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700437 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700438 if (properties().getStaticMatrix()) {
439 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700440 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700441 } else if (properties().getAnimationMatrix()) {
442 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700443 matrix.multiply(anim);
444 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700445
Chris Craikcc39e162014-04-25 18:34:11 -0700446 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700447 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700448 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700449 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700450 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700451 } else {
452 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700453 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700454 } else {
455 mat4 true3dMat;
456 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700457 properties().getPivotX() + properties().getTranslationX(),
458 properties().getPivotY() + properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700459 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700460 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
461 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
462 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
463 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
464 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700465
466 matrix.multiply(true3dMat);
467 }
468 }
469 }
470}
471
472/**
473 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
474 *
475 * This should be called before a call to defer() or drawDisplayList()
476 *
477 * Each DisplayList that serves as a 3d root builds its list of composited children,
478 * which are flagged to not draw in the standard draw loop.
479 */
480void RenderNode::computeOrdering() {
481 ATRACE_CALL();
482 mProjectedNodes.clear();
483
484 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
485 // transform properties are applied correctly to top level children
Chris Craik003cc3d2015-10-16 10:24:55 -0700486 if (mDisplayList == nullptr) return;
Chris Craikb36af872015-10-16 14:23:12 -0700487 for (unsigned int i = 0; i < mDisplayList->getChildren().size(); i++) {
Chris Craik5e00c7c2016-07-06 16:10:09 -0700488 RenderNodeOp* childOp = mDisplayList->getChildren()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700489 childOp->renderNode->computeOrderingImpl(childOp, &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700490 }
491}
492
493void RenderNode::computeOrderingImpl(
Chris Craik5e00c7c2016-07-06 16:10:09 -0700494 RenderNodeOp* opState,
495 std::vector<RenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700496 const mat4* transformFromProjectionSurface) {
497 mProjectedNodes.clear();
Chris Craik003cc3d2015-10-16 10:24:55 -0700498 if (mDisplayList == nullptr || mDisplayList->isEmpty()) return;
John Reck113e0822014-03-18 09:22:59 -0700499
500 // TODO: should avoid this calculation in most cases
501 // TODO: just calculate single matrix, down to all leaf composited elements
502 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
Chris Craik8d1f2122015-11-24 16:40:09 -0800503 localTransformFromProjectionSurface.multiply(opState->localMatrix);
John Reck113e0822014-03-18 09:22:59 -0700504
John Reckd0a0b2a2014-03-20 16:28:56 -0700505 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700506 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
Chris Craik8d1f2122015-11-24 16:40:09 -0800507 opState->skipInOrderDraw = true;
508 opState->transformFromCompositingAncestor = localTransformFromProjectionSurface;
John Reck272a6852015-07-29 16:48:58 -0700509 compositedChildrenOfProjectionSurface->push_back(opState);
John Reck113e0822014-03-18 09:22:59 -0700510 } else {
511 // standard in order draw
Chris Craik8d1f2122015-11-24 16:40:09 -0800512 opState->skipInOrderDraw = false;
John Reck113e0822014-03-18 09:22:59 -0700513 }
514
Chris Craikb36af872015-10-16 14:23:12 -0700515 if (mDisplayList->getChildren().size() > 0) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700516 const bool isProjectionReceiver = mDisplayList->projectionReceiveIndex >= 0;
John Reck113e0822014-03-18 09:22:59 -0700517 bool haveAppliedPropertiesToProjection = false;
Chris Craikb36af872015-10-16 14:23:12 -0700518 for (unsigned int i = 0; i < mDisplayList->getChildren().size(); i++) {
Chris Craik5e00c7c2016-07-06 16:10:09 -0700519 RenderNodeOp* childOp = mDisplayList->getChildren()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700520 RenderNode* child = childOp->renderNode;
John Reck113e0822014-03-18 09:22:59 -0700521
Chris Craik5e00c7c2016-07-06 16:10:09 -0700522 std::vector<RenderNodeOp*>* projectionChildren = nullptr;
Chris Craikd41c4d82015-01-05 15:51:13 -0800523 const mat4* projectionTransform = nullptr;
John Reckd0a0b2a2014-03-20 16:28:56 -0700524 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
Chris Craikbf72eb82015-06-08 11:30:44 -0700525 // if receiving projections, collect projecting descendant
John Reck113e0822014-03-18 09:22:59 -0700526
Chris Craikbf72eb82015-06-08 11:30:44 -0700527 // Note that if a direct descendant is projecting backwards, we pass its
528 // grandparent projection collection, since it shouldn't project onto its
John Reck113e0822014-03-18 09:22:59 -0700529 // parent, where it will already be drawing.
530 projectionChildren = &mProjectedNodes;
531 projectionTransform = &mat4::identity();
532 } else {
533 if (!haveAppliedPropertiesToProjection) {
534 applyViewPropertyTransforms(localTransformFromProjectionSurface);
535 haveAppliedPropertiesToProjection = true;
536 }
537 projectionChildren = compositedChildrenOfProjectionSurface;
538 projectionTransform = &localTransformFromProjectionSurface;
539 }
Derek Sollenbergerf2932592015-08-13 14:59:33 -0400540 child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700541 }
542 }
John Reck113e0822014-03-18 09:22:59 -0700543}
544
John Reck113e0822014-03-18 09:22:59 -0700545} /* namespace uirenderer */
546} /* namespace android */