John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 1 | /* |
| 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" |
| 29 | |
| 30 | namespace android { |
| 31 | namespace uirenderer { |
| 32 | |
| 33 | void RenderNode::outputLogBuffer(int fd) { |
| 34 | DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance(); |
| 35 | if (logBuffer.isEmpty()) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | FILE *file = fdopen(fd, "a"); |
| 40 | |
| 41 | fprintf(file, "\nRecent DisplayList operations\n"); |
| 42 | logBuffer.outputCommands(file); |
| 43 | |
| 44 | String8 cachesLog; |
| 45 | Caches::getInstance().dumpMemoryUsage(cachesLog); |
| 46 | fprintf(file, "\nCaches:\n%s", cachesLog.string()); |
| 47 | fprintf(file, "\n"); |
| 48 | |
| 49 | fflush(file); |
| 50 | } |
| 51 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 52 | RenderNode::RenderNode() : mDestroyed(false), mNeedsPropertiesSync(false), mDisplayListData(0) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | RenderNode::~RenderNode() { |
| 56 | LOG_ALWAYS_FATAL_IF(mDestroyed, "Double destroyed DisplayList %p", this); |
| 57 | |
| 58 | mDestroyed = true; |
| 59 | delete mDisplayListData; |
| 60 | } |
| 61 | |
| 62 | void RenderNode::destroyDisplayListDeferred(RenderNode* displayList) { |
| 63 | if (displayList) { |
| 64 | DISPLAY_LIST_LOGD("Deferring display list destruction"); |
| 65 | Caches::getInstance().deleteDisplayListDeferred(displayList); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void RenderNode::setData(DisplayListData* data) { |
| 70 | delete mDisplayListData; |
| 71 | mDisplayListData = data; |
| 72 | if (mDisplayListData) { |
| 73 | Caches::getInstance().registerFunctors(mDisplayListData->functorCount); |
| 74 | } |
| 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 | */ |
| 81 | void RenderNode::output(uint32_t level) { |
| 82 | ALOGD("%*sStart display list (%p, %s, render=%d)", (level - 1) * 2, "", this, |
| 83 | mName.string(), isRenderable()); |
| 84 | ALOGD("%*s%s %d", level * 2, "", "Save", |
| 85 | SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); |
| 86 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 87 | properties().debugOutputProperties(level); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 88 | 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 | |
| 93 | ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, mName.string()); |
| 94 | } |
| 95 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 96 | void RenderNode::updateProperties() { |
| 97 | if (mNeedsPropertiesSync) { |
| 98 | mNeedsPropertiesSync = false; |
| 99 | mProperties = mStagingProperties; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 100 | } |
| 101 | |
John Reck | 5bf11bb | 2014-03-25 10:22:09 -0700 | [diff] [blame] | 102 | if (mDisplayListData) { |
| 103 | for (size_t i = 0; i < mDisplayListData->children.size(); i++) { |
| 104 | RenderNode* childNode = mDisplayListData->children[i]->mDisplayList; |
| 105 | childNode->updateProperties(); |
| 106 | } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame^] | 110 | bool RenderNode::hasFunctors() { |
| 111 | if (!mDisplayListData) return false; |
| 112 | |
| 113 | if (mDisplayListData->functorCount) { |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | for (size_t i = 0; i < mDisplayListData->children.size(); i++) { |
| 118 | RenderNode* childNode = mDisplayListData->children[i]->mDisplayList; |
| 119 | if (childNode->hasFunctors()) { |
| 120 | return true; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return false; |
| 125 | } |
| 126 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 127 | /* |
| 128 | * For property operations, we pass a savecount of 0, since the operations aren't part of the |
| 129 | * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 130 | * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount()) |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 131 | */ |
| 132 | #define PROPERTY_SAVECOUNT 0 |
| 133 | |
| 134 | template <class T> |
| 135 | void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler, |
| 136 | const int level) { |
| 137 | #if DEBUG_DISPLAY_LIST |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 138 | properties().debugOutputProperties(level); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 139 | #endif |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 140 | if (properties().getLeft() != 0 || properties().getTop() != 0) { |
| 141 | renderer.translate(properties().getLeft(), properties().getTop()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 142 | } |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 143 | if (properties().getStaticMatrix()) { |
| 144 | renderer.concatMatrix(properties().getStaticMatrix()); |
| 145 | } else if (properties().getAnimationMatrix()) { |
| 146 | renderer.concatMatrix(properties().getAnimationMatrix()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 147 | } |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 148 | if (properties().getMatrixFlags() != 0) { |
| 149 | if (properties().getMatrixFlags() == TRANSLATION) { |
| 150 | renderer.translate(properties().getTranslationX(), properties().getTranslationY()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 151 | } else { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 152 | renderer.concatMatrix(*properties().getTransformMatrix()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 155 | bool clipToBoundsNeeded = properties().getCaching() ? false : properties().getClipToBounds(); |
| 156 | if (properties().getAlpha() < 1) { |
| 157 | if (properties().getCaching()) { |
| 158 | renderer.setOverrideLayerAlpha(properties().getAlpha()); |
| 159 | } else if (!properties().getHasOverlappingRendering()) { |
| 160 | renderer.scaleAlpha(properties().getAlpha()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 161 | } else { |
| 162 | // TODO: should be able to store the size of a DL at record time and not |
| 163 | // have to pass it into this call. In fact, this information might be in the |
| 164 | // location/size info that we store with the new native transform data. |
| 165 | int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag; |
| 166 | if (clipToBoundsNeeded) { |
| 167 | saveFlags |= SkCanvas::kClipToLayer_SaveFlag; |
| 168 | clipToBoundsNeeded = false; // clipping done by saveLayer |
| 169 | } |
| 170 | |
| 171 | SaveLayerOp* op = new (handler.allocator()) SaveLayerOp( |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 172 | 0, 0, properties().getWidth(), properties().getHeight(), properties().getAlpha() * 255, saveFlags); |
| 173 | handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | if (clipToBoundsNeeded) { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 177 | ClipRectOp* op = new (handler.allocator()) ClipRectOp(0, 0, |
| 178 | properties().getWidth(), properties().getHeight(), SkRegion::kIntersect_Op); |
| 179 | handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 180 | } |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 181 | if (CC_UNLIKELY(properties().getOutline().willClip())) { |
| 182 | ClipPathOp* op = new (handler.allocator()) ClipPathOp(properties().getOutline().getPath(), |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 183 | SkRegion::kIntersect_Op); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 184 | handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Apply property-based transformations to input matrix |
| 190 | * |
| 191 | * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4 |
| 192 | * matrix computation instead of the Skia 3x3 matrix + camera hackery. |
| 193 | */ |
| 194 | void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 195 | if (properties().getLeft() != 0 || properties().getTop() != 0) { |
| 196 | matrix.translate(properties().getLeft(), properties().getTop()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 197 | } |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 198 | if (properties().getStaticMatrix()) { |
| 199 | mat4 stat(*properties().getStaticMatrix()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 200 | matrix.multiply(stat); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 201 | } else if (properties().getAnimationMatrix()) { |
| 202 | mat4 anim(*properties().getAnimationMatrix()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 203 | matrix.multiply(anim); |
| 204 | } |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 205 | if (properties().getMatrixFlags() != 0) { |
| 206 | if (properties().getMatrixFlags() == TRANSLATION) { |
| 207 | matrix.translate(properties().getTranslationX(), properties().getTranslationY(), |
| 208 | true3dTransform ? properties().getTranslationZ() : 0.0f); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 209 | } else { |
| 210 | if (!true3dTransform) { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 211 | matrix.multiply(*properties().getTransformMatrix()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 212 | } else { |
| 213 | mat4 true3dMat; |
| 214 | true3dMat.loadTranslate( |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 215 | properties().getPivotX() + properties().getTranslationX(), |
| 216 | properties().getPivotY() + properties().getTranslationY(), |
| 217 | properties().getTranslationZ()); |
| 218 | true3dMat.rotate(properties().getRotationX(), 1, 0, 0); |
| 219 | true3dMat.rotate(properties().getRotationY(), 0, 1, 0); |
| 220 | true3dMat.rotate(properties().getRotation(), 0, 0, 1); |
| 221 | true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1); |
| 222 | true3dMat.translate(-properties().getPivotX(), -properties().getPivotY()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 223 | |
| 224 | matrix.multiply(true3dMat); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Organizes the DisplayList hierarchy to prepare for background projection reordering. |
| 232 | * |
| 233 | * This should be called before a call to defer() or drawDisplayList() |
| 234 | * |
| 235 | * Each DisplayList that serves as a 3d root builds its list of composited children, |
| 236 | * which are flagged to not draw in the standard draw loop. |
| 237 | */ |
| 238 | void RenderNode::computeOrdering() { |
| 239 | ATRACE_CALL(); |
| 240 | mProjectedNodes.clear(); |
| 241 | |
| 242 | // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that |
| 243 | // transform properties are applied correctly to top level children |
| 244 | if (mDisplayListData == NULL) return; |
| 245 | for (unsigned int i = 0; i < mDisplayListData->children.size(); i++) { |
| 246 | DrawDisplayListOp* childOp = mDisplayListData->children[i]; |
| 247 | childOp->mDisplayList->computeOrderingImpl(childOp, |
| 248 | &mProjectedNodes, &mat4::identity()); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | void RenderNode::computeOrderingImpl( |
| 253 | DrawDisplayListOp* opState, |
| 254 | Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface, |
| 255 | const mat4* transformFromProjectionSurface) { |
| 256 | mProjectedNodes.clear(); |
| 257 | if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return; |
| 258 | |
| 259 | // TODO: should avoid this calculation in most cases |
| 260 | // TODO: just calculate single matrix, down to all leaf composited elements |
| 261 | Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface); |
| 262 | localTransformFromProjectionSurface.multiply(opState->mTransformFromParent); |
| 263 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 264 | if (properties().getProjectBackwards()) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 265 | // composited projectee, flag for out of order draw, save matrix, and store in proj surface |
| 266 | opState->mSkipInOrderDraw = true; |
| 267 | opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface); |
| 268 | compositedChildrenOfProjectionSurface->add(opState); |
| 269 | } else { |
| 270 | // standard in order draw |
| 271 | opState->mSkipInOrderDraw = false; |
| 272 | } |
| 273 | |
| 274 | if (mDisplayListData->children.size() > 0) { |
| 275 | const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0; |
| 276 | bool haveAppliedPropertiesToProjection = false; |
| 277 | for (unsigned int i = 0; i < mDisplayListData->children.size(); i++) { |
| 278 | DrawDisplayListOp* childOp = mDisplayListData->children[i]; |
| 279 | RenderNode* child = childOp->mDisplayList; |
| 280 | |
| 281 | Vector<DrawDisplayListOp*>* projectionChildren = NULL; |
| 282 | const mat4* projectionTransform = NULL; |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 283 | if (isProjectionReceiver && !child->properties().getProjectBackwards()) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 284 | // if receiving projections, collect projecting descendent |
| 285 | |
| 286 | // Note that if a direct descendent is projecting backwards, we pass it's |
| 287 | // grandparent projection collection, since it shouldn't project onto it's |
| 288 | // parent, where it will already be drawing. |
| 289 | projectionChildren = &mProjectedNodes; |
| 290 | projectionTransform = &mat4::identity(); |
| 291 | } else { |
| 292 | if (!haveAppliedPropertiesToProjection) { |
| 293 | applyViewPropertyTransforms(localTransformFromProjectionSurface); |
| 294 | haveAppliedPropertiesToProjection = true; |
| 295 | } |
| 296 | projectionChildren = compositedChildrenOfProjectionSurface; |
| 297 | projectionTransform = &localTransformFromProjectionSurface; |
| 298 | } |
| 299 | child->computeOrderingImpl(childOp, projectionChildren, projectionTransform); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | } |
| 304 | |
| 305 | class DeferOperationHandler { |
| 306 | public: |
| 307 | DeferOperationHandler(DeferStateStruct& deferStruct, int level) |
| 308 | : mDeferStruct(deferStruct), mLevel(level) {} |
| 309 | inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) { |
| 310 | operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds); |
| 311 | } |
| 312 | inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); } |
| 313 | |
| 314 | private: |
| 315 | DeferStateStruct& mDeferStruct; |
| 316 | const int mLevel; |
| 317 | }; |
| 318 | |
| 319 | void RenderNode::defer(DeferStateStruct& deferStruct, const int level) { |
| 320 | DeferOperationHandler handler(deferStruct, level); |
| 321 | iterate<DeferOperationHandler>(deferStruct.mRenderer, handler, level); |
| 322 | } |
| 323 | |
| 324 | class ReplayOperationHandler { |
| 325 | public: |
| 326 | ReplayOperationHandler(ReplayStateStruct& replayStruct, int level) |
| 327 | : mReplayStruct(replayStruct), mLevel(level) {} |
| 328 | inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) { |
| 329 | #if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 330 | properties().getReplayStruct().mRenderer.eventMark(operation->name()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 331 | #endif |
| 332 | operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds); |
| 333 | } |
| 334 | inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); } |
| 335 | |
| 336 | private: |
| 337 | ReplayStateStruct& mReplayStruct; |
| 338 | const int mLevel; |
| 339 | }; |
| 340 | |
| 341 | void RenderNode::replay(ReplayStateStruct& replayStruct, const int level) { |
| 342 | ReplayOperationHandler handler(replayStruct, level); |
| 343 | |
| 344 | replayStruct.mRenderer.startMark(mName.string()); |
| 345 | iterate<ReplayOperationHandler>(replayStruct.mRenderer, handler, level); |
| 346 | replayStruct.mRenderer.endMark(); |
| 347 | |
| 348 | DISPLAY_LIST_LOGD("%*sDone (%p, %s), returning %d", level * 2, "", this, mName.string(), |
| 349 | replayStruct.mDrawGlStatus); |
| 350 | } |
| 351 | |
| 352 | void RenderNode::buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes) { |
| 353 | if (mDisplayListData == NULL || mDisplayListData->children.size() == 0) return; |
| 354 | |
| 355 | for (unsigned int i = 0; i < mDisplayListData->children.size(); i++) { |
| 356 | DrawDisplayListOp* childOp = mDisplayListData->children[i]; |
| 357 | RenderNode* child = childOp->mDisplayList; |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 358 | float childZ = child->properties().getTranslationZ(); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 359 | |
| 360 | if (childZ != 0.0f) { |
| 361 | zTranslatedNodes.add(ZDrawDisplayListOpPair(childZ, childOp)); |
| 362 | childOp->mSkipInOrderDraw = true; |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 363 | } else if (!child->properties().getProjectBackwards()) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 364 | // regular, in order drawing DisplayList |
| 365 | childOp->mSkipInOrderDraw = false; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order) |
| 370 | std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end()); |
| 371 | } |
| 372 | |
| 373 | #define SHADOW_DELTA 0.1f |
| 374 | |
| 375 | template <class T> |
| 376 | void RenderNode::iterate3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes, |
| 377 | ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) { |
| 378 | const int size = zTranslatedNodes.size(); |
| 379 | if (size == 0 |
| 380 | || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f) |
| 381 | || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) { |
| 382 | // no 3d children to draw |
| 383 | return; |
| 384 | } |
| 385 | |
| 386 | int rootRestoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); |
| 387 | LinearAllocator& alloc = handler.allocator(); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 388 | ClipRectOp* clipOp = new (alloc) ClipRectOp(0, 0, properties().getWidth(), properties().getHeight(), |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 389 | SkRegion::kIntersect_Op); // clip to 3d root bounds |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 390 | handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 391 | |
| 392 | /** |
| 393 | * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters |
| 394 | * with very similar Z heights to draw together. |
| 395 | * |
| 396 | * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are |
| 397 | * underneath both, and neither's shadow is drawn on top of the other. |
| 398 | */ |
| 399 | const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes); |
| 400 | size_t drawIndex, shadowIndex, endIndex; |
| 401 | if (mode == kNegativeZChildren) { |
| 402 | drawIndex = 0; |
| 403 | endIndex = nonNegativeIndex; |
| 404 | shadowIndex = endIndex; // draw no shadows |
| 405 | } else { |
| 406 | drawIndex = nonNegativeIndex; |
| 407 | endIndex = size; |
| 408 | shadowIndex = drawIndex; // potentially draw shadow for each pos Z child |
| 409 | } |
| 410 | float lastCasterZ = 0.0f; |
| 411 | while (shadowIndex < endIndex || drawIndex < endIndex) { |
| 412 | if (shadowIndex < endIndex) { |
| 413 | DrawDisplayListOp* casterOp = zTranslatedNodes[shadowIndex].value; |
| 414 | RenderNode* caster = casterOp->mDisplayList; |
| 415 | const float casterZ = zTranslatedNodes[shadowIndex].key; |
| 416 | // attempt to render the shadow if the caster about to be drawn is its caster, |
| 417 | // OR if its caster's Z value is similar to the previous potential caster |
| 418 | if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) { |
| 419 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 420 | if (caster->properties().getAlpha() > 0.0f) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 421 | mat4 shadowMatrixXY(casterOp->mTransformFromParent); |
| 422 | caster->applyViewPropertyTransforms(shadowMatrixXY); |
| 423 | |
| 424 | // Z matrix needs actual 3d transformation, so mapped z values will be correct |
| 425 | mat4 shadowMatrixZ(casterOp->mTransformFromParent); |
| 426 | caster->applyViewPropertyTransforms(shadowMatrixZ, true); |
| 427 | |
| 428 | DisplayListOp* shadowOp = new (alloc) DrawShadowOp( |
| 429 | shadowMatrixXY, shadowMatrixZ, |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 430 | caster->properties().getAlpha(), caster->properties().getOutline().getPath(), |
| 431 | caster->properties().getWidth(), caster->properties().getHeight()); |
| 432 | handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | lastCasterZ = casterZ; // must do this even if current caster not casting a shadow |
| 436 | shadowIndex++; |
| 437 | continue; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | // only the actual child DL draw needs to be in save/restore, |
| 442 | // since it modifies the renderer's matrix |
| 443 | int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag); |
| 444 | |
| 445 | DrawDisplayListOp* childOp = zTranslatedNodes[drawIndex].value; |
| 446 | RenderNode* child = childOp->mDisplayList; |
| 447 | |
| 448 | renderer.concatMatrix(childOp->mTransformFromParent); |
| 449 | childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 450 | handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 451 | childOp->mSkipInOrderDraw = true; |
| 452 | |
| 453 | renderer.restoreToCount(restoreTo); |
| 454 | drawIndex++; |
| 455 | } |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 456 | handler(new (alloc) RestoreToCountOp(rootRestoreTo), PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | template <class T> |
| 460 | void RenderNode::iterateProjectedChildren(OpenGLRenderer& renderer, T& handler, const int level) { |
| 461 | int rootRestoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); |
| 462 | LinearAllocator& alloc = handler.allocator(); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 463 | ClipRectOp* clipOp = new (alloc) ClipRectOp(0, 0, properties().getWidth(), properties().getHeight(), |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 464 | SkRegion::kReplace_Op); // clip to projection surface root bounds |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 465 | handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 466 | |
| 467 | for (size_t i = 0; i < mProjectedNodes.size(); i++) { |
| 468 | DrawDisplayListOp* childOp = mProjectedNodes[i]; |
| 469 | |
| 470 | // matrix save, concat, and restore can be done safely without allocating operations |
| 471 | int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag); |
| 472 | renderer.concatMatrix(childOp->mTransformFromCompositingAncestor); |
| 473 | childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 474 | handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 475 | childOp->mSkipInOrderDraw = true; |
| 476 | renderer.restoreToCount(restoreTo); |
| 477 | } |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 478 | handler(new (alloc) RestoreToCountOp(rootRestoreTo), PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /** |
| 482 | * This function serves both defer and replay modes, and will organize the displayList's component |
| 483 | * operations for a single frame: |
| 484 | * |
| 485 | * Every 'simple' state operation that affects just the matrix and alpha (or other factors of |
| 486 | * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom |
| 487 | * defer logic) and operations in displayListOps are issued through the 'handler' which handles the |
| 488 | * defer vs replay logic, per operation |
| 489 | */ |
| 490 | template <class T> |
| 491 | void RenderNode::iterate(OpenGLRenderer& renderer, T& handler, const int level) { |
| 492 | if (CC_UNLIKELY(mDestroyed)) { // temporary debug logging |
| 493 | ALOGW("Error: %s is drawing after destruction", mName.string()); |
| 494 | CRASH(); |
| 495 | } |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 496 | if (mDisplayListData->isEmpty() || properties().getAlpha() <= 0) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 497 | DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, mName.string()); |
| 498 | return; |
| 499 | } |
| 500 | |
| 501 | #if DEBUG_DISPLAY_LIST |
| 502 | Rect* clipRect = renderer.getClipRect(); |
| 503 | DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), clipRect: %.0f, %.0f, %.0f, %.0f", |
| 504 | level * 2, "", this, mName.string(), clipRect->left, clipRect->top, |
| 505 | clipRect->right, clipRect->bottom); |
| 506 | #endif |
| 507 | |
| 508 | LinearAllocator& alloc = handler.allocator(); |
| 509 | int restoreTo = renderer.getSaveCount(); |
| 510 | handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag), |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 511 | PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 512 | |
| 513 | DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "", |
| 514 | SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo); |
| 515 | |
| 516 | setViewProperties<T>(renderer, handler, level + 1); |
| 517 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 518 | bool quickRejected = properties().getClipToBounds() && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 519 | if (!quickRejected) { |
| 520 | Vector<ZDrawDisplayListOpPair> zTranslatedNodes; |
| 521 | buildZSortedChildList(zTranslatedNodes); |
| 522 | |
| 523 | // for 3d root, draw children with negative z values |
| 524 | iterate3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler); |
| 525 | |
| 526 | DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance(); |
| 527 | const int saveCountOffset = renderer.getSaveCount() - 1; |
| 528 | const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex; |
| 529 | for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) { |
| 530 | DisplayListOp *op = mDisplayListData->displayListOps[i]; |
| 531 | |
| 532 | #if DEBUG_DISPLAY_LIST |
| 533 | op->output(level + 1); |
| 534 | #endif |
| 535 | |
| 536 | logBuffer.writeCommand(level, op->name()); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 537 | handler(op, saveCountOffset, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 538 | |
| 539 | if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) { |
| 540 | iterateProjectedChildren(renderer, handler, level); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | // for 3d root, draw children with positive z values |
| 545 | iterate3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler); |
| 546 | } |
| 547 | |
| 548 | DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo); |
| 549 | handler(new (alloc) RestoreToCountOp(restoreTo), |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 550 | PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 551 | renderer.setOverrideLayerAlpha(1.0f); |
| 552 | } |
| 553 | |
| 554 | } /* namespace uirenderer */ |
| 555 | } /* namespace android */ |