blob: bc0658f0ae7da3bd5512a9c002192f9c9e08aee2 [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 Reck8de65a82014-04-09 15:23:38 -070052RenderNode::RenderNode()
Chris Craik143912f2014-04-11 13:47:36 -070053 : mNeedsPropertiesSync(false)
John Reck8de65a82014-04-09 15:23:38 -070054 , mNeedsDisplayListDataSync(false)
55 , mDisplayListData(0)
56 , mStagingDisplayListData(0) {
John Reck113e0822014-03-18 09:22:59 -070057}
58
59RenderNode::~RenderNode() {
John Reck113e0822014-03-18 09:22:59 -070060 delete mDisplayListData;
John Reck8de65a82014-04-09 15:23:38 -070061 delete mStagingDisplayListData;
John Reck113e0822014-03-18 09:22:59 -070062}
63
John Reck8de65a82014-04-09 15:23:38 -070064void RenderNode::setStagingDisplayList(DisplayListData* data) {
65 mNeedsDisplayListDataSync = true;
66 delete mStagingDisplayListData;
67 mStagingDisplayListData = data;
68 if (mStagingDisplayListData) {
69 Caches::getInstance().registerFunctors(mStagingDisplayListData->functorCount);
John Reck113e0822014-03-18 09:22:59 -070070 }
71}
72
73/**
74 * This function is a simplified version of replay(), where we simply retrieve and log the
75 * display list. This function should remain in sync with the replay() function.
76 */
77void RenderNode::output(uint32_t level) {
78 ALOGD("%*sStart display list (%p, %s, render=%d)", (level - 1) * 2, "", this,
79 mName.string(), isRenderable());
80 ALOGD("%*s%s %d", level * 2, "", "Save",
81 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
82
John Reckd0a0b2a2014-03-20 16:28:56 -070083 properties().debugOutputProperties(level);
John Reck113e0822014-03-18 09:22:59 -070084 int flags = DisplayListOp::kOpLogFlag_Recurse;
85 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
86 mDisplayListData->displayListOps[i]->output(level, flags);
87 }
88
89 ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, mName.string());
90}
91
John Reckf4198b72014-04-09 17:00:04 -070092void RenderNode::prepareTree(TreeInfo& info) {
93 ATRACE_CALL();
94
95 prepareTreeImpl(info);
96}
97
98void RenderNode::prepareTreeImpl(TreeInfo& info) {
99 pushStagingChanges(info);
100 prepareSubTree(info, mDisplayListData);
101}
102
103void RenderNode::pushStagingChanges(TreeInfo& info) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700104 if (mNeedsPropertiesSync) {
105 mNeedsPropertiesSync = false;
106 mProperties = mStagingProperties;
John Reck113e0822014-03-18 09:22:59 -0700107 }
John Reck8de65a82014-04-09 15:23:38 -0700108 if (mNeedsDisplayListDataSync) {
109 mNeedsDisplayListDataSync = false;
110 // Do a push pass on the old tree to handle freeing DisplayListData
111 // that are no longer used
John Reckf4198b72014-04-09 17:00:04 -0700112 TreeInfo oldTreeInfo = {0};
113 prepareSubTree(oldTreeInfo, mDisplayListData);
114 // TODO: The damage for the old tree should be accounted for
John Reck8de65a82014-04-09 15:23:38 -0700115 delete mDisplayListData;
116 mDisplayListData = mStagingDisplayListData;
117 mStagingDisplayListData = 0;
118 }
John Reck8de65a82014-04-09 15:23:38 -0700119}
120
John Reckf4198b72014-04-09 17:00:04 -0700121void RenderNode::prepareSubTree(TreeInfo& info, DisplayListData* subtree) {
John Reck8de65a82014-04-09 15:23:38 -0700122 if (subtree) {
John Reckf4198b72014-04-09 17:00:04 -0700123 if (!info.hasFunctors) {
124 info.hasFunctors = subtree->functorCount;
125 }
John Reck8de65a82014-04-09 15:23:38 -0700126 for (size_t i = 0; i < subtree->children().size(); i++) {
127 RenderNode* childNode = subtree->children()[i]->mDisplayList;
John Reckf4198b72014-04-09 17:00:04 -0700128 childNode->prepareTreeImpl(info);
John Reck5bf11bb2014-03-25 10:22:09 -0700129 }
John Reck113e0822014-03-18 09:22:59 -0700130 }
131}
132
133/*
134 * For property operations, we pass a savecount of 0, since the operations aren't part of the
135 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
John Reckd0a0b2a2014-03-20 16:28:56 -0700136 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
John Reck113e0822014-03-18 09:22:59 -0700137 */
138#define PROPERTY_SAVECOUNT 0
139
140template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700141void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700142#if DEBUG_DISPLAY_LIST
Chris Craikb265e2c2014-03-27 15:50:09 -0700143 properties().debugOutputProperties(handler.level() + 1);
John Reck113e0822014-03-18 09:22:59 -0700144#endif
John Reckd0a0b2a2014-03-20 16:28:56 -0700145 if (properties().getLeft() != 0 || properties().getTop() != 0) {
146 renderer.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700147 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700148 if (properties().getStaticMatrix()) {
149 renderer.concatMatrix(properties().getStaticMatrix());
150 } else if (properties().getAnimationMatrix()) {
151 renderer.concatMatrix(properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700152 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700153 if (properties().getMatrixFlags() != 0) {
154 if (properties().getMatrixFlags() == TRANSLATION) {
155 renderer.translate(properties().getTranslationX(), properties().getTranslationY());
John Reck113e0822014-03-18 09:22:59 -0700156 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700157 renderer.concatMatrix(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700158 }
159 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700160 bool clipToBoundsNeeded = properties().getCaching() ? false : properties().getClipToBounds();
161 if (properties().getAlpha() < 1) {
162 if (properties().getCaching()) {
163 renderer.setOverrideLayerAlpha(properties().getAlpha());
164 } else if (!properties().getHasOverlappingRendering()) {
165 renderer.scaleAlpha(properties().getAlpha());
John Reck113e0822014-03-18 09:22:59 -0700166 } else {
167 // TODO: should be able to store the size of a DL at record time and not
168 // have to pass it into this call. In fact, this information might be in the
169 // location/size info that we store with the new native transform data.
170 int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
171 if (clipToBoundsNeeded) {
172 saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
173 clipToBoundsNeeded = false; // clipping done by saveLayer
174 }
175
176 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
Chris Craik8c271ca2014-03-25 10:33:01 -0700177 0, 0, properties().getWidth(), properties().getHeight(),
178 properties().getAlpha() * 255, saveFlags);
John Reckd0a0b2a2014-03-20 16:28:56 -0700179 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700180 }
181 }
182 if (clipToBoundsNeeded) {
Chris Craik8c271ca2014-03-25 10:33:01 -0700183 ClipRectOp* op = new (handler.allocator()) ClipRectOp(
184 0, 0, properties().getWidth(), properties().getHeight(), SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700185 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700186 }
Chris Craik8c271ca2014-03-25 10:33:01 -0700187
188 if (CC_UNLIKELY(properties().hasClippingPath())) {
189 // TODO: optimize for round rect/circle clipping
190 const SkPath* path = properties().getClippingPath();
191 ClipPathOp* op = new (handler.allocator()) ClipPathOp(path, SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700192 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700193 }
194}
195
196/**
197 * Apply property-based transformations to input matrix
198 *
199 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
200 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
201 */
202void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700203 if (properties().getLeft() != 0 || properties().getTop() != 0) {
204 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700205 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700206 if (properties().getStaticMatrix()) {
207 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700208 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700209 } else if (properties().getAnimationMatrix()) {
210 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700211 matrix.multiply(anim);
212 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700213 if (properties().getMatrixFlags() != 0) {
214 if (properties().getMatrixFlags() == TRANSLATION) {
215 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
216 true3dTransform ? properties().getTranslationZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700217 } else {
218 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700219 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700220 } else {
221 mat4 true3dMat;
222 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700223 properties().getPivotX() + properties().getTranslationX(),
224 properties().getPivotY() + properties().getTranslationY(),
225 properties().getTranslationZ());
226 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
227 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
228 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
229 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
230 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700231
232 matrix.multiply(true3dMat);
233 }
234 }
235 }
236}
237
238/**
239 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
240 *
241 * This should be called before a call to defer() or drawDisplayList()
242 *
243 * Each DisplayList that serves as a 3d root builds its list of composited children,
244 * which are flagged to not draw in the standard draw loop.
245 */
246void RenderNode::computeOrdering() {
247 ATRACE_CALL();
248 mProjectedNodes.clear();
249
250 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
251 // transform properties are applied correctly to top level children
252 if (mDisplayListData == NULL) return;
John Reck087bc0c2014-04-04 16:20:08 -0700253 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
254 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700255 childOp->mDisplayList->computeOrderingImpl(childOp,
256 &mProjectedNodes, &mat4::identity());
257 }
258}
259
260void RenderNode::computeOrderingImpl(
261 DrawDisplayListOp* opState,
262 Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface,
263 const mat4* transformFromProjectionSurface) {
264 mProjectedNodes.clear();
265 if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
266
267 // TODO: should avoid this calculation in most cases
268 // TODO: just calculate single matrix, down to all leaf composited elements
269 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
270 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
271
John Reckd0a0b2a2014-03-20 16:28:56 -0700272 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700273 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
274 opState->mSkipInOrderDraw = true;
275 opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
276 compositedChildrenOfProjectionSurface->add(opState);
277 } else {
278 // standard in order draw
279 opState->mSkipInOrderDraw = false;
280 }
281
John Reck087bc0c2014-04-04 16:20:08 -0700282 if (mDisplayListData->children().size() > 0) {
John Reck113e0822014-03-18 09:22:59 -0700283 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
284 bool haveAppliedPropertiesToProjection = false;
John Reck087bc0c2014-04-04 16:20:08 -0700285 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
286 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700287 RenderNode* child = childOp->mDisplayList;
288
289 Vector<DrawDisplayListOp*>* projectionChildren = NULL;
290 const mat4* projectionTransform = NULL;
John Reckd0a0b2a2014-03-20 16:28:56 -0700291 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700292 // if receiving projections, collect projecting descendent
293
294 // Note that if a direct descendent is projecting backwards, we pass it's
295 // grandparent projection collection, since it shouldn't project onto it's
296 // parent, where it will already be drawing.
297 projectionChildren = &mProjectedNodes;
298 projectionTransform = &mat4::identity();
299 } else {
300 if (!haveAppliedPropertiesToProjection) {
301 applyViewPropertyTransforms(localTransformFromProjectionSurface);
302 haveAppliedPropertiesToProjection = true;
303 }
304 projectionChildren = compositedChildrenOfProjectionSurface;
305 projectionTransform = &localTransformFromProjectionSurface;
306 }
307 child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
308 }
309 }
John Reck113e0822014-03-18 09:22:59 -0700310}
311
312class DeferOperationHandler {
313public:
314 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
315 : mDeferStruct(deferStruct), mLevel(level) {}
316 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
317 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
318 }
319 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700320 inline void startMark(const char* name) {} // do nothing
321 inline void endMark() {}
322 inline int level() { return mLevel; }
323 inline int replayFlags() { return mDeferStruct.mReplayFlags; }
John Reck113e0822014-03-18 09:22:59 -0700324
325private:
326 DeferStateStruct& mDeferStruct;
327 const int mLevel;
328};
329
Chris Craikb265e2c2014-03-27 15:50:09 -0700330void RenderNode::deferNodeTree(DeferStateStruct& deferStruct) {
331 DeferOperationHandler handler(deferStruct, 0);
332 if (properties().getTranslationZ() > 0.0f) issueDrawShadowOperation(Matrix4::identity(), handler);
333 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
334}
335
336void RenderNode::deferNodeInParent(DeferStateStruct& deferStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700337 DeferOperationHandler handler(deferStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700338 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700339}
340
341class ReplayOperationHandler {
342public:
343 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
344 : mReplayStruct(replayStruct), mLevel(level) {}
345 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
346#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
John Reckd0a0b2a2014-03-20 16:28:56 -0700347 properties().getReplayStruct().mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700348#endif
349 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
350 }
351 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700352 inline void startMark(const char* name) {
353 mReplayStruct.mRenderer.startMark(name);
354 }
355 inline void endMark() {
356 mReplayStruct.mRenderer.endMark();
357 DISPLAY_LIST_LOGD("%*sDone (%p, %s), returning %d", level * 2, "", this, mName.string(),
358 mReplayStruct.mDrawGlStatus);
359 }
360 inline int level() { return mLevel; }
361 inline int replayFlags() { return mReplayStruct.mReplayFlags; }
John Reck113e0822014-03-18 09:22:59 -0700362
363private:
364 ReplayStateStruct& mReplayStruct;
365 const int mLevel;
366};
367
Chris Craikb265e2c2014-03-27 15:50:09 -0700368void RenderNode::replayNodeTree(ReplayStateStruct& replayStruct) {
369 ReplayOperationHandler handler(replayStruct, 0);
370 if (properties().getTranslationZ() > 0.0f) issueDrawShadowOperation(Matrix4::identity(), handler);
371 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
372}
373
374void RenderNode::replayNodeInParent(ReplayStateStruct& replayStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700375 ReplayOperationHandler handler(replayStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700376 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700377}
378
379void RenderNode::buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes) {
John Reck087bc0c2014-04-04 16:20:08 -0700380 if (mDisplayListData == NULL || mDisplayListData->children().size() == 0) return;
John Reck113e0822014-03-18 09:22:59 -0700381
John Reck087bc0c2014-04-04 16:20:08 -0700382 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
383 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700384 RenderNode* child = childOp->mDisplayList;
John Reckd0a0b2a2014-03-20 16:28:56 -0700385 float childZ = child->properties().getTranslationZ();
John Reck113e0822014-03-18 09:22:59 -0700386
387 if (childZ != 0.0f) {
388 zTranslatedNodes.add(ZDrawDisplayListOpPair(childZ, childOp));
389 childOp->mSkipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700390 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700391 // regular, in order drawing DisplayList
392 childOp->mSkipInOrderDraw = false;
393 }
394 }
395
396 // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order)
397 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
398}
399
Chris Craikb265e2c2014-03-27 15:50:09 -0700400template <class T>
401void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
402 if (properties().getAlpha() <= 0.0f) return;
403
404 mat4 shadowMatrixXY(transformFromParent);
405 applyViewPropertyTransforms(shadowMatrixXY);
406
407 // Z matrix needs actual 3d transformation, so mapped z values will be correct
408 mat4 shadowMatrixZ(transformFromParent);
409 applyViewPropertyTransforms(shadowMatrixZ, true);
410
411 const SkPath* outlinePath = properties().getOutline().getPath();
412 const RevealClip& revealClip = properties().getRevealClip();
413 const SkPath* revealClipPath = revealClip.hasConvexClip()
414 ? revealClip.getPath() : NULL; // only pass the reveal clip's path if it's convex
415
416 /**
417 * The drawing area of the caster is always the same as the its perimeter (which
418 * the shadow system uses) *except* in the inverse clip case. Inform the shadow
419 * system that the caster's drawing area (as opposed to its perimeter) has been
420 * clipped, so that it knows the caster can't be opaque.
421 */
422 bool casterUnclipped = !revealClip.willClip() || revealClip.hasConvexClip();
423
424 DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp(
425 shadowMatrixXY, shadowMatrixZ,
426 properties().getAlpha(), casterUnclipped,
427 properties().getWidth(), properties().getHeight(),
428 outlinePath, revealClipPath);
429 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
430}
431
John Reck113e0822014-03-18 09:22:59 -0700432#define SHADOW_DELTA 0.1f
433
434template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700435void RenderNode::issueOperationsOf3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes,
John Reck113e0822014-03-18 09:22:59 -0700436 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) {
437 const int size = zTranslatedNodes.size();
438 if (size == 0
439 || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
440 || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
441 // no 3d children to draw
442 return;
443 }
444
John Reck113e0822014-03-18 09:22:59 -0700445 /**
446 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
447 * with very similar Z heights to draw together.
448 *
449 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
450 * underneath both, and neither's shadow is drawn on top of the other.
451 */
452 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
453 size_t drawIndex, shadowIndex, endIndex;
454 if (mode == kNegativeZChildren) {
455 drawIndex = 0;
456 endIndex = nonNegativeIndex;
457 shadowIndex = endIndex; // draw no shadows
458 } else {
459 drawIndex = nonNegativeIndex;
460 endIndex = size;
461 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
462 }
463 float lastCasterZ = 0.0f;
464 while (shadowIndex < endIndex || drawIndex < endIndex) {
465 if (shadowIndex < endIndex) {
466 DrawDisplayListOp* casterOp = zTranslatedNodes[shadowIndex].value;
467 RenderNode* caster = casterOp->mDisplayList;
468 const float casterZ = zTranslatedNodes[shadowIndex].key;
469 // attempt to render the shadow if the caster about to be drawn is its caster,
470 // OR if its caster's Z value is similar to the previous potential caster
471 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700472 caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
John Reck113e0822014-03-18 09:22:59 -0700473
474 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
475 shadowIndex++;
476 continue;
477 }
478 }
479
480 // only the actual child DL draw needs to be in save/restore,
481 // since it modifies the renderer's matrix
482 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
483
484 DrawDisplayListOp* childOp = zTranslatedNodes[drawIndex].value;
485 RenderNode* child = childOp->mDisplayList;
486
487 renderer.concatMatrix(childOp->mTransformFromParent);
488 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700489 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700490 childOp->mSkipInOrderDraw = true;
491
492 renderer.restoreToCount(restoreTo);
493 drawIndex++;
494 }
John Reck113e0822014-03-18 09:22:59 -0700495}
496
497template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700498void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700499 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
500 DrawDisplayListOp* childOp = mProjectedNodes[i];
501
502 // matrix save, concat, and restore can be done safely without allocating operations
503 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
504 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
505 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700506 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700507 childOp->mSkipInOrderDraw = true;
508 renderer.restoreToCount(restoreTo);
509 }
John Reck113e0822014-03-18 09:22:59 -0700510}
511
512/**
513 * This function serves both defer and replay modes, and will organize the displayList's component
514 * operations for a single frame:
515 *
516 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
517 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
518 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
519 * defer vs replay logic, per operation
520 */
521template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700522void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
523 const int level = handler.level();
John Reckd0a0b2a2014-03-20 16:28:56 -0700524 if (mDisplayListData->isEmpty() || properties().getAlpha() <= 0) {
John Reck113e0822014-03-18 09:22:59 -0700525 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, mName.string());
526 return;
527 }
528
Chris Craikb265e2c2014-03-27 15:50:09 -0700529 handler.startMark(mName.string());
530
John Reck113e0822014-03-18 09:22:59 -0700531#if DEBUG_DISPLAY_LIST
532 Rect* clipRect = renderer.getClipRect();
533 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), clipRect: %.0f, %.0f, %.0f, %.0f",
534 level * 2, "", this, mName.string(), clipRect->left, clipRect->top,
535 clipRect->right, clipRect->bottom);
536#endif
537
538 LinearAllocator& alloc = handler.allocator();
539 int restoreTo = renderer.getSaveCount();
540 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
John Reckd0a0b2a2014-03-20 16:28:56 -0700541 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700542
543 DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
544 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
545
Chris Craikb265e2c2014-03-27 15:50:09 -0700546 setViewProperties<T>(renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700547
Chris Craik8c271ca2014-03-25 10:33:01 -0700548 bool quickRejected = properties().getClipToBounds()
549 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -0700550 if (!quickRejected) {
551 Vector<ZDrawDisplayListOpPair> zTranslatedNodes;
552 buildZSortedChildList(zTranslatedNodes);
553
554 // for 3d root, draw children with negative z values
Chris Craikb265e2c2014-03-27 15:50:09 -0700555 issueOperationsOf3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700556
557 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
558 const int saveCountOffset = renderer.getSaveCount() - 1;
559 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
560 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
561 DisplayListOp *op = mDisplayListData->displayListOps[i];
562
563#if DEBUG_DISPLAY_LIST
564 op->output(level + 1);
565#endif
John Reck113e0822014-03-18 09:22:59 -0700566 logBuffer.writeCommand(level, op->name());
John Reckd0a0b2a2014-03-20 16:28:56 -0700567 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700568
569 if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700570 issueOperationsOfProjectedChildren(renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700571 }
572 }
573
574 // for 3d root, draw children with positive z values
Chris Craikb265e2c2014-03-27 15:50:09 -0700575 issueOperationsOf3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700576 }
577
578 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
579 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -0700580 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700581 renderer.setOverrideLayerAlpha(1.0f);
Chris Craikb265e2c2014-03-27 15:50:09 -0700582
583 handler.endMark();
John Reck113e0822014-03-18 09:22:59 -0700584}
585
586} /* namespace uirenderer */
587} /* namespace android */