forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_canvas.h
314 lines (282 loc) · 11.4 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
// 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/testing/assertions_skia.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/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 : public SkCanvasVirtualEnforcer<SkCanvas> {
public:
using SkCanvas::kHard_ClipEdgeStyle;
using SkCanvas::kSoft_ClipEdgeStyle;
struct SaveData {
int save_to_layer;
};
struct SaveLayerData {
SkRect save_bounds;
SkPaint restore_paint;
sk_sp<SkImageFilter> backdrop_filter;
int save_to_layer;
};
struct RestoreData {
int restore_to_layer;
};
struct ConcatMatrixData {
SkM44 matrix;
};
struct SetMatrixData {
SkM44 matrix;
};
struct DrawRectData {
SkRect rect;
SkPaint paint;
};
struct DrawPathData {
SkPath path;
SkPaint paint;
};
struct DrawTextData {
sk_sp<SkData> serialized_text;
SkPaint paint;
SkPoint offset;
};
struct DrawPictureData {
sk_sp<SkData> serialized_picture;
SkPaint paint;
SkMatrix matrix;
};
struct DrawShadowData {
SkPath path;
};
struct ClipRectData {
SkRect rect;
SkClipOp clip_op;
ClipEdgeStyle style;
};
struct ClipRRectData {
SkRRect rrect;
SkClipOp clip_op;
ClipEdgeStyle style;
};
struct ClipPathData {
SkPath path;
SkClipOp clip_op;
ClipEdgeStyle style;
};
struct DrawPaint {
SkPaint 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,
DrawPictureData,
DrawShadowData,
ClipRectData,
ClipRRectData,
ClipPathData,
DrawPaint>;
// A single call made against this canvas.
struct DrawCall {
int layer;
DrawCallData data;
};
MockCanvas();
~MockCanvas() override;
SkNWayCanvas* internal_canvas() { return &internal_canvas_; }
const std::vector<DrawCall>& draw_calls() const { return draw_calls_; }
protected:
// Save/restore/set operations that we track.
void willSave() override;
SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec& rec) override;
void willRestore() override;
void didRestore() override {}
#ifdef SK_SUPPORT_LEGACY_CANVASMATRIX33
void didConcat(const SkMatrix& matrix) override;
void didSetMatrix(const SkMatrix& matrix) override;
#endif
void didConcat44(const SkM44&) override;
void didSetM44(const SkM44&) override;
void didScale(SkScalar x, SkScalar y) override;
void didTranslate(SkScalar x, SkScalar y) override;
// Draw and clip operations that we track.
void onDrawRect(const SkRect& rect, const SkPaint& paint) override;
void onDrawPath(const SkPath& path, const SkPaint& paint) override;
void onDrawTextBlob(const SkTextBlob* text,
SkScalar x,
SkScalar y,
const SkPaint& paint) override;
void onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) override;
void onDrawPicture(const SkPicture* picture,
const SkMatrix* matrix,
const SkPaint* paint) override;
void onClipRect(const SkRect& rect,
SkClipOp op,
ClipEdgeStyle style) override;
void onClipRRect(const SkRRect& rrect,
SkClipOp op,
ClipEdgeStyle style) override;
void onClipPath(const SkPath& path,
SkClipOp op,
ClipEdgeStyle style) override;
// Operations that we don't track.
bool onDoSaveBehind(const SkRect*) override;
void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
void onDrawPatch(const SkPoint[12],
const SkColor[4],
const SkPoint[4],
SkBlendMode,
const SkPaint&) override;
void onDrawPaint(const SkPaint&) override;
void onDrawBehind(const SkPaint&) override;
void onDrawPoints(PointMode,
size_t,
const SkPoint[],
const SkPaint&) override;
void onDrawRegion(const SkRegion&, const SkPaint&) override;
void onDrawOval(const SkRect&, const SkPaint&) override;
void onDrawArc(const SkRect&,
SkScalar,
SkScalar,
bool,
const SkPaint&) override;
void onDrawRRect(const SkRRect&, const SkPaint&) override;
void onDrawImage(const SkImage* image,
SkScalar x,
SkScalar y,
const SkPaint* paint) override;
void onDrawImageRect(const SkImage*,
const SkRect*,
const SkRect&,
const SkPaint*,
SrcRectConstraint) override;
void onDrawImageNine(const SkImage*,
const SkIRect&,
const SkRect&,
const SkPaint*) override;
void onDrawImageLattice(const SkImage*,
const Lattice&,
const SkRect&,
const SkPaint*) override;
void onDrawVerticesObject(const SkVertices*,
SkBlendMode,
const SkPaint&) override;
void onDrawAtlas(const SkImage*,
const SkRSXform[],
const SkRect[],
const SkColor[],
int,
SkBlendMode,
const SkRect*,
const SkPaint*) override;
void onDrawEdgeAAQuad(const SkRect&,
const SkPoint[4],
QuadAAFlags,
const SkColor4f&,
SkBlendMode) override;
void onDrawEdgeAAImageSet(const ImageSetEntry[],
int,
const SkPoint[],
const SkMatrix[],
const SkPaint*,
SrcRectConstraint) override;
void onClipRegion(const SkRegion&, SkClipOp) override;
private:
SkNWayCanvas internal_canvas_;
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::DrawPictureData& a,
const MockCanvas::DrawPictureData& b);
extern std::ostream& operator<<(std::ostream& os,
const MockCanvas::DrawPictureData& 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::DrawPaint& a,
const MockCanvas::DrawPaint& b);
extern std::ostream& operator<<(std::ostream& os,
const MockCanvas::DrawPaint& data);
} // namespace testing
} // namespace flutter
#endif // TESTING_MOCK_CANVAS_H_