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