Flutter Engine
The Flutter Engine
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"
16#include "impeller/aiks/paint.h"
28
29namespace impeller {
30
33 // |cull_rect| is conservative screen-space bounds of the clipped output area
34 std::optional<Rect> cull_rect;
35 uint32_t clip_depth = 0u;
36 size_t clip_height = 0u;
37 // The number of clips tracked for this canvas stack entry.
38 size_t num_clips = 0u;
41};
42
43enum class PointStyle {
44 /// @brief Points are drawn as squares.
45 kRound,
46
47 /// @brief Points are drawn as circles.
48 kSquare,
49};
50
51/// Controls the behavior of the source rectangle given to DrawImageRect.
53 /// @brief Faster, but may sample outside the bounds of the source rectangle.
54 kFast,
55
56 /// @brief Sample only within the source rectangle. May be slower.
57 kStrict,
58};
59
60class Canvas {
61 public:
62 static constexpr uint32_t kMaxDepth = 1 << 24;
63
64 Canvas();
65
66 explicit Canvas(Rect cull_rect);
67
68 explicit Canvas(IRect cull_rect);
69
70 virtual ~Canvas();
71
72 virtual void Save(uint32_t total_content_depth = kMaxDepth);
73
74 virtual void SaveLayer(
75 const Paint& paint,
76 std::optional<Rect> bounds = std::nullopt,
77 const std::shared_ptr<ImageFilter>& backdrop_filter = nullptr,
79 uint32_t total_content_depth = kMaxDepth,
80 bool can_distribute_opacity = false);
81
82 virtual bool Restore();
83
84 size_t GetSaveCount() const;
85
86 void RestoreToCount(size_t count);
87
88 const Matrix& GetCurrentTransform() const;
89
90 const std::optional<Rect> GetCurrentLocalCullingBounds() const;
91
92 void ResetTransform();
93
94 void Transform(const Matrix& transform);
95
96 void Concat(const Matrix& transform);
97
98 void PreConcat(const Matrix& transform);
99
100 void Translate(const Vector3& offset);
101
102 void Scale(const Vector2& scale);
103
104 void Scale(const Vector3& scale);
105
106 void Skew(Scalar sx, Scalar sy);
107
108 void Rotate(Radians radians);
109
110 void DrawPath(const Path& path, const Paint& paint);
111
112 void DrawPaint(const Paint& paint);
113
114 void DrawLine(const Point& p0, const Point& p1, const Paint& paint);
115
116 void DrawRect(const Rect& rect, const Paint& paint);
117
118 void DrawOval(const Rect& rect, const Paint& paint);
119
120 void DrawRRect(const Rect& rect,
121 const Size& corner_radii,
122 const Paint& paint);
123
124 void DrawCircle(const Point& center, Scalar radius, const Paint& paint);
125
126 void DrawPoints(std::vector<Point> points,
127 Scalar radius,
128 const Paint& paint,
129 PointStyle point_style);
130
131 void DrawImage(const std::shared_ptr<Image>& image,
133 const Paint& paint,
134 SamplerDescriptor sampler = {});
135
136 void DrawImageRect(
137 const std::shared_ptr<Image>& image,
138 Rect source,
139 Rect dest,
140 const Paint& paint,
141 SamplerDescriptor sampler = {},
143
144 void ClipPath(
145 const Path& path,
147
148 void ClipRect(
149 const Rect& rect,
151
152 void ClipOval(
153 const Rect& bounds,
155
156 void ClipRRect(
157 const Rect& rect,
158 const Size& corner_radii,
160
161 virtual void DrawTextFrame(const std::shared_ptr<TextFrame>& text_frame,
162 Point position,
163 const Paint& paint);
164
165 void DrawVertices(const std::shared_ptr<VerticesGeometry>& vertices,
166 BlendMode blend_mode,
167 const Paint& paint);
168
169 void DrawAtlas(const std::shared_ptr<Image>& atlas,
170 std::vector<Matrix> transforms,
171 std::vector<Rect> texture_coordinates,
172 std::vector<Color> colors,
173 BlendMode blend_mode,
174 SamplerDescriptor sampler,
175 std::optional<Rect> cull_rect,
176 const Paint& paint);
177
179
180 protected:
181 std::deque<CanvasStackEntry> transform_stack_;
182 std::optional<Rect> initial_cull_rect_;
183 uint64_t current_depth_ = 0u;
184
185 size_t GetClipHeight() const;
186
187 void Initialize(std::optional<Rect> cull_rect);
188
189 void Reset();
190
191 private:
192 std::unique_ptr<EntityPass> base_pass_;
193 EntityPass* current_pass_ = nullptr;
194
195 EntityPass& GetCurrentPass();
196
197 virtual void AddRenderEntityToCurrentPass(Entity entity,
198 bool reuse_depth = false);
199 virtual void AddClipEntityToCurrentPass(Entity entity);
200
201 void ClipGeometry(const std::shared_ptr<Geometry>& geometry,
202 Entity::ClipOperation clip_op);
203
204 void IntersectCulling(Rect clip_bounds);
205 void SubtractCulling(Rect clip_bounds);
206
207 virtual void Save(
208 bool create_subpass,
209 uint32_t total_content_depth,
211 const std::shared_ptr<ImageFilter>& backdrop_filter = nullptr);
212
213 void RestoreClip();
214
215 bool AttemptDrawBlurredRRect(const Rect& rect,
216 Size corner_radii,
217 const Paint& paint);
218
219 Canvas(const Canvas&) = delete;
220
221 Canvas& operator=(const Canvas&) = delete;
222};
223
224} // namespace impeller
225
226#endif // FLUTTER_IMPELLER_AIKS_CANVAS_H_
int count
Definition: FontMgrTest.cpp:50
static const int points[]
void DrawLine(const Point &p0, const Point &p1, const Paint &paint)
Definition: canvas.cc:485
static constexpr uint32_t kMaxDepth
Definition: canvas.h:62
void Initialize(std::optional< Rect > cull_rect)
Definition: canvas.cc:164
const Matrix & GetCurrentTransform() const
Definition: canvas.cc:298
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:766
void DrawVertices(const std::shared_ptr< VerticesGeometry > &vertices, BlendMode blend_mode, const Paint &paint)
Definition: canvas.cc:933
void DrawOval(const Rect &rect, const Paint &paint)
Definition: canvas.cc:514
void RestoreToCount(size_t count)
Definition: canvas.cc:335
void ClipPath(const Path &path, Entity::ClipOperation clip_op=Entity::ClipOperation::kIntersect)
Definition: canvas.cc:589
virtual void AddClipEntityToCurrentPass(Entity entity)
Definition: canvas.cc:838
size_t GetClipHeight() const
Definition: canvas.cc:825
void ClipRRect(const Rect &rect, const Size &corner_radii, Entity::ClipOperation clip_op=Entity::ClipOperation::kIntersect)
Definition: canvas.cc:640
virtual bool Restore()
Definition: canvas.cc:257
size_t GetSaveCount() const
Definition: canvas.cc:331
void Concat(const Matrix &transform)
Definition: canvas.cc:282
void Transform(const Matrix &transform)
Definition: canvas.cc:294
std::optional< Rect > initial_cull_rect_
Definition: canvas.h:182
void PreConcat(const Matrix &transform)
Definition: canvas.cc:286
virtual void SaveLayer(const Paint &paint, std::optional< Rect > bounds=std::nullopt, const std::shared_ptr< ImageFilter > &backdrop_filter=nullptr, ContentBoundsPromise bounds_promise=ContentBoundsPromise::kUnknown, uint32_t total_content_depth=kMaxDepth, bool can_distribute_opacity=false)
Definition: canvas.cc:842
void DrawImage(const std::shared_ptr< Image > &image, Point offset, const Paint &paint, SamplerDescriptor sampler={})
Definition: canvas.cc:752
void Rotate(Radians radians)
Definition: canvas.cc:327
std::deque< CanvasStackEntry > transform_stack_
Definition: canvas.h:181
void ResetTransform()
Definition: canvas.cc:290
virtual void DrawTextFrame(const std::shared_ptr< TextFrame > &text_frame, Point position, const Paint &paint)
Definition: canvas.cc:884
virtual ~Canvas()
void DrawPaint(const Paint &paint)
Definition: canvas.cc:352
void ClipRect(const Rect &rect, Entity::ClipOperation clip_op=Entity::ClipOperation::kIntersect)
Definition: canvas.cc:599
void Skew(Scalar sx, Scalar sy)
Definition: canvas.cc:323
uint64_t current_depth_
Definition: canvas.h:183
void Scale(const Vector2 &scale)
Definition: canvas.cc:315
const std::optional< Rect > GetCurrentLocalCullingBounds() const
Definition: canvas.cc:302
void Reset()
Definition: canvas.cc:177
Picture EndRecordingAsPicture()
Definition: canvas.cc:804
void DrawPath(const Path &path, const Paint &paint)
Definition: canvas.cc:343
virtual void Save(uint32_t total_content_depth=kMaxDepth)
Definition: canvas.cc:184
void DrawPoints(std::vector< Point > points, Scalar radius, const Paint &paint, PointStyle point_style)
Definition: canvas.cc:733
void ClipOval(const Rect &bounds, Entity::ClipOperation clip_op=Entity::ClipOperation::kIntersect)
Definition: canvas.cc:620
void DrawRect(const Rect &rect, const Paint &paint)
Definition: canvas.cc:495
void DrawRRect(const Rect &rect, const Size &corner_radii, const Paint &paint)
Definition: canvas.cc:540
virtual void AddRenderEntityToCurrentPass(Entity entity, bool reuse_depth=false)
Definition: canvas.cc:829
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:1020
void Translate(const Vector3 &offset)
Definition: canvas.cc:311
void DrawCircle(const Point &center, Scalar radius, const Paint &paint)
Definition: canvas.cc:566
Paths are lightweight objects that describe a collection of linear, quadratic, or cubic segments....
Definition: path.h:52
const Paint & paint
Definition: color_source.cc:38
SkBitmap source
Definition: examples.cpp:28
sk_sp< const SkImage > atlas
Definition: SkRecords.h:331
Optional< SkRect > bounds
Definition: SkRecords.h:189
sk_sp< const SkImage > image
Definition: SkRecords.h:269
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
PODArray< SkColor > colors
Definition: SkRecords.h:276
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
CanvasPath Path
Definition: dart_ui.cc:58
float Scalar
Definition: scalar.h:18
SourceRectConstraint
Controls the behavior of the source rectangle given to DrawImageRect.
Definition: canvas.h:52
@ kStrict
Sample only within the source rectangle. May be slower.
@ kFast
Faster, but may sample outside the bounds of the source rectangle.
TRect< Scalar > Rect
Definition: rect.h:769
PointStyle
Definition: canvas.h:43
TPoint< Scalar > Point
Definition: point.h:322
BlendMode
Definition: color.h:59
ContentBoundsPromise
Definition: entity_pass.h:28
@ kUnknown
The caller makes no claims related to the size of the bounds.
TSize< Scalar > Size
Definition: size.h:137
SK_API sk_sp< PrecompileShader > Picture()
dest
Definition: zip.py:79
static SkColor4f transform(SkColor4f c, SkColorSpace *src, SkColorSpace *dst)
Definition: p3.cpp:47
const Scalar scale
SeparatedVector2 offset
Entity::RenderingMode rendering_mode
Definition: canvas.h:40
std::optional< Rect > cull_rect
Definition: canvas.h:34
A 4x4 matrix using column-major storage.
Definition: matrix.h:37