blob: 823ae7b8d0d16546cda8a7207cdfd845d3a4c9f6 [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()
53 : mDestroyed(false)
54 , mNeedsPropertiesSync(false)
55 , mNeedsDisplayListDataSync(false)
56 , mDisplayListData(0)
57 , mStagingDisplayListData(0) {
John Reck113e0822014-03-18 09:22:59 -070058}
59
60RenderNode::~RenderNode() {
61 LOG_ALWAYS_FATAL_IF(mDestroyed, "Double destroyed DisplayList %p", this);
62
63 mDestroyed = true;
64 delete mDisplayListData;
John Reck8de65a82014-04-09 15:23:38 -070065 delete mStagingDisplayListData;
John Reck113e0822014-03-18 09:22:59 -070066}
67
John Reck8de65a82014-04-09 15:23:38 -070068void RenderNode::setStagingDisplayList(DisplayListData* data) {
69 mNeedsDisplayListDataSync = true;
70 delete mStagingDisplayListData;
71 mStagingDisplayListData = data;
72 if (mStagingDisplayListData) {
73 Caches::getInstance().registerFunctors(mStagingDisplayListData->functorCount);
John Reck113e0822014-03-18 09:22:59 -070074 }
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 Reckf4198b72014-04-09 17:00:04 -070096void RenderNode::prepareTree(TreeInfo& info) {
97 ATRACE_CALL();
98
99 prepareTreeImpl(info);
100}
101
102void RenderNode::prepareTreeImpl(TreeInfo& info) {
103 pushStagingChanges(info);
104 prepareSubTree(info, mDisplayListData);
105}
106
107void RenderNode::pushStagingChanges(TreeInfo& info) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700108 if (mNeedsPropertiesSync) {
109 mNeedsPropertiesSync = false;
110 mProperties = mStagingProperties;
John Reck113e0822014-03-18 09:22:59 -0700111 }
John Reck8de65a82014-04-09 15:23:38 -0700112 if (mNeedsDisplayListDataSync) {
113 mNeedsDisplayListDataSync = false;
114 // Do a push pass on the old tree to handle freeing DisplayListData
115 // that are no longer used
John Reckf4198b72014-04-09 17:00:04 -0700116 TreeInfo oldTreeInfo = {0};
117 prepareSubTree(oldTreeInfo, mDisplayListData);
118 // TODO: The damage for the old tree should be accounted for
John Reck8de65a82014-04-09 15:23:38 -0700119 delete mDisplayListData;
120 mDisplayListData = mStagingDisplayListData;
121 mStagingDisplayListData = 0;
122 }
John Reck8de65a82014-04-09 15:23:38 -0700123}
124
John Reckf4198b72014-04-09 17:00:04 -0700125void RenderNode::prepareSubTree(TreeInfo& info, DisplayListData* subtree) {
John Reck8de65a82014-04-09 15:23:38 -0700126 if (subtree) {
John Reckf4198b72014-04-09 17:00:04 -0700127 if (!info.hasFunctors) {
128 info.hasFunctors = subtree->functorCount;
129 }
John Reck8de65a82014-04-09 15:23:38 -0700130 for (size_t i = 0; i < subtree->children().size(); i++) {
131 RenderNode* childNode = subtree->children()[i]->mDisplayList;
John Reckf4198b72014-04-09 17:00:04 -0700132 childNode->prepareTreeImpl(info);
John Reck5bf11bb2014-03-25 10:22:09 -0700133 }
John Reck113e0822014-03-18 09:22:59 -0700134 }
135}
136
137/*
138 * For property operations, we pass a savecount of 0, since the operations aren't part of the
139 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
John Reckd0a0b2a2014-03-20 16:28:56 -0700140 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
John Reck113e0822014-03-18 09:22:59 -0700141 */
142#define PROPERTY_SAVECOUNT 0
143
144template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700145void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700146#if DEBUG_DISPLAY_LIST
Chris Craikb265e2c2014-03-27 15:50:09 -0700147 properties().debugOutputProperties(handler.level() + 1);
John Reck113e0822014-03-18 09:22:59 -0700148#endif
John Reckd0a0b2a2014-03-20 16:28:56 -0700149 if (properties().getLeft() != 0 || properties().getTop() != 0) {
150 renderer.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700151 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700152 if (properties().getStaticMatrix()) {
153 renderer.concatMatrix(properties().getStaticMatrix());
154 } else if (properties().getAnimationMatrix()) {
155 renderer.concatMatrix(properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700156 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700157 if (properties().getMatrixFlags() != 0) {
158 if (properties().getMatrixFlags() == TRANSLATION) {
159 renderer.translate(properties().getTranslationX(), properties().getTranslationY());
John Reck113e0822014-03-18 09:22:59 -0700160 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700161 renderer.concatMatrix(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700162 }
163 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700164 bool clipToBoundsNeeded = properties().getCaching() ? false : properties().getClipToBounds();
165 if (properties().getAlpha() < 1) {
166 if (properties().getCaching()) {
167 renderer.setOverrideLayerAlpha(properties().getAlpha());
168 } else if (!properties().getHasOverlappingRendering()) {
169 renderer.scaleAlpha(properties().getAlpha());
John Reck113e0822014-03-18 09:22:59 -0700170 } else {
171 // TODO: should be able to store the size of a DL at record time and not
172 // have to pass it into this call. In fact, this information might be in the
173 // location/size info that we store with the new native transform data.
174 int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
175 if (clipToBoundsNeeded) {
176 saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
177 clipToBoundsNeeded = false; // clipping done by saveLayer
178 }
179
180 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
Chris Craik8c271ca2014-03-25 10:33:01 -0700181 0, 0, properties().getWidth(), properties().getHeight(),
182 properties().getAlpha() * 255, saveFlags);
John Reckd0a0b2a2014-03-20 16:28:56 -0700183 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700184 }
185 }
186 if (clipToBoundsNeeded) {
Chris Craik8c271ca2014-03-25 10:33:01 -0700187 ClipRectOp* op = new (handler.allocator()) ClipRectOp(
188 0, 0, properties().getWidth(), properties().getHeight(), SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700189 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700190 }
Chris Craik8c271ca2014-03-25 10:33:01 -0700191
192 if (CC_UNLIKELY(properties().hasClippingPath())) {
193 // TODO: optimize for round rect/circle clipping
194 const SkPath* path = properties().getClippingPath();
195 ClipPathOp* op = new (handler.allocator()) ClipPathOp(path, SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700196 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700197 }
198}
199
200/**
201 * Apply property-based transformations to input matrix
202 *
203 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
204 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
205 */
206void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700207 if (properties().getLeft() != 0 || properties().getTop() != 0) {
208 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700209 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700210 if (properties().getStaticMatrix()) {
211 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700212 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700213 } else if (properties().getAnimationMatrix()) {
214 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700215 matrix.multiply(anim);
216 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700217 if (properties().getMatrixFlags() != 0) {
218 if (properties().getMatrixFlags() == TRANSLATION) {
219 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
220 true3dTransform ? properties().getTranslationZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700221 } else {
222 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700223 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700224 } else {
225 mat4 true3dMat;
226 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700227 properties().getPivotX() + properties().getTranslationX(),
228 properties().getPivotY() + properties().getTranslationY(),
229 properties().getTranslationZ());
230 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
231 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
232 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
233 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
234 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700235
236 matrix.multiply(true3dMat);
237 }
238 }
239 }
240}
241
242/**
243 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
244 *
245 * This should be called before a call to defer() or drawDisplayList()
246 *
247 * Each DisplayList that serves as a 3d root builds its list of composited children,
248 * which are flagged to not draw in the standard draw loop.
249 */
250void RenderNode::computeOrdering() {
251 ATRACE_CALL();
252 mProjectedNodes.clear();
253
254 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
255 // transform properties are applied correctly to top level children
256 if (mDisplayListData == NULL) return;
John Reck087bc0c2014-04-04 16:20:08 -0700257 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
258 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700259 childOp->mDisplayList->computeOrderingImpl(childOp,
260 &mProjectedNodes, &mat4::identity());
261 }
262}
263
264void RenderNode::computeOrderingImpl(
265 DrawDisplayListOp* opState,
266 Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface,
267 const mat4* transformFromProjectionSurface) {
268 mProjectedNodes.clear();
269 if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
270
271 // TODO: should avoid this calculation in most cases
272 // TODO: just calculate single matrix, down to all leaf composited elements
273 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
274 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
275
John Reckd0a0b2a2014-03-20 16:28:56 -0700276 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700277 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
278 opState->mSkipInOrderDraw = true;
279 opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
280 compositedChildrenOfProjectionSurface->add(opState);
281 } else {
282 // standard in order draw
283 opState->mSkipInOrderDraw = false;
284 }
285
John Reck087bc0c2014-04-04 16:20:08 -0700286 if (mDisplayListData->children().size() > 0) {
John Reck113e0822014-03-18 09:22:59 -0700287 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
288 bool haveAppliedPropertiesToProjection = false;
John Reck087bc0c2014-04-04 16:20:08 -0700289 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
290 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700291 RenderNode* child = childOp->mDisplayList;
292
293 Vector<DrawDisplayListOp*>* projectionChildren = NULL;
294 const mat4* projectionTransform = NULL;
John Reckd0a0b2a2014-03-20 16:28:56 -0700295 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700296 // if receiving projections, collect projecting descendent
297
298 // Note that if a direct descendent is projecting backwards, we pass it's
299 // grandparent projection collection, since it shouldn't project onto it's
300 // parent, where it will already be drawing.
301 projectionChildren = &mProjectedNodes;
302 projectionTransform = &mat4::identity();
303 } else {
304 if (!haveAppliedPropertiesToProjection) {
305 applyViewPropertyTransforms(localTransformFromProjectionSurface);
306 haveAppliedPropertiesToProjection = true;
307 }
308 projectionChildren = compositedChildrenOfProjectionSurface;
309 projectionTransform = &localTransformFromProjectionSurface;
310 }
311 child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
312 }
313 }
John Reck113e0822014-03-18 09:22:59 -0700314}
315
316class DeferOperationHandler {
317public:
318 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
319 : mDeferStruct(deferStruct), mLevel(level) {}
320 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
321 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
322 }
323 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700324 inline void startMark(const char* name) {} // do nothing
325 inline void endMark() {}
326 inline int level() { return mLevel; }
327 inline int replayFlags() { return mDeferStruct.mReplayFlags; }
John Reck113e0822014-03-18 09:22:59 -0700328
329private:
330 DeferStateStruct& mDeferStruct;
331 const int mLevel;
332};
333
Chris Craikb265e2c2014-03-27 15:50:09 -0700334void RenderNode::deferNodeTree(DeferStateStruct& deferStruct) {
335 DeferOperationHandler handler(deferStruct, 0);
336 if (properties().getTranslationZ() > 0.0f) issueDrawShadowOperation(Matrix4::identity(), handler);
337 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
338}
339
340void RenderNode::deferNodeInParent(DeferStateStruct& deferStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700341 DeferOperationHandler handler(deferStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700342 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700343}
344
345class ReplayOperationHandler {
346public:
347 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
348 : mReplayStruct(replayStruct), mLevel(level) {}
349 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
350#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
John Reckd0a0b2a2014-03-20 16:28:56 -0700351 properties().getReplayStruct().mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700352#endif
353 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
354 }
355 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700356 inline void startMark(const char* name) {
357 mReplayStruct.mRenderer.startMark(name);
358 }
359 inline void endMark() {
360 mReplayStruct.mRenderer.endMark();
361 DISPLAY_LIST_LOGD("%*sDone (%p, %s), returning %d", level * 2, "", this, mName.string(),
362 mReplayStruct.mDrawGlStatus);
363 }
364 inline int level() { return mLevel; }
365 inline int replayFlags() { return mReplayStruct.mReplayFlags; }
John Reck113e0822014-03-18 09:22:59 -0700366
367private:
368 ReplayStateStruct& mReplayStruct;
369 const int mLevel;
370};
371
Chris Craikb265e2c2014-03-27 15:50:09 -0700372void RenderNode::replayNodeTree(ReplayStateStruct& replayStruct) {
373 ReplayOperationHandler handler(replayStruct, 0);
374 if (properties().getTranslationZ() > 0.0f) issueDrawShadowOperation(Matrix4::identity(), handler);
375 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
376}
377
378void RenderNode::replayNodeInParent(ReplayStateStruct& replayStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700379 ReplayOperationHandler handler(replayStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700380 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700381}
382
383void RenderNode::buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes) {
John Reck087bc0c2014-04-04 16:20:08 -0700384 if (mDisplayListData == NULL || mDisplayListData->children().size() == 0) return;
John Reck113e0822014-03-18 09:22:59 -0700385
John Reck087bc0c2014-04-04 16:20:08 -0700386 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
387 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700388 RenderNode* child = childOp->mDisplayList;
John Reckd0a0b2a2014-03-20 16:28:56 -0700389 float childZ = child->properties().getTranslationZ();
John Reck113e0822014-03-18 09:22:59 -0700390
391 if (childZ != 0.0f) {
392 zTranslatedNodes.add(ZDrawDisplayListOpPair(childZ, childOp));
393 childOp->mSkipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700394 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700395 // regular, in order drawing DisplayList
396 childOp->mSkipInOrderDraw = false;
397 }
398 }
399
400 // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order)
401 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
402}
403
Chris Craikb265e2c2014-03-27 15:50:09 -0700404template <class T>
405void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
406 if (properties().getAlpha() <= 0.0f) return;
407
408 mat4 shadowMatrixXY(transformFromParent);
409 applyViewPropertyTransforms(shadowMatrixXY);
410
411 // Z matrix needs actual 3d transformation, so mapped z values will be correct
412 mat4 shadowMatrixZ(transformFromParent);
413 applyViewPropertyTransforms(shadowMatrixZ, true);
414
415 const SkPath* outlinePath = properties().getOutline().getPath();
416 const RevealClip& revealClip = properties().getRevealClip();
417 const SkPath* revealClipPath = revealClip.hasConvexClip()
418 ? revealClip.getPath() : NULL; // only pass the reveal clip's path if it's convex
419
420 /**
421 * The drawing area of the caster is always the same as the its perimeter (which
422 * the shadow system uses) *except* in the inverse clip case. Inform the shadow
423 * system that the caster's drawing area (as opposed to its perimeter) has been
424 * clipped, so that it knows the caster can't be opaque.
425 */
426 bool casterUnclipped = !revealClip.willClip() || revealClip.hasConvexClip();
427
428 DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp(
429 shadowMatrixXY, shadowMatrixZ,
430 properties().getAlpha(), casterUnclipped,
431 properties().getWidth(), properties().getHeight(),
432 outlinePath, revealClipPath);
433 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
434}
435
John Reck113e0822014-03-18 09:22:59 -0700436#define SHADOW_DELTA 0.1f
437
438template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700439void RenderNode::issueOperationsOf3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes,
John Reck113e0822014-03-18 09:22:59 -0700440 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) {
441 const int size = zTranslatedNodes.size();
442 if (size == 0
443 || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
444 || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
445 // no 3d children to draw
446 return;
447 }
448
John Reck113e0822014-03-18 09:22:59 -0700449 /**
450 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
451 * with very similar Z heights to draw together.
452 *
453 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
454 * underneath both, and neither's shadow is drawn on top of the other.
455 */
456 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
457 size_t drawIndex, shadowIndex, endIndex;
458 if (mode == kNegativeZChildren) {
459 drawIndex = 0;
460 endIndex = nonNegativeIndex;
461 shadowIndex = endIndex; // draw no shadows
462 } else {
463 drawIndex = nonNegativeIndex;
464 endIndex = size;
465 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
466 }
467 float lastCasterZ = 0.0f;
468 while (shadowIndex < endIndex || drawIndex < endIndex) {
469 if (shadowIndex < endIndex) {
470 DrawDisplayListOp* casterOp = zTranslatedNodes[shadowIndex].value;
471 RenderNode* caster = casterOp->mDisplayList;
472 const float casterZ = zTranslatedNodes[shadowIndex].key;
473 // attempt to render the shadow if the caster about to be drawn is its caster,
474 // OR if its caster's Z value is similar to the previous potential caster
475 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700476 caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
John Reck113e0822014-03-18 09:22:59 -0700477
478 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
479 shadowIndex++;
480 continue;
481 }
482 }
483
484 // only the actual child DL draw needs to be in save/restore,
485 // since it modifies the renderer's matrix
486 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
487
488 DrawDisplayListOp* childOp = zTranslatedNodes[drawIndex].value;
489 RenderNode* child = childOp->mDisplayList;
490
491 renderer.concatMatrix(childOp->mTransformFromParent);
492 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700493 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700494 childOp->mSkipInOrderDraw = true;
495
496 renderer.restoreToCount(restoreTo);
497 drawIndex++;
498 }
John Reck113e0822014-03-18 09:22:59 -0700499}
500
501template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700502void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700503 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
504 DrawDisplayListOp* childOp = mProjectedNodes[i];
505
506 // matrix save, concat, and restore can be done safely without allocating operations
507 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
508 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
509 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700510 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700511 childOp->mSkipInOrderDraw = true;
512 renderer.restoreToCount(restoreTo);
513 }
John Reck113e0822014-03-18 09:22:59 -0700514}
515
516/**
517 * This function serves both defer and replay modes, and will organize the displayList's component
518 * operations for a single frame:
519 *
520 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
521 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
522 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
523 * defer vs replay logic, per operation
524 */
525template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700526void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
527 const int level = handler.level();
John Reck113e0822014-03-18 09:22:59 -0700528 if (CC_UNLIKELY(mDestroyed)) { // temporary debug logging
529 ALOGW("Error: %s is drawing after destruction", mName.string());
530 CRASH();
531 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700532 if (mDisplayListData->isEmpty() || properties().getAlpha() <= 0) {
John Reck113e0822014-03-18 09:22:59 -0700533 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, mName.string());
534 return;
535 }
536
Chris Craikb265e2c2014-03-27 15:50:09 -0700537 handler.startMark(mName.string());
538
John Reck113e0822014-03-18 09:22:59 -0700539#if DEBUG_DISPLAY_LIST
540 Rect* clipRect = renderer.getClipRect();
541 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), clipRect: %.0f, %.0f, %.0f, %.0f",
542 level * 2, "", this, mName.string(), clipRect->left, clipRect->top,
543 clipRect->right, clipRect->bottom);
544#endif
545
546 LinearAllocator& alloc = handler.allocator();
547 int restoreTo = renderer.getSaveCount();
548 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
John Reckd0a0b2a2014-03-20 16:28:56 -0700549 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700550
551 DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
552 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
553
Chris Craikb265e2c2014-03-27 15:50:09 -0700554 setViewProperties<T>(renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700555
Chris Craik8c271ca2014-03-25 10:33:01 -0700556 bool quickRejected = properties().getClipToBounds()
557 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -0700558 if (!quickRejected) {
559 Vector<ZDrawDisplayListOpPair> zTranslatedNodes;
560 buildZSortedChildList(zTranslatedNodes);
561
562 // for 3d root, draw children with negative z values
Chris Craikb265e2c2014-03-27 15:50:09 -0700563 issueOperationsOf3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700564
565 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
566 const int saveCountOffset = renderer.getSaveCount() - 1;
567 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
568 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
569 DisplayListOp *op = mDisplayListData->displayListOps[i];
570
571#if DEBUG_DISPLAY_LIST
572 op->output(level + 1);
573#endif
John Reck113e0822014-03-18 09:22:59 -0700574 logBuffer.writeCommand(level, op->name());
John Reckd0a0b2a2014-03-20 16:28:56 -0700575 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700576
577 if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700578 issueOperationsOfProjectedChildren(renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700579 }
580 }
581
582 // for 3d root, draw children with positive z values
Chris Craikb265e2c2014-03-27 15:50:09 -0700583 issueOperationsOf3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700584 }
585
586 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
587 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -0700588 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700589 renderer.setOverrideLayerAlpha(1.0f);
Chris Craikb265e2c2014-03-27 15:50:09 -0700590
591 handler.endMark();
John Reck113e0822014-03-18 09:22:59 -0700592}
593
594} /* namespace uirenderer */
595} /* namespace android */