| 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; |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 168 | // The inner rect is the rrect bounds inset by the radius. Its left, top, right, and bottom |
| 169 | // edges correspond to components x, y, z, and w, respectively. When a side of the rrect has |
| 170 | // only rectangular corners, that side's value corresponds to the rect edge's value outset by |
| 171 | // half a 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 | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 204 | case RRectEffect::kTopLeft_CornerFlag: |
| 205 | builder->fsCodeAppendf("\t\tvec2 dxy = max(%s.xy - %s.xy, 0.0);\n", rectName, fragmentPos); |
| 206 | builder->fsCodeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n", |
| 207 | rectName, fragmentPos); |
| 208 | builder->fsCodeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n", |
| 209 | rectName, fragmentPos); |
| 210 | builder->fsCodeAppendf("\t\tfloat alpha = bottomAlpha * rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| 211 | radiusPlusHalfName); |
| 212 | break; |
| 213 | case RRectEffect::kTopRight_CornerFlag: |
| 214 | builder->fsCodeAppendf("\t\tvec2 dxy = max(vec2(%s.x - %s.z, %s.y - %s.y), 0.0);\n", |
| 215 | fragmentPos, rectName, rectName, fragmentPos); |
| 216 | builder->fsCodeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n", |
| 217 | fragmentPos, rectName); |
| 218 | builder->fsCodeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n", |
| 219 | rectName, fragmentPos); |
| 220 | builder->fsCodeAppendf("\t\tfloat alpha = bottomAlpha * leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| 221 | radiusPlusHalfName); |
| 222 | break; |
| 223 | case RRectEffect::kBottomRight_CornerFlag: |
| 224 | builder->fsCodeAppendf("\t\tvec2 dxy = max(%s.xy - %s.zw, 0.0);\n", |
| 225 | fragmentPos, rectName); |
| 226 | builder->fsCodeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n", |
| 227 | fragmentPos, rectName); |
| 228 | builder->fsCodeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n", |
| 229 | fragmentPos, rectName); |
| 230 | builder->fsCodeAppendf("\t\tfloat alpha = topAlpha * leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| 231 | radiusPlusHalfName); |
| 232 | break; |
| 233 | case RRectEffect::kBottomLeft_CornerFlag: |
| 234 | builder->fsCodeAppendf("\t\tvec2 dxy = max(vec2(%s.x - %s.x, %s.y - %s.w), 0.0);\n", |
| 235 | rectName, fragmentPos, fragmentPos, rectName); |
| 236 | builder->fsCodeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n", |
| 237 | rectName, fragmentPos); |
| 238 | builder->fsCodeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n", |
| 239 | fragmentPos, rectName); |
| 240 | builder->fsCodeAppendf("\t\tfloat alpha = topAlpha * rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| 241 | radiusPlusHalfName); |
| 242 | break; |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 243 | case RRectEffect::kLeft_CornerFlags: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 244 | builder->fsCodeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos); |
| 245 | builder->fsCodeAppendf("\t\tfloat dy1 = %s.y - %s.w;\n", fragmentPos, rectName); |
| 246 | builder->fsCodeAppend("\t\tvec2 dxy = max(vec2(dxy0.x, max(dxy0.y, dy1)), 0.0);\n"); |
| 247 | builder->fsCodeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n", |
| 248 | rectName, fragmentPos); |
| 249 | builder->fsCodeAppendf("\t\tfloat alpha = rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| 250 | radiusPlusHalfName); |
| 251 | break; |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 252 | case RRectEffect::kTop_CornerFlags: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 253 | builder->fsCodeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos); |
| 254 | builder->fsCodeAppendf("\t\tfloat dx1 = %s.x - %s.z;\n", fragmentPos, rectName); |
| 255 | builder->fsCodeAppend("\t\tvec2 dxy = max(vec2(max(dxy0.x, dx1), dxy0.y), 0.0);\n"); |
| 256 | builder->fsCodeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n", |
| 257 | rectName, fragmentPos); |
| 258 | builder->fsCodeAppendf("\t\tfloat alpha = bottomAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| 259 | radiusPlusHalfName); |
| 260 | break; |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 261 | case RRectEffect::kRight_CornerFlags: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 262 | builder->fsCodeAppendf("\t\tfloat dy0 = %s.y - %s.y;\n", rectName, fragmentPos); |
| 263 | builder->fsCodeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName); |
| 264 | builder->fsCodeAppend("\t\tvec2 dxy = max(vec2(dxy1.x, max(dy0, dxy1.y)), 0.0);\n"); |
| 265 | builder->fsCodeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n", |
| 266 | fragmentPos, rectName); |
| 267 | builder->fsCodeAppendf("\t\tfloat alpha = leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| 268 | radiusPlusHalfName); |
| 269 | break; |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 270 | case RRectEffect::kBottom_CornerFlags: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 271 | builder->fsCodeAppendf("\t\tfloat dx0 = %s.x - %s.x;\n", rectName, fragmentPos); |
| 272 | builder->fsCodeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName); |
| 273 | builder->fsCodeAppend("\t\tvec2 dxy = max(vec2(max(dx0, dxy1.x), dxy1.y), 0.0);\n"); |
| 274 | builder->fsCodeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n", |
| 275 | fragmentPos, rectName); |
| 276 | builder->fsCodeAppendf("\t\tfloat alpha = topAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| 277 | radiusPlusHalfName); |
| 278 | break; |
| 279 | } |
| skia.committer@gmail.com | 06acb58 | 2014-03-06 03:02:32 +0000 | [diff] [blame] | 280 | |
| commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 281 | if (kInverseFillAA_GrEffectEdgeType == rre.getEdgeType()) { |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 282 | builder->fsCodeAppend("\t\talpha = 1.0 - alpha;\n"); |
| 283 | } |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 284 | |
| 285 | builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor, |
| 286 | (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str()); |
| 287 | } |
| 288 | |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 289 | GrGLEffect::EffectKey GLRRectEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { |
| 290 | const RRectEffect& rre = drawEffect.castEffect<RRectEffect>(); |
| commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 291 | GR_STATIC_ASSERT(kGrEffectEdgeTypeCnt <= 8); |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 292 | return (rre.getCircularCornerFlags() << 3) | rre.getEdgeType(); |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 295 | void GLRRectEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) { |
| 296 | const RRectEffect& rre = drawEffect.castEffect<RRectEffect>(); |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 297 | const SkRRect& rrect = rre.getRRect(); |
| 298 | if (rrect != fPrevRRect) { |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 299 | SkRect rect = rrect.getBounds(); |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 300 | SkScalar radius = 0; |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 301 | switch (rre.getCircularCornerFlags()) { |
| 302 | case RRectEffect::kAll_CornerFlags: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 303 | SkASSERT(rrect.isSimpleCircular()); |
| 304 | radius = rrect.getSimpleRadii().fX; |
| 305 | SkASSERT(radius >= RRectEffect::kRadiusMin); |
| 306 | rect.inset(radius, radius); |
| 307 | break; |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 308 | case RRectEffect::kTopLeft_CornerFlag: |
| 309 | radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX; |
| 310 | rect.fLeft += radius; |
| 311 | rect.fTop += radius; |
| 312 | rect.fRight += 0.5f; |
| 313 | rect.fBottom += 0.5f; |
| 314 | break; |
| 315 | case RRectEffect::kTopRight_CornerFlag: |
| 316 | radius = rrect.radii(SkRRect::kUpperRight_Corner).fX; |
| bsalomon@google.com | de9f251 | 2014-03-11 17:09:17 +0000 | [diff] [blame^] | 317 | rect.fLeft -= 0.5f; |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 318 | rect.fTop += radius; |
| 319 | rect.fRight -= radius; |
| 320 | rect.fBottom += 0.5f; |
| 321 | break; |
| 322 | case RRectEffect::kBottomRight_CornerFlag: |
| 323 | radius = rrect.radii(SkRRect::kLowerRight_Corner).fX; |
| bsalomon@google.com | de9f251 | 2014-03-11 17:09:17 +0000 | [diff] [blame^] | 324 | rect.fLeft -= 0.5f; |
| 325 | rect.fTop -= 0.5f; |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 326 | rect.fRight -= radius; |
| 327 | rect.fBottom -= radius; |
| 328 | break; |
| 329 | case RRectEffect::kBottomLeft_CornerFlag: |
| 330 | radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX; |
| 331 | rect.fLeft += radius; |
| bsalomon@google.com | de9f251 | 2014-03-11 17:09:17 +0000 | [diff] [blame^] | 332 | rect.fTop -= 0.5f; |
| 333 | rect.fRight += 0.5f; |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 334 | rect.fBottom -= radius; |
| 335 | break; |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 336 | case RRectEffect::kLeft_CornerFlags: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 337 | radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX; |
| 338 | rect.fLeft += radius; |
| 339 | rect.fTop += radius; |
| 340 | rect.fRight += 0.5f; |
| 341 | rect.fBottom -= radius; |
| 342 | break; |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 343 | case RRectEffect::kTop_CornerFlags: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 344 | radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX; |
| 345 | rect.fLeft += radius; |
| 346 | rect.fTop += radius; |
| 347 | rect.fRight -= radius; |
| 348 | rect.fBottom += 0.5f; |
| 349 | break; |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 350 | case RRectEffect::kRight_CornerFlags: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 351 | radius = rrect.radii(SkRRect::kUpperRight_Corner).fX; |
| 352 | rect.fLeft -= 0.5f; |
| 353 | rect.fTop += radius; |
| 354 | rect.fRight -= radius; |
| 355 | rect.fBottom -= radius; |
| 356 | break; |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 357 | case RRectEffect::kBottom_CornerFlags: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 358 | radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX; |
| 359 | rect.fLeft += radius; |
| 360 | rect.fTop -= 0.5f; |
| 361 | rect.fRight -= radius; |
| 362 | rect.fBottom -= radius; |
| 363 | break; |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 364 | default: |
| 365 | GrCrash("Should have been one of the above cases."); |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 366 | } |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 367 | uman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom); |
| 368 | uman.set1f(fRadiusPlusHalfUniform, radius + 0.5f); |
| 369 | fPrevRRect = rrect; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | ////////////////////////////////////////////////////////////////////////////// |
| 374 | |
| commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 375 | GrEffectRef* GrRRectEffect::Create(GrEffectEdgeType edgeType, const SkRRect& rrect) { |
| commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 376 | if (kFillAA_GrEffectEdgeType != edgeType && kInverseFillAA_GrEffectEdgeType != edgeType) { |
| commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 377 | return NULL; |
| 378 | } |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 379 | uint32_t cornerFlags; |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 380 | if (rrect.isSimpleCircular()) { |
| 381 | if (rrect.getSimpleRadii().fX < RRectEffect::kRadiusMin) { |
| 382 | return NULL; |
| 383 | } |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 384 | cornerFlags = RRectEffect::kAll_CornerFlags; |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 385 | } else if (rrect.isComplex()) { |
| 386 | // Check for the "tab" cases - two adjacent circular corners and two square corners. |
| 387 | SkScalar radius = 0; |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 388 | cornerFlags = 0; |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 389 | for (int c = 0; c < 4; ++c) { |
| 390 | const SkVector& r = rrect.radii((SkRRect::Corner)c); |
| 391 | SkASSERT((0 == r.fX) == (0 == r.fY)); |
| 392 | if (0 == r.fX) { |
| 393 | continue; |
| 394 | } |
| 395 | if (r.fX != r.fY) { |
| 396 | return NULL; |
| 397 | } |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 398 | if (!cornerFlags) { |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 399 | radius = r.fX; |
| 400 | if (radius < RRectEffect::kRadiusMin) { |
| 401 | return NULL; |
| 402 | } |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 403 | cornerFlags = 1 << c; |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 404 | } else { |
| 405 | if (r.fX != radius) { |
| 406 | return NULL; |
| 407 | } |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 408 | cornerFlags |= 1 << c; |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 412 | switch (cornerFlags) { |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 413 | case RRectEffect::kTopLeft_CornerFlag: |
| 414 | case RRectEffect::kTopRight_CornerFlag: |
| 415 | case RRectEffect::kBottomRight_CornerFlag: |
| 416 | case RRectEffect::kBottomLeft_CornerFlag: |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 417 | case RRectEffect::kLeft_CornerFlags: |
| 418 | case RRectEffect::kTop_CornerFlags: |
| 419 | case RRectEffect::kRight_CornerFlags: |
| 420 | case RRectEffect::kBottom_CornerFlags: |
| 421 | case RRectEffect::kAll_CornerFlags: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 422 | break; |
| 423 | default: |
| 424 | return NULL; |
| 425 | } |
| 426 | } else { |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 427 | return NULL; |
| 428 | } |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 429 | return RRectEffect::Create(edgeType, cornerFlags, rrect); |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 430 | } |