Flutter Impeller
blend_filter_contents.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_ENTITY_CONTENTS_FILTERS_BLEND_FILTER_CONTENTS_H_
6 #define FLUTTER_IMPELLER_ENTITY_CONTENTS_FILTERS_BLEND_FILTER_CONTENTS_H_
7 
8 #include <optional>
12 
13 namespace impeller {
14 
15 constexpr std::array<std::array<Scalar, 5>, 15> kPorterDuffCoefficients = {{
16  {0, 0, 0, 0, 0}, // Clear
17  {1, 0, 0, 0, 0}, // Source
18  {0, 0, 1, 0, 0}, // Destination
19  {1, 0, 1, -1, 0}, // SourceOver
20  {1, -1, 1, 0, 0}, // DestinationOver
21  {0, 1, 0, 0, 0}, // SourceIn
22  {0, 0, 0, 1, 0}, // DestinationIn
23  {1, -1, 0, 0, 0}, // SourceOut
24  {0, 0, 1, -1, 0}, // DestinationOut
25  {0, 1, 1, -1, 0}, // SourceATop
26  {1, -1, 0, 1, 0}, // DestinationATop
27  {1, -1, 1, -1, 0}, // Xor
28  {1, 0, 1, 0, 0}, // Plus
29  {0, 0, 0, 0, 1}, // Modulate
30  {0, 0, 1, 0, -1}, // Screen
31 }};
32 
33 std::optional<BlendMode> InvertPorterDuffBlend(BlendMode blend_mode);
34 
36  public:
37  using AdvancedBlendProc = std::function<std::optional<Entity>(
38  const FilterInput::Vector& inputs,
39  const ContentContext& renderer,
40  const Entity& entity,
41  const Rect& coverage,
42  BlendMode blend_mode,
43  std::optional<Color> foreground_color,
45  std::optional<Scalar> alpha)>;
46 
48 
50 
51  void SetBlendMode(BlendMode blend_mode);
52 
53  /// @brief Sets a source color which is blended after all of the inputs have
54  /// been blended.
55  void SetForegroundColor(std::optional<Color> color);
56 
57  private:
58  // |FilterContents|
59  std::optional<Entity> RenderFilter(
60  const FilterInput::Vector& inputs,
61  const ContentContext& renderer,
62  const Entity& entity,
63  const Matrix& effect_transform,
64  const Rect& coverage,
65  const std::optional<Rect>& coverage_hint) const override;
66 
67  /// @brief Optimized advanced blend that avoids a second subpass when there is
68  /// only a single input and a foreground color.
69  ///
70  /// These contents cannot absorb opacity.
71  std::optional<Entity> CreateForegroundAdvancedBlend(
72  const std::shared_ptr<FilterInput>& input,
73  const ContentContext& renderer,
74  const Entity& entity,
75  const Rect& coverage,
76  Color foreground_color,
77  BlendMode blend_mode,
78  std::optional<Scalar> alpha,
79  ColorFilterContents::AbsorbOpacity absorb_opacity) const;
80 
81  /// @brief Implements the advanced blends filters in terms of the framebuffer
82  /// blend filters.
83  ///
84  /// This requires device support for frameuffer fetch,
85  /// `Capabilities::SupportsFramebufferFetch` must be true
86  /// This allows a substantial reduction in the number of bootstrapped
87  /// shaders.
88  std::optional<Entity> CreateFramebufferAdvancedBlend(
89  const FilterInput::Vector& inputs,
90  const ContentContext& renderer,
91  const Entity& entity,
92  const Rect& coverage,
93  std::optional<Color> foreground_color,
94  BlendMode blend_mode,
95  std::optional<Scalar> alpha,
96  ColorFilterContents::AbsorbOpacity absorb_opacity) const;
97 
98  /// @brief Optimized porter-duff blend that avoids a second subpass when there
99  /// is only a single input and a foreground color.
100  ///
101  /// These contents cannot absorb opacity.
102  std::optional<Entity> CreateForegroundPorterDuffBlend(
103  const std::shared_ptr<FilterInput>& input,
104  const ContentContext& renderer,
105  const Entity& entity,
106  const Rect& coverage,
107  Color foreground_color,
108  BlendMode blend_mode,
109  std::optional<Scalar> alpha,
110  ColorFilterContents::AbsorbOpacity absorb_opacity) const;
111 
112  BlendMode blend_mode_ = BlendMode::kSrcOver;
113  AdvancedBlendProc advanced_blend_proc_;
114  std::optional<Color> foreground_color_;
115 
116  BlendFilterContents(const BlendFilterContents&) = delete;
117 
118  BlendFilterContents& operator=(const BlendFilterContents&) = delete;
119 };
120 
121 } // namespace impeller
122 
123 #endif // FLUTTER_IMPELLER_ENTITY_CONTENTS_FILTERS_BLEND_FILTER_CONTENTS_H_
void SetBlendMode(BlendMode blend_mode)
void SetForegroundColor(std::optional< Color > color)
Sets a source color which is blended after all of the inputs have been blended.
std::function< std::optional< Entity >(const FilterInput::Vector &inputs, const ContentContext &renderer, const Entity &entity, const Rect &coverage, BlendMode blend_mode, std::optional< Color > foreground_color, ColorFilterContents::AbsorbOpacity absorb_opacity, std::optional< Scalar > alpha)> AdvancedBlendProc
std::vector< FilterInput::Ref > Vector
Definition: filter_input.h:33
constexpr std::array< std::array< Scalar, 5 >, 15 > kPorterDuffCoefficients
std::optional< BlendMode > InvertPorterDuffBlend(BlendMode blend_mode)
BlendMode
Definition: color.h:58
A 4x4 matrix using column-major storage.
Definition: matrix.h:37