| 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_DISPLAY_LIST_PAINT_H_
6 #define FLUTTER_IMPELLER_DISPLAY_LIST_PAINT_H_
7
8 #include <memory>
9
10 #include "display_list/effects/dl_color_filter.h"
11 #include "display_list/effects/dl_color_sources.h"
12 #include "display_list/effects/dl_image_filter.h"
13 #include "impeller/display_list/color_filter.h"
14 #include "impeller/display_list/image_filter.h"
15 #include "impeller/entity/contents/color_source_contents.h"
16 #include "impeller/entity/contents/contents.h"
17 #include "impeller/entity/contents/filters/color_filter_contents.h"
18 #include "impeller/entity/contents/filters/filter_contents.h"
19 #include "impeller/entity/contents/texture_contents.h"
20 #include "impeller/entity/entity.h"
21 #include "impeller/entity/geometry/geometry.h"
22 #include "impeller/geometry/color.h"
23 #include "impeller/geometry/stroke_parameters.h"
24
25 namespace impeller {
26
27 struct Paint {
28 using ImageFilterProc = std::function<std::shared_ptr<FilterContents>(
29 FilterInput::Ref,
30 const Matrix& effect_transform,
31 Entity::RenderingMode rendering_mode)>;
32 using MaskFilterProc = std::function<std::shared_ptr<FilterContents>(
33 FilterInput::Ref,
34 bool is_solid_color,
35 const Matrix& effect_transform)>;
36 using ColorSourceProc = std::function<std::shared_ptr<ColorSourceContents>()>;
37
38 /// @brief Whether or not a save layer with the provided paint can perform the
39 /// opacity peephole optimization.
40 static bool CanApplyOpacityPeephole(const Paint& paint) {
41 return paint.blend_mode == BlendMode::kSrcOver &&
42 paint.invert_colors == false &&
43 !paint.mask_blur_descriptor.has_value() &&
44 paint.image_filter == nullptr && paint.color_filter == nullptr;
45 }
46
47 enum class Style {
48 kFill,
49 kStroke,
50 };
51
52 struct MaskBlurDescriptor {
53 FilterContents::BlurStyle style;
54 Sigma sigma;
55 /// Text mask blurs need to not apply the CTM to the blur kernel.
56 /// See: https://github.com/flutter/flutter/issues/115112
57 bool respect_ctm = true;
58
59 std::shared_ptr<FilterContents> CreateMaskBlur(
60 std::shared_ptr<ColorSourceContents> color_source_contents,
61 const flutter::DlColorFilter* color_filter,
62 bool invert_colors,
63 FillRectGeometry* rect_geom) const;
64
65 std::shared_ptr<FilterContents> CreateMaskBlur(
66 std::shared_ptr<TextureContents> texture_contents,
67 FillRectGeometry* rect_geom) const;
68
69 std::shared_ptr<FilterContents> CreateMaskBlur(
70 const FilterInput::Ref& input,
71 bool is_solid_color,
72 const Matrix& ctm) const;
73 };
74
75 Color color = Color::Black();
76 const flutter::DlColorSource* color_source = nullptr;
77 const flutter::DlColorFilter* color_filter = nullptr;
78 const flutter::DlImageFilter* image_filter = nullptr;
79
80 StrokeParameters stroke;
81 Style style = Style::kFill;
82 BlendMode blend_mode = BlendMode::kSrcOver;
83 bool invert_colors = false;
84
85 std::optional<MaskBlurDescriptor> mask_blur_descriptor;
86
87 /// @brief Wrap this paint's configured filters to the given contents.
88 /// @param[in] input The contents to wrap with paint's filters.
89 /// @return The filter-wrapped contents. If there are no filters that need
90 /// to be wrapped for the current paint configuration, the
91 /// original contents is returned.
92 std::shared_ptr<Contents> WithFilters(std::shared_ptr<Contents> input) const;
93
94 /// @brief Wrap this paint's configured filters to the given contents of
95 /// subpass target.
96 /// @param[in] input The contents of subpass target to wrap with paint's
97 /// filters.
98 ///
99 /// @return The filter-wrapped contents. If there are no filters that need
100 /// to be wrapped for the current paint configuration, the
101 /// original contents is returned.
102 std::shared_ptr<Contents> WithFiltersForSubpassTarget(
103 std::shared_ptr<Contents> input,
104 const Matrix& effect_transform = Matrix()) const;
105
106 /// @brief Whether this paint has a color filter that can apply opacity
107 bool HasColorFilter() const;
108
109 std::shared_ptr<ColorSourceContents> CreateContents() const;
110
111 std::shared_ptr<Contents> WithMaskBlur(std::shared_ptr<Contents> input,
112 bool is_solid_color,
113 const Matrix& ctm) const;
114
115 std::shared_ptr<FilterContents> WithImageFilter(
116 const FilterInput::Variant& input,
117 const Matrix& effect_transform,
118 Entity::RenderingMode rendering_mode) const;
119
120 /// @brief Convert display list colors + stops into impeller colors and
121 /// stops, taking care to ensure that the stops monotonically
122 /// increase from 0.0 to 1.0.
123 ///
124 /// The general process is:
125 /// * Ensure that the first gradient stop value is 0.0. If not, insert a
126 /// new stop with a value of 0.0 and use the first gradient color as this
127 /// new stops color.
128 /// * Ensure the last gradient stop value is 1.0. If not, insert a new stop
129 /// with a value of 1.0 and use the last gradient color as this stops color.
130 /// * Clamp all gradient values between the values of 0.0 and 1.0.
131 /// * For all stop values, ensure that the values are monotonically
132 /// increasing by clamping each value to a minimum of the previous stop
133 /// value and itself. For example, with stop values of 0.0, 0.5, 0.4, 1.0,
134 /// we would clamp such that the values were 0.0, 0.5, 0.5, 1.0.
135 static void ConvertStops(const flutter::DlGradientColorSourceBase* gradient,
136 std::vector<Color>& colors,
137 std::vector<float>& stops);
138
139 private:
140 std::shared_ptr<Contents> WithColorFilter(
141 std::shared_ptr<Contents> input,
142 ColorFilterContents::AbsorbOpacity absorb_opacity =
143 ColorFilterContents::AbsorbOpacity::kNo) const;
144 };
145
146 } // namespace impeller
147
148 #endif // FLUTTER_IMPELLER_DISPLAY_LIST_PAINT_H_
impeller::ColorFilterContents::AbsorbOpacity
AbsorbOpacity
Definition: color_filter_contents.h:14
impeller::ColorFilterContents::AbsorbOpacity::kNo
@ kNo
impeller::Entity::RenderingMode
RenderingMode
Definition: entity.h:33
impeller::FillRectGeometry
Definition: rect_geometry.h:13
impeller::FilterContents::BlurStyle
BlurStyle
Definition: filter_contents.h:28
impeller::FilterInput::Ref
std::shared_ptr< FilterInput > Ref
Definition: filter_input.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.h
color_filter_contents.h
color_source_contents.h
contents.h
color_filter.h
image_filter.h
entity.h
filter_contents.h
geometry.h
impeller
Definition: allocation.cc:12
impeller::BlendMode
BlendMode
Definition: color.h:58
impeller::BlendMode::kSrcOver
@ kSrcOver
stroke_parameters.h
impeller::Color
Definition: color.h:124
impeller::Color::Black
static constexpr Color Black()
Definition: color.h:266
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::Paint::MaskBlurDescriptor
Definition: paint.h:52
impeller::Paint::MaskBlurDescriptor::respect_ctm
bool respect_ctm
Definition: paint.h:57
impeller::Paint::MaskBlurDescriptor::style
FilterContents::BlurStyle style
Definition: paint.h:53
impeller::Paint::MaskBlurDescriptor::CreateMaskBlur
std::shared_ptr< FilterContents > CreateMaskBlur(std::shared_ptr< ColorSourceContents > color_source_contents, const flutter::DlColorFilter *color_filter, bool invert_colors, FillRectGeometry *rect_geom) const
Definition: paint.cc:395
impeller::Paint::MaskBlurDescriptor::sigma
Sigma sigma
Definition: paint.h:54
impeller::Paint
Definition: paint.h:27
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:278
impeller::Paint::color_filter
const flutter::DlColorFilter * color_filter
Definition: paint.h:77
impeller::Paint::color_source
const flutter::DlColorSource * color_source
Definition: paint.h:76
impeller::Paint::Style
Style
Definition: paint.h:47
impeller::Paint::Style::kStroke
@ kStroke
impeller::Paint::Style::kFill
@ kFill
impeller::Paint::ImageFilterProc
std::function< std::shared_ptr< FilterContents >(FilterInput::Ref, const Matrix &effect_transform, Entity::RenderingMode rendering_mode)> ImageFilterProc
Definition: paint.h:31
impeller::Paint::ConvertStops
static void ConvertStops(const flutter::DlGradientColorSourceBase *gradient, std::vector< Color > &colors, std::vector< float > &stops)
Convert display list colors + stops into impeller colors and stops, taking care to ensure that the st...
Definition: paint.cc:37
impeller::Paint::image_filter
const flutter::DlImageFilter * image_filter
Definition: paint.h:78
impeller::Paint::ColorSourceProc
std::function< std::shared_ptr< ColorSourceContents >()> ColorSourceProc
Definition: paint.h:36
impeller::Paint::style
Style style
Definition: paint.h:81
impeller::Paint::WithMaskBlur
std::shared_ptr< Contents > WithMaskBlur(std::shared_ptr< Contents > input, bool is_solid_color, const Matrix &ctm) const
Definition: paint.cc:302
impeller::Paint::invert_colors
bool invert_colors
Definition: paint.h:83
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:40
impeller::Paint::mask_blur_descriptor
std::optional< MaskBlurDescriptor > mask_blur_descriptor
Definition: paint.h:85
impeller::Paint::color
Color color
Definition: paint.h:75
impeller::Paint::blend_mode
BlendMode blend_mode
Definition: paint.h:82
impeller::Paint::MaskFilterProc
std::function< std::shared_ptr< FilterContents >(FilterInput::Ref, bool is_solid_color, const Matrix &effect_transform)> MaskFilterProc
Definition: paint.h:35
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:289
impeller::Paint::WithImageFilter
std::shared_ptr< FilterContents > WithImageFilter(const FilterInput::Variant &input, const Matrix &effect_transform, Entity::RenderingMode rendering_mode) const
Definition: paint.cc:312
impeller::Paint::stroke
StrokeParameters stroke
Definition: paint.h:80
impeller::Paint::CreateContents
std::shared_ptr< ColorSourceContents > CreateContents() const
Definition: paint.cc:62
impeller::Paint::HasColorFilter
bool HasColorFilter() const
Whether this paint has a color filter that can apply opacity.
Definition: paint.cc:469
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::StrokeParameters
A structure to store all of the parameters related to stroking a path or basic geometry object.
Definition: stroke_parameters.h:28
texture_contents.h