blob: cf874cfa52604e1666ddecad3fec720d1063279a [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;
robertphillipsbf536af2016-02-04 06:11:53 -080097 SkRect fPrevRect;
98
egdaniel64c47282015-11-13 06:54:19 -080099 typedef GrGLSLFragmentProcessor INHERITED;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000100};
101
wangyix7c157a92015-07-22 15:08:53 -0700102void GLAARectEffect::emitCode(EmitArgs& args) {
103 const AARectEffect& aare = args.fFp.cast<AARectEffect>();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000104 const char *rectName;
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000105 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
106 // respectively.
cdalton5e58cee2016-02-11 12:49:47 -0800107 fRectUniform = args.fUniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800108 kVec4f_GrSLType,
109 kDefault_GrSLPrecision,
110 "rect",
111 &rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700112
cdalton85285412016-02-18 12:37:07 -0800113 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
egdaniel4ca2e602015-11-18 08:01:26 -0800114 const char* fragmentPos = fragBuilder->fragmentPosition();
joshualittb0a8a372014-09-23 09:50:21 -0700115 if (GrProcessorEdgeTypeIsAA(aare.getEdgeType())) {
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000116 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
117 // numbers, xSub and ySub.
egdaniel4ca2e602015-11-18 08:01:26 -0800118 fragBuilder->codeAppend("\t\tfloat xSub, ySub;\n");
119 fragBuilder->codeAppendf("\t\txSub = min(%s.x - %s.x, 0.0);\n", fragmentPos, rectName);
120 fragBuilder->codeAppendf("\t\txSub += min(%s.z - %s.x, 0.0);\n", rectName, fragmentPos);
121 fragBuilder->codeAppendf("\t\tySub = min(%s.y - %s.y, 0.0);\n", fragmentPos, rectName);
122 fragBuilder->codeAppendf("\t\tySub += min(%s.w - %s.y, 0.0);\n", rectName, fragmentPos);
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000123 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
124 // covered.
egdaniel4ca2e602015-11-18 08:01:26 -0800125 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 +0000126 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800127 fragBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
128 fragBuilder->codeAppendf("\t\talpha *= (%s.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", fragmentPos, rectName);
129 fragBuilder->codeAppendf("\t\talpha *= (%s.z - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName, fragmentPos);
130 fragBuilder->codeAppendf("\t\talpha *= (%s.y - %s.y) > -0.5 ? 1.0 : 0.0;\n", fragmentPos, rectName);
131 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 +0000132 }
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000133
joshualittb0a8a372014-09-23 09:50:21 -0700134 if (GrProcessorEdgeTypeIsInverseFill(aare.getEdgeType())) {
egdaniel4ca2e602015-11-18 08:01:26 -0800135 fragBuilder->codeAppend("\t\talpha = 1.0 - alpha;\n");
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000136 }
egdaniel4ca2e602015-11-18 08:01:26 -0800137 fragBuilder->codeAppendf("\t\t%s = %s;\n", args.fOutputColor,
138 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha")).c_str());
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000139}
140
egdaniel018fb622015-10-28 07:26:40 -0700141void GLAARectEffect::onSetData(const GrGLSLProgramDataManager& pdman,
142 const GrProcessor& processor) {
joshualittb0a8a372014-09-23 09:50:21 -0700143 const AARectEffect& aare = processor.cast<AARectEffect>();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000144 const SkRect& rect = aare.getRect();
145 if (rect != fPrevRect) {
robertphillips7f14c9b2015-01-30 14:44:22 -0800146 pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
147 rect.fRight - 0.5f, rect.fBottom - 0.5f);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000148 fPrevRect = rect;
149 }
150}
151
jvanverthcfc18862015-04-28 08:48:20 -0700152void GLAARectEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700153 GrProcessorKeyBuilder* b) {
154 const AARectEffect& aare = processor.cast<AARectEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700155 b->add32(aare.getEdgeType());
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000156}
157
egdaniel57d3b032015-11-13 11:57:27 -0800158void AARectEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800159 GLAARectEffect::GenKey(*this, caps, b);
160}
161
egdaniel57d3b032015-11-13 11:57:27 -0800162GrGLSLFragmentProcessor* AARectEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -0800163 return new GLAARectEffect;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000164}
165
166//////////////////////////////////////////////////////////////////////////////
167
egdaniel64c47282015-11-13 06:54:19 -0800168class GrGLConvexPolyEffect : public GrGLSLFragmentProcessor {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000169public:
robertphillips9cdb9922016-02-03 12:25:40 -0800170 GrGLConvexPolyEffect() {
171 fPrevEdges[0] = SK_ScalarNaN;
172 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000173
robertphillips9cdb9922016-02-03 12:25:40 -0800174 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;
robertphillipsbf536af2016-02-04 06:11:53 -0800183 SkScalar fPrevEdges[3 * GrConvexPolyEffect::kMaxEdges];
egdaniel64c47282015-11-13 06:54:19 -0800184 typedef GrGLSLFragmentProcessor INHERITED;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000185};
186
wangyix7c157a92015-07-22 15:08:53 -0700187void GrGLConvexPolyEffect::emitCode(EmitArgs& args) {
188 const GrConvexPolyEffect& cpe = args.fFp.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000189
190 const char *edgeArrayName;
cdalton5e58cee2016-02-11 12:49:47 -0800191 fEdgeUniform = args.fUniformHandler->addUniformArray(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800192 kVec3f_GrSLType,
193 kDefault_GrSLPrecision,
194 "edges",
195 cpe.getEdgeCount(),
196 &edgeArrayName);
cdalton85285412016-02-18 12:37:07 -0800197 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
egdaniel4ca2e602015-11-18 08:01:26 -0800198 fragBuilder->codeAppend("\t\tfloat alpha = 1.0;\n");
199 fragBuilder->codeAppend("\t\tfloat edge;\n");
200 const char* fragmentPos = fragBuilder->fragmentPosition();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000201 for (int i = 0; i < cpe.getEdgeCount(); ++i) {
egdaniel4ca2e602015-11-18 08:01:26 -0800202 fragBuilder->codeAppendf("\t\tedge = dot(%s[%d], vec3(%s.x, %s.y, 1));\n",
203 edgeArrayName, i, fragmentPos, fragmentPos);
joshualittb0a8a372014-09-23 09:50:21 -0700204 if (GrProcessorEdgeTypeIsAA(cpe.getEdgeType())) {
egdaniel4ca2e602015-11-18 08:01:26 -0800205 fragBuilder->codeAppend("\t\tedge = clamp(edge, 0.0, 1.0);\n");
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000206 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800207 fragBuilder->codeAppend("\t\tedge = edge >= 0.5 ? 1.0 : 0.0;\n");
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000208 }
egdaniel4ca2e602015-11-18 08:01:26 -0800209 fragBuilder->codeAppend("\t\talpha *= edge;\n");
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000210 }
211
joshualittb0a8a372014-09-23 09:50:21 -0700212 if (GrProcessorEdgeTypeIsInverseFill(cpe.getEdgeType())) {
egdaniel4ca2e602015-11-18 08:01:26 -0800213 fragBuilder->codeAppend("\talpha = 1.0 - alpha;\n");
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000214 }
egdaniel4ca2e602015-11-18 08:01:26 -0800215 fragBuilder->codeAppendf("\t%s = %s;\n", args.fOutputColor,
216 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha")).c_str());
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000217}
218
egdaniel018fb622015-10-28 07:26:40 -0700219void GrGLConvexPolyEffect::onSetData(const GrGLSLProgramDataManager& pdman,
220 const GrProcessor& effect) {
joshualitt49586be2014-09-16 08:21:41 -0700221 const GrConvexPolyEffect& cpe = effect.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000222 size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar);
223 if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) {
kkinnunen7510b222014-07-30 00:04:16 -0700224 pdman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges());
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000225 memcpy(fPrevEdges, cpe.getEdges(), byteSize);
226 }
227}
228
jvanverthcfc18862015-04-28 08:48:20 -0700229void GrGLConvexPolyEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700230 GrProcessorKeyBuilder* b) {
231 const GrConvexPolyEffect& cpe = processor.cast<GrConvexPolyEffect>();
232 GR_STATIC_ASSERT(kGrProcessorEdgeTypeCnt <= 8);
bsalomon63e99f72014-07-21 08:03:14 -0700233 uint32_t key = (cpe.getEdgeCount() << 3) | cpe.getEdgeType();
234 b->add32(key);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000235}
236
237//////////////////////////////////////////////////////////////////////////////
238
joshualittb0a8a372014-09-23 09:50:21 -0700239GrFragmentProcessor* GrConvexPolyEffect::Create(GrPrimitiveEdgeType type, const SkPath& path,
240 const SkVector* offset) {
241 if (kHairlineAA_GrProcessorEdgeType == type) {
halcanary96fcdcc2015-08-27 07:41:13 -0700242 return nullptr;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000243 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000244 if (path.getSegmentMasks() != SkPath::kLine_SegmentMask ||
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000245 !path.isConvex()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700246 return nullptr;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000247 }
248
249 if (path.countPoints() > kMaxEdges) {
halcanary96fcdcc2015-08-27 07:41:13 -0700250 return nullptr;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000251 }
252
253 SkPoint pts[kMaxEdges];
254 SkScalar edges[3 * kMaxEdges];
255
reed026beb52015-06-10 14:23:15 -0700256 SkPathPriv::FirstDirection dir;
257 SkAssertResult(SkPathPriv::CheapComputeFirstDirection(path, &dir));
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000258
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000259 SkVector t;
halcanary96fcdcc2015-08-27 07:41:13 -0700260 if (nullptr == offset) {
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000261 t.set(0, 0);
262 } else {
263 t = *offset;
264 }
265
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000266 int count = path.getPoints(pts, kMaxEdges);
267 int n = 0;
268 for (int lastPt = count - 1, i = 0; i < count; lastPt = i++) {
269 if (pts[lastPt] != pts[i]) {
270 SkVector v = pts[i] - pts[lastPt];
271 v.normalize();
reed026beb52015-06-10 14:23:15 -0700272 if (SkPathPriv::kCCW_FirstDirection == dir) {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000273 edges[3 * n] = v.fY;
274 edges[3 * n + 1] = -v.fX;
275 } else {
276 edges[3 * n] = -v.fY;
277 edges[3 * n + 1] = v.fX;
278 }
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000279 SkPoint p = pts[i] + t;
280 edges[3 * n + 2] = -(edges[3 * n] * p.fX + edges[3 * n + 1] * p.fY);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000281 ++n;
282 }
283 }
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000284 if (path.isInverseFillType()) {
joshualittb0a8a372014-09-23 09:50:21 -0700285 type = GrInvertProcessorEdgeType(type);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000286 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000287 return Create(type, n, edges);
288}
289
joshualittb0a8a372014-09-23 09:50:21 -0700290GrFragmentProcessor* GrConvexPolyEffect::Create(GrPrimitiveEdgeType edgeType, const SkRect& rect) {
291 if (kHairlineAA_GrProcessorEdgeType == edgeType){
halcanary96fcdcc2015-08-27 07:41:13 -0700292 return nullptr;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000293 }
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000294 return AARectEffect::Create(edgeType, rect);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000295}
296
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000297GrConvexPolyEffect::~GrConvexPolyEffect() {}
298
egdaniel605dd0f2014-11-12 08:35:25 -0800299void GrConvexPolyEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
joshualitt56995b52014-12-11 15:44:02 -0800300 inout->mulByUnknownSingleComponent();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000301}
302
egdaniel57d3b032015-11-13 11:57:27 -0800303void GrConvexPolyEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
304 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800305 GrGLConvexPolyEffect::GenKey(*this, caps, b);
306}
307
egdaniel57d3b032015-11-13 11:57:27 -0800308GrGLSLFragmentProcessor* GrConvexPolyEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -0800309 return new GrGLConvexPolyEffect;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000310}
311
joshualittb0a8a372014-09-23 09:50:21 -0700312GrConvexPolyEffect::GrConvexPolyEffect(GrPrimitiveEdgeType edgeType, int n, const SkScalar edges[])
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000313 : fEdgeType(edgeType)
314 , fEdgeCount(n) {
joshualitteb2a6762014-12-04 11:35:33 -0800315 this->initClassID<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000316 // Factory function should have already ensured this.
317 SkASSERT(n <= kMaxEdges);
318 memcpy(fEdges, edges, 3 * n * sizeof(SkScalar));
319 // Outset the edges by 0.5 so that a pixel with center on an edge is 50% covered in the AA case
320 // and 100% covered in the non-AA case.
321 for (int i = 0; i < n; ++i) {
322 fEdges[3 * i + 2] += SK_ScalarHalf;
323 }
324 this->setWillReadFragmentPosition();
325}
326
bsalomon0e08fc12014-10-15 08:19:04 -0700327bool GrConvexPolyEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700328 const GrConvexPolyEffect& cpe = other.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000329 // ignore the fact that 0 == -0 and just use memcmp.
330 return (cpe.fEdgeType == fEdgeType && cpe.fEdgeCount == fEdgeCount &&
331 0 == memcmp(cpe.fEdges, fEdges, 3 * fEdgeCount * sizeof(SkScalar)));
332}
333
334//////////////////////////////////////////////////////////////////////////////
335
joshualittb0a8a372014-09-23 09:50:21 -0700336GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvexPolyEffect);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000337
bsalomonc21b09e2015-08-28 18:46:56 -0700338const GrFragmentProcessor* GrConvexPolyEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700339 int count = d->fRandom->nextULessThan(kMaxEdges) + 1;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000340 SkScalar edges[kMaxEdges * 3];
341 for (int i = 0; i < 3 * count; ++i) {
joshualitt0067ff52015-07-08 14:26:19 -0700342 edges[i] = d->fRandom->nextSScalar1();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000343 }
344
joshualittb0a8a372014-09-23 09:50:21 -0700345 GrFragmentProcessor* fp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000346 do {
joshualittb0a8a372014-09-23 09:50:21 -0700347 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(
joshualitt0067ff52015-07-08 14:26:19 -0700348 d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt));
joshualittb0a8a372014-09-23 09:50:21 -0700349 fp = GrConvexPolyEffect::Create(edgeType, count, edges);
halcanary96fcdcc2015-08-27 07:41:13 -0700350 } while (nullptr == fp);
joshualittb0a8a372014-09-23 09:50:21 -0700351 return fp;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000352}