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" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 10 | #include "SkPath.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 11 | #include "gl/GrGLProcessor.h" |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 12 | #include "gl/GrGLSL.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 13 | #include "gl/builders/GrGLProgramBuilder.h" |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 14 | |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 15 | ////////////////////////////////////////////////////////////////////////////// |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 16 | class AARectEffect : public GrFragmentProcessor { |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 17 | public: |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 18 | const SkRect& getRect() const { return fRect; } |
| 19 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 20 | static GrFragmentProcessor* Create(GrPrimitiveEdgeType edgeType, const SkRect& rect) { |
bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 21 | return SkNEW_ARGS(AARectEffect, (edgeType, rect)); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 22 | } |
| 23 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 24 | GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; } |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 25 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 26 | const char* name() const override { return "AARect"; } |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 27 | |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 28 | void getGLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 29 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 30 | GrGLFragmentProcessor* createGLInstance() const override; |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 31 | |
| 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 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 39 | bool onIsEqual(const GrFragmentProcessor& other) const override { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 40 | const AARectEffect& aare = other.cast<AARectEffect>(); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 41 | return fRect == aare.fRect; |
| 42 | } |
| 43 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 44 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 45 | if (fRect.isEmpty()) { |
| 46 | // An empty rect will have no coverage anywhere. |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 47 | inout->mulByKnownSingleComponent(0); |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 48 | } else { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 49 | inout->mulByUnknownSingleComponent(); |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 50 | } |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 51 | } |
| 52 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 53 | SkRect fRect; |
| 54 | GrPrimitiveEdgeType fEdgeType; |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 55 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 56 | typedef GrFragmentProcessor INHERITED; |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 57 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 58 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 59 | |
| 60 | }; |
| 61 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 62 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(AARectEffect); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 63 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 64 | GrFragmentProcessor* AARectEffect::TestCreate(SkRandom* random, |
| 65 | GrContext*, |
| 66 | const GrDrawTargetCaps& caps, |
| 67 | GrTexture*[]) { |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 68 | SkRect rect = SkRect::MakeLTRB(random->nextSScalar1(), |
| 69 | random->nextSScalar1(), |
| 70 | random->nextSScalar1(), |
| 71 | random->nextSScalar1()); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 72 | GrFragmentProcessor* fp; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 73 | do { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 74 | GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan( |
| 75 | kGrProcessorEdgeTypeCnt)); |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 76 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 77 | fp = AARectEffect::Create(edgeType, rect); |
| 78 | } while (NULL == fp); |
| 79 | return fp; |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | ////////////////////////////////////////////////////////////////////////////// |
| 83 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 84 | class GLAARectEffect : public GrGLFragmentProcessor { |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 85 | public: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 86 | GLAARectEffect(const GrProcessor&); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 87 | |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 88 | virtual void emitCode(GrGLFPBuilder* builder, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 89 | const GrFragmentProcessor& fp, |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 90 | const char* outputColor, |
| 91 | const char* inputColor, |
| 92 | const TransformedCoordsArray&, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 93 | const TextureSamplerArray&) override; |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 94 | |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 95 | static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 96 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 97 | void setData(const GrGLProgramDataManager&, const GrProcessor&) override; |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 98 | |
| 99 | private: |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 100 | GrGLProgramDataManager::UniformHandle fRectUniform; |
| 101 | SkRect fPrevRect; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 102 | typedef GrGLFragmentProcessor INHERITED; |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 105 | GLAARectEffect::GLAARectEffect(const GrProcessor& effect) { |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 106 | fPrevRect.fLeft = SK_ScalarNaN; |
| 107 | } |
| 108 | |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 109 | void GLAARectEffect::emitCode(GrGLFPBuilder* builder, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 110 | const GrFragmentProcessor& fp, |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 111 | const char* outputColor, |
| 112 | const char* inputColor, |
| 113 | const TransformedCoordsArray&, |
| 114 | const TextureSamplerArray& samplers) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 115 | const AARectEffect& aare = fp.cast<AARectEffect>(); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 116 | const char *rectName; |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 117 | // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5), |
| 118 | // respectively. |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 119 | fRectUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 120 | kVec4f_GrSLType, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 121 | kDefault_GrSLPrecision, |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 122 | "rect", |
| 123 | &rectName); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 124 | |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 125 | GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 126 | const char* fragmentPos = fsBuilder->fragmentPosition(); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 127 | if (GrProcessorEdgeTypeIsAA(aare.getEdgeType())) { |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 128 | // The amount of coverage removed in x and y by the edges is computed as a pair of negative |
| 129 | // numbers, xSub and ySub. |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 130 | fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n"); |
| 131 | fsBuilder->codeAppendf("\t\txSub = min(%s.x - %s.x, 0.0);\n", fragmentPos, rectName); |
| 132 | fsBuilder->codeAppendf("\t\txSub += min(%s.z - %s.x, 0.0);\n", rectName, fragmentPos); |
| 133 | fsBuilder->codeAppendf("\t\tySub = min(%s.y - %s.y, 0.0);\n", fragmentPos, rectName); |
| 134 | fsBuilder->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] | 135 | // Now compute coverage in x and y and multiply them to get the fraction of the pixel |
| 136 | // covered. |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 137 | fsBuilder->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] | 138 | } else { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 139 | fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n"); |
robertphillips | 7f14c9b | 2015-01-30 14:44:22 -0800 | [diff] [blame] | 140 | fsBuilder->codeAppendf("\t\talpha *= (%s.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", fragmentPos, rectName); |
| 141 | fsBuilder->codeAppendf("\t\talpha *= (%s.z - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName, fragmentPos); |
| 142 | fsBuilder->codeAppendf("\t\talpha *= (%s.y - %s.y) > -0.5 ? 1.0 : 0.0;\n", fragmentPos, rectName); |
| 143 | fsBuilder->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] | 144 | } |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 145 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 146 | if (GrProcessorEdgeTypeIsInverseFill(aare.getEdgeType())) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 147 | fsBuilder->codeAppend("\t\talpha = 1.0 - alpha;\n"); |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 148 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 149 | fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor, |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 150 | (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str()); |
| 151 | } |
| 152 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 153 | void GLAARectEffect::setData(const GrGLProgramDataManager& pdman, const GrProcessor& processor) { |
| 154 | const AARectEffect& aare = processor.cast<AARectEffect>(); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 155 | const SkRect& rect = aare.getRect(); |
| 156 | if (rect != fPrevRect) { |
robertphillips | 7f14c9b | 2015-01-30 14:44:22 -0800 | [diff] [blame] | 157 | pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f, |
| 158 | rect.fRight - 0.5f, rect.fBottom - 0.5f); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 159 | fPrevRect = rect; |
| 160 | } |
| 161 | } |
| 162 | |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 163 | void GLAARectEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 164 | GrProcessorKeyBuilder* b) { |
| 165 | const AARectEffect& aare = processor.cast<AARectEffect>(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 166 | b->add32(aare.getEdgeType()); |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 167 | } |
| 168 | |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 169 | void AARectEffect::getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 170 | GLAARectEffect::GenKey(*this, caps, b); |
| 171 | } |
| 172 | |
| 173 | GrGLFragmentProcessor* AARectEffect::createGLInstance() const { |
| 174 | return SkNEW_ARGS(GLAARectEffect, (*this)); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | ////////////////////////////////////////////////////////////////////////////// |
| 178 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 179 | class GrGLConvexPolyEffect : public GrGLFragmentProcessor { |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 180 | public: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 181 | GrGLConvexPolyEffect(const GrProcessor&); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 182 | |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 183 | virtual void emitCode(GrGLFPBuilder* builder, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 184 | const GrFragmentProcessor& fp, |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 185 | const char* outputColor, |
| 186 | const char* inputColor, |
| 187 | const TransformedCoordsArray&, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 188 | const TextureSamplerArray&) override; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 189 | |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 190 | static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 191 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 192 | void setData(const GrGLProgramDataManager&, const GrProcessor&) override; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 193 | |
| 194 | private: |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 195 | GrGLProgramDataManager::UniformHandle fEdgeUniform; |
| 196 | SkScalar fPrevEdges[3 * GrConvexPolyEffect::kMaxEdges]; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 197 | typedef GrGLFragmentProcessor INHERITED; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 198 | }; |
| 199 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 200 | GrGLConvexPolyEffect::GrGLConvexPolyEffect(const GrProcessor&) { |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 201 | fPrevEdges[0] = SK_ScalarNaN; |
| 202 | } |
| 203 | |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 204 | void GrGLConvexPolyEffect::emitCode(GrGLFPBuilder* builder, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 205 | const GrFragmentProcessor& fp, |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 206 | const char* outputColor, |
| 207 | const char* inputColor, |
| 208 | const TransformedCoordsArray&, |
| 209 | const TextureSamplerArray& samplers) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 210 | const GrConvexPolyEffect& cpe = fp.cast<GrConvexPolyEffect>(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 211 | |
| 212 | const char *edgeArrayName; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 213 | fEdgeUniform = builder->addUniformArray(GrGLProgramBuilder::kFragment_Visibility, |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 214 | kVec3f_GrSLType, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 215 | kDefault_GrSLPrecision, |
| 216 | "edges", |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 217 | cpe.getEdgeCount(), |
| 218 | &edgeArrayName); |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 219 | GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 220 | fsBuilder->codeAppend("\t\tfloat alpha = 1.0;\n"); |
| 221 | fsBuilder->codeAppend("\t\tfloat edge;\n"); |
| 222 | const char* fragmentPos = fsBuilder->fragmentPosition(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 223 | for (int i = 0; i < cpe.getEdgeCount(); ++i) { |
robertphillips | 7f14c9b | 2015-01-30 14:44:22 -0800 | [diff] [blame] | 224 | fsBuilder->codeAppendf("\t\tedge = dot(%s[%d], vec3(%s.x, %s.y, 1));\n", |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 225 | edgeArrayName, i, fragmentPos, fragmentPos); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 226 | if (GrProcessorEdgeTypeIsAA(cpe.getEdgeType())) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 227 | fsBuilder->codeAppend("\t\tedge = clamp(edge, 0.0, 1.0);\n"); |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 228 | } else { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 229 | fsBuilder->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] | 230 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 231 | fsBuilder->codeAppend("\t\talpha *= edge;\n"); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 232 | } |
| 233 | |
commit-bot@chromium.org | 6dee875 | 2014-02-07 22:39:01 +0000 | [diff] [blame] | 234 | // Woe is me. See skbug.com/2149. |
| 235 | if (kTegra2_GrGLRenderer == builder->ctxInfo().renderer()) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 236 | fsBuilder->codeAppend("\t\tif (-1.0 == alpha) {\n\t\t\tdiscard;\n\t\t}\n"); |
commit-bot@chromium.org | 6dee875 | 2014-02-07 22:39:01 +0000 | [diff] [blame] | 237 | } |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 238 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 239 | if (GrProcessorEdgeTypeIsInverseFill(cpe.getEdgeType())) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 240 | fsBuilder->codeAppend("\talpha = 1.0 - alpha;\n"); |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 241 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 242 | fsBuilder->codeAppendf("\t%s = %s;\n", outputColor, |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 243 | (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str()); |
| 244 | } |
| 245 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 246 | void GrGLConvexPolyEffect::setData(const GrGLProgramDataManager& pdman, const GrProcessor& effect) { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 247 | const GrConvexPolyEffect& cpe = effect.cast<GrConvexPolyEffect>(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 248 | size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar); |
| 249 | if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 250 | pdman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges()); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 251 | memcpy(fPrevEdges, cpe.getEdges(), byteSize); |
| 252 | } |
| 253 | } |
| 254 | |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 255 | void GrGLConvexPolyEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 256 | GrProcessorKeyBuilder* b) { |
| 257 | const GrConvexPolyEffect& cpe = processor.cast<GrConvexPolyEffect>(); |
| 258 | GR_STATIC_ASSERT(kGrProcessorEdgeTypeCnt <= 8); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 259 | uint32_t key = (cpe.getEdgeCount() << 3) | cpe.getEdgeType(); |
| 260 | b->add32(key); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | ////////////////////////////////////////////////////////////////////////////// |
| 264 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 265 | GrFragmentProcessor* GrConvexPolyEffect::Create(GrPrimitiveEdgeType type, const SkPath& path, |
| 266 | const SkVector* offset) { |
| 267 | if (kHairlineAA_GrProcessorEdgeType == type) { |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 268 | return NULL; |
| 269 | } |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 270 | if (path.getSegmentMasks() != SkPath::kLine_SegmentMask || |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 271 | !path.isConvex()) { |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 272 | return NULL; |
| 273 | } |
| 274 | |
| 275 | if (path.countPoints() > kMaxEdges) { |
| 276 | return NULL; |
| 277 | } |
| 278 | |
| 279 | SkPoint pts[kMaxEdges]; |
| 280 | SkScalar edges[3 * kMaxEdges]; |
| 281 | |
| 282 | SkPath::Direction dir; |
| 283 | SkAssertResult(path.cheapComputeDirection(&dir)); |
| 284 | |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 285 | SkVector t; |
| 286 | if (NULL == offset) { |
| 287 | t.set(0, 0); |
| 288 | } else { |
| 289 | t = *offset; |
| 290 | } |
| 291 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 292 | int count = path.getPoints(pts, kMaxEdges); |
| 293 | int n = 0; |
| 294 | for (int lastPt = count - 1, i = 0; i < count; lastPt = i++) { |
| 295 | if (pts[lastPt] != pts[i]) { |
| 296 | SkVector v = pts[i] - pts[lastPt]; |
| 297 | v.normalize(); |
| 298 | if (SkPath::kCCW_Direction == dir) { |
| 299 | edges[3 * n] = v.fY; |
| 300 | edges[3 * n + 1] = -v.fX; |
| 301 | } else { |
| 302 | edges[3 * n] = -v.fY; |
| 303 | edges[3 * n + 1] = v.fX; |
| 304 | } |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 305 | SkPoint p = pts[i] + t; |
| 306 | 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] | 307 | ++n; |
| 308 | } |
| 309 | } |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 310 | if (path.isInverseFillType()) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 311 | type = GrInvertProcessorEdgeType(type); |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 312 | } |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 313 | return Create(type, n, edges); |
| 314 | } |
| 315 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 316 | GrFragmentProcessor* GrConvexPolyEffect::Create(GrPrimitiveEdgeType edgeType, const SkRect& rect) { |
| 317 | if (kHairlineAA_GrProcessorEdgeType == edgeType){ |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 318 | return NULL; |
| 319 | } |
commit-bot@chromium.org | d85f32c | 2014-02-28 14:43:26 +0000 | [diff] [blame] | 320 | return AARectEffect::Create(edgeType, rect); |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 321 | } |
| 322 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 323 | GrConvexPolyEffect::~GrConvexPolyEffect() {} |
| 324 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 325 | void GrConvexPolyEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 326 | inout->mulByUnknownSingleComponent(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 327 | } |
| 328 | |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 329 | void GrConvexPolyEffect::getGLProcessorKey(const GrGLSLCaps& caps, |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 330 | GrProcessorKeyBuilder* b) const { |
| 331 | GrGLConvexPolyEffect::GenKey(*this, caps, b); |
| 332 | } |
| 333 | |
| 334 | GrGLFragmentProcessor* GrConvexPolyEffect::createGLInstance() const { |
| 335 | return SkNEW_ARGS(GrGLConvexPolyEffect, (*this)); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 336 | } |
| 337 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 338 | GrConvexPolyEffect::GrConvexPolyEffect(GrPrimitiveEdgeType edgeType, int n, const SkScalar edges[]) |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 339 | : fEdgeType(edgeType) |
| 340 | , fEdgeCount(n) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 341 | this->initClassID<GrConvexPolyEffect>(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 342 | // Factory function should have already ensured this. |
| 343 | SkASSERT(n <= kMaxEdges); |
| 344 | memcpy(fEdges, edges, 3 * n * sizeof(SkScalar)); |
| 345 | // Outset the edges by 0.5 so that a pixel with center on an edge is 50% covered in the AA case |
| 346 | // and 100% covered in the non-AA case. |
| 347 | for (int i = 0; i < n; ++i) { |
| 348 | fEdges[3 * i + 2] += SK_ScalarHalf; |
| 349 | } |
| 350 | this->setWillReadFragmentPosition(); |
| 351 | } |
| 352 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 353 | bool GrConvexPolyEffect::onIsEqual(const GrFragmentProcessor& other) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 354 | const GrConvexPolyEffect& cpe = other.cast<GrConvexPolyEffect>(); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 355 | // ignore the fact that 0 == -0 and just use memcmp. |
| 356 | return (cpe.fEdgeType == fEdgeType && cpe.fEdgeCount == fEdgeCount && |
| 357 | 0 == memcmp(cpe.fEdges, fEdges, 3 * fEdgeCount * sizeof(SkScalar))); |
| 358 | } |
| 359 | |
| 360 | ////////////////////////////////////////////////////////////////////////////// |
| 361 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 362 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvexPolyEffect); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 363 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 364 | GrFragmentProcessor* GrConvexPolyEffect::TestCreate(SkRandom* random, |
| 365 | GrContext*, |
| 366 | const GrDrawTargetCaps& caps, |
| 367 | GrTexture*[]) { |
commit-bot@chromium.org | 65ee5f4 | 2014-02-04 17:49:48 +0000 | [diff] [blame] | 368 | int count = random->nextULessThan(kMaxEdges) + 1; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 369 | SkScalar edges[kMaxEdges * 3]; |
| 370 | for (int i = 0; i < 3 * count; ++i) { |
| 371 | edges[i] = random->nextSScalar1(); |
| 372 | } |
| 373 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 374 | GrFragmentProcessor* fp; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 375 | do { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 376 | GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>( |
| 377 | random->nextULessThan(kGrProcessorEdgeTypeCnt)); |
| 378 | fp = GrConvexPolyEffect::Create(edgeType, count, edges); |
| 379 | } while (NULL == fp); |
| 380 | return fp; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 381 | } |