blob: bafa64d13ae08ed88672c39c2c4c26829aafa011 [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
joshualitt30ba4362014-08-21 20:18:45 -07008#include "gl/builders/GrGLProgramBuilder.h"
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +00009#include "GrConvexPolyEffect.h"
10
joshualittb0a8a372014-09-23 09:50:21 -070011#include "gl/GrGLProcessor.h"
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000012#include "gl/GrGLSL.h"
joshualittb0a8a372014-09-23 09:50:21 -070013#include "GrTBackendProcessorFactory.h"
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000014
15#include "SkPath.h"
16
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000017//////////////////////////////////////////////////////////////////////////////
18class GLAARectEffect;
19
joshualittb0a8a372014-09-23 09:50:21 -070020class AARectEffect : public GrFragmentProcessor {
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000021public:
joshualittb0a8a372014-09-23 09:50:21 -070022 typedef GLAARectEffect GLProcessor;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000023
24 const SkRect& getRect() const { return fRect; }
25
26 static const char* Name() { return "AARect"; }
27
joshualittb0a8a372014-09-23 09:50:21 -070028 static GrFragmentProcessor* Create(GrPrimitiveEdgeType edgeType, const SkRect& rect) {
bsalomon55fad7a2014-07-08 07:34:20 -070029 return SkNEW_ARGS(AARectEffect, (edgeType, rect));
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000030 }
31
joshualittb0a8a372014-09-23 09:50:21 -070032 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +000033
joshualittb0a8a372014-09-23 09:50:21 -070034 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERRIDE;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000035
36private:
joshualittb0a8a372014-09-23 09:50:21 -070037 AARectEffect(GrPrimitiveEdgeType edgeType, const SkRect& rect) : fRect(rect), fEdgeType(edgeType) {
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000038 this->setWillReadFragmentPosition();
39 }
40
bsalomon0e08fc12014-10-15 08:19:04 -070041 virtual bool onIsEqual(const GrFragmentProcessor& other) const SK_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
egdaniel1a8ecdf2014-10-03 06:24:12 -070046 virtual void onComputeInvariantOutput(InvariantOutput* inout) const SK_OVERRIDE {
47 if (fRect.isEmpty()) {
48 // An empty rect will have no coverage anywhere.
egdanielccb2e382014-10-13 12:53:46 -070049 inout->setToTransparentBlack();
egdaniel1a8ecdf2014-10-03 06:24:12 -070050 } else {
egdanielccb2e382014-10-13 12:53:46 -070051 inout->mulByUnknownAlpha();
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
joshualittb0a8a372014-09-23 09:50:21 -070066GrFragmentProcessor* AARectEffect::TestCreate(SkRandom* random,
67 GrContext*,
68 const GrDrawTargetCaps& caps,
69 GrTexture*[]) {
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000070 SkRect rect = SkRect::MakeLTRB(random->nextSScalar1(),
71 random->nextSScalar1(),
72 random->nextSScalar1(),
73 random->nextSScalar1());
joshualittb0a8a372014-09-23 09:50:21 -070074 GrFragmentProcessor* fp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000075 do {
joshualittb0a8a372014-09-23 09:50:21 -070076 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan(
77 kGrProcessorEdgeTypeCnt));
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000078
joshualittb0a8a372014-09-23 09:50:21 -070079 fp = AARectEffect::Create(edgeType, rect);
80 } while (NULL == fp);
81 return fp;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000082}
83
84//////////////////////////////////////////////////////////////////////////////
85
joshualittb0a8a372014-09-23 09:50:21 -070086class GLAARectEffect : public GrGLFragmentProcessor {
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000087public:
joshualittb0a8a372014-09-23 09:50:21 -070088 GLAARectEffect(const GrBackendProcessorFactory&, const GrProcessor&);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000089
joshualitt15988992014-10-09 15:04:05 -070090 virtual void emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -070091 const GrFragmentProcessor& fp,
92 const GrProcessorKey& key,
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000093 const char* outputColor,
94 const char* inputColor,
95 const TransformedCoordsArray&,
96 const TextureSamplerArray&) SK_OVERRIDE;
97
joshualittb0a8a372014-09-23 09:50:21 -070098 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +000099
joshualittb0a8a372014-09-23 09:50:21 -0700100 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000101
102private:
kkinnunen7510b222014-07-30 00:04:16 -0700103 GrGLProgramDataManager::UniformHandle fRectUniform;
104 SkRect fPrevRect;
joshualittb0a8a372014-09-23 09:50:21 -0700105 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000106};
107
joshualittb0a8a372014-09-23 09:50:21 -0700108GLAARectEffect::GLAARectEffect(const GrBackendProcessorFactory& factory,
109 const GrProcessor& effect)
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000110 : INHERITED (factory) {
111 fPrevRect.fLeft = SK_ScalarNaN;
112}
113
joshualitt15988992014-10-09 15:04:05 -0700114void GLAARectEffect::emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700115 const GrFragmentProcessor& fp,
116 const GrProcessorKey& key,
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000117 const char* outputColor,
118 const char* inputColor,
119 const TransformedCoordsArray&,
120 const TextureSamplerArray& samplers) {
joshualittb0a8a372014-09-23 09:50:21 -0700121 const AARectEffect& aare = fp.cast<AARectEffect>();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000122 const char *rectName;
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000123 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
124 // respectively.
joshualitt30ba4362014-08-21 20:18:45 -0700125 fRectUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000126 kVec4f_GrSLType,
127 "rect",
128 &rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700129
joshualitt15988992014-10-09 15:04:05 -0700130 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700131 const char* fragmentPos = fsBuilder->fragmentPosition();
joshualittb0a8a372014-09-23 09:50:21 -0700132 if (GrProcessorEdgeTypeIsAA(aare.getEdgeType())) {
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000133 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
134 // numbers, xSub and ySub.
joshualitt30ba4362014-08-21 20:18:45 -0700135 fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n");
136 fsBuilder->codeAppendf("\t\txSub = min(%s.x - %s.x, 0.0);\n", fragmentPos, rectName);
137 fsBuilder->codeAppendf("\t\txSub += min(%s.z - %s.x, 0.0);\n", rectName, fragmentPos);
138 fsBuilder->codeAppendf("\t\tySub = min(%s.y - %s.y, 0.0);\n", fragmentPos, rectName);
139 fsBuilder->codeAppendf("\t\tySub += min(%s.w - %s.y, 0.0);\n", rectName, fragmentPos);
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000140 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
141 // covered.
joshualitt30ba4362014-08-21 20:18:45 -0700142 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 +0000143 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700144 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
145 fsBuilder->codeAppendf("\t\talpha *= (%s.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", fragmentPos, rectName);
146 fsBuilder->codeAppendf("\t\talpha *= (%s.z - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName, fragmentPos);
147 fsBuilder->codeAppendf("\t\talpha *= (%s.y - %s.y) > -0.5 ? 1.0 : 0.0;\n", fragmentPos, rectName);
148 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 +0000149 }
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000150
joshualittb0a8a372014-09-23 09:50:21 -0700151 if (GrProcessorEdgeTypeIsInverseFill(aare.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700152 fsBuilder->codeAppend("\t\talpha = 1.0 - alpha;\n");
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000153 }
joshualitt30ba4362014-08-21 20:18:45 -0700154 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor,
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000155 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
156}
157
joshualittb0a8a372014-09-23 09:50:21 -0700158void GLAARectEffect::setData(const GrGLProgramDataManager& pdman, const GrProcessor& processor) {
159 const AARectEffect& aare = processor.cast<AARectEffect>();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000160 const SkRect& rect = aare.getRect();
161 if (rect != fPrevRect) {
kkinnunen7510b222014-07-30 00:04:16 -0700162 pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000163 rect.fRight - 0.5f, rect.fBottom - 0.5f);
164 fPrevRect = rect;
165 }
166}
167
joshualittb0a8a372014-09-23 09:50:21 -0700168void GLAARectEffect::GenKey(const GrProcessor& processor, const GrGLCaps&,
169 GrProcessorKeyBuilder* b) {
170 const AARectEffect& aare = processor.cast<AARectEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700171 b->add32(aare.getEdgeType());
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000172}
173
joshualittb0a8a372014-09-23 09:50:21 -0700174const GrBackendFragmentProcessorFactory& AARectEffect::getFactory() const {
175 return GrTBackendFragmentProcessorFactory<AARectEffect>::getInstance();
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000176}
177
178//////////////////////////////////////////////////////////////////////////////
179
joshualittb0a8a372014-09-23 09:50:21 -0700180class GrGLConvexPolyEffect : public GrGLFragmentProcessor {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000181public:
joshualittb0a8a372014-09-23 09:50:21 -0700182 GrGLConvexPolyEffect(const GrBackendProcessorFactory&, const GrProcessor&);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000183
joshualitt15988992014-10-09 15:04:05 -0700184 virtual void emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700185 const GrFragmentProcessor& fp,
186 const GrProcessorKey& key,
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000187 const char* outputColor,
188 const char* inputColor,
189 const TransformedCoordsArray&,
190 const TextureSamplerArray&) SK_OVERRIDE;
191
joshualittb0a8a372014-09-23 09:50:21 -0700192 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000193
joshualittb0a8a372014-09-23 09:50:21 -0700194 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000195
196private:
kkinnunen7510b222014-07-30 00:04:16 -0700197 GrGLProgramDataManager::UniformHandle fEdgeUniform;
198 SkScalar fPrevEdges[3 * GrConvexPolyEffect::kMaxEdges];
joshualittb0a8a372014-09-23 09:50:21 -0700199 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000200};
201
joshualittb0a8a372014-09-23 09:50:21 -0700202GrGLConvexPolyEffect::GrGLConvexPolyEffect(const GrBackendProcessorFactory& factory,
203 const GrProcessor&)
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000204 : INHERITED (factory) {
205 fPrevEdges[0] = SK_ScalarNaN;
206}
207
joshualitt15988992014-10-09 15:04:05 -0700208void GrGLConvexPolyEffect::emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700209 const GrFragmentProcessor& fp,
210 const GrProcessorKey& key,
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000211 const char* outputColor,
212 const char* inputColor,
213 const TransformedCoordsArray&,
214 const TextureSamplerArray& samplers) {
joshualittb0a8a372014-09-23 09:50:21 -0700215 const GrConvexPolyEffect& cpe = fp.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000216
217 const char *edgeArrayName;
joshualitt30ba4362014-08-21 20:18:45 -0700218 fEdgeUniform = builder->addUniformArray(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000219 kVec3f_GrSLType,
220 "edges",
221 cpe.getEdgeCount(),
222 &edgeArrayName);
joshualitt15988992014-10-09 15:04:05 -0700223 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700224 fsBuilder->codeAppend("\t\tfloat alpha = 1.0;\n");
225 fsBuilder->codeAppend("\t\tfloat edge;\n");
226 const char* fragmentPos = fsBuilder->fragmentPosition();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000227 for (int i = 0; i < cpe.getEdgeCount(); ++i) {
joshualitt30ba4362014-08-21 20:18:45 -0700228 fsBuilder->codeAppendf("\t\tedge = dot(%s[%d], vec3(%s.x, %s.y, 1));\n",
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000229 edgeArrayName, i, fragmentPos, fragmentPos);
joshualittb0a8a372014-09-23 09:50:21 -0700230 if (GrProcessorEdgeTypeIsAA(cpe.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700231 fsBuilder->codeAppend("\t\tedge = clamp(edge, 0.0, 1.0);\n");
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000232 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700233 fsBuilder->codeAppend("\t\tedge = edge >= 0.5 ? 1.0 : 0.0;\n");
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000234 }
joshualitt30ba4362014-08-21 20:18:45 -0700235 fsBuilder->codeAppend("\t\talpha *= edge;\n");
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000236 }
237
commit-bot@chromium.org6dee8752014-02-07 22:39:01 +0000238 // Woe is me. See skbug.com/2149.
239 if (kTegra2_GrGLRenderer == builder->ctxInfo().renderer()) {
joshualitt30ba4362014-08-21 20:18:45 -0700240 fsBuilder->codeAppend("\t\tif (-1.0 == alpha) {\n\t\t\tdiscard;\n\t\t}\n");
commit-bot@chromium.org6dee8752014-02-07 22:39:01 +0000241 }
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000242
joshualittb0a8a372014-09-23 09:50:21 -0700243 if (GrProcessorEdgeTypeIsInverseFill(cpe.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700244 fsBuilder->codeAppend("\talpha = 1.0 - alpha;\n");
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000245 }
joshualitt30ba4362014-08-21 20:18:45 -0700246 fsBuilder->codeAppendf("\t%s = %s;\n", outputColor,
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000247 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
248}
249
joshualittb0a8a372014-09-23 09:50:21 -0700250void GrGLConvexPolyEffect::setData(const GrGLProgramDataManager& pdman, const GrProcessor& effect) {
joshualitt49586be2014-09-16 08:21:41 -0700251 const GrConvexPolyEffect& cpe = effect.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000252 size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar);
253 if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) {
kkinnunen7510b222014-07-30 00:04:16 -0700254 pdman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges());
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000255 memcpy(fPrevEdges, cpe.getEdges(), byteSize);
256 }
257}
258
joshualittb0a8a372014-09-23 09:50:21 -0700259void GrGLConvexPolyEffect::GenKey(const GrProcessor& processor, const GrGLCaps&,
260 GrProcessorKeyBuilder* b) {
261 const GrConvexPolyEffect& cpe = processor.cast<GrConvexPolyEffect>();
262 GR_STATIC_ASSERT(kGrProcessorEdgeTypeCnt <= 8);
bsalomon63e99f72014-07-21 08:03:14 -0700263 uint32_t key = (cpe.getEdgeCount() << 3) | cpe.getEdgeType();
264 b->add32(key);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000265}
266
267//////////////////////////////////////////////////////////////////////////////
268
joshualittb0a8a372014-09-23 09:50:21 -0700269GrFragmentProcessor* GrConvexPolyEffect::Create(GrPrimitiveEdgeType type, const SkPath& path,
270 const SkVector* offset) {
271 if (kHairlineAA_GrProcessorEdgeType == type) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000272 return NULL;
273 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000274 if (path.getSegmentMasks() != SkPath::kLine_SegmentMask ||
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000275 !path.isConvex()) {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000276 return NULL;
277 }
278
279 if (path.countPoints() > kMaxEdges) {
280 return NULL;
281 }
282
283 SkPoint pts[kMaxEdges];
284 SkScalar edges[3 * kMaxEdges];
285
286 SkPath::Direction dir;
287 SkAssertResult(path.cheapComputeDirection(&dir));
288
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000289 SkVector t;
290 if (NULL == offset) {
291 t.set(0, 0);
292 } else {
293 t = *offset;
294 }
295
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000296 int count = path.getPoints(pts, kMaxEdges);
297 int n = 0;
298 for (int lastPt = count - 1, i = 0; i < count; lastPt = i++) {
299 if (pts[lastPt] != pts[i]) {
300 SkVector v = pts[i] - pts[lastPt];
301 v.normalize();
302 if (SkPath::kCCW_Direction == dir) {
303 edges[3 * n] = v.fY;
304 edges[3 * n + 1] = -v.fX;
305 } else {
306 edges[3 * n] = -v.fY;
307 edges[3 * n + 1] = v.fX;
308 }
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000309 SkPoint p = pts[i] + t;
310 edges[3 * n + 2] = -(edges[3 * n] * p.fX + edges[3 * n + 1] * p.fY);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000311 ++n;
312 }
313 }
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000314 if (path.isInverseFillType()) {
joshualittb0a8a372014-09-23 09:50:21 -0700315 type = GrInvertProcessorEdgeType(type);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000316 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000317 return Create(type, n, edges);
318}
319
joshualittb0a8a372014-09-23 09:50:21 -0700320GrFragmentProcessor* GrConvexPolyEffect::Create(GrPrimitiveEdgeType edgeType, const SkRect& rect) {
321 if (kHairlineAA_GrProcessorEdgeType == edgeType){
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000322 return NULL;
323 }
commit-bot@chromium.orgd85f32c2014-02-28 14:43:26 +0000324 return AARectEffect::Create(edgeType, rect);
commit-bot@chromium.orgf0539802014-02-08 19:31:05 +0000325}
326
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000327GrConvexPolyEffect::~GrConvexPolyEffect() {}
328
egdaniel1a8ecdf2014-10-03 06:24:12 -0700329void GrConvexPolyEffect::onComputeInvariantOutput(InvariantOutput* inout) const {
egdanielccb2e382014-10-13 12:53:46 -0700330 inout->mulByUnknownAlpha();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000331}
332
joshualittb0a8a372014-09-23 09:50:21 -0700333const GrBackendFragmentProcessorFactory& GrConvexPolyEffect::getFactory() const {
334 return GrTBackendFragmentProcessorFactory<GrConvexPolyEffect>::getInstance();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000335}
336
joshualittb0a8a372014-09-23 09:50:21 -0700337GrConvexPolyEffect::GrConvexPolyEffect(GrPrimitiveEdgeType edgeType, int n, const SkScalar edges[])
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000338 : fEdgeType(edgeType)
339 , fEdgeCount(n) {
340 // 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 }
348 this->setWillReadFragmentPosition();
349}
350
bsalomon0e08fc12014-10-15 08:19:04 -0700351bool GrConvexPolyEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700352 const GrConvexPolyEffect& cpe = other.cast<GrConvexPolyEffect>();
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000353 // ignore the fact that 0 == -0 and just use memcmp.
354 return (cpe.fEdgeType == fEdgeType && cpe.fEdgeCount == fEdgeCount &&
355 0 == memcmp(cpe.fEdges, fEdges, 3 * fEdgeCount * sizeof(SkScalar)));
356}
357
358//////////////////////////////////////////////////////////////////////////////
359
joshualittb0a8a372014-09-23 09:50:21 -0700360GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConvexPolyEffect);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000361
joshualittb0a8a372014-09-23 09:50:21 -0700362GrFragmentProcessor* GrConvexPolyEffect::TestCreate(SkRandom* random,
363 GrContext*,
364 const GrDrawTargetCaps& caps,
365 GrTexture*[]) {
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000366 int count = random->nextULessThan(kMaxEdges) + 1;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000367 SkScalar edges[kMaxEdges * 3];
368 for (int i = 0; i < 3 * count; ++i) {
369 edges[i] = random->nextSScalar1();
370 }
371
joshualittb0a8a372014-09-23 09:50:21 -0700372 GrFragmentProcessor* fp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000373 do {
joshualittb0a8a372014-09-23 09:50:21 -0700374 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(
375 random->nextULessThan(kGrProcessorEdgeTypeCnt));
376 fp = GrConvexPolyEffect::Create(edgeType, count, edges);
377 } while (NULL == fp);
378 return fp;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000379}