blob: d4ff4a39769da36b4a6287da0857af764daf1d3c [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()
John Reckff941dc2014-05-14 16:34:14 -070056 : mDirtyPropertyFields(0)
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 Reckff941dc2014-05-14 16:34:14 -0700112class PushAnimatorsFunctor {
113public:
114 PushAnimatorsFunctor(RenderNode* target, TreeInfo& info)
115 : mTarget(target), mInfo(info) {}
116
117 bool operator() (const sp<BaseRenderNodeAnimator>& animator) {
118 animator->setupStartValueIfNecessary(mTarget, mInfo);
119 return animator->isFinished();
120 }
121private:
122 RenderNode* mTarget;
123 TreeInfo& mInfo;
124};
John Recke45b1fd2014-04-15 09:50:16 -0700125
John Reckf4198b72014-04-09 17:00:04 -0700126void RenderNode::pushStagingChanges(TreeInfo& info) {
John Reckff941dc2014-05-14 16:34:14 -0700127 // Push the animators first so that setupStartValueIfNecessary() is called
128 // before properties() is trampled by stagingProperties(), as they are
129 // required by some animators.
John Recke45b1fd2014-04-15 09:50:16 -0700130 if (mNeedsAnimatorsSync) {
John Reck52622662014-04-30 14:19:56 -0700131 mAnimators.resize(mStagingAnimators.size());
John Reck52244ff2014-05-01 21:27:37 -0700132 std::vector< sp<BaseRenderNodeAnimator> >::iterator it;
John Reckff941dc2014-05-14 16:34:14 -0700133 PushAnimatorsFunctor functor(this, info);
John Recke45b1fd2014-04-15 09:50:16 -0700134 // hint: this means copy_if_not()
135 it = std::remove_copy_if(mStagingAnimators.begin(), mStagingAnimators.end(),
John Reckff941dc2014-05-14 16:34:14 -0700136 mAnimators.begin(), functor);
John Recke45b1fd2014-04-15 09:50:16 -0700137 mAnimators.resize(std::distance(mAnimators.begin(), it));
138 }
John Reckff941dc2014-05-14 16:34:14 -0700139 if (mDirtyPropertyFields) {
140 mDirtyPropertyFields = 0;
141 mProperties = mStagingProperties;
142 }
John Reck8de65a82014-04-09 15:23:38 -0700143 if (mNeedsDisplayListDataSync) {
144 mNeedsDisplayListDataSync = false;
145 // Do a push pass on the old tree to handle freeing DisplayListData
146 // that are no longer used
John Reck860d1552014-04-11 19:15:05 -0700147 TreeInfo oldTreeInfo;
John Reckf4198b72014-04-09 17:00:04 -0700148 prepareSubTree(oldTreeInfo, mDisplayListData);
149 // TODO: The damage for the old tree should be accounted for
John Reck8de65a82014-04-09 15:23:38 -0700150 delete mDisplayListData;
151 mDisplayListData = mStagingDisplayListData;
152 mStagingDisplayListData = 0;
153 }
John Reck8de65a82014-04-09 15:23:38 -0700154}
155
John Recke45b1fd2014-04-15 09:50:16 -0700156class AnimateFunctor {
157public:
John Reck52244ff2014-05-01 21:27:37 -0700158 AnimateFunctor(RenderNode* target, TreeInfo& info)
John Recke45b1fd2014-04-15 09:50:16 -0700159 : mTarget(target), mInfo(info) {}
160
John Reckff941dc2014-05-14 16:34:14 -0700161 bool operator() (const sp<BaseRenderNodeAnimator>& animator) {
John Reck52244ff2014-05-01 21:27:37 -0700162 return animator->animate(mTarget, mInfo);
John Recke45b1fd2014-04-15 09:50:16 -0700163 }
164private:
John Reck52244ff2014-05-01 21:27:37 -0700165 RenderNode* mTarget;
John Recke45b1fd2014-04-15 09:50:16 -0700166 TreeInfo& mInfo;
167};
168
169void RenderNode::evaluateAnimations(TreeInfo& info) {
170 if (!mAnimators.size()) return;
171
John Reck52244ff2014-05-01 21:27:37 -0700172 AnimateFunctor functor(this, info);
173 std::vector< sp<BaseRenderNodeAnimator> >::iterator newEnd;
John Recke45b1fd2014-04-15 09:50:16 -0700174 newEnd = std::remove_if(mAnimators.begin(), mAnimators.end(), functor);
175 mAnimators.erase(newEnd, mAnimators.end());
176 mProperties.updateMatrix();
John Reckf9be7792014-05-02 18:21:16 -0700177 info.out.hasAnimations |= mAnimators.size();
John Recke45b1fd2014-04-15 09:50:16 -0700178}
179
John Reckf4198b72014-04-09 17:00:04 -0700180void RenderNode::prepareSubTree(TreeInfo& info, DisplayListData* subtree) {
John Reck8de65a82014-04-09 15:23:38 -0700181 if (subtree) {
John Reck860d1552014-04-11 19:15:05 -0700182 TextureCache& cache = Caches::getInstance().textureCache;
John Reckf9be7792014-05-02 18:21:16 -0700183 info.out.hasFunctors |= subtree->functorCount;
John Reck860d1552014-04-11 19:15:05 -0700184 // TODO: Fix ownedBitmapResources to not require disabling prepareTextures
185 // and thus falling out of async drawing path.
186 if (subtree->ownedBitmapResources.size()) {
187 info.prepareTextures = false;
188 }
189 for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) {
190 info.prepareTextures = cache.prefetchAndMarkInUse(subtree->bitmapResources[i]);
John Reckf4198b72014-04-09 17:00:04 -0700191 }
John Reck8de65a82014-04-09 15:23:38 -0700192 for (size_t i = 0; i < subtree->children().size(); i++) {
193 RenderNode* childNode = subtree->children()[i]->mDisplayList;
John Reckf4198b72014-04-09 17:00:04 -0700194 childNode->prepareTreeImpl(info);
John Reck5bf11bb2014-03-25 10:22:09 -0700195 }
John Reck113e0822014-03-18 09:22:59 -0700196 }
197}
198
199/*
200 * For property operations, we pass a savecount of 0, since the operations aren't part of the
201 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
John Reckd0a0b2a2014-03-20 16:28:56 -0700202 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
John Reck113e0822014-03-18 09:22:59 -0700203 */
204#define PROPERTY_SAVECOUNT 0
205
206template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700207void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700208#if DEBUG_DISPLAY_LIST
Chris Craikb265e2c2014-03-27 15:50:09 -0700209 properties().debugOutputProperties(handler.level() + 1);
John Reck113e0822014-03-18 09:22:59 -0700210#endif
John Reckd0a0b2a2014-03-20 16:28:56 -0700211 if (properties().getLeft() != 0 || properties().getTop() != 0) {
212 renderer.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700213 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700214 if (properties().getStaticMatrix()) {
215 renderer.concatMatrix(properties().getStaticMatrix());
216 } else if (properties().getAnimationMatrix()) {
217 renderer.concatMatrix(properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700218 }
John Reckf7483e32014-04-11 08:54:47 -0700219 if (properties().hasTransformMatrix()) {
220 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700221 renderer.translate(properties().getTranslationX(), properties().getTranslationY());
John Reck113e0822014-03-18 09:22:59 -0700222 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700223 renderer.concatMatrix(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700224 }
225 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700226 bool clipToBoundsNeeded = properties().getCaching() ? false : properties().getClipToBounds();
227 if (properties().getAlpha() < 1) {
228 if (properties().getCaching()) {
229 renderer.setOverrideLayerAlpha(properties().getAlpha());
230 } else if (!properties().getHasOverlappingRendering()) {
231 renderer.scaleAlpha(properties().getAlpha());
John Reck113e0822014-03-18 09:22:59 -0700232 } else {
233 // TODO: should be able to store the size of a DL at record time and not
234 // have to pass it into this call. In fact, this information might be in the
235 // location/size info that we store with the new native transform data.
236 int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
237 if (clipToBoundsNeeded) {
238 saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
239 clipToBoundsNeeded = false; // clipping done by saveLayer
240 }
241
242 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
Chris Craik8c271ca2014-03-25 10:33:01 -0700243 0, 0, properties().getWidth(), properties().getHeight(),
244 properties().getAlpha() * 255, saveFlags);
John Reckd0a0b2a2014-03-20 16:28:56 -0700245 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700246 }
247 }
248 if (clipToBoundsNeeded) {
Chris Craik8c271ca2014-03-25 10:33:01 -0700249 ClipRectOp* op = new (handler.allocator()) ClipRectOp(
250 0, 0, properties().getWidth(), properties().getHeight(), SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700251 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700252 }
Chris Craik8c271ca2014-03-25 10:33:01 -0700253
254 if (CC_UNLIKELY(properties().hasClippingPath())) {
Chris Craik2bcad172014-05-14 18:11:23 -0700255 ClipPathOp* op = new (handler.allocator()) ClipPathOp(
256 properties().getClippingPath(), properties().getClippingPathOp());
John Reckd0a0b2a2014-03-20 16:28:56 -0700257 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700258 }
259}
260
261/**
262 * Apply property-based transformations to input matrix
263 *
264 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
265 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
266 */
267void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700268 if (properties().getLeft() != 0 || properties().getTop() != 0) {
269 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700270 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700271 if (properties().getStaticMatrix()) {
272 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700273 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700274 } else if (properties().getAnimationMatrix()) {
275 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700276 matrix.multiply(anim);
277 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700278
Chris Craikcc39e162014-04-25 18:34:11 -0700279 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700280 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700281 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700282 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700283 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700284 } else {
285 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700286 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700287 } else {
288 mat4 true3dMat;
289 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700290 properties().getPivotX() + properties().getTranslationX(),
291 properties().getPivotY() + properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700292 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700293 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
294 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
295 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
296 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
297 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700298
299 matrix.multiply(true3dMat);
300 }
301 }
302 }
303}
304
305/**
306 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
307 *
308 * This should be called before a call to defer() or drawDisplayList()
309 *
310 * Each DisplayList that serves as a 3d root builds its list of composited children,
311 * which are flagged to not draw in the standard draw loop.
312 */
313void RenderNode::computeOrdering() {
314 ATRACE_CALL();
315 mProjectedNodes.clear();
316
317 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
318 // transform properties are applied correctly to top level children
319 if (mDisplayListData == NULL) return;
John Reck087bc0c2014-04-04 16:20:08 -0700320 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
321 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700322 childOp->mDisplayList->computeOrderingImpl(childOp,
Chris Craik3f0854292014-04-15 16:18:08 -0700323 properties().getOutline().getPath(), &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700324 }
325}
326
327void RenderNode::computeOrderingImpl(
328 DrawDisplayListOp* opState,
Chris Craik3f0854292014-04-15 16:18:08 -0700329 const SkPath* outlineOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700330 Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface,
331 const mat4* transformFromProjectionSurface) {
332 mProjectedNodes.clear();
333 if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
334
335 // TODO: should avoid this calculation in most cases
336 // TODO: just calculate single matrix, down to all leaf composited elements
337 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
338 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
339
John Reckd0a0b2a2014-03-20 16:28:56 -0700340 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700341 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
342 opState->mSkipInOrderDraw = true;
343 opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
344 compositedChildrenOfProjectionSurface->add(opState);
345 } else {
346 // standard in order draw
347 opState->mSkipInOrderDraw = false;
348 }
349
John Reck087bc0c2014-04-04 16:20:08 -0700350 if (mDisplayListData->children().size() > 0) {
John Reck113e0822014-03-18 09:22:59 -0700351 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
352 bool haveAppliedPropertiesToProjection = false;
John Reck087bc0c2014-04-04 16:20:08 -0700353 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
354 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700355 RenderNode* child = childOp->mDisplayList;
356
Chris Craik3f0854292014-04-15 16:18:08 -0700357 const SkPath* projectionOutline = NULL;
John Reck113e0822014-03-18 09:22:59 -0700358 Vector<DrawDisplayListOp*>* projectionChildren = NULL;
359 const mat4* projectionTransform = NULL;
John Reckd0a0b2a2014-03-20 16:28:56 -0700360 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700361 // if receiving projections, collect projecting descendent
362
363 // Note that if a direct descendent is projecting backwards, we pass it's
364 // grandparent projection collection, since it shouldn't project onto it's
365 // parent, where it will already be drawing.
Chris Craik3f0854292014-04-15 16:18:08 -0700366 projectionOutline = properties().getOutline().getPath();
John Reck113e0822014-03-18 09:22:59 -0700367 projectionChildren = &mProjectedNodes;
368 projectionTransform = &mat4::identity();
369 } else {
370 if (!haveAppliedPropertiesToProjection) {
371 applyViewPropertyTransforms(localTransformFromProjectionSurface);
372 haveAppliedPropertiesToProjection = true;
373 }
Chris Craik3f0854292014-04-15 16:18:08 -0700374 projectionOutline = outlineOfProjectionSurface;
John Reck113e0822014-03-18 09:22:59 -0700375 projectionChildren = compositedChildrenOfProjectionSurface;
376 projectionTransform = &localTransformFromProjectionSurface;
377 }
Chris Craik3f0854292014-04-15 16:18:08 -0700378 child->computeOrderingImpl(childOp,
379 projectionOutline, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700380 }
381 }
John Reck113e0822014-03-18 09:22:59 -0700382}
383
384class DeferOperationHandler {
385public:
386 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
387 : mDeferStruct(deferStruct), mLevel(level) {}
388 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
389 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
390 }
391 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700392 inline void startMark(const char* name) {} // do nothing
393 inline void endMark() {}
394 inline int level() { return mLevel; }
395 inline int replayFlags() { return mDeferStruct.mReplayFlags; }
John Reck113e0822014-03-18 09:22:59 -0700396
397private:
398 DeferStateStruct& mDeferStruct;
399 const int mLevel;
400};
401
Chris Craikb265e2c2014-03-27 15:50:09 -0700402void RenderNode::deferNodeTree(DeferStateStruct& deferStruct) {
403 DeferOperationHandler handler(deferStruct, 0);
Chris Craikcc39e162014-04-25 18:34:11 -0700404 if (MathUtils::isPositive(properties().getZ())) {
405 issueDrawShadowOperation(Matrix4::identity(), handler);
406 }
Chris Craikb265e2c2014-03-27 15:50:09 -0700407 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
408}
409
410void RenderNode::deferNodeInParent(DeferStateStruct& deferStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700411 DeferOperationHandler handler(deferStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700412 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700413}
414
415class ReplayOperationHandler {
416public:
417 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
418 : mReplayStruct(replayStruct), mLevel(level) {}
419 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
420#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
Chris Craik3f0854292014-04-15 16:18:08 -0700421 mReplayStruct.mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700422#endif
423 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
424 }
425 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700426 inline void startMark(const char* name) {
427 mReplayStruct.mRenderer.startMark(name);
428 }
429 inline void endMark() {
430 mReplayStruct.mRenderer.endMark();
Chris Craikb265e2c2014-03-27 15:50:09 -0700431 }
432 inline int level() { return mLevel; }
433 inline int replayFlags() { return mReplayStruct.mReplayFlags; }
John Reck113e0822014-03-18 09:22:59 -0700434
435private:
436 ReplayStateStruct& mReplayStruct;
437 const int mLevel;
438};
439
Chris Craikb265e2c2014-03-27 15:50:09 -0700440void RenderNode::replayNodeTree(ReplayStateStruct& replayStruct) {
441 ReplayOperationHandler handler(replayStruct, 0);
Chris Craikcc39e162014-04-25 18:34:11 -0700442 if (MathUtils::isPositive(properties().getZ())) {
443 issueDrawShadowOperation(Matrix4::identity(), handler);
444 }
Chris Craikb265e2c2014-03-27 15:50:09 -0700445 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
446}
447
448void RenderNode::replayNodeInParent(ReplayStateStruct& replayStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700449 ReplayOperationHandler handler(replayStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700450 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700451}
452
453void RenderNode::buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes) {
John Reck087bc0c2014-04-04 16:20:08 -0700454 if (mDisplayListData == NULL || mDisplayListData->children().size() == 0) return;
John Reck113e0822014-03-18 09:22:59 -0700455
John Reck087bc0c2014-04-04 16:20:08 -0700456 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
457 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700458 RenderNode* child = childOp->mDisplayList;
Chris Craikcc39e162014-04-25 18:34:11 -0700459 float childZ = child->properties().getZ();
John Reck113e0822014-03-18 09:22:59 -0700460
Chris Craike0bb87d2014-04-22 17:55:41 -0700461 if (!MathUtils::isZero(childZ)) {
John Reck113e0822014-03-18 09:22:59 -0700462 zTranslatedNodes.add(ZDrawDisplayListOpPair(childZ, childOp));
463 childOp->mSkipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700464 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700465 // regular, in order drawing DisplayList
466 childOp->mSkipInOrderDraw = false;
467 }
468 }
469
470 // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order)
471 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
472}
473
Chris Craikb265e2c2014-03-27 15:50:09 -0700474template <class T>
475void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
476 if (properties().getAlpha() <= 0.0f) return;
477
478 mat4 shadowMatrixXY(transformFromParent);
479 applyViewPropertyTransforms(shadowMatrixXY);
480
481 // Z matrix needs actual 3d transformation, so mapped z values will be correct
482 mat4 shadowMatrixZ(transformFromParent);
483 applyViewPropertyTransforms(shadowMatrixZ, true);
484
485 const SkPath* outlinePath = properties().getOutline().getPath();
486 const RevealClip& revealClip = properties().getRevealClip();
487 const SkPath* revealClipPath = revealClip.hasConvexClip()
488 ? revealClip.getPath() : NULL; // only pass the reveal clip's path if it's convex
489
490 /**
491 * The drawing area of the caster is always the same as the its perimeter (which
492 * the shadow system uses) *except* in the inverse clip case. Inform the shadow
493 * system that the caster's drawing area (as opposed to its perimeter) has been
494 * clipped, so that it knows the caster can't be opaque.
495 */
496 bool casterUnclipped = !revealClip.willClip() || revealClip.hasConvexClip();
497
498 DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp(
499 shadowMatrixXY, shadowMatrixZ,
500 properties().getAlpha(), casterUnclipped,
501 properties().getWidth(), properties().getHeight(),
502 outlinePath, revealClipPath);
503 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
504}
505
John Reck113e0822014-03-18 09:22:59 -0700506#define SHADOW_DELTA 0.1f
507
508template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700509void RenderNode::issueOperationsOf3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes,
John Reck113e0822014-03-18 09:22:59 -0700510 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) {
511 const int size = zTranslatedNodes.size();
512 if (size == 0
513 || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
514 || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
515 // no 3d children to draw
516 return;
517 }
518
John Reck113e0822014-03-18 09:22:59 -0700519 /**
520 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
521 * with very similar Z heights to draw together.
522 *
523 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
524 * underneath both, and neither's shadow is drawn on top of the other.
525 */
526 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
527 size_t drawIndex, shadowIndex, endIndex;
528 if (mode == kNegativeZChildren) {
529 drawIndex = 0;
530 endIndex = nonNegativeIndex;
531 shadowIndex = endIndex; // draw no shadows
532 } else {
533 drawIndex = nonNegativeIndex;
534 endIndex = size;
535 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
536 }
Chris Craik3f0854292014-04-15 16:18:08 -0700537
538 DISPLAY_LIST_LOGD("%*s%d %s 3d children:", (handler.level() + 1) * 2, "",
539 endIndex - drawIndex, mode == kNegativeZChildren ? "negative" : "positive");
540
John Reck113e0822014-03-18 09:22:59 -0700541 float lastCasterZ = 0.0f;
542 while (shadowIndex < endIndex || drawIndex < endIndex) {
543 if (shadowIndex < endIndex) {
544 DrawDisplayListOp* casterOp = zTranslatedNodes[shadowIndex].value;
545 RenderNode* caster = casterOp->mDisplayList;
546 const float casterZ = zTranslatedNodes[shadowIndex].key;
547 // attempt to render the shadow if the caster about to be drawn is its caster,
548 // OR if its caster's Z value is similar to the previous potential caster
549 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700550 caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
John Reck113e0822014-03-18 09:22:59 -0700551
552 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
553 shadowIndex++;
554 continue;
555 }
556 }
557
558 // only the actual child DL draw needs to be in save/restore,
559 // since it modifies the renderer's matrix
560 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
561
562 DrawDisplayListOp* childOp = zTranslatedNodes[drawIndex].value;
563 RenderNode* child = childOp->mDisplayList;
564
565 renderer.concatMatrix(childOp->mTransformFromParent);
566 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700567 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700568 childOp->mSkipInOrderDraw = true;
569
570 renderer.restoreToCount(restoreTo);
571 drawIndex++;
572 }
John Reck113e0822014-03-18 09:22:59 -0700573}
574
575template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700576void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
Chris Craik3f0854292014-04-15 16:18:08 -0700577 DISPLAY_LIST_LOGD("%*s%d projected children:", (handler.level() + 1) * 2, "", mProjectedNodes.size());
578 const SkPath* projectionReceiverOutline = properties().getOutline().getPath();
579 bool maskProjecteesWithPath = projectionReceiverOutline != NULL
580 && !projectionReceiverOutline->isRect(NULL);
581 int restoreTo = renderer.getSaveCount();
582
583 // If the projection reciever has an outline, we mask each of the projected rendernodes to it
584 // Either with clipRect, or special saveLayer masking
585 LinearAllocator& alloc = handler.allocator();
586 if (projectionReceiverOutline != NULL) {
587 const SkRect& outlineBounds = projectionReceiverOutline->getBounds();
588 if (projectionReceiverOutline->isRect(NULL)) {
589 // mask to the rect outline simply with clipRect
590 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
591 PROPERTY_SAVECOUNT, properties().getClipToBounds());
592 ClipRectOp* clipOp = new (alloc) ClipRectOp(
593 outlineBounds.left(), outlineBounds.top(),
594 outlineBounds.right(), outlineBounds.bottom(), SkRegion::kIntersect_Op);
595 handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
596 } else {
597 // wrap the projected RenderNodes with a SaveLayer that will mask to the outline
598 SaveLayerOp* op = new (alloc) SaveLayerOp(
599 outlineBounds.left(), outlineBounds.top(),
600 outlineBounds.right(), outlineBounds.bottom(),
601 255, SkCanvas::kARGB_ClipLayer_SaveFlag);
602 op->setMask(projectionReceiverOutline);
603 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
604
605 /* TODO: add optimizations here to take advantage of placement/size of projected
606 * children (which may shrink saveLayer area significantly). This is dependent on
607 * passing actual drawing/dirtying bounds of projected content down to native.
608 */
609 }
610 }
611
612 // draw projected nodes
John Reck113e0822014-03-18 09:22:59 -0700613 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
614 DrawDisplayListOp* childOp = mProjectedNodes[i];
615
616 // matrix save, concat, and restore can be done safely without allocating operations
617 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
618 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
619 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700620 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700621 childOp->mSkipInOrderDraw = true;
622 renderer.restoreToCount(restoreTo);
623 }
Chris Craik3f0854292014-04-15 16:18:08 -0700624
625 if (projectionReceiverOutline != NULL) {
626 handler(new (alloc) RestoreToCountOp(restoreTo),
627 PROPERTY_SAVECOUNT, properties().getClipToBounds());
628 }
John Reck113e0822014-03-18 09:22:59 -0700629}
630
631/**
632 * This function serves both defer and replay modes, and will organize the displayList's component
633 * operations for a single frame:
634 *
635 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
636 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
637 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
638 * defer vs replay logic, per operation
639 */
640template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700641void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
642 const int level = handler.level();
John Reckd0a0b2a2014-03-20 16:28:56 -0700643 if (mDisplayListData->isEmpty() || properties().getAlpha() <= 0) {
Chris Craik3f0854292014-04-15 16:18:08 -0700644 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, getName());
John Reck113e0822014-03-18 09:22:59 -0700645 return;
646 }
647
Chris Craik3f0854292014-04-15 16:18:08 -0700648 handler.startMark(getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700649
John Reck113e0822014-03-18 09:22:59 -0700650#if DEBUG_DISPLAY_LIST
Chris Craik3f0854292014-04-15 16:18:08 -0700651 const Rect& clipRect = renderer.getLocalClipBounds();
652 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), localClipBounds: %.0f, %.0f, %.0f, %.0f",
653 level * 2, "", this, getName(),
654 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
John Reck113e0822014-03-18 09:22:59 -0700655#endif
656
657 LinearAllocator& alloc = handler.allocator();
658 int restoreTo = renderer.getSaveCount();
659 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
John Reckd0a0b2a2014-03-20 16:28:56 -0700660 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700661
662 DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
663 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
664
Chris Craikb265e2c2014-03-27 15:50:09 -0700665 setViewProperties<T>(renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700666
Chris Craik8c271ca2014-03-25 10:33:01 -0700667 bool quickRejected = properties().getClipToBounds()
668 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -0700669 if (!quickRejected) {
670 Vector<ZDrawDisplayListOpPair> zTranslatedNodes;
671 buildZSortedChildList(zTranslatedNodes);
672
673 // for 3d root, draw children with negative z values
Chris Craikb265e2c2014-03-27 15:50:09 -0700674 issueOperationsOf3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700675
676 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
677 const int saveCountOffset = renderer.getSaveCount() - 1;
678 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
679 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
680 DisplayListOp *op = mDisplayListData->displayListOps[i];
681
682#if DEBUG_DISPLAY_LIST
683 op->output(level + 1);
684#endif
John Reck113e0822014-03-18 09:22:59 -0700685 logBuffer.writeCommand(level, op->name());
John Reckd0a0b2a2014-03-20 16:28:56 -0700686 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700687
688 if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700689 issueOperationsOfProjectedChildren(renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700690 }
691 }
692
693 // for 3d root, draw children with positive z values
Chris Craikb265e2c2014-03-27 15:50:09 -0700694 issueOperationsOf3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700695 }
696
697 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
698 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -0700699 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700700 renderer.setOverrideLayerAlpha(1.0f);
Chris Craikb265e2c2014-03-27 15:50:09 -0700701
Chris Craik3f0854292014-04-15 16:18:08 -0700702 DISPLAY_LIST_LOGD("%*sDone (%p, %s)", level * 2, "", this, getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700703 handler.endMark();
John Reck113e0822014-03-18 09:22:59 -0700704}
705
706} /* namespace uirenderer */
707} /* namespace android */