Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
13namespace impeller {
14
15/*******************************************************************************
16 ******* ColorFilter
17 ******************************************************************************/
18
19ColorFilter::ColorFilter() = default;
20
22
23std::shared_ptr<ColorFilter> ColorFilter::MakeBlend(BlendMode blend_mode,
24 Color color) {
25 return std::make_shared<BlendColorFilter>(blend_mode, color);
26}
27
28std::shared_ptr<ColorFilter> ColorFilter::MakeMatrix(ColorMatrix color_matrix) {
29 return std::make_shared<MatrixColorFilter>(color_matrix);
30}
31
32std::shared_ptr<ColorFilter> ColorFilter::MakeSrgbToLinear() {
33 return std::make_shared<SrgbToLinearColorFilter>();
34}
35
36std::shared_ptr<ColorFilter> ColorFilter::MakeLinearToSrgb() {
37 return std::make_shared<LinearToSrgbColorFilter>();
38}
39
40std::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
55std::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
70std::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
83std::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
98std::shared_ptr<ColorFilter> MatrixColorFilter::Clone() const {
99 return std::make_shared<MatrixColorFilter>(*this);
100}
101
102/*******************************************************************************
103 ******* SrgbToLinearColorFilter
104 ******************************************************************************/
105
107
109
110std::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
124std::shared_ptr<ColorFilter> SrgbToLinearColorFilter::Clone() const {
125 return std::make_shared<SrgbToLinearColorFilter>(*this);
126}
127
128/*******************************************************************************
129 ******* LinearToSrgbColorFilter
130 ******************************************************************************/
131
133
135
136std::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
150std::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
165std::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|
186std::shared_ptr<ColorFilter> ComposedColorFilter::Clone() const {
187 return std::make_shared<ComposedColorFilter>(outer_, inner_);
188}
189
190} // namespace impeller
SkColor4f color
ColorFilterProc GetCPUColorFilterProc() const override
Returns a function that can be used to filter unpremultiplied Impeller Colors on the CPU.
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....
BlendColorFilter(BlendMode blend_mode, Color color)
std::shared_ptr< ColorFilter > Clone() const override
static std::shared_ptr< ColorFilterContents > MakeColorMatrix(FilterInput::Ref input, const ColorMatrix &color_matrix)
static std::shared_ptr< ColorFilterContents > MakeSrgbToLinearFilter(FilterInput::Ref input)
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.
static std::shared_ptr< ColorFilter > MakeComposed(const std::shared_ptr< ColorFilter > &outer, const std::shared_ptr< ColorFilter > &inner)
std::function< Color(Color)> ColorFilterProc
static std::shared_ptr< ColorFilter > MakeMatrix(ColorMatrix color_matrix)
static std::shared_ptr< ColorFilter > MakeBlend(BlendMode blend_mode, Color color)
static std::shared_ptr< ColorFilter > MakeLinearToSrgb()
static std::shared_ptr< ColorFilter > MakeSrgbToLinear()
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....
ComposedColorFilter(const std::shared_ptr< ColorFilter > &outer, const std::shared_ptr< ColorFilter > &inner)
std::shared_ptr< ColorFilter > Clone() const override
ColorFilterProc GetCPUColorFilterProc() const override
Returns a function that can be used to filter unpremultiplied Impeller Colors on the CPU.
static FilterInput::Ref Make(Variant input, bool msaa_enabled=true)
ColorFilterProc GetCPUColorFilterProc() const override
Returns a function that can be used to filter unpremultiplied Impeller Colors on the CPU.
std::shared_ptr< ColorFilter > Clone() const override
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....
ColorFilterProc GetCPUColorFilterProc() const override
Returns a function that can be used to filter unpremultiplied Impeller Colors on the CPU.
std::shared_ptr< ColorFilter > Clone() const override
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....
MatrixColorFilter(ColorMatrix color_matrix)
std::shared_ptr< ColorFilter > Clone() const override
ColorFilterProc GetCPUColorFilterProc() const override
Returns a function that can be used to filter unpremultiplied Impeller Colors on the CPU.
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....
BlendMode
Definition color.h:59