| Flutter Impeller |

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_IMPELLER_AIKS_CANVAS_H_
6 #define FLUTTER_IMPELLER_AIKS_CANVAS_H_
7
8 #include <deque>
9 #include <functional>
10 #include <memory>
11 #include <optional>
12 #include <vector>
13
14 #include "impeller/aiks/image.h"
15 #include "impeller/aiks/image_filter.h"
16 #include "impeller/aiks/paint.h"
17 #include "impeller/aiks/picture.h"
18 #include "impeller/core/sampler_descriptor.h"
19 #include "impeller/entity/entity.h"
20 #include "impeller/entity/entity_pass.h"
21 #include "impeller/entity/geometry/geometry.h"
22 #include "impeller/entity/geometry/vertices_geometry.h"
23 #include "impeller/geometry/matrix.h"
24 #include "impeller/geometry/path.h"
25 #include "impeller/geometry/point.h"
26 #include "impeller/geometry/vector.h"
27 #include "impeller/typographer/text_frame.h"
28
29 namespace impeller {
30
31 struct CanvasStackEntry {
32 Matrix transform;
33 // |cull_rect| is conservative screen-space bounds of the clipped output area
34 std::optional<Rect> cull_rect;
35 size_t clip_depth = 0u;
36 // The number of clips tracked for this canvas stack entry.
37 size_t num_clips = 0u;
38 Entity::RenderingMode rendering_mode = Entity::RenderingMode::kDirect;
39 };
40
41 enum class PointStyle {
42 /// @brief Points are drawn as squares.
43 kRound,
44
45 /// @brief Points are drawn as circles.
46 kSquare,
47 };
48
49 /// Controls the behavior of the source rectangle given to DrawImageRect.
50 enum class SourceRectConstraint {
51 /// @brief Faster, but may sample outside the bounds of the source rectangle.
52 kFast,
53
54 /// @brief Sample only within the source rectangle. May be slower.
55 kStrict,
56 };
57
58 class Canvas {
59 public:
60 struct DebugOptions {
61 /// When enabled, layers that are rendered to an offscreen texture
62 /// internally get a translucent checkerboard pattern painted over them.
63 ///
64 /// Requires the `IMPELLER_DEBUG` preprocessor flag.
65 bool offscreen_texture_checkerboard = false;
66 } debug_options;
67
68 Canvas();
69
70 explicit Canvas(Rect cull_rect);
71
72 explicit Canvas(IRect cull_rect);
73
74 ~Canvas();
75
76 void Save();
77
78 void SaveLayer(
79 const Paint& paint,
80 std::optional<Rect> bounds = std::nullopt,
81 const std::shared_ptr<ImageFilter>& backdrop_filter = nullptr,
82 ContentBoundsPromise bounds_promise = ContentBoundsPromise::kUnknown);
83
84 bool Restore();
85
86 size_t GetSaveCount() const;
87
88 void RestoreToCount(size_t count);
89
90 const Matrix& GetCurrentTransform() const;
91
92 const std::optional<Rect> GetCurrentLocalCullingBounds() const;
93
94 void ResetTransform();
95
96 void Transform(const Matrix& transform);
97
98 void Concat(const Matrix& transform);
99
100 void PreConcat(const Matrix& transform);
101
102 void Translate(const Vector3& offset);
103
104 void Scale(const Vector2& scale);
105
106 void Scale(const Vector3& scale);
107
108 void Skew(Scalar sx, Scalar sy);
109
110 void Rotate(Radians radians);
111
112 void DrawPath(const Path& path, const Paint& paint);
113
114 void DrawPaint(const Paint& paint);
115
116 void DrawLine(const Point& p0, const Point& p1, const Paint& paint);
117
118 void DrawRect(const Rect& rect, const Paint& paint);
119
120 void DrawOval(const Rect& rect, const Paint& paint);
121
122 void DrawRRect(const Rect& rect,
123 const Size& corner_radii,
124 const Paint& paint);
125
126 void DrawCircle(const Point& center, Scalar radius, const Paint& paint);
127
128 void DrawPoints(std::vector<Point> points,
129 Scalar radius,
130 const Paint& paint,
131 PointStyle point_style);
132
133 void DrawImage(const std::shared_ptr<Image>& image,
134 Point offset,
135 const Paint& paint,
136 SamplerDescriptor sampler = {});
137
138 void DrawImageRect(
139 const std::shared_ptr<Image>& image,
140 Rect source,
141 Rect dest,
142 const Paint& paint,
143 SamplerDescriptor sampler = {},
144 SourceRectConstraint src_rect_constraint = SourceRectConstraint::kFast);
145
146 void ClipPath(
147 const Path& path,
148 Entity::ClipOperation clip_op = Entity::ClipOperation::kIntersect);
149
150 void ClipRect(
151 const Rect& rect,
152 Entity::ClipOperation clip_op = Entity::ClipOperation::kIntersect);
153
154 void ClipOval(
155 const Rect& bounds,
156 Entity::ClipOperation clip_op = Entity::ClipOperation::kIntersect);
157
158 void ClipRRect(
159 const Rect& rect,
160 const Size& corner_radii,
161 Entity::ClipOperation clip_op = Entity::ClipOperation::kIntersect);
162
163 void DrawTextFrame(const std::shared_ptr<TextFrame>& text_frame,
164 Point position,
165 const Paint& paint);
166
167 void DrawVertices(const std::shared_ptr<VerticesGeometry>& vertices,
168 BlendMode blend_mode,
169 const Paint& paint);
170
171 void DrawAtlas(const std::shared_ptr<Image>& atlas,
172 std::vector<Matrix> transforms,
173 std::vector<Rect> texture_coordinates,
174 std::vector<Color> colors,
175 BlendMode blend_mode,
176 SamplerDescriptor sampler,
177 std::optional<Rect> cull_rect,
178 const Paint& paint);
179
180 Picture EndRecordingAsPicture();
181
182 private:
183 std::unique_ptr<EntityPass> base_pass_;
184 EntityPass* current_pass_ = nullptr;
185 uint64_t current_depth_ = 0u;
186 std::deque<CanvasStackEntry> transform_stack_;
187 std::optional<Rect> initial_cull_rect_;
188
189 void Initialize(std::optional<Rect> cull_rect);
190
191 void Reset();
192
193 EntityPass& GetCurrentPass();
194
195 size_t GetClipDepth() const;
196
197 void AddEntityToCurrentPass(Entity entity);
198
199 void ClipGeometry(const std::shared_ptr<Geometry>& geometry,
200 Entity::ClipOperation clip_op);
201
202 void IntersectCulling(Rect clip_bounds);
203 void SubtractCulling(Rect clip_bounds);
204
205 void Save(bool create_subpass,
206 BlendMode = BlendMode::kSourceOver,
207 const std::shared_ptr<ImageFilter>& backdrop_filter = nullptr);
208
209 void RestoreClip();
210
211 bool AttemptDrawBlurredRRect(const Rect& rect,
212 Size corner_radii,
213 const Paint& paint);
214
215 Canvas(const Canvas&) = delete;
216
217 Canvas& operator=(const Canvas&) = delete;
218 };
219
220 } // namespace impeller
221
222 #endif // FLUTTER_IMPELLER_AIKS_CANVAS_H_
impeller::Canvas::DrawPoints
void DrawPoints(std::vector< Point > points, Scalar radius, const Paint &paint, PointStyle point_style)
Definition: canvas.cc:683
impeller::Entity::ClipOperation::kIntersect
@ kIntersect
path.h
impeller::Canvas::EndRecordingAsPicture
Picture EndRecordingAsPicture()
Definition: canvas.cc:756
impeller::Canvas::ClipOval
void ClipOval(const Rect &bounds, Entity::ClipOperation clip_op=Entity::ClipOperation::kIntersect)
Definition: canvas.cc:570
impeller::Canvas::DrawRRect
void DrawRRect(const Rect &rect, const Size &corner_radii, const Paint &paint)
Definition: canvas.cc:488
point.h
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Canvas::RestoreToCount
void RestoreToCount(size_t count)
Definition: canvas.cc:284
image_filter.h
impeller::Canvas::DrawImageRect
void DrawImageRect(const std::shared_ptr< Image > &image, Rect source, Rect dest, const Paint &paint, SamplerDescriptor sampler={}, SourceRectConstraint src_rect_constraint=SourceRectConstraint::kFast)
Definition: canvas.cc:717
impeller::SourceRectConstraint::kFast
@ kFast
Faster, but may sample outside the bounds of the source rectangle.
entity.h
impeller::CanvasStackEntry
Definition: canvas.h:31
impeller::Paint
Definition: paint.h:23
impeller::CanvasStackEntry::cull_rect
std::optional< Rect > cull_rect
Definition: canvas.h:34
impeller::Canvas::Skew
void Skew(Scalar sx, Scalar sy)
Definition: canvas.cc:272
impeller::BlendMode
BlendMode
Definition: color.h:59
impeller::PointStyle
PointStyle
Definition: canvas.h:41
impeller::Canvas::DrawTextFrame
void DrawTextFrame(const std::shared_ptr< TextFrame > &text_frame, Point position, const Paint &paint)
Definition: canvas.cc:821
impeller::Canvas
Definition: canvas.h:58
impeller::PointStyle::kRound
@ kRound
Points are drawn as squares.
impeller::Canvas::ResetTransform
void ResetTransform()
Definition: canvas.cc:239
impeller::Canvas::DrawVertices
void DrawVertices(const std::shared_ptr< VerticesGeometry > &vertices, BlendMode blend_mode, const Paint &paint)
Definition: canvas.cc:865
impeller::CanvasStackEntry::clip_depth
size_t clip_depth
Definition: canvas.h:35
impeller::ContentBoundsPromise
ContentBoundsPromise
Definition: entity_pass.h:28
impeller::ContentBoundsPromise::kUnknown
@ kUnknown
The caller makes no claims related to the size of the bounds.
impeller::Canvas::DebugOptions::offscreen_texture_checkerboard
bool offscreen_texture_checkerboard
Definition: canvas.h:65
impeller::CanvasStackEntry::num_clips
size_t num_clips
Definition: canvas.h:37
impeller::Size
TSize< Scalar > Size
Definition: size.h:137
impeller::Canvas::debug_options
struct impeller::Canvas::DebugOptions debug_options
picture.h
impeller::CanvasStackEntry::rendering_mode
Entity::RenderingMode rendering_mode
Definition: canvas.h:38
impeller::Canvas::DrawLine
void DrawLine(const Point &p0, const Point &p1, const Paint &paint)
Definition: canvas.cc:430
impeller::Canvas::DrawRect
void DrawRect(const Rect &rect, const Paint &paint)
Definition: canvas.cc:441
impeller::Canvas::GetCurrentTransform
const Matrix & GetCurrentTransform() const
Definition: canvas.cc:247
impeller::Canvas::Concat
void Concat(const Matrix &transform)
Definition: canvas.cc:231
impeller::Canvas::SaveLayer
void SaveLayer(const Paint &paint, std::optional< Rect > bounds=std::nullopt, const std::shared_ptr< ImageFilter > &backdrop_filter=nullptr, ContentBoundsPromise bounds_promise=ContentBoundsPromise::kUnknown)
Definition: canvas.cc:786
impeller::Canvas::DrawImage
void DrawImage(const std::shared_ptr< Image > &image, Point offset, const Paint &paint, SamplerDescriptor sampler={})
Definition: canvas.cc:703
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:15
matrix.h
impeller::SourceRectConstraint::kStrict
@ kStrict
Sample only within the source rectangle. May be slower.
impeller::TSize< Scalar >
impeller::PointStyle::kSquare
@ kSquare
Points are drawn as circles.
impeller::Point
TPoint< Scalar > Point
Definition: point.h:316
impeller::Canvas::Scale
void Scale(const Vector2 &scale)
Definition: canvas.cc:264
impeller::Path
Paths are lightweight objects that describe a collection of linear, quadratic, or cubic segments....
Definition: path.h:51
impeller::Canvas::Save
void Save()
Definition: canvas.cc:136
impeller::Canvas::DrawCircle
void DrawCircle(const Point &center, Scalar radius, const Paint &paint)
Definition: canvas.cc:515
geometry.h
impeller::SourceRectConstraint
SourceRectConstraint
Controls the behavior of the source rectangle given to DrawImageRect.
Definition: canvas.h:50
impeller::Canvas::Restore
bool Restore()
Definition: canvas.cc:208
impeller::Radians
Definition: scalar.h:38
impeller::Entity::RenderingMode::kDirect
@ kDirect
impeller::Canvas::DrawAtlas
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)
Definition: canvas.cc:925
entity_pass.h
impeller::Canvas::DrawPath
void DrawPath(const Path &path, const Paint &paint)
Definition: canvas.cc:292
impeller::Canvas::DrawPaint
void DrawPaint(const Paint &paint)
Definition: canvas.cc:302
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:661
impeller::Canvas::GetSaveCount
size_t GetSaveCount() const
Definition: canvas.cc:280
impeller::Canvas::Canvas
Canvas()
Definition: canvas.cc:104
vertices_geometry.h
impeller::Canvas::ClipRRect
void ClipRRect(const Rect &rect, const Size &corner_radii, Entity::ClipOperation clip_op=Entity::ClipOperation::kIntersect)
Definition: canvas.cc:590
impeller::Canvas::PreConcat
void PreConcat(const Matrix &transform)
Definition: canvas.cc:235
sampler_descriptor.h
impeller::CanvasStackEntry::transform
Matrix transform
Definition: canvas.h:32
impeller::Canvas::DrawOval
void DrawOval(const Rect &rect, const Paint &paint)
Definition: canvas.cc:461
impeller::Canvas::Rotate
void Rotate(Radians radians)
Definition: canvas.cc:276
image.h
vector.h
impeller::TPoint< Scalar >
impeller::Canvas::Transform
void Transform(const Matrix &transform)
Definition: canvas.cc:243
impeller::Canvas::GetCurrentLocalCullingBounds
const std::optional< Rect > GetCurrentLocalCullingBounds() const
Definition: canvas.cc:251
scale
const Scalar scale
Definition: stroke_path_geometry.cc:297
impeller::Entity::ClipOperation
ClipOperation
Definition: entity.h:61
paint.h
impeller::Canvas::ClipRect
void ClipRect(const Rect &rect, Entity::ClipOperation clip_op=Entity::ClipOperation::kIntersect)
Definition: canvas.cc:549
impeller::Entity::RenderingMode
RenderingMode
Definition: entity.h:28
text_frame.h
offset
Point offset
Definition: stroke_path_geometry.cc:300
impeller
Definition: aiks_blur_unittests.cc:20
impeller::TRect< Scalar >
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::Canvas::~Canvas
~Canvas()
impeller::Vector3
Definition: vector.h:20
impeller::BlendMode::kSourceOver
@ kSourceOver
impeller::Canvas::DebugOptions
Definition: canvas.h:60
impeller::Canvas::Translate
void Translate(const Vector3 &offset)
Definition: canvas.cc:260
impeller::Canvas::ClipPath
void ClipPath(const Path &path, Entity::ClipOperation clip_op=Entity::ClipOperation::kIntersect)
Definition: canvas.cc:539