blob: fe03806903a587abd88fc25481af155d17dc7820 [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
17#define ATRACE_TAG ATRACE_TAG_VIEW
Chris Craik80d49022014-06-20 15:03:43 -070018#define LOG_TAG "OpenGLRenderer"
John Reck113e0822014-03-18 09:22:59 -070019
20#include "RenderNode.h"
21
John Recke45b1fd2014-04-15 09:50:16 -070022#include <algorithm>
John Reckc25e5062014-06-18 14:21:29 -070023#include <string>
John Recke45b1fd2014-04-15 09:50:16 -070024
John Reck113e0822014-03-18 09:22:59 -070025#include <SkCanvas.h>
26#include <algorithm>
27
28#include <utils/Trace.h>
29
John Recke4267ea2014-06-03 15:53:15 -070030#include "DamageAccumulator.h"
John Reck113e0822014-03-18 09:22:59 -070031#include "Debug.h"
32#include "DisplayListOp.h"
33#include "DisplayListLogBuffer.h"
John Reck25fbb3f2014-06-12 13:46:45 -070034#include "LayerRenderer.h"
35#include "OpenGLRenderer.h"
Chris Craike0bb87d2014-04-22 17:55:41 -070036#include "utils/MathUtils.h"
John Reck113e0822014-03-18 09:22:59 -070037
38namespace android {
39namespace uirenderer {
40
41void RenderNode::outputLogBuffer(int fd) {
42 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
43 if (logBuffer.isEmpty()) {
44 return;
45 }
46
47 FILE *file = fdopen(fd, "a");
48
49 fprintf(file, "\nRecent DisplayList operations\n");
50 logBuffer.outputCommands(file);
51
52 String8 cachesLog;
53 Caches::getInstance().dumpMemoryUsage(cachesLog);
54 fprintf(file, "\nCaches:\n%s", cachesLog.string());
55 fprintf(file, "\n");
56
57 fflush(file);
58}
59
John Reck8de65a82014-04-09 15:23:38 -070060RenderNode::RenderNode()
John Reckff941dc2014-05-14 16:34:14 -070061 : mDirtyPropertyFields(0)
John Reck8de65a82014-04-09 15:23:38 -070062 , mNeedsDisplayListDataSync(false)
63 , mDisplayListData(0)
John Recke45b1fd2014-04-15 09:50:16 -070064 , mStagingDisplayListData(0)
John Reck68bfe0a2014-06-24 15:34:58 -070065 , mAnimatorManager(*this)
John Reckdcba6722014-07-08 13:59:49 -070066 , mLayer(0)
67 , mParentCount(0) {
John Reck113e0822014-03-18 09:22:59 -070068}
69
70RenderNode::~RenderNode() {
John Reckdcba6722014-07-08 13:59:49 -070071 deleteDisplayListData();
John Reck8de65a82014-04-09 15:23:38 -070072 delete mStagingDisplayListData;
John Reck25fbb3f2014-06-12 13:46:45 -070073 LayerRenderer::destroyLayerDeferred(mLayer);
John Reck113e0822014-03-18 09:22:59 -070074}
75
John Reck8de65a82014-04-09 15:23:38 -070076void RenderNode::setStagingDisplayList(DisplayListData* data) {
77 mNeedsDisplayListDataSync = true;
78 delete mStagingDisplayListData;
79 mStagingDisplayListData = data;
80 if (mStagingDisplayListData) {
81 Caches::getInstance().registerFunctors(mStagingDisplayListData->functorCount);
John Reck113e0822014-03-18 09:22:59 -070082 }
83}
84
85/**
86 * This function is a simplified version of replay(), where we simply retrieve and log the
87 * display list. This function should remain in sync with the replay() function.
88 */
89void RenderNode::output(uint32_t level) {
90 ALOGD("%*sStart display list (%p, %s, render=%d)", (level - 1) * 2, "", this,
Chris Craik3f0854292014-04-15 16:18:08 -070091 getName(), isRenderable());
John Reck113e0822014-03-18 09:22:59 -070092 ALOGD("%*s%s %d", level * 2, "", "Save",
93 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
94
John Reckd0a0b2a2014-03-20 16:28:56 -070095 properties().debugOutputProperties(level);
John Reck113e0822014-03-18 09:22:59 -070096 int flags = DisplayListOp::kOpLogFlag_Recurse;
97 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
98 mDisplayListData->displayListOps[i]->output(level, flags);
99 }
100
Chris Craik3f0854292014-04-15 16:18:08 -0700101 ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, getName());
John Reck113e0822014-03-18 09:22:59 -0700102}
103
John Reckfe5e7b72014-05-23 17:42:28 -0700104int RenderNode::getDebugSize() {
105 int size = sizeof(RenderNode);
106 if (mStagingDisplayListData) {
107 size += mStagingDisplayListData->allocator.usedSize();
108 }
109 if (mDisplayListData && mDisplayListData != mStagingDisplayListData) {
110 size += mDisplayListData->allocator.usedSize();
111 }
112 return size;
113}
114
John Reckf4198b72014-04-09 17:00:04 -0700115void RenderNode::prepareTree(TreeInfo& info) {
116 ATRACE_CALL();
117
118 prepareTreeImpl(info);
119}
120
John Reck68bfe0a2014-06-24 15:34:58 -0700121void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
122 mAnimatorManager.addAnimator(animator);
123}
124
John Recke4267ea2014-06-03 15:53:15 -0700125void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700126 if (isRenderable()) {
John Reck293e8682014-06-17 10:34:02 -0700127 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700128 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
129 } else {
130 // Hope this is big enough?
131 // TODO: Get this from the display list ops or something
132 info.damageAccumulator->dirty(INT_MIN, INT_MIN, INT_MAX, INT_MAX);
133 }
John Recke4267ea2014-06-03 15:53:15 -0700134 }
135}
136
John Reck25fbb3f2014-06-12 13:46:45 -0700137void RenderNode::prepareLayer(TreeInfo& info) {
138 LayerType layerType = properties().layerProperties().type();
139 if (CC_UNLIKELY(layerType == kLayerTypeRenderLayer)) {
140 // We push a null transform here as we don't care what the existing dirty
141 // area is, only what our display list dirty is as well as our children's
142 // dirty area
143 info.damageAccumulator->pushNullTransform();
144 }
145}
146
147void RenderNode::pushLayerUpdate(TreeInfo& info) {
148 LayerType layerType = properties().layerProperties().type();
149 // If we are not a layer OR we cannot be rendered (eg, view was detached)
150 // we need to destroy any Layers we may have had previously
151 if (CC_LIKELY(layerType != kLayerTypeRenderLayer) || CC_UNLIKELY(!isRenderable())) {
152 if (layerType == kLayerTypeRenderLayer) {
153 info.damageAccumulator->popTransform();
154 }
155 if (CC_UNLIKELY(mLayer)) {
156 LayerRenderer::destroyLayer(mLayer);
157 mLayer = NULL;
158 }
159 return;
160 }
161
162 if (!mLayer) {
John Reck3b202512014-06-23 13:13:08 -0700163 mLayer = LayerRenderer::createRenderLayer(info.renderState, getWidth(), getHeight());
John Reck25fbb3f2014-06-12 13:46:45 -0700164 applyLayerPropertiesToLayer(info);
165 damageSelf(info);
166 } else if (mLayer->layer.getWidth() != getWidth() || mLayer->layer.getHeight() != getHeight()) {
John Reckc25e5062014-06-18 14:21:29 -0700167 if (!LayerRenderer::resizeLayer(mLayer, getWidth(), getHeight())) {
168 LayerRenderer::destroyLayer(mLayer);
169 mLayer = 0;
170 }
John Reck25fbb3f2014-06-12 13:46:45 -0700171 damageSelf(info);
172 }
173
174 SkRect dirty;
175 info.damageAccumulator->peekAtDirty(&dirty);
176 info.damageAccumulator->popTransform();
177
John Reckc25e5062014-06-18 14:21:29 -0700178 if (!mLayer) {
179 if (info.errorHandler) {
180 std::string msg = "Unable to create layer for ";
181 msg += getName();
182 info.errorHandler->onError(msg);
183 }
184 return;
185 }
186
John Reck25fbb3f2014-06-12 13:46:45 -0700187 if (!dirty.isEmpty()) {
188 mLayer->updateDeferred(this, dirty.fLeft, dirty.fTop, dirty.fRight, dirty.fBottom);
189 }
190 // This is not inside the above if because we may have called
191 // updateDeferred on a previous prepare pass that didn't have a renderer
192 if (info.renderer && mLayer->deferredUpdateScheduled) {
193 info.renderer->pushLayerUpdate(mLayer);
194 }
195}
196
John Recke4267ea2014-06-03 15:53:15 -0700197void RenderNode::prepareTreeImpl(TreeInfo& info) {
John Recka447d292014-06-11 18:39:44 -0700198 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700199
John Reckdcba6722014-07-08 13:59:49 -0700200 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700201 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700202 }
John Reckdcba6722014-07-08 13:59:49 -0700203 mAnimatorManager.animate(info);
John Reck25fbb3f2014-06-12 13:46:45 -0700204 prepareLayer(info);
John Reckdcba6722014-07-08 13:59:49 -0700205 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700206 pushStagingDisplayListChanges(info);
207 }
John Reckf4198b72014-04-09 17:00:04 -0700208 prepareSubTree(info, mDisplayListData);
John Reck25fbb3f2014-06-12 13:46:45 -0700209 pushLayerUpdate(info);
210
John Recka447d292014-06-11 18:39:44 -0700211 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700212}
213
John Reck25fbb3f2014-06-12 13:46:45 -0700214void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reckff941dc2014-05-14 16:34:14 -0700215 // Push the animators first so that setupStartValueIfNecessary() is called
216 // before properties() is trampled by stagingProperties(), as they are
217 // required by some animators.
John Reck68bfe0a2014-06-24 15:34:58 -0700218 mAnimatorManager.pushStaging(info);
John Reckff941dc2014-05-14 16:34:14 -0700219 if (mDirtyPropertyFields) {
220 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700221 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700222 info.damageAccumulator->popTransform();
John Reckff941dc2014-05-14 16:34:14 -0700223 mProperties = mStagingProperties;
John Reck25fbb3f2014-06-12 13:46:45 -0700224 applyLayerPropertiesToLayer(info);
John Recke4267ea2014-06-03 15:53:15 -0700225 // We could try to be clever and only re-damage if the matrix changed.
226 // However, we don't need to worry about that. The cost of over-damaging
227 // here is only going to be a single additional map rect of this node
228 // plus a rect join(). The parent's transform (and up) will only be
229 // performed once.
John Recka447d292014-06-11 18:39:44 -0700230 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700231 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700232 }
John Reck25fbb3f2014-06-12 13:46:45 -0700233}
234
235void RenderNode::applyLayerPropertiesToLayer(TreeInfo& info) {
236 if (CC_LIKELY(!mLayer)) return;
237
238 const LayerProperties& props = properties().layerProperties();
239 mLayer->setAlpha(props.alpha(), props.xferMode());
240 mLayer->setColorFilter(props.colorFilter());
241 mLayer->setBlend(props.needsBlending());
242}
243
244void RenderNode::pushStagingDisplayListChanges(TreeInfo& info) {
John Reck8de65a82014-04-09 15:23:38 -0700245 if (mNeedsDisplayListDataSync) {
246 mNeedsDisplayListDataSync = false;
John Reckdcba6722014-07-08 13:59:49 -0700247 // Make sure we inc first so that we don't fluctuate between 0 and 1,
248 // which would thrash the layer cache
249 if (mStagingDisplayListData) {
250 for (size_t i = 0; i < mStagingDisplayListData->children().size(); i++) {
251 mStagingDisplayListData->children()[i]->mRenderNode->incParentRefCount();
252 }
253 }
254 deleteDisplayListData();
John Reck8de65a82014-04-09 15:23:38 -0700255 mDisplayListData = mStagingDisplayListData;
John Reckdcba6722014-07-08 13:59:49 -0700256 mStagingDisplayListData = NULL;
John Recke4267ea2014-06-03 15:53:15 -0700257 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700258 }
John Reck8de65a82014-04-09 15:23:38 -0700259}
260
John Reckdcba6722014-07-08 13:59:49 -0700261void RenderNode::deleteDisplayListData() {
262 if (mDisplayListData) {
263 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
264 mDisplayListData->children()[i]->mRenderNode->decParentRefCount();
265 }
266 }
267 delete mDisplayListData;
268 mDisplayListData = NULL;
269}
270
John Reckf4198b72014-04-09 17:00:04 -0700271void RenderNode::prepareSubTree(TreeInfo& info, DisplayListData* subtree) {
John Reck8de65a82014-04-09 15:23:38 -0700272 if (subtree) {
John Reck860d1552014-04-11 19:15:05 -0700273 TextureCache& cache = Caches::getInstance().textureCache;
John Reckf9be7792014-05-02 18:21:16 -0700274 info.out.hasFunctors |= subtree->functorCount;
John Reck860d1552014-04-11 19:15:05 -0700275 // TODO: Fix ownedBitmapResources to not require disabling prepareTextures
276 // and thus falling out of async drawing path.
277 if (subtree->ownedBitmapResources.size()) {
278 info.prepareTextures = false;
279 }
280 for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) {
281 info.prepareTextures = cache.prefetchAndMarkInUse(subtree->bitmapResources[i]);
John Reckf4198b72014-04-09 17:00:04 -0700282 }
John Reck8de65a82014-04-09 15:23:38 -0700283 for (size_t i = 0; i < subtree->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700284 DrawRenderNodeOp* op = subtree->children()[i];
285 RenderNode* childNode = op->mRenderNode;
John Recka447d292014-06-11 18:39:44 -0700286 info.damageAccumulator->pushTransform(&op->mTransformFromParent);
John Reckf4198b72014-04-09 17:00:04 -0700287 childNode->prepareTreeImpl(info);
John Recka447d292014-06-11 18:39:44 -0700288 info.damageAccumulator->popTransform();
John Reck5bf11bb2014-03-25 10:22:09 -0700289 }
John Reck113e0822014-03-18 09:22:59 -0700290 }
291}
292
John Reckdcba6722014-07-08 13:59:49 -0700293void RenderNode::destroyHardwareResources() {
294 if (mLayer) {
295 LayerRenderer::destroyLayer(mLayer);
296 mLayer = NULL;
297 }
298 if (mDisplayListData) {
299 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
300 mDisplayListData->children()[i]->mRenderNode->destroyHardwareResources();
301 }
302 if (mNeedsDisplayListDataSync) {
303 // Next prepare tree we are going to push a new display list, so we can
304 // drop our current one now
305 deleteDisplayListData();
306 }
307 }
308}
309
310void RenderNode::decParentRefCount() {
311 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
312 mParentCount--;
313 if (!mParentCount) {
314 // If a child of ours is being attached to our parent then this will incorrectly
315 // destroy its hardware resources. However, this situation is highly unlikely
316 // and the failure is "just" that the layer is re-created, so this should
317 // be safe enough
318 destroyHardwareResources();
319 }
320}
321
John Reck113e0822014-03-18 09:22:59 -0700322/*
323 * For property operations, we pass a savecount of 0, since the operations aren't part of the
324 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
John Reckd0a0b2a2014-03-20 16:28:56 -0700325 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
John Reck113e0822014-03-18 09:22:59 -0700326 */
327#define PROPERTY_SAVECOUNT 0
328
329template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700330void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700331#if DEBUG_DISPLAY_LIST
Chris Craikb265e2c2014-03-27 15:50:09 -0700332 properties().debugOutputProperties(handler.level() + 1);
John Reck113e0822014-03-18 09:22:59 -0700333#endif
John Reckd0a0b2a2014-03-20 16:28:56 -0700334 if (properties().getLeft() != 0 || properties().getTop() != 0) {
335 renderer.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700336 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700337 if (properties().getStaticMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500338 renderer.concatMatrix(*properties().getStaticMatrix());
John Reckd0a0b2a2014-03-20 16:28:56 -0700339 } else if (properties().getAnimationMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500340 renderer.concatMatrix(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700341 }
John Reckf7483e32014-04-11 08:54:47 -0700342 if (properties().hasTransformMatrix()) {
343 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700344 renderer.translate(properties().getTranslationX(), properties().getTranslationY());
John Reck113e0822014-03-18 09:22:59 -0700345 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700346 renderer.concatMatrix(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700347 }
348 }
John Reck25fbb3f2014-06-12 13:46:45 -0700349 const bool isLayer = properties().layerProperties().type() != kLayerTypeNone;
350 bool clipToBoundsNeeded = isLayer ? false : properties().getClipToBounds();
John Reckd0a0b2a2014-03-20 16:28:56 -0700351 if (properties().getAlpha() < 1) {
John Reck25fbb3f2014-06-12 13:46:45 -0700352 if (isLayer) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700353 renderer.setOverrideLayerAlpha(properties().getAlpha());
354 } else if (!properties().getHasOverlappingRendering()) {
355 renderer.scaleAlpha(properties().getAlpha());
John Reck113e0822014-03-18 09:22:59 -0700356 } else {
357 // TODO: should be able to store the size of a DL at record time and not
358 // have to pass it into this call. In fact, this information might be in the
359 // location/size info that we store with the new native transform data.
360 int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
361 if (clipToBoundsNeeded) {
362 saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
363 clipToBoundsNeeded = false; // clipping done by saveLayer
364 }
365
366 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
Chris Craik8c271ca2014-03-25 10:33:01 -0700367 0, 0, properties().getWidth(), properties().getHeight(),
368 properties().getAlpha() * 255, saveFlags);
John Reckd0a0b2a2014-03-20 16:28:56 -0700369 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700370 }
371 }
372 if (clipToBoundsNeeded) {
Chris Craik8c271ca2014-03-25 10:33:01 -0700373 ClipRectOp* op = new (handler.allocator()) ClipRectOp(
374 0, 0, properties().getWidth(), properties().getHeight(), SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700375 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700376 }
Chris Craik8c271ca2014-03-25 10:33:01 -0700377
378 if (CC_UNLIKELY(properties().hasClippingPath())) {
Chris Craik2bcad172014-05-14 18:11:23 -0700379 ClipPathOp* op = new (handler.allocator()) ClipPathOp(
380 properties().getClippingPath(), properties().getClippingPathOp());
John Reckd0a0b2a2014-03-20 16:28:56 -0700381 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700382 }
383}
384
385/**
386 * Apply property-based transformations to input matrix
387 *
388 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
389 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
390 */
391void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700392 if (properties().getLeft() != 0 || properties().getTop() != 0) {
393 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700394 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700395 if (properties().getStaticMatrix()) {
396 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700397 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700398 } else if (properties().getAnimationMatrix()) {
399 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700400 matrix.multiply(anim);
401 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700402
Chris Craikcc39e162014-04-25 18:34:11 -0700403 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700404 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700405 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700406 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700407 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700408 } else {
409 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700410 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700411 } else {
412 mat4 true3dMat;
413 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700414 properties().getPivotX() + properties().getTranslationX(),
415 properties().getPivotY() + properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700416 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700417 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
418 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
419 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
420 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
421 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700422
423 matrix.multiply(true3dMat);
424 }
425 }
426 }
427}
428
429/**
430 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
431 *
432 * This should be called before a call to defer() or drawDisplayList()
433 *
434 * Each DisplayList that serves as a 3d root builds its list of composited children,
435 * which are flagged to not draw in the standard draw loop.
436 */
437void RenderNode::computeOrdering() {
438 ATRACE_CALL();
439 mProjectedNodes.clear();
440
441 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
442 // transform properties are applied correctly to top level children
443 if (mDisplayListData == NULL) return;
John Reck087bc0c2014-04-04 16:20:08 -0700444 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700445 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
446 childOp->mRenderNode->computeOrderingImpl(childOp,
Chris Craik3f0854292014-04-15 16:18:08 -0700447 properties().getOutline().getPath(), &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700448 }
449}
450
451void RenderNode::computeOrderingImpl(
Chris Craika7090e02014-06-20 16:01:00 -0700452 DrawRenderNodeOp* opState,
Chris Craik3f0854292014-04-15 16:18:08 -0700453 const SkPath* outlineOfProjectionSurface,
Chris Craika7090e02014-06-20 16:01:00 -0700454 Vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700455 const mat4* transformFromProjectionSurface) {
456 mProjectedNodes.clear();
457 if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
458
459 // TODO: should avoid this calculation in most cases
460 // TODO: just calculate single matrix, down to all leaf composited elements
461 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
462 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
463
John Reckd0a0b2a2014-03-20 16:28:56 -0700464 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700465 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
466 opState->mSkipInOrderDraw = true;
467 opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
468 compositedChildrenOfProjectionSurface->add(opState);
469 } else {
470 // standard in order draw
471 opState->mSkipInOrderDraw = false;
472 }
473
John Reck087bc0c2014-04-04 16:20:08 -0700474 if (mDisplayListData->children().size() > 0) {
John Reck113e0822014-03-18 09:22:59 -0700475 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
476 bool haveAppliedPropertiesToProjection = false;
John Reck087bc0c2014-04-04 16:20:08 -0700477 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700478 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
479 RenderNode* child = childOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700480
Chris Craik3f0854292014-04-15 16:18:08 -0700481 const SkPath* projectionOutline = NULL;
Chris Craika7090e02014-06-20 16:01:00 -0700482 Vector<DrawRenderNodeOp*>* projectionChildren = NULL;
John Reck113e0822014-03-18 09:22:59 -0700483 const mat4* projectionTransform = NULL;
John Reckd0a0b2a2014-03-20 16:28:56 -0700484 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700485 // if receiving projections, collect projecting descendent
486
487 // Note that if a direct descendent is projecting backwards, we pass it's
488 // grandparent projection collection, since it shouldn't project onto it's
489 // parent, where it will already be drawing.
Chris Craik3f0854292014-04-15 16:18:08 -0700490 projectionOutline = properties().getOutline().getPath();
John Reck113e0822014-03-18 09:22:59 -0700491 projectionChildren = &mProjectedNodes;
492 projectionTransform = &mat4::identity();
493 } else {
494 if (!haveAppliedPropertiesToProjection) {
495 applyViewPropertyTransforms(localTransformFromProjectionSurface);
496 haveAppliedPropertiesToProjection = true;
497 }
Chris Craik3f0854292014-04-15 16:18:08 -0700498 projectionOutline = outlineOfProjectionSurface;
John Reck113e0822014-03-18 09:22:59 -0700499 projectionChildren = compositedChildrenOfProjectionSurface;
500 projectionTransform = &localTransformFromProjectionSurface;
501 }
Chris Craik3f0854292014-04-15 16:18:08 -0700502 child->computeOrderingImpl(childOp,
503 projectionOutline, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700504 }
505 }
John Reck113e0822014-03-18 09:22:59 -0700506}
507
508class DeferOperationHandler {
509public:
510 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
511 : mDeferStruct(deferStruct), mLevel(level) {}
512 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
513 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
514 }
515 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700516 inline void startMark(const char* name) {} // do nothing
517 inline void endMark() {}
518 inline int level() { return mLevel; }
519 inline int replayFlags() { return mDeferStruct.mReplayFlags; }
John Reck113e0822014-03-18 09:22:59 -0700520
521private:
522 DeferStateStruct& mDeferStruct;
523 const int mLevel;
524};
525
Chris Craik80d49022014-06-20 15:03:43 -0700526void RenderNode::defer(DeferStateStruct& deferStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700527 DeferOperationHandler handler(deferStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700528 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700529}
530
531class ReplayOperationHandler {
532public:
533 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
534 : mReplayStruct(replayStruct), mLevel(level) {}
535 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
536#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
Chris Craik3f0854292014-04-15 16:18:08 -0700537 mReplayStruct.mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700538#endif
539 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
540 }
541 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700542 inline void startMark(const char* name) {
543 mReplayStruct.mRenderer.startMark(name);
544 }
545 inline void endMark() {
546 mReplayStruct.mRenderer.endMark();
Chris Craikb265e2c2014-03-27 15:50:09 -0700547 }
548 inline int level() { return mLevel; }
549 inline int replayFlags() { return mReplayStruct.mReplayFlags; }
John Reck113e0822014-03-18 09:22:59 -0700550
551private:
552 ReplayStateStruct& mReplayStruct;
553 const int mLevel;
554};
555
Chris Craik80d49022014-06-20 15:03:43 -0700556void RenderNode::replay(ReplayStateStruct& replayStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700557 ReplayOperationHandler handler(replayStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700558 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700559}
560
Chris Craika7090e02014-06-20 16:01:00 -0700561void RenderNode::buildZSortedChildList(Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes) {
John Reck087bc0c2014-04-04 16:20:08 -0700562 if (mDisplayListData == NULL || mDisplayListData->children().size() == 0) return;
John Reck113e0822014-03-18 09:22:59 -0700563
John Reck087bc0c2014-04-04 16:20:08 -0700564 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700565 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
566 RenderNode* child = childOp->mRenderNode;
Chris Craikcc39e162014-04-25 18:34:11 -0700567 float childZ = child->properties().getZ();
John Reck113e0822014-03-18 09:22:59 -0700568
Chris Craike0bb87d2014-04-22 17:55:41 -0700569 if (!MathUtils::isZero(childZ)) {
Chris Craika7090e02014-06-20 16:01:00 -0700570 zTranslatedNodes.add(ZDrawRenderNodeOpPair(childZ, childOp));
John Reck113e0822014-03-18 09:22:59 -0700571 childOp->mSkipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700572 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700573 // regular, in order drawing DisplayList
574 childOp->mSkipInOrderDraw = false;
575 }
576 }
577
578 // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order)
579 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
580}
581
Chris Craikb265e2c2014-03-27 15:50:09 -0700582template <class T>
583void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
Chris Craik61317322014-05-21 13:03:52 -0700584 if (properties().getAlpha() <= 0.0f || properties().getOutline().isEmpty()) return;
Chris Craikb265e2c2014-03-27 15:50:09 -0700585
586 mat4 shadowMatrixXY(transformFromParent);
587 applyViewPropertyTransforms(shadowMatrixXY);
588
589 // Z matrix needs actual 3d transformation, so mapped z values will be correct
590 mat4 shadowMatrixZ(transformFromParent);
591 applyViewPropertyTransforms(shadowMatrixZ, true);
592
593 const SkPath* outlinePath = properties().getOutline().getPath();
594 const RevealClip& revealClip = properties().getRevealClip();
595 const SkPath* revealClipPath = revealClip.hasConvexClip()
596 ? revealClip.getPath() : NULL; // only pass the reveal clip's path if it's convex
597
Chris Craik61317322014-05-21 13:03:52 -0700598 if (revealClipPath && revealClipPath->isEmpty()) return;
599
Chris Craikb265e2c2014-03-27 15:50:09 -0700600 /**
601 * The drawing area of the caster is always the same as the its perimeter (which
602 * the shadow system uses) *except* in the inverse clip case. Inform the shadow
603 * system that the caster's drawing area (as opposed to its perimeter) has been
604 * clipped, so that it knows the caster can't be opaque.
605 */
606 bool casterUnclipped = !revealClip.willClip() || revealClip.hasConvexClip();
607
608 DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp(
609 shadowMatrixXY, shadowMatrixZ,
610 properties().getAlpha(), casterUnclipped,
Chris Craikb265e2c2014-03-27 15:50:09 -0700611 outlinePath, revealClipPath);
612 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
613}
614
Chris Craik80d49022014-06-20 15:03:43 -0700615template <class T>
616int RenderNode::issueOperationsOfNegZChildren(
Chris Craika7090e02014-06-20 16:01:00 -0700617 const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craik80d49022014-06-20 15:03:43 -0700618 OpenGLRenderer& renderer, T& handler) {
619 if (zTranslatedNodes.isEmpty()) return -1;
620
621 // create a save around the body of the ViewGroup's draw method, so that
622 // matrix/clip methods don't affect composited children
623 int shadowSaveCount = renderer.getSaveCount();
624 handler(new (handler.allocator()) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
625 PROPERTY_SAVECOUNT, properties().getClipToBounds());
626
627 issueOperationsOf3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler);
628 return shadowSaveCount;
629}
630
631template <class T>
632void RenderNode::issueOperationsOfPosZChildren(int shadowRestoreTo,
Chris Craika7090e02014-06-20 16:01:00 -0700633 const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craik80d49022014-06-20 15:03:43 -0700634 OpenGLRenderer& renderer, T& handler) {
635 if (zTranslatedNodes.isEmpty()) return;
636
637 LOG_ALWAYS_FATAL_IF(shadowRestoreTo < 0, "invalid save to restore to");
638 handler(new (handler.allocator()) RestoreToCountOp(shadowRestoreTo),
639 PROPERTY_SAVECOUNT, properties().getClipToBounds());
640 renderer.setOverrideLayerAlpha(1.0f);
641
642 issueOperationsOf3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler);
643}
644
John Reck113e0822014-03-18 09:22:59 -0700645#define SHADOW_DELTA 0.1f
646
647template <class T>
Chris Craika7090e02014-06-20 16:01:00 -0700648void RenderNode::issueOperationsOf3dChildren(const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
John Reck113e0822014-03-18 09:22:59 -0700649 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) {
650 const int size = zTranslatedNodes.size();
651 if (size == 0
652 || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
653 || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
654 // no 3d children to draw
655 return;
656 }
657
John Reck113e0822014-03-18 09:22:59 -0700658 /**
659 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
660 * with very similar Z heights to draw together.
661 *
662 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
663 * underneath both, and neither's shadow is drawn on top of the other.
664 */
665 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
666 size_t drawIndex, shadowIndex, endIndex;
667 if (mode == kNegativeZChildren) {
668 drawIndex = 0;
669 endIndex = nonNegativeIndex;
670 shadowIndex = endIndex; // draw no shadows
671 } else {
672 drawIndex = nonNegativeIndex;
673 endIndex = size;
674 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
675 }
Chris Craik3f0854292014-04-15 16:18:08 -0700676
677 DISPLAY_LIST_LOGD("%*s%d %s 3d children:", (handler.level() + 1) * 2, "",
678 endIndex - drawIndex, mode == kNegativeZChildren ? "negative" : "positive");
679
John Reck113e0822014-03-18 09:22:59 -0700680 float lastCasterZ = 0.0f;
681 while (shadowIndex < endIndex || drawIndex < endIndex) {
682 if (shadowIndex < endIndex) {
Chris Craika7090e02014-06-20 16:01:00 -0700683 DrawRenderNodeOp* casterOp = zTranslatedNodes[shadowIndex].value;
684 RenderNode* caster = casterOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700685 const float casterZ = zTranslatedNodes[shadowIndex].key;
686 // attempt to render the shadow if the caster about to be drawn is its caster,
687 // OR if its caster's Z value is similar to the previous potential caster
688 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700689 caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
John Reck113e0822014-03-18 09:22:59 -0700690
691 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
692 shadowIndex++;
693 continue;
694 }
695 }
696
697 // only the actual child DL draw needs to be in save/restore,
698 // since it modifies the renderer's matrix
699 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
700
Chris Craika7090e02014-06-20 16:01:00 -0700701 DrawRenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
702 RenderNode* child = childOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700703
704 renderer.concatMatrix(childOp->mTransformFromParent);
705 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700706 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700707 childOp->mSkipInOrderDraw = true;
708
709 renderer.restoreToCount(restoreTo);
710 drawIndex++;
711 }
John Reck113e0822014-03-18 09:22:59 -0700712}
713
714template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700715void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
Chris Craik3f0854292014-04-15 16:18:08 -0700716 DISPLAY_LIST_LOGD("%*s%d projected children:", (handler.level() + 1) * 2, "", mProjectedNodes.size());
717 const SkPath* projectionReceiverOutline = properties().getOutline().getPath();
Chris Craik3f0854292014-04-15 16:18:08 -0700718 int restoreTo = renderer.getSaveCount();
719
720 // If the projection reciever has an outline, we mask each of the projected rendernodes to it
721 // Either with clipRect, or special saveLayer masking
722 LinearAllocator& alloc = handler.allocator();
723 if (projectionReceiverOutline != NULL) {
724 const SkRect& outlineBounds = projectionReceiverOutline->getBounds();
725 if (projectionReceiverOutline->isRect(NULL)) {
726 // mask to the rect outline simply with clipRect
727 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
728 PROPERTY_SAVECOUNT, properties().getClipToBounds());
729 ClipRectOp* clipOp = new (alloc) ClipRectOp(
730 outlineBounds.left(), outlineBounds.top(),
731 outlineBounds.right(), outlineBounds.bottom(), SkRegion::kIntersect_Op);
732 handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
733 } else {
734 // wrap the projected RenderNodes with a SaveLayer that will mask to the outline
735 SaveLayerOp* op = new (alloc) SaveLayerOp(
736 outlineBounds.left(), outlineBounds.top(),
737 outlineBounds.right(), outlineBounds.bottom(),
Chris Craik80d49022014-06-20 15:03:43 -0700738 255, SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag | SkCanvas::kARGB_ClipLayer_SaveFlag);
Chris Craik3f0854292014-04-15 16:18:08 -0700739 op->setMask(projectionReceiverOutline);
740 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
741
742 /* TODO: add optimizations here to take advantage of placement/size of projected
743 * children (which may shrink saveLayer area significantly). This is dependent on
744 * passing actual drawing/dirtying bounds of projected content down to native.
745 */
746 }
747 }
748
749 // draw projected nodes
John Reck113e0822014-03-18 09:22:59 -0700750 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700751 DrawRenderNodeOp* childOp = mProjectedNodes[i];
John Reck113e0822014-03-18 09:22:59 -0700752
753 // matrix save, concat, and restore can be done safely without allocating operations
754 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
755 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
756 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700757 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700758 childOp->mSkipInOrderDraw = true;
759 renderer.restoreToCount(restoreTo);
760 }
Chris Craik3f0854292014-04-15 16:18:08 -0700761
762 if (projectionReceiverOutline != NULL) {
763 handler(new (alloc) RestoreToCountOp(restoreTo),
764 PROPERTY_SAVECOUNT, properties().getClipToBounds());
765 }
John Reck113e0822014-03-18 09:22:59 -0700766}
767
768/**
769 * This function serves both defer and replay modes, and will organize the displayList's component
770 * operations for a single frame:
771 *
772 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
773 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
774 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
775 * defer vs replay logic, per operation
776 */
777template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700778void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
John Reck25fbb3f2014-06-12 13:46:45 -0700779 const bool drawLayer = (mLayer && (&renderer != mLayer->renderer));
780 // If we are updating the contents of mLayer, we don't want to apply any of
781 // the RenderNode's properties to this issueOperations pass. Those will all
782 // be applied when the layer is drawn, aka when this is true.
783 const bool useViewProperties = (!mLayer || drawLayer);
784
Chris Craikb265e2c2014-03-27 15:50:09 -0700785 const int level = handler.level();
John Reck25fbb3f2014-06-12 13:46:45 -0700786 if (mDisplayListData->isEmpty() || (useViewProperties && properties().getAlpha() <= 0)) {
Chris Craik3f0854292014-04-15 16:18:08 -0700787 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, getName());
John Reck113e0822014-03-18 09:22:59 -0700788 return;
789 }
790
Chris Craik3f0854292014-04-15 16:18:08 -0700791 handler.startMark(getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700792
John Reck113e0822014-03-18 09:22:59 -0700793#if DEBUG_DISPLAY_LIST
Chris Craik3f0854292014-04-15 16:18:08 -0700794 const Rect& clipRect = renderer.getLocalClipBounds();
795 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), localClipBounds: %.0f, %.0f, %.0f, %.0f",
796 level * 2, "", this, getName(),
797 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
John Reck113e0822014-03-18 09:22:59 -0700798#endif
799
800 LinearAllocator& alloc = handler.allocator();
801 int restoreTo = renderer.getSaveCount();
802 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
John Reckd0a0b2a2014-03-20 16:28:56 -0700803 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700804
805 DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
806 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
807
John Reck25fbb3f2014-06-12 13:46:45 -0700808 if (useViewProperties) {
809 setViewProperties<T>(renderer, handler);
810 }
John Reck113e0822014-03-18 09:22:59 -0700811
Chris Craik8c271ca2014-03-25 10:33:01 -0700812 bool quickRejected = properties().getClipToBounds()
813 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -0700814 if (!quickRejected) {
Chris Craikdeeda3d2014-05-05 19:09:33 -0700815 if (mProperties.getOutline().willClip()) {
816 renderer.setClippingOutline(alloc, &(mProperties.getOutline()));
817 }
818
John Reck25fbb3f2014-06-12 13:46:45 -0700819 if (drawLayer) {
820 handler(new (alloc) DrawLayerOp(mLayer, 0, 0),
821 renderer.getSaveCount() - 1, properties().getClipToBounds());
822 } else {
Chris Craika7090e02014-06-20 16:01:00 -0700823 Vector<ZDrawRenderNodeOpPair> zTranslatedNodes;
John Reck25fbb3f2014-06-12 13:46:45 -0700824 buildZSortedChildList(zTranslatedNodes);
John Reck113e0822014-03-18 09:22:59 -0700825
John Reck25fbb3f2014-06-12 13:46:45 -0700826 // for 3d root, draw children with negative z values
Chris Craik80d49022014-06-20 15:03:43 -0700827 int shadowRestoreTo = issueOperationsOfNegZChildren(zTranslatedNodes, renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700828
John Reck25fbb3f2014-06-12 13:46:45 -0700829 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
830 const int saveCountOffset = renderer.getSaveCount() - 1;
831 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
832 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
833 DisplayListOp *op = mDisplayListData->displayListOps[i];
John Reck113e0822014-03-18 09:22:59 -0700834
Chris Craik80d49022014-06-20 15:03:43 -0700835#if DEBUG_DISPLAY_LIST
John Reck25fbb3f2014-06-12 13:46:45 -0700836 op->output(level + 1);
Chris Craik80d49022014-06-20 15:03:43 -0700837#endif
John Reck25fbb3f2014-06-12 13:46:45 -0700838 logBuffer.writeCommand(level, op->name());
839 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700840
John Reck25fbb3f2014-06-12 13:46:45 -0700841 if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) {
842 issueOperationsOfProjectedChildren(renderer, handler);
843 }
John Reck113e0822014-03-18 09:22:59 -0700844 }
John Reck113e0822014-03-18 09:22:59 -0700845
John Reck25fbb3f2014-06-12 13:46:45 -0700846 // for 3d root, draw children with positive z values
Chris Craik80d49022014-06-20 15:03:43 -0700847 issueOperationsOfPosZChildren(shadowRestoreTo, zTranslatedNodes, renderer, handler);
John Reck25fbb3f2014-06-12 13:46:45 -0700848 }
John Reck113e0822014-03-18 09:22:59 -0700849 }
850
851 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
852 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -0700853 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700854 renderer.setOverrideLayerAlpha(1.0f);
Chris Craikb265e2c2014-03-27 15:50:09 -0700855
Chris Craik3f0854292014-04-15 16:18:08 -0700856 DISPLAY_LIST_LOGD("%*sDone (%p, %s)", level * 2, "", this, getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700857 handler.endMark();
John Reck113e0822014-03-18 09:22:59 -0700858}
859
860} /* namespace uirenderer */
861} /* namespace android */