| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +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 "GrRRectEffect.h" |
| 9 | |
| 10 | #include "gl/GrGLEffect.h" |
| 11 | #include "gl/GrGLSL.h" |
| 12 | #include "GrTBackendEffectFactory.h" |
| 13 | |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 14 | #include "SkRRect.h" |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 15 | |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 16 | using namespace GrRRectEffect; |
| 17 | |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 18 | class GLRRectEffect; |
| 19 | |
| 20 | class RRectEffect : public GrEffect { |
| 21 | public: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 22 | // This effect only supports circular corner rrects where the radius is >= kRadiusMin. |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 23 | static const SkScalar kRadiusMin; |
| skia.committer@gmail.com | f1f66c0 | 2014-03-05 03:02:06 +0000 | [diff] [blame] | 24 | |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 25 | /// The types of circular corner rrects supported |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 26 | enum RRectType { |
| 27 | kCircleCorner_RRectType, //<! All four corners have the same circular radius. |
| 28 | kLeftCircleTab_RRectType, //<! The left side has circular corners, the right is a rect. |
| 29 | kTopCircleTab_RRectType, //<! etc |
| 30 | kRightCircleTab_RRectType, |
| 31 | kBottomCircleTab_RRectType, |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
| commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame^] | 34 | static GrEffectRef* Create(GrEffectEdgeType, RRectType, const SkRRect&); |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 35 | |
| 36 | virtual ~RRectEffect() {}; |
| 37 | static const char* Name() { return "RRect"; } |
| 38 | |
| 39 | const SkRRect& getRRect() const { return fRRect; } |
| 40 | |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 41 | RRectType getType() const { return fRRectType; } |
| commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame^] | 42 | |
| 43 | GrEffectEdgeType getEdgeType() const { return fEdgeType; } |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 44 | |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 45 | typedef GLRRectEffect GLEffect; |
| 46 | |
| 47 | virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; |
| 48 | |
| 49 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
| 50 | |
| 51 | private: |
| commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame^] | 52 | RRectEffect(GrEffectEdgeType, RRectType, const SkRRect&); |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 53 | |
| 54 | virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE; |
| 55 | |
| commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame^] | 56 | SkRRect fRRect; |
| 57 | GrEffectEdgeType fEdgeType; |
| 58 | RRectType fRRectType; |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 59 | |
| 60 | GR_DECLARE_EFFECT_TEST; |
| 61 | |
| 62 | typedef GrEffect INHERITED; |
| 63 | }; |
| 64 | |
| 65 | const SkScalar RRectEffect::kRadiusMin = 0.5f; |
| 66 | |
| commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame^] | 67 | GrEffectRef* RRectEffect::Create(GrEffectEdgeType edgeType, |
| 68 | RRectType rrType, |
| 69 | const SkRRect& rrect) { |
| 70 | SkASSERT(kFillAA_GrEffectEdgeType == edgeType || kInverseFillBW_GrEffectEdgeType == edgeType); |
| 71 | return CreateEffectRef(AutoEffectUnref(SkNEW_ARGS(RRectEffect, (edgeType, rrType, rrect)))); |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void RRectEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
| 75 | *validFlags = 0; |
| 76 | } |
| 77 | |
| 78 | const GrBackendEffectFactory& RRectEffect::getFactory() const { |
| 79 | return GrTBackendEffectFactory<RRectEffect>::getInstance(); |
| 80 | } |
| 81 | |
| commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame^] | 82 | RRectEffect::RRectEffect(GrEffectEdgeType edgeType, RRectType rrType, const SkRRect& rrect) |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 83 | : fRRect(rrect) |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 84 | , fEdgeType(edgeType) |
| commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame^] | 85 | , fRRectType(rrType) { |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 86 | this->setWillReadFragmentPosition(); |
| 87 | } |
| 88 | |
| 89 | bool RRectEffect::onIsEqual(const GrEffect& other) const { |
| 90 | const RRectEffect& rre = CastEffect<RRectEffect>(other); |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 91 | // type is derived from fRRect, so no need to check it. |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 92 | return fEdgeType == rre.fEdgeType && fRRect == rre.fRRect; |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 93 | } |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 94 | |
| 95 | ////////////////////////////////////////////////////////////////////////////// |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 96 | |
| 97 | GR_DEFINE_EFFECT_TEST(RRectEffect); |
| 98 | |
| 99 | GrEffectRef* RRectEffect::TestCreate(SkRandom* random, |
| 100 | GrContext*, |
| 101 | const GrDrawTargetCaps& caps, |
| 102 | GrTexture*[]) { |
| 103 | SkScalar w = random->nextRangeScalar(20.f, 1000.f); |
| 104 | SkScalar h = random->nextRangeScalar(20.f, 1000.f); |
| 105 | SkScalar r = random->nextRangeF(kRadiusMin, 9.f); |
| 106 | SkRRect rrect; |
| 107 | rrect.setRectXY(SkRect::MakeWH(w, h), r, r); |
| commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame^] | 108 | GrEffectRef* effect; |
| 109 | do { |
| 110 | GrEffectEdgeType et = (GrEffectEdgeType)random->nextULessThan(kGrEffectEdgeTypeCnt); |
| 111 | effect = GrRRectEffect::Create(et, rrect); |
| 112 | } while (NULL == effect); |
| 113 | return effect; |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | ////////////////////////////////////////////////////////////////////////////// |
| 117 | |
| 118 | class GLRRectEffect : public GrGLEffect { |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 119 | public: |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 120 | GLRRectEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 121 | |
| 122 | virtual void emitCode(GrGLShaderBuilder* builder, |
| 123 | const GrDrawEffect& drawEffect, |
| 124 | EffectKey key, |
| 125 | const char* outputColor, |
| 126 | const char* inputColor, |
| 127 | const TransformedCoordsArray&, |
| 128 | const TextureSamplerArray&) SK_OVERRIDE; |
| 129 | |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 130 | static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 131 | |
| 132 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; |
| 133 | |
| 134 | private: |
| 135 | GrGLUniformManager::UniformHandle fInnerRectUniform; |
| 136 | GrGLUniformManager::UniformHandle fRadiusPlusHalfUniform; |
| 137 | SkRRect fPrevRRect; |
| 138 | typedef GrGLEffect INHERITED; |
| 139 | }; |
| 140 | |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 141 | GLRRectEffect::GLRRectEffect(const GrBackendEffectFactory& factory, |
| 142 | const GrDrawEffect& drawEffect) |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 143 | : INHERITED (factory) { |
| 144 | fPrevRRect.setEmpty(); |
| 145 | } |
| 146 | |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 147 | void GLRRectEffect::emitCode(GrGLShaderBuilder* builder, |
| 148 | const GrDrawEffect& drawEffect, |
| 149 | EffectKey key, |
| 150 | const char* outputColor, |
| 151 | const char* inputColor, |
| 152 | const TransformedCoordsArray&, |
| 153 | const TextureSamplerArray& samplers) { |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 154 | const RRectEffect& rre = drawEffect.castEffect<RRectEffect>(); |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 155 | const char *rectName; |
| 156 | const char *radiusPlusHalfName; |
| 157 | // The inner rect is the rrect bounds inset by the radius. Its top, left, right, and bottom |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 158 | // edges correspond to components x, y, z, and w, respectively. When one side of the rrect has |
| 159 | // rectangular corners, that side's value corresponds to the rect edge's value outset by half a |
| 160 | // pixel. |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 161 | fInnerRectUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 162 | kVec4f_GrSLType, |
| 163 | "innerRect", |
| 164 | &rectName); |
| 165 | fRadiusPlusHalfUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 166 | kFloat_GrSLType, |
| 167 | "radiusPlusHalf", |
| 168 | &radiusPlusHalfName); |
| 169 | const char* fragmentPos = builder->fragmentPosition(); |
| 170 | // At each quarter-circle corner we compute a vector that is the offset of the fragment position |
| 171 | // from the circle center. The vector is pinned in x and y to be in the quarter-plane relevant |
| 172 | // to that corner. This means that points near the interior near the rrect top edge will have |
| 173 | // a vector that points straight up for both the TL left and TR corners. Computing an |
| 174 | // alpha from this vector at either the TR or TL corner will give the correct result. Similarly, |
| 175 | // fragments near the other three edges will get the correct AA. Fragments in the interior of |
| 176 | // the rrect will have a (0,0) vector at all four corners. So long as the radius > 0.5 they will |
| 177 | // correctly produce an alpha value of 1 at all four corners. We take the min of all the alphas. |
| 178 | // The code below is a simplified version of the above that performs maxs on the vector |
| 179 | // components before computing distances and alpha values so that only one distance computation |
| 180 | // need be computed to determine the min alpha. |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 181 | // |
| 182 | // For the cases where one half of the rrect is rectangular we drop one of the x or y |
| 183 | // computations, compute a separate rect edge alpha for the rect side, and mul the two computed |
| 184 | // alphas together. |
| 185 | switch (rre.getType()) { |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 186 | case RRectEffect::kCircleCorner_RRectType: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 187 | builder->fsCodeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos); |
| 188 | builder->fsCodeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName); |
| 189 | builder->fsCodeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n"); |
| 190 | builder->fsCodeAppendf("\t\tfloat alpha = clamp(%s - length(dxy), 0.0, 1.0);\n", |
| 191 | radiusPlusHalfName); |
| 192 | break; |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 193 | case RRectEffect::kLeftCircleTab_RRectType: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 194 | builder->fsCodeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos); |
| 195 | builder->fsCodeAppendf("\t\tfloat dy1 = %s.y - %s.w;\n", fragmentPos, rectName); |
| 196 | builder->fsCodeAppend("\t\tvec2 dxy = max(vec2(dxy0.x, max(dxy0.y, dy1)), 0.0);\n"); |
| 197 | builder->fsCodeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n", |
| 198 | rectName, fragmentPos); |
| 199 | builder->fsCodeAppendf("\t\tfloat alpha = rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| 200 | radiusPlusHalfName); |
| 201 | break; |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 202 | case RRectEffect::kTopCircleTab_RRectType: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 203 | builder->fsCodeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos); |
| 204 | builder->fsCodeAppendf("\t\tfloat dx1 = %s.x - %s.z;\n", fragmentPos, rectName); |
| 205 | builder->fsCodeAppend("\t\tvec2 dxy = max(vec2(max(dxy0.x, dx1), dxy0.y), 0.0);\n"); |
| 206 | builder->fsCodeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n", |
| 207 | rectName, fragmentPos); |
| 208 | builder->fsCodeAppendf("\t\tfloat alpha = bottomAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| 209 | radiusPlusHalfName); |
| 210 | break; |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 211 | case RRectEffect::kRightCircleTab_RRectType: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 212 | builder->fsCodeAppendf("\t\tfloat dy0 = %s.y - %s.y;\n", rectName, fragmentPos); |
| 213 | builder->fsCodeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName); |
| 214 | builder->fsCodeAppend("\t\tvec2 dxy = max(vec2(dxy1.x, max(dy0, dxy1.y)), 0.0);\n"); |
| 215 | builder->fsCodeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n", |
| 216 | fragmentPos, rectName); |
| 217 | builder->fsCodeAppendf("\t\tfloat alpha = leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| 218 | radiusPlusHalfName); |
| 219 | break; |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 220 | case RRectEffect::kBottomCircleTab_RRectType: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 221 | builder->fsCodeAppendf("\t\tfloat dx0 = %s.x - %s.x;\n", rectName, fragmentPos); |
| 222 | builder->fsCodeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName); |
| 223 | builder->fsCodeAppend("\t\tvec2 dxy = max(vec2(max(dx0, dxy1.x), dxy1.y), 0.0);\n"); |
| 224 | builder->fsCodeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n", |
| 225 | fragmentPos, rectName); |
| 226 | builder->fsCodeAppendf("\t\tfloat alpha = topAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| 227 | radiusPlusHalfName); |
| 228 | break; |
| 229 | } |
| commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame^] | 230 | |
| 231 | if (kInverseFillBW_GrEffectEdgeType == rre.getEdgeType()) { |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 232 | builder->fsCodeAppend("\t\talpha = 1.0 - alpha;\n"); |
| 233 | } |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 234 | |
| 235 | builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor, |
| 236 | (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str()); |
| 237 | } |
| 238 | |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 239 | GrGLEffect::EffectKey GLRRectEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { |
| 240 | const RRectEffect& rre = drawEffect.castEffect<RRectEffect>(); |
| commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame^] | 241 | GR_STATIC_ASSERT(kGrEffectEdgeTypeCnt <= 8); |
| 242 | return (rre.getType() << 3) | rre.getEdgeType(); |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 245 | void GLRRectEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) { |
| 246 | const RRectEffect& rre = drawEffect.castEffect<RRectEffect>(); |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 247 | const SkRRect& rrect = rre.getRRect(); |
| 248 | if (rrect != fPrevRRect) { |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 249 | SkRect rect = rrect.getBounds(); |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 250 | SkScalar radius = 0; |
| 251 | switch (rre.getType()) { |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 252 | case RRectEffect::kCircleCorner_RRectType: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 253 | SkASSERT(rrect.isSimpleCircular()); |
| 254 | radius = rrect.getSimpleRadii().fX; |
| 255 | SkASSERT(radius >= RRectEffect::kRadiusMin); |
| 256 | rect.inset(radius, radius); |
| 257 | break; |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 258 | case RRectEffect::kLeftCircleTab_RRectType: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 259 | radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX; |
| 260 | rect.fLeft += radius; |
| 261 | rect.fTop += radius; |
| 262 | rect.fRight += 0.5f; |
| 263 | rect.fBottom -= radius; |
| 264 | break; |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 265 | case RRectEffect::kTopCircleTab_RRectType: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 266 | radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX; |
| 267 | rect.fLeft += radius; |
| 268 | rect.fTop += radius; |
| 269 | rect.fRight -= radius; |
| 270 | rect.fBottom += 0.5f; |
| 271 | break; |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 272 | case RRectEffect::kRightCircleTab_RRectType: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 273 | radius = rrect.radii(SkRRect::kUpperRight_Corner).fX; |
| 274 | rect.fLeft -= 0.5f; |
| 275 | rect.fTop += radius; |
| 276 | rect.fRight -= radius; |
| 277 | rect.fBottom -= radius; |
| 278 | break; |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 279 | case RRectEffect::kBottomCircleTab_RRectType: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 280 | radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX; |
| 281 | rect.fLeft += radius; |
| 282 | rect.fTop -= 0.5f; |
| 283 | rect.fRight -= radius; |
| 284 | rect.fBottom -= radius; |
| 285 | break; |
| 286 | } |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 287 | uman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom); |
| 288 | uman.set1f(fRadiusPlusHalfUniform, radius + 0.5f); |
| 289 | fPrevRRect = rrect; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | ////////////////////////////////////////////////////////////////////////////// |
| 294 | |
| commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame^] | 295 | GrEffectRef* GrRRectEffect::Create(GrEffectEdgeType edgeType, const SkRRect& rrect) { |
| 296 | if (kFillAA_GrEffectEdgeType != edgeType && kInverseFillBW_GrEffectEdgeType != edgeType) { |
| 297 | return NULL; |
| 298 | } |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 299 | RRectEffect::RRectType rrtype; |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 300 | if (rrect.isSimpleCircular()) { |
| 301 | if (rrect.getSimpleRadii().fX < RRectEffect::kRadiusMin) { |
| 302 | return NULL; |
| 303 | } |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 304 | rrtype = RRectEffect::kCircleCorner_RRectType; |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 305 | } else if (rrect.isComplex()) { |
| 306 | // Check for the "tab" cases - two adjacent circular corners and two square corners. |
| 307 | SkScalar radius = 0; |
| 308 | int circleCornerBitfield = 0; |
| 309 | for (int c = 0; c < 4; ++c) { |
| 310 | const SkVector& r = rrect.radii((SkRRect::Corner)c); |
| 311 | SkASSERT((0 == r.fX) == (0 == r.fY)); |
| 312 | if (0 == r.fX) { |
| 313 | continue; |
| 314 | } |
| 315 | if (r.fX != r.fY) { |
| 316 | return NULL; |
| 317 | } |
| 318 | if (!circleCornerBitfield) { |
| 319 | radius = r.fX; |
| 320 | if (radius < RRectEffect::kRadiusMin) { |
| 321 | return NULL; |
| 322 | } |
| 323 | circleCornerBitfield = 1 << c; |
| 324 | } else { |
| 325 | if (r.fX != radius) { |
| 326 | return NULL; |
| 327 | } |
| 328 | circleCornerBitfield |= 1 << c; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | GR_STATIC_ASSERT(SkRRect::kUpperLeft_Corner == 0); |
| 333 | GR_STATIC_ASSERT(SkRRect::kUpperRight_Corner == 1); |
| 334 | GR_STATIC_ASSERT(SkRRect::kLowerRight_Corner == 2); |
| 335 | GR_STATIC_ASSERT(SkRRect::kLowerLeft_Corner == 3); |
| 336 | switch (circleCornerBitfield) { |
| 337 | case 3: |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 338 | rrtype = RRectEffect::kTopCircleTab_RRectType; |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 339 | break; |
| 340 | case 6: |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 341 | rrtype = RRectEffect::kRightCircleTab_RRectType; |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 342 | break; |
| 343 | case 9: |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 344 | rrtype = RRectEffect::kLeftCircleTab_RRectType; |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 345 | break; |
| 346 | case 12: |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 347 | rrtype = RRectEffect::kBottomCircleTab_RRectType; |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 348 | break; |
| 349 | default: |
| 350 | return NULL; |
| 351 | } |
| 352 | } else { |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 353 | return NULL; |
| 354 | } |
| commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame^] | 355 | return RRectEffect::Create(edgeType, rrtype, rrect); |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 356 | } |