blob: c2f6df8d70afefa1028b8b7b1c0decd4a9d9df05 [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
John Recke4267ea2014-06-03 15:53:15 -070028#include "DamageAccumulator.h"
John Reck113e0822014-03-18 09:22:59 -070029#include "Debug.h"
30#include "DisplayListOp.h"
31#include "DisplayListLogBuffer.h"
Chris Craike0bb87d2014-04-22 17:55:41 -070032#include "utils/MathUtils.h"
John Reck113e0822014-03-18 09:22:59 -070033
34namespace android {
35namespace uirenderer {
36
37void RenderNode::outputLogBuffer(int fd) {
38 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
39 if (logBuffer.isEmpty()) {
40 return;
41 }
42
43 FILE *file = fdopen(fd, "a");
44
45 fprintf(file, "\nRecent DisplayList operations\n");
46 logBuffer.outputCommands(file);
47
48 String8 cachesLog;
49 Caches::getInstance().dumpMemoryUsage(cachesLog);
50 fprintf(file, "\nCaches:\n%s", cachesLog.string());
51 fprintf(file, "\n");
52
53 fflush(file);
54}
55
John Reck8de65a82014-04-09 15:23:38 -070056RenderNode::RenderNode()
John Reckff941dc2014-05-14 16:34:14 -070057 : mDirtyPropertyFields(0)
John Reck8de65a82014-04-09 15:23:38 -070058 , mNeedsDisplayListDataSync(false)
59 , mDisplayListData(0)
John Recke45b1fd2014-04-15 09:50:16 -070060 , mStagingDisplayListData(0)
61 , mNeedsAnimatorsSync(false) {
John Reck113e0822014-03-18 09:22:59 -070062}
63
64RenderNode::~RenderNode() {
John Reck113e0822014-03-18 09:22:59 -070065 delete mDisplayListData;
John Reck8de65a82014-04-09 15:23:38 -070066 delete mStagingDisplayListData;
John Reck113e0822014-03-18 09:22:59 -070067}
68
John Reck8de65a82014-04-09 15:23:38 -070069void RenderNode::setStagingDisplayList(DisplayListData* data) {
70 mNeedsDisplayListDataSync = true;
71 delete mStagingDisplayListData;
72 mStagingDisplayListData = data;
73 if (mStagingDisplayListData) {
74 Caches::getInstance().registerFunctors(mStagingDisplayListData->functorCount);
John Reck113e0822014-03-18 09:22:59 -070075 }
76}
77
78/**
79 * This function is a simplified version of replay(), where we simply retrieve and log the
80 * display list. This function should remain in sync with the replay() function.
81 */
82void RenderNode::output(uint32_t level) {
83 ALOGD("%*sStart display list (%p, %s, render=%d)", (level - 1) * 2, "", this,
Chris Craik3f0854292014-04-15 16:18:08 -070084 getName(), isRenderable());
John Reck113e0822014-03-18 09:22:59 -070085 ALOGD("%*s%s %d", level * 2, "", "Save",
86 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
87
John Reckd0a0b2a2014-03-20 16:28:56 -070088 properties().debugOutputProperties(level);
John Reck113e0822014-03-18 09:22:59 -070089 int flags = DisplayListOp::kOpLogFlag_Recurse;
90 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
91 mDisplayListData->displayListOps[i]->output(level, flags);
92 }
93
Chris Craik3f0854292014-04-15 16:18:08 -070094 ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, getName());
John Reck113e0822014-03-18 09:22:59 -070095}
96
John Reckfe5e7b72014-05-23 17:42:28 -070097int RenderNode::getDebugSize() {
98 int size = sizeof(RenderNode);
99 if (mStagingDisplayListData) {
100 size += mStagingDisplayListData->allocator.usedSize();
101 }
102 if (mDisplayListData && mDisplayListData != mStagingDisplayListData) {
103 size += mDisplayListData->allocator.usedSize();
104 }
105 return size;
106}
107
John Reckf4198b72014-04-09 17:00:04 -0700108void RenderNode::prepareTree(TreeInfo& info) {
109 ATRACE_CALL();
110
111 prepareTreeImpl(info);
112}
113
John Recke4267ea2014-06-03 15:53:15 -0700114static inline void pushNode(RenderNode* self, TreeInfo& info) {
115 if (info.damageAccumulator) {
116 info.damageAccumulator->pushNode(self);
John Recke45b1fd2014-04-15 09:50:16 -0700117 }
John Recke4267ea2014-06-03 15:53:15 -0700118}
119
120static inline void popNode(TreeInfo& info) {
121 if (info.damageAccumulator) {
122 info.damageAccumulator->popNode();
123 }
124}
125
126void RenderNode::damageSelf(TreeInfo& info) {
127 if (info.damageAccumulator && isRenderable() && properties().getAlpha() > 0) {
128 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
129 }
130}
131
132void RenderNode::prepareTreeImpl(TreeInfo& info) {
133 pushNode(this, info);
134 if (info.mode == TreeInfo::MODE_FULL) {
135 pushStagingChanges(info);
136 evaluateAnimations(info);
137 } else if (info.mode == TreeInfo::MODE_MAYBE_DETACHING) {
138 pushStagingChanges(info);
139 } else if (info.mode == TreeInfo::MODE_RT_ONLY) {
John Recke45b1fd2014-04-15 09:50:16 -0700140 evaluateAnimations(info);
141 }
John Reckf4198b72014-04-09 17:00:04 -0700142 prepareSubTree(info, mDisplayListData);
John Recke4267ea2014-06-03 15:53:15 -0700143 popNode(info);
John Reckf4198b72014-04-09 17:00:04 -0700144}
145
John Reckff941dc2014-05-14 16:34:14 -0700146class PushAnimatorsFunctor {
147public:
148 PushAnimatorsFunctor(RenderNode* target, TreeInfo& info)
149 : mTarget(target), mInfo(info) {}
150
151 bool operator() (const sp<BaseRenderNodeAnimator>& animator) {
152 animator->setupStartValueIfNecessary(mTarget, mInfo);
153 return animator->isFinished();
154 }
155private:
156 RenderNode* mTarget;
157 TreeInfo& mInfo;
158};
John Recke45b1fd2014-04-15 09:50:16 -0700159
John Reckf4198b72014-04-09 17:00:04 -0700160void RenderNode::pushStagingChanges(TreeInfo& info) {
John Reckff941dc2014-05-14 16:34:14 -0700161 // Push the animators first so that setupStartValueIfNecessary() is called
162 // before properties() is trampled by stagingProperties(), as they are
163 // required by some animators.
John Recke45b1fd2014-04-15 09:50:16 -0700164 if (mNeedsAnimatorsSync) {
John Reck52622662014-04-30 14:19:56 -0700165 mAnimators.resize(mStagingAnimators.size());
John Reck52244ff2014-05-01 21:27:37 -0700166 std::vector< sp<BaseRenderNodeAnimator> >::iterator it;
John Reckff941dc2014-05-14 16:34:14 -0700167 PushAnimatorsFunctor functor(this, info);
John Recke45b1fd2014-04-15 09:50:16 -0700168 // hint: this means copy_if_not()
169 it = std::remove_copy_if(mStagingAnimators.begin(), mStagingAnimators.end(),
John Reckff941dc2014-05-14 16:34:14 -0700170 mAnimators.begin(), functor);
John Recke45b1fd2014-04-15 09:50:16 -0700171 mAnimators.resize(std::distance(mAnimators.begin(), it));
172 }
John Reckff941dc2014-05-14 16:34:14 -0700173 if (mDirtyPropertyFields) {
174 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700175 damageSelf(info);
176 popNode(info);
John Reckff941dc2014-05-14 16:34:14 -0700177 mProperties = mStagingProperties;
John Recke4267ea2014-06-03 15:53:15 -0700178 pushNode(this, info);
179 // We could try to be clever and only re-damage if the matrix changed.
180 // However, we don't need to worry about that. The cost of over-damaging
181 // here is only going to be a single additional map rect of this node
182 // plus a rect join(). The parent's transform (and up) will only be
183 // performed once.
184 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700185 }
John Reck8de65a82014-04-09 15:23:38 -0700186 if (mNeedsDisplayListDataSync) {
187 mNeedsDisplayListDataSync = false;
188 // Do a push pass on the old tree to handle freeing DisplayListData
189 // that are no longer used
John Recke4267ea2014-06-03 15:53:15 -0700190 TreeInfo oldTreeInfo(TreeInfo::MODE_MAYBE_DETACHING);
191 oldTreeInfo.damageAccumulator = info.damageAccumulator;
John Reckf4198b72014-04-09 17:00:04 -0700192 prepareSubTree(oldTreeInfo, mDisplayListData);
John Reck8de65a82014-04-09 15:23:38 -0700193 delete mDisplayListData;
194 mDisplayListData = mStagingDisplayListData;
195 mStagingDisplayListData = 0;
John Recke4267ea2014-06-03 15:53:15 -0700196 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700197 }
John Reck8de65a82014-04-09 15:23:38 -0700198}
199
John Recke45b1fd2014-04-15 09:50:16 -0700200class AnimateFunctor {
201public:
John Reck52244ff2014-05-01 21:27:37 -0700202 AnimateFunctor(RenderNode* target, TreeInfo& info)
John Recke45b1fd2014-04-15 09:50:16 -0700203 : mTarget(target), mInfo(info) {}
204
John Reckff941dc2014-05-14 16:34:14 -0700205 bool operator() (const sp<BaseRenderNodeAnimator>& animator) {
John Reck52244ff2014-05-01 21:27:37 -0700206 return animator->animate(mTarget, mInfo);
John Recke45b1fd2014-04-15 09:50:16 -0700207 }
208private:
John Reck52244ff2014-05-01 21:27:37 -0700209 RenderNode* mTarget;
John Recke45b1fd2014-04-15 09:50:16 -0700210 TreeInfo& mInfo;
211};
212
213void RenderNode::evaluateAnimations(TreeInfo& info) {
214 if (!mAnimators.size()) return;
215
John Recke4267ea2014-06-03 15:53:15 -0700216 // TODO: Can we target this better? For now treat it like any other staging
217 // property push and just damage self before and after animators are run
218
219 damageSelf(info);
220 popNode(info);
221
John Reck52244ff2014-05-01 21:27:37 -0700222 AnimateFunctor functor(this, info);
223 std::vector< sp<BaseRenderNodeAnimator> >::iterator newEnd;
John Recke45b1fd2014-04-15 09:50:16 -0700224 newEnd = std::remove_if(mAnimators.begin(), mAnimators.end(), functor);
225 mAnimators.erase(newEnd, mAnimators.end());
226 mProperties.updateMatrix();
John Reckf9be7792014-05-02 18:21:16 -0700227 info.out.hasAnimations |= mAnimators.size();
John Recke4267ea2014-06-03 15:53:15 -0700228
229 pushNode(this, info);
230 damageSelf(info);
John Recke45b1fd2014-04-15 09:50:16 -0700231}
232
John Reckf4198b72014-04-09 17:00:04 -0700233void RenderNode::prepareSubTree(TreeInfo& info, DisplayListData* subtree) {
John Reck8de65a82014-04-09 15:23:38 -0700234 if (subtree) {
John Reck860d1552014-04-11 19:15:05 -0700235 TextureCache& cache = Caches::getInstance().textureCache;
John Reckf9be7792014-05-02 18:21:16 -0700236 info.out.hasFunctors |= subtree->functorCount;
John Reck860d1552014-04-11 19:15:05 -0700237 // TODO: Fix ownedBitmapResources to not require disabling prepareTextures
238 // and thus falling out of async drawing path.
239 if (subtree->ownedBitmapResources.size()) {
240 info.prepareTextures = false;
241 }
242 for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) {
243 info.prepareTextures = cache.prefetchAndMarkInUse(subtree->bitmapResources[i]);
John Reckf4198b72014-04-09 17:00:04 -0700244 }
John Reck8de65a82014-04-09 15:23:38 -0700245 for (size_t i = 0; i < subtree->children().size(); i++) {
246 RenderNode* childNode = subtree->children()[i]->mDisplayList;
John Reckf4198b72014-04-09 17:00:04 -0700247 childNode->prepareTreeImpl(info);
John Reck5bf11bb2014-03-25 10:22:09 -0700248 }
John Reck113e0822014-03-18 09:22:59 -0700249 }
250}
251
252/*
253 * For property operations, we pass a savecount of 0, since the operations aren't part of the
254 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
John Reckd0a0b2a2014-03-20 16:28:56 -0700255 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
John Reck113e0822014-03-18 09:22:59 -0700256 */
257#define PROPERTY_SAVECOUNT 0
258
259template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700260void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700261#if DEBUG_DISPLAY_LIST
Chris Craikb265e2c2014-03-27 15:50:09 -0700262 properties().debugOutputProperties(handler.level() + 1);
John Reck113e0822014-03-18 09:22:59 -0700263#endif
John Reckd0a0b2a2014-03-20 16:28:56 -0700264 if (properties().getLeft() != 0 || properties().getTop() != 0) {
265 renderer.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700266 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700267 if (properties().getStaticMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500268 renderer.concatMatrix(*properties().getStaticMatrix());
John Reckd0a0b2a2014-03-20 16:28:56 -0700269 } else if (properties().getAnimationMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500270 renderer.concatMatrix(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700271 }
John Reckf7483e32014-04-11 08:54:47 -0700272 if (properties().hasTransformMatrix()) {
273 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700274 renderer.translate(properties().getTranslationX(), properties().getTranslationY());
John Reck113e0822014-03-18 09:22:59 -0700275 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700276 renderer.concatMatrix(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700277 }
278 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700279 bool clipToBoundsNeeded = properties().getCaching() ? false : properties().getClipToBounds();
280 if (properties().getAlpha() < 1) {
281 if (properties().getCaching()) {
282 renderer.setOverrideLayerAlpha(properties().getAlpha());
283 } else if (!properties().getHasOverlappingRendering()) {
284 renderer.scaleAlpha(properties().getAlpha());
John Reck113e0822014-03-18 09:22:59 -0700285 } else {
286 // TODO: should be able to store the size of a DL at record time and not
287 // have to pass it into this call. In fact, this information might be in the
288 // location/size info that we store with the new native transform data.
289 int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
290 if (clipToBoundsNeeded) {
291 saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
292 clipToBoundsNeeded = false; // clipping done by saveLayer
293 }
294
295 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
Chris Craik8c271ca2014-03-25 10:33:01 -0700296 0, 0, properties().getWidth(), properties().getHeight(),
297 properties().getAlpha() * 255, saveFlags);
John Reckd0a0b2a2014-03-20 16:28:56 -0700298 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700299 }
300 }
301 if (clipToBoundsNeeded) {
Chris Craik8c271ca2014-03-25 10:33:01 -0700302 ClipRectOp* op = new (handler.allocator()) ClipRectOp(
303 0, 0, properties().getWidth(), properties().getHeight(), SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700304 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700305 }
Chris Craik8c271ca2014-03-25 10:33:01 -0700306
307 if (CC_UNLIKELY(properties().hasClippingPath())) {
Chris Craik2bcad172014-05-14 18:11:23 -0700308 ClipPathOp* op = new (handler.allocator()) ClipPathOp(
309 properties().getClippingPath(), properties().getClippingPathOp());
John Reckd0a0b2a2014-03-20 16:28:56 -0700310 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700311 }
312}
313
314/**
315 * Apply property-based transformations to input matrix
316 *
317 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
318 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
319 */
320void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700321 if (properties().getLeft() != 0 || properties().getTop() != 0) {
322 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700323 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700324 if (properties().getStaticMatrix()) {
325 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700326 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700327 } else if (properties().getAnimationMatrix()) {
328 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700329 matrix.multiply(anim);
330 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700331
Chris Craikcc39e162014-04-25 18:34:11 -0700332 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700333 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700334 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700335 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700336 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700337 } else {
338 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700339 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700340 } else {
341 mat4 true3dMat;
342 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700343 properties().getPivotX() + properties().getTranslationX(),
344 properties().getPivotY() + properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700345 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700346 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
347 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
348 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
349 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
350 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700351
352 matrix.multiply(true3dMat);
353 }
354 }
355 }
356}
357
358/**
359 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
360 *
361 * This should be called before a call to defer() or drawDisplayList()
362 *
363 * Each DisplayList that serves as a 3d root builds its list of composited children,
364 * which are flagged to not draw in the standard draw loop.
365 */
366void RenderNode::computeOrdering() {
367 ATRACE_CALL();
368 mProjectedNodes.clear();
369
370 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
371 // transform properties are applied correctly to top level children
372 if (mDisplayListData == NULL) return;
John Reck087bc0c2014-04-04 16:20:08 -0700373 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
374 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700375 childOp->mDisplayList->computeOrderingImpl(childOp,
Chris Craik3f0854292014-04-15 16:18:08 -0700376 properties().getOutline().getPath(), &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700377 }
378}
379
380void RenderNode::computeOrderingImpl(
381 DrawDisplayListOp* opState,
Chris Craik3f0854292014-04-15 16:18:08 -0700382 const SkPath* outlineOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700383 Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface,
384 const mat4* transformFromProjectionSurface) {
385 mProjectedNodes.clear();
386 if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
387
388 // TODO: should avoid this calculation in most cases
389 // TODO: just calculate single matrix, down to all leaf composited elements
390 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
391 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
392
John Reckd0a0b2a2014-03-20 16:28:56 -0700393 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700394 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
395 opState->mSkipInOrderDraw = true;
396 opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
397 compositedChildrenOfProjectionSurface->add(opState);
398 } else {
399 // standard in order draw
400 opState->mSkipInOrderDraw = false;
401 }
402
John Reck087bc0c2014-04-04 16:20:08 -0700403 if (mDisplayListData->children().size() > 0) {
John Reck113e0822014-03-18 09:22:59 -0700404 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
405 bool haveAppliedPropertiesToProjection = false;
John Reck087bc0c2014-04-04 16:20:08 -0700406 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
407 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700408 RenderNode* child = childOp->mDisplayList;
409
Chris Craik3f0854292014-04-15 16:18:08 -0700410 const SkPath* projectionOutline = NULL;
John Reck113e0822014-03-18 09:22:59 -0700411 Vector<DrawDisplayListOp*>* projectionChildren = NULL;
412 const mat4* projectionTransform = NULL;
John Reckd0a0b2a2014-03-20 16:28:56 -0700413 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700414 // if receiving projections, collect projecting descendent
415
416 // Note that if a direct descendent is projecting backwards, we pass it's
417 // grandparent projection collection, since it shouldn't project onto it's
418 // parent, where it will already be drawing.
Chris Craik3f0854292014-04-15 16:18:08 -0700419 projectionOutline = properties().getOutline().getPath();
John Reck113e0822014-03-18 09:22:59 -0700420 projectionChildren = &mProjectedNodes;
421 projectionTransform = &mat4::identity();
422 } else {
423 if (!haveAppliedPropertiesToProjection) {
424 applyViewPropertyTransforms(localTransformFromProjectionSurface);
425 haveAppliedPropertiesToProjection = true;
426 }
Chris Craik3f0854292014-04-15 16:18:08 -0700427 projectionOutline = outlineOfProjectionSurface;
John Reck113e0822014-03-18 09:22:59 -0700428 projectionChildren = compositedChildrenOfProjectionSurface;
429 projectionTransform = &localTransformFromProjectionSurface;
430 }
Chris Craik3f0854292014-04-15 16:18:08 -0700431 child->computeOrderingImpl(childOp,
432 projectionOutline, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700433 }
434 }
John Reck113e0822014-03-18 09:22:59 -0700435}
436
437class DeferOperationHandler {
438public:
439 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
440 : mDeferStruct(deferStruct), mLevel(level) {}
441 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
442 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
443 }
444 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700445 inline void startMark(const char* name) {} // do nothing
446 inline void endMark() {}
447 inline int level() { return mLevel; }
448 inline int replayFlags() { return mDeferStruct.mReplayFlags; }
John Reck113e0822014-03-18 09:22:59 -0700449
450private:
451 DeferStateStruct& mDeferStruct;
452 const int mLevel;
453};
454
Chris Craikb265e2c2014-03-27 15:50:09 -0700455void RenderNode::deferNodeTree(DeferStateStruct& deferStruct) {
456 DeferOperationHandler handler(deferStruct, 0);
Chris Craikcc39e162014-04-25 18:34:11 -0700457 if (MathUtils::isPositive(properties().getZ())) {
458 issueDrawShadowOperation(Matrix4::identity(), handler);
459 }
Chris Craikb265e2c2014-03-27 15:50:09 -0700460 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
461}
462
463void RenderNode::deferNodeInParent(DeferStateStruct& deferStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700464 DeferOperationHandler handler(deferStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700465 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700466}
467
468class ReplayOperationHandler {
469public:
470 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
471 : mReplayStruct(replayStruct), mLevel(level) {}
472 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
473#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
Chris Craik3f0854292014-04-15 16:18:08 -0700474 mReplayStruct.mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700475#endif
476 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
477 }
478 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700479 inline void startMark(const char* name) {
480 mReplayStruct.mRenderer.startMark(name);
481 }
482 inline void endMark() {
483 mReplayStruct.mRenderer.endMark();
Chris Craikb265e2c2014-03-27 15:50:09 -0700484 }
485 inline int level() { return mLevel; }
486 inline int replayFlags() { return mReplayStruct.mReplayFlags; }
John Reck113e0822014-03-18 09:22:59 -0700487
488private:
489 ReplayStateStruct& mReplayStruct;
490 const int mLevel;
491};
492
Chris Craikb265e2c2014-03-27 15:50:09 -0700493void RenderNode::replayNodeTree(ReplayStateStruct& replayStruct) {
494 ReplayOperationHandler handler(replayStruct, 0);
Chris Craikcc39e162014-04-25 18:34:11 -0700495 if (MathUtils::isPositive(properties().getZ())) {
496 issueDrawShadowOperation(Matrix4::identity(), handler);
497 }
Chris Craikb265e2c2014-03-27 15:50:09 -0700498 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
499}
500
501void RenderNode::replayNodeInParent(ReplayStateStruct& replayStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700502 ReplayOperationHandler handler(replayStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700503 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700504}
505
506void RenderNode::buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes) {
John Reck087bc0c2014-04-04 16:20:08 -0700507 if (mDisplayListData == NULL || mDisplayListData->children().size() == 0) return;
John Reck113e0822014-03-18 09:22:59 -0700508
John Reck087bc0c2014-04-04 16:20:08 -0700509 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
510 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700511 RenderNode* child = childOp->mDisplayList;
Chris Craikcc39e162014-04-25 18:34:11 -0700512 float childZ = child->properties().getZ();
John Reck113e0822014-03-18 09:22:59 -0700513
Chris Craike0bb87d2014-04-22 17:55:41 -0700514 if (!MathUtils::isZero(childZ)) {
John Reck113e0822014-03-18 09:22:59 -0700515 zTranslatedNodes.add(ZDrawDisplayListOpPair(childZ, childOp));
516 childOp->mSkipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700517 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700518 // regular, in order drawing DisplayList
519 childOp->mSkipInOrderDraw = false;
520 }
521 }
522
523 // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order)
524 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
525}
526
Chris Craikb265e2c2014-03-27 15:50:09 -0700527template <class T>
528void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
Chris Craik61317322014-05-21 13:03:52 -0700529 if (properties().getAlpha() <= 0.0f || properties().getOutline().isEmpty()) return;
Chris Craikb265e2c2014-03-27 15:50:09 -0700530
531 mat4 shadowMatrixXY(transformFromParent);
532 applyViewPropertyTransforms(shadowMatrixXY);
533
534 // Z matrix needs actual 3d transformation, so mapped z values will be correct
535 mat4 shadowMatrixZ(transformFromParent);
536 applyViewPropertyTransforms(shadowMatrixZ, true);
537
538 const SkPath* outlinePath = properties().getOutline().getPath();
539 const RevealClip& revealClip = properties().getRevealClip();
540 const SkPath* revealClipPath = revealClip.hasConvexClip()
541 ? revealClip.getPath() : NULL; // only pass the reveal clip's path if it's convex
542
Chris Craik61317322014-05-21 13:03:52 -0700543 if (revealClipPath && revealClipPath->isEmpty()) return;
544
Chris Craikb265e2c2014-03-27 15:50:09 -0700545 /**
546 * The drawing area of the caster is always the same as the its perimeter (which
547 * the shadow system uses) *except* in the inverse clip case. Inform the shadow
548 * system that the caster's drawing area (as opposed to its perimeter) has been
549 * clipped, so that it knows the caster can't be opaque.
550 */
551 bool casterUnclipped = !revealClip.willClip() || revealClip.hasConvexClip();
552
553 DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp(
554 shadowMatrixXY, shadowMatrixZ,
555 properties().getAlpha(), casterUnclipped,
Chris Craikb265e2c2014-03-27 15:50:09 -0700556 outlinePath, revealClipPath);
557 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
558}
559
John Reck113e0822014-03-18 09:22:59 -0700560#define SHADOW_DELTA 0.1f
561
562template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700563void RenderNode::issueOperationsOf3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes,
John Reck113e0822014-03-18 09:22:59 -0700564 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) {
565 const int size = zTranslatedNodes.size();
566 if (size == 0
567 || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
568 || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
569 // no 3d children to draw
570 return;
571 }
572
John Reck113e0822014-03-18 09:22:59 -0700573 /**
574 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
575 * with very similar Z heights to draw together.
576 *
577 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
578 * underneath both, and neither's shadow is drawn on top of the other.
579 */
580 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
581 size_t drawIndex, shadowIndex, endIndex;
582 if (mode == kNegativeZChildren) {
583 drawIndex = 0;
584 endIndex = nonNegativeIndex;
585 shadowIndex = endIndex; // draw no shadows
586 } else {
587 drawIndex = nonNegativeIndex;
588 endIndex = size;
589 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
590 }
Chris Craik3f0854292014-04-15 16:18:08 -0700591
592 DISPLAY_LIST_LOGD("%*s%d %s 3d children:", (handler.level() + 1) * 2, "",
593 endIndex - drawIndex, mode == kNegativeZChildren ? "negative" : "positive");
594
John Reck113e0822014-03-18 09:22:59 -0700595 float lastCasterZ = 0.0f;
596 while (shadowIndex < endIndex || drawIndex < endIndex) {
597 if (shadowIndex < endIndex) {
598 DrawDisplayListOp* casterOp = zTranslatedNodes[shadowIndex].value;
599 RenderNode* caster = casterOp->mDisplayList;
600 const float casterZ = zTranslatedNodes[shadowIndex].key;
601 // attempt to render the shadow if the caster about to be drawn is its caster,
602 // OR if its caster's Z value is similar to the previous potential caster
603 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700604 caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
John Reck113e0822014-03-18 09:22:59 -0700605
606 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
607 shadowIndex++;
608 continue;
609 }
610 }
611
612 // only the actual child DL draw needs to be in save/restore,
613 // since it modifies the renderer's matrix
614 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
615
616 DrawDisplayListOp* childOp = zTranslatedNodes[drawIndex].value;
617 RenderNode* child = childOp->mDisplayList;
618
619 renderer.concatMatrix(childOp->mTransformFromParent);
620 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700621 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700622 childOp->mSkipInOrderDraw = true;
623
624 renderer.restoreToCount(restoreTo);
625 drawIndex++;
626 }
John Reck113e0822014-03-18 09:22:59 -0700627}
628
629template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700630void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
Chris Craik3f0854292014-04-15 16:18:08 -0700631 DISPLAY_LIST_LOGD("%*s%d projected children:", (handler.level() + 1) * 2, "", mProjectedNodes.size());
632 const SkPath* projectionReceiverOutline = properties().getOutline().getPath();
633 bool maskProjecteesWithPath = projectionReceiverOutline != NULL
634 && !projectionReceiverOutline->isRect(NULL);
635 int restoreTo = renderer.getSaveCount();
636
637 // If the projection reciever has an outline, we mask each of the projected rendernodes to it
638 // Either with clipRect, or special saveLayer masking
639 LinearAllocator& alloc = handler.allocator();
640 if (projectionReceiverOutline != NULL) {
641 const SkRect& outlineBounds = projectionReceiverOutline->getBounds();
642 if (projectionReceiverOutline->isRect(NULL)) {
643 // mask to the rect outline simply with clipRect
644 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
645 PROPERTY_SAVECOUNT, properties().getClipToBounds());
646 ClipRectOp* clipOp = new (alloc) ClipRectOp(
647 outlineBounds.left(), outlineBounds.top(),
648 outlineBounds.right(), outlineBounds.bottom(), SkRegion::kIntersect_Op);
649 handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
650 } else {
651 // wrap the projected RenderNodes with a SaveLayer that will mask to the outline
652 SaveLayerOp* op = new (alloc) SaveLayerOp(
653 outlineBounds.left(), outlineBounds.top(),
654 outlineBounds.right(), outlineBounds.bottom(),
655 255, SkCanvas::kARGB_ClipLayer_SaveFlag);
656 op->setMask(projectionReceiverOutline);
657 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
658
659 /* TODO: add optimizations here to take advantage of placement/size of projected
660 * children (which may shrink saveLayer area significantly). This is dependent on
661 * passing actual drawing/dirtying bounds of projected content down to native.
662 */
663 }
664 }
665
666 // draw projected nodes
John Reck113e0822014-03-18 09:22:59 -0700667 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
668 DrawDisplayListOp* childOp = mProjectedNodes[i];
669
670 // matrix save, concat, and restore can be done safely without allocating operations
671 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
672 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
673 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700674 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700675 childOp->mSkipInOrderDraw = true;
676 renderer.restoreToCount(restoreTo);
677 }
Chris Craik3f0854292014-04-15 16:18:08 -0700678
679 if (projectionReceiverOutline != NULL) {
680 handler(new (alloc) RestoreToCountOp(restoreTo),
681 PROPERTY_SAVECOUNT, properties().getClipToBounds());
682 }
John Reck113e0822014-03-18 09:22:59 -0700683}
684
685/**
686 * This function serves both defer and replay modes, and will organize the displayList's component
687 * operations for a single frame:
688 *
689 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
690 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
691 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
692 * defer vs replay logic, per operation
693 */
694template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700695void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
696 const int level = handler.level();
John Reckd0a0b2a2014-03-20 16:28:56 -0700697 if (mDisplayListData->isEmpty() || properties().getAlpha() <= 0) {
Chris Craik3f0854292014-04-15 16:18:08 -0700698 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, getName());
John Reck113e0822014-03-18 09:22:59 -0700699 return;
700 }
701
Chris Craik3f0854292014-04-15 16:18:08 -0700702 handler.startMark(getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700703
John Reck113e0822014-03-18 09:22:59 -0700704#if DEBUG_DISPLAY_LIST
Chris Craik3f0854292014-04-15 16:18:08 -0700705 const Rect& clipRect = renderer.getLocalClipBounds();
706 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), localClipBounds: %.0f, %.0f, %.0f, %.0f",
707 level * 2, "", this, getName(),
708 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
John Reck113e0822014-03-18 09:22:59 -0700709#endif
710
711 LinearAllocator& alloc = handler.allocator();
712 int restoreTo = renderer.getSaveCount();
713 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
John Reckd0a0b2a2014-03-20 16:28:56 -0700714 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700715
716 DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
717 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
718
Chris Craikb265e2c2014-03-27 15:50:09 -0700719 setViewProperties<T>(renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700720
Chris Craik8c271ca2014-03-25 10:33:01 -0700721 bool quickRejected = properties().getClipToBounds()
722 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -0700723 if (!quickRejected) {
Chris Craikdeeda3d2014-05-05 19:09:33 -0700724 if (mProperties.getOutline().willClip()) {
725 renderer.setClippingOutline(alloc, &(mProperties.getOutline()));
726 }
727
John Reck113e0822014-03-18 09:22:59 -0700728 Vector<ZDrawDisplayListOpPair> zTranslatedNodes;
729 buildZSortedChildList(zTranslatedNodes);
730
731 // for 3d root, draw children with negative z values
Chris Craikb265e2c2014-03-27 15:50:09 -0700732 issueOperationsOf3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700733
734 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
735 const int saveCountOffset = renderer.getSaveCount() - 1;
736 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
737 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
738 DisplayListOp *op = mDisplayListData->displayListOps[i];
739
740#if DEBUG_DISPLAY_LIST
741 op->output(level + 1);
742#endif
John Reck113e0822014-03-18 09:22:59 -0700743 logBuffer.writeCommand(level, op->name());
John Reckd0a0b2a2014-03-20 16:28:56 -0700744 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700745
746 if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700747 issueOperationsOfProjectedChildren(renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700748 }
749 }
750
751 // for 3d root, draw children with positive z values
Chris Craikb265e2c2014-03-27 15:50:09 -0700752 issueOperationsOf3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700753 }
754
755 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
756 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -0700757 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700758 renderer.setOverrideLayerAlpha(1.0f);
Chris Craikb265e2c2014-03-27 15:50:09 -0700759
Chris Craik3f0854292014-04-15 16:18:08 -0700760 DISPLAY_LIST_LOGD("%*sDone (%p, %s)", level * 2, "", this, getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700761 handler.endMark();
John Reck113e0822014-03-18 09:22:59 -0700762}
763
764} /* namespace uirenderer */
765} /* namespace android */