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> |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame^] | 135 | void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 136 | #if DEBUG_DISPLAY_LIST |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame^] | 137 | properties().debugOutputProperties(handler.level() + 1); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 138 | #endif |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 139 | if (properties().getLeft() != 0 || properties().getTop() != 0) { |
| 140 | renderer.translate(properties().getLeft(), properties().getTop()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 141 | } |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 142 | if (properties().getStaticMatrix()) { |
| 143 | renderer.concatMatrix(properties().getStaticMatrix()); |
| 144 | } else if (properties().getAnimationMatrix()) { |
| 145 | renderer.concatMatrix(properties().getAnimationMatrix()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 146 | } |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 147 | if (properties().getMatrixFlags() != 0) { |
| 148 | if (properties().getMatrixFlags() == TRANSLATION) { |
| 149 | renderer.translate(properties().getTranslationX(), properties().getTranslationY()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 150 | } else { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 151 | renderer.concatMatrix(*properties().getTransformMatrix()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 154 | bool clipToBoundsNeeded = properties().getCaching() ? false : properties().getClipToBounds(); |
| 155 | if (properties().getAlpha() < 1) { |
| 156 | if (properties().getCaching()) { |
| 157 | renderer.setOverrideLayerAlpha(properties().getAlpha()); |
| 158 | } else if (!properties().getHasOverlappingRendering()) { |
| 159 | renderer.scaleAlpha(properties().getAlpha()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 160 | } else { |
| 161 | // TODO: should be able to store the size of a DL at record time and not |
| 162 | // have to pass it into this call. In fact, this information might be in the |
| 163 | // location/size info that we store with the new native transform data. |
| 164 | int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag; |
| 165 | if (clipToBoundsNeeded) { |
| 166 | saveFlags |= SkCanvas::kClipToLayer_SaveFlag; |
| 167 | clipToBoundsNeeded = false; // clipping done by saveLayer |
| 168 | } |
| 169 | |
| 170 | SaveLayerOp* op = new (handler.allocator()) SaveLayerOp( |
Chris Craik | 8c271ca | 2014-03-25 10:33:01 -0700 | [diff] [blame] | 171 | 0, 0, properties().getWidth(), properties().getHeight(), |
| 172 | properties().getAlpha() * 255, saveFlags); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 173 | handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | if (clipToBoundsNeeded) { |
Chris Craik | 8c271ca | 2014-03-25 10:33:01 -0700 | [diff] [blame] | 177 | ClipRectOp* op = new (handler.allocator()) ClipRectOp( |
| 178 | 0, 0, properties().getWidth(), properties().getHeight(), SkRegion::kIntersect_Op); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 179 | handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 180 | } |
Chris Craik | 8c271ca | 2014-03-25 10:33:01 -0700 | [diff] [blame] | 181 | |
| 182 | if (CC_UNLIKELY(properties().hasClippingPath())) { |
| 183 | // TODO: optimize for round rect/circle clipping |
| 184 | const SkPath* path = properties().getClippingPath(); |
| 185 | ClipPathOp* op = new (handler.allocator()) ClipPathOp(path, SkRegion::kIntersect_Op); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 186 | handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Apply property-based transformations to input matrix |
| 192 | * |
| 193 | * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4 |
| 194 | * matrix computation instead of the Skia 3x3 matrix + camera hackery. |
| 195 | */ |
| 196 | void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 197 | if (properties().getLeft() != 0 || properties().getTop() != 0) { |
| 198 | matrix.translate(properties().getLeft(), properties().getTop()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 199 | } |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 200 | if (properties().getStaticMatrix()) { |
| 201 | mat4 stat(*properties().getStaticMatrix()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 202 | matrix.multiply(stat); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 203 | } else if (properties().getAnimationMatrix()) { |
| 204 | mat4 anim(*properties().getAnimationMatrix()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 205 | matrix.multiply(anim); |
| 206 | } |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 207 | if (properties().getMatrixFlags() != 0) { |
| 208 | if (properties().getMatrixFlags() == TRANSLATION) { |
| 209 | matrix.translate(properties().getTranslationX(), properties().getTranslationY(), |
| 210 | true3dTransform ? properties().getTranslationZ() : 0.0f); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 211 | } else { |
| 212 | if (!true3dTransform) { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 213 | matrix.multiply(*properties().getTransformMatrix()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 214 | } else { |
| 215 | mat4 true3dMat; |
| 216 | true3dMat.loadTranslate( |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 217 | properties().getPivotX() + properties().getTranslationX(), |
| 218 | properties().getPivotY() + properties().getTranslationY(), |
| 219 | properties().getTranslationZ()); |
| 220 | true3dMat.rotate(properties().getRotationX(), 1, 0, 0); |
| 221 | true3dMat.rotate(properties().getRotationY(), 0, 1, 0); |
| 222 | true3dMat.rotate(properties().getRotation(), 0, 0, 1); |
| 223 | true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1); |
| 224 | true3dMat.translate(-properties().getPivotX(), -properties().getPivotY()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 225 | |
| 226 | matrix.multiply(true3dMat); |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Organizes the DisplayList hierarchy to prepare for background projection reordering. |
| 234 | * |
| 235 | * This should be called before a call to defer() or drawDisplayList() |
| 236 | * |
| 237 | * Each DisplayList that serves as a 3d root builds its list of composited children, |
| 238 | * which are flagged to not draw in the standard draw loop. |
| 239 | */ |
| 240 | void RenderNode::computeOrdering() { |
| 241 | ATRACE_CALL(); |
| 242 | mProjectedNodes.clear(); |
| 243 | |
| 244 | // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that |
| 245 | // transform properties are applied correctly to top level children |
| 246 | if (mDisplayListData == NULL) return; |
| 247 | for (unsigned int i = 0; i < mDisplayListData->children.size(); i++) { |
| 248 | DrawDisplayListOp* childOp = mDisplayListData->children[i]; |
| 249 | childOp->mDisplayList->computeOrderingImpl(childOp, |
| 250 | &mProjectedNodes, &mat4::identity()); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | void RenderNode::computeOrderingImpl( |
| 255 | DrawDisplayListOp* opState, |
| 256 | Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface, |
| 257 | const mat4* transformFromProjectionSurface) { |
| 258 | mProjectedNodes.clear(); |
| 259 | if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return; |
| 260 | |
| 261 | // TODO: should avoid this calculation in most cases |
| 262 | // TODO: just calculate single matrix, down to all leaf composited elements |
| 263 | Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface); |
| 264 | localTransformFromProjectionSurface.multiply(opState->mTransformFromParent); |
| 265 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 266 | if (properties().getProjectBackwards()) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 267 | // composited projectee, flag for out of order draw, save matrix, and store in proj surface |
| 268 | opState->mSkipInOrderDraw = true; |
| 269 | opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface); |
| 270 | compositedChildrenOfProjectionSurface->add(opState); |
| 271 | } else { |
| 272 | // standard in order draw |
| 273 | opState->mSkipInOrderDraw = false; |
| 274 | } |
| 275 | |
| 276 | if (mDisplayListData->children.size() > 0) { |
| 277 | const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0; |
| 278 | bool haveAppliedPropertiesToProjection = false; |
| 279 | for (unsigned int i = 0; i < mDisplayListData->children.size(); i++) { |
| 280 | DrawDisplayListOp* childOp = mDisplayListData->children[i]; |
| 281 | RenderNode* child = childOp->mDisplayList; |
| 282 | |
| 283 | Vector<DrawDisplayListOp*>* projectionChildren = NULL; |
| 284 | const mat4* projectionTransform = NULL; |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 285 | if (isProjectionReceiver && !child->properties().getProjectBackwards()) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 286 | // if receiving projections, collect projecting descendent |
| 287 | |
| 288 | // Note that if a direct descendent is projecting backwards, we pass it's |
| 289 | // grandparent projection collection, since it shouldn't project onto it's |
| 290 | // parent, where it will already be drawing. |
| 291 | projectionChildren = &mProjectedNodes; |
| 292 | projectionTransform = &mat4::identity(); |
| 293 | } else { |
| 294 | if (!haveAppliedPropertiesToProjection) { |
| 295 | applyViewPropertyTransforms(localTransformFromProjectionSurface); |
| 296 | haveAppliedPropertiesToProjection = true; |
| 297 | } |
| 298 | projectionChildren = compositedChildrenOfProjectionSurface; |
| 299 | projectionTransform = &localTransformFromProjectionSurface; |
| 300 | } |
| 301 | child->computeOrderingImpl(childOp, projectionChildren, projectionTransform); |
| 302 | } |
| 303 | } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | class DeferOperationHandler { |
| 307 | public: |
| 308 | DeferOperationHandler(DeferStateStruct& deferStruct, int level) |
| 309 | : mDeferStruct(deferStruct), mLevel(level) {} |
| 310 | inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) { |
| 311 | operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds); |
| 312 | } |
| 313 | inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); } |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame^] | 314 | inline void startMark(const char* name) {} // do nothing |
| 315 | inline void endMark() {} |
| 316 | inline int level() { return mLevel; } |
| 317 | inline int replayFlags() { return mDeferStruct.mReplayFlags; } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 318 | |
| 319 | private: |
| 320 | DeferStateStruct& mDeferStruct; |
| 321 | const int mLevel; |
| 322 | }; |
| 323 | |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame^] | 324 | void RenderNode::deferNodeTree(DeferStateStruct& deferStruct) { |
| 325 | DeferOperationHandler handler(deferStruct, 0); |
| 326 | if (properties().getTranslationZ() > 0.0f) issueDrawShadowOperation(Matrix4::identity(), handler); |
| 327 | issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler); |
| 328 | } |
| 329 | |
| 330 | void RenderNode::deferNodeInParent(DeferStateStruct& deferStruct, const int level) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 331 | DeferOperationHandler handler(deferStruct, level); |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame^] | 332 | issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | class ReplayOperationHandler { |
| 336 | public: |
| 337 | ReplayOperationHandler(ReplayStateStruct& replayStruct, int level) |
| 338 | : mReplayStruct(replayStruct), mLevel(level) {} |
| 339 | inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) { |
| 340 | #if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 341 | properties().getReplayStruct().mRenderer.eventMark(operation->name()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 342 | #endif |
| 343 | operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds); |
| 344 | } |
| 345 | inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); } |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame^] | 346 | inline void startMark(const char* name) { |
| 347 | mReplayStruct.mRenderer.startMark(name); |
| 348 | } |
| 349 | inline void endMark() { |
| 350 | mReplayStruct.mRenderer.endMark(); |
| 351 | DISPLAY_LIST_LOGD("%*sDone (%p, %s), returning %d", level * 2, "", this, mName.string(), |
| 352 | mReplayStruct.mDrawGlStatus); |
| 353 | } |
| 354 | inline int level() { return mLevel; } |
| 355 | inline int replayFlags() { return mReplayStruct.mReplayFlags; } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 356 | |
| 357 | private: |
| 358 | ReplayStateStruct& mReplayStruct; |
| 359 | const int mLevel; |
| 360 | }; |
| 361 | |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame^] | 362 | void RenderNode::replayNodeTree(ReplayStateStruct& replayStruct) { |
| 363 | ReplayOperationHandler handler(replayStruct, 0); |
| 364 | if (properties().getTranslationZ() > 0.0f) issueDrawShadowOperation(Matrix4::identity(), handler); |
| 365 | issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler); |
| 366 | } |
| 367 | |
| 368 | void RenderNode::replayNodeInParent(ReplayStateStruct& replayStruct, const int level) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 369 | ReplayOperationHandler handler(replayStruct, level); |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame^] | 370 | issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | void RenderNode::buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes) { |
| 374 | if (mDisplayListData == NULL || mDisplayListData->children.size() == 0) return; |
| 375 | |
| 376 | for (unsigned int i = 0; i < mDisplayListData->children.size(); i++) { |
| 377 | DrawDisplayListOp* childOp = mDisplayListData->children[i]; |
| 378 | RenderNode* child = childOp->mDisplayList; |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 379 | float childZ = child->properties().getTranslationZ(); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 380 | |
| 381 | if (childZ != 0.0f) { |
| 382 | zTranslatedNodes.add(ZDrawDisplayListOpPair(childZ, childOp)); |
| 383 | childOp->mSkipInOrderDraw = true; |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 384 | } else if (!child->properties().getProjectBackwards()) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 385 | // regular, in order drawing DisplayList |
| 386 | childOp->mSkipInOrderDraw = false; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order) |
| 391 | std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end()); |
| 392 | } |
| 393 | |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame^] | 394 | template <class T> |
| 395 | void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) { |
| 396 | if (properties().getAlpha() <= 0.0f) return; |
| 397 | |
| 398 | mat4 shadowMatrixXY(transformFromParent); |
| 399 | applyViewPropertyTransforms(shadowMatrixXY); |
| 400 | |
| 401 | // Z matrix needs actual 3d transformation, so mapped z values will be correct |
| 402 | mat4 shadowMatrixZ(transformFromParent); |
| 403 | applyViewPropertyTransforms(shadowMatrixZ, true); |
| 404 | |
| 405 | const SkPath* outlinePath = properties().getOutline().getPath(); |
| 406 | const RevealClip& revealClip = properties().getRevealClip(); |
| 407 | const SkPath* revealClipPath = revealClip.hasConvexClip() |
| 408 | ? revealClip.getPath() : NULL; // only pass the reveal clip's path if it's convex |
| 409 | |
| 410 | /** |
| 411 | * The drawing area of the caster is always the same as the its perimeter (which |
| 412 | * the shadow system uses) *except* in the inverse clip case. Inform the shadow |
| 413 | * system that the caster's drawing area (as opposed to its perimeter) has been |
| 414 | * clipped, so that it knows the caster can't be opaque. |
| 415 | */ |
| 416 | bool casterUnclipped = !revealClip.willClip() || revealClip.hasConvexClip(); |
| 417 | |
| 418 | DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp( |
| 419 | shadowMatrixXY, shadowMatrixZ, |
| 420 | properties().getAlpha(), casterUnclipped, |
| 421 | properties().getWidth(), properties().getHeight(), |
| 422 | outlinePath, revealClipPath); |
| 423 | handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
| 424 | } |
| 425 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 426 | #define SHADOW_DELTA 0.1f |
| 427 | |
| 428 | template <class T> |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame^] | 429 | void RenderNode::issueOperationsOf3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes, |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 430 | ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) { |
| 431 | const int size = zTranslatedNodes.size(); |
| 432 | if (size == 0 |
| 433 | || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f) |
| 434 | || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) { |
| 435 | // no 3d children to draw |
| 436 | return; |
| 437 | } |
| 438 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 439 | /** |
| 440 | * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters |
| 441 | * with very similar Z heights to draw together. |
| 442 | * |
| 443 | * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are |
| 444 | * underneath both, and neither's shadow is drawn on top of the other. |
| 445 | */ |
| 446 | const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes); |
| 447 | size_t drawIndex, shadowIndex, endIndex; |
| 448 | if (mode == kNegativeZChildren) { |
| 449 | drawIndex = 0; |
| 450 | endIndex = nonNegativeIndex; |
| 451 | shadowIndex = endIndex; // draw no shadows |
| 452 | } else { |
| 453 | drawIndex = nonNegativeIndex; |
| 454 | endIndex = size; |
| 455 | shadowIndex = drawIndex; // potentially draw shadow for each pos Z child |
| 456 | } |
| 457 | float lastCasterZ = 0.0f; |
| 458 | while (shadowIndex < endIndex || drawIndex < endIndex) { |
| 459 | if (shadowIndex < endIndex) { |
| 460 | DrawDisplayListOp* casterOp = zTranslatedNodes[shadowIndex].value; |
| 461 | RenderNode* caster = casterOp->mDisplayList; |
| 462 | const float casterZ = zTranslatedNodes[shadowIndex].key; |
| 463 | // attempt to render the shadow if the caster about to be drawn is its caster, |
| 464 | // OR if its caster's Z value is similar to the previous potential caster |
| 465 | if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) { |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame^] | 466 | caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 467 | |
| 468 | lastCasterZ = casterZ; // must do this even if current caster not casting a shadow |
| 469 | shadowIndex++; |
| 470 | continue; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | // only the actual child DL draw needs to be in save/restore, |
| 475 | // since it modifies the renderer's matrix |
| 476 | int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag); |
| 477 | |
| 478 | DrawDisplayListOp* childOp = zTranslatedNodes[drawIndex].value; |
| 479 | RenderNode* child = childOp->mDisplayList; |
| 480 | |
| 481 | renderer.concatMatrix(childOp->mTransformFromParent); |
| 482 | childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 483 | handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 484 | childOp->mSkipInOrderDraw = true; |
| 485 | |
| 486 | renderer.restoreToCount(restoreTo); |
| 487 | drawIndex++; |
| 488 | } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | template <class T> |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame^] | 492 | void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 493 | for (size_t i = 0; i < mProjectedNodes.size(); i++) { |
| 494 | DrawDisplayListOp* childOp = mProjectedNodes[i]; |
| 495 | |
| 496 | // matrix save, concat, and restore can be done safely without allocating operations |
| 497 | int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag); |
| 498 | renderer.concatMatrix(childOp->mTransformFromCompositingAncestor); |
| 499 | childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 500 | handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 501 | childOp->mSkipInOrderDraw = true; |
| 502 | renderer.restoreToCount(restoreTo); |
| 503 | } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | /** |
| 507 | * This function serves both defer and replay modes, and will organize the displayList's component |
| 508 | * operations for a single frame: |
| 509 | * |
| 510 | * Every 'simple' state operation that affects just the matrix and alpha (or other factors of |
| 511 | * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom |
| 512 | * defer logic) and operations in displayListOps are issued through the 'handler' which handles the |
| 513 | * defer vs replay logic, per operation |
| 514 | */ |
| 515 | template <class T> |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame^] | 516 | void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) { |
| 517 | const int level = handler.level(); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 518 | if (CC_UNLIKELY(mDestroyed)) { // temporary debug logging |
| 519 | ALOGW("Error: %s is drawing after destruction", mName.string()); |
| 520 | CRASH(); |
| 521 | } |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 522 | if (mDisplayListData->isEmpty() || properties().getAlpha() <= 0) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 523 | DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, mName.string()); |
| 524 | return; |
| 525 | } |
| 526 | |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame^] | 527 | handler.startMark(mName.string()); |
| 528 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 529 | #if DEBUG_DISPLAY_LIST |
| 530 | Rect* clipRect = renderer.getClipRect(); |
| 531 | DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), clipRect: %.0f, %.0f, %.0f, %.0f", |
| 532 | level * 2, "", this, mName.string(), clipRect->left, clipRect->top, |
| 533 | clipRect->right, clipRect->bottom); |
| 534 | #endif |
| 535 | |
| 536 | LinearAllocator& alloc = handler.allocator(); |
| 537 | int restoreTo = renderer.getSaveCount(); |
| 538 | handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag), |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 539 | PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 540 | |
| 541 | DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "", |
| 542 | SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo); |
| 543 | |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame^] | 544 | setViewProperties<T>(renderer, handler); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 545 | |
Chris Craik | 8c271ca | 2014-03-25 10:33:01 -0700 | [diff] [blame] | 546 | bool quickRejected = properties().getClipToBounds() |
| 547 | && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 548 | if (!quickRejected) { |
| 549 | Vector<ZDrawDisplayListOpPair> zTranslatedNodes; |
| 550 | buildZSortedChildList(zTranslatedNodes); |
| 551 | |
| 552 | // for 3d root, draw children with negative z values |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame^] | 553 | issueOperationsOf3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 554 | |
| 555 | DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance(); |
| 556 | const int saveCountOffset = renderer.getSaveCount() - 1; |
| 557 | const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex; |
| 558 | for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) { |
| 559 | DisplayListOp *op = mDisplayListData->displayListOps[i]; |
| 560 | |
| 561 | #if DEBUG_DISPLAY_LIST |
| 562 | op->output(level + 1); |
| 563 | #endif |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 564 | logBuffer.writeCommand(level, op->name()); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 565 | handler(op, saveCountOffset, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 566 | |
| 567 | if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) { |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame^] | 568 | issueOperationsOfProjectedChildren(renderer, handler); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
| 572 | // for 3d root, draw children with positive z values |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame^] | 573 | issueOperationsOf3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo); |
| 577 | handler(new (alloc) RestoreToCountOp(restoreTo), |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 578 | PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 579 | renderer.setOverrideLayerAlpha(1.0f); |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame^] | 580 | |
| 581 | handler.endMark(); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | } /* namespace uirenderer */ |
| 585 | } /* namespace android */ |