blob: 3e6473b80af099a1b0c032d702432fed24425384 [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.orgcb3672e2014-02-21 22:41:56 +000025 /// The types of circular corner rrects supported
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000026 enum RRectType {
27 kCircleCorner_RRectType, //<! All four corners have the same circular radius.
28 kLeftCircleTab_RRectType, //<! The left side has circular corners, the right is a rect.
29 kTopCircleTab_RRectType, //<! etc
30 kRightCircleTab_RRectType,
31 kBottomCircleTab_RRectType,
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +000032 };
33
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000034 static GrEffectRef* Create(GrEffectEdgeType, RRectType, const SkRRect&);
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000035
36 virtual ~RRectEffect() {};
37 static const char* Name() { return "RRect"; }
38
39 const SkRRect& getRRect() const { return fRRect; }
40
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000041 RRectType getType() const { return fRRectType; }
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000042
43 GrEffectEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +000044
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000045 typedef GLRRectEffect GLEffect;
46
47 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
48
49 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
50
51private:
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000052 RRectEffect(GrEffectEdgeType, RRectType, const SkRRect&);
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000053
54 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE;
55
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000056 SkRRect fRRect;
57 GrEffectEdgeType fEdgeType;
58 RRectType fRRectType;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000059
60 GR_DECLARE_EFFECT_TEST;
61
62 typedef GrEffect INHERITED;
63};
64
65const SkScalar RRectEffect::kRadiusMin = 0.5f;
66
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000067GrEffectRef* RRectEffect::Create(GrEffectEdgeType edgeType,
68 RRectType rrType,
69 const SkRRect& rrect) {
70 SkASSERT(kFillAA_GrEffectEdgeType == edgeType || kInverseFillBW_GrEffectEdgeType == edgeType);
71 return CreateEffectRef(AutoEffectUnref(SkNEW_ARGS(RRectEffect, (edgeType, rrType, rrect))));
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000072}
73
74void RRectEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
75 *validFlags = 0;
76}
77
78const GrBackendEffectFactory& RRectEffect::getFactory() const {
79 return GrTBackendEffectFactory<RRectEffect>::getInstance();
80}
81
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000082RRectEffect::RRectEffect(GrEffectEdgeType edgeType, RRectType rrType, const SkRRect& rrect)
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +000083 : fRRect(rrect)
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000084 , fEdgeType(edgeType)
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +000085 , fRRectType(rrType) {
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000086 this->setWillReadFragmentPosition();
87}
88
89bool RRectEffect::onIsEqual(const GrEffect& other) const {
90 const RRectEffect& rre = CastEffect<RRectEffect>(other);
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +000091 // type is derived from fRRect, so no need to check it.
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000092 return fEdgeType == rre.fEdgeType && fRRect == rre.fRRect;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000093}
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +000094
95//////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000096
97GR_DEFINE_EFFECT_TEST(RRectEffect);
98
99GrEffectRef* RRectEffect::TestCreate(SkRandom* random,
100 GrContext*,
101 const GrDrawTargetCaps& caps,
102 GrTexture*[]) {
103 SkScalar w = random->nextRangeScalar(20.f, 1000.f);
104 SkScalar h = random->nextRangeScalar(20.f, 1000.f);
105 SkScalar r = random->nextRangeF(kRadiusMin, 9.f);
106 SkRRect rrect;
107 rrect.setRectXY(SkRect::MakeWH(w, h), r, r);
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000108 GrEffectRef* effect;
109 do {
110 GrEffectEdgeType et = (GrEffectEdgeType)random->nextULessThan(kGrEffectEdgeTypeCnt);
111 effect = GrRRectEffect::Create(et, rrect);
112 } while (NULL == effect);
113 return effect;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000114}
115
116//////////////////////////////////////////////////////////////////////////////
117
118class GLRRectEffect : public GrGLEffect {
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000119public:
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000120 GLRRectEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000121
122 virtual void emitCode(GrGLShaderBuilder* builder,
123 const GrDrawEffect& drawEffect,
124 EffectKey key,
125 const char* outputColor,
126 const char* inputColor,
127 const TransformedCoordsArray&,
128 const TextureSamplerArray&) SK_OVERRIDE;
129
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000130 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000131
132 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
133
134private:
135 GrGLUniformManager::UniformHandle fInnerRectUniform;
136 GrGLUniformManager::UniformHandle fRadiusPlusHalfUniform;
137 SkRRect fPrevRRect;
138 typedef GrGLEffect INHERITED;
139};
140
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000141GLRRectEffect::GLRRectEffect(const GrBackendEffectFactory& factory,
142 const GrDrawEffect& drawEffect)
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000143 : INHERITED (factory) {
144 fPrevRRect.setEmpty();
145}
146
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000147void GLRRectEffect::emitCode(GrGLShaderBuilder* builder,
148 const GrDrawEffect& drawEffect,
149 EffectKey key,
150 const char* outputColor,
151 const char* inputColor,
152 const TransformedCoordsArray&,
153 const TextureSamplerArray& samplers) {
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000154 const RRectEffect& rre = drawEffect.castEffect<RRectEffect>();
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000155 const char *rectName;
156 const char *radiusPlusHalfName;
157 // The inner rect is the rrect bounds inset by the radius. Its top, left, right, and bottom
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000158 // edges correspond to components x, y, z, and w, respectively. When one side of the rrect has
159 // rectangular corners, that side's value corresponds to the rect edge's value outset by half a
160 // pixel.
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000161 fInnerRectUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
162 kVec4f_GrSLType,
163 "innerRect",
164 &rectName);
165 fRadiusPlusHalfUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
166 kFloat_GrSLType,
167 "radiusPlusHalf",
168 &radiusPlusHalfName);
169 const char* fragmentPos = builder->fragmentPosition();
170 // At each quarter-circle corner we compute a vector that is the offset of the fragment position
171 // from the circle center. The vector is pinned in x and y to be in the quarter-plane relevant
172 // to that corner. This means that points near the interior near the rrect top edge will have
173 // a vector that points straight up for both the TL left and TR corners. Computing an
174 // alpha from this vector at either the TR or TL corner will give the correct result. Similarly,
175 // fragments near the other three edges will get the correct AA. Fragments in the interior of
176 // the rrect will have a (0,0) vector at all four corners. So long as the radius > 0.5 they will
177 // correctly produce an alpha value of 1 at all four corners. We take the min of all the alphas.
178 // The code below is a simplified version of the above that performs maxs on the vector
179 // components before computing distances and alpha values so that only one distance computation
180 // need be computed to determine the min alpha.
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000181 //
182 // For the cases where one half of the rrect is rectangular we drop one of the x or y
183 // computations, compute a separate rect edge alpha for the rect side, and mul the two computed
184 // alphas together.
185 switch (rre.getType()) {
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000186 case RRectEffect::kCircleCorner_RRectType:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000187 builder->fsCodeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
188 builder->fsCodeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
189 builder->fsCodeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n");
190 builder->fsCodeAppendf("\t\tfloat alpha = clamp(%s - length(dxy), 0.0, 1.0);\n",
191 radiusPlusHalfName);
192 break;
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000193 case RRectEffect::kLeftCircleTab_RRectType:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000194 builder->fsCodeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
195 builder->fsCodeAppendf("\t\tfloat dy1 = %s.y - %s.w;\n", fragmentPos, rectName);
196 builder->fsCodeAppend("\t\tvec2 dxy = max(vec2(dxy0.x, max(dxy0.y, dy1)), 0.0);\n");
197 builder->fsCodeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n",
198 rectName, fragmentPos);
199 builder->fsCodeAppendf("\t\tfloat alpha = rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
200 radiusPlusHalfName);
201 break;
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000202 case RRectEffect::kTopCircleTab_RRectType:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000203 builder->fsCodeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
204 builder->fsCodeAppendf("\t\tfloat dx1 = %s.x - %s.z;\n", fragmentPos, rectName);
205 builder->fsCodeAppend("\t\tvec2 dxy = max(vec2(max(dxy0.x, dx1), dxy0.y), 0.0);\n");
206 builder->fsCodeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n",
207 rectName, fragmentPos);
208 builder->fsCodeAppendf("\t\tfloat alpha = bottomAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
209 radiusPlusHalfName);
210 break;
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000211 case RRectEffect::kRightCircleTab_RRectType:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000212 builder->fsCodeAppendf("\t\tfloat dy0 = %s.y - %s.y;\n", rectName, fragmentPos);
213 builder->fsCodeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
214 builder->fsCodeAppend("\t\tvec2 dxy = max(vec2(dxy1.x, max(dy0, dxy1.y)), 0.0);\n");
215 builder->fsCodeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n",
216 fragmentPos, rectName);
217 builder->fsCodeAppendf("\t\tfloat alpha = leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
218 radiusPlusHalfName);
219 break;
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000220 case RRectEffect::kBottomCircleTab_RRectType:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000221 builder->fsCodeAppendf("\t\tfloat dx0 = %s.x - %s.x;\n", rectName, fragmentPos);
222 builder->fsCodeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
223 builder->fsCodeAppend("\t\tvec2 dxy = max(vec2(max(dx0, dxy1.x), dxy1.y), 0.0);\n");
224 builder->fsCodeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n",
225 fragmentPos, rectName);
226 builder->fsCodeAppendf("\t\tfloat alpha = topAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
227 radiusPlusHalfName);
228 break;
229 }
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000230
231 if (kInverseFillBW_GrEffectEdgeType == rre.getEdgeType()) {
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000232 builder->fsCodeAppend("\t\talpha = 1.0 - alpha;\n");
233 }
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000234
235 builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor,
236 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
237}
238
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000239GrGLEffect::EffectKey GLRRectEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
240 const RRectEffect& rre = drawEffect.castEffect<RRectEffect>();
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000241 GR_STATIC_ASSERT(kGrEffectEdgeTypeCnt <= 8);
242 return (rre.getType() << 3) | rre.getEdgeType();
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000243}
244
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000245void GLRRectEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) {
246 const RRectEffect& rre = drawEffect.castEffect<RRectEffect>();
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000247 const SkRRect& rrect = rre.getRRect();
248 if (rrect != fPrevRRect) {
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000249 SkRect rect = rrect.getBounds();
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000250 SkScalar radius = 0;
251 switch (rre.getType()) {
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000252 case RRectEffect::kCircleCorner_RRectType:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000253 SkASSERT(rrect.isSimpleCircular());
254 radius = rrect.getSimpleRadii().fX;
255 SkASSERT(radius >= RRectEffect::kRadiusMin);
256 rect.inset(radius, radius);
257 break;
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000258 case RRectEffect::kLeftCircleTab_RRectType:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000259 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
260 rect.fLeft += radius;
261 rect.fTop += radius;
262 rect.fRight += 0.5f;
263 rect.fBottom -= radius;
264 break;
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000265 case RRectEffect::kTopCircleTab_RRectType:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000266 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
267 rect.fLeft += radius;
268 rect.fTop += radius;
269 rect.fRight -= radius;
270 rect.fBottom += 0.5f;
271 break;
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000272 case RRectEffect::kRightCircleTab_RRectType:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000273 radius = rrect.radii(SkRRect::kUpperRight_Corner).fX;
274 rect.fLeft -= 0.5f;
275 rect.fTop += radius;
276 rect.fRight -= radius;
277 rect.fBottom -= radius;
278 break;
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000279 case RRectEffect::kBottomCircleTab_RRectType:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000280 radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX;
281 rect.fLeft += radius;
282 rect.fTop -= 0.5f;
283 rect.fRight -= radius;
284 rect.fBottom -= radius;
285 break;
286 }
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000287 uman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
288 uman.set1f(fRadiusPlusHalfUniform, radius + 0.5f);
289 fPrevRRect = rrect;
290 }
291}
292
293//////////////////////////////////////////////////////////////////////////////
294
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000295GrEffectRef* GrRRectEffect::Create(GrEffectEdgeType edgeType, const SkRRect& rrect) {
296 if (kFillAA_GrEffectEdgeType != edgeType && kInverseFillBW_GrEffectEdgeType != edgeType) {
297 return NULL;
298 }
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000299 RRectEffect::RRectType rrtype;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000300 if (rrect.isSimpleCircular()) {
301 if (rrect.getSimpleRadii().fX < RRectEffect::kRadiusMin) {
302 return NULL;
303 }
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000304 rrtype = RRectEffect::kCircleCorner_RRectType;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000305 } else if (rrect.isComplex()) {
306 // Check for the "tab" cases - two adjacent circular corners and two square corners.
307 SkScalar radius = 0;
308 int circleCornerBitfield = 0;
309 for (int c = 0; c < 4; ++c) {
310 const SkVector& r = rrect.radii((SkRRect::Corner)c);
311 SkASSERT((0 == r.fX) == (0 == r.fY));
312 if (0 == r.fX) {
313 continue;
314 }
315 if (r.fX != r.fY) {
316 return NULL;
317 }
318 if (!circleCornerBitfield) {
319 radius = r.fX;
320 if (radius < RRectEffect::kRadiusMin) {
321 return NULL;
322 }
323 circleCornerBitfield = 1 << c;
324 } else {
325 if (r.fX != radius) {
326 return NULL;
327 }
328 circleCornerBitfield |= 1 << c;
329 }
330 }
331
332 GR_STATIC_ASSERT(SkRRect::kUpperLeft_Corner == 0);
333 GR_STATIC_ASSERT(SkRRect::kUpperRight_Corner == 1);
334 GR_STATIC_ASSERT(SkRRect::kLowerRight_Corner == 2);
335 GR_STATIC_ASSERT(SkRRect::kLowerLeft_Corner == 3);
336 switch (circleCornerBitfield) {
337 case 3:
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000338 rrtype = RRectEffect::kTopCircleTab_RRectType;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000339 break;
340 case 6:
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000341 rrtype = RRectEffect::kRightCircleTab_RRectType;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000342 break;
343 case 9:
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000344 rrtype = RRectEffect::kLeftCircleTab_RRectType;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000345 break;
346 case 12:
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000347 rrtype = RRectEffect::kBottomCircleTab_RRectType;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000348 break;
349 default:
350 return NULL;
351 }
352 } else {
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000353 return NULL;
354 }
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000355 return RRectEffect::Create(edgeType, rrtype, rrect);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000356}