blob: 5ac330bcadb8aab4ca0011f7bb9b8a1fa0cf13ff [file] [log] [blame]
John Reck113e0822014-03-18 09:22:59 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
John Reck113e0822014-03-18 09:22:59 -070017#include "RenderNode.h"
18
Chris Craik5e00c7c2016-07-06 16:10:09 -070019#include "BakedOpRenderer.h"
John Recke4267ea2014-06-03 15:53:15 -070020#include "DamageAccumulator.h"
John Reck113e0822014-03-18 09:22:59 -070021#include "Debug.h"
Chris Craik5e00c7c2016-07-06 16:10:09 -070022#include "RecordedOp.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040023#include "TreeInfo.h"
John Reck1bcacfd2017-11-03 10:12:19 -070024#include "VectorDrawable.h"
25#include "renderstate/RenderState.h"
26#include "renderthread/CanvasContext.h"
John Reck2de950d2017-01-25 10:58:30 -080027#include "utils/FatVector.h"
Chris Craike0bb87d2014-04-22 17:55:41 -070028#include "utils/MathUtils.h"
sergeyvc3849aa2016-08-08 13:22:06 -070029#include "utils/StringUtils.h"
Chris Craik70850ea2014-11-18 10:49:23 -080030#include "utils/TraceUtils.h"
John Reck113e0822014-03-18 09:22:59 -070031
Derek Sollenberger579317d42017-08-29 16:33:49 -040032#include <SkPathOps.h>
John Reck77c40102015-10-26 15:49:47 -070033#include <algorithm>
34#include <sstream>
35#include <string>
36
John Reck113e0822014-03-18 09:22:59 -070037namespace android {
38namespace uirenderer {
39
John Reck2de950d2017-01-25 10:58:30 -080040// Used for tree mutations that are purely destructive.
41// Generic tree mutations should use MarkAndSweepObserver instead
42class ImmediateRemoved : public TreeObserver {
43public:
44 explicit ImmediateRemoved(TreeInfo* info) : mTreeInfo(info) {}
45
John Reck1bcacfd2017-11-03 10:12:19 -070046 void onMaybeRemovedFromTree(RenderNode* node) override { node->onRemovedFromTree(mTreeInfo); }
John Reck2de950d2017-01-25 10:58:30 -080047
48private:
49 TreeInfo* mTreeInfo;
50};
51
John Reck8de65a82014-04-09 15:23:38 -070052RenderNode::RenderNode()
John Reckff941dc2014-05-14 16:34:14 -070053 : mDirtyPropertyFields(0)
Chris Craik003cc3d2015-10-16 10:24:55 -070054 , mNeedsDisplayListSync(false)
55 , mDisplayList(nullptr)
56 , mStagingDisplayList(nullptr)
John Reck68bfe0a2014-06-24 15:34:58 -070057 , mAnimatorManager(*this)
John Reck1bcacfd2017-11-03 10:12:19 -070058 , mParentCount(0) {}
John Reck113e0822014-03-18 09:22:59 -070059
60RenderNode::~RenderNode() {
John Reck2de950d2017-01-25 10:58:30 -080061 ImmediateRemoved observer(nullptr);
62 deleteDisplayList(observer);
Chris Craik003cc3d2015-10-16 10:24:55 -070063 delete mStagingDisplayList;
Derek Sollenberger0df62092016-09-27 16:04:42 -040064 LOG_ALWAYS_FATAL_IF(hasLayer(), "layer missed detachment!");
John Reck113e0822014-03-18 09:22:59 -070065}
66
John Reck2de950d2017-01-25 10:58:30 -080067void RenderNode::setStagingDisplayList(DisplayList* displayList) {
68 mValid = (displayList != nullptr);
Chris Craik003cc3d2015-10-16 10:24:55 -070069 mNeedsDisplayListSync = true;
70 delete mStagingDisplayList;
71 mStagingDisplayList = displayList;
John Reck113e0822014-03-18 09:22:59 -070072}
73
74/**
75 * This function is a simplified version of replay(), where we simply retrieve and log the
76 * display list. This function should remain in sync with the replay() function.
77 */
sergeyvc3849aa2016-08-08 13:22:06 -070078void RenderNode::output() {
79 LogcatStream strout;
80 strout << "Root";
81 output(strout, 0);
82}
83
84void RenderNode::output(std::ostream& output, uint32_t level) {
85 output << " (" << getName() << " " << this
John Reck1bcacfd2017-11-03 10:12:19 -070086 << (MathUtils::isZero(properties().getAlpha()) ? ", zero alpha" : "")
87 << (properties().hasShadow() ? ", casting shadow" : "")
88 << (isRenderable() ? "" : ", empty")
89 << (properties().getProjectBackwards() ? ", projected" : "")
90 << (hasLayer() ? ", on HW Layer" : "") << ")" << std::endl;
sergeyvc3849aa2016-08-08 13:22:06 -070091
92 properties().debugOutputProperties(output, level + 1);
Chris Craik91eff222016-02-22 13:39:33 -080093
94 if (mDisplayList) {
Stan Ilievd2172372017-02-09 16:59:27 -050095 mDisplayList->output(output, level);
Chris Craik91eff222016-02-22 13:39:33 -080096 }
sergeyvc3849aa2016-08-08 13:22:06 -070097 output << std::string(level * 2, ' ') << "/RenderNode(" << getName() << " " << this << ")";
98 output << std::endl;
Chris Craik91eff222016-02-22 13:39:33 -080099}
John Reck113e0822014-03-18 09:22:59 -0700100
John Reckfe5e7b72014-05-23 17:42:28 -0700101int RenderNode::getDebugSize() {
102 int size = sizeof(RenderNode);
Chris Craik003cc3d2015-10-16 10:24:55 -0700103 if (mStagingDisplayList) {
104 size += mStagingDisplayList->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700105 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700106 if (mDisplayList && mDisplayList != mStagingDisplayList) {
107 size += mDisplayList->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700108 }
109 return size;
110}
111
John Reckf4198b72014-04-09 17:00:04 -0700112void RenderNode::prepareTree(TreeInfo& info) {
113 ATRACE_CALL();
Chris Craik69e5adf2014-08-14 13:34:01 -0700114 LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
John Reck2de950d2017-01-25 10:58:30 -0800115 MarkAndSweepRemoved observer(&info);
John Reckf4198b72014-04-09 17:00:04 -0700116
Matt Sarettf58cc922016-11-14 18:33:38 -0500117 // The OpenGL renderer reserves the stencil buffer for overdraw debugging. Functors
118 // will need to be drawn in a layer.
119 bool functorsNeedLayer = Properties::debugOverdraw && !Properties::isSkiaEnabled();
Chris Craika766cb22015-06-08 16:49:43 -0700120
John Reck2de950d2017-01-25 10:58:30 -0800121 prepareTreeImpl(observer, info, functorsNeedLayer);
John Reckf4198b72014-04-09 17:00:04 -0700122}
123
John Reck68bfe0a2014-06-24 15:34:58 -0700124void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
125 mAnimatorManager.addAnimator(animator);
126}
127
Doris Liu8b083202016-02-19 21:46:06 +0000128void RenderNode::removeAnimator(const sp<BaseRenderNodeAnimator>& animator) {
129 mAnimatorManager.removeAnimator(animator);
130}
131
John Recke4267ea2014-06-03 15:53:15 -0700132void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700133 if (isRenderable()) {
John Reck293e8682014-06-17 10:34:02 -0700134 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700135 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
136 } else {
137 // Hope this is big enough?
138 // TODO: Get this from the display list ops or something
John Reckc1288232015-08-12 13:39:11 -0700139 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
John Recka447d292014-06-11 18:39:44 -0700140 }
John Recke4267ea2014-06-03 15:53:15 -0700141 }
142}
143
John Recka7c2ea22014-08-08 13:21:00 -0700144void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700145 LayerType layerType = properties().effectiveLayerType();
Chris Craik182952f2015-03-09 14:17:29 -0700146 if (CC_UNLIKELY(layerType == LayerType::RenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700147 // Damage applied so far needs to affect our parent, but does not require
148 // the layer to be updated. So we pop/push here to clear out the current
149 // damage and get a clean state for display list or children updates to
150 // affect, which will require the layer to be updated
151 info.damageAccumulator->popTransform();
152 info.damageAccumulator->pushTransform(this);
153 if (dirtyMask & DISPLAY_LIST) {
154 damageSelf(info);
155 }
John Reck25fbb3f2014-06-12 13:46:45 -0700156 }
157}
158
159void RenderNode::pushLayerUpdate(TreeInfo& info) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700160 LayerType layerType = properties().effectiveLayerType();
John Reck25fbb3f2014-06-12 13:46:45 -0700161 // If we are not a layer OR we cannot be rendered (eg, view was detached)
162 // we need to destroy any Layers we may have had previously
John Reck1bcacfd2017-11-03 10:12:19 -0700163 if (CC_LIKELY(layerType != LayerType::RenderLayer) || CC_UNLIKELY(!isRenderable()) ||
164 CC_UNLIKELY(properties().getWidth() == 0) || CC_UNLIKELY(properties().getHeight() == 0) ||
165 CC_UNLIKELY(!properties().fitsOnLayer())) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400166 if (CC_UNLIKELY(hasLayer())) {
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400167 renderthread::CanvasContext::destroyLayer(this);
John Reck25fbb3f2014-06-12 13:46:45 -0700168 }
169 return;
170 }
171
Stan Iliev216b1572018-03-26 14:29:50 -0400172 if (info.canvasContext.createOrUpdateLayer(this, *info.damageAccumulator, info.errorHandler)) {
Chris Craik9fded232015-11-11 16:42:34 -0800173 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700174 }
175
Derek Sollenberger0df62092016-09-27 16:04:42 -0400176 if (!hasLayer()) {
John Reckc25e5062014-06-18 14:21:29 -0700177 return;
178 }
179
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400180 SkRect dirty;
181 info.damageAccumulator->peekAtDirty(&dirty);
Chris Craik0b7e8242015-10-28 16:50:44 -0700182 info.layerUpdateQueue->enqueueLayerWithDamage(this, dirty);
John Reck998a6d82014-08-28 15:35:53 -0700183
Chris Craike2e53a72015-10-28 15:55:40 -0700184 // There might be prefetched layers that need to be accounted for.
185 // That might be us, so tell CanvasContext that this layer is in the
186 // tree and should not be destroyed.
187 info.canvasContext.markLayerInUse(this);
John Reck25fbb3f2014-06-12 13:46:45 -0700188}
189
Chris Craika766cb22015-06-08 16:49:43 -0700190/**
191 * Traverse down the the draw tree to prepare for a frame.
192 *
193 * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven
194 *
195 * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the
196 * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer.
197 */
John Reck2de950d2017-01-25 10:58:30 -0800198void RenderNode::prepareTreeImpl(TreeObserver& observer, TreeInfo& info, bool functorsNeedLayer) {
John Recka447d292014-06-11 18:39:44 -0700199 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700200
John Reckdcba6722014-07-08 13:59:49 -0700201 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700202 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700203 }
John Reck9eb9f6f2014-08-21 11:23:05 -0700204 uint32_t animatorDirtyMask = 0;
205 if (CC_LIKELY(info.runAnimations)) {
206 animatorDirtyMask = mAnimatorManager.animate(info);
207 }
Chris Craika766cb22015-06-08 16:49:43 -0700208
John Reck3f725f02015-06-16 10:29:31 -0700209 bool willHaveFunctor = false;
Chris Craik003cc3d2015-10-16 10:24:55 -0700210 if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400211 willHaveFunctor = mStagingDisplayList->hasFunctor();
Chris Craik003cc3d2015-10-16 10:24:55 -0700212 } else if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400213 willHaveFunctor = mDisplayList->hasFunctor();
John Reck3f725f02015-06-16 10:29:31 -0700214 }
John Reck1bcacfd2017-11-03 10:12:19 -0700215 bool childFunctorsNeedLayer =
216 mProperties.prepareForFunctorPresence(willHaveFunctor, functorsNeedLayer);
Chris Craika766cb22015-06-08 16:49:43 -0700217
John Reckf6481082016-02-02 15:18:23 -0800218 if (CC_UNLIKELY(mPositionListener.get())) {
219 mPositionListener->onPositionUpdated(*this, info);
220 }
221
John Recka7c2ea22014-08-08 13:21:00 -0700222 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700223 if (info.mode == TreeInfo::MODE_FULL) {
John Reck2de950d2017-01-25 10:58:30 -0800224 pushStagingDisplayListChanges(observer, info);
John Reck25fbb3f2014-06-12 13:46:45 -0700225 }
John Reck25fbb3f2014-06-12 13:46:45 -0700226
Doris Liu07c056d2016-06-13 12:52:44 -0700227 if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400228 info.out.hasFunctors |= mDisplayList->hasFunctor();
John Reck1bcacfd2017-11-03 10:12:19 -0700229 bool isDirty = mDisplayList->prepareListAndChildren(
230 observer, info, childFunctorsNeedLayer,
231 [](RenderNode* child, TreeObserver& observer, TreeInfo& info,
232 bool functorsNeedLayer) {
233 child->prepareTreeImpl(observer, info, functorsNeedLayer);
234 });
Derek Sollenberger0df62092016-09-27 16:04:42 -0400235 if (isDirty) {
236 damageSelf(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700237 }
Doris Liu718cd3e2016-05-17 16:50:31 -0700238 }
Doris Liub51b2862016-08-01 19:56:47 -0700239 pushLayerUpdate(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700240
John Recka447d292014-06-11 18:39:44 -0700241 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700242}
243
Chris Craikb565df12015-10-05 13:00:52 -0700244void RenderNode::syncProperties() {
245 mProperties = mStagingProperties;
246}
247
John Reck25fbb3f2014-06-12 13:46:45 -0700248void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reckff941dc2014-05-14 16:34:14 -0700249 // Push the animators first so that setupStartValueIfNecessary() is called
250 // before properties() is trampled by stagingProperties(), as they are
251 // required by some animators.
John Reck9eb9f6f2014-08-21 11:23:05 -0700252 if (CC_LIKELY(info.runAnimations)) {
John Reck119907c2014-08-14 09:02:01 -0700253 mAnimatorManager.pushStaging();
John Reck9eb9f6f2014-08-21 11:23:05 -0700254 }
John Reckff941dc2014-05-14 16:34:14 -0700255 if (mDirtyPropertyFields) {
256 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700257 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700258 info.damageAccumulator->popTransform();
Chris Craikb565df12015-10-05 13:00:52 -0700259 syncProperties();
John Recke4267ea2014-06-03 15:53:15 -0700260 // We could try to be clever and only re-damage if the matrix changed.
261 // However, we don't need to worry about that. The cost of over-damaging
262 // here is only going to be a single additional map rect of this node
263 // plus a rect join(). The parent's transform (and up) will only be
264 // performed once.
John Recka447d292014-06-11 18:39:44 -0700265 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700266 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700267 }
John Reck25fbb3f2014-06-12 13:46:45 -0700268}
269
John Reck2de950d2017-01-25 10:58:30 -0800270void RenderNode::syncDisplayList(TreeObserver& observer, TreeInfo* info) {
Chris Craikb565df12015-10-05 13:00:52 -0700271 // Make sure we inc first so that we don't fluctuate between 0 and 1,
272 // which would thrash the layer cache
Chris Craik003cc3d2015-10-16 10:24:55 -0700273 if (mStagingDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -0700274 mStagingDisplayList->updateChildren([](RenderNode* child) { child->incParentRefCount(); });
Chris Craikb565df12015-10-05 13:00:52 -0700275 }
John Reck2de950d2017-01-25 10:58:30 -0800276 deleteDisplayList(observer, info);
Chris Craik003cc3d2015-10-16 10:24:55 -0700277 mDisplayList = mStagingDisplayList;
278 mStagingDisplayList = nullptr;
279 if (mDisplayList) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400280 mDisplayList->syncContents();
Chris Craikb565df12015-10-05 13:00:52 -0700281 }
282}
283
John Reck2de950d2017-01-25 10:58:30 -0800284void RenderNode::pushStagingDisplayListChanges(TreeObserver& observer, TreeInfo& info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700285 if (mNeedsDisplayListSync) {
286 mNeedsDisplayListSync = false;
John Reck5c9d7172014-10-22 11:32:27 -0700287 // Damage with the old display list first then the new one to catch any
288 // changes in isRenderable or, in the future, bounds
289 damageSelf(info);
John Reck2de950d2017-01-25 10:58:30 -0800290 syncDisplayList(observer, &info);
John Recke4267ea2014-06-03 15:53:15 -0700291 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700292 }
John Reck8de65a82014-04-09 15:23:38 -0700293}
294
John Reck2de950d2017-01-25 10:58:30 -0800295void RenderNode::deleteDisplayList(TreeObserver& observer, TreeInfo* info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700296 if (mDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -0700297 mDisplayList->updateChildren(
298 [&observer, info](RenderNode* child) { child->decParentRefCount(observer, info); });
Derek Sollenberger0df62092016-09-27 16:04:42 -0400299 if (!mDisplayList->reuseDisplayList(this, info ? &info->canvasContext : nullptr)) {
300 delete mDisplayList;
John Reckdcba6722014-07-08 13:59:49 -0700301 }
302 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700303 mDisplayList = nullptr;
John Reckdcba6722014-07-08 13:59:49 -0700304}
305
John Reck2de950d2017-01-25 10:58:30 -0800306void RenderNode::destroyHardwareResources(TreeInfo* info) {
Derek Sollenberger0df62092016-09-27 16:04:42 -0400307 if (hasLayer()) {
Derek Sollenberger6a21ca52016-09-28 13:39:55 -0400308 renderthread::CanvasContext::destroyLayer(this);
John Reckdcba6722014-07-08 13:59:49 -0700309 }
John Reck3afd6372017-01-30 10:15:48 -0800310 setStagingDisplayList(nullptr);
311
312 ImmediateRemoved observer(info);
313 deleteDisplayList(observer, info);
John Reck2de950d2017-01-25 10:58:30 -0800314}
315
316void RenderNode::destroyLayers() {
317 if (hasLayer()) {
318 renderthread::CanvasContext::destroyLayer(this);
319 }
320 if (mDisplayList) {
John Reck1bcacfd2017-11-03 10:12:19 -0700321 mDisplayList->updateChildren([](RenderNode* child) { child->destroyLayers(); });
John Reck2de950d2017-01-25 10:58:30 -0800322 }
323}
324
325void RenderNode::decParentRefCount(TreeObserver& observer, TreeInfo* info) {
326 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
327 mParentCount--;
328 if (!mParentCount) {
329 observer.onMaybeRemovedFromTree(this);
330 if (CC_UNLIKELY(mPositionListener.get())) {
331 mPositionListener->onPositionLost(*this, info);
John Reckdcba6722014-07-08 13:59:49 -0700332 }
333 }
334}
335
John Reck2de950d2017-01-25 10:58:30 -0800336void RenderNode::onRemovedFromTree(TreeInfo* info) {
337 destroyHardwareResources(info);
338}
339
340void RenderNode::clearRoot() {
341 ImmediateRemoved observer(nullptr);
342 decParentRefCount(observer);
John Reckdcba6722014-07-08 13:59:49 -0700343}
344
John Reck113e0822014-03-18 09:22:59 -0700345/**
346 * Apply property-based transformations to input matrix
347 *
348 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
349 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
350 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700351void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700352 if (properties().getLeft() != 0 || properties().getTop() != 0) {
353 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700354 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700355 if (properties().getStaticMatrix()) {
356 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700357 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700358 } else if (properties().getAnimationMatrix()) {
359 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700360 matrix.multiply(anim);
361 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700362
Chris Craikcc39e162014-04-25 18:34:11 -0700363 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700364 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700365 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700366 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
John Reck1bcacfd2017-11-03 10:12:19 -0700367 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700368 } else {
369 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700370 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700371 } else {
372 mat4 true3dMat;
John Reck1bcacfd2017-11-03 10:12:19 -0700373 true3dMat.loadTranslate(properties().getPivotX() + properties().getTranslationX(),
374 properties().getPivotY() + properties().getTranslationY(),
375 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700376 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
377 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
378 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
379 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
380 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700381
382 matrix.multiply(true3dMat);
383 }
384 }
385 }
386}
387
388/**
389 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
390 *
391 * This should be called before a call to defer() or drawDisplayList()
392 *
393 * Each DisplayList that serves as a 3d root builds its list of composited children,
394 * which are flagged to not draw in the standard draw loop.
395 */
396void RenderNode::computeOrdering() {
397 ATRACE_CALL();
398 mProjectedNodes.clear();
399
400 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
401 // transform properties are applied correctly to top level children
Chris Craik003cc3d2015-10-16 10:24:55 -0700402 if (mDisplayList == nullptr) return;
Chris Craikb36af872015-10-16 14:23:12 -0700403 for (unsigned int i = 0; i < mDisplayList->getChildren().size(); i++) {
Chris Craik5e00c7c2016-07-06 16:10:09 -0700404 RenderNodeOp* childOp = mDisplayList->getChildren()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700405 childOp->renderNode->computeOrderingImpl(childOp, &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700406 }
407}
408
409void RenderNode::computeOrderingImpl(
John Reck1bcacfd2017-11-03 10:12:19 -0700410 RenderNodeOp* opState, std::vector<RenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700411 const mat4* transformFromProjectionSurface) {
412 mProjectedNodes.clear();
Chris Craik003cc3d2015-10-16 10:24:55 -0700413 if (mDisplayList == nullptr || mDisplayList->isEmpty()) return;
John Reck113e0822014-03-18 09:22:59 -0700414
415 // TODO: should avoid this calculation in most cases
416 // TODO: just calculate single matrix, down to all leaf composited elements
417 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
Chris Craik8d1f2122015-11-24 16:40:09 -0800418 localTransformFromProjectionSurface.multiply(opState->localMatrix);
John Reck113e0822014-03-18 09:22:59 -0700419
John Reckd0a0b2a2014-03-20 16:28:56 -0700420 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700421 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
Chris Craik8d1f2122015-11-24 16:40:09 -0800422 opState->skipInOrderDraw = true;
423 opState->transformFromCompositingAncestor = localTransformFromProjectionSurface;
John Reck272a6852015-07-29 16:48:58 -0700424 compositedChildrenOfProjectionSurface->push_back(opState);
John Reck113e0822014-03-18 09:22:59 -0700425 } else {
426 // standard in order draw
Chris Craik8d1f2122015-11-24 16:40:09 -0800427 opState->skipInOrderDraw = false;
John Reck113e0822014-03-18 09:22:59 -0700428 }
429
Chris Craikb36af872015-10-16 14:23:12 -0700430 if (mDisplayList->getChildren().size() > 0) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700431 const bool isProjectionReceiver = mDisplayList->projectionReceiveIndex >= 0;
John Reck113e0822014-03-18 09:22:59 -0700432 bool haveAppliedPropertiesToProjection = false;
Chris Craikb36af872015-10-16 14:23:12 -0700433 for (unsigned int i = 0; i < mDisplayList->getChildren().size(); i++) {
Chris Craik5e00c7c2016-07-06 16:10:09 -0700434 RenderNodeOp* childOp = mDisplayList->getChildren()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700435 RenderNode* child = childOp->renderNode;
John Reck113e0822014-03-18 09:22:59 -0700436
Chris Craik5e00c7c2016-07-06 16:10:09 -0700437 std::vector<RenderNodeOp*>* projectionChildren = nullptr;
Chris Craikd41c4d82015-01-05 15:51:13 -0800438 const mat4* projectionTransform = nullptr;
John Reckd0a0b2a2014-03-20 16:28:56 -0700439 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
Chris Craikbf72eb82015-06-08 11:30:44 -0700440 // if receiving projections, collect projecting descendant
John Reck113e0822014-03-18 09:22:59 -0700441
Chris Craikbf72eb82015-06-08 11:30:44 -0700442 // Note that if a direct descendant is projecting backwards, we pass its
443 // grandparent projection collection, since it shouldn't project onto its
John Reck113e0822014-03-18 09:22:59 -0700444 // parent, where it will already be drawing.
445 projectionChildren = &mProjectedNodes;
446 projectionTransform = &mat4::identity();
447 } else {
448 if (!haveAppliedPropertiesToProjection) {
449 applyViewPropertyTransforms(localTransformFromProjectionSurface);
450 haveAppliedPropertiesToProjection = true;
451 }
452 projectionChildren = compositedChildrenOfProjectionSurface;
453 projectionTransform = &localTransformFromProjectionSurface;
454 }
Derek Sollenbergerf2932592015-08-13 14:59:33 -0400455 child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700456 }
457 }
John Reck113e0822014-03-18 09:22:59 -0700458}
459
Derek Sollenberger579317d42017-08-29 16:33:49 -0400460const SkPath* RenderNode::getClippedOutline(const SkRect& clipRect) const {
461 const SkPath* outlinePath = properties().getOutline().getPath();
462 const uint32_t outlineID = outlinePath->getGenerationID();
463
464 if (outlineID != mClippedOutlineCache.outlineID || clipRect != mClippedOutlineCache.clipRect) {
465 // update the cache keys
466 mClippedOutlineCache.outlineID = outlineID;
467 mClippedOutlineCache.clipRect = clipRect;
468
469 // update the cache value by recomputing a new path
470 SkPath clipPath;
471 clipPath.addRect(clipRect);
472 Op(*outlinePath, clipPath, kIntersect_SkPathOp, &mClippedOutlineCache.clippedOutline);
Derek Sollenberger579317d42017-08-29 16:33:49 -0400473 }
474 return &mClippedOutlineCache.clippedOutline;
475}
476
John Reck113e0822014-03-18 09:22:59 -0700477} /* namespace uirenderer */
478} /* namespace android */