blob: d22a3472a5def57b85457b85b18f5d32babeb883 [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"
egdanielf5294392015-10-21 07:14:17 -070011#include "gl/GrGLContext.h"
wangyix6af0c932015-07-22 10:21:17 -070012#include "gl/GrGLFragmentProcessor.h"
joshualitteb2a6762014-12-04 11:35:33 -080013#include "gl/builders/GrGLProgramBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070014#include "glsl/GrGLSLProgramDataManager.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
wangyix4b3050b2015-08-04 07:59:37 -070029 void onGetGLProcessorKey(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
wangyixb1daa862015-08-18 11:29:31 -070038 GrGLFragmentProcessor* onCreateGLInstance() const override;
39
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
joshualittb0a8a372014-09-23 09:50:21 -070082class GLAARectEffect : public GrGLFragmentProcessor {
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;
joshualittb0a8a372014-09-23 09:50:21 -070096 typedef GrGLFragmentProcessor 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.
wangyix7c157a92015-07-22 15:08:53 -0700108 fRectUniform = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000109 kVec4f_GrSLType,
bsalomon422f56f2014-12-09 10:18:12 -0800110 kDefault_GrSLPrecision,
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000111 "rect",
112 &rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700113
wangyix7c157a92015-07-22 15:08:53 -0700114 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700115 const char* fragmentPos = fsBuilder->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.
joshualitt30ba4362014-08-21 20:18:45 -0700119 fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n");
120 fsBuilder->codeAppendf("\t\txSub = min(%s.x - %s.x, 0.0);\n", fragmentPos, rectName);
121 fsBuilder->codeAppendf("\t\txSub += min(%s.z - %s.x, 0.0);\n", rectName, fragmentPos);
122 fsBuilder->codeAppendf("\t\tySub = min(%s.y - %s.y, 0.0);\n", fragmentPos, rectName);
123 fsBuilder->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.
joshualitt30ba4362014-08-21 20:18:45 -0700126 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 +0000127 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700128 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
robertphillips7f14c9b2015-01-30 14:44:22 -0800129 fsBuilder->codeAppendf("\t\talpha *= (%s.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", fragmentPos, rectName);
130 fsBuilder->codeAppendf("\t\talpha *= (%s.z - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName, fragmentPos);
131 fsBuilder->codeAppendf("\t\talpha *= (%s.y - %s.y) > -0.5 ? 1.0 : 0.0;\n", fragmentPos, rectName);
132 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 +0000133 }
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000134
joshualittb0a8a372014-09-23 09:50:21 -0700135 if (GrProcessorEdgeTypeIsInverseFill(aare.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700136 fsBuilder->codeAppend("\t\talpha = 1.0 - alpha;\n");
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000137 }
wangyix7c157a92015-07-22 15:08:53 -0700138 fsBuilder->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
wangyix4b3050b2015-08-04 07:59:37 -0700159void AARectEffect::onGetGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800160 GLAARectEffect::GenKey(*this, caps, b);
161}
162
wangyixb1daa862015-08-18 11:29:31 -0700163GrGLFragmentProcessor* AARectEffect::onCreateGLInstance() 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
joshualittb0a8a372014-09-23 09:50:21 -0700169class GrGLConvexPolyEffect : public GrGLFragmentProcessor {
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];
joshualittb0a8a372014-09-23 09:50:21 -0700183 typedef GrGLFragmentProcessor 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;
wangyix7c157a92015-07-22 15:08:53 -0700194 fEdgeUniform = args.fBuilder->addUniformArray(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000195 kVec3f_GrSLType,
bsalomon422f56f2014-12-09 10:18:12 -0800196 kDefault_GrSLPrecision,
197 "edges",
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000198 cpe.getEdgeCount(),
199 &edgeArrayName);
wangyix7c157a92015-07-22 15:08:53 -0700200 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700201 fsBuilder->codeAppend("\t\tfloat alpha = 1.0;\n");
202 fsBuilder->codeAppend("\t\tfloat edge;\n");
203 const char* fragmentPos = fsBuilder->fragmentPosition();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000204 for (int i = 0; i < cpe.getEdgeCount(); ++i) {
robertphillips7f14c9b2015-01-30 14:44:22 -0800205 fsBuilder->codeAppendf("\t\tedge = dot(%s[%d], vec3(%s.x, %s.y, 1));\n",
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000206 edgeArrayName, i, fragmentPos, fragmentPos);
joshualittb0a8a372014-09-23 09:50:21 -0700207 if (GrProcessorEdgeTypeIsAA(cpe.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700208 fsBuilder->codeAppend("\t\tedge = clamp(edge, 0.0, 1.0);\n");
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000209 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700210 fsBuilder->codeAppend("\t\tedge = edge >= 0.5 ? 1.0 : 0.0;\n");
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000211 }
joshualitt30ba4362014-08-21 20:18:45 -0700212 fsBuilder->codeAppend("\t\talpha *= edge;\n");
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000213 }
214
commit-bot@chromium.org6dee8752014-02-07 22:39:01 +0000215 // Woe is me. See skbug.com/2149.
wangyix7c157a92015-07-22 15:08:53 -0700216 if (kTegra2_GrGLRenderer == args.fBuilder->ctxInfo().renderer()) {
joshualitt30ba4362014-08-21 20:18:45 -0700217 fsBuilder->codeAppend("\t\tif (-1.0 == alpha) {\n\t\t\tdiscard;\n\t\t}\n");
commit-bot@chromium.org6dee8752014-02-07 22:39:01 +0000218 }
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000219
joshualittb0a8a372014-09-23 09:50:21 -0700220 if (GrProcessorEdgeTypeIsInverseFill(cpe.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700221 fsBuilder->codeAppend("\talpha = 1.0 - alpha;\n");
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000222 }
wangyix7c157a92015-07-22 15:08:53 -0700223 fsBuilder->codeAppendf("\t%s = %s;\n", args.fOutputColor,
224 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha")).c_str());
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000225}
226
egdaniel018fb622015-10-28 07:26:40 -0700227void GrGLConvexPolyEffect::onSetData(const GrGLSLProgramDataManager& pdman,
228 const GrProcessor& effect) {
joshualitt49586be2014-09-16 08:21:41 -0700229 const GrConvexPolyEffect& cpe = effect.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000230 size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar);
231 if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) {
kkinnunen7510b222014-07-30 00:04:16 -0700232 pdman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges());
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000233 memcpy(fPrevEdges, cpe.getEdges(), byteSize);
234 }
235}
236
jvanverthcfc18862015-04-28 08:48:20 -0700237void GrGLConvexPolyEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700238 GrProcessorKeyBuilder* b) {
239 const GrConvexPolyEffect& cpe = processor.cast<GrConvexPolyEffect>();
240 GR_STATIC_ASSERT(kGrProcessorEdgeTypeCnt <= 8);
bsalomon63e99f72014-07-21 08:03:14 -0700241 uint32_t key = (cpe.getEdgeCount() << 3) | cpe.getEdgeType();
242 b->add32(key);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000243}
244
245//////////////////////////////////////////////////////////////////////////////
246
joshualittb0a8a372014-09-23 09:50:21 -0700247GrFragmentProcessor* GrConvexPolyEffect::Create(GrPrimitiveEdgeType type, const SkPath& path,
248 const SkVector* offset) {
249 if (kHairlineAA_GrProcessorEdgeType == type) {
halcanary96fcdcc2015-08-27 07:41:13 -0700250 return nullptr;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000251 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000252 if (path.getSegmentMasks() != SkPath::kLine_SegmentMask ||
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000253 !path.isConvex()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700254 return nullptr;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000255 }
256
257 if (path.countPoints() > kMaxEdges) {
halcanary96fcdcc2015-08-27 07:41:13 -0700258 return nullptr;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000259 }
260
261 SkPoint pts[kMaxEdges];
262 SkScalar edges[3 * kMaxEdges];
263
reed026beb52015-06-10 14:23:15 -0700264 SkPathPriv::FirstDirection dir;
265 SkAssertResult(SkPathPriv::CheapComputeFirstDirection(path, &dir));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000266
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000267 SkVector t;
halcanary96fcdcc2015-08-27 07:41:13 -0700268 if (nullptr == offset) {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000269 t.set(0, 0);
270 } else {
271 t = *offset;
272 }
273
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000274 int count = path.getPoints(pts, kMaxEdges);
275 int n = 0;
276 for (int lastPt = count - 1, i = 0; i < count; lastPt = i++) {
277 if (pts[lastPt] != pts[i]) {
278 SkVector v = pts[i] - pts[lastPt];
279 v.normalize();
reed026beb52015-06-10 14:23:15 -0700280 if (SkPathPriv::kCCW_FirstDirection == dir) {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000281 edges[3 * n] = v.fY;
282 edges[3 * n + 1] = -v.fX;
283 } else {
284 edges[3 * n] = -v.fY;
285 edges[3 * n + 1] = v.fX;
286 }
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000287 SkPoint p = pts[i] + t;
288 edges[3 * n + 2] = -(edges[3 * n] * p.fX + edges[3 * n + 1] * p.fY);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000289 ++n;
290 }
291 }
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000292 if (path.isInverseFillType()) {
joshualittb0a8a372014-09-23 09:50:21 -0700293 type = GrInvertProcessorEdgeType(type);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000294 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000295 return Create(type, n, edges);
296}
297
joshualittb0a8a372014-09-23 09:50:21 -0700298GrFragmentProcessor* GrConvexPolyEffect::Create(GrPrimitiveEdgeType edgeType, const SkRect& rect) {
299 if (kHairlineAA_GrProcessorEdgeType == edgeType){
halcanary96fcdcc2015-08-27 07:41:13 -0700300 return nullptr;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000301 }
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000302 return AARectEffect::Create(edgeType, rect);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000303}
304
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000305GrConvexPolyEffect::~GrConvexPolyEffect() {}
306
egdaniel605dd0f2014-11-12 08:35:25 -0800307void GrConvexPolyEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
joshualitt56995b52014-12-11 15:44:02 -0800308 inout->mulByUnknownSingleComponent();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000309}
310
wangyix4b3050b2015-08-04 07:59:37 -0700311void GrConvexPolyEffect::onGetGLProcessorKey(const GrGLSLCaps& caps,
joshualitteb2a6762014-12-04 11:35:33 -0800312 GrProcessorKeyBuilder* b) const {
313 GrGLConvexPolyEffect::GenKey(*this, caps, b);
314}
315
wangyixb1daa862015-08-18 11:29:31 -0700316GrGLFragmentProcessor* GrConvexPolyEffect::onCreateGLInstance() const {
halcanary385fe4d2015-08-26 13:07:48 -0700317 return new GrGLConvexPolyEffect(*this);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000318}
319
joshualittb0a8a372014-09-23 09:50:21 -0700320GrConvexPolyEffect::GrConvexPolyEffect(GrPrimitiveEdgeType edgeType, int n, const SkScalar edges[])
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000321 : fEdgeType(edgeType)
322 , fEdgeCount(n) {
joshualitteb2a6762014-12-04 11:35:33 -0800323 this->initClassID<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000324 // Factory function should have already ensured this.
325 SkASSERT(n <= kMaxEdges);
326 memcpy(fEdges, edges, 3 * n * sizeof(SkScalar));
327 // Outset the edges by 0.5 so that a pixel with center on an edge is 50% covered in the AA case
328 // and 100% covered in the non-AA case.
329 for (int i = 0; i < n; ++i) {
330 fEdges[3 * i + 2] += SK_ScalarHalf;
331 }
332 this->setWillReadFragmentPosition();
333}
334
bsalomon0e08fc12014-10-15 08:19:04 -0700335bool GrConvexPolyEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700336 const GrConvexPolyEffect& cpe = other.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000337 // ignore the fact that 0 == -0 and just use memcmp.
338 return (cpe.fEdgeType == fEdgeType && cpe.fEdgeCount == fEdgeCount &&
339 0 == memcmp(cpe.fEdges, fEdges, 3 * fEdgeCount * sizeof(SkScalar)));
340}
341
342//////////////////////////////////////////////////////////////////////////////
343
joshualittb0a8a372014-09-23 09:50:21 -0700344GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvexPolyEffect);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000345
bsalomonc21b09e2015-08-28 18:46:56 -0700346const GrFragmentProcessor* GrConvexPolyEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700347 int count = d->fRandom->nextULessThan(kMaxEdges) + 1;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000348 SkScalar edges[kMaxEdges * 3];
349 for (int i = 0; i < 3 * count; ++i) {
joshualitt0067ff52015-07-08 14:26:19 -0700350 edges[i] = d->fRandom->nextSScalar1();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000351 }
352
joshualittb0a8a372014-09-23 09:50:21 -0700353 GrFragmentProcessor* fp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000354 do {
joshualittb0a8a372014-09-23 09:50:21 -0700355 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(
joshualitt0067ff52015-07-08 14:26:19 -0700356 d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt));
joshualittb0a8a372014-09-23 09:50:21 -0700357 fp = GrConvexPolyEffect::Create(edgeType, count, edges);
halcanary96fcdcc2015-08-27 07:41:13 -0700358 } while (nullptr == fp);
joshualittb0a8a372014-09-23 09:50:21 -0700359 return fp;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000360}