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