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" |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 12 | #include "GrConvexPolyEffect.h" |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 13 | #include "GrOvalEffect.h" |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 14 | #include "GrTBackendEffectFactory.h" |
| 15 | |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 16 | #include "SkRRect.h" |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 17 | |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 18 | // The effects defined here only handle rrect radii >= kRadiusMin. |
| 19 | static const SkScalar kRadiusMin = SK_ScalarHalf; |
| 20 | |
| 21 | ////////////////////////////////////////////////////////////////////////////// |
| 22 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 23 | class GLCircularRRectEffect; |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 24 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 25 | class CircularRRectEffect : public GrEffect { |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 26 | public: |
skia.committer@gmail.com | f1f66c0 | 2014-03-05 03:02:06 +0000 | [diff] [blame] | 27 | |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 28 | enum CornerFlags { |
| 29 | kTopLeft_CornerFlag = (1 << SkRRect::kUpperLeft_Corner), |
| 30 | kTopRight_CornerFlag = (1 << SkRRect::kUpperRight_Corner), |
| 31 | kBottomRight_CornerFlag = (1 << SkRRect::kLowerRight_Corner), |
| 32 | kBottomLeft_CornerFlag = (1 << SkRRect::kLowerLeft_Corner), |
| 33 | |
| 34 | kLeft_CornerFlags = kTopLeft_CornerFlag | kBottomLeft_CornerFlag, |
| 35 | kTop_CornerFlags = kTopLeft_CornerFlag | kTopRight_CornerFlag, |
| 36 | kRight_CornerFlags = kTopRight_CornerFlag | kBottomRight_CornerFlag, |
| 37 | kBottom_CornerFlags = kBottomLeft_CornerFlag | kBottomRight_CornerFlag, |
| 38 | |
| 39 | kAll_CornerFlags = kTopLeft_CornerFlag | kTopRight_CornerFlag | |
| 40 | kBottomLeft_CornerFlag | kBottomRight_CornerFlag, |
| 41 | |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 42 | kNone_CornerFlags = 0 |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 45 | // The flags are used to indicate which corners are circluar (unflagged corners are assumed to |
| 46 | // be square). |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame^] | 47 | static GrEffect* Create(GrEffectEdgeType, uint32_t circularCornerFlags, const SkRRect&); |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 48 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 49 | virtual ~CircularRRectEffect() {}; |
| 50 | static const char* Name() { return "CircularRRect"; } |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 51 | |
| 52 | const SkRRect& getRRect() const { return fRRect; } |
| 53 | |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 54 | uint32_t getCircularCornerFlags() const { return fCircularCornerFlags; } |
skia.committer@gmail.com | 06acb58 | 2014-03-06 03:02:32 +0000 | [diff] [blame] | 55 | |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 56 | GrEffectEdgeType getEdgeType() const { return fEdgeType; } |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 57 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 58 | typedef GLCircularRRectEffect GLEffect; |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 59 | |
| 60 | virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; |
| 61 | |
| 62 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
| 63 | |
| 64 | private: |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 65 | CircularRRectEffect(GrEffectEdgeType, uint32_t circularCornerFlags, const SkRRect&); |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 66 | |
| 67 | virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE; |
| 68 | |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 69 | SkRRect fRRect; |
| 70 | GrEffectEdgeType fEdgeType; |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 71 | uint32_t fCircularCornerFlags; |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 72 | |
| 73 | GR_DECLARE_EFFECT_TEST; |
| 74 | |
| 75 | typedef GrEffect INHERITED; |
| 76 | }; |
| 77 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame^] | 78 | GrEffect* CircularRRectEffect::Create(GrEffectEdgeType edgeType, |
| 79 | uint32_t circularCornerFlags, |
| 80 | const SkRRect& rrect) { |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 81 | if (kFillAA_GrEffectEdgeType != edgeType && kInverseFillAA_GrEffectEdgeType != edgeType) { |
| 82 | return NULL; |
| 83 | } |
bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 84 | return SkNEW_ARGS(CircularRRectEffect, (edgeType, circularCornerFlags, rrect)); |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 85 | } |
| 86 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 87 | void CircularRRectEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 88 | *validFlags = 0; |
| 89 | } |
| 90 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 91 | const GrBackendEffectFactory& CircularRRectEffect::getFactory() const { |
| 92 | return GrTBackendEffectFactory<CircularRRectEffect>::getInstance(); |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 93 | } |
| 94 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 95 | CircularRRectEffect::CircularRRectEffect(GrEffectEdgeType edgeType, uint32_t circularCornerFlags, |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 96 | const SkRRect& rrect) |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 97 | : fRRect(rrect) |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 98 | , fEdgeType(edgeType) |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 99 | , fCircularCornerFlags(circularCornerFlags) { |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 100 | this->setWillReadFragmentPosition(); |
| 101 | } |
| 102 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 103 | bool CircularRRectEffect::onIsEqual(const GrEffect& other) const { |
| 104 | const CircularRRectEffect& crre = CastEffect<CircularRRectEffect>(other); |
| 105 | // The corner flags are derived from fRRect, so no need to check them. |
| 106 | return fEdgeType == crre.fEdgeType && fRRect == crre.fRRect; |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 107 | } |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 108 | |
| 109 | ////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 110 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 111 | GR_DEFINE_EFFECT_TEST(CircularRRectEffect); |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 112 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame^] | 113 | GrEffect* CircularRRectEffect::TestCreate(SkRandom* random, |
| 114 | GrContext*, |
| 115 | const GrDrawTargetCaps& caps, |
| 116 | GrTexture*[]) { |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 117 | SkScalar w = random->nextRangeScalar(20.f, 1000.f); |
| 118 | SkScalar h = random->nextRangeScalar(20.f, 1000.f); |
| 119 | SkScalar r = random->nextRangeF(kRadiusMin, 9.f); |
| 120 | SkRRect rrect; |
| 121 | rrect.setRectXY(SkRect::MakeWH(w, h), r, r); |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame^] | 122 | GrEffect* effect; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 123 | do { |
| 124 | GrEffectEdgeType et = (GrEffectEdgeType)random->nextULessThan(kGrEffectEdgeTypeCnt); |
| 125 | effect = GrRRectEffect::Create(et, rrect); |
| 126 | } while (NULL == effect); |
| 127 | return effect; |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | ////////////////////////////////////////////////////////////////////////////// |
| 131 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 132 | class GLCircularRRectEffect : public GrGLEffect { |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 133 | public: |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 134 | GLCircularRRectEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 135 | |
| 136 | virtual void emitCode(GrGLShaderBuilder* builder, |
| 137 | const GrDrawEffect& drawEffect, |
| 138 | EffectKey key, |
| 139 | const char* outputColor, |
| 140 | const char* inputColor, |
| 141 | const TransformedCoordsArray&, |
| 142 | const TextureSamplerArray&) SK_OVERRIDE; |
| 143 | |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 144 | static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 145 | |
| 146 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; |
| 147 | |
| 148 | private: |
| 149 | GrGLUniformManager::UniformHandle fInnerRectUniform; |
| 150 | GrGLUniformManager::UniformHandle fRadiusPlusHalfUniform; |
| 151 | SkRRect fPrevRRect; |
| 152 | typedef GrGLEffect INHERITED; |
| 153 | }; |
| 154 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 155 | GLCircularRRectEffect::GLCircularRRectEffect(const GrBackendEffectFactory& factory, |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 156 | const GrDrawEffect& drawEffect) |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 157 | : INHERITED (factory) { |
| 158 | fPrevRRect.setEmpty(); |
| 159 | } |
| 160 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 161 | void GLCircularRRectEffect::emitCode(GrGLShaderBuilder* builder, |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 162 | const GrDrawEffect& drawEffect, |
| 163 | EffectKey key, |
| 164 | const char* outputColor, |
| 165 | const char* inputColor, |
| 166 | const TransformedCoordsArray&, |
| 167 | const TextureSamplerArray& samplers) { |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 168 | const CircularRRectEffect& crre = drawEffect.castEffect<CircularRRectEffect>(); |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 169 | const char *rectName; |
| 170 | const char *radiusPlusHalfName; |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 171 | // The inner rect is the rrect bounds inset by the radius. Its left, top, right, and bottom |
| 172 | // edges correspond to components x, y, z, and w, respectively. When a side of the rrect has |
| 173 | // only rectangular corners, that side's value corresponds to the rect edge's value outset by |
| 174 | // half a pixel. |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 175 | fInnerRectUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 176 | kVec4f_GrSLType, |
| 177 | "innerRect", |
| 178 | &rectName); |
| 179 | fRadiusPlusHalfUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 180 | kFloat_GrSLType, |
| 181 | "radiusPlusHalf", |
| 182 | &radiusPlusHalfName); |
| 183 | const char* fragmentPos = builder->fragmentPosition(); |
| 184 | // At each quarter-circle corner we compute a vector that is the offset of the fragment position |
| 185 | // from the circle center. The vector is pinned in x and y to be in the quarter-plane relevant |
| 186 | // to that corner. This means that points near the interior near the rrect top edge will have |
| 187 | // a vector that points straight up for both the TL left and TR corners. Computing an |
| 188 | // alpha from this vector at either the TR or TL corner will give the correct result. Similarly, |
| 189 | // fragments near the other three edges will get the correct AA. Fragments in the interior of |
| 190 | // the rrect will have a (0,0) vector at all four corners. So long as the radius > 0.5 they will |
| 191 | // correctly produce an alpha value of 1 at all four corners. We take the min of all the alphas. |
| 192 | // The code below is a simplified version of the above that performs maxs on the vector |
| 193 | // components before computing distances and alpha values so that only one distance computation |
| 194 | // need be computed to determine the min alpha. |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 195 | // |
| 196 | // For the cases where one half of the rrect is rectangular we drop one of the x or y |
| 197 | // computations, compute a separate rect edge alpha for the rect side, and mul the two computed |
| 198 | // alphas together. |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 199 | switch (crre.getCircularCornerFlags()) { |
| 200 | case CircularRRectEffect::kAll_CornerFlags: |
bsalomon | 2290000 | 2014-06-24 11:16:52 -0700 | [diff] [blame] | 201 | builder->fsCodeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos); |
| 202 | builder->fsCodeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 203 | builder->fsCodeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n"); |
| 204 | builder->fsCodeAppendf("\t\tfloat alpha = clamp(%s - length(dxy), 0.0, 1.0);\n", |
| 205 | radiusPlusHalfName); |
| 206 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 207 | case CircularRRectEffect::kTopLeft_CornerFlag: |
| 208 | builder->fsCodeAppendf("\t\tvec2 dxy = max(%s.xy - %s.xy, 0.0);\n", |
| 209 | rectName, fragmentPos); |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 210 | builder->fsCodeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n", |
| 211 | rectName, fragmentPos); |
| 212 | builder->fsCodeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n", |
| 213 | rectName, fragmentPos); |
| 214 | builder->fsCodeAppendf("\t\tfloat alpha = bottomAlpha * rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| 215 | radiusPlusHalfName); |
| 216 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 217 | case CircularRRectEffect::kTopRight_CornerFlag: |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 218 | builder->fsCodeAppendf("\t\tvec2 dxy = max(vec2(%s.x - %s.z, %s.y - %s.y), 0.0);\n", |
| 219 | fragmentPos, rectName, rectName, fragmentPos); |
| 220 | builder->fsCodeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n", |
| 221 | fragmentPos, rectName); |
| 222 | builder->fsCodeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n", |
| 223 | rectName, fragmentPos); |
| 224 | builder->fsCodeAppendf("\t\tfloat alpha = bottomAlpha * leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| 225 | radiusPlusHalfName); |
| 226 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 227 | case CircularRRectEffect::kBottomRight_CornerFlag: |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 228 | builder->fsCodeAppendf("\t\tvec2 dxy = max(%s.xy - %s.zw, 0.0);\n", |
| 229 | fragmentPos, rectName); |
| 230 | builder->fsCodeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n", |
| 231 | fragmentPos, rectName); |
| 232 | builder->fsCodeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n", |
| 233 | fragmentPos, rectName); |
| 234 | builder->fsCodeAppendf("\t\tfloat alpha = topAlpha * leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| 235 | radiusPlusHalfName); |
| 236 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 237 | case CircularRRectEffect::kBottomLeft_CornerFlag: |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 238 | builder->fsCodeAppendf("\t\tvec2 dxy = max(vec2(%s.x - %s.x, %s.y - %s.w), 0.0);\n", |
| 239 | rectName, fragmentPos, fragmentPos, rectName); |
| 240 | builder->fsCodeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n", |
| 241 | rectName, fragmentPos); |
| 242 | builder->fsCodeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n", |
| 243 | fragmentPos, rectName); |
| 244 | builder->fsCodeAppendf("\t\tfloat alpha = topAlpha * rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| 245 | radiusPlusHalfName); |
| 246 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 247 | case CircularRRectEffect::kLeft_CornerFlags: |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 248 | builder->fsCodeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos); |
| 249 | builder->fsCodeAppendf("\t\tfloat dy1 = %s.y - %s.w;\n", fragmentPos, rectName); |
| 250 | builder->fsCodeAppend("\t\tvec2 dxy = max(vec2(dxy0.x, max(dxy0.y, dy1)), 0.0);\n"); |
| 251 | builder->fsCodeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n", |
| 252 | rectName, fragmentPos); |
| 253 | builder->fsCodeAppendf("\t\tfloat alpha = rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| 254 | radiusPlusHalfName); |
| 255 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 256 | case CircularRRectEffect::kTop_CornerFlags: |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 257 | builder->fsCodeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos); |
| 258 | builder->fsCodeAppendf("\t\tfloat dx1 = %s.x - %s.z;\n", fragmentPos, rectName); |
| 259 | builder->fsCodeAppend("\t\tvec2 dxy = max(vec2(max(dxy0.x, dx1), dxy0.y), 0.0);\n"); |
| 260 | builder->fsCodeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n", |
| 261 | rectName, fragmentPos); |
| 262 | builder->fsCodeAppendf("\t\tfloat alpha = bottomAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| 263 | radiusPlusHalfName); |
| 264 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 265 | case CircularRRectEffect::kRight_CornerFlags: |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 266 | builder->fsCodeAppendf("\t\tfloat dy0 = %s.y - %s.y;\n", rectName, fragmentPos); |
| 267 | builder->fsCodeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName); |
| 268 | builder->fsCodeAppend("\t\tvec2 dxy = max(vec2(dxy1.x, max(dy0, dxy1.y)), 0.0);\n"); |
| 269 | builder->fsCodeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n", |
| 270 | fragmentPos, rectName); |
| 271 | builder->fsCodeAppendf("\t\tfloat alpha = leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| 272 | radiusPlusHalfName); |
| 273 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 274 | case CircularRRectEffect::kBottom_CornerFlags: |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 275 | builder->fsCodeAppendf("\t\tfloat dx0 = %s.x - %s.x;\n", rectName, fragmentPos); |
| 276 | builder->fsCodeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName); |
| 277 | builder->fsCodeAppend("\t\tvec2 dxy = max(vec2(max(dx0, dxy1.x), dxy1.y), 0.0);\n"); |
| 278 | builder->fsCodeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n", |
| 279 | fragmentPos, rectName); |
| 280 | builder->fsCodeAppendf("\t\tfloat alpha = topAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| 281 | radiusPlusHalfName); |
| 282 | break; |
| 283 | } |
skia.committer@gmail.com | 06acb58 | 2014-03-06 03:02:32 +0000 | [diff] [blame] | 284 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 285 | if (kInverseFillAA_GrEffectEdgeType == crre.getEdgeType()) { |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 286 | builder->fsCodeAppend("\t\talpha = 1.0 - alpha;\n"); |
| 287 | } |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 288 | |
| 289 | builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor, |
| 290 | (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str()); |
| 291 | } |
| 292 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 293 | GrGLEffect::EffectKey GLCircularRRectEffect::GenKey(const GrDrawEffect& drawEffect, |
| 294 | const GrGLCaps&) { |
| 295 | const CircularRRectEffect& crre = drawEffect.castEffect<CircularRRectEffect>(); |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 296 | GR_STATIC_ASSERT(kGrEffectEdgeTypeCnt <= 8); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 297 | return (crre.getCircularCornerFlags() << 3) | crre.getEdgeType(); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 298 | } |
| 299 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 300 | void GLCircularRRectEffect::setData(const GrGLUniformManager& uman, |
| 301 | const GrDrawEffect& drawEffect) { |
| 302 | const CircularRRectEffect& crre = drawEffect.castEffect<CircularRRectEffect>(); |
| 303 | const SkRRect& rrect = crre.getRRect(); |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 304 | if (rrect != fPrevRRect) { |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 305 | SkRect rect = rrect.getBounds(); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 306 | SkScalar radius = 0; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 307 | switch (crre.getCircularCornerFlags()) { |
| 308 | case CircularRRectEffect::kAll_CornerFlags: |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 309 | SkASSERT(rrect.isSimpleCircular()); |
| 310 | radius = rrect.getSimpleRadii().fX; |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 311 | SkASSERT(radius >= kRadiusMin); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 312 | rect.inset(radius, radius); |
| 313 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 314 | case CircularRRectEffect::kTopLeft_CornerFlag: |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 315 | radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX; |
| 316 | rect.fLeft += radius; |
| 317 | rect.fTop += radius; |
| 318 | rect.fRight += 0.5f; |
| 319 | rect.fBottom += 0.5f; |
| 320 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 321 | case CircularRRectEffect::kTopRight_CornerFlag: |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 322 | radius = rrect.radii(SkRRect::kUpperRight_Corner).fX; |
bsalomon@google.com | de9f251 | 2014-03-11 17:09:17 +0000 | [diff] [blame] | 323 | rect.fLeft -= 0.5f; |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 324 | rect.fTop += radius; |
| 325 | rect.fRight -= radius; |
| 326 | rect.fBottom += 0.5f; |
| 327 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 328 | case CircularRRectEffect::kBottomRight_CornerFlag: |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 329 | radius = rrect.radii(SkRRect::kLowerRight_Corner).fX; |
bsalomon@google.com | de9f251 | 2014-03-11 17:09:17 +0000 | [diff] [blame] | 330 | rect.fLeft -= 0.5f; |
| 331 | rect.fTop -= 0.5f; |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 332 | rect.fRight -= radius; |
| 333 | rect.fBottom -= radius; |
| 334 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 335 | case CircularRRectEffect::kBottomLeft_CornerFlag: |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 336 | radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX; |
| 337 | rect.fLeft += radius; |
bsalomon@google.com | de9f251 | 2014-03-11 17:09:17 +0000 | [diff] [blame] | 338 | rect.fTop -= 0.5f; |
| 339 | rect.fRight += 0.5f; |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 340 | rect.fBottom -= radius; |
| 341 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 342 | case CircularRRectEffect::kLeft_CornerFlags: |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 343 | radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX; |
| 344 | rect.fLeft += radius; |
| 345 | rect.fTop += radius; |
| 346 | rect.fRight += 0.5f; |
| 347 | rect.fBottom -= radius; |
| 348 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 349 | case CircularRRectEffect::kTop_CornerFlags: |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 350 | radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX; |
| 351 | rect.fLeft += radius; |
| 352 | rect.fTop += radius; |
| 353 | rect.fRight -= radius; |
| 354 | rect.fBottom += 0.5f; |
| 355 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 356 | case CircularRRectEffect::kRight_CornerFlags: |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 357 | radius = rrect.radii(SkRRect::kUpperRight_Corner).fX; |
| 358 | rect.fLeft -= 0.5f; |
| 359 | rect.fTop += radius; |
| 360 | rect.fRight -= radius; |
| 361 | rect.fBottom -= radius; |
| 362 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 363 | case CircularRRectEffect::kBottom_CornerFlags: |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 364 | radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX; |
| 365 | rect.fLeft += radius; |
| 366 | rect.fTop -= 0.5f; |
| 367 | rect.fRight -= radius; |
| 368 | rect.fBottom -= radius; |
| 369 | break; |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 370 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 371 | SkFAIL("Should have been one of the above cases."); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 372 | } |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 373 | uman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom); |
| 374 | uman.set1f(fRadiusPlusHalfUniform, radius + 0.5f); |
| 375 | fPrevRRect = rrect; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | ////////////////////////////////////////////////////////////////////////////// |
| 380 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 381 | class GLEllipticalRRectEffect; |
| 382 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 383 | class EllipticalRRectEffect : public GrEffect { |
| 384 | public: |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame^] | 385 | static GrEffect* Create(GrEffectEdgeType, const SkRRect&); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 386 | |
| 387 | virtual ~EllipticalRRectEffect() {}; |
| 388 | static const char* Name() { return "EllipticalRRect"; } |
| 389 | |
| 390 | const SkRRect& getRRect() const { return fRRect; } |
| 391 | |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 392 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 393 | GrEffectEdgeType getEdgeType() const { return fEdgeType; } |
| 394 | |
| 395 | typedef GLEllipticalRRectEffect GLEffect; |
| 396 | |
| 397 | virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; |
| 398 | |
| 399 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
| 400 | |
| 401 | private: |
commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 402 | EllipticalRRectEffect(GrEffectEdgeType, const SkRRect&); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 403 | |
| 404 | virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE; |
| 405 | |
| 406 | SkRRect fRRect; |
| 407 | GrEffectEdgeType fEdgeType; |
| 408 | |
| 409 | GR_DECLARE_EFFECT_TEST; |
| 410 | |
| 411 | typedef GrEffect INHERITED; |
| 412 | }; |
| 413 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame^] | 414 | GrEffect* EllipticalRRectEffect::Create(GrEffectEdgeType edgeType, const SkRRect& rrect) { |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 415 | if (kFillAA_GrEffectEdgeType != edgeType && kInverseFillAA_GrEffectEdgeType != edgeType) { |
| 416 | return NULL; |
| 417 | } |
bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 418 | return SkNEW_ARGS(EllipticalRRectEffect, (edgeType, rrect)); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | void EllipticalRRectEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
| 422 | *validFlags = 0; |
| 423 | } |
| 424 | |
| 425 | const GrBackendEffectFactory& EllipticalRRectEffect::getFactory() const { |
| 426 | return GrTBackendEffectFactory<EllipticalRRectEffect>::getInstance(); |
| 427 | } |
| 428 | |
commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 429 | EllipticalRRectEffect::EllipticalRRectEffect(GrEffectEdgeType edgeType, const SkRRect& rrect) |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 430 | : fRRect(rrect) |
| 431 | , fEdgeType(edgeType){ |
| 432 | this->setWillReadFragmentPosition(); |
| 433 | } |
| 434 | |
| 435 | bool EllipticalRRectEffect::onIsEqual(const GrEffect& other) const { |
| 436 | const EllipticalRRectEffect& erre = CastEffect<EllipticalRRectEffect>(other); |
| 437 | return fEdgeType == erre.fEdgeType && fRRect == erre.fRRect; |
| 438 | } |
| 439 | |
| 440 | ////////////////////////////////////////////////////////////////////////////// |
| 441 | |
| 442 | GR_DEFINE_EFFECT_TEST(EllipticalRRectEffect); |
| 443 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame^] | 444 | GrEffect* EllipticalRRectEffect::TestCreate(SkRandom* random, |
| 445 | GrContext*, |
| 446 | const GrDrawTargetCaps& caps, |
| 447 | GrTexture*[]) { |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 448 | SkScalar w = random->nextRangeScalar(20.f, 1000.f); |
| 449 | SkScalar h = random->nextRangeScalar(20.f, 1000.f); |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 450 | SkVector r[4]; |
| 451 | r[SkRRect::kUpperLeft_Corner].fX = random->nextRangeF(kRadiusMin, 9.f); |
| 452 | // ensure at least one corner really is elliptical |
| 453 | do { |
| 454 | r[SkRRect::kUpperLeft_Corner].fY = random->nextRangeF(kRadiusMin, 9.f); |
| 455 | } while (r[SkRRect::kUpperLeft_Corner].fY == r[SkRRect::kUpperLeft_Corner].fX); |
| 456 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 457 | SkRRect rrect; |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 458 | if (random->nextBool()) { |
| 459 | // half the time create a four-radii rrect. |
| 460 | r[SkRRect::kLowerRight_Corner].fX = random->nextRangeF(kRadiusMin, 9.f); |
| 461 | r[SkRRect::kLowerRight_Corner].fY = random->nextRangeF(kRadiusMin, 9.f); |
| 462 | |
| 463 | r[SkRRect::kUpperRight_Corner].fX = r[SkRRect::kLowerRight_Corner].fX; |
| 464 | r[SkRRect::kUpperRight_Corner].fY = r[SkRRect::kUpperLeft_Corner].fY; |
| 465 | |
| 466 | r[SkRRect::kLowerLeft_Corner].fX = r[SkRRect::kUpperLeft_Corner].fX; |
| 467 | r[SkRRect::kLowerLeft_Corner].fY = r[SkRRect::kLowerRight_Corner].fY; |
| 468 | |
| 469 | rrect.setRectRadii(SkRect::MakeWH(w, h), r); |
| 470 | } else { |
| 471 | rrect.setRectXY(SkRect::MakeWH(w, h), r[SkRRect::kUpperLeft_Corner].fX, |
| 472 | r[SkRRect::kUpperLeft_Corner].fY); |
| 473 | } |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame^] | 474 | GrEffect* effect; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 475 | do { |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 476 | GrEffectEdgeType et = (GrEffectEdgeType)random->nextULessThan(kGrEffectEdgeTypeCnt); |
| 477 | effect = GrRRectEffect::Create(et, rrect); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 478 | } while (NULL == effect); |
| 479 | return effect; |
| 480 | } |
| 481 | |
| 482 | ////////////////////////////////////////////////////////////////////////////// |
| 483 | |
| 484 | class GLEllipticalRRectEffect : public GrGLEffect { |
| 485 | public: |
| 486 | GLEllipticalRRectEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| 487 | |
| 488 | virtual void emitCode(GrGLShaderBuilder* builder, |
| 489 | const GrDrawEffect& drawEffect, |
| 490 | EffectKey key, |
| 491 | const char* outputColor, |
| 492 | const char* inputColor, |
| 493 | const TransformedCoordsArray&, |
| 494 | const TextureSamplerArray&) SK_OVERRIDE; |
| 495 | |
| 496 | static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
| 497 | |
| 498 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; |
| 499 | |
| 500 | private: |
| 501 | GrGLUniformManager::UniformHandle fInnerRectUniform; |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 502 | GrGLUniformManager::UniformHandle fInvRadiiSqdUniform; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 503 | SkRRect fPrevRRect; |
| 504 | typedef GrGLEffect INHERITED; |
| 505 | }; |
| 506 | |
| 507 | GLEllipticalRRectEffect::GLEllipticalRRectEffect(const GrBackendEffectFactory& factory, |
| 508 | const GrDrawEffect& drawEffect) |
| 509 | : INHERITED (factory) { |
| 510 | fPrevRRect.setEmpty(); |
| 511 | } |
| 512 | |
| 513 | void GLEllipticalRRectEffect::emitCode(GrGLShaderBuilder* builder, |
| 514 | const GrDrawEffect& drawEffect, |
| 515 | EffectKey key, |
| 516 | const char* outputColor, |
| 517 | const char* inputColor, |
| 518 | const TransformedCoordsArray&, |
| 519 | const TextureSamplerArray& samplers) { |
| 520 | const EllipticalRRectEffect& erre = drawEffect.castEffect<EllipticalRRectEffect>(); |
| 521 | const char *rectName; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 522 | // The inner rect is the rrect bounds inset by the x/y radii |
| 523 | fInnerRectUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 524 | kVec4f_GrSLType, |
| 525 | "innerRect", |
| 526 | &rectName); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 527 | const char* fragmentPos = builder->fragmentPosition(); |
| 528 | // At each quarter-ellipse corner we compute a vector that is the offset of the fragment pos |
| 529 | // to the ellipse center. The vector is pinned in x and y to be in the quarter-plane relevant |
| 530 | // to that corner. This means that points near the interior near the rrect top edge will have |
| 531 | // a vector that points straight up for both the TL left and TR corners. Computing an |
| 532 | // alpha from this vector at either the TR or TL corner will give the correct result. Similarly, |
| 533 | // fragments near the other three edges will get the correct AA. Fragments in the interior of |
| 534 | // the rrect will have a (0,0) vector at all four corners. So long as the radii > 0.5 they will |
| 535 | // correctly produce an alpha value of 1 at all four corners. We take the min of all the alphas. |
| 536 | // The code below is a simplified version of the above that performs maxs on the vector |
| 537 | // components before computing distances and alpha values so that only one distance computation |
| 538 | // need be computed to determine the min alpha. |
bsalomon | 2290000 | 2014-06-24 11:16:52 -0700 | [diff] [blame] | 539 | builder->fsCodeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos); |
| 540 | builder->fsCodeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName); |
commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 541 | switch (erre.getRRect().getType()) { |
| 542 | case SkRRect::kSimple_Type: { |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 543 | const char *invRadiiXYSqdName; |
| 544 | fInvRadiiSqdUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 545 | kVec2f_GrSLType, |
| 546 | "invRadiiXY", |
| 547 | &invRadiiXYSqdName); |
| 548 | builder->fsCodeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n"); |
| 549 | // Z is the x/y offsets divided by squared radii. |
| 550 | builder->fsCodeAppendf("\t\tvec2 Z = dxy * %s;\n", invRadiiXYSqdName); |
| 551 | break; |
| 552 | } |
commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 553 | case SkRRect::kNinePatch_Type: { |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 554 | const char *invRadiiLTRBSqdName; |
| 555 | fInvRadiiSqdUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 556 | kVec4f_GrSLType, |
| 557 | "invRadiiLTRB", |
| 558 | &invRadiiLTRBSqdName); |
| 559 | builder->fsCodeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n"); |
| 560 | // Z is the x/y offsets divided by squared radii. We only care about the (at most) one |
| 561 | // corner where both the x and y offsets are positive, hence the maxes. (The inverse |
| 562 | // squared radii will always be positive.) |
| 563 | builder->fsCodeAppendf("\t\tvec2 Z = max(max(dxy0 * %s.xy, dxy1 * %s.zw), 0.0);\n", |
| 564 | invRadiiLTRBSqdName, invRadiiLTRBSqdName); |
| 565 | break; |
commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 566 | } |
| 567 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 568 | SkFAIL("RRect should always be simple or nine-patch."); |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 569 | } |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 570 | // implicit is the evaluation of (x/a)^2 + (y/b)^2 - 1. |
| 571 | builder->fsCodeAppend("\t\tfloat implicit = dot(Z, dxy) - 1.0;\n"); |
| 572 | // grad_dot is the squared length of the gradient of the implicit. |
| 573 | builder->fsCodeAppendf("\t\tfloat grad_dot = 4.0 * dot(Z, Z);\n"); |
commit-bot@chromium.org | 1b035d8 | 2014-04-09 17:11:09 +0000 | [diff] [blame] | 574 | // avoid calling inversesqrt on zero. |
| 575 | builder->fsCodeAppend("\t\tgrad_dot = max(grad_dot, 1.0e-4);\n"); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 576 | builder->fsCodeAppendf("\t\tfloat approx_dist = implicit * inversesqrt(grad_dot);\n"); |
| 577 | |
| 578 | if (kFillAA_GrEffectEdgeType == erre.getEdgeType()) { |
| 579 | builder->fsCodeAppend("\t\tfloat alpha = clamp(0.5 - approx_dist, 0.0, 1.0);\n"); |
| 580 | } else { |
| 581 | builder->fsCodeAppend("\t\tfloat alpha = clamp(0.5 + approx_dist, 0.0, 1.0);\n"); |
| 582 | } |
| 583 | |
| 584 | builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor, |
| 585 | (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str()); |
| 586 | } |
| 587 | |
| 588 | GrGLEffect::EffectKey GLEllipticalRRectEffect::GenKey(const GrDrawEffect& drawEffect, |
| 589 | const GrGLCaps&) { |
| 590 | const EllipticalRRectEffect& erre = drawEffect.castEffect<EllipticalRRectEffect>(); |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 591 | GR_STATIC_ASSERT(kLast_GrEffectEdgeType < (1 << 3)); |
commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 592 | return erre.getRRect().getType() | erre.getEdgeType() << 3; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | void GLEllipticalRRectEffect::setData(const GrGLUniformManager& uman, |
| 596 | const GrDrawEffect& drawEffect) { |
| 597 | const EllipticalRRectEffect& erre = drawEffect.castEffect<EllipticalRRectEffect>(); |
| 598 | const SkRRect& rrect = erre.getRRect(); |
| 599 | if (rrect != fPrevRRect) { |
| 600 | SkRect rect = rrect.getBounds(); |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 601 | const SkVector& r0 = rrect.radii(SkRRect::kUpperLeft_Corner); |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 602 | SkASSERT(r0.fX >= kRadiusMin); |
| 603 | SkASSERT(r0.fY >= kRadiusMin); |
commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 604 | switch (erre.getRRect().getType()) { |
| 605 | case SkRRect::kSimple_Type: |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 606 | rect.inset(r0.fX, r0.fY); |
| 607 | uman.set2f(fInvRadiiSqdUniform, 1.f / (r0.fX * r0.fX), |
| 608 | 1.f / (r0.fY * r0.fY)); |
| 609 | break; |
commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 610 | case SkRRect::kNinePatch_Type: { |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 611 | const SkVector& r1 = rrect.radii(SkRRect::kLowerRight_Corner); |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 612 | SkASSERT(r1.fX >= kRadiusMin); |
| 613 | SkASSERT(r1.fY >= kRadiusMin); |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 614 | rect.fLeft += r0.fX; |
| 615 | rect.fTop += r0.fY; |
| 616 | rect.fRight -= r1.fX; |
| 617 | rect.fBottom -= r1.fY; |
| 618 | uman.set4f(fInvRadiiSqdUniform, 1.f / (r0.fX * r0.fX), |
| 619 | 1.f / (r0.fY * r0.fY), |
| 620 | 1.f / (r1.fX * r1.fX), |
| 621 | 1.f / (r1.fY * r1.fY)); |
| 622 | break; |
| 623 | } |
commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 624 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 625 | SkFAIL("RRect should always be simple or nine-patch."); |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 626 | } |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 627 | uman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 628 | fPrevRRect = rrect; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | ////////////////////////////////////////////////////////////////////////////// |
| 633 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame^] | 634 | GrEffect* GrRRectEffect::Create(GrEffectEdgeType edgeType, const SkRRect& rrect) { |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 635 | if (rrect.isRect()) { |
| 636 | return GrConvexPolyEffect::Create(edgeType, rrect.getBounds()); |
| 637 | } |
| 638 | |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 639 | if (rrect.isOval()) { |
| 640 | return GrOvalEffect::Create(edgeType, rrect.getBounds()); |
| 641 | } |
| 642 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 643 | if (rrect.isSimple()) { |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 644 | if (rrect.getSimpleRadii().fX < kRadiusMin || rrect.getSimpleRadii().fY < kRadiusMin) { |
| 645 | // In this case the corners are extremely close to rectangular and we collapse the |
| 646 | // clip to a rectangular clip. |
| 647 | return GrConvexPolyEffect::Create(edgeType, rrect.getBounds()); |
| 648 | } |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 649 | if (rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY) { |
skia.committer@gmail.com | 6e4eb21 | 2014-03-25 03:02:32 +0000 | [diff] [blame] | 650 | return CircularRRectEffect::Create(edgeType, CircularRRectEffect::kAll_CornerFlags, |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 651 | rrect); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 652 | } else { |
commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 653 | return EllipticalRRectEffect::Create(edgeType, rrect); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 654 | } |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | if (rrect.isComplex() || rrect.isNinePatch()) { |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 658 | // Check for the "tab" cases - two adjacent circular corners and two square corners. |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 659 | SkScalar circularRadius = 0; |
| 660 | uint32_t cornerFlags = 0; |
| 661 | |
| 662 | SkVector radii[4]; |
| 663 | bool squashedRadii = false; |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 664 | for (int c = 0; c < 4; ++c) { |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 665 | radii[c] = rrect.radii((SkRRect::Corner)c); |
| 666 | SkASSERT((0 == radii[c].fX) == (0 == radii[c].fY)); |
| 667 | if (0 == radii[c].fX) { |
| 668 | // The corner is square, so no need to squash or flag as circular. |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 669 | continue; |
| 670 | } |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 671 | if (radii[c].fX < kRadiusMin || radii[c].fY < kRadiusMin) { |
| 672 | radii[c].set(0, 0); |
| 673 | squashedRadii = true; |
| 674 | continue; |
| 675 | } |
| 676 | if (radii[c].fX != radii[c].fY) { |
bsalomon@google.com | 44a435b | 2014-03-13 19:20:32 +0000 | [diff] [blame] | 677 | cornerFlags = ~0U; |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 678 | break; |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 679 | } |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 680 | if (!cornerFlags) { |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 681 | circularRadius = radii[c].fX; |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 682 | cornerFlags = 1 << c; |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 683 | } else { |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 684 | if (radii[c].fX != circularRadius) { |
bsalomon@google.com | 44a435b | 2014-03-13 19:20:32 +0000 | [diff] [blame] | 685 | cornerFlags = ~0U; |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 686 | break; |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 687 | } |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 688 | cornerFlags |= 1 << c; |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 692 | switch (cornerFlags) { |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 693 | case CircularRRectEffect::kAll_CornerFlags: |
| 694 | // This rrect should have been caught in the simple case above. Though, it would |
| 695 | // be correctly handled in the fallthrough code. |
| 696 | SkASSERT(false); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 697 | case CircularRRectEffect::kTopLeft_CornerFlag: |
| 698 | case CircularRRectEffect::kTopRight_CornerFlag: |
| 699 | case CircularRRectEffect::kBottomRight_CornerFlag: |
| 700 | case CircularRRectEffect::kBottomLeft_CornerFlag: |
| 701 | case CircularRRectEffect::kLeft_CornerFlags: |
| 702 | case CircularRRectEffect::kTop_CornerFlags: |
| 703 | case CircularRRectEffect::kRight_CornerFlags: |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 704 | case CircularRRectEffect::kBottom_CornerFlags: { |
| 705 | SkTCopyOnFirstWrite<SkRRect> rr(rrect); |
| 706 | if (squashedRadii) { |
| 707 | rr.writable()->setRectRadii(rrect.getBounds(), radii); |
| 708 | } |
| 709 | return CircularRRectEffect::Create(edgeType, cornerFlags, *rr); |
| 710 | } |
| 711 | case CircularRRectEffect::kNone_CornerFlags: |
| 712 | return GrConvexPolyEffect::Create(edgeType, rrect.getBounds()); |
| 713 | default: { |
| 714 | if (squashedRadii) { |
| 715 | // If we got here then we squashed some but not all the radii to zero. (If all |
| 716 | // had been squashed cornerFlags would be 0.) The elliptical effect doesn't |
| 717 | // support some rounded and some square corners. |
| 718 | return NULL; |
| 719 | } |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 720 | if (rrect.isNinePatch()) { |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 721 | return EllipticalRRectEffect::Create(edgeType, rrect); |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 722 | } |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 723 | return NULL; |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 724 | } |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 725 | } |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 726 | } |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 727 | |
| 728 | return NULL; |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 729 | } |