1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
|
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "OpenGLRenderer"
#define ATRACE_TAG ATRACE_TAG_VIEW
#include <SkCanvas.h>
#include <utils/Trace.h>
#include "Caches.h"
#include "Debug.h"
#include "DeferredDisplayList.h"
#include "DisplayListOp.h"
#include "OpenGLRenderer.h"
#if DEBUG_DEFER
#define DEFER_LOGD(...) ALOGD(__VA_ARGS__)
#else
#define DEFER_LOGD(...)
#endif
namespace android {
namespace uirenderer {
// Depth of the save stack at the beginning of batch playback at flush time
#define FLUSH_SAVE_STACK_DEPTH 2
#define DEBUG_COLOR_BARRIER 0x1f000000
#define DEBUG_COLOR_MERGEDBATCH 0x5f7f7fff
#define DEBUG_COLOR_MERGEDBATCH_SOLO 0x5f7fff7f
/////////////////////////////////////////////////////////////////////////////////
// Operation Batches
/////////////////////////////////////////////////////////////////////////////////
class Batch {
public:
virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) = 0;
virtual ~Batch() {}
};
class DrawBatch : public Batch {
public:
DrawBatch(int batchId, mergeid_t mergeId) : mBatchId(batchId), mMergeId(mergeId) {
mOps.clear();
}
virtual ~DrawBatch() { mOps.clear(); }
void add(DrawOp* op) {
// NOTE: ignore empty bounds special case, since we don't merge across those ops
mBounds.unionWith(op->state.mBounds);
mOps.add(op);
}
bool intersects(Rect& rect) {
if (!rect.intersects(mBounds)) return false;
for (unsigned int i = 0; i < mOps.size(); i++) {
if (rect.intersects(mOps[i]->state.mBounds)) {
#if DEBUG_DEFER
DEFER_LOGD("op intersects with op %p with bounds %f %f %f %f:", mOps[i],
mOps[i]->state.mBounds.left, mOps[i]->state.mBounds.top,
mOps[i]->state.mBounds.right, mOps[i]->state.mBounds.bottom);
mOps[i]->output(2);
#endif
return true;
}
}
return false;
}
virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
DEFER_LOGD("%d replaying DrawingBatch %p, with %d ops (batch id %x, merge id %p)",
index, this, mOps.size(), mOps[0]->getBatchId(), mOps[0]->getMergeId());
status_t status = DrawGlInfo::kStatusDone;
DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
for (unsigned int i = 0; i < mOps.size(); i++) {
DrawOp* op = mOps[i];
renderer.restoreDisplayState(op->state);
#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
renderer.eventMark(op->name());
#endif
logBuffer.writeCommand(0, op->name());
status |= op->applyDraw(renderer, dirty);
#if DEBUG_MERGE_BEHAVIOR
Rect& bounds = mOps[i]->state.mBounds;
int batchColor = 0x1f000000;
if (getBatchId() & 0x1) batchColor |= 0x0000ff;
if (getBatchId() & 0x2) batchColor |= 0x00ff00;
if (getBatchId() & 0x4) batchColor |= 0xff0000;
renderer.drawScreenSpaceColorRect(bounds.left, bounds.top, bounds.right, bounds.bottom,
batchColor);
#endif
}
return status;
}
inline int getBatchId() const { return mBatchId; }
inline mergeid_t getMergeId() const { return mMergeId; }
inline int count() const { return mOps.size(); }
protected:
Vector<DrawOp*> mOps;
Rect mBounds;
private:
int mBatchId;
mergeid_t mMergeId;
};
// compare alphas approximately, with a small margin
#define NEQ_FALPHA(lhs, rhs) \
fabs((float)lhs - (float)rhs) > 0.001f
class MergingDrawBatch : public DrawBatch {
public:
MergingDrawBatch(int batchId, mergeid_t mergeId) : DrawBatch(batchId, mergeId) {}
/*
* Checks if a (mergeable) op can be merged into this batch
*
* If true, the op's multiDraw must be guaranteed to handle both ops simultaneously, so it is
* important to consider all paint attributes used in the draw calls in deciding both a) if an
* op tries to merge at all, and b) if the op
*
* False positives can lead to information from the paints of subsequent merged operations being
* dropped, so we make simplifying qualifications on the ops that can merge, per op type.
*/
bool canMergeWith(DrawOp* op) {
if (!op->state.mMatrix.isPureTranslate()) return false;
bool isTextBatch = getBatchId() == DeferredDisplayList::kOpBatch_Text ||
getBatchId() == DeferredDisplayList::kOpBatch_ColorText;
// Overlapping other operations is only allowed for text without shadow. For other ops,
// multiDraw isn't guaranteed to overdraw correctly
if (!isTextBatch || op->state.mDrawModifiers.mHasShadow) {
if (intersects(op->state.mBounds)) return false;
}
const DeferredDisplayState& lhs = op->state;
const DeferredDisplayState& rhs = mOps[0]->state;
if (NEQ_FALPHA(lhs.mAlpha, rhs.mAlpha)) return false;
// if paints are equal, then modifiers + paint attribs don't need to be compared
if (op->mPaint == mOps[0]->mPaint) return true;
if (op->getPaintAlpha() != mOps[0]->getPaintAlpha()) return false;
/* Draw Modifiers compatibility check
*
* Shadows are ignored, as only text uses them, and in that case they are drawn
* per-DrawTextOp, before the unified text draw. Because of this, it's always safe to merge
* text UNLESS a later draw's shadow should overlays a previous draw's text. This is covered
* above with the intersection check.
*
* OverrideLayerAlpha is also ignored, as it's only used for drawing layers, which are never
* merged.
*
* These ignore cases prevent us from simply memcmp'ing the drawModifiers
*/
const DrawModifiers& lhsMod = lhs.mDrawModifiers;
const DrawModifiers& rhsMod = rhs.mDrawModifiers;
if (lhsMod.mShader != rhsMod.mShader) return false;
if (lhsMod.mColorFilter != rhsMod.mColorFilter) return false;
// Draw filter testing expects bit fields to be clear if filter not set.
if (lhsMod.mHasDrawFilter != rhsMod.mHasDrawFilter) return false;
if (lhsMod.mPaintFilterClearBits != rhsMod.mPaintFilterClearBits) return false;
if (lhsMod.mPaintFilterSetBits != rhsMod.mPaintFilterSetBits) return false;
return true;
}
virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
DEFER_LOGD("%d replaying DrawingBatch %p, with %d ops (batch id %x, merge id %p)",
index, this, mOps.size(), getBatchId(), getMergeId());
if (mOps.size() == 1) {
return DrawBatch::replay(renderer, dirty, false);
}
DrawOp* op = mOps[0];
DisplayListLogBuffer& buffer = DisplayListLogBuffer::getInstance();
buffer.writeCommand(0, "multiDraw");
buffer.writeCommand(1, op->name());
status_t status = op->multiDraw(renderer, dirty, mOps, mBounds);
#if DEBUG_MERGE_BEHAVIOR
renderer.drawScreenSpaceColorRect(mBounds.left, mBounds.top, mBounds.right, mBounds.bottom,
DEBUG_COLOR_MERGEDBATCH);
#endif
return status;
}
};
class StateOpBatch : public Batch {
public:
// creates a single operation batch
StateOpBatch(StateOp* op) : mOp(op) {}
virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
DEFER_LOGD("replaying state op batch %p", this);
renderer.restoreDisplayState(mOp->state);
// use invalid save count because it won't be used at flush time - RestoreToCountOp is the
// only one to use it, and we don't use that class at flush time, instead calling
// renderer.restoreToCount directly
int saveCount = -1;
mOp->applyState(renderer, saveCount);
return DrawGlInfo::kStatusDone;
}
private:
const StateOp* mOp;
};
class RestoreToCountBatch : public Batch {
public:
RestoreToCountBatch(StateOp* op, int restoreCount) : mOp(op), mRestoreCount(restoreCount) {}
virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
DEFER_LOGD("batch %p restoring to count %d", this, mRestoreCount);
renderer.restoreDisplayState(mOp->state);
renderer.restoreToCount(mRestoreCount);
return DrawGlInfo::kStatusDone;
}
private:
// we use the state storage for the RestoreToCountOp, but don't replay the op itself
const StateOp* mOp;
/*
* The count used here represents the flush() time saveCount. This is as opposed to the
* DisplayList record time, or defer() time values (which are RestoreToCountOp's mCount, and
* (saveCount + mCount) respectively). Since the count is different from the original
* RestoreToCountOp, we don't store a pointer to the op, as elsewhere.
*/
const int mRestoreCount;
};
#if DEBUG_MERGE_BEHAVIOR
class BarrierDebugBatch : public Batch {
virtual status_t replay(OpenGLRenderer& renderer, Rect& dirty, int index) {
renderer.drawScreenSpaceColorRect(0, 0, 10000, 10000, DEBUG_COLOR_BARRIER);
return DrawGlInfo::kStatusDrew;
}
};
#endif
/////////////////////////////////////////////////////////////////////////////////
// DeferredDisplayList
/////////////////////////////////////////////////////////////////////////////////
void DeferredDisplayList::resetBatchingState() {
for (int i = 0; i < kOpBatch_Count; i++) {
mBatchLookup[i] = NULL;
mMergingBatches[i].clear();
}
#if DEBUG_MERGE_BEHAVIOR
if (mBatches.size() != 0) {
mBatches.add(new BarrierDebugBatch());
}
#endif
mEarliestBatchIndex = mBatches.size();
}
void DeferredDisplayList::clear() {
resetBatchingState();
mComplexClipStackStart = -1;
for (unsigned int i = 0; i < mBatches.size(); i++) {
delete mBatches[i];
}
mBatches.clear();
mSaveStack.clear();
mEarliestBatchIndex = 0;
}
/////////////////////////////////////////////////////////////////////////////////
// Operation adding
/////////////////////////////////////////////////////////////////////////////////
int DeferredDisplayList::getStateOpDeferFlags() const {
// For both clipOp and save(Layer)Op, we don't want to save drawing info, and only want to save
// the clip if we aren't recording a complex clip (and can thus trust it to be a rect)
return recordingComplexClip() ? 0 : kStateDeferFlag_Clip;
}
int DeferredDisplayList::getDrawOpDeferFlags() const {
return kStateDeferFlag_Draw | getStateOpDeferFlags();
}
/**
* When an clipping operation occurs that could cause a complex clip, record the operation and all
* subsequent clipOps, save/restores (if the clip flag is set). During a flush, instead of loading
* the clip from deferred state, we play back all of the relevant state operations that generated
* the complex clip.
*
* Note that we don't need to record the associated restore operation, since operations at defer
* time record whether they should store the renderer's current clip
*/
void DeferredDisplayList::addClip(OpenGLRenderer& renderer, ClipOp* op) {
if (recordingComplexClip() || op->canCauseComplexClip() || !renderer.hasRectToRectTransform()) {
DEFER_LOGD("%p Received complex clip operation %p", this, op);
// NOTE: defer clip op before setting mComplexClipStackStart so previous clip is recorded
storeStateOpBarrier(renderer, op);
if (!recordingComplexClip()) {
mComplexClipStackStart = renderer.getSaveCount() - 1;
DEFER_LOGD(" Starting complex clip region, start is %d", mComplexClipStackStart);
}
}
}
/**
* For now, we record save layer operations as barriers in the batch list, preventing drawing
* operations from reordering around the saveLayer and it's associated restore()
*
* In the future, we should send saveLayer commands (if they can be played out of order) and their
* contained drawing operations to a seperate list of batches, so that they may draw at the
* beginning of the frame. This would avoid targetting and removing an FBO in the middle of a frame.
*
* saveLayer operations should be pulled to the beginning of the frame if the canvas doesn't have a
* complex clip, and if the flags (kClip_SaveFlag & kClipToLayer_SaveFlag) are set.
*/
void DeferredDisplayList::addSaveLayer(OpenGLRenderer& renderer,
SaveLayerOp* op, int newSaveCount) {
DEFER_LOGD("%p adding saveLayerOp %p, flags %x, new count %d",
this, op, op->getFlags(), newSaveCount);
storeStateOpBarrier(renderer, op);
mSaveStack.push(newSaveCount);
}
/**
* Takes save op and it's return value - the new save count - and stores it into the stream as a
* barrier if it's needed to properly modify a complex clip
*/
void DeferredDisplayList::addSave(OpenGLRenderer& renderer, SaveOp* op, int newSaveCount) {
int saveFlags = op->getFlags();
DEFER_LOGD("%p adding saveOp %p, flags %x, new count %d", this, op, saveFlags, newSaveCount);
if (recordingComplexClip() && (saveFlags & SkCanvas::kClip_SaveFlag)) {
// store and replay the save operation, as it may be needed to correctly playback the clip
DEFER_LOGD(" adding save barrier with new save count %d", newSaveCount);
storeStateOpBarrier(renderer, op);
mSaveStack.push(newSaveCount);
}
}
/**
* saveLayer() commands must be associated with a restoreToCount batch that will clean up and draw
* the layer in the deferred list
*
* other save() commands which occur as children of a snapshot with complex clip will be deferred,
* and must be restored
*
* Either will act as a barrier to draw operation reordering, as we want to play back layer
* save/restore and complex canvas modifications (including save/restore) in order.
*/
void DeferredDisplayList::addRestoreToCount(OpenGLRenderer& renderer, StateOp* op,
int newSaveCount) {
DEFER_LOGD("%p addRestoreToCount %d", this, newSaveCount);
if (recordingComplexClip() && newSaveCount <= mComplexClipStackStart) {
mComplexClipStackStart = -1;
resetBatchingState();
}
if (mSaveStack.isEmpty() || newSaveCount > mSaveStack.top()) {
return;
}
while (!mSaveStack.isEmpty() && mSaveStack.top() >= newSaveCount) mSaveStack.pop();
storeRestoreToCountBarrier(renderer, op, mSaveStack.size() + FLUSH_SAVE_STACK_DEPTH);
}
void DeferredDisplayList::addDrawOp(OpenGLRenderer& renderer, DrawOp* op) {
if (renderer.storeDisplayState(op->state, getDrawOpDeferFlags())) {
return; // quick rejected
}
int batchId = kOpBatch_None;
mergeid_t mergeId = (mergeid_t) -1;
bool mergeable = op->onDefer(renderer, &batchId, &mergeId);
// complex clip has a complex set of expectations on the renderer state - for now, avoid taking
// the merge path in those cases
mergeable &= !recordingComplexClip();
if (CC_UNLIKELY(renderer.getCaches().drawReorderDisabled)) {
// TODO: elegant way to reuse batches?
DrawBatch* b = new DrawBatch(batchId, mergeId);
b->add(op);
mBatches.add(b);
return;
}
// find the latest batch of the new op's type, and try to merge the new op into it
DrawBatch* targetBatch = NULL;
// insertion point of a new batch, will hopefully be immediately after similar batch
// (eventually, should be similar shader)
int insertBatchIndex = mBatches.size();
if (!mBatches.isEmpty()) {
if (op->state.mBounds.isEmpty()) {
// don't know the bounds for op, so add to last batch and start from scratch on next op
DrawBatch* b = new DrawBatch(batchId, mergeId);
b->add(op);
mBatches.add(b);
resetBatchingState();
#if DEBUG_DEFER
DEFER_LOGD("Warning: Encountered op with empty bounds, resetting batches");
op->output(2);
#endif
return;
}
if (mergeable) {
// Try to merge with any existing batch with same mergeId.
if (mMergingBatches[batchId].get(mergeId, targetBatch)) {
if (!((MergingDrawBatch*) targetBatch)->canMergeWith(op)) {
targetBatch = NULL;
}
}
} else {
// join with similar, non-merging batch
targetBatch = (DrawBatch*)mBatchLookup[batchId];
}
if (targetBatch || mergeable) {
// iterate back toward target to see if anything drawn since should overlap the new op
// if no target, merging ops still interate to find similar batch to insert after
for (int i = mBatches.size() - 1; i >= mEarliestBatchIndex; i--) {
DrawBatch* overBatch = (DrawBatch*)mBatches[i];
if (overBatch == targetBatch) break;
// TODO: also consider shader shared between batch types
if (batchId == overBatch->getBatchId()) {
insertBatchIndex = i + 1;
if (!targetBatch) break; // found insert position, quit
}
if (overBatch->intersects(op->state.mBounds)) {
// NOTE: it may be possible to optimize for special cases where two operations
// of the same batch/paint could swap order, such as with a non-mergeable
// (clipped) and a mergeable text operation
targetBatch = NULL;
#if DEBUG_DEFER
DEFER_LOGD("op couldn't join batch %d, was intersected by batch %d",
targetIndex, i);
op->output(2);
#endif
break;
}
}
}
}
if (!targetBatch) {
if (mergeable) {
targetBatch = new MergingDrawBatch(batchId, mergeId);
mMergingBatches[batchId].put(mergeId, targetBatch);
} else {
targetBatch = new DrawBatch(batchId, mergeId);
mBatchLookup[batchId] = targetBatch;
DEFER_LOGD("creating Batch %p, bid %x, at %d",
targetBatch, batchId, insertBatchIndex);
}
mBatches.insertAt(targetBatch, insertBatchIndex);
}
targetBatch->add(op);
}
void DeferredDisplayList::storeStateOpBarrier(OpenGLRenderer& renderer, StateOp* op) {
DEFER_LOGD("%p adding state op barrier at pos %d", this, mBatches.size());
renderer.storeDisplayState(op->state, getStateOpDeferFlags());
mBatches.add(new StateOpBatch(op));
resetBatchingState();
}
void DeferredDisplayList::storeRestoreToCountBarrier(OpenGLRenderer& renderer, StateOp* op,
int newSaveCount) {
DEFER_LOGD("%p adding restore to count %d barrier, pos %d",
this, newSaveCount, mBatches.size());
// store displayState for the restore operation, as it may be associated with a saveLayer that
// doesn't have kClip_SaveFlag set
renderer.storeDisplayState(op->state, getStateOpDeferFlags());
mBatches.add(new RestoreToCountBatch(op, newSaveCount));
resetBatchingState();
}
/////////////////////////////////////////////////////////////////////////////////
// Replay / flush
/////////////////////////////////////////////////////////////////////////////////
static status_t replayBatchList(const Vector<Batch*>& batchList,
OpenGLRenderer& renderer, Rect& dirty) {
status_t status = DrawGlInfo::kStatusDone;
for (unsigned int i = 0; i < batchList.size(); i++) {
status |= batchList[i]->replay(renderer, dirty, i);
}
DEFER_LOGD("--flushed, drew %d batches", batchList.size());
return status;
}
status_t DeferredDisplayList::flush(OpenGLRenderer& renderer, Rect& dirty) {
ATRACE_NAME("flush drawing commands");
Caches::getInstance().fontRenderer->endPrecaching();
status_t status = DrawGlInfo::kStatusDone;
if (isEmpty()) return status; // nothing to flush
renderer.restoreToCount(1);
DEFER_LOGD("--flushing");
renderer.eventMark("Flush");
// save and restore (with draw modifiers) so that reordering doesn't affect final state
DrawModifiers restoreDrawModifiers = renderer.getDrawModifiers();
renderer.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
// NOTE: depth of the save stack at this point, before playback, should be reflected in
// FLUSH_SAVE_STACK_DEPTH, so that save/restores match up correctly
status |= replayBatchList(mBatches, renderer, dirty);
renderer.restoreToCount(1);
renderer.setDrawModifiers(restoreDrawModifiers);
DEFER_LOGD("--flush complete, returning %x", status);
clear();
return status;
}
}; // namespace uirenderer
}; // namespace android
|