| 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 |
|
| 8 |
#include "include/core/SkColorFilter.h" |
| 9 |
#include "include/core/SkRefCnt.h" |
| 10 |
#include "include/private/base/SkTPin.h" |
| 11 |
#include "modules/skottie/src/SkottiePriv.h" |
| 12 |
#include "modules/skottie/src/SkottieValue.h" |
| 13 |
#include "modules/skottie/src/animator/Animator.h" |
| 14 |
#include "modules/skottie/src/effects/Effects.h" |
| 15 |
#include "modules/sksg/include/SkSGColorFilter.h" |
| 16 |
#include "modules/sksg/include/SkSGRenderNode.h" |
| 17 |
#include "src/core/SkColorData.h" |
| 18 |
|
| 19 |
#include <array> |
| 20 |
#include <cstddef> |
| 21 |
#include <cstdint> |
| 22 |
#include <utility> |
| 23 |
|
| 24 |
namespace skjson { |
| 25 |
class ArrayValue; |
| 26 |
} |
| 27 |
|
| 28 |
namespace skottie { |
| 29 |
namespace internal { |
| 30 |
|
| 31 |
namespace { |
| 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 |
*/ |
| 42 |
class ShiftChannelsEffectAdapter final : public AnimatablePropertyContainer { |
| 43 |
public: |
| 44 |
static sk_sp<ShiftChannelsEffectAdapter> Make(const skjson::ArrayValue& jprops, |
| 45 |
sk_sp<sksg::RenderNode> layer, |
| 46 |
const AnimationBuilder* abuilder) { |
| 47 |
return sk_sp<ShiftChannelsEffectAdapter>( |
| 48 |
new ShiftChannelsEffectAdapter(jprops, std::move(layer), abuilder)); |
| 49 |
} |
| 50 |
|
| 51 |
const sk_sp<sksg::ExternalColorFilter>& node() const { return fColorFilter; } |
| 52 |
|
| 53 |
private: |
| 54 |
ShiftChannelsEffectAdapter(const skjson::ArrayValue& jprops, |
| 55 |
sk_sp<sksg::RenderNode> layer, |
| 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 |
// When applied to the alpha channel, kFullOn expands the effect coverage |
| 126 |
// to the content bounding box. |
| 127 |
fColorFilter->setCoverage(fA == static_cast<float>(Source::kFullOn) |
| 128 |
? sksg::ExternalColorFilter::Coverage::kBoundingBox |
| 129 |
: sksg::ExternalColorFilter::Coverage::kNormal); |
| 130 |
} |
| 131 |
|
| 132 |
const sk_sp<sksg::ExternalColorFilter> fColorFilter; |
| 133 |
|
| 134 |
ScalarValue fR = static_cast<float>(Source::kRed), |
| 135 |
fG = static_cast<float>(Source::kGreen), |
| 136 |
fB = static_cast<float>(Source::kBlue), |
| 137 |
fA = static_cast<float>(Source::kAlpha); |
| 138 |
}; |
| 139 |
|
| 140 |
} // namespace |
| 141 |
|
| 142 |
|
| 143 |
sk_sp<sksg::RenderNode> EffectBuilder::attachShiftChannelsEffect( |
| 144 |
const skjson::ArrayValue& jprops, sk_sp<sksg::RenderNode> layer) const { |
| 145 |
return fBuilder->attachDiscardableAdapter<ShiftChannelsEffectAdapter>(jprops, |
| 146 |
std::move(layer), |
| 147 |
fBuilder); |
| 148 |
} |
| 149 |
|
| 150 |
} // namespace internal |
| 151 |
} // namespace skottie |