blob: 237d5006e874605e9df46cc56f8db39935a8ab9e [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 Reckc79eabc2014-08-05 11:03:42 -0700187
188 if (dirty.intersect(0, 0, getWidth(), getHeight())) {
189 dirty.roundOut();
John Reck25fbb3f2014-06-12 13:46:45 -0700190 mLayer->updateDeferred(this, dirty.fLeft, dirty.fTop, dirty.fRight, dirty.fBottom);
191 }
192 // This is not inside the above if because we may have called
193 // updateDeferred on a previous prepare pass that didn't have a renderer
194 if (info.renderer && mLayer->deferredUpdateScheduled) {
195 info.renderer->pushLayerUpdate(mLayer);
196 }
197}
198
John Recke4267ea2014-06-03 15:53:15 -0700199void RenderNode::prepareTreeImpl(TreeInfo& info) {
John Recka447d292014-06-11 18:39:44 -0700200 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700201
John Reckdcba6722014-07-08 13:59:49 -0700202 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700203 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700204 }
John Reckdcba6722014-07-08 13:59:49 -0700205 mAnimatorManager.animate(info);
John Reck25fbb3f2014-06-12 13:46:45 -0700206 prepareLayer(info);
John Reckdcba6722014-07-08 13:59:49 -0700207 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700208 pushStagingDisplayListChanges(info);
209 }
John Reckf4198b72014-04-09 17:00:04 -0700210 prepareSubTree(info, mDisplayListData);
John Reck25fbb3f2014-06-12 13:46:45 -0700211 pushLayerUpdate(info);
212
John Recka447d292014-06-11 18:39:44 -0700213 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700214}
215
John Reck25fbb3f2014-06-12 13:46:45 -0700216void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reckff941dc2014-05-14 16:34:14 -0700217 // Push the animators first so that setupStartValueIfNecessary() is called
218 // before properties() is trampled by stagingProperties(), as they are
219 // required by some animators.
John Reck68bfe0a2014-06-24 15:34:58 -0700220 mAnimatorManager.pushStaging(info);
John Reckff941dc2014-05-14 16:34:14 -0700221 if (mDirtyPropertyFields) {
222 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700223 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700224 info.damageAccumulator->popTransform();
John Reckff941dc2014-05-14 16:34:14 -0700225 mProperties = mStagingProperties;
John Reck25fbb3f2014-06-12 13:46:45 -0700226 applyLayerPropertiesToLayer(info);
John Recke4267ea2014-06-03 15:53:15 -0700227 // We could try to be clever and only re-damage if the matrix changed.
228 // However, we don't need to worry about that. The cost of over-damaging
229 // here is only going to be a single additional map rect of this node
230 // plus a rect join(). The parent's transform (and up) will only be
231 // performed once.
John Recka447d292014-06-11 18:39:44 -0700232 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700233 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700234 }
John Reck25fbb3f2014-06-12 13:46:45 -0700235}
236
237void RenderNode::applyLayerPropertiesToLayer(TreeInfo& info) {
238 if (CC_LIKELY(!mLayer)) return;
239
240 const LayerProperties& props = properties().layerProperties();
241 mLayer->setAlpha(props.alpha(), props.xferMode());
242 mLayer->setColorFilter(props.colorFilter());
243 mLayer->setBlend(props.needsBlending());
244}
245
246void RenderNode::pushStagingDisplayListChanges(TreeInfo& info) {
John Reck8de65a82014-04-09 15:23:38 -0700247 if (mNeedsDisplayListDataSync) {
248 mNeedsDisplayListDataSync = false;
John Reckdcba6722014-07-08 13:59:49 -0700249 // Make sure we inc first so that we don't fluctuate between 0 and 1,
250 // which would thrash the layer cache
251 if (mStagingDisplayListData) {
252 for (size_t i = 0; i < mStagingDisplayListData->children().size(); i++) {
253 mStagingDisplayListData->children()[i]->mRenderNode->incParentRefCount();
254 }
255 }
256 deleteDisplayListData();
John Reck8de65a82014-04-09 15:23:38 -0700257 mDisplayListData = mStagingDisplayListData;
John Reckdcba6722014-07-08 13:59:49 -0700258 mStagingDisplayListData = NULL;
John Reck09d5cdd2014-07-24 10:36:08 -0700259 if (mDisplayListData) {
260 for (size_t i = 0; i < mDisplayListData->functors.size(); i++) {
261 (*mDisplayListData->functors[i])(DrawGlInfo::kModeSync, NULL);
262 }
263 }
John Recke4267ea2014-06-03 15:53:15 -0700264 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700265 }
John Reck8de65a82014-04-09 15:23:38 -0700266}
267
John Reckdcba6722014-07-08 13:59:49 -0700268void RenderNode::deleteDisplayListData() {
269 if (mDisplayListData) {
270 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
271 mDisplayListData->children()[i]->mRenderNode->decParentRefCount();
272 }
273 }
274 delete mDisplayListData;
275 mDisplayListData = NULL;
276}
277
John Reckf4198b72014-04-09 17:00:04 -0700278void RenderNode::prepareSubTree(TreeInfo& info, DisplayListData* subtree) {
John Reck8de65a82014-04-09 15:23:38 -0700279 if (subtree) {
John Reck860d1552014-04-11 19:15:05 -0700280 TextureCache& cache = Caches::getInstance().textureCache;
John Reck09d5cdd2014-07-24 10:36:08 -0700281 info.out.hasFunctors |= subtree->functors.size();
John Reck860d1552014-04-11 19:15:05 -0700282 // TODO: Fix ownedBitmapResources to not require disabling prepareTextures
283 // and thus falling out of async drawing path.
284 if (subtree->ownedBitmapResources.size()) {
285 info.prepareTextures = false;
286 }
287 for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) {
288 info.prepareTextures = cache.prefetchAndMarkInUse(subtree->bitmapResources[i]);
John Reckf4198b72014-04-09 17:00:04 -0700289 }
John Reck8de65a82014-04-09 15:23:38 -0700290 for (size_t i = 0; i < subtree->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700291 DrawRenderNodeOp* op = subtree->children()[i];
292 RenderNode* childNode = op->mRenderNode;
John Recka447d292014-06-11 18:39:44 -0700293 info.damageAccumulator->pushTransform(&op->mTransformFromParent);
John Reckf4198b72014-04-09 17:00:04 -0700294 childNode->prepareTreeImpl(info);
John Recka447d292014-06-11 18:39:44 -0700295 info.damageAccumulator->popTransform();
John Reck5bf11bb2014-03-25 10:22:09 -0700296 }
John Reck113e0822014-03-18 09:22:59 -0700297 }
298}
299
John Reckdcba6722014-07-08 13:59:49 -0700300void RenderNode::destroyHardwareResources() {
301 if (mLayer) {
302 LayerRenderer::destroyLayer(mLayer);
303 mLayer = NULL;
304 }
305 if (mDisplayListData) {
306 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
307 mDisplayListData->children()[i]->mRenderNode->destroyHardwareResources();
308 }
309 if (mNeedsDisplayListDataSync) {
310 // Next prepare tree we are going to push a new display list, so we can
311 // drop our current one now
312 deleteDisplayListData();
313 }
314 }
315}
316
317void RenderNode::decParentRefCount() {
318 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
319 mParentCount--;
320 if (!mParentCount) {
321 // If a child of ours is being attached to our parent then this will incorrectly
322 // destroy its hardware resources. However, this situation is highly unlikely
323 // and the failure is "just" that the layer is re-created, so this should
324 // be safe enough
325 destroyHardwareResources();
326 }
327}
328
John Reck113e0822014-03-18 09:22:59 -0700329/*
330 * For property operations, we pass a savecount of 0, since the operations aren't part of the
331 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
John Reckd0a0b2a2014-03-20 16:28:56 -0700332 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
John Reck113e0822014-03-18 09:22:59 -0700333 */
334#define PROPERTY_SAVECOUNT 0
335
336template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700337void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700338#if DEBUG_DISPLAY_LIST
Chris Craikb265e2c2014-03-27 15:50:09 -0700339 properties().debugOutputProperties(handler.level() + 1);
John Reck113e0822014-03-18 09:22:59 -0700340#endif
John Reckd0a0b2a2014-03-20 16:28:56 -0700341 if (properties().getLeft() != 0 || properties().getTop() != 0) {
342 renderer.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700343 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700344 if (properties().getStaticMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500345 renderer.concatMatrix(*properties().getStaticMatrix());
John Reckd0a0b2a2014-03-20 16:28:56 -0700346 } else if (properties().getAnimationMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500347 renderer.concatMatrix(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700348 }
John Reckf7483e32014-04-11 08:54:47 -0700349 if (properties().hasTransformMatrix()) {
350 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700351 renderer.translate(properties().getTranslationX(), properties().getTranslationY());
John Reck113e0822014-03-18 09:22:59 -0700352 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700353 renderer.concatMatrix(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700354 }
355 }
John Reck25fbb3f2014-06-12 13:46:45 -0700356 const bool isLayer = properties().layerProperties().type() != kLayerTypeNone;
Chris Craika753f4c2014-07-24 12:39:17 -0700357 int clipFlags = properties().getClippingFlags();
John Reckd0a0b2a2014-03-20 16:28:56 -0700358 if (properties().getAlpha() < 1) {
John Reck25fbb3f2014-06-12 13:46:45 -0700359 if (isLayer) {
Chris Craika753f4c2014-07-24 12:39:17 -0700360 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
361
John Reckd0a0b2a2014-03-20 16:28:56 -0700362 renderer.setOverrideLayerAlpha(properties().getAlpha());
363 } else if (!properties().getHasOverlappingRendering()) {
364 renderer.scaleAlpha(properties().getAlpha());
John Reck113e0822014-03-18 09:22:59 -0700365 } else {
Chris Craika753f4c2014-07-24 12:39:17 -0700366 Rect layerBounds(0, 0, getWidth(), getHeight());
John Reck113e0822014-03-18 09:22:59 -0700367 int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
Chris Craika753f4c2014-07-24 12:39:17 -0700368 if (clipFlags) {
John Reck113e0822014-03-18 09:22:59 -0700369 saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
Chris Craika753f4c2014-07-24 12:39:17 -0700370 properties().getClippingRectForFlags(clipFlags, &layerBounds);
371 clipFlags = 0; // all clipping done by saveLayer
John Reck113e0822014-03-18 09:22:59 -0700372 }
373
374 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
Chris Craika753f4c2014-07-24 12:39:17 -0700375 layerBounds.left, layerBounds.top, layerBounds.right, layerBounds.bottom,
Chris Craik8c271ca2014-03-25 10:33:01 -0700376 properties().getAlpha() * 255, saveFlags);
John Reckd0a0b2a2014-03-20 16:28:56 -0700377 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700378 }
379 }
Chris Craika753f4c2014-07-24 12:39:17 -0700380 if (clipFlags) {
381 Rect clipRect;
382 properties().getClippingRectForFlags(clipFlags, &clipRect);
Chris Craik8c271ca2014-03-25 10:33:01 -0700383 ClipRectOp* op = new (handler.allocator()) ClipRectOp(
Chris Craika753f4c2014-07-24 12:39:17 -0700384 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
385 SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700386 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700387 }
Chris Craik8c271ca2014-03-25 10:33:01 -0700388
Chris Craikaf4d04c2014-07-29 12:50:14 -0700389 // TODO: support both reveal clip and outline clip simultaneously
390 if (mProperties.getRevealClip().willClip()) {
391 Rect bounds;
392 mProperties.getRevealClip().getBounds(&bounds);
393 renderer.setClippingRoundRect(handler.allocator(), bounds, mProperties.getRevealClip().getRadius());
394 } else if (mProperties.getOutline().willClip()) {
395 renderer.setClippingOutline(handler.allocator(), &(mProperties.getOutline()));
John Reck113e0822014-03-18 09:22:59 -0700396 }
Chris Craikaf4d04c2014-07-29 12:50:14 -0700397
John Reck113e0822014-03-18 09:22:59 -0700398}
399
400/**
401 * Apply property-based transformations to input matrix
402 *
403 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
404 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
405 */
406void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700407 if (properties().getLeft() != 0 || properties().getTop() != 0) {
408 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700409 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700410 if (properties().getStaticMatrix()) {
411 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700412 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700413 } else if (properties().getAnimationMatrix()) {
414 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700415 matrix.multiply(anim);
416 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700417
Chris Craikcc39e162014-04-25 18:34:11 -0700418 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700419 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700420 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700421 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700422 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700423 } else {
424 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700425 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700426 } else {
427 mat4 true3dMat;
428 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700429 properties().getPivotX() + properties().getTranslationX(),
430 properties().getPivotY() + properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700431 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700432 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
433 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
434 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
435 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
436 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700437
438 matrix.multiply(true3dMat);
439 }
440 }
441 }
442}
443
444/**
445 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
446 *
447 * This should be called before a call to defer() or drawDisplayList()
448 *
449 * Each DisplayList that serves as a 3d root builds its list of composited children,
450 * which are flagged to not draw in the standard draw loop.
451 */
452void RenderNode::computeOrdering() {
453 ATRACE_CALL();
454 mProjectedNodes.clear();
455
456 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
457 // transform properties are applied correctly to top level children
458 if (mDisplayListData == NULL) return;
John Reck087bc0c2014-04-04 16:20:08 -0700459 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700460 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
461 childOp->mRenderNode->computeOrderingImpl(childOp,
Chris Craik3f0854292014-04-15 16:18:08 -0700462 properties().getOutline().getPath(), &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700463 }
464}
465
466void RenderNode::computeOrderingImpl(
Chris Craika7090e02014-06-20 16:01:00 -0700467 DrawRenderNodeOp* opState,
Chris Craik3f0854292014-04-15 16:18:08 -0700468 const SkPath* outlineOfProjectionSurface,
Chris Craika7090e02014-06-20 16:01:00 -0700469 Vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700470 const mat4* transformFromProjectionSurface) {
471 mProjectedNodes.clear();
472 if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
473
474 // TODO: should avoid this calculation in most cases
475 // TODO: just calculate single matrix, down to all leaf composited elements
476 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
477 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
478
John Reckd0a0b2a2014-03-20 16:28:56 -0700479 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700480 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
481 opState->mSkipInOrderDraw = true;
482 opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
483 compositedChildrenOfProjectionSurface->add(opState);
484 } else {
485 // standard in order draw
486 opState->mSkipInOrderDraw = false;
487 }
488
John Reck087bc0c2014-04-04 16:20:08 -0700489 if (mDisplayListData->children().size() > 0) {
John Reck113e0822014-03-18 09:22:59 -0700490 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
491 bool haveAppliedPropertiesToProjection = false;
John Reck087bc0c2014-04-04 16:20:08 -0700492 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700493 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
494 RenderNode* child = childOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700495
Chris Craik3f0854292014-04-15 16:18:08 -0700496 const SkPath* projectionOutline = NULL;
Chris Craika7090e02014-06-20 16:01:00 -0700497 Vector<DrawRenderNodeOp*>* projectionChildren = NULL;
John Reck113e0822014-03-18 09:22:59 -0700498 const mat4* projectionTransform = NULL;
John Reckd0a0b2a2014-03-20 16:28:56 -0700499 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700500 // if receiving projections, collect projecting descendent
501
502 // Note that if a direct descendent is projecting backwards, we pass it's
503 // grandparent projection collection, since it shouldn't project onto it's
504 // parent, where it will already be drawing.
Chris Craik3f0854292014-04-15 16:18:08 -0700505 projectionOutline = properties().getOutline().getPath();
John Reck113e0822014-03-18 09:22:59 -0700506 projectionChildren = &mProjectedNodes;
507 projectionTransform = &mat4::identity();
508 } else {
509 if (!haveAppliedPropertiesToProjection) {
510 applyViewPropertyTransforms(localTransformFromProjectionSurface);
511 haveAppliedPropertiesToProjection = true;
512 }
Chris Craik3f0854292014-04-15 16:18:08 -0700513 projectionOutline = outlineOfProjectionSurface;
John Reck113e0822014-03-18 09:22:59 -0700514 projectionChildren = compositedChildrenOfProjectionSurface;
515 projectionTransform = &localTransformFromProjectionSurface;
516 }
Chris Craik3f0854292014-04-15 16:18:08 -0700517 child->computeOrderingImpl(childOp,
518 projectionOutline, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700519 }
520 }
John Reck113e0822014-03-18 09:22:59 -0700521}
522
523class DeferOperationHandler {
524public:
525 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
526 : mDeferStruct(deferStruct), mLevel(level) {}
527 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
528 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
529 }
530 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700531 inline void startMark(const char* name) {} // do nothing
532 inline void endMark() {}
533 inline int level() { return mLevel; }
534 inline int replayFlags() { return mDeferStruct.mReplayFlags; }
John Reck113e0822014-03-18 09:22:59 -0700535
536private:
537 DeferStateStruct& mDeferStruct;
538 const int mLevel;
539};
540
Chris Craik80d49022014-06-20 15:03:43 -0700541void RenderNode::defer(DeferStateStruct& deferStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700542 DeferOperationHandler handler(deferStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700543 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700544}
545
546class ReplayOperationHandler {
547public:
548 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
549 : mReplayStruct(replayStruct), mLevel(level) {}
550 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
551#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
Chris Craik3f0854292014-04-15 16:18:08 -0700552 mReplayStruct.mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700553#endif
554 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
555 }
556 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700557 inline void startMark(const char* name) {
558 mReplayStruct.mRenderer.startMark(name);
559 }
560 inline void endMark() {
561 mReplayStruct.mRenderer.endMark();
Chris Craikb265e2c2014-03-27 15:50:09 -0700562 }
563 inline int level() { return mLevel; }
564 inline int replayFlags() { return mReplayStruct.mReplayFlags; }
John Reck113e0822014-03-18 09:22:59 -0700565
566private:
567 ReplayStateStruct& mReplayStruct;
568 const int mLevel;
569};
570
Chris Craik80d49022014-06-20 15:03:43 -0700571void RenderNode::replay(ReplayStateStruct& replayStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700572 ReplayOperationHandler handler(replayStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700573 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700574}
575
Chris Craika7090e02014-06-20 16:01:00 -0700576void RenderNode::buildZSortedChildList(Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes) {
John Reck087bc0c2014-04-04 16:20:08 -0700577 if (mDisplayListData == NULL || mDisplayListData->children().size() == 0) return;
John Reck113e0822014-03-18 09:22:59 -0700578
John Reck087bc0c2014-04-04 16:20:08 -0700579 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700580 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
581 RenderNode* child = childOp->mRenderNode;
Chris Craikcc39e162014-04-25 18:34:11 -0700582 float childZ = child->properties().getZ();
John Reck113e0822014-03-18 09:22:59 -0700583
Chris Craike0bb87d2014-04-22 17:55:41 -0700584 if (!MathUtils::isZero(childZ)) {
Chris Craika7090e02014-06-20 16:01:00 -0700585 zTranslatedNodes.add(ZDrawRenderNodeOpPair(childZ, childOp));
John Reck113e0822014-03-18 09:22:59 -0700586 childOp->mSkipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700587 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700588 // regular, in order drawing DisplayList
589 childOp->mSkipInOrderDraw = false;
590 }
591 }
592
593 // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order)
594 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
595}
596
Chris Craikb265e2c2014-03-27 15:50:09 -0700597template <class T>
598void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
Chris Craik77b5cad2014-07-30 18:23:07 -0700599 if (properties().getAlpha() <= 0.0f
600 || properties().getOutline().getAlpha() <= 0.0f
601 || !properties().getOutline().getPath()) {
602 // no shadow to draw
603 return;
604 }
Chris Craikb265e2c2014-03-27 15:50:09 -0700605
606 mat4 shadowMatrixXY(transformFromParent);
607 applyViewPropertyTransforms(shadowMatrixXY);
608
609 // Z matrix needs actual 3d transformation, so mapped z values will be correct
610 mat4 shadowMatrixZ(transformFromParent);
611 applyViewPropertyTransforms(shadowMatrixZ, true);
612
613 const SkPath* outlinePath = properties().getOutline().getPath();
Chris Craikaf4d04c2014-07-29 12:50:14 -0700614 const SkPath* revealClipPath = properties().getRevealClip().getPath();
Chris Craik61317322014-05-21 13:03:52 -0700615 if (revealClipPath && revealClipPath->isEmpty()) return;
616
Chris Craik77b5cad2014-07-30 18:23:07 -0700617 float casterAlpha = properties().getAlpha() * properties().getOutline().getAlpha();
Chris Craikb265e2c2014-03-27 15:50:09 -0700618 DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp(
Chris Craik77b5cad2014-07-30 18:23:07 -0700619 shadowMatrixXY, shadowMatrixZ, casterAlpha,
Chris Craikb265e2c2014-03-27 15:50:09 -0700620 outlinePath, revealClipPath);
621 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
622}
623
Chris Craik80d49022014-06-20 15:03:43 -0700624template <class T>
625int RenderNode::issueOperationsOfNegZChildren(
Chris Craika7090e02014-06-20 16:01:00 -0700626 const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craik80d49022014-06-20 15:03:43 -0700627 OpenGLRenderer& renderer, T& handler) {
628 if (zTranslatedNodes.isEmpty()) return -1;
629
630 // create a save around the body of the ViewGroup's draw method, so that
631 // matrix/clip methods don't affect composited children
632 int shadowSaveCount = renderer.getSaveCount();
633 handler(new (handler.allocator()) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
634 PROPERTY_SAVECOUNT, properties().getClipToBounds());
635
636 issueOperationsOf3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler);
637 return shadowSaveCount;
638}
639
640template <class T>
641void RenderNode::issueOperationsOfPosZChildren(int shadowRestoreTo,
Chris Craika7090e02014-06-20 16:01:00 -0700642 const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craik80d49022014-06-20 15:03:43 -0700643 OpenGLRenderer& renderer, T& handler) {
644 if (zTranslatedNodes.isEmpty()) return;
645
646 LOG_ALWAYS_FATAL_IF(shadowRestoreTo < 0, "invalid save to restore to");
647 handler(new (handler.allocator()) RestoreToCountOp(shadowRestoreTo),
648 PROPERTY_SAVECOUNT, properties().getClipToBounds());
649 renderer.setOverrideLayerAlpha(1.0f);
650
651 issueOperationsOf3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler);
652}
653
John Reck113e0822014-03-18 09:22:59 -0700654#define SHADOW_DELTA 0.1f
655
656template <class T>
Chris Craika7090e02014-06-20 16:01:00 -0700657void RenderNode::issueOperationsOf3dChildren(const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
John Reck113e0822014-03-18 09:22:59 -0700658 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) {
659 const int size = zTranslatedNodes.size();
660 if (size == 0
661 || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
662 || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
663 // no 3d children to draw
664 return;
665 }
666
John Reck113e0822014-03-18 09:22:59 -0700667 /**
668 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
669 * with very similar Z heights to draw together.
670 *
671 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
672 * underneath both, and neither's shadow is drawn on top of the other.
673 */
674 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
675 size_t drawIndex, shadowIndex, endIndex;
676 if (mode == kNegativeZChildren) {
677 drawIndex = 0;
678 endIndex = nonNegativeIndex;
679 shadowIndex = endIndex; // draw no shadows
680 } else {
681 drawIndex = nonNegativeIndex;
682 endIndex = size;
683 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
684 }
Chris Craik3f0854292014-04-15 16:18:08 -0700685
686 DISPLAY_LIST_LOGD("%*s%d %s 3d children:", (handler.level() + 1) * 2, "",
687 endIndex - drawIndex, mode == kNegativeZChildren ? "negative" : "positive");
688
John Reck113e0822014-03-18 09:22:59 -0700689 float lastCasterZ = 0.0f;
690 while (shadowIndex < endIndex || drawIndex < endIndex) {
691 if (shadowIndex < endIndex) {
Chris Craika7090e02014-06-20 16:01:00 -0700692 DrawRenderNodeOp* casterOp = zTranslatedNodes[shadowIndex].value;
693 RenderNode* caster = casterOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700694 const float casterZ = zTranslatedNodes[shadowIndex].key;
695 // attempt to render the shadow if the caster about to be drawn is its caster,
696 // OR if its caster's Z value is similar to the previous potential caster
697 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700698 caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
John Reck113e0822014-03-18 09:22:59 -0700699
700 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
701 shadowIndex++;
702 continue;
703 }
704 }
705
706 // only the actual child DL draw needs to be in save/restore,
707 // since it modifies the renderer's matrix
708 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
709
Chris Craika7090e02014-06-20 16:01:00 -0700710 DrawRenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
711 RenderNode* child = childOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700712
713 renderer.concatMatrix(childOp->mTransformFromParent);
714 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700715 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700716 childOp->mSkipInOrderDraw = true;
717
718 renderer.restoreToCount(restoreTo);
719 drawIndex++;
720 }
John Reck113e0822014-03-18 09:22:59 -0700721}
722
723template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700724void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
Chris Craik3f0854292014-04-15 16:18:08 -0700725 DISPLAY_LIST_LOGD("%*s%d projected children:", (handler.level() + 1) * 2, "", mProjectedNodes.size());
726 const SkPath* projectionReceiverOutline = properties().getOutline().getPath();
Chris Craik3f0854292014-04-15 16:18:08 -0700727 int restoreTo = renderer.getSaveCount();
728
729 // If the projection reciever has an outline, we mask each of the projected rendernodes to it
730 // Either with clipRect, or special saveLayer masking
731 LinearAllocator& alloc = handler.allocator();
732 if (projectionReceiverOutline != NULL) {
733 const SkRect& outlineBounds = projectionReceiverOutline->getBounds();
734 if (projectionReceiverOutline->isRect(NULL)) {
735 // mask to the rect outline simply with clipRect
736 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
737 PROPERTY_SAVECOUNT, properties().getClipToBounds());
738 ClipRectOp* clipOp = new (alloc) ClipRectOp(
739 outlineBounds.left(), outlineBounds.top(),
740 outlineBounds.right(), outlineBounds.bottom(), SkRegion::kIntersect_Op);
741 handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
742 } else {
743 // wrap the projected RenderNodes with a SaveLayer that will mask to the outline
744 SaveLayerOp* op = new (alloc) SaveLayerOp(
745 outlineBounds.left(), outlineBounds.top(),
746 outlineBounds.right(), outlineBounds.bottom(),
Chris Craik80d49022014-06-20 15:03:43 -0700747 255, SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag | SkCanvas::kARGB_ClipLayer_SaveFlag);
Chris Craik3f0854292014-04-15 16:18:08 -0700748 op->setMask(projectionReceiverOutline);
749 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
750
751 /* TODO: add optimizations here to take advantage of placement/size of projected
752 * children (which may shrink saveLayer area significantly). This is dependent on
753 * passing actual drawing/dirtying bounds of projected content down to native.
754 */
755 }
756 }
757
758 // draw projected nodes
John Reck113e0822014-03-18 09:22:59 -0700759 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700760 DrawRenderNodeOp* childOp = mProjectedNodes[i];
John Reck113e0822014-03-18 09:22:59 -0700761
762 // matrix save, concat, and restore can be done safely without allocating operations
763 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
764 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
765 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700766 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700767 childOp->mSkipInOrderDraw = true;
768 renderer.restoreToCount(restoreTo);
769 }
Chris Craik3f0854292014-04-15 16:18:08 -0700770
771 if (projectionReceiverOutline != NULL) {
772 handler(new (alloc) RestoreToCountOp(restoreTo),
773 PROPERTY_SAVECOUNT, properties().getClipToBounds());
774 }
John Reck113e0822014-03-18 09:22:59 -0700775}
776
777/**
778 * This function serves both defer and replay modes, and will organize the displayList's component
779 * operations for a single frame:
780 *
781 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
782 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
783 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
784 * defer vs replay logic, per operation
785 */
786template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700787void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
Chris Craik06451282014-07-21 10:25:54 -0700788 const int level = handler.level();
789 if (mDisplayListData->isEmpty()) {
790 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, getName());
791 return;
792 }
793
John Reck25fbb3f2014-06-12 13:46:45 -0700794 const bool drawLayer = (mLayer && (&renderer != mLayer->renderer));
795 // If we are updating the contents of mLayer, we don't want to apply any of
796 // the RenderNode's properties to this issueOperations pass. Those will all
797 // be applied when the layer is drawn, aka when this is true.
798 const bool useViewProperties = (!mLayer || drawLayer);
Chris Craik06451282014-07-21 10:25:54 -0700799 if (useViewProperties) {
800 const Outline& outline = properties().getOutline();
801 if (properties().getAlpha() <= 0 || (outline.getShouldClip() && outline.isEmpty())) {
802 DISPLAY_LIST_LOGD("%*sRejected display list (%p, %s)", level * 2, "", this, getName());
803 return;
804 }
John Reck113e0822014-03-18 09:22:59 -0700805 }
806
Chris Craik3f0854292014-04-15 16:18:08 -0700807 handler.startMark(getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700808
John Reck113e0822014-03-18 09:22:59 -0700809#if DEBUG_DISPLAY_LIST
Chris Craik3f0854292014-04-15 16:18:08 -0700810 const Rect& clipRect = renderer.getLocalClipBounds();
811 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), localClipBounds: %.0f, %.0f, %.0f, %.0f",
812 level * 2, "", this, getName(),
813 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
John Reck113e0822014-03-18 09:22:59 -0700814#endif
815
816 LinearAllocator& alloc = handler.allocator();
817 int restoreTo = renderer.getSaveCount();
818 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
John Reckd0a0b2a2014-03-20 16:28:56 -0700819 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700820
821 DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
822 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
823
John Reck25fbb3f2014-06-12 13:46:45 -0700824 if (useViewProperties) {
825 setViewProperties<T>(renderer, handler);
826 }
John Reck113e0822014-03-18 09:22:59 -0700827
Chris Craik8c271ca2014-03-25 10:33:01 -0700828 bool quickRejected = properties().getClipToBounds()
829 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -0700830 if (!quickRejected) {
John Reck25fbb3f2014-06-12 13:46:45 -0700831 if (drawLayer) {
832 handler(new (alloc) DrawLayerOp(mLayer, 0, 0),
833 renderer.getSaveCount() - 1, properties().getClipToBounds());
834 } else {
Chris Craika7090e02014-06-20 16:01:00 -0700835 Vector<ZDrawRenderNodeOpPair> zTranslatedNodes;
John Reck25fbb3f2014-06-12 13:46:45 -0700836 buildZSortedChildList(zTranslatedNodes);
John Reck113e0822014-03-18 09:22:59 -0700837
John Reck25fbb3f2014-06-12 13:46:45 -0700838 // for 3d root, draw children with negative z values
Chris Craik80d49022014-06-20 15:03:43 -0700839 int shadowRestoreTo = issueOperationsOfNegZChildren(zTranslatedNodes, renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700840
John Reck25fbb3f2014-06-12 13:46:45 -0700841 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
842 const int saveCountOffset = renderer.getSaveCount() - 1;
843 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
John Reck1aa5d2d2014-07-24 13:38:28 -0700844 const int size = static_cast<int>(mDisplayListData->displayListOps.size());
845 for (int i = 0; i < size; i++) {
John Reck25fbb3f2014-06-12 13:46:45 -0700846 DisplayListOp *op = mDisplayListData->displayListOps[i];
John Reck113e0822014-03-18 09:22:59 -0700847
Chris Craik80d49022014-06-20 15:03:43 -0700848#if DEBUG_DISPLAY_LIST
John Reck25fbb3f2014-06-12 13:46:45 -0700849 op->output(level + 1);
Chris Craik80d49022014-06-20 15:03:43 -0700850#endif
John Reck25fbb3f2014-06-12 13:46:45 -0700851 logBuffer.writeCommand(level, op->name());
852 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700853
John Reck25fbb3f2014-06-12 13:46:45 -0700854 if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) {
855 issueOperationsOfProjectedChildren(renderer, handler);
856 }
John Reck113e0822014-03-18 09:22:59 -0700857 }
John Reck113e0822014-03-18 09:22:59 -0700858
John Reck25fbb3f2014-06-12 13:46:45 -0700859 // for 3d root, draw children with positive z values
Chris Craik80d49022014-06-20 15:03:43 -0700860 issueOperationsOfPosZChildren(shadowRestoreTo, zTranslatedNodes, renderer, handler);
John Reck25fbb3f2014-06-12 13:46:45 -0700861 }
John Reck113e0822014-03-18 09:22:59 -0700862 }
863
864 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
865 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -0700866 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700867 renderer.setOverrideLayerAlpha(1.0f);
Chris Craikb265e2c2014-03-27 15:50:09 -0700868
Chris Craik3f0854292014-04-15 16:18:08 -0700869 DISPLAY_LIST_LOGD("%*sDone (%p, %s)", level * 2, "", this, getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700870 handler.endMark();
John Reck113e0822014-03-18 09:22:59 -0700871}
872
873} /* namespace uirenderer */
874} /* namespace android */