blob: dc7a5285013eeaf1b0234e30c98147e00d77411a [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"
egdaniel64c47282015-11-13 06:54:19 -080011#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080012#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070013#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080014#include "glsl/GrGLSLUniformHandler.h"
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000015
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000016//////////////////////////////////////////////////////////////////////////////
joshualittb0a8a372014-09-23 09:50:21 -070017class AARectEffect : public GrFragmentProcessor {
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000018public:
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000019 const SkRect& getRect() const { return fRect; }
20
joshualittb0a8a372014-09-23 09:50:21 -070021 static GrFragmentProcessor* Create(GrPrimitiveEdgeType edgeType, const SkRect& rect) {
halcanary385fe4d2015-08-26 13:07:48 -070022 return new AARectEffect(edgeType, rect);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000023 }
24
joshualittb0a8a372014-09-23 09:50:21 -070025 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +000026
mtklein36352bf2015-03-25 18:17:31 -070027 const char* name() const override { return "AARect"; }
joshualitteb2a6762014-12-04 11:35:33 -080028
egdaniel57d3b032015-11-13 11:57:27 -080029 void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
joshualitteb2a6762014-12-04 11:35:33 -080030
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000031private:
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
egdaniel57d3b032015-11-13 11:57:27 -080038 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -070039
mtklein36352bf2015-03-25 18:17:31 -070040 bool onIsEqual(const GrFragmentProcessor& other) const override {
joshualitt49586be2014-09-16 08:21:41 -070041 const AARectEffect& aare = other.cast<AARectEffect>();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000042 return fRect == aare.fRect;
43 }
44
mtklein36352bf2015-03-25 18:17:31 -070045 void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
egdaniel1a8ecdf2014-10-03 06:24:12 -070046 if (fRect.isEmpty()) {
47 // An empty rect will have no coverage anywhere.
joshualitt56995b52014-12-11 15:44:02 -080048 inout->mulByKnownSingleComponent(0);
egdaniel1a8ecdf2014-10-03 06:24:12 -070049 } else {
joshualitt56995b52014-12-11 15:44:02 -080050 inout->mulByUnknownSingleComponent();
egdaniel1a8ecdf2014-10-03 06:24:12 -070051 }
egdaniel1a8ecdf2014-10-03 06:24:12 -070052 }
53
joshualittb0a8a372014-09-23 09:50:21 -070054 SkRect fRect;
55 GrPrimitiveEdgeType fEdgeType;
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +000056
joshualittb0a8a372014-09-23 09:50:21 -070057 typedef GrFragmentProcessor INHERITED;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000058
joshualittb0a8a372014-09-23 09:50:21 -070059 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000060
61};
62
joshualittb0a8a372014-09-23 09:50:21 -070063GR_DEFINE_FRAGMENT_PROCESSOR_TEST(AARectEffect);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000064
bsalomonc21b09e2015-08-28 18:46:56 -070065const GrFragmentProcessor* AARectEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -070066 SkRect rect = SkRect::MakeLTRB(d->fRandom->nextSScalar1(),
67 d->fRandom->nextSScalar1(),
68 d->fRandom->nextSScalar1(),
69 d->fRandom->nextSScalar1());
joshualittb0a8a372014-09-23 09:50:21 -070070 GrFragmentProcessor* fp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000071 do {
joshualitt0067ff52015-07-08 14:26:19 -070072 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(
73 d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt));
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000074
joshualittb0a8a372014-09-23 09:50:21 -070075 fp = AARectEffect::Create(edgeType, rect);
halcanary96fcdcc2015-08-27 07:41:13 -070076 } while (nullptr == fp);
joshualittb0a8a372014-09-23 09:50:21 -070077 return fp;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000078}
79
80//////////////////////////////////////////////////////////////////////////////
81
egdaniel64c47282015-11-13 06:54:19 -080082class GLAARectEffect : public GrGLSLFragmentProcessor {
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000083public:
joshualitteb2a6762014-12-04 11:35:33 -080084 GLAARectEffect(const GrProcessor&);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000085
wangyix7c157a92015-07-22 15:08:53 -070086 virtual void emitCode(EmitArgs&) override;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000087
jvanverthcfc18862015-04-28 08:48:20 -070088 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000089
wangyixb1daa862015-08-18 11:29:31 -070090protected:
egdaniel018fb622015-10-28 07:26:40 -070091 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000092
93private:
egdaniel018fb622015-10-28 07:26:40 -070094 GrGLSLProgramDataManager::UniformHandle fRectUniform;
kkinnunen7510b222014-07-30 00:04:16 -070095 SkRect fPrevRect;
egdaniel64c47282015-11-13 06:54:19 -080096 typedef GrGLSLFragmentProcessor INHERITED;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000097};
98
joshualitteb2a6762014-12-04 11:35:33 -080099GLAARectEffect::GLAARectEffect(const GrProcessor& effect) {
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000100 fPrevRect.fLeft = SK_ScalarNaN;
101}
102
wangyix7c157a92015-07-22 15:08:53 -0700103void GLAARectEffect::emitCode(EmitArgs& args) {
104 const AARectEffect& aare = args.fFp.cast<AARectEffect>();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000105 const char *rectName;
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000106 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
107 // respectively.
egdaniel7ea439b2015-12-03 09:20:44 -0800108 fRectUniform = args.fUniformHandler->addUniform(GrGLSLUniformHandler::kFragment_Visibility,
109 kVec4f_GrSLType,
110 kDefault_GrSLPrecision,
111 "rect",
112 &rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700113
egdaniel4ca2e602015-11-18 08:01:26 -0800114 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
115 const char* fragmentPos = fragBuilder->fragmentPosition();
joshualittb0a8a372014-09-23 09:50:21 -0700116 if (GrProcessorEdgeTypeIsAA(aare.getEdgeType())) {
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000117 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
118 // numbers, xSub and ySub.
egdaniel4ca2e602015-11-18 08:01:26 -0800119 fragBuilder->codeAppend("\t\tfloat xSub, ySub;\n");
120 fragBuilder->codeAppendf("\t\txSub = min(%s.x - %s.x, 0.0);\n", fragmentPos, rectName);
121 fragBuilder->codeAppendf("\t\txSub += min(%s.z - %s.x, 0.0);\n", rectName, fragmentPos);
122 fragBuilder->codeAppendf("\t\tySub = min(%s.y - %s.y, 0.0);\n", fragmentPos, rectName);
123 fragBuilder->codeAppendf("\t\tySub += min(%s.w - %s.y, 0.0);\n", rectName, fragmentPos);
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000124 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
125 // covered.
egdaniel4ca2e602015-11-18 08:01:26 -0800126 fragBuilder->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 +0000127 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800128 fragBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
129 fragBuilder->codeAppendf("\t\talpha *= (%s.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", fragmentPos, rectName);
130 fragBuilder->codeAppendf("\t\talpha *= (%s.z - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName, fragmentPos);
131 fragBuilder->codeAppendf("\t\talpha *= (%s.y - %s.y) > -0.5 ? 1.0 : 0.0;\n", fragmentPos, rectName);
132 fragBuilder->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 +0000133 }
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000134
joshualittb0a8a372014-09-23 09:50:21 -0700135 if (GrProcessorEdgeTypeIsInverseFill(aare.getEdgeType())) {
egdaniel4ca2e602015-11-18 08:01:26 -0800136 fragBuilder->codeAppend("\t\talpha = 1.0 - alpha;\n");
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000137 }
egdaniel4ca2e602015-11-18 08:01:26 -0800138 fragBuilder->codeAppendf("\t\t%s = %s;\n", args.fOutputColor,
139 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha")).c_str());
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000140}
141
egdaniel018fb622015-10-28 07:26:40 -0700142void GLAARectEffect::onSetData(const GrGLSLProgramDataManager& pdman,
143 const GrProcessor& processor) {
joshualittb0a8a372014-09-23 09:50:21 -0700144 const AARectEffect& aare = processor.cast<AARectEffect>();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000145 const SkRect& rect = aare.getRect();
146 if (rect != fPrevRect) {
robertphillips7f14c9b2015-01-30 14:44:22 -0800147 pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
148 rect.fRight - 0.5f, rect.fBottom - 0.5f);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000149 fPrevRect = rect;
150 }
151}
152
jvanverthcfc18862015-04-28 08:48:20 -0700153void GLAARectEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700154 GrProcessorKeyBuilder* b) {
155 const AARectEffect& aare = processor.cast<AARectEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700156 b->add32(aare.getEdgeType());
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000157}
158
egdaniel57d3b032015-11-13 11:57:27 -0800159void AARectEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800160 GLAARectEffect::GenKey(*this, caps, b);
161}
162
egdaniel57d3b032015-11-13 11:57:27 -0800163GrGLSLFragmentProcessor* AARectEffect::onCreateGLSLInstance() const {
halcanary385fe4d2015-08-26 13:07:48 -0700164 return new GLAARectEffect(*this);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000165}
166
167//////////////////////////////////////////////////////////////////////////////
168
egdaniel64c47282015-11-13 06:54:19 -0800169class GrGLConvexPolyEffect : public GrGLSLFragmentProcessor {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000170public:
joshualitteb2a6762014-12-04 11:35:33 -0800171 GrGLConvexPolyEffect(const GrProcessor&);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000172
wangyix7c157a92015-07-22 15:08:53 -0700173 virtual void emitCode(EmitArgs&) override;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000174
jvanverthcfc18862015-04-28 08:48:20 -0700175 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000176
wangyixb1daa862015-08-18 11:29:31 -0700177protected:
egdaniel018fb622015-10-28 07:26:40 -0700178 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000179
180private:
egdaniel018fb622015-10-28 07:26:40 -0700181 GrGLSLProgramDataManager::UniformHandle fEdgeUniform;
kkinnunen7510b222014-07-30 00:04:16 -0700182 SkScalar fPrevEdges[3 * GrConvexPolyEffect::kMaxEdges];
egdaniel64c47282015-11-13 06:54:19 -0800183 typedef GrGLSLFragmentProcessor INHERITED;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000184};
185
joshualitteb2a6762014-12-04 11:35:33 -0800186GrGLConvexPolyEffect::GrGLConvexPolyEffect(const GrProcessor&) {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000187 fPrevEdges[0] = SK_ScalarNaN;
188}
189
wangyix7c157a92015-07-22 15:08:53 -0700190void GrGLConvexPolyEffect::emitCode(EmitArgs& args) {
191 const GrConvexPolyEffect& cpe = args.fFp.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000192
193 const char *edgeArrayName;
egdaniel7ea439b2015-12-03 09:20:44 -0800194 fEdgeUniform = args.fUniformHandler->addUniformArray(GrGLSLUniformHandler::kFragment_Visibility,
195 kVec3f_GrSLType,
196 kDefault_GrSLPrecision,
197 "edges",
198 cpe.getEdgeCount(),
199 &edgeArrayName);
egdaniel4ca2e602015-11-18 08:01:26 -0800200 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
201 fragBuilder->codeAppend("\t\tfloat alpha = 1.0;\n");
202 fragBuilder->codeAppend("\t\tfloat edge;\n");
203 const char* fragmentPos = fragBuilder->fragmentPosition();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000204 for (int i = 0; i < cpe.getEdgeCount(); ++i) {
egdaniel4ca2e602015-11-18 08:01:26 -0800205 fragBuilder->codeAppendf("\t\tedge = dot(%s[%d], vec3(%s.x, %s.y, 1));\n",
206 edgeArrayName, i, fragmentPos, fragmentPos);
joshualittb0a8a372014-09-23 09:50:21 -0700207 if (GrProcessorEdgeTypeIsAA(cpe.getEdgeType())) {
egdaniel4ca2e602015-11-18 08:01:26 -0800208 fragBuilder->codeAppend("\t\tedge = clamp(edge, 0.0, 1.0);\n");
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000209 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800210 fragBuilder->codeAppend("\t\tedge = edge >= 0.5 ? 1.0 : 0.0;\n");
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000211 }
egdaniel4ca2e602015-11-18 08:01:26 -0800212 fragBuilder->codeAppend("\t\talpha *= edge;\n");
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000213 }
214
joshualittb0a8a372014-09-23 09:50:21 -0700215 if (GrProcessorEdgeTypeIsInverseFill(cpe.getEdgeType())) {
egdaniel4ca2e602015-11-18 08:01:26 -0800216 fragBuilder->codeAppend("\talpha = 1.0 - alpha;\n");
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000217 }
egdaniel4ca2e602015-11-18 08:01:26 -0800218 fragBuilder->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
egdaniel018fb622015-10-28 07:26:40 -0700222void GrGLConvexPolyEffect::onSetData(const GrGLSLProgramDataManager& pdman,
223 const GrProcessor& effect) {
joshualitt49586be2014-09-16 08:21:41 -0700224 const GrConvexPolyEffect& cpe = effect.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000225 size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar);
226 if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) {
kkinnunen7510b222014-07-30 00:04:16 -0700227 pdman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges());
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000228 memcpy(fPrevEdges, cpe.getEdges(), byteSize);
229 }
230}
231
jvanverthcfc18862015-04-28 08:48:20 -0700232void GrGLConvexPolyEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700233 GrProcessorKeyBuilder* b) {
234 const GrConvexPolyEffect& cpe = processor.cast<GrConvexPolyEffect>();
235 GR_STATIC_ASSERT(kGrProcessorEdgeTypeCnt <= 8);
bsalomon63e99f72014-07-21 08:03:14 -0700236 uint32_t key = (cpe.getEdgeCount() << 3) | cpe.getEdgeType();
237 b->add32(key);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000238}
239
240//////////////////////////////////////////////////////////////////////////////
241
joshualittb0a8a372014-09-23 09:50:21 -0700242GrFragmentProcessor* GrConvexPolyEffect::Create(GrPrimitiveEdgeType type, const SkPath& path,
243 const SkVector* offset) {
244 if (kHairlineAA_GrProcessorEdgeType == type) {
halcanary96fcdcc2015-08-27 07:41:13 -0700245 return nullptr;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000246 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000247 if (path.getSegmentMasks() != SkPath::kLine_SegmentMask ||
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000248 !path.isConvex()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700249 return nullptr;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000250 }
251
252 if (path.countPoints() > kMaxEdges) {
halcanary96fcdcc2015-08-27 07:41:13 -0700253 return nullptr;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000254 }
255
256 SkPoint pts[kMaxEdges];
257 SkScalar edges[3 * kMaxEdges];
258
reed026beb52015-06-10 14:23:15 -0700259 SkPathPriv::FirstDirection dir;
260 SkAssertResult(SkPathPriv::CheapComputeFirstDirection(path, &dir));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000261
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000262 SkVector t;
halcanary96fcdcc2015-08-27 07:41:13 -0700263 if (nullptr == offset) {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000264 t.set(0, 0);
265 } else {
266 t = *offset;
267 }
268
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000269 int count = path.getPoints(pts, kMaxEdges);
270 int n = 0;
271 for (int lastPt = count - 1, i = 0; i < count; lastPt = i++) {
272 if (pts[lastPt] != pts[i]) {
273 SkVector v = pts[i] - pts[lastPt];
274 v.normalize();
reed026beb52015-06-10 14:23:15 -0700275 if (SkPathPriv::kCCW_FirstDirection == dir) {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000276 edges[3 * n] = v.fY;
277 edges[3 * n + 1] = -v.fX;
278 } else {
279 edges[3 * n] = -v.fY;
280 edges[3 * n + 1] = v.fX;
281 }
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000282 SkPoint p = pts[i] + t;
283 edges[3 * n + 2] = -(edges[3 * n] * p.fX + edges[3 * n + 1] * p.fY);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000284 ++n;
285 }
286 }
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000287 if (path.isInverseFillType()) {
joshualittb0a8a372014-09-23 09:50:21 -0700288 type = GrInvertProcessorEdgeType(type);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000289 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000290 return Create(type, n, edges);
291}
292
joshualittb0a8a372014-09-23 09:50:21 -0700293GrFragmentProcessor* GrConvexPolyEffect::Create(GrPrimitiveEdgeType edgeType, const SkRect& rect) {
294 if (kHairlineAA_GrProcessorEdgeType == edgeType){
halcanary96fcdcc2015-08-27 07:41:13 -0700295 return nullptr;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000296 }
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000297 return AARectEffect::Create(edgeType, rect);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000298}
299
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000300GrConvexPolyEffect::~GrConvexPolyEffect() {}
301
egdaniel605dd0f2014-11-12 08:35:25 -0800302void GrConvexPolyEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
joshualitt56995b52014-12-11 15:44:02 -0800303 inout->mulByUnknownSingleComponent();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000304}
305
egdaniel57d3b032015-11-13 11:57:27 -0800306void GrConvexPolyEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
307 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800308 GrGLConvexPolyEffect::GenKey(*this, caps, b);
309}
310
egdaniel57d3b032015-11-13 11:57:27 -0800311GrGLSLFragmentProcessor* GrConvexPolyEffect::onCreateGLSLInstance() const {
halcanary385fe4d2015-08-26 13:07:48 -0700312 return new GrGLConvexPolyEffect(*this);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000313}
314
joshualittb0a8a372014-09-23 09:50:21 -0700315GrConvexPolyEffect::GrConvexPolyEffect(GrPrimitiveEdgeType edgeType, int n, const SkScalar edges[])
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000316 : fEdgeType(edgeType)
317 , fEdgeCount(n) {
joshualitteb2a6762014-12-04 11:35:33 -0800318 this->initClassID<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000319 // Factory function should have already ensured this.
320 SkASSERT(n <= kMaxEdges);
321 memcpy(fEdges, edges, 3 * n * sizeof(SkScalar));
322 // Outset the edges by 0.5 so that a pixel with center on an edge is 50% covered in the AA case
323 // and 100% covered in the non-AA case.
324 for (int i = 0; i < n; ++i) {
325 fEdges[3 * i + 2] += SK_ScalarHalf;
326 }
327 this->setWillReadFragmentPosition();
328}
329
bsalomon0e08fc12014-10-15 08:19:04 -0700330bool GrConvexPolyEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700331 const GrConvexPolyEffect& cpe = other.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000332 // ignore the fact that 0 == -0 and just use memcmp.
333 return (cpe.fEdgeType == fEdgeType && cpe.fEdgeCount == fEdgeCount &&
334 0 == memcmp(cpe.fEdges, fEdges, 3 * fEdgeCount * sizeof(SkScalar)));
335}
336
337//////////////////////////////////////////////////////////////////////////////
338
joshualittb0a8a372014-09-23 09:50:21 -0700339GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvexPolyEffect);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000340
bsalomonc21b09e2015-08-28 18:46:56 -0700341const GrFragmentProcessor* GrConvexPolyEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700342 int count = d->fRandom->nextULessThan(kMaxEdges) + 1;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000343 SkScalar edges[kMaxEdges * 3];
344 for (int i = 0; i < 3 * count; ++i) {
joshualitt0067ff52015-07-08 14:26:19 -0700345 edges[i] = d->fRandom->nextSScalar1();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000346 }
347
joshualittb0a8a372014-09-23 09:50:21 -0700348 GrFragmentProcessor* fp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000349 do {
joshualittb0a8a372014-09-23 09:50:21 -0700350 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(
joshualitt0067ff52015-07-08 14:26:19 -0700351 d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt));
joshualittb0a8a372014-09-23 09:50:21 -0700352 fp = GrConvexPolyEffect::Create(edgeType, count, edges);
halcanary96fcdcc2015-08-27 07:41:13 -0700353 } while (nullptr == fp);
joshualittb0a8a372014-09-23 09:50:21 -0700354 return fp;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000355}