| Flutter Impeller |

paint.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_PAINT_H_
6 #define FLUTTER_IMPELLER_AIKS_PAINT_H_
7
8 #include <memory>
9
10 #include "impeller/aiks/color_filter.h"
11 #include "impeller/aiks/color_source.h"
12 #include "impeller/aiks/image_filter.h"
13 #include "impeller/entity/contents/contents.h"
14 #include "impeller/entity/contents/filters/color_filter_contents.h"
15 #include "impeller/entity/contents/filters/filter_contents.h"
16 #include "impeller/entity/contents/texture_contents.h"
17 #include "impeller/entity/entity.h"
18 #include "impeller/entity/geometry/geometry.h"
19 #include "impeller/geometry/color.h"
20
21 namespace impeller {
22
23 struct Paint {
24 using ImageFilterProc = std::function<std::shared_ptr<FilterContents>(
25 FilterInput::Ref,
26 const Matrix& effect_transform,
27 Entity::RenderingMode rendering_mode)>;
28 using MaskFilterProc = std::function<std::shared_ptr<FilterContents>(
29 FilterInput::Ref,
30 bool is_solid_color,
31 const Matrix& effect_transform)>;
32 using ColorSourceProc = std::function<std::shared_ptr<ColorSourceContents>()>;
33
34 /// @brief Whether or not a save layer with the provided paint can perform the
35 /// opacity peephole optimization.
36 static bool CanApplyOpacityPeephole(const Paint& paint) {
37 return paint.blend_mode == BlendMode::kSourceOver &&
38 paint.invert_colors == false &&
39 !paint.mask_blur_descriptor.has_value() &&
40 paint.image_filter == nullptr && paint.color_filter == nullptr;
41 }
42
43 enum class Style {
44 kFill,
45 kStroke,
46 };
47
48 struct MaskBlurDescriptor {
49 FilterContents::BlurStyle style;
50 Sigma sigma;
51 /// Text mask blurs need to not apply the CTM to the blur kernel.
52 /// See: https://github.com/flutter/flutter/issues/115112
53 bool respect_ctm = true;
54
55 std::shared_ptr<FilterContents> CreateMaskBlur(
56 std::shared_ptr<ColorSourceContents> color_source_contents,
57 const std::shared_ptr<ColorFilter>& color_filter) const;
58
59 std::shared_ptr<FilterContents> CreateMaskBlur(
60 std::shared_ptr<TextureContents> texture_contents) const;
61
62 std::shared_ptr<FilterContents> CreateMaskBlur(
63 const FilterInput::Ref& input,
64 bool is_solid_color,
65 const Matrix& ctm) const;
66 };
67
68 Color color = Color::Black();
69 ColorSource color_source;
70
71 Scalar stroke_width = 0.0;
72 Cap stroke_cap = Cap::kButt;
73 Join stroke_join = Join::kMiter;
74 Scalar stroke_miter = 4.0;
75 Style style = Style::kFill;
76 BlendMode blend_mode = BlendMode::kSourceOver;
77 bool invert_colors = false;
78
79 std::shared_ptr<ImageFilter> image_filter;
80 std::shared_ptr<ColorFilter> color_filter;
81 std::optional<MaskBlurDescriptor> mask_blur_descriptor;
82
83 std::shared_ptr<ColorFilter> GetColorFilter() const;
84
85 /// @brief Wrap this paint's configured filters to the given contents.
86 /// @param[in] input The contents to wrap with paint's filters.
87 /// @return The filter-wrapped contents. If there are no filters that need
88 /// to be wrapped for the current paint configuration, the
89 /// original contents is returned.
90 std::shared_ptr<Contents> WithFilters(std::shared_ptr<Contents> input) const;
91
92 /// @brief Wrap this paint's configured filters to the given contents of
93 /// subpass target.
94 /// @param[in] input The contents of subpass target to wrap with paint's
95 /// filters.
96 ///
97 /// @return The filter-wrapped contents. If there are no filters that need
98 /// to be wrapped for the current paint configuration, the
99 /// original contents is returned.
100 std::shared_ptr<Contents> WithFiltersForSubpassTarget(
101 std::shared_ptr<Contents> input,
102 const Matrix& effect_transform = Matrix()) const;
103
104 std::shared_ptr<Contents> CreateContentsForGeometry(
105 const std::shared_ptr<Geometry>& geometry) const;
106
107 /// @brief Whether this paint has a color filter that can apply opacity
108 bool HasColorFilter() const;
109
110 std::shared_ptr<Contents> WithMaskBlur(std::shared_ptr<Contents> input,
111 bool is_solid_color,
112 const Matrix& ctm) const;
113
114 std::shared_ptr<FilterContents> WithImageFilter(
115 const FilterInput::Variant& input,
116 const Matrix& effect_transform,
117 Entity::RenderingMode rendering_mode) const;
118
119 private:
120 std::shared_ptr<Contents> WithColorFilter(
121 std::shared_ptr<Contents> input,
122 ColorFilterContents::AbsorbOpacity absorb_opacity =
123 ColorFilterContents::AbsorbOpacity::kNo) const;
124 };
125
126 } // namespace impeller
127
128 #endif // FLUTTER_IMPELLER_AIKS_PAINT_H_
impeller::Paint::stroke_cap
Cap stroke_cap
Definition: paint.h:72
contents.h
impeller::Scalar
float Scalar
Definition: scalar.h:18
image_filter.h
texture_contents.h
impeller::Paint::Style::kStroke
@ kStroke
entity.h
impeller::Paint
Definition: paint.h:23
impeller::BlendMode
BlendMode
Definition: color.h:59
impeller::FilterContents::BlurStyle
BlurStyle
Definition: filter_contents.h:26
impeller::Color
Definition: color.h:124
impeller::Paint::ImageFilterProc
std::function< std::shared_ptr< FilterContents >(FilterInput::Ref, const Matrix &effect_transform, Entity::RenderingMode rendering_mode)> ImageFilterProc
Definition: paint.h:27
impeller::Paint::MaskBlurDescriptor::sigma
Sigma sigma
Definition: paint.h:50
impeller::Paint::color
Color color
Definition: paint.h:68
impeller::Paint::MaskBlurDescriptor::style
FilterContents::BlurStyle style
Definition: paint.h:49
impeller::FilterInput::Ref
std::shared_ptr< FilterInput > Ref
Definition: filter_input.h:32
impeller::ColorFilterContents::AbsorbOpacity::kNo
@ kNo
impeller::Paint::MaskBlurDescriptor::CreateMaskBlur
std::shared_ptr< FilterContents > CreateMaskBlur(std::shared_ptr< ColorSourceContents > color_source_contents, const std::shared_ptr< ColorFilter > &color_filter) const
Definition: paint.cc:155
impeller::Paint::MaskBlurDescriptor
Definition: paint.h:48
impeller::Cap::kButt
@ kButt
impeller::Paint::color_source
ColorSource color_source
Definition: paint.h:69
impeller::Join::kMiter
@ kMiter
impeller::Paint::stroke_miter
Scalar stroke_miter
Definition: paint.h:74
impeller::Paint::color_filter
std::shared_ptr< ColorFilter > color_filter
Definition: paint.h:80
filter_contents.h
impeller::ColorSource
Definition: color_source.h:99
color_source.h
impeller::Paint::GetColorFilter
std::shared_ptr< ColorFilter > GetColorFilter() const
Definition: paint.cc:222
impeller::Paint::style
Style style
Definition: paint.h:75
impeller::Paint::Style::kFill
@ kFill
impeller::Paint::MaskFilterProc
std::function< std::shared_ptr< FilterContents >(FilterInput::Ref, bool is_solid_color, const Matrix &effect_transform)> MaskFilterProc
Definition: paint.h:31
geometry.h
impeller::Sigma
In filters that use Gaussian distributions, "sigma" is a size of one standard deviation in terms of t...
Definition: sigma.h:32
impeller::FilterInput::Variant
std::variant< std::shared_ptr< FilterContents >, std::shared_ptr< Contents >, std::shared_ptr< Texture >, Rect > Variant
Definition: filter_input.h:37
color_filter_contents.h
impeller::Paint::WithMaskBlur
std::shared_ptr< Contents > WithMaskBlur(std::shared_ptr< Contents > input, bool is_solid_color, const Matrix &ctm) const
Definition: paint.cc:80
impeller::Paint::WithImageFilter
std::shared_ptr< FilterContents > WithImageFilter(const FilterInput::Variant &input, const Matrix &effect_transform, Entity::RenderingMode rendering_mode) const
Definition: paint.cc:90
impeller::Paint::ColorSourceProc
std::function< std::shared_ptr< ColorSourceContents >()> ColorSourceProc
Definition: paint.h:32
impeller::Join
Join
Definition: path.h:24
impeller::Color::Black
static constexpr Color Black()
Definition: color.h:268
impeller::Paint::invert_colors
bool invert_colors
Definition: paint.h:77
paint
const Paint & paint
Definition: color_source.cc:38
color.h
impeller::Paint::HasColorFilter
bool HasColorFilter() const
Whether this paint has a color filter that can apply opacity.
Definition: paint.cc:236
color_filter.h
impeller::Paint::MaskBlurDescriptor::respect_ctm
bool respect_ctm
Definition: paint.h:53
impeller::Paint::Style
Style
Definition: paint.h:43
impeller::Paint::CanApplyOpacityPeephole
static bool CanApplyOpacityPeephole(const Paint &paint)
Whether or not a save layer with the provided paint can perform the opacity peephole optimization.
Definition: paint.h:36
impeller::Entity::RenderingMode
RenderingMode
Definition: entity.h:27
impeller::ColorFilterContents::AbsorbOpacity
AbsorbOpacity
Definition: color_filter_contents.h:14
impeller
Definition: aiks_blend_unittests.cc:18
impeller::Paint::mask_blur_descriptor
std::optional< MaskBlurDescriptor > mask_blur_descriptor
Definition: paint.h:81
impeller::Paint::stroke_width
Scalar stroke_width
Definition: paint.h:71
impeller::Paint::WithFilters
std::shared_ptr< Contents > WithFilters(std::shared_ptr< Contents > input) const
Wrap this paint's configured filters to the given contents.
Definition: paint.cc:56
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::Paint::WithFiltersForSubpassTarget
std::shared_ptr< Contents > WithFiltersForSubpassTarget(std::shared_ptr< Contents > input, const Matrix &effect_transform=Matrix()) const
Wrap this paint's configured filters to the given contents of subpass target.
Definition: paint.cc:67
impeller::BlendMode::kSourceOver
@ kSourceOver
impeller::Paint::blend_mode
BlendMode blend_mode
Definition: paint.h:76
impeller::Cap
Cap
Definition: path.h:18
impeller::Paint::CreateContentsForGeometry
std::shared_ptr< Contents > CreateContentsForGeometry(const std::shared_ptr< Geometry > &geometry) const
Definition: paint.cc:30
impeller::Paint::image_filter
std::shared_ptr< ImageFilter > image_filter
Definition: paint.h:79
impeller::Paint::stroke_join
Join stroke_join
Definition: paint.h:73