blob: 2e02306dcc39a1798a7ff5422635c92f485b9ca1 [file] [log] [blame]
Chris Craikb565df12015-10-05 13:00:52 -07001/*
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
19#include "utils/PaintUtils.h"
20#include "RenderNode.h"
21
22#include "SkCanvas.h"
23#include "utils/Trace.h"
24
25namespace android {
26namespace uirenderer {
27
28class BatchBase {
29
30public:
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, merging %d, bounds " RECT_STRING, this, mMerging, RECT_ARGS(mBounds));
56 }
57protected:
58 batchid_t mBatchId;
59 Rect mBounds;
60 std::vector<BakedOpState*> mOps;
61 bool mMerging;
62};
63
64class OpBatch : public BatchBase {
65public:
66 static void* operator new(size_t size, LinearAllocator& allocator) {
67 return allocator.alloc(size);
68 }
69
70 OpBatch(batchid_t batchId, BakedOpState* op)
71 : BatchBase(batchId, op, false) {
72 }
73
74 void batchOp(BakedOpState* op) {
75 mBounds.unionWith(op->computedState.clippedBounds);
76 mOps.push_back(op);
77 }
78};
79
80class MergingOpBatch : public BatchBase {
81public:
82 static void* operator new(size_t size, LinearAllocator& allocator) {
83 return allocator.alloc(size);
84 }
85
86 MergingOpBatch(batchid_t batchId, BakedOpState* op)
87 : BatchBase(batchId, op, true) {
88 }
89
90 /*
91 * Helper for determining if a new op can merge with a MergingDrawBatch based on their bounds
92 * and clip side flags. Positive bounds delta means new bounds fit in old.
93 */
94 static inline bool checkSide(const int currentFlags, const int newFlags, const int side,
95 float boundsDelta) {
96 bool currentClipExists = currentFlags & side;
97 bool newClipExists = newFlags & side;
98
99 // if current is clipped, we must be able to fit new bounds in current
100 if (boundsDelta > 0 && currentClipExists) return false;
101
102 // if new is clipped, we must be able to fit current bounds in new
103 if (boundsDelta < 0 && newClipExists) return false;
104
105 return true;
106 }
107
108 static bool paintIsDefault(const SkPaint& paint) {
109 return paint.getAlpha() == 255
110 && paint.getColorFilter() == nullptr
111 && paint.getShader() == nullptr;
112 }
113
114 static bool paintsAreEquivalent(const SkPaint& a, const SkPaint& b) {
115 return a.getAlpha() == b.getAlpha()
116 && a.getColorFilter() == b.getColorFilter()
117 && a.getShader() == b.getShader();
118 }
119
120 /*
121 * Checks if a (mergeable) op can be merged into this batch
122 *
123 * If true, the op's multiDraw must be guaranteed to handle both ops simultaneously, so it is
124 * important to consider all paint attributes used in the draw calls in deciding both a) if an
125 * op tries to merge at all, and b) if the op can merge with another set of ops
126 *
127 * False positives can lead to information from the paints of subsequent merged operations being
128 * dropped, so we make simplifying qualifications on the ops that can merge, per op type.
129 */
130 bool canMergeWith(BakedOpState* op) const {
131 bool isTextBatch = getBatchId() == OpBatchType::Text
132 || getBatchId() == OpBatchType::ColorText;
133
134 // Overlapping other operations is only allowed for text without shadow. For other ops,
135 // multiDraw isn't guaranteed to overdraw correctly
136 if (!isTextBatch || PaintUtils::hasTextShadow(op->op->paint)) {
137 if (intersects(op->computedState.clippedBounds)) return false;
138 }
139
140 const BakedOpState* lhs = op;
141 const BakedOpState* rhs = mOps[0];
142
143 if (!MathUtils::areEqual(lhs->alpha, rhs->alpha)) return false;
144
145 // Identical round rect clip state means both ops will clip in the same way, or not at all.
146 // As the state objects are const, we can compare their pointers to determine mergeability
147 if (lhs->roundRectClipState != rhs->roundRectClipState) return false;
148 if (lhs->projectionPathMask != rhs->projectionPathMask) return false;
149
150 /* Clipping compatibility check
151 *
152 * Exploits the fact that if a op or batch is clipped on a side, its bounds will equal its
153 * clip for that side.
154 */
155 const int currentFlags = mClipSideFlags;
156 const int newFlags = op->computedState.clipSideFlags;
157 if (currentFlags != OpClipSideFlags::None || newFlags != OpClipSideFlags::None) {
158 const Rect& opBounds = op->computedState.clippedBounds;
159 float boundsDelta = mBounds.left - opBounds.left;
160 if (!checkSide(currentFlags, newFlags, OpClipSideFlags::Left, boundsDelta)) return false;
161 boundsDelta = mBounds.top - opBounds.top;
162 if (!checkSide(currentFlags, newFlags, OpClipSideFlags::Top, boundsDelta)) return false;
163
164 // right and bottom delta calculation reversed to account for direction
165 boundsDelta = opBounds.right - mBounds.right;
166 if (!checkSide(currentFlags, newFlags, OpClipSideFlags::Right, boundsDelta)) return false;
167 boundsDelta = opBounds.bottom - mBounds.bottom;
168 if (!checkSide(currentFlags, newFlags, OpClipSideFlags::Bottom, boundsDelta)) return false;
169 }
170
171 const SkPaint* newPaint = op->op->paint;
172 const SkPaint* oldPaint = mOps[0]->op->paint;
173
174 if (newPaint == oldPaint) {
175 // if paints are equal, then modifiers + paint attribs don't need to be compared
176 return true;
177 } else if (newPaint && !oldPaint) {
178 return paintIsDefault(*newPaint);
179 } else if (!newPaint && oldPaint) {
180 return paintIsDefault(*oldPaint);
181 }
182 return paintsAreEquivalent(*newPaint, *oldPaint);
183 }
184
185 void mergeOp(BakedOpState* op) {
186 mBounds.unionWith(op->computedState.clippedBounds);
187 mOps.push_back(op);
188
189 const int newClipSideFlags = op->computedState.clipSideFlags;
190 mClipSideFlags |= newClipSideFlags;
191
192 const Rect& opClip = op->computedState.clipRect;
193 if (newClipSideFlags & OpClipSideFlags::Left) mClipRect.left = opClip.left;
194 if (newClipSideFlags & OpClipSideFlags::Top) mClipRect.top = opClip.top;
195 if (newClipSideFlags & OpClipSideFlags::Right) mClipRect.right = opClip.right;
196 if (newClipSideFlags & OpClipSideFlags::Bottom) mClipRect.bottom = opClip.bottom;
197 }
198
199private:
200 int mClipSideFlags = 0;
201 Rect mClipRect;
202};
203
204class NullClient: public CanvasStateClient {
205 void onViewportInitialized() override {}
206 void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {}
207 GLuint getTargetFbo() const override { return 0; }
208};
209static NullClient sNullClient;
210
211OpReorderer::OpReorderer()
212 : mCanvasState(sNullClient) {
213}
214
Chris Craikddf22152015-10-14 17:42:47 -0700215void OpReorderer::defer(const SkRect& clip, int viewportWidth, int viewportHeight,
Chris Craikb565df12015-10-05 13:00:52 -0700216 const std::vector< sp<RenderNode> >& nodes) {
217 mCanvasState.initializeSaveStack(viewportWidth, viewportHeight,
Chris Craikddf22152015-10-14 17:42:47 -0700218 clip.fLeft, clip.fTop, clip.fRight, clip.fBottom,
219 Vector3());
Chris Craikb565df12015-10-05 13:00:52 -0700220 for (const sp<RenderNode>& node : nodes) {
221 if (node->nothingToDraw()) continue;
222
223 // TODO: dedupe this code with onRenderNode()
224 mCanvasState.save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
225 if (node->applyViewProperties(mCanvasState)) {
226 // not rejected do ops...
227 const DisplayListData& data = node->getDisplayListData();
228 deferImpl(data.getChunks(), data.getOps());
229 }
230 mCanvasState.restore();
231 }
232}
233
234void OpReorderer::defer(int viewportWidth, int viewportHeight,
235 const std::vector<DisplayListData::Chunk>& chunks, const std::vector<RecordedOp*>& ops) {
236 ATRACE_NAME("prepare drawing commands");
237 mCanvasState.initializeSaveStack(viewportWidth, viewportHeight,
238 0, 0, viewportWidth, viewportHeight, Vector3());
239 deferImpl(chunks, ops);
240}
241
242/**
243 * Used to define a list of lambdas referencing private OpReorderer::onXXXXOp() methods.
244 *
245 * This allows opIds embedded in the RecordedOps to be used for dispatching to these lambdas. E.g. a
246 * BitmapOp op then would be dispatched to OpReorderer::onBitmapOp(const BitmapOp&)
247 */
248#define OP_RECIEVER(Type) \
249 [](OpReorderer& reorderer, const RecordedOp& op) { reorderer.on##Type(static_cast<const Type&>(op)); },
250void OpReorderer::deferImpl(const std::vector<DisplayListData::Chunk>& chunks,
251 const std::vector<RecordedOp*>& ops) {
252 static std::function<void(OpReorderer& reorderer, const RecordedOp&)> receivers[] = {
253 MAP_OPS(OP_RECIEVER)
254 };
255 for (const DisplayListData::Chunk& chunk : chunks) {
256 for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
257 const RecordedOp* op = ops[opIndex];
258 receivers[op->opId](*this, *op);
259 }
260 }
261}
262
263void OpReorderer::replayBakedOpsImpl(void* arg, BakedOpReceiver* receivers) {
264 ATRACE_NAME("flush drawing commands");
265 for (const BatchBase* batch : mBatches) {
266 // TODO: different behavior based on batch->isMerging()
267 for (const BakedOpState* op : batch->getOps()) {
268 receivers[op->op->opId](arg, *op->op, *op);
269 }
270 }
271}
272
273BakedOpState* OpReorderer::bakeOpState(const RecordedOp& recordedOp) {
274 return BakedOpState::tryConstruct(mAllocator, *mCanvasState.currentSnapshot(), recordedOp);
275}
276
277void OpReorderer::onRenderNodeOp(const RenderNodeOp& op) {
278 if (op.renderNode->nothingToDraw()) {
279 return;
280 }
281 mCanvasState.save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
282
283 // apply state from RecordedOp
284 mCanvasState.concatMatrix(op.localMatrix);
285 mCanvasState.clipRect(op.localClipRect.left, op.localClipRect.top,
286 op.localClipRect.right, op.localClipRect.bottom, SkRegion::kIntersect_Op);
287
288 // apply RenderProperties state
289 if (op.renderNode->applyViewProperties(mCanvasState)) {
290 // not rejected do ops...
291 const DisplayListData& data = op.renderNode->getDisplayListData();
292 deferImpl(data.getChunks(), data.getOps());
293 }
294 mCanvasState.restore();
295}
296
297static batchid_t tessellatedBatchId(const SkPaint& paint) {
298 return paint.getPathEffect()
299 ? OpBatchType::AlphaMaskTexture
300 : (paint.isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices);
301}
302
303void OpReorderer::onBitmapOp(const BitmapOp& op) {
304 BakedOpState* bakedStateOp = bakeOpState(op);
305 if (!bakedStateOp) return; // quick rejected
306
307 mergeid_t mergeId = (mergeid_t) op.bitmap->getGenerationID();
308 // TODO: AssetAtlas
309
310 deferMergeableOp(bakedStateOp, OpBatchType::Bitmap, mergeId);
311}
312
313void OpReorderer::onRectOp(const RectOp& op) {
314 BakedOpState* bakedStateOp = bakeOpState(op);
315 if (!bakedStateOp) return; // quick rejected
316 deferUnmergeableOp(bakedStateOp, tessellatedBatchId(*op.paint));
317}
318
319void OpReorderer::onSimpleRectsOp(const SimpleRectsOp& op) {
320 BakedOpState* bakedStateOp = bakeOpState(op);
321 if (!bakedStateOp) return; // quick rejected
322 deferUnmergeableOp(bakedStateOp, OpBatchType::Vertices);
323}
324
325// iterate back toward target to see if anything drawn since should overlap the new op
326// if no target, merging ops still interate to find similar batch to insert after
327void OpReorderer::locateInsertIndex(int batchId, const Rect& clippedBounds,
328 BatchBase** targetBatch, size_t* insertBatchIndex) const {
Chris Craikddf22152015-10-14 17:42:47 -0700329 for (int i = mBatches.size() - 1; i >= mEarliestBatchIndex; i--) {
Chris Craikb565df12015-10-05 13:00:52 -0700330 BatchBase* overBatch = mBatches[i];
331
332 if (overBatch == *targetBatch) break;
333
334 // TODO: also consider shader shared between batch types
335 if (batchId == overBatch->getBatchId()) {
336 *insertBatchIndex = i + 1;
337 if (!*targetBatch) break; // found insert position, quit
338 }
339
340 if (overBatch->intersects(clippedBounds)) {
341 // NOTE: it may be possible to optimize for special cases where two operations
342 // of the same batch/paint could swap order, such as with a non-mergeable
343 // (clipped) and a mergeable text operation
344 *targetBatch = nullptr;
345 break;
346 }
347 }
348}
349
350void OpReorderer::deferUnmergeableOp(BakedOpState* op, batchid_t batchId) {
351 OpBatch* targetBatch = mBatchLookup[batchId];
352
353 size_t insertBatchIndex = mBatches.size();
354 if (targetBatch) {
355 locateInsertIndex(batchId, op->computedState.clippedBounds,
356 (BatchBase**)(&targetBatch), &insertBatchIndex);
357 }
358
359 if (targetBatch) {
360 targetBatch->batchOp(op);
361 } else {
362 // new non-merging batch
363 targetBatch = new (mAllocator) OpBatch(batchId, op);
364 mBatchLookup[batchId] = targetBatch;
365 mBatches.insert(mBatches.begin() + insertBatchIndex, targetBatch);
366 }
367}
368
369// insertion point of a new batch, will hopefully be immediately after similar batch
370// (generally, should be similar shader)
371void OpReorderer::deferMergeableOp(BakedOpState* op, batchid_t batchId, mergeid_t mergeId) {
372 MergingOpBatch* targetBatch = nullptr;
373
374 // Try to merge with any existing batch with same mergeId
375 auto getResult = mMergingBatches[batchId].find(mergeId);
376 if (getResult != mMergingBatches[batchId].end()) {
377 targetBatch = getResult->second;
378 if (!targetBatch->canMergeWith(op)) {
379 targetBatch = nullptr;
380 }
381 }
382
383 size_t insertBatchIndex = mBatches.size();
384 locateInsertIndex(batchId, op->computedState.clippedBounds,
385 (BatchBase**)(&targetBatch), &insertBatchIndex);
386
387 if (targetBatch) {
388 targetBatch->mergeOp(op);
389 } else {
390 // new merging batch
391 targetBatch = new (mAllocator) MergingOpBatch(batchId, op);
392 mMergingBatches[batchId].insert(std::make_pair(mergeId, targetBatch));
393
394 mBatches.insert(mBatches.begin() + insertBatchIndex, targetBatch);
395 }
396}
397
398void OpReorderer::dump() {
399 for (const BatchBase* batch : mBatches) {
400 batch->dump();
401 }
402}
403
404} // namespace uirenderer
405} // namespace android