blob: dd20a76eb5611c3a1b24ab3c60753b27b0c7b055 [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
Chris Craik5e00c7c2016-07-06 16:10:09 -0700232static OffscreenBuffer* createLayer(RenderState& renderState, uint32_t width, uint32_t height) {
Chris Craik9fded232015-11-11 16:42:34 -0800233 return renderState.layerPool().get(renderState, width, height);
Chris Craik0b7e8242015-10-28 16:50:44 -0700234}
235
Chris Craik5e00c7c2016-07-06 16:10:09 -0700236static void destroyLayer(OffscreenBuffer* layer) {
Chris Craik9fded232015-11-11 16:42:34 -0800237 RenderState& renderState = layer->renderState;
238 renderState.layerPool().putOrDelete(layer);
Chris Craik0b7e8242015-10-28 16:50:44 -0700239}
240
Chris Craik5e00c7c2016-07-06 16:10:09 -0700241static bool layerMatchesWidthAndHeight(OffscreenBuffer* layer, int width, int height) {
Chris Craik9fded232015-11-11 16:42:34 -0800242 return layer->viewportWidth == (uint32_t) width && layer->viewportHeight == (uint32_t)height;
Chris Craik9fded232015-11-11 16:42:34 -0800243}
244
John Reck25fbb3f2014-06-12 13:46:45 -0700245void RenderNode::pushLayerUpdate(TreeInfo& info) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700246 LayerType layerType = properties().effectiveLayerType();
John Reck25fbb3f2014-06-12 13:46:45 -0700247 // If we are not a layer OR we cannot be rendered (eg, view was detached)
248 // we need to destroy any Layers we may have had previously
Chris Craike3e481d2016-07-11 12:20:51 -0700249 if (CC_LIKELY(layerType != LayerType::RenderLayer)
250 || CC_UNLIKELY(!isRenderable())
251 || CC_UNLIKELY(properties().getWidth() == 0)
252 || CC_UNLIKELY(properties().getHeight() == 0)) {
John Reck25fbb3f2014-06-12 13:46:45 -0700253 if (CC_UNLIKELY(mLayer)) {
Chris Craik0b7e8242015-10-28 16:50:44 -0700254 destroyLayer(mLayer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800255 mLayer = nullptr;
John Reck25fbb3f2014-06-12 13:46:45 -0700256 }
257 return;
258 }
259
Chris Craik69e5adf2014-08-14 13:34:01 -0700260 bool transformUpdateNeeded = false;
John Reck25fbb3f2014-06-12 13:46:45 -0700261 if (!mLayer) {
Chris Craik9fded232015-11-11 16:42:34 -0800262 mLayer = createLayer(info.canvasContext.getRenderState(), getWidth(), getHeight());
263 damageSelf(info);
264 transformUpdateNeeded = true;
265 } else if (!layerMatchesWidthAndHeight(mLayer, getWidth(), getHeight())) {
Chris Craikd4fe4d32016-06-09 16:57:11 -0700266 // TODO: remove now irrelevant, currently enqueued damage (respecting damage ordering)
267 // Or, ideally, maintain damage between frames on node/layer so ordering is always correct
Chris Craik9fded232015-11-11 16:42:34 -0800268 RenderState& renderState = mLayer->renderState;
269 if (properties().fitsOnLayer()) {
270 mLayer = renderState.layerPool().resize(mLayer, getWidth(), getHeight());
271 } else {
Chris Craik0b7e8242015-10-28 16:50:44 -0700272 destroyLayer(mLayer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800273 mLayer = nullptr;
John Reckc25e5062014-06-18 14:21:29 -0700274 }
John Reck25fbb3f2014-06-12 13:46:45 -0700275 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700276 transformUpdateNeeded = true;
277 }
278
John Reck25fbb3f2014-06-12 13:46:45 -0700279 SkRect dirty;
280 info.damageAccumulator->peekAtDirty(&dirty);
John Reck25fbb3f2014-06-12 13:46:45 -0700281
John Reckc25e5062014-06-18 14:21:29 -0700282 if (!mLayer) {
John Reck0e89e2b2014-10-31 14:49:06 -0700283 Caches::getInstance().dumpMemoryUsage();
John Reckc25e5062014-06-18 14:21:29 -0700284 if (info.errorHandler) {
John Reck77c40102015-10-26 15:49:47 -0700285 std::ostringstream err;
286 err << "Unable to create layer for " << getName();
Chris Craik76caecf2015-11-02 19:17:45 -0800287 const int maxTextureSize = Caches::getInstance().maxTextureSize;
John Reck77c40102015-10-26 15:49:47 -0700288 if (getWidth() > maxTextureSize || getHeight() > maxTextureSize) {
289 err << ", size " << getWidth() << "x" << getHeight()
290 << " exceeds max size " << maxTextureSize;
291 } else {
292 err << ", see logcat for more info";
293 }
294 info.errorHandler->onError(err.str());
John Reckc25e5062014-06-18 14:21:29 -0700295 }
296 return;
297 }
298
Chris Craik98787e62015-11-13 10:55:30 -0800299 if (transformUpdateNeeded && mLayer) {
Chris Craikc71bfca2014-08-21 10:18:58 -0700300 // update the transform in window of the layer to reset its origin wrt light source position
301 Matrix4 windowTransform;
302 info.damageAccumulator->computeCurrentTransform(&windowTransform);
303 mLayer->setWindowTransform(windowTransform);
304 }
John Reckc79eabc2014-08-05 11:03:42 -0700305
Chris Craik0b7e8242015-10-28 16:50:44 -0700306 info.layerUpdateQueue->enqueueLayerWithDamage(this, dirty);
John Reck998a6d82014-08-28 15:35:53 -0700307
Chris Craike2e53a72015-10-28 15:55:40 -0700308 // There might be prefetched layers that need to be accounted for.
309 // That might be us, so tell CanvasContext that this layer is in the
310 // tree and should not be destroyed.
311 info.canvasContext.markLayerInUse(this);
John Reck25fbb3f2014-06-12 13:46:45 -0700312}
313
Chris Craika766cb22015-06-08 16:49:43 -0700314/**
315 * Traverse down the the draw tree to prepare for a frame.
316 *
317 * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven
318 *
319 * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the
320 * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer.
321 */
322void RenderNode::prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer) {
John Recka447d292014-06-11 18:39:44 -0700323 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700324
John Reckdcba6722014-07-08 13:59:49 -0700325 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700326 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700327 }
John Reck9eb9f6f2014-08-21 11:23:05 -0700328 uint32_t animatorDirtyMask = 0;
329 if (CC_LIKELY(info.runAnimations)) {
330 animatorDirtyMask = mAnimatorManager.animate(info);
331 }
Chris Craika766cb22015-06-08 16:49:43 -0700332
John Reck3f725f02015-06-16 10:29:31 -0700333 bool willHaveFunctor = false;
Chris Craik003cc3d2015-10-16 10:24:55 -0700334 if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700335 willHaveFunctor = !mStagingDisplayList->getFunctors().empty();
Chris Craik003cc3d2015-10-16 10:24:55 -0700336 } else if (mDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700337 willHaveFunctor = !mDisplayList->getFunctors().empty();
John Reck3f725f02015-06-16 10:29:31 -0700338 }
Chris Craika766cb22015-06-08 16:49:43 -0700339 bool childFunctorsNeedLayer = mProperties.prepareForFunctorPresence(
340 willHaveFunctor, functorsNeedLayer);
341
John Reckf6481082016-02-02 15:18:23 -0800342 if (CC_UNLIKELY(mPositionListener.get())) {
343 mPositionListener->onPositionUpdated(*this, info);
344 }
345
John Recka7c2ea22014-08-08 13:21:00 -0700346 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700347 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700348 pushStagingDisplayListChanges(info);
349 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700350 prepareSubTree(info, childFunctorsNeedLayer, mDisplayList);
John Reck25fbb3f2014-06-12 13:46:45 -0700351
Doris Liu07c056d2016-06-13 12:52:44 -0700352 if (mDisplayList) {
353 for (auto& vectorDrawable : mDisplayList->getVectorDrawables()) {
354 // If any vector drawable in the display list needs update, damage the node.
355 if (vectorDrawable->isDirty()) {
356 damageSelf(info);
357 }
358 vectorDrawable->setPropertyChangeWillBeConsumed(true);
Doris Liu718cd3e2016-05-17 16:50:31 -0700359 }
Doris Liu718cd3e2016-05-17 16:50:31 -0700360 }
Doris Liub51b2862016-08-01 19:56:47 -0700361 pushLayerUpdate(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700362
John Recka447d292014-06-11 18:39:44 -0700363 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700364}
365
Chris Craikb565df12015-10-05 13:00:52 -0700366void RenderNode::syncProperties() {
367 mProperties = mStagingProperties;
368}
369
John Reck25fbb3f2014-06-12 13:46:45 -0700370void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reckff941dc2014-05-14 16:34:14 -0700371 // Push the animators first so that setupStartValueIfNecessary() is called
372 // before properties() is trampled by stagingProperties(), as they are
373 // required by some animators.
John Reck9eb9f6f2014-08-21 11:23:05 -0700374 if (CC_LIKELY(info.runAnimations)) {
John Reck119907c2014-08-14 09:02:01 -0700375 mAnimatorManager.pushStaging();
John Reck9eb9f6f2014-08-21 11:23:05 -0700376 }
John Reckff941dc2014-05-14 16:34:14 -0700377 if (mDirtyPropertyFields) {
378 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700379 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700380 info.damageAccumulator->popTransform();
Chris Craikb565df12015-10-05 13:00:52 -0700381 syncProperties();
John Recke4267ea2014-06-03 15:53:15 -0700382 // We could try to be clever and only re-damage if the matrix changed.
383 // However, we don't need to worry about that. The cost of over-damaging
384 // here is only going to be a single additional map rect of this node
385 // plus a rect join(). The parent's transform (and up) will only be
386 // performed once.
John Recka447d292014-06-11 18:39:44 -0700387 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700388 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700389 }
John Reck25fbb3f2014-06-12 13:46:45 -0700390}
391
John Reckaa6e84f2016-06-16 15:36:13 -0700392void RenderNode::syncDisplayList(TreeInfo* info) {
Chris Craikb565df12015-10-05 13:00:52 -0700393 // Make sure we inc first so that we don't fluctuate between 0 and 1,
394 // which would thrash the layer cache
Chris Craik003cc3d2015-10-16 10:24:55 -0700395 if (mStagingDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700396 for (auto&& child : mStagingDisplayList->getChildren()) {
Chris Craikb565df12015-10-05 13:00:52 -0700397 child->renderNode->incParentRefCount();
398 }
399 }
John Reckaa6e84f2016-06-16 15:36:13 -0700400 deleteDisplayList(info ? info->observer : nullptr, info);
Chris Craik003cc3d2015-10-16 10:24:55 -0700401 mDisplayList = mStagingDisplayList;
402 mStagingDisplayList = nullptr;
403 if (mDisplayList) {
John Reckcd1c3eb2016-04-14 10:38:54 -0700404 for (auto& iter : mDisplayList->getFunctors()) {
405 (*iter.functor)(DrawGlInfo::kModeSync, nullptr);
Chris Craikb565df12015-10-05 13:00:52 -0700406 }
Doris Liu718cd3e2016-05-17 16:50:31 -0700407 for (auto& vectorDrawable : mDisplayList->getVectorDrawables()) {
408 vectorDrawable->syncProperties();
Doris Liu1d8e1942016-03-02 15:16:28 -0800409 }
Chris Craikb565df12015-10-05 13:00:52 -0700410 }
411}
412
John Reck25fbb3f2014-06-12 13:46:45 -0700413void RenderNode::pushStagingDisplayListChanges(TreeInfo& info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700414 if (mNeedsDisplayListSync) {
415 mNeedsDisplayListSync = false;
John Reck5c9d7172014-10-22 11:32:27 -0700416 // Damage with the old display list first then the new one to catch any
417 // changes in isRenderable or, in the future, bounds
418 damageSelf(info);
John Reckaa6e84f2016-06-16 15:36:13 -0700419 syncDisplayList(&info);
John Recke4267ea2014-06-03 15:53:15 -0700420 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700421 }
John Reck8de65a82014-04-09 15:23:38 -0700422}
423
John Reckaa6e84f2016-06-16 15:36:13 -0700424void RenderNode::deleteDisplayList(TreeObserver* observer, TreeInfo* info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700425 if (mDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700426 for (auto&& child : mDisplayList->getChildren()) {
John Reckaa6e84f2016-06-16 15:36:13 -0700427 child->renderNode->decParentRefCount(observer, info);
John Reckdcba6722014-07-08 13:59:49 -0700428 }
429 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700430 delete mDisplayList;
431 mDisplayList = nullptr;
John Reckdcba6722014-07-08 13:59:49 -0700432}
433
Chris Craik003cc3d2015-10-16 10:24:55 -0700434void RenderNode::prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayList* subtree) {
John Reck8de65a82014-04-09 15:23:38 -0700435 if (subtree) {
John Reck860d1552014-04-11 19:15:05 -0700436 TextureCache& cache = Caches::getInstance().textureCache;
Chris Craikb36af872015-10-16 14:23:12 -0700437 info.out.hasFunctors |= subtree->getFunctors().size();
438 for (auto&& bitmapResource : subtree->getBitmapResources()) {
Chris Craike2e53a72015-10-28 15:55:40 -0700439 void* ownerToken = &info.canvasContext;
440 info.prepareTextures = cache.prefetchAndMarkInUse(ownerToken, bitmapResource);
John Reckf4198b72014-04-09 17:00:04 -0700441 }
Chris Craikb36af872015-10-16 14:23:12 -0700442 for (auto&& op : subtree->getChildren()) {
Chris Craikb565df12015-10-05 13:00:52 -0700443 RenderNode* childNode = op->renderNode;
Chris Craikb565df12015-10-05 13:00:52 -0700444 info.damageAccumulator->pushTransform(&op->localMatrix);
445 bool childFunctorsNeedLayer = functorsNeedLayer; // TODO! || op->mRecordedWithPotentialStencilClip;
Chris Craika766cb22015-06-08 16:49:43 -0700446 childNode->prepareTreeImpl(info, childFunctorsNeedLayer);
John Recka447d292014-06-11 18:39:44 -0700447 info.damageAccumulator->popTransform();
John Reck5bf11bb2014-03-25 10:22:09 -0700448 }
John Reck113e0822014-03-18 09:22:59 -0700449 }
450}
451
John Reckaa6e84f2016-06-16 15:36:13 -0700452void RenderNode::destroyHardwareResources(TreeObserver* observer, TreeInfo* info) {
John Reckdcba6722014-07-08 13:59:49 -0700453 if (mLayer) {
Chris Craik0b7e8242015-10-28 16:50:44 -0700454 destroyLayer(mLayer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800455 mLayer = nullptr;
John Reckdcba6722014-07-08 13:59:49 -0700456 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700457 if (mDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700458 for (auto&& child : mDisplayList->getChildren()) {
John Reckaa6e84f2016-06-16 15:36:13 -0700459 child->renderNode->destroyHardwareResources(observer, info);
John Reckdcba6722014-07-08 13:59:49 -0700460 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700461 if (mNeedsDisplayListSync) {
John Reckdcba6722014-07-08 13:59:49 -0700462 // Next prepare tree we are going to push a new display list, so we can
463 // drop our current one now
John Reckaa6e84f2016-06-16 15:36:13 -0700464 deleteDisplayList(observer, info);
John Reckdcba6722014-07-08 13:59:49 -0700465 }
466 }
467}
468
John Reckaa6e84f2016-06-16 15:36:13 -0700469void RenderNode::decParentRefCount(TreeObserver* observer, TreeInfo* info) {
John Reckdcba6722014-07-08 13:59:49 -0700470 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
471 mParentCount--;
472 if (!mParentCount) {
John Reck44b49f02016-03-25 14:29:48 -0700473 if (observer) {
474 observer->onMaybeRemovedFromTree(this);
475 }
John Reckaa6e84f2016-06-16 15:36:13 -0700476 if (CC_UNLIKELY(mPositionListener.get())) {
477 mPositionListener->onPositionLost(*this, info);
478 }
John Reckdcba6722014-07-08 13:59:49 -0700479 // If a child of ours is being attached to our parent then this will incorrectly
480 // destroy its hardware resources. However, this situation is highly unlikely
481 // and the failure is "just" that the layer is re-created, so this should
482 // be safe enough
John Reckaa6e84f2016-06-16 15:36:13 -0700483 destroyHardwareResources(observer, info);
John Reckdcba6722014-07-08 13:59:49 -0700484 }
485}
486
John Reck113e0822014-03-18 09:22:59 -0700487/**
488 * Apply property-based transformations to input matrix
489 *
490 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
491 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
492 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700493void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700494 if (properties().getLeft() != 0 || properties().getTop() != 0) {
495 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700496 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700497 if (properties().getStaticMatrix()) {
498 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700499 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700500 } else if (properties().getAnimationMatrix()) {
501 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700502 matrix.multiply(anim);
503 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700504
Chris Craikcc39e162014-04-25 18:34:11 -0700505 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700506 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700507 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700508 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700509 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700510 } else {
511 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700512 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700513 } else {
514 mat4 true3dMat;
515 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700516 properties().getPivotX() + properties().getTranslationX(),
517 properties().getPivotY() + properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700518 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700519 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
520 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
521 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
522 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
523 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700524
525 matrix.multiply(true3dMat);
526 }
527 }
528 }
529}
530
531/**
532 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
533 *
534 * This should be called before a call to defer() or drawDisplayList()
535 *
536 * Each DisplayList that serves as a 3d root builds its list of composited children,
537 * which are flagged to not draw in the standard draw loop.
538 */
539void RenderNode::computeOrdering() {
540 ATRACE_CALL();
541 mProjectedNodes.clear();
542
543 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
544 // transform properties are applied correctly to top level children
Chris Craik003cc3d2015-10-16 10:24:55 -0700545 if (mDisplayList == nullptr) return;
Chris Craikb36af872015-10-16 14:23:12 -0700546 for (unsigned int i = 0; i < mDisplayList->getChildren().size(); i++) {
Chris Craik5e00c7c2016-07-06 16:10:09 -0700547 RenderNodeOp* childOp = mDisplayList->getChildren()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700548 childOp->renderNode->computeOrderingImpl(childOp, &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700549 }
550}
551
552void RenderNode::computeOrderingImpl(
Chris Craik5e00c7c2016-07-06 16:10:09 -0700553 RenderNodeOp* opState,
554 std::vector<RenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700555 const mat4* transformFromProjectionSurface) {
556 mProjectedNodes.clear();
Chris Craik003cc3d2015-10-16 10:24:55 -0700557 if (mDisplayList == nullptr || mDisplayList->isEmpty()) return;
John Reck113e0822014-03-18 09:22:59 -0700558
559 // TODO: should avoid this calculation in most cases
560 // TODO: just calculate single matrix, down to all leaf composited elements
561 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
Chris Craik8d1f2122015-11-24 16:40:09 -0800562 localTransformFromProjectionSurface.multiply(opState->localMatrix);
John Reck113e0822014-03-18 09:22:59 -0700563
John Reckd0a0b2a2014-03-20 16:28:56 -0700564 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700565 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
Chris Craik8d1f2122015-11-24 16:40:09 -0800566 opState->skipInOrderDraw = true;
567 opState->transformFromCompositingAncestor = localTransformFromProjectionSurface;
John Reck272a6852015-07-29 16:48:58 -0700568 compositedChildrenOfProjectionSurface->push_back(opState);
John Reck113e0822014-03-18 09:22:59 -0700569 } else {
570 // standard in order draw
Chris Craik8d1f2122015-11-24 16:40:09 -0800571 opState->skipInOrderDraw = false;
John Reck113e0822014-03-18 09:22:59 -0700572 }
573
Chris Craikb36af872015-10-16 14:23:12 -0700574 if (mDisplayList->getChildren().size() > 0) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700575 const bool isProjectionReceiver = mDisplayList->projectionReceiveIndex >= 0;
John Reck113e0822014-03-18 09:22:59 -0700576 bool haveAppliedPropertiesToProjection = false;
Chris Craikb36af872015-10-16 14:23:12 -0700577 for (unsigned int i = 0; i < mDisplayList->getChildren().size(); i++) {
Chris Craik5e00c7c2016-07-06 16:10:09 -0700578 RenderNodeOp* childOp = mDisplayList->getChildren()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700579 RenderNode* child = childOp->renderNode;
John Reck113e0822014-03-18 09:22:59 -0700580
Chris Craik5e00c7c2016-07-06 16:10:09 -0700581 std::vector<RenderNodeOp*>* projectionChildren = nullptr;
Chris Craikd41c4d82015-01-05 15:51:13 -0800582 const mat4* projectionTransform = nullptr;
John Reckd0a0b2a2014-03-20 16:28:56 -0700583 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
Chris Craikbf72eb82015-06-08 11:30:44 -0700584 // if receiving projections, collect projecting descendant
John Reck113e0822014-03-18 09:22:59 -0700585
Chris Craikbf72eb82015-06-08 11:30:44 -0700586 // Note that if a direct descendant is projecting backwards, we pass its
587 // grandparent projection collection, since it shouldn't project onto its
John Reck113e0822014-03-18 09:22:59 -0700588 // parent, where it will already be drawing.
589 projectionChildren = &mProjectedNodes;
590 projectionTransform = &mat4::identity();
591 } else {
592 if (!haveAppliedPropertiesToProjection) {
593 applyViewPropertyTransforms(localTransformFromProjectionSurface);
594 haveAppliedPropertiesToProjection = true;
595 }
596 projectionChildren = compositedChildrenOfProjectionSurface;
597 projectionTransform = &localTransformFromProjectionSurface;
598 }
Derek Sollenbergerf2932592015-08-13 14:59:33 -0400599 child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700600 }
601 }
John Reck113e0822014-03-18 09:22:59 -0700602}
603
John Reck113e0822014-03-18 09:22:59 -0700604} /* namespace uirenderer */
605} /* namespace android */