| Flutter Impeller |

color_filter_contents.cc
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 #include "impeller/entity/contents/filters/color_filter_contents.h"
6
7 #include <utility>
8
9 #include "impeller/base/validation.h"
10 #include "impeller/entity/contents/filters/blend_filter_contents.h"
11 #include "impeller/entity/contents/filters/color_matrix_filter_contents.h"
12 #include "impeller/entity/contents/filters/linear_to_srgb_filter_contents.h"
13 #include "impeller/entity/contents/filters/srgb_to_linear_filter_contents.h"
14
15 namespace impeller {
16
17 std::shared_ptr<ColorFilterContents> ColorFilterContents::MakeBlend(
18 BlendMode blend_mode,
19 FilterInput::Vector inputs,
20 std::optional<Color> foreground_color) {
21 if (blend_mode > Entity::kLastAdvancedBlendMode) {
22 VALIDATION_LOG << "Invalid blend mode " << static_cast<int>(blend_mode)
23 << " passed to ColorFilterContents::MakeBlend.";
24 return nullptr;
25 }
26
27 size_t total_inputs = inputs.size() + (foreground_color.has_value() ? 1 : 0);
28 if (total_inputs < 2 || blend_mode <= Entity::kLastPipelineBlendMode) {
29 auto blend = std::make_shared<BlendFilterContents>();
30 blend->SetInputs(inputs);
31 blend->SetBlendMode(blend_mode);
32 blend->SetForegroundColor(foreground_color);
33 return blend;
34 }
35
36 auto blend_input = inputs[0];
37 std::shared_ptr<BlendFilterContents> new_blend;
38 for (auto in_i = inputs.begin() + 1; in_i < inputs.end(); in_i++) {
39 new_blend = std::make_shared<BlendFilterContents>();
40 new_blend->SetInputs({blend_input, *in_i});
41 new_blend->SetBlendMode(blend_mode);
42 if (in_i < inputs.end() - 1 || foreground_color.has_value()) {
43 blend_input = FilterInput::Make(
44 std::static_pointer_cast<FilterContents>(new_blend));
45 }
46 }
47
48 if (foreground_color.has_value()) {
49 new_blend = std::make_shared<BlendFilterContents>();
50 new_blend->SetInputs({blend_input});
51 new_blend->SetBlendMode(blend_mode);
52 new_blend->SetForegroundColor(foreground_color);
53 }
54
55 return new_blend;
56 }
57
58 std::shared_ptr<ColorFilterContents> ColorFilterContents::MakeColorMatrix(
59 FilterInput::Ref input,
60 const ColorMatrix& color_matrix) {
61 auto filter = std::make_shared<ColorMatrixFilterContents>();
62 filter->SetInputs({std::move(input)});
63 filter->SetMatrix(color_matrix);
64 return filter;
65 }
66
67 std::shared_ptr<ColorFilterContents>
68 ColorFilterContents::MakeLinearToSrgbFilter(FilterInput::Ref input) {
69 auto filter = std::make_shared<LinearToSrgbFilterContents>();
70 filter->SetInputs({std::move(input)});
71 return filter;
72 }
73
74 std::shared_ptr<ColorFilterContents>
75 ColorFilterContents::MakeSrgbToLinearFilter(FilterInput::Ref input) {
76 auto filter = std::make_shared<SrgbToLinearFilterContents>();
77 filter->SetInputs({std::move(input)});
78 return filter;
79 }
80
81 ColorFilterContents::ColorFilterContents() = default;
82
83 ColorFilterContents::~ColorFilterContents() = default;
84
85 void ColorFilterContents::SetAbsorbOpacity(AbsorbOpacity absorb_opacity) {
86 absorb_opacity_ = absorb_opacity;
87 }
88
89 ColorFilterContents::AbsorbOpacity ColorFilterContents::GetAbsorbOpacity()
90 const {
91 return absorb_opacity_;
92 }
93
94 void ColorFilterContents::SetAlpha(Scalar alpha) {
95 alpha_ = alpha;
96 }
97
98 std::optional<Scalar> ColorFilterContents::GetAlpha() const {
99 return alpha_;
100 }
101
102 } // namespace impeller
impeller::BlendMode
BlendMode
Definition: color.h:57
impeller::Entity::kLastPipelineBlendMode
static constexpr BlendMode kLastPipelineBlendMode
Definition: entity.h:23
color_matrix_filter_contents.h
impeller::Scalar
float Scalar
Definition: scalar.h:15
impeller::ColorFilterContents::ColorFilterContents
ColorFilterContents()
impeller::ColorFilterContents::SetAbsorbOpacity
void SetAbsorbOpacity(AbsorbOpacity absorb_opacity)
Definition: color_filter_contents.cc:85
linear_to_srgb_filter_contents.h
impeller::FilterInput::Make
static FilterInput::Ref Make(Variant input, bool msaa_enabled=true)
Definition: filter_input.cc:19
impeller::FilterInput::Ref
std::shared_ptr< FilterInput > Ref
Definition: filter_input.h:31
validation.h
impeller::ColorFilterContents::~ColorFilterContents
~ColorFilterContents() override
impeller::Entity::kLastAdvancedBlendMode
static constexpr BlendMode kLastAdvancedBlendMode
Definition: entity.h:24
impeller::ColorFilterContents::GetAbsorbOpacity
AbsorbOpacity GetAbsorbOpacity() const
Definition: color_filter_contents.cc:89
impeller::ColorFilterContents::MakeSrgbToLinearFilter
static std::shared_ptr< ColorFilterContents > MakeSrgbToLinearFilter(FilterInput::Ref input)
Definition: color_filter_contents.cc:75
impeller::ColorFilterContents::SetAlpha
void SetAlpha(Scalar alpha)
Sets an alpha that is applied to the final blended result.
Definition: color_filter_contents.cc:94
color_filter_contents.h
impeller::ColorFilterContents::MakeLinearToSrgbFilter
static std::shared_ptr< ColorFilterContents > MakeLinearToSrgbFilter(FilterInput::Ref input)
Definition: color_filter_contents.cc:68
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:60
impeller::ColorFilterContents::MakeColorMatrix
static std::shared_ptr< ColorFilterContents > MakeColorMatrix(FilterInput::Ref input, const ColorMatrix &color_matrix)
Definition: color_filter_contents.cc:58
blend_filter_contents.h
impeller::FilterInput::Vector
std::vector< FilterInput::Ref > Vector
Definition: filter_input.h:32
impeller::ColorFilterContents::GetAlpha
std::optional< Scalar > GetAlpha() const
Definition: color_filter_contents.cc:98
srgb_to_linear_filter_contents.h
impeller::ColorMatrix
Definition: color.h:115
impeller::ColorFilterContents::MakeBlend
static std::shared_ptr< ColorFilterContents > MakeBlend(BlendMode blend_mode, FilterInput::Vector inputs, std::optional< Color > foreground_color=std::nullopt)
the [inputs] are expected to be in the order of dst, src.
Definition: color_filter_contents.cc:17
impeller::ColorFilterContents::AbsorbOpacity
AbsorbOpacity
Definition: color_filter_contents.h:13
impeller
Definition: aiks_context.cc:10