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  SkMatrix matrix;
57  };
58 
60  SkM44 matrix;
61  };
62 
63  struct SetMatrixData {
64  SkMatrix matrix;
65  };
66 
67  struct DrawRectData {
68  SkRect rect;
69  SkPaint paint;
70  };
71 
72  struct DrawPathData {
73  SkPath path;
74  SkPaint paint;
75  };
76 
77  struct DrawTextData {
78  sk_sp<SkData> serialized_text;
79  SkPaint paint;
80  SkPoint offset;
81  };
82 
83  struct DrawPictureData {
84  sk_sp<SkData> serialized_picture;
85  SkPaint paint;
86  SkMatrix matrix;
87  };
88 
89  struct DrawShadowData {
90  SkPath path;
91  };
92 
93  struct ClipRectData {
94  SkRect rect;
95  SkClipOp clip_op;
96  ClipEdgeStyle style;
97  };
98 
99  struct ClipRRectData {
100  SkRRect rrect;
101  SkClipOp clip_op;
102  ClipEdgeStyle style;
103  };
104 
105  struct ClipPathData {
106  SkPath path;
107  SkClipOp clip_op;
108  ClipEdgeStyle style;
109  };
110 
111  struct DrawPaint {
112  SkPaint paint;
113  };
114 
115  // Discriminated union of all the different |DrawCall| types. It is roughly
116  // equivalent to the different methods in |SkCanvas|' public API.
117  using DrawCallData = std::variant<SaveData,
119  RestoreData,
123  DrawRectData,
124  DrawPathData,
125  DrawTextData,
128  ClipRectData,
130  ClipPathData,
132 
133  // A single call made against this canvas.
134  struct DrawCall {
135  int layer;
137  };
138 
139  MockCanvas();
140  ~MockCanvas() override;
141 
142  SkNWayCanvas* internal_canvas() { return &internal_canvas_; }
143 
144  const std::vector<DrawCall>& draw_calls() const { return draw_calls_; }
145 
146  protected:
147  // Save/restore/set operations that we track.
148  void willSave() override;
149  SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec& rec) override;
150  void willRestore() override;
151  void didRestore() override {}
152  void didConcat(const SkMatrix& matrix) override;
153  void didConcat44(const SkM44&) override;
154  void didScale(SkScalar x, SkScalar y) override;
155  void didTranslate(SkScalar x, SkScalar y) override;
156  void didSetMatrix(const SkMatrix& matrix) override;
157 
158  // Draw and clip operations that we track.
159  void onDrawRect(const SkRect& rect, const SkPaint& paint) override;
160  void onDrawPath(const SkPath& path, const SkPaint& paint) override;
161  void onDrawTextBlob(const SkTextBlob* text,
162  SkScalar x,
163  SkScalar y,
164  const SkPaint& paint) override;
165  void onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) override;
166  void onDrawPicture(const SkPicture* picture,
167  const SkMatrix* matrix,
168  const SkPaint* paint) override;
169  void onClipRect(const SkRect& rect,
170  SkClipOp op,
171  ClipEdgeStyle style) override;
172  void onClipRRect(const SkRRect& rrect,
173  SkClipOp op,
174  ClipEdgeStyle style) override;
175  void onClipPath(const SkPath& path,
176  SkClipOp op,
177  ClipEdgeStyle style) override;
178 
179  // Operations that we don't track.
180  bool onDoSaveBehind(const SkRect*) override;
181  void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
182  void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
183  void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
184  void onDrawPatch(const SkPoint[12],
185  const SkColor[4],
186  const SkPoint[4],
187  SkBlendMode,
188  const SkPaint&) override;
189  void onDrawPaint(const SkPaint&) override;
190  void onDrawBehind(const SkPaint&) override;
191  void onDrawPoints(PointMode,
192  size_t,
193  const SkPoint[],
194  const SkPaint&) override;
195  void onDrawRegion(const SkRegion&, const SkPaint&) override;
196  void onDrawOval(const SkRect&, const SkPaint&) override;
197  void onDrawArc(const SkRect&,
198  SkScalar,
199  SkScalar,
200  bool,
201  const SkPaint&) override;
202  void onDrawRRect(const SkRRect&, const SkPaint&) override;
203  void onDrawImage(const SkImage* image,
204  SkScalar x,
205  SkScalar y,
206  const SkPaint* paint) override;
207  void onDrawImageRect(const SkImage*,
208  const SkRect*,
209  const SkRect&,
210  const SkPaint*,
211  SrcRectConstraint) override;
212  void onDrawImageNine(const SkImage*,
213  const SkIRect&,
214  const SkRect&,
215  const SkPaint*) override;
216  void onDrawImageLattice(const SkImage*,
217  const Lattice&,
218  const SkRect&,
219  const SkPaint*) override;
220  void onDrawVerticesObject(const SkVertices*,
221  SkBlendMode,
222  const SkPaint&) override;
223  void onDrawAtlas(const SkImage*,
224  const SkRSXform[],
225  const SkRect[],
226  const SkColor[],
227  int,
228  SkBlendMode,
229  const SkRect*,
230  const SkPaint*) override;
231  void onDrawEdgeAAQuad(const SkRect&,
232  const SkPoint[4],
233  QuadAAFlags,
234  const SkColor4f&,
235  SkBlendMode) override;
236  void onDrawEdgeAAImageSet(const ImageSetEntry[],
237  int,
238  const SkPoint[],
239  const SkMatrix[],
240  const SkPaint*,
241  SrcRectConstraint) override;
242  void onClipRegion(const SkRegion&, SkClipOp) override;
243 
244  private:
245  SkNWayCanvas internal_canvas_;
246 
247  std::vector<DrawCall> draw_calls_;
248  int current_layer_;
249 };
250 
251 extern bool operator==(const MockCanvas::SaveData& a,
252  const MockCanvas::SaveData& b);
253 extern std::ostream& operator<<(std::ostream& os,
254  const MockCanvas::SaveData& data);
255 extern bool operator==(const MockCanvas::SaveLayerData& a,
256  const MockCanvas::SaveLayerData& b);
257 extern std::ostream& operator<<(std::ostream& os,
258  const MockCanvas::SaveLayerData& data);
259 extern bool operator==(const MockCanvas::RestoreData& a,
260  const MockCanvas::RestoreData& b);
261 extern std::ostream& operator<<(std::ostream& os,
262  const MockCanvas::RestoreData& data);
263 extern bool operator==(const MockCanvas::ConcatMatrixData& a,
265 extern std::ostream& operator<<(std::ostream& os,
266  const MockCanvas::ConcatMatrixData& data);
267 extern bool operator==(const MockCanvas::ConcatMatrix44Data& a,
269 extern std::ostream& operator<<(std::ostream& os,
270  const MockCanvas::ConcatMatrix44Data& data);
271 extern bool operator==(const MockCanvas::SetMatrixData& a,
272  const MockCanvas::SetMatrixData& b);
273 extern std::ostream& operator<<(std::ostream& os,
274  const MockCanvas::SetMatrixData& data);
275 extern bool operator==(const MockCanvas::DrawRectData& a,
276  const MockCanvas::DrawRectData& b);
277 extern std::ostream& operator<<(std::ostream& os,
278  const MockCanvas::DrawRectData& data);
279 extern bool operator==(const MockCanvas::DrawPathData& a,
280  const MockCanvas::DrawPathData& b);
281 extern std::ostream& operator<<(std::ostream& os,
282  const MockCanvas::DrawPathData& data);
283 extern bool operator==(const MockCanvas::DrawTextData& a,
284  const MockCanvas::DrawTextData& b);
285 extern std::ostream& operator<<(std::ostream& os,
286  const MockCanvas::DrawTextData& data);
287 extern bool operator==(const MockCanvas::DrawPictureData& a,
288  const MockCanvas::DrawPictureData& b);
289 extern std::ostream& operator<<(std::ostream& os,
290  const MockCanvas::DrawPictureData& data);
291 extern bool operator==(const MockCanvas::DrawShadowData& a,
292  const MockCanvas::DrawShadowData& b);
293 extern std::ostream& operator<<(std::ostream& os,
294  const MockCanvas::DrawShadowData& data);
295 extern bool operator==(const MockCanvas::ClipRectData& a,
296  const MockCanvas::ClipRectData& b);
297 extern std::ostream& operator<<(std::ostream& os,
298  const MockCanvas::ClipRectData& data);
299 extern bool operator==(const MockCanvas::ClipRRectData& a,
300  const MockCanvas::ClipRRectData& b);
301 extern std::ostream& operator<<(std::ostream& os,
302  const MockCanvas::ClipRRectData& data);
303 extern bool operator==(const MockCanvas::ClipPathData& a,
304  const MockCanvas::ClipPathData& b);
305 extern std::ostream& operator<<(std::ostream& os,
306  const MockCanvas::ClipPathData& data);
307 extern std::ostream& operator<<(std::ostream& os,
308  const MockCanvas::DrawCallData& data);
309 extern bool operator==(const MockCanvas::DrawCall& a,
310  const MockCanvas::DrawCall& b);
311 extern std::ostream& operator<<(std::ostream& os,
312  const MockCanvas::DrawCall& draw);
313 extern bool operator==(const MockCanvas::DrawPaint& a,
314  const MockCanvas::DrawPaint& b);
315 extern std::ostream& operator<<(std::ostream& os,
316  const MockCanvas::DrawPaint& data);
317 } // namespace testing
318 } // namespace flutter
319 
320 #endif // TESTING_MOCK_CANVAS_H_
std::variant< SaveData, SaveLayerData, RestoreData, ConcatMatrixData, ConcatMatrix44Data, SetMatrixData, DrawRectData, DrawPathData, DrawTextData, DrawPictureData, DrawShadowData, ClipRectData, ClipRRectData, ClipPathData, DrawPaint > DrawCallData
Definition: mock_canvas.h:131
void onDrawRegion(const SkRegion &, const SkPaint &) override
Definition: mock_canvas.cc:207
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32
void onDrawRect(const SkRect &rect, const SkPaint &paint) override
Definition: mock_canvas.cc:107
std::ostream & operator<<(std::ostream &os, const MockTexture::PaintCall &data)
Definition: mock_texture.cc:28
void onDrawAtlas(const SkImage *, const SkRSXform[], const SkRect[], const SkColor[], int, SkBlendMode, const SkRect *, const SkPaint *) override
Definition: mock_canvas.cc:262
void onDrawImageRect(const SkImage *, const SkRect *, const SkRect &, const SkPaint *, SrcRectConstraint) override
Definition: mock_canvas.cc:234
void onClipRegion(const SkRegion &, SkClipOp) override
Definition: mock_canvas.cc:290
void onDrawAnnotation(const SkRect &, const char[], SkData *) override
Definition: mock_canvas.cc:172
void didScale(SkScalar x, SkScalar y) override
Definition: mock_canvas.cc:68
void onDrawShadowRec(const SkPath &path, const SkDrawShadowRec &rec) override
Definition: mock_canvas.cc:115
void onDrawOval(const SkRect &, const SkPaint &) override
Definition: mock_canvas.cc:211
const std::vector< DrawCall > & draw_calls() const
Definition: mock_canvas.h:144
void onClipRRect(const SkRRect &rrect, SkClipOp op, ClipEdgeStyle style) override
Definition: mock_canvas.cc:153
void onDrawPaint(const SkPaint &) override
Definition: mock_canvas.cc:192
void didRestore() override
Definition: mock_canvas.h:151
void onDrawArc(const SkRect &, SkScalar, SkScalar, bool, const SkPaint &) override
Definition: mock_canvas.cc:215
void onDrawRRect(const SkRRect &, const SkPaint &) override
Definition: mock_canvas.cc:223
void onDrawPoints(PointMode, size_t, const SkPoint[], const SkPaint &) override
Definition: mock_canvas.cc:200
void onDrawDRRect(const SkRRect &, const SkRRect &, const SkPaint &) override
Definition: mock_canvas.cc:176
SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec &rec) override
Definition: mock_canvas.cc:36
void onDrawEdgeAAImageSet(const ImageSetEntry[], int, const SkPoint[], const SkMatrix[], const SkPaint *, SrcRectConstraint) override
Definition: mock_canvas.cc:281
void onDrawVerticesObject(const SkVertices *, SkBlendMode, const SkPaint &) override
Definition: mock_canvas.cc:256
void onDrawPath(const SkPath &path, const SkPaint &paint) override
Definition: mock_canvas.cc:111
void onDrawBehind(const SkPaint &) override
Definition: mock_canvas.cc:196
void onDrawDrawable(SkDrawable *, const SkMatrix *) override
Definition: mock_canvas.cc:180
void onDrawPicture(const SkPicture *picture, const SkMatrix *matrix, const SkPaint *paint) override
Definition: mock_canvas.cc:121
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:74
void onClipPath(const SkPath &path, SkClipOp op, ClipEdgeStyle style) override
Definition: mock_canvas.cc:160
void onDrawEdgeAAQuad(const SkRect &, const SkPoint[4], QuadAAFlags, const SkColor4f &, SkBlendMode) override
Definition: mock_canvas.cc:273
void onDrawImageLattice(const SkImage *, const Lattice &, const SkRect &, const SkPaint *) override
Definition: mock_canvas.cc:249
void onDrawImageNine(const SkImage *, const SkIRect &, const SkRect &, const SkPaint *) override
Definition: mock_canvas.cc:242
void onDrawTextBlob(const SkTextBlob *text, SkScalar x, SkScalar y, const SkPaint &paint) override
Definition: mock_canvas.cc:84
void didConcat(const SkMatrix &matrix) override
Definition: mock_canvas.cc:59
void didConcat44(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:184
void didSetMatrix(const SkMatrix &matrix) override
Definition: mock_canvas.cc:80
void onClipRect(const SkRect &rect, SkClipOp op, ClipEdgeStyle style) override
Definition: mock_canvas.cc:146
void willRestore() override
Definition: mock_canvas.cc:51
SkNWayCanvas * internal_canvas()
Definition: mock_canvas.h:142
bool onDoSaveBehind(const SkRect *) override
Definition: mock_canvas.cc:167
void onDrawImage(const SkImage *image, SkScalar x, SkScalar y, const SkPaint *paint) override
Definition: mock_canvas.cc:227