blob: bdcad798f05edec9a3292c96f4069407ebd95f76 [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
John Recke4267ea2014-06-03 15:53:15 -070019#include "DamageAccumulator.h"
John Reck113e0822014-03-18 09:22:59 -070020#include "Debug.h"
Chris Craikb565df12015-10-05 13:00:52 -070021#if HWUI_NEW_OPS
Chris Craik0b7e8242015-10-28 16:50:44 -070022#include "BakedOpRenderer.h"
Chris Craik91eff222016-02-22 13:39:33 -080023#include "RecordedOp.h"
24#include "OpDumper.h"
Chris Craikb565df12015-10-05 13:00:52 -070025#endif
John Reck113e0822014-03-18 09:22:59 -070026#include "DisplayListOp.h"
John Reck25fbb3f2014-06-12 13:46:45 -070027#include "LayerRenderer.h"
28#include "OpenGLRenderer.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040029#include "TreeInfo.h"
Chris Craike0bb87d2014-04-22 17:55:41 -070030#include "utils/MathUtils.h"
Chris Craik70850ea2014-11-18 10:49:23 -080031#include "utils/TraceUtils.h"
John Reck998a6d82014-08-28 15:35:53 -070032#include "renderthread/CanvasContext.h"
John Reck113e0822014-03-18 09:22:59 -070033
John Recke248bd12015-08-05 13:53:53 -070034#include "protos/hwui.pb.h"
35#include "protos/ProtoHelpers.h"
36
John Reck77c40102015-10-26 15:49:47 -070037#include <algorithm>
38#include <sstream>
39#include <string>
40
John Reck113e0822014-03-18 09:22:59 -070041namespace android {
42namespace uirenderer {
43
John Reck443a7142014-09-04 17:40:05 -070044void RenderNode::debugDumpLayers(const char* prefix) {
Chris Craik0b7e8242015-10-28 16:50:44 -070045#if HWUI_NEW_OPS
46 LOG_ALWAYS_FATAL("TODO: dump layer");
47#else
John Reck443a7142014-09-04 17:40:05 -070048 if (mLayer) {
49 ALOGD("%sNode %p (%s) has layer %p (fbo = %u, wasBuildLayered = %s)",
50 prefix, this, getName(), mLayer, mLayer->getFbo(),
51 mLayer->wasBuildLayered ? "true" : "false");
52 }
Chris Craik0b7e8242015-10-28 16:50:44 -070053#endif
Chris Craik003cc3d2015-10-16 10:24:55 -070054 if (mDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -070055 for (auto&& child : mDisplayList->getChildren()) {
Chris Craik10ed6922015-10-15 10:55:15 -070056 child->renderNode->debugDumpLayers(prefix);
John Reck443a7142014-09-04 17:40:05 -070057 }
58 }
59}
60
John Reck8de65a82014-04-09 15:23:38 -070061RenderNode::RenderNode()
John Reckff941dc2014-05-14 16:34:14 -070062 : mDirtyPropertyFields(0)
Chris Craik003cc3d2015-10-16 10:24:55 -070063 , mNeedsDisplayListSync(false)
64 , mDisplayList(nullptr)
65 , mStagingDisplayList(nullptr)
John Reck68bfe0a2014-06-24 15:34:58 -070066 , mAnimatorManager(*this)
John Reckdcba6722014-07-08 13:59:49 -070067 , mParentCount(0) {
John Reck113e0822014-03-18 09:22:59 -070068}
69
70RenderNode::~RenderNode() {
John Reck44b49f02016-03-25 14:29:48 -070071 deleteDisplayList(nullptr);
Chris Craik003cc3d2015-10-16 10:24:55 -070072 delete mStagingDisplayList;
Chris Craik0b7e8242015-10-28 16:50:44 -070073#if HWUI_NEW_OPS
74 LOG_ALWAYS_FATAL_IF(mLayer, "layer missed detachment!");
75#else
John Reck0e89e2b2014-10-31 14:49:06 -070076 if (mLayer) {
77 ALOGW("Memory Warning: Layer %p missed its detachment, held on to for far too long!", mLayer);
78 mLayer->postDecStrong();
Chris Craikd41c4d82015-01-05 15:51:13 -080079 mLayer = nullptr;
John Reck0e89e2b2014-10-31 14:49:06 -070080 }
Chris Craik0b7e8242015-10-28 16:50:44 -070081#endif
John Reck113e0822014-03-18 09:22:59 -070082}
83
John Reck51f2d602016-04-06 07:50:47 -070084void RenderNode::setStagingDisplayList(DisplayList* displayList, TreeObserver* observer) {
Chris Craik003cc3d2015-10-16 10:24:55 -070085 mNeedsDisplayListSync = true;
86 delete mStagingDisplayList;
87 mStagingDisplayList = displayList;
John Reck9dea0d52015-10-27 10:04:38 -070088 // If mParentCount == 0 we are the sole reference to this RenderNode,
89 // so immediately free the old display list
90 if (!mParentCount && !mStagingDisplayList) {
John Reck51f2d602016-04-06 07:50:47 -070091 deleteDisplayList(observer);
John Reck9dea0d52015-10-27 10:04:38 -070092 }
John Reck113e0822014-03-18 09:22:59 -070093}
94
95/**
96 * This function is a simplified version of replay(), where we simply retrieve and log the
97 * display list. This function should remain in sync with the replay() function.
98 */
Chris Craik91eff222016-02-22 13:39:33 -080099#if HWUI_NEW_OPS
100void RenderNode::output(uint32_t level, const char* label) {
101 ALOGD("%s (%s %p%s%s%s%s%s)",
102 label,
103 getName(),
104 this,
105 (MathUtils::isZero(properties().getAlpha()) ? ", zero alpha" : ""),
106 (properties().hasShadow() ? ", casting shadow" : ""),
107 (isRenderable() ? "" : ", empty"),
108 (properties().getProjectBackwards() ? ", projected" : ""),
109 (mLayer != nullptr ? ", on HW Layer" : ""));
110 properties().debugOutputProperties(level + 1);
111
112 if (mDisplayList) {
113 for (auto&& op : mDisplayList->getOps()) {
114 std::stringstream strout;
115 OpDumper::dump(*op, strout, level + 1);
116 if (op->opId == RecordedOpId::RenderNodeOp) {
117 auto rnOp = reinterpret_cast<const RenderNodeOp*>(op);
118 rnOp->renderNode->output(level + 1, strout.str().c_str());
119 } else {
120 ALOGD("%s", strout.str().c_str());
121 }
122 }
123 }
124 ALOGD("%*s/RenderNode(%s %p)", level * 2, "", getName(), this);
125}
126#else
John Reck113e0822014-03-18 09:22:59 -0700127void RenderNode::output(uint32_t level) {
Chris Craikbf72eb82015-06-08 11:30:44 -0700128 ALOGD("%*sStart display list (%p, %s%s%s%s%s%s)", (level - 1) * 2, "", this,
Chris Craikb5a54352014-11-21 14:54:35 -0800129 getName(),
Chris Craik43a1d312015-05-27 11:28:14 -0700130 (MathUtils::isZero(properties().getAlpha()) ? ", zero alpha" : ""),
Chris Craikb5a54352014-11-21 14:54:35 -0800131 (properties().hasShadow() ? ", casting shadow" : ""),
132 (isRenderable() ? "" : ", empty"),
Chris Craikbf72eb82015-06-08 11:30:44 -0700133 (properties().getProjectBackwards() ? ", projected" : ""),
Chris Craikd41c4d82015-01-05 15:51:13 -0800134 (mLayer != nullptr ? ", on HW Layer" : ""));
Florin Malitaeecff562015-12-21 10:43:01 -0500135 ALOGD("%*s%s %d", level * 2, "", "Save", SaveFlags::MatrixClip);
John Reckd0a0b2a2014-03-20 16:28:56 -0700136 properties().debugOutputProperties(level);
Chris Craik003cc3d2015-10-16 10:24:55 -0700137 if (mDisplayList) {
Chris Craik8afd0f22014-08-21 17:41:57 -0700138 // TODO: consider printing the chunk boundaries here
Chris Craik003cc3d2015-10-16 10:24:55 -0700139 for (auto&& op : mDisplayList->getOps()) {
Chris Craik10ed6922015-10-15 10:55:15 -0700140 op->output(level, DisplayListOp::kOpLogFlag_Recurse);
John Reckdc0349b2014-08-06 15:28:07 -0700141 }
John Reck113e0822014-03-18 09:22:59 -0700142 }
Chris Craik3f0854292014-04-15 16:18:08 -0700143 ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, getName());
Chris Craik91eff222016-02-22 13:39:33 -0800144 }
145#endif
John Reck113e0822014-03-18 09:22:59 -0700146
John Recke248bd12015-08-05 13:53:53 -0700147void RenderNode::copyTo(proto::RenderNode *pnode) {
148 pnode->set_id(static_cast<uint64_t>(
149 reinterpret_cast<uintptr_t>(this)));
150 pnode->set_name(mName.string(), mName.length());
151
152 proto::RenderProperties* pprops = pnode->mutable_properties();
153 pprops->set_left(properties().getLeft());
154 pprops->set_top(properties().getTop());
155 pprops->set_right(properties().getRight());
156 pprops->set_bottom(properties().getBottom());
157 pprops->set_clip_flags(properties().getClippingFlags());
158 pprops->set_alpha(properties().getAlpha());
159 pprops->set_translation_x(properties().getTranslationX());
160 pprops->set_translation_y(properties().getTranslationY());
161 pprops->set_translation_z(properties().getTranslationZ());
162 pprops->set_elevation(properties().getElevation());
163 pprops->set_rotation(properties().getRotation());
164 pprops->set_rotation_x(properties().getRotationX());
165 pprops->set_rotation_y(properties().getRotationY());
166 pprops->set_scale_x(properties().getScaleX());
167 pprops->set_scale_y(properties().getScaleY());
168 pprops->set_pivot_x(properties().getPivotX());
169 pprops->set_pivot_y(properties().getPivotY());
170 pprops->set_has_overlapping_rendering(properties().getHasOverlappingRendering());
171 pprops->set_pivot_explicitly_set(properties().isPivotExplicitlySet());
172 pprops->set_project_backwards(properties().getProjectBackwards());
173 pprops->set_projection_receiver(properties().isProjectionReceiver());
174 set(pprops->mutable_clip_bounds(), properties().getClipBounds());
175
176 const Outline& outline = properties().getOutline();
177 if (outline.getType() != Outline::Type::None) {
178 proto::Outline* poutline = pprops->mutable_outline();
179 poutline->clear_path();
180 if (outline.getType() == Outline::Type::Empty) {
181 poutline->set_type(proto::Outline_Type_Empty);
182 } else if (outline.getType() == Outline::Type::ConvexPath) {
183 poutline->set_type(proto::Outline_Type_ConvexPath);
184 if (const SkPath* path = outline.getPath()) {
185 set(poutline->mutable_path(), *path);
186 }
187 } else if (outline.getType() == Outline::Type::RoundRect) {
188 poutline->set_type(proto::Outline_Type_RoundRect);
189 } else {
190 ALOGW("Uknown outline type! %d", static_cast<int>(outline.getType()));
191 poutline->set_type(proto::Outline_Type_None);
192 }
193 poutline->set_should_clip(outline.getShouldClip());
194 poutline->set_alpha(outline.getAlpha());
195 poutline->set_radius(outline.getRadius());
196 set(poutline->mutable_bounds(), outline.getBounds());
197 } else {
198 pprops->clear_outline();
199 }
200
201 const RevealClip& revealClip = properties().getRevealClip();
202 if (revealClip.willClip()) {
203 proto::RevealClip* prevealClip = pprops->mutable_reveal_clip();
204 prevealClip->set_x(revealClip.getX());
205 prevealClip->set_y(revealClip.getY());
206 prevealClip->set_radius(revealClip.getRadius());
207 } else {
208 pprops->clear_reveal_clip();
209 }
210
211 pnode->clear_children();
Chris Craik003cc3d2015-10-16 10:24:55 -0700212 if (mDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700213 for (auto&& child : mDisplayList->getChildren()) {
Chris Craikb565df12015-10-05 13:00:52 -0700214 child->renderNode->copyTo(pnode->add_children());
John Recke248bd12015-08-05 13:53:53 -0700215 }
216 }
217}
218
John Reckfe5e7b72014-05-23 17:42:28 -0700219int RenderNode::getDebugSize() {
220 int size = sizeof(RenderNode);
Chris Craik003cc3d2015-10-16 10:24:55 -0700221 if (mStagingDisplayList) {
222 size += mStagingDisplayList->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700223 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700224 if (mDisplayList && mDisplayList != mStagingDisplayList) {
225 size += mDisplayList->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700226 }
227 return size;
228}
229
John Reckf4198b72014-04-09 17:00:04 -0700230void RenderNode::prepareTree(TreeInfo& info) {
231 ATRACE_CALL();
Chris Craik69e5adf2014-08-14 13:34:01 -0700232 LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
John Reckf4198b72014-04-09 17:00:04 -0700233
Chris Craika766cb22015-06-08 16:49:43 -0700234 // Functors don't correctly handle stencil usage of overdraw debugging - shove 'em in a layer.
235 bool functorsNeedLayer = Properties::debugOverdraw;
236
237 prepareTreeImpl(info, functorsNeedLayer);
John Reckf4198b72014-04-09 17:00:04 -0700238}
239
John Reck68bfe0a2014-06-24 15:34:58 -0700240void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
241 mAnimatorManager.addAnimator(animator);
242}
243
Doris Liu8b083202016-02-19 21:46:06 +0000244void RenderNode::removeAnimator(const sp<BaseRenderNodeAnimator>& animator) {
245 mAnimatorManager.removeAnimator(animator);
246}
247
John Recke4267ea2014-06-03 15:53:15 -0700248void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700249 if (isRenderable()) {
John Reck293e8682014-06-17 10:34:02 -0700250 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700251 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
252 } else {
253 // Hope this is big enough?
254 // TODO: Get this from the display list ops or something
John Reckc1288232015-08-12 13:39:11 -0700255 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
John Recka447d292014-06-11 18:39:44 -0700256 }
John Recke4267ea2014-06-03 15:53:15 -0700257 }
258}
259
John Recka7c2ea22014-08-08 13:21:00 -0700260void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700261 LayerType layerType = properties().effectiveLayerType();
Chris Craik182952f2015-03-09 14:17:29 -0700262 if (CC_UNLIKELY(layerType == LayerType::RenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700263 // Damage applied so far needs to affect our parent, but does not require
264 // the layer to be updated. So we pop/push here to clear out the current
265 // damage and get a clean state for display list or children updates to
266 // affect, which will require the layer to be updated
267 info.damageAccumulator->popTransform();
268 info.damageAccumulator->pushTransform(this);
269 if (dirtyMask & DISPLAY_LIST) {
270 damageSelf(info);
271 }
John Reck25fbb3f2014-06-12 13:46:45 -0700272 }
273}
274
Chris Craik9fded232015-11-11 16:42:34 -0800275static layer_t* createLayer(RenderState& renderState, uint32_t width, uint32_t height) {
Chris Craik0b7e8242015-10-28 16:50:44 -0700276#if HWUI_NEW_OPS
Chris Craik9fded232015-11-11 16:42:34 -0800277 return renderState.layerPool().get(renderState, width, height);
Chris Craik0b7e8242015-10-28 16:50:44 -0700278#else
279 return LayerRenderer::createRenderLayer(renderState, width, height);
280#endif
281}
282
Chris Craik9fded232015-11-11 16:42:34 -0800283static void destroyLayer(layer_t* layer) {
Chris Craik0b7e8242015-10-28 16:50:44 -0700284#if HWUI_NEW_OPS
Chris Craik9fded232015-11-11 16:42:34 -0800285 RenderState& renderState = layer->renderState;
286 renderState.layerPool().putOrDelete(layer);
Chris Craik0b7e8242015-10-28 16:50:44 -0700287#else
288 LayerRenderer::destroyLayer(layer);
289#endif
290}
291
Chris Craik9fded232015-11-11 16:42:34 -0800292static bool layerMatchesWidthAndHeight(layer_t* layer, int width, int height) {
293#if HWUI_NEW_OPS
294 return layer->viewportWidth == (uint32_t) width && layer->viewportHeight == (uint32_t)height;
295#else
296 return layer->layer.getWidth() == width && layer->layer.getHeight() == height;
297#endif
298}
299
John Reck25fbb3f2014-06-12 13:46:45 -0700300void RenderNode::pushLayerUpdate(TreeInfo& info) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700301 LayerType layerType = properties().effectiveLayerType();
John Reck25fbb3f2014-06-12 13:46:45 -0700302 // If we are not a layer OR we cannot be rendered (eg, view was detached)
303 // we need to destroy any Layers we may have had previously
Chris Craike3e481d2016-07-11 12:20:51 -0700304 if (CC_LIKELY(layerType != LayerType::RenderLayer)
305 || CC_UNLIKELY(!isRenderable())
306 || CC_UNLIKELY(properties().getWidth() == 0)
307 || CC_UNLIKELY(properties().getHeight() == 0)) {
John Reck25fbb3f2014-06-12 13:46:45 -0700308 if (CC_UNLIKELY(mLayer)) {
Chris Craik0b7e8242015-10-28 16:50:44 -0700309 destroyLayer(mLayer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800310 mLayer = nullptr;
John Reck25fbb3f2014-06-12 13:46:45 -0700311 }
312 return;
313 }
314
Chris Craik69e5adf2014-08-14 13:34:01 -0700315 bool transformUpdateNeeded = false;
John Reck25fbb3f2014-06-12 13:46:45 -0700316 if (!mLayer) {
Chris Craik9fded232015-11-11 16:42:34 -0800317 mLayer = createLayer(info.canvasContext.getRenderState(), getWidth(), getHeight());
Chris Craikf559bd12015-11-18 14:42:00 -0800318#if !HWUI_NEW_OPS
319 applyLayerPropertiesToLayer(info);
320#endif
Chris Craik9fded232015-11-11 16:42:34 -0800321 damageSelf(info);
322 transformUpdateNeeded = true;
323 } else if (!layerMatchesWidthAndHeight(mLayer, getWidth(), getHeight())) {
Chris Craik0b7e8242015-10-28 16:50:44 -0700324#if HWUI_NEW_OPS
Chris Craikd4fe4d32016-06-09 16:57:11 -0700325 // TODO: remove now irrelevant, currently enqueued damage (respecting damage ordering)
326 // Or, ideally, maintain damage between frames on node/layer so ordering is always correct
Chris Craik9fded232015-11-11 16:42:34 -0800327 RenderState& renderState = mLayer->renderState;
328 if (properties().fitsOnLayer()) {
329 mLayer = renderState.layerPool().resize(mLayer, getWidth(), getHeight());
330 } else {
Chris Craik0b7e8242015-10-28 16:50:44 -0700331#else
John Reckc25e5062014-06-18 14:21:29 -0700332 if (!LayerRenderer::resizeLayer(mLayer, getWidth(), getHeight())) {
Chris Craik0b7e8242015-10-28 16:50:44 -0700333#endif
334 destroyLayer(mLayer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800335 mLayer = nullptr;
John Reckc25e5062014-06-18 14:21:29 -0700336 }
John Reck25fbb3f2014-06-12 13:46:45 -0700337 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700338 transformUpdateNeeded = true;
339 }
340
John Reck25fbb3f2014-06-12 13:46:45 -0700341 SkRect dirty;
342 info.damageAccumulator->peekAtDirty(&dirty);
John Reck25fbb3f2014-06-12 13:46:45 -0700343
John Reckc25e5062014-06-18 14:21:29 -0700344 if (!mLayer) {
John Reck0e89e2b2014-10-31 14:49:06 -0700345 Caches::getInstance().dumpMemoryUsage();
John Reckc25e5062014-06-18 14:21:29 -0700346 if (info.errorHandler) {
John Reck77c40102015-10-26 15:49:47 -0700347 std::ostringstream err;
348 err << "Unable to create layer for " << getName();
Chris Craik76caecf2015-11-02 19:17:45 -0800349 const int maxTextureSize = Caches::getInstance().maxTextureSize;
John Reck77c40102015-10-26 15:49:47 -0700350 if (getWidth() > maxTextureSize || getHeight() > maxTextureSize) {
351 err << ", size " << getWidth() << "x" << getHeight()
352 << " exceeds max size " << maxTextureSize;
353 } else {
354 err << ", see logcat for more info";
355 }
356 info.errorHandler->onError(err.str());
John Reckc25e5062014-06-18 14:21:29 -0700357 }
358 return;
359 }
360
Chris Craik98787e62015-11-13 10:55:30 -0800361 if (transformUpdateNeeded && mLayer) {
Chris Craikc71bfca2014-08-21 10:18:58 -0700362 // update the transform in window of the layer to reset its origin wrt light source position
363 Matrix4 windowTransform;
364 info.damageAccumulator->computeCurrentTransform(&windowTransform);
365 mLayer->setWindowTransform(windowTransform);
366 }
John Reckc79eabc2014-08-05 11:03:42 -0700367
Chris Craik0b7e8242015-10-28 16:50:44 -0700368#if HWUI_NEW_OPS
369 info.layerUpdateQueue->enqueueLayerWithDamage(this, dirty);
370#else
John Reckc79eabc2014-08-05 11:03:42 -0700371 if (dirty.intersect(0, 0, getWidth(), getHeight())) {
Mike Reed71487eb2014-11-19 16:13:20 -0500372 dirty.roundOut(&dirty);
John Reck25fbb3f2014-06-12 13:46:45 -0700373 mLayer->updateDeferred(this, dirty.fLeft, dirty.fTop, dirty.fRight, dirty.fBottom);
374 }
375 // This is not inside the above if because we may have called
376 // updateDeferred on a previous prepare pass that didn't have a renderer
377 if (info.renderer && mLayer->deferredUpdateScheduled) {
378 info.renderer->pushLayerUpdate(mLayer);
379 }
Chris Craik0b7e8242015-10-28 16:50:44 -0700380#endif
John Reck998a6d82014-08-28 15:35:53 -0700381
Chris Craike2e53a72015-10-28 15:55:40 -0700382 // There might be prefetched layers that need to be accounted for.
383 // That might be us, so tell CanvasContext that this layer is in the
384 // tree and should not be destroyed.
385 info.canvasContext.markLayerInUse(this);
John Reck25fbb3f2014-06-12 13:46:45 -0700386}
387
Chris Craika766cb22015-06-08 16:49:43 -0700388/**
389 * Traverse down the the draw tree to prepare for a frame.
390 *
391 * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven
392 *
393 * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the
394 * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer.
395 */
396void RenderNode::prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer) {
John Recka447d292014-06-11 18:39:44 -0700397 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700398
John Reckdcba6722014-07-08 13:59:49 -0700399 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700400 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700401 }
John Reck9eb9f6f2014-08-21 11:23:05 -0700402 uint32_t animatorDirtyMask = 0;
403 if (CC_LIKELY(info.runAnimations)) {
404 animatorDirtyMask = mAnimatorManager.animate(info);
405 }
Chris Craika766cb22015-06-08 16:49:43 -0700406
John Reck3f725f02015-06-16 10:29:31 -0700407 bool willHaveFunctor = false;
Chris Craik003cc3d2015-10-16 10:24:55 -0700408 if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700409 willHaveFunctor = !mStagingDisplayList->getFunctors().empty();
Chris Craik003cc3d2015-10-16 10:24:55 -0700410 } else if (mDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700411 willHaveFunctor = !mDisplayList->getFunctors().empty();
John Reck3f725f02015-06-16 10:29:31 -0700412 }
Chris Craika766cb22015-06-08 16:49:43 -0700413 bool childFunctorsNeedLayer = mProperties.prepareForFunctorPresence(
414 willHaveFunctor, functorsNeedLayer);
415
John Reckf6481082016-02-02 15:18:23 -0800416 if (CC_UNLIKELY(mPositionListener.get())) {
417 mPositionListener->onPositionUpdated(*this, info);
418 }
419
John Recka7c2ea22014-08-08 13:21:00 -0700420 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700421 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700422 pushStagingDisplayListChanges(info);
423 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700424 prepareSubTree(info, childFunctorsNeedLayer, mDisplayList);
John Reck25fbb3f2014-06-12 13:46:45 -0700425
Doris Liu07c056d2016-06-13 12:52:44 -0700426 if (mDisplayList) {
427 for (auto& vectorDrawable : mDisplayList->getVectorDrawables()) {
428 // If any vector drawable in the display list needs update, damage the node.
429 if (vectorDrawable->isDirty()) {
430 damageSelf(info);
431 }
432 vectorDrawable->setPropertyChangeWillBeConsumed(true);
Doris Liu718cd3e2016-05-17 16:50:31 -0700433 }
Doris Liu718cd3e2016-05-17 16:50:31 -0700434 }
Doris Liub51b2862016-08-01 19:56:47 -0700435 pushLayerUpdate(info);
Doris Liu718cd3e2016-05-17 16:50:31 -0700436
John Recka447d292014-06-11 18:39:44 -0700437 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700438}
439
Chris Craikb565df12015-10-05 13:00:52 -0700440void RenderNode::syncProperties() {
441 mProperties = mStagingProperties;
442}
443
John Reck25fbb3f2014-06-12 13:46:45 -0700444void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reckff941dc2014-05-14 16:34:14 -0700445 // Push the animators first so that setupStartValueIfNecessary() is called
446 // before properties() is trampled by stagingProperties(), as they are
447 // required by some animators.
John Reck9eb9f6f2014-08-21 11:23:05 -0700448 if (CC_LIKELY(info.runAnimations)) {
John Reck119907c2014-08-14 09:02:01 -0700449 mAnimatorManager.pushStaging();
John Reck9eb9f6f2014-08-21 11:23:05 -0700450 }
John Reckff941dc2014-05-14 16:34:14 -0700451 if (mDirtyPropertyFields) {
452 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700453 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700454 info.damageAccumulator->popTransform();
Chris Craikb565df12015-10-05 13:00:52 -0700455 syncProperties();
Chris Craik0b7e8242015-10-28 16:50:44 -0700456#if !HWUI_NEW_OPS
John Reck25fbb3f2014-06-12 13:46:45 -0700457 applyLayerPropertiesToLayer(info);
Chris Craik0b7e8242015-10-28 16:50:44 -0700458#endif
John Recke4267ea2014-06-03 15:53:15 -0700459 // We could try to be clever and only re-damage if the matrix changed.
460 // However, we don't need to worry about that. The cost of over-damaging
461 // here is only going to be a single additional map rect of this node
462 // plus a rect join(). The parent's transform (and up) will only be
463 // performed once.
John Recka447d292014-06-11 18:39:44 -0700464 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700465 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700466 }
John Reck25fbb3f2014-06-12 13:46:45 -0700467}
468
Chris Craik0b7e8242015-10-28 16:50:44 -0700469#if !HWUI_NEW_OPS
Andreas Gampe64bb4132014-11-22 00:35:09 +0000470void RenderNode::applyLayerPropertiesToLayer(TreeInfo& info) {
John Reck25fbb3f2014-06-12 13:46:45 -0700471 if (CC_LIKELY(!mLayer)) return;
472
473 const LayerProperties& props = properties().layerProperties();
474 mLayer->setAlpha(props.alpha(), props.xferMode());
475 mLayer->setColorFilter(props.colorFilter());
476 mLayer->setBlend(props.needsBlending());
477}
Chris Craik0b7e8242015-10-28 16:50:44 -0700478#endif
John Reck25fbb3f2014-06-12 13:46:45 -0700479
John Reckaa6e84f2016-06-16 15:36:13 -0700480void RenderNode::syncDisplayList(TreeInfo* info) {
Chris Craikb565df12015-10-05 13:00:52 -0700481 // Make sure we inc first so that we don't fluctuate between 0 and 1,
482 // which would thrash the layer cache
Chris Craik003cc3d2015-10-16 10:24:55 -0700483 if (mStagingDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700484 for (auto&& child : mStagingDisplayList->getChildren()) {
Chris Craikb565df12015-10-05 13:00:52 -0700485 child->renderNode->incParentRefCount();
486 }
487 }
John Reckaa6e84f2016-06-16 15:36:13 -0700488 deleteDisplayList(info ? info->observer : nullptr, info);
Chris Craik003cc3d2015-10-16 10:24:55 -0700489 mDisplayList = mStagingDisplayList;
490 mStagingDisplayList = nullptr;
491 if (mDisplayList) {
John Reckcd1c3eb2016-04-14 10:38:54 -0700492 for (auto& iter : mDisplayList->getFunctors()) {
493 (*iter.functor)(DrawGlInfo::kModeSync, nullptr);
Chris Craikb565df12015-10-05 13:00:52 -0700494 }
Doris Liu718cd3e2016-05-17 16:50:31 -0700495 for (auto& vectorDrawable : mDisplayList->getVectorDrawables()) {
496 vectorDrawable->syncProperties();
Doris Liu1d8e1942016-03-02 15:16:28 -0800497 }
Chris Craikb565df12015-10-05 13:00:52 -0700498 }
499}
500
John Reck25fbb3f2014-06-12 13:46:45 -0700501void RenderNode::pushStagingDisplayListChanges(TreeInfo& info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700502 if (mNeedsDisplayListSync) {
503 mNeedsDisplayListSync = false;
John Reck5c9d7172014-10-22 11:32:27 -0700504 // Damage with the old display list first then the new one to catch any
505 // changes in isRenderable or, in the future, bounds
506 damageSelf(info);
John Reckaa6e84f2016-06-16 15:36:13 -0700507 syncDisplayList(&info);
John Recke4267ea2014-06-03 15:53:15 -0700508 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700509 }
John Reck8de65a82014-04-09 15:23:38 -0700510}
511
John Reckaa6e84f2016-06-16 15:36:13 -0700512void RenderNode::deleteDisplayList(TreeObserver* observer, TreeInfo* info) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700513 if (mDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700514 for (auto&& child : mDisplayList->getChildren()) {
John Reckaa6e84f2016-06-16 15:36:13 -0700515 child->renderNode->decParentRefCount(observer, info);
John Reckdcba6722014-07-08 13:59:49 -0700516 }
517 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700518 delete mDisplayList;
519 mDisplayList = nullptr;
John Reckdcba6722014-07-08 13:59:49 -0700520}
521
Chris Craik003cc3d2015-10-16 10:24:55 -0700522void RenderNode::prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayList* subtree) {
John Reck8de65a82014-04-09 15:23:38 -0700523 if (subtree) {
John Reck860d1552014-04-11 19:15:05 -0700524 TextureCache& cache = Caches::getInstance().textureCache;
Chris Craikb36af872015-10-16 14:23:12 -0700525 info.out.hasFunctors |= subtree->getFunctors().size();
526 for (auto&& bitmapResource : subtree->getBitmapResources()) {
Chris Craike2e53a72015-10-28 15:55:40 -0700527 void* ownerToken = &info.canvasContext;
528 info.prepareTextures = cache.prefetchAndMarkInUse(ownerToken, bitmapResource);
John Reckf4198b72014-04-09 17:00:04 -0700529 }
Chris Craikb36af872015-10-16 14:23:12 -0700530 for (auto&& op : subtree->getChildren()) {
Chris Craikb565df12015-10-05 13:00:52 -0700531 RenderNode* childNode = op->renderNode;
532#if HWUI_NEW_OPS
533 info.damageAccumulator->pushTransform(&op->localMatrix);
534 bool childFunctorsNeedLayer = functorsNeedLayer; // TODO! || op->mRecordedWithPotentialStencilClip;
535#else
Chris Craik8d1f2122015-11-24 16:40:09 -0800536 info.damageAccumulator->pushTransform(&op->localMatrix);
Chris Craika766cb22015-06-08 16:49:43 -0700537 bool childFunctorsNeedLayer = functorsNeedLayer
538 // Recorded with non-rect clip, or canvas-rotated by parent
539 || op->mRecordedWithPotentialStencilClip;
Chris Craikb565df12015-10-05 13:00:52 -0700540#endif
Chris Craika766cb22015-06-08 16:49:43 -0700541 childNode->prepareTreeImpl(info, childFunctorsNeedLayer);
John Recka447d292014-06-11 18:39:44 -0700542 info.damageAccumulator->popTransform();
John Reck5bf11bb2014-03-25 10:22:09 -0700543 }
John Reck113e0822014-03-18 09:22:59 -0700544 }
545}
546
John Reckaa6e84f2016-06-16 15:36:13 -0700547void RenderNode::destroyHardwareResources(TreeObserver* observer, TreeInfo* info) {
John Reckdcba6722014-07-08 13:59:49 -0700548 if (mLayer) {
Chris Craik0b7e8242015-10-28 16:50:44 -0700549 destroyLayer(mLayer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800550 mLayer = nullptr;
John Reckdcba6722014-07-08 13:59:49 -0700551 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700552 if (mDisplayList) {
Chris Craikb36af872015-10-16 14:23:12 -0700553 for (auto&& child : mDisplayList->getChildren()) {
John Reckaa6e84f2016-06-16 15:36:13 -0700554 child->renderNode->destroyHardwareResources(observer, info);
John Reckdcba6722014-07-08 13:59:49 -0700555 }
Chris Craik003cc3d2015-10-16 10:24:55 -0700556 if (mNeedsDisplayListSync) {
John Reckdcba6722014-07-08 13:59:49 -0700557 // Next prepare tree we are going to push a new display list, so we can
558 // drop our current one now
John Reckaa6e84f2016-06-16 15:36:13 -0700559 deleteDisplayList(observer, info);
John Reckdcba6722014-07-08 13:59:49 -0700560 }
561 }
562}
563
John Reckaa6e84f2016-06-16 15:36:13 -0700564void RenderNode::decParentRefCount(TreeObserver* observer, TreeInfo* info) {
John Reckdcba6722014-07-08 13:59:49 -0700565 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
566 mParentCount--;
567 if (!mParentCount) {
John Reck44b49f02016-03-25 14:29:48 -0700568 if (observer) {
569 observer->onMaybeRemovedFromTree(this);
570 }
John Reckaa6e84f2016-06-16 15:36:13 -0700571 if (CC_UNLIKELY(mPositionListener.get())) {
572 mPositionListener->onPositionLost(*this, info);
573 }
John Reckdcba6722014-07-08 13:59:49 -0700574 // If a child of ours is being attached to our parent then this will incorrectly
575 // destroy its hardware resources. However, this situation is highly unlikely
576 // and the failure is "just" that the layer is re-created, so this should
577 // be safe enough
John Reckaa6e84f2016-06-16 15:36:13 -0700578 destroyHardwareResources(observer, info);
John Reckdcba6722014-07-08 13:59:49 -0700579 }
580}
581
John Reck113e0822014-03-18 09:22:59 -0700582/*
583 * For property operations, we pass a savecount of 0, since the operations aren't part of the
584 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
John Reckd0a0b2a2014-03-20 16:28:56 -0700585 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
John Reck113e0822014-03-18 09:22:59 -0700586 */
587#define PROPERTY_SAVECOUNT 0
588
589template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700590void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700591#if DEBUG_DISPLAY_LIST
Chris Craikb265e2c2014-03-27 15:50:09 -0700592 properties().debugOutputProperties(handler.level() + 1);
John Reck113e0822014-03-18 09:22:59 -0700593#endif
John Reckd0a0b2a2014-03-20 16:28:56 -0700594 if (properties().getLeft() != 0 || properties().getTop() != 0) {
595 renderer.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700596 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700597 if (properties().getStaticMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500598 renderer.concatMatrix(*properties().getStaticMatrix());
John Reckd0a0b2a2014-03-20 16:28:56 -0700599 } else if (properties().getAnimationMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500600 renderer.concatMatrix(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700601 }
John Reckf7483e32014-04-11 08:54:47 -0700602 if (properties().hasTransformMatrix()) {
603 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700604 renderer.translate(properties().getTranslationX(), properties().getTranslationY());
John Reck113e0822014-03-18 09:22:59 -0700605 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700606 renderer.concatMatrix(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700607 }
608 }
Chris Craik856f0cc2015-04-21 15:13:29 -0700609 const bool isLayer = properties().effectiveLayerType() != LayerType::None;
Chris Craika753f4c2014-07-24 12:39:17 -0700610 int clipFlags = properties().getClippingFlags();
John Reckd0a0b2a2014-03-20 16:28:56 -0700611 if (properties().getAlpha() < 1) {
John Reck25fbb3f2014-06-12 13:46:45 -0700612 if (isLayer) {
Chris Craika753f4c2014-07-24 12:39:17 -0700613 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
John Reck113e0822014-03-18 09:22:59 -0700614 }
Chris Craik4e9d9b22015-06-12 11:07:23 -0700615 if (CC_LIKELY(isLayer || !properties().getHasOverlappingRendering())) {
616 // simply scale rendering content's alpha
617 renderer.scaleAlpha(properties().getAlpha());
618 } else {
619 // savelayer needed to create an offscreen buffer
620 Rect layerBounds(0, 0, getWidth(), getHeight());
621 if (clipFlags) {
622 properties().getClippingRectForFlags(clipFlags, &layerBounds);
623 clipFlags = 0; // all clipping done by savelayer
624 }
625 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
626 layerBounds.left, layerBounds.top,
627 layerBounds.right, layerBounds.bottom,
628 (int) (properties().getAlpha() * 255),
Florin Malitaeecff562015-12-21 10:43:01 -0500629 SaveFlags::HasAlphaLayer | SaveFlags::ClipToLayer);
Chris Craik4e9d9b22015-06-12 11:07:23 -0700630 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
631 }
Chris Craik1a0808e2015-05-13 16:33:04 -0700632
633 if (CC_UNLIKELY(ATRACE_ENABLED() && properties().promotedToLayer())) {
Chris Craik4e9d9b22015-06-12 11:07:23 -0700634 // pretend alpha always causes savelayer to warn about
635 // performance problem affecting old versions
Chris Craik1a0808e2015-05-13 16:33:04 -0700636 ATRACE_FORMAT("%s alpha caused saveLayer %dx%d", getName(),
637 static_cast<int>(getWidth()),
638 static_cast<int>(getHeight()));
639 }
John Reck113e0822014-03-18 09:22:59 -0700640 }
Chris Craika753f4c2014-07-24 12:39:17 -0700641 if (clipFlags) {
642 Rect clipRect;
643 properties().getClippingRectForFlags(clipFlags, &clipRect);
Chris Craik8c271ca2014-03-25 10:33:01 -0700644 ClipRectOp* op = new (handler.allocator()) ClipRectOp(
Chris Craika753f4c2014-07-24 12:39:17 -0700645 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
646 SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700647 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700648 }
Chris Craik8c271ca2014-03-25 10:33:01 -0700649
Chris Craike83cbd42014-09-03 17:52:24 -0700650 // TODO: support nesting round rect clips
Chris Craikaf4d04c2014-07-29 12:50:14 -0700651 if (mProperties.getRevealClip().willClip()) {
652 Rect bounds;
653 mProperties.getRevealClip().getBounds(&bounds);
654 renderer.setClippingRoundRect(handler.allocator(), bounds, mProperties.getRevealClip().getRadius());
655 } else if (mProperties.getOutline().willClip()) {
656 renderer.setClippingOutline(handler.allocator(), &(mProperties.getOutline()));
John Reck113e0822014-03-18 09:22:59 -0700657 }
658}
659
660/**
661 * Apply property-based transformations to input matrix
662 *
663 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
664 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
665 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700666void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700667 if (properties().getLeft() != 0 || properties().getTop() != 0) {
668 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700669 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700670 if (properties().getStaticMatrix()) {
671 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700672 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700673 } else if (properties().getAnimationMatrix()) {
674 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700675 matrix.multiply(anim);
676 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700677
Chris Craikcc39e162014-04-25 18:34:11 -0700678 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700679 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700680 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700681 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700682 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700683 } else {
684 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700685 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700686 } else {
687 mat4 true3dMat;
688 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700689 properties().getPivotX() + properties().getTranslationX(),
690 properties().getPivotY() + properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700691 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700692 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
693 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
694 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
695 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
696 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700697
698 matrix.multiply(true3dMat);
699 }
700 }
701 }
702}
703
704/**
705 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
706 *
707 * This should be called before a call to defer() or drawDisplayList()
708 *
709 * Each DisplayList that serves as a 3d root builds its list of composited children,
710 * which are flagged to not draw in the standard draw loop.
711 */
712void RenderNode::computeOrdering() {
713 ATRACE_CALL();
714 mProjectedNodes.clear();
715
716 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
717 // transform properties are applied correctly to top level children
Chris Craik003cc3d2015-10-16 10:24:55 -0700718 if (mDisplayList == nullptr) return;
Chris Craikb36af872015-10-16 14:23:12 -0700719 for (unsigned int i = 0; i < mDisplayList->getChildren().size(); i++) {
Chris Craik8d1f2122015-11-24 16:40:09 -0800720 renderNodeOp_t* childOp = mDisplayList->getChildren()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700721 childOp->renderNode->computeOrderingImpl(childOp, &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700722 }
723}
724
725void RenderNode::computeOrderingImpl(
Chris Craik8d1f2122015-11-24 16:40:09 -0800726 renderNodeOp_t* opState,
727 std::vector<renderNodeOp_t*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700728 const mat4* transformFromProjectionSurface) {
729 mProjectedNodes.clear();
Chris Craik003cc3d2015-10-16 10:24:55 -0700730 if (mDisplayList == nullptr || mDisplayList->isEmpty()) return;
John Reck113e0822014-03-18 09:22:59 -0700731
732 // TODO: should avoid this calculation in most cases
733 // TODO: just calculate single matrix, down to all leaf composited elements
734 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
Chris Craik8d1f2122015-11-24 16:40:09 -0800735 localTransformFromProjectionSurface.multiply(opState->localMatrix);
John Reck113e0822014-03-18 09:22:59 -0700736
John Reckd0a0b2a2014-03-20 16:28:56 -0700737 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700738 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
Chris Craik8d1f2122015-11-24 16:40:09 -0800739 opState->skipInOrderDraw = true;
740 opState->transformFromCompositingAncestor = localTransformFromProjectionSurface;
John Reck272a6852015-07-29 16:48:58 -0700741 compositedChildrenOfProjectionSurface->push_back(opState);
John Reck113e0822014-03-18 09:22:59 -0700742 } else {
743 // standard in order draw
Chris Craik8d1f2122015-11-24 16:40:09 -0800744 opState->skipInOrderDraw = false;
John Reck113e0822014-03-18 09:22:59 -0700745 }
746
Chris Craikb36af872015-10-16 14:23:12 -0700747 if (mDisplayList->getChildren().size() > 0) {
Chris Craik003cc3d2015-10-16 10:24:55 -0700748 const bool isProjectionReceiver = mDisplayList->projectionReceiveIndex >= 0;
John Reck113e0822014-03-18 09:22:59 -0700749 bool haveAppliedPropertiesToProjection = false;
Chris Craikb36af872015-10-16 14:23:12 -0700750 for (unsigned int i = 0; i < mDisplayList->getChildren().size(); i++) {
Chris Craik8d1f2122015-11-24 16:40:09 -0800751 renderNodeOp_t* childOp = mDisplayList->getChildren()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700752 RenderNode* child = childOp->renderNode;
John Reck113e0822014-03-18 09:22:59 -0700753
Chris Craik8d1f2122015-11-24 16:40:09 -0800754 std::vector<renderNodeOp_t*>* projectionChildren = nullptr;
Chris Craikd41c4d82015-01-05 15:51:13 -0800755 const mat4* projectionTransform = nullptr;
John Reckd0a0b2a2014-03-20 16:28:56 -0700756 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
Chris Craikbf72eb82015-06-08 11:30:44 -0700757 // if receiving projections, collect projecting descendant
John Reck113e0822014-03-18 09:22:59 -0700758
Chris Craikbf72eb82015-06-08 11:30:44 -0700759 // Note that if a direct descendant is projecting backwards, we pass its
760 // grandparent projection collection, since it shouldn't project onto its
John Reck113e0822014-03-18 09:22:59 -0700761 // parent, where it will already be drawing.
762 projectionChildren = &mProjectedNodes;
763 projectionTransform = &mat4::identity();
764 } else {
765 if (!haveAppliedPropertiesToProjection) {
766 applyViewPropertyTransforms(localTransformFromProjectionSurface);
767 haveAppliedPropertiesToProjection = true;
768 }
769 projectionChildren = compositedChildrenOfProjectionSurface;
770 projectionTransform = &localTransformFromProjectionSurface;
771 }
Derek Sollenbergerf2932592015-08-13 14:59:33 -0400772 child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700773 }
774 }
John Reck113e0822014-03-18 09:22:59 -0700775}
776
777class DeferOperationHandler {
778public:
779 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
780 : mDeferStruct(deferStruct), mLevel(level) {}
781 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
782 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
783 }
784 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
Andreas Gampe64bb4132014-11-22 00:35:09 +0000785 inline void startMark(const char* name) {} // do nothing
Chris Craikb265e2c2014-03-27 15:50:09 -0700786 inline void endMark() {}
787 inline int level() { return mLevel; }
788 inline int replayFlags() { return mDeferStruct.mReplayFlags; }
Chris Craik74669862014-08-07 17:27:30 -0700789 inline SkPath* allocPathForFrame() { return mDeferStruct.allocPathForFrame(); }
John Reck113e0822014-03-18 09:22:59 -0700790
791private:
792 DeferStateStruct& mDeferStruct;
793 const int mLevel;
794};
795
Chris Craik80d49022014-06-20 15:03:43 -0700796void RenderNode::defer(DeferStateStruct& deferStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700797 DeferOperationHandler handler(deferStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700798 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700799}
800
801class ReplayOperationHandler {
802public:
803 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
804 : mReplayStruct(replayStruct), mLevel(level) {}
805 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
806#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
Chris Craik3f0854292014-04-15 16:18:08 -0700807 mReplayStruct.mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700808#endif
809 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
810 }
811 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700812 inline void startMark(const char* name) {
813 mReplayStruct.mRenderer.startMark(name);
814 }
815 inline void endMark() {
816 mReplayStruct.mRenderer.endMark();
Chris Craikb265e2c2014-03-27 15:50:09 -0700817 }
818 inline int level() { return mLevel; }
819 inline int replayFlags() { return mReplayStruct.mReplayFlags; }
Chris Craik74669862014-08-07 17:27:30 -0700820 inline SkPath* allocPathForFrame() { return mReplayStruct.allocPathForFrame(); }
John Reck113e0822014-03-18 09:22:59 -0700821
822private:
823 ReplayStateStruct& mReplayStruct;
824 const int mLevel;
825};
826
Chris Craik80d49022014-06-20 15:03:43 -0700827void RenderNode::replay(ReplayStateStruct& replayStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700828 ReplayOperationHandler handler(replayStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700829 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700830}
831
Chris Craik003cc3d2015-10-16 10:24:55 -0700832void RenderNode::buildZSortedChildList(const DisplayList::Chunk& chunk,
John Reck272a6852015-07-29 16:48:58 -0700833 std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes) {
Chris Craikb565df12015-10-05 13:00:52 -0700834#if !HWUI_NEW_OPS
Chris Craik8afd0f22014-08-21 17:41:57 -0700835 if (chunk.beginChildIndex == chunk.endChildIndex) return;
John Reck113e0822014-03-18 09:22:59 -0700836
Chris Craik8afd0f22014-08-21 17:41:57 -0700837 for (unsigned int i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
Chris Craikb36af872015-10-16 14:23:12 -0700838 DrawRenderNodeOp* childOp = mDisplayList->getChildren()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700839 RenderNode* child = childOp->renderNode;
Chris Craikcc39e162014-04-25 18:34:11 -0700840 float childZ = child->properties().getZ();
John Reck113e0822014-03-18 09:22:59 -0700841
Chris Craik8afd0f22014-08-21 17:41:57 -0700842 if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
John Reck272a6852015-07-29 16:48:58 -0700843 zTranslatedNodes.push_back(ZDrawRenderNodeOpPair(childZ, childOp));
Chris Craik8d1f2122015-11-24 16:40:09 -0800844 childOp->skipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700845 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700846 // regular, in order drawing DisplayList
Chris Craik8d1f2122015-11-24 16:40:09 -0800847 childOp->skipInOrderDraw = false;
John Reck113e0822014-03-18 09:22:59 -0700848 }
849 }
850
Chris Craik8afd0f22014-08-21 17:41:57 -0700851 // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
John Reck113e0822014-03-18 09:22:59 -0700852 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
Chris Craikb565df12015-10-05 13:00:52 -0700853#endif
John Reck113e0822014-03-18 09:22:59 -0700854}
855
Chris Craikb265e2c2014-03-27 15:50:09 -0700856template <class T>
857void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
Chris Craik77b5cad2014-07-30 18:23:07 -0700858 if (properties().getAlpha() <= 0.0f
859 || properties().getOutline().getAlpha() <= 0.0f
Teng-Hui Zhu8d0ec382015-10-01 16:49:16 -0700860 || !properties().getOutline().getPath()
861 || properties().getScaleX() == 0
862 || properties().getScaleY() == 0) {
Chris Craik77b5cad2014-07-30 18:23:07 -0700863 // no shadow to draw
864 return;
865 }
Chris Craikb265e2c2014-03-27 15:50:09 -0700866
867 mat4 shadowMatrixXY(transformFromParent);
868 applyViewPropertyTransforms(shadowMatrixXY);
869
870 // Z matrix needs actual 3d transformation, so mapped z values will be correct
871 mat4 shadowMatrixZ(transformFromParent);
872 applyViewPropertyTransforms(shadowMatrixZ, true);
873
Chris Craik74669862014-08-07 17:27:30 -0700874 const SkPath* casterOutlinePath = properties().getOutline().getPath();
Chris Craikaf4d04c2014-07-29 12:50:14 -0700875 const SkPath* revealClipPath = properties().getRevealClip().getPath();
Chris Craik61317322014-05-21 13:03:52 -0700876 if (revealClipPath && revealClipPath->isEmpty()) return;
877
Chris Craik77b5cad2014-07-30 18:23:07 -0700878 float casterAlpha = properties().getAlpha() * properties().getOutline().getAlpha();
Chris Craik74669862014-08-07 17:27:30 -0700879
Chris Craik74669862014-08-07 17:27:30 -0700880
Chris Craikfaa79ff2014-12-01 13:44:21 -0800881 // holds temporary SkPath to store the result of intersections
Chris Craikd41c4d82015-01-05 15:51:13 -0800882 SkPath* frameAllocatedPath = nullptr;
Chris Craikfaa79ff2014-12-01 13:44:21 -0800883 const SkPath* outlinePath = casterOutlinePath;
884
885 // intersect the outline with the reveal clip, if present
886 if (revealClipPath) {
887 frameAllocatedPath = handler.allocPathForFrame();
888
Tom Hudson02a26302015-06-24 11:32:42 -0400889 Op(*outlinePath, *revealClipPath, kIntersect_SkPathOp, frameAllocatedPath);
Chris Craikfaa79ff2014-12-01 13:44:21 -0800890 outlinePath = frameAllocatedPath;
891 }
892
893 // intersect the outline with the clipBounds, if present
894 if (properties().getClippingFlags() & CLIP_TO_CLIP_BOUNDS) {
895 if (!frameAllocatedPath) {
896 frameAllocatedPath = handler.allocPathForFrame();
897 }
898
899 Rect clipBounds;
900 properties().getClippingRectForFlags(CLIP_TO_CLIP_BOUNDS, &clipBounds);
901 SkPath clipBoundsPath;
902 clipBoundsPath.addRect(clipBounds.left, clipBounds.top,
903 clipBounds.right, clipBounds.bottom);
904
Tom Hudson02a26302015-06-24 11:32:42 -0400905 Op(*outlinePath, clipBoundsPath, kIntersect_SkPathOp, frameAllocatedPath);
Chris Craik74669862014-08-07 17:27:30 -0700906 outlinePath = frameAllocatedPath;
907 }
908
Chris Craikb265e2c2014-03-27 15:50:09 -0700909 DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp(
Chris Craik74669862014-08-07 17:27:30 -0700910 shadowMatrixXY, shadowMatrixZ, casterAlpha, outlinePath);
Chris Craikb265e2c2014-03-27 15:50:09 -0700911 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
912}
913
John Reck113e0822014-03-18 09:22:59 -0700914#define SHADOW_DELTA 0.1f
915
916template <class T>
Chris Craikc3e75f92014-08-27 15:34:52 -0700917void RenderNode::issueOperationsOf3dChildren(ChildrenSelectMode mode,
John Reck272a6852015-07-29 16:48:58 -0700918 const Matrix4& initialTransform, const std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craikc3e75f92014-08-27 15:34:52 -0700919 OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700920 const int size = zTranslatedNodes.size();
921 if (size == 0
Chris Craikb9ce116d2015-08-20 15:14:06 -0700922 || (mode == ChildrenSelectMode::NegativeZChildren && zTranslatedNodes[0].key > 0.0f)
923 || (mode == ChildrenSelectMode::PositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
John Reck113e0822014-03-18 09:22:59 -0700924 // no 3d children to draw
925 return;
926 }
927
Chris Craikc3e75f92014-08-27 15:34:52 -0700928 // Apply the base transform of the parent of the 3d children. This isolates
929 // 3d children of the current chunk from transformations made in previous chunks.
Florin Malitaeecff562015-12-21 10:43:01 -0500930 int rootRestoreTo = renderer.save(SaveFlags::Matrix);
Chris Craik6daa13c2015-08-19 13:32:12 -0700931 renderer.setGlobalMatrix(initialTransform);
Chris Craikc3e75f92014-08-27 15:34:52 -0700932
John Reck113e0822014-03-18 09:22:59 -0700933 /**
934 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
935 * with very similar Z heights to draw together.
936 *
937 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
938 * underneath both, and neither's shadow is drawn on top of the other.
939 */
940 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
941 size_t drawIndex, shadowIndex, endIndex;
Chris Craikb9ce116d2015-08-20 15:14:06 -0700942 if (mode == ChildrenSelectMode::NegativeZChildren) {
John Reck113e0822014-03-18 09:22:59 -0700943 drawIndex = 0;
944 endIndex = nonNegativeIndex;
945 shadowIndex = endIndex; // draw no shadows
946 } else {
947 drawIndex = nonNegativeIndex;
948 endIndex = size;
949 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
950 }
Chris Craik3f0854292014-04-15 16:18:08 -0700951
952 DISPLAY_LIST_LOGD("%*s%d %s 3d children:", (handler.level() + 1) * 2, "",
953 endIndex - drawIndex, mode == kNegativeZChildren ? "negative" : "positive");
954
John Reck113e0822014-03-18 09:22:59 -0700955 float lastCasterZ = 0.0f;
956 while (shadowIndex < endIndex || drawIndex < endIndex) {
957 if (shadowIndex < endIndex) {
Chris Craika7090e02014-06-20 16:01:00 -0700958 DrawRenderNodeOp* casterOp = zTranslatedNodes[shadowIndex].value;
Chris Craikb565df12015-10-05 13:00:52 -0700959 RenderNode* caster = casterOp->renderNode;
John Reck113e0822014-03-18 09:22:59 -0700960 const float casterZ = zTranslatedNodes[shadowIndex].key;
961 // attempt to render the shadow if the caster about to be drawn is its caster,
962 // OR if its caster's Z value is similar to the previous potential caster
963 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
Chris Craik8d1f2122015-11-24 16:40:09 -0800964 caster->issueDrawShadowOperation(casterOp->localMatrix, handler);
John Reck113e0822014-03-18 09:22:59 -0700965
966 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
967 shadowIndex++;
968 continue;
969 }
970 }
971
972 // only the actual child DL draw needs to be in save/restore,
973 // since it modifies the renderer's matrix
Florin Malitaeecff562015-12-21 10:43:01 -0500974 int restoreTo = renderer.save(SaveFlags::Matrix);
John Reck113e0822014-03-18 09:22:59 -0700975
Chris Craika7090e02014-06-20 16:01:00 -0700976 DrawRenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
John Reck113e0822014-03-18 09:22:59 -0700977
Chris Craik8d1f2122015-11-24 16:40:09 -0800978 renderer.concatMatrix(childOp->localMatrix);
979 childOp->skipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700980 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
Chris Craik8d1f2122015-11-24 16:40:09 -0800981 childOp->skipInOrderDraw = true;
John Reck113e0822014-03-18 09:22:59 -0700982
983 renderer.restoreToCount(restoreTo);
984 drawIndex++;
985 }
Chris Craikc3e75f92014-08-27 15:34:52 -0700986 renderer.restoreToCount(rootRestoreTo);
John Reck113e0822014-03-18 09:22:59 -0700987}
988
989template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700990void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
Chris Craik3f0854292014-04-15 16:18:08 -0700991 DISPLAY_LIST_LOGD("%*s%d projected children:", (handler.level() + 1) * 2, "", mProjectedNodes.size());
992 const SkPath* projectionReceiverOutline = properties().getOutline().getPath();
Chris Craik3f0854292014-04-15 16:18:08 -0700993 int restoreTo = renderer.getSaveCount();
994
Chris Craikb3cca872014-08-08 18:42:51 -0700995 LinearAllocator& alloc = handler.allocator();
Florin Malitaeecff562015-12-21 10:43:01 -0500996 handler(new (alloc) SaveOp(SaveFlags::MatrixClip),
Chris Craikb3cca872014-08-08 18:42:51 -0700997 PROPERTY_SAVECOUNT, properties().getClipToBounds());
998
999 // Transform renderer to match background we're projecting onto
1000 // (by offsetting canvas by translationX/Y of background rendernode, since only those are set)
1001 const DisplayListOp* op =
Chris Craik10ed6922015-10-15 10:55:15 -07001002#if HWUI_NEW_OPS
1003 nullptr;
1004 LOG_ALWAYS_FATAL("unsupported");
1005#else
Chris Craik003cc3d2015-10-16 10:24:55 -07001006 (mDisplayList->getOps()[mDisplayList->projectionReceiveIndex]);
Chris Craik10ed6922015-10-15 10:55:15 -07001007#endif
Chris Craikb3cca872014-08-08 18:42:51 -07001008 const DrawRenderNodeOp* backgroundOp = reinterpret_cast<const DrawRenderNodeOp*>(op);
Chris Craikb565df12015-10-05 13:00:52 -07001009 const RenderProperties& backgroundProps = backgroundOp->renderNode->properties();
Chris Craikb3cca872014-08-08 18:42:51 -07001010 renderer.translate(backgroundProps.getTranslationX(), backgroundProps.getTranslationY());
1011
Chris Craik6fe991e52015-10-20 09:39:42 -07001012 // If the projection receiver has an outline, we mask projected content to it
Chris Craikfca52b752015-04-28 11:45:59 -07001013 // (which we know, apriori, are all tessellated paths)
1014 renderer.setProjectionPathMask(alloc, projectionReceiverOutline);
Chris Craik3f0854292014-04-15 16:18:08 -07001015
1016 // draw projected nodes
John Reck113e0822014-03-18 09:22:59 -07001017 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
Chris Craik8d1f2122015-11-24 16:40:09 -08001018 renderNodeOp_t* childOp = mProjectedNodes[i];
John Reck113e0822014-03-18 09:22:59 -07001019
1020 // matrix save, concat, and restore can be done safely without allocating operations
Florin Malitaeecff562015-12-21 10:43:01 -05001021 int restoreTo = renderer.save(SaveFlags::Matrix);
Chris Craik8d1f2122015-11-24 16:40:09 -08001022 renderer.concatMatrix(childOp->transformFromCompositingAncestor);
1023 childOp->skipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -07001024 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
Chris Craik8d1f2122015-11-24 16:40:09 -08001025 childOp->skipInOrderDraw = true;
John Reck113e0822014-03-18 09:22:59 -07001026 renderer.restoreToCount(restoreTo);
1027 }
Chris Craik3f0854292014-04-15 16:18:08 -07001028
Chris Craikfca52b752015-04-28 11:45:59 -07001029 handler(new (alloc) RestoreToCountOp(restoreTo),
1030 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -07001031}
1032
1033/**
1034 * This function serves both defer and replay modes, and will organize the displayList's component
1035 * operations for a single frame:
1036 *
1037 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
1038 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
1039 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
1040 * defer vs replay logic, per operation
1041 */
1042template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -07001043void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
Chris Craik003cc3d2015-10-16 10:24:55 -07001044 if (mDisplayList->isEmpty()) {
Chris Craikbf72eb82015-06-08 11:30:44 -07001045 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", handler.level() * 2, "",
1046 this, getName());
Chris Craik06451282014-07-21 10:25:54 -07001047 return;
1048 }
1049
Chris Craik0b7e8242015-10-28 16:50:44 -07001050#if HWUI_NEW_OPS
1051 const bool drawLayer = false;
1052#else
Chris Craik51d6a3d2014-12-22 17:16:56 -08001053 const bool drawLayer = (mLayer && (&renderer != mLayer->renderer.get()));
Chris Craik0b7e8242015-10-28 16:50:44 -07001054#endif
John Reck25fbb3f2014-06-12 13:46:45 -07001055 // If we are updating the contents of mLayer, we don't want to apply any of
1056 // the RenderNode's properties to this issueOperations pass. Those will all
1057 // be applied when the layer is drawn, aka when this is true.
1058 const bool useViewProperties = (!mLayer || drawLayer);
Chris Craik06451282014-07-21 10:25:54 -07001059 if (useViewProperties) {
1060 const Outline& outline = properties().getOutline();
Teng-Hui Zhu8d0ec382015-10-01 16:49:16 -07001061 if (properties().getAlpha() <= 0
1062 || (outline.getShouldClip() && outline.isEmpty())
1063 || properties().getScaleX() == 0
1064 || properties().getScaleY() == 0) {
Chris Craikbf72eb82015-06-08 11:30:44 -07001065 DISPLAY_LIST_LOGD("%*sRejected display list (%p, %s)", handler.level() * 2, "",
1066 this, getName());
Chris Craik06451282014-07-21 10:25:54 -07001067 return;
1068 }
John Reck113e0822014-03-18 09:22:59 -07001069 }
1070
Chris Craik3f0854292014-04-15 16:18:08 -07001071 handler.startMark(getName());
Chris Craikb265e2c2014-03-27 15:50:09 -07001072
John Reck113e0822014-03-18 09:22:59 -07001073#if DEBUG_DISPLAY_LIST
Chris Craik3f0854292014-04-15 16:18:08 -07001074 const Rect& clipRect = renderer.getLocalClipBounds();
1075 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), localClipBounds: %.0f, %.0f, %.0f, %.0f",
Chris Craik03188872015-02-02 18:39:33 -08001076 handler.level() * 2, "", this, getName(),
Chris Craik3f0854292014-04-15 16:18:08 -07001077 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
John Reck113e0822014-03-18 09:22:59 -07001078#endif
1079
1080 LinearAllocator& alloc = handler.allocator();
1081 int restoreTo = renderer.getSaveCount();
Florin Malitaeecff562015-12-21 10:43:01 -05001082 handler(new (alloc) SaveOp(SaveFlags::MatrixClip),
John Reckd0a0b2a2014-03-20 16:28:56 -07001083 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -07001084
Chris Craikbf72eb82015-06-08 11:30:44 -07001085 DISPLAY_LIST_LOGD("%*sSave %d %d", (handler.level() + 1) * 2, "",
Florin Malitaeecff562015-12-21 10:43:01 -05001086 SaveFlags::MatrixClip, restoreTo);
John Reck113e0822014-03-18 09:22:59 -07001087
John Reck25fbb3f2014-06-12 13:46:45 -07001088 if (useViewProperties) {
1089 setViewProperties<T>(renderer, handler);
1090 }
John Reck113e0822014-03-18 09:22:59 -07001091
Chris Craik10ed6922015-10-15 10:55:15 -07001092#if HWUI_NEW_OPS
1093 LOG_ALWAYS_FATAL("legacy op traversal not supported");
1094#else
Chris Craik8c271ca2014-03-25 10:33:01 -07001095 bool quickRejected = properties().getClipToBounds()
1096 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -07001097 if (!quickRejected) {
Chris Craikc3e75f92014-08-27 15:34:52 -07001098 Matrix4 initialTransform(*(renderer.currentTransform()));
Tom Hudsonac7b6d32015-06-30 11:26:13 -04001099 renderer.setBaseTransform(initialTransform);
Chris Craikc3e75f92014-08-27 15:34:52 -07001100
John Reck25fbb3f2014-06-12 13:46:45 -07001101 if (drawLayer) {
Chris Craik3aadd602015-08-20 12:41:40 -07001102 handler(new (alloc) DrawLayerOp(mLayer),
John Reck25fbb3f2014-06-12 13:46:45 -07001103 renderer.getSaveCount() - 1, properties().getClipToBounds());
1104 } else {
Chris Craikc166b6c2014-09-05 19:55:30 -07001105 const int saveCountOffset = renderer.getSaveCount() - 1;
Chris Craik003cc3d2015-10-16 10:24:55 -07001106 const int projectionReceiveIndex = mDisplayList->projectionReceiveIndex;
1107 for (size_t chunkIndex = 0; chunkIndex < mDisplayList->getChunks().size(); chunkIndex++) {
1108 const DisplayList::Chunk& chunk = mDisplayList->getChunks()[chunkIndex];
John Reck113e0822014-03-18 09:22:59 -07001109
John Reck272a6852015-07-29 16:48:58 -07001110 std::vector<ZDrawRenderNodeOpPair> zTranslatedNodes;
Chris Craik8afd0f22014-08-21 17:41:57 -07001111 buildZSortedChildList(chunk, zTranslatedNodes);
1112
Chris Craikb9ce116d2015-08-20 15:14:06 -07001113 issueOperationsOf3dChildren(ChildrenSelectMode::NegativeZChildren,
Chris Craikc3e75f92014-08-27 15:34:52 -07001114 initialTransform, zTranslatedNodes, renderer, handler);
1115
Andreas Gampeedaecc12014-11-10 20:54:07 -08001116 for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
Chris Craik003cc3d2015-10-16 10:24:55 -07001117 DisplayListOp *op = mDisplayList->getOps()[opIndex];
Chris Craik80d49022014-06-20 15:03:43 -07001118#if DEBUG_DISPLAY_LIST
Chris Craik03188872015-02-02 18:39:33 -08001119 op->output(handler.level() + 1);
Chris Craik80d49022014-06-20 15:03:43 -07001120#endif
Chris Craik8afd0f22014-08-21 17:41:57 -07001121 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -07001122
John Reck272a6852015-07-29 16:48:58 -07001123 if (CC_UNLIKELY(!mProjectedNodes.empty() && projectionReceiveIndex >= 0 &&
Andreas Gampeedaecc12014-11-10 20:54:07 -08001124 opIndex == static_cast<size_t>(projectionReceiveIndex))) {
Chris Craik8afd0f22014-08-21 17:41:57 -07001125 issueOperationsOfProjectedChildren(renderer, handler);
1126 }
John Reck25fbb3f2014-06-12 13:46:45 -07001127 }
John Reck113e0822014-03-18 09:22:59 -07001128
Chris Craikb9ce116d2015-08-20 15:14:06 -07001129 issueOperationsOf3dChildren(ChildrenSelectMode::PositiveZChildren,
Chris Craikc3e75f92014-08-27 15:34:52 -07001130 initialTransform, zTranslatedNodes, renderer, handler);
Chris Craik8afd0f22014-08-21 17:41:57 -07001131 }
John Reck25fbb3f2014-06-12 13:46:45 -07001132 }
John Reck113e0822014-03-18 09:22:59 -07001133 }
Chris Craik10ed6922015-10-15 10:55:15 -07001134#endif
John Reck113e0822014-03-18 09:22:59 -07001135
Chris Craikbf72eb82015-06-08 11:30:44 -07001136 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (handler.level() + 1) * 2, "", restoreTo);
John Reck113e0822014-03-18 09:22:59 -07001137 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -07001138 PROPERTY_SAVECOUNT, properties().getClipToBounds());
Chris Craikb265e2c2014-03-27 15:50:09 -07001139
Chris Craikbf72eb82015-06-08 11:30:44 -07001140 DISPLAY_LIST_LOGD("%*sDone (%p, %s)", handler.level() * 2, "", this, getName());
Chris Craikb265e2c2014-03-27 15:50:09 -07001141 handler.endMark();
John Reck113e0822014-03-18 09:22:59 -07001142}
1143
1144} /* namespace uirenderer */
1145} /* namespace android */