blob: 838dff8e3a73576e60468b074a521ac44a5097d0 [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"
reed026beb52015-06-10 14:23:15 -07009#include "SkPathPriv.h"
bsalomon7888de02016-03-28 15:04:45 -070010#include "effects/GrConstColorProcessor.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"
Brian Salomon94efbf52016-11-29 13:43:05 -050015#include "../private/GrGLSL.h"
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000016
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000017//////////////////////////////////////////////////////////////////////////////
joshualittb0a8a372014-09-23 09:50:21 -070018class AARectEffect : public GrFragmentProcessor {
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000019public:
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000020 const SkRect& getRect() const { return fRect; }
21
bungeman06ca8ec2016-06-09 08:01:03 -070022 static sk_sp<GrFragmentProcessor> Make(GrPrimitiveEdgeType edgeType, const SkRect& rect) {
23 return sk_sp<GrFragmentProcessor>(new AARectEffect(edgeType, rect));
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000024 }
25
joshualittb0a8a372014-09-23 09:50:21 -070026 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +000027
mtklein36352bf2015-03-25 18:17:31 -070028 const char* name() const override { return "AARect"; }
joshualitteb2a6762014-12-04 11:35:33 -080029
Brian Salomon94efbf52016-11-29 13:43:05 -050030 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
joshualitteb2a6762014-12-04 11:35:33 -080031
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000032private:
joshualitteb2a6762014-12-04 11:35:33 -080033 AARectEffect(GrPrimitiveEdgeType edgeType, const SkRect& rect)
Brian Salomonf3b995b2017-02-15 10:22:23 -050034 : INHERITED(kCompatibleWithCoverageAsAlpha_OptimizationFlag)
35 , fRect(rect)
36 , fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -080037 this->initClassID<AARectEffect>();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000038 }
39
egdaniel57d3b032015-11-13 11:57:27 -080040 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -070041
mtklein36352bf2015-03-25 18:17:31 -070042 bool onIsEqual(const GrFragmentProcessor& other) const override {
joshualitt49586be2014-09-16 08:21:41 -070043 const AARectEffect& aare = other.cast<AARectEffect>();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000044 return fRect == aare.fRect;
45 }
46
joshualittb0a8a372014-09-23 09:50:21 -070047 SkRect fRect;
48 GrPrimitiveEdgeType fEdgeType;
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +000049
joshualittb0a8a372014-09-23 09:50:21 -070050 typedef GrFragmentProcessor INHERITED;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000051
joshualittb0a8a372014-09-23 09:50:21 -070052 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000053
54};
55
joshualittb0a8a372014-09-23 09:50:21 -070056GR_DEFINE_FRAGMENT_PROCESSOR_TEST(AARectEffect);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000057
Hal Canary6f6961e2017-01-31 13:50:44 -050058#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -070059sk_sp<GrFragmentProcessor> AARectEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -070060 SkRect rect = SkRect::MakeLTRB(d->fRandom->nextSScalar1(),
61 d->fRandom->nextSScalar1(),
62 d->fRandom->nextSScalar1(),
63 d->fRandom->nextSScalar1());
bungeman06ca8ec2016-06-09 08:01:03 -070064 sk_sp<GrFragmentProcessor> fp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000065 do {
joshualitt0067ff52015-07-08 14:26:19 -070066 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(
67 d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt));
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000068
bungeman06ca8ec2016-06-09 08:01:03 -070069 fp = AARectEffect::Make(edgeType, rect);
halcanary96fcdcc2015-08-27 07:41:13 -070070 } while (nullptr == fp);
joshualittb0a8a372014-09-23 09:50:21 -070071 return fp;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000072}
Hal Canary6f6961e2017-01-31 13:50:44 -050073#endif
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000074
75//////////////////////////////////////////////////////////////////////////////
76
egdaniel64c47282015-11-13 06:54:19 -080077class GLAARectEffect : public GrGLSLFragmentProcessor {
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000078public:
robertphillips9cdb9922016-02-03 12:25:40 -080079 GLAARectEffect() {
80 fPrevRect.fLeft = SK_ScalarNaN;
81 }
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000082
robertphillips9cdb9922016-02-03 12:25:40 -080083 void emitCode(EmitArgs&) override;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000084
Brian Salomon94efbf52016-11-29 13:43:05 -050085 static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000086
wangyixb1daa862015-08-18 11:29:31 -070087protected:
egdaniel018fb622015-10-28 07:26:40 -070088 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000089
90private:
egdaniel018fb622015-10-28 07:26:40 -070091 GrGLSLProgramDataManager::UniformHandle fRectUniform;
robertphillipsbf536af2016-02-04 06:11:53 -080092 SkRect fPrevRect;
93
egdaniel64c47282015-11-13 06:54:19 -080094 typedef GrGLSLFragmentProcessor INHERITED;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000095};
96
wangyix7c157a92015-07-22 15:08:53 -070097void GLAARectEffect::emitCode(EmitArgs& args) {
98 const AARectEffect& aare = args.fFp.cast<AARectEffect>();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000099 const char *rectName;
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000100 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
101 // respectively.
cdalton5e58cee2016-02-11 12:49:47 -0800102 fRectUniform = args.fUniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800103 kVec4f_GrSLType,
104 kDefault_GrSLPrecision,
105 "rect",
106 &rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700107
cdalton85285412016-02-18 12:37:07 -0800108 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualittb0a8a372014-09-23 09:50:21 -0700109 if (GrProcessorEdgeTypeIsAA(aare.getEdgeType())) {
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000110 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
111 // numbers, xSub and ySub.
egdaniel4ca2e602015-11-18 08:01:26 -0800112 fragBuilder->codeAppend("\t\tfloat xSub, ySub;\n");
Ethan Nicholas38657112017-02-09 17:01:22 -0500113 fragBuilder->codeAppendf("\t\txSub = min(sk_FragCoord.x - %s.x, 0.0);\n", rectName);
114 fragBuilder->codeAppendf("\t\txSub += min(%s.z - sk_FragCoord.x, 0.0);\n", rectName);
115 fragBuilder->codeAppendf("\t\tySub = min(sk_FragCoord.y - %s.y, 0.0);\n", rectName);
116 fragBuilder->codeAppendf("\t\tySub += min(%s.w - sk_FragCoord.y, 0.0);\n", rectName);
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000117 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
118 // covered.
egdaniel4ca2e602015-11-18 08:01:26 -0800119 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 +0000120 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800121 fragBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
Ethan Nicholas38657112017-02-09 17:01:22 -0500122 fragBuilder->codeAppendf("\t\talpha *= (sk_FragCoord.x - %s.x) > -0.5 ? 1.0 : 0.0;\n",
123 rectName);
124 fragBuilder->codeAppendf("\t\talpha *= (%s.z - sk_FragCoord.x) > -0.5 ? 1.0 : 0.0;\n",
125 rectName);
126 fragBuilder->codeAppendf("\t\talpha *= (sk_FragCoord.y - %s.y) > -0.5 ? 1.0 : 0.0;\n",
127 rectName);
128 fragBuilder->codeAppendf("\t\talpha *= (%s.w - sk_FragCoord.y) > -0.5 ? 1.0 : 0.0;\n",
129 rectName);
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())) {
egdaniel4ca2e602015-11-18 08:01:26 -0800133 fragBuilder->codeAppend("\t\talpha = 1.0 - alpha;\n");
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000134 }
egdaniel4ca2e602015-11-18 08:01:26 -0800135 fragBuilder->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
egdaniel018fb622015-10-28 07:26:40 -0700139void GLAARectEffect::onSetData(const GrGLSLProgramDataManager& pdman,
140 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
Brian Salomon94efbf52016-11-29 13:43:05 -0500150void GLAARectEffect::GenKey(const GrProcessor& processor, const GrShaderCaps&,
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
Brian Salomon94efbf52016-11-29 13:43:05 -0500156void AARectEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800157 GLAARectEffect::GenKey(*this, caps, b);
158}
159
egdaniel57d3b032015-11-13 11:57:27 -0800160GrGLSLFragmentProcessor* AARectEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -0800161 return new GLAARectEffect;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000162}
163
164//////////////////////////////////////////////////////////////////////////////
165
egdaniel64c47282015-11-13 06:54:19 -0800166class GrGLConvexPolyEffect : public GrGLSLFragmentProcessor {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000167public:
robertphillips9cdb9922016-02-03 12:25:40 -0800168 GrGLConvexPolyEffect() {
169 fPrevEdges[0] = SK_ScalarNaN;
170 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000171
robertphillips9cdb9922016-02-03 12:25:40 -0800172 void emitCode(EmitArgs&) override;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000173
Brian Salomon94efbf52016-11-29 13:43:05 -0500174 static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000175
wangyixb1daa862015-08-18 11:29:31 -0700176protected:
egdaniel018fb622015-10-28 07:26:40 -0700177 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000178
179private:
egdaniel018fb622015-10-28 07:26:40 -0700180 GrGLSLProgramDataManager::UniformHandle fEdgeUniform;
robertphillipsbf536af2016-02-04 06:11:53 -0800181 SkScalar fPrevEdges[3 * GrConvexPolyEffect::kMaxEdges];
egdaniel64c47282015-11-13 06:54:19 -0800182 typedef GrGLSLFragmentProcessor INHERITED;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000183};
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;
cdalton5e58cee2016-02-11 12:49:47 -0800189 fEdgeUniform = args.fUniformHandler->addUniformArray(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800190 kVec3f_GrSLType,
191 kDefault_GrSLPrecision,
192 "edges",
193 cpe.getEdgeCount(),
194 &edgeArrayName);
cdalton85285412016-02-18 12:37:07 -0800195 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
egdaniel4ca2e602015-11-18 08:01:26 -0800196 fragBuilder->codeAppend("\t\tfloat alpha = 1.0;\n");
197 fragBuilder->codeAppend("\t\tfloat edge;\n");
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000198 for (int i = 0; i < cpe.getEdgeCount(); ++i) {
Ethan Nicholas38657112017-02-09 17:01:22 -0500199 fragBuilder->codeAppendf("\t\tedge = dot(%s[%d], vec3(sk_FragCoord.x, sk_FragCoord.y, "
200 "1));\n",
201 edgeArrayName, i);
joshualittb0a8a372014-09-23 09:50:21 -0700202 if (GrProcessorEdgeTypeIsAA(cpe.getEdgeType())) {
egdaniel4ca2e602015-11-18 08:01:26 -0800203 fragBuilder->codeAppend("\t\tedge = clamp(edge, 0.0, 1.0);\n");
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000204 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800205 fragBuilder->codeAppend("\t\tedge = edge >= 0.5 ? 1.0 : 0.0;\n");
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000206 }
egdaniel4ca2e602015-11-18 08:01:26 -0800207 fragBuilder->codeAppend("\t\talpha *= edge;\n");
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000208 }
209
joshualittb0a8a372014-09-23 09:50:21 -0700210 if (GrProcessorEdgeTypeIsInverseFill(cpe.getEdgeType())) {
egdaniel4ca2e602015-11-18 08:01:26 -0800211 fragBuilder->codeAppend("\talpha = 1.0 - alpha;\n");
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000212 }
egdaniel4ca2e602015-11-18 08:01:26 -0800213 fragBuilder->codeAppendf("\t%s = %s;\n", args.fOutputColor,
214 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha")).c_str());
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000215}
216
egdaniel018fb622015-10-28 07:26:40 -0700217void GrGLConvexPolyEffect::onSetData(const GrGLSLProgramDataManager& pdman,
218 const GrProcessor& effect) {
joshualitt49586be2014-09-16 08:21:41 -0700219 const GrConvexPolyEffect& cpe = effect.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000220 size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar);
221 if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) {
kkinnunen7510b222014-07-30 00:04:16 -0700222 pdman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges());
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000223 memcpy(fPrevEdges, cpe.getEdges(), byteSize);
224 }
225}
226
Brian Salomon94efbf52016-11-29 13:43:05 -0500227void GrGLConvexPolyEffect::GenKey(const GrProcessor& processor, const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700228 GrProcessorKeyBuilder* b) {
229 const GrConvexPolyEffect& cpe = processor.cast<GrConvexPolyEffect>();
230 GR_STATIC_ASSERT(kGrProcessorEdgeTypeCnt <= 8);
bsalomon63e99f72014-07-21 08:03:14 -0700231 uint32_t key = (cpe.getEdgeCount() << 3) | cpe.getEdgeType();
232 b->add32(key);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000233}
234
235//////////////////////////////////////////////////////////////////////////////
236
bungeman06ca8ec2016-06-09 08:01:03 -0700237sk_sp<GrFragmentProcessor> GrConvexPolyEffect::Make(GrPrimitiveEdgeType type, const SkPath& path,
238 const SkVector* offset) {
joshualittb0a8a372014-09-23 09:50:21 -0700239 if (kHairlineAA_GrProcessorEdgeType == type) {
halcanary96fcdcc2015-08-27 07:41:13 -0700240 return nullptr;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000241 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000242 if (path.getSegmentMasks() != SkPath::kLine_SegmentMask ||
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000243 !path.isConvex()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700244 return nullptr;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000245 }
246
reed026beb52015-06-10 14:23:15 -0700247 SkPathPriv::FirstDirection dir;
bsalomon7888de02016-03-28 15:04:45 -0700248 // The only way this should fail is if the clip is effectively a infinitely thin line. In that
249 // case nothing is inside the clip. It'd be nice to detect this at a higher level and either
250 // skip the draw or omit the clip element.
251 if (!SkPathPriv::CheapComputeFirstDirection(path, &dir)) {
252 if (GrProcessorEdgeTypeIsInverseFill(type)) {
Brian Osman618d3042016-10-25 10:51:28 -0400253 return GrConstColorProcessor::Make(GrColor4f::OpaqueWhite(),
bungeman06ca8ec2016-06-09 08:01:03 -0700254 GrConstColorProcessor::kModulateRGBA_InputMode);
bsalomon7888de02016-03-28 15:04:45 -0700255 }
Brian Salomoneb628292017-02-15 14:12:26 -0500256 // This could use kIgnore instead of kModulateRGBA but it would trigger a debug print
257 // about a coverage processor not being compatible with the alpha-as-coverage optimization.
258 // We don't really care about this unlikely case so we just use kModulateRGBA to suppress
259 // the print.
Brian Osman618d3042016-10-25 10:51:28 -0400260 return GrConstColorProcessor::Make(GrColor4f::TransparentBlack(),
Brian Salomoneb628292017-02-15 14:12:26 -0500261 GrConstColorProcessor::kModulateRGBA_InputMode);
bsalomon7888de02016-03-28 15:04:45 -0700262 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000263
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000264 SkVector t;
halcanary96fcdcc2015-08-27 07:41:13 -0700265 if (nullptr == offset) {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000266 t.set(0, 0);
267 } else {
268 t = *offset;
269 }
270
lsalzmand15947e2016-05-31 09:46:00 -0700271 SkScalar edges[3 * kMaxEdges];
272 SkPoint pts[4];
273 SkPath::Verb verb;
274 SkPath::Iter iter(path, true);
275
276 // SkPath considers itself convex so long as there is a convex contour within it,
277 // regardless of any degenerate contours such as a string of moveTos before it.
278 // Iterate here to consume any degenerate contours and only process the points
279 // on the actual convex contour.
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000280 int n = 0;
lsalzmand15947e2016-05-31 09:46:00 -0700281 while ((verb = iter.next(pts, true, true)) != SkPath::kDone_Verb) {
282 switch (verb) {
283 case SkPath::kMove_Verb:
284 SkASSERT(n == 0);
285 case SkPath::kClose_Verb:
286 break;
287 case SkPath::kLine_Verb: {
288 if (n >= kMaxEdges) {
289 return nullptr;
290 }
291 SkVector v = pts[1] - pts[0];
292 v.normalize();
293 if (SkPathPriv::kCCW_FirstDirection == dir) {
294 edges[3 * n] = v.fY;
295 edges[3 * n + 1] = -v.fX;
296 } else {
297 edges[3 * n] = -v.fY;
298 edges[3 * n + 1] = v.fX;
299 }
300 SkPoint p = pts[1] + t;
301 edges[3 * n + 2] = -(edges[3 * n] * p.fX + edges[3 * n + 1] * p.fY);
302 ++n;
303 break;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000304 }
lsalzmand15947e2016-05-31 09:46:00 -0700305 default:
306 return nullptr;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000307 }
308 }
lsalzmand15947e2016-05-31 09:46:00 -0700309
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000310 if (path.isInverseFillType()) {
joshualittb0a8a372014-09-23 09:50:21 -0700311 type = GrInvertProcessorEdgeType(type);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000312 }
bungeman06ca8ec2016-06-09 08:01:03 -0700313 return Make(type, n, edges);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000314}
315
bungeman06ca8ec2016-06-09 08:01:03 -0700316sk_sp<GrFragmentProcessor> GrConvexPolyEffect::Make(GrPrimitiveEdgeType edgeType,
317 const SkRect& rect) {
joshualittb0a8a372014-09-23 09:50:21 -0700318 if (kHairlineAA_GrProcessorEdgeType == edgeType){
halcanary96fcdcc2015-08-27 07:41:13 -0700319 return nullptr;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000320 }
bungeman06ca8ec2016-06-09 08:01:03 -0700321 return AARectEffect::Make(edgeType, rect);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000322}
323
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000324GrConvexPolyEffect::~GrConvexPolyEffect() {}
325
Brian Salomon94efbf52016-11-29 13:43:05 -0500326void GrConvexPolyEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800327 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800328 GrGLConvexPolyEffect::GenKey(*this, caps, b);
329}
330
egdaniel57d3b032015-11-13 11:57:27 -0800331GrGLSLFragmentProcessor* GrConvexPolyEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -0800332 return new GrGLConvexPolyEffect;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000333}
334
joshualittb0a8a372014-09-23 09:50:21 -0700335GrConvexPolyEffect::GrConvexPolyEffect(GrPrimitiveEdgeType edgeType, int n, const SkScalar edges[])
Brian Salomonf3b995b2017-02-15 10:22:23 -0500336 : INHERITED(kCompatibleWithCoverageAsAlpha_OptimizationFlag)
337 , fEdgeType(edgeType)
338 , fEdgeCount(n) {
joshualitteb2a6762014-12-04 11:35:33 -0800339 this->initClassID<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000340 // Factory function should have already ensured this.
341 SkASSERT(n <= kMaxEdges);
342 memcpy(fEdges, edges, 3 * n * sizeof(SkScalar));
343 // Outset the edges by 0.5 so that a pixel with center on an edge is 50% covered in the AA case
344 // and 100% covered in the non-AA case.
345 for (int i = 0; i < n; ++i) {
346 fEdges[3 * i + 2] += SK_ScalarHalf;
347 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000348}
349
bsalomon0e08fc12014-10-15 08:19:04 -0700350bool GrConvexPolyEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700351 const GrConvexPolyEffect& cpe = other.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000352 // ignore the fact that 0 == -0 and just use memcmp.
353 return (cpe.fEdgeType == fEdgeType && cpe.fEdgeCount == fEdgeCount &&
354 0 == memcmp(cpe.fEdges, fEdges, 3 * fEdgeCount * sizeof(SkScalar)));
355}
356
357//////////////////////////////////////////////////////////////////////////////
358
joshualittb0a8a372014-09-23 09:50:21 -0700359GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvexPolyEffect);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000360
Hal Canary6f6961e2017-01-31 13:50:44 -0500361#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700362sk_sp<GrFragmentProcessor> GrConvexPolyEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700363 int count = d->fRandom->nextULessThan(kMaxEdges) + 1;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000364 SkScalar edges[kMaxEdges * 3];
365 for (int i = 0; i < 3 * count; ++i) {
joshualitt0067ff52015-07-08 14:26:19 -0700366 edges[i] = d->fRandom->nextSScalar1();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000367 }
368
bungeman06ca8ec2016-06-09 08:01:03 -0700369 sk_sp<GrFragmentProcessor> fp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000370 do {
joshualittb0a8a372014-09-23 09:50:21 -0700371 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(
joshualitt0067ff52015-07-08 14:26:19 -0700372 d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt));
bungeman06ca8ec2016-06-09 08:01:03 -0700373 fp = GrConvexPolyEffect::Make(edgeType, count, edges);
halcanary96fcdcc2015-08-27 07:41:13 -0700374 } while (nullptr == fp);
joshualittb0a8a372014-09-23 09:50:21 -0700375 return fp;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000376}
Hal Canary6f6961e2017-01-31 13:50:44 -0500377#endif