commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrOvalEffect.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 9 | |
| 10 | #include "GrFragmentProcessor.h" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 11 | #include "GrInvariantOutput.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 12 | #include "SkRect.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 13 | #include "glsl/GrGLSLFragmentProcessor.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" |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 17 | |
| 18 | ////////////////////////////////////////////////////////////////////////////// |
| 19 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 20 | class CircleEffect : public GrFragmentProcessor { |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 21 | public: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 22 | static GrFragmentProcessor* Create(GrPrimitiveEdgeType, const SkPoint& center, SkScalar radius); |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 23 | |
| 24 | virtual ~CircleEffect() {}; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 25 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 26 | const char* name() const override { return "Circle"; } |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 27 | |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 28 | const SkPoint& getCenter() const { return fCenter; } |
| 29 | SkScalar getRadius() const { return fRadius; } |
| 30 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 31 | GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; } |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 32 | |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 33 | private: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 34 | CircleEffect(GrPrimitiveEdgeType, const SkPoint& center, SkScalar radius); |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 35 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 36 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 37 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 38 | void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 39 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 40 | bool onIsEqual(const GrFragmentProcessor&) const override; |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 41 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 42 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override; |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 43 | |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 44 | SkPoint fCenter; |
| 45 | SkScalar fRadius; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 46 | GrPrimitiveEdgeType fEdgeType; |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 47 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 48 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 49 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 50 | typedef GrFragmentProcessor INHERITED; |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 53 | GrFragmentProcessor* CircleEffect::Create(GrPrimitiveEdgeType edgeType, const SkPoint& center, |
| 54 | SkScalar radius) { |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 55 | SkASSERT(radius >= 0); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 56 | return new CircleEffect(edgeType, center, radius); |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 57 | } |
| 58 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 59 | void CircleEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 60 | inout->mulByUnknownSingleComponent(); |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 61 | } |
| 62 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 63 | CircleEffect::CircleEffect(GrPrimitiveEdgeType edgeType, const SkPoint& c, SkScalar r) |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 64 | : fCenter(c) |
| 65 | , fRadius(r) |
| 66 | , fEdgeType(edgeType) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 67 | this->initClassID<CircleEffect>(); |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 68 | this->setWillReadFragmentPosition(); |
| 69 | } |
| 70 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 71 | bool CircleEffect::onIsEqual(const GrFragmentProcessor& other) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 72 | const CircleEffect& ce = other.cast<CircleEffect>(); |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 73 | return fEdgeType == ce.fEdgeType && fCenter == ce.fCenter && fRadius == ce.fRadius; |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | ////////////////////////////////////////////////////////////////////////////// |
| 77 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 78 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(CircleEffect); |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 79 | |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 80 | const GrFragmentProcessor* CircleEffect::TestCreate(GrProcessorTestData* d) { |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 81 | SkPoint center; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 82 | center.fX = d->fRandom->nextRangeScalar(0.f, 1000.f); |
| 83 | center.fY = d->fRandom->nextRangeScalar(0.f, 1000.f); |
| 84 | SkScalar radius = d->fRandom->nextRangeF(0.f, 1000.f); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 85 | GrPrimitiveEdgeType et; |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 86 | do { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 87 | et = (GrPrimitiveEdgeType)d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 88 | } while (kHairlineAA_GrProcessorEdgeType == et); |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 89 | return CircleEffect::Create(et, center, radius); |
| 90 | } |
| 91 | |
| 92 | ////////////////////////////////////////////////////////////////////////////// |
| 93 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 94 | class GLCircleEffect : public GrGLSLFragmentProcessor { |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 95 | public: |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 96 | GLCircleEffect() : fPrevRadius(-1.0f) { } |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 97 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 98 | virtual void emitCode(EmitArgs&) override; |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 99 | |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 100 | static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*); |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 101 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 102 | protected: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 103 | void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override; |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 104 | |
| 105 | private: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 106 | GrGLSLProgramDataManager::UniformHandle fCircleUniform; |
| 107 | SkPoint fPrevCenter; |
| 108 | SkScalar fPrevRadius; |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 109 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 110 | typedef GrGLSLFragmentProcessor INHERITED; |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 113 | void GLCircleEffect::emitCode(EmitArgs& args) { |
| 114 | const CircleEffect& ce = args.fFp.cast<CircleEffect>(); |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 115 | const char *circleName; |
bsalomon | fcabe42 | 2015-04-28 12:43:01 -0700 | [diff] [blame] | 116 | // The circle uniform is (center.x, center.y, radius + 0.5, 1 / (radius + 0.5)) for regular |
| 117 | // fills and (..., radius - 0.5, 1 / (radius - 0.5)) for inverse fills. |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 118 | fCircleUniform = args.fUniformHandler->addUniform(GrGLSLUniformHandler::kFragment_Visibility, |
| 119 | kVec4f_GrSLType, kDefault_GrSLPrecision, |
| 120 | "circle", |
| 121 | &circleName); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 122 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 123 | GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 124 | const char* fragmentPos = fragBuilder->fragmentPosition(); |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 125 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 126 | SkASSERT(kHairlineAA_GrProcessorEdgeType != ce.getEdgeType()); |
bsalomon | fcabe42 | 2015-04-28 12:43:01 -0700 | [diff] [blame] | 127 | // TODO: Right now the distance to circle caclulation is performed in a space normalized to the |
| 128 | // radius and then denormalized. This is to prevent overflow on devices that have a "real" |
| 129 | // mediump. It'd be nice to only to this on mediump devices but we currently don't have the |
| 130 | // caps here. |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 131 | if (GrProcessorEdgeTypeIsInverseFill(ce.getEdgeType())) { |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 132 | fragBuilder->codeAppendf("float d = (length((%s.xy - %s.xy) * %s.w) - 1.0) * %s.z;", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 133 | circleName, fragmentPos, circleName, circleName); |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 134 | } else { |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 135 | fragBuilder->codeAppendf("float d = (1.0 - length((%s.xy - %s.xy) * %s.w)) * %s.z;", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 136 | circleName, fragmentPos, circleName, circleName); |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 137 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 138 | if (GrProcessorEdgeTypeIsAA(ce.getEdgeType())) { |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 139 | fragBuilder->codeAppend("d = clamp(d, 0.0, 1.0);"); |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 140 | } else { |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 141 | fragBuilder->codeAppend("d = d > 0.5 ? 1.0 : 0.0;"); |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 142 | } |
| 143 | |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 144 | fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 145 | (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("d")).c_str()); |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 146 | } |
| 147 | |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 148 | void GLCircleEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 149 | GrProcessorKeyBuilder* b) { |
| 150 | const CircleEffect& ce = processor.cast<CircleEffect>(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 151 | b->add32(ce.getEdgeType()); |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 152 | } |
| 153 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 154 | void GLCircleEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
| 155 | const GrProcessor& processor) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 156 | const CircleEffect& ce = processor.cast<CircleEffect>(); |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 157 | if (ce.getRadius() != fPrevRadius || ce.getCenter() != fPrevCenter) { |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 158 | SkScalar radius = ce.getRadius(); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 159 | if (GrProcessorEdgeTypeIsInverseFill(ce.getEdgeType())) { |
commit-bot@chromium.org | 0a09d71 | 2014-04-09 21:26:11 +0000 | [diff] [blame] | 160 | radius -= 0.5f; |
| 161 | } else { |
| 162 | radius += 0.5f; |
| 163 | } |
bsalomon | fcabe42 | 2015-04-28 12:43:01 -0700 | [diff] [blame] | 164 | pdman.set4f(fCircleUniform, ce.getCenter().fX, ce.getCenter().fY, radius, |
| 165 | SkScalarInvert(radius)); |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 166 | fPrevCenter = ce.getCenter(); |
| 167 | fPrevRadius = ce.getRadius(); |
| 168 | } |
| 169 | } |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 170 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 171 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 172 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 173 | void CircleEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, |
| 174 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 175 | GLCircleEffect::GenKey(*this, caps, b); |
| 176 | } |
| 177 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 178 | GrGLSLFragmentProcessor* CircleEffect::onCreateGLSLInstance() const { |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 179 | return new GLCircleEffect; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | ////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 183 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 184 | class EllipseEffect : public GrFragmentProcessor { |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 185 | public: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 186 | static GrFragmentProcessor* Create(GrPrimitiveEdgeType, const SkPoint& center, SkScalar rx, |
| 187 | SkScalar ry); |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 188 | |
| 189 | virtual ~EllipseEffect() {}; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 190 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 191 | const char* name() const override { return "Ellipse"; } |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 192 | |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 193 | const SkPoint& getCenter() const { return fCenter; } |
| 194 | SkVector getRadii() const { return fRadii; } |
| 195 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 196 | GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; } |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 197 | |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 198 | private: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 199 | EllipseEffect(GrPrimitiveEdgeType, const SkPoint& center, SkScalar rx, SkScalar ry); |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 200 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 201 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 202 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 203 | void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 204 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 205 | bool onIsEqual(const GrFragmentProcessor&) const override; |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 206 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 207 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override; |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 208 | |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 209 | SkPoint fCenter; |
| 210 | SkVector fRadii; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 211 | GrPrimitiveEdgeType fEdgeType; |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 212 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 213 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 214 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 215 | typedef GrFragmentProcessor INHERITED; |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 216 | }; |
| 217 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 218 | GrFragmentProcessor* EllipseEffect::Create(GrPrimitiveEdgeType edgeType, |
| 219 | const SkPoint& center, |
| 220 | SkScalar rx, |
| 221 | SkScalar ry) { |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 222 | SkASSERT(rx >= 0 && ry >= 0); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 223 | return new EllipseEffect(edgeType, center, rx, ry); |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 224 | } |
| 225 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 226 | void EllipseEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 227 | inout->mulByUnknownSingleComponent(); |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 228 | } |
| 229 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 230 | EllipseEffect::EllipseEffect(GrPrimitiveEdgeType edgeType, const SkPoint& c, SkScalar rx, SkScalar ry) |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 231 | : fCenter(c) |
| 232 | , fRadii(SkVector::Make(rx, ry)) |
| 233 | , fEdgeType(edgeType) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 234 | this->initClassID<EllipseEffect>(); |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 235 | this->setWillReadFragmentPosition(); |
| 236 | } |
| 237 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 238 | bool EllipseEffect::onIsEqual(const GrFragmentProcessor& other) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 239 | const EllipseEffect& ee = other.cast<EllipseEffect>(); |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 240 | return fEdgeType == ee.fEdgeType && fCenter == ee.fCenter && fRadii == ee.fRadii; |
| 241 | } |
| 242 | |
| 243 | ////////////////////////////////////////////////////////////////////////////// |
| 244 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 245 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(EllipseEffect); |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 246 | |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 247 | const GrFragmentProcessor* EllipseEffect::TestCreate(GrProcessorTestData* d) { |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 248 | SkPoint center; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 249 | center.fX = d->fRandom->nextRangeScalar(0.f, 1000.f); |
| 250 | center.fY = d->fRandom->nextRangeScalar(0.f, 1000.f); |
| 251 | SkScalar rx = d->fRandom->nextRangeF(0.f, 1000.f); |
| 252 | SkScalar ry = d->fRandom->nextRangeF(0.f, 1000.f); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 253 | GrPrimitiveEdgeType et; |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 254 | do { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 255 | et = (GrPrimitiveEdgeType)d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 256 | } while (kHairlineAA_GrProcessorEdgeType == et); |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 257 | return EllipseEffect::Create(et, center, rx, ry); |
| 258 | } |
| 259 | |
| 260 | ////////////////////////////////////////////////////////////////////////////// |
| 261 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 262 | class GLEllipseEffect : public GrGLSLFragmentProcessor { |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 263 | public: |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 264 | GLEllipseEffect() { |
| 265 | fPrevRadii.fX = -1.0f; |
| 266 | } |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 267 | |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 268 | void emitCode(EmitArgs&) override; |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 269 | |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 270 | static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*); |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 271 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 272 | protected: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 273 | void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override; |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 274 | |
| 275 | private: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 276 | GrGLSLProgramDataManager::UniformHandle fEllipseUniform; |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 277 | GrGLSLProgramDataManager::UniformHandle fScaleUniform; |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 278 | SkPoint fPrevCenter; |
| 279 | SkVector fPrevRadii; |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 280 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 281 | typedef GrGLSLFragmentProcessor INHERITED; |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 282 | }; |
| 283 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 284 | void GLEllipseEffect::emitCode(EmitArgs& args) { |
| 285 | const EllipseEffect& ee = args.fFp.cast<EllipseEffect>(); |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 286 | const char *ellipseName; |
| 287 | // The ellipse uniform is (center.x, center.y, 1 / rx^2, 1 / ry^2) |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 288 | // The last two terms can underflow on mediump, so we use highp. |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 289 | fEllipseUniform = args.fUniformHandler->addUniform(GrGLSLUniformHandler::kFragment_Visibility, |
| 290 | kVec4f_GrSLType, kHigh_GrSLPrecision, |
| 291 | "ellipse", |
| 292 | &ellipseName); |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 293 | // If we're on a device with a "real" mediump then we'll do the distance computation in a space |
| 294 | // that is normalized by the larger radius. The scale uniform will be scale, 1/scale. The |
| 295 | // inverse squared radii uniform values are already in this normalized space. The center is |
| 296 | // not. |
| 297 | const char* scaleName = nullptr; |
| 298 | if (args.fGLSLCaps->floatPrecisionVaries()) { |
| 299 | fScaleUniform = args.fUniformHandler->addUniform( |
| 300 | GrGLSLUniformHandler::kFragment_Visibility, kVec2f_GrSLType, kDefault_GrSLPrecision, |
| 301 | "scale", &scaleName); |
| 302 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 303 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 304 | GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 305 | const char* fragmentPos = fragBuilder->fragmentPosition(); |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 306 | |
| 307 | // d is the offset to the ellipse center |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 308 | fragBuilder->codeAppendf("vec2 d = %s.xy - %s.xy;", fragmentPos, ellipseName); |
| 309 | if (scaleName) { |
| 310 | fragBuilder->codeAppendf("d *= %s.y;", scaleName); |
| 311 | } |
| 312 | fragBuilder->codeAppendf("vec2 Z = d * %s.zw;", ellipseName); |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 313 | // implicit is the evaluation of (x/rx)^2 + (y/ry)^2 - 1. |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 314 | fragBuilder->codeAppend("float implicit = dot(Z, d) - 1.0;"); |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 315 | // grad_dot is the squared length of the gradient of the implicit. |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 316 | fragBuilder->codeAppendf("float grad_dot = 4.0 * dot(Z, Z);"); |
| 317 | // Avoid calling inversesqrt on zero. |
| 318 | fragBuilder->codeAppend("grad_dot = max(grad_dot, 1.0e-4);"); |
| 319 | fragBuilder->codeAppendf("float approx_dist = implicit * inversesqrt(grad_dot);"); |
| 320 | if (scaleName) { |
| 321 | fragBuilder->codeAppendf("approx_dist *= %s.x;", scaleName); |
| 322 | } |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 323 | |
| 324 | switch (ee.getEdgeType()) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 325 | case kFillAA_GrProcessorEdgeType: |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 326 | fragBuilder->codeAppend("float alpha = clamp(0.5 - approx_dist, 0.0, 1.0);"); |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 327 | break; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 328 | case kInverseFillAA_GrProcessorEdgeType: |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 329 | fragBuilder->codeAppend("float alpha = clamp(0.5 + approx_dist, 0.0, 1.0);"); |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 330 | break; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 331 | case kFillBW_GrProcessorEdgeType: |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 332 | fragBuilder->codeAppend("float alpha = approx_dist > 0.0 ? 0.0 : 1.0;"); |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 333 | break; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 334 | case kInverseFillBW_GrProcessorEdgeType: |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 335 | fragBuilder->codeAppend("float alpha = approx_dist > 0.0 ? 1.0 : 0.0;"); |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 336 | break; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 337 | case kHairlineAA_GrProcessorEdgeType: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 338 | SkFAIL("Hairline not expected here."); |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 339 | } |
| 340 | |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 341 | fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 342 | (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha")).c_str()); |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 343 | } |
| 344 | |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 345 | void GLEllipseEffect::GenKey(const GrProcessor& effect, const GrGLSLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 346 | GrProcessorKeyBuilder* b) { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 347 | const EllipseEffect& ee = effect.cast<EllipseEffect>(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 348 | b->add32(ee.getEdgeType()); |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 349 | } |
| 350 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 351 | void GLEllipseEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
| 352 | const GrProcessor& effect) { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 353 | const EllipseEffect& ee = effect.cast<EllipseEffect>(); |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 354 | if (ee.getRadii() != fPrevRadii || ee.getCenter() != fPrevCenter) { |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 355 | float invRXSqd; |
| 356 | float invRYSqd; |
| 357 | // If we're using a scale factor to work around precision issues, choose the larger radius |
| 358 | // as the scale factor. The inv radii need to be pre-adjusted by the scale factor. |
| 359 | if (fScaleUniform.isValid()) { |
| 360 | if (ee.getRadii().fX > ee.getRadii().fY) { |
| 361 | invRXSqd = 1.f; |
| 362 | invRYSqd = (ee.getRadii().fX * ee.getRadii().fX) / |
| 363 | (ee.getRadii().fY * ee.getRadii().fY); |
| 364 | pdman.set2f(fScaleUniform, ee.getRadii().fX, 1.f / ee.getRadii().fX); |
| 365 | } else { |
| 366 | invRXSqd = (ee.getRadii().fY * ee.getRadii().fY) / |
| 367 | (ee.getRadii().fX * ee.getRadii().fX); |
| 368 | invRYSqd = 1.f; |
| 369 | pdman.set2f(fScaleUniform, ee.getRadii().fY, 1.f / ee.getRadii().fY); |
| 370 | } |
| 371 | } else { |
| 372 | invRXSqd = 1.f / (ee.getRadii().fX * ee.getRadii().fX); |
| 373 | invRYSqd = 1.f / (ee.getRadii().fY * ee.getRadii().fY); |
| 374 | } |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 375 | pdman.set4f(fEllipseUniform, ee.getCenter().fX, ee.getCenter().fY, invRXSqd, invRYSqd); |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 376 | fPrevCenter = ee.getCenter(); |
| 377 | fPrevRadii = ee.getRadii(); |
| 378 | } |
| 379 | } |
| 380 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 381 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 382 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 383 | void EllipseEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, |
| 384 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 385 | GLEllipseEffect::GenKey(*this, caps, b); |
| 386 | } |
| 387 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 388 | GrGLSLFragmentProcessor* EllipseEffect::onCreateGLSLInstance() const { |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 389 | return new GLEllipseEffect; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 390 | } |
| 391 | |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 392 | ////////////////////////////////////////////////////////////////////////////// |
| 393 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 394 | GrFragmentProcessor* GrOvalEffect::Create(GrPrimitiveEdgeType edgeType, const SkRect& oval) { |
| 395 | if (kHairlineAA_GrProcessorEdgeType == edgeType) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 396 | return nullptr; |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 397 | } |
| 398 | SkScalar w = oval.width(); |
| 399 | SkScalar h = oval.height(); |
| 400 | if (SkScalarNearlyEqual(w, h)) { |
| 401 | w /= 2; |
| 402 | return CircleEffect::Create(edgeType, SkPoint::Make(oval.fLeft + w, oval.fTop + w), w); |
commit-bot@chromium.org | d0a5029 | 2014-04-02 15:00:39 +0000 | [diff] [blame] | 403 | } else { |
| 404 | w /= 2; |
| 405 | h /= 2; |
| 406 | return EllipseEffect::Create(edgeType, SkPoint::Make(oval.fLeft + w, oval.fTop + h), w, h); |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 407 | } |
| 408 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 409 | return nullptr; |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 410 | } |