Flutter Engine
The Flutter Engine
ThresholdEffect.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2021 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
19
20#include <cstddef>
21#include <utility>
22
23namespace skjson {
24class ArrayValue;
25}
26
27namespace skottie::internal {
28namespace {
29
30// Convert to black & white, based on input luminance and a threshold uniform.
31static constexpr char gThresholdSkSL[] =
32 "uniform half t;"
33
34 "half4 main(half4 color) {"
35 "half4 c = unpremul(color);"
36
37 "half lum = dot(c.rgb, half3(0.2126, 0.7152, 0.0722)),"
38 "bw = step(t, lum);"
39
40 "return bw.xxx1 * c.a;"
41 "}"
42;
43
44static sk_sp<SkRuntimeEffect> threshold_effect() {
45 static const SkRuntimeEffect* effect =
46 SkRuntimeEffect::MakeForColorFilter(SkString(gThresholdSkSL), {}).effect.release();
47 SkASSERT(effect);
48
49 return sk_ref_sp(effect);
50}
51
52class ThresholdAdapter final : public DiscardableAdapterBase<ThresholdAdapter,
53 sksg::ExternalColorFilter> {
54public:
55 ThresholdAdapter(const skjson::ArrayValue& jprops,
57 const AnimationBuilder& abuilder)
58 : INHERITED(sksg::ExternalColorFilter::Make(std::move(layer)))
59 {
60 enum : size_t {
61 kLevel_Index = 0,
62 };
63
64 EffectBinder(jprops, abuilder, this).bind(kLevel_Index, fLevel);
65 }
66
67private:
68 void onSync() override {
69 auto cf =
70 threshold_effect()->makeColorFilter(SkData::MakeWithCopy(&fLevel, sizeof(fLevel)));
71
72 this->node()->setColorFilter(std::move(cf));
73 }
74
75 ScalarValue fLevel = 0;
76
77 using INHERITED = DiscardableAdapterBase<ThresholdAdapter, sksg::ExternalColorFilter>;
78};
79
80} // namespace
81
82sk_sp<sksg::RenderNode> EffectBuilder::attachThresholdEffect(const skjson::ArrayValue& jprops,
83 sk_sp<sksg::RenderNode> layer) const {
84 return fBuilder->attachDiscardableAdapter<ThresholdAdapter>(jprops,
85 std::move(layer),
86 *fBuilder);
87}
88
89} // namespace skottie::internal
#define SkASSERT(cond)
Definition: SkAssert.h:116
#define INHERITED(method,...)
Definition: SkRecorder.cpp:128
sk_sp< T > sk_ref_sp(T *obj)
Definition: SkRefCnt.h:381
static sk_sp< SkData > MakeWithCopy(const void *data, size_t length)
Definition: SkData.cpp:111
static Result MakeForColorFilter(SkString sksl, const Options &)
void attachDiscardableAdapter(sk_sp< T > adapter) const
Definition: SkottiePriv.h:139
SK_API sk_sp< SkDocument > Make(SkWStream *dst, const SkSerialProcs *=nullptr, std::function< void(const SkPicture *)> onEndPage=nullptr)
SkScalar ScalarValue
Definition: SkottieValue.h:22
Definition: Skottie.h:32
Definition: ref_ptr.h:256