blob: d387b67f83a850006728fd69259432733ddace46 [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"
joshualittb0a8a372014-09-23 09:50:21 -070013#include "gl/GrGLProcessor.h"
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000014#include "gl/GrGLSL.h"
joshualitteb2a6762014-12-04 11:35:33 -080015#include "gl/builders/GrGLProgramBuilder.h"
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000016
17//////////////////////////////////////////////////////////////////////////////
18
joshualittb0a8a372014-09-23 09:50:21 -070019class CircleEffect : public GrFragmentProcessor {
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000020public:
joshualittb0a8a372014-09-23 09:50:21 -070021 static GrFragmentProcessor* Create(GrPrimitiveEdgeType, const SkPoint& center, SkScalar radius);
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000022
23 virtual ~CircleEffect() {};
joshualitteb2a6762014-12-04 11:35:33 -080024
mtklein36352bf2015-03-25 18:17:31 -070025 const char* name() const override { return "Circle"; }
joshualitteb2a6762014-12-04 11:35:33 -080026
jvanverthcfc18862015-04-28 08:48:20 -070027 void getGLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
joshualitteb2a6762014-12-04 11:35:33 -080028
mtklein36352bf2015-03-25 18:17:31 -070029 GrGLFragmentProcessor* createGLInstance() const override;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000030
31 const SkPoint& getCenter() const { return fCenter; }
32 SkScalar getRadius() const { return fRadius; }
33
joshualittb0a8a372014-09-23 09:50:21 -070034 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000035
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000036private:
joshualittb0a8a372014-09-23 09:50:21 -070037 CircleEffect(GrPrimitiveEdgeType, const SkPoint& center, SkScalar radius);
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000038
mtklein36352bf2015-03-25 18:17:31 -070039 bool onIsEqual(const GrFragmentProcessor&) const override;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000040
mtklein36352bf2015-03-25 18:17:31 -070041 void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
egdaniel1a8ecdf2014-10-03 06:24:12 -070042
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000043 SkPoint fCenter;
44 SkScalar fRadius;
joshualittb0a8a372014-09-23 09:50:21 -070045 GrPrimitiveEdgeType fEdgeType;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000046
joshualittb0a8a372014-09-23 09:50:21 -070047 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000048
joshualittb0a8a372014-09-23 09:50:21 -070049 typedef GrFragmentProcessor INHERITED;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000050};
51
joshualittb0a8a372014-09-23 09:50:21 -070052GrFragmentProcessor* CircleEffect::Create(GrPrimitiveEdgeType edgeType, const SkPoint& center,
53 SkScalar radius) {
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000054 SkASSERT(radius >= 0);
bsalomon55fad7a2014-07-08 07:34:20 -070055 return SkNEW_ARGS(CircleEffect, (edgeType, center, radius));
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000056}
57
egdaniel605dd0f2014-11-12 08:35:25 -080058void CircleEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
joshualitt56995b52014-12-11 15:44:02 -080059 inout->mulByUnknownSingleComponent();
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000060}
61
joshualittb0a8a372014-09-23 09:50:21 -070062CircleEffect::CircleEffect(GrPrimitiveEdgeType edgeType, const SkPoint& c, SkScalar r)
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000063 : fCenter(c)
64 , fRadius(r)
65 , fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -080066 this->initClassID<CircleEffect>();
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000067 this->setWillReadFragmentPosition();
68}
69
bsalomon0e08fc12014-10-15 08:19:04 -070070bool CircleEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -070071 const CircleEffect& ce = other.cast<CircleEffect>();
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +000072 return fEdgeType == ce.fEdgeType && fCenter == ce.fCenter && fRadius == ce.fRadius;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000073}
74
75//////////////////////////////////////////////////////////////////////////////
76
joshualittb0a8a372014-09-23 09:50:21 -070077GR_DEFINE_FRAGMENT_PROCESSOR_TEST(CircleEffect);
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000078
joshualittb0a8a372014-09-23 09:50:21 -070079GrFragmentProcessor* CircleEffect::TestCreate(SkRandom* random,
80 GrContext*,
81 const GrDrawTargetCaps& caps,
82 GrTexture*[]) {
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000083 SkPoint center;
84 center.fX = random->nextRangeScalar(0.f, 1000.f);
85 center.fY = random->nextRangeScalar(0.f, 1000.f);
86 SkScalar radius = random->nextRangeF(0.f, 1000.f);
joshualittb0a8a372014-09-23 09:50:21 -070087 GrPrimitiveEdgeType et;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000088 do {
joshualittb0a8a372014-09-23 09:50:21 -070089 et = (GrPrimitiveEdgeType)random->nextULessThan(kGrProcessorEdgeTypeCnt);
90 } while (kHairlineAA_GrProcessorEdgeType == et);
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000091 return CircleEffect::Create(et, center, radius);
92}
93
94//////////////////////////////////////////////////////////////////////////////
95
joshualittb0a8a372014-09-23 09:50:21 -070096class GLCircleEffect : public GrGLFragmentProcessor {
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000097public:
joshualitteb2a6762014-12-04 11:35:33 -080098 GLCircleEffect(const GrProcessor&);
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000099
joshualitt15988992014-10-09 15:04:05 -0700100 virtual void emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700101 const GrFragmentProcessor& fp,
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000102 const char* outputColor,
103 const char* inputColor,
104 const TransformedCoordsArray&,
mtklein36352bf2015-03-25 18:17:31 -0700105 const TextureSamplerArray&) override;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000106
jvanverthcfc18862015-04-28 08:48:20 -0700107 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000108
mtklein36352bf2015-03-25 18:17:31 -0700109 void setData(const GrGLProgramDataManager&, const GrProcessor&) override;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000110
111private:
kkinnunen7510b222014-07-30 00:04:16 -0700112 GrGLProgramDataManager::UniformHandle fCircleUniform;
113 SkPoint fPrevCenter;
114 SkScalar fPrevRadius;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000115
joshualittb0a8a372014-09-23 09:50:21 -0700116 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000117};
118
joshualitteb2a6762014-12-04 11:35:33 -0800119GLCircleEffect::GLCircleEffect(const GrProcessor&) {
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000120 fPrevRadius = -1.f;
121}
122
joshualitt15988992014-10-09 15:04:05 -0700123void GLCircleEffect::emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700124 const GrFragmentProcessor& fp,
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000125 const char* outputColor,
126 const char* inputColor,
127 const TransformedCoordsArray&,
128 const TextureSamplerArray& samplers) {
joshualittb0a8a372014-09-23 09:50:21 -0700129 const CircleEffect& ce = fp.cast<CircleEffect>();
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000130 const char *circleName;
bsalomonfcabe422015-04-28 12:43:01 -0700131 // The circle uniform is (center.x, center.y, radius + 0.5, 1 / (radius + 0.5)) for regular
132 // fills and (..., radius - 0.5, 1 / (radius - 0.5)) for inverse fills.
joshualitt30ba4362014-08-21 20:18:45 -0700133 fCircleUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomonfcabe422015-04-28 12:43:01 -0700134 kVec4f_GrSLType, kDefault_GrSLPrecision,
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000135 "circle",
136 &circleName);
joshualitt30ba4362014-08-21 20:18:45 -0700137
egdaniel29bee0f2015-04-29 11:54:42 -0700138 GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700139 const char* fragmentPos = fsBuilder->fragmentPosition();
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000140
joshualittb0a8a372014-09-23 09:50:21 -0700141 SkASSERT(kHairlineAA_GrProcessorEdgeType != ce.getEdgeType());
bsalomonfcabe422015-04-28 12:43:01 -0700142 // TODO: Right now the distance to circle caclulation is performed in a space normalized to the
143 // radius and then denormalized. This is to prevent overflow on devices that have a "real"
144 // mediump. It'd be nice to only to this on mediump devices but we currently don't have the
145 // caps here.
joshualittb0a8a372014-09-23 09:50:21 -0700146 if (GrProcessorEdgeTypeIsInverseFill(ce.getEdgeType())) {
bsalomonfcabe422015-04-28 12:43:01 -0700147 fsBuilder->codeAppendf("\t\tfloat d = (length((%s.xy - %s.xy) * %s.w) - 1.0) * %s.z;\n",
148 circleName, fragmentPos, circleName, circleName);
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000149 } else {
bsalomonfcabe422015-04-28 12:43:01 -0700150 fsBuilder->codeAppendf("\t\tfloat d = (1.0 - length((%s.xy - %s.xy) * %s.w)) * %s.z;\n",
151 circleName, fragmentPos, circleName, circleName);
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000152 }
joshualittb0a8a372014-09-23 09:50:21 -0700153 if (GrProcessorEdgeTypeIsAA(ce.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700154 fsBuilder->codeAppend("\t\td = clamp(d, 0.0, 1.0);\n");
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000155 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700156 fsBuilder->codeAppend("\t\td = d > 0.5 ? 1.0 : 0.0;\n");
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000157 }
158
joshualitt30ba4362014-08-21 20:18:45 -0700159 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor,
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000160 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("d")).c_str());
161}
162
jvanverthcfc18862015-04-28 08:48:20 -0700163void GLCircleEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700164 GrProcessorKeyBuilder* b) {
165 const CircleEffect& ce = processor.cast<CircleEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700166 b->add32(ce.getEdgeType());
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000167}
168
joshualittb0a8a372014-09-23 09:50:21 -0700169void GLCircleEffect::setData(const GrGLProgramDataManager& pdman, const GrProcessor& processor) {
170 const CircleEffect& ce = processor.cast<CircleEffect>();
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000171 if (ce.getRadius() != fPrevRadius || ce.getCenter() != fPrevCenter) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000172 SkScalar radius = ce.getRadius();
joshualittb0a8a372014-09-23 09:50:21 -0700173 if (GrProcessorEdgeTypeIsInverseFill(ce.getEdgeType())) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000174 radius -= 0.5f;
175 } else {
176 radius += 0.5f;
177 }
bsalomonfcabe422015-04-28 12:43:01 -0700178 pdman.set4f(fCircleUniform, ce.getCenter().fX, ce.getCenter().fY, radius,
179 SkScalarInvert(radius));
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000180 fPrevCenter = ce.getCenter();
181 fPrevRadius = ce.getRadius();
182 }
183}
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000184
joshualitteb2a6762014-12-04 11:35:33 -0800185///////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000186
jvanverthcfc18862015-04-28 08:48:20 -0700187void CircleEffect::getGLProcessorKey(const GrGLSLCaps& caps,
joshualitteb2a6762014-12-04 11:35:33 -0800188 GrProcessorKeyBuilder* b) const {
189 GLCircleEffect::GenKey(*this, caps, b);
190}
191
192GrGLFragmentProcessor* CircleEffect::createGLInstance() const {
193 return SkNEW_ARGS(GLCircleEffect, (*this));
194}
195
196//////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000197
joshualittb0a8a372014-09-23 09:50:21 -0700198class EllipseEffect : public GrFragmentProcessor {
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000199public:
joshualittb0a8a372014-09-23 09:50:21 -0700200 static GrFragmentProcessor* Create(GrPrimitiveEdgeType, const SkPoint& center, SkScalar rx,
201 SkScalar ry);
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000202
203 virtual ~EllipseEffect() {};
joshualitteb2a6762014-12-04 11:35:33 -0800204
mtklein36352bf2015-03-25 18:17:31 -0700205 const char* name() const override { return "Ellipse"; }
joshualitteb2a6762014-12-04 11:35:33 -0800206
jvanverthcfc18862015-04-28 08:48:20 -0700207 void getGLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
joshualitteb2a6762014-12-04 11:35:33 -0800208
mtklein36352bf2015-03-25 18:17:31 -0700209 GrGLFragmentProcessor* createGLInstance() const override;
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000210
211 const SkPoint& getCenter() const { return fCenter; }
212 SkVector getRadii() const { return fRadii; }
213
joshualittb0a8a372014-09-23 09:50:21 -0700214 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000215
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000216private:
joshualittb0a8a372014-09-23 09:50:21 -0700217 EllipseEffect(GrPrimitiveEdgeType, const SkPoint& center, SkScalar rx, SkScalar ry);
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000218
mtklein36352bf2015-03-25 18:17:31 -0700219 bool onIsEqual(const GrFragmentProcessor&) const override;
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000220
mtklein36352bf2015-03-25 18:17:31 -0700221 void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700222
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000223 SkPoint fCenter;
224 SkVector fRadii;
joshualittb0a8a372014-09-23 09:50:21 -0700225 GrPrimitiveEdgeType fEdgeType;
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000226
joshualittb0a8a372014-09-23 09:50:21 -0700227 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000228
joshualittb0a8a372014-09-23 09:50:21 -0700229 typedef GrFragmentProcessor INHERITED;
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000230};
231
joshualittb0a8a372014-09-23 09:50:21 -0700232GrFragmentProcessor* EllipseEffect::Create(GrPrimitiveEdgeType edgeType,
233 const SkPoint& center,
234 SkScalar rx,
235 SkScalar ry) {
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000236 SkASSERT(rx >= 0 && ry >= 0);
bsalomon55fad7a2014-07-08 07:34:20 -0700237 return SkNEW_ARGS(EllipseEffect, (edgeType, center, rx, ry));
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000238}
239
egdaniel605dd0f2014-11-12 08:35:25 -0800240void EllipseEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
joshualitt56995b52014-12-11 15:44:02 -0800241 inout->mulByUnknownSingleComponent();
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000242}
243
joshualittb0a8a372014-09-23 09:50:21 -0700244EllipseEffect::EllipseEffect(GrPrimitiveEdgeType edgeType, const SkPoint& c, SkScalar rx, SkScalar ry)
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000245 : fCenter(c)
246 , fRadii(SkVector::Make(rx, ry))
247 , fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800248 this->initClassID<EllipseEffect>();
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000249 this->setWillReadFragmentPosition();
250}
251
bsalomon0e08fc12014-10-15 08:19:04 -0700252bool EllipseEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700253 const EllipseEffect& ee = other.cast<EllipseEffect>();
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000254 return fEdgeType == ee.fEdgeType && fCenter == ee.fCenter && fRadii == ee.fRadii;
255}
256
257//////////////////////////////////////////////////////////////////////////////
258
joshualittb0a8a372014-09-23 09:50:21 -0700259GR_DEFINE_FRAGMENT_PROCESSOR_TEST(EllipseEffect);
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000260
joshualittb0a8a372014-09-23 09:50:21 -0700261GrFragmentProcessor* EllipseEffect::TestCreate(SkRandom* random,
262 GrContext*,
263 const GrDrawTargetCaps& caps,
264 GrTexture*[]) {
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000265 SkPoint center;
266 center.fX = random->nextRangeScalar(0.f, 1000.f);
267 center.fY = random->nextRangeScalar(0.f, 1000.f);
268 SkScalar rx = random->nextRangeF(0.f, 1000.f);
269 SkScalar ry = random->nextRangeF(0.f, 1000.f);
joshualittb0a8a372014-09-23 09:50:21 -0700270 GrPrimitiveEdgeType et;
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000271 do {
joshualittb0a8a372014-09-23 09:50:21 -0700272 et = (GrPrimitiveEdgeType)random->nextULessThan(kGrProcessorEdgeTypeCnt);
273 } while (kHairlineAA_GrProcessorEdgeType == et);
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000274 return EllipseEffect::Create(et, center, rx, ry);
275}
276
277//////////////////////////////////////////////////////////////////////////////
278
joshualittb0a8a372014-09-23 09:50:21 -0700279class GLEllipseEffect : public GrGLFragmentProcessor {
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000280public:
joshualitteb2a6762014-12-04 11:35:33 -0800281 GLEllipseEffect(const GrProcessor&);
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000282
joshualitt15988992014-10-09 15:04:05 -0700283 virtual void emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700284 const GrFragmentProcessor& fp,
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000285 const char* outputColor,
286 const char* inputColor,
287 const TransformedCoordsArray&,
mtklein36352bf2015-03-25 18:17:31 -0700288 const TextureSamplerArray&) override;
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000289
jvanverthcfc18862015-04-28 08:48:20 -0700290 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000291
mtklein36352bf2015-03-25 18:17:31 -0700292 void setData(const GrGLProgramDataManager&, const GrProcessor&) override;
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000293
294private:
kkinnunen7510b222014-07-30 00:04:16 -0700295 GrGLProgramDataManager::UniformHandle fEllipseUniform;
296 SkPoint fPrevCenter;
297 SkVector fPrevRadii;
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000298
joshualittb0a8a372014-09-23 09:50:21 -0700299 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000300};
301
joshualitteb2a6762014-12-04 11:35:33 -0800302GLEllipseEffect::GLEllipseEffect(const GrProcessor& effect) {
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000303 fPrevRadii.fX = -1.f;
304}
305
joshualitt15988992014-10-09 15:04:05 -0700306void GLEllipseEffect::emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700307 const GrFragmentProcessor& fp,
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000308 const char* outputColor,
309 const char* inputColor,
310 const TransformedCoordsArray&,
311 const TextureSamplerArray& samplers) {
joshualittb0a8a372014-09-23 09:50:21 -0700312 const EllipseEffect& ee = fp.cast<EllipseEffect>();
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000313 const char *ellipseName;
314 // The ellipse uniform is (center.x, center.y, 1 / rx^2, 1 / ry^2)
joshualitt30ba4362014-08-21 20:18:45 -0700315 fEllipseUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800316 kVec4f_GrSLType, kDefault_GrSLPrecision,
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000317 "ellipse",
318 &ellipseName);
joshualitt30ba4362014-08-21 20:18:45 -0700319
egdaniel29bee0f2015-04-29 11:54:42 -0700320 GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700321 const char* fragmentPos = fsBuilder->fragmentPosition();
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000322
323 // d is the offset to the ellipse center
joshualitt30ba4362014-08-21 20:18:45 -0700324 fsBuilder->codeAppendf("\t\tvec2 d = %s.xy - %s.xy;\n", fragmentPos, ellipseName);
325 fsBuilder->codeAppendf("\t\tvec2 Z = d * %s.zw;\n", ellipseName);
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000326 // implicit is the evaluation of (x/rx)^2 + (y/ry)^2 - 1.
joshualitt30ba4362014-08-21 20:18:45 -0700327 fsBuilder->codeAppend("\t\tfloat implicit = dot(Z, d) - 1.0;\n");
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000328 // grad_dot is the squared length of the gradient of the implicit.
joshualitt30ba4362014-08-21 20:18:45 -0700329 fsBuilder->codeAppendf("\t\tfloat grad_dot = 4.0 * dot(Z, Z);\n");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000330 // avoid calling inversesqrt on zero.
joshualitt30ba4362014-08-21 20:18:45 -0700331 fsBuilder->codeAppend("\t\tgrad_dot = max(grad_dot, 1.0e-4);\n");
332 fsBuilder->codeAppendf("\t\tfloat approx_dist = implicit * inversesqrt(grad_dot);\n");
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000333
334 switch (ee.getEdgeType()) {
joshualittb0a8a372014-09-23 09:50:21 -0700335 case kFillAA_GrProcessorEdgeType:
joshualitt30ba4362014-08-21 20:18:45 -0700336 fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 - approx_dist, 0.0, 1.0);\n");
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000337 break;
joshualittb0a8a372014-09-23 09:50:21 -0700338 case kInverseFillAA_GrProcessorEdgeType:
joshualitt30ba4362014-08-21 20:18:45 -0700339 fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 + approx_dist, 0.0, 1.0);\n");
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000340 break;
joshualittb0a8a372014-09-23 09:50:21 -0700341 case kFillBW_GrProcessorEdgeType:
joshualitt30ba4362014-08-21 20:18:45 -0700342 fsBuilder->codeAppend("\t\tfloat alpha = approx_dist > 0.0 ? 0.0 : 1.0;\n");
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000343 break;
joshualittb0a8a372014-09-23 09:50:21 -0700344 case kInverseFillBW_GrProcessorEdgeType:
joshualitt30ba4362014-08-21 20:18:45 -0700345 fsBuilder->codeAppend("\t\tfloat alpha = approx_dist > 0.0 ? 1.0 : 0.0;\n");
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000346 break;
joshualittb0a8a372014-09-23 09:50:21 -0700347 case kHairlineAA_GrProcessorEdgeType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000348 SkFAIL("Hairline not expected here.");
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000349 }
350
joshualitt30ba4362014-08-21 20:18:45 -0700351 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor,
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000352 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
353}
354
jvanverthcfc18862015-04-28 08:48:20 -0700355void GLEllipseEffect::GenKey(const GrProcessor& effect, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700356 GrProcessorKeyBuilder* b) {
joshualitt49586be2014-09-16 08:21:41 -0700357 const EllipseEffect& ee = effect.cast<EllipseEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700358 b->add32(ee.getEdgeType());
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000359}
360
joshualittb0a8a372014-09-23 09:50:21 -0700361void GLEllipseEffect::setData(const GrGLProgramDataManager& pdman, const GrProcessor& effect) {
joshualitt49586be2014-09-16 08:21:41 -0700362 const EllipseEffect& ee = effect.cast<EllipseEffect>();
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000363 if (ee.getRadii() != fPrevRadii || ee.getCenter() != fPrevCenter) {
364 SkScalar invRXSqd = 1.f / (ee.getRadii().fX * ee.getRadii().fX);
365 SkScalar invRYSqd = 1.f / (ee.getRadii().fY * ee.getRadii().fY);
kkinnunen7510b222014-07-30 00:04:16 -0700366 pdman.set4f(fEllipseUniform, ee.getCenter().fX, ee.getCenter().fY, invRXSqd, invRYSqd);
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000367 fPrevCenter = ee.getCenter();
368 fPrevRadii = ee.getRadii();
369 }
370}
371
joshualitteb2a6762014-12-04 11:35:33 -0800372///////////////////////////////////////////////////////////////////////////////////////////////////
373
jvanverthcfc18862015-04-28 08:48:20 -0700374void EllipseEffect::getGLProcessorKey(const GrGLSLCaps& caps,
joshualitteb2a6762014-12-04 11:35:33 -0800375 GrProcessorKeyBuilder* b) const {
376 GLEllipseEffect::GenKey(*this, caps, b);
377}
378
379GrGLFragmentProcessor* EllipseEffect::createGLInstance() const {
380 return SkNEW_ARGS(GLEllipseEffect, (*this));
381}
382
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000383//////////////////////////////////////////////////////////////////////////////
384
joshualittb0a8a372014-09-23 09:50:21 -0700385GrFragmentProcessor* GrOvalEffect::Create(GrPrimitiveEdgeType edgeType, const SkRect& oval) {
386 if (kHairlineAA_GrProcessorEdgeType == edgeType) {
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000387 return NULL;
388 }
389 SkScalar w = oval.width();
390 SkScalar h = oval.height();
391 if (SkScalarNearlyEqual(w, h)) {
392 w /= 2;
393 return CircleEffect::Create(edgeType, SkPoint::Make(oval.fLeft + w, oval.fTop + w), w);
commit-bot@chromium.orgd0a50292014-04-02 15:00:39 +0000394 } else {
395 w /= 2;
396 h /= 2;
397 return EllipseEffect::Create(edgeType, SkPoint::Make(oval.fLeft + w, oval.fTop + h), w, h);
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000398 }
399
400 return NULL;
401}