| 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_source.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
24 namespace impeller {
25
26 struct Paint {
27 using ImageFilterProc = std::function<std::shared_ptr<FilterContents>(
28 FilterInput::Ref,
29 const Matrix& effect_transform,
30 Entity::RenderingMode rendering_mode)>;
31 using MaskFilterProc = std::function<std::shared_ptr<FilterContents>(
32 FilterInput::Ref,
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::kSourceOver &&
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
51 struct MaskBlurDescriptor {
52 FilterContents::BlurStyle style;
53 Sigma sigma;
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
74 Color color = Color::Black();
75 const flutter::DlColorSource* color_source = nullptr;
76 const flutter::DlColorFilter* color_filter = nullptr;
77 const flutter::DlImageFilter* image_filter = nullptr;
78
79 Scalar stroke_width = 0.0;
80 Cap stroke_cap = Cap::kButt;
81 Join stroke_join = Join::kMiter;
82 Scalar stroke_miter = 4.0;
83 Style style = Style::kFill;
84 BlendMode blend_mode = BlendMode::kSourceOver;
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 private:
123 std::shared_ptr<Contents> WithColorFilter(
124 std::shared_ptr<Contents> input,
125 ColorFilterContents::AbsorbOpacity absorb_opacity =
126 ColorFilterContents::AbsorbOpacity::kNo) const;
127 };
128
129 } // namespace impeller
130
131 #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:27
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
impeller::RectGeometry
Definition: rect_geometry.h:12
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::Join
Join
Definition: path.h:25
impeller::Join::kMiter
@ kMiter
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Cap
Cap
Definition: path.h:19
impeller::Cap::kButt
@ kButt
impeller::BlendMode
BlendMode
Definition: color.h:58
impeller::BlendMode::kSourceOver
@ kSourceOver
impeller::Color
Definition: color.h:123
impeller::Color::Black
static constexpr Color Black()
Definition: color.h:265
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::Paint::MaskBlurDescriptor
Definition: paint.h:51
impeller::Paint::MaskBlurDescriptor::CreateMaskBlur
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:363
impeller::Paint::MaskBlurDescriptor::respect_ctm
bool respect_ctm
Definition: paint.h:56
impeller::Paint::MaskBlurDescriptor::style
FilterContents::BlurStyle style
Definition: paint.h:52
impeller::Paint::MaskBlurDescriptor::sigma
Sigma sigma
Definition: paint.h:53
impeller::Paint
Definition: paint.h:26
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:246
impeller::Paint::color_filter
const flutter::DlColorFilter * color_filter
Definition: paint.h:76
impeller::Paint::color_source
const flutter::DlColorSource * color_source
Definition: paint.h:75
impeller::Paint::Style
Style
Definition: paint.h:46
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:30
impeller::Paint::image_filter
const flutter::DlImageFilter * image_filter
Definition: paint.h:77
impeller::Paint::stroke_cap
Cap stroke_cap
Definition: paint.h:80
impeller::Paint::stroke_join
Join stroke_join
Definition: paint.h:81
impeller::Paint::stroke_miter
Scalar stroke_miter
Definition: paint.h:82
impeller::Paint::ColorSourceProc
std::function< std::shared_ptr< ColorSourceContents >()> ColorSourceProc
Definition: paint.h:35
impeller::Paint::style
Style style
Definition: paint.h:83
impeller::Paint::WithMaskBlur
std::shared_ptr< Contents > WithMaskBlur(std::shared_ptr< Contents > input, bool is_solid_color, const Matrix &ctm) const
Definition: paint.cc:270
impeller::Paint::invert_colors
bool invert_colors
Definition: paint.h:85
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:39
impeller::Paint::mask_blur_descriptor
std::optional< MaskBlurDescriptor > mask_blur_descriptor
Definition: paint.h:87
impeller::Paint::color
Color color
Definition: paint.h:74
impeller::Paint::blend_mode
BlendMode blend_mode
Definition: paint.h:84
impeller::Paint::MaskFilterProc
std::function< std::shared_ptr< FilterContents >(FilterInput::Ref, bool is_solid_color, const Matrix &effect_transform)> MaskFilterProc
Definition: paint.h:34
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:257
impeller::Paint::WithImageFilter
std::shared_ptr< FilterContents > WithImageFilter(const FilterInput::Variant &input, const Matrix &effect_transform, Entity::RenderingMode rendering_mode) const
Definition: paint.cc:280
impeller::Paint::CreateContents
std::shared_ptr< ColorSourceContents > CreateContents() const
Definition: paint.cc:37
impeller::Paint::HasColorFilter
bool HasColorFilter() const
Whether this paint has a color filter that can apply opacity.
Definition: paint.cc:437
impeller::Paint::stroke_width
Scalar stroke_width
Definition: paint.h:79
impeller::Sigma
In filters that use Gaussian distributions, "sigma" is a size of one standard deviation in terms of t...
Definition: sigma.h:32
texture_contents.h