Flutter Engine
mock_canvas.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef TESTING_MOCK_CANVAS_H_
6 #define TESTING_MOCK_CANVAS_H_
7 
8 #include <ostream>
9 #include <variant>
10 #include <vector>
11 
12 #include "flutter/testing/assertions_skia.h"
13 #include "gtest/gtest.h"
14 #include "third_party/skia/include/core/SkCanvas.h"
15 #include "third_party/skia/include/core/SkCanvasVirtualEnforcer.h"
16 #include "third_party/skia/include/core/SkClipOp.h"
17 #include "third_party/skia/include/core/SkData.h"
18 #include "third_party/skia/include/core/SkImageFilter.h"
19 #include "third_party/skia/include/core/SkM44.h"
20 #include "third_party/skia/include/core/SkPath.h"
21 #include "third_party/skia/include/core/SkRRect.h"
22 #include "third_party/skia/include/core/SkRect.h"
23 #include "third_party/skia/include/utils/SkNWayCanvas.h"
24 
25 namespace flutter {
26 namespace testing {
27 
28 static constexpr SkRect kEmptyRect = SkRect::MakeEmpty();
29 
30 // Mock |SkCanvas|, useful for writing tests that use Skia but do not interact
31 // with the GPU.
32 //
33 // The |MockCanvas| stores a list of |DrawCall| that the test can later verify
34 // against the expected list of primitives to be drawn.
35 class MockCanvas : public SkCanvasVirtualEnforcer<SkCanvas> {
36  public:
37  using SkCanvas::kHard_ClipEdgeStyle;
38  using SkCanvas::kSoft_ClipEdgeStyle;
39 
40  struct SaveData {
42  };
43 
44  struct SaveLayerData {
45  SkRect save_bounds;
46  SkPaint restore_paint;
47  sk_sp<SkImageFilter> backdrop_filter;
49  };
50 
51  struct RestoreData {
53  };
54 
56  SkM44 matrix;
57  };
58 
59  struct SetMatrixData {
60  SkM44 matrix;
61  };
62 
63  struct DrawRectData {
64  SkRect rect;
65  SkPaint paint;
66  };
67 
68  struct DrawPathData {
69  SkPath path;
70  SkPaint paint;
71  };
72 
73  struct DrawTextData {
74  sk_sp<SkData> serialized_text;
75  SkPaint paint;
76  SkPoint offset;
77  };
78 
79  struct DrawPictureData {
80  sk_sp<SkData> serialized_picture;
81  SkPaint paint;
82  SkMatrix matrix;
83  };
84 
85  struct DrawShadowData {
86  SkPath path;
87  };
88 
89  struct ClipRectData {
90  SkRect rect;
91  SkClipOp clip_op;
92  ClipEdgeStyle style;
93  };
94 
95  struct ClipRRectData {
96  SkRRect rrect;
97  SkClipOp clip_op;
98  ClipEdgeStyle style;
99  };
100 
101  struct ClipPathData {
102  SkPath path;
103  SkClipOp clip_op;
104  ClipEdgeStyle style;
105  };
106 
107  struct DrawPaint {
108  SkPaint paint;
109  };
110 
111  // Discriminated union of all the different |DrawCall| types. It is roughly
112  // equivalent to the different methods in |SkCanvas|' public API.
113  using DrawCallData = std::variant<SaveData,
115  RestoreData,
118  DrawRectData,
119  DrawPathData,
120  DrawTextData,
123  ClipRectData,
125  ClipPathData,
127 
128  // A single call made against this canvas.
129  struct DrawCall {
130  int layer;
132  };
133 
134  MockCanvas();
135  ~MockCanvas() override;
136 
137  SkNWayCanvas* internal_canvas() { return &internal_canvas_; }
138 
139  const std::vector<DrawCall>& draw_calls() const { return draw_calls_; }
140 
141  protected:
142  // Save/restore/set operations that we track.
143  void willSave() override;
144  SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec& rec) override;
145  void willRestore() override;
146  void didRestore() override {}
147  void didConcat44(const SkM44&) override;
148  void didSetM44(const SkM44&) override;
149  void didScale(SkScalar x, SkScalar y) override;
150  void didTranslate(SkScalar x, SkScalar y) override;
151 
152  // Draw and clip operations that we track.
153  void onDrawRect(const SkRect& rect, const SkPaint& paint) override;
154  void onDrawPath(const SkPath& path, const SkPaint& paint) override;
155  void onDrawTextBlob(const SkTextBlob* text,
156  SkScalar x,
157  SkScalar y,
158  const SkPaint& paint) override;
159  void onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) override;
160  void onDrawPicture(const SkPicture* picture,
161  const SkMatrix* matrix,
162  const SkPaint* paint) override;
163  void onClipRect(const SkRect& rect,
164  SkClipOp op,
165  ClipEdgeStyle style) override;
166  void onClipRRect(const SkRRect& rrect,
167  SkClipOp op,
168  ClipEdgeStyle style) override;
169  void onClipPath(const SkPath& path,
170  SkClipOp op,
171  ClipEdgeStyle style) override;
172 
173  // Operations that we don't track.
174  bool onDoSaveBehind(const SkRect*) override;
175  void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
176  void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
177  void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
178  void onDrawPatch(const SkPoint[12],
179  const SkColor[4],
180  const SkPoint[4],
181  SkBlendMode,
182  const SkPaint&) override;
183  void onDrawPaint(const SkPaint&) override;
184  void onDrawBehind(const SkPaint&) override;
185  void onDrawPoints(PointMode,
186  size_t,
187  const SkPoint[],
188  const SkPaint&) override;
189  void onDrawRegion(const SkRegion&, const SkPaint&) override;
190  void onDrawOval(const SkRect&, const SkPaint&) override;
191  void onDrawArc(const SkRect&,
192  SkScalar,
193  SkScalar,
194  bool,
195  const SkPaint&) override;
196  void onDrawRRect(const SkRRect&, const SkPaint&) override;
197  void onDrawImage2(const SkImage* image,
198  SkScalar x,
199  SkScalar y,
200  const SkSamplingOptions&,
201  const SkPaint* paint) override;
202  void onDrawImageRect2(const SkImage*,
203  const SkRect&,
204  const SkRect&,
205  const SkSamplingOptions&,
206  const SkPaint*,
207  SrcRectConstraint) override;
208  void onDrawImageLattice2(const SkImage*,
209  const Lattice&,
210  const SkRect&,
211  SkFilterMode,
212  const SkPaint*) override;
213  void onDrawVerticesObject(const SkVertices*,
214  SkBlendMode,
215  const SkPaint&) override;
216  void onDrawAtlas2(const SkImage*,
217  const SkRSXform[],
218  const SkRect[],
219  const SkColor[],
220  int,
221  SkBlendMode,
222  const SkSamplingOptions&,
223  const SkRect*,
224  const SkPaint*) override;
225  void onDrawEdgeAAQuad(const SkRect&,
226  const SkPoint[4],
227  QuadAAFlags,
228  const SkColor4f&,
229  SkBlendMode) override;
230  void onClipRegion(const SkRegion&, SkClipOp) override;
231 
232  private:
233  SkNWayCanvas internal_canvas_;
234 
235  std::vector<DrawCall> draw_calls_;
236  int current_layer_;
237 };
238 
239 extern bool operator==(const MockCanvas::SaveData& a,
240  const MockCanvas::SaveData& b);
241 extern std::ostream& operator<<(std::ostream& os,
242  const MockCanvas::SaveData& data);
243 extern bool operator==(const MockCanvas::SaveLayerData& a,
244  const MockCanvas::SaveLayerData& b);
245 extern std::ostream& operator<<(std::ostream& os,
246  const MockCanvas::SaveLayerData& data);
247 extern bool operator==(const MockCanvas::RestoreData& a,
248  const MockCanvas::RestoreData& b);
249 extern std::ostream& operator<<(std::ostream& os,
250  const MockCanvas::RestoreData& data);
251 extern bool operator==(const MockCanvas::ConcatMatrixData& a,
253 extern std::ostream& operator<<(std::ostream& os,
254  const MockCanvas::ConcatMatrixData& data);
255 extern bool operator==(const MockCanvas::SetMatrixData& a,
256  const MockCanvas::SetMatrixData& b);
257 extern std::ostream& operator<<(std::ostream& os,
258  const MockCanvas::SetMatrixData& data);
259 extern bool operator==(const MockCanvas::DrawRectData& a,
260  const MockCanvas::DrawRectData& b);
261 extern std::ostream& operator<<(std::ostream& os,
262  const MockCanvas::DrawRectData& data);
263 extern bool operator==(const MockCanvas::DrawPathData& a,
264  const MockCanvas::DrawPathData& b);
265 extern std::ostream& operator<<(std::ostream& os,
266  const MockCanvas::DrawPathData& data);
267 extern bool operator==(const MockCanvas::DrawTextData& a,
268  const MockCanvas::DrawTextData& b);
269 extern std::ostream& operator<<(std::ostream& os,
270  const MockCanvas::DrawTextData& data);
271 extern bool operator==(const MockCanvas::DrawPictureData& a,
272  const MockCanvas::DrawPictureData& b);
273 extern std::ostream& operator<<(std::ostream& os,
274  const MockCanvas::DrawPictureData& data);
275 extern bool operator==(const MockCanvas::DrawShadowData& a,
276  const MockCanvas::DrawShadowData& b);
277 extern std::ostream& operator<<(std::ostream& os,
278  const MockCanvas::DrawShadowData& data);
279 extern bool operator==(const MockCanvas::ClipRectData& a,
280  const MockCanvas::ClipRectData& b);
281 extern std::ostream& operator<<(std::ostream& os,
282  const MockCanvas::ClipRectData& data);
283 extern bool operator==(const MockCanvas::ClipRRectData& a,
284  const MockCanvas::ClipRRectData& b);
285 extern std::ostream& operator<<(std::ostream& os,
286  const MockCanvas::ClipRRectData& data);
287 extern bool operator==(const MockCanvas::ClipPathData& a,
288  const MockCanvas::ClipPathData& b);
289 extern std::ostream& operator<<(std::ostream& os,
290  const MockCanvas::ClipPathData& data);
291 extern std::ostream& operator<<(std::ostream& os,
292  const MockCanvas::DrawCallData& data);
293 extern bool operator==(const MockCanvas::DrawCall& a,
294  const MockCanvas::DrawCall& b);
295 extern std::ostream& operator<<(std::ostream& os,
296  const MockCanvas::DrawCall& draw);
297 extern bool operator==(const MockCanvas::DrawPaint& a,
298  const MockCanvas::DrawPaint& b);
299 extern std::ostream& operator<<(std::ostream& os,
300  const MockCanvas::DrawPaint& data);
301 } // namespace testing
302 } // namespace flutter
303 
304 #endif // TESTING_MOCK_CANVAS_H_
void onDrawRegion(const SkRegion &, const SkPaint &) override
Definition: mock_canvas.cc:205
void onDrawRect(const SkRect &rect, const SkPaint &paint) override
Definition: mock_canvas.cc:98
std::ostream & operator<<(std::ostream &os, const MockTexture::PaintCall &data)
Definition: mock_texture.cc:28
void onClipRegion(const SkRegion &, SkClipOp) override
Definition: mock_canvas.cc:276
void onDrawAnnotation(const SkRect &, const char[], SkData *) override
Definition: mock_canvas.cc:170
void didScale(SkScalar x, SkScalar y) override
Definition: mock_canvas.cc:67
void onDrawShadowRec(const SkPath &path, const SkDrawShadowRec &rec) override
Definition: mock_canvas.cc:106
void onDrawImageLattice2(const SkImage *, const Lattice &, const SkRect &, SkFilterMode, const SkPaint *) override
Definition: mock_canvas.cc:242
void onDrawOval(const SkRect &, const SkPaint &) override
Definition: mock_canvas.cc:209
const std::vector< DrawCall > & draw_calls() const
Definition: mock_canvas.h:139
void onClipRRect(const SkRRect &rrect, SkClipOp op, ClipEdgeStyle style) override
Definition: mock_canvas.cc:147
void onDrawPaint(const SkPaint &) override
Definition: mock_canvas.cc:190
void didRestore() override
Definition: mock_canvas.h:146
void onDrawImageRect2(const SkImage *, const SkRect &, const SkRect &, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint) override
Definition: mock_canvas.cc:233
void onDrawArc(const SkRect &, SkScalar, SkScalar, bool, const SkPaint &) override
Definition: mock_canvas.cc:213
std::variant< SaveData, SaveLayerData, RestoreData, ConcatMatrixData, SetMatrixData, DrawRectData, DrawPathData, DrawTextData, DrawPictureData, DrawShadowData, ClipRectData, ClipRRectData, ClipPathData, DrawPaint > DrawCallData
Definition: mock_canvas.h:126
void onDrawRRect(const SkRRect &, const SkPaint &) override
Definition: mock_canvas.cc:221
void onDrawPoints(PointMode, size_t, const SkPoint[], const SkPaint &) override
Definition: mock_canvas.cc:198
void onDrawDRRect(const SkRRect &, const SkRRect &, const SkPaint &) override
Definition: mock_canvas.cc:174
SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec &rec) override
Definition: mock_canvas.cc:36
void onDrawAtlas2(const SkImage *, const SkRSXform[], const SkRect[], const SkColor[], int, SkBlendMode, const SkSamplingOptions &, const SkRect *, const SkPaint *) override
Definition: mock_canvas.cc:256
void onDrawVerticesObject(const SkVertices *, SkBlendMode, const SkPaint &) override
Definition: mock_canvas.cc:250
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition: switches.h:41
void onDrawPath(const SkPath &path, const SkPaint &paint) override
Definition: mock_canvas.cc:102
void onDrawBehind(const SkPaint &) override
Definition: mock_canvas.cc:194
void onDrawDrawable(SkDrawable *, const SkMatrix *) override
Definition: mock_canvas.cc:178
void onDrawPicture(const SkPicture *picture, const SkMatrix *matrix, const SkPaint *paint) override
Definition: mock_canvas.cc:113
bool operator==(const MockTexture::PaintCall &a, const MockTexture::PaintCall &b)
Definition: mock_texture.cc:21
void didTranslate(SkScalar x, SkScalar y) override
Definition: mock_canvas.cc:71
void onClipPath(const SkPath &path, SkClipOp op, ClipEdgeStyle style) override
Definition: mock_canvas.cc:156
void onDrawEdgeAAQuad(const SkRect &, const SkPoint[4], QuadAAFlags, const SkColor4f &, SkBlendMode) override
Definition: mock_canvas.cc:268
std::u16string text
void onDrawTextBlob(const SkTextBlob *text, SkScalar x, SkScalar y, const SkPaint &paint) override
Definition: mock_canvas.cc:75
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
void didConcat44(const SkM44 &) override
Definition: mock_canvas.cc:59
void didSetM44(const SkM44 &) override
Definition: mock_canvas.cc:63
static constexpr SkRect kEmptyRect
Definition: mock_canvas.h:28
void onDrawPatch(const SkPoint[12], const SkColor[4], const SkPoint[4], SkBlendMode, const SkPaint &) override
Definition: mock_canvas.cc:182
void onClipRect(const SkRect &rect, SkClipOp op, ClipEdgeStyle style) override
Definition: mock_canvas.cc:138
void willRestore() override
Definition: mock_canvas.cc:51
SkNWayCanvas * internal_canvas()
Definition: mock_canvas.h:137
bool onDoSaveBehind(const SkRect *) override
Definition: mock_canvas.cc:165
void onDrawImage2(const SkImage *image, SkScalar x, SkScalar y, const SkSamplingOptions &, const SkPaint *paint) override
Definition: mock_canvas.cc:225