blob: 150b5b3718d1417c30040c876120614377e6eebb [file] [log] [blame]
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +00001/*
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"
joshualitteb2a6762014-12-04 11:35:33 -08009
10#include "GrFragmentProcessor.h"
egdaniel605dd0f2014-11-12 08:35:25 -080011#include "GrInvariantOutput.h"
joshualitteb2a6762014-12-04 11:35:33 -080012#include "SkRect.h"
egdaniel64c47282015-11-13 06:54:19 -080013#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080014#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070015#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080016#include "glsl/GrGLSLUniformHandler.h"
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000017
18//////////////////////////////////////////////////////////////////////////////
19
joshualittb0a8a372014-09-23 09:50:21 -070020class CircleEffect : public GrFragmentProcessor {
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000021public:
joshualittb0a8a372014-09-23 09:50:21 -070022 static GrFragmentProcessor* Create(GrPrimitiveEdgeType, const SkPoint& center, SkScalar radius);
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000023
24 virtual ~CircleEffect() {};
joshualitteb2a6762014-12-04 11:35:33 -080025
mtklein36352bf2015-03-25 18:17:31 -070026 const char* name() const override { return "Circle"; }
joshualitteb2a6762014-12-04 11:35:33 -080027
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000028 const SkPoint& getCenter() const { return fCenter; }
29 SkScalar getRadius() const { return fRadius; }
30
joshualittb0a8a372014-09-23 09:50:21 -070031 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000032
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000033private:
joshualittb0a8a372014-09-23 09:50:21 -070034 CircleEffect(GrPrimitiveEdgeType, const SkPoint& center, SkScalar radius);
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000035
egdaniel57d3b032015-11-13 11:57:27 -080036 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -070037
egdaniel57d3b032015-11-13 11:57:27 -080038 void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
wangyix4b3050b2015-08-04 07:59:37 -070039
mtklein36352bf2015-03-25 18:17:31 -070040 bool onIsEqual(const GrFragmentProcessor&) const override;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000041
mtklein36352bf2015-03-25 18:17:31 -070042 void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
egdaniel1a8ecdf2014-10-03 06:24:12 -070043
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000044 SkPoint fCenter;
45 SkScalar fRadius;
joshualittb0a8a372014-09-23 09:50:21 -070046 GrPrimitiveEdgeType fEdgeType;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000047
joshualittb0a8a372014-09-23 09:50:21 -070048 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000049
joshualittb0a8a372014-09-23 09:50:21 -070050 typedef GrFragmentProcessor INHERITED;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000051};
52
joshualittb0a8a372014-09-23 09:50:21 -070053GrFragmentProcessor* CircleEffect::Create(GrPrimitiveEdgeType edgeType, const SkPoint& center,
54 SkScalar radius) {
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000055 SkASSERT(radius >= 0);
halcanary385fe4d2015-08-26 13:07:48 -070056 return new CircleEffect(edgeType, center, radius);
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000057}
58
egdaniel605dd0f2014-11-12 08:35:25 -080059void CircleEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
joshualitt56995b52014-12-11 15:44:02 -080060 inout->mulByUnknownSingleComponent();
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000061}
62
joshualittb0a8a372014-09-23 09:50:21 -070063CircleEffect::CircleEffect(GrPrimitiveEdgeType edgeType, const SkPoint& c, SkScalar r)
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000064 : fCenter(c)
65 , fRadius(r)
66 , fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -080067 this->initClassID<CircleEffect>();
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000068 this->setWillReadFragmentPosition();
69}
70
bsalomon0e08fc12014-10-15 08:19:04 -070071bool CircleEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -070072 const CircleEffect& ce = other.cast<CircleEffect>();
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +000073 return fEdgeType == ce.fEdgeType && fCenter == ce.fCenter && fRadius == ce.fRadius;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000074}
75
76//////////////////////////////////////////////////////////////////////////////
77
joshualittb0a8a372014-09-23 09:50:21 -070078GR_DEFINE_FRAGMENT_PROCESSOR_TEST(CircleEffect);
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000079
bsalomonc21b09e2015-08-28 18:46:56 -070080const GrFragmentProcessor* CircleEffect::TestCreate(GrProcessorTestData* d) {
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000081 SkPoint center;
joshualitt0067ff52015-07-08 14:26:19 -070082 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);
joshualittb0a8a372014-09-23 09:50:21 -070085 GrPrimitiveEdgeType et;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000086 do {
joshualitt0067ff52015-07-08 14:26:19 -070087 et = (GrPrimitiveEdgeType)d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt);
joshualittb0a8a372014-09-23 09:50:21 -070088 } while (kHairlineAA_GrProcessorEdgeType == et);
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000089 return CircleEffect::Create(et, center, radius);
90}
91
92//////////////////////////////////////////////////////////////////////////////
93
egdaniel64c47282015-11-13 06:54:19 -080094class GLCircleEffect : public GrGLSLFragmentProcessor {
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000095public:
robertphillips9cdb9922016-02-03 12:25:40 -080096 GLCircleEffect() : fPrevRadius(-1.0f) { }
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000097
wangyix7c157a92015-07-22 15:08:53 -070098 virtual void emitCode(EmitArgs&) override;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000099
jvanverthcfc18862015-04-28 08:48:20 -0700100 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000101
wangyixb1daa862015-08-18 11:29:31 -0700102protected:
egdaniel018fb622015-10-28 07:26:40 -0700103 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000104
105private:
egdaniel018fb622015-10-28 07:26:40 -0700106 GrGLSLProgramDataManager::UniformHandle fCircleUniform;
107 SkPoint fPrevCenter;
108 SkScalar fPrevRadius;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000109
egdaniel64c47282015-11-13 06:54:19 -0800110 typedef GrGLSLFragmentProcessor INHERITED;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000111};
112
wangyix7c157a92015-07-22 15:08:53 -0700113void GLCircleEffect::emitCode(EmitArgs& args) {
114 const CircleEffect& ce = args.fFp.cast<CircleEffect>();
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000115 const char *circleName;
bsalomonfcabe422015-04-28 12:43:01 -0700116 // 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.
egdaniel7ea439b2015-12-03 09:20:44 -0800118 fCircleUniform = args.fUniformHandler->addUniform(GrGLSLUniformHandler::kFragment_Visibility,
119 kVec4f_GrSLType, kDefault_GrSLPrecision,
120 "circle",
121 &circleName);
joshualitt30ba4362014-08-21 20:18:45 -0700122
egdaniel4ca2e602015-11-18 08:01:26 -0800123 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
124 const char* fragmentPos = fragBuilder->fragmentPosition();
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000125
joshualittb0a8a372014-09-23 09:50:21 -0700126 SkASSERT(kHairlineAA_GrProcessorEdgeType != ce.getEdgeType());
bsalomonfcabe422015-04-28 12:43:01 -0700127 // 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.
joshualittb0a8a372014-09-23 09:50:21 -0700131 if (GrProcessorEdgeTypeIsInverseFill(ce.getEdgeType())) {
bsalomone87256c42015-12-09 17:14:40 -0800132 fragBuilder->codeAppendf("float d = (length((%s.xy - %s.xy) * %s.w) - 1.0) * %s.z;",
egdaniel4ca2e602015-11-18 08:01:26 -0800133 circleName, fragmentPos, circleName, circleName);
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000134 } else {
bsalomone87256c42015-12-09 17:14:40 -0800135 fragBuilder->codeAppendf("float d = (1.0 - length((%s.xy - %s.xy) * %s.w)) * %s.z;",
egdaniel4ca2e602015-11-18 08:01:26 -0800136 circleName, fragmentPos, circleName, circleName);
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000137 }
joshualittb0a8a372014-09-23 09:50:21 -0700138 if (GrProcessorEdgeTypeIsAA(ce.getEdgeType())) {
bsalomone87256c42015-12-09 17:14:40 -0800139 fragBuilder->codeAppend("d = clamp(d, 0.0, 1.0);");
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000140 } else {
bsalomone87256c42015-12-09 17:14:40 -0800141 fragBuilder->codeAppend("d = d > 0.5 ? 1.0 : 0.0;");
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000142 }
143
bsalomone87256c42015-12-09 17:14:40 -0800144 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor,
egdaniel4ca2e602015-11-18 08:01:26 -0800145 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("d")).c_str());
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000146}
147
jvanverthcfc18862015-04-28 08:48:20 -0700148void GLCircleEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700149 GrProcessorKeyBuilder* b) {
150 const CircleEffect& ce = processor.cast<CircleEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700151 b->add32(ce.getEdgeType());
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000152}
153
egdaniel018fb622015-10-28 07:26:40 -0700154void GLCircleEffect::onSetData(const GrGLSLProgramDataManager& pdman,
155 const GrProcessor& processor) {
joshualittb0a8a372014-09-23 09:50:21 -0700156 const CircleEffect& ce = processor.cast<CircleEffect>();
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000157 if (ce.getRadius() != fPrevRadius || ce.getCenter() != fPrevCenter) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000158 SkScalar radius = ce.getRadius();
joshualittb0a8a372014-09-23 09:50:21 -0700159 if (GrProcessorEdgeTypeIsInverseFill(ce.getEdgeType())) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000160 radius -= 0.5f;
161 } else {
162 radius += 0.5f;
163 }
bsalomonfcabe422015-04-28 12:43:01 -0700164 pdman.set4f(fCircleUniform, ce.getCenter().fX, ce.getCenter().fY, radius,
165 SkScalarInvert(radius));
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000166 fPrevCenter = ce.getCenter();
167 fPrevRadius = ce.getRadius();
168 }
169}
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000170
joshualitteb2a6762014-12-04 11:35:33 -0800171///////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000172
egdaniel57d3b032015-11-13 11:57:27 -0800173void CircleEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
174 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800175 GLCircleEffect::GenKey(*this, caps, b);
176}
177
egdaniel57d3b032015-11-13 11:57:27 -0800178GrGLSLFragmentProcessor* CircleEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -0800179 return new GLCircleEffect;
joshualitteb2a6762014-12-04 11:35:33 -0800180}
181
182//////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000183
joshualittb0a8a372014-09-23 09:50:21 -0700184class EllipseEffect : public GrFragmentProcessor {
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000185public:
joshualittb0a8a372014-09-23 09:50:21 -0700186 static GrFragmentProcessor* Create(GrPrimitiveEdgeType, const SkPoint& center, SkScalar rx,
187 SkScalar ry);
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000188
189 virtual ~EllipseEffect() {};
joshualitteb2a6762014-12-04 11:35:33 -0800190
mtklein36352bf2015-03-25 18:17:31 -0700191 const char* name() const override { return "Ellipse"; }
joshualitteb2a6762014-12-04 11:35:33 -0800192
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000193 const SkPoint& getCenter() const { return fCenter; }
194 SkVector getRadii() const { return fRadii; }
195
joshualittb0a8a372014-09-23 09:50:21 -0700196 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000197
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000198private:
joshualittb0a8a372014-09-23 09:50:21 -0700199 EllipseEffect(GrPrimitiveEdgeType, const SkPoint& center, SkScalar rx, SkScalar ry);
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000200
egdaniel57d3b032015-11-13 11:57:27 -0800201 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -0700202
egdaniel57d3b032015-11-13 11:57:27 -0800203 void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
wangyix4b3050b2015-08-04 07:59:37 -0700204
mtklein36352bf2015-03-25 18:17:31 -0700205 bool onIsEqual(const GrFragmentProcessor&) const override;
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000206
mtklein36352bf2015-03-25 18:17:31 -0700207 void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700208
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000209 SkPoint fCenter;
210 SkVector fRadii;
joshualittb0a8a372014-09-23 09:50:21 -0700211 GrPrimitiveEdgeType fEdgeType;
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000212
joshualittb0a8a372014-09-23 09:50:21 -0700213 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000214
joshualittb0a8a372014-09-23 09:50:21 -0700215 typedef GrFragmentProcessor INHERITED;
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000216};
217
joshualittb0a8a372014-09-23 09:50:21 -0700218GrFragmentProcessor* EllipseEffect::Create(GrPrimitiveEdgeType edgeType,
219 const SkPoint& center,
220 SkScalar rx,
221 SkScalar ry) {
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000222 SkASSERT(rx >= 0 && ry >= 0);
halcanary385fe4d2015-08-26 13:07:48 -0700223 return new EllipseEffect(edgeType, center, rx, ry);
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000224}
225
egdaniel605dd0f2014-11-12 08:35:25 -0800226void EllipseEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
joshualitt56995b52014-12-11 15:44:02 -0800227 inout->mulByUnknownSingleComponent();
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000228}
229
joshualittb0a8a372014-09-23 09:50:21 -0700230EllipseEffect::EllipseEffect(GrPrimitiveEdgeType edgeType, const SkPoint& c, SkScalar rx, SkScalar ry)
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000231 : fCenter(c)
232 , fRadii(SkVector::Make(rx, ry))
233 , fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800234 this->initClassID<EllipseEffect>();
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000235 this->setWillReadFragmentPosition();
236}
237
bsalomon0e08fc12014-10-15 08:19:04 -0700238bool EllipseEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700239 const EllipseEffect& ee = other.cast<EllipseEffect>();
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000240 return fEdgeType == ee.fEdgeType && fCenter == ee.fCenter && fRadii == ee.fRadii;
241}
242
243//////////////////////////////////////////////////////////////////////////////
244
joshualittb0a8a372014-09-23 09:50:21 -0700245GR_DEFINE_FRAGMENT_PROCESSOR_TEST(EllipseEffect);
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000246
bsalomonc21b09e2015-08-28 18:46:56 -0700247const GrFragmentProcessor* EllipseEffect::TestCreate(GrProcessorTestData* d) {
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000248 SkPoint center;
joshualitt0067ff52015-07-08 14:26:19 -0700249 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);
joshualittb0a8a372014-09-23 09:50:21 -0700253 GrPrimitiveEdgeType et;
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000254 do {
joshualitt0067ff52015-07-08 14:26:19 -0700255 et = (GrPrimitiveEdgeType)d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt);
joshualittb0a8a372014-09-23 09:50:21 -0700256 } while (kHairlineAA_GrProcessorEdgeType == et);
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000257 return EllipseEffect::Create(et, center, rx, ry);
258}
259
260//////////////////////////////////////////////////////////////////////////////
261
egdaniel64c47282015-11-13 06:54:19 -0800262class GLEllipseEffect : public GrGLSLFragmentProcessor {
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000263public:
robertphillips9cdb9922016-02-03 12:25:40 -0800264 GLEllipseEffect() {
265 fPrevRadii.fX = -1.0f;
266 }
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000267
robertphillips9cdb9922016-02-03 12:25:40 -0800268 void emitCode(EmitArgs&) override;
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000269
jvanverthcfc18862015-04-28 08:48:20 -0700270 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000271
wangyixb1daa862015-08-18 11:29:31 -0700272protected:
egdaniel018fb622015-10-28 07:26:40 -0700273 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000274
275private:
egdaniel018fb622015-10-28 07:26:40 -0700276 GrGLSLProgramDataManager::UniformHandle fEllipseUniform;
bsalomone87256c42015-12-09 17:14:40 -0800277 GrGLSLProgramDataManager::UniformHandle fScaleUniform;
egdaniel018fb622015-10-28 07:26:40 -0700278 SkPoint fPrevCenter;
279 SkVector fPrevRadii;
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000280
egdaniel64c47282015-11-13 06:54:19 -0800281 typedef GrGLSLFragmentProcessor INHERITED;
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000282};
283
wangyix7c157a92015-07-22 15:08:53 -0700284void GLEllipseEffect::emitCode(EmitArgs& args) {
285 const EllipseEffect& ee = args.fFp.cast<EllipseEffect>();
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000286 const char *ellipseName;
287 // The ellipse uniform is (center.x, center.y, 1 / rx^2, 1 / ry^2)
bsalomonc41f4d62015-08-03 14:23:03 -0700288 // The last two terms can underflow on mediump, so we use highp.
egdaniel7ea439b2015-12-03 09:20:44 -0800289 fEllipseUniform = args.fUniformHandler->addUniform(GrGLSLUniformHandler::kFragment_Visibility,
290 kVec4f_GrSLType, kHigh_GrSLPrecision,
291 "ellipse",
292 &ellipseName);
bsalomone87256c42015-12-09 17:14:40 -0800293 // 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 }
joshualitt30ba4362014-08-21 20:18:45 -0700303
egdaniel4ca2e602015-11-18 08:01:26 -0800304 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
305 const char* fragmentPos = fragBuilder->fragmentPosition();
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000306
307 // d is the offset to the ellipse center
bsalomone87256c42015-12-09 17:14:40 -0800308 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.orgd0a50292014-04-02 15:00:39 +0000313 // implicit is the evaluation of (x/rx)^2 + (y/ry)^2 - 1.
bsalomone87256c42015-12-09 17:14:40 -0800314 fragBuilder->codeAppend("float implicit = dot(Z, d) - 1.0;");
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000315 // grad_dot is the squared length of the gradient of the implicit.
bsalomone87256c42015-12-09 17:14:40 -0800316 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.orgd0a50292014-04-02 15:00:39 +0000323
324 switch (ee.getEdgeType()) {
joshualittb0a8a372014-09-23 09:50:21 -0700325 case kFillAA_GrProcessorEdgeType:
bsalomone87256c42015-12-09 17:14:40 -0800326 fragBuilder->codeAppend("float alpha = clamp(0.5 - approx_dist, 0.0, 1.0);");
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000327 break;
joshualittb0a8a372014-09-23 09:50:21 -0700328 case kInverseFillAA_GrProcessorEdgeType:
bsalomone87256c42015-12-09 17:14:40 -0800329 fragBuilder->codeAppend("float alpha = clamp(0.5 + approx_dist, 0.0, 1.0);");
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000330 break;
joshualittb0a8a372014-09-23 09:50:21 -0700331 case kFillBW_GrProcessorEdgeType:
bsalomone87256c42015-12-09 17:14:40 -0800332 fragBuilder->codeAppend("float alpha = approx_dist > 0.0 ? 0.0 : 1.0;");
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000333 break;
joshualittb0a8a372014-09-23 09:50:21 -0700334 case kInverseFillBW_GrProcessorEdgeType:
bsalomone87256c42015-12-09 17:14:40 -0800335 fragBuilder->codeAppend("float alpha = approx_dist > 0.0 ? 1.0 : 0.0;");
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000336 break;
joshualittb0a8a372014-09-23 09:50:21 -0700337 case kHairlineAA_GrProcessorEdgeType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000338 SkFAIL("Hairline not expected here.");
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000339 }
340
bsalomone87256c42015-12-09 17:14:40 -0800341 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor,
egdaniel4ca2e602015-11-18 08:01:26 -0800342 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha")).c_str());
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000343}
344
jvanverthcfc18862015-04-28 08:48:20 -0700345void GLEllipseEffect::GenKey(const GrProcessor& effect, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700346 GrProcessorKeyBuilder* b) {
joshualitt49586be2014-09-16 08:21:41 -0700347 const EllipseEffect& ee = effect.cast<EllipseEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700348 b->add32(ee.getEdgeType());
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000349}
350
egdaniel018fb622015-10-28 07:26:40 -0700351void GLEllipseEffect::onSetData(const GrGLSLProgramDataManager& pdman,
352 const GrProcessor& effect) {
joshualitt49586be2014-09-16 08:21:41 -0700353 const EllipseEffect& ee = effect.cast<EllipseEffect>();
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000354 if (ee.getRadii() != fPrevRadii || ee.getCenter() != fPrevCenter) {
bsalomone87256c42015-12-09 17:14:40 -0800355 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 }
kkinnunen7510b222014-07-30 00:04:16 -0700375 pdman.set4f(fEllipseUniform, ee.getCenter().fX, ee.getCenter().fY, invRXSqd, invRYSqd);
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000376 fPrevCenter = ee.getCenter();
377 fPrevRadii = ee.getRadii();
378 }
379}
380
joshualitteb2a6762014-12-04 11:35:33 -0800381///////////////////////////////////////////////////////////////////////////////////////////////////
382
egdaniel57d3b032015-11-13 11:57:27 -0800383void EllipseEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
384 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800385 GLEllipseEffect::GenKey(*this, caps, b);
386}
387
egdaniel57d3b032015-11-13 11:57:27 -0800388GrGLSLFragmentProcessor* EllipseEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -0800389 return new GLEllipseEffect;
joshualitteb2a6762014-12-04 11:35:33 -0800390}
391
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000392//////////////////////////////////////////////////////////////////////////////
393
joshualittb0a8a372014-09-23 09:50:21 -0700394GrFragmentProcessor* GrOvalEffect::Create(GrPrimitiveEdgeType edgeType, const SkRect& oval) {
395 if (kHairlineAA_GrProcessorEdgeType == edgeType) {
halcanary96fcdcc2015-08-27 07:41:13 -0700396 return nullptr;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000397 }
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.orgd0a50292014-04-02 15:00:39 +0000403 } 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.org3eedb802014-03-28 15:58:31 +0000407 }
408
halcanary96fcdcc2015-08-27 07:41:13 -0700409 return nullptr;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000410}