Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/ccpr/GrCCClipProcessor.h" |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 9 | |
Ben Wagner | 729a23f | 2019-05-17 16:29:34 -0400 | [diff] [blame] | 10 | #include "src/gpu/ccpr/GrCCClipPath.h" |
Brian Salomon | 5cd393b | 2020-06-12 11:31:37 -0400 | [diff] [blame] | 11 | #include "src/gpu/effects/GrTextureEffect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 13 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 14 | |
Greg Daniel | e810d83 | 2020-02-07 17:20:56 -0500 | [diff] [blame] | 15 | static GrSurfaceProxyView make_view(const GrCaps& caps, GrSurfaceProxy* proxy, |
| 16 | bool isCoverageCount) { |
| 17 | GrColorType ct = isCoverageCount ? GrColorType::kAlpha_F16 : GrColorType::kAlpha_8; |
| 18 | GrSwizzle swizzle = caps.getReadSwizzle(proxy->backendFormat(), ct); |
| 19 | return { sk_ref_sp(proxy), GrCCAtlas::kTextureOrigin, swizzle }; |
| 20 | } |
| 21 | |
John Stiles | 956ec8a | 2020-06-19 15:32:16 -0400 | [diff] [blame] | 22 | GrCCClipProcessor::GrCCClipProcessor(std::unique_ptr<GrFragmentProcessor> inputFP, |
| 23 | const GrCaps& caps, |
Brian Salomon | 5cd393b | 2020-06-12 11:31:37 -0400 | [diff] [blame] | 24 | const GrCCClipPath* clipPath, |
Greg Daniel | e810d83 | 2020-02-07 17:20:56 -0500 | [diff] [blame] | 25 | IsCoverageCount isCoverageCount, |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 26 | MustCheckBounds mustCheckBounds) |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 27 | : INHERITED(kGrCCClipProcessor_ClassID, kCompatibleWithCoverageAsAlpha_OptimizationFlag) |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 28 | , fClipPath(clipPath) |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 29 | , fIsCoverageCount(IsCoverageCount::kYes == isCoverageCount) |
Brian Salomon | 5cd393b | 2020-06-12 11:31:37 -0400 | [diff] [blame] | 30 | , fMustCheckBounds(MustCheckBounds::kYes == mustCheckBounds) { |
| 31 | auto view = make_view(caps, clipPath->atlasLazyProxy(), fIsCoverageCount); |
John Stiles | 956ec8a | 2020-06-19 15:32:16 -0400 | [diff] [blame] | 32 | auto texEffect = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType); |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 33 | this->registerExplicitlySampledChild(std::move(texEffect)); |
John Stiles | 956ec8a | 2020-06-19 15:32:16 -0400 | [diff] [blame] | 34 | |
| 35 | if (inputFP != nullptr) { |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 36 | this->registerChild(std::move(inputFP)); |
John Stiles | 956ec8a | 2020-06-19 15:32:16 -0400 | [diff] [blame] | 37 | } |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Brian Salomon | 5cd393b | 2020-06-12 11:31:37 -0400 | [diff] [blame] | 40 | GrCCClipProcessor::GrCCClipProcessor(const GrCCClipProcessor& that) |
| 41 | : INHERITED(kGrCCClipProcessor_ClassID, that.optimizationFlags()) |
| 42 | , fClipPath(that.fClipPath) |
| 43 | , fIsCoverageCount(that.fIsCoverageCount) |
| 44 | , fMustCheckBounds(that.fMustCheckBounds) { |
John Stiles | 956ec8a | 2020-06-19 15:32:16 -0400 | [diff] [blame] | 45 | this->cloneAndRegisterAllChildProcessors(that); |
Greg Daniel | e810d83 | 2020-02-07 17:20:56 -0500 | [diff] [blame] | 46 | } |
| 47 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 48 | std::unique_ptr<GrFragmentProcessor> GrCCClipProcessor::clone() const { |
Brian Salomon | 5cd393b | 2020-06-12 11:31:37 -0400 | [diff] [blame] | 49 | return std::unique_ptr<GrFragmentProcessor>(new GrCCClipProcessor(*this)); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 52 | void GrCCClipProcessor::onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const { |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 53 | const SkPath& clipPath = fClipPath->deviceSpacePath(); |
| 54 | uint32_t key = (fIsCoverageCount) ? (uint32_t)GrFillRuleForSkPath(clipPath) : 0; |
| 55 | key = (key << 1) | ((clipPath.isInverseFillType()) ? 1 : 0); |
| 56 | key = (key << 1) | ((fMustCheckBounds) ? 1 : 0); |
| 57 | b->add32(key); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 60 | bool GrCCClipProcessor::onIsEqual(const GrFragmentProcessor& fp) const { |
| 61 | const GrCCClipProcessor& that = fp.cast<GrCCClipProcessor>(); |
Brian Salomon | 5cd393b | 2020-06-12 11:31:37 -0400 | [diff] [blame] | 62 | return that.fClipPath->deviceSpacePath().getGenerationID() == |
| 63 | fClipPath->deviceSpacePath().getGenerationID() && |
| 64 | that.fClipPath->deviceSpacePath().getFillType() == |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 65 | fClipPath->deviceSpacePath().getFillType() && |
| 66 | that.fIsCoverageCount == fIsCoverageCount && that.fMustCheckBounds == fMustCheckBounds; |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 67 | } |
| 68 | |
John Stiles | 956ec8a | 2020-06-19 15:32:16 -0400 | [diff] [blame] | 69 | bool GrCCClipProcessor::hasInputFP() const { |
| 70 | // We always have a `texEffect`, and this accounts for one child. |
| 71 | // The second child will be the input FP, if we have one. |
| 72 | return this->numChildProcessors() > 1; |
| 73 | } |
| 74 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 75 | class GrCCClipProcessor::Impl : public GrGLSLFragmentProcessor { |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 76 | public: |
| 77 | void emitCode(EmitArgs& args) override { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 78 | const GrCCClipProcessor& proc = args.fFp.cast<GrCCClipProcessor>(); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 79 | GrGLSLUniformHandler* uniHandler = args.fUniformHandler; |
| 80 | GrGLSLFPFragmentBuilder* f = args.fFragBuilder; |
| 81 | |
John Stiles | 956ec8a | 2020-06-19 15:32:16 -0400 | [diff] [blame] | 82 | f->codeAppend("half coverage;"); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 83 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 84 | if (proc.fMustCheckBounds) { |
| 85 | const char* pathIBounds; |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 86 | fPathIBoundsUniform = uniHandler->addUniform(&proc, kFragment_GrShaderFlag, |
| 87 | kFloat4_GrSLType, "path_ibounds", |
| 88 | &pathIBounds); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 89 | f->codeAppendf("if (all(greaterThan(float4(sk_FragCoord.xy, %s.zw), " |
| 90 | "float4(%s.xy, sk_FragCoord.xy)))) {", |
| 91 | pathIBounds, pathIBounds); |
| 92 | } |
| 93 | |
Brian Salomon | 5cd393b | 2020-06-12 11:31:37 -0400 | [diff] [blame] | 94 | const char* atlasTranslate; |
| 95 | fAtlasTranslateUniform = uniHandler->addUniform(&proc, kFragment_GrShaderFlag, |
| 96 | kFloat2_GrSLType, "atlas_translate", |
| 97 | &atlasTranslate); |
| 98 | SkString coord; |
| 99 | coord.printf("sk_FragCoord.xy + %s.xy", atlasTranslate); |
John Stiles | 956ec8a | 2020-06-19 15:32:16 -0400 | [diff] [blame] | 100 | constexpr int kTexEffectFPIndex = 0; |
| 101 | SkString sample = this->invokeChild(kTexEffectFPIndex, args, coord.c_str()); |
Brian Salomon | 5cd393b | 2020-06-12 11:31:37 -0400 | [diff] [blame] | 102 | f->codeAppendf("coverage = %s.a;", sample.c_str()); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 103 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 104 | if (proc.fIsCoverageCount) { |
| 105 | auto fillRule = GrFillRuleForSkPath(proc.fClipPath->deviceSpacePath()); |
| 106 | if (GrFillRule::kEvenOdd == fillRule) { |
John Stiles | 956ec8a | 2020-06-19 15:32:16 -0400 | [diff] [blame] | 107 | f->codeAppend("half t = mod(abs(coverage), 2);"); |
| 108 | f->codeAppend("coverage = 1 - abs(t - 1);"); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 109 | } else { |
| 110 | SkASSERT(GrFillRule::kNonzero == fillRule); |
John Stiles | 956ec8a | 2020-06-19 15:32:16 -0400 | [diff] [blame] | 111 | f->codeAppend("coverage = min(abs(coverage), 1);"); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 112 | } |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | if (proc.fMustCheckBounds) { |
John Stiles | 956ec8a | 2020-06-19 15:32:16 -0400 | [diff] [blame] | 116 | f->codeAppend("} else {"); |
| 117 | f->codeAppend( "coverage = 0;"); |
| 118 | f->codeAppend("}"); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 121 | if (proc.fClipPath->deviceSpacePath().isInverseFillType()) { |
John Stiles | 956ec8a | 2020-06-19 15:32:16 -0400 | [diff] [blame] | 122 | f->codeAppend("coverage = 1 - coverage;"); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 123 | } |
| 124 | |
John Stiles | 956ec8a | 2020-06-19 15:32:16 -0400 | [diff] [blame] | 125 | constexpr int kInputFPIndex = 1; |
| 126 | SkString inputColor = proc.hasInputFP() |
| 127 | ? this->invokeChild(kInputFPIndex, args.fInputColor, args) |
| 128 | : SkString(args.fInputColor); |
| 129 | |
| 130 | f->codeAppendf("%s = %s * coverage;", args.fOutputColor, inputColor.c_str()); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void onSetData(const GrGLSLProgramDataManager& pdman, |
| 134 | const GrFragmentProcessor& fp) override { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 135 | const GrCCClipProcessor& proc = fp.cast<GrCCClipProcessor>(); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 136 | if (proc.fMustCheckBounds) { |
| 137 | const SkRect pathIBounds = SkRect::Make(proc.fClipPath->pathDevIBounds()); |
| 138 | pdman.set4f(fPathIBoundsUniform, pathIBounds.left(), pathIBounds.top(), |
| 139 | pathIBounds.right(), pathIBounds.bottom()); |
| 140 | } |
Brian Salomon | 5cd393b | 2020-06-12 11:31:37 -0400 | [diff] [blame] | 141 | const SkIVector& trans = proc.fClipPath->atlasTranslate(); |
| 142 | pdman.set2f(fAtlasTranslateUniform, trans.x(), trans.y()); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | private: |
| 146 | UniformHandle fPathIBoundsUniform; |
Brian Salomon | 5cd393b | 2020-06-12 11:31:37 -0400 | [diff] [blame] | 147 | UniformHandle fAtlasTranslateUniform; |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 150 | GrGLSLFragmentProcessor* GrCCClipProcessor::onCreateGLSLInstance() const { |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 151 | return new Impl(); |
| 152 | } |