blob: 8c36ab5d94ddbba048116e19b113846c47e3a738 [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;
Chris Craik0b7e8242015-10-28 16:50:44 -070054 LOG_ALWAYS_FATAL_IF(mLayer, "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" : "")
84 << (mLayer != nullptr ? ", on HW Layer" : "")
85 << ")" << 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)) {
John Reck25fbb3f2014-06-12 13:46:45 -0700240 if (CC_UNLIKELY(mLayer)) {
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
John Reckc25e5062014-06-18 14:21:29 -0700250 if (!mLayer) {
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) {
Chris Craikb36af872015-10-16 14:23:12 -0700298 willHaveFunctor = !mStagingDisplayList->getFunctors().empty();
Chris Craik003cc3d2015-10-16 10:24:55 -0700299 } else if (mDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700300 willHaveFunctor = !mDisplayList->getFunctors().empty();
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 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700313 prepareSubTree(info, childFunctorsNeedLayer, mDisplayList);
John Reck25fbb3f2014-06-12 13:46:45 -0700314
Doris Liu07c056d2016-06-13 12:52:44 -0700315 if (mDisplayList) {
316 for (auto& vectorDrawable : mDisplayList->getVectorDrawables()) {
317 // If any vector drawable in the display list needs update, damage the node.
318 if (vectorDrawable->isDirty()) {
319 damageSelf(info);
320 }
321 vectorDrawable->setPropertyChangeWillBeConsumed(true);
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) {
Chris Craikb36af872015-10-16 14:23:12 -0700359 for (auto&& child : mStagingDisplayList->getChildren()) {
Chris Craikb565df12015-10-05 13:00:52 -0700360 child->renderNode->incParentRefCount();
361 }
362 }
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) {
John Reckcd1c3eb2016-04-14 10:38:54 -0700367 for (auto& iter : mDisplayList->getFunctors()) {
368 (*iter.functor)(DrawGlInfo::kModeSync, nullptr);
Chris Craikb565df12015-10-05 13:00:52 -0700369 }
Doris Liu718cd3e2016-05-17 16:50:31 -0700370 for (auto& vectorDrawable : mDisplayList->getVectorDrawables()) {
371 vectorDrawable->syncProperties();
Doris Liu1d8e1942016-03-02 15:16:28 -0800372 }
Chris Craikb565df12015-10-05 13:00:52 -0700373 }
374}
375
John Reck25fbb3f2014-06-12 13:46:45 -0700376void RenderNode::pushStagingDisplayListChanges(TreeInfo& info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700377 if (mNeedsDisplayListSync) {
378 mNeedsDisplayListSync = false;
John Reck5c9d7172014-10-22 11:32:27 -0700379 // Damage with the old display list first then the new one to catch any
380 // changes in isRenderable or, in the future, bounds
381 damageSelf(info);
John Reckaa6e84f2016-06-16 15:36:13 -0700382 syncDisplayList(&info);
John Recke4267ea2014-06-03 15:53:15 -0700383 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700384 }
John Reck8de65a82014-04-09 15:23:38 -0700385}
386
John Reckaa6e84f2016-06-16 15:36:13 -0700387void RenderNode::deleteDisplayList(TreeObserver* observer, TreeInfo* info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700388 if (mDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700389 for (auto&& child : mDisplayList->getChildren()) {
John Reckaa6e84f2016-06-16 15:36:13 -0700390 child->renderNode->decParentRefCount(observer, info);
John Reckdcba6722014-07-08 13:59:49 -0700391 }
392 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700393 delete mDisplayList;
394 mDisplayList = nullptr;
John Reckdcba6722014-07-08 13:59:49 -0700395}
396
Chris Craik003cc3d2015-10-16 10:24:55 -0700397void RenderNode::prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayList* subtree) {
John Reck8de65a82014-04-09 15:23:38 -0700398 if (subtree) {
John Reck860d1552014-04-11 19:15:05 -0700399 TextureCache& cache = Caches::getInstance().textureCache;
Chris Craikb36af872015-10-16 14:23:12 -0700400 info.out.hasFunctors |= subtree->getFunctors().size();
401 for (auto&& bitmapResource : subtree->getBitmapResources()) {
Chris Craike2e53a72015-10-28 15:55:40 -0700402 void* ownerToken = &info.canvasContext;
403 info.prepareTextures = cache.prefetchAndMarkInUse(ownerToken, bitmapResource);
John Reckf4198b72014-04-09 17:00:04 -0700404 }
Chris Craikb36af872015-10-16 14:23:12 -0700405 for (auto&& op : subtree->getChildren()) {
Chris Craikb565df12015-10-05 13:00:52 -0700406 RenderNode* childNode = op->renderNode;
Chris Craikb565df12015-10-05 13:00:52 -0700407 info.damageAccumulator->pushTransform(&op->localMatrix);
408 bool childFunctorsNeedLayer = functorsNeedLayer; // TODO! || op->mRecordedWithPotentialStencilClip;
Chris Craika766cb22015-06-08 16:49:43 -0700409 childNode->prepareTreeImpl(info, childFunctorsNeedLayer);
John Recka447d292014-06-11 18:39:44 -0700410 info.damageAccumulator->popTransform();
John Reck5bf11bb2014-03-25 10:22:09 -0700411 }
John Reck113e0822014-03-18 09:22:59 -0700412 }
413}
414
John Reckaa6e84f2016-06-16 15:36:13 -0700415void RenderNode::destroyHardwareResources(TreeObserver* observer, TreeInfo* info) {
John Reckdcba6722014-07-08 13:59:49 -0700416 if (mLayer) {
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400417 renderthread::CanvasContext::destroyLayer(this);
John Reckdcba6722014-07-08 13:59:49 -0700418 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700419 if (mDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700420 for (auto&& child : mDisplayList->getChildren()) {
John Reckaa6e84f2016-06-16 15:36:13 -0700421 child->renderNode->destroyHardwareResources(observer, info);
John Reckdcba6722014-07-08 13:59:49 -0700422 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700423 if (mNeedsDisplayListSync) {
John Reckdcba6722014-07-08 13:59:49 -0700424 // Next prepare tree we are going to push a new display list, so we can
425 // drop our current one now
John Reckaa6e84f2016-06-16 15:36:13 -0700426 deleteDisplayList(observer, info);
John Reckdcba6722014-07-08 13:59:49 -0700427 }
428 }
429}
430
John Reckaa6e84f2016-06-16 15:36:13 -0700431void RenderNode::decParentRefCount(TreeObserver* observer, TreeInfo* info) {
John Reckdcba6722014-07-08 13:59:49 -0700432 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
433 mParentCount--;
434 if (!mParentCount) {
John Reck44b49f02016-03-25 14:29:48 -0700435 if (observer) {
436 observer->onMaybeRemovedFromTree(this);
437 }
John Reckaa6e84f2016-06-16 15:36:13 -0700438 if (CC_UNLIKELY(mPositionListener.get())) {
439 mPositionListener->onPositionLost(*this, info);
440 }
John Reckdcba6722014-07-08 13:59:49 -0700441 // If a child of ours is being attached to our parent then this will incorrectly
442 // destroy its hardware resources. However, this situation is highly unlikely
443 // and the failure is "just" that the layer is re-created, so this should
444 // be safe enough
John Reckaa6e84f2016-06-16 15:36:13 -0700445 destroyHardwareResources(observer, info);
John Reckdcba6722014-07-08 13:59:49 -0700446 }
447}
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 */