forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dl_canvas.h
464 lines (422 loc) · 17.9 KB
/
dl_canvas.h
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// 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_DISPLAY_LIST_DL_CANVAS_H_
#define FLUTTER_DISPLAY_LIST_DL_CANVAS_H_
#include "flutter/display_list/dl_blend_mode.h"
#include "flutter/display_list/dl_paint.h"
#include "flutter/display_list/dl_vertices.h"
#include "flutter/display_list/geometry/dl_geometry_types.h"
#include "flutter/display_list/geometry/dl_path.h"
#include "flutter/display_list/image/dl_image.h"
#include "third_party/skia/include/core/SkM44.h"
#include "third_party/skia/include/core/SkMatrix.h"
#include "third_party/skia/include/core/SkPath.h"
#include "third_party/skia/include/core/SkRRect.h"
#include "third_party/skia/include/core/SkRSXform.h"
#include "third_party/skia/include/core/SkRect.h"
#include "third_party/skia/include/core/SkTextBlob.h"
#include "impeller/typographer/text_frame.h"
namespace flutter {
//------------------------------------------------------------------------------
/// @brief Developer-facing API for rendering anything *within* the engine.
///
/// |DlCanvas| should be used to render anything in the framework classes (i.e.
/// `lib/ui`), flow and flow layers, embedders, shell, and elsewhere.
///
/// The only state carried by implementations of this interface are the clip
/// and transform which are saved and restored by the |save|, |saveLayer|, and
/// |restore| calls.
///
/// @note The interface resembles closely the familiar |SkCanvas| interface
/// used throughout the engine.
class DlCanvas {
public:
enum class ClipOp {
kDifference,
kIntersect,
};
enum class PointMode {
kPoints, //!< draw each point separately
kLines, //!< draw each separate pair of points as a line segment
kPolygon, //!< draw each pair of overlapping points as a line segment
};
enum class SrcRectConstraint {
kStrict,
kFast,
};
virtual ~DlCanvas() = default;
virtual DlISize GetBaseLayerDimensions() const = 0;
virtual SkImageInfo GetImageInfo() const = 0;
virtual void Save() = 0;
virtual void SaveLayer(std::optional<const DlRect>& bounds,
const DlPaint* paint = nullptr,
const DlImageFilter* backdrop = nullptr) = 0;
virtual void Restore() = 0;
virtual int GetSaveCount() const = 0;
virtual void RestoreToCount(int restore_count) = 0;
virtual void Translate(DlScalar tx, DlScalar ty) = 0;
virtual void Scale(DlScalar sx, DlScalar sy) = 0;
virtual void Rotate(DlScalar degrees) = 0;
virtual void Skew(DlScalar sx, DlScalar sy) = 0;
// clang-format off
// 2x3 2D affine subset of a 4x4 transform in row major order
virtual void Transform2DAffine(DlScalar mxx, DlScalar mxy, DlScalar mxt,
DlScalar myx, DlScalar myy, DlScalar myt) = 0;
// full 4x4 transform in row major order
virtual void TransformFullPerspective(
DlScalar mxx, DlScalar mxy, DlScalar mxz, DlScalar mxt,
DlScalar myx, DlScalar myy, DlScalar myz, DlScalar myt,
DlScalar mzx, DlScalar mzy, DlScalar mzz, DlScalar mzt,
DlScalar mwx, DlScalar mwy, DlScalar mwz, DlScalar mwt) = 0;
// clang-format on
virtual void TransformReset() = 0;
virtual void Transform(const DlMatrix& matrix) = 0;
virtual void SetTransform(const DlMatrix& matrix) = 0;
virtual DlMatrix GetMatrix() const = 0;
virtual void ClipRect(const DlRect& rect,
ClipOp clip_op = ClipOp::kIntersect,
bool is_aa = false) = 0;
virtual void ClipOval(const DlRect& bounds,
ClipOp clip_op = ClipOp::kIntersect,
bool is_aa = false) = 0;
virtual void ClipRRect(const SkRRect& rrect,
ClipOp clip_op = ClipOp::kIntersect,
bool is_aa = false) = 0;
virtual void ClipPath(const DlPath& path,
ClipOp clip_op = ClipOp::kIntersect,
bool is_aa = false) = 0;
/// Conservative estimate of the bounds of all outstanding clip operations
/// measured in the coordinate space within which this DisplayList will
/// be rendered.
virtual DlRect GetDestinationClipCoverage() const = 0;
/// Conservative estimate of the bounds of all outstanding clip operations
/// transformed into the local coordinate space in which currently
/// recorded rendering operations are interpreted.
virtual DlRect GetLocalClipCoverage() const = 0;
/// Return true iff the supplied bounds are easily shown to be outside
/// of the current clip bounds. This method may conservatively return
/// false if it cannot make the determination.
virtual bool QuickReject(const DlRect& bounds) const = 0;
virtual void DrawPaint(const DlPaint& paint) = 0;
virtual void DrawColor(DlColor color,
DlBlendMode mode = DlBlendMode::kSrcOver) = 0;
void Clear(DlColor color) { DrawColor(color, DlBlendMode::kSrc); }
virtual void DrawLine(const DlPoint& p0,
const DlPoint& p1,
const DlPaint& paint) = 0;
virtual void DrawDashedLine(const DlPoint& p0,
const DlPoint& p1,
DlScalar on_length,
DlScalar off_length,
const DlPaint& paint) = 0;
virtual void DrawRect(const DlRect& rect, const DlPaint& paint) = 0;
virtual void DrawOval(const DlRect& bounds, const DlPaint& paint) = 0;
virtual void DrawCircle(const DlPoint& center,
DlScalar radius,
const DlPaint& paint) = 0;
virtual void DrawRRect(const SkRRect& rrect, const DlPaint& paint) = 0;
virtual void DrawDRRect(const SkRRect& outer,
const SkRRect& inner,
const DlPaint& paint) = 0;
virtual void DrawPath(const DlPath& path, const DlPaint& paint) = 0;
virtual void DrawArc(const DlRect& bounds,
DlScalar start,
DlScalar sweep,
bool useCenter,
const DlPaint& paint) = 0;
virtual void DrawPoints(PointMode mode,
uint32_t count,
const DlPoint pts[],
const DlPaint& paint) = 0;
virtual void DrawVertices(const std::shared_ptr<DlVertices>& vertices,
DlBlendMode mode,
const DlPaint& paint) = 0;
virtual void DrawImage(const sk_sp<DlImage>& image,
const DlPoint& point,
DlImageSampling sampling,
const DlPaint* paint = nullptr) = 0;
virtual void DrawImageRect(
const sk_sp<DlImage>& image,
const DlRect& src,
const DlRect& dst,
DlImageSampling sampling,
const DlPaint* paint = nullptr,
SrcRectConstraint constraint = SrcRectConstraint::kFast) = 0;
virtual void DrawImageRect(
const sk_sp<DlImage>& image,
const DlIRect& src,
const DlRect& dst,
DlImageSampling sampling,
const DlPaint* paint = nullptr,
SrcRectConstraint constraint = SrcRectConstraint::kFast) {
auto float_src = DlRect::MakeLTRB(src.GetLeft(), src.GetTop(),
src.GetRight(), src.GetBottom());
DrawImageRect(image, float_src, dst, sampling, paint, constraint);
}
void DrawImageRect(const sk_sp<DlImage>& image,
const DlRect& dst,
DlImageSampling sampling,
const DlPaint* paint = nullptr,
SrcRectConstraint constraint = SrcRectConstraint::kFast) {
DrawImageRect(image, image->GetBounds(), dst, sampling, paint, constraint);
}
virtual void DrawImageNine(const sk_sp<DlImage>& image,
const DlIRect& center,
const DlRect& dst,
DlFilterMode filter,
const DlPaint* paint = nullptr) = 0;
virtual void DrawAtlas(const sk_sp<DlImage>& atlas,
const SkRSXform xform[],
const DlRect tex[],
const DlColor colors[],
int count,
DlBlendMode mode,
DlImageSampling sampling,
const DlRect* cullRect,
const DlPaint* paint = nullptr) = 0;
virtual void DrawDisplayList(const sk_sp<DisplayList> display_list,
DlScalar opacity = SK_Scalar1) = 0;
virtual void DrawTextFrame(
const std::shared_ptr<impeller::TextFrame>& text_frame,
DlScalar x,
DlScalar y,
const DlPaint& paint) = 0;
virtual void DrawTextBlob(const sk_sp<SkTextBlob>& blob,
DlScalar x,
DlScalar y,
const DlPaint& paint) = 0;
virtual void DrawShadow(const DlPath& path,
const DlColor color,
const DlScalar elevation,
bool transparent_occluder,
DlScalar dpr) = 0;
virtual void Flush() = 0;
static constexpr DlScalar kShadowLightHeight = 600;
static constexpr DlScalar kShadowLightRadius = 800;
static DlRect ComputeShadowBounds(const DlPath& path,
float elevation,
DlScalar dpr,
const DlMatrix& ctm);
// -----------------------------------------------------------------
// SkObject Compatibility section - deprecated...
// -----------------------------------------------------------------
SkISize GetBaseLayerSize() const {
return ToSkISize(GetBaseLayerDimensions());
}
void SaveLayer(const SkRect* bounds,
const DlPaint* paint = nullptr,
const DlImageFilter* backdrop = nullptr) {
auto optional_bounds = ToOptDlRect(bounds);
SaveLayer(optional_bounds, paint, backdrop);
}
void Transform(const SkMatrix* matrix) {
if (matrix) {
Transform(*matrix);
}
}
void Transform(const SkM44* matrix44) {
if (matrix44) {
Transform(*matrix44);
}
}
void Transform(const SkMatrix& matrix) { Transform(ToDlMatrix(matrix)); }
void Transform(const SkM44& m44) { Transform(ToDlMatrix(m44)); }
void SetTransform(const SkMatrix* matrix) {
if (matrix) {
SetTransform(*matrix);
}
}
void SetTransform(const SkM44* matrix44) {
if (matrix44) {
SetTransform(*matrix44);
}
}
void SetTransform(const SkMatrix& matrix) {
SetTransform(ToDlMatrix(matrix));
}
void SetTransform(const SkM44& m44) { SetTransform(ToDlMatrix(m44)); }
/// Returns the 4x4 full perspective transform representing all transform
/// operations executed so far in this DisplayList within the enclosing
/// save stack.
SkM44 GetTransformFullPerspective() const { return ToSkM44(GetMatrix()); }
/// Returns the 3x3 partial perspective transform representing all transform
/// operations executed so far in this DisplayList within the enclosing
/// save stack.
SkMatrix GetTransform() const { return ToSkMatrix(GetMatrix()); }
void ClipRect(const SkRect& rect,
ClipOp clip_op = ClipOp::kIntersect,
bool is_aa = false) {
ClipRect(ToDlRect(rect), clip_op, is_aa);
}
void ClipOval(const SkRect& bounds,
ClipOp clip_op = ClipOp::kIntersect,
bool is_aa = false) {
ClipOval(ToDlRect(bounds), clip_op, is_aa);
}
void ClipPath(const SkPath& path,
ClipOp clip_op = ClipOp::kIntersect,
bool is_aa = false) {
ClipPath(DlPath(path), clip_op, is_aa);
}
SkRect GetDestinationClipBounds() const {
return ToSkRect(GetDestinationClipCoverage());
}
SkRect GetLocalClipBounds() const { return ToSkRect(GetLocalClipCoverage()); }
bool QuickReject(const SkRect& bounds) const {
return QuickReject(ToDlRect(bounds));
}
void DrawLine(const SkPoint& p0, const SkPoint& p1, const DlPaint& paint) {
DrawLine(ToDlPoint(p0), ToDlPoint(p1), paint);
}
void DrawRect(const SkRect& rect, const DlPaint& paint) {
DrawRect(ToDlRect(rect), paint);
}
void DrawOval(const SkRect& bounds, const DlPaint& paint) {
DrawOval(ToDlRect(bounds), paint);
}
void DrawCircle(const SkPoint& center,
DlScalar radius,
const DlPaint& paint) {
DrawCircle(ToDlPoint(center), radius, paint);
}
void DrawPath(const SkPath& path, const DlPaint& paint) {
DrawPath(DlPath(path), paint);
}
void DrawArc(const SkRect& bounds,
DlScalar start,
DlScalar sweep,
bool useCenter,
const DlPaint& paint) {
DrawArc(ToDlRect(bounds), start, sweep, useCenter, paint);
}
void DrawPoints(PointMode mode,
uint32_t count,
const SkPoint pts[],
const DlPaint& paint) {
DrawPoints(mode, count, ToDlPoints(pts), paint);
}
void DrawImage(const sk_sp<DlImage>& image,
const SkPoint& point,
DlImageSampling sampling,
const DlPaint* paint = nullptr) {
DrawImage(image, ToDlPoint(point), sampling, paint);
}
void DrawImageRect(const sk_sp<DlImage>& image,
const SkRect& src,
const SkRect& dst,
DlImageSampling sampling,
const DlPaint* paint = nullptr,
SrcRectConstraint constraint = SrcRectConstraint::kFast) {
DrawImageRect(image, ToDlRect(src), ToDlRect(dst), sampling, paint,
constraint);
}
void DrawImageRect(const sk_sp<DlImage>& image,
const SkIRect& src,
const SkRect& dst,
DlImageSampling sampling,
const DlPaint* paint = nullptr,
SrcRectConstraint constraint = SrcRectConstraint::kFast) {
DrawImageRect(image, ToDlRect(src), ToDlRect(dst), sampling, paint,
constraint);
}
void DrawImageRect(const sk_sp<DlImage>& image,
const SkRect& dst,
DlImageSampling sampling,
const DlPaint* paint = nullptr,
SrcRectConstraint constraint = SrcRectConstraint::kFast) {
DrawImageRect(image, image->GetBounds(), ToDlRect(dst), sampling, paint,
constraint);
}
void DrawImageNine(const sk_sp<DlImage>& image,
const SkIRect& center,
const SkRect& dst,
DlFilterMode filter,
const DlPaint* paint = nullptr) {
DrawImageNine(image, ToDlIRect(center), ToDlRect(dst), filter, paint);
}
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) {
DrawAtlas(atlas, xform, ToDlRects(tex), colors, count, mode, sampling,
ToDlRect(cullRect), paint);
}
void DrawShadow(const SkPath& path,
const DlColor color,
const DlScalar elevation,
bool transparent_occluder,
DlScalar dpr) {
DrawShadow(DlPath(path), color, elevation, transparent_occluder, dpr);
}
static SkRect ComputeShadowBounds(const SkPath& path,
float elevation,
DlScalar dpr,
const SkMatrix& ctm) {
return ToSkRect(
ComputeShadowBounds(DlPath(path), elevation, dpr, ToDlMatrix(ctm)));
}
#define ENABLE_DL_CANVAS_BACKWARDS_COMPATIBILITY \
using DlCanvas::GetBaseLayerSize; \
\
using DlCanvas::SaveLayer; \
\
using DlCanvas::Transform; \
using DlCanvas::SetTransform; \
using DlCanvas::GetTransformFullPerspective; \
using DlCanvas::GetTransform; \
\
using DlCanvas::ClipRect; \
using DlCanvas::ClipOval; \
using DlCanvas::ClipPath; \
\
using DlCanvas::GetDestinationClipBounds; \
using DlCanvas::GetLocalClipBounds; \
using DlCanvas::QuickReject; \
\
using DlCanvas::DrawLine; \
using DlCanvas::DrawRect; \
using DlCanvas::DrawOval; \
using DlCanvas::DrawCircle; \
using DlCanvas::DrawPath; \
using DlCanvas::DrawArc; \
using DlCanvas::DrawPoints; \
using DlCanvas::DrawImage; \
using DlCanvas::DrawImageRect; \
using DlCanvas::DrawImageNine; \
using DlCanvas::DrawAtlas; \
using DlCanvas::DrawShadow;
};
class DlAutoCanvasRestore {
public:
DlAutoCanvasRestore(DlCanvas* canvas, bool do_save) : canvas_(canvas) {
if (canvas) {
canvas_ = canvas;
restore_count_ = canvas->GetSaveCount();
if (do_save) {
canvas_->Save();
}
} else {
canvas_ = nullptr;
restore_count_ = 0;
}
}
~DlAutoCanvasRestore() { Restore(); }
void Restore() {
if (canvas_) {
canvas_->RestoreToCount(restore_count_);
canvas_ = nullptr;
}
}
private:
DlCanvas* canvas_;
int restore_count_;
FML_DISALLOW_COPY_ASSIGN_AND_MOVE(DlAutoCanvasRestore);
};
} // namespace flutter
#endif // FLUTTER_DISPLAY_LIST_DL_CANVAS_H_