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" |
| 12 | #include "gl/GrGLVertexEffect.h" |
| 13 | #include "GrTBackendEffectFactory.h" |
| 14 | |
| 15 | #include "SkPath.h" |
| 16 | |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame^] | 17 | ////////////////////////////////////////////////////////////////////////////// |
| 18 | class GLAARectEffect; |
| 19 | |
| 20 | class AARectEffect : public GrEffect { |
| 21 | public: |
| 22 | typedef GLAARectEffect GLEffect; |
| 23 | |
| 24 | const SkRect& getRect() const { return fRect; } |
| 25 | |
| 26 | static const char* Name() { return "AARect"; } |
| 27 | |
| 28 | static GrEffectRef* Create(const SkRect& rect) { |
| 29 | return CreateEffectRef(AutoEffectUnref(SkNEW_ARGS(AARectEffect, (rect)))); |
| 30 | } |
| 31 | |
| 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. |
| 36 | *color = 0x00000000; |
| 37 | *validFlags = kRGBA_GrColorComponentFlags; |
| 38 | } else { |
| 39 | *validFlags = 0; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
| 44 | |
| 45 | private: |
| 46 | AARectEffect(const SkRect& rect) : fRect(rect) { |
| 47 | this->setWillReadFragmentPosition(); |
| 48 | } |
| 49 | |
| 50 | virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { |
| 51 | const AARectEffect& aare = CastEffect<AARectEffect>(other); |
| 52 | return fRect == aare.fRect; |
| 53 | } |
| 54 | |
| 55 | SkRect fRect; |
| 56 | typedef GrEffect INHERITED; |
| 57 | |
| 58 | GR_DECLARE_EFFECT_TEST; |
| 59 | |
| 60 | }; |
| 61 | |
| 62 | GR_DEFINE_EFFECT_TEST(AARectEffect); |
| 63 | |
| 64 | GrEffectRef* AARectEffect::TestCreate(SkRandom* random, |
| 65 | GrContext*, |
| 66 | const GrDrawTargetCaps& caps, |
| 67 | GrTexture*[]) { |
| 68 | SkRect rect = SkRect::MakeLTRB(random->nextSScalar1(), |
| 69 | random->nextSScalar1(), |
| 70 | random->nextSScalar1(), |
| 71 | random->nextSScalar1()); |
| 72 | return AARectEffect::Create(rect); |
| 73 | } |
| 74 | |
| 75 | ////////////////////////////////////////////////////////////////////////////// |
| 76 | |
| 77 | class GLAARectEffect : public GrGLEffect { |
| 78 | public: |
| 79 | GLAARectEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| 80 | |
| 81 | virtual void emitCode(GrGLShaderBuilder* builder, |
| 82 | const GrDrawEffect& drawEffect, |
| 83 | EffectKey key, |
| 84 | const char* outputColor, |
| 85 | const char* inputColor, |
| 86 | const TransformedCoordsArray&, |
| 87 | const TextureSamplerArray&) SK_OVERRIDE; |
| 88 | |
| 89 | static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&) { return 0; } |
| 90 | |
| 91 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; |
| 92 | |
| 93 | private: |
| 94 | GrGLUniformManager::UniformHandle fRectUniform; |
| 95 | SkRect fPrevRect; |
| 96 | typedef GrGLEffect INHERITED; |
| 97 | }; |
| 98 | |
| 99 | GLAARectEffect::GLAARectEffect(const GrBackendEffectFactory& factory, |
| 100 | const GrDrawEffect& drawEffect) |
| 101 | : INHERITED (factory) { |
| 102 | fPrevRect.fLeft = SK_ScalarNaN; |
| 103 | } |
| 104 | |
| 105 | void GLAARectEffect::emitCode(GrGLShaderBuilder* builder, |
| 106 | const GrDrawEffect& drawEffect, |
| 107 | EffectKey key, |
| 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, |
| 114 | kVec4f_GrSLType, |
| 115 | "rect", |
| 116 | &rectName); |
| 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"); |
| 128 | |
| 129 | builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor, |
| 130 | (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str()); |
| 131 | } |
| 132 | |
| 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); |
| 139 | fPrevRect = rect; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | const GrBackendEffectFactory& AARectEffect::getFactory() const { |
| 144 | return GrTBackendEffectFactory<AARectEffect>::getInstance(); |
| 145 | } |
| 146 | |
| 147 | ////////////////////////////////////////////////////////////////////////////// |
| 148 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 149 | class GrGLConvexPolyEffect : public GrGLEffect { |
| 150 | public: |
| 151 | GrGLConvexPolyEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| 152 | |
| 153 | virtual void emitCode(GrGLShaderBuilder* builder, |
| 154 | const GrDrawEffect& drawEffect, |
| 155 | EffectKey key, |
| 156 | const char* outputColor, |
| 157 | const char* inputColor, |
| 158 | const TransformedCoordsArray&, |
| 159 | const TextureSamplerArray&) SK_OVERRIDE; |
| 160 | |
| 161 | static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
| 162 | |
| 163 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; |
| 164 | |
| 165 | private: |
| 166 | GrGLUniformManager::UniformHandle fEdgeUniform; |
| 167 | SkScalar fPrevEdges[3 * GrConvexPolyEffect::kMaxEdges]; |
| 168 | typedef GrGLEffect INHERITED; |
| 169 | }; |
| 170 | |
| 171 | GrGLConvexPolyEffect::GrGLConvexPolyEffect(const GrBackendEffectFactory& factory, |
| 172 | const GrDrawEffect& drawEffect) |
| 173 | : INHERITED (factory) { |
| 174 | fPrevEdges[0] = SK_ScalarNaN; |
| 175 | } |
| 176 | |
| 177 | void GrGLConvexPolyEffect::emitCode(GrGLShaderBuilder* builder, |
| 178 | const GrDrawEffect& drawEffect, |
| 179 | EffectKey key, |
| 180 | const char* outputColor, |
| 181 | const char* inputColor, |
| 182 | const TransformedCoordsArray&, |
| 183 | const TextureSamplerArray& samplers) { |
| 184 | const GrConvexPolyEffect& cpe = drawEffect.castEffect<GrConvexPolyEffect>(); |
| 185 | |
| 186 | const char *edgeArrayName; |
| 187 | fEdgeUniform = builder->addUniformArray(GrGLShaderBuilder::kFragment_Visibility, |
| 188 | kVec3f_GrSLType, |
| 189 | "edges", |
| 190 | cpe.getEdgeCount(), |
| 191 | &edgeArrayName); |
| 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"); |
| 202 | break; |
| 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"); |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | |
commit-bot@chromium.org | 6dee875 | 2014-02-07 22:39:01 +0000 | [diff] [blame] | 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"); |
| 213 | } |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 214 | builder->fsCodeAppendf("\t%s = %s;\n", outputColor, |
| 215 | (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str()); |
| 216 | } |
| 217 | |
| 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); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | GrGLEffect::EffectKey GrGLConvexPolyEffect::GenKey(const GrDrawEffect& drawEffect, |
| 228 | const GrGLCaps&) { |
| 229 | const GrConvexPolyEffect& cpe = drawEffect.castEffect<GrConvexPolyEffect>(); |
| 230 | GR_STATIC_ASSERT(GrConvexPolyEffect::kEdgeTypeCnt <= 4); |
| 231 | return (cpe.getEdgeCount() << 2) | cpe.getEdgeType(); |
| 232 | } |
| 233 | |
| 234 | ////////////////////////////////////////////////////////////////////////////// |
| 235 | |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 236 | 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] | 237 | if (path.getSegmentMasks() != SkPath::kLine_SegmentMask || |
| 238 | !path.isConvex() || |
| 239 | path.isInverseFillType()) { |
| 240 | return NULL; |
| 241 | } |
| 242 | |
| 243 | if (path.countPoints() > kMaxEdges) { |
| 244 | return NULL; |
| 245 | } |
| 246 | |
| 247 | SkPoint pts[kMaxEdges]; |
| 248 | SkScalar edges[3 * kMaxEdges]; |
| 249 | |
| 250 | SkPath::Direction dir; |
| 251 | SkAssertResult(path.cheapComputeDirection(&dir)); |
| 252 | |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 253 | SkVector t; |
| 254 | if (NULL == offset) { |
| 255 | t.set(0, 0); |
| 256 | } else { |
| 257 | t = *offset; |
| 258 | } |
| 259 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 260 | int count = path.getPoints(pts, kMaxEdges); |
| 261 | int n = 0; |
| 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]; |
| 265 | v.normalize(); |
| 266 | if (SkPath::kCCW_Direction == dir) { |
| 267 | edges[3 * n] = v.fY; |
| 268 | edges[3 * n + 1] = -v.fX; |
| 269 | } else { |
| 270 | edges[3 * n] = -v.fY; |
| 271 | edges[3 * n + 1] = v.fX; |
| 272 | } |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 273 | SkPoint p = pts[i] + t; |
| 274 | 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] | 275 | ++n; |
| 276 | } |
| 277 | } |
| 278 | return Create(type, n, edges); |
| 279 | } |
| 280 | |
commit-bot@chromium.org | f053980 | 2014-02-08 19:31:05 +0000 | [diff] [blame^] | 281 | GrEffectRef* GrConvexPolyEffect::CreateForAAFillRect(const SkRect& rect) { |
| 282 | return AARectEffect::Create(rect); |
| 283 | } |
| 284 | |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 285 | GrConvexPolyEffect::~GrConvexPolyEffect() {} |
| 286 | |
| 287 | void GrConvexPolyEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
| 288 | *validFlags = 0; |
| 289 | } |
| 290 | |
| 291 | const GrBackendEffectFactory& GrConvexPolyEffect::getFactory() const { |
| 292 | return GrTBackendEffectFactory<GrConvexPolyEffect>::getInstance(); |
| 293 | } |
| 294 | |
| 295 | GrConvexPolyEffect::GrConvexPolyEffect(EdgeType edgeType, int n, const SkScalar edges[]) |
| 296 | : fEdgeType(edgeType) |
| 297 | , fEdgeCount(n) { |
| 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; |
| 305 | } |
| 306 | this->setWillReadFragmentPosition(); |
| 307 | } |
| 308 | |
| 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))); |
| 314 | } |
| 315 | |
| 316 | ////////////////////////////////////////////////////////////////////////////// |
| 317 | |
| 318 | GR_DEFINE_EFFECT_TEST(GrConvexPolyEffect); |
| 319 | |
| 320 | GrEffectRef* GrConvexPolyEffect::TestCreate(SkRandom* random, |
| 321 | GrContext*, |
| 322 | const GrDrawTargetCaps& caps, |
| 323 | GrTexture*[]) { |
| 324 | EdgeType edgeType = static_cast<EdgeType>(random->nextULessThan(kEdgeTypeCnt)); |
commit-bot@chromium.org | 65ee5f4 | 2014-02-04 17:49:48 +0000 | [diff] [blame] | 325 | int count = random->nextULessThan(kMaxEdges) + 1; |
commit-bot@chromium.org | c3fe549 | 2014-01-30 18:15:51 +0000 | [diff] [blame] | 326 | SkScalar edges[kMaxEdges * 3]; |
| 327 | for (int i = 0; i < 3 * count; ++i) { |
| 328 | edges[i] = random->nextSScalar1(); |
| 329 | } |
| 330 | |
| 331 | return GrConvexPolyEffect::Create(edgeType, count, edges); |
| 332 | } |