blob: cf218347ee2a9dfa91e747359223de37533f6030 [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 Reck860d1552014-04-11 19:15:05 -0700112 TreeInfo oldTreeInfo;
John Reckf4198b72014-04-09 17:00:04 -0700113 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 Reck860d1552014-04-11 19:15:05 -0700123 TextureCache& cache = Caches::getInstance().textureCache;
124 info.hasFunctors |= subtree->functorCount;
125 // TODO: Fix ownedBitmapResources to not require disabling prepareTextures
126 // and thus falling out of async drawing path.
127 if (subtree->ownedBitmapResources.size()) {
128 info.prepareTextures = false;
129 }
130 for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) {
131 info.prepareTextures = cache.prefetchAndMarkInUse(subtree->bitmapResources[i]);
John Reckf4198b72014-04-09 17:00:04 -0700132 }
John Reck8de65a82014-04-09 15:23:38 -0700133 for (size_t i = 0; i < subtree->children().size(); i++) {
134 RenderNode* childNode = subtree->children()[i]->mDisplayList;
John Reckf4198b72014-04-09 17:00:04 -0700135 childNode->prepareTreeImpl(info);
John Reck5bf11bb2014-03-25 10:22:09 -0700136 }
John Reck113e0822014-03-18 09:22:59 -0700137 }
138}
139
140/*
141 * For property operations, we pass a savecount of 0, since the operations aren't part of the
142 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
John Reckd0a0b2a2014-03-20 16:28:56 -0700143 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
John Reck113e0822014-03-18 09:22:59 -0700144 */
145#define PROPERTY_SAVECOUNT 0
146
147template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700148void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700149#if DEBUG_DISPLAY_LIST
Chris Craikb265e2c2014-03-27 15:50:09 -0700150 properties().debugOutputProperties(handler.level() + 1);
John Reck113e0822014-03-18 09:22:59 -0700151#endif
John Reckd0a0b2a2014-03-20 16:28:56 -0700152 if (properties().getLeft() != 0 || properties().getTop() != 0) {
153 renderer.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700154 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700155 if (properties().getStaticMatrix()) {
156 renderer.concatMatrix(properties().getStaticMatrix());
157 } else if (properties().getAnimationMatrix()) {
158 renderer.concatMatrix(properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700159 }
John Reckf7483e32014-04-11 08:54:47 -0700160 if (properties().hasTransformMatrix()) {
161 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700162 renderer.translate(properties().getTranslationX(), properties().getTranslationY());
John Reck113e0822014-03-18 09:22:59 -0700163 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700164 renderer.concatMatrix(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700165 }
166 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700167 bool clipToBoundsNeeded = properties().getCaching() ? false : properties().getClipToBounds();
168 if (properties().getAlpha() < 1) {
169 if (properties().getCaching()) {
170 renderer.setOverrideLayerAlpha(properties().getAlpha());
171 } else if (!properties().getHasOverlappingRendering()) {
172 renderer.scaleAlpha(properties().getAlpha());
John Reck113e0822014-03-18 09:22:59 -0700173 } else {
174 // TODO: should be able to store the size of a DL at record time and not
175 // have to pass it into this call. In fact, this information might be in the
176 // location/size info that we store with the new native transform data.
177 int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
178 if (clipToBoundsNeeded) {
179 saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
180 clipToBoundsNeeded = false; // clipping done by saveLayer
181 }
182
183 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
Chris Craik8c271ca2014-03-25 10:33:01 -0700184 0, 0, properties().getWidth(), properties().getHeight(),
185 properties().getAlpha() * 255, saveFlags);
John Reckd0a0b2a2014-03-20 16:28:56 -0700186 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700187 }
188 }
189 if (clipToBoundsNeeded) {
Chris Craik8c271ca2014-03-25 10:33:01 -0700190 ClipRectOp* op = new (handler.allocator()) ClipRectOp(
191 0, 0, properties().getWidth(), properties().getHeight(), 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 }
Chris Craik8c271ca2014-03-25 10:33:01 -0700194
195 if (CC_UNLIKELY(properties().hasClippingPath())) {
196 // TODO: optimize for round rect/circle clipping
197 const SkPath* path = properties().getClippingPath();
198 ClipPathOp* op = new (handler.allocator()) ClipPathOp(path, SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700199 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700200 }
201}
202
203/**
204 * Apply property-based transformations to input matrix
205 *
206 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
207 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
208 */
209void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700210 if (properties().getLeft() != 0 || properties().getTop() != 0) {
211 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700212 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700213 if (properties().getStaticMatrix()) {
214 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700215 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700216 } else if (properties().getAnimationMatrix()) {
217 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700218 matrix.multiply(anim);
219 }
John Reckf7483e32014-04-11 08:54:47 -0700220 if (properties().hasTransformMatrix()) {
221 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700222 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
223 true3dTransform ? properties().getTranslationZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700224 } else {
225 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700226 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700227 } else {
228 mat4 true3dMat;
229 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700230 properties().getPivotX() + properties().getTranslationX(),
231 properties().getPivotY() + properties().getTranslationY(),
232 properties().getTranslationZ());
233 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
234 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
235 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
236 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
237 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700238
239 matrix.multiply(true3dMat);
240 }
241 }
242 }
243}
244
245/**
246 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
247 *
248 * This should be called before a call to defer() or drawDisplayList()
249 *
250 * Each DisplayList that serves as a 3d root builds its list of composited children,
251 * which are flagged to not draw in the standard draw loop.
252 */
253void RenderNode::computeOrdering() {
254 ATRACE_CALL();
255 mProjectedNodes.clear();
256
257 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
258 // transform properties are applied correctly to top level children
259 if (mDisplayListData == NULL) return;
John Reck087bc0c2014-04-04 16:20:08 -0700260 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
261 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700262 childOp->mDisplayList->computeOrderingImpl(childOp,
263 &mProjectedNodes, &mat4::identity());
264 }
265}
266
267void RenderNode::computeOrderingImpl(
268 DrawDisplayListOp* opState,
269 Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface,
270 const mat4* transformFromProjectionSurface) {
271 mProjectedNodes.clear();
272 if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
273
274 // TODO: should avoid this calculation in most cases
275 // TODO: just calculate single matrix, down to all leaf composited elements
276 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
277 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
278
John Reckd0a0b2a2014-03-20 16:28:56 -0700279 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700280 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
281 opState->mSkipInOrderDraw = true;
282 opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
283 compositedChildrenOfProjectionSurface->add(opState);
284 } else {
285 // standard in order draw
286 opState->mSkipInOrderDraw = false;
287 }
288
John Reck087bc0c2014-04-04 16:20:08 -0700289 if (mDisplayListData->children().size() > 0) {
John Reck113e0822014-03-18 09:22:59 -0700290 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
291 bool haveAppliedPropertiesToProjection = false;
John Reck087bc0c2014-04-04 16:20:08 -0700292 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
293 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700294 RenderNode* child = childOp->mDisplayList;
295
296 Vector<DrawDisplayListOp*>* projectionChildren = NULL;
297 const mat4* projectionTransform = NULL;
John Reckd0a0b2a2014-03-20 16:28:56 -0700298 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700299 // if receiving projections, collect projecting descendent
300
301 // Note that if a direct descendent is projecting backwards, we pass it's
302 // grandparent projection collection, since it shouldn't project onto it's
303 // parent, where it will already be drawing.
304 projectionChildren = &mProjectedNodes;
305 projectionTransform = &mat4::identity();
306 } else {
307 if (!haveAppliedPropertiesToProjection) {
308 applyViewPropertyTransforms(localTransformFromProjectionSurface);
309 haveAppliedPropertiesToProjection = true;
310 }
311 projectionChildren = compositedChildrenOfProjectionSurface;
312 projectionTransform = &localTransformFromProjectionSurface;
313 }
314 child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
315 }
316 }
John Reck113e0822014-03-18 09:22:59 -0700317}
318
319class DeferOperationHandler {
320public:
321 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
322 : mDeferStruct(deferStruct), mLevel(level) {}
323 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
324 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
325 }
326 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700327 inline void startMark(const char* name) {} // do nothing
328 inline void endMark() {}
329 inline int level() { return mLevel; }
330 inline int replayFlags() { return mDeferStruct.mReplayFlags; }
John Reck113e0822014-03-18 09:22:59 -0700331
332private:
333 DeferStateStruct& mDeferStruct;
334 const int mLevel;
335};
336
Chris Craikb265e2c2014-03-27 15:50:09 -0700337void RenderNode::deferNodeTree(DeferStateStruct& deferStruct) {
338 DeferOperationHandler handler(deferStruct, 0);
339 if (properties().getTranslationZ() > 0.0f) issueDrawShadowOperation(Matrix4::identity(), handler);
340 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
341}
342
343void RenderNode::deferNodeInParent(DeferStateStruct& deferStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700344 DeferOperationHandler handler(deferStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700345 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700346}
347
348class ReplayOperationHandler {
349public:
350 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
351 : mReplayStruct(replayStruct), mLevel(level) {}
352 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
353#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
John Reckd0a0b2a2014-03-20 16:28:56 -0700354 properties().getReplayStruct().mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700355#endif
356 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
357 }
358 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700359 inline void startMark(const char* name) {
360 mReplayStruct.mRenderer.startMark(name);
361 }
362 inline void endMark() {
363 mReplayStruct.mRenderer.endMark();
364 DISPLAY_LIST_LOGD("%*sDone (%p, %s), returning %d", level * 2, "", this, mName.string(),
365 mReplayStruct.mDrawGlStatus);
366 }
367 inline int level() { return mLevel; }
368 inline int replayFlags() { return mReplayStruct.mReplayFlags; }
John Reck113e0822014-03-18 09:22:59 -0700369
370private:
371 ReplayStateStruct& mReplayStruct;
372 const int mLevel;
373};
374
Chris Craikb265e2c2014-03-27 15:50:09 -0700375void RenderNode::replayNodeTree(ReplayStateStruct& replayStruct) {
376 ReplayOperationHandler handler(replayStruct, 0);
377 if (properties().getTranslationZ() > 0.0f) issueDrawShadowOperation(Matrix4::identity(), handler);
378 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
379}
380
381void RenderNode::replayNodeInParent(ReplayStateStruct& replayStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700382 ReplayOperationHandler handler(replayStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700383 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700384}
385
386void RenderNode::buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes) {
John Reck087bc0c2014-04-04 16:20:08 -0700387 if (mDisplayListData == NULL || mDisplayListData->children().size() == 0) return;
John Reck113e0822014-03-18 09:22:59 -0700388
John Reck087bc0c2014-04-04 16:20:08 -0700389 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
390 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700391 RenderNode* child = childOp->mDisplayList;
John Reckd0a0b2a2014-03-20 16:28:56 -0700392 float childZ = child->properties().getTranslationZ();
John Reck113e0822014-03-18 09:22:59 -0700393
394 if (childZ != 0.0f) {
395 zTranslatedNodes.add(ZDrawDisplayListOpPair(childZ, childOp));
396 childOp->mSkipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700397 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700398 // regular, in order drawing DisplayList
399 childOp->mSkipInOrderDraw = false;
400 }
401 }
402
403 // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order)
404 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
405}
406
Chris Craikb265e2c2014-03-27 15:50:09 -0700407template <class T>
408void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
409 if (properties().getAlpha() <= 0.0f) return;
410
411 mat4 shadowMatrixXY(transformFromParent);
412 applyViewPropertyTransforms(shadowMatrixXY);
413
414 // Z matrix needs actual 3d transformation, so mapped z values will be correct
415 mat4 shadowMatrixZ(transformFromParent);
416 applyViewPropertyTransforms(shadowMatrixZ, true);
417
418 const SkPath* outlinePath = properties().getOutline().getPath();
419 const RevealClip& revealClip = properties().getRevealClip();
420 const SkPath* revealClipPath = revealClip.hasConvexClip()
421 ? revealClip.getPath() : NULL; // only pass the reveal clip's path if it's convex
422
423 /**
424 * The drawing area of the caster is always the same as the its perimeter (which
425 * the shadow system uses) *except* in the inverse clip case. Inform the shadow
426 * system that the caster's drawing area (as opposed to its perimeter) has been
427 * clipped, so that it knows the caster can't be opaque.
428 */
429 bool casterUnclipped = !revealClip.willClip() || revealClip.hasConvexClip();
430
431 DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp(
432 shadowMatrixXY, shadowMatrixZ,
433 properties().getAlpha(), casterUnclipped,
434 properties().getWidth(), properties().getHeight(),
435 outlinePath, revealClipPath);
436 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
437}
438
John Reck113e0822014-03-18 09:22:59 -0700439#define SHADOW_DELTA 0.1f
440
441template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700442void RenderNode::issueOperationsOf3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes,
John Reck113e0822014-03-18 09:22:59 -0700443 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) {
444 const int size = zTranslatedNodes.size();
445 if (size == 0
446 || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
447 || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
448 // no 3d children to draw
449 return;
450 }
451
John Reck113e0822014-03-18 09:22:59 -0700452 /**
453 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
454 * with very similar Z heights to draw together.
455 *
456 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
457 * underneath both, and neither's shadow is drawn on top of the other.
458 */
459 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
460 size_t drawIndex, shadowIndex, endIndex;
461 if (mode == kNegativeZChildren) {
462 drawIndex = 0;
463 endIndex = nonNegativeIndex;
464 shadowIndex = endIndex; // draw no shadows
465 } else {
466 drawIndex = nonNegativeIndex;
467 endIndex = size;
468 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
469 }
470 float lastCasterZ = 0.0f;
471 while (shadowIndex < endIndex || drawIndex < endIndex) {
472 if (shadowIndex < endIndex) {
473 DrawDisplayListOp* casterOp = zTranslatedNodes[shadowIndex].value;
474 RenderNode* caster = casterOp->mDisplayList;
475 const float casterZ = zTranslatedNodes[shadowIndex].key;
476 // attempt to render the shadow if the caster about to be drawn is its caster,
477 // OR if its caster's Z value is similar to the previous potential caster
478 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700479 caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
John Reck113e0822014-03-18 09:22:59 -0700480
481 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
482 shadowIndex++;
483 continue;
484 }
485 }
486
487 // only the actual child DL draw needs to be in save/restore,
488 // since it modifies the renderer's matrix
489 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
490
491 DrawDisplayListOp* childOp = zTranslatedNodes[drawIndex].value;
492 RenderNode* child = childOp->mDisplayList;
493
494 renderer.concatMatrix(childOp->mTransformFromParent);
495 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700496 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700497 childOp->mSkipInOrderDraw = true;
498
499 renderer.restoreToCount(restoreTo);
500 drawIndex++;
501 }
John Reck113e0822014-03-18 09:22:59 -0700502}
503
504template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700505void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700506 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
507 DrawDisplayListOp* childOp = mProjectedNodes[i];
508
509 // matrix save, concat, and restore can be done safely without allocating operations
510 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
511 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
512 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700513 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700514 childOp->mSkipInOrderDraw = true;
515 renderer.restoreToCount(restoreTo);
516 }
John Reck113e0822014-03-18 09:22:59 -0700517}
518
519/**
520 * This function serves both defer and replay modes, and will organize the displayList's component
521 * operations for a single frame:
522 *
523 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
524 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
525 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
526 * defer vs replay logic, per operation
527 */
528template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700529void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
530 const int level = handler.level();
John Reckd0a0b2a2014-03-20 16:28:56 -0700531 if (mDisplayListData->isEmpty() || properties().getAlpha() <= 0) {
John Reck113e0822014-03-18 09:22:59 -0700532 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, mName.string());
533 return;
534 }
535
Chris Craikb265e2c2014-03-27 15:50:09 -0700536 handler.startMark(mName.string());
537
John Reck113e0822014-03-18 09:22:59 -0700538#if DEBUG_DISPLAY_LIST
539 Rect* clipRect = renderer.getClipRect();
540 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), clipRect: %.0f, %.0f, %.0f, %.0f",
541 level * 2, "", this, mName.string(), clipRect->left, clipRect->top,
542 clipRect->right, clipRect->bottom);
543#endif
544
545 LinearAllocator& alloc = handler.allocator();
546 int restoreTo = renderer.getSaveCount();
547 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
John Reckd0a0b2a2014-03-20 16:28:56 -0700548 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700549
550 DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
551 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
552
Chris Craikb265e2c2014-03-27 15:50:09 -0700553 setViewProperties<T>(renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700554
Chris Craik8c271ca2014-03-25 10:33:01 -0700555 bool quickRejected = properties().getClipToBounds()
556 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -0700557 if (!quickRejected) {
558 Vector<ZDrawDisplayListOpPair> zTranslatedNodes;
559 buildZSortedChildList(zTranslatedNodes);
560
561 // for 3d root, draw children with negative z values
Chris Craikb265e2c2014-03-27 15:50:09 -0700562 issueOperationsOf3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700563
564 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
565 const int saveCountOffset = renderer.getSaveCount() - 1;
566 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
567 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
568 DisplayListOp *op = mDisplayListData->displayListOps[i];
569
570#if DEBUG_DISPLAY_LIST
571 op->output(level + 1);
572#endif
John Reck113e0822014-03-18 09:22:59 -0700573 logBuffer.writeCommand(level, op->name());
John Reckd0a0b2a2014-03-20 16:28:56 -0700574 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700575
576 if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700577 issueOperationsOfProjectedChildren(renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700578 }
579 }
580
581 // for 3d root, draw children with positive z values
Chris Craikb265e2c2014-03-27 15:50:09 -0700582 issueOperationsOf3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700583 }
584
585 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
586 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -0700587 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700588 renderer.setOverrideLayerAlpha(1.0f);
Chris Craikb265e2c2014-03-27 15:50:09 -0700589
590 handler.endMark();
John Reck113e0822014-03-18 09:22:59 -0700591}
592
593} /* namespace uirenderer */
594} /* namespace android */