forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
display_list_matrix_clip_tracker.cc
390 lines (355 loc) · 12 KB
/
display_list_matrix_clip_tracker.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
// 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 "flutter/display_list/display_list_matrix_clip_tracker.h"
#include "flutter/display_list/display_list_builder.h"
#include "flutter/fml/logging.h"
#include "third_party/skia/include/core/SkPath.h"
namespace flutter {
class Data4x4 : public DisplayListMatrixClipTracker::Data {
public:
Data4x4(const SkM44& m44, const SkRect& rect) : Data(rect), m44_(m44) {}
explicit Data4x4(const Data* copy)
: Data(copy->device_cull_rect()), m44_(copy->matrix_4x4()) {}
~Data4x4() override = default;
bool is_4x4() const override { return true; }
SkMatrix matrix_3x3() const override { return m44_.asM33(); }
SkM44 matrix_4x4() const override { return m44_; }
SkRect local_cull_rect() const override;
void translate(SkScalar tx, SkScalar ty) override {
m44_.preTranslate(tx, ty);
}
void scale(SkScalar sx, SkScalar sy) override { m44_.preScale(sx, sy); }
void skew(SkScalar skx, SkScalar sky) override {
m44_.preConcat(SkMatrix::Skew(skx, sky));
}
void rotate(SkScalar degrees) override {
m44_.preConcat(SkMatrix::RotateDeg(degrees));
}
void transform(const SkMatrix& matrix) override { m44_.preConcat(matrix); }
void transform(const SkM44& m44) override { m44_.preConcat(m44); }
void setTransform(const SkMatrix& matrix) override { m44_ = SkM44(matrix); }
void setTransform(const SkM44& m44) override { m44_ = m44; }
void setIdentity() override { m44_.setIdentity(); }
bool mapRect(const SkRect& rect, SkRect* mapped) const override {
return m44_.asM33().mapRect(mapped, rect);
}
bool canBeInverted() const override { return m44_.asM33().invert(nullptr); }
protected:
bool has_perspective() const override;
private:
SkM44 m44_;
};
class Data3x3 : public DisplayListMatrixClipTracker::Data {
public:
Data3x3(const SkMatrix& matrix, const SkRect& rect)
: Data(rect), matrix_(matrix) {}
explicit Data3x3(const Data* copy)
: Data(copy->device_cull_rect()), matrix_(copy->matrix_3x3()) {}
~Data3x3() override = default;
bool is_4x4() const override { return false; }
SkMatrix matrix_3x3() const override { return matrix_; }
SkM44 matrix_4x4() const override { return SkM44(matrix_); }
SkRect local_cull_rect() const override;
void translate(SkScalar tx, SkScalar ty) override {
matrix_.preTranslate(tx, ty);
}
void scale(SkScalar sx, SkScalar sy) override { matrix_.preScale(sx, sy); }
void skew(SkScalar skx, SkScalar sky) override { matrix_.preSkew(skx, sky); }
void rotate(SkScalar degrees) override { matrix_.preRotate(degrees); }
void transform(const SkMatrix& matrix) override { matrix_.preConcat(matrix); }
void transform(const SkM44& m44) override {
FML_CHECK(false) << "SkM44 was concatenated without upgrading Data";
}
void setTransform(const SkMatrix& matrix) override { matrix_ = matrix; }
void setTransform(const SkM44& m44) override {
FML_CHECK(false) << "SkM44 was set without upgrading Data";
}
void setIdentity() override { matrix_.setIdentity(); }
bool mapRect(const SkRect& rect, SkRect* mapped) const override {
return matrix_.mapRect(mapped, rect);
}
bool canBeInverted() const override { return matrix_.invert(nullptr); }
protected:
bool has_perspective() const override { return matrix_.hasPerspective(); }
private:
SkMatrix matrix_;
};
static bool is_3x3(const SkM44& m) {
// clang-format off
return ( m.rc(0, 2) == 0 &&
m.rc(1, 2) == 0 &&
m.rc(2, 0) == 0 && m.rc(2, 1) == 0 && m.rc(2, 2) == 1 && m.rc(2, 3) == 0 &&
m.rc(3, 2) == 0);
// clang-format on
}
DisplayListMatrixClipTracker::DisplayListMatrixClipTracker(
const SkRect& cull_rect,
const SkMatrix& matrix) {
// isEmpty protects us against NaN as we normalize any empty cull rects
SkRect cull = cull_rect.isEmpty() ? SkRect::MakeEmpty() : cull_rect;
saved_.emplace_back(std::make_unique<Data3x3>(matrix, cull));
current_ = saved_.back().get();
}
DisplayListMatrixClipTracker::DisplayListMatrixClipTracker(
const SkRect& cull_rect,
const SkM44& m44) {
// isEmpty protects us against NaN as we normalize any empty cull rects
SkRect cull = cull_rect.isEmpty() ? SkRect::MakeEmpty() : cull_rect;
if (is_3x3(m44)) {
saved_.emplace_back(std::make_unique<Data3x3>(m44.asM33(), cull));
} else {
saved_.emplace_back(std::make_unique<Data4x4>(m44, cull));
}
current_ = saved_.back().get();
}
// clang-format off
void DisplayListMatrixClipTracker::transform2DAffine(
SkScalar mxx, SkScalar mxy, SkScalar mxt,
SkScalar myx, SkScalar myy, SkScalar myt) {
if (!current_->is_4x4()) {
transform(SkMatrix::MakeAll(mxx, mxy, mxt,
myx, myy, myt,
0, 0, 1));
} else {
transform(SkM44(mxx, mxy, 0, mxt,
myx, myy, 0, myt,
0, 0, 1, 0,
0, 0, 0, 1));
}
}
void DisplayListMatrixClipTracker::transformFullPerspective(
SkScalar mxx, SkScalar mxy, SkScalar mxz, SkScalar mxt,
SkScalar myx, SkScalar myy, SkScalar myz, SkScalar myt,
SkScalar mzx, SkScalar mzy, SkScalar mzz, SkScalar mzt,
SkScalar mwx, SkScalar mwy, SkScalar mwz, SkScalar mwt) {
if (!current_->is_4x4()) {
if ( mxz == 0 &&
myz == 0 &&
mzx == 0 && mzy == 0 && mzz == 1 && mzt == 0 &&
mwz == 0) {
transform(SkMatrix::MakeAll(mxx, mxy, mxt,
myx, myy, myt,
mwx, mwy, mwt));
return;
}
}
transform(SkM44(mxx, mxy, mxz, mxt,
myx, myy, myz, myt,
mzx, mzy, mzz, mzt,
mwx, mwy, mwz, mwt));
}
// clang-format on
void DisplayListMatrixClipTracker::save() {
if (current_->is_4x4()) {
saved_.emplace_back(std::make_unique<Data4x4>(current_));
} else {
saved_.emplace_back(std::make_unique<Data3x3>(current_));
}
current_ = saved_.back().get();
}
void DisplayListMatrixClipTracker::restore() {
saved_.pop_back();
current_ = saved_.back().get();
}
void DisplayListMatrixClipTracker::restoreToCount(int restore_count) {
FML_DCHECK(restore_count <= getSaveCount());
if (restore_count < 1) {
restore_count = 1;
}
while (restore_count < getSaveCount()) {
restore();
}
}
void DisplayListMatrixClipTracker::transform(const SkM44& m44) {
if (!current_->is_4x4()) {
if (is_3x3(m44)) {
current_->transform(m44.asM33());
return;
}
saved_.back() = std::make_unique<Data4x4>(current_);
current_ = saved_.back().get();
}
current_->transform(m44);
}
void DisplayListMatrixClipTracker::setTransform(const SkM44& m44) {
if (!current_->is_4x4()) {
if (is_3x3(m44)) {
current_->setTransform(m44.asM33());
return;
}
saved_.back() = std::make_unique<Data4x4>(current_);
current_ = saved_.back().get();
}
current_->setTransform(m44);
}
void DisplayListMatrixClipTracker::clipRRect(const SkRRect& rrect,
SkClipOp op,
bool is_aa) {
switch (op) {
case SkClipOp::kIntersect:
break;
case SkClipOp::kDifference:
if (!rrect.isRect()) {
return;
}
break;
}
current_->clipBounds(rrect.getBounds(), op, is_aa);
}
void DisplayListMatrixClipTracker::clipPath(const SkPath& path,
SkClipOp op,
bool is_aa) {
// Map "kDifference of inverse path" to "kIntersect of the original path" and
// map "kIntersect of inverse path" to "kDifference of the original path"
if (path.isInverseFillType()) {
switch (op) {
case SkClipOp::kIntersect:
op = SkClipOp::kDifference;
break;
case SkClipOp::kDifference:
op = SkClipOp::kIntersect;
break;
}
}
SkRect bounds;
switch (op) {
case SkClipOp::kIntersect:
bounds = path.getBounds();
break;
case SkClipOp::kDifference:
if (!path.isRect(&bounds)) {
return;
}
break;
}
current_->clipBounds(bounds, op, is_aa);
}
bool DisplayListMatrixClipTracker::Data::content_culled(
const SkRect& content_bounds) const {
if (cull_rect_.isEmpty() || content_bounds.isEmpty()) {
return true;
}
if (!canBeInverted()) {
return true;
}
if (has_perspective()) {
return false;
}
SkRect mapped;
mapRect(content_bounds, &mapped);
return !mapped.intersects(cull_rect_);
}
void DisplayListMatrixClipTracker::Data::clipBounds(const SkRect& clip,
SkClipOp op,
bool is_aa) {
if (cull_rect_.isEmpty()) {
// No point in intersecting further.
return;
}
if (has_perspective()) {
// We can conservatively ignore this clip.
return;
}
switch (op) {
case SkClipOp::kIntersect: {
if (clip.isEmpty()) {
cull_rect_.setEmpty();
break;
}
SkRect rect;
mapRect(clip, &rect);
if (is_aa) {
rect.roundOut(&rect);
}
if (!cull_rect_.intersect(rect)) {
cull_rect_.setEmpty();
}
break;
}
case SkClipOp::kDifference: {
if (clip.isEmpty() || !clip.intersects(cull_rect_)) {
break;
}
SkRect rect;
if (mapRect(clip, &rect)) {
// This technique only works if it is rect -> rect
if (is_aa) {
SkIRect rounded;
rect.round(&rounded);
if (rounded.isEmpty()) {
break;
}
rect.set(rounded);
}
if (rect.fLeft <= cull_rect_.fLeft &&
rect.fRight >= cull_rect_.fRight) {
// bounds spans entire width of cull_rect_
// therefore we can slice off a top or bottom
// edge of the cull_rect_.
SkScalar top = std::max(rect.fBottom, cull_rect_.fTop);
SkScalar btm = std::min(rect.fTop, cull_rect_.fBottom);
if (top < btm) {
cull_rect_.fTop = top;
cull_rect_.fBottom = btm;
} else {
cull_rect_.setEmpty();
}
} else if (rect.fTop <= cull_rect_.fTop &&
rect.fBottom >= cull_rect_.fBottom) {
// bounds spans entire height of cull_rect_
// therefore we can slice off a left or right
// edge of the cull_rect_.
SkScalar lft = std::max(rect.fRight, cull_rect_.fLeft);
SkScalar rgt = std::min(rect.fLeft, cull_rect_.fRight);
if (lft < rgt) {
cull_rect_.fLeft = lft;
cull_rect_.fRight = rgt;
} else {
cull_rect_.setEmpty();
}
}
}
break;
}
}
}
SkRect Data4x4::local_cull_rect() const {
if (cull_rect_.isEmpty()) {
return cull_rect_;
}
SkMatrix inverse;
if (!m44_.asM33().invert(&inverse)) {
return SkRect::MakeEmpty();
}
if (has_perspective()) {
// We could do a 4-point long-form conversion, but since this is
// only used for culling, let's just return a non-constricting
// cull rect.
return DisplayListBuilder::kMaxCullRect;
}
return inverse.mapRect(cull_rect_);
}
bool Data4x4::has_perspective() const {
return (m44_.rc(3, 0) != 0 || //
m44_.rc(3, 1) != 0 || //
m44_.rc(3, 2) != 0 || //
m44_.rc(3, 3) != 1);
}
SkRect Data3x3::local_cull_rect() const {
if (cull_rect_.isEmpty()) {
return cull_rect_;
}
SkMatrix inverse;
if (!matrix_.invert(&inverse)) {
return SkRect::MakeEmpty();
}
if (matrix_.hasPerspective()) {
// We could do a 4-point long-form conversion, but since this is
// only used for culling, let's just return a non-constricting
// cull rect.
return DisplayListBuilder::kMaxCullRect;
}
return inverse.mapRect(cull_rect_);
}
} // namespace flutter