Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #include "OpReorderer.h" |
| 18 | |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 19 | #include "LayerUpdateQueue.h" |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 20 | #include "RenderNode.h" |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame^] | 21 | #include "renderstate/OffscreenBufferPool.h" |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 22 | #include "utils/FatVector.h" |
| 23 | #include "utils/PaintUtils.h" |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 24 | |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 25 | #include <SkCanvas.h> |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 26 | #include <SkPathOps.h> |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 27 | #include <utils/Trace.h> |
| 28 | #include <utils/TypeHelpers.h> |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | namespace uirenderer { |
| 32 | |
| 33 | class BatchBase { |
| 34 | |
| 35 | public: |
| 36 | BatchBase(batchid_t batchId, BakedOpState* op, bool merging) |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame^] | 37 | : mBatchId(batchId) |
| 38 | , mMerging(merging) { |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 39 | mBounds = op->computedState.clippedBounds; |
| 40 | mOps.push_back(op); |
| 41 | } |
| 42 | |
| 43 | bool intersects(const Rect& rect) const { |
| 44 | if (!rect.intersects(mBounds)) return false; |
| 45 | |
| 46 | for (const BakedOpState* op : mOps) { |
| 47 | if (rect.intersects(op->computedState.clippedBounds)) { |
| 48 | return true; |
| 49 | } |
| 50 | } |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | batchid_t getBatchId() const { return mBatchId; } |
| 55 | bool isMerging() const { return mMerging; } |
| 56 | |
| 57 | const std::vector<BakedOpState*>& getOps() const { return mOps; } |
| 58 | |
| 59 | void dump() const { |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 60 | ALOGD(" Batch %p, id %d, merging %d, count %d, bounds " RECT_STRING, |
| 61 | this, mBatchId, mMerging, mOps.size(), RECT_ARGS(mBounds)); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 62 | } |
| 63 | protected: |
| 64 | batchid_t mBatchId; |
| 65 | Rect mBounds; |
| 66 | std::vector<BakedOpState*> mOps; |
| 67 | bool mMerging; |
| 68 | }; |
| 69 | |
| 70 | class OpBatch : public BatchBase { |
| 71 | public: |
| 72 | static void* operator new(size_t size, LinearAllocator& allocator) { |
| 73 | return allocator.alloc(size); |
| 74 | } |
| 75 | |
| 76 | OpBatch(batchid_t batchId, BakedOpState* op) |
| 77 | : BatchBase(batchId, op, false) { |
| 78 | } |
| 79 | |
| 80 | void batchOp(BakedOpState* op) { |
| 81 | mBounds.unionWith(op->computedState.clippedBounds); |
| 82 | mOps.push_back(op); |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | class MergingOpBatch : public BatchBase { |
| 87 | public: |
| 88 | static void* operator new(size_t size, LinearAllocator& allocator) { |
| 89 | return allocator.alloc(size); |
| 90 | } |
| 91 | |
| 92 | MergingOpBatch(batchid_t batchId, BakedOpState* op) |
| 93 | : BatchBase(batchId, op, true) { |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Helper for determining if a new op can merge with a MergingDrawBatch based on their bounds |
| 98 | * and clip side flags. Positive bounds delta means new bounds fit in old. |
| 99 | */ |
| 100 | static inline bool checkSide(const int currentFlags, const int newFlags, const int side, |
| 101 | float boundsDelta) { |
| 102 | bool currentClipExists = currentFlags & side; |
| 103 | bool newClipExists = newFlags & side; |
| 104 | |
| 105 | // if current is clipped, we must be able to fit new bounds in current |
| 106 | if (boundsDelta > 0 && currentClipExists) return false; |
| 107 | |
| 108 | // if new is clipped, we must be able to fit current bounds in new |
| 109 | if (boundsDelta < 0 && newClipExists) return false; |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | static bool paintIsDefault(const SkPaint& paint) { |
| 115 | return paint.getAlpha() == 255 |
| 116 | && paint.getColorFilter() == nullptr |
| 117 | && paint.getShader() == nullptr; |
| 118 | } |
| 119 | |
| 120 | static bool paintsAreEquivalent(const SkPaint& a, const SkPaint& b) { |
| 121 | return a.getAlpha() == b.getAlpha() |
| 122 | && a.getColorFilter() == b.getColorFilter() |
| 123 | && a.getShader() == b.getShader(); |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Checks if a (mergeable) op can be merged into this batch |
| 128 | * |
| 129 | * If true, the op's multiDraw must be guaranteed to handle both ops simultaneously, so it is |
| 130 | * important to consider all paint attributes used in the draw calls in deciding both a) if an |
| 131 | * op tries to merge at all, and b) if the op can merge with another set of ops |
| 132 | * |
| 133 | * False positives can lead to information from the paints of subsequent merged operations being |
| 134 | * dropped, so we make simplifying qualifications on the ops that can merge, per op type. |
| 135 | */ |
| 136 | bool canMergeWith(BakedOpState* op) const { |
| 137 | bool isTextBatch = getBatchId() == OpBatchType::Text |
| 138 | || getBatchId() == OpBatchType::ColorText; |
| 139 | |
| 140 | // Overlapping other operations is only allowed for text without shadow. For other ops, |
| 141 | // multiDraw isn't guaranteed to overdraw correctly |
| 142 | if (!isTextBatch || PaintUtils::hasTextShadow(op->op->paint)) { |
| 143 | if (intersects(op->computedState.clippedBounds)) return false; |
| 144 | } |
| 145 | |
| 146 | const BakedOpState* lhs = op; |
| 147 | const BakedOpState* rhs = mOps[0]; |
| 148 | |
| 149 | if (!MathUtils::areEqual(lhs->alpha, rhs->alpha)) return false; |
| 150 | |
| 151 | // Identical round rect clip state means both ops will clip in the same way, or not at all. |
| 152 | // As the state objects are const, we can compare their pointers to determine mergeability |
| 153 | if (lhs->roundRectClipState != rhs->roundRectClipState) return false; |
| 154 | if (lhs->projectionPathMask != rhs->projectionPathMask) return false; |
| 155 | |
| 156 | /* Clipping compatibility check |
| 157 | * |
| 158 | * Exploits the fact that if a op or batch is clipped on a side, its bounds will equal its |
| 159 | * clip for that side. |
| 160 | */ |
| 161 | const int currentFlags = mClipSideFlags; |
| 162 | const int newFlags = op->computedState.clipSideFlags; |
| 163 | if (currentFlags != OpClipSideFlags::None || newFlags != OpClipSideFlags::None) { |
| 164 | const Rect& opBounds = op->computedState.clippedBounds; |
| 165 | float boundsDelta = mBounds.left - opBounds.left; |
| 166 | if (!checkSide(currentFlags, newFlags, OpClipSideFlags::Left, boundsDelta)) return false; |
| 167 | boundsDelta = mBounds.top - opBounds.top; |
| 168 | if (!checkSide(currentFlags, newFlags, OpClipSideFlags::Top, boundsDelta)) return false; |
| 169 | |
| 170 | // right and bottom delta calculation reversed to account for direction |
| 171 | boundsDelta = opBounds.right - mBounds.right; |
| 172 | if (!checkSide(currentFlags, newFlags, OpClipSideFlags::Right, boundsDelta)) return false; |
| 173 | boundsDelta = opBounds.bottom - mBounds.bottom; |
| 174 | if (!checkSide(currentFlags, newFlags, OpClipSideFlags::Bottom, boundsDelta)) return false; |
| 175 | } |
| 176 | |
| 177 | const SkPaint* newPaint = op->op->paint; |
| 178 | const SkPaint* oldPaint = mOps[0]->op->paint; |
| 179 | |
| 180 | if (newPaint == oldPaint) { |
| 181 | // if paints are equal, then modifiers + paint attribs don't need to be compared |
| 182 | return true; |
| 183 | } else if (newPaint && !oldPaint) { |
| 184 | return paintIsDefault(*newPaint); |
| 185 | } else if (!newPaint && oldPaint) { |
| 186 | return paintIsDefault(*oldPaint); |
| 187 | } |
| 188 | return paintsAreEquivalent(*newPaint, *oldPaint); |
| 189 | } |
| 190 | |
| 191 | void mergeOp(BakedOpState* op) { |
| 192 | mBounds.unionWith(op->computedState.clippedBounds); |
| 193 | mOps.push_back(op); |
| 194 | |
| 195 | const int newClipSideFlags = op->computedState.clipSideFlags; |
| 196 | mClipSideFlags |= newClipSideFlags; |
| 197 | |
| 198 | const Rect& opClip = op->computedState.clipRect; |
| 199 | if (newClipSideFlags & OpClipSideFlags::Left) mClipRect.left = opClip.left; |
| 200 | if (newClipSideFlags & OpClipSideFlags::Top) mClipRect.top = opClip.top; |
| 201 | if (newClipSideFlags & OpClipSideFlags::Right) mClipRect.right = opClip.right; |
| 202 | if (newClipSideFlags & OpClipSideFlags::Bottom) mClipRect.bottom = opClip.bottom; |
| 203 | } |
| 204 | |
| 205 | private: |
| 206 | int mClipSideFlags = 0; |
| 207 | Rect mClipRect; |
| 208 | }; |
| 209 | |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 210 | OpReorderer::LayerReorderer::LayerReorderer(uint32_t width, uint32_t height, |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame^] | 211 | const Rect& repaintRect, const BeginLayerOp* beginLayerOp, RenderNode* renderNode) |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 212 | : width(width) |
| 213 | , height(height) |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame^] | 214 | , repaintRect(repaintRect) |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 215 | , offscreenBuffer(renderNode ? renderNode->getLayer() : nullptr) |
| 216 | , beginLayerOp(beginLayerOp) |
| 217 | , renderNode(renderNode) {} |
| 218 | |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 219 | // iterate back toward target to see if anything drawn since should overlap the new op |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 220 | // if no target, merging ops still iterate to find similar batch to insert after |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 221 | void OpReorderer::LayerReorderer::locateInsertIndex(int batchId, const Rect& clippedBounds, |
| 222 | BatchBase** targetBatch, size_t* insertBatchIndex) const { |
| 223 | for (int i = mBatches.size() - 1; i >= 0; i--) { |
| 224 | BatchBase* overBatch = mBatches[i]; |
| 225 | |
| 226 | if (overBatch == *targetBatch) break; |
| 227 | |
| 228 | // TODO: also consider shader shared between batch types |
| 229 | if (batchId == overBatch->getBatchId()) { |
| 230 | *insertBatchIndex = i + 1; |
| 231 | if (!*targetBatch) break; // found insert position, quit |
| 232 | } |
| 233 | |
| 234 | if (overBatch->intersects(clippedBounds)) { |
| 235 | // NOTE: it may be possible to optimize for special cases where two operations |
| 236 | // of the same batch/paint could swap order, such as with a non-mergeable |
| 237 | // (clipped) and a mergeable text operation |
| 238 | *targetBatch = nullptr; |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | void OpReorderer::LayerReorderer::deferUnmergeableOp(LinearAllocator& allocator, |
| 245 | BakedOpState* op, batchid_t batchId) { |
| 246 | OpBatch* targetBatch = mBatchLookup[batchId]; |
| 247 | |
| 248 | size_t insertBatchIndex = mBatches.size(); |
| 249 | if (targetBatch) { |
| 250 | locateInsertIndex(batchId, op->computedState.clippedBounds, |
| 251 | (BatchBase**)(&targetBatch), &insertBatchIndex); |
| 252 | } |
| 253 | |
| 254 | if (targetBatch) { |
| 255 | targetBatch->batchOp(op); |
| 256 | } else { |
| 257 | // new non-merging batch |
| 258 | targetBatch = new (allocator) OpBatch(batchId, op); |
| 259 | mBatchLookup[batchId] = targetBatch; |
| 260 | mBatches.insert(mBatches.begin() + insertBatchIndex, targetBatch); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | // insertion point of a new batch, will hopefully be immediately after similar batch |
| 265 | // (generally, should be similar shader) |
| 266 | void OpReorderer::LayerReorderer::deferMergeableOp(LinearAllocator& allocator, |
| 267 | BakedOpState* op, batchid_t batchId, mergeid_t mergeId) { |
| 268 | MergingOpBatch* targetBatch = nullptr; |
| 269 | |
| 270 | // Try to merge with any existing batch with same mergeId |
| 271 | auto getResult = mMergingBatchLookup[batchId].find(mergeId); |
| 272 | if (getResult != mMergingBatchLookup[batchId].end()) { |
| 273 | targetBatch = getResult->second; |
| 274 | if (!targetBatch->canMergeWith(op)) { |
| 275 | targetBatch = nullptr; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | size_t insertBatchIndex = mBatches.size(); |
| 280 | locateInsertIndex(batchId, op->computedState.clippedBounds, |
| 281 | (BatchBase**)(&targetBatch), &insertBatchIndex); |
| 282 | |
| 283 | if (targetBatch) { |
| 284 | targetBatch->mergeOp(op); |
| 285 | } else { |
| 286 | // new merging batch |
| 287 | targetBatch = new (allocator) MergingOpBatch(batchId, op); |
| 288 | mMergingBatchLookup[batchId].insert(std::make_pair(mergeId, targetBatch)); |
| 289 | |
| 290 | mBatches.insert(mBatches.begin() + insertBatchIndex, targetBatch); |
| 291 | } |
| 292 | } |
| 293 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame] | 294 | void OpReorderer::LayerReorderer::replayBakedOpsImpl(void* arg, BakedOpDispatcher* receivers) const { |
| 295 | ATRACE_NAME("flush drawing commands"); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 296 | for (const BatchBase* batch : mBatches) { |
| 297 | // TODO: different behavior based on batch->isMerging() |
| 298 | for (const BakedOpState* op : batch->getOps()) { |
| 299 | receivers[op->op->opId](arg, *op->op, *op); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | void OpReorderer::LayerReorderer::dump() const { |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 305 | ALOGD("LayerReorderer %p, %ux%u buffer %p, blo %p, rn %p", |
| 306 | this, width, height, offscreenBuffer, beginLayerOp, renderNode); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 307 | for (const BatchBase* batch : mBatches) { |
| 308 | batch->dump(); |
| 309 | } |
| 310 | } |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 311 | |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 312 | OpReorderer::OpReorderer(const LayerUpdateQueue& layers, const SkRect& clip, |
| 313 | uint32_t viewportWidth, uint32_t viewportHeight, |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame^] | 314 | const std::vector< sp<RenderNode> >& nodes, const Vector3& lightCenter) |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 315 | : mCanvasState(*this) { |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 316 | ATRACE_NAME("prepare drawing commands"); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 317 | |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame^] | 318 | mLayerReorderers.reserve(layers.entries().size()); |
| 319 | mLayerStack.reserve(layers.entries().size()); |
| 320 | |
| 321 | // Prepare to defer Fbo0 |
| 322 | mLayerReorderers.emplace_back(viewportWidth, viewportHeight, Rect(clip)); |
| 323 | mLayerStack.push_back(0); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 324 | mCanvasState.initializeSaveStack(viewportWidth, viewportHeight, |
Chris Craik | ddf2215 | 2015-10-14 17:42:47 -0700 | [diff] [blame] | 325 | clip.fLeft, clip.fTop, clip.fRight, clip.fBottom, |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame^] | 326 | lightCenter); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 327 | |
| 328 | // Render all layers to be updated, in order. Defer in reverse order, so that they'll be |
| 329 | // updated in the order they're passed in (mLayerReorderers are issued to Renderer in reverse) |
| 330 | for (int i = layers.entries().size() - 1; i >= 0; i--) { |
| 331 | RenderNode* layerNode = layers.entries()[i].renderNode; |
| 332 | const Rect& layerDamage = layers.entries()[i].damage; |
| 333 | |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame^] | 334 | saveForLayer(layerNode->getWidth(), layerNode->getHeight(), |
| 335 | layerDamage, nullptr, layerNode); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 336 | mCanvasState.writableSnapshot()->setClip( |
| 337 | layerDamage.left, layerDamage.top, layerDamage.right, layerDamage.bottom); |
| 338 | |
| 339 | if (layerNode->getDisplayList()) { |
| 340 | deferImpl(*(layerNode->getDisplayList())); |
| 341 | } |
| 342 | restoreForLayer(); |
| 343 | } |
| 344 | |
| 345 | // Defer Fbo0 |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 346 | for (const sp<RenderNode>& node : nodes) { |
| 347 | if (node->nothingToDraw()) continue; |
| 348 | |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 349 | int count = mCanvasState.save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag); |
| 350 | deferNodePropsAndOps(*node); |
| 351 | mCanvasState.restoreToCount(count); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame^] | 355 | OpReorderer::OpReorderer(int viewportWidth, int viewportHeight, const DisplayList& displayList, |
| 356 | const Vector3& lightCenter) |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 357 | : mCanvasState(*this) { |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 358 | ATRACE_NAME("prepare drawing commands"); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame^] | 359 | // Prepare to defer Fbo0 |
| 360 | mLayerReorderers.emplace_back(viewportWidth, viewportHeight, |
| 361 | Rect(viewportWidth, viewportHeight)); |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 362 | mLayerStack.push_back(0); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 363 | mCanvasState.initializeSaveStack(viewportWidth, viewportHeight, |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame^] | 364 | 0, 0, viewportWidth, viewportHeight, lightCenter); |
| 365 | |
Chris Craik | b36af87 | 2015-10-16 14:23:12 -0700 | [diff] [blame] | 366 | deferImpl(displayList); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 369 | void OpReorderer::onViewportInitialized() {} |
| 370 | |
| 371 | void OpReorderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {} |
| 372 | |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 373 | void OpReorderer::deferNodePropsAndOps(RenderNode& node) { |
Chris Craik | 76caecf | 2015-11-02 19:17:45 -0800 | [diff] [blame] | 374 | if (node.applyViewProperties(mCanvasState, mAllocator)) { |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 375 | // not rejected so render |
| 376 | if (node.getLayer()) { |
| 377 | // HW layer |
| 378 | LayerOp* drawLayerOp = new (mAllocator) LayerOp(node); |
| 379 | BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp); |
| 380 | if (bakedOpState) { |
| 381 | // Layer will be drawn into parent layer (which is now current, since we popped mLayerStack) |
| 382 | currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap); |
| 383 | } |
| 384 | } else { |
| 385 | deferImpl(*(node.getDisplayList())); |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 390 | typedef key_value_pair_t<float, const RenderNodeOp*> ZRenderNodeOpPair; |
| 391 | |
| 392 | template <typename V> |
| 393 | static void buildZSortedChildList(V* zTranslatedNodes, |
| 394 | const DisplayList& displayList, const DisplayList::Chunk& chunk) { |
| 395 | if (chunk.beginChildIndex == chunk.endChildIndex) return; |
| 396 | |
| 397 | for (size_t i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) { |
| 398 | RenderNodeOp* childOp = displayList.getChildren()[i]; |
| 399 | RenderNode* child = childOp->renderNode; |
| 400 | float childZ = child->properties().getZ(); |
| 401 | |
| 402 | if (!MathUtils::isZero(childZ) && chunk.reorderChildren) { |
| 403 | zTranslatedNodes->push_back(ZRenderNodeOpPair(childZ, childOp)); |
| 404 | childOp->skipInOrderDraw = true; |
| 405 | } else if (!child->properties().getProjectBackwards()) { |
| 406 | // regular, in order drawing DisplayList |
| 407 | childOp->skipInOrderDraw = false; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order) |
| 412 | std::stable_sort(zTranslatedNodes->begin(), zTranslatedNodes->end()); |
| 413 | } |
| 414 | |
| 415 | template <typename V> |
| 416 | static size_t findNonNegativeIndex(const V& zTranslatedNodes) { |
| 417 | for (size_t i = 0; i < zTranslatedNodes.size(); i++) { |
| 418 | if (zTranslatedNodes[i].key >= 0.0f) return i; |
| 419 | } |
| 420 | return zTranslatedNodes.size(); |
| 421 | } |
| 422 | |
| 423 | template <typename V> |
| 424 | void OpReorderer::defer3dChildren(ChildrenSelectMode mode, const V& zTranslatedNodes) { |
| 425 | const int size = zTranslatedNodes.size(); |
| 426 | if (size == 0 |
| 427 | || (mode == ChildrenSelectMode::Negative&& zTranslatedNodes[0].key > 0.0f) |
| 428 | || (mode == ChildrenSelectMode::Positive && zTranslatedNodes[size - 1].key < 0.0f)) { |
| 429 | // no 3d children to draw |
| 430 | return; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters |
| 435 | * with very similar Z heights to draw together. |
| 436 | * |
| 437 | * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are |
| 438 | * underneath both, and neither's shadow is drawn on top of the other. |
| 439 | */ |
| 440 | const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes); |
| 441 | size_t drawIndex, shadowIndex, endIndex; |
| 442 | if (mode == ChildrenSelectMode::Negative) { |
| 443 | drawIndex = 0; |
| 444 | endIndex = nonNegativeIndex; |
| 445 | shadowIndex = endIndex; // draw no shadows |
| 446 | } else { |
| 447 | drawIndex = nonNegativeIndex; |
| 448 | endIndex = size; |
| 449 | shadowIndex = drawIndex; // potentially draw shadow for each pos Z child |
| 450 | } |
| 451 | |
| 452 | float lastCasterZ = 0.0f; |
| 453 | while (shadowIndex < endIndex || drawIndex < endIndex) { |
| 454 | if (shadowIndex < endIndex) { |
| 455 | const RenderNodeOp* casterNodeOp = zTranslatedNodes[shadowIndex].value; |
| 456 | const float casterZ = zTranslatedNodes[shadowIndex].key; |
| 457 | // attempt to render the shadow if the caster about to be drawn is its caster, |
| 458 | // OR if its caster's Z value is similar to the previous potential caster |
| 459 | if (shadowIndex == drawIndex || casterZ - lastCasterZ < 0.1f) { |
| 460 | deferShadow(*casterNodeOp); |
| 461 | |
| 462 | lastCasterZ = casterZ; // must do this even if current caster not casting a shadow |
| 463 | shadowIndex++; |
| 464 | continue; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | const RenderNodeOp* childOp = zTranslatedNodes[drawIndex].value; |
| 469 | deferRenderNodeOp(*childOp); |
| 470 | drawIndex++; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | void OpReorderer::deferShadow(const RenderNodeOp& casterNodeOp) { |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 475 | auto& node = *casterNodeOp.renderNode; |
| 476 | auto& properties = node.properties(); |
| 477 | |
| 478 | if (properties.getAlpha() <= 0.0f |
| 479 | || properties.getOutline().getAlpha() <= 0.0f |
| 480 | || !properties.getOutline().getPath() |
| 481 | || properties.getScaleX() == 0 |
| 482 | || properties.getScaleY() == 0) { |
| 483 | // no shadow to draw |
| 484 | return; |
| 485 | } |
| 486 | |
| 487 | const SkPath* casterOutlinePath = properties.getOutline().getPath(); |
| 488 | const SkPath* revealClipPath = properties.getRevealClip().getPath(); |
| 489 | if (revealClipPath && revealClipPath->isEmpty()) return; |
| 490 | |
| 491 | float casterAlpha = properties.getAlpha() * properties.getOutline().getAlpha(); |
| 492 | |
| 493 | // holds temporary SkPath to store the result of intersections |
| 494 | SkPath* frameAllocatedPath = nullptr; |
| 495 | const SkPath* casterPath = casterOutlinePath; |
| 496 | |
| 497 | // intersect the shadow-casting path with the reveal, if present |
| 498 | if (revealClipPath) { |
| 499 | frameAllocatedPath = createFrameAllocatedPath(); |
| 500 | |
| 501 | Op(*casterPath, *revealClipPath, kIntersect_SkPathOp, frameAllocatedPath); |
| 502 | casterPath = frameAllocatedPath; |
| 503 | } |
| 504 | |
| 505 | // intersect the shadow-casting path with the clipBounds, if present |
| 506 | if (properties.getClippingFlags() & CLIP_TO_CLIP_BOUNDS) { |
| 507 | if (!frameAllocatedPath) { |
| 508 | frameAllocatedPath = createFrameAllocatedPath(); |
| 509 | } |
| 510 | Rect clipBounds; |
| 511 | properties.getClippingRectForFlags(CLIP_TO_CLIP_BOUNDS, &clipBounds); |
| 512 | SkPath clipBoundsPath; |
| 513 | clipBoundsPath.addRect(clipBounds.left, clipBounds.top, |
| 514 | clipBounds.right, clipBounds.bottom); |
| 515 | |
| 516 | Op(*casterPath, clipBoundsPath, kIntersect_SkPathOp, frameAllocatedPath); |
| 517 | casterPath = frameAllocatedPath; |
| 518 | } |
| 519 | |
| 520 | ShadowOp* shadowOp = new (mAllocator) ShadowOp(casterNodeOp, casterAlpha, casterPath, |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame^] | 521 | mCanvasState.getLocalClipBounds(), |
| 522 | mCanvasState.currentSnapshot()->getRelativeLightCenter()); |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 523 | BakedOpState* bakedOpState = BakedOpState::tryShadowOpConstruct( |
| 524 | mAllocator, *mCanvasState.currentSnapshot(), shadowOp); |
| 525 | if (CC_LIKELY(bakedOpState)) { |
| 526 | currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Shadow); |
| 527 | } |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 528 | } |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 529 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 530 | /** |
| 531 | * Used to define a list of lambdas referencing private OpReorderer::onXXXXOp() methods. |
| 532 | * |
| 533 | * This allows opIds embedded in the RecordedOps to be used for dispatching to these lambdas. E.g. a |
| 534 | * BitmapOp op then would be dispatched to OpReorderer::onBitmapOp(const BitmapOp&) |
| 535 | */ |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 536 | #define OP_RECEIVER(Type) \ |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 537 | [](OpReorderer& reorderer, const RecordedOp& op) { reorderer.on##Type(static_cast<const Type&>(op)); }, |
Chris Craik | b36af87 | 2015-10-16 14:23:12 -0700 | [diff] [blame] | 538 | void OpReorderer::deferImpl(const DisplayList& displayList) { |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 539 | static std::function<void(OpReorderer& reorderer, const RecordedOp&)> receivers[] = { |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 540 | MAP_OPS(OP_RECEIVER) |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 541 | }; |
Chris Craik | b36af87 | 2015-10-16 14:23:12 -0700 | [diff] [blame] | 542 | for (const DisplayList::Chunk& chunk : displayList.getChunks()) { |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 543 | FatVector<ZRenderNodeOpPair, 16> zTranslatedNodes; |
| 544 | buildZSortedChildList(&zTranslatedNodes, displayList, chunk); |
| 545 | |
| 546 | defer3dChildren(ChildrenSelectMode::Negative, zTranslatedNodes); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 547 | for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) { |
Chris Craik | b36af87 | 2015-10-16 14:23:12 -0700 | [diff] [blame] | 548 | const RecordedOp* op = displayList.getOps()[opIndex]; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 549 | receivers[op->opId](*this, *op); |
| 550 | } |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 551 | defer3dChildren(ChildrenSelectMode::Positive, zTranslatedNodes); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 555 | void OpReorderer::deferRenderNodeOp(const RenderNodeOp& op) { |
| 556 | if (op.renderNode->nothingToDraw()) return; |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 557 | int count = mCanvasState.save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 558 | |
| 559 | // apply state from RecordedOp |
| 560 | mCanvasState.concatMatrix(op.localMatrix); |
| 561 | mCanvasState.clipRect(op.localClipRect.left, op.localClipRect.top, |
| 562 | op.localClipRect.right, op.localClipRect.bottom, SkRegion::kIntersect_Op); |
| 563 | |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 564 | // then apply state from node properties, and defer ops |
| 565 | deferNodePropsAndOps(*op.renderNode); |
| 566 | |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 567 | mCanvasState.restoreToCount(count); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 570 | void OpReorderer::onRenderNodeOp(const RenderNodeOp& op) { |
| 571 | if (!op.skipInOrderDraw) { |
| 572 | deferRenderNodeOp(op); |
| 573 | } |
| 574 | } |
| 575 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 576 | static batchid_t tessellatedBatchId(const SkPaint& paint) { |
| 577 | return paint.getPathEffect() |
| 578 | ? OpBatchType::AlphaMaskTexture |
| 579 | : (paint.isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices); |
| 580 | } |
| 581 | |
| 582 | void OpReorderer::onBitmapOp(const BitmapOp& op) { |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 583 | BakedOpState* bakedStateOp = tryBakeOpState(op); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 584 | if (!bakedStateOp) return; // quick rejected |
| 585 | |
| 586 | mergeid_t mergeId = (mergeid_t) op.bitmap->getGenerationID(); |
| 587 | // TODO: AssetAtlas |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 588 | currentLayer().deferMergeableOp(mAllocator, bakedStateOp, OpBatchType::Bitmap, mergeId); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | void OpReorderer::onRectOp(const RectOp& op) { |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 592 | BakedOpState* bakedStateOp = tryBakeOpState(op); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 593 | if (!bakedStateOp) return; // quick rejected |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 594 | currentLayer().deferUnmergeableOp(mAllocator, bakedStateOp, tessellatedBatchId(*op.paint)); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | void OpReorderer::onSimpleRectsOp(const SimpleRectsOp& op) { |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 598 | BakedOpState* bakedStateOp = tryBakeOpState(op); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 599 | if (!bakedStateOp) return; // quick rejected |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 600 | currentLayer().deferUnmergeableOp(mAllocator, bakedStateOp, OpBatchType::Vertices); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame^] | 603 | void OpReorderer::saveForLayer(uint32_t layerWidth, uint32_t layerHeight, const Rect& repaintRect, |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 604 | const BeginLayerOp* beginLayerOp, RenderNode* renderNode) { |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 605 | |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame^] | 606 | auto previous = mCanvasState.currentSnapshot(); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 607 | mCanvasState.save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag); |
| 608 | mCanvasState.writableSnapshot()->transform->loadIdentity(); |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 609 | mCanvasState.writableSnapshot()->initializeViewport(layerWidth, layerHeight); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 610 | mCanvasState.writableSnapshot()->roundRectClipState = nullptr; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 611 | |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame^] | 612 | Vector3 lightCenter = previous->getRelativeLightCenter(); |
| 613 | if (renderNode) { |
| 614 | Matrix4& inverse = renderNode->getLayer()->inverseTransformInWindow; |
| 615 | inverse.mapPoint3d(lightCenter); |
| 616 | } else { |
| 617 | // Combine all transforms used to present saveLayer content: |
| 618 | // parent content transform * canvas transform * bounds offset |
| 619 | Matrix4 contentTransform(*previous->transform); |
| 620 | contentTransform.multiply(beginLayerOp->localMatrix); |
| 621 | contentTransform.translate(beginLayerOp->unmappedBounds.left, beginLayerOp->unmappedBounds.top); |
| 622 | |
| 623 | // inverse the total transform, to map light center into layer-relative space |
| 624 | Matrix4 inverse; |
| 625 | inverse.loadInverse(contentTransform); |
| 626 | inverse.mapPoint3d(lightCenter); |
| 627 | } |
| 628 | mCanvasState.writableSnapshot()->setRelativeLightCenter(lightCenter); |
| 629 | |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 630 | // create a new layer, and push its index on the stack |
| 631 | mLayerStack.push_back(mLayerReorderers.size()); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame^] | 632 | mLayerReorderers.emplace_back(layerWidth, layerHeight, repaintRect, beginLayerOp, renderNode); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | void OpReorderer::restoreForLayer() { |
| 636 | // restore canvas, and pop finished layer off of the stack |
| 637 | mCanvasState.restore(); |
| 638 | mLayerStack.pop_back(); |
| 639 | } |
| 640 | |
| 641 | // TODO: test rejection at defer time, where the bounds become empty |
| 642 | void OpReorderer::onBeginLayerOp(const BeginLayerOp& op) { |
| 643 | const uint32_t layerWidth = (uint32_t) op.unmappedBounds.getWidth(); |
| 644 | const uint32_t layerHeight = (uint32_t) op.unmappedBounds.getHeight(); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame^] | 645 | saveForLayer(layerWidth, layerHeight, Rect(layerWidth, layerHeight), &op, nullptr); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 646 | } |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 647 | |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 648 | void OpReorderer::onEndLayerOp(const EndLayerOp& /* ignored */) { |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 649 | const BeginLayerOp& beginLayerOp = *currentLayer().beginLayerOp; |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 650 | int finishedLayerIndex = mLayerStack.back(); |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 651 | |
| 652 | restoreForLayer(); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 653 | |
| 654 | // record the draw operation into the previous layer's list of draw commands |
| 655 | // uses state from the associated beginLayerOp, since it has all the state needed for drawing |
| 656 | LayerOp* drawLayerOp = new (mAllocator) LayerOp( |
| 657 | beginLayerOp.unmappedBounds, |
| 658 | beginLayerOp.localMatrix, |
| 659 | beginLayerOp.localClipRect, |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 660 | beginLayerOp.paint, |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame] | 661 | &mLayerReorderers[finishedLayerIndex].offscreenBuffer); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 662 | BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp); |
| 663 | |
| 664 | if (bakedOpState) { |
| 665 | // Layer will be drawn into parent layer (which is now current, since we popped mLayerStack) |
| 666 | currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap); |
| 667 | } else { |
| 668 | // Layer won't be drawn - delete its drawing batches to prevent it from doing any work |
| 669 | mLayerReorderers[finishedLayerIndex].clear(); |
| 670 | return; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 674 | void OpReorderer::onLayerOp(const LayerOp& op) { |
| 675 | LOG_ALWAYS_FATAL("unsupported"); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Chris Craik | d3daa31 | 2015-11-06 10:59:56 -0800 | [diff] [blame] | 678 | void OpReorderer::onShadowOp(const ShadowOp& op) { |
| 679 | LOG_ALWAYS_FATAL("unsupported"); |
| 680 | } |
| 681 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 682 | } // namespace uirenderer |
| 683 | } // namespace android |