blob: 2f641982d74a316ecc12f4e0f92db5f9b491034c [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"
9
10#include "gl/GrGLEffect.h"
11#include "gl/GrGLSL.h"
12#include "gl/GrGLVertexEffect.h"
13#include "GrTBackendEffectFactory.h"
14
15#include "SkPath.h"
16
17class GrGLConvexPolyEffect : public GrGLEffect {
18public:
19 GrGLConvexPolyEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
20
21 virtual void emitCode(GrGLShaderBuilder* builder,
22 const GrDrawEffect& drawEffect,
23 EffectKey key,
24 const char* outputColor,
25 const char* inputColor,
26 const TransformedCoordsArray&,
27 const TextureSamplerArray&) SK_OVERRIDE;
28
29 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&);
30
31 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
32
33private:
34 GrGLUniformManager::UniformHandle fEdgeUniform;
35 SkScalar fPrevEdges[3 * GrConvexPolyEffect::kMaxEdges];
36 typedef GrGLEffect INHERITED;
37};
38
39GrGLConvexPolyEffect::GrGLConvexPolyEffect(const GrBackendEffectFactory& factory,
40 const GrDrawEffect& drawEffect)
41 : INHERITED (factory) {
42 fPrevEdges[0] = SK_ScalarNaN;
43}
44
45void GrGLConvexPolyEffect::emitCode(GrGLShaderBuilder* builder,
46 const GrDrawEffect& drawEffect,
47 EffectKey key,
48 const char* outputColor,
49 const char* inputColor,
50 const TransformedCoordsArray&,
51 const TextureSamplerArray& samplers) {
52 const GrConvexPolyEffect& cpe = drawEffect.castEffect<GrConvexPolyEffect>();
53
54 const char *edgeArrayName;
55 fEdgeUniform = builder->addUniformArray(GrGLShaderBuilder::kFragment_Visibility,
56 kVec3f_GrSLType,
57 "edges",
58 cpe.getEdgeCount(),
59 &edgeArrayName);
60 builder->fsCodeAppend("\t\tfloat alpha = 1.0;\n");
61 builder->fsCodeAppend("\t\tfloat edge;\n");
62 const char* fragmentPos = builder->fragmentPosition();
63 for (int i = 0; i < cpe.getEdgeCount(); ++i) {
64 builder->fsCodeAppendf("\t\tedge = dot(%s[%d], vec3(%s.x, %s.y, 1));\n",
65 edgeArrayName, i, fragmentPos, fragmentPos);
66 switch (cpe.getEdgeType()) {
67 case GrConvexPolyEffect::kFillAA_EdgeType:
68 builder->fsCodeAppend("\t\tedge = clamp(edge, 0.0, 1.0);\n");
69 builder->fsCodeAppend("\t\talpha *= edge;\n");
70 break;
71 case GrConvexPolyEffect::kFillNoAA_EdgeType:
72 builder->fsCodeAppend("\t\tedge = edge >= 0.5 ? 1.0 : 0.0;\n");
73 builder->fsCodeAppend("\t\talpha *= edge;\n");
74 break;
75 }
76 }
77
commit-bot@chromium.org6dee8752014-02-07 22:39:01 +000078 // Woe is me. See skbug.com/2149.
79 if (kTegra2_GrGLRenderer == builder->ctxInfo().renderer()) {
80 builder->fsCodeAppend("\t\tif (-1.0 == alpha) {\n\t\t\tdiscard;\n\t\t}\n");
81 }
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +000082 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
83 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
84}
85
86void GrGLConvexPolyEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) {
87 const GrConvexPolyEffect& cpe = drawEffect.castEffect<GrConvexPolyEffect>();
88 size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar);
89 if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) {
90 uman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges());
91 memcpy(fPrevEdges, cpe.getEdges(), byteSize);
92 }
93}
94
95GrGLEffect::EffectKey GrGLConvexPolyEffect::GenKey(const GrDrawEffect& drawEffect,
96 const GrGLCaps&) {
97 const GrConvexPolyEffect& cpe = drawEffect.castEffect<GrConvexPolyEffect>();
98 GR_STATIC_ASSERT(GrConvexPolyEffect::kEdgeTypeCnt <= 4);
99 return (cpe.getEdgeCount() << 2) | cpe.getEdgeType();
100}
101
102//////////////////////////////////////////////////////////////////////////////
103
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000104GrEffectRef* GrConvexPolyEffect::Create(EdgeType type, const SkPath& path, const SkVector* offset) {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000105 if (path.getSegmentMasks() != SkPath::kLine_SegmentMask ||
106 !path.isConvex() ||
107 path.isInverseFillType()) {
108 return NULL;
109 }
110
111 if (path.countPoints() > kMaxEdges) {
112 return NULL;
113 }
114
115 SkPoint pts[kMaxEdges];
116 SkScalar edges[3 * kMaxEdges];
117
118 SkPath::Direction dir;
119 SkAssertResult(path.cheapComputeDirection(&dir));
120
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000121 SkVector t;
122 if (NULL == offset) {
123 t.set(0, 0);
124 } else {
125 t = *offset;
126 }
127
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000128 int count = path.getPoints(pts, kMaxEdges);
129 int n = 0;
130 for (int lastPt = count - 1, i = 0; i < count; lastPt = i++) {
131 if (pts[lastPt] != pts[i]) {
132 SkVector v = pts[i] - pts[lastPt];
133 v.normalize();
134 if (SkPath::kCCW_Direction == dir) {
135 edges[3 * n] = v.fY;
136 edges[3 * n + 1] = -v.fX;
137 } else {
138 edges[3 * n] = -v.fY;
139 edges[3 * n + 1] = v.fX;
140 }
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000141 SkPoint p = pts[i] + t;
142 edges[3 * n + 2] = -(edges[3 * n] * p.fX + edges[3 * n + 1] * p.fY);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000143 ++n;
144 }
145 }
146 return Create(type, n, edges);
147}
148
149GrConvexPolyEffect::~GrConvexPolyEffect() {}
150
151void GrConvexPolyEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
152 *validFlags = 0;
153}
154
155const GrBackendEffectFactory& GrConvexPolyEffect::getFactory() const {
156 return GrTBackendEffectFactory<GrConvexPolyEffect>::getInstance();
157}
158
159GrConvexPolyEffect::GrConvexPolyEffect(EdgeType edgeType, int n, const SkScalar edges[])
160 : fEdgeType(edgeType)
161 , fEdgeCount(n) {
162 // Factory function should have already ensured this.
163 SkASSERT(n <= kMaxEdges);
164 memcpy(fEdges, edges, 3 * n * sizeof(SkScalar));
165 // Outset the edges by 0.5 so that a pixel with center on an edge is 50% covered in the AA case
166 // and 100% covered in the non-AA case.
167 for (int i = 0; i < n; ++i) {
168 fEdges[3 * i + 2] += SK_ScalarHalf;
169 }
170 this->setWillReadFragmentPosition();
171}
172
173bool GrConvexPolyEffect::onIsEqual(const GrEffect& other) const {
174 const GrConvexPolyEffect& cpe = CastEffect<GrConvexPolyEffect>(other);
175 // ignore the fact that 0 == -0 and just use memcmp.
176 return (cpe.fEdgeType == fEdgeType && cpe.fEdgeCount == fEdgeCount &&
177 0 == memcmp(cpe.fEdges, fEdges, 3 * fEdgeCount * sizeof(SkScalar)));
178}
179
180//////////////////////////////////////////////////////////////////////////////
181
182GR_DEFINE_EFFECT_TEST(GrConvexPolyEffect);
183
184GrEffectRef* GrConvexPolyEffect::TestCreate(SkRandom* random,
185 GrContext*,
186 const GrDrawTargetCaps& caps,
187 GrTexture*[]) {
188 EdgeType edgeType = static_cast<EdgeType>(random->nextULessThan(kEdgeTypeCnt));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000189 int count = random->nextULessThan(kMaxEdges) + 1;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000190 SkScalar edges[kMaxEdges * 3];
191 for (int i = 0; i < 3 * count; ++i) {
192 edges[i] = random->nextSScalar1();
193 }
194
195 return GrConvexPolyEffect::Create(edgeType, count, edges);
196}