commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +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 | */ |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 7 | |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 8 | |
| 9 | #include "SkTwoPointConicalGradient.h" |
| 10 | |
commit-bot@chromium.org | ef93d29 | 2014-04-10 15:37:52 +0000 | [diff] [blame] | 11 | #if SK_SUPPORT_GPU |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 12 | #include "GrCoordTransform.h" |
joshualitt | 8ca93e7 | 2015-07-08 06:51:43 -0700 | [diff] [blame] | 13 | #include "GrPaint.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 14 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 15 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 16 | #include "glsl/GrGLSLUniformHandler.h" |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 17 | #include "SkTwoPointConicalGradient_gpu.h" |
| 18 | |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 19 | // For brevity |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 20 | typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 21 | |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 22 | static const SkScalar kErrorTol = 0.00001f; |
egdaniel | 8405ef9 | 2014-06-09 11:57:28 -0700 | [diff] [blame] | 23 | static const SkScalar kEdgeErrorTol = 5.f * kErrorTol; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * We have three general cases for 2pt conical gradients. First we always assume that |
| 27 | * the start radius <= end radius. Our first case (kInside_) is when the start circle |
| 28 | * is completely enclosed by the end circle. The second case (kOutside_) is the case |
| 29 | * when the start circle is either completely outside the end circle or the circles |
| 30 | * overlap. The final case (kEdge_) is when the start circle is inside the end one, |
| 31 | * but the two are just barely touching at 1 point along their edges. |
| 32 | */ |
| 33 | enum ConicalType { |
| 34 | kInside_ConicalType, |
| 35 | kOutside_ConicalType, |
| 36 | kEdge_ConicalType, |
| 37 | }; |
| 38 | |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 39 | ////////////////////////////////////////////////////////////////////////////// |
| 40 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 41 | static void set_matrix_edge_conical(const SkTwoPointConicalGradient& shader, |
| 42 | SkMatrix* invLMatrix) { |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 43 | // Inverse of the current local matrix is passed in then, |
| 44 | // translate to center1, rotate so center2 is on x axis. |
| 45 | const SkPoint& center1 = shader.getStartCenter(); |
| 46 | const SkPoint& center2 = shader.getEndCenter(); |
| 47 | |
| 48 | invLMatrix->postTranslate(-center1.fX, -center1.fY); |
| 49 | |
| 50 | SkPoint diff = center2 - center1; |
| 51 | SkScalar diffLen = diff.length(); |
| 52 | if (0 != diffLen) { |
| 53 | SkScalar invDiffLen = SkScalarInvert(diffLen); |
| 54 | SkMatrix rot; |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 55 | rot.setSinCos(-invDiffLen * diff.fY, invDiffLen * diff.fX); |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 56 | invLMatrix->postConcat(rot); |
| 57 | } |
| 58 | } |
| 59 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 60 | class Edge2PtConicalEffect : public GrGradientEffect { |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 61 | public: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 62 | class GLSLEdge2PtConicalProcessor; |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 63 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 64 | static std::unique_ptr<GrFragmentProcessor> Make(const CreateArgs& args) { |
| 65 | auto processor = std::unique_ptr<Edge2PtConicalEffect>(new Edge2PtConicalEffect(args)); |
Brian Salomon | 6af2701 | 2017-06-09 08:21:42 -0400 | [diff] [blame] | 66 | return processor->isValid() ? std::move(processor) : nullptr; |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 67 | } |
| 68 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 69 | const char* name() const override { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 70 | return "Two-Point Conical Gradient Edge Touching"; |
| 71 | } |
| 72 | |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 73 | // The radial gradient parameters can collapse to a linear (instead of quadratic) equation. |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 74 | SkScalar center() const { return fCenterX1; } |
| 75 | SkScalar diffRadius() const { return fDiffRadius; } |
| 76 | SkScalar radius() const { return fRadius0; } |
| 77 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 78 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 79 | return std::unique_ptr<GrFragmentProcessor>(new Edge2PtConicalEffect(*this)); |
Brian Salomon | f8480b9 | 2017-07-27 15:45:59 -0400 | [diff] [blame] | 80 | } |
| 81 | |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 82 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 83 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 84 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 85 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 86 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 87 | bool onIsEqual(const GrFragmentProcessor& sBase) const override { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 88 | const Edge2PtConicalEffect& s = sBase.cast<Edge2PtConicalEffect>(); |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 89 | return (INHERITED::onIsEqual(sBase) && |
| 90 | this->fCenterX1 == s.fCenterX1 && |
| 91 | this->fRadius0 == s.fRadius0 && |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 92 | this->fDiffRadius == s.fDiffRadius); |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Brian Salomon | f8480b9 | 2017-07-27 15:45:59 -0400 | [diff] [blame] | 95 | explicit Edge2PtConicalEffect(const CreateArgs& args) |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 96 | : INHERITED(args, false /* opaque: draws transparent black outside of the cone. */) { |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 97 | const SkTwoPointConicalGradient& shader = |
| 98 | *static_cast<const SkTwoPointConicalGradient*>(args.fShader); |
| 99 | fCenterX1 = shader.getCenterX1(); |
| 100 | fRadius0 = shader.getStartRadius(); |
| 101 | fDiffRadius = shader.getDiffRadius(); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 102 | this->initClassID<Edge2PtConicalEffect>(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 103 | // We should only be calling this shader if we are degenerate case with touching circles |
egdaniel | 8405ef9 | 2014-06-09 11:57:28 -0700 | [diff] [blame] | 104 | // When deciding if we are in edge case, we scaled by the end radius for cases when the |
joshualitt | 0125847 | 2014-09-22 10:29:30 -0700 | [diff] [blame] | 105 | // start radius was close to zero, otherwise we scaled by the start radius. In addition |
| 106 | // Our test for the edge case in set_matrix_circle_conical has a higher tolerance so we |
| 107 | // need the sqrt value below |
| 108 | SkASSERT(SkScalarAbs(SkScalarAbs(fDiffRadius) - fCenterX1) < |
| 109 | (fRadius0 < kErrorTol ? shader.getEndRadius() * kEdgeErrorTol : |
| 110 | fRadius0 * sqrt(kEdgeErrorTol))); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 111 | |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 112 | // We pass the linear part of the quadratic as a varying. |
| 113 | // float b = -2.0 * (fCenterX1 * x + fRadius0 * fDiffRadius * z) |
| 114 | fBTransform = this->getCoordTransform(); |
| 115 | SkMatrix& bMatrix = *fBTransform.accessMatrix(); |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 116 | SkScalar r0dr = fRadius0 * fDiffRadius; |
| 117 | bMatrix[SkMatrix::kMScaleX] = -2 * (fCenterX1 * bMatrix[SkMatrix::kMScaleX] + |
| 118 | r0dr * bMatrix[SkMatrix::kMPersp0]); |
| 119 | bMatrix[SkMatrix::kMSkewX] = -2 * (fCenterX1 * bMatrix[SkMatrix::kMSkewX] + |
| 120 | r0dr * bMatrix[SkMatrix::kMPersp1]); |
| 121 | bMatrix[SkMatrix::kMTransX] = -2 * (fCenterX1 * bMatrix[SkMatrix::kMTransX] + |
| 122 | r0dr * bMatrix[SkMatrix::kMPersp2]); |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 123 | this->addCoordTransform(&fBTransform); |
| 124 | } |
| 125 | |
Brian Salomon | f8480b9 | 2017-07-27 15:45:59 -0400 | [diff] [blame] | 126 | explicit Edge2PtConicalEffect(const Edge2PtConicalEffect& that) |
| 127 | : INHERITED(that) |
| 128 | , fBTransform(that.fBTransform) |
| 129 | , fCenterX1(that.fCenterX1) |
| 130 | , fRadius0(that.fRadius0) |
| 131 | , fDiffRadius(that.fDiffRadius) { |
| 132 | this->initClassID<Edge2PtConicalEffect>(); |
| 133 | this->addCoordTransform(&fBTransform); |
| 134 | } |
| 135 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 136 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 137 | |
| 138 | // @{ |
| 139 | // Cache of values - these can change arbitrarily, EXCEPT |
| 140 | // we shouldn't change between degenerate and non-degenerate?! |
| 141 | |
| 142 | GrCoordTransform fBTransform; |
| 143 | SkScalar fCenterX1; |
| 144 | SkScalar fRadius0; |
| 145 | SkScalar fDiffRadius; |
| 146 | |
| 147 | // @} |
| 148 | |
| 149 | typedef GrGradientEffect INHERITED; |
| 150 | }; |
| 151 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 152 | class Edge2PtConicalEffect::GLSLEdge2PtConicalProcessor : public GrGradientEffect::GLSLProcessor { |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 153 | public: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 154 | GLSLEdge2PtConicalProcessor(const GrProcessor&); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 155 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 156 | virtual void emitCode(EmitArgs&) override; |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 157 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 158 | static void GenKey(const GrProcessor&, const GrShaderCaps& caps, GrProcessorKeyBuilder* b); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 159 | |
| 160 | protected: |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 161 | void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 162 | |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 163 | UniformHandle fParamUni; |
| 164 | |
| 165 | const char* fVSVaryingName; |
| 166 | const char* fFSVaryingName; |
| 167 | |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 168 | // @{ |
| 169 | /// Values last uploaded as uniforms |
| 170 | |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 171 | SkScalar fCachedRadius; |
| 172 | SkScalar fCachedDiffRadius; |
| 173 | |
| 174 | // @} |
| 175 | |
| 176 | private: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 177 | typedef GrGradientEffect::GLSLProcessor INHERITED; |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 178 | |
| 179 | }; |
| 180 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 181 | void Edge2PtConicalEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 182 | GrProcessorKeyBuilder* b) const { |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 183 | Edge2PtConicalEffect::GLSLEdge2PtConicalProcessor::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 184 | } |
| 185 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 186 | GrGLSLFragmentProcessor* Edge2PtConicalEffect::onCreateGLSLInstance() const { |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 187 | return new Edge2PtConicalEffect::GLSLEdge2PtConicalProcessor(*this); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 188 | } |
skia.committer@gmail.com | 221b911 | 2014-04-04 03:04:32 +0000 | [diff] [blame] | 189 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 190 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(Edge2PtConicalEffect); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 191 | |
joshualitt | 0125847 | 2014-09-22 10:29:30 -0700 | [diff] [blame] | 192 | /* |
| 193 | * All Two point conical gradient test create functions may occasionally create edge case shaders |
| 194 | */ |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 195 | #if GR_TEST_UTILS |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 196 | std::unique_ptr<GrFragmentProcessor> Edge2PtConicalEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 197 | SkPoint center1 = {d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()}; |
| 198 | SkScalar radius1 = d->fRandom->nextUScalar1(); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 199 | SkPoint center2; |
| 200 | SkScalar radius2; |
| 201 | do { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 202 | center2.set(d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 203 | // If the circles are identical the factory will give us an empty shader. |
| 204 | // This will happen if we pick identical centers |
| 205 | } while (center1 == center2); |
| 206 | |
| 207 | // Below makes sure that circle one is contained within circle two |
| 208 | // and both circles are touching on an edge |
| 209 | SkPoint diff = center2 - center1; |
| 210 | SkScalar diffLen = diff.length(); |
| 211 | radius2 = radius1 + diffLen; |
| 212 | |
Brian Osman | 3f74860 | 2016-10-03 18:29:03 -0400 | [diff] [blame] | 213 | RandomGradientParams params(d->fRandom); |
Brian Osman | a219653 | 2016-10-17 12:48:13 -0400 | [diff] [blame] | 214 | auto shader = params.fUseColors4f ? |
| 215 | SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2, |
| 216 | params.fColors4f, params.fColorSpace, params.fStops, |
| 217 | params.fColorCount, params.fTileMode) : |
| 218 | SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2, |
| 219 | params.fColors, params.fStops, |
| 220 | params.fColorCount, params.fTileMode); |
Brian Osman | 9f532a3 | 2016-10-19 11:12:09 -0400 | [diff] [blame] | 221 | GrTest::TestAsFPArgs asFPArgs(d); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 222 | std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args()); |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 223 | GrAlwaysAssert(fp); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 224 | return fp; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 225 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 226 | #endif |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 227 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 228 | Edge2PtConicalEffect::GLSLEdge2PtConicalProcessor::GLSLEdge2PtConicalProcessor(const GrProcessor&) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 229 | : fVSVaryingName(nullptr) |
| 230 | , fFSVaryingName(nullptr) |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 231 | , fCachedRadius(-SK_ScalarMax) |
| 232 | , fCachedDiffRadius(-SK_ScalarMax) {} |
| 233 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 234 | void Edge2PtConicalEffect::GLSLEdge2PtConicalProcessor::emitCode(EmitArgs& args) { |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 235 | const Edge2PtConicalEffect& ge = args.fFp.cast<Edge2PtConicalEffect>(); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 236 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 237 | this->emitUniforms(uniformHandler, ge); |
jvanverth | de11ee4 | 2016-02-26 13:58:40 -0800 | [diff] [blame] | 238 | fParamUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
| 239 | kVec3f_GrSLType, kDefault_GrSLPrecision, |
| 240 | "Conical2FSParams"); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 241 | |
| 242 | SkString cName("c"); |
| 243 | SkString tName("t"); |
| 244 | SkString p0; // start radius |
| 245 | SkString p1; // start radius squared |
| 246 | SkString p2; // difference in radii (r1 - r0) |
| 247 | |
jvanverth | de11ee4 | 2016-02-26 13:58:40 -0800 | [diff] [blame] | 248 | |
| 249 | p0.appendf("%s.x", uniformHandler->getUniformVariable(fParamUni).getName().c_str()); |
| 250 | p1.appendf("%s.y", uniformHandler->getUniformVariable(fParamUni).getName().c_str()); |
| 251 | p2.appendf("%s.z", uniformHandler->getUniformVariable(fParamUni).getName().c_str()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 252 | |
| 253 | // We interpolate the linear component in coords[1]. |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 254 | SkASSERT(args.fTransformedCoords[0].getType() == args.fTransformedCoords[1].getType()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 255 | const char* coords2D; |
| 256 | SkString bVar; |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 257 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 258 | if (kVec3f_GrSLType == args.fTransformedCoords[0].getType()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 259 | fragBuilder->codeAppendf("\tfloat3 interpolants = float3(%s.xy / %s.z, %s.x / %s.z);\n", |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 260 | args.fTransformedCoords[0].c_str(), |
| 261 | args.fTransformedCoords[0].c_str(), |
| 262 | args.fTransformedCoords[1].c_str(), |
| 263 | args.fTransformedCoords[1].c_str()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 264 | coords2D = "interpolants.xy"; |
| 265 | bVar = "interpolants.z"; |
| 266 | } else { |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 267 | coords2D = args.fTransformedCoords[0].c_str(); |
| 268 | bVar.printf("%s.x", args.fTransformedCoords[1].c_str()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | // output will default to transparent black (we simply won't write anything |
| 272 | // else to it if invalid, instead of discarding or returning prematurely) |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 273 | fragBuilder->codeAppendf("\t%s = float4(0.0,0.0,0.0,0.0);\n", args.fOutputColor); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 274 | |
| 275 | // c = (x^2)+(y^2) - params[1] |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 276 | fragBuilder->codeAppendf("\tfloat %s = dot(%s, %s) - %s;\n", |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 277 | cName.c_str(), coords2D, coords2D, p1.c_str()); |
| 278 | |
| 279 | // linear case: t = -c/b |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 280 | fragBuilder->codeAppendf("\tfloat %s = -(%s / %s);\n", tName.c_str(), |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 281 | cName.c_str(), bVar.c_str()); |
| 282 | |
| 283 | // if r(t) > 0, then t will be the x coordinate |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 284 | fragBuilder->codeAppendf("\tif (%s * %s + %s > 0.0) {\n", tName.c_str(), |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 285 | p2.c_str(), p0.c_str()); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 286 | fragBuilder->codeAppend("\t"); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 287 | this->emitColor(fragBuilder, |
| 288 | uniformHandler, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 289 | args.fShaderCaps, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 290 | ge, |
| 291 | tName.c_str(), |
| 292 | args.fOutputColor, |
| 293 | args.fInputColor, |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 294 | args.fTexSamplers); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 295 | fragBuilder->codeAppend("\t}\n"); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 296 | } |
| 297 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 298 | void Edge2PtConicalEffect::GLSLEdge2PtConicalProcessor::onSetData( |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 299 | const GrGLSLProgramDataManager& pdman, const GrFragmentProcessor& processor) { |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 300 | INHERITED::onSetData(pdman, processor); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 301 | const Edge2PtConicalEffect& data = processor.cast<Edge2PtConicalEffect>(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 302 | SkScalar radius0 = data.radius(); |
| 303 | SkScalar diffRadius = data.diffRadius(); |
| 304 | |
| 305 | if (fCachedRadius != radius0 || |
| 306 | fCachedDiffRadius != diffRadius) { |
| 307 | |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 308 | pdman.set3f(fParamUni, radius0, radius0 * radius0, diffRadius); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 309 | fCachedRadius = radius0; |
| 310 | fCachedDiffRadius = diffRadius; |
| 311 | } |
| 312 | } |
| 313 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 314 | void Edge2PtConicalEffect::GLSLEdge2PtConicalProcessor::GenKey(const GrProcessor& processor, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 315 | const GrShaderCaps&, GrProcessorKeyBuilder* b) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 316 | b->add32(GenBaseGradientKey(processor)); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | ////////////////////////////////////////////////////////////////////////////// |
| 320 | // Focal Conical Gradients |
| 321 | ////////////////////////////////////////////////////////////////////////////// |
| 322 | |
| 323 | static ConicalType set_matrix_focal_conical(const SkTwoPointConicalGradient& shader, |
| 324 | SkMatrix* invLMatrix, SkScalar* focalX) { |
| 325 | // Inverse of the current local matrix is passed in then, |
| 326 | // translate, scale, and rotate such that endCircle is unit circle on x-axis, |
| 327 | // and focal point is at the origin. |
| 328 | ConicalType conicalType; |
| 329 | const SkPoint& focal = shader.getStartCenter(); |
| 330 | const SkPoint& centerEnd = shader.getEndCenter(); |
| 331 | SkScalar radius = shader.getEndRadius(); |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 332 | SkScalar invRadius = 1.f / radius; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 333 | |
| 334 | SkMatrix matrix; |
| 335 | |
| 336 | matrix.setTranslate(-centerEnd.fX, -centerEnd.fY); |
| 337 | matrix.postScale(invRadius, invRadius); |
| 338 | |
| 339 | SkPoint focalTrans; |
| 340 | matrix.mapPoints(&focalTrans, &focal, 1); |
| 341 | *focalX = focalTrans.length(); |
| 342 | |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 343 | if (0.f != *focalX) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 344 | SkScalar invFocalX = SkScalarInvert(*focalX); |
| 345 | SkMatrix rot; |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 346 | rot.setSinCos(-invFocalX * focalTrans.fY, invFocalX * focalTrans.fX); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 347 | matrix.postConcat(rot); |
| 348 | } |
| 349 | |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 350 | matrix.postTranslate(-(*focalX), 0.f); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 351 | |
| 352 | // If the focal point is touching the edge of the circle it will |
| 353 | // cause a degenerate case that must be handled separately |
egdaniel | 8405ef9 | 2014-06-09 11:57:28 -0700 | [diff] [blame] | 354 | // kEdgeErrorTol = 5 * kErrorTol was picked after manual testing the |
| 355 | // stability trade off versus the linear approx used in the Edge Shader |
| 356 | if (SkScalarAbs(1.f - (*focalX)) < kEdgeErrorTol) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 357 | return kEdge_ConicalType; |
| 358 | } |
| 359 | |
| 360 | // Scale factor 1 / (1 - focalX * focalX) |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 361 | SkScalar oneMinusF2 = 1.f - *focalX * *focalX; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 362 | SkScalar s = SkScalarInvert(oneMinusF2); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 363 | |
| 364 | |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 365 | if (s >= 0.f) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 366 | conicalType = kInside_ConicalType; |
| 367 | matrix.postScale(s, s * SkScalarSqrt(oneMinusF2)); |
| 368 | } else { |
| 369 | conicalType = kOutside_ConicalType; |
| 370 | matrix.postScale(s, s); |
| 371 | } |
| 372 | |
| 373 | invLMatrix->postConcat(matrix); |
| 374 | |
| 375 | return conicalType; |
| 376 | } |
| 377 | |
| 378 | ////////////////////////////////////////////////////////////////////////////// |
| 379 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 380 | class FocalOutside2PtConicalEffect : public GrGradientEffect { |
| 381 | public: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 382 | class GLSLFocalOutside2PtConicalProcessor; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 383 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 384 | static std::unique_ptr<GrFragmentProcessor> Make(const CreateArgs& args, SkScalar focalX) { |
| 385 | auto processor = std::unique_ptr<FocalOutside2PtConicalEffect>( |
| 386 | new FocalOutside2PtConicalEffect(args, focalX)); |
Brian Salomon | 6af2701 | 2017-06-09 08:21:42 -0400 | [diff] [blame] | 387 | return processor->isValid() ? std::move(processor) : nullptr; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 388 | } |
| 389 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 390 | const char* name() const override { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 391 | return "Two-Point Conical Gradient Focal Outside"; |
| 392 | } |
| 393 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 394 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 395 | return std::unique_ptr<GrFragmentProcessor>(new FocalOutside2PtConicalEffect(*this)); |
Brian Salomon | f8480b9 | 2017-07-27 15:45:59 -0400 | [diff] [blame] | 396 | } |
| 397 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 398 | bool isFlipped() const { return fIsFlipped; } |
| 399 | SkScalar focal() const { return fFocalX; } |
| 400 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 401 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 402 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 403 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 404 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 405 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 406 | bool onIsEqual(const GrFragmentProcessor& sBase) const override { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 407 | const FocalOutside2PtConicalEffect& s = sBase.cast<FocalOutside2PtConicalEffect>(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 408 | return (INHERITED::onIsEqual(sBase) && |
| 409 | this->fFocalX == s.fFocalX && |
| 410 | this->fIsFlipped == s.fIsFlipped); |
| 411 | } |
| 412 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 413 | static bool IsFlipped(const CreateArgs& args) { |
| 414 | // eww. |
| 415 | return static_cast<const SkTwoPointConicalGradient*>(args.fShader)->isFlippedGrad(); |
| 416 | } |
| 417 | |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 418 | FocalOutside2PtConicalEffect(const CreateArgs& args, SkScalar focalX) |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 419 | : INHERITED(args, false /* opaque: draws transparent black outside of the cone. */) |
| 420 | , fFocalX(focalX) |
| 421 | , fIsFlipped(IsFlipped(args)) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 422 | this->initClassID<FocalOutside2PtConicalEffect>(); |
| 423 | } |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 424 | |
Brian Salomon | f8480b9 | 2017-07-27 15:45:59 -0400 | [diff] [blame] | 425 | explicit FocalOutside2PtConicalEffect(const FocalOutside2PtConicalEffect& that) |
| 426 | : INHERITED(that), fFocalX(that.fFocalX), fIsFlipped(that.fIsFlipped) { |
| 427 | this->initClassID<FocalOutside2PtConicalEffect>(); |
| 428 | } |
| 429 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 430 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 431 | |
| 432 | SkScalar fFocalX; |
| 433 | bool fIsFlipped; |
| 434 | |
| 435 | typedef GrGradientEffect INHERITED; |
| 436 | }; |
| 437 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 438 | class FocalOutside2PtConicalEffect::GLSLFocalOutside2PtConicalProcessor |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 439 | : public GrGradientEffect::GLSLProcessor { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 440 | public: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 441 | GLSLFocalOutside2PtConicalProcessor(const GrProcessor&); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 442 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 443 | virtual void emitCode(EmitArgs&) override; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 444 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 445 | static void GenKey(const GrProcessor&, const GrShaderCaps& caps, GrProcessorKeyBuilder* b); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 446 | |
| 447 | protected: |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 448 | void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 449 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 450 | UniformHandle fParamUni; |
| 451 | |
| 452 | const char* fVSVaryingName; |
| 453 | const char* fFSVaryingName; |
| 454 | |
| 455 | bool fIsFlipped; |
| 456 | |
| 457 | // @{ |
| 458 | /// Values last uploaded as uniforms |
| 459 | |
| 460 | SkScalar fCachedFocal; |
| 461 | |
| 462 | // @} |
| 463 | |
| 464 | private: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 465 | typedef GrGradientEffect::GLSLProcessor INHERITED; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 466 | |
| 467 | }; |
| 468 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 469 | void FocalOutside2PtConicalEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 470 | GrProcessorKeyBuilder* b) const { |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 471 | FocalOutside2PtConicalEffect::GLSLFocalOutside2PtConicalProcessor::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 472 | } |
| 473 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 474 | GrGLSLFragmentProcessor* FocalOutside2PtConicalEffect::onCreateGLSLInstance() const { |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 475 | return new FocalOutside2PtConicalEffect::GLSLFocalOutside2PtConicalProcessor(*this); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 476 | } |
| 477 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 478 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(FocalOutside2PtConicalEffect); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 479 | |
joshualitt | 0125847 | 2014-09-22 10:29:30 -0700 | [diff] [blame] | 480 | /* |
| 481 | * All Two point conical gradient test create functions may occasionally create edge case shaders |
| 482 | */ |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 483 | #if GR_TEST_UTILS |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 484 | std::unique_ptr<GrFragmentProcessor> FocalOutside2PtConicalEffect::TestCreate( |
| 485 | GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 486 | SkPoint center1 = {d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()}; |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 487 | SkScalar radius1 = 0.f; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 488 | SkPoint center2; |
| 489 | SkScalar radius2; |
| 490 | do { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 491 | center2.set(d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()); |
skia.committer@gmail.com | ede0c5c | 2014-04-23 03:04:11 +0000 | [diff] [blame] | 492 | // Need to make sure the centers are not the same or else focal point will be inside |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 493 | } while (center1 == center2); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 494 | |
Brian Osman | 3f74860 | 2016-10-03 18:29:03 -0400 | [diff] [blame] | 495 | SkPoint diff = center2 - center1; |
| 496 | SkScalar diffLen = diff.length(); |
| 497 | // Below makes sure that the focal point is not contained within circle two |
| 498 | radius2 = d->fRandom->nextRangeF(0.f, diffLen); |
| 499 | |
| 500 | RandomGradientParams params(d->fRandom); |
Brian Osman | a219653 | 2016-10-17 12:48:13 -0400 | [diff] [blame] | 501 | auto shader = params.fUseColors4f ? |
| 502 | SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2, |
| 503 | params.fColors4f, params.fColorSpace, params.fStops, |
| 504 | params.fColorCount, params.fTileMode) : |
| 505 | SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2, |
| 506 | params.fColors, params.fStops, |
| 507 | params.fColorCount, params.fTileMode); |
Brian Osman | 9f532a3 | 2016-10-19 11:12:09 -0400 | [diff] [blame] | 508 | GrTest::TestAsFPArgs asFPArgs(d); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 509 | std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args()); |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 510 | GrAlwaysAssert(fp); |
| 511 | return fp; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 512 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 513 | #endif |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 514 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 515 | FocalOutside2PtConicalEffect::GLSLFocalOutside2PtConicalProcessor |
| 516 | ::GLSLFocalOutside2PtConicalProcessor(const GrProcessor& processor) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 517 | : fVSVaryingName(nullptr) |
| 518 | , fFSVaryingName(nullptr) |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 519 | , fCachedFocal(SK_ScalarMax) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 520 | const FocalOutside2PtConicalEffect& data = processor.cast<FocalOutside2PtConicalEffect>(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 521 | fIsFlipped = data.isFlipped(); |
| 522 | } |
| 523 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 524 | void FocalOutside2PtConicalEffect::GLSLFocalOutside2PtConicalProcessor::emitCode(EmitArgs& args) { |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 525 | const FocalOutside2PtConicalEffect& ge = args.fFp.cast<FocalOutside2PtConicalEffect>(); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 526 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 527 | this->emitUniforms(uniformHandler, ge); |
jvanverth | de11ee4 | 2016-02-26 13:58:40 -0800 | [diff] [blame] | 528 | fParamUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
| 529 | kVec2f_GrSLType, kDefault_GrSLPrecision, |
| 530 | "Conical2FSParams"); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 531 | SkString tName("t"); |
| 532 | SkString p0; // focalX |
| 533 | SkString p1; // 1 - focalX * focalX |
| 534 | |
jvanverth | de11ee4 | 2016-02-26 13:58:40 -0800 | [diff] [blame] | 535 | p0.appendf("%s.x", uniformHandler->getUniformVariable(fParamUni).getName().c_str()); |
| 536 | p1.appendf("%s.y", uniformHandler->getUniformVariable(fParamUni).getName().c_str()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 537 | |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 538 | // if we have a float3 from being in perspective, convert it to a float2 first |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 539 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 540 | SkString coords2DString = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); |
skia.committer@gmail.com | ede0c5c | 2014-04-23 03:04:11 +0000 | [diff] [blame] | 541 | const char* coords2D = coords2DString.c_str(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 542 | |
| 543 | // t = p.x * focal.x +/- sqrt(p.x^2 + (1 - focal.x^2) * p.y^2) |
| 544 | |
| 545 | // output will default to transparent black (we simply won't write anything |
| 546 | // else to it if invalid, instead of discarding or returning prematurely) |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 547 | fragBuilder->codeAppendf("\t%s = float4(0.0,0.0,0.0,0.0);\n", args.fOutputColor); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 548 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 549 | fragBuilder->codeAppendf("\tfloat xs = %s.x * %s.x;\n", coords2D, coords2D); |
| 550 | fragBuilder->codeAppendf("\tfloat ys = %s.y * %s.y;\n", coords2D, coords2D); |
| 551 | fragBuilder->codeAppendf("\tfloat d = xs + %s * ys;\n", p1.c_str()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 552 | |
| 553 | // Must check to see if we flipped the circle order (to make sure start radius < end radius) |
| 554 | // If so we must also flip sign on sqrt |
| 555 | if (!fIsFlipped) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 556 | fragBuilder->codeAppendf("\tfloat %s = %s.x * %s + sqrt(d);\n", tName.c_str(), |
| 557 | coords2D, p0.c_str()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 558 | } else { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 559 | fragBuilder->codeAppendf("\tfloat %s = %s.x * %s - sqrt(d);\n", tName.c_str(), |
| 560 | coords2D, p0.c_str()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 561 | } |
| 562 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 563 | fragBuilder->codeAppendf("\tif (%s >= 0.0 && d >= 0.0) {\n", tName.c_str()); |
| 564 | fragBuilder->codeAppend("\t\t"); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 565 | this->emitColor(fragBuilder, |
| 566 | uniformHandler, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 567 | args.fShaderCaps, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 568 | ge, |
| 569 | tName.c_str(), |
| 570 | args.fOutputColor, |
| 571 | args.fInputColor, |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 572 | args.fTexSamplers); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 573 | fragBuilder->codeAppend("\t}\n"); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 574 | } |
| 575 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 576 | void FocalOutside2PtConicalEffect::GLSLFocalOutside2PtConicalProcessor::onSetData( |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 577 | const GrGLSLProgramDataManager& pdman, const GrFragmentProcessor& processor) { |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 578 | INHERITED::onSetData(pdman, processor); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 579 | const FocalOutside2PtConicalEffect& data = processor.cast<FocalOutside2PtConicalEffect>(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 580 | SkASSERT(data.isFlipped() == fIsFlipped); |
| 581 | SkScalar focal = data.focal(); |
| 582 | |
| 583 | if (fCachedFocal != focal) { |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 584 | SkScalar oneMinus2F = 1.f - focal * focal; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 585 | |
jvanverth | de11ee4 | 2016-02-26 13:58:40 -0800 | [diff] [blame] | 586 | pdman.set2f(fParamUni, SkScalarToFloat(focal), SkScalarToFloat(oneMinus2F)); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 587 | fCachedFocal = focal; |
| 588 | } |
| 589 | } |
| 590 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 591 | void FocalOutside2PtConicalEffect::GLSLFocalOutside2PtConicalProcessor::GenKey( |
| 592 | const GrProcessor& processor, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 593 | const GrShaderCaps&, GrProcessorKeyBuilder* b) { |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 594 | uint32_t* key = b->add32n(2); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 595 | key[0] = GenBaseGradientKey(processor); |
| 596 | key[1] = processor.cast<FocalOutside2PtConicalEffect>().isFlipped(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | ////////////////////////////////////////////////////////////////////////////// |
| 600 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 601 | class FocalInside2PtConicalEffect : public GrGradientEffect { |
| 602 | public: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 603 | class GLSLFocalInside2PtConicalProcessor; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 604 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 605 | static std::unique_ptr<GrFragmentProcessor> Make(const CreateArgs& args, SkScalar focalX) { |
| 606 | auto processor = std::unique_ptr<FocalInside2PtConicalEffect>( |
| 607 | new FocalInside2PtConicalEffect(args, focalX)); |
Brian Salomon | 6af2701 | 2017-06-09 08:21:42 -0400 | [diff] [blame] | 608 | return processor->isValid() ? std::move(processor) : nullptr; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 609 | } |
| 610 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 611 | const char* name() const override { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 612 | return "Two-Point Conical Gradient Focal Inside"; |
| 613 | } |
| 614 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 615 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 616 | return std::unique_ptr<GrFragmentProcessor>(new FocalInside2PtConicalEffect(*this)); |
Brian Salomon | f8480b9 | 2017-07-27 15:45:59 -0400 | [diff] [blame] | 617 | } |
| 618 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 619 | SkScalar focal() const { return fFocalX; } |
| 620 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 621 | typedef FocalInside2PtConicalEffect::GLSLFocalInside2PtConicalProcessor GLSLProcessor; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 622 | |
| 623 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 624 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 625 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 626 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 627 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 628 | bool onIsEqual(const GrFragmentProcessor& sBase) const override { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 629 | const FocalInside2PtConicalEffect& s = sBase.cast<FocalInside2PtConicalEffect>(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 630 | return (INHERITED::onIsEqual(sBase) && |
| 631 | this->fFocalX == s.fFocalX); |
| 632 | } |
| 633 | |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 634 | FocalInside2PtConicalEffect(const CreateArgs& args, SkScalar focalX) |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 635 | : INHERITED(args, args.fShader->colorsAreOpaque()), fFocalX(focalX) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 636 | this->initClassID<FocalInside2PtConicalEffect>(); |
| 637 | } |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 638 | |
Brian Salomon | f8480b9 | 2017-07-27 15:45:59 -0400 | [diff] [blame] | 639 | explicit FocalInside2PtConicalEffect(const FocalInside2PtConicalEffect& that) |
| 640 | : INHERITED(that), fFocalX(that.fFocalX) { |
| 641 | this->initClassID<FocalInside2PtConicalEffect>(); |
| 642 | } |
| 643 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 644 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 645 | |
| 646 | SkScalar fFocalX; |
| 647 | |
| 648 | typedef GrGradientEffect INHERITED; |
| 649 | }; |
| 650 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 651 | class FocalInside2PtConicalEffect::GLSLFocalInside2PtConicalProcessor |
| 652 | : public GrGradientEffect::GLSLProcessor { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 653 | public: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 654 | GLSLFocalInside2PtConicalProcessor(const GrProcessor&); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 655 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 656 | virtual void emitCode(EmitArgs&) override; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 657 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 658 | static void GenKey(const GrProcessor&, const GrShaderCaps& caps, GrProcessorKeyBuilder* b); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 659 | |
| 660 | protected: |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 661 | void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 662 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 663 | UniformHandle fFocalUni; |
| 664 | |
| 665 | const char* fVSVaryingName; |
| 666 | const char* fFSVaryingName; |
| 667 | |
| 668 | // @{ |
| 669 | /// Values last uploaded as uniforms |
| 670 | |
| 671 | SkScalar fCachedFocal; |
| 672 | |
| 673 | // @} |
| 674 | |
| 675 | private: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 676 | typedef GrGradientEffect::GLSLProcessor INHERITED; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 677 | |
| 678 | }; |
| 679 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 680 | void FocalInside2PtConicalEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 681 | GrProcessorKeyBuilder* b) const { |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 682 | FocalInside2PtConicalEffect::GLSLFocalInside2PtConicalProcessor::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 683 | } |
| 684 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 685 | GrGLSLFragmentProcessor* FocalInside2PtConicalEffect::onCreateGLSLInstance() const { |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 686 | return new FocalInside2PtConicalEffect::GLSLFocalInside2PtConicalProcessor(*this); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 687 | } |
| 688 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 689 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(FocalInside2PtConicalEffect); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 690 | |
joshualitt | 0125847 | 2014-09-22 10:29:30 -0700 | [diff] [blame] | 691 | /* |
| 692 | * All Two point conical gradient test create functions may occasionally create edge case shaders |
| 693 | */ |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 694 | #if GR_TEST_UTILS |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 695 | std::unique_ptr<GrFragmentProcessor> FocalInside2PtConicalEffect::TestCreate( |
| 696 | GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 697 | SkPoint center1 = {d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()}; |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 698 | SkScalar radius1 = 0.f; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 699 | SkPoint center2; |
| 700 | SkScalar radius2; |
| 701 | do { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 702 | center2.set(d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 703 | // Below makes sure radius2 is larger enouch such that the focal point |
| 704 | // is inside the end circle |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 705 | SkScalar increase = d->fRandom->nextUScalar1(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 706 | SkPoint diff = center2 - center1; |
| 707 | SkScalar diffLen = diff.length(); |
| 708 | radius2 = diffLen + increase; |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 709 | // If the circles are identical the factory will give us an empty shader. |
| 710 | } while (radius1 == radius2 && center1 == center2); |
| 711 | |
Brian Osman | 3f74860 | 2016-10-03 18:29:03 -0400 | [diff] [blame] | 712 | RandomGradientParams params(d->fRandom); |
Brian Osman | a219653 | 2016-10-17 12:48:13 -0400 | [diff] [blame] | 713 | auto shader = params.fUseColors4f ? |
| 714 | SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2, |
| 715 | params.fColors4f, params.fColorSpace, params.fStops, |
| 716 | params.fColorCount, params.fTileMode) : |
| 717 | SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2, |
| 718 | params.fColors, params.fStops, |
| 719 | params.fColorCount, params.fTileMode); |
Brian Osman | 9f532a3 | 2016-10-19 11:12:09 -0400 | [diff] [blame] | 720 | GrTest::TestAsFPArgs asFPArgs(d); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 721 | std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args()); |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 722 | GrAlwaysAssert(fp); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 723 | return fp; |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 724 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 725 | #endif |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 726 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 727 | FocalInside2PtConicalEffect::GLSLFocalInside2PtConicalProcessor |
| 728 | ::GLSLFocalInside2PtConicalProcessor(const GrProcessor&) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 729 | : fVSVaryingName(nullptr) |
| 730 | , fFSVaryingName(nullptr) |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 731 | , fCachedFocal(SK_ScalarMax) {} |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 732 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 733 | void FocalInside2PtConicalEffect::GLSLFocalInside2PtConicalProcessor::emitCode(EmitArgs& args) { |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 734 | const FocalInside2PtConicalEffect& ge = args.fFp.cast<FocalInside2PtConicalEffect>(); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 735 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 736 | this->emitUniforms(uniformHandler, ge); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 737 | fFocalUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 738 | kFloat_GrSLType, kDefault_GrSLPrecision, |
| 739 | "Conical2FSParams"); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 740 | SkString tName("t"); |
| 741 | |
| 742 | // this is the distance along x-axis from the end center to focal point in |
| 743 | // transformed coordinates |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 744 | GrShaderVar focal = uniformHandler->getUniformVariable(fFocalUni); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 745 | |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 746 | // if we have a float3 from being in perspective, convert it to a float2 first |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 747 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 748 | SkString coords2DString = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); |
skia.committer@gmail.com | ede0c5c | 2014-04-23 03:04:11 +0000 | [diff] [blame] | 749 | const char* coords2D = coords2DString.c_str(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 750 | |
| 751 | // t = p.x * focalX + length(p) |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 752 | fragBuilder->codeAppendf("\tfloat %s = %s.x * %s + length(%s);\n", tName.c_str(), |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 753 | coords2D, focal.c_str(), coords2D); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 754 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 755 | this->emitColor(fragBuilder, |
| 756 | uniformHandler, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 757 | args.fShaderCaps, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 758 | ge, |
| 759 | tName.c_str(), |
| 760 | args.fOutputColor, |
| 761 | args.fInputColor, |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 762 | args.fTexSamplers); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 763 | } |
| 764 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 765 | void FocalInside2PtConicalEffect::GLSLFocalInside2PtConicalProcessor::onSetData( |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 766 | const GrGLSLProgramDataManager& pdman, const GrFragmentProcessor& processor) { |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 767 | INHERITED::onSetData(pdman, processor); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 768 | const FocalInside2PtConicalEffect& data = processor.cast<FocalInside2PtConicalEffect>(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 769 | SkScalar focal = data.focal(); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 770 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 771 | if (fCachedFocal != focal) { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 772 | pdman.set1f(fFocalUni, SkScalarToFloat(focal)); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 773 | fCachedFocal = focal; |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 774 | } |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 775 | } |
| 776 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 777 | void FocalInside2PtConicalEffect::GLSLFocalInside2PtConicalProcessor::GenKey( |
| 778 | const GrProcessor& processor, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 779 | const GrShaderCaps&, GrProcessorKeyBuilder* b) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 780 | b->add32(GenBaseGradientKey(processor)); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | ////////////////////////////////////////////////////////////////////////////// |
| 784 | // Circle Conical Gradients |
| 785 | ////////////////////////////////////////////////////////////////////////////// |
| 786 | |
| 787 | struct CircleConicalInfo { |
| 788 | SkPoint fCenterEnd; |
| 789 | SkScalar fA; |
| 790 | SkScalar fB; |
| 791 | SkScalar fC; |
| 792 | }; |
| 793 | |
| 794 | // Returns focal distance along x-axis in transformed coords |
| 795 | static ConicalType set_matrix_circle_conical(const SkTwoPointConicalGradient& shader, |
| 796 | SkMatrix* invLMatrix, CircleConicalInfo* info) { |
| 797 | // Inverse of the current local matrix is passed in then, |
| 798 | // translate and scale such that start circle is on the origin and has radius 1 |
| 799 | const SkPoint& centerStart = shader.getStartCenter(); |
| 800 | const SkPoint& centerEnd = shader.getEndCenter(); |
| 801 | SkScalar radiusStart = shader.getStartRadius(); |
| 802 | SkScalar radiusEnd = shader.getEndRadius(); |
| 803 | |
| 804 | SkMatrix matrix; |
| 805 | |
| 806 | matrix.setTranslate(-centerStart.fX, -centerStart.fY); |
| 807 | |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 808 | SkScalar invStartRad = 1.f / radiusStart; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 809 | matrix.postScale(invStartRad, invStartRad); |
| 810 | |
| 811 | radiusEnd /= radiusStart; |
| 812 | |
| 813 | SkPoint centerEndTrans; |
| 814 | matrix.mapPoints(¢erEndTrans, ¢erEnd, 1); |
| 815 | |
| 816 | SkScalar A = centerEndTrans.fX * centerEndTrans.fX + centerEndTrans.fY * centerEndTrans.fY |
| 817 | - radiusEnd * radiusEnd + 2 * radiusEnd - 1; |
| 818 | |
| 819 | // Check to see if start circle is inside end circle with edges touching. |
| 820 | // If touching we return that it is of kEdge_ConicalType, and leave the matrix setting |
egdaniel | 8405ef9 | 2014-06-09 11:57:28 -0700 | [diff] [blame] | 821 | // to the edge shader. kEdgeErrorTol = 5 * kErrorTol was picked after manual testing |
| 822 | // so that C = 1 / A is stable, and the linear approximation used in the Edge shader is |
| 823 | // still accurate. |
| 824 | if (SkScalarAbs(A) < kEdgeErrorTol) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 825 | return kEdge_ConicalType; |
| 826 | } |
| 827 | |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 828 | SkScalar C = 1.f / A; |
| 829 | SkScalar B = (radiusEnd - 1.f) * C; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 830 | |
| 831 | matrix.postScale(C, C); |
| 832 | |
| 833 | invLMatrix->postConcat(matrix); |
| 834 | |
| 835 | info->fCenterEnd = centerEndTrans; |
| 836 | info->fA = A; |
| 837 | info->fB = B; |
| 838 | info->fC = C; |
| 839 | |
| 840 | // if A ends up being negative, the start circle is contained completely inside the end cirlce |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 841 | if (A < 0.f) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 842 | return kInside_ConicalType; |
| 843 | } |
| 844 | return kOutside_ConicalType; |
| 845 | } |
| 846 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 847 | class CircleInside2PtConicalEffect : public GrGradientEffect { |
| 848 | public: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 849 | class GLSLCircleInside2PtConicalProcessor; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 850 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 851 | static std::unique_ptr<GrFragmentProcessor> Make(const CreateArgs& args, |
| 852 | const CircleConicalInfo& info) { |
| 853 | auto processor = std::unique_ptr<CircleInside2PtConicalEffect>( |
| 854 | new CircleInside2PtConicalEffect(args, info)); |
Brian Salomon | 6af2701 | 2017-06-09 08:21:42 -0400 | [diff] [blame] | 855 | return processor->isValid() ? std::move(processor) : nullptr; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 856 | } |
| 857 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 858 | const char* name() const override { return "Two-Point Conical Gradient Inside"; } |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 859 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 860 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 861 | return std::unique_ptr<GrFragmentProcessor>(new CircleInside2PtConicalEffect(*this)); |
Brian Salomon | f8480b9 | 2017-07-27 15:45:59 -0400 | [diff] [blame] | 862 | } |
| 863 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 864 | SkScalar centerX() const { return fInfo.fCenterEnd.fX; } |
| 865 | SkScalar centerY() const { return fInfo.fCenterEnd.fY; } |
| 866 | SkScalar A() const { return fInfo.fA; } |
| 867 | SkScalar B() const { return fInfo.fB; } |
| 868 | SkScalar C() const { return fInfo.fC; } |
| 869 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 870 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 871 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 872 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 873 | virtual void onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 874 | GrProcessorKeyBuilder* b) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 875 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 876 | bool onIsEqual(const GrFragmentProcessor& sBase) const override { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 877 | const CircleInside2PtConicalEffect& s = sBase.cast<CircleInside2PtConicalEffect>(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 878 | return (INHERITED::onIsEqual(sBase) && |
| 879 | this->fInfo.fCenterEnd == s.fInfo.fCenterEnd && |
| 880 | this->fInfo.fA == s.fInfo.fA && |
| 881 | this->fInfo.fB == s.fInfo.fB && |
| 882 | this->fInfo.fC == s.fInfo.fC); |
| 883 | } |
| 884 | |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 885 | CircleInside2PtConicalEffect(const CreateArgs& args, const CircleConicalInfo& info) |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 886 | : INHERITED(args, args.fShader->colorsAreOpaque()), fInfo(info) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 887 | this->initClassID<CircleInside2PtConicalEffect>(); |
| 888 | } |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 889 | |
Brian Salomon | f8480b9 | 2017-07-27 15:45:59 -0400 | [diff] [blame] | 890 | explicit CircleInside2PtConicalEffect(const CircleInside2PtConicalEffect& that) |
| 891 | : INHERITED(that), fInfo(that.fInfo) { |
| 892 | this->initClassID<CircleInside2PtConicalEffect>(); |
| 893 | } |
| 894 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 895 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 896 | |
| 897 | const CircleConicalInfo fInfo; |
| 898 | |
| 899 | typedef GrGradientEffect INHERITED; |
| 900 | }; |
| 901 | |
Brian Salomon | f8480b9 | 2017-07-27 15:45:59 -0400 | [diff] [blame] | 902 | class CircleInside2PtConicalEffect::GLSLCircleInside2PtConicalProcessor |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 903 | : public GrGradientEffect::GLSLProcessor { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 904 | public: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 905 | GLSLCircleInside2PtConicalProcessor(const GrProcessor&); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 906 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 907 | virtual void emitCode(EmitArgs&) override; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 908 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 909 | static void GenKey(const GrProcessor&, const GrShaderCaps& caps, GrProcessorKeyBuilder* b); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 910 | |
| 911 | protected: |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 912 | void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 913 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 914 | UniformHandle fCenterUni; |
| 915 | UniformHandle fParamUni; |
| 916 | |
| 917 | const char* fVSVaryingName; |
| 918 | const char* fFSVaryingName; |
| 919 | |
| 920 | // @{ |
| 921 | /// Values last uploaded as uniforms |
| 922 | |
| 923 | SkScalar fCachedCenterX; |
| 924 | SkScalar fCachedCenterY; |
| 925 | SkScalar fCachedA; |
| 926 | SkScalar fCachedB; |
| 927 | SkScalar fCachedC; |
| 928 | |
| 929 | // @} |
| 930 | |
| 931 | private: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 932 | typedef GrGradientEffect::GLSLProcessor INHERITED; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 933 | |
| 934 | }; |
| 935 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 936 | void CircleInside2PtConicalEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 937 | GrProcessorKeyBuilder* b) const { |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 938 | CircleInside2PtConicalEffect::GLSLCircleInside2PtConicalProcessor::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 939 | } |
| 940 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 941 | GrGLSLFragmentProcessor* CircleInside2PtConicalEffect::onCreateGLSLInstance() const { |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 942 | return new CircleInside2PtConicalEffect::GLSLCircleInside2PtConicalProcessor(*this); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 943 | } |
| 944 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 945 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(CircleInside2PtConicalEffect); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 946 | |
joshualitt | 0125847 | 2014-09-22 10:29:30 -0700 | [diff] [blame] | 947 | /* |
| 948 | * All Two point conical gradient test create functions may occasionally create edge case shaders |
| 949 | */ |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 950 | #if GR_TEST_UTILS |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 951 | std::unique_ptr<GrFragmentProcessor> CircleInside2PtConicalEffect::TestCreate( |
| 952 | GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 953 | SkPoint center1 = {d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()}; |
| 954 | SkScalar radius1 = d->fRandom->nextUScalar1() + 0.0001f; // make sure radius1 != 0 |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 955 | SkPoint center2; |
| 956 | SkScalar radius2; |
| 957 | do { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 958 | center2.set(d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 959 | // Below makes sure that circle one is contained within circle two |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 960 | SkScalar increase = d->fRandom->nextUScalar1(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 961 | SkPoint diff = center2 - center1; |
| 962 | SkScalar diffLen = diff.length(); |
| 963 | radius2 = radius1 + diffLen + increase; |
| 964 | // If the circles are identical the factory will give us an empty shader. |
| 965 | } while (radius1 == radius2 && center1 == center2); |
| 966 | |
Brian Osman | 3f74860 | 2016-10-03 18:29:03 -0400 | [diff] [blame] | 967 | RandomGradientParams params(d->fRandom); |
Brian Osman | a219653 | 2016-10-17 12:48:13 -0400 | [diff] [blame] | 968 | auto shader = params.fUseColors4f ? |
| 969 | SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2, |
| 970 | params.fColors4f, params.fColorSpace, params.fStops, |
| 971 | params.fColorCount, params.fTileMode) : |
| 972 | SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2, |
| 973 | params.fColors, params.fStops, |
| 974 | params.fColorCount, params.fTileMode); |
Brian Osman | 9f532a3 | 2016-10-19 11:12:09 -0400 | [diff] [blame] | 975 | GrTest::TestAsFPArgs asFPArgs(d); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 976 | std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args()); |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 977 | GrAlwaysAssert(fp); |
joshualitt | 8ca93e7 | 2015-07-08 06:51:43 -0700 | [diff] [blame] | 978 | return fp; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 979 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 980 | #endif |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 981 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 982 | CircleInside2PtConicalEffect::GLSLCircleInside2PtConicalProcessor |
| 983 | ::GLSLCircleInside2PtConicalProcessor(const GrProcessor& processor) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 984 | : fVSVaryingName(nullptr) |
| 985 | , fFSVaryingName(nullptr) |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 986 | , fCachedCenterX(SK_ScalarMax) |
| 987 | , fCachedCenterY(SK_ScalarMax) |
| 988 | , fCachedA(SK_ScalarMax) |
| 989 | , fCachedB(SK_ScalarMax) |
| 990 | , fCachedC(SK_ScalarMax) {} |
| 991 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 992 | void CircleInside2PtConicalEffect::GLSLCircleInside2PtConicalProcessor::emitCode(EmitArgs& args) { |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 993 | const CircleInside2PtConicalEffect& ge = args.fFp.cast<CircleInside2PtConicalEffect>(); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 994 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 995 | this->emitUniforms(uniformHandler, ge); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 996 | fCenterUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 997 | kVec2f_GrSLType, kDefault_GrSLPrecision, |
| 998 | "Conical2FSCenter"); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 999 | fParamUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1000 | kVec3f_GrSLType, kDefault_GrSLPrecision, |
| 1001 | "Conical2FSParams"); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1002 | SkString tName("t"); |
| 1003 | |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 1004 | GrShaderVar center = uniformHandler->getUniformVariable(fCenterUni); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1005 | // params.x = A |
| 1006 | // params.y = B |
| 1007 | // params.z = C |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 1008 | GrShaderVar params = uniformHandler->getUniformVariable(fParamUni); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1009 | |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1010 | // if we have a float3 from being in perspective, convert it to a float2 first |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 1011 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 1012 | SkString coords2DString = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); |
skia.committer@gmail.com | ede0c5c | 2014-04-23 03:04:11 +0000 | [diff] [blame] | 1013 | const char* coords2D = coords2DString.c_str(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1014 | |
| 1015 | // p = coords2D |
| 1016 | // e = center end |
| 1017 | // r = radius end |
| 1018 | // A = dot(e, e) - r^2 + 2 * r - 1 |
| 1019 | // B = (r -1) / A |
| 1020 | // C = 1 / A |
| 1021 | // d = dot(e, p) + B |
| 1022 | // t = d +/- sqrt(d^2 - A * dot(p, p) + C) |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1023 | fragBuilder->codeAppendf("\tfloat pDotp = dot(%s, %s);\n", coords2D, coords2D); |
| 1024 | fragBuilder->codeAppendf("\tfloat d = dot(%s, %s) + %s.y;\n", coords2D, center.c_str(), |
| 1025 | params.c_str()); |
| 1026 | fragBuilder->codeAppendf("\tfloat %s = d + sqrt(d * d - %s.x * pDotp + %s.z);\n", |
| 1027 | tName.c_str(), params.c_str(), params.c_str()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1028 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1029 | this->emitColor(fragBuilder, |
| 1030 | uniformHandler, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 1031 | args.fShaderCaps, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1032 | ge, |
| 1033 | tName.c_str(), |
| 1034 | args.fOutputColor, |
| 1035 | args.fInputColor, |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 1036 | args.fTexSamplers); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1039 | void CircleInside2PtConicalEffect::GLSLCircleInside2PtConicalProcessor::onSetData( |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 1040 | const GrGLSLProgramDataManager& pdman, const GrFragmentProcessor& processor) { |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 1041 | INHERITED::onSetData(pdman, processor); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1042 | const CircleInside2PtConicalEffect& data = processor.cast<CircleInside2PtConicalEffect>(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1043 | SkScalar centerX = data.centerX(); |
| 1044 | SkScalar centerY = data.centerY(); |
| 1045 | SkScalar A = data.A(); |
| 1046 | SkScalar B = data.B(); |
| 1047 | SkScalar C = data.C(); |
| 1048 | |
| 1049 | if (fCachedCenterX != centerX || fCachedCenterY != centerY || |
| 1050 | fCachedA != A || fCachedB != B || fCachedC != C) { |
| 1051 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1052 | pdman.set2f(fCenterUni, SkScalarToFloat(centerX), SkScalarToFloat(centerY)); |
| 1053 | pdman.set3f(fParamUni, SkScalarToFloat(A), SkScalarToFloat(B), SkScalarToFloat(C)); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1054 | |
| 1055 | fCachedCenterX = centerX; |
| 1056 | fCachedCenterY = centerY; |
| 1057 | fCachedA = A; |
| 1058 | fCachedB = B; |
| 1059 | fCachedC = C; |
| 1060 | } |
| 1061 | } |
| 1062 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1063 | void CircleInside2PtConicalEffect::GLSLCircleInside2PtConicalProcessor::GenKey( |
| 1064 | const GrProcessor& processor, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 1065 | const GrShaderCaps&, GrProcessorKeyBuilder* b) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1066 | b->add32(GenBaseGradientKey(processor)); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | ////////////////////////////////////////////////////////////////////////////// |
| 1070 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1071 | class CircleOutside2PtConicalEffect : public GrGradientEffect { |
| 1072 | public: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1073 | class GLSLCircleOutside2PtConicalProcessor; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1074 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 1075 | static std::unique_ptr<GrFragmentProcessor> Make(const CreateArgs& args, |
| 1076 | const CircleConicalInfo& info) { |
| 1077 | return std::unique_ptr<GrFragmentProcessor>(new CircleOutside2PtConicalEffect(args, info)); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1080 | const char* name() const override { return "Two-Point Conical Gradient Outside"; } |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 1081 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 1082 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 1083 | return std::unique_ptr<GrFragmentProcessor>(new CircleOutside2PtConicalEffect(*this)); |
Brian Salomon | f8480b9 | 2017-07-27 15:45:59 -0400 | [diff] [blame] | 1084 | } |
| 1085 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1086 | SkScalar centerX() const { return fInfo.fCenterEnd.fX; } |
| 1087 | SkScalar centerY() const { return fInfo.fCenterEnd.fY; } |
| 1088 | SkScalar A() const { return fInfo.fA; } |
| 1089 | SkScalar B() const { return fInfo.fB; } |
| 1090 | SkScalar C() const { return fInfo.fC; } |
| 1091 | SkScalar tLimit() const { return fTLimit; } |
| 1092 | bool isFlipped() const { return fIsFlipped; } |
| 1093 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1094 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 1095 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 1096 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 1097 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 1098 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 1099 | bool onIsEqual(const GrFragmentProcessor& sBase) const override { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 1100 | const CircleOutside2PtConicalEffect& s = sBase.cast<CircleOutside2PtConicalEffect>(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1101 | return (INHERITED::onIsEqual(sBase) && |
| 1102 | this->fInfo.fCenterEnd == s.fInfo.fCenterEnd && |
| 1103 | this->fInfo.fA == s.fInfo.fA && |
| 1104 | this->fInfo.fB == s.fInfo.fB && |
| 1105 | this->fInfo.fC == s.fInfo.fC && |
| 1106 | this->fTLimit == s.fTLimit && |
| 1107 | this->fIsFlipped == s.fIsFlipped); |
| 1108 | } |
| 1109 | |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1110 | CircleOutside2PtConicalEffect(const CreateArgs& args, const CircleConicalInfo& info) |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 1111 | : INHERITED(args, false /* opaque: draws transparent black outside of the cone. */) |
| 1112 | , fInfo(info) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 1113 | this->initClassID<CircleOutside2PtConicalEffect>(); |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1114 | const SkTwoPointConicalGradient& shader = |
| 1115 | *static_cast<const SkTwoPointConicalGradient*>(args.fShader); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1116 | if (shader.getStartRadius() != shader.getEndRadius()) { |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 1117 | fTLimit = shader.getStartRadius() / (shader.getStartRadius() - shader.getEndRadius()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1118 | } else { |
| 1119 | fTLimit = SK_ScalarMin; |
| 1120 | } |
| 1121 | |
| 1122 | fIsFlipped = shader.isFlippedGrad(); |
| 1123 | } |
| 1124 | |
Brian Salomon | f8480b9 | 2017-07-27 15:45:59 -0400 | [diff] [blame] | 1125 | explicit CircleOutside2PtConicalEffect(const CircleOutside2PtConicalEffect& that) |
| 1126 | : INHERITED(that) |
| 1127 | , fInfo(that.fInfo) |
| 1128 | , fTLimit(that.fTLimit) |
| 1129 | , fIsFlipped(that.fIsFlipped) { |
| 1130 | this->initClassID<CircleOutside2PtConicalEffect>(); |
| 1131 | } |
| 1132 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 1133 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1134 | |
| 1135 | const CircleConicalInfo fInfo; |
| 1136 | SkScalar fTLimit; |
| 1137 | bool fIsFlipped; |
| 1138 | |
| 1139 | typedef GrGradientEffect INHERITED; |
| 1140 | }; |
| 1141 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1142 | class CircleOutside2PtConicalEffect::GLSLCircleOutside2PtConicalProcessor |
| 1143 | : public GrGradientEffect::GLSLProcessor { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1144 | public: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1145 | GLSLCircleOutside2PtConicalProcessor(const GrProcessor&); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1146 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 1147 | virtual void emitCode(EmitArgs&) override; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1148 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 1149 | static void GenKey(const GrProcessor&, const GrShaderCaps& caps, GrProcessorKeyBuilder* b); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1150 | |
| 1151 | protected: |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 1152 | void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 1153 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1154 | UniformHandle fCenterUni; |
| 1155 | UniformHandle fParamUni; |
| 1156 | |
| 1157 | const char* fVSVaryingName; |
| 1158 | const char* fFSVaryingName; |
| 1159 | |
| 1160 | bool fIsFlipped; |
| 1161 | |
| 1162 | // @{ |
| 1163 | /// Values last uploaded as uniforms |
| 1164 | |
| 1165 | SkScalar fCachedCenterX; |
| 1166 | SkScalar fCachedCenterY; |
| 1167 | SkScalar fCachedA; |
| 1168 | SkScalar fCachedB; |
| 1169 | SkScalar fCachedC; |
| 1170 | SkScalar fCachedTLimit; |
| 1171 | |
| 1172 | // @} |
| 1173 | |
| 1174 | private: |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1175 | typedef GrGradientEffect::GLSLProcessor INHERITED; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1176 | |
| 1177 | }; |
| 1178 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 1179 | void CircleOutside2PtConicalEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 1180 | GrProcessorKeyBuilder* b) const { |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1181 | CircleOutside2PtConicalEffect::GLSLCircleOutside2PtConicalProcessor::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 1182 | } |
| 1183 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 1184 | GrGLSLFragmentProcessor* CircleOutside2PtConicalEffect::onCreateGLSLInstance() const { |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1185 | return new CircleOutside2PtConicalEffect::GLSLCircleOutside2PtConicalProcessor(*this); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1188 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(CircleOutside2PtConicalEffect); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1189 | |
joshualitt | 0125847 | 2014-09-22 10:29:30 -0700 | [diff] [blame] | 1190 | /* |
| 1191 | * All Two point conical gradient test create functions may occasionally create edge case shaders |
| 1192 | */ |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 1193 | #if GR_TEST_UTILS |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 1194 | std::unique_ptr<GrFragmentProcessor> CircleOutside2PtConicalEffect::TestCreate( |
| 1195 | GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 1196 | SkPoint center1 = {d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()}; |
| 1197 | SkScalar radius1 = d->fRandom->nextUScalar1() + 0.0001f; // make sure radius1 != 0 |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1198 | SkPoint center2; |
| 1199 | SkScalar radius2; |
| 1200 | SkScalar diffLen; |
| 1201 | do { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 1202 | center2.set(d->fRandom->nextUScalar1(), d->fRandom->nextUScalar1()); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1203 | // If the circles share a center than we can't be in the outside case |
| 1204 | } while (center1 == center2); |
joshualitt | 0125847 | 2014-09-22 10:29:30 -0700 | [diff] [blame] | 1205 | SkPoint diff = center2 - center1; |
| 1206 | diffLen = diff.length(); |
| 1207 | // Below makes sure that circle one is not contained within circle two |
| 1208 | // and have radius2 >= radius to match sorting on cpu side |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 1209 | radius2 = radius1 + d->fRandom->nextRangeF(0.f, diffLen); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1210 | |
Brian Osman | 3f74860 | 2016-10-03 18:29:03 -0400 | [diff] [blame] | 1211 | RandomGradientParams params(d->fRandom); |
Brian Osman | a219653 | 2016-10-17 12:48:13 -0400 | [diff] [blame] | 1212 | auto shader = params.fUseColors4f ? |
| 1213 | SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2, |
| 1214 | params.fColors4f, params.fColorSpace, params.fStops, |
| 1215 | params.fColorCount, params.fTileMode) : |
| 1216 | SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2, |
| 1217 | params.fColors, params.fStops, |
| 1218 | params.fColorCount, params.fTileMode); |
Brian Osman | 9f532a3 | 2016-10-19 11:12:09 -0400 | [diff] [blame] | 1219 | GrTest::TestAsFPArgs asFPArgs(d); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 1220 | std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args()); |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 1221 | GrAlwaysAssert(fp); |
joshualitt | 8ca93e7 | 2015-07-08 06:51:43 -0700 | [diff] [blame] | 1222 | return fp; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1223 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 1224 | #endif |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1225 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1226 | CircleOutside2PtConicalEffect::GLSLCircleOutside2PtConicalProcessor |
| 1227 | ::GLSLCircleOutside2PtConicalProcessor(const GrProcessor& processor) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1228 | : fVSVaryingName(nullptr) |
| 1229 | , fFSVaryingName(nullptr) |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1230 | , fCachedCenterX(SK_ScalarMax) |
| 1231 | , fCachedCenterY(SK_ScalarMax) |
| 1232 | , fCachedA(SK_ScalarMax) |
| 1233 | , fCachedB(SK_ScalarMax) |
| 1234 | , fCachedC(SK_ScalarMax) |
| 1235 | , fCachedTLimit(SK_ScalarMax) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1236 | const CircleOutside2PtConicalEffect& data = processor.cast<CircleOutside2PtConicalEffect>(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1237 | fIsFlipped = data.isFlipped(); |
| 1238 | } |
| 1239 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1240 | void CircleOutside2PtConicalEffect::GLSLCircleOutside2PtConicalProcessor::emitCode(EmitArgs& args) { |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 1241 | const CircleOutside2PtConicalEffect& ge = args.fFp.cast<CircleOutside2PtConicalEffect>(); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1242 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 1243 | this->emitUniforms(uniformHandler, ge); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 1244 | fCenterUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1245 | kVec2f_GrSLType, kDefault_GrSLPrecision, |
| 1246 | "Conical2FSCenter"); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 1247 | fParamUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1248 | kVec4f_GrSLType, kDefault_GrSLPrecision, |
| 1249 | "Conical2FSParams"); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1250 | SkString tName("t"); |
| 1251 | |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 1252 | GrShaderVar center = uniformHandler->getUniformVariable(fCenterUni); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1253 | // params.x = A |
| 1254 | // params.y = B |
| 1255 | // params.z = C |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 1256 | GrShaderVar params = uniformHandler->getUniformVariable(fParamUni); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1257 | |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1258 | // if we have a float3 from being in perspective, convert it to a float2 first |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 1259 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 1260 | SkString coords2DString = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); |
skia.committer@gmail.com | ede0c5c | 2014-04-23 03:04:11 +0000 | [diff] [blame] | 1261 | const char* coords2D = coords2DString.c_str(); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1262 | |
| 1263 | // output will default to transparent black (we simply won't write anything |
| 1264 | // else to it if invalid, instead of discarding or returning prematurely) |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1265 | fragBuilder->codeAppendf("\t%s = float4(0.0,0.0,0.0,0.0);\n", args.fOutputColor); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1266 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1267 | // p = coords2D |
| 1268 | // e = center end |
| 1269 | // r = radius end |
| 1270 | // A = dot(e, e) - r^2 + 2 * r - 1 |
| 1271 | // B = (r -1) / A |
| 1272 | // C = 1 / A |
| 1273 | // d = dot(e, p) + B |
| 1274 | // t = d +/- sqrt(d^2 - A * dot(p, p) + C) |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1275 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1276 | fragBuilder->codeAppendf("\tfloat pDotp = dot(%s, %s);\n", coords2D, coords2D); |
| 1277 | fragBuilder->codeAppendf("\tfloat d = dot(%s, %s) + %s.y;\n", coords2D, center.c_str(), |
| 1278 | params.c_str()); |
| 1279 | fragBuilder->codeAppendf("\tfloat deter = d * d - %s.x * pDotp + %s.z;\n", params.c_str(), |
| 1280 | params.c_str()); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1281 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1282 | // Must check to see if we flipped the circle order (to make sure start radius < end radius) |
| 1283 | // If so we must also flip sign on sqrt |
| 1284 | if (!fIsFlipped) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1285 | fragBuilder->codeAppendf("\tfloat %s = d + sqrt(deter);\n", tName.c_str()); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1286 | } else { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1287 | fragBuilder->codeAppendf("\tfloat %s = d - sqrt(deter);\n", tName.c_str()); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1288 | } |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1289 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1290 | fragBuilder->codeAppendf("\tif (%s >= %s.w && deter >= 0.0) {\n", |
| 1291 | tName.c_str(), params.c_str()); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1292 | fragBuilder->codeAppend("\t\t"); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 1293 | this->emitColor(fragBuilder, |
| 1294 | uniformHandler, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 1295 | args.fShaderCaps, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1296 | ge, |
| 1297 | tName.c_str(), |
| 1298 | args.fOutputColor, |
| 1299 | args.fInputColor, |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 1300 | args.fTexSamplers); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1301 | fragBuilder->codeAppend("\t}\n"); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1304 | void CircleOutside2PtConicalEffect::GLSLCircleOutside2PtConicalProcessor::onSetData( |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 1305 | const GrGLSLProgramDataManager& pdman, const GrFragmentProcessor& processor) { |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 1306 | INHERITED::onSetData(pdman, processor); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1307 | const CircleOutside2PtConicalEffect& data = processor.cast<CircleOutside2PtConicalEffect>(); |
commit-bot@chromium.org | 44d83c1 | 2014-04-21 13:10:25 +0000 | [diff] [blame] | 1308 | SkASSERT(data.isFlipped() == fIsFlipped); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1309 | SkScalar centerX = data.centerX(); |
| 1310 | SkScalar centerY = data.centerY(); |
| 1311 | SkScalar A = data.A(); |
| 1312 | SkScalar B = data.B(); |
| 1313 | SkScalar C = data.C(); |
| 1314 | SkScalar tLimit = data.tLimit(); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1315 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1316 | if (fCachedCenterX != centerX || fCachedCenterY != centerY || |
| 1317 | fCachedA != A || fCachedB != B || fCachedC != C || fCachedTLimit != tLimit) { |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1318 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1319 | pdman.set2f(fCenterUni, SkScalarToFloat(centerX), SkScalarToFloat(centerY)); |
| 1320 | pdman.set4f(fParamUni, SkScalarToFloat(A), SkScalarToFloat(B), SkScalarToFloat(C), |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1321 | SkScalarToFloat(tLimit)); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1322 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1323 | fCachedCenterX = centerX; |
| 1324 | fCachedCenterY = centerY; |
| 1325 | fCachedA = A; |
| 1326 | fCachedB = B; |
| 1327 | fCachedC = C; |
| 1328 | fCachedTLimit = tLimit; |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1329 | } |
| 1330 | } |
| 1331 | |
fmenozzi | 55d318d | 2016-08-09 08:05:57 -0700 | [diff] [blame] | 1332 | void CircleOutside2PtConicalEffect::GLSLCircleOutside2PtConicalProcessor::GenKey( |
| 1333 | const GrProcessor& processor, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 1334 | const GrShaderCaps&, GrProcessorKeyBuilder* b) { |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 1335 | uint32_t* key = b->add32n(2); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1336 | key[0] = GenBaseGradientKey(processor); |
| 1337 | key[1] = processor.cast<CircleOutside2PtConicalEffect>().isFlipped(); |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1338 | } |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 1339 | |
| 1340 | ////////////////////////////////////////////////////////////////////////////// |
| 1341 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame^] | 1342 | std::unique_ptr<GrFragmentProcessor> Gr2PtConicalGradientEffect::Make( |
| 1343 | const GrGradientEffect::CreateArgs& args) { |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1344 | const SkTwoPointConicalGradient& shader = |
| 1345 | *static_cast<const SkTwoPointConicalGradient*>(args.fShader); |
| 1346 | |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 1347 | SkMatrix matrix; |
| 1348 | if (!shader.getLocalMatrix().invert(&matrix)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1349 | return nullptr; |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 1350 | } |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1351 | if (args.fMatrix) { |
commit-bot@chromium.org | 96fb748 | 2014-05-09 20:28:11 +0000 | [diff] [blame] | 1352 | SkMatrix inv; |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1353 | if (!args.fMatrix->invert(&inv)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1354 | return nullptr; |
commit-bot@chromium.org | 96fb748 | 2014-05-09 20:28:11 +0000 | [diff] [blame] | 1355 | } |
| 1356 | matrix.postConcat(inv); |
| 1357 | } |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 1358 | |
brianosman | b9c5137 | 2016-09-15 11:09:45 -0700 | [diff] [blame] | 1359 | GrGradientEffect::CreateArgs newArgs(args.fContext, args.fShader, &matrix, args.fTileMode, |
| 1360 | std::move(args.fColorSpaceXform), args.fGammaCorrect); |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1361 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1362 | if (shader.getStartRadius() < kErrorTol) { |
| 1363 | SkScalar focalX; |
| 1364 | ConicalType type = set_matrix_focal_conical(shader, &matrix, &focalX); |
| 1365 | if (type == kInside_ConicalType) { |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1366 | return FocalInside2PtConicalEffect::Make(newArgs, focalX); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1367 | } else if(type == kEdge_ConicalType) { |
| 1368 | set_matrix_edge_conical(shader, &matrix); |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1369 | return Edge2PtConicalEffect::Make(newArgs); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1370 | } else { |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1371 | return FocalOutside2PtConicalEffect::Make(newArgs, focalX); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | CircleConicalInfo info; |
| 1376 | ConicalType type = set_matrix_circle_conical(shader, &matrix, &info); |
| 1377 | |
| 1378 | if (type == kInside_ConicalType) { |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1379 | return CircleInside2PtConicalEffect::Make(newArgs, info); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1380 | } else if (type == kEdge_ConicalType) { |
| 1381 | set_matrix_edge_conical(shader, &matrix); |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1382 | return Edge2PtConicalEffect::Make(newArgs); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1383 | } else { |
brianosman | 9557c27 | 2016-09-15 06:59:15 -0700 | [diff] [blame] | 1384 | return CircleOutside2PtConicalEffect::Make(newArgs, info); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1385 | } |
commit-bot@chromium.org | 2af1a2d | 2014-04-04 13:50:50 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
commit-bot@chromium.org | aa64fbf | 2014-04-03 14:59:19 +0000 | [diff] [blame] | 1388 | #endif |