| 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 #pragma once
6
7 #include <deque>
8 #include <functional>
9 #include <memory>
10 #include <optional>
11 #include <vector>
12
13 #include "flutter/fml/macros.h"
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_pass.h"
20 #include "impeller/entity/geometry/geometry.h"
21 #include "impeller/entity/geometry/vertices_geometry.h"
22 #include "impeller/geometry/matrix.h"
23 #include "impeller/geometry/path.h"
24 #include "impeller/geometry/point.h"
25 #include "impeller/geometry/vector.h"
26 #include "impeller/typographer/glyph_atlas.h"
27 #include "impeller/typographer/text_frame.h"
28
29 namespace impeller {
30
31 class Entity;
32
33 struct CanvasStackEntry {
34 Matrix xformation;
35 // |cull_rect| is conservative screen-space bounds of the clipped output area
36 std::optional<Rect> cull_rect;
37 size_t stencil_depth = 0u;
38 Entity::RenderingMode rendering_mode = Entity::RenderingMode::kDirect;
39 bool contains_clips = false;
40 };
41
42 enum class PointStyle {
43 /// @brief Points are drawn as squares.
44 kRound,
45
46 /// @brief Points are drawn as circles.
47 kSquare,
48 };
49
50 class Canvas {
51 public:
52 struct DebugOptions {
53 /// When enabled, layers that are rendered to an offscreen texture
54 /// internally get a translucent checkerboard pattern painted over them.
55 ///
56 /// Requires the `IMPELLER_DEBUG` preprocessor flag.
57 bool offscreen_texture_checkerboard = false;
58 } debug_options;
59
60 Canvas();
61
62 explicit Canvas(Rect cull_rect);
63
64 explicit Canvas(IRect cull_rect);
65
66 ~Canvas();
67
68 void Save();
69
70 void SaveLayer(const Paint& paint,
71 std::optional<Rect> bounds = std::nullopt,
72 const std::shared_ptr<ImageFilter>& backdrop_filter = nullptr);
73
74 bool Restore();
75
76 size_t GetSaveCount() const;
77
78 void RestoreToCount(size_t count);
79
80 const Matrix& GetCurrentTransformation() const;
81
82 const std::optional<Rect> GetCurrentLocalCullingBounds() const;
83
84 void ResetTransform();
85
86 void Transform(const Matrix& xformation);
87
88 void Concat(const Matrix& xformation);
89
90 void PreConcat(const Matrix& xformation);
91
92 void Translate(const Vector3& offset);
93
94 void Scale(const Vector2& scale);
95
96 void Scale(const Vector3& scale);
97
98 void Skew(Scalar sx, Scalar sy);
99
100 void Rotate(Radians radians);
101
102 void DrawPath(const Path& path, const Paint& paint);
103
104 void DrawPaint(const Paint& paint);
105
106 void DrawRect(Rect rect, const Paint& paint);
107
108 void DrawRRect(Rect rect, Scalar corner_radius, const Paint& paint);
109
110 void DrawCircle(Point center, Scalar radius, const Paint& paint);
111
112 void DrawPoints(std::vector<Point>,
113 Scalar radius,
114 const Paint& paint,
115 PointStyle point_style);
116
117 void DrawImage(const std::shared_ptr<Image>& image,
118 Point offset,
119 const Paint& paint,
120 SamplerDescriptor sampler = {});
121
122 void DrawImageRect(const std::shared_ptr<Image>& image,
123 Rect source,
124 Rect dest,
125 const Paint& paint,
126 SamplerDescriptor sampler = {});
127
128 void ClipPath(
129 const Path& path,
130 Entity::ClipOperation clip_op = Entity::ClipOperation::kIntersect);
131
132 void ClipRect(
133 const Rect& rect,
134 Entity::ClipOperation clip_op = Entity::ClipOperation::kIntersect);
135
136 void ClipRRect(
137 const Rect& rect,
138 Scalar corner_radius,
139 Entity::ClipOperation clip_op = Entity::ClipOperation::kIntersect);
140
141 void DrawPicture(const Picture& picture);
142
143 void DrawTextFrame(const std::shared_ptr<TextFrame>& text_frame,
144 Point position,
145 const Paint& paint);
146
147 void DrawVertices(const std::shared_ptr<VerticesGeometry>& vertices,
148 BlendMode blend_mode,
149 const Paint& paint);
150
151 void DrawAtlas(const std::shared_ptr<Image>& atlas,
152 std::vector<Matrix> transforms,
153 std::vector<Rect> texture_coordinates,
154 std::vector<Color> colors,
155 BlendMode blend_mode,
156 SamplerDescriptor sampler,
157 std::optional<Rect> cull_rect,
158 const Paint& paint);
159
160 Picture EndRecordingAsPicture();
161
162 private:
163 std::unique_ptr<EntityPass> base_pass_;
164 EntityPass* current_pass_ = nullptr;
165 std::deque<CanvasStackEntry> xformation_stack_;
166 std::optional<Rect> initial_cull_rect_;
167
168 void Initialize(std::optional<Rect> cull_rect);
169
170 void Reset();
171
172 EntityPass& GetCurrentPass();
173
174 size_t GetStencilDepth() const;
175
176 void ClipGeometry(std::unique_ptr<Geometry> geometry,
177 Entity::ClipOperation clip_op);
178
179 void IntersectCulling(Rect clip_bounds);
180 void SubtractCulling(Rect clip_bounds);
181
182 void Save(bool create_subpass,
183 BlendMode = BlendMode::kSourceOver,
184 const std::shared_ptr<ImageFilter>& backdrop_filter = nullptr);
185
186 void RestoreClip();
187
188 bool AttemptDrawBlurredRRect(const Rect& rect,
189 Scalar corner_radius,
190 const Paint& paint);
191
192 FML_DISALLOW_COPY_AND_ASSIGN(Canvas);
193 };
194
195 } // namespace impeller
impeller::BlendMode
BlendMode
Definition: color.h:57
impeller::Entity::ClipOperation::kIntersect
@ kIntersect
path.h
impeller::Canvas::EndRecordingAsPicture
Picture EndRecordingAsPicture()
Definition: canvas.cc:513
point.h
impeller::Scalar
float Scalar
Definition: scalar.h:15
impeller::Canvas::RestoreToCount
void RestoreToCount(size_t count)
Definition: canvas.cc:165
image_filter.h
impeller::CanvasStackEntry
Definition: canvas.h:33
impeller::Paint
Definition: paint.h:25
impeller::CanvasStackEntry::cull_rect
std::optional< Rect > cull_rect
Definition: canvas.h:36
impeller::Canvas::Skew
void Skew(Scalar sx, Scalar sy)
Definition: canvas.cc:153
impeller::PointStyle
PointStyle
Definition: canvas.h:42
impeller::Canvas::DrawTextFrame
void DrawTextFrame(const std::shared_ptr< TextFrame > &text_frame, Point position, const Paint &paint)
Definition: canvas.cc:549
impeller::Canvas
Definition: canvas.h:50
impeller::PointStyle::kRound
@ kRound
Points are drawn as squares.
impeller::Canvas::ResetTransform
void ResetTransform()
Definition: canvas.cc:120
impeller::Canvas::DrawVertices
void DrawVertices(const std::shared_ptr< VerticesGeometry > &vertices, BlendMode blend_mode, const Paint &paint)
Definition: canvas.cc:592
impeller::CanvasStackEntry::contains_clips
bool contains_clips
Definition: canvas.h:39
impeller::Canvas::DebugOptions::offscreen_texture_checkerboard
bool offscreen_texture_checkerboard
Definition: canvas.h:57
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:532
impeller::CanvasStackEntry::rendering_mode
Entity::RenderingMode rendering_mode
Definition: canvas.h:38
impeller::Canvas::DrawImage
void DrawImage(const std::shared_ptr< Image > &image, Point offset, const Paint &paint, SamplerDescriptor sampler={})
Definition: canvas.cc:467
impeller::Canvas::DrawPicture
void DrawPicture(const Picture &picture)
Definition: canvas.cc:437
impeller::SamplerDescriptor
Definition: sampler_descriptor.h:18
matrix.h
impeller::Canvas::DrawImageRect
void DrawImageRect(const std::shared_ptr< Image > &image, Rect source, Rect dest, const Paint &paint, SamplerDescriptor sampler={})
Definition: canvas.cc:482
impeller::PointStyle::kSquare
@ kSquare
Points are drawn as circles.
impeller::Point
TPoint< Scalar > Point
Definition: point.h:306
impeller::BlendMode::kSourceOver
@ kSourceOver
impeller::Canvas::Scale
void Scale(const Vector2 &scale)
Definition: canvas.cc:145
impeller::Path
Paths are lightweight objects that describe a collection of linear, quadratic, or cubic segments....
Definition: path.h:54
impeller::Canvas::Save
void Save()
Definition: canvas.cc:55
impeller::Canvas::DrawCircle
void DrawCircle(Point center, Scalar radius, const Paint &paint)
Definition: canvas.cc:271
impeller::Canvas::DrawRRect
void DrawRRect(Rect rect, Scalar corner_radius, const Paint &paint)
Definition: canvas.cc:249
geometry.h
impeller::Canvas::Restore
bool Restore()
Definition: canvas.cc:91
impeller::Radians
Definition: scalar.h:35
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:653
entity_pass.h
impeller::Canvas::DrawPath
void DrawPath(const Path &path, const Paint &paint)
Definition: canvas.cc:173
impeller::Canvas::DrawPaint
void DrawPaint(const Paint &paint)
Definition: canvas.cc:183
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:306
impeller::Canvas::GetSaveCount
size_t GetSaveCount() const
Definition: canvas.cc:161
impeller::Canvas::PreConcat
void PreConcat(const Matrix &xformation)
Definition: canvas.cc:116
impeller::Canvas::Canvas
Canvas()
Definition: canvas.cc:25
vertices_geometry.h
impeller::Canvas::Transform
void Transform(const Matrix &xformation)
Definition: canvas.cc:124
sampler_descriptor.h
impeller::Canvas::ClipRRect
void ClipRRect(const Rect &rect, Scalar corner_radius, Entity::ClipOperation clip_op=Entity::ClipOperation::kIntersect)
Definition: canvas.cc:315
impeller::Canvas::Rotate
void Rotate(Radians radians)
Definition: canvas.cc:157
image.h
vector.h
impeller::Canvas::GetCurrentTransformation
const Matrix & GetCurrentTransformation() const
Definition: canvas.cc:128
impeller::TPoint< Scalar >
impeller::Canvas::GetCurrentLocalCullingBounds
const std::optional< Rect > GetCurrentLocalCullingBounds() const
Definition: canvas.cc:132
impeller::CanvasStackEntry::stencil_depth
size_t stencil_depth
Definition: canvas.h:37
impeller::Canvas::Concat
void Concat(const Matrix &xformation)
Definition: canvas.cc:112
impeller::Entity::ClipOperation
ClipOperation
Definition: entity.h:59
paint.h
impeller::Canvas::DrawRect
void DrawRect(Rect rect, const Paint &paint)
Definition: canvas.cc:229
impeller::Canvas::DrawPoints
void DrawPoints(std::vector< Point >, Scalar radius, const Paint &paint, PointStyle point_style)
Definition: canvas.cc:418
impeller::Canvas::ClipRect
void ClipRect(const Rect &rect, Entity::ClipOperation clip_op=Entity::ClipOperation::kIntersect)
Definition: canvas.cc:294
impeller::Entity::RenderingMode
RenderingMode
Definition: entity.h:26
glyph_atlas.h
text_frame.h
impeller
Definition: aiks_context.cc:10
impeller::TRect
Definition: rect.h:20
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:36
impeller::Canvas::~Canvas
~Canvas()
impeller::Vector3
Definition: vector.h:17
impeller::CanvasStackEntry::xformation
Matrix xformation
Definition: canvas.h:34
impeller::Canvas::DebugOptions
Definition: canvas.h:52
impeller::Canvas::Translate
void Translate(const Vector3 &offset)
Definition: canvas.cc:141
impeller::Canvas::ClipPath
void ClipPath(const Path &path, Entity::ClipOperation clip_op=Entity::ClipOperation::kIntersect)
Definition: canvas.cc:284