forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay_list.cc
479 lines (430 loc) · 15.7 KB
/
display_list.cc
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
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <type_traits>
#include "flutter/display_list/display_list.h"
#include "flutter/display_list/dl_op_records.h"
#include "flutter/fml/trace_event.h"
namespace flutter {
const SaveLayerOptions SaveLayerOptions::kNoAttributes = SaveLayerOptions();
const SaveLayerOptions SaveLayerOptions::kWithAttributes =
kNoAttributes.with_renders_with_attributes();
DisplayList::DisplayList()
: op_count_(0),
nested_byte_count_(0),
nested_op_count_(0),
total_depth_(0),
unique_id_(0),
bounds_({0, 0, 0, 0}),
can_apply_group_opacity_(true),
is_ui_thread_safe_(true),
modifies_transparent_black_(false),
root_has_backdrop_filter_(false),
root_is_unbounded_(false),
max_root_blend_mode_(DlBlendMode::kClear) {
FML_DCHECK(offsets_.size() == 0u);
FML_DCHECK(storage_.size() == 0u);
}
DisplayList::DisplayList(DisplayListStorage&& storage,
std::vector<size_t>&& offsets,
uint32_t op_count,
size_t nested_byte_count,
uint32_t nested_op_count,
uint32_t total_depth,
const SkRect& bounds,
bool can_apply_group_opacity,
bool is_ui_thread_safe,
bool modifies_transparent_black,
DlBlendMode max_root_blend_mode,
bool root_has_backdrop_filter,
bool root_is_unbounded,
sk_sp<const DlRTree> rtree)
: storage_(std::move(storage)),
offsets_(std::move(offsets)),
op_count_(op_count),
nested_byte_count_(nested_byte_count),
nested_op_count_(nested_op_count),
total_depth_(total_depth),
unique_id_(next_unique_id()),
bounds_(bounds),
can_apply_group_opacity_(can_apply_group_opacity),
is_ui_thread_safe_(is_ui_thread_safe),
modifies_transparent_black_(modifies_transparent_black),
root_has_backdrop_filter_(root_has_backdrop_filter),
root_is_unbounded_(root_is_unbounded),
max_root_blend_mode_(max_root_blend_mode),
rtree_(std::move(rtree)) {
FML_DCHECK(storage_.capacity() == storage_.size());
}
DisplayList::~DisplayList() {
DisposeOps(storage_, offsets_);
}
uint32_t DisplayList::next_unique_id() {
static std::atomic<uint32_t> next_id{1};
uint32_t id;
do {
id = next_id.fetch_add(+1, std::memory_order_relaxed);
} while (id == 0);
return id;
}
struct SaveInfo {
SaveInfo(DlIndex previous_restore_index, bool save_was_needed)
: previous_restore_index(previous_restore_index),
save_was_needed(save_was_needed) {}
DlIndex previous_restore_index;
bool save_was_needed;
};
void DisplayList::RTreeResultsToIndexVector(
std::vector<DlIndex>& indices,
const std::vector<int>& rtree_results) const {
FML_DCHECK(rtree_);
auto cur_rect = rtree_results.begin();
auto end_rect = rtree_results.end();
if (cur_rect >= end_rect) {
return;
}
DlIndex next_render_index = rtree_->id(*cur_rect++);
DlIndex next_restore_index = std::numeric_limits<DlIndex>::max();
std::vector<SaveInfo> save_infos;
for (DlIndex index = 0u; index < offsets_.size(); index++) {
while (index > next_render_index) {
if (cur_rect < end_rect) {
next_render_index = rtree_->id(*cur_rect++);
} else {
// Nothing left to render.
// Nothing left to do, but match our restores from the stack.
while (!save_infos.empty()) {
SaveInfo& info = save_infos.back();
// stack top boolean tells us whether the local variable
// next_restore_index should be executed. The local variable
// then gets reset to the value stored in the stack top
if (info.save_was_needed) {
FML_DCHECK(next_restore_index < offsets_.size());
indices.push_back(next_restore_index);
}
next_restore_index = info.previous_restore_index;
save_infos.pop_back();
}
return;
}
}
const uint8_t* ptr = storage_.base() + offsets_[index];
const DLOp* op = reinterpret_cast<const DLOp*>(ptr);
switch (GetOpCategory(op->type)) {
case DisplayListOpCategory::kAttribute:
// Attributes are always needed
indices.push_back(index);
break;
case DisplayListOpCategory::kTransform:
case DisplayListOpCategory::kClip:
if (next_render_index < next_restore_index) {
indices.push_back(index);
}
break;
case DisplayListOpCategory::kRendering:
case DisplayListOpCategory::kSubDisplayList:
if (index == next_render_index) {
indices.push_back(index);
}
break;
case DisplayListOpCategory::kSave:
case DisplayListOpCategory::kSaveLayer: {
bool needed = (next_render_index < next_restore_index);
save_infos.emplace_back(next_restore_index, needed);
switch (op->type) {
case DisplayListOpType::kSave:
case DisplayListOpType::kSaveLayer:
case DisplayListOpType::kSaveLayerBackdrop:
next_restore_index =
static_cast<const SaveOpBase*>(op)->restore_index;
break;
default:
FML_UNREACHABLE();
}
if (needed) {
indices.push_back(index);
}
break;
}
case DisplayListOpCategory::kRestore: {
FML_DCHECK(!save_infos.empty());
FML_DCHECK(index == next_restore_index);
SaveInfo& info = save_infos.back();
next_restore_index = info.previous_restore_index;
if (info.save_was_needed) {
indices.push_back(index);
}
save_infos.pop_back();
break;
}
case DisplayListOpCategory::kInvalidCategory:
FML_UNREACHABLE();
}
}
}
void DisplayList::Dispatch(DlOpReceiver& receiver) const {
const uint8_t* base = storage_.base();
for (size_t offset : offsets_) {
DispatchOneOp(receiver, base + offset);
}
}
void DisplayList::Dispatch(DlOpReceiver& receiver,
const SkIRect& cull_rect) const {
Dispatch(receiver, SkRect::Make(cull_rect));
}
void DisplayList::Dispatch(DlOpReceiver& receiver,
const SkRect& cull_rect) const {
if (cull_rect.isEmpty()) {
return;
}
if (!has_rtree() || cull_rect.contains(bounds())) {
Dispatch(receiver);
} else {
auto op_indices = GetCulledIndices(cull_rect);
const uint8_t* base = storage_.base();
for (DlIndex index : op_indices) {
DispatchOneOp(receiver, base + offsets_[index]);
}
}
}
void DisplayList::DispatchOneOp(DlOpReceiver& receiver,
const uint8_t* ptr) const {
auto op = reinterpret_cast<const DLOp*>(ptr);
switch (op->type) {
#define DL_OP_DISPATCH(name) \
case DisplayListOpType::k##name: \
static_cast<const name##Op*>(op)->dispatch(receiver); \
break;
FOR_EACH_DISPLAY_LIST_OP(DL_OP_DISPATCH)
#undef DL_OP_DISPATCH
case DisplayListOpType::kInvalidOp:
default:
FML_DCHECK(false) << "Unrecognized op type: "
<< static_cast<int>(op->type);
}
}
void DisplayList::DisposeOps(const DisplayListStorage& storage,
const std::vector<size_t>& offsets) {
const uint8_t* base = storage.base();
if (!base) {
return;
}
for (size_t offset : offsets) {
auto op = reinterpret_cast<const DLOp*>(base + offset);
switch (op->type) {
#define DL_OP_DISPOSE(name) \
case DisplayListOpType::k##name: \
if (!std::is_trivially_destructible_v<name##Op>) { \
static_cast<const name##Op*>(op)->~name##Op(); \
} \
break;
FOR_EACH_DISPLAY_LIST_OP(DL_OP_DISPOSE)
#undef DL_OP_DISPOSE
default:
FML_UNREACHABLE();
}
}
}
DisplayListOpCategory DisplayList::GetOpCategory(DlIndex index) const {
return GetOpCategory(GetOpType(index));
}
DisplayListOpCategory DisplayList::GetOpCategory(DisplayListOpType type) {
switch (type) {
case DisplayListOpType::kSetAntiAlias:
case DisplayListOpType::kSetInvertColors:
case DisplayListOpType::kSetStrokeCap:
case DisplayListOpType::kSetStrokeJoin:
case DisplayListOpType::kSetStyle:
case DisplayListOpType::kSetStrokeWidth:
case DisplayListOpType::kSetStrokeMiter:
case DisplayListOpType::kSetColor:
case DisplayListOpType::kSetBlendMode:
case DisplayListOpType::kClearColorFilter:
case DisplayListOpType::kSetPodColorFilter:
case DisplayListOpType::kClearColorSource:
case DisplayListOpType::kSetPodColorSource:
case DisplayListOpType::kSetImageColorSource:
case DisplayListOpType::kSetRuntimeEffectColorSource:
case DisplayListOpType::kClearImageFilter:
case DisplayListOpType::kSetPodImageFilter:
case DisplayListOpType::kSetSharedImageFilter:
case DisplayListOpType::kClearMaskFilter:
case DisplayListOpType::kSetPodMaskFilter:
return DisplayListOpCategory::kAttribute;
case DisplayListOpType::kSave:
return DisplayListOpCategory::kSave;
case DisplayListOpType::kSaveLayer:
case DisplayListOpType::kSaveLayerBackdrop:
return DisplayListOpCategory::kSaveLayer;
case DisplayListOpType::kRestore:
return DisplayListOpCategory::kRestore;
case DisplayListOpType::kTranslate:
case DisplayListOpType::kScale:
case DisplayListOpType::kRotate:
case DisplayListOpType::kSkew:
case DisplayListOpType::kTransform2DAffine:
case DisplayListOpType::kTransformFullPerspective:
case DisplayListOpType::kTransformReset:
return DisplayListOpCategory::kTransform;
case DisplayListOpType::kClipIntersectRect:
case DisplayListOpType::kClipIntersectOval:
case DisplayListOpType::kClipIntersectRoundRect:
case DisplayListOpType::kClipIntersectPath:
case DisplayListOpType::kClipDifferenceRect:
case DisplayListOpType::kClipDifferenceOval:
case DisplayListOpType::kClipDifferenceRoundRect:
case DisplayListOpType::kClipDifferencePath:
return DisplayListOpCategory::kClip;
case DisplayListOpType::kDrawPaint:
case DisplayListOpType::kDrawColor:
case DisplayListOpType::kDrawLine:
case DisplayListOpType::kDrawDashedLine:
case DisplayListOpType::kDrawRect:
case DisplayListOpType::kDrawOval:
case DisplayListOpType::kDrawCircle:
case DisplayListOpType::kDrawRoundRect:
case DisplayListOpType::kDrawDiffRoundRect:
case DisplayListOpType::kDrawArc:
case DisplayListOpType::kDrawPath:
case DisplayListOpType::kDrawPoints:
case DisplayListOpType::kDrawLines:
case DisplayListOpType::kDrawPolygon:
case DisplayListOpType::kDrawVertices:
case DisplayListOpType::kDrawImage:
case DisplayListOpType::kDrawImageWithAttr:
case DisplayListOpType::kDrawImageRect:
case DisplayListOpType::kDrawImageNine:
case DisplayListOpType::kDrawImageNineWithAttr:
case DisplayListOpType::kDrawAtlas:
case DisplayListOpType::kDrawAtlasCulled:
case DisplayListOpType::kDrawTextBlob:
case DisplayListOpType::kDrawTextFrame:
case DisplayListOpType::kDrawShadow:
case DisplayListOpType::kDrawShadowTransparentOccluder:
return DisplayListOpCategory::kRendering;
case DisplayListOpType::kDrawDisplayList:
return DisplayListOpCategory::kSubDisplayList;
case DisplayListOpType::kInvalidOp:
return DisplayListOpCategory::kInvalidCategory;
}
}
DisplayListOpType DisplayList::GetOpType(DlIndex index) const {
// Assert unsigned type so we can eliminate >= 0 comparison
static_assert(std::is_unsigned_v<DlIndex>);
if (index >= offsets_.size()) {
return DisplayListOpType::kInvalidOp;
}
size_t offset = offsets_[index];
FML_DCHECK(offset < storage_.size());
auto ptr = storage_.base() + offset;
auto op = reinterpret_cast<const DLOp*>(ptr);
return op->type;
}
static void FillAllIndices(std::vector<DlIndex>& indices, DlIndex size) {
indices.reserve(size);
for (DlIndex i = 0u; i < size; i++) {
indices.push_back(i);
}
}
std::vector<DlIndex> DisplayList::GetCulledIndices(
const SkRect& cull_rect) const {
std::vector<DlIndex> indices;
if (!cull_rect.isEmpty()) {
if (rtree_) {
std::vector<int> rect_indices;
rtree_->search(cull_rect, &rect_indices);
RTreeResultsToIndexVector(indices, rect_indices);
} else {
FillAllIndices(indices, offsets_.size());
}
}
return indices;
}
bool DisplayList::Dispatch(DlOpReceiver& receiver, DlIndex index) const {
// Assert unsigned type so we can eliminate >= 0 comparison
static_assert(std::is_unsigned_v<DlIndex>);
if (index >= offsets_.size()) {
return false;
}
size_t offset = offsets_[index];
FML_DCHECK(offset < storage_.size());
auto ptr = storage_.base() + offset;
DispatchOneOp(receiver, ptr);
return true;
}
static bool CompareOps(const DisplayListStorage& storageA,
const std::vector<size_t>& offsetsA,
const DisplayListStorage& storageB,
const std::vector<size_t>& offsetsB) {
const uint8_t* base_a = storageA.base();
const uint8_t* base_b = storageB.base();
// These conditions are checked by the caller...
FML_DCHECK(offsetsA.size() == offsetsB.size());
FML_DCHECK(base_a != base_b);
size_t bulk_start = 0u;
for (size_t i = 0; i < offsetsA.size(); i++) {
size_t offset = offsetsA[i];
FML_DCHECK(offsetsB[i] == offset);
auto opA = reinterpret_cast<const DLOp*>(base_a + offset);
auto opB = reinterpret_cast<const DLOp*>(base_b + offset);
if (opA->type != opB->type) {
return false;
}
DisplayListCompare result;
switch (opA->type) {
#define DL_OP_EQUALS(name) \
case DisplayListOpType::k##name: \
result = static_cast<const name##Op*>(opA)->equals( \
static_cast<const name##Op*>(opB)); \
break;
FOR_EACH_DISPLAY_LIST_OP(DL_OP_EQUALS)
#undef DL_OP_EQUALS
default:
FML_DCHECK(false);
return false;
}
switch (result) {
case DisplayListCompare::kNotEqual:
return false;
case DisplayListCompare::kUseBulkCompare:
break;
case DisplayListCompare::kEqual:
// Check if we have a backlog of bytes to bulk compare and then
// reset the bulk compare pointers to the address following this op
if (bulk_start < offset) {
const uint8_t* bulk_start_a = base_a + bulk_start;
const uint8_t* bulk_start_b = base_b + bulk_start;
if (memcmp(bulk_start_a, bulk_start_b, offset - bulk_start) != 0) {
return false;
}
}
bulk_start =
i + 1 < offsetsA.size() ? offsetsA[i + 1] : storageA.size();
break;
}
}
if (bulk_start < storageA.size()) {
// Perform a final bulk compare if we have remaining bytes waiting
const uint8_t* bulk_start_a = base_a + bulk_start;
const uint8_t* bulk_start_b = base_b + bulk_start;
if (memcmp(bulk_start_a, bulk_start_b, storageA.size() - bulk_start) != 0) {
return false;
}
}
return true;
}
bool DisplayList::Equals(const DisplayList* other) const {
if (this == other) {
return true;
}
if (offsets_.size() != other->offsets_.size() ||
storage_.size() != other->storage_.size() ||
op_count_ != other->op_count_) {
return false;
}
if (storage_.base() == other->storage_.base()) {
return true;
}
return CompareOps(storage_, offsets_, other->storage_, other->offsets_);
}
} // namespace flutter