blob: d122a55ef70273de57aba7632021e383b879bf28 [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 Recke45b1fd2014-04-15 09:50:16 -070019#include <algorithm>
John Reckc25e5062014-06-18 14:21:29 -070020#include <string>
John Recke45b1fd2014-04-15 09:50:16 -070021
John Reck113e0822014-03-18 09:22:59 -070022#include <SkCanvas.h>
23#include <algorithm>
24
John Reck113e0822014-03-18 09:22:59 -070025
John Recke4267ea2014-06-03 15:53:15 -070026#include "DamageAccumulator.h"
John Reck113e0822014-03-18 09:22:59 -070027#include "Debug.h"
Chris Craikb565df12015-10-05 13:00:52 -070028#if HWUI_NEW_OPS
29#include "RecordedOp.h"
30#endif
John Reck113e0822014-03-18 09:22:59 -070031#include "DisplayListOp.h"
John Reck25fbb3f2014-06-12 13:46:45 -070032#include "LayerRenderer.h"
33#include "OpenGLRenderer.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040034#include "TreeInfo.h"
Chris Craike0bb87d2014-04-22 17:55:41 -070035#include "utils/MathUtils.h"
Chris Craik70850ea2014-11-18 10:49:23 -080036#include "utils/TraceUtils.h"
John Reck998a6d82014-08-28 15:35:53 -070037#include "renderthread/CanvasContext.h"
John Reck113e0822014-03-18 09:22:59 -070038
John Recke248bd12015-08-05 13:53:53 -070039#include "protos/hwui.pb.h"
40#include "protos/ProtoHelpers.h"
41
John Reck113e0822014-03-18 09:22:59 -070042namespace android {
43namespace uirenderer {
44
John Reck443a7142014-09-04 17:40:05 -070045void RenderNode::debugDumpLayers(const char* prefix) {
46 if (mLayer) {
47 ALOGD("%sNode %p (%s) has layer %p (fbo = %u, wasBuildLayered = %s)",
48 prefix, this, getName(), mLayer, mLayer->getFbo(),
49 mLayer->wasBuildLayered ? "true" : "false");
50 }
51 if (mDisplayListData) {
52 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craikb565df12015-10-05 13:00:52 -070053 mDisplayListData->children()[i]->renderNode->debugDumpLayers(prefix);
John Reck443a7142014-09-04 17:40:05 -070054 }
55 }
56}
57
John Reck8de65a82014-04-09 15:23:38 -070058RenderNode::RenderNode()
John Reckff941dc2014-05-14 16:34:14 -070059 : mDirtyPropertyFields(0)
John Reck8de65a82014-04-09 15:23:38 -070060 , mNeedsDisplayListDataSync(false)
Chris Craikd41c4d82015-01-05 15:51:13 -080061 , mDisplayListData(nullptr)
62 , mStagingDisplayListData(nullptr)
John Reck68bfe0a2014-06-24 15:34:58 -070063 , mAnimatorManager(*this)
Chris Craikd41c4d82015-01-05 15:51:13 -080064 , mLayer(nullptr)
John Reckdcba6722014-07-08 13:59:49 -070065 , mParentCount(0) {
John Reck113e0822014-03-18 09:22:59 -070066}
67
68RenderNode::~RenderNode() {
John Reckdcba6722014-07-08 13:59:49 -070069 deleteDisplayListData();
John Reck8de65a82014-04-09 15:23:38 -070070 delete mStagingDisplayListData;
John Reck0e89e2b2014-10-31 14:49:06 -070071 if (mLayer) {
72 ALOGW("Memory Warning: Layer %p missed its detachment, held on to for far too long!", mLayer);
73 mLayer->postDecStrong();
Chris Craikd41c4d82015-01-05 15:51:13 -080074 mLayer = nullptr;
John Reck0e89e2b2014-10-31 14:49:06 -070075 }
John Reck113e0822014-03-18 09:22:59 -070076}
77
John Reck8de65a82014-04-09 15:23:38 -070078void RenderNode::setStagingDisplayList(DisplayListData* data) {
79 mNeedsDisplayListDataSync = true;
80 delete mStagingDisplayListData;
81 mStagingDisplayListData = data;
John Reck113e0822014-03-18 09:22:59 -070082}
83
84/**
85 * This function is a simplified version of replay(), where we simply retrieve and log the
86 * display list. This function should remain in sync with the replay() function.
87 */
88void RenderNode::output(uint32_t level) {
Chris Craikbf72eb82015-06-08 11:30:44 -070089 ALOGD("%*sStart display list (%p, %s%s%s%s%s%s)", (level - 1) * 2, "", this,
Chris Craikb5a54352014-11-21 14:54:35 -080090 getName(),
Chris Craik43a1d312015-05-27 11:28:14 -070091 (MathUtils::isZero(properties().getAlpha()) ? ", zero alpha" : ""),
Chris Craikb5a54352014-11-21 14:54:35 -080092 (properties().hasShadow() ? ", casting shadow" : ""),
93 (isRenderable() ? "" : ", empty"),
Chris Craikbf72eb82015-06-08 11:30:44 -070094 (properties().getProjectBackwards() ? ", projected" : ""),
Chris Craikd41c4d82015-01-05 15:51:13 -080095 (mLayer != nullptr ? ", on HW Layer" : ""));
John Reck113e0822014-03-18 09:22:59 -070096 ALOGD("%*s%s %d", level * 2, "", "Save",
97 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
98
John Reckd0a0b2a2014-03-20 16:28:56 -070099 properties().debugOutputProperties(level);
John Reck113e0822014-03-18 09:22:59 -0700100 int flags = DisplayListOp::kOpLogFlag_Recurse;
John Reckdc0349b2014-08-06 15:28:07 -0700101 if (mDisplayListData) {
Chris Craik8afd0f22014-08-21 17:41:57 -0700102 // TODO: consider printing the chunk boundaries here
John Reckdc0349b2014-08-06 15:28:07 -0700103 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
104 mDisplayListData->displayListOps[i]->output(level, flags);
105 }
John Reck113e0822014-03-18 09:22:59 -0700106 }
107
Chris Craik3f0854292014-04-15 16:18:08 -0700108 ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, getName());
John Reck113e0822014-03-18 09:22:59 -0700109}
110
John Recke248bd12015-08-05 13:53:53 -0700111void RenderNode::copyTo(proto::RenderNode *pnode) {
112 pnode->set_id(static_cast<uint64_t>(
113 reinterpret_cast<uintptr_t>(this)));
114 pnode->set_name(mName.string(), mName.length());
115
116 proto::RenderProperties* pprops = pnode->mutable_properties();
117 pprops->set_left(properties().getLeft());
118 pprops->set_top(properties().getTop());
119 pprops->set_right(properties().getRight());
120 pprops->set_bottom(properties().getBottom());
121 pprops->set_clip_flags(properties().getClippingFlags());
122 pprops->set_alpha(properties().getAlpha());
123 pprops->set_translation_x(properties().getTranslationX());
124 pprops->set_translation_y(properties().getTranslationY());
125 pprops->set_translation_z(properties().getTranslationZ());
126 pprops->set_elevation(properties().getElevation());
127 pprops->set_rotation(properties().getRotation());
128 pprops->set_rotation_x(properties().getRotationX());
129 pprops->set_rotation_y(properties().getRotationY());
130 pprops->set_scale_x(properties().getScaleX());
131 pprops->set_scale_y(properties().getScaleY());
132 pprops->set_pivot_x(properties().getPivotX());
133 pprops->set_pivot_y(properties().getPivotY());
134 pprops->set_has_overlapping_rendering(properties().getHasOverlappingRendering());
135 pprops->set_pivot_explicitly_set(properties().isPivotExplicitlySet());
136 pprops->set_project_backwards(properties().getProjectBackwards());
137 pprops->set_projection_receiver(properties().isProjectionReceiver());
138 set(pprops->mutable_clip_bounds(), properties().getClipBounds());
139
140 const Outline& outline = properties().getOutline();
141 if (outline.getType() != Outline::Type::None) {
142 proto::Outline* poutline = pprops->mutable_outline();
143 poutline->clear_path();
144 if (outline.getType() == Outline::Type::Empty) {
145 poutline->set_type(proto::Outline_Type_Empty);
146 } else if (outline.getType() == Outline::Type::ConvexPath) {
147 poutline->set_type(proto::Outline_Type_ConvexPath);
148 if (const SkPath* path = outline.getPath()) {
149 set(poutline->mutable_path(), *path);
150 }
151 } else if (outline.getType() == Outline::Type::RoundRect) {
152 poutline->set_type(proto::Outline_Type_RoundRect);
153 } else {
154 ALOGW("Uknown outline type! %d", static_cast<int>(outline.getType()));
155 poutline->set_type(proto::Outline_Type_None);
156 }
157 poutline->set_should_clip(outline.getShouldClip());
158 poutline->set_alpha(outline.getAlpha());
159 poutline->set_radius(outline.getRadius());
160 set(poutline->mutable_bounds(), outline.getBounds());
161 } else {
162 pprops->clear_outline();
163 }
164
165 const RevealClip& revealClip = properties().getRevealClip();
166 if (revealClip.willClip()) {
167 proto::RevealClip* prevealClip = pprops->mutable_reveal_clip();
168 prevealClip->set_x(revealClip.getX());
169 prevealClip->set_y(revealClip.getY());
170 prevealClip->set_radius(revealClip.getRadius());
171 } else {
172 pprops->clear_reveal_clip();
173 }
174
175 pnode->clear_children();
176 if (mDisplayListData) {
177 for (auto&& child : mDisplayListData->children()) {
Chris Craikb565df12015-10-05 13:00:52 -0700178 child->renderNode->copyTo(pnode->add_children());
John Recke248bd12015-08-05 13:53:53 -0700179 }
180 }
181}
182
John Reckfe5e7b72014-05-23 17:42:28 -0700183int RenderNode::getDebugSize() {
184 int size = sizeof(RenderNode);
185 if (mStagingDisplayListData) {
Chris Craik8afd0f22014-08-21 17:41:57 -0700186 size += mStagingDisplayListData->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700187 }
188 if (mDisplayListData && mDisplayListData != mStagingDisplayListData) {
Chris Craik8afd0f22014-08-21 17:41:57 -0700189 size += mDisplayListData->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700190 }
191 return size;
192}
193
John Reckf4198b72014-04-09 17:00:04 -0700194void RenderNode::prepareTree(TreeInfo& info) {
195 ATRACE_CALL();
Chris Craik69e5adf2014-08-14 13:34:01 -0700196 LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
John Reckf4198b72014-04-09 17:00:04 -0700197
Chris Craika766cb22015-06-08 16:49:43 -0700198 // Functors don't correctly handle stencil usage of overdraw debugging - shove 'em in a layer.
199 bool functorsNeedLayer = Properties::debugOverdraw;
200
201 prepareTreeImpl(info, functorsNeedLayer);
John Reckf4198b72014-04-09 17:00:04 -0700202}
203
John Reck68bfe0a2014-06-24 15:34:58 -0700204void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
205 mAnimatorManager.addAnimator(animator);
206}
207
John Recke4267ea2014-06-03 15:53:15 -0700208void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700209 if (isRenderable()) {
John Reck293e8682014-06-17 10:34:02 -0700210 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700211 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
212 } else {
213 // Hope this is big enough?
214 // TODO: Get this from the display list ops or something
John Reckc1288232015-08-12 13:39:11 -0700215 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
John Recka447d292014-06-11 18:39:44 -0700216 }
John Recke4267ea2014-06-03 15:53:15 -0700217 }
218}
219
John Recka7c2ea22014-08-08 13:21:00 -0700220void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700221 LayerType layerType = properties().effectiveLayerType();
Chris Craik182952f2015-03-09 14:17:29 -0700222 if (CC_UNLIKELY(layerType == LayerType::RenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700223 // Damage applied so far needs to affect our parent, but does not require
224 // the layer to be updated. So we pop/push here to clear out the current
225 // damage and get a clean state for display list or children updates to
226 // affect, which will require the layer to be updated
227 info.damageAccumulator->popTransform();
228 info.damageAccumulator->pushTransform(this);
229 if (dirtyMask & DISPLAY_LIST) {
230 damageSelf(info);
231 }
John Reck25fbb3f2014-06-12 13:46:45 -0700232 }
233}
234
235void RenderNode::pushLayerUpdate(TreeInfo& info) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700236 LayerType layerType = properties().effectiveLayerType();
John Reck25fbb3f2014-06-12 13:46:45 -0700237 // If we are not a layer OR we cannot be rendered (eg, view was detached)
238 // we need to destroy any Layers we may have had previously
Chris Craik182952f2015-03-09 14:17:29 -0700239 if (CC_LIKELY(layerType != LayerType::RenderLayer) || CC_UNLIKELY(!isRenderable())) {
John Reck25fbb3f2014-06-12 13:46:45 -0700240 if (CC_UNLIKELY(mLayer)) {
241 LayerRenderer::destroyLayer(mLayer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800242 mLayer = nullptr;
John Reck25fbb3f2014-06-12 13:46:45 -0700243 }
244 return;
245 }
246
Chris Craik69e5adf2014-08-14 13:34:01 -0700247 bool transformUpdateNeeded = false;
John Reck25fbb3f2014-06-12 13:46:45 -0700248 if (!mLayer) {
John Reck3b202512014-06-23 13:13:08 -0700249 mLayer = LayerRenderer::createRenderLayer(info.renderState, getWidth(), getHeight());
John Reck25fbb3f2014-06-12 13:46:45 -0700250 applyLayerPropertiesToLayer(info);
251 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700252 transformUpdateNeeded = true;
John Reck25fbb3f2014-06-12 13:46:45 -0700253 } else if (mLayer->layer.getWidth() != getWidth() || mLayer->layer.getHeight() != getHeight()) {
John Reckc25e5062014-06-18 14:21:29 -0700254 if (!LayerRenderer::resizeLayer(mLayer, getWidth(), getHeight())) {
255 LayerRenderer::destroyLayer(mLayer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800256 mLayer = nullptr;
John Reckc25e5062014-06-18 14:21:29 -0700257 }
John Reck25fbb3f2014-06-12 13:46:45 -0700258 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700259 transformUpdateNeeded = true;
260 }
261
John Reck25fbb3f2014-06-12 13:46:45 -0700262 SkRect dirty;
263 info.damageAccumulator->peekAtDirty(&dirty);
John Reck25fbb3f2014-06-12 13:46:45 -0700264
John Reckc25e5062014-06-18 14:21:29 -0700265 if (!mLayer) {
John Reck0e89e2b2014-10-31 14:49:06 -0700266 Caches::getInstance().dumpMemoryUsage();
John Reckc25e5062014-06-18 14:21:29 -0700267 if (info.errorHandler) {
268 std::string msg = "Unable to create layer for ";
269 msg += getName();
270 info.errorHandler->onError(msg);
271 }
272 return;
273 }
274
Chris Craikc71bfca2014-08-21 10:18:58 -0700275 if (transformUpdateNeeded) {
276 // update the transform in window of the layer to reset its origin wrt light source position
277 Matrix4 windowTransform;
278 info.damageAccumulator->computeCurrentTransform(&windowTransform);
279 mLayer->setWindowTransform(windowTransform);
280 }
John Reckc79eabc2014-08-05 11:03:42 -0700281
282 if (dirty.intersect(0, 0, getWidth(), getHeight())) {
Mike Reed71487eb2014-11-19 16:13:20 -0500283 dirty.roundOut(&dirty);
John Reck25fbb3f2014-06-12 13:46:45 -0700284 mLayer->updateDeferred(this, dirty.fLeft, dirty.fTop, dirty.fRight, dirty.fBottom);
285 }
286 // This is not inside the above if because we may have called
287 // updateDeferred on a previous prepare pass that didn't have a renderer
288 if (info.renderer && mLayer->deferredUpdateScheduled) {
289 info.renderer->pushLayerUpdate(mLayer);
290 }
John Reck998a6d82014-08-28 15:35:53 -0700291
John Reck00e79c92015-07-21 10:23:59 -0700292 if (info.canvasContext) {
293 // There might be prefetched layers that need to be accounted for.
294 // That might be us, so tell CanvasContext that this layer is in the
295 // tree and should not be destroyed.
John Reck998a6d82014-08-28 15:35:53 -0700296 info.canvasContext->markLayerInUse(this);
297 }
John Reck25fbb3f2014-06-12 13:46:45 -0700298}
299
Chris Craika766cb22015-06-08 16:49:43 -0700300/**
301 * Traverse down the the draw tree to prepare for a frame.
302 *
303 * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven
304 *
305 * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the
306 * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer.
307 */
308void RenderNode::prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer) {
John Recka447d292014-06-11 18:39:44 -0700309 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700310
John Reckdcba6722014-07-08 13:59:49 -0700311 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700312 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700313 }
John Reck9eb9f6f2014-08-21 11:23:05 -0700314 uint32_t animatorDirtyMask = 0;
315 if (CC_LIKELY(info.runAnimations)) {
316 animatorDirtyMask = mAnimatorManager.animate(info);
317 }
Chris Craika766cb22015-06-08 16:49:43 -0700318
John Reck3f725f02015-06-16 10:29:31 -0700319 bool willHaveFunctor = false;
320 if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayListData) {
321 willHaveFunctor = !mStagingDisplayListData->functors.isEmpty();
322 } else if (mDisplayListData) {
323 willHaveFunctor = !mDisplayListData->functors.isEmpty();
324 }
Chris Craika766cb22015-06-08 16:49:43 -0700325 bool childFunctorsNeedLayer = mProperties.prepareForFunctorPresence(
326 willHaveFunctor, functorsNeedLayer);
327
John Recka7c2ea22014-08-08 13:21:00 -0700328 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700329 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700330 pushStagingDisplayListChanges(info);
331 }
Chris Craika766cb22015-06-08 16:49:43 -0700332 prepareSubTree(info, childFunctorsNeedLayer, mDisplayListData);
John Reck25fbb3f2014-06-12 13:46:45 -0700333 pushLayerUpdate(info);
334
John Recka447d292014-06-11 18:39:44 -0700335 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700336}
337
Chris Craikb565df12015-10-05 13:00:52 -0700338void RenderNode::syncProperties() {
339 mProperties = mStagingProperties;
340}
341
John Reck25fbb3f2014-06-12 13:46:45 -0700342void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reckff941dc2014-05-14 16:34:14 -0700343 // Push the animators first so that setupStartValueIfNecessary() is called
344 // before properties() is trampled by stagingProperties(), as they are
345 // required by some animators.
John Reck9eb9f6f2014-08-21 11:23:05 -0700346 if (CC_LIKELY(info.runAnimations)) {
John Reck119907c2014-08-14 09:02:01 -0700347 mAnimatorManager.pushStaging();
John Reck9eb9f6f2014-08-21 11:23:05 -0700348 }
John Reckff941dc2014-05-14 16:34:14 -0700349 if (mDirtyPropertyFields) {
350 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700351 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700352 info.damageAccumulator->popTransform();
Chris Craikb565df12015-10-05 13:00:52 -0700353 syncProperties();
John Reck25fbb3f2014-06-12 13:46:45 -0700354 applyLayerPropertiesToLayer(info);
John Recke4267ea2014-06-03 15:53:15 -0700355 // We could try to be clever and only re-damage if the matrix changed.
356 // However, we don't need to worry about that. The cost of over-damaging
357 // here is only going to be a single additional map rect of this node
358 // plus a rect join(). The parent's transform (and up) will only be
359 // performed once.
John Recka447d292014-06-11 18:39:44 -0700360 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700361 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700362 }
John Reck25fbb3f2014-06-12 13:46:45 -0700363}
364
Andreas Gampe64bb4132014-11-22 00:35:09 +0000365void RenderNode::applyLayerPropertiesToLayer(TreeInfo& info) {
John Reck25fbb3f2014-06-12 13:46:45 -0700366 if (CC_LIKELY(!mLayer)) return;
367
368 const LayerProperties& props = properties().layerProperties();
369 mLayer->setAlpha(props.alpha(), props.xferMode());
370 mLayer->setColorFilter(props.colorFilter());
371 mLayer->setBlend(props.needsBlending());
372}
373
Chris Craikb565df12015-10-05 13:00:52 -0700374void RenderNode::syncDisplayList() {
375 // Make sure we inc first so that we don't fluctuate between 0 and 1,
376 // which would thrash the layer cache
377 if (mStagingDisplayListData) {
378 for (auto&& child : mStagingDisplayListData->children()) {
379 child->renderNode->incParentRefCount();
380 }
381 }
382 deleteDisplayListData();
383 mDisplayListData = mStagingDisplayListData;
384 mStagingDisplayListData = nullptr;
385 if (mDisplayListData) {
386 for (size_t i = 0; i < mDisplayListData->functors.size(); i++) {
387 (*mDisplayListData->functors[i])(DrawGlInfo::kModeSync, nullptr);
388 }
389 }
390}
391
John Reck25fbb3f2014-06-12 13:46:45 -0700392void RenderNode::pushStagingDisplayListChanges(TreeInfo& info) {
John Reck8de65a82014-04-09 15:23:38 -0700393 if (mNeedsDisplayListDataSync) {
394 mNeedsDisplayListDataSync = false;
John Reck5c9d7172014-10-22 11:32:27 -0700395 // Damage with the old display list first then the new one to catch any
396 // changes in isRenderable or, in the future, bounds
397 damageSelf(info);
Chris Craikb565df12015-10-05 13:00:52 -0700398 syncDisplayList();
John Recke4267ea2014-06-03 15:53:15 -0700399 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700400 }
John Reck8de65a82014-04-09 15:23:38 -0700401}
402
John Reckdcba6722014-07-08 13:59:49 -0700403void RenderNode::deleteDisplayListData() {
404 if (mDisplayListData) {
Chris Craikb565df12015-10-05 13:00:52 -0700405 for (auto&& child : mDisplayListData->children()) {
406 child->renderNode->decParentRefCount();
John Reckdcba6722014-07-08 13:59:49 -0700407 }
408 }
409 delete mDisplayListData;
Chris Craikd41c4d82015-01-05 15:51:13 -0800410 mDisplayListData = nullptr;
John Reckdcba6722014-07-08 13:59:49 -0700411}
412
Chris Craika766cb22015-06-08 16:49:43 -0700413void RenderNode::prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayListData* subtree) {
John Reck8de65a82014-04-09 15:23:38 -0700414 if (subtree) {
John Reck860d1552014-04-11 19:15:05 -0700415 TextureCache& cache = Caches::getInstance().textureCache;
John Reck09d5cdd2014-07-24 10:36:08 -0700416 info.out.hasFunctors |= subtree->functors.size();
John Reck860d1552014-04-11 19:15:05 -0700417 for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) {
John Reck00e79c92015-07-21 10:23:59 -0700418 info.prepareTextures = cache.prefetchAndMarkInUse(
419 info.canvasContext, subtree->bitmapResources[i]);
John Reckf4198b72014-04-09 17:00:04 -0700420 }
Chris Craikb565df12015-10-05 13:00:52 -0700421 for (auto&& op : subtree->children()) {
422 RenderNode* childNode = op->renderNode;
423#if HWUI_NEW_OPS
424 info.damageAccumulator->pushTransform(&op->localMatrix);
425 bool childFunctorsNeedLayer = functorsNeedLayer; // TODO! || op->mRecordedWithPotentialStencilClip;
426#else
John Recka447d292014-06-11 18:39:44 -0700427 info.damageAccumulator->pushTransform(&op->mTransformFromParent);
Chris Craika766cb22015-06-08 16:49:43 -0700428 bool childFunctorsNeedLayer = functorsNeedLayer
429 // Recorded with non-rect clip, or canvas-rotated by parent
430 || op->mRecordedWithPotentialStencilClip;
Chris Craikb565df12015-10-05 13:00:52 -0700431#endif
Chris Craika766cb22015-06-08 16:49:43 -0700432 childNode->prepareTreeImpl(info, childFunctorsNeedLayer);
John Recka447d292014-06-11 18:39:44 -0700433 info.damageAccumulator->popTransform();
John Reck5bf11bb2014-03-25 10:22:09 -0700434 }
John Reck113e0822014-03-18 09:22:59 -0700435 }
436}
437
John Reckdcba6722014-07-08 13:59:49 -0700438void RenderNode::destroyHardwareResources() {
439 if (mLayer) {
440 LayerRenderer::destroyLayer(mLayer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800441 mLayer = nullptr;
John Reckdcba6722014-07-08 13:59:49 -0700442 }
443 if (mDisplayListData) {
Chris Craikb565df12015-10-05 13:00:52 -0700444 for (auto&& child : mDisplayListData->children()) {
445 child->renderNode->destroyHardwareResources();
John Reckdcba6722014-07-08 13:59:49 -0700446 }
447 if (mNeedsDisplayListDataSync) {
448 // Next prepare tree we are going to push a new display list, so we can
449 // drop our current one now
450 deleteDisplayListData();
451 }
452 }
453}
454
455void RenderNode::decParentRefCount() {
456 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
457 mParentCount--;
458 if (!mParentCount) {
459 // If a child of ours is being attached to our parent then this will incorrectly
460 // destroy its hardware resources. However, this situation is highly unlikely
461 // and the failure is "just" that the layer is re-created, so this should
462 // be safe enough
463 destroyHardwareResources();
464 }
465}
466
Chris Craikb565df12015-10-05 13:00:52 -0700467bool RenderNode::applyViewProperties(CanvasState& canvasState) const {
468 const Outline& outline = properties().getOutline();
469 if (properties().getAlpha() <= 0
470 || (outline.getShouldClip() && outline.isEmpty())
471 || properties().getScaleX() == 0
472 || properties().getScaleY() == 0) {
473 return false; // rejected
474 }
475
476 if (properties().getLeft() != 0 || properties().getTop() != 0) {
477 canvasState.translate(properties().getLeft(), properties().getTop());
478 }
479 if (properties().getStaticMatrix()) {
480 canvasState.concatMatrix(*properties().getStaticMatrix());
481 } else if (properties().getAnimationMatrix()) {
482 canvasState.concatMatrix(*properties().getAnimationMatrix());
483 }
484 if (properties().hasTransformMatrix()) {
485 if (properties().isTransformTranslateOnly()) {
486 canvasState.translate(properties().getTranslationX(), properties().getTranslationY());
487 } else {
488 canvasState.concatMatrix(*properties().getTransformMatrix());
489 }
490 }
491 return !canvasState.quickRejectConservative(
492 0, 0, properties().getWidth(), properties().getHeight());
493}
494
John Reck113e0822014-03-18 09:22:59 -0700495/*
496 * For property operations, we pass a savecount of 0, since the operations aren't part of the
497 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
John Reckd0a0b2a2014-03-20 16:28:56 -0700498 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
John Reck113e0822014-03-18 09:22:59 -0700499 */
500#define PROPERTY_SAVECOUNT 0
501
502template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700503void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700504#if DEBUG_DISPLAY_LIST
Chris Craikb265e2c2014-03-27 15:50:09 -0700505 properties().debugOutputProperties(handler.level() + 1);
John Reck113e0822014-03-18 09:22:59 -0700506#endif
John Reckd0a0b2a2014-03-20 16:28:56 -0700507 if (properties().getLeft() != 0 || properties().getTop() != 0) {
508 renderer.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700509 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700510 if (properties().getStaticMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500511 renderer.concatMatrix(*properties().getStaticMatrix());
John Reckd0a0b2a2014-03-20 16:28:56 -0700512 } else if (properties().getAnimationMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500513 renderer.concatMatrix(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700514 }
John Reckf7483e32014-04-11 08:54:47 -0700515 if (properties().hasTransformMatrix()) {
516 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700517 renderer.translate(properties().getTranslationX(), properties().getTranslationY());
John Reck113e0822014-03-18 09:22:59 -0700518 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700519 renderer.concatMatrix(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700520 }
521 }
Chris Craik856f0cc2015-04-21 15:13:29 -0700522 const bool isLayer = properties().effectiveLayerType() != LayerType::None;
Chris Craika753f4c2014-07-24 12:39:17 -0700523 int clipFlags = properties().getClippingFlags();
John Reckd0a0b2a2014-03-20 16:28:56 -0700524 if (properties().getAlpha() < 1) {
John Reck25fbb3f2014-06-12 13:46:45 -0700525 if (isLayer) {
Chris Craika753f4c2014-07-24 12:39:17 -0700526 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
John Reck113e0822014-03-18 09:22:59 -0700527 }
Chris Craik4e9d9b22015-06-12 11:07:23 -0700528 if (CC_LIKELY(isLayer || !properties().getHasOverlappingRendering())) {
529 // simply scale rendering content's alpha
530 renderer.scaleAlpha(properties().getAlpha());
531 } else {
532 // savelayer needed to create an offscreen buffer
533 Rect layerBounds(0, 0, getWidth(), getHeight());
534 if (clipFlags) {
535 properties().getClippingRectForFlags(clipFlags, &layerBounds);
536 clipFlags = 0; // all clipping done by savelayer
537 }
538 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
539 layerBounds.left, layerBounds.top,
540 layerBounds.right, layerBounds.bottom,
541 (int) (properties().getAlpha() * 255),
542 SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kClipToLayer_SaveFlag);
543 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
544 }
Chris Craik1a0808e2015-05-13 16:33:04 -0700545
546 if (CC_UNLIKELY(ATRACE_ENABLED() && properties().promotedToLayer())) {
Chris Craik4e9d9b22015-06-12 11:07:23 -0700547 // pretend alpha always causes savelayer to warn about
548 // performance problem affecting old versions
Chris Craik1a0808e2015-05-13 16:33:04 -0700549 ATRACE_FORMAT("%s alpha caused saveLayer %dx%d", getName(),
550 static_cast<int>(getWidth()),
551 static_cast<int>(getHeight()));
552 }
John Reck113e0822014-03-18 09:22:59 -0700553 }
Chris Craika753f4c2014-07-24 12:39:17 -0700554 if (clipFlags) {
555 Rect clipRect;
556 properties().getClippingRectForFlags(clipFlags, &clipRect);
Chris Craik8c271ca2014-03-25 10:33:01 -0700557 ClipRectOp* op = new (handler.allocator()) ClipRectOp(
Chris Craika753f4c2014-07-24 12:39:17 -0700558 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
559 SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700560 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700561 }
Chris Craik8c271ca2014-03-25 10:33:01 -0700562
Chris Craike83cbd42014-09-03 17:52:24 -0700563 // TODO: support nesting round rect clips
Chris Craikaf4d04c2014-07-29 12:50:14 -0700564 if (mProperties.getRevealClip().willClip()) {
565 Rect bounds;
566 mProperties.getRevealClip().getBounds(&bounds);
567 renderer.setClippingRoundRect(handler.allocator(), bounds, mProperties.getRevealClip().getRadius());
568 } else if (mProperties.getOutline().willClip()) {
569 renderer.setClippingOutline(handler.allocator(), &(mProperties.getOutline()));
John Reck113e0822014-03-18 09:22:59 -0700570 }
571}
572
573/**
574 * Apply property-based transformations to input matrix
575 *
576 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
577 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
578 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700579void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700580 if (properties().getLeft() != 0 || properties().getTop() != 0) {
581 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700582 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700583 if (properties().getStaticMatrix()) {
584 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700585 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700586 } else if (properties().getAnimationMatrix()) {
587 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700588 matrix.multiply(anim);
589 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700590
Chris Craikcc39e162014-04-25 18:34:11 -0700591 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700592 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700593 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700594 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700595 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700596 } else {
597 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700598 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700599 } else {
600 mat4 true3dMat;
601 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700602 properties().getPivotX() + properties().getTranslationX(),
603 properties().getPivotY() + properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700604 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700605 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
606 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
607 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
608 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
609 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700610
611 matrix.multiply(true3dMat);
612 }
613 }
614 }
615}
616
617/**
618 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
619 *
620 * This should be called before a call to defer() or drawDisplayList()
621 *
622 * Each DisplayList that serves as a 3d root builds its list of composited children,
623 * which are flagged to not draw in the standard draw loop.
624 */
625void RenderNode::computeOrdering() {
Chris Craikb565df12015-10-05 13:00:52 -0700626#if !HWUI_NEW_OPS
John Reck113e0822014-03-18 09:22:59 -0700627 ATRACE_CALL();
628 mProjectedNodes.clear();
629
630 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
631 // transform properties are applied correctly to top level children
Chris Craikd41c4d82015-01-05 15:51:13 -0800632 if (mDisplayListData == nullptr) return;
John Reck087bc0c2014-04-04 16:20:08 -0700633 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700634 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700635 childOp->renderNode->computeOrderingImpl(childOp, &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700636 }
Chris Craikb565df12015-10-05 13:00:52 -0700637#endif
John Reck113e0822014-03-18 09:22:59 -0700638}
639
640void RenderNode::computeOrderingImpl(
Chris Craika7090e02014-06-20 16:01:00 -0700641 DrawRenderNodeOp* opState,
John Reck272a6852015-07-29 16:48:58 -0700642 std::vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700643 const mat4* transformFromProjectionSurface) {
Chris Craikb565df12015-10-05 13:00:52 -0700644#if !HWUI_NEW_OPS
John Reck113e0822014-03-18 09:22:59 -0700645 mProjectedNodes.clear();
Chris Craikd41c4d82015-01-05 15:51:13 -0800646 if (mDisplayListData == nullptr || mDisplayListData->isEmpty()) return;
John Reck113e0822014-03-18 09:22:59 -0700647
648 // TODO: should avoid this calculation in most cases
649 // TODO: just calculate single matrix, down to all leaf composited elements
650 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
651 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
652
John Reckd0a0b2a2014-03-20 16:28:56 -0700653 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700654 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
655 opState->mSkipInOrderDraw = true;
Chris Craik7c85c542015-08-19 15:10:24 -0700656 opState->mTransformFromCompositingAncestor = localTransformFromProjectionSurface;
John Reck272a6852015-07-29 16:48:58 -0700657 compositedChildrenOfProjectionSurface->push_back(opState);
John Reck113e0822014-03-18 09:22:59 -0700658 } else {
659 // standard in order draw
660 opState->mSkipInOrderDraw = false;
661 }
662
John Reck087bc0c2014-04-04 16:20:08 -0700663 if (mDisplayListData->children().size() > 0) {
John Reck113e0822014-03-18 09:22:59 -0700664 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
665 bool haveAppliedPropertiesToProjection = false;
John Reck087bc0c2014-04-04 16:20:08 -0700666 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700667 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700668 RenderNode* child = childOp->renderNode;
John Reck113e0822014-03-18 09:22:59 -0700669
John Reck272a6852015-07-29 16:48:58 -0700670 std::vector<DrawRenderNodeOp*>* projectionChildren = nullptr;
Chris Craikd41c4d82015-01-05 15:51:13 -0800671 const mat4* projectionTransform = nullptr;
John Reckd0a0b2a2014-03-20 16:28:56 -0700672 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
Chris Craikbf72eb82015-06-08 11:30:44 -0700673 // if receiving projections, collect projecting descendant
John Reck113e0822014-03-18 09:22:59 -0700674
Chris Craikbf72eb82015-06-08 11:30:44 -0700675 // Note that if a direct descendant is projecting backwards, we pass its
676 // grandparent projection collection, since it shouldn't project onto its
John Reck113e0822014-03-18 09:22:59 -0700677 // parent, where it will already be drawing.
678 projectionChildren = &mProjectedNodes;
679 projectionTransform = &mat4::identity();
680 } else {
681 if (!haveAppliedPropertiesToProjection) {
682 applyViewPropertyTransforms(localTransformFromProjectionSurface);
683 haveAppliedPropertiesToProjection = true;
684 }
685 projectionChildren = compositedChildrenOfProjectionSurface;
686 projectionTransform = &localTransformFromProjectionSurface;
687 }
Derek Sollenbergerf2932592015-08-13 14:59:33 -0400688 child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700689 }
690 }
Chris Craikb565df12015-10-05 13:00:52 -0700691#endif
John Reck113e0822014-03-18 09:22:59 -0700692}
693
694class DeferOperationHandler {
695public:
696 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
697 : mDeferStruct(deferStruct), mLevel(level) {}
698 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
699 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
700 }
701 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
Andreas Gampe64bb4132014-11-22 00:35:09 +0000702 inline void startMark(const char* name) {} // do nothing
Chris Craikb265e2c2014-03-27 15:50:09 -0700703 inline void endMark() {}
704 inline int level() { return mLevel; }
705 inline int replayFlags() { return mDeferStruct.mReplayFlags; }
Chris Craik74669862014-08-07 17:27:30 -0700706 inline SkPath* allocPathForFrame() { return mDeferStruct.allocPathForFrame(); }
John Reck113e0822014-03-18 09:22:59 -0700707
708private:
709 DeferStateStruct& mDeferStruct;
710 const int mLevel;
711};
712
Chris Craik80d49022014-06-20 15:03:43 -0700713void RenderNode::defer(DeferStateStruct& deferStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700714 DeferOperationHandler handler(deferStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700715 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700716}
717
718class ReplayOperationHandler {
719public:
720 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
721 : mReplayStruct(replayStruct), mLevel(level) {}
722 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
723#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
Chris Craik3f0854292014-04-15 16:18:08 -0700724 mReplayStruct.mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700725#endif
726 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
727 }
728 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700729 inline void startMark(const char* name) {
730 mReplayStruct.mRenderer.startMark(name);
731 }
732 inline void endMark() {
733 mReplayStruct.mRenderer.endMark();
Chris Craikb265e2c2014-03-27 15:50:09 -0700734 }
735 inline int level() { return mLevel; }
736 inline int replayFlags() { return mReplayStruct.mReplayFlags; }
Chris Craik74669862014-08-07 17:27:30 -0700737 inline SkPath* allocPathForFrame() { return mReplayStruct.allocPathForFrame(); }
John Reck113e0822014-03-18 09:22:59 -0700738
739private:
740 ReplayStateStruct& mReplayStruct;
741 const int mLevel;
742};
743
Chris Craik80d49022014-06-20 15:03:43 -0700744void RenderNode::replay(ReplayStateStruct& replayStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700745 ReplayOperationHandler handler(replayStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700746 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700747}
748
Chris Craik8afd0f22014-08-21 17:41:57 -0700749void RenderNode::buildZSortedChildList(const DisplayListData::Chunk& chunk,
John Reck272a6852015-07-29 16:48:58 -0700750 std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes) {
Chris Craikb565df12015-10-05 13:00:52 -0700751#if !HWUI_NEW_OPS
Chris Craik8afd0f22014-08-21 17:41:57 -0700752 if (chunk.beginChildIndex == chunk.endChildIndex) return;
John Reck113e0822014-03-18 09:22:59 -0700753
Chris Craik8afd0f22014-08-21 17:41:57 -0700754 for (unsigned int i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700755 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700756 RenderNode* child = childOp->renderNode;
Chris Craikcc39e162014-04-25 18:34:11 -0700757 float childZ = child->properties().getZ();
John Reck113e0822014-03-18 09:22:59 -0700758
Chris Craik8afd0f22014-08-21 17:41:57 -0700759 if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
John Reck272a6852015-07-29 16:48:58 -0700760 zTranslatedNodes.push_back(ZDrawRenderNodeOpPair(childZ, childOp));
John Reck113e0822014-03-18 09:22:59 -0700761 childOp->mSkipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700762 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700763 // regular, in order drawing DisplayList
764 childOp->mSkipInOrderDraw = false;
765 }
766 }
767
Chris Craik8afd0f22014-08-21 17:41:57 -0700768 // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
John Reck113e0822014-03-18 09:22:59 -0700769 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
Chris Craikb565df12015-10-05 13:00:52 -0700770#endif
John Reck113e0822014-03-18 09:22:59 -0700771}
772
Chris Craikb265e2c2014-03-27 15:50:09 -0700773template <class T>
774void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
Chris Craik77b5cad2014-07-30 18:23:07 -0700775 if (properties().getAlpha() <= 0.0f
776 || properties().getOutline().getAlpha() <= 0.0f
Teng-Hui Zhu8d0ec382015-10-01 16:49:16 -0700777 || !properties().getOutline().getPath()
778 || properties().getScaleX() == 0
779 || properties().getScaleY() == 0) {
Chris Craik77b5cad2014-07-30 18:23:07 -0700780 // no shadow to draw
781 return;
782 }
Chris Craikb265e2c2014-03-27 15:50:09 -0700783
784 mat4 shadowMatrixXY(transformFromParent);
785 applyViewPropertyTransforms(shadowMatrixXY);
786
787 // Z matrix needs actual 3d transformation, so mapped z values will be correct
788 mat4 shadowMatrixZ(transformFromParent);
789 applyViewPropertyTransforms(shadowMatrixZ, true);
790
Chris Craik74669862014-08-07 17:27:30 -0700791 const SkPath* casterOutlinePath = properties().getOutline().getPath();
Chris Craikaf4d04c2014-07-29 12:50:14 -0700792 const SkPath* revealClipPath = properties().getRevealClip().getPath();
Chris Craik61317322014-05-21 13:03:52 -0700793 if (revealClipPath && revealClipPath->isEmpty()) return;
794
Chris Craik77b5cad2014-07-30 18:23:07 -0700795 float casterAlpha = properties().getAlpha() * properties().getOutline().getAlpha();
Chris Craik74669862014-08-07 17:27:30 -0700796
Chris Craik74669862014-08-07 17:27:30 -0700797
Chris Craikfaa79ff2014-12-01 13:44:21 -0800798 // holds temporary SkPath to store the result of intersections
Chris Craikd41c4d82015-01-05 15:51:13 -0800799 SkPath* frameAllocatedPath = nullptr;
Chris Craikfaa79ff2014-12-01 13:44:21 -0800800 const SkPath* outlinePath = casterOutlinePath;
801
802 // intersect the outline with the reveal clip, if present
803 if (revealClipPath) {
804 frameAllocatedPath = handler.allocPathForFrame();
805
Tom Hudson02a26302015-06-24 11:32:42 -0400806 Op(*outlinePath, *revealClipPath, kIntersect_SkPathOp, frameAllocatedPath);
Chris Craikfaa79ff2014-12-01 13:44:21 -0800807 outlinePath = frameAllocatedPath;
808 }
809
810 // intersect the outline with the clipBounds, if present
811 if (properties().getClippingFlags() & CLIP_TO_CLIP_BOUNDS) {
812 if (!frameAllocatedPath) {
813 frameAllocatedPath = handler.allocPathForFrame();
814 }
815
816 Rect clipBounds;
817 properties().getClippingRectForFlags(CLIP_TO_CLIP_BOUNDS, &clipBounds);
818 SkPath clipBoundsPath;
819 clipBoundsPath.addRect(clipBounds.left, clipBounds.top,
820 clipBounds.right, clipBounds.bottom);
821
Tom Hudson02a26302015-06-24 11:32:42 -0400822 Op(*outlinePath, clipBoundsPath, kIntersect_SkPathOp, frameAllocatedPath);
Chris Craik74669862014-08-07 17:27:30 -0700823 outlinePath = frameAllocatedPath;
824 }
825
Chris Craikb265e2c2014-03-27 15:50:09 -0700826 DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp(
Chris Craik74669862014-08-07 17:27:30 -0700827 shadowMatrixXY, shadowMatrixZ, casterAlpha, outlinePath);
Chris Craikb265e2c2014-03-27 15:50:09 -0700828 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
829}
830
John Reck113e0822014-03-18 09:22:59 -0700831#define SHADOW_DELTA 0.1f
832
833template <class T>
Chris Craikc3e75f92014-08-27 15:34:52 -0700834void RenderNode::issueOperationsOf3dChildren(ChildrenSelectMode mode,
John Reck272a6852015-07-29 16:48:58 -0700835 const Matrix4& initialTransform, const std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craikc3e75f92014-08-27 15:34:52 -0700836 OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700837 const int size = zTranslatedNodes.size();
838 if (size == 0
Chris Craikb9ce116d2015-08-20 15:14:06 -0700839 || (mode == ChildrenSelectMode::NegativeZChildren && zTranslatedNodes[0].key > 0.0f)
840 || (mode == ChildrenSelectMode::PositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
John Reck113e0822014-03-18 09:22:59 -0700841 // no 3d children to draw
842 return;
843 }
844
Chris Craikc3e75f92014-08-27 15:34:52 -0700845 // Apply the base transform of the parent of the 3d children. This isolates
846 // 3d children of the current chunk from transformations made in previous chunks.
847 int rootRestoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
Chris Craik6daa13c2015-08-19 13:32:12 -0700848 renderer.setGlobalMatrix(initialTransform);
Chris Craikc3e75f92014-08-27 15:34:52 -0700849
John Reck113e0822014-03-18 09:22:59 -0700850 /**
851 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
852 * with very similar Z heights to draw together.
853 *
854 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
855 * underneath both, and neither's shadow is drawn on top of the other.
856 */
857 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
858 size_t drawIndex, shadowIndex, endIndex;
Chris Craikb9ce116d2015-08-20 15:14:06 -0700859 if (mode == ChildrenSelectMode::NegativeZChildren) {
John Reck113e0822014-03-18 09:22:59 -0700860 drawIndex = 0;
861 endIndex = nonNegativeIndex;
862 shadowIndex = endIndex; // draw no shadows
863 } else {
864 drawIndex = nonNegativeIndex;
865 endIndex = size;
866 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
867 }
Chris Craik3f0854292014-04-15 16:18:08 -0700868
869 DISPLAY_LIST_LOGD("%*s%d %s 3d children:", (handler.level() + 1) * 2, "",
870 endIndex - drawIndex, mode == kNegativeZChildren ? "negative" : "positive");
871
John Reck113e0822014-03-18 09:22:59 -0700872 float lastCasterZ = 0.0f;
873 while (shadowIndex < endIndex || drawIndex < endIndex) {
874 if (shadowIndex < endIndex) {
Chris Craika7090e02014-06-20 16:01:00 -0700875 DrawRenderNodeOp* casterOp = zTranslatedNodes[shadowIndex].value;
Chris Craikb565df12015-10-05 13:00:52 -0700876 RenderNode* caster = casterOp->renderNode;
John Reck113e0822014-03-18 09:22:59 -0700877 const float casterZ = zTranslatedNodes[shadowIndex].key;
878 // attempt to render the shadow if the caster about to be drawn is its caster,
879 // OR if its caster's Z value is similar to the previous potential caster
880 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700881 caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
John Reck113e0822014-03-18 09:22:59 -0700882
883 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
884 shadowIndex++;
885 continue;
886 }
887 }
888
889 // only the actual child DL draw needs to be in save/restore,
890 // since it modifies the renderer's matrix
891 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
892
Chris Craika7090e02014-06-20 16:01:00 -0700893 DrawRenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
John Reck113e0822014-03-18 09:22:59 -0700894
895 renderer.concatMatrix(childOp->mTransformFromParent);
896 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700897 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700898 childOp->mSkipInOrderDraw = true;
899
900 renderer.restoreToCount(restoreTo);
901 drawIndex++;
902 }
Chris Craikc3e75f92014-08-27 15:34:52 -0700903 renderer.restoreToCount(rootRestoreTo);
John Reck113e0822014-03-18 09:22:59 -0700904}
905
906template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700907void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
Chris Craik3f0854292014-04-15 16:18:08 -0700908 DISPLAY_LIST_LOGD("%*s%d projected children:", (handler.level() + 1) * 2, "", mProjectedNodes.size());
909 const SkPath* projectionReceiverOutline = properties().getOutline().getPath();
Chris Craik3f0854292014-04-15 16:18:08 -0700910 int restoreTo = renderer.getSaveCount();
911
Chris Craikb3cca872014-08-08 18:42:51 -0700912 LinearAllocator& alloc = handler.allocator();
913 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
914 PROPERTY_SAVECOUNT, properties().getClipToBounds());
915
916 // Transform renderer to match background we're projecting onto
917 // (by offsetting canvas by translationX/Y of background rendernode, since only those are set)
918 const DisplayListOp* op =
919 (mDisplayListData->displayListOps[mDisplayListData->projectionReceiveIndex]);
920 const DrawRenderNodeOp* backgroundOp = reinterpret_cast<const DrawRenderNodeOp*>(op);
Chris Craikb565df12015-10-05 13:00:52 -0700921 const RenderProperties& backgroundProps = backgroundOp->renderNode->properties();
Chris Craikb3cca872014-08-08 18:42:51 -0700922 renderer.translate(backgroundProps.getTranslationX(), backgroundProps.getTranslationY());
923
Chris Craikfca52b752015-04-28 11:45:59 -0700924 // If the projection reciever has an outline, we mask projected content to it
925 // (which we know, apriori, are all tessellated paths)
926 renderer.setProjectionPathMask(alloc, projectionReceiverOutline);
Chris Craik3f0854292014-04-15 16:18:08 -0700927
928 // draw projected nodes
John Reck113e0822014-03-18 09:22:59 -0700929 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700930 DrawRenderNodeOp* childOp = mProjectedNodes[i];
John Reck113e0822014-03-18 09:22:59 -0700931
932 // matrix save, concat, and restore can be done safely without allocating operations
933 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
934 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
935 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700936 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700937 childOp->mSkipInOrderDraw = true;
938 renderer.restoreToCount(restoreTo);
939 }
Chris Craik3f0854292014-04-15 16:18:08 -0700940
Chris Craikfca52b752015-04-28 11:45:59 -0700941 handler(new (alloc) RestoreToCountOp(restoreTo),
942 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700943}
944
945/**
946 * This function serves both defer and replay modes, and will organize the displayList's component
947 * operations for a single frame:
948 *
949 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
950 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
951 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
952 * defer vs replay logic, per operation
953 */
954template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700955void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
Chris Craik06451282014-07-21 10:25:54 -0700956 if (mDisplayListData->isEmpty()) {
Chris Craikbf72eb82015-06-08 11:30:44 -0700957 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", handler.level() * 2, "",
958 this, getName());
Chris Craik06451282014-07-21 10:25:54 -0700959 return;
960 }
961
Chris Craik51d6a3d2014-12-22 17:16:56 -0800962 const bool drawLayer = (mLayer && (&renderer != mLayer->renderer.get()));
John Reck25fbb3f2014-06-12 13:46:45 -0700963 // If we are updating the contents of mLayer, we don't want to apply any of
964 // the RenderNode's properties to this issueOperations pass. Those will all
965 // be applied when the layer is drawn, aka when this is true.
966 const bool useViewProperties = (!mLayer || drawLayer);
Chris Craik06451282014-07-21 10:25:54 -0700967 if (useViewProperties) {
968 const Outline& outline = properties().getOutline();
Teng-Hui Zhu8d0ec382015-10-01 16:49:16 -0700969 if (properties().getAlpha() <= 0
970 || (outline.getShouldClip() && outline.isEmpty())
971 || properties().getScaleX() == 0
972 || properties().getScaleY() == 0) {
Chris Craikbf72eb82015-06-08 11:30:44 -0700973 DISPLAY_LIST_LOGD("%*sRejected display list (%p, %s)", handler.level() * 2, "",
974 this, getName());
Chris Craik06451282014-07-21 10:25:54 -0700975 return;
976 }
John Reck113e0822014-03-18 09:22:59 -0700977 }
978
Chris Craik3f0854292014-04-15 16:18:08 -0700979 handler.startMark(getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700980
John Reck113e0822014-03-18 09:22:59 -0700981#if DEBUG_DISPLAY_LIST
Chris Craik3f0854292014-04-15 16:18:08 -0700982 const Rect& clipRect = renderer.getLocalClipBounds();
983 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), localClipBounds: %.0f, %.0f, %.0f, %.0f",
Chris Craik03188872015-02-02 18:39:33 -0800984 handler.level() * 2, "", this, getName(),
Chris Craik3f0854292014-04-15 16:18:08 -0700985 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
John Reck113e0822014-03-18 09:22:59 -0700986#endif
987
988 LinearAllocator& alloc = handler.allocator();
989 int restoreTo = renderer.getSaveCount();
990 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
John Reckd0a0b2a2014-03-20 16:28:56 -0700991 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700992
Chris Craikbf72eb82015-06-08 11:30:44 -0700993 DISPLAY_LIST_LOGD("%*sSave %d %d", (handler.level() + 1) * 2, "",
John Reck113e0822014-03-18 09:22:59 -0700994 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
995
John Reck25fbb3f2014-06-12 13:46:45 -0700996 if (useViewProperties) {
997 setViewProperties<T>(renderer, handler);
998 }
John Reck113e0822014-03-18 09:22:59 -0700999
Chris Craik8c271ca2014-03-25 10:33:01 -07001000 bool quickRejected = properties().getClipToBounds()
1001 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -07001002 if (!quickRejected) {
Chris Craikc3e75f92014-08-27 15:34:52 -07001003 Matrix4 initialTransform(*(renderer.currentTransform()));
Tom Hudsonac7b6d32015-06-30 11:26:13 -04001004 renderer.setBaseTransform(initialTransform);
Chris Craikc3e75f92014-08-27 15:34:52 -07001005
John Reck25fbb3f2014-06-12 13:46:45 -07001006 if (drawLayer) {
Chris Craik3aadd602015-08-20 12:41:40 -07001007 handler(new (alloc) DrawLayerOp(mLayer),
John Reck25fbb3f2014-06-12 13:46:45 -07001008 renderer.getSaveCount() - 1, properties().getClipToBounds());
1009 } else {
Chris Craikc166b6c2014-09-05 19:55:30 -07001010 const int saveCountOffset = renderer.getSaveCount() - 1;
1011 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
Chris Craik8afd0f22014-08-21 17:41:57 -07001012 for (size_t chunkIndex = 0; chunkIndex < mDisplayListData->getChunks().size(); chunkIndex++) {
1013 const DisplayListData::Chunk& chunk = mDisplayListData->getChunks()[chunkIndex];
John Reck113e0822014-03-18 09:22:59 -07001014
John Reck272a6852015-07-29 16:48:58 -07001015 std::vector<ZDrawRenderNodeOpPair> zTranslatedNodes;
Chris Craik8afd0f22014-08-21 17:41:57 -07001016 buildZSortedChildList(chunk, zTranslatedNodes);
1017
Chris Craikb9ce116d2015-08-20 15:14:06 -07001018 issueOperationsOf3dChildren(ChildrenSelectMode::NegativeZChildren,
Chris Craikc3e75f92014-08-27 15:34:52 -07001019 initialTransform, zTranslatedNodes, renderer, handler);
1020
Chris Craik8afd0f22014-08-21 17:41:57 -07001021
Andreas Gampeedaecc12014-11-10 20:54:07 -08001022 for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
Chris Craik8afd0f22014-08-21 17:41:57 -07001023 DisplayListOp *op = mDisplayListData->displayListOps[opIndex];
Chris Craik80d49022014-06-20 15:03:43 -07001024#if DEBUG_DISPLAY_LIST
Chris Craik03188872015-02-02 18:39:33 -08001025 op->output(handler.level() + 1);
Chris Craik80d49022014-06-20 15:03:43 -07001026#endif
Chris Craik8afd0f22014-08-21 17:41:57 -07001027 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -07001028
John Reck272a6852015-07-29 16:48:58 -07001029 if (CC_UNLIKELY(!mProjectedNodes.empty() && projectionReceiveIndex >= 0 &&
Andreas Gampeedaecc12014-11-10 20:54:07 -08001030 opIndex == static_cast<size_t>(projectionReceiveIndex))) {
Chris Craik8afd0f22014-08-21 17:41:57 -07001031 issueOperationsOfProjectedChildren(renderer, handler);
1032 }
John Reck25fbb3f2014-06-12 13:46:45 -07001033 }
John Reck113e0822014-03-18 09:22:59 -07001034
Chris Craikb9ce116d2015-08-20 15:14:06 -07001035 issueOperationsOf3dChildren(ChildrenSelectMode::PositiveZChildren,
Chris Craikc3e75f92014-08-27 15:34:52 -07001036 initialTransform, zTranslatedNodes, renderer, handler);
Chris Craik8afd0f22014-08-21 17:41:57 -07001037 }
John Reck25fbb3f2014-06-12 13:46:45 -07001038 }
John Reck113e0822014-03-18 09:22:59 -07001039 }
1040
Chris Craikbf72eb82015-06-08 11:30:44 -07001041 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (handler.level() + 1) * 2, "", restoreTo);
John Reck113e0822014-03-18 09:22:59 -07001042 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -07001043 PROPERTY_SAVECOUNT, properties().getClipToBounds());
Chris Craikb265e2c2014-03-27 15:50:09 -07001044
Chris Craikbf72eb82015-06-08 11:30:44 -07001045 DISPLAY_LIST_LOGD("%*sDone (%p, %s)", handler.level() * 2, "", this, getName());
Chris Craikb265e2c2014-03-27 15:50:09 -07001046 handler.endMark();
John Reck113e0822014-03-18 09:22:59 -07001047}
1048
1049} /* namespace uirenderer */
1050} /* namespace android */