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