blob: e39e5ae52ed2e6d033f00c4e3f6b7deb2a635a88 [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
110/*
111 * For property operations, we pass a savecount of 0, since the operations aren't part of the
112 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
John Reckd0a0b2a2014-03-20 16:28:56 -0700113 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
John Reck113e0822014-03-18 09:22:59 -0700114 */
115#define PROPERTY_SAVECOUNT 0
116
117template <class T>
118void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler,
119 const int level) {
120#if DEBUG_DISPLAY_LIST
John Reckd0a0b2a2014-03-20 16:28:56 -0700121 properties().debugOutputProperties(level);
John Reck113e0822014-03-18 09:22:59 -0700122#endif
John Reckd0a0b2a2014-03-20 16:28:56 -0700123 if (properties().getLeft() != 0 || properties().getTop() != 0) {
124 renderer.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700125 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700126 if (properties().getStaticMatrix()) {
127 renderer.concatMatrix(properties().getStaticMatrix());
128 } else if (properties().getAnimationMatrix()) {
129 renderer.concatMatrix(properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700130 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700131 if (properties().getMatrixFlags() != 0) {
132 if (properties().getMatrixFlags() == TRANSLATION) {
133 renderer.translate(properties().getTranslationX(), properties().getTranslationY());
John Reck113e0822014-03-18 09:22:59 -0700134 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700135 renderer.concatMatrix(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700136 }
137 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700138 bool clipToBoundsNeeded = properties().getCaching() ? false : properties().getClipToBounds();
139 if (properties().getAlpha() < 1) {
140 if (properties().getCaching()) {
141 renderer.setOverrideLayerAlpha(properties().getAlpha());
142 } else if (!properties().getHasOverlappingRendering()) {
143 renderer.scaleAlpha(properties().getAlpha());
John Reck113e0822014-03-18 09:22:59 -0700144 } else {
145 // TODO: should be able to store the size of a DL at record time and not
146 // have to pass it into this call. In fact, this information might be in the
147 // location/size info that we store with the new native transform data.
148 int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
149 if (clipToBoundsNeeded) {
150 saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
151 clipToBoundsNeeded = false; // clipping done by saveLayer
152 }
153
154 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
Chris Craik8c271ca2014-03-25 10:33:01 -0700155 0, 0, properties().getWidth(), properties().getHeight(),
156 properties().getAlpha() * 255, saveFlags);
John Reckd0a0b2a2014-03-20 16:28:56 -0700157 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700158 }
159 }
160 if (clipToBoundsNeeded) {
Chris Craik8c271ca2014-03-25 10:33:01 -0700161 ClipRectOp* op = new (handler.allocator()) ClipRectOp(
162 0, 0, properties().getWidth(), properties().getHeight(), SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700163 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700164 }
Chris Craik8c271ca2014-03-25 10:33:01 -0700165
166 if (CC_UNLIKELY(properties().hasClippingPath())) {
167 // TODO: optimize for round rect/circle clipping
168 const SkPath* path = properties().getClippingPath();
169 ClipPathOp* op = new (handler.allocator()) ClipPathOp(path, SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700170 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700171 }
172}
173
174/**
175 * Apply property-based transformations to input matrix
176 *
177 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
178 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
179 */
180void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700181 if (properties().getLeft() != 0 || properties().getTop() != 0) {
182 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700183 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700184 if (properties().getStaticMatrix()) {
185 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700186 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700187 } else if (properties().getAnimationMatrix()) {
188 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700189 matrix.multiply(anim);
190 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700191 if (properties().getMatrixFlags() != 0) {
192 if (properties().getMatrixFlags() == TRANSLATION) {
193 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
194 true3dTransform ? properties().getTranslationZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700195 } else {
196 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700197 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700198 } else {
199 mat4 true3dMat;
200 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700201 properties().getPivotX() + properties().getTranslationX(),
202 properties().getPivotY() + properties().getTranslationY(),
203 properties().getTranslationZ());
204 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
205 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
206 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
207 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
208 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700209
210 matrix.multiply(true3dMat);
211 }
212 }
213 }
214}
215
216/**
217 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
218 *
219 * This should be called before a call to defer() or drawDisplayList()
220 *
221 * Each DisplayList that serves as a 3d root builds its list of composited children,
222 * which are flagged to not draw in the standard draw loop.
223 */
224void RenderNode::computeOrdering() {
225 ATRACE_CALL();
226 mProjectedNodes.clear();
227
228 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
229 // transform properties are applied correctly to top level children
230 if (mDisplayListData == NULL) return;
231 for (unsigned int i = 0; i < mDisplayListData->children.size(); i++) {
232 DrawDisplayListOp* childOp = mDisplayListData->children[i];
233 childOp->mDisplayList->computeOrderingImpl(childOp,
234 &mProjectedNodes, &mat4::identity());
235 }
236}
237
238void RenderNode::computeOrderingImpl(
239 DrawDisplayListOp* opState,
240 Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface,
241 const mat4* transformFromProjectionSurface) {
242 mProjectedNodes.clear();
243 if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
244
245 // TODO: should avoid this calculation in most cases
246 // TODO: just calculate single matrix, down to all leaf composited elements
247 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
248 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
249
John Reckd0a0b2a2014-03-20 16:28:56 -0700250 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700251 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
252 opState->mSkipInOrderDraw = true;
253 opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
254 compositedChildrenOfProjectionSurface->add(opState);
255 } else {
256 // standard in order draw
257 opState->mSkipInOrderDraw = false;
258 }
259
260 if (mDisplayListData->children.size() > 0) {
261 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
262 bool haveAppliedPropertiesToProjection = false;
263 for (unsigned int i = 0; i < mDisplayListData->children.size(); i++) {
264 DrawDisplayListOp* childOp = mDisplayListData->children[i];
265 RenderNode* child = childOp->mDisplayList;
266
267 Vector<DrawDisplayListOp*>* projectionChildren = NULL;
268 const mat4* projectionTransform = NULL;
John Reckd0a0b2a2014-03-20 16:28:56 -0700269 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700270 // if receiving projections, collect projecting descendent
271
272 // Note that if a direct descendent is projecting backwards, we pass it's
273 // grandparent projection collection, since it shouldn't project onto it's
274 // parent, where it will already be drawing.
275 projectionChildren = &mProjectedNodes;
276 projectionTransform = &mat4::identity();
277 } else {
278 if (!haveAppliedPropertiesToProjection) {
279 applyViewPropertyTransforms(localTransformFromProjectionSurface);
280 haveAppliedPropertiesToProjection = true;
281 }
282 projectionChildren = compositedChildrenOfProjectionSurface;
283 projectionTransform = &localTransformFromProjectionSurface;
284 }
285 child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
286 }
287 }
288
289}
290
291class DeferOperationHandler {
292public:
293 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
294 : mDeferStruct(deferStruct), mLevel(level) {}
295 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
296 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
297 }
298 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
299
300private:
301 DeferStateStruct& mDeferStruct;
302 const int mLevel;
303};
304
305void RenderNode::defer(DeferStateStruct& deferStruct, const int level) {
306 DeferOperationHandler handler(deferStruct, level);
307 iterate<DeferOperationHandler>(deferStruct.mRenderer, handler, level);
308}
309
310class ReplayOperationHandler {
311public:
312 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
313 : mReplayStruct(replayStruct), mLevel(level) {}
314 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
315#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
John Reckd0a0b2a2014-03-20 16:28:56 -0700316 properties().getReplayStruct().mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700317#endif
318 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
319 }
320 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
321
322private:
323 ReplayStateStruct& mReplayStruct;
324 const int mLevel;
325};
326
327void RenderNode::replay(ReplayStateStruct& replayStruct, const int level) {
328 ReplayOperationHandler handler(replayStruct, level);
329
330 replayStruct.mRenderer.startMark(mName.string());
331 iterate<ReplayOperationHandler>(replayStruct.mRenderer, handler, level);
332 replayStruct.mRenderer.endMark();
333
334 DISPLAY_LIST_LOGD("%*sDone (%p, %s), returning %d", level * 2, "", this, mName.string(),
335 replayStruct.mDrawGlStatus);
336}
337
338void RenderNode::buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes) {
339 if (mDisplayListData == NULL || mDisplayListData->children.size() == 0) return;
340
341 for (unsigned int i = 0; i < mDisplayListData->children.size(); i++) {
342 DrawDisplayListOp* childOp = mDisplayListData->children[i];
343 RenderNode* child = childOp->mDisplayList;
John Reckd0a0b2a2014-03-20 16:28:56 -0700344 float childZ = child->properties().getTranslationZ();
John Reck113e0822014-03-18 09:22:59 -0700345
346 if (childZ != 0.0f) {
347 zTranslatedNodes.add(ZDrawDisplayListOpPair(childZ, childOp));
348 childOp->mSkipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700349 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700350 // regular, in order drawing DisplayList
351 childOp->mSkipInOrderDraw = false;
352 }
353 }
354
355 // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order)
356 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
357}
358
359#define SHADOW_DELTA 0.1f
360
361template <class T>
362void RenderNode::iterate3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes,
363 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) {
364 const int size = zTranslatedNodes.size();
365 if (size == 0
366 || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
367 || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
368 // no 3d children to draw
369 return;
370 }
371
372 int rootRestoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
373 LinearAllocator& alloc = handler.allocator();
John Reckd0a0b2a2014-03-20 16:28:56 -0700374 ClipRectOp* clipOp = new (alloc) ClipRectOp(0, 0, properties().getWidth(), properties().getHeight(),
John Reck113e0822014-03-18 09:22:59 -0700375 SkRegion::kIntersect_Op); // clip to 3d root bounds
John Reckd0a0b2a2014-03-20 16:28:56 -0700376 handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700377
378 /**
379 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
380 * with very similar Z heights to draw together.
381 *
382 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
383 * underneath both, and neither's shadow is drawn on top of the other.
384 */
385 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
386 size_t drawIndex, shadowIndex, endIndex;
387 if (mode == kNegativeZChildren) {
388 drawIndex = 0;
389 endIndex = nonNegativeIndex;
390 shadowIndex = endIndex; // draw no shadows
391 } else {
392 drawIndex = nonNegativeIndex;
393 endIndex = size;
394 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
395 }
396 float lastCasterZ = 0.0f;
397 while (shadowIndex < endIndex || drawIndex < endIndex) {
398 if (shadowIndex < endIndex) {
399 DrawDisplayListOp* casterOp = zTranslatedNodes[shadowIndex].value;
400 RenderNode* caster = casterOp->mDisplayList;
401 const float casterZ = zTranslatedNodes[shadowIndex].key;
402 // attempt to render the shadow if the caster about to be drawn is its caster,
403 // OR if its caster's Z value is similar to the previous potential caster
404 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
405
John Reckd0a0b2a2014-03-20 16:28:56 -0700406 if (caster->properties().getAlpha() > 0.0f) {
John Reck113e0822014-03-18 09:22:59 -0700407 mat4 shadowMatrixXY(casterOp->mTransformFromParent);
408 caster->applyViewPropertyTransforms(shadowMatrixXY);
409
410 // Z matrix needs actual 3d transformation, so mapped z values will be correct
411 mat4 shadowMatrixZ(casterOp->mTransformFromParent);
412 caster->applyViewPropertyTransforms(shadowMatrixZ, true);
413
Chris Craik8c271ca2014-03-25 10:33:01 -0700414 const SkPath* outlinePath = caster->properties().getOutline().getPath();
415 const RevealClip& revealClip = caster->properties().getRevealClip();
416 const SkPath* revealClipPath = revealClip.hasConvexClip()
417 ? revealClip.getPath() : NULL; // only pass the reveal clip's path if it's convex
418
John Reck113e0822014-03-18 09:22:59 -0700419 DisplayListOp* shadowOp = new (alloc) DrawShadowOp(
Chris Craik8c271ca2014-03-25 10:33:01 -0700420 shadowMatrixXY, shadowMatrixZ, caster->properties().getAlpha(),
421 caster->properties().getWidth(), caster->properties().getHeight(),
422 outlinePath, revealClipPath);
John Reckd0a0b2a2014-03-20 16:28:56 -0700423 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700424 }
425
426 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
427 shadowIndex++;
428 continue;
429 }
430 }
431
432 // only the actual child DL draw needs to be in save/restore,
433 // since it modifies the renderer's matrix
434 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
435
436 DrawDisplayListOp* childOp = zTranslatedNodes[drawIndex].value;
437 RenderNode* child = childOp->mDisplayList;
438
439 renderer.concatMatrix(childOp->mTransformFromParent);
440 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700441 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700442 childOp->mSkipInOrderDraw = true;
443
444 renderer.restoreToCount(restoreTo);
445 drawIndex++;
446 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700447 handler(new (alloc) RestoreToCountOp(rootRestoreTo), PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700448}
449
450template <class T>
451void RenderNode::iterateProjectedChildren(OpenGLRenderer& renderer, T& handler, const int level) {
452 int rootRestoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
453 LinearAllocator& alloc = handler.allocator();
John Reckd0a0b2a2014-03-20 16:28:56 -0700454 ClipRectOp* clipOp = new (alloc) ClipRectOp(0, 0, properties().getWidth(), properties().getHeight(),
John Reck113e0822014-03-18 09:22:59 -0700455 SkRegion::kReplace_Op); // clip to projection surface root bounds
John Reckd0a0b2a2014-03-20 16:28:56 -0700456 handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700457
458 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
459 DrawDisplayListOp* childOp = mProjectedNodes[i];
460
461 // matrix save, concat, and restore can be done safely without allocating operations
462 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
463 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
464 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700465 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700466 childOp->mSkipInOrderDraw = true;
467 renderer.restoreToCount(restoreTo);
468 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700469 handler(new (alloc) RestoreToCountOp(rootRestoreTo), PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700470}
471
472/**
473 * This function serves both defer and replay modes, and will organize the displayList's component
474 * operations for a single frame:
475 *
476 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
477 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
478 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
479 * defer vs replay logic, per operation
480 */
481template <class T>
482void RenderNode::iterate(OpenGLRenderer& renderer, T& handler, const int level) {
483 if (CC_UNLIKELY(mDestroyed)) { // temporary debug logging
484 ALOGW("Error: %s is drawing after destruction", mName.string());
485 CRASH();
486 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700487 if (mDisplayListData->isEmpty() || properties().getAlpha() <= 0) {
John Reck113e0822014-03-18 09:22:59 -0700488 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, mName.string());
489 return;
490 }
491
492#if DEBUG_DISPLAY_LIST
493 Rect* clipRect = renderer.getClipRect();
494 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), clipRect: %.0f, %.0f, %.0f, %.0f",
495 level * 2, "", this, mName.string(), clipRect->left, clipRect->top,
496 clipRect->right, clipRect->bottom);
497#endif
498
499 LinearAllocator& alloc = handler.allocator();
500 int restoreTo = renderer.getSaveCount();
501 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
John Reckd0a0b2a2014-03-20 16:28:56 -0700502 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700503
504 DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
505 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
506
507 setViewProperties<T>(renderer, handler, level + 1);
508
Chris Craik8c271ca2014-03-25 10:33:01 -0700509 bool quickRejected = properties().getClipToBounds()
510 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -0700511 if (!quickRejected) {
512 Vector<ZDrawDisplayListOpPair> zTranslatedNodes;
513 buildZSortedChildList(zTranslatedNodes);
514
515 // for 3d root, draw children with negative z values
516 iterate3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler);
517
518 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
519 const int saveCountOffset = renderer.getSaveCount() - 1;
520 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
521 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
522 DisplayListOp *op = mDisplayListData->displayListOps[i];
523
524#if DEBUG_DISPLAY_LIST
525 op->output(level + 1);
526#endif
527
528 logBuffer.writeCommand(level, op->name());
John Reckd0a0b2a2014-03-20 16:28:56 -0700529 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700530
531 if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) {
532 iterateProjectedChildren(renderer, handler, level);
533 }
534 }
535
536 // for 3d root, draw children with positive z values
537 iterate3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler);
538 }
539
540 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
541 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -0700542 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700543 renderer.setOverrideLayerAlpha(1.0f);
544}
545
546} /* namespace uirenderer */
547} /* namespace android */