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