blob: 5330e2365d66ca5da945029e47951165c554021f [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"
John Reck25fbb3f2014-06-12 13:46:45 -070022#include "LayerRenderer.h"
Chris Craik5e00c7c2016-07-06 16:10:09 -070023#include "OpDumper.h"
24#include "RecordedOp.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040025#include "TreeInfo.h"
Chris Craike0bb87d2014-04-22 17:55:41 -070026#include "utils/MathUtils.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 */
Chris Craik91eff222016-02-22 13:39:33 -080072void RenderNode::output(uint32_t level, const char* label) {
73 ALOGD("%s (%s %p%s%s%s%s%s)",
74 label,
75 getName(),
76 this,
77 (MathUtils::isZero(properties().getAlpha()) ? ", zero alpha" : ""),
78 (properties().hasShadow() ? ", casting shadow" : ""),
79 (isRenderable() ? "" : ", empty"),
80 (properties().getProjectBackwards() ? ", projected" : ""),
81 (mLayer != nullptr ? ", on HW Layer" : ""));
82 properties().debugOutputProperties(level + 1);
83
84 if (mDisplayList) {
85 for (auto&& op : mDisplayList->getOps()) {
86 std::stringstream strout;
87 OpDumper::dump(*op, strout, level + 1);
88 if (op->opId == RecordedOpId::RenderNodeOp) {
89 auto rnOp = reinterpret_cast<const RenderNodeOp*>(op);
90 rnOp->renderNode->output(level + 1, strout.str().c_str());
91 } else {
92 ALOGD("%s", strout.str().c_str());
93 }
94 }
95 }
96 ALOGD("%*s/RenderNode(%s %p)", level * 2, "", getName(), this);
97}
John Reck113e0822014-03-18 09:22:59 -070098
John Recke248bd12015-08-05 13:53:53 -070099void RenderNode::copyTo(proto::RenderNode *pnode) {
100 pnode->set_id(static_cast<uint64_t>(
101 reinterpret_cast<uintptr_t>(this)));
102 pnode->set_name(mName.string(), mName.length());
103
104 proto::RenderProperties* pprops = pnode->mutable_properties();
105 pprops->set_left(properties().getLeft());
106 pprops->set_top(properties().getTop());
107 pprops->set_right(properties().getRight());
108 pprops->set_bottom(properties().getBottom());
109 pprops->set_clip_flags(properties().getClippingFlags());
110 pprops->set_alpha(properties().getAlpha());
111 pprops->set_translation_x(properties().getTranslationX());
112 pprops->set_translation_y(properties().getTranslationY());
113 pprops->set_translation_z(properties().getTranslationZ());
114 pprops->set_elevation(properties().getElevation());
115 pprops->set_rotation(properties().getRotation());
116 pprops->set_rotation_x(properties().getRotationX());
117 pprops->set_rotation_y(properties().getRotationY());
118 pprops->set_scale_x(properties().getScaleX());
119 pprops->set_scale_y(properties().getScaleY());
120 pprops->set_pivot_x(properties().getPivotX());
121 pprops->set_pivot_y(properties().getPivotY());
122 pprops->set_has_overlapping_rendering(properties().getHasOverlappingRendering());
123 pprops->set_pivot_explicitly_set(properties().isPivotExplicitlySet());
124 pprops->set_project_backwards(properties().getProjectBackwards());
125 pprops->set_projection_receiver(properties().isProjectionReceiver());
126 set(pprops->mutable_clip_bounds(), properties().getClipBounds());
127
128 const Outline& outline = properties().getOutline();
129 if (outline.getType() != Outline::Type::None) {
130 proto::Outline* poutline = pprops->mutable_outline();
131 poutline->clear_path();
132 if (outline.getType() == Outline::Type::Empty) {
133 poutline->set_type(proto::Outline_Type_Empty);
134 } else if (outline.getType() == Outline::Type::ConvexPath) {
135 poutline->set_type(proto::Outline_Type_ConvexPath);
136 if (const SkPath* path = outline.getPath()) {
137 set(poutline->mutable_path(), *path);
138 }
139 } else if (outline.getType() == Outline::Type::RoundRect) {
140 poutline->set_type(proto::Outline_Type_RoundRect);
141 } else {
142 ALOGW("Uknown outline type! %d", static_cast<int>(outline.getType()));
143 poutline->set_type(proto::Outline_Type_None);
144 }
145 poutline->set_should_clip(outline.getShouldClip());
146 poutline->set_alpha(outline.getAlpha());
147 poutline->set_radius(outline.getRadius());
148 set(poutline->mutable_bounds(), outline.getBounds());
149 } else {
150 pprops->clear_outline();
151 }
152
153 const RevealClip& revealClip = properties().getRevealClip();
154 if (revealClip.willClip()) {
155 proto::RevealClip* prevealClip = pprops->mutable_reveal_clip();
156 prevealClip->set_x(revealClip.getX());
157 prevealClip->set_y(revealClip.getY());
158 prevealClip->set_radius(revealClip.getRadius());
159 } else {
160 pprops->clear_reveal_clip();
161 }
162
163 pnode->clear_children();
Chris Craik003cc3d2015-10-16 10:24:55 -0700164 if (mDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700165 for (auto&& child : mDisplayList->getChildren()) {
Chris Craikb565df12015-10-05 13:00:52 -0700166 child->renderNode->copyTo(pnode->add_children());
John Recke248bd12015-08-05 13:53:53 -0700167 }
168 }
169}
170
John Reckfe5e7b72014-05-23 17:42:28 -0700171int RenderNode::getDebugSize() {
172 int size = sizeof(RenderNode);
Chris Craik003cc3d2015-10-16 10:24:55 -0700173 if (mStagingDisplayList) {
174 size += mStagingDisplayList->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700175 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700176 if (mDisplayList && mDisplayList != mStagingDisplayList) {
177 size += mDisplayList->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700178 }
179 return size;
180}
181
John Reckf4198b72014-04-09 17:00:04 -0700182void RenderNode::prepareTree(TreeInfo& info) {
183 ATRACE_CALL();
Chris Craik69e5adf2014-08-14 13:34:01 -0700184 LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
John Reckf4198b72014-04-09 17:00:04 -0700185
Chris Craika766cb22015-06-08 16:49:43 -0700186 // Functors don't correctly handle stencil usage of overdraw debugging - shove 'em in a layer.
187 bool functorsNeedLayer = Properties::debugOverdraw;
188
189 prepareTreeImpl(info, functorsNeedLayer);
John Reckf4198b72014-04-09 17:00:04 -0700190}
191
John Reck68bfe0a2014-06-24 15:34:58 -0700192void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
193 mAnimatorManager.addAnimator(animator);
194}
195
Doris Liu8b083202016-02-19 21:46:06 +0000196void RenderNode::removeAnimator(const sp<BaseRenderNodeAnimator>& animator) {
197 mAnimatorManager.removeAnimator(animator);
198}
199
John Recke4267ea2014-06-03 15:53:15 -0700200void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700201 if (isRenderable()) {
John Reck293e8682014-06-17 10:34:02 -0700202 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700203 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
204 } else {
205 // Hope this is big enough?
206 // TODO: Get this from the display list ops or something
John Reckc1288232015-08-12 13:39:11 -0700207 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
John Recka447d292014-06-11 18:39:44 -0700208 }
John Recke4267ea2014-06-03 15:53:15 -0700209 }
210}
211
John Recka7c2ea22014-08-08 13:21:00 -0700212void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700213 LayerType layerType = properties().effectiveLayerType();
Chris Craik182952f2015-03-09 14:17:29 -0700214 if (CC_UNLIKELY(layerType == LayerType::RenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700215 // Damage applied so far needs to affect our parent, but does not require
216 // the layer to be updated. So we pop/push here to clear out the current
217 // damage and get a clean state for display list or children updates to
218 // affect, which will require the layer to be updated
219 info.damageAccumulator->popTransform();
220 info.damageAccumulator->pushTransform(this);
221 if (dirtyMask & DISPLAY_LIST) {
222 damageSelf(info);
223 }
John Reck25fbb3f2014-06-12 13:46:45 -0700224 }
225}
226
Chris Craik5e00c7c2016-07-06 16:10:09 -0700227static OffscreenBuffer* createLayer(RenderState& renderState, uint32_t width, uint32_t height) {
Chris Craik9fded232015-11-11 16:42:34 -0800228 return renderState.layerPool().get(renderState, width, height);
Chris Craik0b7e8242015-10-28 16:50:44 -0700229}
230
Chris Craik5e00c7c2016-07-06 16:10:09 -0700231static void destroyLayer(OffscreenBuffer* layer) {
Chris Craik9fded232015-11-11 16:42:34 -0800232 RenderState& renderState = layer->renderState;
233 renderState.layerPool().putOrDelete(layer);
Chris Craik0b7e8242015-10-28 16:50:44 -0700234}
235
Chris Craik5e00c7c2016-07-06 16:10:09 -0700236static bool layerMatchesWidthAndHeight(OffscreenBuffer* layer, int width, int height) {
Chris Craik9fded232015-11-11 16:42:34 -0800237 return layer->viewportWidth == (uint32_t) width && layer->viewportHeight == (uint32_t)height;
Chris Craik9fded232015-11-11 16:42:34 -0800238}
239
John Reck25fbb3f2014-06-12 13:46:45 -0700240void RenderNode::pushLayerUpdate(TreeInfo& info) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700241 LayerType layerType = properties().effectiveLayerType();
John Reck25fbb3f2014-06-12 13:46:45 -0700242 // If we are not a layer OR we cannot be rendered (eg, view was detached)
243 // we need to destroy any Layers we may have had previously
Chris Craik182952f2015-03-09 14:17:29 -0700244 if (CC_LIKELY(layerType != LayerType::RenderLayer) || CC_UNLIKELY(!isRenderable())) {
John Reck25fbb3f2014-06-12 13:46:45 -0700245 if (CC_UNLIKELY(mLayer)) {
Chris Craik0b7e8242015-10-28 16:50:44 -0700246 destroyLayer(mLayer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800247 mLayer = nullptr;
John Reck25fbb3f2014-06-12 13:46:45 -0700248 }
249 return;
250 }
251
Chris Craik69e5adf2014-08-14 13:34:01 -0700252 bool transformUpdateNeeded = false;
John Reck25fbb3f2014-06-12 13:46:45 -0700253 if (!mLayer) {
Chris Craik9fded232015-11-11 16:42:34 -0800254 mLayer = createLayer(info.canvasContext.getRenderState(), getWidth(), getHeight());
255 damageSelf(info);
256 transformUpdateNeeded = true;
257 } else if (!layerMatchesWidthAndHeight(mLayer, getWidth(), getHeight())) {
Chris Craikd4fe4d32016-06-09 16:57:11 -0700258 // TODO: remove now irrelevant, currently enqueued damage (respecting damage ordering)
259 // Or, ideally, maintain damage between frames on node/layer so ordering is always correct
Chris Craik9fded232015-11-11 16:42:34 -0800260 RenderState& renderState = mLayer->renderState;
261 if (properties().fitsOnLayer()) {
262 mLayer = renderState.layerPool().resize(mLayer, getWidth(), getHeight());
263 } else {
Chris Craik0b7e8242015-10-28 16:50:44 -0700264 destroyLayer(mLayer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800265 mLayer = nullptr;
John Reckc25e5062014-06-18 14:21:29 -0700266 }
John Reck25fbb3f2014-06-12 13:46:45 -0700267 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700268 transformUpdateNeeded = true;
269 }
270
John Reck25fbb3f2014-06-12 13:46:45 -0700271 SkRect dirty;
272 info.damageAccumulator->peekAtDirty(&dirty);
John Reck25fbb3f2014-06-12 13:46:45 -0700273
John Reckc25e5062014-06-18 14:21:29 -0700274 if (!mLayer) {
John Reck0e89e2b2014-10-31 14:49:06 -0700275 Caches::getInstance().dumpMemoryUsage();
John Reckc25e5062014-06-18 14:21:29 -0700276 if (info.errorHandler) {
John Reck77c40102015-10-26 15:49:47 -0700277 std::ostringstream err;
278 err << "Unable to create layer for " << getName();
Chris Craik76caecf2015-11-02 19:17:45 -0800279 const int maxTextureSize = Caches::getInstance().maxTextureSize;
John Reck77c40102015-10-26 15:49:47 -0700280 if (getWidth() > maxTextureSize || getHeight() > maxTextureSize) {
281 err << ", size " << getWidth() << "x" << getHeight()
282 << " exceeds max size " << maxTextureSize;
283 } else {
284 err << ", see logcat for more info";
285 }
286 info.errorHandler->onError(err.str());
John Reckc25e5062014-06-18 14:21:29 -0700287 }
288 return;
289 }
290
Chris Craik98787e62015-11-13 10:55:30 -0800291 if (transformUpdateNeeded && mLayer) {
Chris Craikc71bfca2014-08-21 10:18:58 -0700292 // update the transform in window of the layer to reset its origin wrt light source position
293 Matrix4 windowTransform;
294 info.damageAccumulator->computeCurrentTransform(&windowTransform);
295 mLayer->setWindowTransform(windowTransform);
296 }
John Reckc79eabc2014-08-05 11:03:42 -0700297
Chris Craik0b7e8242015-10-28 16:50:44 -0700298 info.layerUpdateQueue->enqueueLayerWithDamage(this, dirty);
John Reck998a6d82014-08-28 15:35:53 -0700299
Chris Craike2e53a72015-10-28 15:55:40 -0700300 // There might be prefetched layers that need to be accounted for.
301 // That might be us, so tell CanvasContext that this layer is in the
302 // tree and should not be destroyed.
303 info.canvasContext.markLayerInUse(this);
John Reck25fbb3f2014-06-12 13:46:45 -0700304}
305
Chris Craika766cb22015-06-08 16:49:43 -0700306/**
307 * Traverse down the the draw tree to prepare for a frame.
308 *
309 * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven
310 *
311 * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the
312 * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer.
313 */
314void RenderNode::prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer) {
John Recka447d292014-06-11 18:39:44 -0700315 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700316
John Reckdcba6722014-07-08 13:59:49 -0700317 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700318 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700319 }
John Reck9eb9f6f2014-08-21 11:23:05 -0700320 uint32_t animatorDirtyMask = 0;
321 if (CC_LIKELY(info.runAnimations)) {
322 animatorDirtyMask = mAnimatorManager.animate(info);
323 }
Chris Craika766cb22015-06-08 16:49:43 -0700324
John Reck3f725f02015-06-16 10:29:31 -0700325 bool willHaveFunctor = false;
Chris Craik003cc3d2015-10-16 10:24:55 -0700326 if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700327 willHaveFunctor = !mStagingDisplayList->getFunctors().empty();
Chris Craik003cc3d2015-10-16 10:24:55 -0700328 } else if (mDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700329 willHaveFunctor = !mDisplayList->getFunctors().empty();
John Reck3f725f02015-06-16 10:29:31 -0700330 }
Chris Craika766cb22015-06-08 16:49:43 -0700331 bool childFunctorsNeedLayer = mProperties.prepareForFunctorPresence(
332 willHaveFunctor, functorsNeedLayer);
333
John Reckf6481082016-02-02 15:18:23 -0800334 if (CC_UNLIKELY(mPositionListener.get())) {
335 mPositionListener->onPositionUpdated(*this, info);
336 }
337
John Recka7c2ea22014-08-08 13:21:00 -0700338 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700339 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700340 pushStagingDisplayListChanges(info);
341 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700342 prepareSubTree(info, childFunctorsNeedLayer, mDisplayList);
John Reck25fbb3f2014-06-12 13:46:45 -0700343 pushLayerUpdate(info);
344
Doris Liu07c056d2016-06-13 12:52:44 -0700345 if (mDisplayList) {
346 for (auto& vectorDrawable : mDisplayList->getVectorDrawables()) {
347 // If any vector drawable in the display list needs update, damage the node.
348 if (vectorDrawable->isDirty()) {
349 damageSelf(info);
350 }
351 vectorDrawable->setPropertyChangeWillBeConsumed(true);
Doris Liu718cd3e2016-05-17 16:50:31 -0700352 }
Doris Liu718cd3e2016-05-17 16:50:31 -0700353 }
354
John Recka447d292014-06-11 18:39:44 -0700355 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700356}
357
Chris Craikb565df12015-10-05 13:00:52 -0700358void RenderNode::syncProperties() {
359 mProperties = mStagingProperties;
360}
361
John Reck25fbb3f2014-06-12 13:46:45 -0700362void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reckff941dc2014-05-14 16:34:14 -0700363 // Push the animators first so that setupStartValueIfNecessary() is called
364 // before properties() is trampled by stagingProperties(), as they are
365 // required by some animators.
John Reck9eb9f6f2014-08-21 11:23:05 -0700366 if (CC_LIKELY(info.runAnimations)) {
John Reck119907c2014-08-14 09:02:01 -0700367 mAnimatorManager.pushStaging();
John Reck9eb9f6f2014-08-21 11:23:05 -0700368 }
John Reckff941dc2014-05-14 16:34:14 -0700369 if (mDirtyPropertyFields) {
370 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700371 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700372 info.damageAccumulator->popTransform();
Chris Craikb565df12015-10-05 13:00:52 -0700373 syncProperties();
John Recke4267ea2014-06-03 15:53:15 -0700374 // We could try to be clever and only re-damage if the matrix changed.
375 // However, we don't need to worry about that. The cost of over-damaging
376 // here is only going to be a single additional map rect of this node
377 // plus a rect join(). The parent's transform (and up) will only be
378 // performed once.
John Recka447d292014-06-11 18:39:44 -0700379 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700380 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700381 }
John Reck25fbb3f2014-06-12 13:46:45 -0700382}
383
John Reckaa6e84f2016-06-16 15:36:13 -0700384void RenderNode::syncDisplayList(TreeInfo* info) {
Chris Craikb565df12015-10-05 13:00:52 -0700385 // Make sure we inc first so that we don't fluctuate between 0 and 1,
386 // which would thrash the layer cache
Chris Craik003cc3d2015-10-16 10:24:55 -0700387 if (mStagingDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700388 for (auto&& child : mStagingDisplayList->getChildren()) {
Chris Craikb565df12015-10-05 13:00:52 -0700389 child->renderNode->incParentRefCount();
390 }
391 }
John Reckaa6e84f2016-06-16 15:36:13 -0700392 deleteDisplayList(info ? info->observer : nullptr, info);
Chris Craik003cc3d2015-10-16 10:24:55 -0700393 mDisplayList = mStagingDisplayList;
394 mStagingDisplayList = nullptr;
395 if (mDisplayList) {
John Reckcd1c3eb2016-04-14 10:38:54 -0700396 for (auto& iter : mDisplayList->getFunctors()) {
397 (*iter.functor)(DrawGlInfo::kModeSync, nullptr);
Chris Craikb565df12015-10-05 13:00:52 -0700398 }
Doris Liu718cd3e2016-05-17 16:50:31 -0700399 for (auto& vectorDrawable : mDisplayList->getVectorDrawables()) {
400 vectorDrawable->syncProperties();
Doris Liu1d8e1942016-03-02 15:16:28 -0800401 }
Chris Craikb565df12015-10-05 13:00:52 -0700402 }
403}
404
John Reck25fbb3f2014-06-12 13:46:45 -0700405void RenderNode::pushStagingDisplayListChanges(TreeInfo& info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700406 if (mNeedsDisplayListSync) {
407 mNeedsDisplayListSync = false;
John Reck5c9d7172014-10-22 11:32:27 -0700408 // Damage with the old display list first then the new one to catch any
409 // changes in isRenderable or, in the future, bounds
410 damageSelf(info);
John Reckaa6e84f2016-06-16 15:36:13 -0700411 syncDisplayList(&info);
John Recke4267ea2014-06-03 15:53:15 -0700412 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700413 }
John Reck8de65a82014-04-09 15:23:38 -0700414}
415
John Reckaa6e84f2016-06-16 15:36:13 -0700416void RenderNode::deleteDisplayList(TreeObserver* observer, TreeInfo* info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700417 if (mDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700418 for (auto&& child : mDisplayList->getChildren()) {
John Reckaa6e84f2016-06-16 15:36:13 -0700419 child->renderNode->decParentRefCount(observer, info);
John Reckdcba6722014-07-08 13:59:49 -0700420 }
421 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700422 delete mDisplayList;
423 mDisplayList = nullptr;
John Reckdcba6722014-07-08 13:59:49 -0700424}
425
Chris Craik003cc3d2015-10-16 10:24:55 -0700426void RenderNode::prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayList* subtree) {
John Reck8de65a82014-04-09 15:23:38 -0700427 if (subtree) {
John Reck860d1552014-04-11 19:15:05 -0700428 TextureCache& cache = Caches::getInstance().textureCache;
Chris Craikb36af872015-10-16 14:23:12 -0700429 info.out.hasFunctors |= subtree->getFunctors().size();
430 for (auto&& bitmapResource : subtree->getBitmapResources()) {
Chris Craike2e53a72015-10-28 15:55:40 -0700431 void* ownerToken = &info.canvasContext;
432 info.prepareTextures = cache.prefetchAndMarkInUse(ownerToken, bitmapResource);
John Reckf4198b72014-04-09 17:00:04 -0700433 }
Chris Craikb36af872015-10-16 14:23:12 -0700434 for (auto&& op : subtree->getChildren()) {
Chris Craikb565df12015-10-05 13:00:52 -0700435 RenderNode* childNode = op->renderNode;
Chris Craikb565df12015-10-05 13:00:52 -0700436 info.damageAccumulator->pushTransform(&op->localMatrix);
437 bool childFunctorsNeedLayer = functorsNeedLayer; // TODO! || op->mRecordedWithPotentialStencilClip;
Chris Craika766cb22015-06-08 16:49:43 -0700438 childNode->prepareTreeImpl(info, childFunctorsNeedLayer);
John Recka447d292014-06-11 18:39:44 -0700439 info.damageAccumulator->popTransform();
John Reck5bf11bb2014-03-25 10:22:09 -0700440 }
John Reck113e0822014-03-18 09:22:59 -0700441 }
442}
443
John Reckaa6e84f2016-06-16 15:36:13 -0700444void RenderNode::destroyHardwareResources(TreeObserver* observer, TreeInfo* info) {
John Reckdcba6722014-07-08 13:59:49 -0700445 if (mLayer) {
Chris Craik0b7e8242015-10-28 16:50:44 -0700446 destroyLayer(mLayer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800447 mLayer = nullptr;
John Reckdcba6722014-07-08 13:59:49 -0700448 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700449 if (mDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700450 for (auto&& child : mDisplayList->getChildren()) {
John Reckaa6e84f2016-06-16 15:36:13 -0700451 child->renderNode->destroyHardwareResources(observer, info);
John Reckdcba6722014-07-08 13:59:49 -0700452 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700453 if (mNeedsDisplayListSync) {
John Reckdcba6722014-07-08 13:59:49 -0700454 // Next prepare tree we are going to push a new display list, so we can
455 // drop our current one now
John Reckaa6e84f2016-06-16 15:36:13 -0700456 deleteDisplayList(observer, info);
John Reckdcba6722014-07-08 13:59:49 -0700457 }
458 }
459}
460
John Reckaa6e84f2016-06-16 15:36:13 -0700461void RenderNode::decParentRefCount(TreeObserver* observer, TreeInfo* info) {
John Reckdcba6722014-07-08 13:59:49 -0700462 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
463 mParentCount--;
464 if (!mParentCount) {
John Reck44b49f02016-03-25 14:29:48 -0700465 if (observer) {
466 observer->onMaybeRemovedFromTree(this);
467 }
John Reckaa6e84f2016-06-16 15:36:13 -0700468 if (CC_UNLIKELY(mPositionListener.get())) {
469 mPositionListener->onPositionLost(*this, info);
470 }
John Reckdcba6722014-07-08 13:59:49 -0700471 // If a child of ours is being attached to our parent then this will incorrectly
472 // destroy its hardware resources. However, this situation is highly unlikely
473 // and the failure is "just" that the layer is re-created, so this should
474 // be safe enough
John Reckaa6e84f2016-06-16 15:36:13 -0700475 destroyHardwareResources(observer, info);
John Reckdcba6722014-07-08 13:59:49 -0700476 }
477}
478
John Reck113e0822014-03-18 09:22:59 -0700479/**
480 * Apply property-based transformations to input matrix
481 *
482 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
483 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
484 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700485void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700486 if (properties().getLeft() != 0 || properties().getTop() != 0) {
487 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700488 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700489 if (properties().getStaticMatrix()) {
490 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700491 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700492 } else if (properties().getAnimationMatrix()) {
493 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700494 matrix.multiply(anim);
495 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700496
Chris Craikcc39e162014-04-25 18:34:11 -0700497 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700498 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700499 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700500 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700501 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700502 } else {
503 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700504 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700505 } else {
506 mat4 true3dMat;
507 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700508 properties().getPivotX() + properties().getTranslationX(),
509 properties().getPivotY() + properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700510 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700511 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
512 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
513 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
514 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
515 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700516
517 matrix.multiply(true3dMat);
518 }
519 }
520 }
521}
522
523/**
524 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
525 *
526 * This should be called before a call to defer() or drawDisplayList()
527 *
528 * Each DisplayList that serves as a 3d root builds its list of composited children,
529 * which are flagged to not draw in the standard draw loop.
530 */
531void RenderNode::computeOrdering() {
532 ATRACE_CALL();
533 mProjectedNodes.clear();
534
535 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
536 // transform properties are applied correctly to top level children
Chris Craik003cc3d2015-10-16 10:24:55 -0700537 if (mDisplayList == nullptr) return;
Chris Craikb36af872015-10-16 14:23:12 -0700538 for (unsigned int i = 0; i < mDisplayList->getChildren().size(); i++) {
Chris Craik5e00c7c2016-07-06 16:10:09 -0700539 RenderNodeOp* childOp = mDisplayList->getChildren()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700540 childOp->renderNode->computeOrderingImpl(childOp, &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700541 }
542}
543
544void RenderNode::computeOrderingImpl(
Chris Craik5e00c7c2016-07-06 16:10:09 -0700545 RenderNodeOp* opState,
546 std::vector<RenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700547 const mat4* transformFromProjectionSurface) {
548 mProjectedNodes.clear();
Chris Craik003cc3d2015-10-16 10:24:55 -0700549 if (mDisplayList == nullptr || mDisplayList->isEmpty()) return;
John Reck113e0822014-03-18 09:22:59 -0700550
551 // TODO: should avoid this calculation in most cases
552 // TODO: just calculate single matrix, down to all leaf composited elements
553 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
Chris Craik8d1f2122015-11-24 16:40:09 -0800554 localTransformFromProjectionSurface.multiply(opState->localMatrix);
John Reck113e0822014-03-18 09:22:59 -0700555
John Reckd0a0b2a2014-03-20 16:28:56 -0700556 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700557 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
Chris Craik8d1f2122015-11-24 16:40:09 -0800558 opState->skipInOrderDraw = true;
559 opState->transformFromCompositingAncestor = localTransformFromProjectionSurface;
John Reck272a6852015-07-29 16:48:58 -0700560 compositedChildrenOfProjectionSurface->push_back(opState);
John Reck113e0822014-03-18 09:22:59 -0700561 } else {
562 // standard in order draw
Chris Craik8d1f2122015-11-24 16:40:09 -0800563 opState->skipInOrderDraw = false;
John Reck113e0822014-03-18 09:22:59 -0700564 }
565
Chris Craikb36af872015-10-16 14:23:12 -0700566 if (mDisplayList->getChildren().size() > 0) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700567 const bool isProjectionReceiver = mDisplayList->projectionReceiveIndex >= 0;
John Reck113e0822014-03-18 09:22:59 -0700568 bool haveAppliedPropertiesToProjection = false;
Chris Craikb36af872015-10-16 14:23:12 -0700569 for (unsigned int i = 0; i < mDisplayList->getChildren().size(); i++) {
Chris Craik5e00c7c2016-07-06 16:10:09 -0700570 RenderNodeOp* childOp = mDisplayList->getChildren()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700571 RenderNode* child = childOp->renderNode;
John Reck113e0822014-03-18 09:22:59 -0700572
Chris Craik5e00c7c2016-07-06 16:10:09 -0700573 std::vector<RenderNodeOp*>* projectionChildren = nullptr;
Chris Craikd41c4d82015-01-05 15:51:13 -0800574 const mat4* projectionTransform = nullptr;
John Reckd0a0b2a2014-03-20 16:28:56 -0700575 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
Chris Craikbf72eb82015-06-08 11:30:44 -0700576 // if receiving projections, collect projecting descendant
John Reck113e0822014-03-18 09:22:59 -0700577
Chris Craikbf72eb82015-06-08 11:30:44 -0700578 // Note that if a direct descendant is projecting backwards, we pass its
579 // grandparent projection collection, since it shouldn't project onto its
John Reck113e0822014-03-18 09:22:59 -0700580 // parent, where it will already be drawing.
581 projectionChildren = &mProjectedNodes;
582 projectionTransform = &mat4::identity();
583 } else {
584 if (!haveAppliedPropertiesToProjection) {
585 applyViewPropertyTransforms(localTransformFromProjectionSurface);
586 haveAppliedPropertiesToProjection = true;
587 }
588 projectionChildren = compositedChildrenOfProjectionSurface;
589 projectionTransform = &localTransformFromProjectionSurface;
590 }
Derek Sollenbergerf2932592015-08-13 14:59:33 -0400591 child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700592 }
593 }
John Reck113e0822014-03-18 09:22:59 -0700594}
595
John Reck113e0822014-03-18 09:22:59 -0700596} /* namespace uirenderer */
597} /* namespace android */