Flutter Engine
The Flutter Engine
GrModulateAtlasCoverageEffect.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2021 Google LLC.
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
16#include "src/gpu/KeyBuilder.h"
23
24#include <utility>
25
26class SkMatrix;
27struct GrShaderCaps;
28
31 std::unique_ptr<GrFragmentProcessor> inputFP,
32 GrSurfaceProxyView atlasView,
33 const SkMatrix& devToAtlasMatrix,
34 const SkIRect& devIBounds)
35 : GrFragmentProcessor(kTessellate_GrModulateAtlasCoverageEffect_ClassID,
36 kCompatibleWithCoverageAsAlpha_OptimizationFlag)
37 , fFlags(flags)
38 , fBounds((fFlags & Flags::kCheckBounds) ? devIBounds : SkIRect{0,0,0,0}) {
39 this->registerChild(std::move(inputFP));
41 devToAtlasMatrix, GrSamplerState::Filter::kNearest),
43}
44
48 , fFlags(that.fFlags)
49 , fBounds(that.fBounds) {}
50
51void GrModulateAtlasCoverageEffect::onAddToKey(const GrShaderCaps&,
52 skgpu::KeyBuilder* b) const {
54}
55
56std::unique_ptr<GrFragmentProcessor::ProgramImpl>
57GrModulateAtlasCoverageEffect::onMakeProgramImpl() const {
58 class Impl : public ProgramImpl {
59 void emitCode(EmitArgs& args) override {
60 auto fp = args.fFp.cast<GrModulateAtlasCoverageEffect>();
61 auto f = args.fFragBuilder;
62 auto uniHandler = args.fUniformHandler;
63 SkString inputColor = this->invokeChild(0, args);
64 f->codeAppend("half coverage = 0;");
65 if (fp.fFlags & Flags::kCheckBounds) {
66 const char* boundsName;
67 fBoundsUniform = uniHandler->addUniform(&fp, kFragment_GrShaderFlag,
68 SkSLType::kFloat4, "bounds", &boundsName);
69 // Are we inside the path's valid atlas bounds?
70 f->codeAppendf("if (all(greaterThan(sk_FragCoord.xy, %s.xy)) && "
71 "all(lessThan(sk_FragCoord.xy, %s.zw))) ",
72 boundsName, boundsName);
73 }
74 f->codeAppendf("{");
75 SkString atlasCoverage = this->invokeChild(1, args, "sk_FragCoord.xy");
76 f->codeAppendf("coverage = %s.a;", atlasCoverage.c_str());
77 f->codeAppendf("}");
78 const char* coverageMaybeInvertName;
79 fCoverageMaybeInvertUniform = uniHandler->addUniform(&fp, kFragment_GrShaderFlag,
80 SkSLType::kHalf2, "coverageInvert",
81 &coverageMaybeInvertName);
82 // Invert coverage, if needed.
83 f->codeAppendf("coverage = coverage * %s.x + %s.y;",
84 coverageMaybeInvertName, coverageMaybeInvertName);
85 f->codeAppendf("return %s * coverage;", inputColor.c_str());
86 }
87
88 private:
89 void onSetData(const GrGLSLProgramDataManager& pdman,
90 const GrFragmentProcessor& processor) override {
91 const auto& fp = processor.cast<GrModulateAtlasCoverageEffect>();
92 if (fp.fFlags & Flags::kCheckBounds) {
93 pdman.set4fv(fBoundsUniform, 1, SkRect::Make(fp.fBounds).asScalars());
94 }
95 if (fp.fFlags & Flags::kInvertCoverage) {
96 pdman.set2f(fCoverageMaybeInvertUniform, -1, 1); // -1*coverage + 1 = 1 - coverage.
97 } else {
98 pdman.set2f(fCoverageMaybeInvertUniform, 1, 0); // 1*coverage + 0 = coverage.
99 }
100 }
101 UniformHandle fBoundsUniform;
102 UniformHandle fCoverageMaybeInvertUniform;
103 };
104
105 return std::make_unique<Impl>();
106}
@ kFragment_GrShaderFlag
Definition: GrTypesPriv.h:287
const SkRect fBounds
uint16_t fFlags
Definition: ShapeLayer.cpp:106
@ kUnknown_SkAlphaType
uninitialized
Definition: SkAlphaType.h:27
void registerChild(std::unique_ptr< GrFragmentProcessor > child, SkSL::SampleUsage sampleUsage=SkSL::SampleUsage::PassThrough())
virtual void set4fv(UniformHandle, int arrayCount, const float v[]) const =0
virtual void set2f(UniformHandle, float, float) const =0
GrModulateAtlasCoverageEffect(Flags flags, std::unique_ptr< GrFragmentProcessor > inputFP, GrSurfaceProxyView atlasView, const SkMatrix &devToAtlasMatrix, const SkIRect &devIBounds)
const T & cast() const
Definition: GrProcessor.h:127
static std::unique_ptr< GrFragmentProcessor > Make(GrSurfaceProxyView, SkAlphaType, const SkMatrix &=SkMatrix::I(), GrSamplerState::Filter=GrSamplerState::Filter::kNearest, GrSamplerState::MipmapMode mipmapMode=GrSamplerState::MipmapMode::kNone)
static SampleUsage Explicit()
const char * c_str() const
Definition: SkString.h:133
static bool b
FlutterSemanticsFlag flags
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
const uint32_t fp
Definition: SkRect.h:32
static SkRect Make(const SkISize &size)
Definition: SkRect.h:669
const float * asScalars() const
Definition: SkRect.h:1340