blob: 619cd99daf26d37160e5ce4d11b6213c5cf581eb [file] [log] [blame]
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +00001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrRRectEffect.h"
9
10#include "gl/GrGLEffect.h"
11#include "gl/GrGLSL.h"
12#include "GrTBackendEffectFactory.h"
13
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000014#include "SkRRect.h"
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +000015
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000016using namespace GrRRectEffect;
17
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000018class GLRRectEffect;
19
20class RRectEffect : public GrEffect {
21public:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +000022 // This effect only supports circular corner rrects where the radius is >= kRadiusMin.
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000023 static const SkScalar kRadiusMin;
skia.committer@gmail.comf1f66c02014-03-05 03:02:06 +000024
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000025 enum CornerFlags {
26 kTopLeft_CornerFlag = (1 << SkRRect::kUpperLeft_Corner),
27 kTopRight_CornerFlag = (1 << SkRRect::kUpperRight_Corner),
28 kBottomRight_CornerFlag = (1 << SkRRect::kLowerRight_Corner),
29 kBottomLeft_CornerFlag = (1 << SkRRect::kLowerLeft_Corner),
30
31 kLeft_CornerFlags = kTopLeft_CornerFlag | kBottomLeft_CornerFlag,
32 kTop_CornerFlags = kTopLeft_CornerFlag | kTopRight_CornerFlag,
33 kRight_CornerFlags = kTopRight_CornerFlag | kBottomRight_CornerFlag,
34 kBottom_CornerFlags = kBottomLeft_CornerFlag | kBottomRight_CornerFlag,
35
36 kAll_CornerFlags = kTopLeft_CornerFlag | kTopRight_CornerFlag |
37 kBottomLeft_CornerFlag | kBottomRight_CornerFlag,
38
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +000039 };
40
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000041 // The flags are used to indicate which corners are circluar (unflagged corners are assumed to
42 // be square).
43 static GrEffectRef* Create(GrEffectEdgeType, uint32_t circularCornerFlags, const SkRRect&);
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000044
45 virtual ~RRectEffect() {};
46 static const char* Name() { return "RRect"; }
47
48 const SkRRect& getRRect() const { return fRRect; }
49
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000050 uint32_t getCircularCornerFlags() const { return fCircularCornerFlags; }
skia.committer@gmail.com06acb582014-03-06 03:02:32 +000051
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000052 GrEffectEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +000053
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000054 typedef GLRRectEffect GLEffect;
55
56 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
57
58 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
59
60private:
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000061 RRectEffect(GrEffectEdgeType, uint32_t circularCornerFlags, const SkRRect&);
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000062
63 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE;
64
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000065 SkRRect fRRect;
66 GrEffectEdgeType fEdgeType;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000067 uint32_t fCircularCornerFlags;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000068
69 GR_DECLARE_EFFECT_TEST;
70
71 typedef GrEffect INHERITED;
72};
73
74const SkScalar RRectEffect::kRadiusMin = 0.5f;
75
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000076GrEffectRef* RRectEffect::Create(GrEffectEdgeType edgeType,
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000077 uint32_t circularCornerFlags,
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000078 const SkRRect& rrect) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +000079 SkASSERT(kFillAA_GrEffectEdgeType == edgeType || kInverseFillAA_GrEffectEdgeType == edgeType);
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000080 return CreateEffectRef(AutoEffectUnref(SkNEW_ARGS(RRectEffect,
81 (edgeType, circularCornerFlags, rrect))));
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000082}
83
84void RRectEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
85 *validFlags = 0;
86}
87
88const GrBackendEffectFactory& RRectEffect::getFactory() const {
89 return GrTBackendEffectFactory<RRectEffect>::getInstance();
90}
91
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000092RRectEffect::RRectEffect(GrEffectEdgeType edgeType, uint32_t circularCornerFlags,
93 const SkRRect& rrect)
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +000094 : fRRect(rrect)
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000095 , fEdgeType(edgeType)
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000096 , fCircularCornerFlags(circularCornerFlags) {
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000097 this->setWillReadFragmentPosition();
98}
99
100bool RRectEffect::onIsEqual(const GrEffect& other) const {
101 const RRectEffect& rre = CastEffect<RRectEffect>(other);
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000102 // type is derived from fRRect, so no need to check it.
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000103 return fEdgeType == rre.fEdgeType && fRRect == rre.fRRect;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000104}
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000105
106//////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000107
108GR_DEFINE_EFFECT_TEST(RRectEffect);
109
110GrEffectRef* RRectEffect::TestCreate(SkRandom* random,
111 GrContext*,
112 const GrDrawTargetCaps& caps,
113 GrTexture*[]) {
114 SkScalar w = random->nextRangeScalar(20.f, 1000.f);
115 SkScalar h = random->nextRangeScalar(20.f, 1000.f);
116 SkScalar r = random->nextRangeF(kRadiusMin, 9.f);
117 SkRRect rrect;
118 rrect.setRectXY(SkRect::MakeWH(w, h), r, r);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000119 GrEffectRef* effect;
120 do {
121 GrEffectEdgeType et = (GrEffectEdgeType)random->nextULessThan(kGrEffectEdgeTypeCnt);
122 effect = GrRRectEffect::Create(et, rrect);
123 } while (NULL == effect);
124 return effect;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000125}
126
127//////////////////////////////////////////////////////////////////////////////
128
129class GLRRectEffect : public GrGLEffect {
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000130public:
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000131 GLRRectEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000132
133 virtual void emitCode(GrGLShaderBuilder* builder,
134 const GrDrawEffect& drawEffect,
135 EffectKey key,
136 const char* outputColor,
137 const char* inputColor,
138 const TransformedCoordsArray&,
139 const TextureSamplerArray&) SK_OVERRIDE;
140
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000141 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000142
143 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
144
145private:
146 GrGLUniformManager::UniformHandle fInnerRectUniform;
147 GrGLUniformManager::UniformHandle fRadiusPlusHalfUniform;
148 SkRRect fPrevRRect;
149 typedef GrGLEffect INHERITED;
150};
151
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000152GLRRectEffect::GLRRectEffect(const GrBackendEffectFactory& factory,
153 const GrDrawEffect& drawEffect)
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000154 : INHERITED (factory) {
155 fPrevRRect.setEmpty();
156}
157
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000158void GLRRectEffect::emitCode(GrGLShaderBuilder* builder,
159 const GrDrawEffect& drawEffect,
160 EffectKey key,
161 const char* outputColor,
162 const char* inputColor,
163 const TransformedCoordsArray&,
164 const TextureSamplerArray& samplers) {
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000165 const RRectEffect& rre = drawEffect.castEffect<RRectEffect>();
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000166 const char *rectName;
167 const char *radiusPlusHalfName;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000168 // The inner rect is the rrect bounds inset by the radius. Its left, top, right, and bottom
169 // edges correspond to components x, y, z, and w, respectively. When a side of the rrect has
170 // only rectangular corners, that side's value corresponds to the rect edge's value outset by
171 // half a pixel.
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000172 fInnerRectUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
173 kVec4f_GrSLType,
174 "innerRect",
175 &rectName);
176 fRadiusPlusHalfUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
177 kFloat_GrSLType,
178 "radiusPlusHalf",
179 &radiusPlusHalfName);
180 const char* fragmentPos = builder->fragmentPosition();
181 // At each quarter-circle corner we compute a vector that is the offset of the fragment position
182 // from the circle center. The vector is pinned in x and y to be in the quarter-plane relevant
183 // to that corner. This means that points near the interior near the rrect top edge will have
184 // a vector that points straight up for both the TL left and TR corners. Computing an
185 // alpha from this vector at either the TR or TL corner will give the correct result. Similarly,
186 // fragments near the other three edges will get the correct AA. Fragments in the interior of
187 // the rrect will have a (0,0) vector at all four corners. So long as the radius > 0.5 they will
188 // correctly produce an alpha value of 1 at all four corners. We take the min of all the alphas.
189 // The code below is a simplified version of the above that performs maxs on the vector
190 // components before computing distances and alpha values so that only one distance computation
191 // need be computed to determine the min alpha.
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000192 //
193 // For the cases where one half of the rrect is rectangular we drop one of the x or y
194 // computations, compute a separate rect edge alpha for the rect side, and mul the two computed
195 // alphas together.
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000196 switch (rre.getCircularCornerFlags()) {
197 case RRectEffect::kAll_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000198 builder->fsCodeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
199 builder->fsCodeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
200 builder->fsCodeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n");
201 builder->fsCodeAppendf("\t\tfloat alpha = clamp(%s - length(dxy), 0.0, 1.0);\n",
202 radiusPlusHalfName);
203 break;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000204 case RRectEffect::kTopLeft_CornerFlag:
205 builder->fsCodeAppendf("\t\tvec2 dxy = max(%s.xy - %s.xy, 0.0);\n", rectName, fragmentPos);
206 builder->fsCodeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n",
207 rectName, fragmentPos);
208 builder->fsCodeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n",
209 rectName, fragmentPos);
210 builder->fsCodeAppendf("\t\tfloat alpha = bottomAlpha * rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
211 radiusPlusHalfName);
212 break;
213 case RRectEffect::kTopRight_CornerFlag:
214 builder->fsCodeAppendf("\t\tvec2 dxy = max(vec2(%s.x - %s.z, %s.y - %s.y), 0.0);\n",
215 fragmentPos, rectName, rectName, fragmentPos);
216 builder->fsCodeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n",
217 fragmentPos, rectName);
218 builder->fsCodeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n",
219 rectName, fragmentPos);
220 builder->fsCodeAppendf("\t\tfloat alpha = bottomAlpha * leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
221 radiusPlusHalfName);
222 break;
223 case RRectEffect::kBottomRight_CornerFlag:
224 builder->fsCodeAppendf("\t\tvec2 dxy = max(%s.xy - %s.zw, 0.0);\n",
225 fragmentPos, rectName);
226 builder->fsCodeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n",
227 fragmentPos, rectName);
228 builder->fsCodeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n",
229 fragmentPos, rectName);
230 builder->fsCodeAppendf("\t\tfloat alpha = topAlpha * leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
231 radiusPlusHalfName);
232 break;
233 case RRectEffect::kBottomLeft_CornerFlag:
234 builder->fsCodeAppendf("\t\tvec2 dxy = max(vec2(%s.x - %s.x, %s.y - %s.w), 0.0);\n",
235 rectName, fragmentPos, fragmentPos, rectName);
236 builder->fsCodeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n",
237 rectName, fragmentPos);
238 builder->fsCodeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n",
239 fragmentPos, rectName);
240 builder->fsCodeAppendf("\t\tfloat alpha = topAlpha * rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
241 radiusPlusHalfName);
242 break;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000243 case RRectEffect::kLeft_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000244 builder->fsCodeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
245 builder->fsCodeAppendf("\t\tfloat dy1 = %s.y - %s.w;\n", fragmentPos, rectName);
246 builder->fsCodeAppend("\t\tvec2 dxy = max(vec2(dxy0.x, max(dxy0.y, dy1)), 0.0);\n");
247 builder->fsCodeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n",
248 rectName, fragmentPos);
249 builder->fsCodeAppendf("\t\tfloat alpha = rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
250 radiusPlusHalfName);
251 break;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000252 case RRectEffect::kTop_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000253 builder->fsCodeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
254 builder->fsCodeAppendf("\t\tfloat dx1 = %s.x - %s.z;\n", fragmentPos, rectName);
255 builder->fsCodeAppend("\t\tvec2 dxy = max(vec2(max(dxy0.x, dx1), dxy0.y), 0.0);\n");
256 builder->fsCodeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n",
257 rectName, fragmentPos);
258 builder->fsCodeAppendf("\t\tfloat alpha = bottomAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
259 radiusPlusHalfName);
260 break;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000261 case RRectEffect::kRight_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000262 builder->fsCodeAppendf("\t\tfloat dy0 = %s.y - %s.y;\n", rectName, fragmentPos);
263 builder->fsCodeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
264 builder->fsCodeAppend("\t\tvec2 dxy = max(vec2(dxy1.x, max(dy0, dxy1.y)), 0.0);\n");
265 builder->fsCodeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n",
266 fragmentPos, rectName);
267 builder->fsCodeAppendf("\t\tfloat alpha = leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
268 radiusPlusHalfName);
269 break;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000270 case RRectEffect::kBottom_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000271 builder->fsCodeAppendf("\t\tfloat dx0 = %s.x - %s.x;\n", rectName, fragmentPos);
272 builder->fsCodeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
273 builder->fsCodeAppend("\t\tvec2 dxy = max(vec2(max(dx0, dxy1.x), dxy1.y), 0.0);\n");
274 builder->fsCodeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n",
275 fragmentPos, rectName);
276 builder->fsCodeAppendf("\t\tfloat alpha = topAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
277 radiusPlusHalfName);
278 break;
279 }
skia.committer@gmail.com06acb582014-03-06 03:02:32 +0000280
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000281 if (kInverseFillAA_GrEffectEdgeType == rre.getEdgeType()) {
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000282 builder->fsCodeAppend("\t\talpha = 1.0 - alpha;\n");
283 }
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000284
285 builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor,
286 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
287}
288
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000289GrGLEffect::EffectKey GLRRectEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
290 const RRectEffect& rre = drawEffect.castEffect<RRectEffect>();
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000291 GR_STATIC_ASSERT(kGrEffectEdgeTypeCnt <= 8);
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000292 return (rre.getCircularCornerFlags() << 3) | rre.getEdgeType();
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000293}
294
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000295void GLRRectEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) {
296 const RRectEffect& rre = drawEffect.castEffect<RRectEffect>();
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000297 const SkRRect& rrect = rre.getRRect();
298 if (rrect != fPrevRRect) {
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000299 SkRect rect = rrect.getBounds();
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000300 SkScalar radius = 0;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000301 switch (rre.getCircularCornerFlags()) {
302 case RRectEffect::kAll_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000303 SkASSERT(rrect.isSimpleCircular());
304 radius = rrect.getSimpleRadii().fX;
305 SkASSERT(radius >= RRectEffect::kRadiusMin);
306 rect.inset(radius, radius);
307 break;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000308 case RRectEffect::kTopLeft_CornerFlag:
309 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
310 rect.fLeft += radius;
311 rect.fTop += radius;
312 rect.fRight += 0.5f;
313 rect.fBottom += 0.5f;
314 break;
315 case RRectEffect::kTopRight_CornerFlag:
316 radius = rrect.radii(SkRRect::kUpperRight_Corner).fX;
317 rect.fLeft -= 0.5;
318 rect.fTop += radius;
319 rect.fRight -= radius;
320 rect.fBottom += 0.5f;
321 break;
322 case RRectEffect::kBottomRight_CornerFlag:
323 radius = rrect.radii(SkRRect::kLowerRight_Corner).fX;
324 rect.fLeft -= 0.5;
325 rect.fTop -= 0.5;
326 rect.fRight -= radius;
327 rect.fBottom -= radius;
328 break;
329 case RRectEffect::kBottomLeft_CornerFlag:
330 radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX;
331 rect.fLeft += radius;
332 rect.fTop -= 0.5;
333 rect.fRight += 0.5;
334 rect.fBottom -= radius;
335 break;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000336 case RRectEffect::kLeft_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000337 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
338 rect.fLeft += radius;
339 rect.fTop += radius;
340 rect.fRight += 0.5f;
341 rect.fBottom -= radius;
342 break;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000343 case RRectEffect::kTop_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000344 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
345 rect.fLeft += radius;
346 rect.fTop += radius;
347 rect.fRight -= radius;
348 rect.fBottom += 0.5f;
349 break;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000350 case RRectEffect::kRight_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000351 radius = rrect.radii(SkRRect::kUpperRight_Corner).fX;
352 rect.fLeft -= 0.5f;
353 rect.fTop += radius;
354 rect.fRight -= radius;
355 rect.fBottom -= radius;
356 break;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000357 case RRectEffect::kBottom_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000358 radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX;
359 rect.fLeft += radius;
360 rect.fTop -= 0.5f;
361 rect.fRight -= radius;
362 rect.fBottom -= radius;
363 break;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000364 default:
365 GrCrash("Should have been one of the above cases.");
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000366 }
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000367 uman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
368 uman.set1f(fRadiusPlusHalfUniform, radius + 0.5f);
369 fPrevRRect = rrect;
370 }
371}
372
373//////////////////////////////////////////////////////////////////////////////
374
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000375GrEffectRef* GrRRectEffect::Create(GrEffectEdgeType edgeType, const SkRRect& rrect) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000376 if (kFillAA_GrEffectEdgeType != edgeType && kInverseFillAA_GrEffectEdgeType != edgeType) {
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000377 return NULL;
378 }
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000379 uint32_t cornerFlags;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000380 if (rrect.isSimpleCircular()) {
381 if (rrect.getSimpleRadii().fX < RRectEffect::kRadiusMin) {
382 return NULL;
383 }
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000384 cornerFlags = RRectEffect::kAll_CornerFlags;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000385 } else if (rrect.isComplex()) {
386 // Check for the "tab" cases - two adjacent circular corners and two square corners.
387 SkScalar radius = 0;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000388 cornerFlags = 0;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000389 for (int c = 0; c < 4; ++c) {
390 const SkVector& r = rrect.radii((SkRRect::Corner)c);
391 SkASSERT((0 == r.fX) == (0 == r.fY));
392 if (0 == r.fX) {
393 continue;
394 }
395 if (r.fX != r.fY) {
396 return NULL;
397 }
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000398 if (!cornerFlags) {
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000399 radius = r.fX;
400 if (radius < RRectEffect::kRadiusMin) {
401 return NULL;
402 }
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000403 cornerFlags = 1 << c;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000404 } else {
405 if (r.fX != radius) {
406 return NULL;
407 }
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000408 cornerFlags |= 1 << c;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000409 }
410 }
411
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000412 switch (cornerFlags) {
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000413 case RRectEffect::kTopLeft_CornerFlag:
414 case RRectEffect::kTopRight_CornerFlag:
415 case RRectEffect::kBottomRight_CornerFlag:
416 case RRectEffect::kBottomLeft_CornerFlag:
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000417 case RRectEffect::kLeft_CornerFlags:
418 case RRectEffect::kTop_CornerFlags:
419 case RRectEffect::kRight_CornerFlags:
420 case RRectEffect::kBottom_CornerFlags:
421 case RRectEffect::kAll_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000422 break;
423 default:
424 return NULL;
425 }
426 } else {
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000427 return NULL;
428 }
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000429 return RRectEffect::Create(edgeType, cornerFlags, rrect);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000430}