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