blob: 36a747519c37312b0f28459fcdb300a14661bee2 [file] [log] [blame]
John Reck113e0822014-03-18 09:22:59 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
John Reck113e0822014-03-18 09:22:59 -070017#include "RenderNode.h"
18
Chris Craik5e00c7c2016-07-06 16:10:09 -070019#include "BakedOpRenderer.h"
John Recke4267ea2014-06-03 15:53:15 -070020#include "DamageAccumulator.h"
John Reck113e0822014-03-18 09:22:59 -070021#include "Debug.h"
Chris Craik5e00c7c2016-07-06 16:10:09 -070022#include "RecordedOp.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040023#include "TreeInfo.h"
John Reck2de950d2017-01-25 10:58:30 -080024#include "utils/FatVector.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
Derek Sollenberger579317d42017-08-29 16:33:49 -040035#include <SkPathOps.h>
John Reck77c40102015-10-26 15:49:47 -070036#include <algorithm>
37#include <sstream>
38#include <string>
39
John Reck113e0822014-03-18 09:22:59 -070040namespace android {
41namespace uirenderer {
42
John Reck2de950d2017-01-25 10:58:30 -080043// Used for tree mutations that are purely destructive.
44// Generic tree mutations should use MarkAndSweepObserver instead
45class ImmediateRemoved : public TreeObserver {
46public:
47 explicit ImmediateRemoved(TreeInfo* info) : mTreeInfo(info) {}
48
49 void onMaybeRemovedFromTree(RenderNode* node) override {
50 node->onRemovedFromTree(mTreeInfo);
51 }
52
53private:
54 TreeInfo* mTreeInfo;
55};
56
John Reck8de65a82014-04-09 15:23:38 -070057RenderNode::RenderNode()
John Reckff941dc2014-05-14 16:34:14 -070058 : mDirtyPropertyFields(0)
Chris Craik003cc3d2015-10-16 10:24:55 -070059 , mNeedsDisplayListSync(false)
60 , mDisplayList(nullptr)
61 , mStagingDisplayList(nullptr)
John Reck68bfe0a2014-06-24 15:34:58 -070062 , mAnimatorManager(*this)
John Reckdcba6722014-07-08 13:59:49 -070063 , mParentCount(0) {
John Reck113e0822014-03-18 09:22:59 -070064}
65
66RenderNode::~RenderNode() {
John Reck2de950d2017-01-25 10:58:30 -080067 ImmediateRemoved observer(nullptr);
68 deleteDisplayList(observer);
Chris Craik003cc3d2015-10-16 10:24:55 -070069 delete mStagingDisplayList;
Derek Sollenberger0df62092016-09-27 16:04:42 -040070 LOG_ALWAYS_FATAL_IF(hasLayer(), "layer missed detachment!");
John Reck113e0822014-03-18 09:22:59 -070071}
72
John Reck2de950d2017-01-25 10:58:30 -080073void RenderNode::setStagingDisplayList(DisplayList* displayList) {
74 mValid = (displayList != nullptr);
Chris Craik003cc3d2015-10-16 10:24:55 -070075 mNeedsDisplayListSync = true;
76 delete mStagingDisplayList;
77 mStagingDisplayList = displayList;
John Reck113e0822014-03-18 09:22:59 -070078}
79
80/**
81 * This function is a simplified version of replay(), where we simply retrieve and log the
82 * display list. This function should remain in sync with the replay() function.
83 */
sergeyvc3849aa2016-08-08 13:22:06 -070084void RenderNode::output() {
85 LogcatStream strout;
86 strout << "Root";
87 output(strout, 0);
88}
89
90void RenderNode::output(std::ostream& output, uint32_t level) {
91 output << " (" << getName() << " " << this
92 << (MathUtils::isZero(properties().getAlpha()) ? ", zero alpha" : "")
93 << (properties().hasShadow() ? ", casting shadow" : "")
94 << (isRenderable() ? "" : ", empty")
95 << (properties().getProjectBackwards() ? ", projected" : "")
Derek Sollenberger0df62092016-09-27 16:04:42 -040096 << (hasLayer() ? ", on HW Layer" : "")
sergeyvc3849aa2016-08-08 13:22:06 -070097 << ")" << std::endl;
98
99 properties().debugOutputProperties(output, level + 1);
Chris Craik91eff222016-02-22 13:39:33 -0800100
101 if (mDisplayList) {
Stan Ilievd2172372017-02-09 16:59:27 -0500102 mDisplayList->output(output, level);
Chris Craik91eff222016-02-22 13:39:33 -0800103 }
sergeyvc3849aa2016-08-08 13:22:06 -0700104 output << std::string(level * 2, ' ') << "/RenderNode(" << getName() << " " << this << ")";
105 output << std::endl;
Chris Craik91eff222016-02-22 13:39:33 -0800106}
John Reck113e0822014-03-18 09:22:59 -0700107
John Recke248bd12015-08-05 13:53:53 -0700108void RenderNode::copyTo(proto::RenderNode *pnode) {
109 pnode->set_id(static_cast<uint64_t>(
110 reinterpret_cast<uintptr_t>(this)));
111 pnode->set_name(mName.string(), mName.length());
112
113 proto::RenderProperties* pprops = pnode->mutable_properties();
114 pprops->set_left(properties().getLeft());
115 pprops->set_top(properties().getTop());
116 pprops->set_right(properties().getRight());
117 pprops->set_bottom(properties().getBottom());
118 pprops->set_clip_flags(properties().getClippingFlags());
119 pprops->set_alpha(properties().getAlpha());
120 pprops->set_translation_x(properties().getTranslationX());
121 pprops->set_translation_y(properties().getTranslationY());
122 pprops->set_translation_z(properties().getTranslationZ());
123 pprops->set_elevation(properties().getElevation());
124 pprops->set_rotation(properties().getRotation());
125 pprops->set_rotation_x(properties().getRotationX());
126 pprops->set_rotation_y(properties().getRotationY());
127 pprops->set_scale_x(properties().getScaleX());
128 pprops->set_scale_y(properties().getScaleY());
129 pprops->set_pivot_x(properties().getPivotX());
130 pprops->set_pivot_y(properties().getPivotY());
131 pprops->set_has_overlapping_rendering(properties().getHasOverlappingRendering());
132 pprops->set_pivot_explicitly_set(properties().isPivotExplicitlySet());
133 pprops->set_project_backwards(properties().getProjectBackwards());
134 pprops->set_projection_receiver(properties().isProjectionReceiver());
135 set(pprops->mutable_clip_bounds(), properties().getClipBounds());
136
137 const Outline& outline = properties().getOutline();
138 if (outline.getType() != Outline::Type::None) {
139 proto::Outline* poutline = pprops->mutable_outline();
140 poutline->clear_path();
141 if (outline.getType() == Outline::Type::Empty) {
142 poutline->set_type(proto::Outline_Type_Empty);
143 } else if (outline.getType() == Outline::Type::ConvexPath) {
144 poutline->set_type(proto::Outline_Type_ConvexPath);
145 if (const SkPath* path = outline.getPath()) {
146 set(poutline->mutable_path(), *path);
147 }
148 } else if (outline.getType() == Outline::Type::RoundRect) {
149 poutline->set_type(proto::Outline_Type_RoundRect);
150 } else {
151 ALOGW("Uknown outline type! %d", static_cast<int>(outline.getType()));
152 poutline->set_type(proto::Outline_Type_None);
153 }
154 poutline->set_should_clip(outline.getShouldClip());
155 poutline->set_alpha(outline.getAlpha());
156 poutline->set_radius(outline.getRadius());
157 set(poutline->mutable_bounds(), outline.getBounds());
158 } else {
159 pprops->clear_outline();
160 }
161
162 const RevealClip& revealClip = properties().getRevealClip();
163 if (revealClip.willClip()) {
164 proto::RevealClip* prevealClip = pprops->mutable_reveal_clip();
165 prevealClip->set_x(revealClip.getX());
166 prevealClip->set_y(revealClip.getY());
167 prevealClip->set_radius(revealClip.getRadius());
168 } else {
169 pprops->clear_reveal_clip();
170 }
171
172 pnode->clear_children();
Chris Craik003cc3d2015-10-16 10:24:55 -0700173 if (mDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700174 for (auto&& child : mDisplayList->getChildren()) {
Chris Craikb565df12015-10-05 13:00:52 -0700175 child->renderNode->copyTo(pnode->add_children());
John Recke248bd12015-08-05 13:53:53 -0700176 }
177 }
178}
179
John Reckfe5e7b72014-05-23 17:42:28 -0700180int RenderNode::getDebugSize() {
181 int size = sizeof(RenderNode);
Chris Craik003cc3d2015-10-16 10:24:55 -0700182 if (mStagingDisplayList) {
183 size += mStagingDisplayList->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700184 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700185 if (mDisplayList && mDisplayList != mStagingDisplayList) {
186 size += mDisplayList->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700187 }
188 return size;
189}
190
John Reckf4198b72014-04-09 17:00:04 -0700191void RenderNode::prepareTree(TreeInfo& info) {
192 ATRACE_CALL();
Chris Craik69e5adf2014-08-14 13:34:01 -0700193 LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
John Reck2de950d2017-01-25 10:58:30 -0800194 MarkAndSweepRemoved observer(&info);
John Reckf4198b72014-04-09 17:00:04 -0700195
Matt Sarettf58cc922016-11-14 18:33:38 -0500196 // The OpenGL renderer reserves the stencil buffer for overdraw debugging. Functors
197 // will need to be drawn in a layer.
198 bool functorsNeedLayer = Properties::debugOverdraw && !Properties::isSkiaEnabled();
Chris Craika766cb22015-06-08 16:49:43 -0700199
John Reck2de950d2017-01-25 10:58:30 -0800200 prepareTreeImpl(observer, info, functorsNeedLayer);
John Reckf4198b72014-04-09 17:00:04 -0700201}
202
John Reck68bfe0a2014-06-24 15:34:58 -0700203void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
204 mAnimatorManager.addAnimator(animator);
205}
206
Doris Liu8b083202016-02-19 21:46:06 +0000207void RenderNode::removeAnimator(const sp<BaseRenderNodeAnimator>& animator) {
208 mAnimatorManager.removeAnimator(animator);
209}
210
John Recke4267ea2014-06-03 15:53:15 -0700211void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700212 if (isRenderable()) {
John Reck293e8682014-06-17 10:34:02 -0700213 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700214 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
215 } else {
216 // Hope this is big enough?
217 // TODO: Get this from the display list ops or something
John Reckc1288232015-08-12 13:39:11 -0700218 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
John Recka447d292014-06-11 18:39:44 -0700219 }
John Recke4267ea2014-06-03 15:53:15 -0700220 }
221}
222
John Recka7c2ea22014-08-08 13:21:00 -0700223void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700224 LayerType layerType = properties().effectiveLayerType();
Chris Craik182952f2015-03-09 14:17:29 -0700225 if (CC_UNLIKELY(layerType == LayerType::RenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700226 // Damage applied so far needs to affect our parent, but does not require
227 // the layer to be updated. So we pop/push here to clear out the current
228 // damage and get a clean state for display list or children updates to
229 // affect, which will require the layer to be updated
230 info.damageAccumulator->popTransform();
231 info.damageAccumulator->pushTransform(this);
232 if (dirtyMask & DISPLAY_LIST) {
233 damageSelf(info);
234 }
John Reck25fbb3f2014-06-12 13:46:45 -0700235 }
236}
237
238void RenderNode::pushLayerUpdate(TreeInfo& info) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700239 LayerType layerType = properties().effectiveLayerType();
John Reck25fbb3f2014-06-12 13:46:45 -0700240 // If we are not a layer OR we cannot be rendered (eg, view was detached)
241 // we need to destroy any Layers we may have had previously
Chris Craike3e481d2016-07-11 12:20:51 -0700242 if (CC_LIKELY(layerType != LayerType::RenderLayer)
243 || CC_UNLIKELY(!isRenderable())
244 || CC_UNLIKELY(properties().getWidth() == 0)
John Reck679e7f12017-08-02 16:11:43 -0700245 || CC_UNLIKELY(properties().getHeight() == 0)
246 || CC_UNLIKELY(!properties().fitsOnLayer())) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400247 if (CC_UNLIKELY(hasLayer())) {
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400248 renderthread::CanvasContext::destroyLayer(this);
John Reck25fbb3f2014-06-12 13:46:45 -0700249 }
250 return;
251 }
252
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400253 if(info.canvasContext.createOrUpdateLayer(this, *info.damageAccumulator)) {
Chris Craik9fded232015-11-11 16:42:34 -0800254 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700255 }
256
Derek Sollenberger0df62092016-09-27 16:04:42 -0400257 if (!hasLayer()) {
John Reck0e89e2b2014-10-31 14:49:06 -0700258 Caches::getInstance().dumpMemoryUsage();
John Reckc25e5062014-06-18 14:21:29 -0700259 if (info.errorHandler) {
John Reck77c40102015-10-26 15:49:47 -0700260 std::ostringstream err;
261 err << "Unable to create layer for " << getName();
Chris Craik76caecf2015-11-02 19:17:45 -0800262 const int maxTextureSize = Caches::getInstance().maxTextureSize;
John Reck77c40102015-10-26 15:49:47 -0700263 if (getWidth() > maxTextureSize || getHeight() > maxTextureSize) {
264 err << ", size " << getWidth() << "x" << getHeight()
265 << " exceeds max size " << maxTextureSize;
266 } else {
267 err << ", see logcat for more info";
268 }
269 info.errorHandler->onError(err.str());
John Reckc25e5062014-06-18 14:21:29 -0700270 }
271 return;
272 }
273
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400274 SkRect dirty;
275 info.damageAccumulator->peekAtDirty(&dirty);
Chris Craik0b7e8242015-10-28 16:50:44 -0700276 info.layerUpdateQueue->enqueueLayerWithDamage(this, dirty);
John Reck998a6d82014-08-28 15:35:53 -0700277
Chris Craike2e53a72015-10-28 15:55:40 -0700278 // There might be prefetched layers that need to be accounted for.
279 // That might be us, so tell CanvasContext that this layer is in the
280 // tree and should not be destroyed.
281 info.canvasContext.markLayerInUse(this);
John Reck25fbb3f2014-06-12 13:46:45 -0700282}
283
Chris Craika766cb22015-06-08 16:49:43 -0700284/**
285 * Traverse down the the draw tree to prepare for a frame.
286 *
287 * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven
288 *
289 * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the
290 * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer.
291 */
John Reck2de950d2017-01-25 10:58:30 -0800292void RenderNode::prepareTreeImpl(TreeObserver& observer, TreeInfo& info, bool functorsNeedLayer) {
John Recka447d292014-06-11 18:39:44 -0700293 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700294
John Reckdcba6722014-07-08 13:59:49 -0700295 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700296 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700297 }
John Reck9eb9f6f2014-08-21 11:23:05 -0700298 uint32_t animatorDirtyMask = 0;
299 if (CC_LIKELY(info.runAnimations)) {
300 animatorDirtyMask = mAnimatorManager.animate(info);
301 }
Chris Craika766cb22015-06-08 16:49:43 -0700302
John Reck3f725f02015-06-16 10:29:31 -0700303 bool willHaveFunctor = false;
Chris Craik003cc3d2015-10-16 10:24:55 -0700304 if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400305 willHaveFunctor = mStagingDisplayList->hasFunctor();
Chris Craik003cc3d2015-10-16 10:24:55 -0700306 } else if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400307 willHaveFunctor = mDisplayList->hasFunctor();
John Reck3f725f02015-06-16 10:29:31 -0700308 }
Chris Craika766cb22015-06-08 16:49:43 -0700309 bool childFunctorsNeedLayer = mProperties.prepareForFunctorPresence(
310 willHaveFunctor, functorsNeedLayer);
311
John Reckf6481082016-02-02 15:18:23 -0800312 if (CC_UNLIKELY(mPositionListener.get())) {
313 mPositionListener->onPositionUpdated(*this, info);
314 }
315
John Recka7c2ea22014-08-08 13:21:00 -0700316 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700317 if (info.mode == TreeInfo::MODE_FULL) {
John Reck2de950d2017-01-25 10:58:30 -0800318 pushStagingDisplayListChanges(observer, info);
John Reck25fbb3f2014-06-12 13:46:45 -0700319 }
John Reck25fbb3f2014-06-12 13:46:45 -0700320
Doris Liu07c056d2016-06-13 12:52:44 -0700321 if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400322 info.out.hasFunctors |= mDisplayList->hasFunctor();
John Reck2de950d2017-01-25 10:58:30 -0800323 bool isDirty = mDisplayList->prepareListAndChildren(observer, info, childFunctorsNeedLayer,
324 [](RenderNode* child, TreeObserver& observer, TreeInfo& info, bool functorsNeedLayer) {
325 child->prepareTreeImpl(observer, info, functorsNeedLayer);
Derek Sollenberger0df62092016-09-27 16:04:42 -0400326 });
327 if (isDirty) {
328 damageSelf(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700329 }
Doris Liu718cd3e2016-05-17 16:50:31 -0700330 }
Doris Liub51b2862016-08-01 19:56:47 -0700331 pushLayerUpdate(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700332
John Recka447d292014-06-11 18:39:44 -0700333 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700334}
335
Chris Craikb565df12015-10-05 13:00:52 -0700336void RenderNode::syncProperties() {
337 mProperties = mStagingProperties;
338}
339
John Reck25fbb3f2014-06-12 13:46:45 -0700340void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reckff941dc2014-05-14 16:34:14 -0700341 // Push the animators first so that setupStartValueIfNecessary() is called
342 // before properties() is trampled by stagingProperties(), as they are
343 // required by some animators.
John Reck9eb9f6f2014-08-21 11:23:05 -0700344 if (CC_LIKELY(info.runAnimations)) {
John Reck119907c2014-08-14 09:02:01 -0700345 mAnimatorManager.pushStaging();
John Reck9eb9f6f2014-08-21 11:23:05 -0700346 }
John Reckff941dc2014-05-14 16:34:14 -0700347 if (mDirtyPropertyFields) {
348 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700349 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700350 info.damageAccumulator->popTransform();
Chris Craikb565df12015-10-05 13:00:52 -0700351 syncProperties();
John Recke4267ea2014-06-03 15:53:15 -0700352 // We could try to be clever and only re-damage if the matrix changed.
353 // However, we don't need to worry about that. The cost of over-damaging
354 // here is only going to be a single additional map rect of this node
355 // plus a rect join(). The parent's transform (and up) will only be
356 // performed once.
John Recka447d292014-06-11 18:39:44 -0700357 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700358 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700359 }
John Reck25fbb3f2014-06-12 13:46:45 -0700360}
361
John Reck2de950d2017-01-25 10:58:30 -0800362void RenderNode::syncDisplayList(TreeObserver& observer, TreeInfo* info) {
Chris Craikb565df12015-10-05 13:00:52 -0700363 // Make sure we inc first so that we don't fluctuate between 0 and 1,
364 // which would thrash the layer cache
Chris Craik003cc3d2015-10-16 10:24:55 -0700365 if (mStagingDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400366 mStagingDisplayList->updateChildren([](RenderNode* child) {
367 child->incParentRefCount();
368 });
Chris Craikb565df12015-10-05 13:00:52 -0700369 }
John Reck2de950d2017-01-25 10:58:30 -0800370 deleteDisplayList(observer, info);
Chris Craik003cc3d2015-10-16 10:24:55 -0700371 mDisplayList = mStagingDisplayList;
372 mStagingDisplayList = nullptr;
373 if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400374 mDisplayList->syncContents();
Chris Craikb565df12015-10-05 13:00:52 -0700375 }
376}
377
John Reck2de950d2017-01-25 10:58:30 -0800378void RenderNode::pushStagingDisplayListChanges(TreeObserver& observer, TreeInfo& info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700379 if (mNeedsDisplayListSync) {
380 mNeedsDisplayListSync = false;
John Reck5c9d7172014-10-22 11:32:27 -0700381 // Damage with the old display list first then the new one to catch any
382 // changes in isRenderable or, in the future, bounds
383 damageSelf(info);
John Reck2de950d2017-01-25 10:58:30 -0800384 syncDisplayList(observer, &info);
John Recke4267ea2014-06-03 15:53:15 -0700385 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700386 }
John Reck8de65a82014-04-09 15:23:38 -0700387}
388
John Reck2de950d2017-01-25 10:58:30 -0800389void RenderNode::deleteDisplayList(TreeObserver& observer, TreeInfo* info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700390 if (mDisplayList) {
John Reck2de950d2017-01-25 10:58:30 -0800391 mDisplayList->updateChildren([&observer, info](RenderNode* child) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400392 child->decParentRefCount(observer, info);
393 });
394 if (!mDisplayList->reuseDisplayList(this, info ? &info->canvasContext : nullptr)) {
395 delete mDisplayList;
John Reckdcba6722014-07-08 13:59:49 -0700396 }
397 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700398 mDisplayList = nullptr;
John Reckdcba6722014-07-08 13:59:49 -0700399}
400
John Reck2de950d2017-01-25 10:58:30 -0800401void RenderNode::destroyHardwareResources(TreeInfo* info) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400402 if (hasLayer()) {
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400403 renderthread::CanvasContext::destroyLayer(this);
John Reckdcba6722014-07-08 13:59:49 -0700404 }
John Reck3afd6372017-01-30 10:15:48 -0800405 setStagingDisplayList(nullptr);
406
407 ImmediateRemoved observer(info);
408 deleteDisplayList(observer, info);
John Reck2de950d2017-01-25 10:58:30 -0800409}
410
411void RenderNode::destroyLayers() {
412 if (hasLayer()) {
413 renderthread::CanvasContext::destroyLayer(this);
414 }
415 if (mDisplayList) {
416 mDisplayList->updateChildren([](RenderNode* child) {
417 child->destroyLayers();
418 });
419 }
420}
421
422void RenderNode::decParentRefCount(TreeObserver& observer, TreeInfo* info) {
423 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
424 mParentCount--;
425 if (!mParentCount) {
426 observer.onMaybeRemovedFromTree(this);
427 if (CC_UNLIKELY(mPositionListener.get())) {
428 mPositionListener->onPositionLost(*this, info);
John Reckdcba6722014-07-08 13:59:49 -0700429 }
430 }
431}
432
John Reck2de950d2017-01-25 10:58:30 -0800433void RenderNode::onRemovedFromTree(TreeInfo* info) {
434 destroyHardwareResources(info);
435}
436
437void RenderNode::clearRoot() {
438 ImmediateRemoved observer(nullptr);
439 decParentRefCount(observer);
John Reckdcba6722014-07-08 13:59:49 -0700440}
441
John Reck113e0822014-03-18 09:22:59 -0700442/**
443 * Apply property-based transformations to input matrix
444 *
445 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
446 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
447 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700448void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700449 if (properties().getLeft() != 0 || properties().getTop() != 0) {
450 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700451 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700452 if (properties().getStaticMatrix()) {
453 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700454 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700455 } else if (properties().getAnimationMatrix()) {
456 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700457 matrix.multiply(anim);
458 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700459
Chris Craikcc39e162014-04-25 18:34:11 -0700460 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700461 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700462 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700463 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700464 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700465 } else {
466 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700467 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700468 } else {
469 mat4 true3dMat;
470 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700471 properties().getPivotX() + properties().getTranslationX(),
472 properties().getPivotY() + properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700473 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700474 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
475 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
476 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
477 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
478 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700479
480 matrix.multiply(true3dMat);
481 }
482 }
483 }
484}
485
486/**
487 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
488 *
489 * This should be called before a call to defer() or drawDisplayList()
490 *
491 * Each DisplayList that serves as a 3d root builds its list of composited children,
492 * which are flagged to not draw in the standard draw loop.
493 */
494void RenderNode::computeOrdering() {
495 ATRACE_CALL();
496 mProjectedNodes.clear();
497
498 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
499 // transform properties are applied correctly to top level children
Chris Craik003cc3d2015-10-16 10:24:55 -0700500 if (mDisplayList == nullptr) return;
Chris Craikb36af872015-10-16 14:23:12 -0700501 for (unsigned int i = 0; i < mDisplayList->getChildren().size(); i++) {
Chris Craik5e00c7c2016-07-06 16:10:09 -0700502 RenderNodeOp* childOp = mDisplayList->getChildren()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700503 childOp->renderNode->computeOrderingImpl(childOp, &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700504 }
505}
506
507void RenderNode::computeOrderingImpl(
Chris Craik5e00c7c2016-07-06 16:10:09 -0700508 RenderNodeOp* opState,
509 std::vector<RenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700510 const mat4* transformFromProjectionSurface) {
511 mProjectedNodes.clear();
Chris Craik003cc3d2015-10-16 10:24:55 -0700512 if (mDisplayList == nullptr || mDisplayList->isEmpty()) return;
John Reck113e0822014-03-18 09:22:59 -0700513
514 // TODO: should avoid this calculation in most cases
515 // TODO: just calculate single matrix, down to all leaf composited elements
516 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
Chris Craik8d1f2122015-11-24 16:40:09 -0800517 localTransformFromProjectionSurface.multiply(opState->localMatrix);
John Reck113e0822014-03-18 09:22:59 -0700518
John Reckd0a0b2a2014-03-20 16:28:56 -0700519 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700520 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
Chris Craik8d1f2122015-11-24 16:40:09 -0800521 opState->skipInOrderDraw = true;
522 opState->transformFromCompositingAncestor = localTransformFromProjectionSurface;
John Reck272a6852015-07-29 16:48:58 -0700523 compositedChildrenOfProjectionSurface->push_back(opState);
John Reck113e0822014-03-18 09:22:59 -0700524 } else {
525 // standard in order draw
Chris Craik8d1f2122015-11-24 16:40:09 -0800526 opState->skipInOrderDraw = false;
John Reck113e0822014-03-18 09:22:59 -0700527 }
528
Chris Craikb36af872015-10-16 14:23:12 -0700529 if (mDisplayList->getChildren().size() > 0) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700530 const bool isProjectionReceiver = mDisplayList->projectionReceiveIndex >= 0;
John Reck113e0822014-03-18 09:22:59 -0700531 bool haveAppliedPropertiesToProjection = false;
Chris Craikb36af872015-10-16 14:23:12 -0700532 for (unsigned int i = 0; i < mDisplayList->getChildren().size(); i++) {
Chris Craik5e00c7c2016-07-06 16:10:09 -0700533 RenderNodeOp* childOp = mDisplayList->getChildren()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700534 RenderNode* child = childOp->renderNode;
John Reck113e0822014-03-18 09:22:59 -0700535
Chris Craik5e00c7c2016-07-06 16:10:09 -0700536 std::vector<RenderNodeOp*>* projectionChildren = nullptr;
Chris Craikd41c4d82015-01-05 15:51:13 -0800537 const mat4* projectionTransform = nullptr;
John Reckd0a0b2a2014-03-20 16:28:56 -0700538 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
Chris Craikbf72eb82015-06-08 11:30:44 -0700539 // if receiving projections, collect projecting descendant
John Reck113e0822014-03-18 09:22:59 -0700540
Chris Craikbf72eb82015-06-08 11:30:44 -0700541 // Note that if a direct descendant is projecting backwards, we pass its
542 // grandparent projection collection, since it shouldn't project onto its
John Reck113e0822014-03-18 09:22:59 -0700543 // parent, where it will already be drawing.
544 projectionChildren = &mProjectedNodes;
545 projectionTransform = &mat4::identity();
546 } else {
547 if (!haveAppliedPropertiesToProjection) {
548 applyViewPropertyTransforms(localTransformFromProjectionSurface);
549 haveAppliedPropertiesToProjection = true;
550 }
551 projectionChildren = compositedChildrenOfProjectionSurface;
552 projectionTransform = &localTransformFromProjectionSurface;
553 }
Derek Sollenbergerf2932592015-08-13 14:59:33 -0400554 child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700555 }
556 }
John Reck113e0822014-03-18 09:22:59 -0700557}
558
Derek Sollenberger579317d42017-08-29 16:33:49 -0400559const SkPath* RenderNode::getClippedOutline(const SkRect& clipRect) const {
560 const SkPath* outlinePath = properties().getOutline().getPath();
561 const uint32_t outlineID = outlinePath->getGenerationID();
562
563 if (outlineID != mClippedOutlineCache.outlineID || clipRect != mClippedOutlineCache.clipRect) {
564 // update the cache keys
565 mClippedOutlineCache.outlineID = outlineID;
566 mClippedOutlineCache.clipRect = clipRect;
567
568 // update the cache value by recomputing a new path
569 SkPath clipPath;
570 clipPath.addRect(clipRect);
571 Op(*outlinePath, clipPath, kIntersect_SkPathOp, &mClippedOutlineCache.clippedOutline);
572
573 }
574 return &mClippedOutlineCache.clippedOutline;
575}
576
John Reck113e0822014-03-18 09:22:59 -0700577} /* namespace uirenderer */
578} /* namespace android */