2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "GrRRectEffect.h"
10 #include "GrConvexPolyEffect.h"
11 #include "GrFragmentProcessor.h"
12 #include "GrInvariantOutput.h"
13 #include "GrOvalEffect.h"
15 #include "gl/GrGLProcessor.h"
16 #include "gl/GrGLSL.h"
17 #include "gl/builders/GrGLProgramBuilder.h"
19 // The effects defined here only handle rrect radii >= kRadiusMin.
20 static const SkScalar kRadiusMin = SK_ScalarHalf;
22 //////////////////////////////////////////////////////////////////////////////
24 class CircularRRectEffect : public GrFragmentProcessor {
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),
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,
38 kAll_CornerFlags = kTopLeft_CornerFlag | kTopRight_CornerFlag |
39 kBottomLeft_CornerFlag | kBottomRight_CornerFlag,
44 // The flags are used to indicate which corners are circluar (unflagged corners are assumed to
46 static GrFragmentProcessor* Create(GrPrimitiveEdgeType, uint32_t circularCornerFlags,
49 virtual ~CircularRRectEffect() {};
51 const char* name() const SK_OVERRIDE { return "CircularRRect"; }
53 void getGLProcessorKey(const GrGLCaps&, GrProcessorKeyBuilder*) const SK_OVERRIDE;
55 GrGLFragmentProcessor* createGLInstance() const SK_OVERRIDE;
57 const SkRRect& getRRect() const { return fRRect; }
59 uint32_t getCircularCornerFlags() const { return fCircularCornerFlags; }
61 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
64 CircularRRectEffect(GrPrimitiveEdgeType, uint32_t circularCornerFlags, const SkRRect&);
66 bool onIsEqual(const GrFragmentProcessor& other) const SK_OVERRIDE;
68 void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE;
71 GrPrimitiveEdgeType fEdgeType;
72 uint32_t fCircularCornerFlags;
74 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
76 typedef GrFragmentProcessor INHERITED;
79 GrFragmentProcessor* CircularRRectEffect::Create(GrPrimitiveEdgeType edgeType,
80 uint32_t circularCornerFlags,
81 const SkRRect& rrect) {
82 if (kFillAA_GrProcessorEdgeType != edgeType && kInverseFillAA_GrProcessorEdgeType != edgeType) {
85 return SkNEW_ARGS(CircularRRectEffect, (edgeType, circularCornerFlags, rrect));
88 void CircularRRectEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
89 inout->mulByUnknownSingleComponent();
92 CircularRRectEffect::CircularRRectEffect(GrPrimitiveEdgeType edgeType, uint32_t circularCornerFlags,
96 , fCircularCornerFlags(circularCornerFlags) {
97 this->initClassID<CircularRRectEffect>();
98 this->setWillReadFragmentPosition();
101 bool CircularRRectEffect::onIsEqual(const GrFragmentProcessor& other) const {
102 const CircularRRectEffect& crre = other.cast<CircularRRectEffect>();
103 // The corner flags are derived from fRRect, so no need to check them.
104 return fEdgeType == crre.fEdgeType && fRRect == crre.fRRect;
107 //////////////////////////////////////////////////////////////////////////////
109 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(CircularRRectEffect);
111 GrFragmentProcessor* CircularRRectEffect::TestCreate(SkRandom* random,
113 const GrDrawTargetCaps& caps,
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);
119 rrect.setRectXY(SkRect::MakeWH(w, h), r, r);
120 GrFragmentProcessor* fp;
122 GrPrimitiveEdgeType et =
123 (GrPrimitiveEdgeType)random->nextULessThan(kGrProcessorEdgeTypeCnt);
124 fp = GrRRectEffect::Create(et, rrect);
125 } while (NULL == fp);
129 //////////////////////////////////////////////////////////////////////////////
131 class GLCircularRRectEffect : public GrGLFragmentProcessor {
133 GLCircularRRectEffect(const GrProcessor&);
135 virtual void emitCode(GrGLFPBuilder* builder,
136 const GrFragmentProcessor& fp,
137 const char* outputColor,
138 const char* inputColor,
139 const TransformedCoordsArray&,
140 const TextureSamplerArray&) SK_OVERRIDE;
142 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*);
144 void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
147 GrGLProgramDataManager::UniformHandle fInnerRectUniform;
148 GrGLProgramDataManager::UniformHandle fRadiusPlusHalfUniform;
150 typedef GrGLFragmentProcessor INHERITED;
153 GLCircularRRectEffect::GLCircularRRectEffect(const GrProcessor& ) {
154 fPrevRRect.setEmpty();
157 void GLCircularRRectEffect::emitCode(GrGLFPBuilder* builder,
158 const GrFragmentProcessor& fp,
159 const char* outputColor,
160 const char* inputColor,
161 const TransformedCoordsArray&,
162 const TextureSamplerArray& samplers) {
163 const CircularRRectEffect& crre = fp.cast<CircularRRectEffect>();
164 const char *rectName;
165 const char *radiusPlusHalfName;
166 // The inner rect is the rrect bounds inset by the radius. Its left, top, right, and bottom
167 // edges correspond to components x, y, z, and w, respectively. When a side of the rrect has
168 // only rectangular corners, that side's value corresponds to the rect edge's value outset by
170 fInnerRectUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
171 kVec4f_GrSLType, kDefault_GrSLPrecision,
174 fRadiusPlusHalfUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
175 kFloat_GrSLType, kDefault_GrSLPrecision,
177 &radiusPlusHalfName);
179 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
180 const char* fragmentPos = fsBuilder->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.
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
196 switch (crre.getCircularCornerFlags()) {
197 case CircularRRectEffect::kAll_CornerFlags:
198 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
199 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
200 fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n");
201 fsBuilder->codeAppendf("\t\tfloat alpha = clamp(%s - length(dxy), 0.0, 1.0);\n",
204 case CircularRRectEffect::kTopLeft_CornerFlag:
205 fsBuilder->codeAppendf("\t\tvec2 dxy = max(%s.xy - %s.xy, 0.0);\n",
206 rectName, fragmentPos);
207 fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n",
208 rectName, fragmentPos);
209 fsBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n",
210 rectName, fragmentPos);
211 fsBuilder->codeAppendf("\t\tfloat alpha = bottomAlpha * rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
214 case CircularRRectEffect::kTopRight_CornerFlag:
215 fsBuilder->codeAppendf("\t\tvec2 dxy = max(vec2(%s.x - %s.z, %s.y - %s.y), 0.0);\n",
216 fragmentPos, rectName, rectName, fragmentPos);
217 fsBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n",
218 fragmentPos, rectName);
219 fsBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n",
220 rectName, fragmentPos);
221 fsBuilder->codeAppendf("\t\tfloat alpha = bottomAlpha * leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
224 case CircularRRectEffect::kBottomRight_CornerFlag:
225 fsBuilder->codeAppendf("\t\tvec2 dxy = max(%s.xy - %s.zw, 0.0);\n",
226 fragmentPos, rectName);
227 fsBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n",
228 fragmentPos, rectName);
229 fsBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n",
230 fragmentPos, rectName);
231 fsBuilder->codeAppendf("\t\tfloat alpha = topAlpha * leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
234 case CircularRRectEffect::kBottomLeft_CornerFlag:
235 fsBuilder->codeAppendf("\t\tvec2 dxy = max(vec2(%s.x - %s.x, %s.y - %s.w), 0.0);\n",
236 rectName, fragmentPos, fragmentPos, rectName);
237 fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n",
238 rectName, fragmentPos);
239 fsBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n",
240 fragmentPos, rectName);
241 fsBuilder->codeAppendf("\t\tfloat alpha = topAlpha * rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
244 case CircularRRectEffect::kLeft_CornerFlags:
245 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
246 fsBuilder->codeAppendf("\t\tfloat dy1 = %s.y - %s.w;\n", fragmentPos, rectName);
247 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(dxy0.x, max(dxy0.y, dy1)), 0.0);\n");
248 fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n",
249 rectName, fragmentPos);
250 fsBuilder->codeAppendf("\t\tfloat alpha = rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
253 case CircularRRectEffect::kTop_CornerFlags:
254 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
255 fsBuilder->codeAppendf("\t\tfloat dx1 = %s.x - %s.z;\n", fragmentPos, rectName);
256 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(max(dxy0.x, dx1), dxy0.y), 0.0);\n");
257 fsBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n",
258 rectName, fragmentPos);
259 fsBuilder->codeAppendf("\t\tfloat alpha = bottomAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
262 case CircularRRectEffect::kRight_CornerFlags:
263 fsBuilder->codeAppendf("\t\tfloat dy0 = %s.y - %s.y;\n", rectName, fragmentPos);
264 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
265 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(dxy1.x, max(dy0, dxy1.y)), 0.0);\n");
266 fsBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n",
267 fragmentPos, rectName);
268 fsBuilder->codeAppendf("\t\tfloat alpha = leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
271 case CircularRRectEffect::kBottom_CornerFlags:
272 fsBuilder->codeAppendf("\t\tfloat dx0 = %s.x - %s.x;\n", rectName, fragmentPos);
273 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
274 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(max(dx0, dxy1.x), dxy1.y), 0.0);\n");
275 fsBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n",
276 fragmentPos, rectName);
277 fsBuilder->codeAppendf("\t\tfloat alpha = topAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
282 if (kInverseFillAA_GrProcessorEdgeType == crre.getEdgeType()) {
283 fsBuilder->codeAppend("\t\talpha = 1.0 - alpha;\n");
286 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor,
287 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
290 void GLCircularRRectEffect::GenKey(const GrProcessor& processor, const GrGLCaps&,
291 GrProcessorKeyBuilder* b) {
292 const CircularRRectEffect& crre = processor.cast<CircularRRectEffect>();
293 GR_STATIC_ASSERT(kGrProcessorEdgeTypeCnt <= 8);
294 b->add32((crre.getCircularCornerFlags() << 3) | crre.getEdgeType());
297 void GLCircularRRectEffect::setData(const GrGLProgramDataManager& pdman,
298 const GrProcessor& processor) {
299 const CircularRRectEffect& crre = processor.cast<CircularRRectEffect>();
300 const SkRRect& rrect = crre.getRRect();
301 if (rrect != fPrevRRect) {
302 SkRect rect = rrect.getBounds();
304 switch (crre.getCircularCornerFlags()) {
305 case CircularRRectEffect::kAll_CornerFlags:
306 SkASSERT(rrect.isSimpleCircular());
307 radius = rrect.getSimpleRadii().fX;
308 SkASSERT(radius >= kRadiusMin);
309 rect.inset(radius, radius);
311 case CircularRRectEffect::kTopLeft_CornerFlag:
312 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
313 rect.fLeft += radius;
316 rect.fBottom += 0.5f;
318 case CircularRRectEffect::kTopRight_CornerFlag:
319 radius = rrect.radii(SkRRect::kUpperRight_Corner).fX;
322 rect.fRight -= radius;
323 rect.fBottom += 0.5f;
325 case CircularRRectEffect::kBottomRight_CornerFlag:
326 radius = rrect.radii(SkRRect::kLowerRight_Corner).fX;
329 rect.fRight -= radius;
330 rect.fBottom -= radius;
332 case CircularRRectEffect::kBottomLeft_CornerFlag:
333 radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX;
334 rect.fLeft += radius;
337 rect.fBottom -= radius;
339 case CircularRRectEffect::kLeft_CornerFlags:
340 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
341 rect.fLeft += radius;
344 rect.fBottom -= radius;
346 case CircularRRectEffect::kTop_CornerFlags:
347 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
348 rect.fLeft += radius;
350 rect.fRight -= radius;
351 rect.fBottom += 0.5f;
353 case CircularRRectEffect::kRight_CornerFlags:
354 radius = rrect.radii(SkRRect::kUpperRight_Corner).fX;
357 rect.fRight -= radius;
358 rect.fBottom -= radius;
360 case CircularRRectEffect::kBottom_CornerFlags:
361 radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX;
362 rect.fLeft += radius;
364 rect.fRight -= radius;
365 rect.fBottom -= radius;
368 SkFAIL("Should have been one of the above cases.");
370 pdman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
371 pdman.set1f(fRadiusPlusHalfUniform, radius + 0.5f);
376 ////////////////////////////////////////////////////////////////////////////////////////////////////
378 void CircularRRectEffect::getGLProcessorKey(const GrGLCaps& caps,
379 GrProcessorKeyBuilder* b) const {
380 GLCircularRRectEffect::GenKey(*this, caps, b);
383 GrGLFragmentProcessor* CircularRRectEffect::createGLInstance() const {
384 return SkNEW_ARGS(GLCircularRRectEffect, (*this));
387 //////////////////////////////////////////////////////////////////////////////
389 class EllipticalRRectEffect : public GrFragmentProcessor {
391 static GrFragmentProcessor* Create(GrPrimitiveEdgeType, const SkRRect&);
393 virtual ~EllipticalRRectEffect() {};
395 const char* name() const SK_OVERRIDE { return "EllipticalRRect"; }
397 void getGLProcessorKey(const GrGLCaps&, GrProcessorKeyBuilder*) const SK_OVERRIDE;
399 GrGLFragmentProcessor* createGLInstance() const SK_OVERRIDE;
401 const SkRRect& getRRect() const { return fRRect; }
403 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
406 EllipticalRRectEffect(GrPrimitiveEdgeType, const SkRRect&);
408 bool onIsEqual(const GrFragmentProcessor& other) const SK_OVERRIDE;
410 void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE;
413 GrPrimitiveEdgeType fEdgeType;
415 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
417 typedef GrFragmentProcessor INHERITED;
421 EllipticalRRectEffect::Create(GrPrimitiveEdgeType edgeType, const SkRRect& rrect) {
422 if (kFillAA_GrProcessorEdgeType != edgeType && kInverseFillAA_GrProcessorEdgeType != edgeType) {
425 return SkNEW_ARGS(EllipticalRRectEffect, (edgeType, rrect));
428 void EllipticalRRectEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
429 inout->mulByUnknownSingleComponent();
432 EllipticalRRectEffect::EllipticalRRectEffect(GrPrimitiveEdgeType edgeType, const SkRRect& rrect)
434 , fEdgeType(edgeType) {
435 this->initClassID<EllipticalRRectEffect>();
436 this->setWillReadFragmentPosition();
439 bool EllipticalRRectEffect::onIsEqual(const GrFragmentProcessor& other) const {
440 const EllipticalRRectEffect& erre = other.cast<EllipticalRRectEffect>();
441 return fEdgeType == erre.fEdgeType && fRRect == erre.fRRect;
444 //////////////////////////////////////////////////////////////////////////////
446 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(EllipticalRRectEffect);
448 GrFragmentProcessor* EllipticalRRectEffect::TestCreate(SkRandom* random,
450 const GrDrawTargetCaps& caps,
452 SkScalar w = random->nextRangeScalar(20.f, 1000.f);
453 SkScalar h = random->nextRangeScalar(20.f, 1000.f);
455 r[SkRRect::kUpperLeft_Corner].fX = random->nextRangeF(kRadiusMin, 9.f);
456 // ensure at least one corner really is elliptical
458 r[SkRRect::kUpperLeft_Corner].fY = random->nextRangeF(kRadiusMin, 9.f);
459 } while (r[SkRRect::kUpperLeft_Corner].fY == r[SkRRect::kUpperLeft_Corner].fX);
462 if (random->nextBool()) {
463 // half the time create a four-radii rrect.
464 r[SkRRect::kLowerRight_Corner].fX = random->nextRangeF(kRadiusMin, 9.f);
465 r[SkRRect::kLowerRight_Corner].fY = random->nextRangeF(kRadiusMin, 9.f);
467 r[SkRRect::kUpperRight_Corner].fX = r[SkRRect::kLowerRight_Corner].fX;
468 r[SkRRect::kUpperRight_Corner].fY = r[SkRRect::kUpperLeft_Corner].fY;
470 r[SkRRect::kLowerLeft_Corner].fX = r[SkRRect::kUpperLeft_Corner].fX;
471 r[SkRRect::kLowerLeft_Corner].fY = r[SkRRect::kLowerRight_Corner].fY;
473 rrect.setRectRadii(SkRect::MakeWH(w, h), r);
475 rrect.setRectXY(SkRect::MakeWH(w, h), r[SkRRect::kUpperLeft_Corner].fX,
476 r[SkRRect::kUpperLeft_Corner].fY);
478 GrFragmentProcessor* fp;
480 GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)random->nextULessThan(kGrProcessorEdgeTypeCnt);
481 fp = GrRRectEffect::Create(et, rrect);
482 } while (NULL == fp);
486 //////////////////////////////////////////////////////////////////////////////
488 class GLEllipticalRRectEffect : public GrGLFragmentProcessor {
490 GLEllipticalRRectEffect(const GrProcessor&);
492 virtual void emitCode(GrGLFPBuilder* builder,
493 const GrFragmentProcessor& effect,
494 const char* outputColor,
495 const char* inputColor,
496 const TransformedCoordsArray&,
497 const TextureSamplerArray&) SK_OVERRIDE;
499 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*);
501 void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
504 GrGLProgramDataManager::UniformHandle fInnerRectUniform;
505 GrGLProgramDataManager::UniformHandle fInvRadiiSqdUniform;
507 typedef GrGLFragmentProcessor INHERITED;
510 GLEllipticalRRectEffect::GLEllipticalRRectEffect(const GrProcessor& effect) {
511 fPrevRRect.setEmpty();
514 void GLEllipticalRRectEffect::emitCode(GrGLFPBuilder* builder,
515 const GrFragmentProcessor& effect,
516 const char* outputColor,
517 const char* inputColor,
518 const TransformedCoordsArray&,
519 const TextureSamplerArray& samplers) {
520 const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>();
521 const char *rectName;
522 // The inner rect is the rrect bounds inset by the x/y radii
523 fInnerRectUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
524 kVec4f_GrSLType, kDefault_GrSLPrecision,
528 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
529 const char* fragmentPos = fsBuilder->fragmentPosition();
530 // At each quarter-ellipse corner we compute a vector that is the offset of the fragment pos
531 // to the ellipse center. The vector is pinned in x and y to be in the quarter-plane relevant
532 // to that corner. This means that points near the interior near the rrect top edge will have
533 // a vector that points straight up for both the TL left and TR corners. Computing an
534 // alpha from this vector at either the TR or TL corner will give the correct result. Similarly,
535 // fragments near the other three edges will get the correct AA. Fragments in the interior of
536 // the rrect will have a (0,0) vector at all four corners. So long as the radii > 0.5 they will
537 // correctly produce an alpha value of 1 at all four corners. We take the min of all the alphas.
538 // The code below is a simplified version of the above that performs maxs on the vector
539 // components before computing distances and alpha values so that only one distance computation
540 // need be computed to determine the min alpha.
541 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
542 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
543 switch (erre.getRRect().getType()) {
544 case SkRRect::kSimple_Type: {
545 const char *invRadiiXYSqdName;
546 fInvRadiiSqdUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
547 kVec2f_GrSLType, kDefault_GrSLPrecision,
550 fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n");
551 // Z is the x/y offsets divided by squared radii.
552 fsBuilder->codeAppendf("\t\tvec2 Z = dxy * %s;\n", invRadiiXYSqdName);
555 case SkRRect::kNinePatch_Type: {
556 const char *invRadiiLTRBSqdName;
557 fInvRadiiSqdUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
558 kVec4f_GrSLType, kDefault_GrSLPrecision,
560 &invRadiiLTRBSqdName);
561 fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n");
562 // Z is the x/y offsets divided by squared radii. We only care about the (at most) one
563 // corner where both the x and y offsets are positive, hence the maxes. (The inverse
564 // squared radii will always be positive.)
565 fsBuilder->codeAppendf("\t\tvec2 Z = max(max(dxy0 * %s.xy, dxy1 * %s.zw), 0.0);\n",
566 invRadiiLTRBSqdName, invRadiiLTRBSqdName);
570 SkFAIL("RRect should always be simple or nine-patch.");
572 // implicit is the evaluation of (x/a)^2 + (y/b)^2 - 1.
573 fsBuilder->codeAppend("\t\tfloat implicit = dot(Z, dxy) - 1.0;\n");
574 // grad_dot is the squared length of the gradient of the implicit.
575 fsBuilder->codeAppendf("\t\tfloat grad_dot = 4.0 * dot(Z, Z);\n");
576 // avoid calling inversesqrt on zero.
577 fsBuilder->codeAppend("\t\tgrad_dot = max(grad_dot, 1.0e-4);\n");
578 fsBuilder->codeAppendf("\t\tfloat approx_dist = implicit * inversesqrt(grad_dot);\n");
580 if (kFillAA_GrProcessorEdgeType == erre.getEdgeType()) {
581 fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 - approx_dist, 0.0, 1.0);\n");
583 fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 + approx_dist, 0.0, 1.0);\n");
586 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor,
587 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
590 void GLEllipticalRRectEffect::GenKey(const GrProcessor& effect, const GrGLCaps&,
591 GrProcessorKeyBuilder* b) {
592 const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>();
593 GR_STATIC_ASSERT(kLast_GrProcessorEdgeType < (1 << 3));
594 b->add32(erre.getRRect().getType() | erre.getEdgeType() << 3);
597 void GLEllipticalRRectEffect::setData(const GrGLProgramDataManager& pdman,
598 const GrProcessor& effect) {
599 const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>();
600 const SkRRect& rrect = erre.getRRect();
601 if (rrect != fPrevRRect) {
602 SkRect rect = rrect.getBounds();
603 const SkVector& r0 = rrect.radii(SkRRect::kUpperLeft_Corner);
604 SkASSERT(r0.fX >= kRadiusMin);
605 SkASSERT(r0.fY >= kRadiusMin);
606 switch (erre.getRRect().getType()) {
607 case SkRRect::kSimple_Type:
608 rect.inset(r0.fX, r0.fY);
609 pdman.set2f(fInvRadiiSqdUniform, 1.f / (r0.fX * r0.fX),
610 1.f / (r0.fY * r0.fY));
612 case SkRRect::kNinePatch_Type: {
613 const SkVector& r1 = rrect.radii(SkRRect::kLowerRight_Corner);
614 SkASSERT(r1.fX >= kRadiusMin);
615 SkASSERT(r1.fY >= kRadiusMin);
618 rect.fRight -= r1.fX;
619 rect.fBottom -= r1.fY;
620 pdman.set4f(fInvRadiiSqdUniform, 1.f / (r0.fX * r0.fX),
621 1.f / (r0.fY * r0.fY),
622 1.f / (r1.fX * r1.fX),
623 1.f / (r1.fY * r1.fY));
627 SkFAIL("RRect should always be simple or nine-patch.");
629 pdman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
634 ////////////////////////////////////////////////////////////////////////////////////////////////////
636 void EllipticalRRectEffect::getGLProcessorKey(const GrGLCaps& caps,
637 GrProcessorKeyBuilder* b) const {
638 GLEllipticalRRectEffect::GenKey(*this, caps, b);
641 GrGLFragmentProcessor* EllipticalRRectEffect::createGLInstance() const {
642 return SkNEW_ARGS(GLEllipticalRRectEffect, (*this));
645 //////////////////////////////////////////////////////////////////////////////
647 GrFragmentProcessor* GrRRectEffect::Create(GrPrimitiveEdgeType edgeType, const SkRRect& rrect) {
648 if (rrect.isRect()) {
649 return GrConvexPolyEffect::Create(edgeType, rrect.getBounds());
652 if (rrect.isOval()) {
653 return GrOvalEffect::Create(edgeType, rrect.getBounds());
656 if (rrect.isSimple()) {
657 if (rrect.getSimpleRadii().fX < kRadiusMin || rrect.getSimpleRadii().fY < kRadiusMin) {
658 // In this case the corners are extremely close to rectangular and we collapse the
659 // clip to a rectangular clip.
660 return GrConvexPolyEffect::Create(edgeType, rrect.getBounds());
662 if (rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY) {
663 return CircularRRectEffect::Create(edgeType, CircularRRectEffect::kAll_CornerFlags,
666 return EllipticalRRectEffect::Create(edgeType, rrect);
670 if (rrect.isComplex() || rrect.isNinePatch()) {
671 // Check for the "tab" cases - two adjacent circular corners and two square corners.
672 SkScalar circularRadius = 0;
673 uint32_t cornerFlags = 0;
676 bool squashedRadii = false;
677 for (int c = 0; c < 4; ++c) {
678 radii[c] = rrect.radii((SkRRect::Corner)c);
679 SkASSERT((0 == radii[c].fX) == (0 == radii[c].fY));
680 if (0 == radii[c].fX) {
681 // The corner is square, so no need to squash or flag as circular.
684 if (radii[c].fX < kRadiusMin || radii[c].fY < kRadiusMin) {
686 squashedRadii = true;
689 if (radii[c].fX != radii[c].fY) {
694 circularRadius = radii[c].fX;
695 cornerFlags = 1 << c;
697 if (radii[c].fX != circularRadius) {
701 cornerFlags |= 1 << c;
705 switch (cornerFlags) {
706 case CircularRRectEffect::kAll_CornerFlags:
707 // This rrect should have been caught in the simple case above. Though, it would
708 // be correctly handled in the fallthrough code.
710 case CircularRRectEffect::kTopLeft_CornerFlag:
711 case CircularRRectEffect::kTopRight_CornerFlag:
712 case CircularRRectEffect::kBottomRight_CornerFlag:
713 case CircularRRectEffect::kBottomLeft_CornerFlag:
714 case CircularRRectEffect::kLeft_CornerFlags:
715 case CircularRRectEffect::kTop_CornerFlags:
716 case CircularRRectEffect::kRight_CornerFlags:
717 case CircularRRectEffect::kBottom_CornerFlags: {
718 SkTCopyOnFirstWrite<SkRRect> rr(rrect);
720 rr.writable()->setRectRadii(rrect.getBounds(), radii);
722 return CircularRRectEffect::Create(edgeType, cornerFlags, *rr);
724 case CircularRRectEffect::kNone_CornerFlags:
725 return GrConvexPolyEffect::Create(edgeType, rrect.getBounds());
728 // If we got here then we squashed some but not all the radii to zero. (If all
729 // had been squashed cornerFlags would be 0.) The elliptical effect doesn't
730 // support some rounded and some square corners.
733 if (rrect.isNinePatch()) {
734 return EllipticalRRectEffect::Create(edgeType, rrect);