forked from flutter-team-archive/engine
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcanvas_recorder.h
More file actions
331 lines (279 loc) · 10.8 KB
/
Copy pathcanvas_recorder.h
File metadata and controls
331 lines (279 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_IMPELLER_AIKS_CANVAS_RECORDER_H_
#define FLUTTER_IMPELLER_AIKS_CANVAS_RECORDER_H_
#include <cstdint>
#include "impeller/aiks/canvas.h"
#define FLT_CANVAS_RECORDER_OP_ARG(name) \
CanvasRecorderOp::k##name, &Canvas::name
namespace impeller {
/// TODO(tbd): These are very similar to `flutter::DisplayListOpType`. When
/// golden tests can be written at a higher level, migrate these to
/// flutter::DisplayListOpType.
enum CanvasRecorderOp : uint16_t {
kNew,
kSave,
kSaveLayer,
kRestore,
kRestoreToCount,
kResetTransform,
kTransform,
kConcat,
kPreConcat,
kTranslate,
kScale2,
kScale3,
kSkew,
kRotate,
kDrawPath,
kDrawPaint,
kDrawLine,
kDrawRect,
kDrawOval,
kDrawRRect,
kDrawCircle,
kDrawPoints,
kDrawImage,
kDrawImageRect,
kClipPath,
kClipRect,
kClipOval,
kClipRRect,
kDrawPicture,
kDrawTextFrame,
kDrawVertices,
kDrawAtlas,
};
// Canvas recorder should only be used when IMPELLER_TRACE_CANVAS is defined
// (never in production code).
#ifdef IMPELLER_TRACE_CANVAS
/// Static polymorphic replacement for impeller::Canvas that records methods
/// called on an impeller::Canvas and forwards it to a real instance.
/// TODO(https://github.com/flutter/flutter/issues/135718): Move this recorder
/// to the DisplayList level when golden tests can be written at the ui.Canvas
/// layer.
template <typename Serializer>
class CanvasRecorder {
public:
CanvasRecorder() : canvas_() { serializer_.Write(CanvasRecorderOp::kNew); }
explicit CanvasRecorder(Rect cull_rect) : canvas_(cull_rect) {
serializer_.Write(CanvasRecorderOp::kNew);
}
explicit CanvasRecorder(IRect cull_rect) : canvas_(cull_rect) {
serializer_.Write(CanvasRecorderOp::kNew);
}
~CanvasRecorder() {}
const Serializer& GetSerializer() const { return serializer_; }
template <typename ReturnType>
ReturnType ExecuteAndSerialize(CanvasRecorderOp op,
ReturnType (Canvas::*canvasMethod)()) {
serializer_.Write(op);
return (canvas_.*canvasMethod)();
}
template <typename FuncType, typename... Args>
auto ExecuteAndSerialize(CanvasRecorderOp op,
FuncType canvasMethod,
Args&&... args)
-> decltype((std::declval<Canvas>().*
canvasMethod)(std::forward<Args>(args)...)) {
// Serialize each argument
(serializer_.Write(std::forward<Args>(args)), ...);
serializer_.Write(op);
return (canvas_.*canvasMethod)(std::forward<Args>(args)...);
}
template <typename FuncType, typename... Args>
auto ExecuteAndSkipArgSerialize(CanvasRecorderOp op,
FuncType canvasMethod,
Args&&... args)
-> decltype((std::declval<Canvas>().*
canvasMethod)(std::forward<Args>(args)...)) {
serializer_.Write(op);
return (canvas_.*canvasMethod)(std::forward<Args>(args)...);
}
//////////////////////////////////////////////////////////////////////////////
// Canvas Static Polymorphism
// ////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
void Save() {
return ExecuteAndSerialize(CanvasRecorderOp::kSave, &Canvas::Save);
}
void SaveLayer(
const Paint& paint,
std::optional<Rect> bounds = std::nullopt,
const std::shared_ptr<ImageFilter>& backdrop_filter = nullptr) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(SaveLayer), paint,
bounds, backdrop_filter);
}
bool Restore() {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(Restore));
}
size_t GetSaveCount() const { return canvas_.GetSaveCount(); }
void RestoreToCount(size_t count) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(RestoreToCount),
count);
}
const Matrix& GetCurrentTransform() const {
return canvas_.GetCurrentTransform();
}
const std::optional<Rect> GetCurrentLocalCullingBounds() const {
return canvas_.GetCurrentLocalCullingBounds();
}
void ResetTransform() {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(ResetTransform));
}
void Transform(const Matrix& transform) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(Transform),
transform);
}
void Concat(const Matrix& transform) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(Concat), transform);
}
void PreConcat(const Matrix& transform) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(PreConcat),
transform);
}
void Translate(const Vector3& offset) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(Translate), offset);
}
void Scale(const Vector2& scale) {
return ExecuteAndSerialize(
CanvasRecorderOp::kScale2,
static_cast<void (Canvas::*)(const Vector2&)>(&Canvas::Scale), scale);
}
void Scale(const Vector3& scale) {
return ExecuteAndSerialize(
CanvasRecorderOp::kScale3,
static_cast<void (Canvas::*)(const Vector3&)>(&Canvas::Scale), scale);
}
void Skew(Scalar sx, Scalar sy) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(Skew), sx, sy);
}
void Rotate(Radians radians) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(Rotate), radians);
}
void DrawPath(Path path, const Paint& paint) {
serializer_.Write(path);
serializer_.Write(paint);
return ExecuteAndSkipArgSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawPath),
std::move(path), paint);
}
void DrawPaint(const Paint& paint) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawPaint), paint);
}
void DrawLine(const Point& p0, const Point& p1, const Paint& paint) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawLine), p0, p1,
paint);
}
void DrawRect(Rect rect, const Paint& paint) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawRect), rect,
paint);
}
void DrawOval(const Rect& rect, const Paint& paint) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawOval), rect,
paint);
}
void DrawRRect(const Rect& rect,
const Size& corner_radii,
const Paint& paint) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawRRect), rect,
corner_radii, paint);
}
void DrawCircle(Point center, Scalar radius, const Paint& paint) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawCircle), center,
radius, paint);
}
void DrawPoints(std::vector<Point> points,
Scalar radius,
const Paint& paint,
PointStyle point_style) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawPoints), points,
radius, paint, point_style);
}
void DrawImage(const std::shared_ptr<Image>& image,
Point offset,
const Paint& paint,
SamplerDescriptor sampler = {}) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawImage), image,
offset, paint, sampler);
}
void DrawImageRect(
const std::shared_ptr<Image>& image,
Rect source,
Rect dest,
const Paint& paint,
SamplerDescriptor sampler = {},
SourceRectConstraint src_rect_constraint = SourceRectConstraint::kFast) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawImageRect), image,
source, dest, paint, sampler,
src_rect_constraint);
}
void ClipPath(
Path path,
Entity::ClipOperation clip_op = Entity::ClipOperation::kIntersect) {
serializer_.Write(path);
serializer_.Write(clip_op);
return ExecuteAndSkipArgSerialize(FLT_CANVAS_RECORDER_OP_ARG(ClipPath),
std::move(path), clip_op);
}
void ClipRect(
const Rect& rect,
Entity::ClipOperation clip_op = Entity::ClipOperation::kIntersect) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(ClipRect), rect,
clip_op);
}
void ClipOval(
const Rect& bounds,
Entity::ClipOperation clip_op = Entity::ClipOperation::kIntersect) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(ClipOval), bounds,
clip_op);
}
void ClipRRect(
const Rect& rect,
const Size& corner_radii,
Entity::ClipOperation clip_op = Entity::ClipOperation::kIntersect) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(ClipRRect), rect,
corner_radii, clip_op);
}
void DrawPicture(const Picture& picture) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawPicture),
picture);
}
void DrawTextFrame(const std::shared_ptr<TextFrame>& text_frame,
Point position,
const Paint& paint) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawTextFrame),
text_frame, position, paint);
}
void DrawVertices(const std::shared_ptr<VerticesGeometry>& vertices,
BlendMode blend_mode,
const Paint& paint) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawVertices),
vertices, blend_mode, paint);
}
void DrawAtlas(const std::shared_ptr<Image>& atlas,
std::vector<Matrix> transforms,
std::vector<Rect> texture_coordinates,
std::vector<Color> colors,
BlendMode blend_mode,
SamplerDescriptor sampler,
std::optional<Rect> cull_rect,
const Paint& paint) {
return ExecuteAndSerialize(FLT_CANVAS_RECORDER_OP_ARG(DrawAtlas), //
atlas, //
transforms, //
texture_coordinates, //
colors, //
blend_mode, //
sampler, //
cull_rect, //
paint);
}
Picture EndRecordingAsPicture() { return canvas_.EndRecordingAsPicture(); }
private:
Canvas canvas_;
Serializer serializer_;
};
#endif
} // namespace impeller
#endif // FLUTTER_IMPELLER_AIKS_CANVAS_RECORDER_H_