blob: 501007679308c3e135f81fd8b90f116396d73c7d [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
John Reck113e0822014-03-18 09:22:59 -070062void RenderNode::setData(DisplayListData* data) {
63 delete mDisplayListData;
64 mDisplayListData = data;
65 if (mDisplayListData) {
66 Caches::getInstance().registerFunctors(mDisplayListData->functorCount);
67 }
68}
69
70/**
71 * This function is a simplified version of replay(), where we simply retrieve and log the
72 * display list. This function should remain in sync with the replay() function.
73 */
74void RenderNode::output(uint32_t level) {
75 ALOGD("%*sStart display list (%p, %s, render=%d)", (level - 1) * 2, "", this,
76 mName.string(), isRenderable());
77 ALOGD("%*s%s %d", level * 2, "", "Save",
78 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
79
John Reckd0a0b2a2014-03-20 16:28:56 -070080 properties().debugOutputProperties(level);
John Reck113e0822014-03-18 09:22:59 -070081 int flags = DisplayListOp::kOpLogFlag_Recurse;
82 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
83 mDisplayListData->displayListOps[i]->output(level, flags);
84 }
85
86 ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, mName.string());
87}
88
John Reckd0a0b2a2014-03-20 16:28:56 -070089void RenderNode::updateProperties() {
90 if (mNeedsPropertiesSync) {
91 mNeedsPropertiesSync = false;
92 mProperties = mStagingProperties;
John Reck113e0822014-03-18 09:22:59 -070093 }
94
John Reck5bf11bb2014-03-25 10:22:09 -070095 if (mDisplayListData) {
John Reck087bc0c2014-04-04 16:20:08 -070096 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
97 RenderNode* childNode = mDisplayListData->children()[i]->mDisplayList;
John Reck5bf11bb2014-03-25 10:22:09 -070098 childNode->updateProperties();
99 }
John Reck113e0822014-03-18 09:22:59 -0700100 }
101}
102
John Reck668f0e32014-03-26 15:10:40 -0700103bool RenderNode::hasFunctors() {
104 if (!mDisplayListData) return false;
105
106 if (mDisplayListData->functorCount) {
107 return true;
108 }
109
John Reck087bc0c2014-04-04 16:20:08 -0700110 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
111 RenderNode* childNode = mDisplayListData->children()[i]->mDisplayList;
John Reck668f0e32014-03-26 15:10:40 -0700112 if (childNode->hasFunctors()) {
113 return true;
114 }
115 }
116
117 return false;
118}
119
John Reck113e0822014-03-18 09:22:59 -0700120/*
121 * For property operations, we pass a savecount of 0, since the operations aren't part of the
122 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
John Reckd0a0b2a2014-03-20 16:28:56 -0700123 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
John Reck113e0822014-03-18 09:22:59 -0700124 */
125#define PROPERTY_SAVECOUNT 0
126
127template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700128void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700129#if DEBUG_DISPLAY_LIST
Chris Craikb265e2c2014-03-27 15:50:09 -0700130 properties().debugOutputProperties(handler.level() + 1);
John Reck113e0822014-03-18 09:22:59 -0700131#endif
John Reckd0a0b2a2014-03-20 16:28:56 -0700132 if (properties().getLeft() != 0 || properties().getTop() != 0) {
133 renderer.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700134 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700135 if (properties().getStaticMatrix()) {
136 renderer.concatMatrix(properties().getStaticMatrix());
137 } else if (properties().getAnimationMatrix()) {
138 renderer.concatMatrix(properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700139 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700140 if (properties().getMatrixFlags() != 0) {
141 if (properties().getMatrixFlags() == TRANSLATION) {
142 renderer.translate(properties().getTranslationX(), properties().getTranslationY());
John Reck113e0822014-03-18 09:22:59 -0700143 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700144 renderer.concatMatrix(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700145 }
146 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700147 bool clipToBoundsNeeded = properties().getCaching() ? false : properties().getClipToBounds();
148 if (properties().getAlpha() < 1) {
149 if (properties().getCaching()) {
150 renderer.setOverrideLayerAlpha(properties().getAlpha());
151 } else if (!properties().getHasOverlappingRendering()) {
152 renderer.scaleAlpha(properties().getAlpha());
John Reck113e0822014-03-18 09:22:59 -0700153 } else {
154 // TODO: should be able to store the size of a DL at record time and not
155 // have to pass it into this call. In fact, this information might be in the
156 // location/size info that we store with the new native transform data.
157 int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
158 if (clipToBoundsNeeded) {
159 saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
160 clipToBoundsNeeded = false; // clipping done by saveLayer
161 }
162
163 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
Chris Craik8c271ca2014-03-25 10:33:01 -0700164 0, 0, properties().getWidth(), properties().getHeight(),
165 properties().getAlpha() * 255, saveFlags);
John Reckd0a0b2a2014-03-20 16:28:56 -0700166 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700167 }
168 }
169 if (clipToBoundsNeeded) {
Chris Craik8c271ca2014-03-25 10:33:01 -0700170 ClipRectOp* op = new (handler.allocator()) ClipRectOp(
171 0, 0, properties().getWidth(), properties().getHeight(), SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700172 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700173 }
Chris Craik8c271ca2014-03-25 10:33:01 -0700174
175 if (CC_UNLIKELY(properties().hasClippingPath())) {
176 // TODO: optimize for round rect/circle clipping
177 const SkPath* path = properties().getClippingPath();
178 ClipPathOp* op = new (handler.allocator()) ClipPathOp(path, SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700179 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700180 }
181}
182
183/**
184 * Apply property-based transformations to input matrix
185 *
186 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
187 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
188 */
189void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700190 if (properties().getLeft() != 0 || properties().getTop() != 0) {
191 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700192 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700193 if (properties().getStaticMatrix()) {
194 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700195 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700196 } else if (properties().getAnimationMatrix()) {
197 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700198 matrix.multiply(anim);
199 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700200 if (properties().getMatrixFlags() != 0) {
201 if (properties().getMatrixFlags() == TRANSLATION) {
202 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
203 true3dTransform ? properties().getTranslationZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700204 } else {
205 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700206 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700207 } else {
208 mat4 true3dMat;
209 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700210 properties().getPivotX() + properties().getTranslationX(),
211 properties().getPivotY() + properties().getTranslationY(),
212 properties().getTranslationZ());
213 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
214 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
215 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
216 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
217 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700218
219 matrix.multiply(true3dMat);
220 }
221 }
222 }
223}
224
225/**
226 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
227 *
228 * This should be called before a call to defer() or drawDisplayList()
229 *
230 * Each DisplayList that serves as a 3d root builds its list of composited children,
231 * which are flagged to not draw in the standard draw loop.
232 */
233void RenderNode::computeOrdering() {
234 ATRACE_CALL();
235 mProjectedNodes.clear();
236
237 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
238 // transform properties are applied correctly to top level children
239 if (mDisplayListData == NULL) return;
John Reck087bc0c2014-04-04 16:20:08 -0700240 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
241 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700242 childOp->mDisplayList->computeOrderingImpl(childOp,
243 &mProjectedNodes, &mat4::identity());
244 }
245}
246
247void RenderNode::computeOrderingImpl(
248 DrawDisplayListOp* opState,
249 Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface,
250 const mat4* transformFromProjectionSurface) {
251 mProjectedNodes.clear();
252 if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
253
254 // TODO: should avoid this calculation in most cases
255 // TODO: just calculate single matrix, down to all leaf composited elements
256 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
257 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
258
John Reckd0a0b2a2014-03-20 16:28:56 -0700259 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700260 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
261 opState->mSkipInOrderDraw = true;
262 opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
263 compositedChildrenOfProjectionSurface->add(opState);
264 } else {
265 // standard in order draw
266 opState->mSkipInOrderDraw = false;
267 }
268
John Reck087bc0c2014-04-04 16:20:08 -0700269 if (mDisplayListData->children().size() > 0) {
John Reck113e0822014-03-18 09:22:59 -0700270 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
271 bool haveAppliedPropertiesToProjection = false;
John Reck087bc0c2014-04-04 16:20:08 -0700272 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
273 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700274 RenderNode* child = childOp->mDisplayList;
275
276 Vector<DrawDisplayListOp*>* projectionChildren = NULL;
277 const mat4* projectionTransform = NULL;
John Reckd0a0b2a2014-03-20 16:28:56 -0700278 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700279 // if receiving projections, collect projecting descendent
280
281 // Note that if a direct descendent is projecting backwards, we pass it's
282 // grandparent projection collection, since it shouldn't project onto it's
283 // parent, where it will already be drawing.
284 projectionChildren = &mProjectedNodes;
285 projectionTransform = &mat4::identity();
286 } else {
287 if (!haveAppliedPropertiesToProjection) {
288 applyViewPropertyTransforms(localTransformFromProjectionSurface);
289 haveAppliedPropertiesToProjection = true;
290 }
291 projectionChildren = compositedChildrenOfProjectionSurface;
292 projectionTransform = &localTransformFromProjectionSurface;
293 }
294 child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
295 }
296 }
John Reck113e0822014-03-18 09:22:59 -0700297}
298
299class DeferOperationHandler {
300public:
301 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
302 : mDeferStruct(deferStruct), mLevel(level) {}
303 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
304 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
305 }
306 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700307 inline void startMark(const char* name) {} // do nothing
308 inline void endMark() {}
309 inline int level() { return mLevel; }
310 inline int replayFlags() { return mDeferStruct.mReplayFlags; }
John Reck113e0822014-03-18 09:22:59 -0700311
312private:
313 DeferStateStruct& mDeferStruct;
314 const int mLevel;
315};
316
Chris Craikb265e2c2014-03-27 15:50:09 -0700317void RenderNode::deferNodeTree(DeferStateStruct& deferStruct) {
318 DeferOperationHandler handler(deferStruct, 0);
319 if (properties().getTranslationZ() > 0.0f) issueDrawShadowOperation(Matrix4::identity(), handler);
320 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
321}
322
323void RenderNode::deferNodeInParent(DeferStateStruct& deferStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700324 DeferOperationHandler handler(deferStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700325 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700326}
327
328class ReplayOperationHandler {
329public:
330 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
331 : mReplayStruct(replayStruct), mLevel(level) {}
332 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
333#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
John Reckd0a0b2a2014-03-20 16:28:56 -0700334 properties().getReplayStruct().mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700335#endif
336 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
337 }
338 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700339 inline void startMark(const char* name) {
340 mReplayStruct.mRenderer.startMark(name);
341 }
342 inline void endMark() {
343 mReplayStruct.mRenderer.endMark();
344 DISPLAY_LIST_LOGD("%*sDone (%p, %s), returning %d", level * 2, "", this, mName.string(),
345 mReplayStruct.mDrawGlStatus);
346 }
347 inline int level() { return mLevel; }
348 inline int replayFlags() { return mReplayStruct.mReplayFlags; }
John Reck113e0822014-03-18 09:22:59 -0700349
350private:
351 ReplayStateStruct& mReplayStruct;
352 const int mLevel;
353};
354
Chris Craikb265e2c2014-03-27 15:50:09 -0700355void RenderNode::replayNodeTree(ReplayStateStruct& replayStruct) {
356 ReplayOperationHandler handler(replayStruct, 0);
357 if (properties().getTranslationZ() > 0.0f) issueDrawShadowOperation(Matrix4::identity(), handler);
358 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
359}
360
361void RenderNode::replayNodeInParent(ReplayStateStruct& replayStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700362 ReplayOperationHandler handler(replayStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700363 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700364}
365
366void RenderNode::buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes) {
John Reck087bc0c2014-04-04 16:20:08 -0700367 if (mDisplayListData == NULL || mDisplayListData->children().size() == 0) return;
John Reck113e0822014-03-18 09:22:59 -0700368
John Reck087bc0c2014-04-04 16:20:08 -0700369 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
370 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700371 RenderNode* child = childOp->mDisplayList;
John Reckd0a0b2a2014-03-20 16:28:56 -0700372 float childZ = child->properties().getTranslationZ();
John Reck113e0822014-03-18 09:22:59 -0700373
374 if (childZ != 0.0f) {
375 zTranslatedNodes.add(ZDrawDisplayListOpPair(childZ, childOp));
376 childOp->mSkipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700377 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700378 // regular, in order drawing DisplayList
379 childOp->mSkipInOrderDraw = false;
380 }
381 }
382
383 // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order)
384 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
385}
386
Chris Craikb265e2c2014-03-27 15:50:09 -0700387template <class T>
388void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
389 if (properties().getAlpha() <= 0.0f) return;
390
391 mat4 shadowMatrixXY(transformFromParent);
392 applyViewPropertyTransforms(shadowMatrixXY);
393
394 // Z matrix needs actual 3d transformation, so mapped z values will be correct
395 mat4 shadowMatrixZ(transformFromParent);
396 applyViewPropertyTransforms(shadowMatrixZ, true);
397
398 const SkPath* outlinePath = properties().getOutline().getPath();
399 const RevealClip& revealClip = properties().getRevealClip();
400 const SkPath* revealClipPath = revealClip.hasConvexClip()
401 ? revealClip.getPath() : NULL; // only pass the reveal clip's path if it's convex
402
403 /**
404 * The drawing area of the caster is always the same as the its perimeter (which
405 * the shadow system uses) *except* in the inverse clip case. Inform the shadow
406 * system that the caster's drawing area (as opposed to its perimeter) has been
407 * clipped, so that it knows the caster can't be opaque.
408 */
409 bool casterUnclipped = !revealClip.willClip() || revealClip.hasConvexClip();
410
411 DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp(
412 shadowMatrixXY, shadowMatrixZ,
413 properties().getAlpha(), casterUnclipped,
414 properties().getWidth(), properties().getHeight(),
415 outlinePath, revealClipPath);
416 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
417}
418
John Reck113e0822014-03-18 09:22:59 -0700419#define SHADOW_DELTA 0.1f
420
421template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700422void RenderNode::issueOperationsOf3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes,
John Reck113e0822014-03-18 09:22:59 -0700423 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) {
424 const int size = zTranslatedNodes.size();
425 if (size == 0
426 || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
427 || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
428 // no 3d children to draw
429 return;
430 }
431
John Reck113e0822014-03-18 09:22:59 -0700432 /**
433 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
434 * with very similar Z heights to draw together.
435 *
436 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
437 * underneath both, and neither's shadow is drawn on top of the other.
438 */
439 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
440 size_t drawIndex, shadowIndex, endIndex;
441 if (mode == kNegativeZChildren) {
442 drawIndex = 0;
443 endIndex = nonNegativeIndex;
444 shadowIndex = endIndex; // draw no shadows
445 } else {
446 drawIndex = nonNegativeIndex;
447 endIndex = size;
448 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
449 }
450 float lastCasterZ = 0.0f;
451 while (shadowIndex < endIndex || drawIndex < endIndex) {
452 if (shadowIndex < endIndex) {
453 DrawDisplayListOp* casterOp = zTranslatedNodes[shadowIndex].value;
454 RenderNode* caster = casterOp->mDisplayList;
455 const float casterZ = zTranslatedNodes[shadowIndex].key;
456 // attempt to render the shadow if the caster about to be drawn is its caster,
457 // OR if its caster's Z value is similar to the previous potential caster
458 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700459 caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
John Reck113e0822014-03-18 09:22:59 -0700460
461 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
462 shadowIndex++;
463 continue;
464 }
465 }
466
467 // only the actual child DL draw needs to be in save/restore,
468 // since it modifies the renderer's matrix
469 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
470
471 DrawDisplayListOp* childOp = zTranslatedNodes[drawIndex].value;
472 RenderNode* child = childOp->mDisplayList;
473
474 renderer.concatMatrix(childOp->mTransformFromParent);
475 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700476 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700477 childOp->mSkipInOrderDraw = true;
478
479 renderer.restoreToCount(restoreTo);
480 drawIndex++;
481 }
John Reck113e0822014-03-18 09:22:59 -0700482}
483
484template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700485void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700486 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
487 DrawDisplayListOp* childOp = mProjectedNodes[i];
488
489 // matrix save, concat, and restore can be done safely without allocating operations
490 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
491 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
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 renderer.restoreToCount(restoreTo);
496 }
John Reck113e0822014-03-18 09:22:59 -0700497}
498
499/**
500 * This function serves both defer and replay modes, and will organize the displayList's component
501 * operations for a single frame:
502 *
503 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
504 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
505 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
506 * defer vs replay logic, per operation
507 */
508template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700509void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
510 const int level = handler.level();
John Reck113e0822014-03-18 09:22:59 -0700511 if (CC_UNLIKELY(mDestroyed)) { // temporary debug logging
512 ALOGW("Error: %s is drawing after destruction", mName.string());
513 CRASH();
514 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700515 if (mDisplayListData->isEmpty() || properties().getAlpha() <= 0) {
John Reck113e0822014-03-18 09:22:59 -0700516 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, mName.string());
517 return;
518 }
519
Chris Craikb265e2c2014-03-27 15:50:09 -0700520 handler.startMark(mName.string());
521
John Reck113e0822014-03-18 09:22:59 -0700522#if DEBUG_DISPLAY_LIST
523 Rect* clipRect = renderer.getClipRect();
524 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), clipRect: %.0f, %.0f, %.0f, %.0f",
525 level * 2, "", this, mName.string(), clipRect->left, clipRect->top,
526 clipRect->right, clipRect->bottom);
527#endif
528
529 LinearAllocator& alloc = handler.allocator();
530 int restoreTo = renderer.getSaveCount();
531 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
John Reckd0a0b2a2014-03-20 16:28:56 -0700532 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700533
534 DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
535 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
536
Chris Craikb265e2c2014-03-27 15:50:09 -0700537 setViewProperties<T>(renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700538
Chris Craik8c271ca2014-03-25 10:33:01 -0700539 bool quickRejected = properties().getClipToBounds()
540 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -0700541 if (!quickRejected) {
542 Vector<ZDrawDisplayListOpPair> zTranslatedNodes;
543 buildZSortedChildList(zTranslatedNodes);
544
545 // for 3d root, draw children with negative z values
Chris Craikb265e2c2014-03-27 15:50:09 -0700546 issueOperationsOf3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700547
548 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
549 const int saveCountOffset = renderer.getSaveCount() - 1;
550 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
551 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
552 DisplayListOp *op = mDisplayListData->displayListOps[i];
553
554#if DEBUG_DISPLAY_LIST
555 op->output(level + 1);
556#endif
John Reck113e0822014-03-18 09:22:59 -0700557 logBuffer.writeCommand(level, op->name());
John Reckd0a0b2a2014-03-20 16:28:56 -0700558 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700559
560 if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700561 issueOperationsOfProjectedChildren(renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700562 }
563 }
564
565 // for 3d root, draw children with positive z values
Chris Craikb265e2c2014-03-27 15:50:09 -0700566 issueOperationsOf3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700567 }
568
569 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
570 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -0700571 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700572 renderer.setOverrideLayerAlpha(1.0f);
Chris Craikb265e2c2014-03-27 15:50:09 -0700573
574 handler.endMark();
John Reck113e0822014-03-18 09:22:59 -0700575}
576
577} /* namespace uirenderer */
578} /* namespace android */