Flutter Engine
The Flutter Engine
GrBlendFragmentProcessor.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2015 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
9
13#include "src/base/SkRandom.h"
15#include "src/gpu/Blend.h"
16#include "src/gpu/KeyBuilder.h"
21
22#include <string>
23
25struct GrShaderCaps;
26
27// Some of the CPU implementations of blend modes differ from the GPU enough that
28// we can't use the CPU implementation to implement constantOutputForConstantInput.
30 // The non-separable modes differ too much. So does SoftLight. ColorBurn differs too much on our
31 // test iOS device, but we just disable it across the board since it might differ on untested
32 // GPUs.
35}
36
37//////////////////////////////////////////////////////////////////////////////
38
40public:
41 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> src,
42 std::unique_ptr<GrFragmentProcessor> dst,
44 bool shareBlendLogic) {
45 return std::unique_ptr<GrFragmentProcessor>(
46 new BlendFragmentProcessor(std::move(src), std::move(dst), mode, shareBlendLogic));
47 }
48
49 const char* name() const override { return "Blend"; }
50
51 std::unique_ptr<GrFragmentProcessor> clone() const override;
52
53private:
54 BlendFragmentProcessor(std::unique_ptr<GrFragmentProcessor> src,
55 std::unique_ptr<GrFragmentProcessor> dst,
57 bool shareBlendLogic)
58 : INHERITED(kBlendFragmentProcessor_ClassID, OptFlags(src.get(), dst.get(), mode))
59 , fMode(mode)
60 , fShareBlendLogic(shareBlendLogic) {
61 this->setIsBlendFunction();
62 this->registerChild(std::move(src));
63 this->registerChild(std::move(dst));
64 }
65
67 : INHERITED(that)
68 , fMode(that.fMode)
69 , fShareBlendLogic(that.fShareBlendLogic) {}
70
71#if defined(GR_TEST_UTILS)
72 SkString onDumpInfo() const override {
73 return SkStringPrintf("(fMode=%s)", SkBlendMode_Name(fMode));
74 }
75#endif
76
77 static OptimizationFlags OptFlags(const GrFragmentProcessor* src,
80 switch (mode) {
83 break;
84
85 // Just propagates src, and its flags:
88 ~kConstantOutputForConstantInput_OptimizationFlag;
89 break;
90
91 // Just propagates dst, and its flags:
94 ~kConstantOutputForConstantInput_OptimizationFlag;
95 break;
96
97 // Produces opaque if both src and dst are opaque. These also will modulate the child's
98 // output by either the input color or alpha. However, if the child is not compatible
99 // with the coverage as alpha then it may produce a color that is not valid premul.
103 if (src && dst) {
106 } else if (src) {
108 ~kConstantOutputForConstantInput_OptimizationFlag;
109 } else if (dst) {
111 ~kConstantOutputForConstantInput_OptimizationFlag;
112 } else {
114 }
115 break;
116
117 // Produces zero when both are opaque, indeterminate if one is opaque.
122 break;
123
124 // Is opaque if the dst is opaque.
127 break;
128
129 // DstATop is the converse of kSrcATop. Screen is also opaque if the src is a opaque.
133 break;
134
135 // These modes are all opaque if either src or dst is opaque. All the advanced modes
136 // compute alpha as src-over.
156 break;
157 }
159 (!src || src->hasConstantOutputForConstantInput()) &&
160 (!dst || dst->hasConstantOutputForConstantInput())) {
162 }
163 return flags;
164 }
165
166 void onAddToKey(const GrShaderCaps& caps, skgpu::KeyBuilder* b) const override {
167 if (fShareBlendLogic) {
168 b->add32(GrGLSLBlend::BlendKey(fMode));
169 } else {
170 b->add32((int)fMode);
171 }
172 }
173
174 bool onIsEqual(const GrFragmentProcessor& other) const override {
176 return fMode == cs.fMode;
177 }
178
179 SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
180 const auto* src = this->childProcessor(0);
181 const auto* dst = this->childProcessor(1);
182
185
186 return SkBlendMode_Apply(fMode, srcColor, dstColor);
187 }
188
189 std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override;
190
191 SkBlendMode fMode;
192 bool fShareBlendLogic;
193
195
196 using INHERITED = GrFragmentProcessor;
197};
198
199/////////////////////////////////////////////////////////////////////
200
201
203
204#if defined(GR_TEST_UTILS)
205std::unique_ptr<GrFragmentProcessor> BlendFragmentProcessor::TestCreate(GrProcessorTestData* d) {
206 // Create one or two random fragment processors.
207 std::unique_ptr<GrFragmentProcessor> src(GrProcessorUnitTest::MakeOptionalChildFP(d));
208 std::unique_ptr<GrFragmentProcessor> dst(GrProcessorUnitTest::MakeChildFP(d));
209 if (d->fRandom->nextBool()) {
210 std::swap(src, dst);
211 }
212 bool shareLogic = d->fRandom->nextBool();
213
215 static_cast<SkBlendMode>(d->fRandom->nextRangeU(0, (int)SkBlendMode::kLastMode));
216 return std::unique_ptr<GrFragmentProcessor>(
217 new BlendFragmentProcessor(std::move(src), std::move(dst), mode, shareLogic));
218}
219#endif
220
221std::unique_ptr<GrFragmentProcessor> BlendFragmentProcessor::clone() const {
222 return std::unique_ptr<GrFragmentProcessor>(new BlendFragmentProcessor(*this));
223}
224
225std::unique_ptr<GrFragmentProcessor::ProgramImpl> BlendFragmentProcessor::onMakeProgramImpl()
226 const {
227 class Impl : public ProgramImpl {
228 public:
229 void emitCode(EmitArgs& args) override {
230 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
231 const BlendFragmentProcessor& bfp = args.fFp.cast<BlendFragmentProcessor>();
232 const SkBlendMode mode = bfp.fMode;
233
234 // Invoke src/dst with our input color (or substitute input color if no child FP)
235 SkString srcColor = this->invokeChild(0, args);
236 SkString dstColor = this->invokeChild(1, args);
237
238 if (bfp.fShareBlendLogic) {
239 // Use a blend expression that may rely on uniforms.
240 std::string blendExpr = GrGLSLBlend::BlendExpression(&args.fFp,
241 args.fUniformHandler,
242 &fBlendUniform,
243 srcColor.c_str(),
244 dstColor.c_str(),
245 mode);
246 fragBuilder->codeAppendf("return %s;", blendExpr.c_str());
247 } else {
248 // Blend src and dst colors together using a hardwired built-in blend function.
249 fragBuilder->codeAppendf("return %s(%s, %s);",
251 srcColor.c_str(),
252 dstColor.c_str());
253 }
254 }
255
256 void onSetData(const GrGLSLProgramDataManager& pdman,
257 const GrFragmentProcessor& fp) override {
259 if (bfp.fShareBlendLogic) {
260 GrGLSLBlend::SetBlendModeUniformData(pdman, fBlendUniform, bfp.fMode);
261 }
262 }
263
264 UniformHandle fBlendUniform;
265 };
266
267 return std::make_unique<Impl>();
268}
269
270//////////////////////////////////////////////////////////////////////////////
271
272std::unique_ptr<GrFragmentProcessor> GrBlendFragmentProcessor::Make(
273 std::unique_ptr<GrFragmentProcessor> src,
274 std::unique_ptr<GrFragmentProcessor> dst,
276 bool shareBlendLogic) {
277 // These modes simplify dramatically in the shader, but only if we bypass the shared logic:
279 shareBlendLogic = false;
280 }
281
282 return BlendFragmentProcessor::Make(std::move(src), std::move(dst), mode, shareBlendLogic);
283}
static bool does_cpu_blend_impl_match_gpu(SkBlendMode mode)
#define GR_DEFINE_FRAGMENT_PROCESSOR_TEST(...)
#define GR_DECLARE_FRAGMENT_PROCESSOR_TEST
SkPMColor4f SkBlendMode_Apply(SkBlendMode mode, const SkPMColor4f &src, const SkPMColor4f &dst)
SK_API const char * SkBlendMode_Name(SkBlendMode blendMode)
SkBlendMode
Definition: SkBlendMode.h:38
@ kSrcOut
r = s * (1-da)
@ 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
@ kDstIn
r = d * sa
@ kModulate
r = s*d
@ 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
@ kXor
r = s*(1-da) + d*(1-sa)
@ kLastSeparableMode
last blend mode operating separately on components
@ 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
@ kSrcATop
r = s*da + d*(1-sa)
@ kDstATop
r = d*sa + s*(1-da)
@ kDstOver
r = d + (1-da)*s
@ kLastMode
last valid value
@ kColor
hue and saturation of source with luminosity of destination
@ kHardLight
multiply or screen, depending on source
@ kDstOut
r = d * (1-sa)
@ kDarken
rc = s + d - max(s*da, d*sa), ra = kSrcOver
@ kSrcIn
r = s * da
@ kClear
r = 0
void swap(sk_sp< T > &a, sk_sp< T > &b)
Definition: SkRefCnt.h:341
SK_API SkString SkStringPrintf(const char *format,...) SK_PRINTF_LIKE(1
Creates a new string and writes into it using a printf()-style format.
static std::unique_ptr< GrFragmentProcessor > Make(std::unique_ptr< GrFragmentProcessor > src, std::unique_ptr< GrFragmentProcessor > dst, SkBlendMode mode, bool shareBlendLogic)
const char * name() const override
std::unique_ptr< GrFragmentProcessor > clone() const override
GrFragmentProcessor(ClassID classID, OptimizationFlags optimizationFlags)
static OptimizationFlags ProcessorOptimizationFlags(const GrFragmentProcessor *fp)
GrFragmentProcessor * childProcessor(int index)
void registerChild(std::unique_ptr< GrFragmentProcessor > child, SkSL::SampleUsage sampleUsage=SkSL::SampleUsage::PassThrough())
static SkPMColor4f ConstantOutputForConstantInput(const GrFragmentProcessor *fp, const SkPMColor4f &input)
void codeAppendf(const char format[],...) SK_PRINTF_LIKE(2
const T & cast() const
Definition: GrProcessor.h:127
@ kBlendFragmentProcessor_ClassID
Definition: GrProcessor.h:29
const char * c_str() const
Definition: SkString.h:133
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition: main.cc:19
static bool b
FlutterSemanticsFlag flags
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
std::unique_ptr< GrFragmentProcessor > Make(std::unique_ptr< GrFragmentProcessor > src, std::unique_ptr< GrFragmentProcessor > dst, SkBlendMode mode, bool shareBlendLogic=true)
GrFPResult MakeChildFP(const SkRuntimeEffect::ChildPtr &child, const GrFPArgs &childArgs)
std::string BlendExpression(const GrProcessor *processor, GrGLSLUniformHandler *uniformHandler, GrGLSLProgramDataManager::UniformHandle *blendUniform, const char *srcColor, const char *dstColor, SkBlendMode mode)
Definition: GrGLSLBlend.cpp:20
int BlendKey(SkBlendMode mode)
Definition: GrGLSLBlend.cpp:40
void SetBlendModeUniformData(const GrGLSLProgramDataManager &pdman, GrGLSLProgramDataManager::UniformHandle blendUniform, SkBlendMode mode)
Definition: GrGLSLBlend.cpp:73
const uint32_t fp
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
dst
Definition: cp.py:12
const myers::Point & get(const myers::Segment &)
const char * BlendFuncName(SkBlendMode mode)
Definition: Blend.cpp:18