commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 1 | /* |
| 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" |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 12 | #include "GrTBackendEffectFactory.h" |
| 13 | |
| 14 | #include "SkPath.h" |
| 15 | |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 16 | ////////////////////////////////////////////////////////////////////////////// |
| 17 | class GLAARectEffect; |
| 18 | |
| 19 | class AARectEffect : public GrEffect { |
| 20 | public: |
| 21 | typedef GLAARectEffect GLEffect; |
| 22 | |
| 23 | const SkRect& getRect() const { return fRect; } |
| 24 | |
| 25 | static const char* Name() { return "AARect"; } |
| 26 | |
| 27 | static GrEffectRef* Create(const SkRect& rect) { |
| 28 | return CreateEffectRef(AutoEffectUnref(SkNEW_ARGS(AARectEffect, (rect)))); |
| 29 | } |
| 30 | |
| 31 | virtual void getConstantColorComponents(GrColor* color, |
| 32 | uint32_t* validFlags) const SK_OVERRIDE { |
| 33 | if (fRect.isEmpty()) { |
| 34 | // An empty rect will have no coverage anywhere. |
| 35 | *color = 0x00000000; |
| 36 | *validFlags = kRGBA_GrColorComponentFlags; |
| 37 | } else { |
| 38 | *validFlags = 0; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
| 43 | |
| 44 | private: |
| 45 | AARectEffect(const SkRect& rect) : fRect(rect) { |
| 46 | this->setWillReadFragmentPosition(); |
| 47 | } |
| 48 | |
| 49 | virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { |
| 50 | const AARectEffect& aare = CastEffect<AARectEffect>(other); |
| 51 | return fRect == aare.fRect; |
| 52 | } |
| 53 | |
| 54 | SkRect fRect; |
| 55 | typedef GrEffect INHERITED; |
| 56 | |
| 57 | GR_DECLARE_EFFECT_TEST; |
| 58 | |
| 59 | }; |
| 60 | |
| 61 | GR_DEFINE_EFFECT_TEST(AARectEffect); |
| 62 | |
| 63 | GrEffectRef* AARectEffect::TestCreate(SkRandom* random, |
| 64 | GrContext*, |
| 65 | const GrDrawTargetCaps& caps, |
| 66 | GrTexture*[]) { |
| 67 | SkRect rect = SkRect::MakeLTRB(random->nextSScalar1(), |
| 68 | random->nextSScalar1(), |
| 69 | random->nextSScalar1(), |
| 70 | random->nextSScalar1()); |
| 71 | return AARectEffect::Create(rect); |
| 72 | } |
| 73 | |
| 74 | ////////////////////////////////////////////////////////////////////////////// |
| 75 | |
| 76 | class GLAARectEffect : public GrGLEffect { |
| 77 | public: |
| 78 | GLAARectEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| 79 | |
| 80 | virtual void emitCode(GrGLShaderBuilder* builder, |
| 81 | const GrDrawEffect& drawEffect, |
| 82 | EffectKey key, |
| 83 | const char* outputColor, |
| 84 | const char* inputColor, |
| 85 | const TransformedCoordsArray&, |
| 86 | const TextureSamplerArray&) SK_OVERRIDE; |
| 87 | |
| 88 | static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&) { return 0; } |
| 89 | |
| 90 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; |
| 91 | |
| 92 | private: |
| 93 | GrGLUniformManager::UniformHandle fRectUniform; |
| 94 | SkRect fPrevRect; |
| 95 | typedef GrGLEffect INHERITED; |
| 96 | }; |
| 97 | |
| 98 | GLAARectEffect::GLAARectEffect(const GrBackendEffectFactory& factory, |
| 99 | const GrDrawEffect& drawEffect) |
| 100 | : INHERITED (factory) { |
| 101 | fPrevRect.fLeft = SK_ScalarNaN; |
| 102 | } |
| 103 | |
| 104 | void GLAARectEffect::emitCode(GrGLShaderBuilder* builder, |
| 105 | const GrDrawEffect& drawEffect, |
| 106 | EffectKey key, |
| 107 | const char* outputColor, |
| 108 | const char* inputColor, |
| 109 | const TransformedCoordsArray&, |
| 110 | const TextureSamplerArray& samplers) { |
| 111 | const char *rectName; |
| 112 | fRectUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 113 | kVec4f_GrSLType, |
| 114 | "rect", |
| 115 | &rectName); |
| 116 | const char* fragmentPos = builder->fragmentPosition(); |
| 117 | // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5), |
| 118 | // respectively. The amount of coverage removed in x and y by the edges is computed as a pair of |
| 119 | // negative numbers, xSub and ySub. |
| 120 | builder->fsCodeAppend("\t\tfloat xSub, ySub;\n"); |
| 121 | builder->fsCodeAppendf("\t\txSub = min(%s.x - %s.x, 0.0);\n", fragmentPos, rectName); |
| 122 | builder->fsCodeAppendf("\t\txSub += min(%s.z - %s.x, 0.0);\n", rectName, fragmentPos); |
| 123 | builder->fsCodeAppendf("\t\tySub = min(%s.y - %s.y, 0.0);\n", fragmentPos, rectName); |
| 124 | builder->fsCodeAppendf("\t\tySub += min(%s.w - %s.y, 0.0);\n", rectName, fragmentPos); |
| 125 | // Now compute coverage in x and y and multiply them to get the fraction of the pixel covered. |
| 126 | builder->fsCodeAppendf("\t\tfloat alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0));\n"); |
| 127 | |
| 128 | builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor, |
| 129 | (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str()); |
| 130 | } |
| 131 | |
| 132 | void GLAARectEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) { |
| 133 | const AARectEffect& aare = drawEffect.castEffect<AARectEffect>(); |
| 134 | const SkRect& rect = aare.getRect(); |
| 135 | if (rect != fPrevRect) { |
| 136 | uman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f, |
| 137 | rect.fRight - 0.5f, rect.fBottom - 0.5f); |
| 138 | fPrevRect = rect; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | const GrBackendEffectFactory& AARectEffect::getFactory() const { |
| 143 | return GrTBackendEffectFactory<AARectEffect>::getInstance(); |
| 144 | } |
| 145 | |
| 146 | ////////////////////////////////////////////////////////////////////////////// |
| 147 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 148 | class GrGLConvexPolyEffect : public GrGLEffect { |
| 149 | public: |
| 150 | GrGLConvexPolyEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| 151 | |
| 152 | virtual void emitCode(GrGLShaderBuilder* builder, |
| 153 | const GrDrawEffect& drawEffect, |
| 154 | EffectKey key, |
| 155 | const char* outputColor, |
| 156 | const char* inputColor, |
| 157 | const TransformedCoordsArray&, |
| 158 | const TextureSamplerArray&) SK_OVERRIDE; |
| 159 | |
| 160 | static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
| 161 | |
| 162 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; |
| 163 | |
| 164 | private: |
| 165 | GrGLUniformManager::UniformHandle fEdgeUniform; |
| 166 | SkScalar fPrevEdges[3 * GrConvexPolyEffect::kMaxEdges]; |
| 167 | typedef GrGLEffect INHERITED; |
| 168 | }; |
| 169 | |
| 170 | GrGLConvexPolyEffect::GrGLConvexPolyEffect(const GrBackendEffectFactory& factory, |
| 171 | const GrDrawEffect& drawEffect) |
| 172 | : INHERITED (factory) { |
| 173 | fPrevEdges[0] = SK_ScalarNaN; |
| 174 | } |
| 175 | |
| 176 | void GrGLConvexPolyEffect::emitCode(GrGLShaderBuilder* builder, |
| 177 | const GrDrawEffect& drawEffect, |
| 178 | EffectKey key, |
| 179 | const char* outputColor, |
| 180 | const char* inputColor, |
| 181 | const TransformedCoordsArray&, |
| 182 | const TextureSamplerArray& samplers) { |
| 183 | const GrConvexPolyEffect& cpe = drawEffect.castEffect<GrConvexPolyEffect>(); |
| 184 | |
| 185 | const char *edgeArrayName; |
| 186 | fEdgeUniform = builder->addUniformArray(GrGLShaderBuilder::kFragment_Visibility, |
| 187 | kVec3f_GrSLType, |
| 188 | "edges", |
| 189 | cpe.getEdgeCount(), |
| 190 | &edgeArrayName); |
| 191 | builder->fsCodeAppend("\t\tfloat alpha = 1.0;\n"); |
| 192 | builder->fsCodeAppend("\t\tfloat edge;\n"); |
| 193 | const char* fragmentPos = builder->fragmentPosition(); |
| 194 | for (int i = 0; i < cpe.getEdgeCount(); ++i) { |
| 195 | builder->fsCodeAppendf("\t\tedge = dot(%s[%d], vec3(%s.x, %s.y, 1));\n", |
| 196 | edgeArrayName, i, fragmentPos, fragmentPos); |
| 197 | switch (cpe.getEdgeType()) { |
| 198 | case GrConvexPolyEffect::kFillAA_EdgeType: |
| 199 | builder->fsCodeAppend("\t\tedge = clamp(edge, 0.0, 1.0);\n"); |
| 200 | builder->fsCodeAppend("\t\talpha *= edge;\n"); |
| 201 | break; |
| 202 | case GrConvexPolyEffect::kFillNoAA_EdgeType: |
| 203 | builder->fsCodeAppend("\t\tedge = edge >= 0.5 ? 1.0 : 0.0;\n"); |
| 204 | builder->fsCodeAppend("\t\talpha *= edge;\n"); |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | |
commit-bot@chromium.org | 6dee875 | 2014-02-07 22:39:01 +0000 | [diff] [blame] | 209 | // Woe is me. See skbug.com/2149. |
| 210 | if (kTegra2_GrGLRenderer == builder->ctxInfo().renderer()) { |
| 211 | builder->fsCodeAppend("\t\tif (-1.0 == alpha) {\n\t\t\tdiscard;\n\t\t}\n"); |
| 212 | } |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 213 | builder->fsCodeAppendf("\t%s = %s;\n", outputColor, |
| 214 | (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str()); |
| 215 | } |
| 216 | |
| 217 | void GrGLConvexPolyEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) { |
| 218 | const GrConvexPolyEffect& cpe = drawEffect.castEffect<GrConvexPolyEffect>(); |
| 219 | size_t byteSize = 3 * cpe.getEdgeCount() * sizeof(SkScalar); |
| 220 | if (0 != memcmp(fPrevEdges, cpe.getEdges(), byteSize)) { |
| 221 | uman.set3fv(fEdgeUniform, cpe.getEdgeCount(), cpe.getEdges()); |
| 222 | memcpy(fPrevEdges, cpe.getEdges(), byteSize); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | GrGLEffect::EffectKey GrGLConvexPolyEffect::GenKey(const GrDrawEffect& drawEffect, |
| 227 | const GrGLCaps&) { |
| 228 | const GrConvexPolyEffect& cpe = drawEffect.castEffect<GrConvexPolyEffect>(); |
| 229 | GR_STATIC_ASSERT(GrConvexPolyEffect::kEdgeTypeCnt <= 4); |
| 230 | return (cpe.getEdgeCount() << 2) | cpe.getEdgeType(); |
| 231 | } |
| 232 | |
| 233 | ////////////////////////////////////////////////////////////////////////////// |
| 234 | |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 235 | GrEffectRef* GrConvexPolyEffect::Create(EdgeType type, const SkPath& path, const SkVector* offset) { |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 236 | if (path.getSegmentMasks() != SkPath::kLine_SegmentMask || |
| 237 | !path.isConvex() || |
| 238 | path.isInverseFillType()) { |
| 239 | return NULL; |
| 240 | } |
| 241 | |
| 242 | if (path.countPoints() > kMaxEdges) { |
| 243 | return NULL; |
| 244 | } |
| 245 | |
| 246 | SkPoint pts[kMaxEdges]; |
| 247 | SkScalar edges[3 * kMaxEdges]; |
| 248 | |
| 249 | SkPath::Direction dir; |
| 250 | SkAssertResult(path.cheapComputeDirection(&dir)); |
| 251 | |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 252 | SkVector t; |
| 253 | if (NULL == offset) { |
| 254 | t.set(0, 0); |
| 255 | } else { |
| 256 | t = *offset; |
| 257 | } |
| 258 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 259 | int count = path.getPoints(pts, kMaxEdges); |
| 260 | int n = 0; |
| 261 | for (int lastPt = count - 1, i = 0; i < count; lastPt = i++) { |
| 262 | if (pts[lastPt] != pts[i]) { |
| 263 | SkVector v = pts[i] - pts[lastPt]; |
| 264 | v.normalize(); |
| 265 | if (SkPath::kCCW_Direction == dir) { |
| 266 | edges[3 * n] = v.fY; |
| 267 | edges[3 * n + 1] = -v.fX; |
| 268 | } else { |
| 269 | edges[3 * n] = -v.fY; |
| 270 | edges[3 * n + 1] = v.fX; |
| 271 | } |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 272 | SkPoint p = pts[i] + t; |
| 273 | edges[3 * n + 2] = -(edges[3 * n] * p.fX + edges[3 * n + 1] * p.fY); |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 274 | ++n; |
| 275 | } |
| 276 | } |
| 277 | return Create(type, n, edges); |
| 278 | } |
| 279 | |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame] | 280 | GrEffectRef* GrConvexPolyEffect::CreateForAAFillRect(const SkRect& rect) { |
| 281 | return AARectEffect::Create(rect); |
| 282 | } |
| 283 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 284 | GrConvexPolyEffect::~GrConvexPolyEffect() {} |
| 285 | |
| 286 | void GrConvexPolyEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
| 287 | *validFlags = 0; |
| 288 | } |
| 289 | |
| 290 | const GrBackendEffectFactory& GrConvexPolyEffect::getFactory() const { |
| 291 | return GrTBackendEffectFactory<GrConvexPolyEffect>::getInstance(); |
| 292 | } |
| 293 | |
| 294 | GrConvexPolyEffect::GrConvexPolyEffect(EdgeType edgeType, int n, const SkScalar edges[]) |
| 295 | : fEdgeType(edgeType) |
| 296 | , fEdgeCount(n) { |
| 297 | // Factory function should have already ensured this. |
| 298 | SkASSERT(n <= kMaxEdges); |
| 299 | memcpy(fEdges, edges, 3 * n * sizeof(SkScalar)); |
| 300 | // Outset the edges by 0.5 so that a pixel with center on an edge is 50% covered in the AA case |
| 301 | // and 100% covered in the non-AA case. |
| 302 | for (int i = 0; i < n; ++i) { |
| 303 | fEdges[3 * i + 2] += SK_ScalarHalf; |
| 304 | } |
| 305 | this->setWillReadFragmentPosition(); |
| 306 | } |
| 307 | |
| 308 | bool GrConvexPolyEffect::onIsEqual(const GrEffect& other) const { |
| 309 | const GrConvexPolyEffect& cpe = CastEffect<GrConvexPolyEffect>(other); |
| 310 | // ignore the fact that 0 == -0 and just use memcmp. |
| 311 | return (cpe.fEdgeType == fEdgeType && cpe.fEdgeCount == fEdgeCount && |
| 312 | 0 == memcmp(cpe.fEdges, fEdges, 3 * fEdgeCount * sizeof(SkScalar))); |
| 313 | } |
| 314 | |
| 315 | ////////////////////////////////////////////////////////////////////////////// |
| 316 | |
| 317 | GR_DEFINE_EFFECT_TEST(GrConvexPolyEffect); |
| 318 | |
| 319 | GrEffectRef* GrConvexPolyEffect::TestCreate(SkRandom* random, |
| 320 | GrContext*, |
| 321 | const GrDrawTargetCaps& caps, |
| 322 | GrTexture*[]) { |
| 323 | EdgeType edgeType = static_cast<EdgeType>(random->nextULessThan(kEdgeTypeCnt)); |
commit-bot@chromium.org | 65ee5f4 | 2014-02-04 17:49:48 +0000 | [diff] [blame] | 324 | int count = random->nextULessThan(kMaxEdges) + 1; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 325 | SkScalar edges[kMaxEdges * 3]; |
| 326 | for (int i = 0; i < 3 * count; ++i) { |
| 327 | edges[i] = random->nextSScalar1(); |
| 328 | } |
| 329 | |
| 330 | return GrConvexPolyEffect::Create(edgeType, count, edges); |
| 331 | } |