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