blob: c90abad10ccfca16b3e8ed3b5cabc497c4d4ca0d [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"
Chris Craik70850ea2014-11-18 10:49:23 -080026#include "utils/TraceUtils.h"
Chris Craik5e00c7c2016-07-06 16:10:09 -070027#include "VectorDrawable.h"
28#include "renderstate/RenderState.h"
John Reck998a6d82014-08-28 15:35:53 -070029#include "renderthread/CanvasContext.h"
John Reck113e0822014-03-18 09:22:59 -070030
John Recke248bd12015-08-05 13:53:53 -070031#include "protos/hwui.pb.h"
32#include "protos/ProtoHelpers.h"
33
John Reck77c40102015-10-26 15:49:47 -070034#include <algorithm>
35#include <sstream>
36#include <string>
37
John Reck113e0822014-03-18 09:22:59 -070038namespace android {
39namespace uirenderer {
40
John Reck8de65a82014-04-09 15:23:38 -070041RenderNode::RenderNode()
John Reckff941dc2014-05-14 16:34:14 -070042 : mDirtyPropertyFields(0)
Chris Craik003cc3d2015-10-16 10:24:55 -070043 , mNeedsDisplayListSync(false)
44 , mDisplayList(nullptr)
45 , mStagingDisplayList(nullptr)
John Reck68bfe0a2014-06-24 15:34:58 -070046 , mAnimatorManager(*this)
John Reckdcba6722014-07-08 13:59:49 -070047 , mParentCount(0) {
John Reck113e0822014-03-18 09:22:59 -070048}
49
50RenderNode::~RenderNode() {
John Reck44b49f02016-03-25 14:29:48 -070051 deleteDisplayList(nullptr);
Chris Craik003cc3d2015-10-16 10:24:55 -070052 delete mStagingDisplayList;
Chris Craik0b7e8242015-10-28 16:50:44 -070053 LOG_ALWAYS_FATAL_IF(mLayer, "layer missed detachment!");
John Reck113e0822014-03-18 09:22:59 -070054}
55
John Reck51f2d602016-04-06 07:50:47 -070056void RenderNode::setStagingDisplayList(DisplayList* displayList, TreeObserver* observer) {
Chris Craik003cc3d2015-10-16 10:24:55 -070057 mNeedsDisplayListSync = true;
58 delete mStagingDisplayList;
59 mStagingDisplayList = displayList;
John Reck9dea0d52015-10-27 10:04:38 -070060 // If mParentCount == 0 we are the sole reference to this RenderNode,
61 // so immediately free the old display list
62 if (!mParentCount && !mStagingDisplayList) {
John Reck51f2d602016-04-06 07:50:47 -070063 deleteDisplayList(observer);
John Reck9dea0d52015-10-27 10:04:38 -070064 }
John Reck113e0822014-03-18 09:22:59 -070065}
66
67/**
68 * This function is a simplified version of replay(), where we simply retrieve and log the
69 * display list. This function should remain in sync with the replay() function.
70 */
Chris Craik91eff222016-02-22 13:39:33 -080071void RenderNode::output(uint32_t level, const char* label) {
72 ALOGD("%s (%s %p%s%s%s%s%s)",
73 label,
74 getName(),
75 this,
76 (MathUtils::isZero(properties().getAlpha()) ? ", zero alpha" : ""),
77 (properties().hasShadow() ? ", casting shadow" : ""),
78 (isRenderable() ? "" : ", empty"),
79 (properties().getProjectBackwards() ? ", projected" : ""),
80 (mLayer != nullptr ? ", on HW Layer" : ""));
81 properties().debugOutputProperties(level + 1);
82
83 if (mDisplayList) {
84 for (auto&& op : mDisplayList->getOps()) {
85 std::stringstream strout;
86 OpDumper::dump(*op, strout, level + 1);
87 if (op->opId == RecordedOpId::RenderNodeOp) {
88 auto rnOp = reinterpret_cast<const RenderNodeOp*>(op);
89 rnOp->renderNode->output(level + 1, strout.str().c_str());
90 } else {
91 ALOGD("%s", strout.str().c_str());
92 }
93 }
94 }
95 ALOGD("%*s/RenderNode(%s %p)", level * 2, "", getName(), this);
96}
John Reck113e0822014-03-18 09:22:59 -070097
John Recke248bd12015-08-05 13:53:53 -070098void RenderNode::copyTo(proto::RenderNode *pnode) {
99 pnode->set_id(static_cast<uint64_t>(
100 reinterpret_cast<uintptr_t>(this)));
101 pnode->set_name(mName.string(), mName.length());
102
103 proto::RenderProperties* pprops = pnode->mutable_properties();
104 pprops->set_left(properties().getLeft());
105 pprops->set_top(properties().getTop());
106 pprops->set_right(properties().getRight());
107 pprops->set_bottom(properties().getBottom());
108 pprops->set_clip_flags(properties().getClippingFlags());
109 pprops->set_alpha(properties().getAlpha());
110 pprops->set_translation_x(properties().getTranslationX());
111 pprops->set_translation_y(properties().getTranslationY());
112 pprops->set_translation_z(properties().getTranslationZ());
113 pprops->set_elevation(properties().getElevation());
114 pprops->set_rotation(properties().getRotation());
115 pprops->set_rotation_x(properties().getRotationX());
116 pprops->set_rotation_y(properties().getRotationY());
117 pprops->set_scale_x(properties().getScaleX());
118 pprops->set_scale_y(properties().getScaleY());
119 pprops->set_pivot_x(properties().getPivotX());
120 pprops->set_pivot_y(properties().getPivotY());
121 pprops->set_has_overlapping_rendering(properties().getHasOverlappingRendering());
122 pprops->set_pivot_explicitly_set(properties().isPivotExplicitlySet());
123 pprops->set_project_backwards(properties().getProjectBackwards());
124 pprops->set_projection_receiver(properties().isProjectionReceiver());
125 set(pprops->mutable_clip_bounds(), properties().getClipBounds());
126
127 const Outline& outline = properties().getOutline();
128 if (outline.getType() != Outline::Type::None) {
129 proto::Outline* poutline = pprops->mutable_outline();
130 poutline->clear_path();
131 if (outline.getType() == Outline::Type::Empty) {
132 poutline->set_type(proto::Outline_Type_Empty);
133 } else if (outline.getType() == Outline::Type::ConvexPath) {
134 poutline->set_type(proto::Outline_Type_ConvexPath);
135 if (const SkPath* path = outline.getPath()) {
136 set(poutline->mutable_path(), *path);
137 }
138 } else if (outline.getType() == Outline::Type::RoundRect) {
139 poutline->set_type(proto::Outline_Type_RoundRect);
140 } else {
141 ALOGW("Uknown outline type! %d", static_cast<int>(outline.getType()));
142 poutline->set_type(proto::Outline_Type_None);
143 }
144 poutline->set_should_clip(outline.getShouldClip());
145 poutline->set_alpha(outline.getAlpha());
146 poutline->set_radius(outline.getRadius());
147 set(poutline->mutable_bounds(), outline.getBounds());
148 } else {
149 pprops->clear_outline();
150 }
151
152 const RevealClip& revealClip = properties().getRevealClip();
153 if (revealClip.willClip()) {
154 proto::RevealClip* prevealClip = pprops->mutable_reveal_clip();
155 prevealClip->set_x(revealClip.getX());
156 prevealClip->set_y(revealClip.getY());
157 prevealClip->set_radius(revealClip.getRadius());
158 } else {
159 pprops->clear_reveal_clip();
160 }
161
162 pnode->clear_children();
Chris Craik003cc3d2015-10-16 10:24:55 -0700163 if (mDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700164 for (auto&& child : mDisplayList->getChildren()) {
Chris Craikb565df12015-10-05 13:00:52 -0700165 child->renderNode->copyTo(pnode->add_children());
John Recke248bd12015-08-05 13:53:53 -0700166 }
167 }
168}
169
John Reckfe5e7b72014-05-23 17:42:28 -0700170int RenderNode::getDebugSize() {
171 int size = sizeof(RenderNode);
Chris Craik003cc3d2015-10-16 10:24:55 -0700172 if (mStagingDisplayList) {
173 size += mStagingDisplayList->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700174 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700175 if (mDisplayList && mDisplayList != mStagingDisplayList) {
176 size += mDisplayList->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700177 }
178 return size;
179}
180
John Reckf4198b72014-04-09 17:00:04 -0700181void RenderNode::prepareTree(TreeInfo& info) {
182 ATRACE_CALL();
Chris Craik69e5adf2014-08-14 13:34:01 -0700183 LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
John Reckf4198b72014-04-09 17:00:04 -0700184
Chris Craika766cb22015-06-08 16:49:43 -0700185 // Functors don't correctly handle stencil usage of overdraw debugging - shove 'em in a layer.
186 bool functorsNeedLayer = Properties::debugOverdraw;
187
188 prepareTreeImpl(info, functorsNeedLayer);
John Reckf4198b72014-04-09 17:00:04 -0700189}
190
John Reck68bfe0a2014-06-24 15:34:58 -0700191void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
192 mAnimatorManager.addAnimator(animator);
193}
194
Doris Liu8b083202016-02-19 21:46:06 +0000195void RenderNode::removeAnimator(const sp<BaseRenderNodeAnimator>& animator) {
196 mAnimatorManager.removeAnimator(animator);
197}
198
John Recke4267ea2014-06-03 15:53:15 -0700199void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700200 if (isRenderable()) {
John Reck293e8682014-06-17 10:34:02 -0700201 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700202 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
203 } else {
204 // Hope this is big enough?
205 // TODO: Get this from the display list ops or something
John Reckc1288232015-08-12 13:39:11 -0700206 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
John Recka447d292014-06-11 18:39:44 -0700207 }
John Recke4267ea2014-06-03 15:53:15 -0700208 }
209}
210
John Recka7c2ea22014-08-08 13:21:00 -0700211void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700212 LayerType layerType = properties().effectiveLayerType();
Chris Craik182952f2015-03-09 14:17:29 -0700213 if (CC_UNLIKELY(layerType == LayerType::RenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700214 // Damage applied so far needs to affect our parent, but does not require
215 // the layer to be updated. So we pop/push here to clear out the current
216 // damage and get a clean state for display list or children updates to
217 // affect, which will require the layer to be updated
218 info.damageAccumulator->popTransform();
219 info.damageAccumulator->pushTransform(this);
220 if (dirtyMask & DISPLAY_LIST) {
221 damageSelf(info);
222 }
John Reck25fbb3f2014-06-12 13:46:45 -0700223 }
224}
225
Chris Craik5e00c7c2016-07-06 16:10:09 -0700226static OffscreenBuffer* createLayer(RenderState& renderState, uint32_t width, uint32_t height) {
Chris Craik9fded232015-11-11 16:42:34 -0800227 return renderState.layerPool().get(renderState, width, height);
Chris Craik0b7e8242015-10-28 16:50:44 -0700228}
229
Chris Craik5e00c7c2016-07-06 16:10:09 -0700230static void destroyLayer(OffscreenBuffer* layer) {
Chris Craik9fded232015-11-11 16:42:34 -0800231 RenderState& renderState = layer->renderState;
232 renderState.layerPool().putOrDelete(layer);
Chris Craik0b7e8242015-10-28 16:50:44 -0700233}
234
Chris Craik5e00c7c2016-07-06 16:10:09 -0700235static bool layerMatchesWidthAndHeight(OffscreenBuffer* layer, int width, int height) {
Chris Craik9fded232015-11-11 16:42:34 -0800236 return layer->viewportWidth == (uint32_t) width && layer->viewportHeight == (uint32_t)height;
Chris Craik9fded232015-11-11 16:42:34 -0800237}
238
John Reck25fbb3f2014-06-12 13:46:45 -0700239void RenderNode::pushLayerUpdate(TreeInfo& info) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700240 LayerType layerType = properties().effectiveLayerType();
John Reck25fbb3f2014-06-12 13:46:45 -0700241 // If we are not a layer OR we cannot be rendered (eg, view was detached)
242 // we need to destroy any Layers we may have had previously
Chris Craike3e481d2016-07-11 12:20:51 -0700243 if (CC_LIKELY(layerType != LayerType::RenderLayer)
244 || CC_UNLIKELY(!isRenderable())
245 || CC_UNLIKELY(properties().getWidth() == 0)
246 || CC_UNLIKELY(properties().getHeight() == 0)) {
John Reck25fbb3f2014-06-12 13:46:45 -0700247 if (CC_UNLIKELY(mLayer)) {
Chris Craik0b7e8242015-10-28 16:50:44 -0700248 destroyLayer(mLayer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800249 mLayer = nullptr;
John Reck25fbb3f2014-06-12 13:46:45 -0700250 }
251 return;
252 }
253
Chris Craik69e5adf2014-08-14 13:34:01 -0700254 bool transformUpdateNeeded = false;
John Reck25fbb3f2014-06-12 13:46:45 -0700255 if (!mLayer) {
Chris Craik9fded232015-11-11 16:42:34 -0800256 mLayer = createLayer(info.canvasContext.getRenderState(), getWidth(), getHeight());
257 damageSelf(info);
258 transformUpdateNeeded = true;
259 } else if (!layerMatchesWidthAndHeight(mLayer, getWidth(), getHeight())) {
Chris Craikd4fe4d32016-06-09 16:57:11 -0700260 // TODO: remove now irrelevant, currently enqueued damage (respecting damage ordering)
261 // Or, ideally, maintain damage between frames on node/layer so ordering is always correct
Chris Craik9fded232015-11-11 16:42:34 -0800262 RenderState& renderState = mLayer->renderState;
263 if (properties().fitsOnLayer()) {
264 mLayer = renderState.layerPool().resize(mLayer, getWidth(), getHeight());
265 } else {
Chris Craik0b7e8242015-10-28 16:50:44 -0700266 destroyLayer(mLayer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800267 mLayer = nullptr;
John Reckc25e5062014-06-18 14:21:29 -0700268 }
John Reck25fbb3f2014-06-12 13:46:45 -0700269 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700270 transformUpdateNeeded = true;
271 }
272
John Reck25fbb3f2014-06-12 13:46:45 -0700273 SkRect dirty;
274 info.damageAccumulator->peekAtDirty(&dirty);
John Reck25fbb3f2014-06-12 13:46:45 -0700275
John Reckc25e5062014-06-18 14:21:29 -0700276 if (!mLayer) {
John Reck0e89e2b2014-10-31 14:49:06 -0700277 Caches::getInstance().dumpMemoryUsage();
John Reckc25e5062014-06-18 14:21:29 -0700278 if (info.errorHandler) {
John Reck77c40102015-10-26 15:49:47 -0700279 std::ostringstream err;
280 err << "Unable to create layer for " << getName();
Chris Craik76caecf2015-11-02 19:17:45 -0800281 const int maxTextureSize = Caches::getInstance().maxTextureSize;
John Reck77c40102015-10-26 15:49:47 -0700282 if (getWidth() > maxTextureSize || getHeight() > maxTextureSize) {
283 err << ", size " << getWidth() << "x" << getHeight()
284 << " exceeds max size " << maxTextureSize;
285 } else {
286 err << ", see logcat for more info";
287 }
288 info.errorHandler->onError(err.str());
John Reckc25e5062014-06-18 14:21:29 -0700289 }
290 return;
291 }
292
Chris Craik98787e62015-11-13 10:55:30 -0800293 if (transformUpdateNeeded && mLayer) {
Chris Craikc71bfca2014-08-21 10:18:58 -0700294 // update the transform in window of the layer to reset its origin wrt light source position
295 Matrix4 windowTransform;
296 info.damageAccumulator->computeCurrentTransform(&windowTransform);
297 mLayer->setWindowTransform(windowTransform);
298 }
John Reckc79eabc2014-08-05 11:03:42 -0700299
Chris Craik0b7e8242015-10-28 16:50:44 -0700300 info.layerUpdateQueue->enqueueLayerWithDamage(this, dirty);
John Reck998a6d82014-08-28 15:35:53 -0700301
Chris Craike2e53a72015-10-28 15:55:40 -0700302 // There might be prefetched layers that need to be accounted for.
303 // That might be us, so tell CanvasContext that this layer is in the
304 // tree and should not be destroyed.
305 info.canvasContext.markLayerInUse(this);
John Reck25fbb3f2014-06-12 13:46:45 -0700306}
307
Chris Craika766cb22015-06-08 16:49:43 -0700308/**
309 * Traverse down the the draw tree to prepare for a frame.
310 *
311 * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven
312 *
313 * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the
314 * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer.
315 */
316void RenderNode::prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer) {
John Recka447d292014-06-11 18:39:44 -0700317 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700318
John Reckdcba6722014-07-08 13:59:49 -0700319 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700320 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700321 }
John Reck9eb9f6f2014-08-21 11:23:05 -0700322 uint32_t animatorDirtyMask = 0;
323 if (CC_LIKELY(info.runAnimations)) {
324 animatorDirtyMask = mAnimatorManager.animate(info);
325 }
Chris Craika766cb22015-06-08 16:49:43 -0700326
John Reck3f725f02015-06-16 10:29:31 -0700327 bool willHaveFunctor = false;
Chris Craik003cc3d2015-10-16 10:24:55 -0700328 if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700329 willHaveFunctor = !mStagingDisplayList->getFunctors().empty();
Chris Craik003cc3d2015-10-16 10:24:55 -0700330 } else if (mDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700331 willHaveFunctor = !mDisplayList->getFunctors().empty();
John Reck3f725f02015-06-16 10:29:31 -0700332 }
Chris Craika766cb22015-06-08 16:49:43 -0700333 bool childFunctorsNeedLayer = mProperties.prepareForFunctorPresence(
334 willHaveFunctor, functorsNeedLayer);
335
John Reckf6481082016-02-02 15:18:23 -0800336 if (CC_UNLIKELY(mPositionListener.get())) {
337 mPositionListener->onPositionUpdated(*this, info);
338 }
339
John Recka7c2ea22014-08-08 13:21:00 -0700340 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700341 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700342 pushStagingDisplayListChanges(info);
343 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700344 prepareSubTree(info, childFunctorsNeedLayer, mDisplayList);
John Reck25fbb3f2014-06-12 13:46:45 -0700345
Doris Liu07c056d2016-06-13 12:52:44 -0700346 if (mDisplayList) {
347 for (auto& vectorDrawable : mDisplayList->getVectorDrawables()) {
348 // If any vector drawable in the display list needs update, damage the node.
349 if (vectorDrawable->isDirty()) {
350 damageSelf(info);
351 }
352 vectorDrawable->setPropertyChangeWillBeConsumed(true);
Doris Liu718cd3e2016-05-17 16:50:31 -0700353 }
Doris Liu718cd3e2016-05-17 16:50:31 -0700354 }
Doris Liub51b2862016-08-01 19:56:47 -0700355 pushLayerUpdate(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700356
John Recka447d292014-06-11 18:39:44 -0700357 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700358}
359
Chris Craikb565df12015-10-05 13:00:52 -0700360void RenderNode::syncProperties() {
361 mProperties = mStagingProperties;
362}
363
John Reck25fbb3f2014-06-12 13:46:45 -0700364void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reckff941dc2014-05-14 16:34:14 -0700365 // Push the animators first so that setupStartValueIfNecessary() is called
366 // before properties() is trampled by stagingProperties(), as they are
367 // required by some animators.
John Reck9eb9f6f2014-08-21 11:23:05 -0700368 if (CC_LIKELY(info.runAnimations)) {
John Reck119907c2014-08-14 09:02:01 -0700369 mAnimatorManager.pushStaging();
John Reck9eb9f6f2014-08-21 11:23:05 -0700370 }
John Reckff941dc2014-05-14 16:34:14 -0700371 if (mDirtyPropertyFields) {
372 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700373 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700374 info.damageAccumulator->popTransform();
Chris Craikb565df12015-10-05 13:00:52 -0700375 syncProperties();
John Recke4267ea2014-06-03 15:53:15 -0700376 // We could try to be clever and only re-damage if the matrix changed.
377 // However, we don't need to worry about that. The cost of over-damaging
378 // here is only going to be a single additional map rect of this node
379 // plus a rect join(). The parent's transform (and up) will only be
380 // performed once.
John Recka447d292014-06-11 18:39:44 -0700381 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700382 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700383 }
John Reck25fbb3f2014-06-12 13:46:45 -0700384}
385
John Reckaa6e84f2016-06-16 15:36:13 -0700386void RenderNode::syncDisplayList(TreeInfo* info) {
Chris Craikb565df12015-10-05 13:00:52 -0700387 // Make sure we inc first so that we don't fluctuate between 0 and 1,
388 // which would thrash the layer cache
Chris Craik003cc3d2015-10-16 10:24:55 -0700389 if (mStagingDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700390 for (auto&& child : mStagingDisplayList->getChildren()) {
Chris Craikb565df12015-10-05 13:00:52 -0700391 child->renderNode->incParentRefCount();
392 }
393 }
John Reckaa6e84f2016-06-16 15:36:13 -0700394 deleteDisplayList(info ? info->observer : nullptr, info);
Chris Craik003cc3d2015-10-16 10:24:55 -0700395 mDisplayList = mStagingDisplayList;
396 mStagingDisplayList = nullptr;
397 if (mDisplayList) {
John Reckcd1c3eb2016-04-14 10:38:54 -0700398 for (auto& iter : mDisplayList->getFunctors()) {
399 (*iter.functor)(DrawGlInfo::kModeSync, nullptr);
Chris Craikb565df12015-10-05 13:00:52 -0700400 }
Doris Liu718cd3e2016-05-17 16:50:31 -0700401 for (auto& vectorDrawable : mDisplayList->getVectorDrawables()) {
402 vectorDrawable->syncProperties();
Doris Liu1d8e1942016-03-02 15:16:28 -0800403 }
Chris Craikb565df12015-10-05 13:00:52 -0700404 }
405}
406
John Reck25fbb3f2014-06-12 13:46:45 -0700407void RenderNode::pushStagingDisplayListChanges(TreeInfo& info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700408 if (mNeedsDisplayListSync) {
409 mNeedsDisplayListSync = false;
John Reck5c9d7172014-10-22 11:32:27 -0700410 // Damage with the old display list first then the new one to catch any
411 // changes in isRenderable or, in the future, bounds
412 damageSelf(info);
John Reckaa6e84f2016-06-16 15:36:13 -0700413 syncDisplayList(&info);
John Recke4267ea2014-06-03 15:53:15 -0700414 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700415 }
John Reck8de65a82014-04-09 15:23:38 -0700416}
417
John Reckaa6e84f2016-06-16 15:36:13 -0700418void RenderNode::deleteDisplayList(TreeObserver* observer, TreeInfo* info) {
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->decParentRefCount(observer, info);
John Reckdcba6722014-07-08 13:59:49 -0700422 }
423 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700424 delete mDisplayList;
425 mDisplayList = nullptr;
John Reckdcba6722014-07-08 13:59:49 -0700426}
427
Chris Craik003cc3d2015-10-16 10:24:55 -0700428void RenderNode::prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayList* subtree) {
John Reck8de65a82014-04-09 15:23:38 -0700429 if (subtree) {
John Reck860d1552014-04-11 19:15:05 -0700430 TextureCache& cache = Caches::getInstance().textureCache;
Chris Craikb36af872015-10-16 14:23:12 -0700431 info.out.hasFunctors |= subtree->getFunctors().size();
432 for (auto&& bitmapResource : subtree->getBitmapResources()) {
Chris Craike2e53a72015-10-28 15:55:40 -0700433 void* ownerToken = &info.canvasContext;
434 info.prepareTextures = cache.prefetchAndMarkInUse(ownerToken, bitmapResource);
John Reckf4198b72014-04-09 17:00:04 -0700435 }
Chris Craikb36af872015-10-16 14:23:12 -0700436 for (auto&& op : subtree->getChildren()) {
Chris Craikb565df12015-10-05 13:00:52 -0700437 RenderNode* childNode = op->renderNode;
Chris Craikb565df12015-10-05 13:00:52 -0700438 info.damageAccumulator->pushTransform(&op->localMatrix);
439 bool childFunctorsNeedLayer = functorsNeedLayer; // TODO! || op->mRecordedWithPotentialStencilClip;
Chris Craika766cb22015-06-08 16:49:43 -0700440 childNode->prepareTreeImpl(info, childFunctorsNeedLayer);
John Recka447d292014-06-11 18:39:44 -0700441 info.damageAccumulator->popTransform();
John Reck5bf11bb2014-03-25 10:22:09 -0700442 }
John Reck113e0822014-03-18 09:22:59 -0700443 }
444}
445
John Reckaa6e84f2016-06-16 15:36:13 -0700446void RenderNode::destroyHardwareResources(TreeObserver* observer, TreeInfo* info) {
John Reckdcba6722014-07-08 13:59:49 -0700447 if (mLayer) {
Chris Craik0b7e8242015-10-28 16:50:44 -0700448 destroyLayer(mLayer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800449 mLayer = nullptr;
John Reckdcba6722014-07-08 13:59:49 -0700450 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700451 if (mDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700452 for (auto&& child : mDisplayList->getChildren()) {
John Reckaa6e84f2016-06-16 15:36:13 -0700453 child->renderNode->destroyHardwareResources(observer, info);
John Reckdcba6722014-07-08 13:59:49 -0700454 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700455 if (mNeedsDisplayListSync) {
John Reckdcba6722014-07-08 13:59:49 -0700456 // Next prepare tree we are going to push a new display list, so we can
457 // drop our current one now
John Reckaa6e84f2016-06-16 15:36:13 -0700458 deleteDisplayList(observer, info);
John Reckdcba6722014-07-08 13:59:49 -0700459 }
460 }
461}
462
John Reckaa6e84f2016-06-16 15:36:13 -0700463void RenderNode::decParentRefCount(TreeObserver* observer, TreeInfo* info) {
John Reckdcba6722014-07-08 13:59:49 -0700464 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
465 mParentCount--;
466 if (!mParentCount) {
John Reck44b49f02016-03-25 14:29:48 -0700467 if (observer) {
468 observer->onMaybeRemovedFromTree(this);
469 }
John Reckaa6e84f2016-06-16 15:36:13 -0700470 if (CC_UNLIKELY(mPositionListener.get())) {
471 mPositionListener->onPositionLost(*this, info);
472 }
John Reckdcba6722014-07-08 13:59:49 -0700473 // If a child of ours is being attached to our parent then this will incorrectly
474 // destroy its hardware resources. However, this situation is highly unlikely
475 // and the failure is "just" that the layer is re-created, so this should
476 // be safe enough
John Reckaa6e84f2016-06-16 15:36:13 -0700477 destroyHardwareResources(observer, info);
John Reckdcba6722014-07-08 13:59:49 -0700478 }
479}
480
John Reck113e0822014-03-18 09:22:59 -0700481/**
482 * Apply property-based transformations to input matrix
483 *
484 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
485 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
486 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700487void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700488 if (properties().getLeft() != 0 || properties().getTop() != 0) {
489 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700490 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700491 if (properties().getStaticMatrix()) {
492 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700493 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700494 } else if (properties().getAnimationMatrix()) {
495 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700496 matrix.multiply(anim);
497 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700498
Chris Craikcc39e162014-04-25 18:34:11 -0700499 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700500 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700501 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700502 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700503 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700504 } else {
505 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700506 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700507 } else {
508 mat4 true3dMat;
509 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700510 properties().getPivotX() + properties().getTranslationX(),
511 properties().getPivotY() + properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700512 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700513 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
514 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
515 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
516 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
517 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700518
519 matrix.multiply(true3dMat);
520 }
521 }
522 }
523}
524
525/**
526 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
527 *
528 * This should be called before a call to defer() or drawDisplayList()
529 *
530 * Each DisplayList that serves as a 3d root builds its list of composited children,
531 * which are flagged to not draw in the standard draw loop.
532 */
533void RenderNode::computeOrdering() {
534 ATRACE_CALL();
535 mProjectedNodes.clear();
536
537 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
538 // transform properties are applied correctly to top level children
Chris Craik003cc3d2015-10-16 10:24:55 -0700539 if (mDisplayList == nullptr) return;
Chris Craikb36af872015-10-16 14:23:12 -0700540 for (unsigned int i = 0; i < mDisplayList->getChildren().size(); i++) {
Chris Craik5e00c7c2016-07-06 16:10:09 -0700541 RenderNodeOp* childOp = mDisplayList->getChildren()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700542 childOp->renderNode->computeOrderingImpl(childOp, &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700543 }
544}
545
546void RenderNode::computeOrderingImpl(
Chris Craik5e00c7c2016-07-06 16:10:09 -0700547 RenderNodeOp* opState,
548 std::vector<RenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700549 const mat4* transformFromProjectionSurface) {
550 mProjectedNodes.clear();
Chris Craik003cc3d2015-10-16 10:24:55 -0700551 if (mDisplayList == nullptr || mDisplayList->isEmpty()) return;
John Reck113e0822014-03-18 09:22:59 -0700552
553 // TODO: should avoid this calculation in most cases
554 // TODO: just calculate single matrix, down to all leaf composited elements
555 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
Chris Craik8d1f2122015-11-24 16:40:09 -0800556 localTransformFromProjectionSurface.multiply(opState->localMatrix);
John Reck113e0822014-03-18 09:22:59 -0700557
John Reckd0a0b2a2014-03-20 16:28:56 -0700558 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700559 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
Chris Craik8d1f2122015-11-24 16:40:09 -0800560 opState->skipInOrderDraw = true;
561 opState->transformFromCompositingAncestor = localTransformFromProjectionSurface;
John Reck272a6852015-07-29 16:48:58 -0700562 compositedChildrenOfProjectionSurface->push_back(opState);
John Reck113e0822014-03-18 09:22:59 -0700563 } else {
564 // standard in order draw
Chris Craik8d1f2122015-11-24 16:40:09 -0800565 opState->skipInOrderDraw = false;
John Reck113e0822014-03-18 09:22:59 -0700566 }
567
Chris Craikb36af872015-10-16 14:23:12 -0700568 if (mDisplayList->getChildren().size() > 0) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700569 const bool isProjectionReceiver = mDisplayList->projectionReceiveIndex >= 0;
John Reck113e0822014-03-18 09:22:59 -0700570 bool haveAppliedPropertiesToProjection = false;
Chris Craikb36af872015-10-16 14:23:12 -0700571 for (unsigned int i = 0; i < mDisplayList->getChildren().size(); i++) {
Chris Craik5e00c7c2016-07-06 16:10:09 -0700572 RenderNodeOp* childOp = mDisplayList->getChildren()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700573 RenderNode* child = childOp->renderNode;
John Reck113e0822014-03-18 09:22:59 -0700574
Chris Craik5e00c7c2016-07-06 16:10:09 -0700575 std::vector<RenderNodeOp*>* projectionChildren = nullptr;
Chris Craikd41c4d82015-01-05 15:51:13 -0800576 const mat4* projectionTransform = nullptr;
John Reckd0a0b2a2014-03-20 16:28:56 -0700577 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
Chris Craikbf72eb82015-06-08 11:30:44 -0700578 // if receiving projections, collect projecting descendant
John Reck113e0822014-03-18 09:22:59 -0700579
Chris Craikbf72eb82015-06-08 11:30:44 -0700580 // Note that if a direct descendant is projecting backwards, we pass its
581 // grandparent projection collection, since it shouldn't project onto its
John Reck113e0822014-03-18 09:22:59 -0700582 // parent, where it will already be drawing.
583 projectionChildren = &mProjectedNodes;
584 projectionTransform = &mat4::identity();
585 } else {
586 if (!haveAppliedPropertiesToProjection) {
587 applyViewPropertyTransforms(localTransformFromProjectionSurface);
588 haveAppliedPropertiesToProjection = true;
589 }
590 projectionChildren = compositedChildrenOfProjectionSurface;
591 projectionTransform = &localTransformFromProjectionSurface;
592 }
Derek Sollenbergerf2932592015-08-13 14:59:33 -0400593 child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700594 }
595 }
John Reck113e0822014-03-18 09:22:59 -0700596}
597
John Reck113e0822014-03-18 09:22:59 -0700598} /* namespace uirenderer */
599} /* namespace android */