blob: 28586fa602a7b69b7759679b674ee2c490a47cd6 [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) {
Chris Craik10ed6922015-10-15 10:55:15 -070052 for (auto&& child : mDisplayListData->children()) {
53 child->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);
Chris Craik10ed6922015-10-15 10:55:15 -0700100
John Reckdc0349b2014-08-06 15:28:07 -0700101 if (mDisplayListData) {
Chris Craik10ed6922015-10-15 10:55:15 -0700102#if HWUI_NEW_OPS
103 LOG_ALWAYS_FATAL("op dumping unsupported");
104#else
Chris Craik8afd0f22014-08-21 17:41:57 -0700105 // TODO: consider printing the chunk boundaries here
Chris Craik10ed6922015-10-15 10:55:15 -0700106 for (auto&& op : mDisplayListData->getOps()) {
107 op->output(level, DisplayListOp::kOpLogFlag_Recurse);
John Reckdc0349b2014-08-06 15:28:07 -0700108 }
Chris Craik10ed6922015-10-15 10:55:15 -0700109#endif
John Reck113e0822014-03-18 09:22:59 -0700110 }
111
Chris Craik3f0854292014-04-15 16:18:08 -0700112 ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, getName());
John Reck113e0822014-03-18 09:22:59 -0700113}
114
John Recke248bd12015-08-05 13:53:53 -0700115void RenderNode::copyTo(proto::RenderNode *pnode) {
116 pnode->set_id(static_cast<uint64_t>(
117 reinterpret_cast<uintptr_t>(this)));
118 pnode->set_name(mName.string(), mName.length());
119
120 proto::RenderProperties* pprops = pnode->mutable_properties();
121 pprops->set_left(properties().getLeft());
122 pprops->set_top(properties().getTop());
123 pprops->set_right(properties().getRight());
124 pprops->set_bottom(properties().getBottom());
125 pprops->set_clip_flags(properties().getClippingFlags());
126 pprops->set_alpha(properties().getAlpha());
127 pprops->set_translation_x(properties().getTranslationX());
128 pprops->set_translation_y(properties().getTranslationY());
129 pprops->set_translation_z(properties().getTranslationZ());
130 pprops->set_elevation(properties().getElevation());
131 pprops->set_rotation(properties().getRotation());
132 pprops->set_rotation_x(properties().getRotationX());
133 pprops->set_rotation_y(properties().getRotationY());
134 pprops->set_scale_x(properties().getScaleX());
135 pprops->set_scale_y(properties().getScaleY());
136 pprops->set_pivot_x(properties().getPivotX());
137 pprops->set_pivot_y(properties().getPivotY());
138 pprops->set_has_overlapping_rendering(properties().getHasOverlappingRendering());
139 pprops->set_pivot_explicitly_set(properties().isPivotExplicitlySet());
140 pprops->set_project_backwards(properties().getProjectBackwards());
141 pprops->set_projection_receiver(properties().isProjectionReceiver());
142 set(pprops->mutable_clip_bounds(), properties().getClipBounds());
143
144 const Outline& outline = properties().getOutline();
145 if (outline.getType() != Outline::Type::None) {
146 proto::Outline* poutline = pprops->mutable_outline();
147 poutline->clear_path();
148 if (outline.getType() == Outline::Type::Empty) {
149 poutline->set_type(proto::Outline_Type_Empty);
150 } else if (outline.getType() == Outline::Type::ConvexPath) {
151 poutline->set_type(proto::Outline_Type_ConvexPath);
152 if (const SkPath* path = outline.getPath()) {
153 set(poutline->mutable_path(), *path);
154 }
155 } else if (outline.getType() == Outline::Type::RoundRect) {
156 poutline->set_type(proto::Outline_Type_RoundRect);
157 } else {
158 ALOGW("Uknown outline type! %d", static_cast<int>(outline.getType()));
159 poutline->set_type(proto::Outline_Type_None);
160 }
161 poutline->set_should_clip(outline.getShouldClip());
162 poutline->set_alpha(outline.getAlpha());
163 poutline->set_radius(outline.getRadius());
164 set(poutline->mutable_bounds(), outline.getBounds());
165 } else {
166 pprops->clear_outline();
167 }
168
169 const RevealClip& revealClip = properties().getRevealClip();
170 if (revealClip.willClip()) {
171 proto::RevealClip* prevealClip = pprops->mutable_reveal_clip();
172 prevealClip->set_x(revealClip.getX());
173 prevealClip->set_y(revealClip.getY());
174 prevealClip->set_radius(revealClip.getRadius());
175 } else {
176 pprops->clear_reveal_clip();
177 }
178
179 pnode->clear_children();
180 if (mDisplayListData) {
181 for (auto&& child : mDisplayListData->children()) {
Chris Craikb565df12015-10-05 13:00:52 -0700182 child->renderNode->copyTo(pnode->add_children());
John Recke248bd12015-08-05 13:53:53 -0700183 }
184 }
185}
186
John Reckfe5e7b72014-05-23 17:42:28 -0700187int RenderNode::getDebugSize() {
188 int size = sizeof(RenderNode);
189 if (mStagingDisplayListData) {
Chris Craik8afd0f22014-08-21 17:41:57 -0700190 size += mStagingDisplayListData->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700191 }
192 if (mDisplayListData && mDisplayListData != mStagingDisplayListData) {
Chris Craik8afd0f22014-08-21 17:41:57 -0700193 size += mDisplayListData->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700194 }
195 return size;
196}
197
John Reckf4198b72014-04-09 17:00:04 -0700198void RenderNode::prepareTree(TreeInfo& info) {
199 ATRACE_CALL();
Chris Craik69e5adf2014-08-14 13:34:01 -0700200 LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
John Reckf4198b72014-04-09 17:00:04 -0700201
Chris Craika766cb22015-06-08 16:49:43 -0700202 // Functors don't correctly handle stencil usage of overdraw debugging - shove 'em in a layer.
203 bool functorsNeedLayer = Properties::debugOverdraw;
204
205 prepareTreeImpl(info, functorsNeedLayer);
John Reckf4198b72014-04-09 17:00:04 -0700206}
207
John Reck68bfe0a2014-06-24 15:34:58 -0700208void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
209 mAnimatorManager.addAnimator(animator);
210}
211
John Recke4267ea2014-06-03 15:53:15 -0700212void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700213 if (isRenderable()) {
John Reck293e8682014-06-17 10:34:02 -0700214 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700215 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
216 } else {
217 // Hope this is big enough?
218 // TODO: Get this from the display list ops or something
John Reckc1288232015-08-12 13:39:11 -0700219 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
John Recka447d292014-06-11 18:39:44 -0700220 }
John Recke4267ea2014-06-03 15:53:15 -0700221 }
222}
223
John Recka7c2ea22014-08-08 13:21:00 -0700224void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700225 LayerType layerType = properties().effectiveLayerType();
Chris Craik182952f2015-03-09 14:17:29 -0700226 if (CC_UNLIKELY(layerType == LayerType::RenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700227 // Damage applied so far needs to affect our parent, but does not require
228 // the layer to be updated. So we pop/push here to clear out the current
229 // damage and get a clean state for display list or children updates to
230 // affect, which will require the layer to be updated
231 info.damageAccumulator->popTransform();
232 info.damageAccumulator->pushTransform(this);
233 if (dirtyMask & DISPLAY_LIST) {
234 damageSelf(info);
235 }
John Reck25fbb3f2014-06-12 13:46:45 -0700236 }
237}
238
239void RenderNode::pushLayerUpdate(TreeInfo& info) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700240 LayerType layerType = properties().effectiveLayerType();
John Reck25fbb3f2014-06-12 13:46:45 -0700241 // If we are not a layer OR we cannot be rendered (eg, view was detached)
242 // we need to destroy any Layers we may have had previously
Chris Craik182952f2015-03-09 14:17:29 -0700243 if (CC_LIKELY(layerType != LayerType::RenderLayer) || CC_UNLIKELY(!isRenderable())) {
John Reck25fbb3f2014-06-12 13:46:45 -0700244 if (CC_UNLIKELY(mLayer)) {
245 LayerRenderer::destroyLayer(mLayer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800246 mLayer = nullptr;
John Reck25fbb3f2014-06-12 13:46:45 -0700247 }
248 return;
249 }
250
Chris Craik69e5adf2014-08-14 13:34:01 -0700251 bool transformUpdateNeeded = false;
John Reck25fbb3f2014-06-12 13:46:45 -0700252 if (!mLayer) {
John Reck3b202512014-06-23 13:13:08 -0700253 mLayer = LayerRenderer::createRenderLayer(info.renderState, getWidth(), getHeight());
John Reck25fbb3f2014-06-12 13:46:45 -0700254 applyLayerPropertiesToLayer(info);
255 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700256 transformUpdateNeeded = true;
John Reck25fbb3f2014-06-12 13:46:45 -0700257 } else if (mLayer->layer.getWidth() != getWidth() || mLayer->layer.getHeight() != getHeight()) {
John Reckc25e5062014-06-18 14:21:29 -0700258 if (!LayerRenderer::resizeLayer(mLayer, getWidth(), getHeight())) {
259 LayerRenderer::destroyLayer(mLayer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800260 mLayer = nullptr;
John Reckc25e5062014-06-18 14:21:29 -0700261 }
John Reck25fbb3f2014-06-12 13:46:45 -0700262 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700263 transformUpdateNeeded = true;
264 }
265
John Reck25fbb3f2014-06-12 13:46:45 -0700266 SkRect dirty;
267 info.damageAccumulator->peekAtDirty(&dirty);
John Reck25fbb3f2014-06-12 13:46:45 -0700268
John Reckc25e5062014-06-18 14:21:29 -0700269 if (!mLayer) {
John Reck0e89e2b2014-10-31 14:49:06 -0700270 Caches::getInstance().dumpMemoryUsage();
John Reckc25e5062014-06-18 14:21:29 -0700271 if (info.errorHandler) {
272 std::string msg = "Unable to create layer for ";
273 msg += getName();
274 info.errorHandler->onError(msg);
275 }
276 return;
277 }
278
Chris Craikc71bfca2014-08-21 10:18:58 -0700279 if (transformUpdateNeeded) {
280 // update the transform in window of the layer to reset its origin wrt light source position
281 Matrix4 windowTransform;
282 info.damageAccumulator->computeCurrentTransform(&windowTransform);
283 mLayer->setWindowTransform(windowTransform);
284 }
John Reckc79eabc2014-08-05 11:03:42 -0700285
286 if (dirty.intersect(0, 0, getWidth(), getHeight())) {
Mike Reed71487eb2014-11-19 16:13:20 -0500287 dirty.roundOut(&dirty);
John Reck25fbb3f2014-06-12 13:46:45 -0700288 mLayer->updateDeferred(this, dirty.fLeft, dirty.fTop, dirty.fRight, dirty.fBottom);
289 }
290 // This is not inside the above if because we may have called
291 // updateDeferred on a previous prepare pass that didn't have a renderer
292 if (info.renderer && mLayer->deferredUpdateScheduled) {
293 info.renderer->pushLayerUpdate(mLayer);
294 }
John Reck998a6d82014-08-28 15:35:53 -0700295
John Reck00e79c92015-07-21 10:23:59 -0700296 if (info.canvasContext) {
297 // There might be prefetched layers that need to be accounted for.
298 // That might be us, so tell CanvasContext that this layer is in the
299 // tree and should not be destroyed.
John Reck998a6d82014-08-28 15:35:53 -0700300 info.canvasContext->markLayerInUse(this);
301 }
John Reck25fbb3f2014-06-12 13:46:45 -0700302}
303
Chris Craika766cb22015-06-08 16:49:43 -0700304/**
305 * Traverse down the the draw tree to prepare for a frame.
306 *
307 * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven
308 *
309 * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the
310 * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer.
311 */
312void RenderNode::prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer) {
John Recka447d292014-06-11 18:39:44 -0700313 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700314
John Reckdcba6722014-07-08 13:59:49 -0700315 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700316 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700317 }
John Reck9eb9f6f2014-08-21 11:23:05 -0700318 uint32_t animatorDirtyMask = 0;
319 if (CC_LIKELY(info.runAnimations)) {
320 animatorDirtyMask = mAnimatorManager.animate(info);
321 }
Chris Craika766cb22015-06-08 16:49:43 -0700322
John Reck3f725f02015-06-16 10:29:31 -0700323 bool willHaveFunctor = false;
324 if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayListData) {
325 willHaveFunctor = !mStagingDisplayListData->functors.isEmpty();
326 } else if (mDisplayListData) {
327 willHaveFunctor = !mDisplayListData->functors.isEmpty();
328 }
Chris Craika766cb22015-06-08 16:49:43 -0700329 bool childFunctorsNeedLayer = mProperties.prepareForFunctorPresence(
330 willHaveFunctor, functorsNeedLayer);
331
John Recka7c2ea22014-08-08 13:21:00 -0700332 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700333 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700334 pushStagingDisplayListChanges(info);
335 }
Chris Craika766cb22015-06-08 16:49:43 -0700336 prepareSubTree(info, childFunctorsNeedLayer, mDisplayListData);
John Reck25fbb3f2014-06-12 13:46:45 -0700337 pushLayerUpdate(info);
338
John Recka447d292014-06-11 18:39:44 -0700339 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700340}
341
Chris Craikb565df12015-10-05 13:00:52 -0700342void RenderNode::syncProperties() {
343 mProperties = mStagingProperties;
344}
345
John Reck25fbb3f2014-06-12 13:46:45 -0700346void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reckff941dc2014-05-14 16:34:14 -0700347 // Push the animators first so that setupStartValueIfNecessary() is called
348 // before properties() is trampled by stagingProperties(), as they are
349 // required by some animators.
John Reck9eb9f6f2014-08-21 11:23:05 -0700350 if (CC_LIKELY(info.runAnimations)) {
John Reck119907c2014-08-14 09:02:01 -0700351 mAnimatorManager.pushStaging();
John Reck9eb9f6f2014-08-21 11:23:05 -0700352 }
John Reckff941dc2014-05-14 16:34:14 -0700353 if (mDirtyPropertyFields) {
354 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700355 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700356 info.damageAccumulator->popTransform();
Chris Craikb565df12015-10-05 13:00:52 -0700357 syncProperties();
John Reck25fbb3f2014-06-12 13:46:45 -0700358 applyLayerPropertiesToLayer(info);
John Recke4267ea2014-06-03 15:53:15 -0700359 // We could try to be clever and only re-damage if the matrix changed.
360 // However, we don't need to worry about that. The cost of over-damaging
361 // here is only going to be a single additional map rect of this node
362 // plus a rect join(). The parent's transform (and up) will only be
363 // performed once.
John Recka447d292014-06-11 18:39:44 -0700364 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700365 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700366 }
John Reck25fbb3f2014-06-12 13:46:45 -0700367}
368
Andreas Gampe64bb4132014-11-22 00:35:09 +0000369void RenderNode::applyLayerPropertiesToLayer(TreeInfo& info) {
John Reck25fbb3f2014-06-12 13:46:45 -0700370 if (CC_LIKELY(!mLayer)) return;
371
372 const LayerProperties& props = properties().layerProperties();
373 mLayer->setAlpha(props.alpha(), props.xferMode());
374 mLayer->setColorFilter(props.colorFilter());
375 mLayer->setBlend(props.needsBlending());
376}
377
Chris Craikb565df12015-10-05 13:00:52 -0700378void RenderNode::syncDisplayList() {
379 // Make sure we inc first so that we don't fluctuate between 0 and 1,
380 // which would thrash the layer cache
381 if (mStagingDisplayListData) {
382 for (auto&& child : mStagingDisplayListData->children()) {
383 child->renderNode->incParentRefCount();
384 }
385 }
386 deleteDisplayListData();
387 mDisplayListData = mStagingDisplayListData;
388 mStagingDisplayListData = nullptr;
389 if (mDisplayListData) {
390 for (size_t i = 0; i < mDisplayListData->functors.size(); i++) {
391 (*mDisplayListData->functors[i])(DrawGlInfo::kModeSync, nullptr);
392 }
393 }
394}
395
John Reck25fbb3f2014-06-12 13:46:45 -0700396void RenderNode::pushStagingDisplayListChanges(TreeInfo& info) {
John Reck8de65a82014-04-09 15:23:38 -0700397 if (mNeedsDisplayListDataSync) {
398 mNeedsDisplayListDataSync = false;
John Reck5c9d7172014-10-22 11:32:27 -0700399 // Damage with the old display list first then the new one to catch any
400 // changes in isRenderable or, in the future, bounds
401 damageSelf(info);
Chris Craikb565df12015-10-05 13:00:52 -0700402 syncDisplayList();
John Recke4267ea2014-06-03 15:53:15 -0700403 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700404 }
John Reck8de65a82014-04-09 15:23:38 -0700405}
406
John Reckdcba6722014-07-08 13:59:49 -0700407void RenderNode::deleteDisplayListData() {
408 if (mDisplayListData) {
Chris Craikb565df12015-10-05 13:00:52 -0700409 for (auto&& child : mDisplayListData->children()) {
410 child->renderNode->decParentRefCount();
John Reckdcba6722014-07-08 13:59:49 -0700411 }
412 }
413 delete mDisplayListData;
Chris Craikd41c4d82015-01-05 15:51:13 -0800414 mDisplayListData = nullptr;
John Reckdcba6722014-07-08 13:59:49 -0700415}
416
Chris Craika766cb22015-06-08 16:49:43 -0700417void RenderNode::prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayListData* subtree) {
John Reck8de65a82014-04-09 15:23:38 -0700418 if (subtree) {
John Reck860d1552014-04-11 19:15:05 -0700419 TextureCache& cache = Caches::getInstance().textureCache;
John Reck09d5cdd2014-07-24 10:36:08 -0700420 info.out.hasFunctors |= subtree->functors.size();
John Reck860d1552014-04-11 19:15:05 -0700421 for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) {
John Reck00e79c92015-07-21 10:23:59 -0700422 info.prepareTextures = cache.prefetchAndMarkInUse(
423 info.canvasContext, subtree->bitmapResources[i]);
John Reckf4198b72014-04-09 17:00:04 -0700424 }
Chris Craikb565df12015-10-05 13:00:52 -0700425 for (auto&& op : subtree->children()) {
426 RenderNode* childNode = op->renderNode;
427#if HWUI_NEW_OPS
428 info.damageAccumulator->pushTransform(&op->localMatrix);
429 bool childFunctorsNeedLayer = functorsNeedLayer; // TODO! || op->mRecordedWithPotentialStencilClip;
430#else
John Recka447d292014-06-11 18:39:44 -0700431 info.damageAccumulator->pushTransform(&op->mTransformFromParent);
Chris Craika766cb22015-06-08 16:49:43 -0700432 bool childFunctorsNeedLayer = functorsNeedLayer
433 // Recorded with non-rect clip, or canvas-rotated by parent
434 || op->mRecordedWithPotentialStencilClip;
Chris Craikb565df12015-10-05 13:00:52 -0700435#endif
Chris Craika766cb22015-06-08 16:49:43 -0700436 childNode->prepareTreeImpl(info, childFunctorsNeedLayer);
John Recka447d292014-06-11 18:39:44 -0700437 info.damageAccumulator->popTransform();
John Reck5bf11bb2014-03-25 10:22:09 -0700438 }
John Reck113e0822014-03-18 09:22:59 -0700439 }
440}
441
John Reckdcba6722014-07-08 13:59:49 -0700442void RenderNode::destroyHardwareResources() {
443 if (mLayer) {
444 LayerRenderer::destroyLayer(mLayer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800445 mLayer = nullptr;
John Reckdcba6722014-07-08 13:59:49 -0700446 }
447 if (mDisplayListData) {
Chris Craikb565df12015-10-05 13:00:52 -0700448 for (auto&& child : mDisplayListData->children()) {
449 child->renderNode->destroyHardwareResources();
John Reckdcba6722014-07-08 13:59:49 -0700450 }
451 if (mNeedsDisplayListDataSync) {
452 // Next prepare tree we are going to push a new display list, so we can
453 // drop our current one now
454 deleteDisplayListData();
455 }
456 }
457}
458
459void RenderNode::decParentRefCount() {
460 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
461 mParentCount--;
462 if (!mParentCount) {
463 // If a child of ours is being attached to our parent then this will incorrectly
464 // destroy its hardware resources. However, this situation is highly unlikely
465 // and the failure is "just" that the layer is re-created, so this should
466 // be safe enough
467 destroyHardwareResources();
468 }
469}
470
Chris Craikb565df12015-10-05 13:00:52 -0700471bool RenderNode::applyViewProperties(CanvasState& canvasState) const {
472 const Outline& outline = properties().getOutline();
473 if (properties().getAlpha() <= 0
474 || (outline.getShouldClip() && outline.isEmpty())
475 || properties().getScaleX() == 0
476 || properties().getScaleY() == 0) {
477 return false; // rejected
478 }
479
480 if (properties().getLeft() != 0 || properties().getTop() != 0) {
481 canvasState.translate(properties().getLeft(), properties().getTop());
482 }
483 if (properties().getStaticMatrix()) {
484 canvasState.concatMatrix(*properties().getStaticMatrix());
485 } else if (properties().getAnimationMatrix()) {
486 canvasState.concatMatrix(*properties().getAnimationMatrix());
487 }
488 if (properties().hasTransformMatrix()) {
489 if (properties().isTransformTranslateOnly()) {
490 canvasState.translate(properties().getTranslationX(), properties().getTranslationY());
491 } else {
492 canvasState.concatMatrix(*properties().getTransformMatrix());
493 }
494 }
495 return !canvasState.quickRejectConservative(
496 0, 0, properties().getWidth(), properties().getHeight());
497}
498
John Reck113e0822014-03-18 09:22:59 -0700499/*
500 * For property operations, we pass a savecount of 0, since the operations aren't part of the
501 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
John Reckd0a0b2a2014-03-20 16:28:56 -0700502 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
John Reck113e0822014-03-18 09:22:59 -0700503 */
504#define PROPERTY_SAVECOUNT 0
505
506template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700507void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700508#if DEBUG_DISPLAY_LIST
Chris Craikb265e2c2014-03-27 15:50:09 -0700509 properties().debugOutputProperties(handler.level() + 1);
John Reck113e0822014-03-18 09:22:59 -0700510#endif
John Reckd0a0b2a2014-03-20 16:28:56 -0700511 if (properties().getLeft() != 0 || properties().getTop() != 0) {
512 renderer.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700513 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700514 if (properties().getStaticMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500515 renderer.concatMatrix(*properties().getStaticMatrix());
John Reckd0a0b2a2014-03-20 16:28:56 -0700516 } else if (properties().getAnimationMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500517 renderer.concatMatrix(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700518 }
John Reckf7483e32014-04-11 08:54:47 -0700519 if (properties().hasTransformMatrix()) {
520 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700521 renderer.translate(properties().getTranslationX(), properties().getTranslationY());
John Reck113e0822014-03-18 09:22:59 -0700522 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700523 renderer.concatMatrix(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700524 }
525 }
Chris Craik856f0cc2015-04-21 15:13:29 -0700526 const bool isLayer = properties().effectiveLayerType() != LayerType::None;
Chris Craika753f4c2014-07-24 12:39:17 -0700527 int clipFlags = properties().getClippingFlags();
John Reckd0a0b2a2014-03-20 16:28:56 -0700528 if (properties().getAlpha() < 1) {
John Reck25fbb3f2014-06-12 13:46:45 -0700529 if (isLayer) {
Chris Craika753f4c2014-07-24 12:39:17 -0700530 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
John Reck113e0822014-03-18 09:22:59 -0700531 }
Chris Craik4e9d9b22015-06-12 11:07:23 -0700532 if (CC_LIKELY(isLayer || !properties().getHasOverlappingRendering())) {
533 // simply scale rendering content's alpha
534 renderer.scaleAlpha(properties().getAlpha());
535 } else {
536 // savelayer needed to create an offscreen buffer
537 Rect layerBounds(0, 0, getWidth(), getHeight());
538 if (clipFlags) {
539 properties().getClippingRectForFlags(clipFlags, &layerBounds);
540 clipFlags = 0; // all clipping done by savelayer
541 }
542 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
543 layerBounds.left, layerBounds.top,
544 layerBounds.right, layerBounds.bottom,
545 (int) (properties().getAlpha() * 255),
546 SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kClipToLayer_SaveFlag);
547 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
548 }
Chris Craik1a0808e2015-05-13 16:33:04 -0700549
550 if (CC_UNLIKELY(ATRACE_ENABLED() && properties().promotedToLayer())) {
Chris Craik4e9d9b22015-06-12 11:07:23 -0700551 // pretend alpha always causes savelayer to warn about
552 // performance problem affecting old versions
Chris Craik1a0808e2015-05-13 16:33:04 -0700553 ATRACE_FORMAT("%s alpha caused saveLayer %dx%d", getName(),
554 static_cast<int>(getWidth()),
555 static_cast<int>(getHeight()));
556 }
John Reck113e0822014-03-18 09:22:59 -0700557 }
Chris Craika753f4c2014-07-24 12:39:17 -0700558 if (clipFlags) {
559 Rect clipRect;
560 properties().getClippingRectForFlags(clipFlags, &clipRect);
Chris Craik8c271ca2014-03-25 10:33:01 -0700561 ClipRectOp* op = new (handler.allocator()) ClipRectOp(
Chris Craika753f4c2014-07-24 12:39:17 -0700562 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
563 SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700564 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700565 }
Chris Craik8c271ca2014-03-25 10:33:01 -0700566
Chris Craike83cbd42014-09-03 17:52:24 -0700567 // TODO: support nesting round rect clips
Chris Craikaf4d04c2014-07-29 12:50:14 -0700568 if (mProperties.getRevealClip().willClip()) {
569 Rect bounds;
570 mProperties.getRevealClip().getBounds(&bounds);
571 renderer.setClippingRoundRect(handler.allocator(), bounds, mProperties.getRevealClip().getRadius());
572 } else if (mProperties.getOutline().willClip()) {
573 renderer.setClippingOutline(handler.allocator(), &(mProperties.getOutline()));
John Reck113e0822014-03-18 09:22:59 -0700574 }
575}
576
577/**
578 * Apply property-based transformations to input matrix
579 *
580 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
581 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
582 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700583void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700584 if (properties().getLeft() != 0 || properties().getTop() != 0) {
585 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700586 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700587 if (properties().getStaticMatrix()) {
588 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700589 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700590 } else if (properties().getAnimationMatrix()) {
591 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700592 matrix.multiply(anim);
593 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700594
Chris Craikcc39e162014-04-25 18:34:11 -0700595 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700596 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700597 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700598 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700599 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700600 } else {
601 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700602 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700603 } else {
604 mat4 true3dMat;
605 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700606 properties().getPivotX() + properties().getTranslationX(),
607 properties().getPivotY() + properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700608 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700609 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
610 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
611 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
612 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
613 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700614
615 matrix.multiply(true3dMat);
616 }
617 }
618 }
619}
620
621/**
622 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
623 *
624 * This should be called before a call to defer() or drawDisplayList()
625 *
626 * Each DisplayList that serves as a 3d root builds its list of composited children,
627 * which are flagged to not draw in the standard draw loop.
628 */
629void RenderNode::computeOrdering() {
Chris Craikb565df12015-10-05 13:00:52 -0700630#if !HWUI_NEW_OPS
John Reck113e0822014-03-18 09:22:59 -0700631 ATRACE_CALL();
632 mProjectedNodes.clear();
633
634 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
635 // transform properties are applied correctly to top level children
Chris Craikd41c4d82015-01-05 15:51:13 -0800636 if (mDisplayListData == nullptr) return;
John Reck087bc0c2014-04-04 16:20:08 -0700637 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700638 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700639 childOp->renderNode->computeOrderingImpl(childOp, &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700640 }
Chris Craikb565df12015-10-05 13:00:52 -0700641#endif
John Reck113e0822014-03-18 09:22:59 -0700642}
643
644void RenderNode::computeOrderingImpl(
Chris Craika7090e02014-06-20 16:01:00 -0700645 DrawRenderNodeOp* opState,
John Reck272a6852015-07-29 16:48:58 -0700646 std::vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700647 const mat4* transformFromProjectionSurface) {
Chris Craikb565df12015-10-05 13:00:52 -0700648#if !HWUI_NEW_OPS
John Reck113e0822014-03-18 09:22:59 -0700649 mProjectedNodes.clear();
Chris Craikd41c4d82015-01-05 15:51:13 -0800650 if (mDisplayListData == nullptr || mDisplayListData->isEmpty()) return;
John Reck113e0822014-03-18 09:22:59 -0700651
652 // TODO: should avoid this calculation in most cases
653 // TODO: just calculate single matrix, down to all leaf composited elements
654 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
655 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
656
John Reckd0a0b2a2014-03-20 16:28:56 -0700657 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700658 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
659 opState->mSkipInOrderDraw = true;
Chris Craik7c85c542015-08-19 15:10:24 -0700660 opState->mTransformFromCompositingAncestor = localTransformFromProjectionSurface;
John Reck272a6852015-07-29 16:48:58 -0700661 compositedChildrenOfProjectionSurface->push_back(opState);
John Reck113e0822014-03-18 09:22:59 -0700662 } else {
663 // standard in order draw
664 opState->mSkipInOrderDraw = false;
665 }
666
John Reck087bc0c2014-04-04 16:20:08 -0700667 if (mDisplayListData->children().size() > 0) {
John Reck113e0822014-03-18 09:22:59 -0700668 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
669 bool haveAppliedPropertiesToProjection = false;
John Reck087bc0c2014-04-04 16:20:08 -0700670 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700671 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700672 RenderNode* child = childOp->renderNode;
John Reck113e0822014-03-18 09:22:59 -0700673
John Reck272a6852015-07-29 16:48:58 -0700674 std::vector<DrawRenderNodeOp*>* projectionChildren = nullptr;
Chris Craikd41c4d82015-01-05 15:51:13 -0800675 const mat4* projectionTransform = nullptr;
John Reckd0a0b2a2014-03-20 16:28:56 -0700676 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
Chris Craikbf72eb82015-06-08 11:30:44 -0700677 // if receiving projections, collect projecting descendant
John Reck113e0822014-03-18 09:22:59 -0700678
Chris Craikbf72eb82015-06-08 11:30:44 -0700679 // Note that if a direct descendant is projecting backwards, we pass its
680 // grandparent projection collection, since it shouldn't project onto its
John Reck113e0822014-03-18 09:22:59 -0700681 // parent, where it will already be drawing.
682 projectionChildren = &mProjectedNodes;
683 projectionTransform = &mat4::identity();
684 } else {
685 if (!haveAppliedPropertiesToProjection) {
686 applyViewPropertyTransforms(localTransformFromProjectionSurface);
687 haveAppliedPropertiesToProjection = true;
688 }
689 projectionChildren = compositedChildrenOfProjectionSurface;
690 projectionTransform = &localTransformFromProjectionSurface;
691 }
Derek Sollenbergerf2932592015-08-13 14:59:33 -0400692 child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700693 }
694 }
Chris Craikb565df12015-10-05 13:00:52 -0700695#endif
John Reck113e0822014-03-18 09:22:59 -0700696}
697
698class DeferOperationHandler {
699public:
700 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
701 : mDeferStruct(deferStruct), mLevel(level) {}
702 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
703 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
704 }
705 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
Andreas Gampe64bb4132014-11-22 00:35:09 +0000706 inline void startMark(const char* name) {} // do nothing
Chris Craikb265e2c2014-03-27 15:50:09 -0700707 inline void endMark() {}
708 inline int level() { return mLevel; }
709 inline int replayFlags() { return mDeferStruct.mReplayFlags; }
Chris Craik74669862014-08-07 17:27:30 -0700710 inline SkPath* allocPathForFrame() { return mDeferStruct.allocPathForFrame(); }
John Reck113e0822014-03-18 09:22:59 -0700711
712private:
713 DeferStateStruct& mDeferStruct;
714 const int mLevel;
715};
716
Chris Craik80d49022014-06-20 15:03:43 -0700717void RenderNode::defer(DeferStateStruct& deferStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700718 DeferOperationHandler handler(deferStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700719 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700720}
721
722class ReplayOperationHandler {
723public:
724 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
725 : mReplayStruct(replayStruct), mLevel(level) {}
726 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
727#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
Chris Craik3f0854292014-04-15 16:18:08 -0700728 mReplayStruct.mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700729#endif
730 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
731 }
732 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700733 inline void startMark(const char* name) {
734 mReplayStruct.mRenderer.startMark(name);
735 }
736 inline void endMark() {
737 mReplayStruct.mRenderer.endMark();
Chris Craikb265e2c2014-03-27 15:50:09 -0700738 }
739 inline int level() { return mLevel; }
740 inline int replayFlags() { return mReplayStruct.mReplayFlags; }
Chris Craik74669862014-08-07 17:27:30 -0700741 inline SkPath* allocPathForFrame() { return mReplayStruct.allocPathForFrame(); }
John Reck113e0822014-03-18 09:22:59 -0700742
743private:
744 ReplayStateStruct& mReplayStruct;
745 const int mLevel;
746};
747
Chris Craik80d49022014-06-20 15:03:43 -0700748void RenderNode::replay(ReplayStateStruct& replayStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700749 ReplayOperationHandler handler(replayStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700750 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700751}
752
Chris Craik8afd0f22014-08-21 17:41:57 -0700753void RenderNode::buildZSortedChildList(const DisplayListData::Chunk& chunk,
John Reck272a6852015-07-29 16:48:58 -0700754 std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes) {
Chris Craikb565df12015-10-05 13:00:52 -0700755#if !HWUI_NEW_OPS
Chris Craik8afd0f22014-08-21 17:41:57 -0700756 if (chunk.beginChildIndex == chunk.endChildIndex) return;
John Reck113e0822014-03-18 09:22:59 -0700757
Chris Craik8afd0f22014-08-21 17:41:57 -0700758 for (unsigned int i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700759 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
Chris Craikb565df12015-10-05 13:00:52 -0700760 RenderNode* child = childOp->renderNode;
Chris Craikcc39e162014-04-25 18:34:11 -0700761 float childZ = child->properties().getZ();
John Reck113e0822014-03-18 09:22:59 -0700762
Chris Craik8afd0f22014-08-21 17:41:57 -0700763 if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
John Reck272a6852015-07-29 16:48:58 -0700764 zTranslatedNodes.push_back(ZDrawRenderNodeOpPair(childZ, childOp));
John Reck113e0822014-03-18 09:22:59 -0700765 childOp->mSkipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700766 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700767 // regular, in order drawing DisplayList
768 childOp->mSkipInOrderDraw = false;
769 }
770 }
771
Chris Craik8afd0f22014-08-21 17:41:57 -0700772 // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
John Reck113e0822014-03-18 09:22:59 -0700773 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
Chris Craikb565df12015-10-05 13:00:52 -0700774#endif
John Reck113e0822014-03-18 09:22:59 -0700775}
776
Chris Craikb265e2c2014-03-27 15:50:09 -0700777template <class T>
778void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
Chris Craik77b5cad2014-07-30 18:23:07 -0700779 if (properties().getAlpha() <= 0.0f
780 || properties().getOutline().getAlpha() <= 0.0f
Teng-Hui Zhu8d0ec382015-10-01 16:49:16 -0700781 || !properties().getOutline().getPath()
782 || properties().getScaleX() == 0
783 || properties().getScaleY() == 0) {
Chris Craik77b5cad2014-07-30 18:23:07 -0700784 // no shadow to draw
785 return;
786 }
Chris Craikb265e2c2014-03-27 15:50:09 -0700787
788 mat4 shadowMatrixXY(transformFromParent);
789 applyViewPropertyTransforms(shadowMatrixXY);
790
791 // Z matrix needs actual 3d transformation, so mapped z values will be correct
792 mat4 shadowMatrixZ(transformFromParent);
793 applyViewPropertyTransforms(shadowMatrixZ, true);
794
Chris Craik74669862014-08-07 17:27:30 -0700795 const SkPath* casterOutlinePath = properties().getOutline().getPath();
Chris Craikaf4d04c2014-07-29 12:50:14 -0700796 const SkPath* revealClipPath = properties().getRevealClip().getPath();
Chris Craik61317322014-05-21 13:03:52 -0700797 if (revealClipPath && revealClipPath->isEmpty()) return;
798
Chris Craik77b5cad2014-07-30 18:23:07 -0700799 float casterAlpha = properties().getAlpha() * properties().getOutline().getAlpha();
Chris Craik74669862014-08-07 17:27:30 -0700800
Chris Craik74669862014-08-07 17:27:30 -0700801
Chris Craikfaa79ff2014-12-01 13:44:21 -0800802 // holds temporary SkPath to store the result of intersections
Chris Craikd41c4d82015-01-05 15:51:13 -0800803 SkPath* frameAllocatedPath = nullptr;
Chris Craikfaa79ff2014-12-01 13:44:21 -0800804 const SkPath* outlinePath = casterOutlinePath;
805
806 // intersect the outline with the reveal clip, if present
807 if (revealClipPath) {
808 frameAllocatedPath = handler.allocPathForFrame();
809
Tom Hudson02a26302015-06-24 11:32:42 -0400810 Op(*outlinePath, *revealClipPath, kIntersect_SkPathOp, frameAllocatedPath);
Chris Craikfaa79ff2014-12-01 13:44:21 -0800811 outlinePath = frameAllocatedPath;
812 }
813
814 // intersect the outline with the clipBounds, if present
815 if (properties().getClippingFlags() & CLIP_TO_CLIP_BOUNDS) {
816 if (!frameAllocatedPath) {
817 frameAllocatedPath = handler.allocPathForFrame();
818 }
819
820 Rect clipBounds;
821 properties().getClippingRectForFlags(CLIP_TO_CLIP_BOUNDS, &clipBounds);
822 SkPath clipBoundsPath;
823 clipBoundsPath.addRect(clipBounds.left, clipBounds.top,
824 clipBounds.right, clipBounds.bottom);
825
Tom Hudson02a26302015-06-24 11:32:42 -0400826 Op(*outlinePath, clipBoundsPath, kIntersect_SkPathOp, frameAllocatedPath);
Chris Craik74669862014-08-07 17:27:30 -0700827 outlinePath = frameAllocatedPath;
828 }
829
Chris Craikb265e2c2014-03-27 15:50:09 -0700830 DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp(
Chris Craik74669862014-08-07 17:27:30 -0700831 shadowMatrixXY, shadowMatrixZ, casterAlpha, outlinePath);
Chris Craikb265e2c2014-03-27 15:50:09 -0700832 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
833}
834
John Reck113e0822014-03-18 09:22:59 -0700835#define SHADOW_DELTA 0.1f
836
837template <class T>
Chris Craikc3e75f92014-08-27 15:34:52 -0700838void RenderNode::issueOperationsOf3dChildren(ChildrenSelectMode mode,
John Reck272a6852015-07-29 16:48:58 -0700839 const Matrix4& initialTransform, const std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craikc3e75f92014-08-27 15:34:52 -0700840 OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700841 const int size = zTranslatedNodes.size();
842 if (size == 0
Chris Craikb9ce116d2015-08-20 15:14:06 -0700843 || (mode == ChildrenSelectMode::NegativeZChildren && zTranslatedNodes[0].key > 0.0f)
844 || (mode == ChildrenSelectMode::PositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
John Reck113e0822014-03-18 09:22:59 -0700845 // no 3d children to draw
846 return;
847 }
848
Chris Craikc3e75f92014-08-27 15:34:52 -0700849 // Apply the base transform of the parent of the 3d children. This isolates
850 // 3d children of the current chunk from transformations made in previous chunks.
851 int rootRestoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
Chris Craik6daa13c2015-08-19 13:32:12 -0700852 renderer.setGlobalMatrix(initialTransform);
Chris Craikc3e75f92014-08-27 15:34:52 -0700853
John Reck113e0822014-03-18 09:22:59 -0700854 /**
855 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
856 * with very similar Z heights to draw together.
857 *
858 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
859 * underneath both, and neither's shadow is drawn on top of the other.
860 */
861 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
862 size_t drawIndex, shadowIndex, endIndex;
Chris Craikb9ce116d2015-08-20 15:14:06 -0700863 if (mode == ChildrenSelectMode::NegativeZChildren) {
John Reck113e0822014-03-18 09:22:59 -0700864 drawIndex = 0;
865 endIndex = nonNegativeIndex;
866 shadowIndex = endIndex; // draw no shadows
867 } else {
868 drawIndex = nonNegativeIndex;
869 endIndex = size;
870 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
871 }
Chris Craik3f0854292014-04-15 16:18:08 -0700872
873 DISPLAY_LIST_LOGD("%*s%d %s 3d children:", (handler.level() + 1) * 2, "",
874 endIndex - drawIndex, mode == kNegativeZChildren ? "negative" : "positive");
875
John Reck113e0822014-03-18 09:22:59 -0700876 float lastCasterZ = 0.0f;
877 while (shadowIndex < endIndex || drawIndex < endIndex) {
878 if (shadowIndex < endIndex) {
Chris Craika7090e02014-06-20 16:01:00 -0700879 DrawRenderNodeOp* casterOp = zTranslatedNodes[shadowIndex].value;
Chris Craikb565df12015-10-05 13:00:52 -0700880 RenderNode* caster = casterOp->renderNode;
John Reck113e0822014-03-18 09:22:59 -0700881 const float casterZ = zTranslatedNodes[shadowIndex].key;
882 // attempt to render the shadow if the caster about to be drawn is its caster,
883 // OR if its caster's Z value is similar to the previous potential caster
884 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700885 caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
John Reck113e0822014-03-18 09:22:59 -0700886
887 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
888 shadowIndex++;
889 continue;
890 }
891 }
892
893 // only the actual child DL draw needs to be in save/restore,
894 // since it modifies the renderer's matrix
895 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
896
Chris Craika7090e02014-06-20 16:01:00 -0700897 DrawRenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
John Reck113e0822014-03-18 09:22:59 -0700898
899 renderer.concatMatrix(childOp->mTransformFromParent);
900 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700901 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700902 childOp->mSkipInOrderDraw = true;
903
904 renderer.restoreToCount(restoreTo);
905 drawIndex++;
906 }
Chris Craikc3e75f92014-08-27 15:34:52 -0700907 renderer.restoreToCount(rootRestoreTo);
John Reck113e0822014-03-18 09:22:59 -0700908}
909
910template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700911void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
Chris Craik3f0854292014-04-15 16:18:08 -0700912 DISPLAY_LIST_LOGD("%*s%d projected children:", (handler.level() + 1) * 2, "", mProjectedNodes.size());
913 const SkPath* projectionReceiverOutline = properties().getOutline().getPath();
Chris Craik3f0854292014-04-15 16:18:08 -0700914 int restoreTo = renderer.getSaveCount();
915
Chris Craikb3cca872014-08-08 18:42:51 -0700916 LinearAllocator& alloc = handler.allocator();
917 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
918 PROPERTY_SAVECOUNT, properties().getClipToBounds());
919
920 // Transform renderer to match background we're projecting onto
921 // (by offsetting canvas by translationX/Y of background rendernode, since only those are set)
922 const DisplayListOp* op =
Chris Craik10ed6922015-10-15 10:55:15 -0700923#if HWUI_NEW_OPS
924 nullptr;
925 LOG_ALWAYS_FATAL("unsupported");
926#else
927 (mDisplayListData->getOps()[mDisplayListData->projectionReceiveIndex]);
928#endif
Chris Craikb3cca872014-08-08 18:42:51 -0700929 const DrawRenderNodeOp* backgroundOp = reinterpret_cast<const DrawRenderNodeOp*>(op);
Chris Craikb565df12015-10-05 13:00:52 -0700930 const RenderProperties& backgroundProps = backgroundOp->renderNode->properties();
Chris Craikb3cca872014-08-08 18:42:51 -0700931 renderer.translate(backgroundProps.getTranslationX(), backgroundProps.getTranslationY());
932
Chris Craikfca52b752015-04-28 11:45:59 -0700933 // If the projection reciever has an outline, we mask projected content to it
934 // (which we know, apriori, are all tessellated paths)
935 renderer.setProjectionPathMask(alloc, projectionReceiverOutline);
Chris Craik3f0854292014-04-15 16:18:08 -0700936
937 // draw projected nodes
John Reck113e0822014-03-18 09:22:59 -0700938 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700939 DrawRenderNodeOp* childOp = mProjectedNodes[i];
John Reck113e0822014-03-18 09:22:59 -0700940
941 // matrix save, concat, and restore can be done safely without allocating operations
942 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
943 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
944 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700945 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700946 childOp->mSkipInOrderDraw = true;
947 renderer.restoreToCount(restoreTo);
948 }
Chris Craik3f0854292014-04-15 16:18:08 -0700949
Chris Craikfca52b752015-04-28 11:45:59 -0700950 handler(new (alloc) RestoreToCountOp(restoreTo),
951 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700952}
953
954/**
955 * This function serves both defer and replay modes, and will organize the displayList's component
956 * operations for a single frame:
957 *
958 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
959 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
960 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
961 * defer vs replay logic, per operation
962 */
963template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700964void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
Chris Craik06451282014-07-21 10:25:54 -0700965 if (mDisplayListData->isEmpty()) {
Chris Craikbf72eb82015-06-08 11:30:44 -0700966 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", handler.level() * 2, "",
967 this, getName());
Chris Craik06451282014-07-21 10:25:54 -0700968 return;
969 }
970
Chris Craik51d6a3d2014-12-22 17:16:56 -0800971 const bool drawLayer = (mLayer && (&renderer != mLayer->renderer.get()));
John Reck25fbb3f2014-06-12 13:46:45 -0700972 // If we are updating the contents of mLayer, we don't want to apply any of
973 // the RenderNode's properties to this issueOperations pass. Those will all
974 // be applied when the layer is drawn, aka when this is true.
975 const bool useViewProperties = (!mLayer || drawLayer);
Chris Craik06451282014-07-21 10:25:54 -0700976 if (useViewProperties) {
977 const Outline& outline = properties().getOutline();
Teng-Hui Zhu8d0ec382015-10-01 16:49:16 -0700978 if (properties().getAlpha() <= 0
979 || (outline.getShouldClip() && outline.isEmpty())
980 || properties().getScaleX() == 0
981 || properties().getScaleY() == 0) {
Chris Craikbf72eb82015-06-08 11:30:44 -0700982 DISPLAY_LIST_LOGD("%*sRejected display list (%p, %s)", handler.level() * 2, "",
983 this, getName());
Chris Craik06451282014-07-21 10:25:54 -0700984 return;
985 }
John Reck113e0822014-03-18 09:22:59 -0700986 }
987
Chris Craik3f0854292014-04-15 16:18:08 -0700988 handler.startMark(getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700989
John Reck113e0822014-03-18 09:22:59 -0700990#if DEBUG_DISPLAY_LIST
Chris Craik3f0854292014-04-15 16:18:08 -0700991 const Rect& clipRect = renderer.getLocalClipBounds();
992 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), localClipBounds: %.0f, %.0f, %.0f, %.0f",
Chris Craik03188872015-02-02 18:39:33 -0800993 handler.level() * 2, "", this, getName(),
Chris Craik3f0854292014-04-15 16:18:08 -0700994 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
John Reck113e0822014-03-18 09:22:59 -0700995#endif
996
997 LinearAllocator& alloc = handler.allocator();
998 int restoreTo = renderer.getSaveCount();
999 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
John Reckd0a0b2a2014-03-20 16:28:56 -07001000 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -07001001
Chris Craikbf72eb82015-06-08 11:30:44 -07001002 DISPLAY_LIST_LOGD("%*sSave %d %d", (handler.level() + 1) * 2, "",
John Reck113e0822014-03-18 09:22:59 -07001003 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
1004
John Reck25fbb3f2014-06-12 13:46:45 -07001005 if (useViewProperties) {
1006 setViewProperties<T>(renderer, handler);
1007 }
John Reck113e0822014-03-18 09:22:59 -07001008
Chris Craik10ed6922015-10-15 10:55:15 -07001009#if HWUI_NEW_OPS
1010 LOG_ALWAYS_FATAL("legacy op traversal not supported");
1011#else
Chris Craik8c271ca2014-03-25 10:33:01 -07001012 bool quickRejected = properties().getClipToBounds()
1013 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -07001014 if (!quickRejected) {
Chris Craikc3e75f92014-08-27 15:34:52 -07001015 Matrix4 initialTransform(*(renderer.currentTransform()));
Tom Hudsonac7b6d32015-06-30 11:26:13 -04001016 renderer.setBaseTransform(initialTransform);
Chris Craikc3e75f92014-08-27 15:34:52 -07001017
John Reck25fbb3f2014-06-12 13:46:45 -07001018 if (drawLayer) {
Chris Craik3aadd602015-08-20 12:41:40 -07001019 handler(new (alloc) DrawLayerOp(mLayer),
John Reck25fbb3f2014-06-12 13:46:45 -07001020 renderer.getSaveCount() - 1, properties().getClipToBounds());
1021 } else {
Chris Craikc166b6c2014-09-05 19:55:30 -07001022 const int saveCountOffset = renderer.getSaveCount() - 1;
1023 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
Chris Craik8afd0f22014-08-21 17:41:57 -07001024 for (size_t chunkIndex = 0; chunkIndex < mDisplayListData->getChunks().size(); chunkIndex++) {
1025 const DisplayListData::Chunk& chunk = mDisplayListData->getChunks()[chunkIndex];
John Reck113e0822014-03-18 09:22:59 -07001026
John Reck272a6852015-07-29 16:48:58 -07001027 std::vector<ZDrawRenderNodeOpPair> zTranslatedNodes;
Chris Craik8afd0f22014-08-21 17:41:57 -07001028 buildZSortedChildList(chunk, zTranslatedNodes);
1029
Chris Craikb9ce116d2015-08-20 15:14:06 -07001030 issueOperationsOf3dChildren(ChildrenSelectMode::NegativeZChildren,
Chris Craikc3e75f92014-08-27 15:34:52 -07001031 initialTransform, zTranslatedNodes, renderer, handler);
1032
Andreas Gampeedaecc12014-11-10 20:54:07 -08001033 for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
Chris Craik10ed6922015-10-15 10:55:15 -07001034 DisplayListOp *op = mDisplayListData->getOps()[opIndex];
Chris Craik80d49022014-06-20 15:03:43 -07001035#if DEBUG_DISPLAY_LIST
Chris Craik03188872015-02-02 18:39:33 -08001036 op->output(handler.level() + 1);
Chris Craik80d49022014-06-20 15:03:43 -07001037#endif
Chris Craik8afd0f22014-08-21 17:41:57 -07001038 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -07001039
John Reck272a6852015-07-29 16:48:58 -07001040 if (CC_UNLIKELY(!mProjectedNodes.empty() && projectionReceiveIndex >= 0 &&
Andreas Gampeedaecc12014-11-10 20:54:07 -08001041 opIndex == static_cast<size_t>(projectionReceiveIndex))) {
Chris Craik8afd0f22014-08-21 17:41:57 -07001042 issueOperationsOfProjectedChildren(renderer, handler);
1043 }
John Reck25fbb3f2014-06-12 13:46:45 -07001044 }
John Reck113e0822014-03-18 09:22:59 -07001045
Chris Craikb9ce116d2015-08-20 15:14:06 -07001046 issueOperationsOf3dChildren(ChildrenSelectMode::PositiveZChildren,
Chris Craikc3e75f92014-08-27 15:34:52 -07001047 initialTransform, zTranslatedNodes, renderer, handler);
Chris Craik8afd0f22014-08-21 17:41:57 -07001048 }
John Reck25fbb3f2014-06-12 13:46:45 -07001049 }
John Reck113e0822014-03-18 09:22:59 -07001050 }
Chris Craik10ed6922015-10-15 10:55:15 -07001051#endif
John Reck113e0822014-03-18 09:22:59 -07001052
Chris Craikbf72eb82015-06-08 11:30:44 -07001053 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (handler.level() + 1) * 2, "", restoreTo);
John Reck113e0822014-03-18 09:22:59 -07001054 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -07001055 PROPERTY_SAVECOUNT, properties().getClipToBounds());
Chris Craikb265e2c2014-03-27 15:50:09 -07001056
Chris Craikbf72eb82015-06-08 11:30:44 -07001057 DISPLAY_LIST_LOGD("%*sDone (%p, %s)", handler.level() * 2, "", this, getName());
Chris Craikb265e2c2014-03-27 15:50:09 -07001058 handler.endMark();
John Reck113e0822014-03-18 09:22:59 -07001059}
1060
1061} /* namespace uirenderer */
1062} /* namespace android */