commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
| 8 | #include "GrConvexPolyEffect.h" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 9 | #include "GrInvariantOutput.h" |
reed | 026beb5 | 2015-06-10 14:23:15 -0700 | [diff] [blame] | 10 | #include "SkPathPriv.h" |
bsalomon | 7888de0 | 2016-03-28 15:04:45 -0700 | [diff] [blame] | 11 | #include "effects/GrConstColorProcessor.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 12 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 13 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 14 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 15 | #include "glsl/GrGLSLUniformHandler.h" |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 16 | #include "../private/GrGLSL.h" |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 17 | |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 18 | ////////////////////////////////////////////////////////////////////////////// |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 19 | class AARectEffect : public GrFragmentProcessor { |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 20 | public: |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 21 | const SkRect& getRect() const { return fRect; } |
| 22 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 23 | static sk_sp<GrFragmentProcessor> Make(GrPrimitiveEdgeType edgeType, const SkRect& rect) { |
| 24 | return sk_sp<GrFragmentProcessor>(new AARectEffect(edgeType, rect)); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 25 | } |
| 26 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 27 | GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; } |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 28 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 29 | const char* name() const override { return "AARect"; } |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 30 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 31 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 32 | |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 33 | private: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 34 | AARectEffect(GrPrimitiveEdgeType edgeType, const SkRect& rect) |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 35 | : INHERITED(kModulatesInput_OptimizationFlag), fRect(rect), fEdgeType(edgeType) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 36 | this->initClassID<AARectEffect>(); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 37 | } |
| 38 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 39 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 40 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 41 | bool onIsEqual(const GrFragmentProcessor& other) const override { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 42 | const AARectEffect& aare = other.cast<AARectEffect>(); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 43 | return fRect == aare.fRect; |
| 44 | } |
| 45 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 46 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 47 | if (fRect.isEmpty()) { |
| 48 | // An empty rect will have no coverage anywhere. |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 49 | inout->mulByKnownSingleComponent(0); |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 50 | } else { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 51 | inout->mulByUnknownSingleComponent(); |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 52 | } |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 53 | } |
| 54 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 55 | SkRect fRect; |
| 56 | GrPrimitiveEdgeType fEdgeType; |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 57 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 58 | typedef GrFragmentProcessor INHERITED; |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 59 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 60 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 61 | |
| 62 | }; |
| 63 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 64 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(AARectEffect); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 65 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 66 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 67 | sk_sp<GrFragmentProcessor> AARectEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 68 | SkRect rect = SkRect::MakeLTRB(d->fRandom->nextSScalar1(), |
| 69 | d->fRandom->nextSScalar1(), |
| 70 | d->fRandom->nextSScalar1(), |
| 71 | d->fRandom->nextSScalar1()); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 72 | sk_sp<GrFragmentProcessor> fp; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 73 | do { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 74 | GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>( |
| 75 | d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt)); |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 76 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 77 | fp = AARectEffect::Make(edgeType, rect); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 78 | } while (nullptr == fp); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 79 | return fp; |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 80 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 81 | #endif |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 82 | |
| 83 | ////////////////////////////////////////////////////////////////////////////// |
| 84 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 85 | class GLAARectEffect : public GrGLSLFragmentProcessor { |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 86 | public: |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 87 | GLAARectEffect() { |
| 88 | fPrevRect.fLeft = SK_ScalarNaN; |
| 89 | } |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 90 | |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 91 | void emitCode(EmitArgs&) override; |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 92 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 93 | static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 94 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 95 | protected: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 96 | void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override; |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 97 | |
| 98 | private: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 99 | GrGLSLProgramDataManager::UniformHandle fRectUniform; |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 100 | SkRect fPrevRect; |
| 101 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 102 | typedef GrGLSLFragmentProcessor INHERITED; |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 105 | void GLAARectEffect::emitCode(EmitArgs& args) { |
| 106 | const AARectEffect& aare = args.fFp.cast<AARectEffect>(); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 107 | const char *rectName; |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 108 | // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5), |
| 109 | // respectively. |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 110 | fRectUniform = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 111 | kVec4f_GrSLType, |
| 112 | kDefault_GrSLPrecision, |
| 113 | "rect", |
| 114 | &rectName); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 115 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 116 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 117 | if (GrProcessorEdgeTypeIsAA(aare.getEdgeType())) { |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 118 | // The amount of coverage removed in x and y by the edges is computed as a pair of negative |
| 119 | // numbers, xSub and ySub. |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 120 | fragBuilder->codeAppend("\t\tfloat xSub, ySub;\n"); |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame^] | 121 | fragBuilder->codeAppendf("\t\txSub = min(sk_FragCoord.x - %s.x, 0.0);\n", rectName); |
| 122 | fragBuilder->codeAppendf("\t\txSub += min(%s.z - sk_FragCoord.x, 0.0);\n", rectName); |
| 123 | fragBuilder->codeAppendf("\t\tySub = min(sk_FragCoord.y - %s.y, 0.0);\n", rectName); |
| 124 | fragBuilder->codeAppendf("\t\tySub += min(%s.w - sk_FragCoord.y, 0.0);\n", rectName); |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 125 | // Now compute coverage in x and y and multiply them to get the fraction of the pixel |
| 126 | // covered. |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 127 | fragBuilder->codeAppendf("\t\tfloat alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0));\n"); |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 128 | } else { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 129 | fragBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n"); |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame^] | 130 | fragBuilder->codeAppendf("\t\talpha *= (sk_FragCoord.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", |
| 131 | rectName); |
| 132 | fragBuilder->codeAppendf("\t\talpha *= (%s.z - sk_FragCoord.x) > -0.5 ? 1.0 : 0.0;\n", |
| 133 | rectName); |
| 134 | fragBuilder->codeAppendf("\t\talpha *= (sk_FragCoord.y - %s.y) > -0.5 ? 1.0 : 0.0;\n", |
| 135 | rectName); |
| 136 | fragBuilder->codeAppendf("\t\talpha *= (%s.w - sk_FragCoord.y) > -0.5 ? 1.0 : 0.0;\n", |
| 137 | rectName); |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 138 | } |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 139 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 140 | if (GrProcessorEdgeTypeIsInverseFill(aare.getEdgeType())) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 141 | fragBuilder->codeAppend("\t\talpha = 1.0 - alpha;\n"); |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 142 | } |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 143 | fragBuilder->codeAppendf("\t\t%s = %s;\n", args.fOutputColor, |
| 144 | (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha")).c_str()); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 145 | } |
| 146 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 147 | void GLAARectEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
| 148 | const GrProcessor& processor) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 149 | const AARectEffect& aare = processor.cast<AARectEffect>(); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 150 | const SkRect& rect = aare.getRect(); |
| 151 | if (rect != fPrevRect) { |
robertphillips | 7f14c9b | 2015-01-30 14:44:22 -0800 | [diff] [blame] | 152 | pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f, |
| 153 | rect.fRight - 0.5f, rect.fBottom - 0.5f); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 154 | fPrevRect = rect; |
| 155 | } |
| 156 | } |
| 157 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 158 | void GLAARectEffect::GenKey(const GrProcessor& processor, const GrShaderCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 159 | GrProcessorKeyBuilder* b) { |
| 160 | const AARectEffect& aare = processor.cast<AARectEffect>(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 161 | b->add32(aare.getEdgeType()); |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 164 | void AARectEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 165 | GLAARectEffect::GenKey(*this, caps, b); |
| 166 | } |
| 167 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 168 | GrGLSLFragmentProcessor* AARectEffect::onCreateGLSLInstance() const { |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 169 | return new GLAARectEffect; |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | ////////////////////////////////////////////////////////////////////////////// |
| 173 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 174 | class GrGLConvexPolyEffect : public GrGLSLFragmentProcessor { |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 175 | public: |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 176 | GrGLConvexPolyEffect() { |
| 177 | fPrevEdges[0] = SK_ScalarNaN; |
| 178 | } |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 179 | |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 180 | void emitCode(EmitArgs&) override; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 181 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 182 | static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 183 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 184 | protected: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 185 | void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 186 | |
| 187 | private: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 188 | GrGLSLProgramDataManager::UniformHandle fEdgeUniform; |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 189 | SkScalar fPrevEdges[3 * GrConvexPolyEffect::kMaxEdges]; |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 190 | typedef GrGLSLFragmentProcessor INHERITED; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 193 | void GrGLConvexPolyEffect::emitCode(EmitArgs& args) { |
| 194 | const GrConvexPolyEffect& cpe = args.fFp.cast<GrConvexPolyEffect>(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 195 | |
| 196 | const char *edgeArrayName; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 197 | fEdgeUniform = args.fUniformHandler->addUniformArray(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 198 | kVec3f_GrSLType, |
| 199 | kDefault_GrSLPrecision, |
| 200 | "edges", |
| 201 | cpe.getEdgeCount(), |
| 202 | &edgeArrayName); |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 203 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 204 | fragBuilder->codeAppend("\t\tfloat alpha = 1.0;\n"); |
| 205 | fragBuilder->codeAppend("\t\tfloat edge;\n"); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 206 | for (int i = 0; i < cpe.getEdgeCount(); ++i) { |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame^] | 207 | fragBuilder->codeAppendf("\t\tedge = dot(%s[%d], vec3(sk_FragCoord.x, sk_FragCoord.y, " |
| 208 | "1));\n", |
| 209 | edgeArrayName, i); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 210 | if (GrProcessorEdgeTypeIsAA(cpe.getEdgeType())) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 211 | fragBuilder->codeAppend("\t\tedge = clamp(edge, 0.0, 1.0);\n"); |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 212 | } else { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 213 | fragBuilder->codeAppend("\t\tedge = edge >= 0.5 ? 1.0 : 0.0;\n"); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 214 | } |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 215 | fragBuilder->codeAppend("\t\talpha *= edge;\n"); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 216 | } |
| 217 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 218 | if (GrProcessorEdgeTypeIsInverseFill(cpe.getEdgeType())) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 219 | fragBuilder->codeAppend("\talpha = 1.0 - alpha;\n"); |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 220 | } |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 221 | fragBuilder->codeAppendf("\t%s = %s;\n", args.fOutputColor, |
| 222 | (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha")).c_str()); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 223 | } |
| 224 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 225 | void GrGLConvexPolyEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
| 226 | const GrProcessor& effect) { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 227 | const GrConvexPolyEffect& cpe = effect.cast<GrConvexPolyEffect>(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 228 | size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar); |
| 229 | if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 230 | pdman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges()); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 231 | memcpy(fPrevEdges, cpe.getEdges(), byteSize); |
| 232 | } |
| 233 | } |
| 234 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 235 | void GrGLConvexPolyEffect::GenKey(const GrProcessor& processor, const GrShaderCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 236 | GrProcessorKeyBuilder* b) { |
| 237 | const GrConvexPolyEffect& cpe = processor.cast<GrConvexPolyEffect>(); |
| 238 | GR_STATIC_ASSERT(kGrProcessorEdgeTypeCnt <= 8); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 239 | uint32_t key = (cpe.getEdgeCount() << 3) | cpe.getEdgeType(); |
| 240 | b->add32(key); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | ////////////////////////////////////////////////////////////////////////////// |
| 244 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 245 | sk_sp<GrFragmentProcessor> GrConvexPolyEffect::Make(GrPrimitiveEdgeType type, const SkPath& path, |
| 246 | const SkVector* offset) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 247 | if (kHairlineAA_GrProcessorEdgeType == type) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 248 | return nullptr; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 249 | } |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 250 | if (path.getSegmentMasks() != SkPath::kLine_SegmentMask || |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 251 | !path.isConvex()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 252 | return nullptr; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 253 | } |
| 254 | |
reed | 026beb5 | 2015-06-10 14:23:15 -0700 | [diff] [blame] | 255 | SkPathPriv::FirstDirection dir; |
bsalomon | 7888de0 | 2016-03-28 15:04:45 -0700 | [diff] [blame] | 256 | // The only way this should fail is if the clip is effectively a infinitely thin line. In that |
| 257 | // case nothing is inside the clip. It'd be nice to detect this at a higher level and either |
| 258 | // skip the draw or omit the clip element. |
| 259 | if (!SkPathPriv::CheapComputeFirstDirection(path, &dir)) { |
| 260 | if (GrProcessorEdgeTypeIsInverseFill(type)) { |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 261 | return GrConstColorProcessor::Make(GrColor4f::OpaqueWhite(), |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 262 | GrConstColorProcessor::kModulateRGBA_InputMode); |
bsalomon | 7888de0 | 2016-03-28 15:04:45 -0700 | [diff] [blame] | 263 | } |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 264 | return GrConstColorProcessor::Make(GrColor4f::TransparentBlack(), |
| 265 | GrConstColorProcessor::kIgnore_InputMode); |
bsalomon | 7888de0 | 2016-03-28 15:04:45 -0700 | [diff] [blame] | 266 | } |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 267 | |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 268 | SkVector t; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 269 | if (nullptr == offset) { |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 270 | t.set(0, 0); |
| 271 | } else { |
| 272 | t = *offset; |
| 273 | } |
| 274 | |
lsalzman | d15947e | 2016-05-31 09:46:00 -0700 | [diff] [blame] | 275 | SkScalar edges[3 * kMaxEdges]; |
| 276 | SkPoint pts[4]; |
| 277 | SkPath::Verb verb; |
| 278 | SkPath::Iter iter(path, true); |
| 279 | |
| 280 | // SkPath considers itself convex so long as there is a convex contour within it, |
| 281 | // regardless of any degenerate contours such as a string of moveTos before it. |
| 282 | // Iterate here to consume any degenerate contours and only process the points |
| 283 | // on the actual convex contour. |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 284 | int n = 0; |
lsalzman | d15947e | 2016-05-31 09:46:00 -0700 | [diff] [blame] | 285 | while ((verb = iter.next(pts, true, true)) != SkPath::kDone_Verb) { |
| 286 | switch (verb) { |
| 287 | case SkPath::kMove_Verb: |
| 288 | SkASSERT(n == 0); |
| 289 | case SkPath::kClose_Verb: |
| 290 | break; |
| 291 | case SkPath::kLine_Verb: { |
| 292 | if (n >= kMaxEdges) { |
| 293 | return nullptr; |
| 294 | } |
| 295 | SkVector v = pts[1] - pts[0]; |
| 296 | v.normalize(); |
| 297 | if (SkPathPriv::kCCW_FirstDirection == dir) { |
| 298 | edges[3 * n] = v.fY; |
| 299 | edges[3 * n + 1] = -v.fX; |
| 300 | } else { |
| 301 | edges[3 * n] = -v.fY; |
| 302 | edges[3 * n + 1] = v.fX; |
| 303 | } |
| 304 | SkPoint p = pts[1] + t; |
| 305 | edges[3 * n + 2] = -(edges[3 * n] * p.fX + edges[3 * n + 1] * p.fY); |
| 306 | ++n; |
| 307 | break; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 308 | } |
lsalzman | d15947e | 2016-05-31 09:46:00 -0700 | [diff] [blame] | 309 | default: |
| 310 | return nullptr; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
lsalzman | d15947e | 2016-05-31 09:46:00 -0700 | [diff] [blame] | 313 | |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 314 | if (path.isInverseFillType()) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 315 | type = GrInvertProcessorEdgeType(type); |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 316 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 317 | return Make(type, n, edges); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 318 | } |
| 319 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 320 | sk_sp<GrFragmentProcessor> GrConvexPolyEffect::Make(GrPrimitiveEdgeType edgeType, |
| 321 | const SkRect& rect) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 322 | if (kHairlineAA_GrProcessorEdgeType == edgeType){ |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 323 | return nullptr; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 324 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 325 | return AARectEffect::Make(edgeType, rect); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 326 | } |
| 327 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 328 | GrConvexPolyEffect::~GrConvexPolyEffect() {} |
| 329 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 330 | void GrConvexPolyEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 331 | inout->mulByUnknownSingleComponent(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 334 | void GrConvexPolyEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 335 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 336 | GrGLConvexPolyEffect::GenKey(*this, caps, b); |
| 337 | } |
| 338 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 339 | GrGLSLFragmentProcessor* GrConvexPolyEffect::onCreateGLSLInstance() const { |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 340 | return new GrGLConvexPolyEffect; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 341 | } |
| 342 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 343 | GrConvexPolyEffect::GrConvexPolyEffect(GrPrimitiveEdgeType edgeType, int n, const SkScalar edges[]) |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 344 | : INHERITED(kModulatesInput_OptimizationFlag), fEdgeType(edgeType), fEdgeCount(n) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 345 | this->initClassID<GrConvexPolyEffect>(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 346 | // Factory function should have already ensured this. |
| 347 | SkASSERT(n <= kMaxEdges); |
| 348 | memcpy(fEdges, edges, 3 * n * sizeof(SkScalar)); |
| 349 | // Outset the edges by 0.5 so that a pixel with center on an edge is 50% covered in the AA case |
| 350 | // and 100% covered in the non-AA case. |
| 351 | for (int i = 0; i < n; ++i) { |
| 352 | fEdges[3 * i + 2] += SK_ScalarHalf; |
| 353 | } |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 354 | } |
| 355 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 356 | bool GrConvexPolyEffect::onIsEqual(const GrFragmentProcessor& other) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 357 | const GrConvexPolyEffect& cpe = other.cast<GrConvexPolyEffect>(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 358 | // ignore the fact that 0 == -0 and just use memcmp. |
| 359 | return (cpe.fEdgeType == fEdgeType && cpe.fEdgeCount == fEdgeCount && |
| 360 | 0 == memcmp(cpe.fEdges, fEdges, 3 * fEdgeCount * sizeof(SkScalar))); |
| 361 | } |
| 362 | |
| 363 | ////////////////////////////////////////////////////////////////////////////// |
| 364 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 365 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvexPolyEffect); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 366 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 367 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 368 | sk_sp<GrFragmentProcessor> GrConvexPolyEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 369 | int count = d->fRandom->nextULessThan(kMaxEdges) + 1; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 370 | SkScalar edges[kMaxEdges * 3]; |
| 371 | for (int i = 0; i < 3 * count; ++i) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 372 | edges[i] = d->fRandom->nextSScalar1(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 373 | } |
| 374 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 375 | sk_sp<GrFragmentProcessor> fp; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 376 | do { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 377 | GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>( |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 378 | d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt)); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 379 | fp = GrConvexPolyEffect::Make(edgeType, count, edges); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 380 | } while (nullptr == fp); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 381 | return fp; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 382 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 383 | #endif |