blob: 1c45c3774a051cbc572addf3dd4a4565d7c3d9a1 [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
joshualitt30ba4362014-08-21 20:18:45 -07008#include "gl/builders/GrGLProgramBuilder.h"
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +00009#include "GrRRectEffect.h"
10
11#include "gl/GrGLEffect.h"
12#include "gl/GrGLSL.h"
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +000013#include "GrConvexPolyEffect.h"
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000014#include "GrOvalEffect.h"
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +000015#include "GrTBackendEffectFactory.h"
16
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000017#include "SkRRect.h"
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +000018
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +000019// The effects defined here only handle rrect radii >= kRadiusMin.
20static const SkScalar kRadiusMin = SK_ScalarHalf;
21
22//////////////////////////////////////////////////////////////////////////////
23
commit-bot@chromium.org4355f212014-03-12 15:32:50 +000024class GLCircularRRectEffect;
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000025
commit-bot@chromium.org4355f212014-03-12 15:32:50 +000026class CircularRRectEffect : public GrEffect {
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000027public:
skia.committer@gmail.comf1f66c02014-03-05 03:02:06 +000028
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000029 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.org2a8be902014-03-24 19:24:59 +000043 kNone_CornerFlags = 0
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +000044 };
45
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000046 // The flags are used to indicate which corners are circluar (unflagged corners are assumed to
47 // be square).
bsalomon83d081a2014-07-08 09:56:10 -070048 static GrEffect* Create(GrEffectEdgeType, uint32_t circularCornerFlags, const SkRRect&);
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000049
commit-bot@chromium.org4355f212014-03-12 15:32:50 +000050 virtual ~CircularRRectEffect() {};
51 static const char* Name() { return "CircularRRect"; }
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000052
53 const SkRRect& getRRect() const { return fRRect; }
54
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000055 uint32_t getCircularCornerFlags() const { return fCircularCornerFlags; }
skia.committer@gmail.com06acb582014-03-06 03:02:32 +000056
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000057 GrEffectEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +000058
commit-bot@chromium.org4355f212014-03-12 15:32:50 +000059 typedef GLCircularRRectEffect GLEffect;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000060
61 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
62
63 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
64
65private:
commit-bot@chromium.org4355f212014-03-12 15:32:50 +000066 CircularRRectEffect(GrEffectEdgeType, uint32_t circularCornerFlags, const SkRRect&);
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000067
68 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE;
69
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000070 SkRRect fRRect;
71 GrEffectEdgeType fEdgeType;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000072 uint32_t fCircularCornerFlags;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000073
74 GR_DECLARE_EFFECT_TEST;
75
76 typedef GrEffect INHERITED;
77};
78
bsalomon83d081a2014-07-08 09:56:10 -070079GrEffect* CircularRRectEffect::Create(GrEffectEdgeType edgeType,
80 uint32_t circularCornerFlags,
81 const SkRRect& rrect) {
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000082 if (kFillAA_GrEffectEdgeType != edgeType && kInverseFillAA_GrEffectEdgeType != edgeType) {
83 return NULL;
84 }
bsalomon55fad7a2014-07-08 07:34:20 -070085 return SkNEW_ARGS(CircularRRectEffect, (edgeType, circularCornerFlags, rrect));
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000086}
87
commit-bot@chromium.org4355f212014-03-12 15:32:50 +000088void CircularRRectEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000089 *validFlags = 0;
90}
91
commit-bot@chromium.org4355f212014-03-12 15:32:50 +000092const GrBackendEffectFactory& CircularRRectEffect::getFactory() const {
93 return GrTBackendEffectFactory<CircularRRectEffect>::getInstance();
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000094}
95
commit-bot@chromium.org4355f212014-03-12 15:32:50 +000096CircularRRectEffect::CircularRRectEffect(GrEffectEdgeType edgeType, uint32_t circularCornerFlags,
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000097 const SkRRect& rrect)
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +000098 : fRRect(rrect)
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000099 , fEdgeType(edgeType)
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000100 , fCircularCornerFlags(circularCornerFlags) {
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000101 this->setWillReadFragmentPosition();
102}
103
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000104bool CircularRRectEffect::onIsEqual(const GrEffect& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700105 const CircularRRectEffect& crre = other.cast<CircularRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000106 // The corner flags are derived from fRRect, so no need to check them.
107 return fEdgeType == crre.fEdgeType && fRRect == crre.fRRect;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000108}
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000109
110//////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000111
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000112GR_DEFINE_EFFECT_TEST(CircularRRectEffect);
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000113
bsalomon83d081a2014-07-08 09:56:10 -0700114GrEffect* CircularRRectEffect::TestCreate(SkRandom* random,
115 GrContext*,
116 const GrDrawTargetCaps& caps,
117 GrTexture*[]) {
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000118 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);
bsalomon83d081a2014-07-08 09:56:10 -0700123 GrEffect* effect;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000124 do {
125 GrEffectEdgeType et = (GrEffectEdgeType)random->nextULessThan(kGrEffectEdgeTypeCnt);
126 effect = GrRRectEffect::Create(et, rrect);
127 } while (NULL == effect);
128 return effect;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000129}
130
131//////////////////////////////////////////////////////////////////////////////
132
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000133class GLCircularRRectEffect : public GrGLEffect {
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000134public:
joshualitt49586be2014-09-16 08:21:41 -0700135 GLCircularRRectEffect(const GrBackendEffectFactory&, const GrEffect&);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000136
joshualitt30ba4362014-08-21 20:18:45 -0700137 virtual void emitCode(GrGLProgramBuilder* builder,
joshualitt49586be2014-09-16 08:21:41 -0700138 const GrEffect& effect,
bsalomon63e99f72014-07-21 08:03:14 -0700139 const GrEffectKey& key,
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000140 const char* outputColor,
141 const char* inputColor,
142 const TransformedCoordsArray&,
143 const TextureSamplerArray&) SK_OVERRIDE;
144
joshualitt49586be2014-09-16 08:21:41 -0700145 static inline void GenKey(const GrEffect&, const GrGLCaps&, GrEffectKeyBuilder*);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000146
joshualitt49586be2014-09-16 08:21:41 -0700147 virtual void setData(const GrGLProgramDataManager&, const GrEffect&) SK_OVERRIDE;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000148
149private:
kkinnunen7510b222014-07-30 00:04:16 -0700150 GrGLProgramDataManager::UniformHandle fInnerRectUniform;
151 GrGLProgramDataManager::UniformHandle fRadiusPlusHalfUniform;
152 SkRRect fPrevRRect;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000153 typedef GrGLEffect INHERITED;
154};
155
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000156GLCircularRRectEffect::GLCircularRRectEffect(const GrBackendEffectFactory& factory,
joshualitt49586be2014-09-16 08:21:41 -0700157 const GrEffect& effect)
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000158 : INHERITED (factory) {
159 fPrevRRect.setEmpty();
160}
161
joshualitt30ba4362014-08-21 20:18:45 -0700162void GLCircularRRectEffect::emitCode(GrGLProgramBuilder* builder,
joshualitt49586be2014-09-16 08:21:41 -0700163 const GrEffect& effect,
bsalomon63e99f72014-07-21 08:03:14 -0700164 const GrEffectKey& key,
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000165 const char* outputColor,
166 const char* inputColor,
167 const TransformedCoordsArray&,
168 const TextureSamplerArray& samplers) {
joshualitt49586be2014-09-16 08:21:41 -0700169 const CircularRRectEffect& crre = effect.cast<CircularRRectEffect>();
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000170 const char *rectName;
171 const char *radiusPlusHalfName;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000172 // 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.
joshualitt30ba4362014-08-21 20:18:45 -0700176 fInnerRectUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000177 kVec4f_GrSLType,
178 "innerRect",
179 &rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700180 fRadiusPlusHalfUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000181 kFloat_GrSLType,
182 "radiusPlusHalf",
183 &radiusPlusHalfName);
joshualitt30ba4362014-08-21 20:18:45 -0700184
185 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
186 const char* fragmentPos = fsBuilder->fragmentPosition();
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000187 // 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.orgcb3672e2014-02-21 22:41:56 +0000198 //
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.org4355f212014-03-12 15:32:50 +0000202 switch (crre.getCircularCornerFlags()) {
203 case CircularRRectEffect::kAll_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700204 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.orgcb3672e2014-02-21 22:41:56 +0000208 radiusPlusHalfName);
209 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000210 case CircularRRectEffect::kTopLeft_CornerFlag:
joshualitt30ba4362014-08-21 20:18:45 -0700211 fsBuilder->codeAppendf("\t\tvec2 dxy = max(%s.xy - %s.xy, 0.0);\n",
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000212 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700213 fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000214 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700215 fsBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000216 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700217 fsBuilder->codeAppendf("\t\tfloat alpha = bottomAlpha * rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000218 radiusPlusHalfName);
219 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000220 case CircularRRectEffect::kTopRight_CornerFlag:
joshualitt30ba4362014-08-21 20:18:45 -0700221 fsBuilder->codeAppendf("\t\tvec2 dxy = max(vec2(%s.x - %s.z, %s.y - %s.y), 0.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000222 fragmentPos, rectName, rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700223 fsBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000224 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700225 fsBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000226 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700227 fsBuilder->codeAppendf("\t\tfloat alpha = bottomAlpha * leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000228 radiusPlusHalfName);
229 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000230 case CircularRRectEffect::kBottomRight_CornerFlag:
joshualitt30ba4362014-08-21 20:18:45 -0700231 fsBuilder->codeAppendf("\t\tvec2 dxy = max(%s.xy - %s.zw, 0.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000232 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700233 fsBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000234 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700235 fsBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000236 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700237 fsBuilder->codeAppendf("\t\tfloat alpha = topAlpha * leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000238 radiusPlusHalfName);
239 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000240 case CircularRRectEffect::kBottomLeft_CornerFlag:
joshualitt30ba4362014-08-21 20:18:45 -0700241 fsBuilder->codeAppendf("\t\tvec2 dxy = max(vec2(%s.x - %s.x, %s.y - %s.w), 0.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000242 rectName, fragmentPos, fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700243 fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000244 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700245 fsBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000246 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700247 fsBuilder->codeAppendf("\t\tfloat alpha = topAlpha * rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000248 radiusPlusHalfName);
249 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000250 case CircularRRectEffect::kLeft_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700251 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.orgcb3672e2014-02-21 22:41:56 +0000255 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700256 fsBuilder->codeAppendf("\t\tfloat alpha = rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000257 radiusPlusHalfName);
258 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000259 case CircularRRectEffect::kTop_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700260 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.orgcb3672e2014-02-21 22:41:56 +0000264 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700265 fsBuilder->codeAppendf("\t\tfloat alpha = bottomAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000266 radiusPlusHalfName);
267 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000268 case CircularRRectEffect::kRight_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700269 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.orgcb3672e2014-02-21 22:41:56 +0000273 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700274 fsBuilder->codeAppendf("\t\tfloat alpha = leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000275 radiusPlusHalfName);
276 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000277 case CircularRRectEffect::kBottom_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700278 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.orgcb3672e2014-02-21 22:41:56 +0000282 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700283 fsBuilder->codeAppendf("\t\tfloat alpha = topAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000284 radiusPlusHalfName);
285 break;
286 }
skia.committer@gmail.com06acb582014-03-06 03:02:32 +0000287
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000288 if (kInverseFillAA_GrEffectEdgeType == crre.getEdgeType()) {
joshualitt30ba4362014-08-21 20:18:45 -0700289 fsBuilder->codeAppend("\t\talpha = 1.0 - alpha;\n");
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000290 }
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000291
joshualitt30ba4362014-08-21 20:18:45 -0700292 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor,
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000293 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
294}
295
joshualitt49586be2014-09-16 08:21:41 -0700296void GLCircularRRectEffect::GenKey(const GrEffect& effect, const GrGLCaps&,
bsalomon63e99f72014-07-21 08:03:14 -0700297 GrEffectKeyBuilder* b) {
joshualitt49586be2014-09-16 08:21:41 -0700298 const CircularRRectEffect& crre = effect.cast<CircularRRectEffect>();
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000299 GR_STATIC_ASSERT(kGrEffectEdgeTypeCnt <= 8);
bsalomon63e99f72014-07-21 08:03:14 -0700300 b->add32((crre.getCircularCornerFlags() << 3) | crre.getEdgeType());
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000301}
302
kkinnunen7510b222014-07-30 00:04:16 -0700303void GLCircularRRectEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt49586be2014-09-16 08:21:41 -0700304 const GrEffect& effect) {
305 const CircularRRectEffect& crre = effect.cast<CircularRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000306 const SkRRect& rrect = crre.getRRect();
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000307 if (rrect != fPrevRRect) {
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000308 SkRect rect = rrect.getBounds();
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000309 SkScalar radius = 0;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000310 switch (crre.getCircularCornerFlags()) {
311 case CircularRRectEffect::kAll_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000312 SkASSERT(rrect.isSimpleCircular());
313 radius = rrect.getSimpleRadii().fX;
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000314 SkASSERT(radius >= kRadiusMin);
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000315 rect.inset(radius, radius);
316 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000317 case CircularRRectEffect::kTopLeft_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000318 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.org4355f212014-03-12 15:32:50 +0000324 case CircularRRectEffect::kTopRight_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000325 radius = rrect.radii(SkRRect::kUpperRight_Corner).fX;
bsalomon@google.comde9f2512014-03-11 17:09:17 +0000326 rect.fLeft -= 0.5f;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000327 rect.fTop += radius;
328 rect.fRight -= radius;
329 rect.fBottom += 0.5f;
330 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000331 case CircularRRectEffect::kBottomRight_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000332 radius = rrect.radii(SkRRect::kLowerRight_Corner).fX;
bsalomon@google.comde9f2512014-03-11 17:09:17 +0000333 rect.fLeft -= 0.5f;
334 rect.fTop -= 0.5f;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000335 rect.fRight -= radius;
336 rect.fBottom -= radius;
337 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000338 case CircularRRectEffect::kBottomLeft_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000339 radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX;
340 rect.fLeft += radius;
bsalomon@google.comde9f2512014-03-11 17:09:17 +0000341 rect.fTop -= 0.5f;
342 rect.fRight += 0.5f;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000343 rect.fBottom -= radius;
344 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000345 case CircularRRectEffect::kLeft_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000346 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.org4355f212014-03-12 15:32:50 +0000352 case CircularRRectEffect::kTop_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000353 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.org4355f212014-03-12 15:32:50 +0000359 case CircularRRectEffect::kRight_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000360 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.org4355f212014-03-12 15:32:50 +0000366 case CircularRRectEffect::kBottom_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000367 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.orgdd584222014-03-10 16:08:22 +0000373 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000374 SkFAIL("Should have been one of the above cases.");
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000375 }
kkinnunen7510b222014-07-30 00:04:16 -0700376 pdman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
377 pdman.set1f(fRadiusPlusHalfUniform, radius + 0.5f);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000378 fPrevRRect = rrect;
379 }
380}
381
382//////////////////////////////////////////////////////////////////////////////
383
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000384class GLEllipticalRRectEffect;
385
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000386class EllipticalRRectEffect : public GrEffect {
387public:
bsalomon83d081a2014-07-08 09:56:10 -0700388 static GrEffect* Create(GrEffectEdgeType, const SkRRect&);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000389
390 virtual ~EllipticalRRectEffect() {};
391 static const char* Name() { return "EllipticalRRect"; }
392
393 const SkRRect& getRRect() const { return fRRect; }
394
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000395
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000396 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
404private:
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000405 EllipticalRRectEffect(GrEffectEdgeType, const SkRRect&);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000406
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
bsalomon83d081a2014-07-08 09:56:10 -0700417GrEffect* EllipticalRRectEffect::Create(GrEffectEdgeType edgeType, const SkRRect& rrect) {
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000418 if (kFillAA_GrEffectEdgeType != edgeType && kInverseFillAA_GrEffectEdgeType != edgeType) {
419 return NULL;
420 }
bsalomon55fad7a2014-07-08 07:34:20 -0700421 return SkNEW_ARGS(EllipticalRRectEffect, (edgeType, rrect));
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000422}
423
424void EllipticalRRectEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
425 *validFlags = 0;
426}
427
428const GrBackendEffectFactory& EllipticalRRectEffect::getFactory() const {
429 return GrTBackendEffectFactory<EllipticalRRectEffect>::getInstance();
430}
431
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000432EllipticalRRectEffect::EllipticalRRectEffect(GrEffectEdgeType edgeType, const SkRRect& rrect)
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000433 : fRRect(rrect)
434 , fEdgeType(edgeType){
435 this->setWillReadFragmentPosition();
436}
437
438bool EllipticalRRectEffect::onIsEqual(const GrEffect& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700439 const EllipticalRRectEffect& erre = other.cast<EllipticalRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000440 return fEdgeType == erre.fEdgeType && fRRect == erre.fRRect;
441}
442
443//////////////////////////////////////////////////////////////////////////////
444
445GR_DEFINE_EFFECT_TEST(EllipticalRRectEffect);
446
bsalomon83d081a2014-07-08 09:56:10 -0700447GrEffect* EllipticalRRectEffect::TestCreate(SkRandom* random,
448 GrContext*,
449 const GrDrawTargetCaps& caps,
450 GrTexture*[]) {
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000451 SkScalar w = random->nextRangeScalar(20.f, 1000.f);
452 SkScalar h = random->nextRangeScalar(20.f, 1000.f);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000453 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.org4355f212014-03-12 15:32:50 +0000460 SkRRect rrect;
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000461 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 }
bsalomon83d081a2014-07-08 09:56:10 -0700477 GrEffect* effect;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000478 do {
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000479 GrEffectEdgeType et = (GrEffectEdgeType)random->nextULessThan(kGrEffectEdgeTypeCnt);
480 effect = GrRRectEffect::Create(et, rrect);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000481 } while (NULL == effect);
482 return effect;
483}
484
485//////////////////////////////////////////////////////////////////////////////
486
487class GLEllipticalRRectEffect : public GrGLEffect {
488public:
joshualitt49586be2014-09-16 08:21:41 -0700489 GLEllipticalRRectEffect(const GrBackendEffectFactory&, const GrEffect&);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000490
joshualitt30ba4362014-08-21 20:18:45 -0700491 virtual void emitCode(GrGLProgramBuilder* builder,
joshualitt49586be2014-09-16 08:21:41 -0700492 const GrEffect& effect,
bsalomon63e99f72014-07-21 08:03:14 -0700493 const GrEffectKey& key,
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000494 const char* outputColor,
495 const char* inputColor,
496 const TransformedCoordsArray&,
497 const TextureSamplerArray&) SK_OVERRIDE;
498
joshualitt49586be2014-09-16 08:21:41 -0700499 static inline void GenKey(const GrEffect&, const GrGLCaps&, GrEffectKeyBuilder*);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000500
joshualitt49586be2014-09-16 08:21:41 -0700501 virtual void setData(const GrGLProgramDataManager&, const GrEffect&) SK_OVERRIDE;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000502
503private:
kkinnunen7510b222014-07-30 00:04:16 -0700504 GrGLProgramDataManager::UniformHandle fInnerRectUniform;
505 GrGLProgramDataManager::UniformHandle fInvRadiiSqdUniform;
506 SkRRect fPrevRRect;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000507 typedef GrGLEffect INHERITED;
508};
509
510GLEllipticalRRectEffect::GLEllipticalRRectEffect(const GrBackendEffectFactory& factory,
joshualitt49586be2014-09-16 08:21:41 -0700511 const GrEffect& effect)
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000512 : INHERITED (factory) {
513 fPrevRRect.setEmpty();
514}
515
joshualitt30ba4362014-08-21 20:18:45 -0700516void GLEllipticalRRectEffect::emitCode(GrGLProgramBuilder* builder,
joshualitt49586be2014-09-16 08:21:41 -0700517 const GrEffect& effect,
bsalomon63e99f72014-07-21 08:03:14 -0700518 const GrEffectKey& key,
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000519 const char* outputColor,
520 const char* inputColor,
521 const TransformedCoordsArray&,
522 const TextureSamplerArray& samplers) {
joshualitt49586be2014-09-16 08:21:41 -0700523 const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000524 const char *rectName;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000525 // The inner rect is the rrect bounds inset by the x/y radii
joshualitt30ba4362014-08-21 20:18:45 -0700526 fInnerRectUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000527 kVec4f_GrSLType,
528 "innerRect",
529 &rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700530
531 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
532 const char* fragmentPos = fsBuilder->fragmentPosition();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000533 // 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.
joshualitt30ba4362014-08-21 20:18:45 -0700544 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.org9615d5f2014-03-20 21:39:03 +0000546 switch (erre.getRRect().getType()) {
547 case SkRRect::kSimple_Type: {
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000548 const char *invRadiiXYSqdName;
joshualitt30ba4362014-08-21 20:18:45 -0700549 fInvRadiiSqdUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000550 kVec2f_GrSLType,
551 "invRadiiXY",
552 &invRadiiXYSqdName);
joshualitt30ba4362014-08-21 20:18:45 -0700553 fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000554 // Z is the x/y offsets divided by squared radii.
joshualitt30ba4362014-08-21 20:18:45 -0700555 fsBuilder->codeAppendf("\t\tvec2 Z = dxy * %s;\n", invRadiiXYSqdName);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000556 break;
557 }
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000558 case SkRRect::kNinePatch_Type: {
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000559 const char *invRadiiLTRBSqdName;
joshualitt30ba4362014-08-21 20:18:45 -0700560 fInvRadiiSqdUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000561 kVec4f_GrSLType,
562 "invRadiiLTRB",
563 &invRadiiLTRBSqdName);
joshualitt30ba4362014-08-21 20:18:45 -0700564 fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000565 // 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.)
joshualitt30ba4362014-08-21 20:18:45 -0700568 fsBuilder->codeAppendf("\t\tvec2 Z = max(max(dxy0 * %s.xy, dxy1 * %s.zw), 0.0);\n",
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000569 invRadiiLTRBSqdName, invRadiiLTRBSqdName);
570 break;
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000571 }
572 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000573 SkFAIL("RRect should always be simple or nine-patch.");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000574 }
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000575 // implicit is the evaluation of (x/a)^2 + (y/b)^2 - 1.
joshualitt30ba4362014-08-21 20:18:45 -0700576 fsBuilder->codeAppend("\t\tfloat implicit = dot(Z, dxy) - 1.0;\n");
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000577 // grad_dot is the squared length of the gradient of the implicit.
joshualitt30ba4362014-08-21 20:18:45 -0700578 fsBuilder->codeAppendf("\t\tfloat grad_dot = 4.0 * dot(Z, Z);\n");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000579 // avoid calling inversesqrt on zero.
joshualitt30ba4362014-08-21 20:18:45 -0700580 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.org4355f212014-03-12 15:32:50 +0000582
583 if (kFillAA_GrEffectEdgeType == erre.getEdgeType()) {
joshualitt30ba4362014-08-21 20:18:45 -0700584 fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 - approx_dist, 0.0, 1.0);\n");
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000585 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700586 fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 + approx_dist, 0.0, 1.0);\n");
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000587 }
588
joshualitt30ba4362014-08-21 20:18:45 -0700589 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor,
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000590 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
591}
592
joshualitt49586be2014-09-16 08:21:41 -0700593void GLEllipticalRRectEffect::GenKey(const GrEffect& effect, const GrGLCaps&,
bsalomon63e99f72014-07-21 08:03:14 -0700594 GrEffectKeyBuilder* b) {
joshualitt49586be2014-09-16 08:21:41 -0700595 const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>();
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000596 GR_STATIC_ASSERT(kLast_GrEffectEdgeType < (1 << 3));
bsalomon63e99f72014-07-21 08:03:14 -0700597 b->add32(erre.getRRect().getType() | erre.getEdgeType() << 3);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000598}
599
kkinnunen7510b222014-07-30 00:04:16 -0700600void GLEllipticalRRectEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt49586be2014-09-16 08:21:41 -0700601 const GrEffect& effect) {
602 const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000603 const SkRRect& rrect = erre.getRRect();
604 if (rrect != fPrevRRect) {
605 SkRect rect = rrect.getBounds();
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000606 const SkVector& r0 = rrect.radii(SkRRect::kUpperLeft_Corner);
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000607 SkASSERT(r0.fX >= kRadiusMin);
608 SkASSERT(r0.fY >= kRadiusMin);
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000609 switch (erre.getRRect().getType()) {
610 case SkRRect::kSimple_Type:
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000611 rect.inset(r0.fX, r0.fY);
kkinnunen7510b222014-07-30 00:04:16 -0700612 pdman.set2f(fInvRadiiSqdUniform, 1.f / (r0.fX * r0.fX),
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000613 1.f / (r0.fY * r0.fY));
614 break;
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000615 case SkRRect::kNinePatch_Type: {
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000616 const SkVector& r1 = rrect.radii(SkRRect::kLowerRight_Corner);
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000617 SkASSERT(r1.fX >= kRadiusMin);
618 SkASSERT(r1.fY >= kRadiusMin);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000619 rect.fLeft += r0.fX;
620 rect.fTop += r0.fY;
621 rect.fRight -= r1.fX;
622 rect.fBottom -= r1.fY;
kkinnunen7510b222014-07-30 00:04:16 -0700623 pdman.set4f(fInvRadiiSqdUniform, 1.f / (r0.fX * r0.fX),
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000624 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.org9615d5f2014-03-20 21:39:03 +0000629 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000630 SkFAIL("RRect should always be simple or nine-patch.");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000631 }
kkinnunen7510b222014-07-30 00:04:16 -0700632 pdman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000633 fPrevRRect = rrect;
634 }
635}
636
637//////////////////////////////////////////////////////////////////////////////
638
bsalomon83d081a2014-07-08 09:56:10 -0700639GrEffect* GrRRectEffect::Create(GrEffectEdgeType edgeType, const SkRRect& rrect) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000640 if (rrect.isRect()) {
641 return GrConvexPolyEffect::Create(edgeType, rrect.getBounds());
642 }
643
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000644 if (rrect.isOval()) {
645 return GrOvalEffect::Create(edgeType, rrect.getBounds());
646 }
647
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000648 if (rrect.isSimple()) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000649 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.org4355f212014-03-12 15:32:50 +0000654 if (rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY) {
skia.committer@gmail.com6e4eb212014-03-25 03:02:32 +0000655 return CircularRRectEffect::Create(edgeType, CircularRRectEffect::kAll_CornerFlags,
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000656 rrect);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000657 } else {
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000658 return EllipticalRRectEffect::Create(edgeType, rrect);
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000659 }
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000660 }
661
662 if (rrect.isComplex() || rrect.isNinePatch()) {
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000663 // Check for the "tab" cases - two adjacent circular corners and two square corners.
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000664 SkScalar circularRadius = 0;
665 uint32_t cornerFlags = 0;
666
667 SkVector radii[4];
668 bool squashedRadii = false;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000669 for (int c = 0; c < 4; ++c) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000670 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.orgcb3672e2014-02-21 22:41:56 +0000674 continue;
675 }
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000676 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.com44a435b2014-03-13 19:20:32 +0000682 cornerFlags = ~0U;
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000683 break;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000684 }
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000685 if (!cornerFlags) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000686 circularRadius = radii[c].fX;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000687 cornerFlags = 1 << c;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000688 } else {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000689 if (radii[c].fX != circularRadius) {
bsalomon@google.com44a435b2014-03-13 19:20:32 +0000690 cornerFlags = ~0U;
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000691 break;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000692 }
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000693 cornerFlags |= 1 << c;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000694 }
695 }
696
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000697 switch (cornerFlags) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000698 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.org4355f212014-03-12 15:32:50 +0000702 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.org2a8be902014-03-24 19:24:59 +0000709 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.orgfa5edbe2014-03-13 18:01:05 +0000725 if (rrect.isNinePatch()) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000726 return EllipticalRRectEffect::Create(edgeType, rrect);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000727 }
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000728 return NULL;
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000729 }
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000730 }
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000731 }
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000732
733 return NULL;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000734}