| 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 Entity::RenderingMode rendering_mode = Entity::RenderingMode::kDirect;
37 bool contains_clips = false;
38 };
39
40 enum class PointStyle {
41 /// @brief Points are drawn as squares.
42 kRound,
43
44 /// @brief Points are drawn as circles.
45 kSquare,
46 };
47
48 class Canvas {
49 public:
50 struct DebugOptions {
51 /// When enabled, layers that are rendered to an offscreen texture
52 /// internally get a translucent checkerboard pattern painted over them.
53 ///
54 /// Requires the `IMPELLER_DEBUG` preprocessor flag.
55 bool offscreen_texture_checkerboard = false;
56 } debug_options;
57
58 Canvas();
59
60 explicit Canvas(Rect cull_rect);
61
62 explicit Canvas(IRect cull_rect);
63
64 ~Canvas();
65
66 void Save();
67
68 void SaveLayer(const Paint& paint,
69 std::optional<Rect> bounds = std::nullopt,
70 const std::shared_ptr<ImageFilter>& backdrop_filter = nullptr);
71
72 bool Restore();
73
74 size_t GetSaveCount() const;
75
76 void RestoreToCount(size_t count);
77
78 const Matrix& GetCurrentTransform() const;
79
80 const std::optional<Rect> GetCurrentLocalCullingBounds() const;
81
82 void ResetTransform();
83
84 void Transform(const Matrix& transform);
85
86 void Concat(const Matrix& transform);
87
88 void PreConcat(const Matrix& transform);
89
90 void Translate(const Vector3& offset);
91
92 void Scale(const Vector2& scale);
93
94 void Scale(const Vector3& scale);
95
96 void Skew(Scalar sx, Scalar sy);
97
98 void Rotate(Radians radians);
99
100 void DrawPath(Path path, const Paint& paint);
101
102 void DrawPaint(const Paint& paint);
103
104 void DrawLine(const Point& p0, const Point& p1, const Paint& paint);
105
106 void DrawRect(const Rect& rect, const Paint& paint);
107
108 void DrawOval(const Rect& rect, const Paint& paint);
109
110 void DrawRRect(const Rect& rect,
111 const Size& corner_radii,
112 const Paint& paint);
113
114 void DrawCircle(const Point& center, Scalar radius, const Paint& paint);
115
116 void DrawPoints(std::vector<Point> points,
117 Scalar radius,
118 const Paint& paint,
119 PointStyle point_style);
120
121 void DrawImage(const std::shared_ptr<Image>& image,
122 Point offset,
123 const Paint& paint,
124 SamplerDescriptor sampler = {});
125
126 void DrawImageRect(const std::shared_ptr<Image>& image,
127 Rect source,
128 Rect dest,
129 const Paint& paint,
130 SamplerDescriptor sampler = {});
131
132 void ClipPath(
133 Path path,
134 Entity::ClipOperation clip_op = Entity::ClipOperation::kIntersect);
135
136 void ClipRect(
137 const Rect& rect,
138 Entity::ClipOperation clip_op = Entity::ClipOperation::kIntersect);
139
140 void ClipOval(
141 const Rect& bounds,
142 Entity::ClipOperation clip_op = Entity::ClipOperation::kIntersect);
143
144 void ClipRRect(
145 const Rect& rect,
146 const Size& corner_radii,
147 Entity::ClipOperation clip_op = Entity::ClipOperation::kIntersect);
148
149 void DrawPicture(const Picture& picture);
150
151 void DrawTextFrame(const std::shared_ptr<TextFrame>& text_frame,
152 Point position,
153 const Paint& paint);
154
155 void DrawVertices(const std::shared_ptr<VerticesGeometry>& vertices,
156 BlendMode blend_mode,
157 const Paint& paint);
158
159 void DrawAtlas(const std::shared_ptr<Image>& atlas,
160 std::vector<Matrix> transforms,
161 std::vector<Rect> texture_coordinates,
162 std::vector<Color> colors,
163 BlendMode blend_mode,
164 SamplerDescriptor sampler,
165 std::optional<Rect> cull_rect,
166 const Paint& paint);
167
168 Picture EndRecordingAsPicture();
169
170 private:
171 std::unique_ptr<EntityPass> base_pass_;
172 EntityPass* current_pass_ = nullptr;
173 std::deque<CanvasStackEntry> transform_stack_;
174 std::optional<Rect> initial_cull_rect_;
175
176 void Initialize(std::optional<Rect> cull_rect);
177
178 void Reset();
179
180 EntityPass& GetCurrentPass();
181
182 size_t GetClipDepth() const;
183
184 void ClipGeometry(const std::shared_ptr<Geometry>& geometry,
185 Entity::ClipOperation clip_op);
186
187 void IntersectCulling(Rect clip_bounds);
188 void SubtractCulling(Rect clip_bounds);
189
190 void Save(bool create_subpass,
191 BlendMode = BlendMode::kSourceOver,
192 const std::shared_ptr<ImageFilter>& backdrop_filter = nullptr);
193
194 void RestoreClip();
195
196 bool AttemptDrawBlurredRRect(const Rect& rect,
197 Size corner_radius,
198 const Paint& paint);
199
200 Canvas(const Canvas&) = delete;
201
202 Canvas& operator=(const Canvas&) = delete;
203 };
204
205 } // namespace impeller
206
207 #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:563
impeller::Entity::ClipOperation::kIntersect
@ kIntersect
path.h
impeller::Canvas::EndRecordingAsPicture
Picture EndRecordingAsPicture()
Definition: canvas.cc:657
impeller::Canvas::ClipOval
void ClipOval(const Rect &bounds, Entity::ClipOperation clip_op=Entity::ClipOperation::kIntersect)
Definition: canvas.cc:450
impeller::Canvas::DrawRRect
void DrawRRect(const Rect &rect, const Size &corner_radii, const Paint &paint)
Definition: canvas.cc:368
point.h
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Canvas::RestoreToCount
void RestoreToCount(size_t count)
Definition: canvas.cc:242
image_filter.h
impeller::Canvas::DrawPath
void DrawPath(Path path, const Paint &paint)
Definition: canvas.cc:250
entity.h
impeller::CanvasStackEntry
Definition: canvas.h:31
impeller::Paint
Definition: paint.h:22
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:230
impeller::BlendMode
BlendMode
Definition: color.h:59
impeller::PointStyle
PointStyle
Definition: canvas.h:40
impeller::Canvas::DrawTextFrame
void DrawTextFrame(const std::shared_ptr< TextFrame > &text_frame, Point position, const Paint &paint)
Definition: canvas.cc:702
impeller::Canvas
Definition: canvas.h:48
impeller::PointStyle::kRound
@ kRound
Points are drawn as squares.
impeller::Canvas::ResetTransform
void ResetTransform()
Definition: canvas.cc:197
impeller::Canvas::DrawVertices
void DrawVertices(const std::shared_ptr< VerticesGeometry > &vertices, BlendMode blend_mode, const Paint &paint)
Definition: canvas.cc:746
impeller::CanvasStackEntry::contains_clips
bool contains_clips
Definition: canvas.h:37
impeller::CanvasStackEntry::clip_depth
size_t clip_depth
Definition: canvas.h:35
impeller::Canvas::DebugOptions::offscreen_texture_checkerboard
bool offscreen_texture_checkerboard
Definition: canvas.h:55
impeller::Size
TSize< Scalar > Size
Definition: size.h:137
impeller::Canvas::debug_options
struct impeller::Canvas::DebugOptions debug_options
picture.h
impeller::Canvas::SaveLayer
void SaveLayer(const Paint &paint, std::optional< Rect > bounds=std::nullopt, const std::shared_ptr< ImageFilter > &backdrop_filter=nullptr)
Definition: canvas.cc:676
impeller::CanvasStackEntry::rendering_mode
Entity::RenderingMode rendering_mode
Definition: canvas.h:36
impeller::Canvas::DrawLine
void DrawLine(const Point &p0, const Point &p1, const Paint &paint)
Definition: canvas.cc:310
impeller::Canvas::DrawRect
void DrawRect(const Rect &rect, const Paint &paint)
Definition: canvas.cc:321
impeller::Canvas::GetCurrentTransform
const Matrix & GetCurrentTransform() const
Definition: canvas.cc:205
impeller::Canvas::Concat
void Concat(const Matrix &transform)
Definition: canvas.cc:189
impeller::Canvas::DrawImage
void DrawImage(const std::shared_ptr< Image > &image, Point offset, const Paint &paint, SamplerDescriptor sampler={})
Definition: canvas.cc:612
impeller::Canvas::DrawPicture
void DrawPicture(const Picture &picture)
Definition: canvas.cc:583
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:15
matrix.h
impeller::TSize< Scalar >
impeller::Canvas::DrawImageRect
void DrawImageRect(const std::shared_ptr< Image > &image, Rect source, Rect dest, const Paint &paint, SamplerDescriptor sampler={})
Definition: canvas.cc:626
impeller::PointStyle::kSquare
@ kSquare
Points are drawn as circles.
impeller::Point
TPoint< Scalar > Point
Definition: point.h:308
impeller::Canvas::Scale
void Scale(const Vector2 &scale)
Definition: canvas.cc:222
impeller::Path
Paths are lightweight objects that describe a collection of linear, quadratic, or cubic segments....
Definition: path.h:55
impeller::Canvas::Save
void Save()
Definition: canvas.cc:132
impeller::Canvas::DrawCircle
void DrawCircle(const Point &center, Scalar radius, const Paint &paint)
Definition: canvas.cc:395
geometry.h
impeller::Canvas::Restore
bool Restore()
Definition: canvas.cc:168
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:806
entity_pass.h
impeller::Canvas::DrawPaint
void DrawPaint(const Paint &paint)
Definition: canvas.cc:260
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:488
impeller::Canvas::GetSaveCount
size_t GetSaveCount() const
Definition: canvas.cc:238
impeller::Canvas::Canvas
Canvas()
Definition: canvas.cc:102
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:470
impeller::Canvas::PreConcat
void PreConcat(const Matrix &transform)
Definition: canvas.cc:193
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:341
impeller::Canvas::Rotate
void Rotate(Radians radians)
Definition: canvas.cc:234
image.h
vector.h
impeller::TPoint< Scalar >
impeller::Canvas::Transform
void Transform(const Matrix &transform)
Definition: canvas.cc:201
impeller::Canvas::GetCurrentLocalCullingBounds
const std::optional< Rect > GetCurrentLocalCullingBounds() const
Definition: canvas.cc:209
impeller::Entity::ClipOperation
ClipOperation
Definition: entity.h:59
paint.h
impeller::Canvas::ClipRect
void ClipRect(const Rect &rect, Entity::ClipOperation clip_op=Entity::ClipOperation::kIntersect)
Definition: canvas.cc:429
impeller::Entity::RenderingMode
RenderingMode
Definition: entity.h:26
text_frame.h
impeller::Canvas::ClipPath
void ClipPath(Path path, Entity::ClipOperation clip_op=Entity::ClipOperation::kIntersect)
Definition: canvas.cc:419
impeller
Definition: aiks_context.cc:10
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:50
impeller::Canvas::Translate
void Translate(const Vector3 &offset)
Definition: canvas.cc:218