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