blob: 6c477688c0ce4fa7eca41272c1b583e5207facc7 [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
wangyix4b3050b2015-08-04 07:59:37 -070027 void onGetGLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
joshualitteb2a6762014-12-04 11:35:33 -080028
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000029private:
joshualitteb2a6762014-12-04 11:35:33 -080030 AARectEffect(GrPrimitiveEdgeType edgeType, const SkRect& rect)
31 : fRect(rect), fEdgeType(edgeType) {
32 this->initClassID<AARectEffect>();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000033 this->setWillReadFragmentPosition();
34 }
35
wangyixb1daa862015-08-18 11:29:31 -070036 GrGLFragmentProcessor* onCreateGLInstance() const override;
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
wangyixb1daa862015-08-18 11:29:31 -070088protected:
89 void onSetData(const GrGLProgramDataManager&, const GrProcessor&) override;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000090
91private:
kkinnunen7510b222014-07-30 00:04:16 -070092 GrGLProgramDataManager::UniformHandle fRectUniform;
93 SkRect fPrevRect;
joshualittb0a8a372014-09-23 09:50:21 -070094 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000095};
96
joshualitteb2a6762014-12-04 11:35:33 -080097GLAARectEffect::GLAARectEffect(const GrProcessor& effect) {
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000098 fPrevRect.fLeft = SK_ScalarNaN;
99}
100
wangyix7c157a92015-07-22 15:08:53 -0700101void GLAARectEffect::emitCode(EmitArgs& args) {
102 const AARectEffect& aare = args.fFp.cast<AARectEffect>();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000103 const char *rectName;
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000104 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
105 // respectively.
wangyix7c157a92015-07-22 15:08:53 -0700106 fRectUniform = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000107 kVec4f_GrSLType,
bsalomon422f56f2014-12-09 10:18:12 -0800108 kDefault_GrSLPrecision,
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000109 "rect",
110 &rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700111
wangyix7c157a92015-07-22 15:08:53 -0700112 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700113 const char* fragmentPos = fsBuilder->fragmentPosition();
joshualittb0a8a372014-09-23 09:50:21 -0700114 if (GrProcessorEdgeTypeIsAA(aare.getEdgeType())) {
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000115 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
116 // numbers, xSub and ySub.
joshualitt30ba4362014-08-21 20:18:45 -0700117 fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n");
118 fsBuilder->codeAppendf("\t\txSub = min(%s.x - %s.x, 0.0);\n", fragmentPos, rectName);
119 fsBuilder->codeAppendf("\t\txSub += min(%s.z - %s.x, 0.0);\n", rectName, fragmentPos);
120 fsBuilder->codeAppendf("\t\tySub = min(%s.y - %s.y, 0.0);\n", fragmentPos, rectName);
121 fsBuilder->codeAppendf("\t\tySub += min(%s.w - %s.y, 0.0);\n", rectName, fragmentPos);
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000122 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
123 // covered.
joshualitt30ba4362014-08-21 20:18:45 -0700124 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 +0000125 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700126 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
robertphillips7f14c9b2015-01-30 14:44:22 -0800127 fsBuilder->codeAppendf("\t\talpha *= (%s.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", fragmentPos, rectName);
128 fsBuilder->codeAppendf("\t\talpha *= (%s.z - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName, fragmentPos);
129 fsBuilder->codeAppendf("\t\talpha *= (%s.y - %s.y) > -0.5 ? 1.0 : 0.0;\n", fragmentPos, rectName);
130 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 +0000131 }
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000132
joshualittb0a8a372014-09-23 09:50:21 -0700133 if (GrProcessorEdgeTypeIsInverseFill(aare.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700134 fsBuilder->codeAppend("\t\talpha = 1.0 - alpha;\n");
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000135 }
wangyix7c157a92015-07-22 15:08:53 -0700136 fsBuilder->codeAppendf("\t\t%s = %s;\n", args.fOutputColor,
137 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha")).c_str());
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000138}
139
wangyixb1daa862015-08-18 11:29:31 -0700140void GLAARectEffect::onSetData(const GrGLProgramDataManager& pdman, const GrProcessor& processor) {
joshualittb0a8a372014-09-23 09:50:21 -0700141 const AARectEffect& aare = processor.cast<AARectEffect>();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000142 const SkRect& rect = aare.getRect();
143 if (rect != fPrevRect) {
robertphillips7f14c9b2015-01-30 14:44:22 -0800144 pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
145 rect.fRight - 0.5f, rect.fBottom - 0.5f);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000146 fPrevRect = rect;
147 }
148}
149
jvanverthcfc18862015-04-28 08:48:20 -0700150void GLAARectEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700151 GrProcessorKeyBuilder* b) {
152 const AARectEffect& aare = processor.cast<AARectEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700153 b->add32(aare.getEdgeType());
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000154}
155
wangyix4b3050b2015-08-04 07:59:37 -0700156void AARectEffect::onGetGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800157 GLAARectEffect::GenKey(*this, caps, b);
158}
159
wangyixb1daa862015-08-18 11:29:31 -0700160GrGLFragmentProcessor* AARectEffect::onCreateGLInstance() const {
joshualitteb2a6762014-12-04 11:35:33 -0800161 return SkNEW_ARGS(GLAARectEffect, (*this));
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000162}
163
164//////////////////////////////////////////////////////////////////////////////
165
joshualittb0a8a372014-09-23 09:50:21 -0700166class GrGLConvexPolyEffect : public GrGLFragmentProcessor {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000167public:
joshualitteb2a6762014-12-04 11:35:33 -0800168 GrGLConvexPolyEffect(const GrProcessor&);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000169
wangyix7c157a92015-07-22 15:08:53 -0700170 virtual void emitCode(EmitArgs&) override;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000171
jvanverthcfc18862015-04-28 08:48:20 -0700172 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000173
wangyixb1daa862015-08-18 11:29:31 -0700174protected:
175 void onSetData(const GrGLProgramDataManager&, const GrProcessor&) override;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000176
177private:
kkinnunen7510b222014-07-30 00:04:16 -0700178 GrGLProgramDataManager::UniformHandle fEdgeUniform;
179 SkScalar fPrevEdges[3 * GrConvexPolyEffect::kMaxEdges];
joshualittb0a8a372014-09-23 09:50:21 -0700180 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000181};
182
joshualitteb2a6762014-12-04 11:35:33 -0800183GrGLConvexPolyEffect::GrGLConvexPolyEffect(const GrProcessor&) {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000184 fPrevEdges[0] = SK_ScalarNaN;
185}
186
wangyix7c157a92015-07-22 15:08:53 -0700187void GrGLConvexPolyEffect::emitCode(EmitArgs& args) {
188 const GrConvexPolyEffect& cpe = args.fFp.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000189
190 const char *edgeArrayName;
wangyix7c157a92015-07-22 15:08:53 -0700191 fEdgeUniform = args.fBuilder->addUniformArray(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000192 kVec3f_GrSLType,
bsalomon422f56f2014-12-09 10:18:12 -0800193 kDefault_GrSLPrecision,
194 "edges",
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000195 cpe.getEdgeCount(),
196 &edgeArrayName);
wangyix7c157a92015-07-22 15:08:53 -0700197 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700198 fsBuilder->codeAppend("\t\tfloat alpha = 1.0;\n");
199 fsBuilder->codeAppend("\t\tfloat edge;\n");
200 const char* fragmentPos = fsBuilder->fragmentPosition();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000201 for (int i = 0; i < cpe.getEdgeCount(); ++i) {
robertphillips7f14c9b2015-01-30 14:44:22 -0800202 fsBuilder->codeAppendf("\t\tedge = dot(%s[%d], vec3(%s.x, %s.y, 1));\n",
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000203 edgeArrayName, i, fragmentPos, fragmentPos);
joshualittb0a8a372014-09-23 09:50:21 -0700204 if (GrProcessorEdgeTypeIsAA(cpe.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700205 fsBuilder->codeAppend("\t\tedge = clamp(edge, 0.0, 1.0);\n");
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000206 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700207 fsBuilder->codeAppend("\t\tedge = edge >= 0.5 ? 1.0 : 0.0;\n");
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000208 }
joshualitt30ba4362014-08-21 20:18:45 -0700209 fsBuilder->codeAppend("\t\talpha *= edge;\n");
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000210 }
211
commit-bot@chromium.org6dee8752014-02-07 22:39:01 +0000212 // Woe is me. See skbug.com/2149.
wangyix7c157a92015-07-22 15:08:53 -0700213 if (kTegra2_GrGLRenderer == args.fBuilder->ctxInfo().renderer()) {
joshualitt30ba4362014-08-21 20:18:45 -0700214 fsBuilder->codeAppend("\t\tif (-1.0 == alpha) {\n\t\t\tdiscard;\n\t\t}\n");
commit-bot@chromium.org6dee8752014-02-07 22:39:01 +0000215 }
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000216
joshualittb0a8a372014-09-23 09:50:21 -0700217 if (GrProcessorEdgeTypeIsInverseFill(cpe.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700218 fsBuilder->codeAppend("\talpha = 1.0 - alpha;\n");
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000219 }
wangyix7c157a92015-07-22 15:08:53 -0700220 fsBuilder->codeAppendf("\t%s = %s;\n", args.fOutputColor,
221 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha")).c_str());
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000222}
223
wangyixb1daa862015-08-18 11:29:31 -0700224void GrGLConvexPolyEffect::onSetData(const GrGLProgramDataManager& pdman, const GrProcessor& effect) {
joshualitt49586be2014-09-16 08:21:41 -0700225 const GrConvexPolyEffect& cpe = effect.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000226 size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar);
227 if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) {
kkinnunen7510b222014-07-30 00:04:16 -0700228 pdman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges());
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000229 memcpy(fPrevEdges, cpe.getEdges(), byteSize);
230 }
231}
232
jvanverthcfc18862015-04-28 08:48:20 -0700233void GrGLConvexPolyEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700234 GrProcessorKeyBuilder* b) {
235 const GrConvexPolyEffect& cpe = processor.cast<GrConvexPolyEffect>();
236 GR_STATIC_ASSERT(kGrProcessorEdgeTypeCnt <= 8);
bsalomon63e99f72014-07-21 08:03:14 -0700237 uint32_t key = (cpe.getEdgeCount() << 3) | cpe.getEdgeType();
238 b->add32(key);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000239}
240
241//////////////////////////////////////////////////////////////////////////////
242
joshualittb0a8a372014-09-23 09:50:21 -0700243GrFragmentProcessor* GrConvexPolyEffect::Create(GrPrimitiveEdgeType type, const SkPath& path,
244 const SkVector* offset) {
245 if (kHairlineAA_GrProcessorEdgeType == type) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000246 return NULL;
247 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000248 if (path.getSegmentMasks() != SkPath::kLine_SegmentMask ||
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000249 !path.isConvex()) {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000250 return NULL;
251 }
252
253 if (path.countPoints() > kMaxEdges) {
254 return NULL;
255 }
256
257 SkPoint pts[kMaxEdges];
258 SkScalar edges[3 * kMaxEdges];
259
reed026beb52015-06-10 14:23:15 -0700260 SkPathPriv::FirstDirection dir;
261 SkAssertResult(SkPathPriv::CheapComputeFirstDirection(path, &dir));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000262
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000263 SkVector t;
264 if (NULL == offset) {
265 t.set(0, 0);
266 } else {
267 t = *offset;
268 }
269
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000270 int count = path.getPoints(pts, kMaxEdges);
271 int n = 0;
272 for (int lastPt = count - 1, i = 0; i < count; lastPt = i++) {
273 if (pts[lastPt] != pts[i]) {
274 SkVector v = pts[i] - pts[lastPt];
275 v.normalize();
reed026beb52015-06-10 14:23:15 -0700276 if (SkPathPriv::kCCW_FirstDirection == dir) {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000277 edges[3 * n] = v.fY;
278 edges[3 * n + 1] = -v.fX;
279 } else {
280 edges[3 * n] = -v.fY;
281 edges[3 * n + 1] = v.fX;
282 }
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000283 SkPoint p = pts[i] + t;
284 edges[3 * n + 2] = -(edges[3 * n] * p.fX + edges[3 * n + 1] * p.fY);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000285 ++n;
286 }
287 }
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000288 if (path.isInverseFillType()) {
joshualittb0a8a372014-09-23 09:50:21 -0700289 type = GrInvertProcessorEdgeType(type);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000290 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000291 return Create(type, n, edges);
292}
293
joshualittb0a8a372014-09-23 09:50:21 -0700294GrFragmentProcessor* GrConvexPolyEffect::Create(GrPrimitiveEdgeType edgeType, const SkRect& rect) {
295 if (kHairlineAA_GrProcessorEdgeType == edgeType){
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000296 return NULL;
297 }
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000298 return AARectEffect::Create(edgeType, rect);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000299}
300
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000301GrConvexPolyEffect::~GrConvexPolyEffect() {}
302
egdaniel605dd0f2014-11-12 08:35:25 -0800303void GrConvexPolyEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
joshualitt56995b52014-12-11 15:44:02 -0800304 inout->mulByUnknownSingleComponent();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000305}
306
wangyix4b3050b2015-08-04 07:59:37 -0700307void GrConvexPolyEffect::onGetGLProcessorKey(const GrGLSLCaps& caps,
joshualitteb2a6762014-12-04 11:35:33 -0800308 GrProcessorKeyBuilder* b) const {
309 GrGLConvexPolyEffect::GenKey(*this, caps, b);
310}
311
wangyixb1daa862015-08-18 11:29:31 -0700312GrGLFragmentProcessor* GrConvexPolyEffect::onCreateGLInstance() const {
joshualitteb2a6762014-12-04 11:35:33 -0800313 return SkNEW_ARGS(GrGLConvexPolyEffect, (*this));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000314}
315
joshualittb0a8a372014-09-23 09:50:21 -0700316GrConvexPolyEffect::GrConvexPolyEffect(GrPrimitiveEdgeType edgeType, int n, const SkScalar edges[])
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000317 : fEdgeType(edgeType)
318 , fEdgeCount(n) {
joshualitteb2a6762014-12-04 11:35:33 -0800319 this->initClassID<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000320 // Factory function should have already ensured this.
321 SkASSERT(n <= kMaxEdges);
322 memcpy(fEdges, edges, 3 * n * sizeof(SkScalar));
323 // Outset the edges by 0.5 so that a pixel with center on an edge is 50% covered in the AA case
324 // and 100% covered in the non-AA case.
325 for (int i = 0; i < n; ++i) {
326 fEdges[3 * i + 2] += SK_ScalarHalf;
327 }
328 this->setWillReadFragmentPosition();
329}
330
bsalomon0e08fc12014-10-15 08:19:04 -0700331bool GrConvexPolyEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700332 const GrConvexPolyEffect& cpe = other.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000333 // ignore the fact that 0 == -0 and just use memcmp.
334 return (cpe.fEdgeType == fEdgeType && cpe.fEdgeCount == fEdgeCount &&
335 0 == memcmp(cpe.fEdges, fEdges, 3 * fEdgeCount * sizeof(SkScalar)));
336}
337
338//////////////////////////////////////////////////////////////////////////////
339
joshualittb0a8a372014-09-23 09:50:21 -0700340GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvexPolyEffect);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000341
joshualitt0067ff52015-07-08 14:26:19 -0700342GrFragmentProcessor* GrConvexPolyEffect::TestCreate(GrProcessorTestData* d) {
343 int count = d->fRandom->nextULessThan(kMaxEdges) + 1;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000344 SkScalar edges[kMaxEdges * 3];
345 for (int i = 0; i < 3 * count; ++i) {
joshualitt0067ff52015-07-08 14:26:19 -0700346 edges[i] = d->fRandom->nextSScalar1();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000347 }
348
joshualittb0a8a372014-09-23 09:50:21 -0700349 GrFragmentProcessor* fp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000350 do {
joshualittb0a8a372014-09-23 09:50:21 -0700351 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(
joshualitt0067ff52015-07-08 14:26:19 -0700352 d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt));
joshualittb0a8a372014-09-23 09:50:21 -0700353 fp = GrConvexPolyEffect::Create(edgeType, count, edges);
354 } while (NULL == fp);
355 return fp;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000356}