Flutter Engine
The Flutter Engine
BlendModes.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2022 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 */
18#include "src/utils/SkJSON.h"
19
20#include <array>
21#include <cstddef>
22#include <utility>
23
24namespace skottie::internal {
25
26namespace {
27
28enum CustomBlenders {
29 HARDMIX = 17,
30};
31
32static sk_sp<SkBlender> hardMix() {
33 static SkRuntimeEffect* hardMixEffect = []{
34 const char hardMix[] =
35 "half4 main(half4 src, half4 dst) {"
36 "src.rgb = unpremul(src).rgb + unpremul(dst).rgb;"
37 "src.rgb = min(floor(src.rgb), 1) * src.a;"
38
39 "return src + (1 - src.a)*dst;"
40 "}"
41 ;
43 return result.effect.release();
44 }();
45 return hardMixEffect->makeBlender(nullptr);
46}
47
48static sk_sp<SkBlender> get_blender(const skjson::ObjectValue& jobject,
49 const AnimationBuilder* abuilder) {
50 static constexpr SkBlendMode kBlendModeMap[] = {
51 SkBlendMode::kSrcOver, // 0:'normal'
52 SkBlendMode::kMultiply, // 1:'multiply'
53 SkBlendMode::kScreen, // 2:'screen'
54 SkBlendMode::kOverlay, // 3:'overlay
55 SkBlendMode::kDarken, // 4:'darken'
56 SkBlendMode::kLighten, // 5:'lighten'
57 SkBlendMode::kColorDodge, // 6:'color-dodge'
58 SkBlendMode::kColorBurn, // 7:'color-burn'
59 SkBlendMode::kHardLight, // 8:'hard-light'
60 SkBlendMode::kSoftLight, // 9:'soft-light'
61 SkBlendMode::kDifference, // 10:'difference'
62 SkBlendMode::kExclusion, // 11:'exclusion'
63 SkBlendMode::kHue, // 12:'hue'
64 SkBlendMode::kSaturation, // 13:'saturation'
65 SkBlendMode::kColor, // 14:'color'
66 SkBlendMode::kLuminosity, // 15:'luminosity'
67 SkBlendMode::kPlus, // 16:'add'
68 };
69
70 const size_t mode = ParseDefault<size_t>(jobject["bm"], 0);
71
72 // Special handling of src-over, so we can detect the trivial/no-fancy-blending case
73 // (a null blender is equivalent to src-over).
74 if (!mode) {
75 return nullptr;
76 }
77
78 // Modes that are expressible as SkBlendMode.
79 if (mode < std::size(kBlendModeMap)) {
80 return SkBlender::Mode(kBlendModeMap[mode]);
81 }
82
83 // Modes that require custom blenders.
84 switch (mode)
85 {
86 case HARDMIX:
87 return hardMix();
88 default:
89 break;
90 }
91
92 abuilder->log(Logger::Level::kWarning, &jobject, "Unsupported blend mode %zu\n", mode);
93 return nullptr;
94}
95
96} // namespace
97
98sk_sp<sksg::RenderNode> AnimationBuilder::attachBlendMode(const skjson::ObjectValue& jobject,
99 sk_sp<sksg::RenderNode> child) const {
100 if (auto blender = get_blender(jobject, this)) {
101 fHasNontrivialBlending = true;
102 child = sksg::BlenderEffect::Make(std::move(child), std::move(blender));
103 }
104
105 return child;
106}
107
108} // namespace skottie::internal
SkBlendMode
Definition: SkBlendMode.h:38
@ kExclusion
rc = s + d - two(s*d), ra = kSrcOver
@ kSaturation
saturation of source with hue and luminosity of destination
@ kColorBurn
darken destination to reflect source
@ kPlus
r = min(s + d, 1)
@ kLighten
rc = s + d - min(s*da, d*sa), ra = kSrcOver
@ kHue
hue of source with saturation and luminosity of destination
@ kMultiply
r = s*(1-da) + d*(1-sa) + s*d
@ kColorDodge
brighten destination to reflect source
@ kScreen
r = s + d - s*d
@ kSrcOver
r = s + (1-sa)*d
@ kLuminosity
luminosity of source with hue and saturation of destination
@ kSoftLight
lighten or darken, depending on source
@ kDifference
rc = s + d - 2*(min(s*da, d*sa)), ra = kSrcOver
@ kOverlay
multiply or screen, depending on destination
@ kColor
hue and saturation of source with luminosity of destination
@ kHardLight
multiply or screen, depending on source
@ kDarken
rc = s + d - max(s*da, d*sa), ra = kSrcOver
static sk_sp< SkBlender > Mode(SkBlendMode mode)
sk_sp< SkBlender > makeBlender(sk_sp< const SkData > uniforms, SkSpan< const ChildPtr > children={}) const
static Result MakeForBlender(SkString sksl, const Options &)
static sk_sp< BlenderEffect > Make(sk_sp< RenderNode > child, sk_sp< SkBlender >=nullptr)
GAsyncResult * result
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 mode
Definition: switches.h:228
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 On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259