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