Chris Craik | 5ea1724 | 2016-01-11 14:07:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 17 | #include "LayerBuilder.h" |
Chris Craik | 5ea1724 | 2016-01-11 14:07:59 -0800 | [diff] [blame] | 18 | |
| 19 | #include "BakedOpState.h" |
| 20 | #include "RenderNode.h" |
| 21 | #include "utils/PaintUtils.h" |
| 22 | #include "utils/TraceUtils.h" |
| 23 | |
| 24 | #include <utils/TypeHelpers.h> |
| 25 | |
| 26 | namespace android { |
| 27 | namespace uirenderer { |
| 28 | |
| 29 | class BatchBase { |
| 30 | public: |
| 31 | BatchBase(batchid_t batchId, BakedOpState* op, bool merging) |
| 32 | : mBatchId(batchId) |
| 33 | , mMerging(merging) { |
| 34 | mBounds = op->computedState.clippedBounds; |
| 35 | mOps.push_back(op); |
| 36 | } |
| 37 | |
| 38 | bool intersects(const Rect& rect) const { |
| 39 | if (!rect.intersects(mBounds)) return false; |
| 40 | |
| 41 | for (const BakedOpState* op : mOps) { |
| 42 | if (rect.intersects(op->computedState.clippedBounds)) { |
| 43 | return true; |
| 44 | } |
| 45 | } |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | batchid_t getBatchId() const { return mBatchId; } |
| 50 | bool isMerging() const { return mMerging; } |
| 51 | |
| 52 | const std::vector<BakedOpState*>& getOps() const { return mOps; } |
| 53 | |
| 54 | void dump() const { |
| 55 | ALOGD(" Batch %p, id %d, merging %d, count %d, bounds " RECT_STRING, |
Chris Craik | b250a83 | 2016-01-11 19:28:17 -0800 | [diff] [blame] | 56 | this, mBatchId, mMerging, (int) mOps.size(), RECT_ARGS(mBounds)); |
Chris Craik | 5ea1724 | 2016-01-11 14:07:59 -0800 | [diff] [blame] | 57 | } |
| 58 | protected: |
| 59 | batchid_t mBatchId; |
| 60 | Rect mBounds; |
| 61 | std::vector<BakedOpState*> mOps; |
| 62 | bool mMerging; |
| 63 | }; |
| 64 | |
| 65 | class OpBatch : public BatchBase { |
| 66 | public: |
Chris Craik | 5ea1724 | 2016-01-11 14:07:59 -0800 | [diff] [blame] | 67 | OpBatch(batchid_t batchId, BakedOpState* op) |
| 68 | : BatchBase(batchId, op, false) { |
| 69 | } |
| 70 | |
| 71 | void batchOp(BakedOpState* op) { |
| 72 | mBounds.unionWith(op->computedState.clippedBounds); |
| 73 | mOps.push_back(op); |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | class MergingOpBatch : public BatchBase { |
| 78 | public: |
Chris Craik | 5ea1724 | 2016-01-11 14:07:59 -0800 | [diff] [blame] | 79 | MergingOpBatch(batchid_t batchId, BakedOpState* op) |
| 80 | : BatchBase(batchId, op, true) |
| 81 | , mClipSideFlags(op->computedState.clipSideFlags) { |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * Helper for determining if a new op can merge with a MergingDrawBatch based on their bounds |
| 86 | * and clip side flags. Positive bounds delta means new bounds fit in old. |
| 87 | */ |
| 88 | static inline bool checkSide(const int currentFlags, const int newFlags, const int side, |
| 89 | float boundsDelta) { |
| 90 | bool currentClipExists = currentFlags & side; |
| 91 | bool newClipExists = newFlags & side; |
| 92 | |
| 93 | // if current is clipped, we must be able to fit new bounds in current |
| 94 | if (boundsDelta > 0 && currentClipExists) return false; |
| 95 | |
| 96 | // if new is clipped, we must be able to fit current bounds in new |
| 97 | if (boundsDelta < 0 && newClipExists) return false; |
| 98 | |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | static bool paintIsDefault(const SkPaint& paint) { |
| 103 | return paint.getAlpha() == 255 |
| 104 | && paint.getColorFilter() == nullptr |
| 105 | && paint.getShader() == nullptr; |
| 106 | } |
| 107 | |
| 108 | static bool paintsAreEquivalent(const SkPaint& a, const SkPaint& b) { |
| 109 | // Note: don't check color, since all currently mergeable ops can merge across colors |
| 110 | return a.getAlpha() == b.getAlpha() |
| 111 | && a.getColorFilter() == b.getColorFilter() |
| 112 | && a.getShader() == b.getShader(); |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Checks if a (mergeable) op can be merged into this batch |
| 117 | * |
| 118 | * If true, the op's multiDraw must be guaranteed to handle both ops simultaneously, so it is |
| 119 | * important to consider all paint attributes used in the draw calls in deciding both a) if an |
| 120 | * op tries to merge at all, and b) if the op can merge with another set of ops |
| 121 | * |
| 122 | * False positives can lead to information from the paints of subsequent merged operations being |
| 123 | * dropped, so we make simplifying qualifications on the ops that can merge, per op type. |
| 124 | */ |
| 125 | bool canMergeWith(BakedOpState* op) const { |
| 126 | bool isTextBatch = getBatchId() == OpBatchType::Text |
| 127 | || getBatchId() == OpBatchType::ColorText; |
| 128 | |
| 129 | // Overlapping other operations is only allowed for text without shadow. For other ops, |
| 130 | // multiDraw isn't guaranteed to overdraw correctly |
| 131 | if (!isTextBatch || PaintUtils::hasTextShadow(op->op->paint)) { |
| 132 | if (intersects(op->computedState.clippedBounds)) return false; |
| 133 | } |
| 134 | |
| 135 | const BakedOpState* lhs = op; |
| 136 | const BakedOpState* rhs = mOps[0]; |
| 137 | |
| 138 | if (!MathUtils::areEqual(lhs->alpha, rhs->alpha)) return false; |
| 139 | |
| 140 | // Identical round rect clip state means both ops will clip in the same way, or not at all. |
| 141 | // As the state objects are const, we can compare their pointers to determine mergeability |
| 142 | if (lhs->roundRectClipState != rhs->roundRectClipState) return false; |
| 143 | if (lhs->projectionPathMask != rhs->projectionPathMask) return false; |
| 144 | |
| 145 | /* Clipping compatibility check |
| 146 | * |
| 147 | * Exploits the fact that if a op or batch is clipped on a side, its bounds will equal its |
| 148 | * clip for that side. |
| 149 | */ |
| 150 | const int currentFlags = mClipSideFlags; |
| 151 | const int newFlags = op->computedState.clipSideFlags; |
| 152 | if (currentFlags != OpClipSideFlags::None || newFlags != OpClipSideFlags::None) { |
| 153 | const Rect& opBounds = op->computedState.clippedBounds; |
| 154 | float boundsDelta = mBounds.left - opBounds.left; |
| 155 | if (!checkSide(currentFlags, newFlags, OpClipSideFlags::Left, boundsDelta)) return false; |
| 156 | boundsDelta = mBounds.top - opBounds.top; |
| 157 | if (!checkSide(currentFlags, newFlags, OpClipSideFlags::Top, boundsDelta)) return false; |
| 158 | |
| 159 | // right and bottom delta calculation reversed to account for direction |
| 160 | boundsDelta = opBounds.right - mBounds.right; |
| 161 | if (!checkSide(currentFlags, newFlags, OpClipSideFlags::Right, boundsDelta)) return false; |
| 162 | boundsDelta = opBounds.bottom - mBounds.bottom; |
| 163 | if (!checkSide(currentFlags, newFlags, OpClipSideFlags::Bottom, boundsDelta)) return false; |
| 164 | } |
| 165 | |
| 166 | const SkPaint* newPaint = op->op->paint; |
| 167 | const SkPaint* oldPaint = mOps[0]->op->paint; |
| 168 | |
| 169 | if (newPaint == oldPaint) { |
| 170 | // if paints are equal, then modifiers + paint attribs don't need to be compared |
| 171 | return true; |
| 172 | } else if (newPaint && !oldPaint) { |
| 173 | return paintIsDefault(*newPaint); |
| 174 | } else if (!newPaint && oldPaint) { |
| 175 | return paintIsDefault(*oldPaint); |
| 176 | } |
| 177 | return paintsAreEquivalent(*newPaint, *oldPaint); |
| 178 | } |
| 179 | |
| 180 | void mergeOp(BakedOpState* op) { |
| 181 | mBounds.unionWith(op->computedState.clippedBounds); |
| 182 | mOps.push_back(op); |
| 183 | |
| 184 | // Because a new op must have passed canMergeWith(), we know it's passed the clipping compat |
| 185 | // check, and doesn't extend past a side of the clip that's in use by the merged batch. |
| 186 | // Therefore it's safe to simply always merge flags, and use the bounds as the clip rect. |
| 187 | mClipSideFlags |= op->computedState.clipSideFlags; |
| 188 | } |
| 189 | |
| 190 | int getClipSideFlags() const { return mClipSideFlags; } |
| 191 | const Rect& getClipRect() const { return mBounds; } |
| 192 | |
| 193 | private: |
| 194 | int mClipSideFlags; |
| 195 | }; |
| 196 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 197 | LayerBuilder::LayerBuilder(uint32_t width, uint32_t height, |
Chris Craik | 5ea1724 | 2016-01-11 14:07:59 -0800 | [diff] [blame] | 198 | const Rect& repaintRect, const BeginLayerOp* beginLayerOp, RenderNode* renderNode) |
| 199 | : width(width) |
| 200 | , height(height) |
| 201 | , repaintRect(repaintRect) |
| 202 | , offscreenBuffer(renderNode ? renderNode->getLayer() : nullptr) |
| 203 | , beginLayerOp(beginLayerOp) |
| 204 | , renderNode(renderNode) |
| 205 | , viewportClip(Rect(width, height)) {} |
| 206 | |
| 207 | // iterate back toward target to see if anything drawn since should overlap the new op |
| 208 | // if no target, merging ops still iterate to find similar batch to insert after |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 209 | void LayerBuilder::locateInsertIndex(int batchId, const Rect& clippedBounds, |
Chris Craik | 5ea1724 | 2016-01-11 14:07:59 -0800 | [diff] [blame] | 210 | BatchBase** targetBatch, size_t* insertBatchIndex) const { |
| 211 | for (int i = mBatches.size() - 1; i >= 0; i--) { |
| 212 | BatchBase* overBatch = mBatches[i]; |
| 213 | |
| 214 | if (overBatch == *targetBatch) break; |
| 215 | |
| 216 | // TODO: also consider shader shared between batch types |
| 217 | if (batchId == overBatch->getBatchId()) { |
| 218 | *insertBatchIndex = i + 1; |
| 219 | if (!*targetBatch) break; // found insert position, quit |
| 220 | } |
| 221 | |
| 222 | if (overBatch->intersects(clippedBounds)) { |
| 223 | // NOTE: it may be possible to optimize for special cases where two operations |
| 224 | // of the same batch/paint could swap order, such as with a non-mergeable |
| 225 | // (clipped) and a mergeable text operation |
| 226 | *targetBatch = nullptr; |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 232 | void LayerBuilder::deferLayerClear(const Rect& rect) { |
Chris Craik | 5ea1724 | 2016-01-11 14:07:59 -0800 | [diff] [blame] | 233 | mClearRects.push_back(rect); |
| 234 | } |
| 235 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 236 | void LayerBuilder::flushLayerClears(LinearAllocator& allocator) { |
Chris Craik | 5ea1724 | 2016-01-11 14:07:59 -0800 | [diff] [blame] | 237 | if (CC_UNLIKELY(!mClearRects.empty())) { |
| 238 | const int vertCount = mClearRects.size() * 4; |
| 239 | // put the verts in the frame allocator, since |
| 240 | // 1) SimpleRectsOps needs verts, not rects |
| 241 | // 2) even if mClearRects stored verts, std::vectors will move their contents |
Chris Craik | 7a89600 | 2016-02-19 15:51:02 -0800 | [diff] [blame] | 242 | Vertex* const verts = (Vertex*) allocator.create_trivial_array<Vertex>(vertCount); |
Chris Craik | 5ea1724 | 2016-01-11 14:07:59 -0800 | [diff] [blame] | 243 | |
| 244 | Vertex* currentVert = verts; |
| 245 | Rect bounds = mClearRects[0]; |
| 246 | for (auto&& rect : mClearRects) { |
| 247 | bounds.unionWith(rect); |
| 248 | Vertex::set(currentVert++, rect.left, rect.top); |
| 249 | Vertex::set(currentVert++, rect.right, rect.top); |
| 250 | Vertex::set(currentVert++, rect.left, rect.bottom); |
| 251 | Vertex::set(currentVert++, rect.right, rect.bottom); |
| 252 | } |
| 253 | mClearRects.clear(); // discard rects before drawing so this method isn't reentrant |
| 254 | |
| 255 | // One or more unclipped saveLayers have been enqueued, with deferred clears. |
| 256 | // Flush all of these clears with a single draw |
| 257 | SkPaint* paint = allocator.create<SkPaint>(); |
| 258 | paint->setXfermodeMode(SkXfermode::kClear_Mode); |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 259 | SimpleRectsOp* op = allocator.create_trivial<SimpleRectsOp>(bounds, |
Chris Craik | 5ea1724 | 2016-01-11 14:07:59 -0800 | [diff] [blame] | 260 | Matrix4::identity(), nullptr, paint, |
| 261 | verts, vertCount); |
| 262 | BakedOpState* bakedState = BakedOpState::directConstruct(allocator, |
| 263 | &viewportClip, bounds, *op); |
Chris Craik | 5ea1724 | 2016-01-11 14:07:59 -0800 | [diff] [blame] | 264 | deferUnmergeableOp(allocator, bakedState, OpBatchType::Vertices); |
| 265 | } |
| 266 | } |
| 267 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 268 | void LayerBuilder::deferUnmergeableOp(LinearAllocator& allocator, |
Chris Craik | 5ea1724 | 2016-01-11 14:07:59 -0800 | [diff] [blame] | 269 | BakedOpState* op, batchid_t batchId) { |
| 270 | if (batchId != OpBatchType::CopyToLayer) { |
| 271 | // if first op after one or more unclipped saveLayers, flush the layer clears |
| 272 | flushLayerClears(allocator); |
| 273 | } |
| 274 | |
| 275 | OpBatch* targetBatch = mBatchLookup[batchId]; |
| 276 | |
| 277 | size_t insertBatchIndex = mBatches.size(); |
| 278 | if (targetBatch) { |
| 279 | locateInsertIndex(batchId, op->computedState.clippedBounds, |
| 280 | (BatchBase**)(&targetBatch), &insertBatchIndex); |
| 281 | } |
| 282 | |
| 283 | if (targetBatch) { |
| 284 | targetBatch->batchOp(op); |
| 285 | } else { |
| 286 | // new non-merging batch |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 287 | targetBatch = allocator.create<OpBatch>(batchId, op); |
Chris Craik | 5ea1724 | 2016-01-11 14:07:59 -0800 | [diff] [blame] | 288 | mBatchLookup[batchId] = targetBatch; |
| 289 | mBatches.insert(mBatches.begin() + insertBatchIndex, targetBatch); |
| 290 | } |
| 291 | } |
| 292 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 293 | void LayerBuilder::deferMergeableOp(LinearAllocator& allocator, |
Chris Craik | 5ea1724 | 2016-01-11 14:07:59 -0800 | [diff] [blame] | 294 | BakedOpState* op, batchid_t batchId, mergeid_t mergeId) { |
| 295 | if (batchId != OpBatchType::CopyToLayer) { |
| 296 | // if first op after one or more unclipped saveLayers, flush the layer clears |
| 297 | flushLayerClears(allocator); |
| 298 | } |
| 299 | MergingOpBatch* targetBatch = nullptr; |
| 300 | |
| 301 | // Try to merge with any existing batch with same mergeId |
| 302 | auto getResult = mMergingBatchLookup[batchId].find(mergeId); |
| 303 | if (getResult != mMergingBatchLookup[batchId].end()) { |
| 304 | targetBatch = getResult->second; |
| 305 | if (!targetBatch->canMergeWith(op)) { |
| 306 | targetBatch = nullptr; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | size_t insertBatchIndex = mBatches.size(); |
| 311 | locateInsertIndex(batchId, op->computedState.clippedBounds, |
| 312 | (BatchBase**)(&targetBatch), &insertBatchIndex); |
| 313 | |
| 314 | if (targetBatch) { |
| 315 | targetBatch->mergeOp(op); |
| 316 | } else { |
| 317 | // new merging batch |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 318 | targetBatch = allocator.create<MergingOpBatch>(batchId, op); |
Chris Craik | 5ea1724 | 2016-01-11 14:07:59 -0800 | [diff] [blame] | 319 | mMergingBatchLookup[batchId].insert(std::make_pair(mergeId, targetBatch)); |
| 320 | |
| 321 | mBatches.insert(mBatches.begin() + insertBatchIndex, targetBatch); |
| 322 | } |
| 323 | } |
| 324 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 325 | void LayerBuilder::replayBakedOpsImpl(void* arg, |
Chris Craik | 5ea1724 | 2016-01-11 14:07:59 -0800 | [diff] [blame] | 326 | BakedOpReceiver* unmergedReceivers, MergedOpReceiver* mergedReceivers) const { |
| 327 | ATRACE_NAME("flush drawing commands"); |
| 328 | for (const BatchBase* batch : mBatches) { |
| 329 | size_t size = batch->getOps().size(); |
| 330 | if (size > 1 && batch->isMerging()) { |
| 331 | int opId = batch->getOps()[0]->op->opId; |
| 332 | const MergingOpBatch* mergingBatch = static_cast<const MergingOpBatch*>(batch); |
| 333 | MergedBakedOpList data = { |
| 334 | batch->getOps().data(), |
| 335 | size, |
| 336 | mergingBatch->getClipSideFlags(), |
| 337 | mergingBatch->getClipRect() |
| 338 | }; |
| 339 | mergedReceivers[opId](arg, data); |
| 340 | } else { |
| 341 | for (const BakedOpState* op : batch->getOps()) { |
| 342 | unmergedReceivers[op->op->opId](arg, *op); |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
Chris Craik | f158b49 | 2016-01-12 14:45:08 -0800 | [diff] [blame] | 348 | void LayerBuilder::dump() const { |
| 349 | ALOGD("LayerBuilder %p, %ux%u buffer %p, blo %p, rn %p", |
Chris Craik | 5ea1724 | 2016-01-11 14:07:59 -0800 | [diff] [blame] | 350 | this, width, height, offscreenBuffer, beginLayerOp, renderNode); |
| 351 | for (const BatchBase* batch : mBatches) { |
| 352 | batch->dump(); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | } // namespace uirenderer |
| 357 | } // namespace android |