Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Namespaces | Macros | Enumerations
canvas_recorder.h File Reference
#include <cstdint>
#include "impeller/aiks/canvas.h"

Go to the source code of this file.

Namespaces

namespace  impeller
 

Macros

#define FLT_CANVAS_RECORDER_OP_ARG(name)    CanvasRecorderOp::k##name, &Canvas::name
 

Enumerations

enum  impeller::CanvasRecorderOp : uint16_t {
  impeller::kNew , impeller::kSave , impeller::kSaveLayer , impeller::kRestore ,
  impeller::kRestoreToCount , impeller::kResetTransform , impeller::kTransform , impeller::kConcat ,
  impeller::kPreConcat , impeller::kTranslate , impeller::kScale2 , impeller::kScale3 ,
  impeller::kSkew , impeller::kRotate , impeller::kDrawPath , impeller::kDrawPaint ,
  impeller::kDrawLine , impeller::kDrawRect , impeller::kDrawOval , impeller::kDrawRRect ,
  impeller::kDrawCircle , impeller::kDrawPoints , impeller::kDrawImage , impeller::kDrawImageRect ,
  impeller::kClipPath , impeller::kClipRect , impeller::kClipOval , impeller::kClipRRect ,
  impeller::kDrawTextFrame , impeller::kDrawVertices , impeller::kDrawAtlas
}
 

Macro Definition Documentation

◆ FLT_CANVAS_RECORDER_OP_ARG

#define FLT_CANVAS_RECORDER_OP_ARG (   name)     CanvasRecorderOp::k##name, &Canvas::name

Definition at line 12 of file canvas_recorder.h.

