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"
16 #include "impeller/aiks/paint.h"
17 #include "impeller/aiks/picture.h"
19 #include "impeller/entity/entity.h"
24 #include "impeller/geometry/path.h"
28 
29 namespace 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 
43 enum 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 
60 class 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,
132  Point offset,
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 = {},
142  SourceRectConstraint src_rect_constraint = SourceRectConstraint::kFast);
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 
178  Picture EndRecordingAsPicture();
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_
impeller::Canvas::DrawPoints
void DrawPoints(std::vector< Point > points, Scalar radius, const Paint &paint, PointStyle point_style)
Definition: canvas.cc:733
impeller::Entity::ClipOperation::kIntersect
@ kIntersect
path.h
impeller::Canvas::EndRecordingAsPicture
Picture EndRecordingAsPicture()
Definition: canvas.cc:804
impeller::Canvas::ClipOval
void ClipOval(const Rect &bounds, Entity::ClipOperation clip_op=Entity::ClipOperation::kIntersect)
Definition: canvas.cc:620
impeller::CanvasStackEntry::distributed_opacity
Scalar distributed_opacity
Definition: canvas.h:39
impeller::Canvas::DrawRRect
void DrawRRect(const Rect &rect, const Size &corner_radii, const Paint &paint)
Definition: canvas.cc:540
point.h
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Canvas::RestoreToCount
void RestoreToCount(size_t count)
Definition: canvas.cc:335
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:766
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:323
impeller::BlendMode
BlendMode
Definition: color.h:59
impeller::PointStyle
PointStyle
Definition: canvas.h:43
impeller::Canvas::DrawTextFrame
virtual void DrawTextFrame(const std::shared_ptr< TextFrame > &text_frame, Point position, const Paint &paint)
Definition: canvas.cc:884
impeller::Canvas
Definition: canvas.h:60
impeller::PointStyle::kRound
@ kRound
Points are drawn as squares.
impeller::Canvas::ResetTransform
void ResetTransform()
Definition: canvas.cc:290
impeller::Canvas::DrawVertices
void DrawVertices(const std::shared_ptr< VerticesGeometry > &vertices, BlendMode blend_mode, const Paint &paint)
Definition: canvas.cc:933
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::CanvasStackEntry::num_clips
size_t num_clips
Definition: canvas.h:38
impeller::Size
TSize< Scalar > Size
Definition: size.h:137
picture.h
impeller::CanvasStackEntry::rendering_mode
Entity::RenderingMode rendering_mode
Definition: canvas.h:40
impeller::Canvas::DrawLine
void DrawLine(const Point &p0, const Point &p1, const Paint &paint)
Definition: canvas.cc:485
impeller::Canvas::initial_cull_rect_
std::optional< Rect > initial_cull_rect_
Definition: canvas.h:182
impeller::Canvas::DrawRect
void DrawRect(const Rect &rect, const Paint &paint)
Definition: canvas.cc:495
offset
SeparatedVector2 offset
Definition: stroke_path_geometry.cc:311
impeller::Canvas::GetCurrentTransform
const Matrix & GetCurrentTransform() const
Definition: canvas.cc:298
impeller::Canvas::Concat
void Concat(const Matrix &transform)
Definition: canvas.cc:282
impeller::Canvas::DrawImage
void DrawImage(const std::shared_ptr< Image > &image, Point offset, const Paint &paint, SamplerDescriptor sampler={})
Definition: canvas.cc:752
impeller::CanvasStackEntry::clip_height
size_t clip_height
Definition: canvas.h:36
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:15
matrix.h
impeller::Canvas::Initialize
void Initialize(std::optional< Rect > cull_rect)
Definition: canvas.cc:164
impeller::Entity
Definition: entity.h:20
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:322
impeller::Canvas::Scale
void Scale(const Vector2 &scale)
Definition: canvas.cc:315
impeller::Path
Paths are lightweight objects that describe a collection of linear, quadratic, or cubic segments....
Definition: path.h:52
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:231
impeller::EntityPass
Definition: entity_pass.h:43
impeller::Canvas::DrawCircle
void DrawCircle(const Point &center, Scalar radius, const Paint &paint)
Definition: canvas.cc:566
geometry.h
impeller::SourceRectConstraint
SourceRectConstraint
Controls the behavior of the source rectangle given to DrawImageRect.
Definition: canvas.h:52
impeller::Canvas::Restore
virtual bool Restore()
Definition: canvas.cc:257
impeller::Radians
Definition: scalar.h:38
impeller::Entity::RenderingMode::kDirect
@ kDirect
impeller::CanvasStackEntry::clip_depth
uint32_t clip_depth
Definition: canvas.h:35
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:1027
entity_pass.h
impeller::Canvas::DrawPath
void DrawPath(const Path &path, const Paint &paint)
Definition: canvas.cc:343
impeller::Canvas::DrawPaint
void DrawPaint(const Paint &paint)
Definition: canvas.cc:352
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:769
impeller::Canvas::GetSaveCount
size_t GetSaveCount() const
Definition: canvas.cc:331
impeller::Canvas::Canvas
Canvas()
Definition: canvas.cc:149
vertices_geometry.h
impeller::Canvas::current_depth_
uint64_t current_depth_
Definition: canvas.h:183
impeller::Canvas::ClipRRect
void ClipRRect(const Rect &rect, const Size &corner_radii, Entity::ClipOperation clip_op=Entity::ClipOperation::kIntersect)
Definition: canvas.cc:640
impeller::Canvas::PreConcat
void PreConcat(const Matrix &transform)
Definition: canvas.cc:286
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:514
impeller::Canvas::GetClipHeight
size_t GetClipHeight() const
Definition: canvas.cc:825
impeller::Canvas::Rotate
void Rotate(Radians radians)
Definition: canvas.cc:327
image.h
vector.h
impeller::TPoint< Scalar >
impeller::Canvas::Transform
void Transform(const Matrix &transform)
Definition: canvas.cc:294
impeller::Canvas::Save
virtual void Save(uint32_t total_content_depth=kMaxDepth)
Definition: canvas.cc:184
impeller::Canvas::GetCurrentLocalCullingBounds
const std::optional< Rect > GetCurrentLocalCullingBounds() const
Definition: canvas.cc:302
scale
const Scalar scale
Definition: stroke_path_geometry.cc:308
paint
const Paint & paint
Definition: color_source.cc:38
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:599
impeller::Entity::RenderingMode
RenderingMode
Definition: entity.h:27
text_frame.h
impeller
Definition: aiks_blend_unittests.cc:18
impeller::Canvas::transform_stack_
std::deque< CanvasStackEntry > transform_stack_
Definition: canvas.h:181
impeller::Canvas::SaveLayer
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
impeller::TRect
Definition: rect.h:122
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::Canvas::~Canvas
virtual ~Canvas()
impeller::Vector3
Definition: vector.h:20
impeller::BlendMode::kSourceOver
@ kSourceOver
impeller::Canvas::Reset
void Reset()
Definition: canvas.cc:177
impeller::Canvas::Translate
void Translate(const Vector3 &offset)
Definition: canvas.cc:311
impeller::Canvas::ClipPath
void ClipPath(const Path &path, Entity::ClipOperation clip_op=Entity::ClipOperation::kIntersect)
Definition: canvas.cc:589
impeller::Canvas::kMaxDepth
static constexpr uint32_t kMaxDepth
Definition: canvas.h:62