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