blob: bdf6674c7f0d651ab8150dfc3810e2e5bc017481 [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(
Chris Craik8c271ca2014-03-25 10:33:01 -0700172 0, 0, properties().getWidth(), properties().getHeight(),
173 properties().getAlpha() * 255, saveFlags);
John Reckd0a0b2a2014-03-20 16:28:56 -0700174 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700175 }
176 }
177 if (clipToBoundsNeeded) {
Chris Craik8c271ca2014-03-25 10:33:01 -0700178 ClipRectOp* op = new (handler.allocator()) ClipRectOp(
179 0, 0, properties().getWidth(), properties().getHeight(), SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700180 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700181 }
Chris Craik8c271ca2014-03-25 10:33:01 -0700182
183 if (CC_UNLIKELY(properties().hasClippingPath())) {
184 // TODO: optimize for round rect/circle clipping
185 const SkPath* path = properties().getClippingPath();
186 ClipPathOp* op = new (handler.allocator()) ClipPathOp(path, SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700187 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700188 }
189}
190
191/**
192 * Apply property-based transformations to input matrix
193 *
194 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
195 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
196 */
197void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700198 if (properties().getLeft() != 0 || properties().getTop() != 0) {
199 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700200 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700201 if (properties().getStaticMatrix()) {
202 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700203 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700204 } else if (properties().getAnimationMatrix()) {
205 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700206 matrix.multiply(anim);
207 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700208 if (properties().getMatrixFlags() != 0) {
209 if (properties().getMatrixFlags() == TRANSLATION) {
210 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
211 true3dTransform ? properties().getTranslationZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700212 } else {
213 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700214 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700215 } else {
216 mat4 true3dMat;
217 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700218 properties().getPivotX() + properties().getTranslationX(),
219 properties().getPivotY() + properties().getTranslationY(),
220 properties().getTranslationZ());
221 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
222 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
223 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
224 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
225 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700226
227 matrix.multiply(true3dMat);
228 }
229 }
230 }
231}
232
233/**
234 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
235 *
236 * This should be called before a call to defer() or drawDisplayList()
237 *
238 * Each DisplayList that serves as a 3d root builds its list of composited children,
239 * which are flagged to not draw in the standard draw loop.
240 */
241void RenderNode::computeOrdering() {
242 ATRACE_CALL();
243 mProjectedNodes.clear();
244
245 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
246 // transform properties are applied correctly to top level children
247 if (mDisplayListData == NULL) return;
248 for (unsigned int i = 0; i < mDisplayListData->children.size(); i++) {
249 DrawDisplayListOp* childOp = mDisplayListData->children[i];
250 childOp->mDisplayList->computeOrderingImpl(childOp,
251 &mProjectedNodes, &mat4::identity());
252 }
253}
254
255void RenderNode::computeOrderingImpl(
256 DrawDisplayListOp* opState,
257 Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface,
258 const mat4* transformFromProjectionSurface) {
259 mProjectedNodes.clear();
260 if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
261
262 // TODO: should avoid this calculation in most cases
263 // TODO: just calculate single matrix, down to all leaf composited elements
264 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
265 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
266
John Reckd0a0b2a2014-03-20 16:28:56 -0700267 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700268 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
269 opState->mSkipInOrderDraw = true;
270 opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
271 compositedChildrenOfProjectionSurface->add(opState);
272 } else {
273 // standard in order draw
274 opState->mSkipInOrderDraw = false;
275 }
276
277 if (mDisplayListData->children.size() > 0) {
278 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
279 bool haveAppliedPropertiesToProjection = false;
280 for (unsigned int i = 0; i < mDisplayListData->children.size(); i++) {
281 DrawDisplayListOp* childOp = mDisplayListData->children[i];
282 RenderNode* child = childOp->mDisplayList;
283
284 Vector<DrawDisplayListOp*>* projectionChildren = NULL;
285 const mat4* projectionTransform = NULL;
John Reckd0a0b2a2014-03-20 16:28:56 -0700286 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700287 // if receiving projections, collect projecting descendent
288
289 // Note that if a direct descendent is projecting backwards, we pass it's
290 // grandparent projection collection, since it shouldn't project onto it's
291 // parent, where it will already be drawing.
292 projectionChildren = &mProjectedNodes;
293 projectionTransform = &mat4::identity();
294 } else {
295 if (!haveAppliedPropertiesToProjection) {
296 applyViewPropertyTransforms(localTransformFromProjectionSurface);
297 haveAppliedPropertiesToProjection = true;
298 }
299 projectionChildren = compositedChildrenOfProjectionSurface;
300 projectionTransform = &localTransformFromProjectionSurface;
301 }
302 child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
303 }
304 }
305
306}
307
308class DeferOperationHandler {
309public:
310 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
311 : mDeferStruct(deferStruct), mLevel(level) {}
312 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
313 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
314 }
315 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
316
317private:
318 DeferStateStruct& mDeferStruct;
319 const int mLevel;
320};
321
322void RenderNode::defer(DeferStateStruct& deferStruct, const int level) {
323 DeferOperationHandler handler(deferStruct, level);
324 iterate<DeferOperationHandler>(deferStruct.mRenderer, handler, level);
325}
326
327class ReplayOperationHandler {
328public:
329 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
330 : mReplayStruct(replayStruct), mLevel(level) {}
331 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
332#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
John Reckd0a0b2a2014-03-20 16:28:56 -0700333 properties().getReplayStruct().mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700334#endif
335 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
336 }
337 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
338
339private:
340 ReplayStateStruct& mReplayStruct;
341 const int mLevel;
342};
343
344void RenderNode::replay(ReplayStateStruct& replayStruct, const int level) {
345 ReplayOperationHandler handler(replayStruct, level);
346
347 replayStruct.mRenderer.startMark(mName.string());
348 iterate<ReplayOperationHandler>(replayStruct.mRenderer, handler, level);
349 replayStruct.mRenderer.endMark();
350
351 DISPLAY_LIST_LOGD("%*sDone (%p, %s), returning %d", level * 2, "", this, mName.string(),
352 replayStruct.mDrawGlStatus);
353}
354
355void RenderNode::buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes) {
356 if (mDisplayListData == NULL || mDisplayListData->children.size() == 0) return;
357
358 for (unsigned int i = 0; i < mDisplayListData->children.size(); i++) {
359 DrawDisplayListOp* childOp = mDisplayListData->children[i];
360 RenderNode* child = childOp->mDisplayList;
John Reckd0a0b2a2014-03-20 16:28:56 -0700361 float childZ = child->properties().getTranslationZ();
John Reck113e0822014-03-18 09:22:59 -0700362
363 if (childZ != 0.0f) {
364 zTranslatedNodes.add(ZDrawDisplayListOpPair(childZ, childOp));
365 childOp->mSkipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700366 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700367 // regular, in order drawing DisplayList
368 childOp->mSkipInOrderDraw = false;
369 }
370 }
371
372 // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order)
373 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
374}
375
376#define SHADOW_DELTA 0.1f
377
378template <class T>
379void RenderNode::iterate3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes,
380 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) {
381 const int size = zTranslatedNodes.size();
382 if (size == 0
383 || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
384 || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
385 // no 3d children to draw
386 return;
387 }
388
389 int rootRestoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
390 LinearAllocator& alloc = handler.allocator();
John Reckd0a0b2a2014-03-20 16:28:56 -0700391 ClipRectOp* clipOp = new (alloc) ClipRectOp(0, 0, properties().getWidth(), properties().getHeight(),
John Reck113e0822014-03-18 09:22:59 -0700392 SkRegion::kIntersect_Op); // clip to 3d root bounds
John Reckd0a0b2a2014-03-20 16:28:56 -0700393 handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700394
395 /**
396 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
397 * with very similar Z heights to draw together.
398 *
399 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
400 * underneath both, and neither's shadow is drawn on top of the other.
401 */
402 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
403 size_t drawIndex, shadowIndex, endIndex;
404 if (mode == kNegativeZChildren) {
405 drawIndex = 0;
406 endIndex = nonNegativeIndex;
407 shadowIndex = endIndex; // draw no shadows
408 } else {
409 drawIndex = nonNegativeIndex;
410 endIndex = size;
411 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
412 }
413 float lastCasterZ = 0.0f;
414 while (shadowIndex < endIndex || drawIndex < endIndex) {
415 if (shadowIndex < endIndex) {
416 DrawDisplayListOp* casterOp = zTranslatedNodes[shadowIndex].value;
417 RenderNode* caster = casterOp->mDisplayList;
418 const float casterZ = zTranslatedNodes[shadowIndex].key;
419 // attempt to render the shadow if the caster about to be drawn is its caster,
420 // OR if its caster's Z value is similar to the previous potential caster
421 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
422
John Reckd0a0b2a2014-03-20 16:28:56 -0700423 if (caster->properties().getAlpha() > 0.0f) {
John Reck113e0822014-03-18 09:22:59 -0700424 mat4 shadowMatrixXY(casterOp->mTransformFromParent);
425 caster->applyViewPropertyTransforms(shadowMatrixXY);
426
427 // Z matrix needs actual 3d transformation, so mapped z values will be correct
428 mat4 shadowMatrixZ(casterOp->mTransformFromParent);
429 caster->applyViewPropertyTransforms(shadowMatrixZ, true);
430
Chris Craik8c271ca2014-03-25 10:33:01 -0700431 const SkPath* outlinePath = caster->properties().getOutline().getPath();
432 const RevealClip& revealClip = caster->properties().getRevealClip();
433 const SkPath* revealClipPath = revealClip.hasConvexClip()
434 ? revealClip.getPath() : NULL; // only pass the reveal clip's path if it's convex
435
Chris Craik024433f2014-03-26 13:19:14 -0700436 /**
437 * The drawing area of the caster is always the same as the its perimeter (which
438 * the shadow system uses) *except* in the inverse clip case. Inform the shadow
439 * system that the caster's drawing area (as opposed to its perimeter) has been
440 * clipped, so that it knows the caster can't be opaque.
441 */
442 bool casterUnclipped = !revealClip.willClip() || revealClip.hasConvexClip();
443
John Reck113e0822014-03-18 09:22:59 -0700444 DisplayListOp* shadowOp = new (alloc) DrawShadowOp(
Chris Craik024433f2014-03-26 13:19:14 -0700445 shadowMatrixXY, shadowMatrixZ,
446 caster->properties().getAlpha(), casterUnclipped,
Chris Craik8c271ca2014-03-25 10:33:01 -0700447 caster->properties().getWidth(), caster->properties().getHeight(),
448 outlinePath, revealClipPath);
John Reckd0a0b2a2014-03-20 16:28:56 -0700449 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700450 }
451
452 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
453 shadowIndex++;
454 continue;
455 }
456 }
457
458 // only the actual child DL draw needs to be in save/restore,
459 // since it modifies the renderer's matrix
460 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
461
462 DrawDisplayListOp* childOp = zTranslatedNodes[drawIndex].value;
463 RenderNode* child = childOp->mDisplayList;
464
465 renderer.concatMatrix(childOp->mTransformFromParent);
466 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700467 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700468 childOp->mSkipInOrderDraw = true;
469
470 renderer.restoreToCount(restoreTo);
471 drawIndex++;
472 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700473 handler(new (alloc) RestoreToCountOp(rootRestoreTo), PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700474}
475
476template <class T>
477void RenderNode::iterateProjectedChildren(OpenGLRenderer& renderer, T& handler, const int level) {
478 int rootRestoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
479 LinearAllocator& alloc = handler.allocator();
John Reckd0a0b2a2014-03-20 16:28:56 -0700480 ClipRectOp* clipOp = new (alloc) ClipRectOp(0, 0, properties().getWidth(), properties().getHeight(),
John Reck113e0822014-03-18 09:22:59 -0700481 SkRegion::kReplace_Op); // clip to projection surface root bounds
John Reckd0a0b2a2014-03-20 16:28:56 -0700482 handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700483
484 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
485 DrawDisplayListOp* childOp = mProjectedNodes[i];
486
487 // matrix save, concat, and restore can be done safely without allocating operations
488 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
489 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
490 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700491 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700492 childOp->mSkipInOrderDraw = true;
493 renderer.restoreToCount(restoreTo);
494 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700495 handler(new (alloc) RestoreToCountOp(rootRestoreTo), PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700496}
497
498/**
499 * This function serves both defer and replay modes, and will organize the displayList's component
500 * operations for a single frame:
501 *
502 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
503 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
504 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
505 * defer vs replay logic, per operation
506 */
507template <class T>
508void RenderNode::iterate(OpenGLRenderer& renderer, T& handler, const int level) {
509 if (CC_UNLIKELY(mDestroyed)) { // temporary debug logging
510 ALOGW("Error: %s is drawing after destruction", mName.string());
511 CRASH();
512 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700513 if (mDisplayListData->isEmpty() || properties().getAlpha() <= 0) {
John Reck113e0822014-03-18 09:22:59 -0700514 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, mName.string());
515 return;
516 }
517
518#if DEBUG_DISPLAY_LIST
519 Rect* clipRect = renderer.getClipRect();
520 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), clipRect: %.0f, %.0f, %.0f, %.0f",
521 level * 2, "", this, mName.string(), clipRect->left, clipRect->top,
522 clipRect->right, clipRect->bottom);
523#endif
524
525 LinearAllocator& alloc = handler.allocator();
526 int restoreTo = renderer.getSaveCount();
527 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
John Reckd0a0b2a2014-03-20 16:28:56 -0700528 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700529
530 DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
531 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
532
533 setViewProperties<T>(renderer, handler, level + 1);
534
Chris Craik8c271ca2014-03-25 10:33:01 -0700535 bool quickRejected = properties().getClipToBounds()
536 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -0700537 if (!quickRejected) {
538 Vector<ZDrawDisplayListOpPair> zTranslatedNodes;
539 buildZSortedChildList(zTranslatedNodes);
540
541 // for 3d root, draw children with negative z values
542 iterate3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler);
543
544 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
545 const int saveCountOffset = renderer.getSaveCount() - 1;
546 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
547 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
548 DisplayListOp *op = mDisplayListData->displayListOps[i];
549
550#if DEBUG_DISPLAY_LIST
551 op->output(level + 1);
552#endif
553
554 logBuffer.writeCommand(level, op->name());
John Reckd0a0b2a2014-03-20 16:28:56 -0700555 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700556
557 if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) {
558 iterateProjectedChildren(renderer, handler, level);
559 }
560 }
561
562 // for 3d root, draw children with positive z values
563 iterate3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler);
564 }
565
566 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
567 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -0700568 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700569 renderer.setOverrideLayerAlpha(1.0f);
570}
571
572} /* namespace uirenderer */
573} /* namespace android */