blob: 98af6948eadc60da09fa32cd8a70934b8e24bf3f [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
78 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
79 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
80}
81
82void GrGLConvexPolyEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) {
83 const GrConvexPolyEffect& cpe = drawEffect.castEffect<GrConvexPolyEffect>();
84 size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar);
85 if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) {
86 uman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges());
87 memcpy(fPrevEdges, cpe.getEdges(), byteSize);
88 }
89}
90
91GrGLEffect::EffectKey GrGLConvexPolyEffect::GenKey(const GrDrawEffect& drawEffect,
92 const GrGLCaps&) {
93 const GrConvexPolyEffect& cpe = drawEffect.castEffect<GrConvexPolyEffect>();
94 GR_STATIC_ASSERT(GrConvexPolyEffect::kEdgeTypeCnt <= 4);
95 return (cpe.getEdgeCount() << 2) | cpe.getEdgeType();
96}
97
98//////////////////////////////////////////////////////////////////////////////
99
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000100GrEffectRef* GrConvexPolyEffect::Create(EdgeType type, const SkPath& path, const SkVector* offset) {
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000101 if (path.getSegmentMasks() != SkPath::kLine_SegmentMask ||
102 !path.isConvex() ||
103 path.isInverseFillType()) {
104 return NULL;
105 }
106
107 if (path.countPoints() > kMaxEdges) {
108 return NULL;
109 }
110
111 SkPoint pts[kMaxEdges];
112 SkScalar edges[3 * kMaxEdges];
113
114 SkPath::Direction dir;
115 SkAssertResult(path.cheapComputeDirection(&dir));
116
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000117 SkVector t;
118 if (NULL == offset) {
119 t.set(0, 0);
120 } else {
121 t = *offset;
122 }
123
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000124 int count = path.getPoints(pts, kMaxEdges);
125 int n = 0;
126 for (int lastPt = count - 1, i = 0; i < count; lastPt = i++) {
127 if (pts[lastPt] != pts[i]) {
128 SkVector v = pts[i] - pts[lastPt];
129 v.normalize();
130 if (SkPath::kCCW_Direction == dir) {
131 edges[3 * n] = v.fY;
132 edges[3 * n + 1] = -v.fX;
133 } else {
134 edges[3 * n] = -v.fY;
135 edges[3 * n + 1] = v.fX;
136 }
commit-bot@chromium.orgb21fac12014-02-07 21:13:11 +0000137 SkPoint p = pts[i] + t;
138 edges[3 * n + 2] = -(edges[3 * n] * p.fX + edges[3 * n + 1] * p.fY);
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000139 ++n;
140 }
141 }
142 return Create(type, n, edges);
143}
144
145GrConvexPolyEffect::~GrConvexPolyEffect() {}
146
147void GrConvexPolyEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
148 *validFlags = 0;
149}
150
151const GrBackendEffectFactory& GrConvexPolyEffect::getFactory() const {
152 return GrTBackendEffectFactory<GrConvexPolyEffect>::getInstance();
153}
154
155GrConvexPolyEffect::GrConvexPolyEffect(EdgeType edgeType, int n, const SkScalar edges[])
156 : fEdgeType(edgeType)
157 , fEdgeCount(n) {
158 // Factory function should have already ensured this.
159 SkASSERT(n <= kMaxEdges);
160 memcpy(fEdges, edges, 3 * n * sizeof(SkScalar));
161 // Outset the edges by 0.5 so that a pixel with center on an edge is 50% covered in the AA case
162 // and 100% covered in the non-AA case.
163 for (int i = 0; i < n; ++i) {
164 fEdges[3 * i + 2] += SK_ScalarHalf;
165 }
166 this->setWillReadFragmentPosition();
167}
168
169bool GrConvexPolyEffect::onIsEqual(const GrEffect& other) const {
170 const GrConvexPolyEffect& cpe = CastEffect<GrConvexPolyEffect>(other);
171 // ignore the fact that 0 == -0 and just use memcmp.
172 return (cpe.fEdgeType == fEdgeType && cpe.fEdgeCount == fEdgeCount &&
173 0 == memcmp(cpe.fEdges, fEdges, 3 * fEdgeCount * sizeof(SkScalar)));
174}
175
176//////////////////////////////////////////////////////////////////////////////
177
178GR_DEFINE_EFFECT_TEST(GrConvexPolyEffect);
179
180GrEffectRef* GrConvexPolyEffect::TestCreate(SkRandom* random,
181 GrContext*,
182 const GrDrawTargetCaps& caps,
183 GrTexture*[]) {
184 EdgeType edgeType = static_cast<EdgeType>(random->nextULessThan(kEdgeTypeCnt));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000185 int count = random->nextULessThan(kMaxEdges) + 1;
commit-bot@chromium.orgc3fe5492014-01-30 18:15:51 +0000186 SkScalar edges[kMaxEdges * 3];
187 for (int i = 0; i < 3 * count; ++i) {
188 edges[i] = random->nextSScalar1();
189 }
190
191 return GrConvexPolyEffect::Create(edgeType, count, edges);
192}