Flutter Impeller
color_filter.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 
6 
7 #include <utility>
12 
13 namespace impeller {
14 
15 /*******************************************************************************
16  ******* ColorFilter
17  ******************************************************************************/
18 
19 ColorFilter::ColorFilter() = default;
20 
21 ColorFilter::~ColorFilter() = default;
22 
23 std::shared_ptr<ColorFilter> ColorFilter::MakeBlend(BlendMode blend_mode,
24  Color color) {
25  return std::make_shared<BlendColorFilter>(blend_mode, color);
26 }
27 
28 std::shared_ptr<ColorFilter> ColorFilter::MakeMatrix(ColorMatrix color_matrix) {
29  return std::make_shared<MatrixColorFilter>(color_matrix);
30 }
31 
32 std::shared_ptr<ColorFilter> ColorFilter::MakeSrgbToLinear() {
33  return std::make_shared<SrgbToLinearColorFilter>();
34 }
35 
36 std::shared_ptr<ColorFilter> ColorFilter::MakeLinearToSrgb() {
37  return std::make_shared<LinearToSrgbColorFilter>();
38 }
39 
40 std::shared_ptr<ColorFilter> ColorFilter::MakeComposed(
41  const std::shared_ptr<ColorFilter>& outer,
42  const std::shared_ptr<ColorFilter>& inner) {
43  return std::make_shared<ComposedColorFilter>(outer, inner);
44 }
45 
46 /*******************************************************************************
47  ******* BlendColorFilter
48  ******************************************************************************/
49 
51  : blend_mode_(blend_mode), color_(color) {}
52 
54 
55 std::shared_ptr<ColorFilterContents> BlendColorFilter::WrapWithGPUColorFilter(
56  std::shared_ptr<FilterInput> input,
57  ColorFilterContents::AbsorbOpacity absorb_opacity) const {
58  auto filter =
59  ColorFilterContents::MakeBlend(blend_mode_, {std::move(input)}, color_);
60  filter->SetAbsorbOpacity(absorb_opacity);
61  return filter;
62 }
63 
65  return [filter_blend_mode = blend_mode_, filter_color = color_](Color color) {
66  return color.Blend(filter_color, filter_blend_mode);
67  };
68 }
69 
70 std::shared_ptr<ColorFilter> BlendColorFilter::Clone() const {
71  return std::make_shared<BlendColorFilter>(*this);
72 }
73 
74 /*******************************************************************************
75  ******* MatrixColorFilter
76  ******************************************************************************/
77 
79  : color_matrix_(color_matrix) {}
80 
82 
83 std::shared_ptr<ColorFilterContents> MatrixColorFilter::WrapWithGPUColorFilter(
84  std::shared_ptr<FilterInput> input,
85  ColorFilterContents::AbsorbOpacity absorb_opacity) const {
86  auto filter =
87  ColorFilterContents::MakeColorMatrix({std::move(input)}, color_matrix_);
88  filter->SetAbsorbOpacity(absorb_opacity);
89  return filter;
90 }
91 
93  return [color_matrix = color_matrix_](Color color) {
94  return color.ApplyColorMatrix(color_matrix);
95  };
96 }
97 
98 std::shared_ptr<ColorFilter> MatrixColorFilter::Clone() const {
99  return std::make_shared<MatrixColorFilter>(*this);
100 }
101 
102 /*******************************************************************************
103  ******* SrgbToLinearColorFilter
104  ******************************************************************************/
105 
107 
109 
110 std::shared_ptr<ColorFilterContents>
112  std::shared_ptr<FilterInput> input,
113  ColorFilterContents::AbsorbOpacity absorb_opacity) const {
114  auto filter = ColorFilterContents::MakeSrgbToLinearFilter({std::move(input)});
115  filter->SetAbsorbOpacity(absorb_opacity);
116  return filter;
117 }
118 
120  const {
121  return [](Color color) { return color.SRGBToLinear(); };
122 }
123 
124 std::shared_ptr<ColorFilter> SrgbToLinearColorFilter::Clone() const {
125  return std::make_shared<SrgbToLinearColorFilter>(*this);
126 }
127 
128 /*******************************************************************************
129  ******* LinearToSrgbColorFilter
130  ******************************************************************************/
131 
133 
135 
136 std::shared_ptr<ColorFilterContents>
138  std::shared_ptr<FilterInput> input,
139  ColorFilterContents::AbsorbOpacity absorb_opacity) const {
140  auto filter = ColorFilterContents::MakeSrgbToLinearFilter({std::move(input)});
141  filter->SetAbsorbOpacity(absorb_opacity);
142  return filter;
143 }
144 
146  const {
147  return [](Color color) { return color.LinearToSRGB(); };
148 }
149 
150 std::shared_ptr<ColorFilter> LinearToSrgbColorFilter::Clone() const {
151  return std::make_shared<LinearToSrgbColorFilter>(*this);
152 }
153 
154 /*******************************************************************************
155  ******* ComposedColorFilter
156  ******************************************************************************/
157 
159  const std::shared_ptr<ColorFilter>& outer,
160  const std::shared_ptr<ColorFilter>& inner)
161  : outer_(outer), inner_(inner) {}
162 
164 
165 std::shared_ptr<ColorFilterContents>
167  std::shared_ptr<FilterInput> input,
168  ColorFilterContents::AbsorbOpacity absorb_opacity) const {
169  std::shared_ptr<FilterContents> inner = inner_->WrapWithGPUColorFilter(
171  return outer_->WrapWithGPUColorFilter(FilterInput::Make(inner),
172  absorb_opacity);
173 }
174 
175 // |ColorFilter|
177  const {
178  return [inner = inner_, outer = outer_](Color color) {
179  auto inner_proc = inner->GetCPUColorFilterProc();
180  auto outer_proc = outer->GetCPUColorFilterProc();
181  return outer_proc(inner_proc(color));
182  };
183 }
184 
185 // |ColorFilter|
186 std::shared_ptr<ColorFilter> ComposedColorFilter::Clone() const {
187  return std::make_shared<ComposedColorFilter>(outer_, inner_);
188 }
189 
190 } // namespace impeller
impeller::MatrixColorFilter::~MatrixColorFilter
~MatrixColorFilter() override
impeller::MatrixColorFilter::GetCPUColorFilterProc
ColorFilterProc GetCPUColorFilterProc() const override
Returns a function that can be used to filter unpremultiplied Impeller Colors on the CPU.
Definition: color_filter.cc:92
impeller::BlendColorFilter::WrapWithGPUColorFilter
std::shared_ptr< ColorFilterContents > WrapWithGPUColorFilter(std::shared_ptr< FilterInput > input, ColorFilterContents::AbsorbOpacity absorb_opacity) const override
Wraps the given filter input with a GPU-based filter that will perform the color operation....
Definition: color_filter.cc:55
impeller::BlendColorFilter::BlendColorFilter
BlendColorFilter(BlendMode blend_mode, Color color)
Definition: color_filter.cc:50
impeller::BlendMode
BlendMode
Definition: color.h:59
impeller::Color
Definition: color.h:124
impeller::FilterInput::Make
static FilterInput::Ref Make(Variant input, bool msaa_enabled=true)
Definition: filter_input.cc:19
impeller::ColorFilter::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: color_filter.h:25
impeller::ColorFilter::MakeComposed
static std::shared_ptr< ColorFilter > MakeComposed(const std::shared_ptr< ColorFilter > &outer, const std::shared_ptr< ColorFilter > &inner)
Definition: color_filter.cc:40
impeller::MatrixColorFilter::MatrixColorFilter
MatrixColorFilter(ColorMatrix color_matrix)
Definition: color_filter.cc:78
impeller::ColorFilterContents::AbsorbOpacity::kNo
@ kNo
impeller::ColorFilter::ColorFilter
ColorFilter()
impeller::MatrixColorFilter::WrapWithGPUColorFilter
std::shared_ptr< ColorFilterContents > WrapWithGPUColorFilter(std::shared_ptr< FilterInput > input, ColorFilterContents::AbsorbOpacity absorb_opacity) const override
Wraps the given filter input with a GPU-based filter that will perform the color operation....
Definition: color_filter.cc:83
impeller::BlendColorFilter::Clone
std::shared_ptr< ColorFilter > Clone() const override
Definition: color_filter.cc:70
impeller::ComposedColorFilter::GetCPUColorFilterProc
ColorFilterProc GetCPUColorFilterProc() const override
Returns a function that can be used to filter unpremultiplied Impeller Colors on the CPU.
Definition: color_filter.cc:176
impeller::SrgbToLinearColorFilter::Clone
std::shared_ptr< ColorFilter > Clone() const override
Definition: color_filter.cc:124
impeller::LinearToSrgbColorFilter::LinearToSrgbColorFilter
LinearToSrgbColorFilter()
filter_contents.h
impeller::ColorFilter::MakeBlend
static std::shared_ptr< ColorFilter > MakeBlend(BlendMode blend_mode, Color color)
Definition: color_filter.cc:23
impeller::LinearToSrgbColorFilter::Clone
std::shared_ptr< ColorFilter > Clone() const override
Definition: color_filter.cc:150
impeller::BlendColorFilter::~BlendColorFilter
~BlendColorFilter() override
impeller::ComposedColorFilter::WrapWithGPUColorFilter
std::shared_ptr< ColorFilterContents > WrapWithGPUColorFilter(std::shared_ptr< FilterInput > input, ColorFilterContents::AbsorbOpacity absorb_opacity) const override
Wraps the given filter input with a GPU-based filter that will perform the color operation....
Definition: color_filter.cc:166
impeller::SrgbToLinearColorFilter::WrapWithGPUColorFilter
std::shared_ptr< ColorFilterContents > WrapWithGPUColorFilter(std::shared_ptr< FilterInput > input, ColorFilterContents::AbsorbOpacity absorb_opacity) const override
Wraps the given filter input with a GPU-based filter that will perform the color operation....
Definition: color_filter.cc:111
impeller::SrgbToLinearColorFilter::~SrgbToLinearColorFilter
~SrgbToLinearColorFilter() override
impeller::ColorFilterContents::MakeSrgbToLinearFilter
static std::shared_ptr< ColorFilterContents > MakeSrgbToLinearFilter(FilterInput::Ref input)
Definition: color_filter_contents.cc:75
impeller::LinearToSrgbColorFilter::WrapWithGPUColorFilter
std::shared_ptr< ColorFilterContents > WrapWithGPUColorFilter(std::shared_ptr< FilterInput > input, ColorFilterContents::AbsorbOpacity absorb_opacity) const override
Wraps the given filter input with a GPU-based filter that will perform the color operation....
Definition: color_filter.cc:137
impeller::SrgbToLinearColorFilter::SrgbToLinearColorFilter
SrgbToLinearColorFilter()
filter_input.h
impeller::ComposedColorFilter::~ComposedColorFilter
~ComposedColorFilter() override
color_filter_contents.h
impeller::MatrixColorFilter::Clone
std::shared_ptr< ColorFilter > Clone() const override
Definition: color_filter.cc:98
impeller::ColorFilter::MakeMatrix
static std::shared_ptr< ColorFilter > MakeMatrix(ColorMatrix color_matrix)
Definition: color_filter.cc:28
impeller::BlendColorFilter::GetCPUColorFilterProc
ColorFilterProc GetCPUColorFilterProc() const override
Returns a function that can be used to filter unpremultiplied Impeller Colors on the CPU.
Definition: color_filter.cc:64
impeller::ComposedColorFilter::ComposedColorFilter
ComposedColorFilter(const std::shared_ptr< ColorFilter > &outer, const std::shared_ptr< ColorFilter > &inner)
Definition: color_filter.cc:158
impeller::ColorFilter::~ColorFilter
virtual ~ColorFilter()
impeller::ColorFilter::MakeLinearToSrgb
static std::shared_ptr< ColorFilter > MakeLinearToSrgb()
Definition: color_filter.cc:36
impeller::LinearToSrgbColorFilter::~LinearToSrgbColorFilter
~LinearToSrgbColorFilter() override
impeller::ComposedColorFilter::Clone
std::shared_ptr< ColorFilter > Clone() const override
Definition: color_filter.cc:186
impeller::ColorFilterContents::MakeColorMatrix
static std::shared_ptr< ColorFilterContents > MakeColorMatrix(FilterInput::Ref input, const ColorMatrix &color_matrix)
Definition: color_filter_contents.cc:58
color.h
color_filter.h
impeller::ColorFilter::MakeSrgbToLinear
static std::shared_ptr< ColorFilter > MakeSrgbToLinear()
Definition: color_filter.cc:32
impeller::LinearToSrgbColorFilter::GetCPUColorFilterProc
ColorFilterProc GetCPUColorFilterProc() const override
Returns a function that can be used to filter unpremultiplied Impeller Colors on the CPU.
Definition: color_filter.cc:145
impeller::SrgbToLinearColorFilter::GetCPUColorFilterProc
ColorFilterProc GetCPUColorFilterProc() const override
Returns a function that can be used to filter unpremultiplied Impeller Colors on the CPU.
Definition: color_filter.cc:119
impeller::ColorMatrix
Definition: color.h:117
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:14
impeller
Definition: aiks_blur_unittests.cc:20