forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock_canvas.h
367 lines (326 loc) · 13.3 KB
/
mock_canvas.h
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
// 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.
#ifndef TESTING_MOCK_CANVAS_H_
#define TESTING_MOCK_CANVAS_H_
#include <ostream>
#include <variant>
#include <vector>
#include "flutter/display_list/dl_canvas.h"
#include "flutter/display_list/utils/dl_matrix_clip_tracker.h"
#include "gtest/gtest.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkCanvasVirtualEnforcer.h"
#include "third_party/skia/include/core/SkClipOp.h"
#include "third_party/skia/include/core/SkData.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkImageFilter.h"
#include "third_party/skia/include/core/SkM44.h"
#include "third_party/skia/include/core/SkPath.h"
#include "third_party/skia/include/core/SkRRect.h"
#include "third_party/skia/include/core/SkRect.h"
#include "third_party/skia/include/utils/SkNWayCanvas.h"
namespace flutter {
namespace testing {
static constexpr SkRect kEmptyRect = SkRect::MakeEmpty();
// Mock |SkCanvas|, useful for writing tests that use Skia but do not interact
// with the GPU.
//
// The |MockCanvas| stores a list of |DrawCall| that the test can later verify
// against the expected list of primitives to be drawn.
class MockCanvas final : public DlCanvas {
public:
enum ClipEdgeStyle {
kHardClipEdgeStyle,
kSoftClipEdgeStyle,
};
struct SaveData {
int save_to_layer;
};
struct SaveLayerData {
SkRect save_bounds;
DlPaint restore_paint;
std::shared_ptr<DlImageFilter> backdrop_filter;
int save_to_layer;
};
struct RestoreData {
int restore_to_layer;
};
struct ConcatMatrixData {
SkM44 matrix;
};
struct SetMatrixData {
SkM44 matrix;
};
struct DrawRectData {
SkRect rect;
DlPaint paint;
};
struct DrawPathData {
SkPath path;
DlPaint paint;
};
struct DrawTextData {
sk_sp<SkData> serialized_text;
DlPaint paint;
SkPoint offset;
};
struct DrawImageDataNoPaint {
sk_sp<DlImage> image;
SkScalar x;
SkScalar y;
DlImageSampling options;
};
struct DrawImageData {
sk_sp<DlImage> image;
SkScalar x;
SkScalar y;
DlImageSampling options;
DlPaint paint;
};
struct DrawDisplayListData {
sk_sp<DisplayList> display_list;
SkScalar opacity;
};
struct DrawShadowData {
SkPath path;
DlColor color;
SkScalar elevation;
bool transparent_occluder;
SkScalar dpr;
};
struct ClipRectData {
SkRect rect;
ClipOp clip_op;
ClipEdgeStyle style;
};
struct ClipRRectData {
SkRRect rrect;
ClipOp clip_op;
ClipEdgeStyle style;
};
struct ClipPathData {
SkPath path;
ClipOp clip_op;
ClipEdgeStyle style;
};
struct DrawPaintData {
DlPaint paint;
};
// Discriminated union of all the different |DrawCall| types. It is roughly
// equivalent to the different methods in |SkCanvas|' public API.
using DrawCallData = std::variant<SaveData,
SaveLayerData,
RestoreData,
ConcatMatrixData,
SetMatrixData,
DrawRectData,
DrawPathData,
DrawTextData,
DrawImageDataNoPaint,
DrawImageData,
DrawDisplayListData,
DrawShadowData,
ClipRectData,
ClipRRectData,
ClipPathData,
DrawPaintData>;
// A single call made against this canvas.
struct DrawCall {
int layer;
DrawCallData data;
};
MockCanvas();
MockCanvas(int width, int height);
~MockCanvas();
const std::vector<DrawCall>& draw_calls() const { return draw_calls_; }
void reset_draw_calls() { draw_calls_.clear(); }
SkISize GetBaseLayerSize() const override;
SkImageInfo GetImageInfo() const override;
void Save() override;
void SaveLayer(const SkRect* bounds,
const DlPaint* paint = nullptr,
const DlImageFilter* backdrop = nullptr) override;
void Restore() override;
int GetSaveCount() const { return current_layer_; }
void RestoreToCount(int restore_count) {
while (current_layer_ > restore_count) {
Restore();
}
}
// clang-format off
// 2x3 2D affine subset of a 4x4 transform in row major order
void Transform2DAffine(SkScalar mxx, SkScalar mxy, SkScalar mxt,
SkScalar myx, SkScalar myy, SkScalar myt) override;
// full 4x4 transform in row major order
void 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) override;
// clang-format on
void Translate(SkScalar tx, SkScalar ty) override;
void Scale(SkScalar sx, SkScalar sy) override;
void Rotate(SkScalar degrees) override;
void Skew(SkScalar sx, SkScalar sy) override;
void TransformReset() override;
void Transform(const SkMatrix* matrix) override;
void Transform(const SkM44* matrix44) override;
void SetTransform(const SkMatrix* matrix) override;
void SetTransform(const SkM44* matrix44) override;
using DlCanvas::SetTransform;
using DlCanvas::Transform;
SkM44 GetTransformFullPerspective() const override;
SkMatrix GetTransform() const override;
void ClipRect(const SkRect& rect, ClipOp clip_op, bool is_aa) override;
void ClipRRect(const SkRRect& rrect, ClipOp clip_op, bool is_aa) override;
void ClipPath(const SkPath& path, ClipOp clip_op, bool is_aa) override;
SkRect GetDestinationClipBounds() const override;
SkRect GetLocalClipBounds() const override;
bool QuickReject(const SkRect& bounds) const override;
void DrawPaint(const DlPaint& paint) override;
void DrawColor(DlColor color, DlBlendMode mode) override;
void DrawLine(const SkPoint& p0,
const SkPoint& p1,
const DlPaint& paint) override;
void DrawRect(const SkRect& rect, const DlPaint& paint) override;
void DrawOval(const SkRect& bounds, const DlPaint& paint) override;
void DrawCircle(const SkPoint& center,
SkScalar radius,
const DlPaint& paint) override;
void DrawRRect(const SkRRect& rrect, const DlPaint& paint) override;
void DrawDRRect(const SkRRect& outer,
const SkRRect& inner,
const DlPaint& paint) override;
void DrawPath(const SkPath& path, const DlPaint& paint) override;
void DrawArc(const SkRect& bounds,
SkScalar start,
SkScalar sweep,
bool useCenter,
const DlPaint& paint) override;
void DrawPoints(PointMode mode,
uint32_t count,
const SkPoint pts[],
const DlPaint& paint) override;
void DrawVertices(const DlVertices* vertices,
DlBlendMode mode,
const DlPaint& paint) override;
void DrawImage(const sk_sp<DlImage>& image,
const SkPoint point,
DlImageSampling sampling,
const DlPaint* paint = nullptr) override;
void DrawImageRect(
const sk_sp<DlImage>& image,
const SkRect& src,
const SkRect& dst,
DlImageSampling sampling,
const DlPaint* paint = nullptr,
SrcRectConstraint constraint = SrcRectConstraint::kFast) override;
void DrawImageNine(const sk_sp<DlImage>& image,
const SkIRect& center,
const SkRect& dst,
DlFilterMode filter,
const DlPaint* paint = nullptr) override;
void DrawAtlas(const sk_sp<DlImage>& atlas,
const SkRSXform xform[],
const SkRect tex[],
const DlColor colors[],
int count,
DlBlendMode mode,
DlImageSampling sampling,
const SkRect* cullRect,
const DlPaint* paint = nullptr) override;
void DrawDisplayList(const sk_sp<DisplayList> display_list,
SkScalar opacity) override;
void DrawTextBlob(const sk_sp<SkTextBlob>& blob,
SkScalar x,
SkScalar y,
const DlPaint& paint) override;
void DrawTextFrame(const std::shared_ptr<impeller::TextFrame>& text_frame,
SkScalar x,
SkScalar y,
const DlPaint& paint) override;
void DrawShadow(const SkPath& path,
const DlColor color,
const SkScalar elevation,
bool transparent_occluder,
SkScalar dpr) override;
void Flush() override;
private:
DisplayListMatrixClipTracker tracker_;
std::vector<DrawCall> draw_calls_;
int current_layer_;
};
extern bool operator==(const MockCanvas::SaveData& a,
const MockCanvas::SaveData& b);
extern std::ostream& operator<<(std::ostream& os,
const MockCanvas::SaveData& data);
extern bool operator==(const MockCanvas::SaveLayerData& a,
const MockCanvas::SaveLayerData& b);
extern std::ostream& operator<<(std::ostream& os,
const MockCanvas::SaveLayerData& data);
extern bool operator==(const MockCanvas::RestoreData& a,
const MockCanvas::RestoreData& b);
extern std::ostream& operator<<(std::ostream& os,
const MockCanvas::RestoreData& data);
extern bool operator==(const MockCanvas::ConcatMatrixData& a,
const MockCanvas::ConcatMatrixData& b);
extern std::ostream& operator<<(std::ostream& os,
const MockCanvas::ConcatMatrixData& data);
extern bool operator==(const MockCanvas::SetMatrixData& a,
const MockCanvas::SetMatrixData& b);
extern std::ostream& operator<<(std::ostream& os,
const MockCanvas::SetMatrixData& data);
extern bool operator==(const MockCanvas::DrawRectData& a,
const MockCanvas::DrawRectData& b);
extern std::ostream& operator<<(std::ostream& os,
const MockCanvas::DrawRectData& data);
extern bool operator==(const MockCanvas::DrawPathData& a,
const MockCanvas::DrawPathData& b);
extern std::ostream& operator<<(std::ostream& os,
const MockCanvas::DrawPathData& data);
extern bool operator==(const MockCanvas::DrawTextData& a,
const MockCanvas::DrawTextData& b);
extern std::ostream& operator<<(std::ostream& os,
const MockCanvas::DrawTextData& data);
extern bool operator==(const MockCanvas::DrawImageData& a,
const MockCanvas::DrawImageData& b);
extern std::ostream& operator<<(std::ostream& os,
const MockCanvas::DrawImageData& data);
extern bool operator==(const MockCanvas::DrawImageDataNoPaint& a,
const MockCanvas::DrawImageDataNoPaint& b);
extern std::ostream& operator<<(std::ostream& os,
const MockCanvas::DrawImageDataNoPaint& data);
extern bool operator==(const MockCanvas::DrawDisplayListData& a,
const MockCanvas::DrawDisplayListData& b);
extern std::ostream& operator<<(std::ostream& os,
const MockCanvas::DrawDisplayListData& data);
extern bool operator==(const MockCanvas::DrawShadowData& a,
const MockCanvas::DrawShadowData& b);
extern std::ostream& operator<<(std::ostream& os,
const MockCanvas::DrawShadowData& data);
extern bool operator==(const MockCanvas::ClipRectData& a,
const MockCanvas::ClipRectData& b);
extern std::ostream& operator<<(std::ostream& os,
const MockCanvas::ClipRectData& data);
extern bool operator==(const MockCanvas::ClipRRectData& a,
const MockCanvas::ClipRRectData& b);
extern std::ostream& operator<<(std::ostream& os,
const MockCanvas::ClipRRectData& data);
extern bool operator==(const MockCanvas::ClipPathData& a,
const MockCanvas::ClipPathData& b);
extern std::ostream& operator<<(std::ostream& os,
const MockCanvas::ClipPathData& data);
extern std::ostream& operator<<(std::ostream& os,
const MockCanvas::DrawCallData& data);
extern bool operator==(const MockCanvas::DrawCall& a,
const MockCanvas::DrawCall& b);
extern std::ostream& operator<<(std::ostream& os,
const MockCanvas::DrawCall& draw);
extern bool operator==(const MockCanvas::DrawPaintData& a,
const MockCanvas::DrawPaintData& b);
extern std::ostream& operator<<(std::ostream& os,
const MockCanvas::DrawPaintData& data);
} // namespace testing
} // namespace flutter
#endif // TESTING_MOCK_CANVAS_H_