Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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;
40};
41
42enum class PointStyle {
43 /// @brief Points are drawn as squares.
44 kRound,
45
46 /// @brief Points are drawn as circles.
47 kSquare,
48};
49
50/// Controls the behavior of the source rectangle given to DrawImageRect.
52 /// @brief Faster, but may sample outside the bounds of the source rectangle.
53 kFast,
54
55 /// @brief Sample only within the source rectangle. May be slower.
56 kStrict,
57};
58
59class Canvas {
60 public:
61 static constexpr uint32_t kMaxDepth = 1 << 24;
62
63 struct DebugOptions {
64 /// When enabled, layers that are rendered to an offscreen texture
65 /// internally get a translucent checkerboard pattern painted over them.
66 ///
67 /// Requires the `IMPELLER_DEBUG` preprocessor flag.
70
71 Canvas();
72
73 explicit Canvas(Rect cull_rect);
74
75 explicit Canvas(IRect cull_rect);
76
77 virtual ~Canvas();
78
79 virtual void Save(uint32_t total_content_depth = kMaxDepth);
80
81 virtual void SaveLayer(
82 const Paint& paint,
83 std::optional<Rect> bounds = std::nullopt,
84 const std::shared_ptr<ImageFilter>& backdrop_filter = nullptr,
86 uint32_t total_content_depth = kMaxDepth);
87
88 virtual bool Restore();
89
90 size_t GetSaveCount() const;
91
92 void RestoreToCount(size_t count);
93
94 const Matrix& GetCurrentTransform() const;
95
96 const std::optional<Rect> GetCurrentLocalCullingBounds() const;
97
98 void ResetTransform();
99
100 void Transform(const Matrix& transform);
101
102 void Concat(const Matrix& transform);
103
104 void PreConcat(const Matrix& transform);
105
106 void Translate(const Vector3& offset);
107
108 void Scale(const Vector2& scale);
109
110 void Scale(const Vector3& scale);
111
112 void Skew(Scalar sx, Scalar sy);
113
114 void Rotate(Radians radians);
115
116 void DrawPath(const Path& path, const Paint& paint);
117
118 void DrawPaint(const Paint& paint);
119
120 void DrawLine(const Point& p0, const Point& p1, const Paint& paint);
121
122 void DrawRect(const Rect& rect, const Paint& paint);
123
124 void DrawOval(const Rect& rect, const Paint& paint);
125
126 void DrawRRect(const Rect& rect,
127 const Size& corner_radii,
128 const Paint& paint);
129
130 void DrawCircle(const Point& center, Scalar radius, const Paint& paint);
131
132 void DrawPoints(std::vector<Point> points,
133 Scalar radius,
134 const Paint& paint,
135 PointStyle point_style);
136
137 void DrawImage(const std::shared_ptr<Image>& image,
139 const Paint& paint,
140 SamplerDescriptor sampler = {});
141
142 void DrawImageRect(
143 const std::shared_ptr<Image>& image,
144 Rect source,
145 Rect dest,
146 const Paint& paint,
147 SamplerDescriptor sampler = {},
149
150 void ClipPath(
151 const Path& path,
153
154 void ClipRect(
155 const Rect& rect,
157
158 void ClipOval(
159 const Rect& bounds,
161
162 void ClipRRect(
163 const Rect& rect,
164 const Size& corner_radii,
166
167 virtual void DrawTextFrame(const std::shared_ptr<TextFrame>& text_frame,
168 Point position,
169 const Paint& paint);
170
171 void DrawVertices(const std::shared_ptr<VerticesGeometry>& vertices,
172 BlendMode blend_mode,
173 const Paint& paint);
174
175 void DrawAtlas(const std::shared_ptr<Image>& atlas,
176 std::vector<Matrix> transforms,
177 std::vector<Rect> texture_coordinates,
178 std::vector<Color> colors,
179 BlendMode blend_mode,
180 SamplerDescriptor sampler,
181 std::optional<Rect> cull_rect,
182 const Paint& paint);
183
184 Picture EndRecordingAsPicture();
185
186 protected:
187 std::deque<CanvasStackEntry> transform_stack_;
188 std::optional<Rect> initial_cull_rect_;
189 uint64_t current_depth_ = 0u;
190
191 size_t GetClipHeight() const;
192
193 void Initialize(std::optional<Rect> cull_rect);
194
195 void Reset();
196
197 private:
198 std::unique_ptr<EntityPass> base_pass_;
199 EntityPass* current_pass_ = nullptr;
200
201 EntityPass& GetCurrentPass();
202
203 virtual void AddRenderEntityToCurrentPass(Entity entity,
204 bool reuse_depth = false);
205 virtual void AddClipEntityToCurrentPass(Entity entity);
206
207 void ClipGeometry(const std::shared_ptr<Geometry>& geometry,
208 Entity::ClipOperation clip_op);
209
210 void IntersectCulling(Rect clip_bounds);
211 void SubtractCulling(Rect clip_bounds);
212
213 virtual void Save(
214 bool create_subpass,
215 uint32_t total_content_depth,
217 const std::shared_ptr<ImageFilter>& backdrop_filter = nullptr);
218
219 void RestoreClip();
220
221 bool AttemptDrawBlurredRRect(const Rect& rect,
222 Size corner_radii,
223 const Paint& paint);
224
225 Canvas(const Canvas&) = delete;
226
227 Canvas& operator=(const Canvas&) = delete;
228};
229
230} // namespace impeller
231
232#endif // FLUTTER_IMPELLER_AIKS_CANVAS_H_
int count
static const int points[]
static SkScalar center(float pos0, float pos1)
void DrawLine(const Point &p0, const Point &p1, const Paint &paint)
Definition canvas.cc:483
static constexpr uint32_t kMaxDepth
Definition canvas.h:61
void Initialize(std::optional< Rect > cull_rect)
Definition canvas.cc:164
const Matrix & GetCurrentTransform() const
Definition canvas.cc:296
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:764
void DrawVertices(const std::shared_ptr< VerticesGeometry > &vertices, BlendMode blend_mode, const Paint &paint)
Definition canvas.cc:916
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)
Definition canvas.cc:839
void DrawOval(const Rect &rect, const Paint &paint)
Definition canvas.cc:512
void RestoreToCount(size_t count)
Definition canvas.cc:333
void ClipPath(const Path &path, Entity::ClipOperation clip_op=Entity::ClipOperation::kIntersect)
Definition canvas.cc:587
virtual void AddClipEntityToCurrentPass(Entity entity)
Definition canvas.cc:835
size_t GetClipHeight() const
Definition canvas.cc:823
void ClipRRect(const Rect &rect, const Size &corner_radii, Entity::ClipOperation clip_op=Entity::ClipOperation::kIntersect)
Definition canvas.cc:638
virtual bool Restore()
Definition canvas.cc:257
size_t GetSaveCount() const
Definition canvas.cc:329
void Concat(const Matrix &transform)
Definition canvas.cc:280
void Transform(const Matrix &transform)
Definition canvas.cc:292
std::optional< Rect > initial_cull_rect_
Definition canvas.h:188
void PreConcat(const Matrix &transform)
Definition canvas.cc:284
void DrawImage(const std::shared_ptr< Image > &image, Point offset, const Paint &paint, SamplerDescriptor sampler={})
Definition canvas.cc:750
void Rotate(Radians radians)
Definition canvas.cc:325
std::deque< CanvasStackEntry > transform_stack_
Definition canvas.h:187
void ResetTransform()
Definition canvas.cc:288
virtual void DrawTextFrame(const std::shared_ptr< TextFrame > &text_frame, Point position, const Paint &paint)
Definition canvas.cc:875
virtual ~Canvas()
void DrawPaint(const Paint &paint)
Definition canvas.cc:350
void Skew(Scalar sx, Scalar sy)
Definition canvas.cc:321
uint64_t current_depth_
Definition canvas.h:189
const std::optional< Rect > GetCurrentLocalCullingBounds() const
Definition canvas.cc:300
Picture EndRecordingAsPicture()
Definition canvas.cc:802
void DrawPath(const Path &path, const Paint &paint)
Definition canvas.cc:341
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:731
void ClipOval(const Rect &bounds, Entity::ClipOperation clip_op=Entity::ClipOperation::kIntersect)
Definition canvas.cc:618
void DrawRect(const Rect &rect, const Paint &paint)
Definition canvas.cc:493
void DrawRRect(const Rect &rect, const Size &corner_radii, const Paint &paint)
Definition canvas.cc:538
virtual void AddRenderEntityToCurrentPass(Entity entity, bool reuse_depth=false)
Definition canvas.cc:827
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:1003
void Translate(const Vector3 &offset)
Definition canvas.cc:309
void DrawCircle(const Point &center, Scalar radius, const Paint &paint)
Definition canvas.cc:564
struct impeller::Canvas::DebugOptions debug_options
Paths are lightweight objects that describe a collection of linear, quadratic, or cubic segments....
Definition path.h:51
const Paint & paint
sk_sp< SkImage > image
Definition examples.cpp:29
SkBitmap source
Definition examples.cpp:28
float Scalar
Definition scalar.h:18
SourceRectConstraint
Controls the behavior of the source rectangle given to DrawImageRect.
Definition canvas.h:51
@ 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:746
PointStyle
Definition canvas.h:42
@ kRound
Points are drawn as squares.
@ kSquare
Points are drawn as circles.
TPoint< Scalar > Point
Definition point.h:316
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
static SkColor4f transform(SkColor4f c, SkColorSpace *src, SkColorSpace *dst)
Definition p3.cpp:47
const Scalar scale
Point offset
Entity::RenderingMode rendering_mode
Definition canvas.h:39
std::optional< Rect > cull_rect
Definition canvas.h:34
A 4x4 matrix using column-major storage.
Definition matrix.h:37