blob: 1d8be2b47d438fac7672267e0f60ae29182f5406 [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
215void OpReorderer::defer(int viewportWidth, int viewportHeight,
216 const std::vector< sp<RenderNode> >& nodes) {
217 mCanvasState.initializeSaveStack(viewportWidth, viewportHeight,
218 0, 0, viewportWidth, viewportHeight, Vector3());
219 for (const sp<RenderNode>& node : nodes) {
220 if (node->nothingToDraw()) continue;
221
222 // TODO: dedupe this code with onRenderNode()
223 mCanvasState.save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
224 if (node->applyViewProperties(mCanvasState)) {
225 // not rejected do ops...
226 const DisplayListData& data = node->getDisplayListData();
227 deferImpl(data.getChunks(), data.getOps());
228 }
229 mCanvasState.restore();
230 }
231}
232
233void OpReorderer::defer(int viewportWidth, int viewportHeight,
234 const std::vector<DisplayListData::Chunk>& chunks, const std::vector<RecordedOp*>& ops) {
235 ATRACE_NAME("prepare drawing commands");
236 mCanvasState.initializeSaveStack(viewportWidth, viewportHeight,
237 0, 0, viewportWidth, viewportHeight, Vector3());
238 deferImpl(chunks, ops);
239}
240
241/**
242 * Used to define a list of lambdas referencing private OpReorderer::onXXXXOp() methods.
243 *
244 * This allows opIds embedded in the RecordedOps to be used for dispatching to these lambdas. E.g. a
245 * BitmapOp op then would be dispatched to OpReorderer::onBitmapOp(const BitmapOp&)
246 */
247#define OP_RECIEVER(Type) \
248 [](OpReorderer& reorderer, const RecordedOp& op) { reorderer.on##Type(static_cast<const Type&>(op)); },
249void OpReorderer::deferImpl(const std::vector<DisplayListData::Chunk>& chunks,
250 const std::vector<RecordedOp*>& ops) {
251 static std::function<void(OpReorderer& reorderer, const RecordedOp&)> receivers[] = {
252 MAP_OPS(OP_RECIEVER)
253 };
254 for (const DisplayListData::Chunk& chunk : chunks) {
255 for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
256 const RecordedOp* op = ops[opIndex];
257 receivers[op->opId](*this, *op);
258 }
259 }
260}
261
262void OpReorderer::replayBakedOpsImpl(void* arg, BakedOpReceiver* receivers) {
263 ATRACE_NAME("flush drawing commands");
264 for (const BatchBase* batch : mBatches) {
265 // TODO: different behavior based on batch->isMerging()
266 for (const BakedOpState* op : batch->getOps()) {
267 receivers[op->op->opId](arg, *op->op, *op);
268 }
269 }
270}
271
272BakedOpState* OpReorderer::bakeOpState(const RecordedOp& recordedOp) {
273 return BakedOpState::tryConstruct(mAllocator, *mCanvasState.currentSnapshot(), recordedOp);
274}
275
276void OpReorderer::onRenderNodeOp(const RenderNodeOp& op) {
277 if (op.renderNode->nothingToDraw()) {
278 return;
279 }
280 mCanvasState.save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
281
282 // apply state from RecordedOp
283 mCanvasState.concatMatrix(op.localMatrix);
284 mCanvasState.clipRect(op.localClipRect.left, op.localClipRect.top,
285 op.localClipRect.right, op.localClipRect.bottom, SkRegion::kIntersect_Op);
286
287 // apply RenderProperties state
288 if (op.renderNode->applyViewProperties(mCanvasState)) {
289 // not rejected do ops...
290 const DisplayListData& data = op.renderNode->getDisplayListData();
291 deferImpl(data.getChunks(), data.getOps());
292 }
293 mCanvasState.restore();
294}
295
296static batchid_t tessellatedBatchId(const SkPaint& paint) {
297 return paint.getPathEffect()
298 ? OpBatchType::AlphaMaskTexture
299 : (paint.isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices);
300}
301
302void OpReorderer::onBitmapOp(const BitmapOp& op) {
303 BakedOpState* bakedStateOp = bakeOpState(op);
304 if (!bakedStateOp) return; // quick rejected
305
306 mergeid_t mergeId = (mergeid_t) op.bitmap->getGenerationID();
307 // TODO: AssetAtlas
308
309 deferMergeableOp(bakedStateOp, OpBatchType::Bitmap, mergeId);
310}
311
312void OpReorderer::onRectOp(const RectOp& op) {
313 BakedOpState* bakedStateOp = bakeOpState(op);
314 if (!bakedStateOp) return; // quick rejected
315 deferUnmergeableOp(bakedStateOp, tessellatedBatchId(*op.paint));
316}
317
318void OpReorderer::onSimpleRectsOp(const SimpleRectsOp& op) {
319 BakedOpState* bakedStateOp = bakeOpState(op);
320 if (!bakedStateOp) return; // quick rejected
321 deferUnmergeableOp(bakedStateOp, OpBatchType::Vertices);
322}
323
324// iterate back toward target to see if anything drawn since should overlap the new op
325// if no target, merging ops still interate to find similar batch to insert after
326void OpReorderer::locateInsertIndex(int batchId, const Rect& clippedBounds,
327 BatchBase** targetBatch, size_t* insertBatchIndex) const {
328 for (size_t i = mBatches.size() - 1; i >= mEarliestBatchIndex; i--) {
329 BatchBase* overBatch = mBatches[i];
330
331 if (overBatch == *targetBatch) break;
332
333 // TODO: also consider shader shared between batch types
334 if (batchId == overBatch->getBatchId()) {
335 *insertBatchIndex = i + 1;
336 if (!*targetBatch) break; // found insert position, quit
337 }
338
339 if (overBatch->intersects(clippedBounds)) {
340 // NOTE: it may be possible to optimize for special cases where two operations
341 // of the same batch/paint could swap order, such as with a non-mergeable
342 // (clipped) and a mergeable text operation
343 *targetBatch = nullptr;
344 break;
345 }
346 }
347}
348
349void OpReorderer::deferUnmergeableOp(BakedOpState* op, batchid_t batchId) {
350 OpBatch* targetBatch = mBatchLookup[batchId];
351
352 size_t insertBatchIndex = mBatches.size();
353 if (targetBatch) {
354 locateInsertIndex(batchId, op->computedState.clippedBounds,
355 (BatchBase**)(&targetBatch), &insertBatchIndex);
356 }
357
358 if (targetBatch) {
359 targetBatch->batchOp(op);
360 } else {
361 // new non-merging batch
362 targetBatch = new (mAllocator) OpBatch(batchId, op);
363 mBatchLookup[batchId] = targetBatch;
364 mBatches.insert(mBatches.begin() + insertBatchIndex, targetBatch);
365 }
366}
367
368// insertion point of a new batch, will hopefully be immediately after similar batch
369// (generally, should be similar shader)
370void OpReorderer::deferMergeableOp(BakedOpState* op, batchid_t batchId, mergeid_t mergeId) {
371 MergingOpBatch* targetBatch = nullptr;
372
373 // Try to merge with any existing batch with same mergeId
374 auto getResult = mMergingBatches[batchId].find(mergeId);
375 if (getResult != mMergingBatches[batchId].end()) {
376 targetBatch = getResult->second;
377 if (!targetBatch->canMergeWith(op)) {
378 targetBatch = nullptr;
379 }
380 }
381
382 size_t insertBatchIndex = mBatches.size();
383 locateInsertIndex(batchId, op->computedState.clippedBounds,
384 (BatchBase**)(&targetBatch), &insertBatchIndex);
385
386 if (targetBatch) {
387 targetBatch->mergeOp(op);
388 } else {
389 // new merging batch
390 targetBatch = new (mAllocator) MergingOpBatch(batchId, op);
391 mMergingBatches[batchId].insert(std::make_pair(mergeId, targetBatch));
392
393 mBatches.insert(mBatches.begin() + insertBatchIndex, targetBatch);
394 }
395}
396
397void OpReorderer::dump() {
398 for (const BatchBase* batch : mBatches) {
399 batch->dump();
400 }
401}
402
403} // namespace uirenderer
404} // namespace android