blob: 69c5862aa3b796fee47414b97b413a9e66db646f [file] [log] [blame]
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +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 "GrConvexPolyEffect.h"
egdaniel605dd0f2014-11-12 08:35:25 -08009#include "GrInvariantOutput.h"
reed026beb52015-06-10 14:23:15 -070010#include "SkPathPriv.h"
wangyix6af0c932015-07-22 10:21:17 -070011#include "gl/GrGLFragmentProcessor.h"
joshualitteb2a6762014-12-04 11:35:33 -080012#include "gl/builders/GrGLProgramBuilder.h"
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000013
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000014//////////////////////////////////////////////////////////////////////////////
joshualittb0a8a372014-09-23 09:50:21 -070015class AARectEffect : public GrFragmentProcessor {
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000016public:
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000017 const SkRect& getRect() const { return fRect; }
18
joshualittb0a8a372014-09-23 09:50:21 -070019 static GrFragmentProcessor* Create(GrPrimitiveEdgeType edgeType, const SkRect& rect) {
bsalomon55fad7a2014-07-08 07:34:20 -070020 return SkNEW_ARGS(AARectEffect, (edgeType, rect));
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000021 }
22
joshualittb0a8a372014-09-23 09:50:21 -070023 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +000024
mtklein36352bf2015-03-25 18:17:31 -070025 const char* name() const override { return "AARect"; }
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.orgf0539802014-02-08 19:31:05 +000030
31private:
joshualitteb2a6762014-12-04 11:35:33 -080032 AARectEffect(GrPrimitiveEdgeType edgeType, const SkRect& rect)
33 : fRect(rect), fEdgeType(edgeType) {
34 this->initClassID<AARectEffect>();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000035 this->setWillReadFragmentPosition();
36 }
37
mtklein36352bf2015-03-25 18:17:31 -070038 bool onIsEqual(const GrFragmentProcessor& other) const override {
joshualitt49586be2014-09-16 08:21:41 -070039 const AARectEffect& aare = other.cast<AARectEffect>();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000040 return fRect == aare.fRect;
41 }
42
mtklein36352bf2015-03-25 18:17:31 -070043 void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
egdaniel1a8ecdf2014-10-03 06:24:12 -070044 if (fRect.isEmpty()) {
45 // An empty rect will have no coverage anywhere.
joshualitt56995b52014-12-11 15:44:02 -080046 inout->mulByKnownSingleComponent(0);
egdaniel1a8ecdf2014-10-03 06:24:12 -070047 } else {
joshualitt56995b52014-12-11 15:44:02 -080048 inout->mulByUnknownSingleComponent();
egdaniel1a8ecdf2014-10-03 06:24:12 -070049 }
egdaniel1a8ecdf2014-10-03 06:24:12 -070050 }
51
joshualittb0a8a372014-09-23 09:50:21 -070052 SkRect fRect;
53 GrPrimitiveEdgeType fEdgeType;
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +000054
joshualittb0a8a372014-09-23 09:50:21 -070055 typedef GrFragmentProcessor INHERITED;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000056
joshualittb0a8a372014-09-23 09:50:21 -070057 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000058
59};
60
joshualittb0a8a372014-09-23 09:50:21 -070061GR_DEFINE_FRAGMENT_PROCESSOR_TEST(AARectEffect);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000062
joshualitt0067ff52015-07-08 14:26:19 -070063GrFragmentProcessor* AARectEffect::TestCreate(GrProcessorTestData* d) {
64 SkRect rect = SkRect::MakeLTRB(d->fRandom->nextSScalar1(),
65 d->fRandom->nextSScalar1(),
66 d->fRandom->nextSScalar1(),
67 d->fRandom->nextSScalar1());
joshualittb0a8a372014-09-23 09:50:21 -070068 GrFragmentProcessor* fp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000069 do {
joshualitt0067ff52015-07-08 14:26:19 -070070 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(
71 d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt));
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000072
joshualittb0a8a372014-09-23 09:50:21 -070073 fp = AARectEffect::Create(edgeType, rect);
74 } while (NULL == fp);
75 return fp;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000076}
77
78//////////////////////////////////////////////////////////////////////////////
79
joshualittb0a8a372014-09-23 09:50:21 -070080class GLAARectEffect : public GrGLFragmentProcessor {
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000081public:
joshualitteb2a6762014-12-04 11:35:33 -080082 GLAARectEffect(const GrProcessor&);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000083
wangyix7c157a92015-07-22 15:08:53 -070084 virtual void emitCode(EmitArgs&) override;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000085
jvanverthcfc18862015-04-28 08:48:20 -070086 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000087
mtklein36352bf2015-03-25 18:17:31 -070088 void setData(const GrGLProgramDataManager&, const GrProcessor&) override;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000089
90private:
kkinnunen7510b222014-07-30 00:04:16 -070091 GrGLProgramDataManager::UniformHandle fRectUniform;
92 SkRect fPrevRect;
joshualittb0a8a372014-09-23 09:50:21 -070093 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000094};
95
joshualitteb2a6762014-12-04 11:35:33 -080096GLAARectEffect::GLAARectEffect(const GrProcessor& effect) {
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000097 fPrevRect.fLeft = SK_ScalarNaN;
98}
99
wangyix7c157a92015-07-22 15:08:53 -0700100void GLAARectEffect::emitCode(EmitArgs& args) {
101 const AARectEffect& aare = args.fFp.cast<AARectEffect>();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000102 const char *rectName;
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000103 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
104 // respectively.
wangyix7c157a92015-07-22 15:08:53 -0700105 fRectUniform = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000106 kVec4f_GrSLType,
bsalomon422f56f2014-12-09 10:18:12 -0800107 kDefault_GrSLPrecision,
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000108 "rect",
109 &rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700110
wangyix7c157a92015-07-22 15:08:53 -0700111 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700112 const char* fragmentPos = fsBuilder->fragmentPosition();
joshualittb0a8a372014-09-23 09:50:21 -0700113 if (GrProcessorEdgeTypeIsAA(aare.getEdgeType())) {
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000114 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
115 // numbers, xSub and ySub.
joshualitt30ba4362014-08-21 20:18:45 -0700116 fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n");
117 fsBuilder->codeAppendf("\t\txSub = min(%s.x - %s.x, 0.0);\n", fragmentPos, rectName);
118 fsBuilder->codeAppendf("\t\txSub += min(%s.z - %s.x, 0.0);\n", rectName, fragmentPos);
119 fsBuilder->codeAppendf("\t\tySub = min(%s.y - %s.y, 0.0);\n", fragmentPos, rectName);
120 fsBuilder->codeAppendf("\t\tySub += min(%s.w - %s.y, 0.0);\n", rectName, fragmentPos);
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000121 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
122 // covered.
joshualitt30ba4362014-08-21 20:18:45 -0700123 fsBuilder->codeAppendf("\t\tfloat alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0));\n");
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000124 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700125 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
robertphillips7f14c9b2015-01-30 14:44:22 -0800126 fsBuilder->codeAppendf("\t\talpha *= (%s.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", fragmentPos, rectName);
127 fsBuilder->codeAppendf("\t\talpha *= (%s.z - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName, fragmentPos);
128 fsBuilder->codeAppendf("\t\talpha *= (%s.y - %s.y) > -0.5 ? 1.0 : 0.0;\n", fragmentPos, rectName);
129 fsBuilder->codeAppendf("\t\talpha *= (%s.w - %s.y) > -0.5 ? 1.0 : 0.0;\n", rectName, fragmentPos);
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000130 }
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000131
joshualittb0a8a372014-09-23 09:50:21 -0700132 if (GrProcessorEdgeTypeIsInverseFill(aare.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700133 fsBuilder->codeAppend("\t\talpha = 1.0 - alpha;\n");
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000134 }
wangyix7c157a92015-07-22 15:08:53 -0700135 fsBuilder->codeAppendf("\t\t%s = %s;\n", args.fOutputColor,
136 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha")).c_str());
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000137}
138
joshualittb0a8a372014-09-23 09:50:21 -0700139void GLAARectEffect::setData(const GrGLProgramDataManager& pdman, const GrProcessor& processor) {
140 const AARectEffect& aare = processor.cast<AARectEffect>();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000141 const SkRect& rect = aare.getRect();
142 if (rect != fPrevRect) {
robertphillips7f14c9b2015-01-30 14:44:22 -0800143 pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
144 rect.fRight - 0.5f, rect.fBottom - 0.5f);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000145 fPrevRect = rect;
146 }
147}
148
jvanverthcfc18862015-04-28 08:48:20 -0700149void GLAARectEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700150 GrProcessorKeyBuilder* b) {
151 const AARectEffect& aare = processor.cast<AARectEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700152 b->add32(aare.getEdgeType());
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000153}
154
jvanverthcfc18862015-04-28 08:48:20 -0700155void AARectEffect::getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800156 GLAARectEffect::GenKey(*this, caps, b);
157}
158
159GrGLFragmentProcessor* AARectEffect::createGLInstance() const {
160 return SkNEW_ARGS(GLAARectEffect, (*this));
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000161}
162
163//////////////////////////////////////////////////////////////////////////////
164
joshualittb0a8a372014-09-23 09:50:21 -0700165class GrGLConvexPolyEffect : public GrGLFragmentProcessor {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000166public:
joshualitteb2a6762014-12-04 11:35:33 -0800167 GrGLConvexPolyEffect(const GrProcessor&);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000168
wangyix7c157a92015-07-22 15:08:53 -0700169 virtual void emitCode(EmitArgs&) override;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000170
jvanverthcfc18862015-04-28 08:48:20 -0700171 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000172
mtklein36352bf2015-03-25 18:17:31 -0700173 void setData(const GrGLProgramDataManager&, const GrProcessor&) override;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000174
175private:
kkinnunen7510b222014-07-30 00:04:16 -0700176 GrGLProgramDataManager::UniformHandle fEdgeUniform;
177 SkScalar fPrevEdges[3 * GrConvexPolyEffect::kMaxEdges];
joshualittb0a8a372014-09-23 09:50:21 -0700178 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000179};
180
joshualitteb2a6762014-12-04 11:35:33 -0800181GrGLConvexPolyEffect::GrGLConvexPolyEffect(const GrProcessor&) {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000182 fPrevEdges[0] = SK_ScalarNaN;
183}
184
wangyix7c157a92015-07-22 15:08:53 -0700185void GrGLConvexPolyEffect::emitCode(EmitArgs& args) {
186 const GrConvexPolyEffect& cpe = args.fFp.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000187
188 const char *edgeArrayName;
wangyix7c157a92015-07-22 15:08:53 -0700189 fEdgeUniform = args.fBuilder->addUniformArray(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000190 kVec3f_GrSLType,
bsalomon422f56f2014-12-09 10:18:12 -0800191 kDefault_GrSLPrecision,
192 "edges",
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000193 cpe.getEdgeCount(),
194 &edgeArrayName);
wangyix7c157a92015-07-22 15:08:53 -0700195 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700196 fsBuilder->codeAppend("\t\tfloat alpha = 1.0;\n");
197 fsBuilder->codeAppend("\t\tfloat edge;\n");
198 const char* fragmentPos = fsBuilder->fragmentPosition();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000199 for (int i = 0; i < cpe.getEdgeCount(); ++i) {
robertphillips7f14c9b2015-01-30 14:44:22 -0800200 fsBuilder->codeAppendf("\t\tedge = dot(%s[%d], vec3(%s.x, %s.y, 1));\n",
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000201 edgeArrayName, i, fragmentPos, fragmentPos);
joshualittb0a8a372014-09-23 09:50:21 -0700202 if (GrProcessorEdgeTypeIsAA(cpe.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700203 fsBuilder->codeAppend("\t\tedge = clamp(edge, 0.0, 1.0);\n");
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000204 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700205 fsBuilder->codeAppend("\t\tedge = edge >= 0.5 ? 1.0 : 0.0;\n");
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000206 }
joshualitt30ba4362014-08-21 20:18:45 -0700207 fsBuilder->codeAppend("\t\talpha *= edge;\n");
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000208 }
209
commit-bot@chromium.org6dee8752014-02-07 22:39:01 +0000210 // Woe is me. See skbug.com/2149.
wangyix7c157a92015-07-22 15:08:53 -0700211 if (kTegra2_GrGLRenderer == args.fBuilder->ctxInfo().renderer()) {
joshualitt30ba4362014-08-21 20:18:45 -0700212 fsBuilder->codeAppend("\t\tif (-1.0 == alpha) {\n\t\t\tdiscard;\n\t\t}\n");
commit-bot@chromium.org6dee8752014-02-07 22:39:01 +0000213 }
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000214
joshualittb0a8a372014-09-23 09:50:21 -0700215 if (GrProcessorEdgeTypeIsInverseFill(cpe.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700216 fsBuilder->codeAppend("\talpha = 1.0 - alpha;\n");
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000217 }
wangyix7c157a92015-07-22 15:08:53 -0700218 fsBuilder->codeAppendf("\t%s = %s;\n", args.fOutputColor,
219 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha")).c_str());
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000220}
221
joshualittb0a8a372014-09-23 09:50:21 -0700222void GrGLConvexPolyEffect::setData(const GrGLProgramDataManager& pdman, const GrProcessor& effect) {
joshualitt49586be2014-09-16 08:21:41 -0700223 const GrConvexPolyEffect& cpe = effect.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000224 size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar);
225 if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) {
kkinnunen7510b222014-07-30 00:04:16 -0700226 pdman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges());
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000227 memcpy(fPrevEdges, cpe.getEdges(), byteSize);
228 }
229}
230
jvanverthcfc18862015-04-28 08:48:20 -0700231void GrGLConvexPolyEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700232 GrProcessorKeyBuilder* b) {
233 const GrConvexPolyEffect& cpe = processor.cast<GrConvexPolyEffect>();
234 GR_STATIC_ASSERT(kGrProcessorEdgeTypeCnt <= 8);
bsalomon63e99f72014-07-21 08:03:14 -0700235 uint32_t key = (cpe.getEdgeCount() << 3) | cpe.getEdgeType();
236 b->add32(key);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000237}
238
239//////////////////////////////////////////////////////////////////////////////
240
joshualittb0a8a372014-09-23 09:50:21 -0700241GrFragmentProcessor* GrConvexPolyEffect::Create(GrPrimitiveEdgeType type, const SkPath& path,
242 const SkVector* offset) {
243 if (kHairlineAA_GrProcessorEdgeType == type) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000244 return NULL;
245 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000246 if (path.getSegmentMasks() != SkPath::kLine_SegmentMask ||
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000247 !path.isConvex()) {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000248 return NULL;
249 }
250
251 if (path.countPoints() > kMaxEdges) {
252 return NULL;
253 }
254
255 SkPoint pts[kMaxEdges];
256 SkScalar edges[3 * kMaxEdges];
257
reed026beb52015-06-10 14:23:15 -0700258 SkPathPriv::FirstDirection dir;
259 SkAssertResult(SkPathPriv::CheapComputeFirstDirection(path, &dir));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000260
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000261 SkVector t;
262 if (NULL == offset) {
263 t.set(0, 0);
264 } else {
265 t = *offset;
266 }
267
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000268 int count = path.getPoints(pts, kMaxEdges);
269 int n = 0;
270 for (int lastPt = count - 1, i = 0; i < count; lastPt = i++) {
271 if (pts[lastPt] != pts[i]) {
272 SkVector v = pts[i] - pts[lastPt];
273 v.normalize();
reed026beb52015-06-10 14:23:15 -0700274 if (SkPathPriv::kCCW_FirstDirection == dir) {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000275 edges[3 * n] = v.fY;
276 edges[3 * n + 1] = -v.fX;
277 } else {
278 edges[3 * n] = -v.fY;
279 edges[3 * n + 1] = v.fX;
280 }
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000281 SkPoint p = pts[i] + t;
282 edges[3 * n + 2] = -(edges[3 * n] * p.fX + edges[3 * n + 1] * p.fY);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000283 ++n;
284 }
285 }
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000286 if (path.isInverseFillType()) {
joshualittb0a8a372014-09-23 09:50:21 -0700287 type = GrInvertProcessorEdgeType(type);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000288 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000289 return Create(type, n, edges);
290}
291
joshualittb0a8a372014-09-23 09:50:21 -0700292GrFragmentProcessor* GrConvexPolyEffect::Create(GrPrimitiveEdgeType edgeType, const SkRect& rect) {
293 if (kHairlineAA_GrProcessorEdgeType == edgeType){
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000294 return NULL;
295 }
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000296 return AARectEffect::Create(edgeType, rect);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000297}
298
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000299GrConvexPolyEffect::~GrConvexPolyEffect() {}
300
egdaniel605dd0f2014-11-12 08:35:25 -0800301void GrConvexPolyEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
joshualitt56995b52014-12-11 15:44:02 -0800302 inout->mulByUnknownSingleComponent();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000303}
304
jvanverthcfc18862015-04-28 08:48:20 -0700305void GrConvexPolyEffect::getGLProcessorKey(const GrGLSLCaps& caps,
joshualitteb2a6762014-12-04 11:35:33 -0800306 GrProcessorKeyBuilder* b) const {
307 GrGLConvexPolyEffect::GenKey(*this, caps, b);
308}
309
310GrGLFragmentProcessor* GrConvexPolyEffect::createGLInstance() const {
311 return SkNEW_ARGS(GrGLConvexPolyEffect, (*this));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000312}
313
joshualittb0a8a372014-09-23 09:50:21 -0700314GrConvexPolyEffect::GrConvexPolyEffect(GrPrimitiveEdgeType edgeType, int n, const SkScalar edges[])
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000315 : fEdgeType(edgeType)
316 , fEdgeCount(n) {
joshualitteb2a6762014-12-04 11:35:33 -0800317 this->initClassID<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000318 // Factory function should have already ensured this.
319 SkASSERT(n <= kMaxEdges);
320 memcpy(fEdges, edges, 3 * n * sizeof(SkScalar));
321 // Outset the edges by 0.5 so that a pixel with center on an edge is 50% covered in the AA case
322 // and 100% covered in the non-AA case.
323 for (int i = 0; i < n; ++i) {
324 fEdges[3 * i + 2] += SK_ScalarHalf;
325 }
326 this->setWillReadFragmentPosition();
327}
328
bsalomon0e08fc12014-10-15 08:19:04 -0700329bool GrConvexPolyEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700330 const GrConvexPolyEffect& cpe = other.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000331 // ignore the fact that 0 == -0 and just use memcmp.
332 return (cpe.fEdgeType == fEdgeType && cpe.fEdgeCount == fEdgeCount &&
333 0 == memcmp(cpe.fEdges, fEdges, 3 * fEdgeCount * sizeof(SkScalar)));
334}
335
336//////////////////////////////////////////////////////////////////////////////
337
joshualittb0a8a372014-09-23 09:50:21 -0700338GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvexPolyEffect);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000339
joshualitt0067ff52015-07-08 14:26:19 -0700340GrFragmentProcessor* GrConvexPolyEffect::TestCreate(GrProcessorTestData* d) {
341 int count = d->fRandom->nextULessThan(kMaxEdges) + 1;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000342 SkScalar edges[kMaxEdges * 3];
343 for (int i = 0; i < 3 * count; ++i) {
joshualitt0067ff52015-07-08 14:26:19 -0700344 edges[i] = d->fRandom->nextSScalar1();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000345 }
346
joshualittb0a8a372014-09-23 09:50:21 -0700347 GrFragmentProcessor* fp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000348 do {
joshualittb0a8a372014-09-23 09:50:21 -0700349 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(
joshualitt0067ff52015-07-08 14:26:19 -0700350 d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt));
joshualittb0a8a372014-09-23 09:50:21 -0700351 fp = GrConvexPolyEffect::Create(edgeType, count, edges);
352 } while (NULL == fp);
353 return fp;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000354}