Flutter Engine
The 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 FLUTTER_TESTING_MOCK_CANVAS_H_
6#define FLUTTER_TESTING_MOCK_CANVAS_H_
7
8#include <ostream>
9#include <variant>
10#include <vector>
11
12#include "flutter/display_list/dl_canvas.h"
13#include "flutter/display_list/utils/dl_matrix_clip_tracker.h"
14#include "gtest/gtest.h"
26
27namespace flutter {
28namespace testing {
29
30static constexpr SkRect kEmptyRect = SkRect::MakeEmpty();
31
32// Mock |SkCanvas|, useful for writing tests that use Skia but do not interact
33// with the GPU.
34//
35// The |MockCanvas| stores a list of |DrawCall| that the test can later verify
36// against the expected list of primitives to be drawn.
37class MockCanvas final : public DlCanvas {
38 public:
42 };
43
44 struct SaveData {
46 };
47
51 std::shared_ptr<DlImageFilter> backdrop_filter;
53 };
54
55 struct RestoreData {
57 };
58
61 };
62
65 };
66
67 struct DrawRectData {
70 };
71
72 struct DrawPathData {
75 };
76
77 struct DrawTextData {
81 };
82
88 };
89
96 };
97
101 };
102
109 };
110
115 };
116
121 };
122
127 };
128
131 };
132
133 // Discriminated union of all the different |DrawCall| types. It is roughly
134 // equivalent to the different methods in |SkCanvas|' public API.
135 using DrawCallData = std::variant<SaveData,
151
152 // A single call made against this canvas.
153 struct DrawCall {
154 int layer;
156 };
157
158 MockCanvas();
159 MockCanvas(int width, int height);
160 ~MockCanvas();
161
162 const std::vector<DrawCall>& draw_calls() const { return draw_calls_; }
163 void reset_draw_calls() { draw_calls_.clear(); }
164
165 SkISize GetBaseLayerSize() const override;
166 SkImageInfo GetImageInfo() const override;
167
168 void Save() override;
169 void SaveLayer(const SkRect* bounds,
170 const DlPaint* paint = nullptr,
171 const DlImageFilter* backdrop = nullptr) override;
172 void Restore() override;
173 int GetSaveCount() const { return current_layer_; }
174 void RestoreToCount(int restore_count) {
175 while (current_layer_ > restore_count) {
176 Restore();
177 }
178 }
179
180 // clang-format off
181
182 // 2x3 2D affine subset of a 4x4 transform in row major order
183 void Transform2DAffine(SkScalar mxx, SkScalar mxy, SkScalar mxt,
184 SkScalar myx, SkScalar myy, SkScalar myt) override;
185 // full 4x4 transform in row major order
187 SkScalar mxx, SkScalar mxy, SkScalar mxz, SkScalar mxt,
188 SkScalar myx, SkScalar myy, SkScalar myz, SkScalar myt,
189 SkScalar mzx, SkScalar mzy, SkScalar mzz, SkScalar mzt,
190 SkScalar mwx, SkScalar mwy, SkScalar mwz, SkScalar mwt) override;
191 // clang-format on
192
193 void Translate(SkScalar tx, SkScalar ty) override;
194 void Scale(SkScalar sx, SkScalar sy) override;
195 void Rotate(SkScalar degrees) override;
196 void Skew(SkScalar sx, SkScalar sy) override;
197 void TransformReset() override;
198 void Transform(const SkMatrix* matrix) override;
199 void Transform(const SkM44* matrix44) override;
200 void SetTransform(const SkMatrix* matrix) override;
201 void SetTransform(const SkM44* matrix44) override;
204
205 SkM44 GetTransformFullPerspective() const override;
206 SkMatrix GetTransform() const override;
207
208 void ClipRect(const SkRect& rect, ClipOp clip_op, bool is_aa) override;
209 void ClipRRect(const SkRRect& rrect, ClipOp clip_op, bool is_aa) override;
210 void ClipPath(const SkPath& path, ClipOp clip_op, bool is_aa) override;
211
212 SkRect GetDestinationClipBounds() const override;
213 SkRect GetLocalClipBounds() const override;
214 bool QuickReject(const SkRect& bounds) const override;
215
216 void DrawPaint(const DlPaint& paint) override;
217 void DrawColor(DlColor color, DlBlendMode mode) override;
218 void DrawLine(const SkPoint& p0,
219 const SkPoint& p1,
220 const DlPaint& paint) override;
221 void DrawDashedLine(const DlPoint& p0,
222 const DlPoint& p1,
223 DlScalar on_length,
224 DlScalar off_length,
225 const DlPaint& paint) override;
226 void DrawRect(const SkRect& rect, const DlPaint& paint) override;
227 void DrawOval(const SkRect& bounds, const DlPaint& paint) override;
228 void DrawCircle(const SkPoint& center,
229 SkScalar radius,
230 const DlPaint& paint) override;
231 void DrawRRect(const SkRRect& rrect, const DlPaint& paint) override;
232 void DrawDRRect(const SkRRect& outer,
233 const SkRRect& inner,
234 const DlPaint& paint) override;
235 void DrawPath(const SkPath& path, const DlPaint& paint) override;
236 void DrawArc(const SkRect& bounds,
237 SkScalar start,
238 SkScalar sweep,
239 bool useCenter,
240 const DlPaint& paint) override;
242 uint32_t count,
243 const SkPoint pts[],
244 const DlPaint& paint) override;
245 void DrawVertices(const DlVertices* vertices,
247 const DlPaint& paint) override;
248
249 void DrawImage(const sk_sp<DlImage>& image,
250 const SkPoint point,
252 const DlPaint* paint = nullptr) override;
253 void DrawImageRect(
254 const sk_sp<DlImage>& image,
255 const SkRect& src,
256 const SkRect& dst,
258 const DlPaint* paint = nullptr,
259 SrcRectConstraint constraint = SrcRectConstraint::kFast) override;
261 const SkIRect& center,
262 const SkRect& dst,
263 DlFilterMode filter,
264 const DlPaint* paint = nullptr) override;
265 void DrawAtlas(const sk_sp<DlImage>& atlas,
266 const SkRSXform xform[],
267 const SkRect tex[],
268 const DlColor colors[],
269 int count,
272 const SkRect* cullRect,
273 const DlPaint* paint = nullptr) override;
274
275 void DrawDisplayList(const sk_sp<DisplayList> display_list,
276 SkScalar opacity) override;
277 void DrawTextBlob(const sk_sp<SkTextBlob>& blob,
278 SkScalar x,
279 SkScalar y,
280 const DlPaint& paint) override;
281 void DrawTextFrame(const std::shared_ptr<impeller::TextFrame>& text_frame,
282 SkScalar x,
283 SkScalar y,
284 const DlPaint& paint) override;
285 void DrawShadow(const SkPath& path,
286 const DlColor color,
287 const SkScalar elevation,
288 bool transparent_occluder,
289 SkScalar dpr) override;
290
291 void Flush() override;
292
293 private:
294 SkISize base_layer_size_;
295 std::vector<DisplayListMatrixClipState> state_stack_;
296 std::vector<DrawCall> draw_calls_;
297 int current_layer_;
298};
299
300extern bool operator==(const MockCanvas::SaveData& a,
301 const MockCanvas::SaveData& b);
302extern std::ostream& operator<<(std::ostream& os,
303 const MockCanvas::SaveData& data);
304extern bool operator==(const MockCanvas::SaveLayerData& a,
305 const MockCanvas::SaveLayerData& b);
306extern std::ostream& operator<<(std::ostream& os,
307 const MockCanvas::SaveLayerData& data);
308extern bool operator==(const MockCanvas::RestoreData& a,
309 const MockCanvas::RestoreData& b);
310extern std::ostream& operator<<(std::ostream& os,
311 const MockCanvas::RestoreData& data);
312extern bool operator==(const MockCanvas::ConcatMatrixData& a,
313 const MockCanvas::ConcatMatrixData& b);
314extern std::ostream& operator<<(std::ostream& os,
315 const MockCanvas::ConcatMatrixData& data);
316extern bool operator==(const MockCanvas::SetMatrixData& a,
317 const MockCanvas::SetMatrixData& b);
318extern std::ostream& operator<<(std::ostream& os,
319 const MockCanvas::SetMatrixData& data);
320extern bool operator==(const MockCanvas::DrawRectData& a,
321 const MockCanvas::DrawRectData& b);
322extern std::ostream& operator<<(std::ostream& os,
323 const MockCanvas::DrawRectData& data);
324extern bool operator==(const MockCanvas::DrawPathData& a,
325 const MockCanvas::DrawPathData& b);
326extern std::ostream& operator<<(std::ostream& os,
327 const MockCanvas::DrawPathData& data);
328extern bool operator==(const MockCanvas::DrawTextData& a,
329 const MockCanvas::DrawTextData& b);
330extern std::ostream& operator<<(std::ostream& os,
331 const MockCanvas::DrawTextData& data);
332extern bool operator==(const MockCanvas::DrawImageData& a,
333 const MockCanvas::DrawImageData& b);
334extern std::ostream& operator<<(std::ostream& os,
335 const MockCanvas::DrawImageData& data);
336extern bool operator==(const MockCanvas::DrawImageDataNoPaint& a,
337 const MockCanvas::DrawImageDataNoPaint& b);
338extern std::ostream& operator<<(std::ostream& os,
339 const MockCanvas::DrawImageDataNoPaint& data);
340extern bool operator==(const MockCanvas::DrawDisplayListData& a,
341 const MockCanvas::DrawDisplayListData& b);
342extern std::ostream& operator<<(std::ostream& os,
343 const MockCanvas::DrawDisplayListData& data);
344extern bool operator==(const MockCanvas::DrawShadowData& a,
345 const MockCanvas::DrawShadowData& b);
346extern std::ostream& operator<<(std::ostream& os,
347 const MockCanvas::DrawShadowData& data);
348extern bool operator==(const MockCanvas::ClipRectData& a,
349 const MockCanvas::ClipRectData& b);
350extern std::ostream& operator<<(std::ostream& os,
351 const MockCanvas::ClipRectData& data);
352extern bool operator==(const MockCanvas::ClipRRectData& a,
353 const MockCanvas::ClipRRectData& b);
354extern std::ostream& operator<<(std::ostream& os,
355 const MockCanvas::ClipRRectData& data);
356extern bool operator==(const MockCanvas::ClipPathData& a,
357 const MockCanvas::ClipPathData& b);
358extern std::ostream& operator<<(std::ostream& os,
359 const MockCanvas::ClipPathData& data);
360extern std::ostream& operator<<(std::ostream& os,
362extern bool operator==(const MockCanvas::DrawCall& a,
363 const MockCanvas::DrawCall& b);
364extern std::ostream& operator<<(std::ostream& os,
365 const MockCanvas::DrawCall& draw);
366extern bool operator==(const MockCanvas::DrawPaintData& a,
367 const MockCanvas::DrawPaintData& b);
368extern std::ostream& operator<<(std::ostream& os,
369 const MockCanvas::DrawPaintData& data);
370} // namespace testing
371} // namespace flutter
372
373#endif // FLUTTER_TESTING_MOCK_CANVAS_H_
int count
Definition: FontMgrTest.cpp:50
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition: aaclip.cpp:27
Definition: SkM44.h:150
Definition: SkPath.h:59
Developer-facing API for rendering anything within the engine.
Definition: dl_canvas.h:38
virtual void Transform(const SkMatrix *matrix)=0
virtual void SetTransform(const SkMatrix *matrix)=0
Holds all of the data (both required and optional) for a DisplayList drawVertices call.
Definition: dl_vertices.h:71
SkM44 GetTransformFullPerspective() const override
Definition: mock_canvas.cc:134
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
Definition: mock_canvas.cc:78
void RestoreToCount(int restore_count)
Definition: mock_canvas.h:174
void Translate(SkScalar tx, SkScalar ty) override
Definition: mock_canvas.cc:118
void DrawShadow(const SkPath &path, const DlColor color, const SkScalar elevation, bool transparent_occluder, SkScalar dpr) override
Definition: mock_canvas.cc:179
void SetTransform(const SkMatrix *matrix) override
Definition: mock_canvas.cc:102
void DrawCircle(const SkPoint &center, SkScalar radius, const DlPaint &paint) override
Definition: mock_canvas.cc:280
SkMatrix GetTransform() const override
Definition: mock_canvas.cc:138
void Transform2DAffine(SkScalar mxx, SkScalar mxy, SkScalar mxt, SkScalar myx, SkScalar myy, SkScalar myt) override
Definition: mock_canvas.cc:72
void DrawTextBlob(const sk_sp< SkTextBlob > &blob, SkScalar x, SkScalar y, const DlPaint &paint) override
Definition: mock_canvas.cc:142
SkRect GetLocalClipBounds() const override
Definition: mock_canvas.cc:235
SkRect GetDestinationClipBounds() const override
Definition: mock_canvas.cc:231
SkISize GetBaseLayerSize() const override
Definition: mock_canvas.cc:30
const std::vector< DrawCall > & draw_calls() const
Definition: mock_canvas.h:162
void DrawImageNine(const sk_sp< DlImage > &image, const SkIRect &center, const SkRect &dst, DlFilterMode filter, const DlPaint *paint=nullptr) override
Definition: mock_canvas.cc:307
SkImageInfo GetImageInfo() const override
Definition: mock_canvas.cc:34
void ClipRRect(const SkRRect &rrect, ClipOp clip_op, bool is_aa) override
Definition: mock_canvas.cc:217
void Transform(const SkMatrix *matrix) override
Definition: mock_canvas.cc:91
void DrawPaint(const DlPaint &paint) override
Definition: mock_canvas.cc:247
void DrawLine(const SkPoint &p0, const SkPoint &p1, const DlPaint &paint) override
Definition: mock_canvas.cc:255
void DrawImage(const sk_sp< DlImage > &image, const SkPoint point, DlImageSampling sampling, const DlPaint *paint=nullptr) override
Definition: mock_canvas.cc:189
void Scale(SkScalar sx, SkScalar sy) override
Definition: mock_canvas.cc:122
void Rotate(SkScalar degrees) override
Definition: mock_canvas.cc:126
void ClipPath(const SkPath &path, ClipOp clip_op, bool is_aa) override
Definition: mock_canvas.cc:224
void ClipRect(const SkRect &rect, ClipOp clip_op, bool is_aa) override
Definition: mock_canvas.cc:210
void DrawImageRect(const sk_sp< DlImage > &image, const SkRect &src, const SkRect &dst, DlImageSampling sampling, const DlPaint *paint=nullptr, SrcRectConstraint constraint=SrcRectConstraint::kFast) override
Definition: mock_canvas.cc:298
void DrawColor(DlColor color, DlBlendMode mode) override
Definition: mock_canvas.cc:251
void TransformReset() override
Definition: mock_canvas.cc:113
void DrawVertices(const DlVertices *vertices, DlBlendMode mode, const DlPaint &paint) override
Definition: mock_canvas.cc:315
std::variant< SaveData, SaveLayerData, RestoreData, ConcatMatrixData, SetMatrixData, DrawRectData, DrawPathData, DrawTextData, DrawImageDataNoPaint, DrawImageData, DrawDisplayListData, DrawShadowData, ClipRectData, ClipRRectData, ClipPathData, DrawPaintData > DrawCallData
Definition: mock_canvas.h:150
void DrawRect(const SkRect &rect, const DlPaint &paint) override
Definition: mock_canvas.cc:171
void DrawPath(const SkPath &path, const DlPaint &paint) override
Definition: mock_canvas.cc:175
void DrawRRect(const SkRRect &rrect, const DlPaint &paint) override
Definition: mock_canvas.cc:294
void DrawPoints(PointMode mode, uint32_t count, const SkPoint pts[], const DlPaint &paint) override
Definition: mock_canvas.cc:269
void DrawDisplayList(const sk_sp< DisplayList > display_list, SkScalar opacity) override
Definition: mock_canvas.cc:204
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
Definition: mock_canvas.cc:319
void DrawTextFrame(const std::shared_ptr< impeller::TextFrame > &text_frame, SkScalar x, SkScalar y, const DlPaint &paint) override
Definition: mock_canvas.cc:163
void SaveLayer(const SkRect *bounds, const DlPaint *paint=nullptr, const DlImageFilter *backdrop=nullptr) override
Definition: mock_canvas.cc:46
void Skew(SkScalar sx, SkScalar sy) override
Definition: mock_canvas.cc:130
void DrawDRRect(const SkRRect &outer, const SkRRect &inner, const DlPaint &paint) override
Definition: mock_canvas.cc:243
bool QuickReject(const SkRect &bounds) const override
Definition: mock_canvas.cc:239
void DrawOval(const SkRect &bounds, const DlPaint &paint) override
Definition: mock_canvas.cc:276
void DrawDashedLine(const DlPoint &p0, const DlPoint &p1, DlScalar on_length, DlScalar off_length, const DlPaint &paint) override
Definition: mock_canvas.cc:261
void DrawArc(const SkRect &bounds, SkScalar start, SkScalar sweep, bool useCenter, const DlPaint &paint) override
Definition: mock_canvas.cc:286
const Paint & paint
Definition: color_source.cc:38
DlColor color
float SkScalar
Definition: extension.cpp:12
static bool b
struct MyStruct a[10]
double y
double x
sk_sp< const SkImage > atlas
Definition: SkRecords.h:331
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
Optional< SkRect > bounds
Definition: SkRecords.h:189
sk_sp< const SkImage > image
Definition: SkRecords.h:269
sk_sp< const SkImageFilter > backdrop
Definition: SkRecords.h:191
SkRRect rrect
Definition: SkRecords.h:232
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
PODArray< SkColor > colors
Definition: SkRecords.h:276
SkSamplingOptions sampling
Definition: SkRecords.h:337
static std::ostream & operator<<(std::ostream &os, const SkPoint &r)
Definition: mock_canvas.cc:342
bool operator==(const CGRect &lhs, const CGRect &rhs)
static constexpr SkRect kEmptyRect
Definition: mock_canvas.h:30
impeller::Scalar DlScalar
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
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
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode
Definition: switches.h:228
dst
Definition: cp.py:12
int32_t height
int32_t width
Definition: SkRect.h:32
Definition: SkSize.h:16
static constexpr SkRect MakeEmpty()
Definition: SkRect.h:595
std::shared_ptr< DlImageFilter > backdrop_filter
Definition: mock_canvas.h:51