| 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
5 #include "impeller/display_list/color_filter.h"
6
7 #include "display_list/effects/dl_color_filters.h"
8 #include "fml/logging.h"
9 #include "impeller/display_list/skia_conversions.h"
10 #include "impeller/entity/contents/filters/color_filter_contents.h"
11 #include "impeller/entity/contents/filters/inputs/filter_input.h"
12 #include "impeller/geometry/color.h"
13
14 namespace impeller {
15
16 std::shared_ptr<ColorFilterContents> WrapWithInvertColors(
17 const std::shared_ptr<FilterInput>& input,
18 ColorFilterContents::AbsorbOpacity absorb_opacity) {
19 auto filter = ColorFilterContents::MakeColorMatrix({input}, kColorInversion);
20 filter->SetAbsorbOpacity(absorb_opacity);
21 return filter;
22 }
23
24 std::shared_ptr<ColorFilterContents> WrapWithGPUColorFilter(
25 const flutter::DlColorFilter* filter,
26 const std::shared_ptr<FilterInput>& input,
27 ColorFilterContents::AbsorbOpacity absorb_opacity) {
28 FML_DCHECK(filter);
29
30 switch (filter->type()) {
31 case flutter::DlColorFilterType::kBlend: {
32 const flutter::DlBlendColorFilter* blend_filter = filter->asBlend();
33 FML_DCHECK(blend_filter);
34
35 auto filter = ColorFilterContents::MakeBlend(
36 static_cast<BlendMode>(blend_filter->mode()), {input},
37 skia_conversions::ToColor(blend_filter->color()));
38 filter->SetAbsorbOpacity(absorb_opacity);
39 return filter;
40 }
41 case flutter::DlColorFilterType::kMatrix: {
42 const flutter::DlMatrixColorFilter* matrix_filter = filter->asMatrix();
43 FML_DCHECK(matrix_filter);
44
45 impeller::ColorMatrix color_matrix;
46 matrix_filter->get_matrix(color_matrix.array);
47 auto filter = ColorFilterContents::MakeColorMatrix({input}, color_matrix);
48 filter->SetAbsorbOpacity(absorb_opacity);
49 return filter;
50 }
51 case flutter::DlColorFilterType::kSrgbToLinearGamma: {
52 auto filter = ColorFilterContents::MakeSrgbToLinearFilter({input});
53 filter->SetAbsorbOpacity(absorb_opacity);
54 return filter;
55 }
56 case flutter::DlColorFilterType::kLinearToSrgbGamma: {
57 auto filter = ColorFilterContents::MakeLinearToSrgbFilter({input});
58 filter->SetAbsorbOpacity(absorb_opacity);
59 return filter;
60 }
61 }
62
63 FML_UNREACHABLE();
64 }
65
66 ColorFilterProc GetCPUColorFilterProc(const flutter::DlColorFilter* filter) {
67 FML_DCHECK(filter);
68
69 switch (filter->type()) {
70 case flutter::DlColorFilterType::kBlend: {
71 const flutter::DlBlendColorFilter* blend_filter = filter->asBlend();
72 FML_DCHECK(blend_filter);
73
74 return [filter_blend_mode = static_cast<BlendMode>(blend_filter->mode()),
75 filter_color = skia_conversions::ToColor(blend_filter->color())](
76 Color color) {
77 return color.Blend(filter_color, filter_blend_mode);
78 };
79 }
80 case flutter::DlColorFilterType::kMatrix: {
81 const flutter::DlMatrixColorFilter* matrix_filter = filter->asMatrix();
82 FML_DCHECK(matrix_filter);
83
84 impeller::ColorMatrix color_matrix;
85 matrix_filter->get_matrix(color_matrix.array);
86 return [color_matrix = color_matrix](Color color) {
87 return color.ApplyColorMatrix(color_matrix);
88 };
89 }
90 case flutter::DlColorFilterType::kSrgbToLinearGamma: {
91 return [](Color color) { return color.SRGBToLinear(); };
92 }
93
94 case flutter::DlColorFilterType::kLinearToSrgbGamma: {
95 return [](Color color) { return color.LinearToSRGB(); };
96 }
97 }
98
99 FML_UNREACHABLE();
100 }
101
102 } // namespace impeller
impeller::ColorFilterContents::MakeColorMatrix
static std::shared_ptr< ColorFilterContents > MakeColorMatrix(FilterInput::Ref input, const ColorMatrix &color_matrix)
Definition: color_filter_contents.cc:58
impeller::ColorFilterContents::AbsorbOpacity
AbsorbOpacity
Definition: color_filter_contents.h:14
impeller::ColorFilterContents::MakeSrgbToLinearFilter
static std::shared_ptr< ColorFilterContents > MakeSrgbToLinearFilter(FilterInput::Ref input)
Definition: color_filter_contents.cc:75
impeller::ColorFilterContents::MakeLinearToSrgbFilter
static std::shared_ptr< ColorFilterContents > MakeLinearToSrgbFilter(FilterInput::Ref input)
Definition: color_filter_contents.cc:68
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
color.h
color_filter_contents.h
color_filter.h
filter_input.h
impeller::skia_conversions::ToColor
Color ToColor(const flutter::DlColor &color)
Definition: skia_conversions.cc:13
impeller
Definition: allocation.cc:12
impeller::WrapWithGPUColorFilter
std::shared_ptr< ColorFilterContents > WrapWithGPUColorFilter(const flutter::DlColorFilter *filter, const std::shared_ptr< FilterInput > &input, ColorFilterContents::AbsorbOpacity absorb_opacity)
Definition: color_filter.cc:24
impeller::WrapWithInvertColors
std::shared_ptr< ColorFilterContents > WrapWithInvertColors(const std::shared_ptr< FilterInput > &input, ColorFilterContents::AbsorbOpacity absorb_opacity)
Definition: color_filter.cc:16
impeller::GetCPUColorFilterProc
ColorFilterProc GetCPUColorFilterProc(const flutter::DlColorFilter *filter)
Definition: color_filter.cc:66
impeller::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: color_filter.h:36
impeller::BlendMode
BlendMode
Definition: color.h:58
impeller::kColorInversion
static constexpr const ColorMatrix kColorInversion
A color matrix which inverts colors.
Definition: color_filter.h:16
skia_conversions.h
impeller::Color
Definition: color.h:124
impeller::Color::Blend
Color Blend(Color source, BlendMode blend_mode) const
Blends an unpremultiplied destination color into a given unpremultiplied source color to form a new u...
Definition: color.cc:157
impeller::ColorMatrix
Definition: color.h:117
impeller::ColorMatrix::array
Scalar array[20]
Definition: color.h:118