14 {
15/// TODO(tbd): These are very similar to `flutter::DisplayListOpType`. When
16/// golden tests can be written at a higher level, migrate these to
17/// flutter::DisplayListOpType.
18enum CanvasRecorderOp : uint16_t {
19 kNew,
20 kSave,
26 kConcat,
29 kScale2,
30 kScale3,
31 kSkew,
32 kRotate,
50};
51
52// Canvas recorder should only be used when IMPELLER_TRACE_CANVAS is defined
53// (never in production code).
54#ifdef IMPELLER_TRACE_CANVAS
55/// Static polymorphic replacement for impeller::Canvas that records methods
56/// called on an impeller::Canvas and forwards it to a real instance.
57/// TODO(https://github.com/flutter/flutter/issues/135718): Move this recorder
58/// to the DisplayList level when golden tests can be written at the ui.Canvas
59/// layer.
60template <typename Serializer>
61class CanvasRecorder {
62 public:
63 CanvasRecorder() : canvas_() { serializer_.Write(CanvasRecorderOp::kNew); }
64
65 explicit CanvasRecorder(Rect cull_rect) : canvas_(cull_rect) {
66 serializer_.Write(CanvasRecorderOp::kNew);
67 }
68
69 explicit CanvasRecorder(IRect cull_rect) : canvas_(cull_rect) {
70 serializer_.Write(CanvasRecorderOp::kNew);
71 }
72
73 ~CanvasRecorder() {}
74
75 const Serializer& GetSerializer() const { return serializer_; }
76
77 template <typename ReturnType>
78 ReturnType ExecuteAndSerialize(CanvasRecorderOp op,
79 ReturnType (Canvas::*canvasMethod)()) {
80 serializer_.Write(op);
81 return (canvas_.*canvasMethod)();
82 }
83
84 template <typename FuncType, typename... Args>
85 auto ExecuteAndSerialize(CanvasRecorderOp op,
86 FuncType canvasMethod,
87 Args&&... args)
88 -> decltype((std::declval<Canvas>().*
89 canvasMethod)(std::forward<Args>(args)...)) {
90 // Serialize each argument
91 (serializer_.Write(args), ...);
92 serializer_.Write(op);
93 return (canvas_.*canvasMethod)(std::forward<Args>(args)...);
94 }
95
96 template <typename FuncType, typename... Args>
97 auto ExecuteAndSkipArgSerialize(CanvasRecorderOp op,
98 FuncType canvasMethod,
99 Args&&... args)
100 -> decltype((std::declval<Canvas>().*
101 canvasMethod)(std::forward<Args>(args)...)) {
102 serializer_.Write(op);
103 return (canvas_.*canvasMethod)(std::forward<Args>(args)...);
104 }
105
106 //////////////////////////////////////////////////////////////////////////////
107 // Canvas Static Polymorphism
108 // ////////////////////////////////////////////////
109 //////////////////////////////////////////////////////////////////////////////
110
111 void Save(uint32_t total_content_depth = Canvas::kMaxDepth) {
112 void (Canvas::*save_method)(uint32_t) = &Canvas::Save;
113 return ExecuteAndSerialize(CanvasRecorderOp::kSave, save_method,
114 total_content_depth);
115 }
116
117 void SaveLayer(
118 const Paint& paint,
119 std::optional<Rect> bounds = std::nullopt,
120 const std::shared_ptr<ImageFilter>& backdrop_filter = nullptr,
121 ContentBoundsPromise bounds_promise = ContentBoundsPromise::kUnknown,
122 uint32_t total_content_depth = Canvas::kMaxDepth) {
123 return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(SaveLayer), paint,
124 bounds, backdrop_filter, bounds_promise,
125 total_content_depth);
126 }
127
128 bool Restore() {
129 return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(Restore));
130 }
131
132 size_t GetSaveCount() const { return canvas_.GetSaveCount(); }
133
134 void RestoreToCount(size_t count) {
135 return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(RestoreToCount),
136 count);
137 }
138
139 const Matrix& GetCurrentTransform() const {
140 return canvas_.GetCurrentTransform();
141 }
142
143 const std::optional<Rect> GetCurrentLocalCullingBounds() const {
144 return canvas_.GetCurrentLocalCullingBounds();
145 }
146
147 void ResetTransform() {
148 return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(ResetTransform));
149 }
150
151 void Transform(const Matrix& transform) {
152 return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(Transform),
153 transform);
154 }
155
156 void Concat(const Matrix& transform) {
157 return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(Concat), transform);
158 }
159
160 void PreConcat(const Matrix& transform) {
161 return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(PreConcat),
162 transform);
163 }
164
165 void Translate(const Vector3& offset) {
166 return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(Translate), offset);
167 }
168
169 void Scale(const Vector2& scale) {
170 return ExecuteAndSerialize(
171 CanvasRecorderOp::kScale2,
172 static_cast<void (Canvas::*)(const Vector2&)>(&Canvas::Scale), scale);
173 }
174
175 void Scale(const Vector3& scale) {
176 return ExecuteAndSerialize(
177 CanvasRecorderOp::kScale3,
178 static_cast<void (Canvas::*)(const Vector3&)>(&Canvas::Scale), scale);
179 }
180
181 void Skew(Scalar sx, Scalar sy) {
182 return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(Skew), sx, sy);
183 }
184
185 void Rotate(Radians radians) {
186 return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(Rotate), radians);
187 }
188
189 void DrawPath(Path path, const Paint& paint) {
190 serializer_.Write(path);
191 serializer_.Write(paint);
192 return ExecuteAndSkipArgSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawPath),
193 std::move(path), paint);
194 }
195
196 void DrawPaint(const Paint& paint) {
197 return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawPaint), paint);
198 }
199
200 void DrawLine(const Point& p0, const Point& p1, const Paint& paint) {
201 return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawLine), p0, p1,
202 paint);
203 }
204
205 void DrawRect(Rect rect, const Paint& paint) {
206 return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawRect), rect,
207 paint);
208 }
209
210 void DrawOval(const Rect& rect, const Paint& paint) {
211 return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawOval), rect,
212 paint);
213 }
214
215 void DrawRRect(const Rect& rect,
216 const Size& corner_radii,
217 const Paint& paint) {
218 return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawRRect), rect,
219 corner_radii, paint);
220 }
221
222 void DrawCircle(Point center, Scalar radius, const Paint& paint) {
223 return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawCircle), center,
224 radius, paint);
225 }
226
227 void DrawPoints(std::vector<Point> points,
228 Scalar radius,
229 const Paint& paint,
230 PointStyle point_style) {
231 return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawPoints), points,
232 radius, paint, point_style);
233 }
234
235 void DrawImage(const std::shared_ptr<Image>& image,
236 Point offset,
237 const Paint& paint,
238 SamplerDescriptor sampler = {}) {
239 return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawImage), image,
240 offset, paint, sampler);
241 }
242
243 void DrawImageRect(
244 const std::shared_ptr<Image>& image,
245 Rect source,
246 Rect dest,
247 const Paint& paint,
248 SamplerDescriptor sampler = {},
249 SourceRectConstraint src_rect_constraint = SourceRectConstraint::kFast) {
250 return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawImageRect), image,
251 source, dest, paint, sampler,
252 src_rect_constraint);
253 }
254
255 void ClipPath(
256 Path path,
257 Entity::ClipOperation clip_op = Entity::ClipOperation::kIntersect) {
258 serializer_.Write(path);
259 serializer_.Write(clip_op);
260 return ExecuteAndSkipArgSerialize(FLT_CANVAS_RECORDER_OP_ARG(ClipPath),
261 std::move(path), clip_op);
262 }
263
264 void ClipRect(
265 const Rect& rect,
266 Entity::ClipOperation clip_op = Entity::ClipOperation::kIntersect) {
267 return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(ClipRect), rect,
268 clip_op);
269 }
270
271 void ClipOval(
272 const Rect& bounds,
273 Entity::ClipOperation clip_op = Entity::ClipOperation::kIntersect) {
274 return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(ClipOval), bounds,
275 clip_op);
276 }
277
278 void ClipRRect(
279 const Rect& rect,
280 const Size& corner_radii,
281 Entity::ClipOperation clip_op = Entity::ClipOperation::kIntersect) {
282 return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(ClipRRect), rect,
283 corner_radii, clip_op);
284 }
285
286 void DrawTextFrame(const std::shared_ptr<TextFrame>& text_frame,
287 Point position,
288 const Paint& paint) {
289 return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawTextFrame),
290 text_frame, position, paint);
291 }
292
293 void DrawVertices(const std::shared_ptr<VerticesGeometry>& vertices,
294 BlendMode blend_mode,
295 const Paint& paint) {
296 return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawVertices),
297 vertices, blend_mode, paint);
298 }
299
300 void DrawAtlas(const std::shared_ptr<Image>& atlas,
301 std::vector<Matrix> transforms,
302 std::vector<Rect> texture_coordinates,
303 std::vector<Color> colors,
304 BlendMode blend_mode,
305 SamplerDescriptor sampler,
306 std::optional<Rect> cull_rect,
307 const Paint& paint) {
308 return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawAtlas), //
309 atlas, //
310 transforms, //
311 texture_coordinates, //
312 colors, //
313 blend_mode, //
314 sampler, //
315 cull_rect, //
316 paint);
317 }
318
319 Picture EndRecordingAsPicture() { return canvas_.EndRecordingAsPicture(); }
320
321 private:
322 Canvas canvas_;
323 Serializer serializer_;
324};
325#endif
326
327} // namespace impeller
328
329#endif // FLUTTER_IMPELLER_AIKS_CANVAS_RECORDER_H_
int count
static const SkRect kDrawRect
static const int points[]
static SkScalar center(float pos0, float pos1)
#define FLT_CANVAS_RECORDER_OP_ARG(name)
const Paint & paint
sk_sp< SkImage > image
Definition examples.cpp:29
SkBitmap source
Definition examples.cpp:28
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
static constexpr char kTransform[]
void DrawImage(SkCanvas *canvas, const SkImage *image, SkScalar x, SkScalar y, const SkSamplingOptions &sampling={}, const SkPaint *paint=nullptr, SkCanvas::SrcRectConstraint constraint=SkCanvas::kFast_SrcRectConstraint)
SK_API void DrawImageRect(SkCanvas *canvas, const SkImage *image, const SkRect &src, const SkRect &dst, const SkSamplingOptions &sampling={}, const SkPaint *paint=nullptr, SkCanvas::SrcRectConstraint constraint=SkCanvas::kFast_SrcRectConstraint)
Point Vector2
Definition point.h:320
SourceRectConstraint
Controls the behavior of the source rectangle given to DrawImageRect.
Definition canvas.h:51
SK_API sk_sp< PrecompileShader > Picture()
skgpu::graphite::DrawAtlas DrawAtlas
skgpu::graphite::Transform Transform
static SkColor4f transform(SkColor4f c, SkColorSpace *src, SkColorSpace *dst)
Definition p3.cpp:47
const Scalar scale
Point offset