blob: c373b041fea6b186b74c08084bdb1dd52819cba9 [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"
egdaniel2d721d32015-11-11 13:06:05 -080013#include "glsl/GrGLSLFragmentShaderBuilder.h"
14#include "glsl/GrGLSLProgramBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070015#include "glsl/GrGLSLProgramDataManager.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
joshualittb0a8a372014-09-23 09:50:21 -070022 static GrFragmentProcessor* Create(GrPrimitiveEdgeType edgeType, const SkRect& rect) {
halcanary385fe4d2015-08-26 13:07:48 -070023 return 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
wangyix4b3050b2015-08-04 07:59:37 -070030 void onGetGLProcessorKey(const GrGLSLCaps&, 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)
34 : fRect(rect), fEdgeType(edgeType) {
35 this->initClassID<AARectEffect>();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000036 this->setWillReadFragmentPosition();
37 }
38
wangyixb1daa862015-08-18 11:29:31 -070039 GrGLFragmentProcessor* onCreateGLInstance() const override;
40
mtklein36352bf2015-03-25 18:17:31 -070041 bool onIsEqual(const GrFragmentProcessor& other) const override {
joshualitt49586be2014-09-16 08:21:41 -070042 const AARectEffect& aare = other.cast<AARectEffect>();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000043 return fRect == aare.fRect;
44 }
45
mtklein36352bf2015-03-25 18:17:31 -070046 void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
egdaniel1a8ecdf2014-10-03 06:24:12 -070047 if (fRect.isEmpty()) {
48 // An empty rect will have no coverage anywhere.
joshualitt56995b52014-12-11 15:44:02 -080049 inout->mulByKnownSingleComponent(0);
egdaniel1a8ecdf2014-10-03 06:24:12 -070050 } else {
joshualitt56995b52014-12-11 15:44:02 -080051 inout->mulByUnknownSingleComponent();
egdaniel1a8ecdf2014-10-03 06:24:12 -070052 }
egdaniel1a8ecdf2014-10-03 06:24:12 -070053 }
54
joshualittb0a8a372014-09-23 09:50:21 -070055 SkRect fRect;
56 GrPrimitiveEdgeType fEdgeType;
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +000057
joshualittb0a8a372014-09-23 09:50:21 -070058 typedef GrFragmentProcessor INHERITED;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000059
joshualittb0a8a372014-09-23 09:50:21 -070060 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000061
62};
63
joshualittb0a8a372014-09-23 09:50:21 -070064GR_DEFINE_FRAGMENT_PROCESSOR_TEST(AARectEffect);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000065
bsalomonc21b09e2015-08-28 18:46:56 -070066const GrFragmentProcessor* AARectEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -070067 SkRect rect = SkRect::MakeLTRB(d->fRandom->nextSScalar1(),
68 d->fRandom->nextSScalar1(),
69 d->fRandom->nextSScalar1(),
70 d->fRandom->nextSScalar1());
joshualittb0a8a372014-09-23 09:50:21 -070071 GrFragmentProcessor* fp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000072 do {
joshualitt0067ff52015-07-08 14:26:19 -070073 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(
74 d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt));
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000075
joshualittb0a8a372014-09-23 09:50:21 -070076 fp = AARectEffect::Create(edgeType, rect);
halcanary96fcdcc2015-08-27 07:41:13 -070077 } while (nullptr == fp);
joshualittb0a8a372014-09-23 09:50:21 -070078 return fp;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000079}
80
81//////////////////////////////////////////////////////////////////////////////
82
joshualittb0a8a372014-09-23 09:50:21 -070083class GLAARectEffect : public GrGLFragmentProcessor {
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000084public:
joshualitteb2a6762014-12-04 11:35:33 -080085 GLAARectEffect(const GrProcessor&);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000086
wangyix7c157a92015-07-22 15:08:53 -070087 virtual void emitCode(EmitArgs&) override;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000088
jvanverthcfc18862015-04-28 08:48:20 -070089 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000090
wangyixb1daa862015-08-18 11:29:31 -070091protected:
egdaniel018fb622015-10-28 07:26:40 -070092 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000093
94private:
egdaniel018fb622015-10-28 07:26:40 -070095 GrGLSLProgramDataManager::UniformHandle fRectUniform;
kkinnunen7510b222014-07-30 00:04:16 -070096 SkRect fPrevRect;
joshualittb0a8a372014-09-23 09:50:21 -070097 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000098};
99
joshualitteb2a6762014-12-04 11:35:33 -0800100GLAARectEffect::GLAARectEffect(const GrProcessor& effect) {
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000101 fPrevRect.fLeft = SK_ScalarNaN;
102}
103
wangyix7c157a92015-07-22 15:08:53 -0700104void GLAARectEffect::emitCode(EmitArgs& args) {
105 const AARectEffect& aare = args.fFp.cast<AARectEffect>();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000106 const char *rectName;
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000107 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
108 // respectively.
egdaniel2d721d32015-11-11 13:06:05 -0800109 fRectUniform = args.fBuilder->addUniform(GrGLSLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000110 kVec4f_GrSLType,
bsalomon422f56f2014-12-09 10:18:12 -0800111 kDefault_GrSLPrecision,
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000112 "rect",
113 &rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700114
egdaniel2d721d32015-11-11 13:06:05 -0800115 GrGLSLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700116 const char* fragmentPos = fsBuilder->fragmentPosition();
joshualittb0a8a372014-09-23 09:50:21 -0700117 if (GrProcessorEdgeTypeIsAA(aare.getEdgeType())) {
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000118 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
119 // numbers, xSub and ySub.
joshualitt30ba4362014-08-21 20:18:45 -0700120 fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n");
121 fsBuilder->codeAppendf("\t\txSub = min(%s.x - %s.x, 0.0);\n", fragmentPos, rectName);
122 fsBuilder->codeAppendf("\t\txSub += min(%s.z - %s.x, 0.0);\n", rectName, fragmentPos);
123 fsBuilder->codeAppendf("\t\tySub = min(%s.y - %s.y, 0.0);\n", fragmentPos, rectName);
124 fsBuilder->codeAppendf("\t\tySub += min(%s.w - %s.y, 0.0);\n", rectName, fragmentPos);
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000125 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
126 // covered.
joshualitt30ba4362014-08-21 20:18:45 -0700127 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 +0000128 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700129 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
robertphillips7f14c9b2015-01-30 14:44:22 -0800130 fsBuilder->codeAppendf("\t\talpha *= (%s.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", fragmentPos, rectName);
131 fsBuilder->codeAppendf("\t\talpha *= (%s.z - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName, fragmentPos);
132 fsBuilder->codeAppendf("\t\talpha *= (%s.y - %s.y) > -0.5 ? 1.0 : 0.0;\n", fragmentPos, rectName);
133 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 +0000134 }
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000135
joshualittb0a8a372014-09-23 09:50:21 -0700136 if (GrProcessorEdgeTypeIsInverseFill(aare.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700137 fsBuilder->codeAppend("\t\talpha = 1.0 - alpha;\n");
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000138 }
wangyix7c157a92015-07-22 15:08:53 -0700139 fsBuilder->codeAppendf("\t\t%s = %s;\n", args.fOutputColor,
140 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha")).c_str());
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000141}
142
egdaniel018fb622015-10-28 07:26:40 -0700143void GLAARectEffect::onSetData(const GrGLSLProgramDataManager& pdman,
144 const GrProcessor& processor) {
joshualittb0a8a372014-09-23 09:50:21 -0700145 const AARectEffect& aare = processor.cast<AARectEffect>();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000146 const SkRect& rect = aare.getRect();
147 if (rect != fPrevRect) {
robertphillips7f14c9b2015-01-30 14:44:22 -0800148 pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
149 rect.fRight - 0.5f, rect.fBottom - 0.5f);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000150 fPrevRect = rect;
151 }
152}
153
jvanverthcfc18862015-04-28 08:48:20 -0700154void GLAARectEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700155 GrProcessorKeyBuilder* b) {
156 const AARectEffect& aare = processor.cast<AARectEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700157 b->add32(aare.getEdgeType());
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000158}
159
wangyix4b3050b2015-08-04 07:59:37 -0700160void AARectEffect::onGetGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800161 GLAARectEffect::GenKey(*this, caps, b);
162}
163
wangyixb1daa862015-08-18 11:29:31 -0700164GrGLFragmentProcessor* AARectEffect::onCreateGLInstance() const {
halcanary385fe4d2015-08-26 13:07:48 -0700165 return new GLAARectEffect(*this);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000166}
167
168//////////////////////////////////////////////////////////////////////////////
169
joshualittb0a8a372014-09-23 09:50:21 -0700170class GrGLConvexPolyEffect : public GrGLFragmentProcessor {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000171public:
joshualitteb2a6762014-12-04 11:35:33 -0800172 GrGLConvexPolyEffect(const GrProcessor&);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000173
wangyix7c157a92015-07-22 15:08:53 -0700174 virtual void emitCode(EmitArgs&) override;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000175
jvanverthcfc18862015-04-28 08:48:20 -0700176 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000177
wangyixb1daa862015-08-18 11:29:31 -0700178protected:
egdaniel018fb622015-10-28 07:26:40 -0700179 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000180
181private:
egdaniel018fb622015-10-28 07:26:40 -0700182 GrGLSLProgramDataManager::UniformHandle fEdgeUniform;
kkinnunen7510b222014-07-30 00:04:16 -0700183 SkScalar fPrevEdges[3 * GrConvexPolyEffect::kMaxEdges];
joshualittb0a8a372014-09-23 09:50:21 -0700184 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000185};
186
joshualitteb2a6762014-12-04 11:35:33 -0800187GrGLConvexPolyEffect::GrGLConvexPolyEffect(const GrProcessor&) {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000188 fPrevEdges[0] = SK_ScalarNaN;
189}
190
wangyix7c157a92015-07-22 15:08:53 -0700191void GrGLConvexPolyEffect::emitCode(EmitArgs& args) {
192 const GrConvexPolyEffect& cpe = args.fFp.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000193
194 const char *edgeArrayName;
egdaniel2d721d32015-11-11 13:06:05 -0800195 fEdgeUniform = args.fBuilder->addUniformArray(GrGLSLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000196 kVec3f_GrSLType,
bsalomon422f56f2014-12-09 10:18:12 -0800197 kDefault_GrSLPrecision,
198 "edges",
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000199 cpe.getEdgeCount(),
200 &edgeArrayName);
egdaniel2d721d32015-11-11 13:06:05 -0800201 GrGLSLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700202 fsBuilder->codeAppend("\t\tfloat alpha = 1.0;\n");
203 fsBuilder->codeAppend("\t\tfloat edge;\n");
204 const char* fragmentPos = fsBuilder->fragmentPosition();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000205 for (int i = 0; i < cpe.getEdgeCount(); ++i) {
robertphillips7f14c9b2015-01-30 14:44:22 -0800206 fsBuilder->codeAppendf("\t\tedge = dot(%s[%d], vec3(%s.x, %s.y, 1));\n",
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000207 edgeArrayName, i, fragmentPos, fragmentPos);
joshualittb0a8a372014-09-23 09:50:21 -0700208 if (GrProcessorEdgeTypeIsAA(cpe.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700209 fsBuilder->codeAppend("\t\tedge = clamp(edge, 0.0, 1.0);\n");
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000210 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700211 fsBuilder->codeAppend("\t\tedge = edge >= 0.5 ? 1.0 : 0.0;\n");
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000212 }
joshualitt30ba4362014-08-21 20:18:45 -0700213 fsBuilder->codeAppend("\t\talpha *= edge;\n");
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000214 }
215
joshualittb0a8a372014-09-23 09:50:21 -0700216 if (GrProcessorEdgeTypeIsInverseFill(cpe.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700217 fsBuilder->codeAppend("\talpha = 1.0 - alpha;\n");
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000218 }
wangyix7c157a92015-07-22 15:08:53 -0700219 fsBuilder->codeAppendf("\t%s = %s;\n", args.fOutputColor,
220 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha")).c_str());
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000221}
222
egdaniel018fb622015-10-28 07:26:40 -0700223void GrGLConvexPolyEffect::onSetData(const GrGLSLProgramDataManager& pdman,
224 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) {
halcanary96fcdcc2015-08-27 07:41:13 -0700246 return nullptr;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000247 }
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()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700250 return nullptr;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000251 }
252
253 if (path.countPoints() > kMaxEdges) {
halcanary96fcdcc2015-08-27 07:41:13 -0700254 return nullptr;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000255 }
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;
halcanary96fcdcc2015-08-27 07:41:13 -0700264 if (nullptr == offset) {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000265 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){
halcanary96fcdcc2015-08-27 07:41:13 -0700296 return nullptr;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000297 }
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,
egdaniel2d721d32015-11-11 13:06:05 -0800308 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800309 GrGLConvexPolyEffect::GenKey(*this, caps, b);
310}
311
wangyixb1daa862015-08-18 11:29:31 -0700312GrGLFragmentProcessor* GrConvexPolyEffect::onCreateGLInstance() const {
halcanary385fe4d2015-08-26 13:07:48 -0700313 return new 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
bsalomonc21b09e2015-08-28 18:46:56 -0700342const GrFragmentProcessor* GrConvexPolyEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700343 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);
halcanary96fcdcc2015-08-27 07:41:13 -0700354 } while (nullptr == fp);
joshualittb0a8a372014-09-23 09:50:21 -0700355 return fp;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000356}