blob: 738e126e11b26ff19f7aae63fcf5b2627453bd61 [file] [log] [blame]
Chris Daltonabed2672021-06-17 16:54:28 -06001/*
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
Chris Daltonc3176002021-07-23 15:33:09 -06008#include "src/gpu/effects/GrModulateAtlasCoverageEffect.h"
Chris Daltonabed2672021-06-17 16:54:28 -06009
10#include "src/gpu/GrDynamicAtlas.h"
11#include "src/gpu/effects/GrTextureEffect.h"
12
Chris Daltonc3176002021-07-23 15:33:09 -060013GrModulateAtlasCoverageEffect::GrModulateAtlasCoverageEffect(
14 Flags flags,
15 std::unique_ptr<GrFragmentProcessor> inputFP,
16 GrSurfaceProxyView atlasView,
17 const SkMatrix& devToAtlasMatrix,
18 const SkIRect& devIBounds)
19 : GrFragmentProcessor(kTessellate_GrModulateAtlasCoverageEffect_ClassID,
Chris Daltonabed2672021-06-17 16:54:28 -060020 kCompatibleWithCoverageAsAlpha_OptimizationFlag)
21 , fFlags(flags)
22 , fBounds((fFlags & Flags::kCheckBounds) ? devIBounds : SkIRect{0,0,0,0}) {
23 this->registerChild(std::move(inputFP));
24 this->registerChild(GrTextureEffect::Make(std::move(atlasView), kUnknown_SkAlphaType,
25 devToAtlasMatrix, GrSamplerState::Filter::kNearest),
26 SkSL::SampleUsage::Explicit());
27}
28
Chris Daltonc3176002021-07-23 15:33:09 -060029GrModulateAtlasCoverageEffect::GrModulateAtlasCoverageEffect(
30 const GrModulateAtlasCoverageEffect& that)
31 : GrFragmentProcessor(kTessellate_GrModulateAtlasCoverageEffect_ClassID,
Chris Daltonabed2672021-06-17 16:54:28 -060032 kCompatibleWithCoverageAsAlpha_OptimizationFlag)
33 , fFlags(that.fFlags)
34 , fBounds(that.fBounds) {
35 this->cloneAndRegisterAllChildProcessors(that);
36}
37
Brian Salomon3176e862021-08-09 11:23:04 -040038std::unique_ptr<GrFragmentProcessor::ProgramImpl>
39GrModulateAtlasCoverageEffect::onMakeProgramImpl() const {
40 class Impl : public ProgramImpl {
Chris Daltonabed2672021-06-17 16:54:28 -060041 void emitCode(EmitArgs& args) override {
Chris Daltonc3176002021-07-23 15:33:09 -060042 auto fp = args.fFp.cast<GrModulateAtlasCoverageEffect>();
Chris Daltonabed2672021-06-17 16:54:28 -060043 auto f = args.fFragBuilder;
44 auto uniHandler = args.fUniformHandler;
45 SkString inputColor = this->invokeChild(0, args);
46 f->codeAppend("half coverage = 0;");
47 if (fp.fFlags & Flags::kCheckBounds) {
48 const char* boundsName;
49 fBoundsUniform = uniHandler->addUniform(&fp, kFragment_GrShaderFlag,
50 kFloat4_GrSLType, "bounds", &boundsName);
51 // Are we inside the path's valid atlas bounds?
52 f->codeAppendf("if (all(greaterThan(sk_FragCoord.xy, %s.xy)) && "
53 "all(lessThan(sk_FragCoord.xy, %s.zw))) ",
54 boundsName, boundsName);
55 }
56 f->codeAppendf("{");
57 SkString atlasCoverage = this->invokeChild(1, args, "sk_FragCoord.xy");
58 f->codeAppendf("coverage = %s.a;", atlasCoverage.c_str());
59 f->codeAppendf("}");
60 const char* coverageMaybeInvertName;
61 fCoverageMaybeInvertUniform = uniHandler->addUniform(&fp, kFragment_GrShaderFlag,
62 kHalf2_GrSLType, "coverageInvert",
63 &coverageMaybeInvertName);
64 // Invert coverage, if needed.
65 f->codeAppendf("coverage = coverage * %s.x + %s.y;",
66 coverageMaybeInvertName, coverageMaybeInvertName);
67 f->codeAppendf("return %s * coverage;", inputColor.c_str());
68 }
69 void onSetData(const GrGLSLProgramDataManager& pdman,
70 const GrFragmentProcessor& processor) override {
Chris Daltonc3176002021-07-23 15:33:09 -060071 auto fp = processor.cast<GrModulateAtlasCoverageEffect>();
Chris Daltonabed2672021-06-17 16:54:28 -060072 if (fp.fFlags & Flags::kCheckBounds) {
73 pdman.set4fv(fBoundsUniform, 1, SkRect::Make(fp.fBounds).asScalars());
74 }
75 if (fp.fFlags & Flags::kInvertCoverage) {
76 pdman.set2f(fCoverageMaybeInvertUniform, -1, 1); // -1*coverage + 1 = 1 - coverage.
77 } else {
78 pdman.set2f(fCoverageMaybeInvertUniform, 1, 0); // 1*coverage + 0 = coverage.
79 }
80 }
81 UniformHandle fBoundsUniform;
82 UniformHandle fCoverageMaybeInvertUniform;
83 };
84 return std::make_unique<Impl>();
85}