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