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