blob: bf1b4d0b0d0e3b5aff7a3dab7c45b43a5e563689 [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"
28#include "DisplayListOp.h"
John Reck25fbb3f2014-06-12 13:46:45 -070029#include "LayerRenderer.h"
30#include "OpenGLRenderer.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040031#include "TreeInfo.h"
Chris Craike0bb87d2014-04-22 17:55:41 -070032#include "utils/MathUtils.h"
Chris Craik70850ea2014-11-18 10:49:23 -080033#include "utils/TraceUtils.h"
John Reck998a6d82014-08-28 15:35:53 -070034#include "renderthread/CanvasContext.h"
John Reck113e0822014-03-18 09:22:59 -070035
John Recke248bd12015-08-05 13:53:53 -070036#include "protos/hwui.pb.h"
37#include "protos/ProtoHelpers.h"
38
John Reck113e0822014-03-18 09:22:59 -070039namespace android {
40namespace uirenderer {
41
John Reck443a7142014-09-04 17:40:05 -070042void RenderNode::debugDumpLayers(const char* prefix) {
43 if (mLayer) {
44 ALOGD("%sNode %p (%s) has layer %p (fbo = %u, wasBuildLayered = %s)",
45 prefix, this, getName(), mLayer, mLayer->getFbo(),
46 mLayer->wasBuildLayered ? "true" : "false");
47 }
48 if (mDisplayListData) {
49 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
50 mDisplayListData->children()[i]->mRenderNode->debugDumpLayers(prefix);
51 }
52 }
53}
54
John Reck8de65a82014-04-09 15:23:38 -070055RenderNode::RenderNode()
John Reckff941dc2014-05-14 16:34:14 -070056 : mDirtyPropertyFields(0)
John Reck8de65a82014-04-09 15:23:38 -070057 , mNeedsDisplayListDataSync(false)
Chris Craikd41c4d82015-01-05 15:51:13 -080058 , mDisplayListData(nullptr)
59 , mStagingDisplayListData(nullptr)
John Reck68bfe0a2014-06-24 15:34:58 -070060 , mAnimatorManager(*this)
Chris Craikd41c4d82015-01-05 15:51:13 -080061 , mLayer(nullptr)
John Reckdcba6722014-07-08 13:59:49 -070062 , mParentCount(0) {
John Reck113e0822014-03-18 09:22:59 -070063}
64
65RenderNode::~RenderNode() {
John Reckdcba6722014-07-08 13:59:49 -070066 deleteDisplayListData();
John Reck8de65a82014-04-09 15:23:38 -070067 delete mStagingDisplayListData;
John Reck0e89e2b2014-10-31 14:49:06 -070068 if (mLayer) {
69 ALOGW("Memory Warning: Layer %p missed its detachment, held on to for far too long!", mLayer);
70 mLayer->postDecStrong();
Chris Craikd41c4d82015-01-05 15:51:13 -080071 mLayer = nullptr;
John Reck0e89e2b2014-10-31 14:49:06 -070072 }
John Reck113e0822014-03-18 09:22:59 -070073}
74
John Reck8de65a82014-04-09 15:23:38 -070075void RenderNode::setStagingDisplayList(DisplayListData* data) {
76 mNeedsDisplayListDataSync = true;
77 delete mStagingDisplayListData;
78 mStagingDisplayListData = data;
John Reck113e0822014-03-18 09:22:59 -070079}
80
81/**
82 * This function is a simplified version of replay(), where we simply retrieve and log the
83 * display list. This function should remain in sync with the replay() function.
84 */
85void RenderNode::output(uint32_t level) {
Chris Craikbf72eb82015-06-08 11:30:44 -070086 ALOGD("%*sStart display list (%p, %s%s%s%s%s%s)", (level - 1) * 2, "", this,
Chris Craikb5a54352014-11-21 14:54:35 -080087 getName(),
Chris Craik43a1d312015-05-27 11:28:14 -070088 (MathUtils::isZero(properties().getAlpha()) ? ", zero alpha" : ""),
Chris Craikb5a54352014-11-21 14:54:35 -080089 (properties().hasShadow() ? ", casting shadow" : ""),
90 (isRenderable() ? "" : ", empty"),
Chris Craikbf72eb82015-06-08 11:30:44 -070091 (properties().getProjectBackwards() ? ", projected" : ""),
Chris Craikd41c4d82015-01-05 15:51:13 -080092 (mLayer != nullptr ? ", on HW Layer" : ""));
John Reck113e0822014-03-18 09:22:59 -070093 ALOGD("%*s%s %d", level * 2, "", "Save",
94 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
95
John Reckd0a0b2a2014-03-20 16:28:56 -070096 properties().debugOutputProperties(level);
John Reck113e0822014-03-18 09:22:59 -070097 int flags = DisplayListOp::kOpLogFlag_Recurse;
John Reckdc0349b2014-08-06 15:28:07 -070098 if (mDisplayListData) {
Chris Craik8afd0f22014-08-21 17:41:57 -070099 // TODO: consider printing the chunk boundaries here
John Reckdc0349b2014-08-06 15:28:07 -0700100 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
101 mDisplayListData->displayListOps[i]->output(level, flags);
102 }
John Reck113e0822014-03-18 09:22:59 -0700103 }
104
Chris Craik3f0854292014-04-15 16:18:08 -0700105 ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, getName());
John Reck113e0822014-03-18 09:22:59 -0700106}
107
John Recke248bd12015-08-05 13:53:53 -0700108void RenderNode::copyTo(proto::RenderNode *pnode) {
109 pnode->set_id(static_cast<uint64_t>(
110 reinterpret_cast<uintptr_t>(this)));
111 pnode->set_name(mName.string(), mName.length());
112
113 proto::RenderProperties* pprops = pnode->mutable_properties();
114 pprops->set_left(properties().getLeft());
115 pprops->set_top(properties().getTop());
116 pprops->set_right(properties().getRight());
117 pprops->set_bottom(properties().getBottom());
118 pprops->set_clip_flags(properties().getClippingFlags());
119 pprops->set_alpha(properties().getAlpha());
120 pprops->set_translation_x(properties().getTranslationX());
121 pprops->set_translation_y(properties().getTranslationY());
122 pprops->set_translation_z(properties().getTranslationZ());
123 pprops->set_elevation(properties().getElevation());
124 pprops->set_rotation(properties().getRotation());
125 pprops->set_rotation_x(properties().getRotationX());
126 pprops->set_rotation_y(properties().getRotationY());
127 pprops->set_scale_x(properties().getScaleX());
128 pprops->set_scale_y(properties().getScaleY());
129 pprops->set_pivot_x(properties().getPivotX());
130 pprops->set_pivot_y(properties().getPivotY());
131 pprops->set_has_overlapping_rendering(properties().getHasOverlappingRendering());
132 pprops->set_pivot_explicitly_set(properties().isPivotExplicitlySet());
133 pprops->set_project_backwards(properties().getProjectBackwards());
134 pprops->set_projection_receiver(properties().isProjectionReceiver());
135 set(pprops->mutable_clip_bounds(), properties().getClipBounds());
136
137 const Outline& outline = properties().getOutline();
138 if (outline.getType() != Outline::Type::None) {
139 proto::Outline* poutline = pprops->mutable_outline();
140 poutline->clear_path();
141 if (outline.getType() == Outline::Type::Empty) {
142 poutline->set_type(proto::Outline_Type_Empty);
143 } else if (outline.getType() == Outline::Type::ConvexPath) {
144 poutline->set_type(proto::Outline_Type_ConvexPath);
145 if (const SkPath* path = outline.getPath()) {
146 set(poutline->mutable_path(), *path);
147 }
148 } else if (outline.getType() == Outline::Type::RoundRect) {
149 poutline->set_type(proto::Outline_Type_RoundRect);
150 } else {
151 ALOGW("Uknown outline type! %d", static_cast<int>(outline.getType()));
152 poutline->set_type(proto::Outline_Type_None);
153 }
154 poutline->set_should_clip(outline.getShouldClip());
155 poutline->set_alpha(outline.getAlpha());
156 poutline->set_radius(outline.getRadius());
157 set(poutline->mutable_bounds(), outline.getBounds());
158 } else {
159 pprops->clear_outline();
160 }
161
162 const RevealClip& revealClip = properties().getRevealClip();
163 if (revealClip.willClip()) {
164 proto::RevealClip* prevealClip = pprops->mutable_reveal_clip();
165 prevealClip->set_x(revealClip.getX());
166 prevealClip->set_y(revealClip.getY());
167 prevealClip->set_radius(revealClip.getRadius());
168 } else {
169 pprops->clear_reveal_clip();
170 }
171
172 pnode->clear_children();
173 if (mDisplayListData) {
174 for (auto&& child : mDisplayListData->children()) {
175 child->mRenderNode->copyTo(pnode->add_children());
176 }
177 }
178}
179
John Reckfe5e7b72014-05-23 17:42:28 -0700180int RenderNode::getDebugSize() {
181 int size = sizeof(RenderNode);
182 if (mStagingDisplayListData) {
Chris Craik8afd0f22014-08-21 17:41:57 -0700183 size += mStagingDisplayListData->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700184 }
185 if (mDisplayListData && mDisplayListData != mStagingDisplayListData) {
Chris Craik8afd0f22014-08-21 17:41:57 -0700186 size += mDisplayListData->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700187 }
188 return size;
189}
190
John Reckf4198b72014-04-09 17:00:04 -0700191void RenderNode::prepareTree(TreeInfo& info) {
192 ATRACE_CALL();
Chris Craik69e5adf2014-08-14 13:34:01 -0700193 LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
John Reckf4198b72014-04-09 17:00:04 -0700194
Chris Craika766cb22015-06-08 16:49:43 -0700195 // Functors don't correctly handle stencil usage of overdraw debugging - shove 'em in a layer.
196 bool functorsNeedLayer = Properties::debugOverdraw;
197
198 prepareTreeImpl(info, functorsNeedLayer);
John Reckf4198b72014-04-09 17:00:04 -0700199}
200
John Reck68bfe0a2014-06-24 15:34:58 -0700201void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
202 mAnimatorManager.addAnimator(animator);
203}
204
John Recke4267ea2014-06-03 15:53:15 -0700205void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700206 if (isRenderable()) {
John Reck293e8682014-06-17 10:34:02 -0700207 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700208 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
209 } else {
210 // Hope this is big enough?
211 // TODO: Get this from the display list ops or something
John Reckc1288232015-08-12 13:39:11 -0700212 info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX);
John Recka447d292014-06-11 18:39:44 -0700213 }
John Recke4267ea2014-06-03 15:53:15 -0700214 }
215}
216
John Recka7c2ea22014-08-08 13:21:00 -0700217void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700218 LayerType layerType = properties().effectiveLayerType();
Chris Craik182952f2015-03-09 14:17:29 -0700219 if (CC_UNLIKELY(layerType == LayerType::RenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700220 // Damage applied so far needs to affect our parent, but does not require
221 // the layer to be updated. So we pop/push here to clear out the current
222 // damage and get a clean state for display list or children updates to
223 // affect, which will require the layer to be updated
224 info.damageAccumulator->popTransform();
225 info.damageAccumulator->pushTransform(this);
226 if (dirtyMask & DISPLAY_LIST) {
227 damageSelf(info);
228 }
John Reck25fbb3f2014-06-12 13:46:45 -0700229 }
230}
231
232void RenderNode::pushLayerUpdate(TreeInfo& info) {
Chris Craik856f0cc2015-04-21 15:13:29 -0700233 LayerType layerType = properties().effectiveLayerType();
John Reck25fbb3f2014-06-12 13:46:45 -0700234 // If we are not a layer OR we cannot be rendered (eg, view was detached)
235 // we need to destroy any Layers we may have had previously
Chris Craik182952f2015-03-09 14:17:29 -0700236 if (CC_LIKELY(layerType != LayerType::RenderLayer) || CC_UNLIKELY(!isRenderable())) {
John Reck25fbb3f2014-06-12 13:46:45 -0700237 if (CC_UNLIKELY(mLayer)) {
238 LayerRenderer::destroyLayer(mLayer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800239 mLayer = nullptr;
John Reck25fbb3f2014-06-12 13:46:45 -0700240 }
241 return;
242 }
243
Chris Craik69e5adf2014-08-14 13:34:01 -0700244 bool transformUpdateNeeded = false;
John Reck25fbb3f2014-06-12 13:46:45 -0700245 if (!mLayer) {
John Reck3b202512014-06-23 13:13:08 -0700246 mLayer = LayerRenderer::createRenderLayer(info.renderState, getWidth(), getHeight());
John Reck25fbb3f2014-06-12 13:46:45 -0700247 applyLayerPropertiesToLayer(info);
248 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700249 transformUpdateNeeded = true;
John Reck25fbb3f2014-06-12 13:46:45 -0700250 } else if (mLayer->layer.getWidth() != getWidth() || mLayer->layer.getHeight() != getHeight()) {
John Reckc25e5062014-06-18 14:21:29 -0700251 if (!LayerRenderer::resizeLayer(mLayer, getWidth(), getHeight())) {
252 LayerRenderer::destroyLayer(mLayer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800253 mLayer = nullptr;
John Reckc25e5062014-06-18 14:21:29 -0700254 }
John Reck25fbb3f2014-06-12 13:46:45 -0700255 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700256 transformUpdateNeeded = true;
257 }
258
John Reck25fbb3f2014-06-12 13:46:45 -0700259 SkRect dirty;
260 info.damageAccumulator->peekAtDirty(&dirty);
John Reck25fbb3f2014-06-12 13:46:45 -0700261
John Reckc25e5062014-06-18 14:21:29 -0700262 if (!mLayer) {
John Reck0e89e2b2014-10-31 14:49:06 -0700263 Caches::getInstance().dumpMemoryUsage();
John Reckc25e5062014-06-18 14:21:29 -0700264 if (info.errorHandler) {
265 std::string msg = "Unable to create layer for ";
266 msg += getName();
267 info.errorHandler->onError(msg);
268 }
269 return;
270 }
271
Chris Craikc71bfca2014-08-21 10:18:58 -0700272 if (transformUpdateNeeded) {
273 // update the transform in window of the layer to reset its origin wrt light source position
274 Matrix4 windowTransform;
275 info.damageAccumulator->computeCurrentTransform(&windowTransform);
276 mLayer->setWindowTransform(windowTransform);
277 }
John Reckc79eabc2014-08-05 11:03:42 -0700278
279 if (dirty.intersect(0, 0, getWidth(), getHeight())) {
Mike Reed71487eb2014-11-19 16:13:20 -0500280 dirty.roundOut(&dirty);
John Reck25fbb3f2014-06-12 13:46:45 -0700281 mLayer->updateDeferred(this, dirty.fLeft, dirty.fTop, dirty.fRight, dirty.fBottom);
282 }
283 // This is not inside the above if because we may have called
284 // updateDeferred on a previous prepare pass that didn't have a renderer
285 if (info.renderer && mLayer->deferredUpdateScheduled) {
286 info.renderer->pushLayerUpdate(mLayer);
287 }
John Reck998a6d82014-08-28 15:35:53 -0700288
John Reck00e79c92015-07-21 10:23:59 -0700289 if (info.canvasContext) {
290 // There might be prefetched layers that need to be accounted for.
291 // That might be us, so tell CanvasContext that this layer is in the
292 // tree and should not be destroyed.
John Reck998a6d82014-08-28 15:35:53 -0700293 info.canvasContext->markLayerInUse(this);
294 }
John Reck25fbb3f2014-06-12 13:46:45 -0700295}
296
Chris Craika766cb22015-06-08 16:49:43 -0700297/**
298 * Traverse down the the draw tree to prepare for a frame.
299 *
300 * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven
301 *
302 * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the
303 * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer.
304 */
305void RenderNode::prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer) {
John Recka447d292014-06-11 18:39:44 -0700306 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700307
John Reckdcba6722014-07-08 13:59:49 -0700308 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700309 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700310 }
John Reck9eb9f6f2014-08-21 11:23:05 -0700311 uint32_t animatorDirtyMask = 0;
312 if (CC_LIKELY(info.runAnimations)) {
313 animatorDirtyMask = mAnimatorManager.animate(info);
314 }
Chris Craika766cb22015-06-08 16:49:43 -0700315
John Reck3f725f02015-06-16 10:29:31 -0700316 bool willHaveFunctor = false;
317 if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayListData) {
318 willHaveFunctor = !mStagingDisplayListData->functors.isEmpty();
319 } else if (mDisplayListData) {
320 willHaveFunctor = !mDisplayListData->functors.isEmpty();
321 }
Chris Craika766cb22015-06-08 16:49:43 -0700322 bool childFunctorsNeedLayer = mProperties.prepareForFunctorPresence(
323 willHaveFunctor, functorsNeedLayer);
324
John Recka7c2ea22014-08-08 13:21:00 -0700325 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700326 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700327 pushStagingDisplayListChanges(info);
328 }
Chris Craika766cb22015-06-08 16:49:43 -0700329 prepareSubTree(info, childFunctorsNeedLayer, mDisplayListData);
John Reck25fbb3f2014-06-12 13:46:45 -0700330 pushLayerUpdate(info);
331
John Recka447d292014-06-11 18:39:44 -0700332 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700333}
334
John Reck25fbb3f2014-06-12 13:46:45 -0700335void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reckff941dc2014-05-14 16:34:14 -0700336 // Push the animators first so that setupStartValueIfNecessary() is called
337 // before properties() is trampled by stagingProperties(), as they are
338 // required by some animators.
John Reck9eb9f6f2014-08-21 11:23:05 -0700339 if (CC_LIKELY(info.runAnimations)) {
John Reck119907c2014-08-14 09:02:01 -0700340 mAnimatorManager.pushStaging();
John Reck9eb9f6f2014-08-21 11:23:05 -0700341 }
John Reckff941dc2014-05-14 16:34:14 -0700342 if (mDirtyPropertyFields) {
343 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700344 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700345 info.damageAccumulator->popTransform();
John Reckff941dc2014-05-14 16:34:14 -0700346 mProperties = mStagingProperties;
John Reck25fbb3f2014-06-12 13:46:45 -0700347 applyLayerPropertiesToLayer(info);
John Recke4267ea2014-06-03 15:53:15 -0700348 // We could try to be clever and only re-damage if the matrix changed.
349 // However, we don't need to worry about that. The cost of over-damaging
350 // here is only going to be a single additional map rect of this node
351 // plus a rect join(). The parent's transform (and up) will only be
352 // performed once.
John Recka447d292014-06-11 18:39:44 -0700353 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700354 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700355 }
John Reck25fbb3f2014-06-12 13:46:45 -0700356}
357
Andreas Gampe64bb4132014-11-22 00:35:09 +0000358void RenderNode::applyLayerPropertiesToLayer(TreeInfo& info) {
John Reck25fbb3f2014-06-12 13:46:45 -0700359 if (CC_LIKELY(!mLayer)) return;
360
361 const LayerProperties& props = properties().layerProperties();
362 mLayer->setAlpha(props.alpha(), props.xferMode());
363 mLayer->setColorFilter(props.colorFilter());
364 mLayer->setBlend(props.needsBlending());
365}
366
367void RenderNode::pushStagingDisplayListChanges(TreeInfo& info) {
John Reck8de65a82014-04-09 15:23:38 -0700368 if (mNeedsDisplayListDataSync) {
369 mNeedsDisplayListDataSync = false;
John Reckdcba6722014-07-08 13:59:49 -0700370 // Make sure we inc first so that we don't fluctuate between 0 and 1,
371 // which would thrash the layer cache
372 if (mStagingDisplayListData) {
373 for (size_t i = 0; i < mStagingDisplayListData->children().size(); i++) {
374 mStagingDisplayListData->children()[i]->mRenderNode->incParentRefCount();
375 }
376 }
John Reck5c9d7172014-10-22 11:32:27 -0700377 // Damage with the old display list first then the new one to catch any
378 // changes in isRenderable or, in the future, bounds
379 damageSelf(info);
John Reckdcba6722014-07-08 13:59:49 -0700380 deleteDisplayListData();
John Reck8de65a82014-04-09 15:23:38 -0700381 mDisplayListData = mStagingDisplayListData;
Chris Craikd41c4d82015-01-05 15:51:13 -0800382 mStagingDisplayListData = nullptr;
John Reck09d5cdd2014-07-24 10:36:08 -0700383 if (mDisplayListData) {
384 for (size_t i = 0; i < mDisplayListData->functors.size(); i++) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800385 (*mDisplayListData->functors[i])(DrawGlInfo::kModeSync, nullptr);
John Reck09d5cdd2014-07-24 10:36:08 -0700386 }
387 }
John Recke4267ea2014-06-03 15:53:15 -0700388 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700389 }
John Reck8de65a82014-04-09 15:23:38 -0700390}
391
John Reckdcba6722014-07-08 13:59:49 -0700392void RenderNode::deleteDisplayListData() {
393 if (mDisplayListData) {
394 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
395 mDisplayListData->children()[i]->mRenderNode->decParentRefCount();
396 }
397 }
398 delete mDisplayListData;
Chris Craikd41c4d82015-01-05 15:51:13 -0800399 mDisplayListData = nullptr;
John Reckdcba6722014-07-08 13:59:49 -0700400}
401
Chris Craika766cb22015-06-08 16:49:43 -0700402void RenderNode::prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayListData* subtree) {
John Reck8de65a82014-04-09 15:23:38 -0700403 if (subtree) {
John Reck860d1552014-04-11 19:15:05 -0700404 TextureCache& cache = Caches::getInstance().textureCache;
John Reck09d5cdd2014-07-24 10:36:08 -0700405 info.out.hasFunctors |= subtree->functors.size();
John Reck860d1552014-04-11 19:15:05 -0700406 for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) {
John Reck00e79c92015-07-21 10:23:59 -0700407 info.prepareTextures = cache.prefetchAndMarkInUse(
408 info.canvasContext, subtree->bitmapResources[i]);
John Reckf4198b72014-04-09 17:00:04 -0700409 }
John Reck8de65a82014-04-09 15:23:38 -0700410 for (size_t i = 0; i < subtree->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700411 DrawRenderNodeOp* op = subtree->children()[i];
412 RenderNode* childNode = op->mRenderNode;
John Recka447d292014-06-11 18:39:44 -0700413 info.damageAccumulator->pushTransform(&op->mTransformFromParent);
Chris Craika766cb22015-06-08 16:49:43 -0700414 bool childFunctorsNeedLayer = functorsNeedLayer
415 // Recorded with non-rect clip, or canvas-rotated by parent
416 || op->mRecordedWithPotentialStencilClip;
417 childNode->prepareTreeImpl(info, childFunctorsNeedLayer);
John Recka447d292014-06-11 18:39:44 -0700418 info.damageAccumulator->popTransform();
John Reck5bf11bb2014-03-25 10:22:09 -0700419 }
John Reck113e0822014-03-18 09:22:59 -0700420 }
421}
422
John Reckdcba6722014-07-08 13:59:49 -0700423void RenderNode::destroyHardwareResources() {
424 if (mLayer) {
425 LayerRenderer::destroyLayer(mLayer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800426 mLayer = nullptr;
John Reckdcba6722014-07-08 13:59:49 -0700427 }
428 if (mDisplayListData) {
429 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
430 mDisplayListData->children()[i]->mRenderNode->destroyHardwareResources();
431 }
432 if (mNeedsDisplayListDataSync) {
433 // Next prepare tree we are going to push a new display list, so we can
434 // drop our current one now
435 deleteDisplayListData();
436 }
437 }
438}
439
440void RenderNode::decParentRefCount() {
441 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
442 mParentCount--;
443 if (!mParentCount) {
444 // If a child of ours is being attached to our parent then this will incorrectly
445 // destroy its hardware resources. However, this situation is highly unlikely
446 // and the failure is "just" that the layer is re-created, so this should
447 // be safe enough
448 destroyHardwareResources();
449 }
450}
451
John Reck113e0822014-03-18 09:22:59 -0700452/*
453 * For property operations, we pass a savecount of 0, since the operations aren't part of the
454 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
John Reckd0a0b2a2014-03-20 16:28:56 -0700455 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
John Reck113e0822014-03-18 09:22:59 -0700456 */
457#define PROPERTY_SAVECOUNT 0
458
459template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700460void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700461#if DEBUG_DISPLAY_LIST
Chris Craikb265e2c2014-03-27 15:50:09 -0700462 properties().debugOutputProperties(handler.level() + 1);
John Reck113e0822014-03-18 09:22:59 -0700463#endif
John Reckd0a0b2a2014-03-20 16:28:56 -0700464 if (properties().getLeft() != 0 || properties().getTop() != 0) {
465 renderer.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700466 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700467 if (properties().getStaticMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500468 renderer.concatMatrix(*properties().getStaticMatrix());
John Reckd0a0b2a2014-03-20 16:28:56 -0700469 } else if (properties().getAnimationMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500470 renderer.concatMatrix(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700471 }
John Reckf7483e32014-04-11 08:54:47 -0700472 if (properties().hasTransformMatrix()) {
473 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700474 renderer.translate(properties().getTranslationX(), properties().getTranslationY());
John Reck113e0822014-03-18 09:22:59 -0700475 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700476 renderer.concatMatrix(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700477 }
478 }
Chris Craik856f0cc2015-04-21 15:13:29 -0700479 const bool isLayer = properties().effectiveLayerType() != LayerType::None;
Chris Craika753f4c2014-07-24 12:39:17 -0700480 int clipFlags = properties().getClippingFlags();
John Reckd0a0b2a2014-03-20 16:28:56 -0700481 if (properties().getAlpha() < 1) {
John Reck25fbb3f2014-06-12 13:46:45 -0700482 if (isLayer) {
Chris Craika753f4c2014-07-24 12:39:17 -0700483 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
John Reck113e0822014-03-18 09:22:59 -0700484 }
Chris Craik4e9d9b22015-06-12 11:07:23 -0700485 if (CC_LIKELY(isLayer || !properties().getHasOverlappingRendering())) {
486 // simply scale rendering content's alpha
487 renderer.scaleAlpha(properties().getAlpha());
488 } else {
489 // savelayer needed to create an offscreen buffer
490 Rect layerBounds(0, 0, getWidth(), getHeight());
491 if (clipFlags) {
492 properties().getClippingRectForFlags(clipFlags, &layerBounds);
493 clipFlags = 0; // all clipping done by savelayer
494 }
495 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
496 layerBounds.left, layerBounds.top,
497 layerBounds.right, layerBounds.bottom,
498 (int) (properties().getAlpha() * 255),
499 SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kClipToLayer_SaveFlag);
500 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
501 }
Chris Craik1a0808e2015-05-13 16:33:04 -0700502
503 if (CC_UNLIKELY(ATRACE_ENABLED() && properties().promotedToLayer())) {
Chris Craik4e9d9b22015-06-12 11:07:23 -0700504 // pretend alpha always causes savelayer to warn about
505 // performance problem affecting old versions
Chris Craik1a0808e2015-05-13 16:33:04 -0700506 ATRACE_FORMAT("%s alpha caused saveLayer %dx%d", getName(),
507 static_cast<int>(getWidth()),
508 static_cast<int>(getHeight()));
509 }
John Reck113e0822014-03-18 09:22:59 -0700510 }
Chris Craika753f4c2014-07-24 12:39:17 -0700511 if (clipFlags) {
512 Rect clipRect;
513 properties().getClippingRectForFlags(clipFlags, &clipRect);
Chris Craik8c271ca2014-03-25 10:33:01 -0700514 ClipRectOp* op = new (handler.allocator()) ClipRectOp(
Chris Craika753f4c2014-07-24 12:39:17 -0700515 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
516 SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700517 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700518 }
Chris Craik8c271ca2014-03-25 10:33:01 -0700519
Chris Craike83cbd42014-09-03 17:52:24 -0700520 // TODO: support nesting round rect clips
Chris Craikaf4d04c2014-07-29 12:50:14 -0700521 if (mProperties.getRevealClip().willClip()) {
522 Rect bounds;
523 mProperties.getRevealClip().getBounds(&bounds);
524 renderer.setClippingRoundRect(handler.allocator(), bounds, mProperties.getRevealClip().getRadius());
525 } else if (mProperties.getOutline().willClip()) {
526 renderer.setClippingOutline(handler.allocator(), &(mProperties.getOutline()));
John Reck113e0822014-03-18 09:22:59 -0700527 }
528}
529
530/**
531 * Apply property-based transformations to input matrix
532 *
533 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
534 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
535 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700536void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700537 if (properties().getLeft() != 0 || properties().getTop() != 0) {
538 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700539 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700540 if (properties().getStaticMatrix()) {
541 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700542 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700543 } else if (properties().getAnimationMatrix()) {
544 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700545 matrix.multiply(anim);
546 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700547
Chris Craikcc39e162014-04-25 18:34:11 -0700548 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700549 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700550 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700551 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700552 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700553 } else {
554 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700555 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700556 } else {
557 mat4 true3dMat;
558 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700559 properties().getPivotX() + properties().getTranslationX(),
560 properties().getPivotY() + properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700561 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700562 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
563 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
564 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
565 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
566 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700567
568 matrix.multiply(true3dMat);
569 }
570 }
571 }
572}
573
574/**
575 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
576 *
577 * This should be called before a call to defer() or drawDisplayList()
578 *
579 * Each DisplayList that serves as a 3d root builds its list of composited children,
580 * which are flagged to not draw in the standard draw loop.
581 */
582void RenderNode::computeOrdering() {
583 ATRACE_CALL();
584 mProjectedNodes.clear();
585
586 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
587 // transform properties are applied correctly to top level children
Chris Craikd41c4d82015-01-05 15:51:13 -0800588 if (mDisplayListData == nullptr) return;
John Reck087bc0c2014-04-04 16:20:08 -0700589 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700590 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
Derek Sollenbergerf2932592015-08-13 14:59:33 -0400591 childOp->mRenderNode->computeOrderingImpl(childOp, &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700592 }
593}
594
595void RenderNode::computeOrderingImpl(
Chris Craika7090e02014-06-20 16:01:00 -0700596 DrawRenderNodeOp* opState,
John Reck272a6852015-07-29 16:48:58 -0700597 std::vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700598 const mat4* transformFromProjectionSurface) {
599 mProjectedNodes.clear();
Chris Craikd41c4d82015-01-05 15:51:13 -0800600 if (mDisplayListData == nullptr || mDisplayListData->isEmpty()) return;
John Reck113e0822014-03-18 09:22:59 -0700601
602 // TODO: should avoid this calculation in most cases
603 // TODO: just calculate single matrix, down to all leaf composited elements
604 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
605 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
606
John Reckd0a0b2a2014-03-20 16:28:56 -0700607 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700608 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
609 opState->mSkipInOrderDraw = true;
Chris Craik7c85c542015-08-19 15:10:24 -0700610 opState->mTransformFromCompositingAncestor = localTransformFromProjectionSurface;
John Reck272a6852015-07-29 16:48:58 -0700611 compositedChildrenOfProjectionSurface->push_back(opState);
John Reck113e0822014-03-18 09:22:59 -0700612 } else {
613 // standard in order draw
614 opState->mSkipInOrderDraw = false;
615 }
616
John Reck087bc0c2014-04-04 16:20:08 -0700617 if (mDisplayListData->children().size() > 0) {
John Reck113e0822014-03-18 09:22:59 -0700618 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
619 bool haveAppliedPropertiesToProjection = false;
John Reck087bc0c2014-04-04 16:20:08 -0700620 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700621 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
622 RenderNode* child = childOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700623
John Reck272a6852015-07-29 16:48:58 -0700624 std::vector<DrawRenderNodeOp*>* projectionChildren = nullptr;
Chris Craikd41c4d82015-01-05 15:51:13 -0800625 const mat4* projectionTransform = nullptr;
John Reckd0a0b2a2014-03-20 16:28:56 -0700626 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
Chris Craikbf72eb82015-06-08 11:30:44 -0700627 // if receiving projections, collect projecting descendant
John Reck113e0822014-03-18 09:22:59 -0700628
Chris Craikbf72eb82015-06-08 11:30:44 -0700629 // Note that if a direct descendant is projecting backwards, we pass its
630 // grandparent projection collection, since it shouldn't project onto its
John Reck113e0822014-03-18 09:22:59 -0700631 // parent, where it will already be drawing.
632 projectionChildren = &mProjectedNodes;
633 projectionTransform = &mat4::identity();
634 } else {
635 if (!haveAppliedPropertiesToProjection) {
636 applyViewPropertyTransforms(localTransformFromProjectionSurface);
637 haveAppliedPropertiesToProjection = true;
638 }
639 projectionChildren = compositedChildrenOfProjectionSurface;
640 projectionTransform = &localTransformFromProjectionSurface;
641 }
Derek Sollenbergerf2932592015-08-13 14:59:33 -0400642 child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700643 }
644 }
John Reck113e0822014-03-18 09:22:59 -0700645}
646
647class DeferOperationHandler {
648public:
649 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
650 : mDeferStruct(deferStruct), mLevel(level) {}
651 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
652 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
653 }
654 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
Andreas Gampe64bb4132014-11-22 00:35:09 +0000655 inline void startMark(const char* name) {} // do nothing
Chris Craikb265e2c2014-03-27 15:50:09 -0700656 inline void endMark() {}
657 inline int level() { return mLevel; }
658 inline int replayFlags() { return mDeferStruct.mReplayFlags; }
Chris Craik74669862014-08-07 17:27:30 -0700659 inline SkPath* allocPathForFrame() { return mDeferStruct.allocPathForFrame(); }
John Reck113e0822014-03-18 09:22:59 -0700660
661private:
662 DeferStateStruct& mDeferStruct;
663 const int mLevel;
664};
665
Chris Craik80d49022014-06-20 15:03:43 -0700666void RenderNode::defer(DeferStateStruct& deferStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700667 DeferOperationHandler handler(deferStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700668 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700669}
670
671class ReplayOperationHandler {
672public:
673 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
674 : mReplayStruct(replayStruct), mLevel(level) {}
675 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
676#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
Chris Craik3f0854292014-04-15 16:18:08 -0700677 mReplayStruct.mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700678#endif
679 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
680 }
681 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700682 inline void startMark(const char* name) {
683 mReplayStruct.mRenderer.startMark(name);
684 }
685 inline void endMark() {
686 mReplayStruct.mRenderer.endMark();
Chris Craikb265e2c2014-03-27 15:50:09 -0700687 }
688 inline int level() { return mLevel; }
689 inline int replayFlags() { return mReplayStruct.mReplayFlags; }
Chris Craik74669862014-08-07 17:27:30 -0700690 inline SkPath* allocPathForFrame() { return mReplayStruct.allocPathForFrame(); }
John Reck113e0822014-03-18 09:22:59 -0700691
692private:
693 ReplayStateStruct& mReplayStruct;
694 const int mLevel;
695};
696
Chris Craik80d49022014-06-20 15:03:43 -0700697void RenderNode::replay(ReplayStateStruct& replayStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700698 ReplayOperationHandler handler(replayStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700699 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700700}
701
Chris Craik8afd0f22014-08-21 17:41:57 -0700702void RenderNode::buildZSortedChildList(const DisplayListData::Chunk& chunk,
John Reck272a6852015-07-29 16:48:58 -0700703 std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes) {
Chris Craik8afd0f22014-08-21 17:41:57 -0700704 if (chunk.beginChildIndex == chunk.endChildIndex) return;
John Reck113e0822014-03-18 09:22:59 -0700705
Chris Craik8afd0f22014-08-21 17:41:57 -0700706 for (unsigned int i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700707 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
708 RenderNode* child = childOp->mRenderNode;
Chris Craikcc39e162014-04-25 18:34:11 -0700709 float childZ = child->properties().getZ();
John Reck113e0822014-03-18 09:22:59 -0700710
Chris Craik8afd0f22014-08-21 17:41:57 -0700711 if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
John Reck272a6852015-07-29 16:48:58 -0700712 zTranslatedNodes.push_back(ZDrawRenderNodeOpPair(childZ, childOp));
John Reck113e0822014-03-18 09:22:59 -0700713 childOp->mSkipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700714 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700715 // regular, in order drawing DisplayList
716 childOp->mSkipInOrderDraw = false;
717 }
718 }
719
Chris Craik8afd0f22014-08-21 17:41:57 -0700720 // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
John Reck113e0822014-03-18 09:22:59 -0700721 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
722}
723
Chris Craikb265e2c2014-03-27 15:50:09 -0700724template <class T>
725void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
Chris Craik77b5cad2014-07-30 18:23:07 -0700726 if (properties().getAlpha() <= 0.0f
727 || properties().getOutline().getAlpha() <= 0.0f
Teng-Hui Zhu8d0ec382015-10-01 16:49:16 -0700728 || !properties().getOutline().getPath()
729 || properties().getScaleX() == 0
730 || properties().getScaleY() == 0) {
Chris Craik77b5cad2014-07-30 18:23:07 -0700731 // no shadow to draw
732 return;
733 }
Chris Craikb265e2c2014-03-27 15:50:09 -0700734
735 mat4 shadowMatrixXY(transformFromParent);
736 applyViewPropertyTransforms(shadowMatrixXY);
737
738 // Z matrix needs actual 3d transformation, so mapped z values will be correct
739 mat4 shadowMatrixZ(transformFromParent);
740 applyViewPropertyTransforms(shadowMatrixZ, true);
741
Chris Craik74669862014-08-07 17:27:30 -0700742 const SkPath* casterOutlinePath = properties().getOutline().getPath();
Chris Craikaf4d04c2014-07-29 12:50:14 -0700743 const SkPath* revealClipPath = properties().getRevealClip().getPath();
Chris Craik61317322014-05-21 13:03:52 -0700744 if (revealClipPath && revealClipPath->isEmpty()) return;
745
Chris Craik77b5cad2014-07-30 18:23:07 -0700746 float casterAlpha = properties().getAlpha() * properties().getOutline().getAlpha();
Chris Craik74669862014-08-07 17:27:30 -0700747
Chris Craik74669862014-08-07 17:27:30 -0700748
Chris Craikfaa79ff2014-12-01 13:44:21 -0800749 // holds temporary SkPath to store the result of intersections
Chris Craikd41c4d82015-01-05 15:51:13 -0800750 SkPath* frameAllocatedPath = nullptr;
Chris Craikfaa79ff2014-12-01 13:44:21 -0800751 const SkPath* outlinePath = casterOutlinePath;
752
753 // intersect the outline with the reveal clip, if present
754 if (revealClipPath) {
755 frameAllocatedPath = handler.allocPathForFrame();
756
Tom Hudson02a26302015-06-24 11:32:42 -0400757 Op(*outlinePath, *revealClipPath, kIntersect_SkPathOp, frameAllocatedPath);
Chris Craikfaa79ff2014-12-01 13:44:21 -0800758 outlinePath = frameAllocatedPath;
759 }
760
761 // intersect the outline with the clipBounds, if present
762 if (properties().getClippingFlags() & CLIP_TO_CLIP_BOUNDS) {
763 if (!frameAllocatedPath) {
764 frameAllocatedPath = handler.allocPathForFrame();
765 }
766
767 Rect clipBounds;
768 properties().getClippingRectForFlags(CLIP_TO_CLIP_BOUNDS, &clipBounds);
769 SkPath clipBoundsPath;
770 clipBoundsPath.addRect(clipBounds.left, clipBounds.top,
771 clipBounds.right, clipBounds.bottom);
772
Tom Hudson02a26302015-06-24 11:32:42 -0400773 Op(*outlinePath, clipBoundsPath, kIntersect_SkPathOp, frameAllocatedPath);
Chris Craik74669862014-08-07 17:27:30 -0700774 outlinePath = frameAllocatedPath;
775 }
776
Chris Craikb265e2c2014-03-27 15:50:09 -0700777 DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp(
Chris Craik74669862014-08-07 17:27:30 -0700778 shadowMatrixXY, shadowMatrixZ, casterAlpha, outlinePath);
Chris Craikb265e2c2014-03-27 15:50:09 -0700779 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
780}
781
John Reck113e0822014-03-18 09:22:59 -0700782#define SHADOW_DELTA 0.1f
783
784template <class T>
Chris Craikc3e75f92014-08-27 15:34:52 -0700785void RenderNode::issueOperationsOf3dChildren(ChildrenSelectMode mode,
John Reck272a6852015-07-29 16:48:58 -0700786 const Matrix4& initialTransform, const std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craikc3e75f92014-08-27 15:34:52 -0700787 OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700788 const int size = zTranslatedNodes.size();
789 if (size == 0
Chris Craikb9ce116d2015-08-20 15:14:06 -0700790 || (mode == ChildrenSelectMode::NegativeZChildren && zTranslatedNodes[0].key > 0.0f)
791 || (mode == ChildrenSelectMode::PositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
John Reck113e0822014-03-18 09:22:59 -0700792 // no 3d children to draw
793 return;
794 }
795
Chris Craikc3e75f92014-08-27 15:34:52 -0700796 // Apply the base transform of the parent of the 3d children. This isolates
797 // 3d children of the current chunk from transformations made in previous chunks.
798 int rootRestoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
Chris Craik6daa13c2015-08-19 13:32:12 -0700799 renderer.setGlobalMatrix(initialTransform);
Chris Craikc3e75f92014-08-27 15:34:52 -0700800
John Reck113e0822014-03-18 09:22:59 -0700801 /**
802 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
803 * with very similar Z heights to draw together.
804 *
805 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
806 * underneath both, and neither's shadow is drawn on top of the other.
807 */
808 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
809 size_t drawIndex, shadowIndex, endIndex;
Chris Craikb9ce116d2015-08-20 15:14:06 -0700810 if (mode == ChildrenSelectMode::NegativeZChildren) {
John Reck113e0822014-03-18 09:22:59 -0700811 drawIndex = 0;
812 endIndex = nonNegativeIndex;
813 shadowIndex = endIndex; // draw no shadows
814 } else {
815 drawIndex = nonNegativeIndex;
816 endIndex = size;
817 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
818 }
Chris Craik3f0854292014-04-15 16:18:08 -0700819
820 DISPLAY_LIST_LOGD("%*s%d %s 3d children:", (handler.level() + 1) * 2, "",
821 endIndex - drawIndex, mode == kNegativeZChildren ? "negative" : "positive");
822
John Reck113e0822014-03-18 09:22:59 -0700823 float lastCasterZ = 0.0f;
824 while (shadowIndex < endIndex || drawIndex < endIndex) {
825 if (shadowIndex < endIndex) {
Chris Craika7090e02014-06-20 16:01:00 -0700826 DrawRenderNodeOp* casterOp = zTranslatedNodes[shadowIndex].value;
827 RenderNode* caster = casterOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700828 const float casterZ = zTranslatedNodes[shadowIndex].key;
829 // attempt to render the shadow if the caster about to be drawn is its caster,
830 // OR if its caster's Z value is similar to the previous potential caster
831 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700832 caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
John Reck113e0822014-03-18 09:22:59 -0700833
834 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
835 shadowIndex++;
836 continue;
837 }
838 }
839
840 // only the actual child DL draw needs to be in save/restore,
841 // since it modifies the renderer's matrix
842 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
843
Chris Craika7090e02014-06-20 16:01:00 -0700844 DrawRenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
John Reck113e0822014-03-18 09:22:59 -0700845
846 renderer.concatMatrix(childOp->mTransformFromParent);
847 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700848 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700849 childOp->mSkipInOrderDraw = true;
850
851 renderer.restoreToCount(restoreTo);
852 drawIndex++;
853 }
Chris Craikc3e75f92014-08-27 15:34:52 -0700854 renderer.restoreToCount(rootRestoreTo);
John Reck113e0822014-03-18 09:22:59 -0700855}
856
857template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700858void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
Chris Craik3f0854292014-04-15 16:18:08 -0700859 DISPLAY_LIST_LOGD("%*s%d projected children:", (handler.level() + 1) * 2, "", mProjectedNodes.size());
860 const SkPath* projectionReceiverOutline = properties().getOutline().getPath();
Chris Craik3f0854292014-04-15 16:18:08 -0700861 int restoreTo = renderer.getSaveCount();
862
Chris Craikb3cca872014-08-08 18:42:51 -0700863 LinearAllocator& alloc = handler.allocator();
864 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
865 PROPERTY_SAVECOUNT, properties().getClipToBounds());
866
867 // Transform renderer to match background we're projecting onto
868 // (by offsetting canvas by translationX/Y of background rendernode, since only those are set)
869 const DisplayListOp* op =
870 (mDisplayListData->displayListOps[mDisplayListData->projectionReceiveIndex]);
871 const DrawRenderNodeOp* backgroundOp = reinterpret_cast<const DrawRenderNodeOp*>(op);
872 const RenderProperties& backgroundProps = backgroundOp->mRenderNode->properties();
873 renderer.translate(backgroundProps.getTranslationX(), backgroundProps.getTranslationY());
874
Chris Craikfca52b752015-04-28 11:45:59 -0700875 // If the projection reciever has an outline, we mask projected content to it
876 // (which we know, apriori, are all tessellated paths)
877 renderer.setProjectionPathMask(alloc, projectionReceiverOutline);
Chris Craik3f0854292014-04-15 16:18:08 -0700878
879 // draw projected nodes
John Reck113e0822014-03-18 09:22:59 -0700880 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700881 DrawRenderNodeOp* childOp = mProjectedNodes[i];
John Reck113e0822014-03-18 09:22:59 -0700882
883 // matrix save, concat, and restore can be done safely without allocating operations
884 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
885 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
886 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700887 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700888 childOp->mSkipInOrderDraw = true;
889 renderer.restoreToCount(restoreTo);
890 }
Chris Craik3f0854292014-04-15 16:18:08 -0700891
Chris Craikfca52b752015-04-28 11:45:59 -0700892 handler(new (alloc) RestoreToCountOp(restoreTo),
893 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700894}
895
896/**
897 * This function serves both defer and replay modes, and will organize the displayList's component
898 * operations for a single frame:
899 *
900 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
901 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
902 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
903 * defer vs replay logic, per operation
904 */
905template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700906void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
Chris Craik06451282014-07-21 10:25:54 -0700907 if (mDisplayListData->isEmpty()) {
Chris Craikbf72eb82015-06-08 11:30:44 -0700908 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", handler.level() * 2, "",
909 this, getName());
Chris Craik06451282014-07-21 10:25:54 -0700910 return;
911 }
912
Chris Craik51d6a3d2014-12-22 17:16:56 -0800913 const bool drawLayer = (mLayer && (&renderer != mLayer->renderer.get()));
John Reck25fbb3f2014-06-12 13:46:45 -0700914 // If we are updating the contents of mLayer, we don't want to apply any of
915 // the RenderNode's properties to this issueOperations pass. Those will all
916 // be applied when the layer is drawn, aka when this is true.
917 const bool useViewProperties = (!mLayer || drawLayer);
Chris Craik06451282014-07-21 10:25:54 -0700918 if (useViewProperties) {
919 const Outline& outline = properties().getOutline();
Teng-Hui Zhu8d0ec382015-10-01 16:49:16 -0700920 if (properties().getAlpha() <= 0
921 || (outline.getShouldClip() && outline.isEmpty())
922 || properties().getScaleX() == 0
923 || properties().getScaleY() == 0) {
Chris Craikbf72eb82015-06-08 11:30:44 -0700924 DISPLAY_LIST_LOGD("%*sRejected display list (%p, %s)", handler.level() * 2, "",
925 this, getName());
Chris Craik06451282014-07-21 10:25:54 -0700926 return;
927 }
John Reck113e0822014-03-18 09:22:59 -0700928 }
929
Chris Craik3f0854292014-04-15 16:18:08 -0700930 handler.startMark(getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700931
John Reck113e0822014-03-18 09:22:59 -0700932#if DEBUG_DISPLAY_LIST
Chris Craik3f0854292014-04-15 16:18:08 -0700933 const Rect& clipRect = renderer.getLocalClipBounds();
934 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), localClipBounds: %.0f, %.0f, %.0f, %.0f",
Chris Craik03188872015-02-02 18:39:33 -0800935 handler.level() * 2, "", this, getName(),
Chris Craik3f0854292014-04-15 16:18:08 -0700936 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
John Reck113e0822014-03-18 09:22:59 -0700937#endif
938
939 LinearAllocator& alloc = handler.allocator();
940 int restoreTo = renderer.getSaveCount();
941 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
John Reckd0a0b2a2014-03-20 16:28:56 -0700942 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700943
Chris Craikbf72eb82015-06-08 11:30:44 -0700944 DISPLAY_LIST_LOGD("%*sSave %d %d", (handler.level() + 1) * 2, "",
John Reck113e0822014-03-18 09:22:59 -0700945 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
946
John Reck25fbb3f2014-06-12 13:46:45 -0700947 if (useViewProperties) {
948 setViewProperties<T>(renderer, handler);
949 }
John Reck113e0822014-03-18 09:22:59 -0700950
Chris Craik8c271ca2014-03-25 10:33:01 -0700951 bool quickRejected = properties().getClipToBounds()
952 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -0700953 if (!quickRejected) {
Chris Craikc3e75f92014-08-27 15:34:52 -0700954 Matrix4 initialTransform(*(renderer.currentTransform()));
Tom Hudsonac7b6d32015-06-30 11:26:13 -0400955 renderer.setBaseTransform(initialTransform);
Chris Craikc3e75f92014-08-27 15:34:52 -0700956
John Reck25fbb3f2014-06-12 13:46:45 -0700957 if (drawLayer) {
Chris Craik3aadd602015-08-20 12:41:40 -0700958 handler(new (alloc) DrawLayerOp(mLayer),
John Reck25fbb3f2014-06-12 13:46:45 -0700959 renderer.getSaveCount() - 1, properties().getClipToBounds());
960 } else {
Chris Craikc166b6c2014-09-05 19:55:30 -0700961 const int saveCountOffset = renderer.getSaveCount() - 1;
962 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
Chris Craik8afd0f22014-08-21 17:41:57 -0700963 for (size_t chunkIndex = 0; chunkIndex < mDisplayListData->getChunks().size(); chunkIndex++) {
964 const DisplayListData::Chunk& chunk = mDisplayListData->getChunks()[chunkIndex];
John Reck113e0822014-03-18 09:22:59 -0700965
John Reck272a6852015-07-29 16:48:58 -0700966 std::vector<ZDrawRenderNodeOpPair> zTranslatedNodes;
Chris Craik8afd0f22014-08-21 17:41:57 -0700967 buildZSortedChildList(chunk, zTranslatedNodes);
968
Chris Craikb9ce116d2015-08-20 15:14:06 -0700969 issueOperationsOf3dChildren(ChildrenSelectMode::NegativeZChildren,
Chris Craikc3e75f92014-08-27 15:34:52 -0700970 initialTransform, zTranslatedNodes, renderer, handler);
971
Chris Craik8afd0f22014-08-21 17:41:57 -0700972
Andreas Gampeedaecc12014-11-10 20:54:07 -0800973 for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
Chris Craik8afd0f22014-08-21 17:41:57 -0700974 DisplayListOp *op = mDisplayListData->displayListOps[opIndex];
Chris Craik80d49022014-06-20 15:03:43 -0700975#if DEBUG_DISPLAY_LIST
Chris Craik03188872015-02-02 18:39:33 -0800976 op->output(handler.level() + 1);
Chris Craik80d49022014-06-20 15:03:43 -0700977#endif
Chris Craik8afd0f22014-08-21 17:41:57 -0700978 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700979
John Reck272a6852015-07-29 16:48:58 -0700980 if (CC_UNLIKELY(!mProjectedNodes.empty() && projectionReceiveIndex >= 0 &&
Andreas Gampeedaecc12014-11-10 20:54:07 -0800981 opIndex == static_cast<size_t>(projectionReceiveIndex))) {
Chris Craik8afd0f22014-08-21 17:41:57 -0700982 issueOperationsOfProjectedChildren(renderer, handler);
983 }
John Reck25fbb3f2014-06-12 13:46:45 -0700984 }
John Reck113e0822014-03-18 09:22:59 -0700985
Chris Craikb9ce116d2015-08-20 15:14:06 -0700986 issueOperationsOf3dChildren(ChildrenSelectMode::PositiveZChildren,
Chris Craikc3e75f92014-08-27 15:34:52 -0700987 initialTransform, zTranslatedNodes, renderer, handler);
Chris Craik8afd0f22014-08-21 17:41:57 -0700988 }
John Reck25fbb3f2014-06-12 13:46:45 -0700989 }
John Reck113e0822014-03-18 09:22:59 -0700990 }
991
Chris Craikbf72eb82015-06-08 11:30:44 -0700992 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (handler.level() + 1) * 2, "", restoreTo);
John Reck113e0822014-03-18 09:22:59 -0700993 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -0700994 PROPERTY_SAVECOUNT, properties().getClipToBounds());
Chris Craikb265e2c2014-03-27 15:50:09 -0700995
Chris Craikbf72eb82015-06-08 11:30:44 -0700996 DISPLAY_LIST_LOGD("%*sDone (%p, %s)", handler.level() * 2, "", this, getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700997 handler.endMark();
John Reck113e0822014-03-18 09:22:59 -0700998}
999
1000} /* namespace uirenderer */
1001} /* namespace android */