2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "GrConvexPolyEffect.h"
10 #include "gl/GrGLEffect.h"
11 #include "gl/GrGLSL.h"
12 #include "gl/GrGLVertexEffect.h"
13 #include "GrTBackendEffectFactory.h"
17 //////////////////////////////////////////////////////////////////////////////
20 class AARectEffect : public GrEffect {
22 typedef GLAARectEffect GLEffect;
24 const SkRect& getRect() const { return fRect; }
26 static const char* Name() { return "AARect"; }
28 static GrEffectRef* Create(const SkRect& rect) {
29 return CreateEffectRef(AutoEffectUnref(SkNEW_ARGS(AARectEffect, (rect))));
32 virtual void getConstantColorComponents(GrColor* color,
33 uint32_t* validFlags) const SK_OVERRIDE {
34 if (fRect.isEmpty()) {
35 // An empty rect will have no coverage anywhere.
37 *validFlags = kRGBA_GrColorComponentFlags;
43 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
46 AARectEffect(const SkRect& rect) : fRect(rect) {
47 this->setWillReadFragmentPosition();
50 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
51 const AARectEffect& aare = CastEffect<AARectEffect>(other);
52 return fRect == aare.fRect;
56 typedef GrEffect INHERITED;
58 GR_DECLARE_EFFECT_TEST;
62 GR_DEFINE_EFFECT_TEST(AARectEffect);
64 GrEffectRef* AARectEffect::TestCreate(SkRandom* random,
66 const GrDrawTargetCaps& caps,
68 SkRect rect = SkRect::MakeLTRB(random->nextSScalar1(),
69 random->nextSScalar1(),
70 random->nextSScalar1(),
71 random->nextSScalar1());
72 return AARectEffect::Create(rect);
75 //////////////////////////////////////////////////////////////////////////////
77 class GLAARectEffect : public GrGLEffect {
79 GLAARectEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
81 virtual void emitCode(GrGLShaderBuilder* builder,
82 const GrDrawEffect& drawEffect,
84 const char* outputColor,
85 const char* inputColor,
86 const TransformedCoordsArray&,
87 const TextureSamplerArray&) SK_OVERRIDE;
89 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&) { return 0; }
91 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
94 GrGLUniformManager::UniformHandle fRectUniform;
96 typedef GrGLEffect INHERITED;
99 GLAARectEffect::GLAARectEffect(const GrBackendEffectFactory& factory,
100 const GrDrawEffect& drawEffect)
101 : INHERITED (factory) {
102 fPrevRect.fLeft = SK_ScalarNaN;
105 void GLAARectEffect::emitCode(GrGLShaderBuilder* builder,
106 const GrDrawEffect& drawEffect,
108 const char* outputColor,
109 const char* inputColor,
110 const TransformedCoordsArray&,
111 const TextureSamplerArray& samplers) {
112 const char *rectName;
113 fRectUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
117 const char* fragmentPos = builder->fragmentPosition();
118 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
119 // respectively. The amount of coverage removed in x and y by the edges is computed as a pair of
120 // negative numbers, xSub and ySub.
121 builder->fsCodeAppend("\t\tfloat xSub, ySub;\n");
122 builder->fsCodeAppendf("\t\txSub = min(%s.x - %s.x, 0.0);\n", fragmentPos, rectName);
123 builder->fsCodeAppendf("\t\txSub += min(%s.z - %s.x, 0.0);\n", rectName, fragmentPos);
124 builder->fsCodeAppendf("\t\tySub = min(%s.y - %s.y, 0.0);\n", fragmentPos, rectName);
125 builder->fsCodeAppendf("\t\tySub += min(%s.w - %s.y, 0.0);\n", rectName, fragmentPos);
126 // Now compute coverage in x and y and multiply them to get the fraction of the pixel covered.
127 builder->fsCodeAppendf("\t\tfloat alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0));\n");
129 builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor,
130 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
133 void GLAARectEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) {
134 const AARectEffect& aare = drawEffect.castEffect<AARectEffect>();
135 const SkRect& rect = aare.getRect();
136 if (rect != fPrevRect) {
137 uman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
138 rect.fRight - 0.5f, rect.fBottom - 0.5f);
143 const GrBackendEffectFactory& AARectEffect::getFactory() const {
144 return GrTBackendEffectFactory<AARectEffect>::getInstance();
147 //////////////////////////////////////////////////////////////////////////////
149 class GrGLConvexPolyEffect : public GrGLEffect {
151 GrGLConvexPolyEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
153 virtual void emitCode(GrGLShaderBuilder* builder,
154 const GrDrawEffect& drawEffect,
156 const char* outputColor,
157 const char* inputColor,
158 const TransformedCoordsArray&,
159 const TextureSamplerArray&) SK_OVERRIDE;
161 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&);
163 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
166 GrGLUniformManager::UniformHandle fEdgeUniform;
167 SkScalar fPrevEdges[3 * GrConvexPolyEffect::kMaxEdges];
168 typedef GrGLEffect INHERITED;
171 GrGLConvexPolyEffect::GrGLConvexPolyEffect(const GrBackendEffectFactory& factory,
172 const GrDrawEffect& drawEffect)
173 : INHERITED (factory) {
174 fPrevEdges[0] = SK_ScalarNaN;
177 void GrGLConvexPolyEffect::emitCode(GrGLShaderBuilder* builder,
178 const GrDrawEffect& drawEffect,
180 const char* outputColor,
181 const char* inputColor,
182 const TransformedCoordsArray&,
183 const TextureSamplerArray& samplers) {
184 const GrConvexPolyEffect& cpe = drawEffect.castEffect<GrConvexPolyEffect>();
186 const char *edgeArrayName;
187 fEdgeUniform = builder->addUniformArray(GrGLShaderBuilder::kFragment_Visibility,
192 builder->fsCodeAppend("\t\tfloat alpha = 1.0;\n");
193 builder->fsCodeAppend("\t\tfloat edge;\n");
194 const char* fragmentPos = builder->fragmentPosition();
195 for (int i = 0; i < cpe.getEdgeCount(); ++i) {
196 builder->fsCodeAppendf("\t\tedge = dot(%s[%d], vec3(%s.x, %s.y, 1));\n",
197 edgeArrayName, i, fragmentPos, fragmentPos);
198 switch (cpe.getEdgeType()) {
199 case GrConvexPolyEffect::kFillAA_EdgeType:
200 builder->fsCodeAppend("\t\tedge = clamp(edge, 0.0, 1.0);\n");
201 builder->fsCodeAppend("\t\talpha *= edge;\n");
203 case GrConvexPolyEffect::kFillNoAA_EdgeType:
204 builder->fsCodeAppend("\t\tedge = edge >= 0.5 ? 1.0 : 0.0;\n");
205 builder->fsCodeAppend("\t\talpha *= edge;\n");
210 // Woe is me. See skbug.com/2149.
211 if (kTegra2_GrGLRenderer == builder->ctxInfo().renderer()) {
212 builder->fsCodeAppend("\t\tif (-1.0 == alpha) {\n\t\t\tdiscard;\n\t\t}\n");
214 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
215 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
218 void GrGLConvexPolyEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) {
219 const GrConvexPolyEffect& cpe = drawEffect.castEffect<GrConvexPolyEffect>();
220 size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar);
221 if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) {
222 uman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges());
223 memcpy(fPrevEdges, cpe.getEdges(), byteSize);
227 GrGLEffect::EffectKey GrGLConvexPolyEffect::GenKey(const GrDrawEffect& drawEffect,
229 const GrConvexPolyEffect& cpe = drawEffect.castEffect<GrConvexPolyEffect>();
230 GR_STATIC_ASSERT(GrConvexPolyEffect::kEdgeTypeCnt <= 4);
231 return (cpe.getEdgeCount() << 2) | cpe.getEdgeType();
234 //////////////////////////////////////////////////////////////////////////////
236 GrEffectRef* GrConvexPolyEffect::Create(EdgeType type, const SkPath& path, const SkVector* offset) {
237 if (path.getSegmentMasks() != SkPath::kLine_SegmentMask ||
239 path.isInverseFillType()) {
243 if (path.countPoints() > kMaxEdges) {
247 SkPoint pts[kMaxEdges];
248 SkScalar edges[3 * kMaxEdges];
250 SkPath::Direction dir;
251 SkAssertResult(path.cheapComputeDirection(&dir));
254 if (NULL == offset) {
260 int count = path.getPoints(pts, kMaxEdges);
262 for (int lastPt = count - 1, i = 0; i < count; lastPt = i++) {
263 if (pts[lastPt] != pts[i]) {
264 SkVector v = pts[i] - pts[lastPt];
266 if (SkPath::kCCW_Direction == dir) {
268 edges[3 * n + 1] = -v.fX;
270 edges[3 * n] = -v.fY;
271 edges[3 * n + 1] = v.fX;
273 SkPoint p = pts[i] + t;
274 edges[3 * n + 2] = -(edges[3 * n] * p.fX + edges[3 * n + 1] * p.fY);
278 return Create(type, n, edges);
281 GrEffectRef* GrConvexPolyEffect::CreateForAAFillRect(const SkRect& rect) {
282 return AARectEffect::Create(rect);
285 GrConvexPolyEffect::~GrConvexPolyEffect() {}
287 void GrConvexPolyEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
291 const GrBackendEffectFactory& GrConvexPolyEffect::getFactory() const {
292 return GrTBackendEffectFactory<GrConvexPolyEffect>::getInstance();
295 GrConvexPolyEffect::GrConvexPolyEffect(EdgeType edgeType, int n, const SkScalar edges[])
296 : fEdgeType(edgeType)
298 // Factory function should have already ensured this.
299 SkASSERT(n <= kMaxEdges);
300 memcpy(fEdges, edges, 3 * n * sizeof(SkScalar));
301 // Outset the edges by 0.5 so that a pixel with center on an edge is 50% covered in the AA case
302 // and 100% covered in the non-AA case.
303 for (int i = 0; i < n; ++i) {
304 fEdges[3 * i + 2] += SK_ScalarHalf;
306 this->setWillReadFragmentPosition();
309 bool GrConvexPolyEffect::onIsEqual(const GrEffect& other) const {
310 const GrConvexPolyEffect& cpe = CastEffect<GrConvexPolyEffect>(other);
311 // ignore the fact that 0 == -0 and just use memcmp.
312 return (cpe.fEdgeType == fEdgeType && cpe.fEdgeCount == fEdgeCount &&
313 0 == memcmp(cpe.fEdges, fEdges, 3 * fEdgeCount * sizeof(SkScalar)));
316 //////////////////////////////////////////////////////////////////////////////
318 GR_DEFINE_EFFECT_TEST(GrConvexPolyEffect);
320 GrEffectRef* GrConvexPolyEffect::TestCreate(SkRandom* random,
322 const GrDrawTargetCaps& caps,
324 EdgeType edgeType = static_cast<EdgeType>(random->nextULessThan(kEdgeTypeCnt));
325 int count = random->nextULessThan(kMaxEdges) + 1;
326 SkScalar edges[kMaxEdges * 3];
327 for (int i = 0; i < 3 * count; ++i) {
328 edges[i] = random->nextSScalar1();
331 return GrConvexPolyEffect::Create(edgeType, count, edges);