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