blob: 9aa47a3b2529cb6d8ab2534f81de0c632db54a72 [file] [log] [blame]
John Reck113e0822014-03-18 09:22:59 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define ATRACE_TAG ATRACE_TAG_VIEW
18
19#include "RenderNode.h"
20
21#include <SkCanvas.h>
22#include <algorithm>
23
24#include <utils/Trace.h>
25
26#include "Debug.h"
27#include "DisplayListOp.h"
28#include "DisplayListLogBuffer.h"
Chris Craike0bb87d2014-04-22 17:55:41 -070029#include "utils/MathUtils.h"
John Reck113e0822014-03-18 09:22:59 -070030
31namespace android {
32namespace uirenderer {
33
34void RenderNode::outputLogBuffer(int fd) {
35 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
36 if (logBuffer.isEmpty()) {
37 return;
38 }
39
40 FILE *file = fdopen(fd, "a");
41
42 fprintf(file, "\nRecent DisplayList operations\n");
43 logBuffer.outputCommands(file);
44
45 String8 cachesLog;
46 Caches::getInstance().dumpMemoryUsage(cachesLog);
47 fprintf(file, "\nCaches:\n%s", cachesLog.string());
48 fprintf(file, "\n");
49
50 fflush(file);
51}
52
John Reck8de65a82014-04-09 15:23:38 -070053RenderNode::RenderNode()
Chris Craik143912f2014-04-11 13:47:36 -070054 : mNeedsPropertiesSync(false)
John Reck8de65a82014-04-09 15:23:38 -070055 , mNeedsDisplayListDataSync(false)
56 , mDisplayListData(0)
57 , mStagingDisplayListData(0) {
John Reck113e0822014-03-18 09:22:59 -070058}
59
60RenderNode::~RenderNode() {
John Reck113e0822014-03-18 09:22:59 -070061 delete mDisplayListData;
John Reck8de65a82014-04-09 15:23:38 -070062 delete mStagingDisplayListData;
John Reck113e0822014-03-18 09:22:59 -070063}
64
John Reck8de65a82014-04-09 15:23:38 -070065void RenderNode::setStagingDisplayList(DisplayListData* data) {
66 mNeedsDisplayListDataSync = true;
67 delete mStagingDisplayListData;
68 mStagingDisplayListData = data;
69 if (mStagingDisplayListData) {
70 Caches::getInstance().registerFunctors(mStagingDisplayListData->functorCount);
John Reck113e0822014-03-18 09:22:59 -070071 }
72}
73
74/**
75 * This function is a simplified version of replay(), where we simply retrieve and log the
76 * display list. This function should remain in sync with the replay() function.
77 */
78void RenderNode::output(uint32_t level) {
79 ALOGD("%*sStart display list (%p, %s, render=%d)", (level - 1) * 2, "", this,
80 mName.string(), isRenderable());
81 ALOGD("%*s%s %d", level * 2, "", "Save",
82 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
83
John Reckd0a0b2a2014-03-20 16:28:56 -070084 properties().debugOutputProperties(level);
John Reck113e0822014-03-18 09:22:59 -070085 int flags = DisplayListOp::kOpLogFlag_Recurse;
86 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
87 mDisplayListData->displayListOps[i]->output(level, flags);
88 }
89
90 ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, mName.string());
91}
92
John Reckf4198b72014-04-09 17:00:04 -070093void RenderNode::prepareTree(TreeInfo& info) {
94 ATRACE_CALL();
95
96 prepareTreeImpl(info);
97}
98
99void RenderNode::prepareTreeImpl(TreeInfo& info) {
100 pushStagingChanges(info);
101 prepareSubTree(info, mDisplayListData);
102}
103
104void RenderNode::pushStagingChanges(TreeInfo& info) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700105 if (mNeedsPropertiesSync) {
106 mNeedsPropertiesSync = false;
107 mProperties = mStagingProperties;
John Reck113e0822014-03-18 09:22:59 -0700108 }
John Reck8de65a82014-04-09 15:23:38 -0700109 if (mNeedsDisplayListDataSync) {
110 mNeedsDisplayListDataSync = false;
111 // Do a push pass on the old tree to handle freeing DisplayListData
112 // that are no longer used
John Reck860d1552014-04-11 19:15:05 -0700113 TreeInfo oldTreeInfo;
John Reckf4198b72014-04-09 17:00:04 -0700114 prepareSubTree(oldTreeInfo, mDisplayListData);
115 // TODO: The damage for the old tree should be accounted for
John Reck8de65a82014-04-09 15:23:38 -0700116 delete mDisplayListData;
117 mDisplayListData = mStagingDisplayListData;
118 mStagingDisplayListData = 0;
119 }
John Reck8de65a82014-04-09 15:23:38 -0700120}
121
John Reckf4198b72014-04-09 17:00:04 -0700122void RenderNode::prepareSubTree(TreeInfo& info, DisplayListData* subtree) {
John Reck8de65a82014-04-09 15:23:38 -0700123 if (subtree) {
John Reck860d1552014-04-11 19:15:05 -0700124 TextureCache& cache = Caches::getInstance().textureCache;
125 info.hasFunctors |= subtree->functorCount;
126 // TODO: Fix ownedBitmapResources to not require disabling prepareTextures
127 // and thus falling out of async drawing path.
128 if (subtree->ownedBitmapResources.size()) {
129 info.prepareTextures = false;
130 }
131 for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) {
132 info.prepareTextures = cache.prefetchAndMarkInUse(subtree->bitmapResources[i]);
John Reckf4198b72014-04-09 17:00:04 -0700133 }
John Reck8de65a82014-04-09 15:23:38 -0700134 for (size_t i = 0; i < subtree->children().size(); i++) {
135 RenderNode* childNode = subtree->children()[i]->mDisplayList;
John Reckf4198b72014-04-09 17:00:04 -0700136 childNode->prepareTreeImpl(info);
John Reck5bf11bb2014-03-25 10:22:09 -0700137 }
John Reck113e0822014-03-18 09:22:59 -0700138 }
139}
140
141/*
142 * For property operations, we pass a savecount of 0, since the operations aren't part of the
143 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
John Reckd0a0b2a2014-03-20 16:28:56 -0700144 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
John Reck113e0822014-03-18 09:22:59 -0700145 */
146#define PROPERTY_SAVECOUNT 0
147
148template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700149void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700150#if DEBUG_DISPLAY_LIST
Chris Craikb265e2c2014-03-27 15:50:09 -0700151 properties().debugOutputProperties(handler.level() + 1);
John Reck113e0822014-03-18 09:22:59 -0700152#endif
John Reckd0a0b2a2014-03-20 16:28:56 -0700153 if (properties().getLeft() != 0 || properties().getTop() != 0) {
154 renderer.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700155 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700156 if (properties().getStaticMatrix()) {
157 renderer.concatMatrix(properties().getStaticMatrix());
158 } else if (properties().getAnimationMatrix()) {
159 renderer.concatMatrix(properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700160 }
John Reckf7483e32014-04-11 08:54:47 -0700161 if (properties().hasTransformMatrix()) {
162 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700163 renderer.translate(properties().getTranslationX(), properties().getTranslationY());
John Reck113e0822014-03-18 09:22:59 -0700164 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700165 renderer.concatMatrix(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700166 }
167 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700168 bool clipToBoundsNeeded = properties().getCaching() ? false : properties().getClipToBounds();
169 if (properties().getAlpha() < 1) {
170 if (properties().getCaching()) {
171 renderer.setOverrideLayerAlpha(properties().getAlpha());
172 } else if (!properties().getHasOverlappingRendering()) {
173 renderer.scaleAlpha(properties().getAlpha());
John Reck113e0822014-03-18 09:22:59 -0700174 } else {
175 // TODO: should be able to store the size of a DL at record time and not
176 // have to pass it into this call. In fact, this information might be in the
177 // location/size info that we store with the new native transform data.
178 int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
179 if (clipToBoundsNeeded) {
180 saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
181 clipToBoundsNeeded = false; // clipping done by saveLayer
182 }
183
184 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
Chris Craik8c271ca2014-03-25 10:33:01 -0700185 0, 0, properties().getWidth(), properties().getHeight(),
186 properties().getAlpha() * 255, saveFlags);
John Reckd0a0b2a2014-03-20 16:28:56 -0700187 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700188 }
189 }
190 if (clipToBoundsNeeded) {
Chris Craik8c271ca2014-03-25 10:33:01 -0700191 ClipRectOp* op = new (handler.allocator()) ClipRectOp(
192 0, 0, properties().getWidth(), properties().getHeight(), SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700193 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700194 }
Chris Craik8c271ca2014-03-25 10:33:01 -0700195
196 if (CC_UNLIKELY(properties().hasClippingPath())) {
197 // TODO: optimize for round rect/circle clipping
198 const SkPath* path = properties().getClippingPath();
199 ClipPathOp* op = new (handler.allocator()) ClipPathOp(path, SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700200 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700201 }
202}
203
204/**
205 * Apply property-based transformations to input matrix
206 *
207 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
208 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
209 */
210void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700211 if (properties().getLeft() != 0 || properties().getTop() != 0) {
212 matrix.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 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700216 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700217 } else if (properties().getAnimationMatrix()) {
218 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700219 matrix.multiply(anim);
220 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700221
222 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getTranslationZ());
223 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700224 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700225 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
226 true3dTransform ? properties().getTranslationZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700227 } else {
228 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700229 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700230 } else {
231 mat4 true3dMat;
232 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700233 properties().getPivotX() + properties().getTranslationX(),
234 properties().getPivotY() + properties().getTranslationY(),
235 properties().getTranslationZ());
236 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
237 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
238 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
239 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
240 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700241
242 matrix.multiply(true3dMat);
243 }
244 }
245 }
246}
247
248/**
249 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
250 *
251 * This should be called before a call to defer() or drawDisplayList()
252 *
253 * Each DisplayList that serves as a 3d root builds its list of composited children,
254 * which are flagged to not draw in the standard draw loop.
255 */
256void RenderNode::computeOrdering() {
257 ATRACE_CALL();
258 mProjectedNodes.clear();
259
260 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
261 // transform properties are applied correctly to top level children
262 if (mDisplayListData == NULL) return;
John Reck087bc0c2014-04-04 16:20:08 -0700263 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
264 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700265 childOp->mDisplayList->computeOrderingImpl(childOp,
266 &mProjectedNodes, &mat4::identity());
267 }
268}
269
270void RenderNode::computeOrderingImpl(
271 DrawDisplayListOp* opState,
272 Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface,
273 const mat4* transformFromProjectionSurface) {
274 mProjectedNodes.clear();
275 if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
276
277 // TODO: should avoid this calculation in most cases
278 // TODO: just calculate single matrix, down to all leaf composited elements
279 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
280 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
281
John Reckd0a0b2a2014-03-20 16:28:56 -0700282 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700283 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
284 opState->mSkipInOrderDraw = true;
285 opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
286 compositedChildrenOfProjectionSurface->add(opState);
287 } else {
288 // standard in order draw
289 opState->mSkipInOrderDraw = false;
290 }
291
John Reck087bc0c2014-04-04 16:20:08 -0700292 if (mDisplayListData->children().size() > 0) {
John Reck113e0822014-03-18 09:22:59 -0700293 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
294 bool haveAppliedPropertiesToProjection = false;
John Reck087bc0c2014-04-04 16:20:08 -0700295 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
296 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700297 RenderNode* child = childOp->mDisplayList;
298
299 Vector<DrawDisplayListOp*>* projectionChildren = NULL;
300 const mat4* projectionTransform = NULL;
John Reckd0a0b2a2014-03-20 16:28:56 -0700301 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700302 // if receiving projections, collect projecting descendent
303
304 // Note that if a direct descendent is projecting backwards, we pass it's
305 // grandparent projection collection, since it shouldn't project onto it's
306 // parent, where it will already be drawing.
307 projectionChildren = &mProjectedNodes;
308 projectionTransform = &mat4::identity();
309 } else {
310 if (!haveAppliedPropertiesToProjection) {
311 applyViewPropertyTransforms(localTransformFromProjectionSurface);
312 haveAppliedPropertiesToProjection = true;
313 }
314 projectionChildren = compositedChildrenOfProjectionSurface;
315 projectionTransform = &localTransformFromProjectionSurface;
316 }
317 child->computeOrderingImpl(childOp, projectionChildren, projectionTransform);
318 }
319 }
John Reck113e0822014-03-18 09:22:59 -0700320}
321
322class DeferOperationHandler {
323public:
324 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
325 : mDeferStruct(deferStruct), mLevel(level) {}
326 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
327 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
328 }
329 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700330 inline void startMark(const char* name) {} // do nothing
331 inline void endMark() {}
332 inline int level() { return mLevel; }
333 inline int replayFlags() { return mDeferStruct.mReplayFlags; }
John Reck113e0822014-03-18 09:22:59 -0700334
335private:
336 DeferStateStruct& mDeferStruct;
337 const int mLevel;
338};
339
Chris Craikb265e2c2014-03-27 15:50:09 -0700340void RenderNode::deferNodeTree(DeferStateStruct& deferStruct) {
341 DeferOperationHandler handler(deferStruct, 0);
342 if (properties().getTranslationZ() > 0.0f) issueDrawShadowOperation(Matrix4::identity(), handler);
343 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
344}
345
346void RenderNode::deferNodeInParent(DeferStateStruct& deferStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700347 DeferOperationHandler handler(deferStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700348 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700349}
350
351class ReplayOperationHandler {
352public:
353 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
354 : mReplayStruct(replayStruct), mLevel(level) {}
355 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
356#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
John Reckd0a0b2a2014-03-20 16:28:56 -0700357 properties().getReplayStruct().mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700358#endif
359 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
360 }
361 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700362 inline void startMark(const char* name) {
363 mReplayStruct.mRenderer.startMark(name);
364 }
365 inline void endMark() {
366 mReplayStruct.mRenderer.endMark();
367 DISPLAY_LIST_LOGD("%*sDone (%p, %s), returning %d", level * 2, "", this, mName.string(),
368 mReplayStruct.mDrawGlStatus);
369 }
370 inline int level() { return mLevel; }
371 inline int replayFlags() { return mReplayStruct.mReplayFlags; }
John Reck113e0822014-03-18 09:22:59 -0700372
373private:
374 ReplayStateStruct& mReplayStruct;
375 const int mLevel;
376};
377
Chris Craikb265e2c2014-03-27 15:50:09 -0700378void RenderNode::replayNodeTree(ReplayStateStruct& replayStruct) {
379 ReplayOperationHandler handler(replayStruct, 0);
380 if (properties().getTranslationZ() > 0.0f) issueDrawShadowOperation(Matrix4::identity(), handler);
381 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
382}
383
384void RenderNode::replayNodeInParent(ReplayStateStruct& replayStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700385 ReplayOperationHandler handler(replayStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700386 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700387}
388
389void RenderNode::buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes) {
John Reck087bc0c2014-04-04 16:20:08 -0700390 if (mDisplayListData == NULL || mDisplayListData->children().size() == 0) return;
John Reck113e0822014-03-18 09:22:59 -0700391
John Reck087bc0c2014-04-04 16:20:08 -0700392 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
393 DrawDisplayListOp* childOp = mDisplayListData->children()[i];
John Reck113e0822014-03-18 09:22:59 -0700394 RenderNode* child = childOp->mDisplayList;
John Reckd0a0b2a2014-03-20 16:28:56 -0700395 float childZ = child->properties().getTranslationZ();
John Reck113e0822014-03-18 09:22:59 -0700396
Chris Craike0bb87d2014-04-22 17:55:41 -0700397 if (!MathUtils::isZero(childZ)) {
John Reck113e0822014-03-18 09:22:59 -0700398 zTranslatedNodes.add(ZDrawDisplayListOpPair(childZ, childOp));
399 childOp->mSkipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700400 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700401 // regular, in order drawing DisplayList
402 childOp->mSkipInOrderDraw = false;
403 }
404 }
405
406 // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order)
407 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
408}
409
Chris Craikb265e2c2014-03-27 15:50:09 -0700410template <class T>
411void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
412 if (properties().getAlpha() <= 0.0f) return;
413
414 mat4 shadowMatrixXY(transformFromParent);
415 applyViewPropertyTransforms(shadowMatrixXY);
416
417 // Z matrix needs actual 3d transformation, so mapped z values will be correct
418 mat4 shadowMatrixZ(transformFromParent);
419 applyViewPropertyTransforms(shadowMatrixZ, true);
420
421 const SkPath* outlinePath = properties().getOutline().getPath();
422 const RevealClip& revealClip = properties().getRevealClip();
423 const SkPath* revealClipPath = revealClip.hasConvexClip()
424 ? revealClip.getPath() : NULL; // only pass the reveal clip's path if it's convex
425
426 /**
427 * The drawing area of the caster is always the same as the its perimeter (which
428 * the shadow system uses) *except* in the inverse clip case. Inform the shadow
429 * system that the caster's drawing area (as opposed to its perimeter) has been
430 * clipped, so that it knows the caster can't be opaque.
431 */
432 bool casterUnclipped = !revealClip.willClip() || revealClip.hasConvexClip();
433
434 DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp(
435 shadowMatrixXY, shadowMatrixZ,
436 properties().getAlpha(), casterUnclipped,
437 properties().getWidth(), properties().getHeight(),
438 outlinePath, revealClipPath);
439 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
440}
441
John Reck113e0822014-03-18 09:22:59 -0700442#define SHADOW_DELTA 0.1f
443
444template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700445void RenderNode::issueOperationsOf3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes,
John Reck113e0822014-03-18 09:22:59 -0700446 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) {
447 const int size = zTranslatedNodes.size();
448 if (size == 0
449 || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
450 || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
451 // no 3d children to draw
452 return;
453 }
454
John Reck113e0822014-03-18 09:22:59 -0700455 /**
456 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
457 * with very similar Z heights to draw together.
458 *
459 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
460 * underneath both, and neither's shadow is drawn on top of the other.
461 */
462 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
463 size_t drawIndex, shadowIndex, endIndex;
464 if (mode == kNegativeZChildren) {
465 drawIndex = 0;
466 endIndex = nonNegativeIndex;
467 shadowIndex = endIndex; // draw no shadows
468 } else {
469 drawIndex = nonNegativeIndex;
470 endIndex = size;
471 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
472 }
473 float lastCasterZ = 0.0f;
474 while (shadowIndex < endIndex || drawIndex < endIndex) {
475 if (shadowIndex < endIndex) {
476 DrawDisplayListOp* casterOp = zTranslatedNodes[shadowIndex].value;
477 RenderNode* caster = casterOp->mDisplayList;
478 const float casterZ = zTranslatedNodes[shadowIndex].key;
479 // attempt to render the shadow if the caster about to be drawn is its caster,
480 // OR if its caster's Z value is similar to the previous potential caster
481 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700482 caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
John Reck113e0822014-03-18 09:22:59 -0700483
484 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
485 shadowIndex++;
486 continue;
487 }
488 }
489
490 // only the actual child DL draw needs to be in save/restore,
491 // since it modifies the renderer's matrix
492 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
493
494 DrawDisplayListOp* childOp = zTranslatedNodes[drawIndex].value;
495 RenderNode* child = childOp->mDisplayList;
496
497 renderer.concatMatrix(childOp->mTransformFromParent);
498 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700499 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700500 childOp->mSkipInOrderDraw = true;
501
502 renderer.restoreToCount(restoreTo);
503 drawIndex++;
504 }
John Reck113e0822014-03-18 09:22:59 -0700505}
506
507template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700508void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700509 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
510 DrawDisplayListOp* childOp = mProjectedNodes[i];
511
512 // matrix save, concat, and restore can be done safely without allocating operations
513 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
514 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
515 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700516 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700517 childOp->mSkipInOrderDraw = true;
518 renderer.restoreToCount(restoreTo);
519 }
John Reck113e0822014-03-18 09:22:59 -0700520}
521
522/**
523 * This function serves both defer and replay modes, and will organize the displayList's component
524 * operations for a single frame:
525 *
526 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
527 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
528 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
529 * defer vs replay logic, per operation
530 */
531template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700532void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
533 const int level = handler.level();
John Reckd0a0b2a2014-03-20 16:28:56 -0700534 if (mDisplayListData->isEmpty() || properties().getAlpha() <= 0) {
John Reck113e0822014-03-18 09:22:59 -0700535 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, mName.string());
536 return;
537 }
538
Chris Craikb265e2c2014-03-27 15:50:09 -0700539 handler.startMark(mName.string());
540
John Reck113e0822014-03-18 09:22:59 -0700541#if DEBUG_DISPLAY_LIST
542 Rect* clipRect = renderer.getClipRect();
543 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), clipRect: %.0f, %.0f, %.0f, %.0f",
544 level * 2, "", this, mName.string(), clipRect->left, clipRect->top,
545 clipRect->right, clipRect->bottom);
546#endif
547
548 LinearAllocator& alloc = handler.allocator();
549 int restoreTo = renderer.getSaveCount();
550 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
John Reckd0a0b2a2014-03-20 16:28:56 -0700551 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700552
553 DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
554 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
555
Chris Craikb265e2c2014-03-27 15:50:09 -0700556 setViewProperties<T>(renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700557
Chris Craik8c271ca2014-03-25 10:33:01 -0700558 bool quickRejected = properties().getClipToBounds()
559 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -0700560 if (!quickRejected) {
561 Vector<ZDrawDisplayListOpPair> zTranslatedNodes;
562 buildZSortedChildList(zTranslatedNodes);
563
564 // for 3d root, draw children with negative z values
Chris Craikb265e2c2014-03-27 15:50:09 -0700565 issueOperationsOf3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700566
567 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
568 const int saveCountOffset = renderer.getSaveCount() - 1;
569 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
570 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
571 DisplayListOp *op = mDisplayListData->displayListOps[i];
572
573#if DEBUG_DISPLAY_LIST
574 op->output(level + 1);
575#endif
John Reck113e0822014-03-18 09:22:59 -0700576 logBuffer.writeCommand(level, op->name());
John Reckd0a0b2a2014-03-20 16:28:56 -0700577 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700578
579 if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700580 issueOperationsOfProjectedChildren(renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700581 }
582 }
583
584 // for 3d root, draw children with positive z values
Chris Craikb265e2c2014-03-27 15:50:09 -0700585 issueOperationsOf3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700586 }
587
588 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
589 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -0700590 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700591 renderer.setOverrideLayerAlpha(1.0f);
Chris Craikb265e2c2014-03-27 15:50:09 -0700592
593 handler.endMark();
John Reck113e0822014-03-18 09:22:59 -0700594}
595
596} /* namespace uirenderer */
597} /* namespace android */