blob: f0cf42f2f634374c0a4e07da249b8aa7831c5082 [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
18
19#include "RenderNode.h"
20
21#include <SkCanvas.h>
22#include <algorithm>
23
24#include <utils/Trace.h>
25
26#include "Debug.h"
27#include "DisplayListOp.h"
28#include "DisplayListLogBuffer.h"
29
30namespace android {
31namespace uirenderer {
32
33void RenderNode::outputLogBuffer(int fd) {
34 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
35 if (logBuffer.isEmpty()) {
36 return;
37 }
38
39 FILE *file = fdopen(fd, "a");
40
41 fprintf(file, "\nRecent DisplayList operations\n");
42 logBuffer.outputCommands(file);
43
44 String8 cachesLog;
45 Caches::getInstance().dumpMemoryUsage(cachesLog);
46 fprintf(file, "\nCaches:\n%s", cachesLog.string());
47 fprintf(file, "\n");
48
49 fflush(file);
50}
51
John Reckd0a0b2a2014-03-20 16:28:56 -070052RenderNode::RenderNode() : mDestroyed(false), mNeedsPropertiesSync(false), mDisplayListData(0) {
John Reck113e0822014-03-18 09:22:59 -070053}
54
55RenderNode::~RenderNode() {
56 LOG_ALWAYS_FATAL_IF(mDestroyed, "Double destroyed DisplayList %p", this);
57
58 mDestroyed = true;
59 delete mDisplayListData;
60}
61
62void RenderNode::destroyDisplayListDeferred(RenderNode* displayList) {
63 if (displayList) {
64 DISPLAY_LIST_LOGD("Deferring display list destruction");
65 Caches::getInstance().deleteDisplayListDeferred(displayList);
66 }
67}
68
69void RenderNode::setData(DisplayListData* data) {
70 delete mDisplayListData;
71 mDisplayListData = data;
72 if (mDisplayListData) {
73 Caches::getInstance().registerFunctors(mDisplayListData->functorCount);
74 }
75}
76
77/**
78 * This function is a simplified version of replay(), where we simply retrieve and log the
79 * display list. This function should remain in sync with the replay() function.
80 */
81void RenderNode::output(uint32_t level) {
82 ALOGD("%*sStart display list (%p, %s, render=%d)", (level - 1) * 2, "", this,
83 mName.string(), isRenderable());
84 ALOGD("%*s%s %d", level * 2, "", "Save",
85 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
86
John Reckd0a0b2a2014-03-20 16:28:56 -070087 properties().debugOutputProperties(level);
John Reck113e0822014-03-18 09:22:59 -070088 int flags = DisplayListOp::kOpLogFlag_Recurse;
89 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
90 mDisplayListData->displayListOps[i]->output(level, flags);
91 }
92
93 ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, mName.string());
94}
95
John Reckd0a0b2a2014-03-20 16:28:56 -070096void RenderNode::updateProperties() {
97 if (mNeedsPropertiesSync) {
98 mNeedsPropertiesSync = false;
99 mProperties = mStagingProperties;
John Reck113e0822014-03-18 09:22:59 -0700100 }
101
John Reck5bf11bb2014-03-25 10:22:09 -0700102 if (mDisplayListData) {
103 for (size_t i = 0; i < mDisplayListData->children.size(); i++) {
104 RenderNode* childNode = mDisplayListData->children[i]->mDisplayList;
105 childNode->updateProperties();
106 }
John Reck113e0822014-03-18 09:22:59 -0700107 }
108}
109
John Reck668f0e32014-03-26 15:10:40 -0700110bool RenderNode::hasFunctors() {
111 if (!mDisplayListData) return false;
112
113 if (mDisplayListData->functorCount) {
114 return true;
115 }
116
117 for (size_t i = 0; i < mDisplayListData->children.size(); i++) {
118 RenderNode* childNode = mDisplayListData->children[i]->mDisplayList;
119 if (childNode->hasFunctors()) {
120 return true;
121 }
122 }
123
124 return false;
125}
126
John Reck113e0822014-03-18 09:22:59 -0700127/*
128 * For property operations, we pass a savecount of 0, since the operations aren't part of the
129 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
John Reckd0a0b2a2014-03-20 16:28:56 -0700130 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
John Reck113e0822014-03-18 09:22:59 -0700131 */
132#define PROPERTY_SAVECOUNT 0
133
134template <class T>
135void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler,
136 const int level) {
137#if DEBUG_DISPLAY_LIST
John Reckd0a0b2a2014-03-20 16:28:56 -0700138 properties().debugOutputProperties(level);
John Reck113e0822014-03-18 09:22:59 -0700139#endif
John Reckd0a0b2a2014-03-20 16:28:56 -0700140 if (properties().getLeft() != 0 || properties().getTop() != 0) {
141 renderer.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700142 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700143 if (properties().getStaticMatrix()) {
144 renderer.concatMatrix(properties().getStaticMatrix());
145 } else if (properties().getAnimationMatrix()) {
146 renderer.concatMatrix(properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700147 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700148 if (properties().getMatrixFlags() != 0) {
149 if (properties().getMatrixFlags() == TRANSLATION) {
150 renderer.translate(properties().getTranslationX(), properties().getTranslationY());
John Reck113e0822014-03-18 09:22:59 -0700151 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700152 renderer.concatMatrix(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700153 }
154 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700155 bool clipToBoundsNeeded = properties().getCaching() ? false : properties().getClipToBounds();
156 if (properties().getAlpha() < 1) {
157 if (properties().getCaching()) {
158 renderer.setOverrideLayerAlpha(properties().getAlpha());
159 } else if (!properties().getHasOverlappingRendering()) {
160 renderer.scaleAlpha(properties().getAlpha());
John Reck113e0822014-03-18 09:22:59 -0700161 } else {
162 // TODO: should be able to store the size of a DL at record time and not
163 // have to pass it into this call. In fact, this information might be in the
164 // location/size info that we store with the new native transform data.
165 int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
166 if (clipToBoundsNeeded) {
167 saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
168 clipToBoundsNeeded = false; // clipping done by saveLayer
169 }
170
171 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
John Reckd0a0b2a2014-03-20 16:28:56 -0700172 0, 0, properties().getWidth(), properties().getHeight(), properties().getAlpha() * 255, saveFlags);
173 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700174 }
175 }
176 if (clipToBoundsNeeded) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700177 ClipRectOp* op = new (handler.allocator()) ClipRectOp(0, 0,
178 properties().getWidth(), properties().getHeight(), SkRegion::kIntersect_Op);
179 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700180 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700181 if (CC_UNLIKELY(properties().getOutline().willClip())) {
182 ClipPathOp* op = new (handler.allocator()) ClipPathOp(properties().getOutline().getPath(),
Chris Craikb49f4462014-03-20 12:44:20 -0700183 SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700184 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700185 }
186}
187
188/**
189 * Apply property-based transformations to input matrix
190 *
191 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
192 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
193 */
194void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700195 if (properties().getLeft() != 0 || properties().getTop() != 0) {
196 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700197 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700198 if (properties().getStaticMatrix()) {
199 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700200 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700201 } else if (properties().getAnimationMatrix()) {
202 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700203 matrix.multiply(anim);
204 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700205 if (properties().getMatrixFlags() != 0) {
206 if (properties().getMatrixFlags() == TRANSLATION) {
207 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
208 true3dTransform ? properties().getTranslationZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700209 } else {
210 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700211 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700212 } else {
213 mat4 true3dMat;
214 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700215 properties().getPivotX() + properties().getTranslationX(),
216 properties().getPivotY() + properties().getTranslationY(),
217 properties().getTranslationZ());
218 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
219 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
220 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
221 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
222 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700223
224 matrix.multiply(true3dMat);
225 }
226 }
227 }
228}
229
230/**
231 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
232 *
233 * This should be called before a call to defer() or drawDisplayList()
234 *
235 * Each DisplayList that serves as a 3d root builds its list of composited children,
236 * which are flagged to not draw in the standard draw loop.
237 */
238void RenderNode::computeOrdering() {
239 ATRACE_CALL();
240 mProjectedNodes.clear();
241
242 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
243 // transform properties are applied correctly to top level children
244 if (mDisplayListData == NULL) return;
245 for (unsigned int i = 0; i < mDisplayListData->children.size(); i++) {
246 DrawDisplayListOp* childOp = mDisplayListData->children[i];
247 childOp->mDisplayList->computeOrderingImpl(childOp,
248 &mProjectedNodes, &mat4::identity());
249 }
250}
251
252void RenderNode::computeOrderingImpl(
253 DrawDisplayListOp* opState,
254 Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface,
255 const mat4* transformFromProjectionSurface) {
256 mProjectedNodes.clear();
257 if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
258
259 // TODO: should avoid this calculation in most cases
260 // TODO: just calculate single matrix, down to all leaf composited elements
261 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
262 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
263
John Reckd0a0b2a2014-03-20 16:28:56 -0700264 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700265 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
266 opState->mSkipInOrderDraw = true;
267 opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
268 compositedChildrenOfProjectionSurface->add(opState);
269 } else {
270 // standard in order draw
271 opState->mSkipInOrderDraw = false;
272 }
273
274 if (mDisplayListData->children.size() > 0) {
275 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
276 bool haveAppliedPropertiesToProjection = false;
277 for (unsigned int i = 0; i < mDisplayListData->children.size(); i++) {
278 DrawDisplayListOp* childOp = mDisplayListData->children[i];
279 RenderNode* child = childOp->mDisplayList;
280
281 Vector<DrawDisplayListOp*>* projectionChildren = NULL;
282 const mat4* projectionTransform = NULL;
John Reckd0a0b2a2014-03-20 16:28:56 -0700283 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700284 // if receiving projections, collect projecting descendent
285
286 // Note that if a direct descendent is projecting backwards, we pass it's
287 // grandparent projection collection, since it shouldn't project onto it's
288 // parent, where it will already be drawing.
289 projectionChildren = &mProjectedNodes;
290 projectionTransform = &mat4::identity();
291 } else {
292 if (!haveAppliedPropertiesToProjection) {
293 applyViewPropertyTransforms(localTransformFromProjectionSurface);
294 haveAppliedPropertiesToProjection = true;
295 }
296 projectionChildren = compositedChildrenOfProjectionSurface;
297 projectionTransform = &localTransformFromProjectionSurface;
298 }
299 child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
300 }
301 }
302
303}
304
305class DeferOperationHandler {
306public:
307 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
308 : mDeferStruct(deferStruct), mLevel(level) {}
309 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
310 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
311 }
312 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
313
314private:
315 DeferStateStruct& mDeferStruct;
316 const int mLevel;
317};
318
319void RenderNode::defer(DeferStateStruct& deferStruct, const int level) {
320 DeferOperationHandler handler(deferStruct, level);
321 iterate<DeferOperationHandler>(deferStruct.mRenderer, handler, level);
322}
323
324class ReplayOperationHandler {
325public:
326 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
327 : mReplayStruct(replayStruct), mLevel(level) {}
328 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
329#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
John Reckd0a0b2a2014-03-20 16:28:56 -0700330 properties().getReplayStruct().mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700331#endif
332 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
333 }
334 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
335
336private:
337 ReplayStateStruct& mReplayStruct;
338 const int mLevel;
339};
340
341void RenderNode::replay(ReplayStateStruct& replayStruct, const int level) {
342 ReplayOperationHandler handler(replayStruct, level);
343
344 replayStruct.mRenderer.startMark(mName.string());
345 iterate<ReplayOperationHandler>(replayStruct.mRenderer, handler, level);
346 replayStruct.mRenderer.endMark();
347
348 DISPLAY_LIST_LOGD("%*sDone (%p, %s), returning %d", level * 2, "", this, mName.string(),
349 replayStruct.mDrawGlStatus);
350}
351
352void RenderNode::buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes) {
353 if (mDisplayListData == NULL || mDisplayListData->children.size() == 0) return;
354
355 for (unsigned int i = 0; i < mDisplayListData->children.size(); i++) {
356 DrawDisplayListOp* childOp = mDisplayListData->children[i];
357 RenderNode* child = childOp->mDisplayList;
John Reckd0a0b2a2014-03-20 16:28:56 -0700358 float childZ = child->properties().getTranslationZ();
John Reck113e0822014-03-18 09:22:59 -0700359
360 if (childZ != 0.0f) {
361 zTranslatedNodes.add(ZDrawDisplayListOpPair(childZ, childOp));
362 childOp->mSkipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700363 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700364 // regular, in order drawing DisplayList
365 childOp->mSkipInOrderDraw = false;
366 }
367 }
368
369 // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order)
370 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
371}
372
373#define SHADOW_DELTA 0.1f
374
375template <class T>
376void RenderNode::iterate3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes,
377 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) {
378 const int size = zTranslatedNodes.size();
379 if (size == 0
380 || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
381 || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
382 // no 3d children to draw
383 return;
384 }
385
386 int rootRestoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
387 LinearAllocator& alloc = handler.allocator();
John Reckd0a0b2a2014-03-20 16:28:56 -0700388 ClipRectOp* clipOp = new (alloc) ClipRectOp(0, 0, properties().getWidth(), properties().getHeight(),
John Reck113e0822014-03-18 09:22:59 -0700389 SkRegion::kIntersect_Op); // clip to 3d root bounds
John Reckd0a0b2a2014-03-20 16:28:56 -0700390 handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700391
392 /**
393 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
394 * with very similar Z heights to draw together.
395 *
396 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
397 * underneath both, and neither's shadow is drawn on top of the other.
398 */
399 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
400 size_t drawIndex, shadowIndex, endIndex;
401 if (mode == kNegativeZChildren) {
402 drawIndex = 0;
403 endIndex = nonNegativeIndex;
404 shadowIndex = endIndex; // draw no shadows
405 } else {
406 drawIndex = nonNegativeIndex;
407 endIndex = size;
408 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
409 }
410 float lastCasterZ = 0.0f;
411 while (shadowIndex < endIndex || drawIndex < endIndex) {
412 if (shadowIndex < endIndex) {
413 DrawDisplayListOp* casterOp = zTranslatedNodes[shadowIndex].value;
414 RenderNode* caster = casterOp->mDisplayList;
415 const float casterZ = zTranslatedNodes[shadowIndex].key;
416 // attempt to render the shadow if the caster about to be drawn is its caster,
417 // OR if its caster's Z value is similar to the previous potential caster
418 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
419
John Reckd0a0b2a2014-03-20 16:28:56 -0700420 if (caster->properties().getAlpha() > 0.0f) {
John Reck113e0822014-03-18 09:22:59 -0700421 mat4 shadowMatrixXY(casterOp->mTransformFromParent);
422 caster->applyViewPropertyTransforms(shadowMatrixXY);
423
424 // Z matrix needs actual 3d transformation, so mapped z values will be correct
425 mat4 shadowMatrixZ(casterOp->mTransformFromParent);
426 caster->applyViewPropertyTransforms(shadowMatrixZ, true);
427
428 DisplayListOp* shadowOp = new (alloc) DrawShadowOp(
429 shadowMatrixXY, shadowMatrixZ,
John Reckd0a0b2a2014-03-20 16:28:56 -0700430 caster->properties().getAlpha(), caster->properties().getOutline().getPath(),
431 caster->properties().getWidth(), caster->properties().getHeight());
432 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700433 }
434
435 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
436 shadowIndex++;
437 continue;
438 }
439 }
440
441 // only the actual child DL draw needs to be in save/restore,
442 // since it modifies the renderer's matrix
443 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
444
445 DrawDisplayListOp* childOp = zTranslatedNodes[drawIndex].value;
446 RenderNode* child = childOp->mDisplayList;
447
448 renderer.concatMatrix(childOp->mTransformFromParent);
449 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700450 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700451 childOp->mSkipInOrderDraw = true;
452
453 renderer.restoreToCount(restoreTo);
454 drawIndex++;
455 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700456 handler(new (alloc) RestoreToCountOp(rootRestoreTo), PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700457}
458
459template <class T>
460void RenderNode::iterateProjectedChildren(OpenGLRenderer& renderer, T& handler, const int level) {
461 int rootRestoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
462 LinearAllocator& alloc = handler.allocator();
John Reckd0a0b2a2014-03-20 16:28:56 -0700463 ClipRectOp* clipOp = new (alloc) ClipRectOp(0, 0, properties().getWidth(), properties().getHeight(),
John Reck113e0822014-03-18 09:22:59 -0700464 SkRegion::kReplace_Op); // clip to projection surface root bounds
John Reckd0a0b2a2014-03-20 16:28:56 -0700465 handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700466
467 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
468 DrawDisplayListOp* childOp = mProjectedNodes[i];
469
470 // matrix save, concat, and restore can be done safely without allocating operations
471 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
472 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
473 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700474 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700475 childOp->mSkipInOrderDraw = true;
476 renderer.restoreToCount(restoreTo);
477 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700478 handler(new (alloc) RestoreToCountOp(rootRestoreTo), PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700479}
480
481/**
482 * This function serves both defer and replay modes, and will organize the displayList's component
483 * operations for a single frame:
484 *
485 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
486 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
487 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
488 * defer vs replay logic, per operation
489 */
490template <class T>
491void RenderNode::iterate(OpenGLRenderer& renderer, T& handler, const int level) {
492 if (CC_UNLIKELY(mDestroyed)) { // temporary debug logging
493 ALOGW("Error: %s is drawing after destruction", mName.string());
494 CRASH();
495 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700496 if (mDisplayListData->isEmpty() || properties().getAlpha() <= 0) {
John Reck113e0822014-03-18 09:22:59 -0700497 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, mName.string());
498 return;
499 }
500
501#if DEBUG_DISPLAY_LIST
502 Rect* clipRect = renderer.getClipRect();
503 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), clipRect: %.0f, %.0f, %.0f, %.0f",
504 level * 2, "", this, mName.string(), clipRect->left, clipRect->top,
505 clipRect->right, clipRect->bottom);
506#endif
507
508 LinearAllocator& alloc = handler.allocator();
509 int restoreTo = renderer.getSaveCount();
510 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
John Reckd0a0b2a2014-03-20 16:28:56 -0700511 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700512
513 DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
514 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
515
516 setViewProperties<T>(renderer, handler, level + 1);
517
John Reckd0a0b2a2014-03-20 16:28:56 -0700518 bool quickRejected = properties().getClipToBounds() && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -0700519 if (!quickRejected) {
520 Vector<ZDrawDisplayListOpPair> zTranslatedNodes;
521 buildZSortedChildList(zTranslatedNodes);
522
523 // for 3d root, draw children with negative z values
524 iterate3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler);
525
526 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
527 const int saveCountOffset = renderer.getSaveCount() - 1;
528 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
529 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
530 DisplayListOp *op = mDisplayListData->displayListOps[i];
531
532#if DEBUG_DISPLAY_LIST
533 op->output(level + 1);
534#endif
535
536 logBuffer.writeCommand(level, op->name());
John Reckd0a0b2a2014-03-20 16:28:56 -0700537 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700538
539 if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) {
540 iterateProjectedChildren(renderer, handler, level);
541 }
542 }
543
544 // for 3d root, draw children with positive z values
545 iterate3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler);
546 }
547
548 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
549 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -0700550 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700551 renderer.setOverrideLayerAlpha(1.0f);
552}
553
554} /* namespace uirenderer */
555} /* namespace android */