blob: 2c29985d2937a89d47e827891a6304b82d3b9e3c [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
John Recke45b1fd2014-04-15 09:50:16 -070021#include <algorithm>
22
John Reck113e0822014-03-18 09:22:59 -070023#include <SkCanvas.h>
24#include <algorithm>
25
26#include <utils/Trace.h>
27
28#include "Debug.h"
29#include "DisplayListOp.h"
30#include "DisplayListLogBuffer.h"
Chris Craike0bb87d2014-04-22 17:55:41 -070031#include "utils/MathUtils.h"
John Reck113e0822014-03-18 09:22:59 -070032
33namespace android {
34namespace uirenderer {
35
36void RenderNode::outputLogBuffer(int fd) {
37 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
38 if (logBuffer.isEmpty()) {
39 return;
40 }
41
42 FILE *file = fdopen(fd, "a");
43
44 fprintf(file, "\nRecent DisplayList operations\n");
45 logBuffer.outputCommands(file);
46
47 String8 cachesLog;
48 Caches::getInstance().dumpMemoryUsage(cachesLog);
49 fprintf(file, "\nCaches:\n%s", cachesLog.string());
50 fprintf(file, "\n");
51
52 fflush(file);
53}
54
John Reck8de65a82014-04-09 15:23:38 -070055RenderNode::RenderNode()
Chris Craik143912f2014-04-11 13:47:36 -070056 : mNeedsPropertiesSync(false)
John Reck8de65a82014-04-09 15:23:38 -070057 , mNeedsDisplayListDataSync(false)
58 , mDisplayListData(0)
John Recke45b1fd2014-04-15 09:50:16 -070059 , mStagingDisplayListData(0)
60 , mNeedsAnimatorsSync(false) {
John Reck113e0822014-03-18 09:22:59 -070061}
62
63RenderNode::~RenderNode() {
John Reck113e0822014-03-18 09:22:59 -070064 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,
Chris Craik3f0854292014-04-15 16:18:08 -070083 getName(), isRenderable());
John Reck113e0822014-03-18 09:22:59 -070084 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
Chris Craik3f0854292014-04-15 16:18:08 -070093 ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, getName());
John Reck113e0822014-03-18 09:22:59 -070094}
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) {
John Recke45b1fd2014-04-15 09:50:16 -0700103 if (info.performStagingPush) {
104 pushStagingChanges(info);
105 }
106 if (info.evaluateAnimations) {
107 evaluateAnimations(info);
108 }
John Reckf4198b72014-04-09 17:00:04 -0700109 prepareSubTree(info, mDisplayListData);
110}
111
John Reck52244ff2014-05-01 21:27:37 -0700112static bool is_finished(const sp<BaseRenderNodeAnimator>& animator) {
John Recke45b1fd2014-04-15 09:50:16 -0700113 return animator->isFinished();
114}
115
John Reckf4198b72014-04-09 17:00:04 -0700116void RenderNode::pushStagingChanges(TreeInfo& info) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700117 if (mNeedsPropertiesSync) {
118 mNeedsPropertiesSync = false;
119 mProperties = mStagingProperties;
John Reck113e0822014-03-18 09:22:59 -0700120 }
John Recke45b1fd2014-04-15 09:50:16 -0700121 if (mNeedsAnimatorsSync) {
John Reck52622662014-04-30 14:19:56 -0700122 mAnimators.resize(mStagingAnimators.size());
John Reck52244ff2014-05-01 21:27:37 -0700123 std::vector< sp<BaseRenderNodeAnimator> >::iterator it;
John Recke45b1fd2014-04-15 09:50:16 -0700124 // hint: this means copy_if_not()
125 it = std::remove_copy_if(mStagingAnimators.begin(), mStagingAnimators.end(),
126 mAnimators.begin(), is_finished);
127 mAnimators.resize(std::distance(mAnimators.begin(), it));
128 }
John Reck8de65a82014-04-09 15:23:38 -0700129 if (mNeedsDisplayListDataSync) {
130 mNeedsDisplayListDataSync = false;
131 // Do a push pass on the old tree to handle freeing DisplayListData
132 // that are no longer used
John Reck860d1552014-04-11 19:15:05 -0700133 TreeInfo oldTreeInfo;
John Reckf4198b72014-04-09 17:00:04 -0700134 prepareSubTree(oldTreeInfo, mDisplayListData);
135 // TODO: The damage for the old tree should be accounted for
John Reck8de65a82014-04-09 15:23:38 -0700136 delete mDisplayListData;
137 mDisplayListData = mStagingDisplayListData;
138 mStagingDisplayListData = 0;
139 }
John Reck8de65a82014-04-09 15:23:38 -0700140}
141
John Recke45b1fd2014-04-15 09:50:16 -0700142class AnimateFunctor {
143public:
John Reck52244ff2014-05-01 21:27:37 -0700144 AnimateFunctor(RenderNode* target, TreeInfo& info)
John Recke45b1fd2014-04-15 09:50:16 -0700145 : mTarget(target), mInfo(info) {}
146
John Reck52244ff2014-05-01 21:27:37 -0700147 bool operator() (sp<BaseRenderNodeAnimator>& animator) {
148 return animator->animate(mTarget, mInfo);
John Recke45b1fd2014-04-15 09:50:16 -0700149 }
150private:
John Reck52244ff2014-05-01 21:27:37 -0700151 RenderNode* mTarget;
John Recke45b1fd2014-04-15 09:50:16 -0700152 TreeInfo& mInfo;
153};
154
155void RenderNode::evaluateAnimations(TreeInfo& info) {
156 if (!mAnimators.size()) return;
157
John Reck52244ff2014-05-01 21:27:37 -0700158 AnimateFunctor functor(this, info);
159 std::vector< sp<BaseRenderNodeAnimator> >::iterator newEnd;
John Recke45b1fd2014-04-15 09:50:16 -0700160 newEnd = std::remove_if(mAnimators.begin(), mAnimators.end(), functor);
161 mAnimators.erase(newEnd, mAnimators.end());
162 mProperties.updateMatrix();
163 info.hasAnimations |= mAnimators.size();
164}
165
John Reckf4198b72014-04-09 17:00:04 -0700166void RenderNode::prepareSubTree(TreeInfo& info, DisplayListData* subtree) {
John Reck8de65a82014-04-09 15:23:38 -0700167 if (subtree) {
John Reck860d1552014-04-11 19:15:05 -0700168 TextureCache& cache = Caches::getInstance().textureCache;
169 info.hasFunctors |= subtree->functorCount;
170 // TODO: Fix ownedBitmapResources to not require disabling prepareTextures
171 // and thus falling out of async drawing path.
172 if (subtree->ownedBitmapResources.size()) {
173 info.prepareTextures = false;
174 }
175 for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) {
176 info.prepareTextures = cache.prefetchAndMarkInUse(subtree->bitmapResources[i]);
John Reckf4198b72014-04-09 17:00:04 -0700177 }
John Reck8de65a82014-04-09 15:23:38 -0700178 for (size_t i = 0; i < subtree->children().size(); i++) {
179 RenderNode* childNode = subtree->children()[i]->mDisplayList;
John Reckf4198b72014-04-09 17:00:04 -0700180 childNode->prepareTreeImpl(info);
John Reck5bf11bb2014-03-25 10:22:09 -0700181 }
John Reck113e0822014-03-18 09:22:59 -0700182 }
183}
184
185/*
186 * For property operations, we pass a savecount of 0, since the operations aren't part of the
187 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
John Reckd0a0b2a2014-03-20 16:28:56 -0700188 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
John Reck113e0822014-03-18 09:22:59 -0700189 */
190#define PROPERTY_SAVECOUNT 0
191
192template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700193void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700194#if DEBUG_DISPLAY_LIST
Chris Craikb265e2c2014-03-27 15:50:09 -0700195 properties().debugOutputProperties(handler.level() + 1);
John Reck113e0822014-03-18 09:22:59 -0700196#endif
John Reckd0a0b2a2014-03-20 16:28:56 -0700197 if (properties().getLeft() != 0 || properties().getTop() != 0) {
198 renderer.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700199 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700200 if (properties().getStaticMatrix()) {
201 renderer.concatMatrix(properties().getStaticMatrix());
202 } else if (properties().getAnimationMatrix()) {
203 renderer.concatMatrix(properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700204 }
John Reckf7483e32014-04-11 08:54:47 -0700205 if (properties().hasTransformMatrix()) {
206 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700207 renderer.translate(properties().getTranslationX(), properties().getTranslationY());
John Reck113e0822014-03-18 09:22:59 -0700208 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700209 renderer.concatMatrix(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700210 }
211 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700212 bool clipToBoundsNeeded = properties().getCaching() ? false : properties().getClipToBounds();
213 if (properties().getAlpha() < 1) {
214 if (properties().getCaching()) {
215 renderer.setOverrideLayerAlpha(properties().getAlpha());
216 } else if (!properties().getHasOverlappingRendering()) {
217 renderer.scaleAlpha(properties().getAlpha());
John Reck113e0822014-03-18 09:22:59 -0700218 } else {
219 // TODO: should be able to store the size of a DL at record time and not
220 // have to pass it into this call. In fact, this information might be in the
221 // location/size info that we store with the new native transform data.
222 int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
223 if (clipToBoundsNeeded) {
224 saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
225 clipToBoundsNeeded = false; // clipping done by saveLayer
226 }
227
228 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
Chris Craik8c271ca2014-03-25 10:33:01 -0700229 0, 0, properties().getWidth(), properties().getHeight(),
230 properties().getAlpha() * 255, saveFlags);
John Reckd0a0b2a2014-03-20 16:28:56 -0700231 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700232 }
233 }
234 if (clipToBoundsNeeded) {
Chris Craik8c271ca2014-03-25 10:33:01 -0700235 ClipRectOp* op = new (handler.allocator()) ClipRectOp(
236 0, 0, properties().getWidth(), properties().getHeight(), SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700237 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700238 }
Chris Craik8c271ca2014-03-25 10:33:01 -0700239
240 if (CC_UNLIKELY(properties().hasClippingPath())) {
241 // TODO: optimize for round rect/circle clipping
242 const SkPath* path = properties().getClippingPath();
243 ClipPathOp* op = new (handler.allocator()) ClipPathOp(path, SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700244 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700245 }
246}
247
248/**
249 * Apply property-based transformations to input matrix
250 *
251 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
252 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
253 */
254void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700255 if (properties().getLeft() != 0 || properties().getTop() != 0) {
256 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700257 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700258 if (properties().getStaticMatrix()) {
259 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700260 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700261 } else if (properties().getAnimationMatrix()) {
262 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700263 matrix.multiply(anim);
264 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700265
Chris Craikcc39e162014-04-25 18:34:11 -0700266 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700267 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700268 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700269 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700270 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700271 } else {
272 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700273 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700274 } else {
275 mat4 true3dMat;
276 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700277 properties().getPivotX() + properties().getTranslationX(),
278 properties().getPivotY() + properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700279 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700280 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
281 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
282 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
283 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
284 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700285
286 matrix.multiply(true3dMat);
287 }
288 }
289 }
290}
291
292/**
293 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
294 *
295 * This should be called before a call to defer() or drawDisplayList()
296 *
297 * Each DisplayList that serves as a 3d root builds its list of composited children,
298 * which are flagged to not draw in the standard draw loop.
299 */
300void RenderNode::computeOrdering() {
301 ATRACE_CALL();
302 mProjectedNodes.clear();
303
304 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
305 // transform properties are applied correctly to top level children
306 if (mDisplayListData == NULL) return;
John Reck087bc0c2014-04-04 16:20:08 -0700307 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
308 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700309 childOp->mDisplayList->computeOrderingImpl(childOp,
Chris Craik3f0854292014-04-15 16:18:08 -0700310 properties().getOutline().getPath(), &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700311 }
312}
313
314void RenderNode::computeOrderingImpl(
315 DrawDisplayListOp* opState,
Chris Craik3f0854292014-04-15 16:18:08 -0700316 const SkPath* outlineOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700317 Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface,
318 const mat4* transformFromProjectionSurface) {
319 mProjectedNodes.clear();
320 if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
321
322 // TODO: should avoid this calculation in most cases
323 // TODO: just calculate single matrix, down to all leaf composited elements
324 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
325 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
326
John Reckd0a0b2a2014-03-20 16:28:56 -0700327 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700328 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
329 opState->mSkipInOrderDraw = true;
330 opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
331 compositedChildrenOfProjectionSurface->add(opState);
332 } else {
333 // standard in order draw
334 opState->mSkipInOrderDraw = false;
335 }
336
John Reck087bc0c2014-04-04 16:20:08 -0700337 if (mDisplayListData->children().size() > 0) {
John Reck113e0822014-03-18 09:22:59 -0700338 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
339 bool haveAppliedPropertiesToProjection = false;
John Reck087bc0c2014-04-04 16:20:08 -0700340 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
341 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700342 RenderNode* child = childOp->mDisplayList;
343
Chris Craik3f0854292014-04-15 16:18:08 -0700344 const SkPath* projectionOutline = NULL;
John Reck113e0822014-03-18 09:22:59 -0700345 Vector<DrawDisplayListOp*>* projectionChildren = NULL;
346 const mat4* projectionTransform = NULL;
John Reckd0a0b2a2014-03-20 16:28:56 -0700347 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700348 // if receiving projections, collect projecting descendent
349
350 // Note that if a direct descendent is projecting backwards, we pass it's
351 // grandparent projection collection, since it shouldn't project onto it's
352 // parent, where it will already be drawing.
Chris Craik3f0854292014-04-15 16:18:08 -0700353 projectionOutline = properties().getOutline().getPath();
John Reck113e0822014-03-18 09:22:59 -0700354 projectionChildren = &mProjectedNodes;
355 projectionTransform = &mat4::identity();
356 } else {
357 if (!haveAppliedPropertiesToProjection) {
358 applyViewPropertyTransforms(localTransformFromProjectionSurface);
359 haveAppliedPropertiesToProjection = true;
360 }
Chris Craik3f0854292014-04-15 16:18:08 -0700361 projectionOutline = outlineOfProjectionSurface;
John Reck113e0822014-03-18 09:22:59 -0700362 projectionChildren = compositedChildrenOfProjectionSurface;
363 projectionTransform = &localTransformFromProjectionSurface;
364 }
Chris Craik3f0854292014-04-15 16:18:08 -0700365 child->computeOrderingImpl(childOp,
366 projectionOutline, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700367 }
368 }
John Reck113e0822014-03-18 09:22:59 -0700369}
370
371class DeferOperationHandler {
372public:
373 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
374 : mDeferStruct(deferStruct), mLevel(level) {}
375 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
376 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
377 }
378 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700379 inline void startMark(const char* name) {} // do nothing
380 inline void endMark() {}
381 inline int level() { return mLevel; }
382 inline int replayFlags() { return mDeferStruct.mReplayFlags; }
John Reck113e0822014-03-18 09:22:59 -0700383
384private:
385 DeferStateStruct& mDeferStruct;
386 const int mLevel;
387};
388
Chris Craikb265e2c2014-03-27 15:50:09 -0700389void RenderNode::deferNodeTree(DeferStateStruct& deferStruct) {
390 DeferOperationHandler handler(deferStruct, 0);
Chris Craikcc39e162014-04-25 18:34:11 -0700391 if (MathUtils::isPositive(properties().getZ())) {
392 issueDrawShadowOperation(Matrix4::identity(), handler);
393 }
Chris Craikb265e2c2014-03-27 15:50:09 -0700394 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
395}
396
397void RenderNode::deferNodeInParent(DeferStateStruct& deferStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700398 DeferOperationHandler handler(deferStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700399 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700400}
401
402class ReplayOperationHandler {
403public:
404 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
405 : mReplayStruct(replayStruct), mLevel(level) {}
406 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
407#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
Chris Craik3f0854292014-04-15 16:18:08 -0700408 mReplayStruct.mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700409#endif
410 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
411 }
412 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700413 inline void startMark(const char* name) {
414 mReplayStruct.mRenderer.startMark(name);
415 }
416 inline void endMark() {
417 mReplayStruct.mRenderer.endMark();
Chris Craikb265e2c2014-03-27 15:50:09 -0700418 }
419 inline int level() { return mLevel; }
420 inline int replayFlags() { return mReplayStruct.mReplayFlags; }
John Reck113e0822014-03-18 09:22:59 -0700421
422private:
423 ReplayStateStruct& mReplayStruct;
424 const int mLevel;
425};
426
Chris Craikb265e2c2014-03-27 15:50:09 -0700427void RenderNode::replayNodeTree(ReplayStateStruct& replayStruct) {
428 ReplayOperationHandler handler(replayStruct, 0);
Chris Craikcc39e162014-04-25 18:34:11 -0700429 if (MathUtils::isPositive(properties().getZ())) {
430 issueDrawShadowOperation(Matrix4::identity(), handler);
431 }
Chris Craikb265e2c2014-03-27 15:50:09 -0700432 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
433}
434
435void RenderNode::replayNodeInParent(ReplayStateStruct& replayStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700436 ReplayOperationHandler handler(replayStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700437 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700438}
439
440void RenderNode::buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes) {
John Reck087bc0c2014-04-04 16:20:08 -0700441 if (mDisplayListData == NULL || mDisplayListData->children().size() == 0) return;
John Reck113e0822014-03-18 09:22:59 -0700442
John Reck087bc0c2014-04-04 16:20:08 -0700443 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
444 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700445 RenderNode* child = childOp->mDisplayList;
Chris Craikcc39e162014-04-25 18:34:11 -0700446 float childZ = child->properties().getZ();
John Reck113e0822014-03-18 09:22:59 -0700447
Chris Craike0bb87d2014-04-22 17:55:41 -0700448 if (!MathUtils::isZero(childZ)) {
John Reck113e0822014-03-18 09:22:59 -0700449 zTranslatedNodes.add(ZDrawDisplayListOpPair(childZ, childOp));
450 childOp->mSkipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700451 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700452 // regular, in order drawing DisplayList
453 childOp->mSkipInOrderDraw = false;
454 }
455 }
456
457 // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order)
458 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
459}
460
Chris Craikb265e2c2014-03-27 15:50:09 -0700461template <class T>
462void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
463 if (properties().getAlpha() <= 0.0f) return;
464
465 mat4 shadowMatrixXY(transformFromParent);
466 applyViewPropertyTransforms(shadowMatrixXY);
467
468 // Z matrix needs actual 3d transformation, so mapped z values will be correct
469 mat4 shadowMatrixZ(transformFromParent);
470 applyViewPropertyTransforms(shadowMatrixZ, true);
471
472 const SkPath* outlinePath = properties().getOutline().getPath();
473 const RevealClip& revealClip = properties().getRevealClip();
474 const SkPath* revealClipPath = revealClip.hasConvexClip()
475 ? revealClip.getPath() : NULL; // only pass the reveal clip's path if it's convex
476
477 /**
478 * The drawing area of the caster is always the same as the its perimeter (which
479 * the shadow system uses) *except* in the inverse clip case. Inform the shadow
480 * system that the caster's drawing area (as opposed to its perimeter) has been
481 * clipped, so that it knows the caster can't be opaque.
482 */
483 bool casterUnclipped = !revealClip.willClip() || revealClip.hasConvexClip();
484
485 DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp(
486 shadowMatrixXY, shadowMatrixZ,
487 properties().getAlpha(), casterUnclipped,
488 properties().getWidth(), properties().getHeight(),
489 outlinePath, revealClipPath);
490 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
491}
492
John Reck113e0822014-03-18 09:22:59 -0700493#define SHADOW_DELTA 0.1f
494
495template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700496void RenderNode::issueOperationsOf3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes,
John Reck113e0822014-03-18 09:22:59 -0700497 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) {
498 const int size = zTranslatedNodes.size();
499 if (size == 0
500 || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
501 || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
502 // no 3d children to draw
503 return;
504 }
505
John Reck113e0822014-03-18 09:22:59 -0700506 /**
507 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
508 * with very similar Z heights to draw together.
509 *
510 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
511 * underneath both, and neither's shadow is drawn on top of the other.
512 */
513 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
514 size_t drawIndex, shadowIndex, endIndex;
515 if (mode == kNegativeZChildren) {
516 drawIndex = 0;
517 endIndex = nonNegativeIndex;
518 shadowIndex = endIndex; // draw no shadows
519 } else {
520 drawIndex = nonNegativeIndex;
521 endIndex = size;
522 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
523 }
Chris Craik3f0854292014-04-15 16:18:08 -0700524
525 DISPLAY_LIST_LOGD("%*s%d %s 3d children:", (handler.level() + 1) * 2, "",
526 endIndex - drawIndex, mode == kNegativeZChildren ? "negative" : "positive");
527
John Reck113e0822014-03-18 09:22:59 -0700528 float lastCasterZ = 0.0f;
529 while (shadowIndex < endIndex || drawIndex < endIndex) {
530 if (shadowIndex < endIndex) {
531 DrawDisplayListOp* casterOp = zTranslatedNodes[shadowIndex].value;
532 RenderNode* caster = casterOp->mDisplayList;
533 const float casterZ = zTranslatedNodes[shadowIndex].key;
534 // attempt to render the shadow if the caster about to be drawn is its caster,
535 // OR if its caster's Z value is similar to the previous potential caster
536 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700537 caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
John Reck113e0822014-03-18 09:22:59 -0700538
539 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
540 shadowIndex++;
541 continue;
542 }
543 }
544
545 // only the actual child DL draw needs to be in save/restore,
546 // since it modifies the renderer's matrix
547 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
548
549 DrawDisplayListOp* childOp = zTranslatedNodes[drawIndex].value;
550 RenderNode* child = childOp->mDisplayList;
551
552 renderer.concatMatrix(childOp->mTransformFromParent);
553 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700554 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700555 childOp->mSkipInOrderDraw = true;
556
557 renderer.restoreToCount(restoreTo);
558 drawIndex++;
559 }
John Reck113e0822014-03-18 09:22:59 -0700560}
561
562template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700563void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
Chris Craik3f0854292014-04-15 16:18:08 -0700564 DISPLAY_LIST_LOGD("%*s%d projected children:", (handler.level() + 1) * 2, "", mProjectedNodes.size());
565 const SkPath* projectionReceiverOutline = properties().getOutline().getPath();
566 bool maskProjecteesWithPath = projectionReceiverOutline != NULL
567 && !projectionReceiverOutline->isRect(NULL);
568 int restoreTo = renderer.getSaveCount();
569
570 // If the projection reciever has an outline, we mask each of the projected rendernodes to it
571 // Either with clipRect, or special saveLayer masking
572 LinearAllocator& alloc = handler.allocator();
573 if (projectionReceiverOutline != NULL) {
574 const SkRect& outlineBounds = projectionReceiverOutline->getBounds();
575 if (projectionReceiverOutline->isRect(NULL)) {
576 // mask to the rect outline simply with clipRect
577 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
578 PROPERTY_SAVECOUNT, properties().getClipToBounds());
579 ClipRectOp* clipOp = new (alloc) ClipRectOp(
580 outlineBounds.left(), outlineBounds.top(),
581 outlineBounds.right(), outlineBounds.bottom(), SkRegion::kIntersect_Op);
582 handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
583 } else {
584 // wrap the projected RenderNodes with a SaveLayer that will mask to the outline
585 SaveLayerOp* op = new (alloc) SaveLayerOp(
586 outlineBounds.left(), outlineBounds.top(),
587 outlineBounds.right(), outlineBounds.bottom(),
588 255, SkCanvas::kARGB_ClipLayer_SaveFlag);
589 op->setMask(projectionReceiverOutline);
590 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
591
592 /* TODO: add optimizations here to take advantage of placement/size of projected
593 * children (which may shrink saveLayer area significantly). This is dependent on
594 * passing actual drawing/dirtying bounds of projected content down to native.
595 */
596 }
597 }
598
599 // draw projected nodes
John Reck113e0822014-03-18 09:22:59 -0700600 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
601 DrawDisplayListOp* childOp = mProjectedNodes[i];
602
603 // matrix save, concat, and restore can be done safely without allocating operations
604 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
605 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
606 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700607 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700608 childOp->mSkipInOrderDraw = true;
609 renderer.restoreToCount(restoreTo);
610 }
Chris Craik3f0854292014-04-15 16:18:08 -0700611
612 if (projectionReceiverOutline != NULL) {
613 handler(new (alloc) RestoreToCountOp(restoreTo),
614 PROPERTY_SAVECOUNT, properties().getClipToBounds());
615 }
John Reck113e0822014-03-18 09:22:59 -0700616}
617
618/**
619 * This function serves both defer and replay modes, and will organize the displayList's component
620 * operations for a single frame:
621 *
622 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
623 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
624 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
625 * defer vs replay logic, per operation
626 */
627template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700628void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
629 const int level = handler.level();
John Reckd0a0b2a2014-03-20 16:28:56 -0700630 if (mDisplayListData->isEmpty() || properties().getAlpha() <= 0) {
Chris Craik3f0854292014-04-15 16:18:08 -0700631 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, getName());
John Reck113e0822014-03-18 09:22:59 -0700632 return;
633 }
634
Chris Craik3f0854292014-04-15 16:18:08 -0700635 handler.startMark(getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700636
John Reck113e0822014-03-18 09:22:59 -0700637#if DEBUG_DISPLAY_LIST
Chris Craik3f0854292014-04-15 16:18:08 -0700638 const Rect& clipRect = renderer.getLocalClipBounds();
639 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), localClipBounds: %.0f, %.0f, %.0f, %.0f",
640 level * 2, "", this, getName(),
641 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
John Reck113e0822014-03-18 09:22:59 -0700642#endif
643
644 LinearAllocator& alloc = handler.allocator();
645 int restoreTo = renderer.getSaveCount();
646 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
John Reckd0a0b2a2014-03-20 16:28:56 -0700647 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700648
649 DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
650 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
651
Chris Craikb265e2c2014-03-27 15:50:09 -0700652 setViewProperties<T>(renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700653
Chris Craik8c271ca2014-03-25 10:33:01 -0700654 bool quickRejected = properties().getClipToBounds()
655 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -0700656 if (!quickRejected) {
657 Vector<ZDrawDisplayListOpPair> zTranslatedNodes;
658 buildZSortedChildList(zTranslatedNodes);
659
660 // for 3d root, draw children with negative z values
Chris Craikb265e2c2014-03-27 15:50:09 -0700661 issueOperationsOf3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700662
663 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
664 const int saveCountOffset = renderer.getSaveCount() - 1;
665 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
666 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
667 DisplayListOp *op = mDisplayListData->displayListOps[i];
668
669#if DEBUG_DISPLAY_LIST
670 op->output(level + 1);
671#endif
John Reck113e0822014-03-18 09:22:59 -0700672 logBuffer.writeCommand(level, op->name());
John Reckd0a0b2a2014-03-20 16:28:56 -0700673 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700674
675 if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700676 issueOperationsOfProjectedChildren(renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700677 }
678 }
679
680 // for 3d root, draw children with positive z values
Chris Craikb265e2c2014-03-27 15:50:09 -0700681 issueOperationsOf3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700682 }
683
684 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
685 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -0700686 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700687 renderer.setOverrideLayerAlpha(1.0f);
Chris Craikb265e2c2014-03-27 15:50:09 -0700688
Chris Craik3f0854292014-04-15 16:18:08 -0700689 DISPLAY_LIST_LOGD("%*sDone (%p, %s)", level * 2, "", this, getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700690 handler.endMark();
John Reck113e0822014-03-18 09:22:59 -0700691}
692
693} /* namespace uirenderer */
694} /* namespace android */