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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/gpu/GrTexture.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 11 | #include "src/gpu/GrTextureProxy.h" |
Ben Wagner | 729a23f | 2019-05-17 16:29:34 -0400 | [diff] [blame] | 12 | #include "src/gpu/ccpr/GrCCClipPath.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 14 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 15 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 16 | GrCCClipProcessor::GrCCClipProcessor(const GrCCClipPath* clipPath, IsCoverageCount isCoverageCount, |
| 17 | MustCheckBounds mustCheckBounds) |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 18 | : INHERITED(kGrCCClipProcessor_ClassID, kCompatibleWithCoverageAsAlpha_OptimizationFlag) |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 19 | , fClipPath(clipPath) |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 20 | , fIsCoverageCount(IsCoverageCount::kYes == isCoverageCount) |
| 21 | , fMustCheckBounds(MustCheckBounds::kYes == mustCheckBounds) |
Robert Phillips | f272bea | 2019-10-17 08:56:16 -0400 | [diff] [blame] | 22 | , fAtlasAccess(sk_ref_sp(fClipPath->atlasLazyProxy())) { |
Robert Phillips | bd99c0c | 2019-12-12 13:26:58 +0000 | [diff] [blame] | 23 | SkASSERT(fAtlasAccess.view().proxy()); |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 24 | this->setTextureSamplerCnt(1); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 25 | } |
| 26 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 27 | std::unique_ptr<GrFragmentProcessor> GrCCClipProcessor::clone() const { |
Mike Klein | f46d5ca | 2019-12-11 10:45:01 -0500 | [diff] [blame] | 28 | return std::make_unique<GrCCClipProcessor>( |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 29 | fClipPath, IsCoverageCount(fIsCoverageCount), MustCheckBounds(fMustCheckBounds)); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 32 | void GrCCClipProcessor::onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const { |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 33 | const SkPath& clipPath = fClipPath->deviceSpacePath(); |
| 34 | uint32_t key = (fIsCoverageCount) ? (uint32_t)GrFillRuleForSkPath(clipPath) : 0; |
| 35 | key = (key << 1) | ((clipPath.isInverseFillType()) ? 1 : 0); |
| 36 | key = (key << 1) | ((fMustCheckBounds) ? 1 : 0); |
| 37 | b->add32(key); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 40 | bool GrCCClipProcessor::onIsEqual(const GrFragmentProcessor& fp) const { |
| 41 | const GrCCClipProcessor& that = fp.cast<GrCCClipProcessor>(); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 42 | // Each ClipPath path has a unique atlas proxy, so hasSameSamplersAndAccesses should have |
| 43 | // already weeded out FPs with different ClipPaths. |
| 44 | SkASSERT(that.fClipPath->deviceSpacePath().getGenerationID() == |
| 45 | fClipPath->deviceSpacePath().getGenerationID()); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 46 | return that.fClipPath->deviceSpacePath().getFillType() == |
| 47 | fClipPath->deviceSpacePath().getFillType() && |
| 48 | that.fIsCoverageCount == fIsCoverageCount && that.fMustCheckBounds == fMustCheckBounds; |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 51 | class GrCCClipProcessor::Impl : public GrGLSLFragmentProcessor { |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 52 | public: |
| 53 | void emitCode(EmitArgs& args) override { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 54 | const GrCCClipProcessor& proc = args.fFp.cast<GrCCClipProcessor>(); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 55 | GrGLSLUniformHandler* uniHandler = args.fUniformHandler; |
| 56 | GrGLSLFPFragmentBuilder* f = args.fFragBuilder; |
| 57 | |
| 58 | f->codeAppend ("half coverage;"); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 59 | |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 60 | if (proc.fMustCheckBounds) { |
| 61 | const char* pathIBounds; |
| 62 | fPathIBoundsUniform = uniHandler->addUniform(kFragment_GrShaderFlag, kFloat4_GrSLType, |
| 63 | "path_ibounds", &pathIBounds); |
| 64 | f->codeAppendf("if (all(greaterThan(float4(sk_FragCoord.xy, %s.zw), " |
| 65 | "float4(%s.xy, sk_FragCoord.xy)))) {", |
| 66 | pathIBounds, pathIBounds); |
| 67 | } |
| 68 | |
| 69 | const char* atlasTransform; |
| 70 | fAtlasTransformUniform = uniHandler->addUniform(kFragment_GrShaderFlag, kFloat4_GrSLType, |
| 71 | "atlas_transform", &atlasTransform); |
| 72 | f->codeAppendf("float2 texcoord = sk_FragCoord.xy * %s.xy + %s.zw;", |
| 73 | atlasTransform, atlasTransform); |
| 74 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 75 | f->codeAppend ("coverage = "); |
Brian Salomon | d19cd76 | 2020-01-06 13:16:31 -0500 | [diff] [blame] | 76 | f->appendTextureLookup(args.fTexSamplers[0], "texcoord"); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 77 | f->codeAppend (".a;"); |
| 78 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 79 | if (proc.fIsCoverageCount) { |
| 80 | auto fillRule = GrFillRuleForSkPath(proc.fClipPath->deviceSpacePath()); |
| 81 | if (GrFillRule::kEvenOdd == fillRule) { |
| 82 | f->codeAppend ("half t = mod(abs(coverage), 2);"); |
| 83 | f->codeAppend ("coverage = 1 - abs(t - 1);"); |
| 84 | } else { |
| 85 | SkASSERT(GrFillRule::kNonzero == fillRule); |
| 86 | f->codeAppend ("coverage = min(abs(coverage), 1);"); |
| 87 | } |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | if (proc.fMustCheckBounds) { |
| 91 | f->codeAppend ("} else {"); |
| 92 | f->codeAppend ( "coverage = 0;"); |
| 93 | f->codeAppend ("}"); |
| 94 | } |
| 95 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame] | 96 | if (proc.fClipPath->deviceSpacePath().isInverseFillType()) { |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 97 | f->codeAppend ("coverage = 1 - coverage;"); |
| 98 | } |
| 99 | |
| 100 | f->codeAppendf("%s = %s * coverage;", args.fOutputColor, args.fInputColor); |
| 101 | } |
| 102 | |
| 103 | void onSetData(const GrGLSLProgramDataManager& pdman, |
| 104 | const GrFragmentProcessor& fp) override { |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 105 | const GrCCClipProcessor& proc = fp.cast<GrCCClipProcessor>(); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 106 | if (proc.fMustCheckBounds) { |
| 107 | const SkRect pathIBounds = SkRect::Make(proc.fClipPath->pathDevIBounds()); |
| 108 | pdman.set4f(fPathIBoundsUniform, pathIBounds.left(), pathIBounds.top(), |
| 109 | pathIBounds.right(), pathIBounds.bottom()); |
| 110 | } |
| 111 | const SkVector& scale = proc.fClipPath->atlasScale(); |
| 112 | const SkVector& trans = proc.fClipPath->atlasTranslate(); |
| 113 | pdman.set4f(fAtlasTransformUniform, scale.x(), scale.y(), trans.x(), trans.y()); |
| 114 | } |
| 115 | |
| 116 | private: |
| 117 | UniformHandle fPathIBoundsUniform; |
| 118 | UniformHandle fAtlasTransformUniform; |
| 119 | }; |
| 120 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 121 | GrGLSLFragmentProcessor* GrCCClipProcessor::onCreateGLSLInstance() const { |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 122 | return new Impl(); |
| 123 | } |