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