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