blob: f95d1a2fb3d1e060b4e0163924e6584fc34d277c [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:
robertphillips9cdb9922016-02-03 12:25:40 -080084 GLAARectEffect() {
85 fPrevRect.fLeft = SK_ScalarNaN;
86 }
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000087
robertphillips9cdb9922016-02-03 12:25:40 -080088 void emitCode(EmitArgs&) override;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000089
jvanverthcfc18862015-04-28 08:48:20 -070090 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000091
wangyixb1daa862015-08-18 11:29:31 -070092protected:
egdaniel018fb622015-10-28 07:26:40 -070093 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000094
95private:
egdaniel018fb622015-10-28 07:26:40 -070096 GrGLSLProgramDataManager::UniformHandle fRectUniform;
kkinnunen7510b222014-07-30 00:04:16 -070097 SkRect fPrevRect;
egdaniel64c47282015-11-13 06:54:19 -080098 typedef GrGLSLFragmentProcessor INHERITED;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000099};
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.
egdaniel7ea439b2015-12-03 09:20:44 -0800106 fRectUniform = args.fUniformHandler->addUniform(GrGLSLUniformHandler::kFragment_Visibility,
107 kVec4f_GrSLType,
108 kDefault_GrSLPrecision,
109 "rect",
110 &rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700111
egdaniel4ca2e602015-11-18 08:01:26 -0800112 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
113 const char* fragmentPos = fragBuilder->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.
egdaniel4ca2e602015-11-18 08:01:26 -0800117 fragBuilder->codeAppend("\t\tfloat xSub, ySub;\n");
118 fragBuilder->codeAppendf("\t\txSub = min(%s.x - %s.x, 0.0);\n", fragmentPos, rectName);
119 fragBuilder->codeAppendf("\t\txSub += min(%s.z - %s.x, 0.0);\n", rectName, fragmentPos);
120 fragBuilder->codeAppendf("\t\tySub = min(%s.y - %s.y, 0.0);\n", fragmentPos, rectName);
121 fragBuilder->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.
egdaniel4ca2e602015-11-18 08:01:26 -0800124 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 +0000125 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800126 fragBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
127 fragBuilder->codeAppendf("\t\talpha *= (%s.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", fragmentPos, rectName);
128 fragBuilder->codeAppendf("\t\talpha *= (%s.z - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName, fragmentPos);
129 fragBuilder->codeAppendf("\t\talpha *= (%s.y - %s.y) > -0.5 ? 1.0 : 0.0;\n", fragmentPos, rectName);
130 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 +0000131 }
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000132
joshualittb0a8a372014-09-23 09:50:21 -0700133 if (GrProcessorEdgeTypeIsInverseFill(aare.getEdgeType())) {
egdaniel4ca2e602015-11-18 08:01:26 -0800134 fragBuilder->codeAppend("\t\talpha = 1.0 - alpha;\n");
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000135 }
egdaniel4ca2e602015-11-18 08:01:26 -0800136 fragBuilder->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
egdaniel018fb622015-10-28 07:26:40 -0700140void GLAARectEffect::onSetData(const GrGLSLProgramDataManager& pdman,
141 const GrProcessor& processor) {
joshualittb0a8a372014-09-23 09:50:21 -0700142 const AARectEffect& aare = processor.cast<AARectEffect>();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000143 const SkRect& rect = aare.getRect();
144 if (rect != fPrevRect) {
robertphillips7f14c9b2015-01-30 14:44:22 -0800145 pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
146 rect.fRight - 0.5f, rect.fBottom - 0.5f);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000147 fPrevRect = rect;
148 }
149}
150
jvanverthcfc18862015-04-28 08:48:20 -0700151void GLAARectEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700152 GrProcessorKeyBuilder* b) {
153 const AARectEffect& aare = processor.cast<AARectEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700154 b->add32(aare.getEdgeType());
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000155}
156
egdaniel57d3b032015-11-13 11:57:27 -0800157void AARectEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800158 GLAARectEffect::GenKey(*this, caps, b);
159}
160
egdaniel57d3b032015-11-13 11:57:27 -0800161GrGLSLFragmentProcessor* AARectEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -0800162 return new GLAARectEffect;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000163}
164
165//////////////////////////////////////////////////////////////////////////////
166
egdaniel64c47282015-11-13 06:54:19 -0800167class GrGLConvexPolyEffect : public GrGLSLFragmentProcessor {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000168public:
robertphillips9cdb9922016-02-03 12:25:40 -0800169 GrGLConvexPolyEffect() {
170 fPrevEdges[0] = SK_ScalarNaN;
171 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000172
robertphillips9cdb9922016-02-03 12:25:40 -0800173 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
wangyix7c157a92015-07-22 15:08:53 -0700186void GrGLConvexPolyEffect::emitCode(EmitArgs& args) {
187 const GrConvexPolyEffect& cpe = args.fFp.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000188
189 const char *edgeArrayName;
egdaniel7ea439b2015-12-03 09:20:44 -0800190 fEdgeUniform = args.fUniformHandler->addUniformArray(GrGLSLUniformHandler::kFragment_Visibility,
191 kVec3f_GrSLType,
192 kDefault_GrSLPrecision,
193 "edges",
194 cpe.getEdgeCount(),
195 &edgeArrayName);
egdaniel4ca2e602015-11-18 08:01:26 -0800196 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
197 fragBuilder->codeAppend("\t\tfloat alpha = 1.0;\n");
198 fragBuilder->codeAppend("\t\tfloat edge;\n");
199 const char* fragmentPos = fragBuilder->fragmentPosition();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000200 for (int i = 0; i < cpe.getEdgeCount(); ++i) {
egdaniel4ca2e602015-11-18 08:01:26 -0800201 fragBuilder->codeAppendf("\t\tedge = dot(%s[%d], vec3(%s.x, %s.y, 1));\n",
202 edgeArrayName, i, fragmentPos, fragmentPos);
joshualittb0a8a372014-09-23 09:50:21 -0700203 if (GrProcessorEdgeTypeIsAA(cpe.getEdgeType())) {
egdaniel4ca2e602015-11-18 08:01:26 -0800204 fragBuilder->codeAppend("\t\tedge = clamp(edge, 0.0, 1.0);\n");
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000205 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800206 fragBuilder->codeAppend("\t\tedge = edge >= 0.5 ? 1.0 : 0.0;\n");
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000207 }
egdaniel4ca2e602015-11-18 08:01:26 -0800208 fragBuilder->codeAppend("\t\talpha *= edge;\n");
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000209 }
210
joshualittb0a8a372014-09-23 09:50:21 -0700211 if (GrProcessorEdgeTypeIsInverseFill(cpe.getEdgeType())) {
egdaniel4ca2e602015-11-18 08:01:26 -0800212 fragBuilder->codeAppend("\talpha = 1.0 - alpha;\n");
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000213 }
egdaniel4ca2e602015-11-18 08:01:26 -0800214 fragBuilder->codeAppendf("\t%s = %s;\n", args.fOutputColor,
215 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha")).c_str());
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000216}
217
egdaniel018fb622015-10-28 07:26:40 -0700218void GrGLConvexPolyEffect::onSetData(const GrGLSLProgramDataManager& pdman,
219 const GrProcessor& effect) {
joshualitt49586be2014-09-16 08:21:41 -0700220 const GrConvexPolyEffect& cpe = effect.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000221 size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar);
222 if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) {
kkinnunen7510b222014-07-30 00:04:16 -0700223 pdman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges());
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000224 memcpy(fPrevEdges, cpe.getEdges(), byteSize);
225 }
226}
227
jvanverthcfc18862015-04-28 08:48:20 -0700228void GrGLConvexPolyEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700229 GrProcessorKeyBuilder* b) {
230 const GrConvexPolyEffect& cpe = processor.cast<GrConvexPolyEffect>();
231 GR_STATIC_ASSERT(kGrProcessorEdgeTypeCnt <= 8);
bsalomon63e99f72014-07-21 08:03:14 -0700232 uint32_t key = (cpe.getEdgeCount() << 3) | cpe.getEdgeType();
233 b->add32(key);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000234}
235
236//////////////////////////////////////////////////////////////////////////////
237
joshualittb0a8a372014-09-23 09:50:21 -0700238GrFragmentProcessor* GrConvexPolyEffect::Create(GrPrimitiveEdgeType type, const SkPath& path,
239 const SkVector* offset) {
240 if (kHairlineAA_GrProcessorEdgeType == type) {
halcanary96fcdcc2015-08-27 07:41:13 -0700241 return nullptr;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000242 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000243 if (path.getSegmentMasks() != SkPath::kLine_SegmentMask ||
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000244 !path.isConvex()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700245 return nullptr;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000246 }
247
248 if (path.countPoints() > kMaxEdges) {
halcanary96fcdcc2015-08-27 07:41:13 -0700249 return nullptr;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000250 }
251
252 SkPoint pts[kMaxEdges];
253 SkScalar edges[3 * kMaxEdges];
254
reed026beb52015-06-10 14:23:15 -0700255 SkPathPriv::FirstDirection dir;
256 SkAssertResult(SkPathPriv::CheapComputeFirstDirection(path, &dir));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000257
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000258 SkVector t;
halcanary96fcdcc2015-08-27 07:41:13 -0700259 if (nullptr == offset) {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000260 t.set(0, 0);
261 } else {
262 t = *offset;
263 }
264
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000265 int count = path.getPoints(pts, kMaxEdges);
266 int n = 0;
267 for (int lastPt = count - 1, i = 0; i < count; lastPt = i++) {
268 if (pts[lastPt] != pts[i]) {
269 SkVector v = pts[i] - pts[lastPt];
270 v.normalize();
reed026beb52015-06-10 14:23:15 -0700271 if (SkPathPriv::kCCW_FirstDirection == dir) {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000272 edges[3 * n] = v.fY;
273 edges[3 * n + 1] = -v.fX;
274 } else {
275 edges[3 * n] = -v.fY;
276 edges[3 * n + 1] = v.fX;
277 }
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000278 SkPoint p = pts[i] + t;
279 edges[3 * n + 2] = -(edges[3 * n] * p.fX + edges[3 * n + 1] * p.fY);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000280 ++n;
281 }
282 }
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000283 if (path.isInverseFillType()) {
joshualittb0a8a372014-09-23 09:50:21 -0700284 type = GrInvertProcessorEdgeType(type);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000285 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000286 return Create(type, n, edges);
287}
288
joshualittb0a8a372014-09-23 09:50:21 -0700289GrFragmentProcessor* GrConvexPolyEffect::Create(GrPrimitiveEdgeType edgeType, const SkRect& rect) {
290 if (kHairlineAA_GrProcessorEdgeType == edgeType){
halcanary96fcdcc2015-08-27 07:41:13 -0700291 return nullptr;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000292 }
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000293 return AARectEffect::Create(edgeType, rect);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000294}
295
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000296GrConvexPolyEffect::~GrConvexPolyEffect() {}
297
egdaniel605dd0f2014-11-12 08:35:25 -0800298void GrConvexPolyEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
joshualitt56995b52014-12-11 15:44:02 -0800299 inout->mulByUnknownSingleComponent();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000300}
301
egdaniel57d3b032015-11-13 11:57:27 -0800302void GrConvexPolyEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
303 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800304 GrGLConvexPolyEffect::GenKey(*this, caps, b);
305}
306
egdaniel57d3b032015-11-13 11:57:27 -0800307GrGLSLFragmentProcessor* GrConvexPolyEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -0800308 return new GrGLConvexPolyEffect;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000309}
310
joshualittb0a8a372014-09-23 09:50:21 -0700311GrConvexPolyEffect::GrConvexPolyEffect(GrPrimitiveEdgeType edgeType, int n, const SkScalar edges[])
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000312 : fEdgeType(edgeType)
313 , fEdgeCount(n) {
joshualitteb2a6762014-12-04 11:35:33 -0800314 this->initClassID<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000315 // Factory function should have already ensured this.
316 SkASSERT(n <= kMaxEdges);
317 memcpy(fEdges, edges, 3 * n * sizeof(SkScalar));
318 // Outset the edges by 0.5 so that a pixel with center on an edge is 50% covered in the AA case
319 // and 100% covered in the non-AA case.
320 for (int i = 0; i < n; ++i) {
321 fEdges[3 * i + 2] += SK_ScalarHalf;
322 }
323 this->setWillReadFragmentPosition();
324}
325
bsalomon0e08fc12014-10-15 08:19:04 -0700326bool GrConvexPolyEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700327 const GrConvexPolyEffect& cpe = other.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000328 // ignore the fact that 0 == -0 and just use memcmp.
329 return (cpe.fEdgeType == fEdgeType && cpe.fEdgeCount == fEdgeCount &&
330 0 == memcmp(cpe.fEdges, fEdges, 3 * fEdgeCount * sizeof(SkScalar)));
331}
332
333//////////////////////////////////////////////////////////////////////////////
334
joshualittb0a8a372014-09-23 09:50:21 -0700335GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvexPolyEffect);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000336
bsalomonc21b09e2015-08-28 18:46:56 -0700337const GrFragmentProcessor* GrConvexPolyEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700338 int count = d->fRandom->nextULessThan(kMaxEdges) + 1;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000339 SkScalar edges[kMaxEdges * 3];
340 for (int i = 0; i < 3 * count; ++i) {
joshualitt0067ff52015-07-08 14:26:19 -0700341 edges[i] = d->fRandom->nextSScalar1();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000342 }
343
joshualittb0a8a372014-09-23 09:50:21 -0700344 GrFragmentProcessor* fp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000345 do {
joshualittb0a8a372014-09-23 09:50:21 -0700346 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(
joshualitt0067ff52015-07-08 14:26:19 -0700347 d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt));
joshualittb0a8a372014-09-23 09:50:21 -0700348 fp = GrConvexPolyEffect::Create(edgeType, count, edges);
halcanary96fcdcc2015-08-27 07:41:13 -0700349 } while (nullptr == fp);
joshualittb0a8a372014-09-23 09:50:21 -0700350 return fp;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000351}