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 | |
Brian Salomon | 5cd393b | 2020-06-12 11:31:37 -0400 | [diff] [blame^] | 22 | GrCCClipProcessor::GrCCClipProcessor(const GrCaps& caps, |
| 23 | const GrCCClipPath* clipPath, |
Greg Daniel | e810d83 | 2020-02-07 17:20:56 -0500 | [diff] [blame] | 24 | IsCoverageCount isCoverageCount, |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 25 | MustCheckBounds mustCheckBounds) |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 26 | : INHERITED(kGrCCClipProcessor_ClassID, kCompatibleWithCoverageAsAlpha_OptimizationFlag) |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 27 | , fClipPath(clipPath) |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 28 | , fIsCoverageCount(IsCoverageCount::kYes == isCoverageCount) |
Brian Salomon | 5cd393b | 2020-06-12 11:31:37 -0400 | [diff] [blame^] | 29 | , fMustCheckBounds(MustCheckBounds::kYes == mustCheckBounds) { |
| 30 | auto view = make_view(caps, clipPath->atlasLazyProxy(), fIsCoverageCount); |
| 31 | auto te = GrTextureEffect::Make(std::move(view), kUnknown_SkAlphaType); |
| 32 | te->setSampledWithExplicitCoords(); |
| 33 | this->registerChildProcessor(std::move(te)); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Brian Salomon | 5cd393b | 2020-06-12 11:31:37 -0400 | [diff] [blame^] | 36 | GrCCClipProcessor::GrCCClipProcessor(const GrCCClipProcessor& that) |
| 37 | : INHERITED(kGrCCClipProcessor_ClassID, that.optimizationFlags()) |
| 38 | , fClipPath(that.fClipPath) |
| 39 | , fIsCoverageCount(that.fIsCoverageCount) |
| 40 | , fMustCheckBounds(that.fMustCheckBounds) { |
| 41 | auto child = that.childProcessor(0).clone(); |
| 42 | child->setSampledWithExplicitCoords(); |
| 43 | this->registerChildProcessor(std::move(child)); |
Greg Daniel | e810d83 | 2020-02-07 17:20:56 -0500 | [diff] [blame] | 44 | } |
| 45 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 46 | std::unique_ptr<GrFragmentProcessor> GrCCClipProcessor::clone() const { |
Brian Salomon | 5cd393b | 2020-06-12 11:31:37 -0400 | [diff] [blame^] | 47 | return std::unique_ptr<GrFragmentProcessor>(new GrCCClipProcessor(*this)); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 50 | void GrCCClipProcessor::onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const { |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 51 | const SkPath& clipPath = fClipPath->deviceSpacePath(); |
| 52 | uint32_t key = (fIsCoverageCount) ? (uint32_t)GrFillRuleForSkPath(clipPath) : 0; |
| 53 | key = (key << 1) | ((clipPath.isInverseFillType()) ? 1 : 0); |
| 54 | key = (key << 1) | ((fMustCheckBounds) ? 1 : 0); |
| 55 | b->add32(key); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 58 | bool GrCCClipProcessor::onIsEqual(const GrFragmentProcessor& fp) const { |
| 59 | const GrCCClipProcessor& that = fp.cast<GrCCClipProcessor>(); |
Brian Salomon | 5cd393b | 2020-06-12 11:31:37 -0400 | [diff] [blame^] | 60 | return that.fClipPath->deviceSpacePath().getGenerationID() == |
| 61 | fClipPath->deviceSpacePath().getGenerationID() && |
| 62 | that.fClipPath->deviceSpacePath().getFillType() == |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 63 | fClipPath->deviceSpacePath().getFillType() && |
| 64 | that.fIsCoverageCount == fIsCoverageCount && that.fMustCheckBounds == fMustCheckBounds; |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 67 | class GrCCClipProcessor::Impl : public GrGLSLFragmentProcessor { |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 68 | public: |
| 69 | void emitCode(EmitArgs& args) override { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 70 | const GrCCClipProcessor& proc = args.fFp.cast<GrCCClipProcessor>(); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 71 | GrGLSLUniformHandler* uniHandler = args.fUniformHandler; |
| 72 | GrGLSLFPFragmentBuilder* f = args.fFragBuilder; |
| 73 | |
| 74 | f->codeAppend ("half coverage;"); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 75 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 76 | if (proc.fMustCheckBounds) { |
| 77 | const char* pathIBounds; |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 78 | fPathIBoundsUniform = uniHandler->addUniform(&proc, kFragment_GrShaderFlag, |
| 79 | kFloat4_GrSLType, "path_ibounds", |
| 80 | &pathIBounds); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 81 | f->codeAppendf("if (all(greaterThan(float4(sk_FragCoord.xy, %s.zw), " |
| 82 | "float4(%s.xy, sk_FragCoord.xy)))) {", |
| 83 | pathIBounds, pathIBounds); |
| 84 | } |
| 85 | |
Brian Salomon | 5cd393b | 2020-06-12 11:31:37 -0400 | [diff] [blame^] | 86 | const char* atlasTranslate; |
| 87 | fAtlasTranslateUniform = uniHandler->addUniform(&proc, kFragment_GrShaderFlag, |
| 88 | kFloat2_GrSLType, "atlas_translate", |
| 89 | &atlasTranslate); |
| 90 | SkString coord; |
| 91 | coord.printf("sk_FragCoord.xy + %s.xy", atlasTranslate); |
| 92 | SkString sample = this->invokeChild(0, args, coord.c_str()); |
| 93 | f->codeAppendf("coverage = %s.a;", sample.c_str()); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 94 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 95 | if (proc.fIsCoverageCount) { |
| 96 | auto fillRule = GrFillRuleForSkPath(proc.fClipPath->deviceSpacePath()); |
| 97 | if (GrFillRule::kEvenOdd == fillRule) { |
| 98 | f->codeAppend ("half t = mod(abs(coverage), 2);"); |
| 99 | f->codeAppend ("coverage = 1 - abs(t - 1);"); |
| 100 | } else { |
| 101 | SkASSERT(GrFillRule::kNonzero == fillRule); |
| 102 | f->codeAppend ("coverage = min(abs(coverage), 1);"); |
| 103 | } |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | if (proc.fMustCheckBounds) { |
| 107 | f->codeAppend ("} else {"); |
| 108 | f->codeAppend ( "coverage = 0;"); |
| 109 | f->codeAppend ("}"); |
| 110 | } |
| 111 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 112 | if (proc.fClipPath->deviceSpacePath().isInverseFillType()) { |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 113 | f->codeAppend ("coverage = 1 - coverage;"); |
| 114 | } |
| 115 | |
| 116 | f->codeAppendf("%s = %s * coverage;", args.fOutputColor, args.fInputColor); |
| 117 | } |
| 118 | |
| 119 | void onSetData(const GrGLSLProgramDataManager& pdman, |
| 120 | const GrFragmentProcessor& fp) override { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 121 | const GrCCClipProcessor& proc = fp.cast<GrCCClipProcessor>(); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 122 | if (proc.fMustCheckBounds) { |
| 123 | const SkRect pathIBounds = SkRect::Make(proc.fClipPath->pathDevIBounds()); |
| 124 | pdman.set4f(fPathIBoundsUniform, pathIBounds.left(), pathIBounds.top(), |
| 125 | pathIBounds.right(), pathIBounds.bottom()); |
| 126 | } |
Brian Salomon | 5cd393b | 2020-06-12 11:31:37 -0400 | [diff] [blame^] | 127 | const SkIVector& trans = proc.fClipPath->atlasTranslate(); |
| 128 | pdman.set2f(fAtlasTranslateUniform, trans.x(), trans.y()); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | private: |
| 132 | UniformHandle fPathIBoundsUniform; |
Brian Salomon | 5cd393b | 2020-06-12 11:31:37 -0400 | [diff] [blame^] | 133 | UniformHandle fAtlasTranslateUniform; |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 136 | GrGLSLFragmentProcessor* GrCCClipProcessor::onCreateGLSLInstance() const { |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 137 | return new Impl(); |
| 138 | } |