Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
ShiftChannelsEffect.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2019 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
18
19#include <array>
20#include <cstddef>
21#include <cstdint>
22#include <utility>
23
24namespace skjson {
25class ArrayValue;
26}
27
28namespace skottie {
29namespace internal {
30
31namespace {
32
33/*
34 * AE's Shift Channels effect overrides individual channels based on a selectable function:
35 *
36 * C' = {fR(C), fG(C), fB(C), fA(C)}
37 *
38 * where fR, fG, fB, fA can be one of
39 *
40 * C.r, C.g, C.b, C.a, Luminance(C), Hue(C), Saturation(C), Lightness(C), 1 or 0.
41 */
42class ShiftChannelsEffectAdapter final : public AnimatablePropertyContainer {
43public:
46 const AnimationBuilder* abuilder) {
48 new ShiftChannelsEffectAdapter(jprops, std::move(layer), abuilder));
49 }
50
51 const sk_sp<sksg::ExternalColorFilter>& node() const { return fColorFilter; }
52
53private:
54 ShiftChannelsEffectAdapter(const skjson::ArrayValue& jprops,
56 const AnimationBuilder* abuilder)
57 : fColorFilter(sksg::ExternalColorFilter::Make(std::move(layer))) {
58 enum : size_t {
59 kTakeAlphaFrom_Index = 0,
60 kTakeRedFrom_Index = 1,
61 kTakeGreenFrom_Index = 2,
62 kTakeBlueFrom_Index = 3,
63 };
64
65 EffectBinder(jprops, *abuilder, this)
66 .bind( kTakeRedFrom_Index, fR)
67 .bind(kTakeGreenFrom_Index, fG)
68 .bind( kTakeBlueFrom_Index, fB)
69 .bind(kTakeAlphaFrom_Index, fA);
70 }
71
72 enum class Source : uint8_t {
73 kAlpha = 1,
74 kRed = 2,
75 kGreen = 3,
76 kBlue = 4,
77 kLuminance = 5,
78 kHue = 6,
79 kLightness = 7,
80 kSaturation = 8,
81 kFullOn = 9,
82 kFullOff = 10,
83
84 kMax = kFullOff
85 };
86
87 void onSync() override {
88 // TODO: support for HSL sources will require a custom color filter.
89
90 static constexpr float gSourceCoeffs[][5] = {
91 { 0, 0, 0, 1, 0}, // kAlpha
92 { 1, 0, 0, 0, 0}, // kRed
93 { 0, 1, 0, 0, 0}, // kGreen
94 { 0, 0, 1, 0, 0}, // kBlue
95 {SK_LUM_COEFF_R, SK_LUM_COEFF_G, SK_LUM_COEFF_B, 0, 0}, // kLuminance
96 { 0, 0, 0, 0, 0}, // TODO: kHue
97 { 0, 0, 0, 0, 0}, // TODO: kLightness
98 { 0, 0, 0, 0, 0}, // TODO: kSaturation
99 { 0, 0, 0, 0, 1}, // kFullOn
100 { 0, 0, 0, 0, 0}, // kFullOff
101 };
102 static_assert(std::size(gSourceCoeffs) == static_cast<size_t>(Source::kMax), "");
103
104 auto coeffs = [](float src) {
105 // Channel sources are encoded as Source enum values.
106 // We map these onto our coeffs table.
107 src = SkTPin(src, 1.0f, static_cast<float>(Source::kMax));
108 return gSourceCoeffs[static_cast<size_t>(src) - 1];
109 };
110
111 const float* rc = coeffs(fR);
112 const float* gc = coeffs(fG);
113 const float* bc = coeffs(fB);
114 const float* ac = coeffs(fA);
115
116 const float cm[] = {
117 rc[0], rc[1], rc[2], rc[3], rc[4],
118 gc[0], gc[1], gc[2], gc[3], gc[4],
119 bc[0], bc[1], bc[2], bc[3], bc[4],
120 ac[0], ac[1], ac[2], ac[3], ac[4],
121 };
122
123 fColorFilter->setColorFilter(SkColorFilters::Matrix(cm));
124 }
125
126 const sk_sp<sksg::ExternalColorFilter> fColorFilter;
127
128 ScalarValue fR = static_cast<float>(Source::kRed),
129 fG = static_cast<float>(Source::kGreen),
130 fB = static_cast<float>(Source::kBlue),
131 fA = static_cast<float>(Source::kAlpha);
132};
133
134} // namespace
135
136
137sk_sp<sksg::RenderNode> EffectBuilder::attachShiftChannelsEffect(
138 const skjson::ArrayValue& jprops, sk_sp<sksg::RenderNode> layer) const {
139 return fBuilder->attachDiscardableAdapter<ShiftChannelsEffectAdapter>(jprops,
140 std::move(layer),
141 fBuilder);
142}
143
144} // namespace internal
145} // namespace skottie
static const uint64_t kGreen
static const uint64_t kAlpha
static const uint64_t kBlue
static const uint64_t kRed
@ kSaturation
saturation of source with hue and luminosity of destination
@ kHue
hue of source with saturation and luminosity of destination
#define SK_LUM_COEFF_B
#define SK_LUM_COEFF_R
#define SK_LUM_COEFF_G
static std::unique_ptr< SkEncoder > Make(SkWStream *dst, const SkPixmap *src, const SkYUVAPixmaps *srcYUVA, const SkColorSpace *srcYUVAColorSpace, const SkJpegEncoder::Options &options)
static constexpr const T & SkTPin(const T &x, const T &lo, const T &hi)
Definition SkTPin.h:19
static sk_sp< SkColorFilter > Matrix(const SkColorMatrix &)
void attachDiscardableAdapter(sk_sp< T > adapter) const
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial gc
Definition switches.h:238
SkScalar ScalarValue
Definition Skottie.h:32
Definition ref_ptr.h:256