blob: fba482d0f483cbe086891c165cfee3f907db6e33 [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();
John Reckf9be7792014-05-02 18:21:16 -0700163 info.out.hasAnimations |= mAnimators.size();
John Recke45b1fd2014-04-15 09:50:16 -0700164}
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;
John Reckf9be7792014-05-02 18:21:16 -0700169 info.out.hasFunctors |= subtree->functorCount;
John Reck860d1552014-04-11 19:15:05 -0700170 // 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())) {
Chris Craik2bcad172014-05-14 18:11:23 -0700241 ClipPathOp* op = new (handler.allocator()) ClipPathOp(
242 properties().getClippingPath(), properties().getClippingPathOp());
John Reckd0a0b2a2014-03-20 16:28:56 -0700243 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700244 }
245}
246
247/**
248 * Apply property-based transformations to input matrix
249 *
250 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
251 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
252 */
253void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700254 if (properties().getLeft() != 0 || properties().getTop() != 0) {
255 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700256 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700257 if (properties().getStaticMatrix()) {
258 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700259 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700260 } else if (properties().getAnimationMatrix()) {
261 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700262 matrix.multiply(anim);
263 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700264
Chris Craikcc39e162014-04-25 18:34:11 -0700265 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700266 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700267 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700268 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700269 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700270 } else {
271 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700272 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700273 } else {
274 mat4 true3dMat;
275 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700276 properties().getPivotX() + properties().getTranslationX(),
277 properties().getPivotY() + properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700278 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700279 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
280 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
281 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
282 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
283 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700284
285 matrix.multiply(true3dMat);
286 }
287 }
288 }
289}
290
291/**
292 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
293 *
294 * This should be called before a call to defer() or drawDisplayList()
295 *
296 * Each DisplayList that serves as a 3d root builds its list of composited children,
297 * which are flagged to not draw in the standard draw loop.
298 */
299void RenderNode::computeOrdering() {
300 ATRACE_CALL();
301 mProjectedNodes.clear();
302
303 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
304 // transform properties are applied correctly to top level children
305 if (mDisplayListData == NULL) return;
John Reck087bc0c2014-04-04 16:20:08 -0700306 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
307 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700308 childOp->mDisplayList->computeOrderingImpl(childOp,
Chris Craik3f0854292014-04-15 16:18:08 -0700309 properties().getOutline().getPath(), &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700310 }
311}
312
313void RenderNode::computeOrderingImpl(
314 DrawDisplayListOp* opState,
Chris Craik3f0854292014-04-15 16:18:08 -0700315 const SkPath* outlineOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700316 Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface,
317 const mat4* transformFromProjectionSurface) {
318 mProjectedNodes.clear();
319 if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
320
321 // TODO: should avoid this calculation in most cases
322 // TODO: just calculate single matrix, down to all leaf composited elements
323 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
324 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
325
John Reckd0a0b2a2014-03-20 16:28:56 -0700326 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700327 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
328 opState->mSkipInOrderDraw = true;
329 opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
330 compositedChildrenOfProjectionSurface->add(opState);
331 } else {
332 // standard in order draw
333 opState->mSkipInOrderDraw = false;
334 }
335
John Reck087bc0c2014-04-04 16:20:08 -0700336 if (mDisplayListData->children().size() > 0) {
John Reck113e0822014-03-18 09:22:59 -0700337 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
338 bool haveAppliedPropertiesToProjection = false;
John Reck087bc0c2014-04-04 16:20:08 -0700339 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
340 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700341 RenderNode* child = childOp->mDisplayList;
342
Chris Craik3f0854292014-04-15 16:18:08 -0700343 const SkPath* projectionOutline = NULL;
John Reck113e0822014-03-18 09:22:59 -0700344 Vector<DrawDisplayListOp*>* projectionChildren = NULL;
345 const mat4* projectionTransform = NULL;
John Reckd0a0b2a2014-03-20 16:28:56 -0700346 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700347 // if receiving projections, collect projecting descendent
348
349 // Note that if a direct descendent is projecting backwards, we pass it's
350 // grandparent projection collection, since it shouldn't project onto it's
351 // parent, where it will already be drawing.
Chris Craik3f0854292014-04-15 16:18:08 -0700352 projectionOutline = properties().getOutline().getPath();
John Reck113e0822014-03-18 09:22:59 -0700353 projectionChildren = &mProjectedNodes;
354 projectionTransform = &mat4::identity();
355 } else {
356 if (!haveAppliedPropertiesToProjection) {
357 applyViewPropertyTransforms(localTransformFromProjectionSurface);
358 haveAppliedPropertiesToProjection = true;
359 }
Chris Craik3f0854292014-04-15 16:18:08 -0700360 projectionOutline = outlineOfProjectionSurface;
John Reck113e0822014-03-18 09:22:59 -0700361 projectionChildren = compositedChildrenOfProjectionSurface;
362 projectionTransform = &localTransformFromProjectionSurface;
363 }
Chris Craik3f0854292014-04-15 16:18:08 -0700364 child->computeOrderingImpl(childOp,
365 projectionOutline, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700366 }
367 }
John Reck113e0822014-03-18 09:22:59 -0700368}
369
370class DeferOperationHandler {
371public:
372 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
373 : mDeferStruct(deferStruct), mLevel(level) {}
374 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
375 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
376 }
377 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700378 inline void startMark(const char* name) {} // do nothing
379 inline void endMark() {}
380 inline int level() { return mLevel; }
381 inline int replayFlags() { return mDeferStruct.mReplayFlags; }
John Reck113e0822014-03-18 09:22:59 -0700382
383private:
384 DeferStateStruct& mDeferStruct;
385 const int mLevel;
386};
387
Chris Craikb265e2c2014-03-27 15:50:09 -0700388void RenderNode::deferNodeTree(DeferStateStruct& deferStruct) {
389 DeferOperationHandler handler(deferStruct, 0);
Chris Craikcc39e162014-04-25 18:34:11 -0700390 if (MathUtils::isPositive(properties().getZ())) {
391 issueDrawShadowOperation(Matrix4::identity(), handler);
392 }
Chris Craikb265e2c2014-03-27 15:50:09 -0700393 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
394}
395
396void RenderNode::deferNodeInParent(DeferStateStruct& deferStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700397 DeferOperationHandler handler(deferStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700398 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700399}
400
401class ReplayOperationHandler {
402public:
403 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
404 : mReplayStruct(replayStruct), mLevel(level) {}
405 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
406#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
Chris Craik3f0854292014-04-15 16:18:08 -0700407 mReplayStruct.mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700408#endif
409 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
410 }
411 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700412 inline void startMark(const char* name) {
413 mReplayStruct.mRenderer.startMark(name);
414 }
415 inline void endMark() {
416 mReplayStruct.mRenderer.endMark();
Chris Craikb265e2c2014-03-27 15:50:09 -0700417 }
418 inline int level() { return mLevel; }
419 inline int replayFlags() { return mReplayStruct.mReplayFlags; }
John Reck113e0822014-03-18 09:22:59 -0700420
421private:
422 ReplayStateStruct& mReplayStruct;
423 const int mLevel;
424};
425
Chris Craikb265e2c2014-03-27 15:50:09 -0700426void RenderNode::replayNodeTree(ReplayStateStruct& replayStruct) {
427 ReplayOperationHandler handler(replayStruct, 0);
Chris Craikcc39e162014-04-25 18:34:11 -0700428 if (MathUtils::isPositive(properties().getZ())) {
429 issueDrawShadowOperation(Matrix4::identity(), handler);
430 }
Chris Craikb265e2c2014-03-27 15:50:09 -0700431 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
432}
433
434void RenderNode::replayNodeInParent(ReplayStateStruct& replayStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700435 ReplayOperationHandler handler(replayStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700436 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700437}
438
439void RenderNode::buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes) {
John Reck087bc0c2014-04-04 16:20:08 -0700440 if (mDisplayListData == NULL || mDisplayListData->children().size() == 0) return;
John Reck113e0822014-03-18 09:22:59 -0700441
John Reck087bc0c2014-04-04 16:20:08 -0700442 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
443 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700444 RenderNode* child = childOp->mDisplayList;
Chris Craikcc39e162014-04-25 18:34:11 -0700445 float childZ = child->properties().getZ();
John Reck113e0822014-03-18 09:22:59 -0700446
Chris Craike0bb87d2014-04-22 17:55:41 -0700447 if (!MathUtils::isZero(childZ)) {
John Reck113e0822014-03-18 09:22:59 -0700448 zTranslatedNodes.add(ZDrawDisplayListOpPair(childZ, childOp));
449 childOp->mSkipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700450 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700451 // regular, in order drawing DisplayList
452 childOp->mSkipInOrderDraw = false;
453 }
454 }
455
456 // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order)
457 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
458}
459
Chris Craikb265e2c2014-03-27 15:50:09 -0700460template <class T>
461void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
462 if (properties().getAlpha() <= 0.0f) return;
463
464 mat4 shadowMatrixXY(transformFromParent);
465 applyViewPropertyTransforms(shadowMatrixXY);
466
467 // Z matrix needs actual 3d transformation, so mapped z values will be correct
468 mat4 shadowMatrixZ(transformFromParent);
469 applyViewPropertyTransforms(shadowMatrixZ, true);
470
471 const SkPath* outlinePath = properties().getOutline().getPath();
472 const RevealClip& revealClip = properties().getRevealClip();
473 const SkPath* revealClipPath = revealClip.hasConvexClip()
474 ? revealClip.getPath() : NULL; // only pass the reveal clip's path if it's convex
475
476 /**
477 * The drawing area of the caster is always the same as the its perimeter (which
478 * the shadow system uses) *except* in the inverse clip case. Inform the shadow
479 * system that the caster's drawing area (as opposed to its perimeter) has been
480 * clipped, so that it knows the caster can't be opaque.
481 */
482 bool casterUnclipped = !revealClip.willClip() || revealClip.hasConvexClip();
483
484 DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp(
485 shadowMatrixXY, shadowMatrixZ,
486 properties().getAlpha(), casterUnclipped,
487 properties().getWidth(), properties().getHeight(),
488 outlinePath, revealClipPath);
489 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
490}
491
John Reck113e0822014-03-18 09:22:59 -0700492#define SHADOW_DELTA 0.1f
493
494template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700495void RenderNode::issueOperationsOf3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes,
John Reck113e0822014-03-18 09:22:59 -0700496 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) {
497 const int size = zTranslatedNodes.size();
498 if (size == 0
499 || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
500 || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
501 // no 3d children to draw
502 return;
503 }
504
John Reck113e0822014-03-18 09:22:59 -0700505 /**
506 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
507 * with very similar Z heights to draw together.
508 *
509 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
510 * underneath both, and neither's shadow is drawn on top of the other.
511 */
512 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
513 size_t drawIndex, shadowIndex, endIndex;
514 if (mode == kNegativeZChildren) {
515 drawIndex = 0;
516 endIndex = nonNegativeIndex;
517 shadowIndex = endIndex; // draw no shadows
518 } else {
519 drawIndex = nonNegativeIndex;
520 endIndex = size;
521 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
522 }
Chris Craik3f0854292014-04-15 16:18:08 -0700523
524 DISPLAY_LIST_LOGD("%*s%d %s 3d children:", (handler.level() + 1) * 2, "",
525 endIndex - drawIndex, mode == kNegativeZChildren ? "negative" : "positive");
526
John Reck113e0822014-03-18 09:22:59 -0700527 float lastCasterZ = 0.0f;
528 while (shadowIndex < endIndex || drawIndex < endIndex) {
529 if (shadowIndex < endIndex) {
530 DrawDisplayListOp* casterOp = zTranslatedNodes[shadowIndex].value;
531 RenderNode* caster = casterOp->mDisplayList;
532 const float casterZ = zTranslatedNodes[shadowIndex].key;
533 // attempt to render the shadow if the caster about to be drawn is its caster,
534 // OR if its caster's Z value is similar to the previous potential caster
535 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700536 caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
John Reck113e0822014-03-18 09:22:59 -0700537
538 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
539 shadowIndex++;
540 continue;
541 }
542 }
543
544 // only the actual child DL draw needs to be in save/restore,
545 // since it modifies the renderer's matrix
546 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
547
548 DrawDisplayListOp* childOp = zTranslatedNodes[drawIndex].value;
549 RenderNode* child = childOp->mDisplayList;
550
551 renderer.concatMatrix(childOp->mTransformFromParent);
552 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700553 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700554 childOp->mSkipInOrderDraw = true;
555
556 renderer.restoreToCount(restoreTo);
557 drawIndex++;
558 }
John Reck113e0822014-03-18 09:22:59 -0700559}
560
561template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700562void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
Chris Craik3f0854292014-04-15 16:18:08 -0700563 DISPLAY_LIST_LOGD("%*s%d projected children:", (handler.level() + 1) * 2, "", mProjectedNodes.size());
564 const SkPath* projectionReceiverOutline = properties().getOutline().getPath();
565 bool maskProjecteesWithPath = projectionReceiverOutline != NULL
566 && !projectionReceiverOutline->isRect(NULL);
567 int restoreTo = renderer.getSaveCount();
568
569 // If the projection reciever has an outline, we mask each of the projected rendernodes to it
570 // Either with clipRect, or special saveLayer masking
571 LinearAllocator& alloc = handler.allocator();
572 if (projectionReceiverOutline != NULL) {
573 const SkRect& outlineBounds = projectionReceiverOutline->getBounds();
574 if (projectionReceiverOutline->isRect(NULL)) {
575 // mask to the rect outline simply with clipRect
576 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
577 PROPERTY_SAVECOUNT, properties().getClipToBounds());
578 ClipRectOp* clipOp = new (alloc) ClipRectOp(
579 outlineBounds.left(), outlineBounds.top(),
580 outlineBounds.right(), outlineBounds.bottom(), SkRegion::kIntersect_Op);
581 handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
582 } else {
583 // wrap the projected RenderNodes with a SaveLayer that will mask to the outline
584 SaveLayerOp* op = new (alloc) SaveLayerOp(
585 outlineBounds.left(), outlineBounds.top(),
586 outlineBounds.right(), outlineBounds.bottom(),
587 255, SkCanvas::kARGB_ClipLayer_SaveFlag);
588 op->setMask(projectionReceiverOutline);
589 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
590
591 /* TODO: add optimizations here to take advantage of placement/size of projected
592 * children (which may shrink saveLayer area significantly). This is dependent on
593 * passing actual drawing/dirtying bounds of projected content down to native.
594 */
595 }
596 }
597
598 // draw projected nodes
John Reck113e0822014-03-18 09:22:59 -0700599 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
600 DrawDisplayListOp* childOp = mProjectedNodes[i];
601
602 // matrix save, concat, and restore can be done safely without allocating operations
603 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
604 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
605 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700606 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700607 childOp->mSkipInOrderDraw = true;
608 renderer.restoreToCount(restoreTo);
609 }
Chris Craik3f0854292014-04-15 16:18:08 -0700610
611 if (projectionReceiverOutline != NULL) {
612 handler(new (alloc) RestoreToCountOp(restoreTo),
613 PROPERTY_SAVECOUNT, properties().getClipToBounds());
614 }
John Reck113e0822014-03-18 09:22:59 -0700615}
616
617/**
618 * This function serves both defer and replay modes, and will organize the displayList's component
619 * operations for a single frame:
620 *
621 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
622 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
623 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
624 * defer vs replay logic, per operation
625 */
626template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700627void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
628 const int level = handler.level();
John Reckd0a0b2a2014-03-20 16:28:56 -0700629 if (mDisplayListData->isEmpty() || properties().getAlpha() <= 0) {
Chris Craik3f0854292014-04-15 16:18:08 -0700630 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, getName());
John Reck113e0822014-03-18 09:22:59 -0700631 return;
632 }
633
Chris Craik3f0854292014-04-15 16:18:08 -0700634 handler.startMark(getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700635
John Reck113e0822014-03-18 09:22:59 -0700636#if DEBUG_DISPLAY_LIST
Chris Craik3f0854292014-04-15 16:18:08 -0700637 const Rect& clipRect = renderer.getLocalClipBounds();
638 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), localClipBounds: %.0f, %.0f, %.0f, %.0f",
639 level * 2, "", this, getName(),
640 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
John Reck113e0822014-03-18 09:22:59 -0700641#endif
642
643 LinearAllocator& alloc = handler.allocator();
644 int restoreTo = renderer.getSaveCount();
645 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
John Reckd0a0b2a2014-03-20 16:28:56 -0700646 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700647
648 DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
649 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
650
Chris Craikb265e2c2014-03-27 15:50:09 -0700651 setViewProperties<T>(renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700652
Chris Craik8c271ca2014-03-25 10:33:01 -0700653 bool quickRejected = properties().getClipToBounds()
654 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -0700655 if (!quickRejected) {
656 Vector<ZDrawDisplayListOpPair> zTranslatedNodes;
657 buildZSortedChildList(zTranslatedNodes);
658
659 // for 3d root, draw children with negative z values
Chris Craikb265e2c2014-03-27 15:50:09 -0700660 issueOperationsOf3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700661
662 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
663 const int saveCountOffset = renderer.getSaveCount() - 1;
664 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
665 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
666 DisplayListOp *op = mDisplayListData->displayListOps[i];
667
668#if DEBUG_DISPLAY_LIST
669 op->output(level + 1);
670#endif
John Reck113e0822014-03-18 09:22:59 -0700671 logBuffer.writeCommand(level, op->name());
John Reckd0a0b2a2014-03-20 16:28:56 -0700672 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700673
674 if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700675 issueOperationsOfProjectedChildren(renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700676 }
677 }
678
679 // for 3d root, draw children with positive z values
Chris Craikb265e2c2014-03-27 15:50:09 -0700680 issueOperationsOf3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700681 }
682
683 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
684 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -0700685 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700686 renderer.setOverrideLayerAlpha(1.0f);
Chris Craikb265e2c2014-03-27 15:50:09 -0700687
Chris Craik3f0854292014-04-15 16:18:08 -0700688 DISPLAY_LIST_LOGD("%*sDone (%p, %s)", level * 2, "", this, getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700689 handler.endMark();
John Reck113e0822014-03-18 09:22:59 -0700690}
691
692} /* namespace uirenderer */
693} /* namespace android */