blob: fa1b21d01adbc7c705eeb8da81193565c374192c [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) {
John Reck09d5cdd2014-07-24 10:36:08 -070081 Caches::getInstance().registerFunctors(mStagingDisplayListData->functors.size());
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 Reck09d5cdd2014-07-24 10:36:08 -0700257 if (mDisplayListData) {
258 for (size_t i = 0; i < mDisplayListData->functors.size(); i++) {
259 (*mDisplayListData->functors[i])(DrawGlInfo::kModeSync, NULL);
260 }
261 }
John Recke4267ea2014-06-03 15:53:15 -0700262 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700263 }
John Reck8de65a82014-04-09 15:23:38 -0700264}
265
John Reckdcba6722014-07-08 13:59:49 -0700266void RenderNode::deleteDisplayListData() {
267 if (mDisplayListData) {
268 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
269 mDisplayListData->children()[i]->mRenderNode->decParentRefCount();
270 }
271 }
272 delete mDisplayListData;
273 mDisplayListData = NULL;
274}
275
John Reckf4198b72014-04-09 17:00:04 -0700276void RenderNode::prepareSubTree(TreeInfo& info, DisplayListData* subtree) {
John Reck8de65a82014-04-09 15:23:38 -0700277 if (subtree) {
John Reck860d1552014-04-11 19:15:05 -0700278 TextureCache& cache = Caches::getInstance().textureCache;
John Reck09d5cdd2014-07-24 10:36:08 -0700279 info.out.hasFunctors |= subtree->functors.size();
John Reck860d1552014-04-11 19:15:05 -0700280 // TODO: Fix ownedBitmapResources to not require disabling prepareTextures
281 // and thus falling out of async drawing path.
282 if (subtree->ownedBitmapResources.size()) {
283 info.prepareTextures = false;
284 }
285 for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) {
286 info.prepareTextures = cache.prefetchAndMarkInUse(subtree->bitmapResources[i]);
John Reckf4198b72014-04-09 17:00:04 -0700287 }
John Reck8de65a82014-04-09 15:23:38 -0700288 for (size_t i = 0; i < subtree->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700289 DrawRenderNodeOp* op = subtree->children()[i];
290 RenderNode* childNode = op->mRenderNode;
John Recka447d292014-06-11 18:39:44 -0700291 info.damageAccumulator->pushTransform(&op->mTransformFromParent);
John Reckf4198b72014-04-09 17:00:04 -0700292 childNode->prepareTreeImpl(info);
John Recka447d292014-06-11 18:39:44 -0700293 info.damageAccumulator->popTransform();
John Reck5bf11bb2014-03-25 10:22:09 -0700294 }
John Reck113e0822014-03-18 09:22:59 -0700295 }
296}
297
John Reckdcba6722014-07-08 13:59:49 -0700298void RenderNode::destroyHardwareResources() {
299 if (mLayer) {
300 LayerRenderer::destroyLayer(mLayer);
301 mLayer = NULL;
302 }
303 if (mDisplayListData) {
304 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
305 mDisplayListData->children()[i]->mRenderNode->destroyHardwareResources();
306 }
307 if (mNeedsDisplayListDataSync) {
308 // Next prepare tree we are going to push a new display list, so we can
309 // drop our current one now
310 deleteDisplayListData();
311 }
312 }
313}
314
315void RenderNode::decParentRefCount() {
316 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
317 mParentCount--;
318 if (!mParentCount) {
319 // If a child of ours is being attached to our parent then this will incorrectly
320 // destroy its hardware resources. However, this situation is highly unlikely
321 // and the failure is "just" that the layer is re-created, so this should
322 // be safe enough
323 destroyHardwareResources();
324 }
325}
326
John Reck113e0822014-03-18 09:22:59 -0700327/*
328 * For property operations, we pass a savecount of 0, since the operations aren't part of the
329 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
John Reckd0a0b2a2014-03-20 16:28:56 -0700330 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
John Reck113e0822014-03-18 09:22:59 -0700331 */
332#define PROPERTY_SAVECOUNT 0
333
334template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700335void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700336#if DEBUG_DISPLAY_LIST
Chris Craikb265e2c2014-03-27 15:50:09 -0700337 properties().debugOutputProperties(handler.level() + 1);
John Reck113e0822014-03-18 09:22:59 -0700338#endif
John Reckd0a0b2a2014-03-20 16:28:56 -0700339 if (properties().getLeft() != 0 || properties().getTop() != 0) {
340 renderer.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700341 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700342 if (properties().getStaticMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500343 renderer.concatMatrix(*properties().getStaticMatrix());
John Reckd0a0b2a2014-03-20 16:28:56 -0700344 } else if (properties().getAnimationMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500345 renderer.concatMatrix(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700346 }
John Reckf7483e32014-04-11 08:54:47 -0700347 if (properties().hasTransformMatrix()) {
348 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700349 renderer.translate(properties().getTranslationX(), properties().getTranslationY());
John Reck113e0822014-03-18 09:22:59 -0700350 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700351 renderer.concatMatrix(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700352 }
353 }
John Reck25fbb3f2014-06-12 13:46:45 -0700354 const bool isLayer = properties().layerProperties().type() != kLayerTypeNone;
Chris Craika753f4c2014-07-24 12:39:17 -0700355 int clipFlags = properties().getClippingFlags();
John Reckd0a0b2a2014-03-20 16:28:56 -0700356 if (properties().getAlpha() < 1) {
John Reck25fbb3f2014-06-12 13:46:45 -0700357 if (isLayer) {
Chris Craika753f4c2014-07-24 12:39:17 -0700358 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
359
John Reckd0a0b2a2014-03-20 16:28:56 -0700360 renderer.setOverrideLayerAlpha(properties().getAlpha());
361 } else if (!properties().getHasOverlappingRendering()) {
362 renderer.scaleAlpha(properties().getAlpha());
John Reck113e0822014-03-18 09:22:59 -0700363 } else {
Chris Craika753f4c2014-07-24 12:39:17 -0700364 Rect layerBounds(0, 0, getWidth(), getHeight());
John Reck113e0822014-03-18 09:22:59 -0700365 int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
Chris Craika753f4c2014-07-24 12:39:17 -0700366 if (clipFlags) {
John Reck113e0822014-03-18 09:22:59 -0700367 saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
Chris Craika753f4c2014-07-24 12:39:17 -0700368 properties().getClippingRectForFlags(clipFlags, &layerBounds);
369 clipFlags = 0; // all clipping done by saveLayer
John Reck113e0822014-03-18 09:22:59 -0700370 }
371
372 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
Chris Craika753f4c2014-07-24 12:39:17 -0700373 layerBounds.left, layerBounds.top, layerBounds.right, layerBounds.bottom,
Chris Craik8c271ca2014-03-25 10:33:01 -0700374 properties().getAlpha() * 255, saveFlags);
John Reckd0a0b2a2014-03-20 16:28:56 -0700375 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700376 }
377 }
Chris Craika753f4c2014-07-24 12:39:17 -0700378 if (clipFlags) {
379 Rect clipRect;
380 properties().getClippingRectForFlags(clipFlags, &clipRect);
Chris Craik8c271ca2014-03-25 10:33:01 -0700381 ClipRectOp* op = new (handler.allocator()) ClipRectOp(
Chris Craika753f4c2014-07-24 12:39:17 -0700382 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
383 SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700384 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700385 }
Chris Craik8c271ca2014-03-25 10:33:01 -0700386
Chris Craikaf4d04c2014-07-29 12:50:14 -0700387 // TODO: support both reveal clip and outline clip simultaneously
388 if (mProperties.getRevealClip().willClip()) {
389 Rect bounds;
390 mProperties.getRevealClip().getBounds(&bounds);
391 renderer.setClippingRoundRect(handler.allocator(), bounds, mProperties.getRevealClip().getRadius());
392 } else if (mProperties.getOutline().willClip()) {
393 renderer.setClippingOutline(handler.allocator(), &(mProperties.getOutline()));
John Reck113e0822014-03-18 09:22:59 -0700394 }
Chris Craikaf4d04c2014-07-29 12:50:14 -0700395
John Reck113e0822014-03-18 09:22:59 -0700396}
397
398/**
399 * Apply property-based transformations to input matrix
400 *
401 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
402 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
403 */
404void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700405 if (properties().getLeft() != 0 || properties().getTop() != 0) {
406 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700407 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700408 if (properties().getStaticMatrix()) {
409 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700410 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700411 } else if (properties().getAnimationMatrix()) {
412 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700413 matrix.multiply(anim);
414 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700415
Chris Craikcc39e162014-04-25 18:34:11 -0700416 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700417 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700418 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700419 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700420 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700421 } else {
422 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700423 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700424 } else {
425 mat4 true3dMat;
426 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700427 properties().getPivotX() + properties().getTranslationX(),
428 properties().getPivotY() + properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700429 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700430 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
431 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
432 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
433 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
434 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700435
436 matrix.multiply(true3dMat);
437 }
438 }
439 }
440}
441
442/**
443 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
444 *
445 * This should be called before a call to defer() or drawDisplayList()
446 *
447 * Each DisplayList that serves as a 3d root builds its list of composited children,
448 * which are flagged to not draw in the standard draw loop.
449 */
450void RenderNode::computeOrdering() {
451 ATRACE_CALL();
452 mProjectedNodes.clear();
453
454 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
455 // transform properties are applied correctly to top level children
456 if (mDisplayListData == NULL) return;
John Reck087bc0c2014-04-04 16:20:08 -0700457 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700458 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
459 childOp->mRenderNode->computeOrderingImpl(childOp,
Chris Craik3f0854292014-04-15 16:18:08 -0700460 properties().getOutline().getPath(), &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700461 }
462}
463
464void RenderNode::computeOrderingImpl(
Chris Craika7090e02014-06-20 16:01:00 -0700465 DrawRenderNodeOp* opState,
Chris Craik3f0854292014-04-15 16:18:08 -0700466 const SkPath* outlineOfProjectionSurface,
Chris Craika7090e02014-06-20 16:01:00 -0700467 Vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700468 const mat4* transformFromProjectionSurface) {
469 mProjectedNodes.clear();
470 if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
471
472 // TODO: should avoid this calculation in most cases
473 // TODO: just calculate single matrix, down to all leaf composited elements
474 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
475 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
476
John Reckd0a0b2a2014-03-20 16:28:56 -0700477 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700478 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
479 opState->mSkipInOrderDraw = true;
480 opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
481 compositedChildrenOfProjectionSurface->add(opState);
482 } else {
483 // standard in order draw
484 opState->mSkipInOrderDraw = false;
485 }
486
John Reck087bc0c2014-04-04 16:20:08 -0700487 if (mDisplayListData->children().size() > 0) {
John Reck113e0822014-03-18 09:22:59 -0700488 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
489 bool haveAppliedPropertiesToProjection = false;
John Reck087bc0c2014-04-04 16:20:08 -0700490 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700491 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
492 RenderNode* child = childOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700493
Chris Craik3f0854292014-04-15 16:18:08 -0700494 const SkPath* projectionOutline = NULL;
Chris Craika7090e02014-06-20 16:01:00 -0700495 Vector<DrawRenderNodeOp*>* projectionChildren = NULL;
John Reck113e0822014-03-18 09:22:59 -0700496 const mat4* projectionTransform = NULL;
John Reckd0a0b2a2014-03-20 16:28:56 -0700497 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700498 // if receiving projections, collect projecting descendent
499
500 // Note that if a direct descendent is projecting backwards, we pass it's
501 // grandparent projection collection, since it shouldn't project onto it's
502 // parent, where it will already be drawing.
Chris Craik3f0854292014-04-15 16:18:08 -0700503 projectionOutline = properties().getOutline().getPath();
John Reck113e0822014-03-18 09:22:59 -0700504 projectionChildren = &mProjectedNodes;
505 projectionTransform = &mat4::identity();
506 } else {
507 if (!haveAppliedPropertiesToProjection) {
508 applyViewPropertyTransforms(localTransformFromProjectionSurface);
509 haveAppliedPropertiesToProjection = true;
510 }
Chris Craik3f0854292014-04-15 16:18:08 -0700511 projectionOutline = outlineOfProjectionSurface;
John Reck113e0822014-03-18 09:22:59 -0700512 projectionChildren = compositedChildrenOfProjectionSurface;
513 projectionTransform = &localTransformFromProjectionSurface;
514 }
Chris Craik3f0854292014-04-15 16:18:08 -0700515 child->computeOrderingImpl(childOp,
516 projectionOutline, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700517 }
518 }
John Reck113e0822014-03-18 09:22:59 -0700519}
520
521class DeferOperationHandler {
522public:
523 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
524 : mDeferStruct(deferStruct), mLevel(level) {}
525 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
526 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
527 }
528 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700529 inline void startMark(const char* name) {} // do nothing
530 inline void endMark() {}
531 inline int level() { return mLevel; }
532 inline int replayFlags() { return mDeferStruct.mReplayFlags; }
John Reck113e0822014-03-18 09:22:59 -0700533
534private:
535 DeferStateStruct& mDeferStruct;
536 const int mLevel;
537};
538
Chris Craik80d49022014-06-20 15:03:43 -0700539void RenderNode::defer(DeferStateStruct& deferStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700540 DeferOperationHandler handler(deferStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700541 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700542}
543
544class ReplayOperationHandler {
545public:
546 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
547 : mReplayStruct(replayStruct), mLevel(level) {}
548 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
549#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
Chris Craik3f0854292014-04-15 16:18:08 -0700550 mReplayStruct.mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700551#endif
552 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
553 }
554 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700555 inline void startMark(const char* name) {
556 mReplayStruct.mRenderer.startMark(name);
557 }
558 inline void endMark() {
559 mReplayStruct.mRenderer.endMark();
Chris Craikb265e2c2014-03-27 15:50:09 -0700560 }
561 inline int level() { return mLevel; }
562 inline int replayFlags() { return mReplayStruct.mReplayFlags; }
John Reck113e0822014-03-18 09:22:59 -0700563
564private:
565 ReplayStateStruct& mReplayStruct;
566 const int mLevel;
567};
568
Chris Craik80d49022014-06-20 15:03:43 -0700569void RenderNode::replay(ReplayStateStruct& replayStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700570 ReplayOperationHandler handler(replayStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700571 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700572}
573
Chris Craika7090e02014-06-20 16:01:00 -0700574void RenderNode::buildZSortedChildList(Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes) {
John Reck087bc0c2014-04-04 16:20:08 -0700575 if (mDisplayListData == NULL || mDisplayListData->children().size() == 0) return;
John Reck113e0822014-03-18 09:22:59 -0700576
John Reck087bc0c2014-04-04 16:20:08 -0700577 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700578 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
579 RenderNode* child = childOp->mRenderNode;
Chris Craikcc39e162014-04-25 18:34:11 -0700580 float childZ = child->properties().getZ();
John Reck113e0822014-03-18 09:22:59 -0700581
Chris Craike0bb87d2014-04-22 17:55:41 -0700582 if (!MathUtils::isZero(childZ)) {
Chris Craika7090e02014-06-20 16:01:00 -0700583 zTranslatedNodes.add(ZDrawRenderNodeOpPair(childZ, childOp));
John Reck113e0822014-03-18 09:22:59 -0700584 childOp->mSkipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700585 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700586 // regular, in order drawing DisplayList
587 childOp->mSkipInOrderDraw = false;
588 }
589 }
590
591 // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order)
592 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
593}
594
Chris Craikb265e2c2014-03-27 15:50:09 -0700595template <class T>
596void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
Chris Craik77b5cad2014-07-30 18:23:07 -0700597 if (properties().getAlpha() <= 0.0f
598 || properties().getOutline().getAlpha() <= 0.0f
599 || !properties().getOutline().getPath()) {
600 // no shadow to draw
601 return;
602 }
Chris Craikb265e2c2014-03-27 15:50:09 -0700603
604 mat4 shadowMatrixXY(transformFromParent);
605 applyViewPropertyTransforms(shadowMatrixXY);
606
607 // Z matrix needs actual 3d transformation, so mapped z values will be correct
608 mat4 shadowMatrixZ(transformFromParent);
609 applyViewPropertyTransforms(shadowMatrixZ, true);
610
611 const SkPath* outlinePath = properties().getOutline().getPath();
Chris Craikaf4d04c2014-07-29 12:50:14 -0700612 const SkPath* revealClipPath = properties().getRevealClip().getPath();
Chris Craik61317322014-05-21 13:03:52 -0700613 if (revealClipPath && revealClipPath->isEmpty()) return;
614
Chris Craik77b5cad2014-07-30 18:23:07 -0700615 float casterAlpha = properties().getAlpha() * properties().getOutline().getAlpha();
Chris Craikb265e2c2014-03-27 15:50:09 -0700616 DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp(
Chris Craik77b5cad2014-07-30 18:23:07 -0700617 shadowMatrixXY, shadowMatrixZ, casterAlpha,
Chris Craikb265e2c2014-03-27 15:50:09 -0700618 outlinePath, revealClipPath);
619 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
620}
621
Chris Craik80d49022014-06-20 15:03:43 -0700622template <class T>
623int RenderNode::issueOperationsOfNegZChildren(
Chris Craika7090e02014-06-20 16:01:00 -0700624 const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craik80d49022014-06-20 15:03:43 -0700625 OpenGLRenderer& renderer, T& handler) {
626 if (zTranslatedNodes.isEmpty()) return -1;
627
628 // create a save around the body of the ViewGroup's draw method, so that
629 // matrix/clip methods don't affect composited children
630 int shadowSaveCount = renderer.getSaveCount();
631 handler(new (handler.allocator()) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
632 PROPERTY_SAVECOUNT, properties().getClipToBounds());
633
634 issueOperationsOf3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler);
635 return shadowSaveCount;
636}
637
638template <class T>
639void RenderNode::issueOperationsOfPosZChildren(int shadowRestoreTo,
Chris Craika7090e02014-06-20 16:01:00 -0700640 const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craik80d49022014-06-20 15:03:43 -0700641 OpenGLRenderer& renderer, T& handler) {
642 if (zTranslatedNodes.isEmpty()) return;
643
644 LOG_ALWAYS_FATAL_IF(shadowRestoreTo < 0, "invalid save to restore to");
645 handler(new (handler.allocator()) RestoreToCountOp(shadowRestoreTo),
646 PROPERTY_SAVECOUNT, properties().getClipToBounds());
647 renderer.setOverrideLayerAlpha(1.0f);
648
649 issueOperationsOf3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler);
650}
651
John Reck113e0822014-03-18 09:22:59 -0700652#define SHADOW_DELTA 0.1f
653
654template <class T>
Chris Craika7090e02014-06-20 16:01:00 -0700655void RenderNode::issueOperationsOf3dChildren(const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
John Reck113e0822014-03-18 09:22:59 -0700656 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) {
657 const int size = zTranslatedNodes.size();
658 if (size == 0
659 || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
660 || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
661 // no 3d children to draw
662 return;
663 }
664
John Reck113e0822014-03-18 09:22:59 -0700665 /**
666 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
667 * with very similar Z heights to draw together.
668 *
669 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
670 * underneath both, and neither's shadow is drawn on top of the other.
671 */
672 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
673 size_t drawIndex, shadowIndex, endIndex;
674 if (mode == kNegativeZChildren) {
675 drawIndex = 0;
676 endIndex = nonNegativeIndex;
677 shadowIndex = endIndex; // draw no shadows
678 } else {
679 drawIndex = nonNegativeIndex;
680 endIndex = size;
681 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
682 }
Chris Craik3f0854292014-04-15 16:18:08 -0700683
684 DISPLAY_LIST_LOGD("%*s%d %s 3d children:", (handler.level() + 1) * 2, "",
685 endIndex - drawIndex, mode == kNegativeZChildren ? "negative" : "positive");
686
John Reck113e0822014-03-18 09:22:59 -0700687 float lastCasterZ = 0.0f;
688 while (shadowIndex < endIndex || drawIndex < endIndex) {
689 if (shadowIndex < endIndex) {
Chris Craika7090e02014-06-20 16:01:00 -0700690 DrawRenderNodeOp* casterOp = zTranslatedNodes[shadowIndex].value;
691 RenderNode* caster = casterOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700692 const float casterZ = zTranslatedNodes[shadowIndex].key;
693 // attempt to render the shadow if the caster about to be drawn is its caster,
694 // OR if its caster's Z value is similar to the previous potential caster
695 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700696 caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
John Reck113e0822014-03-18 09:22:59 -0700697
698 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
699 shadowIndex++;
700 continue;
701 }
702 }
703
704 // only the actual child DL draw needs to be in save/restore,
705 // since it modifies the renderer's matrix
706 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
707
Chris Craika7090e02014-06-20 16:01:00 -0700708 DrawRenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
709 RenderNode* child = childOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700710
711 renderer.concatMatrix(childOp->mTransformFromParent);
712 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700713 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700714 childOp->mSkipInOrderDraw = true;
715
716 renderer.restoreToCount(restoreTo);
717 drawIndex++;
718 }
John Reck113e0822014-03-18 09:22:59 -0700719}
720
721template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700722void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
Chris Craik3f0854292014-04-15 16:18:08 -0700723 DISPLAY_LIST_LOGD("%*s%d projected children:", (handler.level() + 1) * 2, "", mProjectedNodes.size());
724 const SkPath* projectionReceiverOutline = properties().getOutline().getPath();
Chris Craik3f0854292014-04-15 16:18:08 -0700725 int restoreTo = renderer.getSaveCount();
726
727 // If the projection reciever has an outline, we mask each of the projected rendernodes to it
728 // Either with clipRect, or special saveLayer masking
729 LinearAllocator& alloc = handler.allocator();
730 if (projectionReceiverOutline != NULL) {
731 const SkRect& outlineBounds = projectionReceiverOutline->getBounds();
732 if (projectionReceiverOutline->isRect(NULL)) {
733 // mask to the rect outline simply with clipRect
734 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
735 PROPERTY_SAVECOUNT, properties().getClipToBounds());
736 ClipRectOp* clipOp = new (alloc) ClipRectOp(
737 outlineBounds.left(), outlineBounds.top(),
738 outlineBounds.right(), outlineBounds.bottom(), SkRegion::kIntersect_Op);
739 handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
740 } else {
741 // wrap the projected RenderNodes with a SaveLayer that will mask to the outline
742 SaveLayerOp* op = new (alloc) SaveLayerOp(
743 outlineBounds.left(), outlineBounds.top(),
744 outlineBounds.right(), outlineBounds.bottom(),
Chris Craik80d49022014-06-20 15:03:43 -0700745 255, SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag | SkCanvas::kARGB_ClipLayer_SaveFlag);
Chris Craik3f0854292014-04-15 16:18:08 -0700746 op->setMask(projectionReceiverOutline);
747 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
748
749 /* TODO: add optimizations here to take advantage of placement/size of projected
750 * children (which may shrink saveLayer area significantly). This is dependent on
751 * passing actual drawing/dirtying bounds of projected content down to native.
752 */
753 }
754 }
755
756 // draw projected nodes
John Reck113e0822014-03-18 09:22:59 -0700757 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700758 DrawRenderNodeOp* childOp = mProjectedNodes[i];
John Reck113e0822014-03-18 09:22:59 -0700759
760 // matrix save, concat, and restore can be done safely without allocating operations
761 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
762 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
763 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700764 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700765 childOp->mSkipInOrderDraw = true;
766 renderer.restoreToCount(restoreTo);
767 }
Chris Craik3f0854292014-04-15 16:18:08 -0700768
769 if (projectionReceiverOutline != NULL) {
770 handler(new (alloc) RestoreToCountOp(restoreTo),
771 PROPERTY_SAVECOUNT, properties().getClipToBounds());
772 }
John Reck113e0822014-03-18 09:22:59 -0700773}
774
775/**
776 * This function serves both defer and replay modes, and will organize the displayList's component
777 * operations for a single frame:
778 *
779 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
780 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
781 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
782 * defer vs replay logic, per operation
783 */
784template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700785void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
Chris Craik06451282014-07-21 10:25:54 -0700786 const int level = handler.level();
787 if (mDisplayListData->isEmpty()) {
788 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, getName());
789 return;
790 }
791
John Reck25fbb3f2014-06-12 13:46:45 -0700792 const bool drawLayer = (mLayer && (&renderer != mLayer->renderer));
793 // If we are updating the contents of mLayer, we don't want to apply any of
794 // the RenderNode's properties to this issueOperations pass. Those will all
795 // be applied when the layer is drawn, aka when this is true.
796 const bool useViewProperties = (!mLayer || drawLayer);
Chris Craik06451282014-07-21 10:25:54 -0700797 if (useViewProperties) {
798 const Outline& outline = properties().getOutline();
799 if (properties().getAlpha() <= 0 || (outline.getShouldClip() && outline.isEmpty())) {
800 DISPLAY_LIST_LOGD("%*sRejected display list (%p, %s)", level * 2, "", this, getName());
801 return;
802 }
John Reck113e0822014-03-18 09:22:59 -0700803 }
804
Chris Craik3f0854292014-04-15 16:18:08 -0700805 handler.startMark(getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700806
John Reck113e0822014-03-18 09:22:59 -0700807#if DEBUG_DISPLAY_LIST
Chris Craik3f0854292014-04-15 16:18:08 -0700808 const Rect& clipRect = renderer.getLocalClipBounds();
809 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), localClipBounds: %.0f, %.0f, %.0f, %.0f",
810 level * 2, "", this, getName(),
811 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
John Reck113e0822014-03-18 09:22:59 -0700812#endif
813
814 LinearAllocator& alloc = handler.allocator();
815 int restoreTo = renderer.getSaveCount();
816 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
John Reckd0a0b2a2014-03-20 16:28:56 -0700817 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700818
819 DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
820 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
821
John Reck25fbb3f2014-06-12 13:46:45 -0700822 if (useViewProperties) {
823 setViewProperties<T>(renderer, handler);
824 }
John Reck113e0822014-03-18 09:22:59 -0700825
Chris Craik8c271ca2014-03-25 10:33:01 -0700826 bool quickRejected = properties().getClipToBounds()
827 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -0700828 if (!quickRejected) {
John Reck25fbb3f2014-06-12 13:46:45 -0700829 if (drawLayer) {
830 handler(new (alloc) DrawLayerOp(mLayer, 0, 0),
831 renderer.getSaveCount() - 1, properties().getClipToBounds());
832 } else {
Chris Craika7090e02014-06-20 16:01:00 -0700833 Vector<ZDrawRenderNodeOpPair> zTranslatedNodes;
John Reck25fbb3f2014-06-12 13:46:45 -0700834 buildZSortedChildList(zTranslatedNodes);
John Reck113e0822014-03-18 09:22:59 -0700835
John Reck25fbb3f2014-06-12 13:46:45 -0700836 // for 3d root, draw children with negative z values
Chris Craik80d49022014-06-20 15:03:43 -0700837 int shadowRestoreTo = issueOperationsOfNegZChildren(zTranslatedNodes, renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700838
John Reck25fbb3f2014-06-12 13:46:45 -0700839 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
840 const int saveCountOffset = renderer.getSaveCount() - 1;
841 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
John Reck1aa5d2d2014-07-24 13:38:28 -0700842 const int size = static_cast<int>(mDisplayListData->displayListOps.size());
843 for (int i = 0; i < size; i++) {
John Reck25fbb3f2014-06-12 13:46:45 -0700844 DisplayListOp *op = mDisplayListData->displayListOps[i];
John Reck113e0822014-03-18 09:22:59 -0700845
Chris Craik80d49022014-06-20 15:03:43 -0700846#if DEBUG_DISPLAY_LIST
John Reck25fbb3f2014-06-12 13:46:45 -0700847 op->output(level + 1);
Chris Craik80d49022014-06-20 15:03:43 -0700848#endif
John Reck25fbb3f2014-06-12 13:46:45 -0700849 logBuffer.writeCommand(level, op->name());
850 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700851
John Reck25fbb3f2014-06-12 13:46:45 -0700852 if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) {
853 issueOperationsOfProjectedChildren(renderer, handler);
854 }
John Reck113e0822014-03-18 09:22:59 -0700855 }
John Reck113e0822014-03-18 09:22:59 -0700856
John Reck25fbb3f2014-06-12 13:46:45 -0700857 // for 3d root, draw children with positive z values
Chris Craik80d49022014-06-20 15:03:43 -0700858 issueOperationsOfPosZChildren(shadowRestoreTo, zTranslatedNodes, renderer, handler);
John Reck25fbb3f2014-06-12 13:46:45 -0700859 }
John Reck113e0822014-03-18 09:22:59 -0700860 }
861
862 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
863 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -0700864 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700865 renderer.setOverrideLayerAlpha(1.0f);
Chris Craikb265e2c2014-03-27 15:50:09 -0700866
Chris Craik3f0854292014-04-15 16:18:08 -0700867 DISPLAY_LIST_LOGD("%*sDone (%p, %s)", level * 2, "", this, getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700868 handler.endMark();
John Reck113e0822014-03-18 09:22:59 -0700869}
870
871} /* namespace uirenderer */
872} /* namespace android */