blob: b378b1025bce55cdc0bb3ca8f78dd532f915f06b [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
joshualittb0a8a372014-09-23 09:50:21 -070011#include "gl/GrGLProcessor.h"
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +000012#include "gl/GrGLSL.h"
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +000013#include "GrConvexPolyEffect.h"
egdaniel605dd0f2014-11-12 08:35:25 -080014#include "GrInvariantOutput.h"
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000015#include "GrOvalEffect.h"
joshualittb0a8a372014-09-23 09:50:21 -070016#include "GrTBackendProcessorFactory.h"
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +000017
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000018#include "SkRRect.h"
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +000019
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +000020// The effects defined here only handle rrect radii >= kRadiusMin.
21static const SkScalar kRadiusMin = SK_ScalarHalf;
22
23//////////////////////////////////////////////////////////////////////////////
24
commit-bot@chromium.org4355f212014-03-12 15:32:50 +000025class GLCircularRRectEffect;
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000026
joshualittb0a8a372014-09-23 09:50:21 -070027class CircularRRectEffect : public GrFragmentProcessor {
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000028public:
skia.committer@gmail.comf1f66c02014-03-05 03:02:06 +000029
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000030 enum CornerFlags {
31 kTopLeft_CornerFlag = (1 << SkRRect::kUpperLeft_Corner),
32 kTopRight_CornerFlag = (1 << SkRRect::kUpperRight_Corner),
33 kBottomRight_CornerFlag = (1 << SkRRect::kLowerRight_Corner),
34 kBottomLeft_CornerFlag = (1 << SkRRect::kLowerLeft_Corner),
35
36 kLeft_CornerFlags = kTopLeft_CornerFlag | kBottomLeft_CornerFlag,
37 kTop_CornerFlags = kTopLeft_CornerFlag | kTopRight_CornerFlag,
38 kRight_CornerFlags = kTopRight_CornerFlag | kBottomRight_CornerFlag,
39 kBottom_CornerFlags = kBottomLeft_CornerFlag | kBottomRight_CornerFlag,
40
41 kAll_CornerFlags = kTopLeft_CornerFlag | kTopRight_CornerFlag |
42 kBottomLeft_CornerFlag | kBottomRight_CornerFlag,
43
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +000044 kNone_CornerFlags = 0
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +000045 };
46
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000047 // The flags are used to indicate which corners are circluar (unflagged corners are assumed to
48 // be square).
joshualittb0a8a372014-09-23 09:50:21 -070049 static GrFragmentProcessor* Create(GrPrimitiveEdgeType, uint32_t circularCornerFlags,
50 const SkRRect&);
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000051
commit-bot@chromium.org4355f212014-03-12 15:32:50 +000052 virtual ~CircularRRectEffect() {};
53 static const char* Name() { return "CircularRRect"; }
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000054
55 const SkRRect& getRRect() const { return fRRect; }
56
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000057 uint32_t getCircularCornerFlags() const { return fCircularCornerFlags; }
skia.committer@gmail.com06acb582014-03-06 03:02:32 +000058
joshualittb0a8a372014-09-23 09:50:21 -070059 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +000060
joshualittb0a8a372014-09-23 09:50:21 -070061 typedef GLCircularRRectEffect GLProcessor;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000062
joshualittb0a8a372014-09-23 09:50:21 -070063 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERRIDE;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000064
65private:
joshualittb0a8a372014-09-23 09:50:21 -070066 CircularRRectEffect(GrPrimitiveEdgeType, uint32_t circularCornerFlags, const SkRRect&);
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000067
bsalomon0e08fc12014-10-15 08:19:04 -070068 virtual bool onIsEqual(const GrFragmentProcessor& other) const SK_OVERRIDE;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000069
egdaniel605dd0f2014-11-12 08:35:25 -080070 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE;
egdaniel1a8ecdf2014-10-03 06:24:12 -070071
joshualittb0a8a372014-09-23 09:50:21 -070072 SkRRect fRRect;
73 GrPrimitiveEdgeType fEdgeType;
74 uint32_t fCircularCornerFlags;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000075
joshualittb0a8a372014-09-23 09:50:21 -070076 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000077
joshualittb0a8a372014-09-23 09:50:21 -070078 typedef GrFragmentProcessor INHERITED;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000079};
80
joshualittb0a8a372014-09-23 09:50:21 -070081GrFragmentProcessor* CircularRRectEffect::Create(GrPrimitiveEdgeType edgeType,
82 uint32_t circularCornerFlags,
83 const SkRRect& rrect) {
84 if (kFillAA_GrProcessorEdgeType != edgeType && kInverseFillAA_GrProcessorEdgeType != edgeType) {
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000085 return NULL;
86 }
bsalomon55fad7a2014-07-08 07:34:20 -070087 return SkNEW_ARGS(CircularRRectEffect, (edgeType, circularCornerFlags, rrect));
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000088}
89
egdaniel605dd0f2014-11-12 08:35:25 -080090void CircularRRectEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
egdanielccb2e382014-10-13 12:53:46 -070091 inout->mulByUnknownAlpha();
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000092}
93
joshualittb0a8a372014-09-23 09:50:21 -070094const GrBackendFragmentProcessorFactory& CircularRRectEffect::getFactory() const {
95 return GrTBackendFragmentProcessorFactory<CircularRRectEffect>::getInstance();
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000096}
97
joshualittb0a8a372014-09-23 09:50:21 -070098CircularRRectEffect::CircularRRectEffect(GrPrimitiveEdgeType edgeType, uint32_t circularCornerFlags,
99 const SkRRect& rrect)
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000100 : fRRect(rrect)
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000101 , fEdgeType(edgeType)
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000102 , fCircularCornerFlags(circularCornerFlags) {
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000103 this->setWillReadFragmentPosition();
104}
105
bsalomon0e08fc12014-10-15 08:19:04 -0700106bool CircularRRectEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700107 const CircularRRectEffect& crre = other.cast<CircularRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000108 // The corner flags are derived from fRRect, so no need to check them.
109 return fEdgeType == crre.fEdgeType && fRRect == crre.fRRect;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000110}
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000111
112//////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000113
joshualittb0a8a372014-09-23 09:50:21 -0700114GR_DEFINE_FRAGMENT_PROCESSOR_TEST(CircularRRectEffect);
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000115
joshualittb0a8a372014-09-23 09:50:21 -0700116GrFragmentProcessor* CircularRRectEffect::TestCreate(SkRandom* random,
117 GrContext*,
118 const GrDrawTargetCaps& caps,
119 GrTexture*[]) {
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000120 SkScalar w = random->nextRangeScalar(20.f, 1000.f);
121 SkScalar h = random->nextRangeScalar(20.f, 1000.f);
122 SkScalar r = random->nextRangeF(kRadiusMin, 9.f);
123 SkRRect rrect;
124 rrect.setRectXY(SkRect::MakeWH(w, h), r, r);
joshualittb0a8a372014-09-23 09:50:21 -0700125 GrFragmentProcessor* fp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000126 do {
joshualittb0a8a372014-09-23 09:50:21 -0700127 GrPrimitiveEdgeType et =
128 (GrPrimitiveEdgeType)random->nextULessThan(kGrProcessorEdgeTypeCnt);
129 fp = GrRRectEffect::Create(et, rrect);
130 } while (NULL == fp);
131 return fp;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000132}
133
134//////////////////////////////////////////////////////////////////////////////
135
joshualittb0a8a372014-09-23 09:50:21 -0700136class GLCircularRRectEffect : public GrGLFragmentProcessor {
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000137public:
joshualittb0a8a372014-09-23 09:50:21 -0700138 GLCircularRRectEffect(const GrBackendProcessorFactory&, const GrProcessor&);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000139
joshualitt15988992014-10-09 15:04:05 -0700140 virtual void emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700141 const GrFragmentProcessor& fp,
142 const GrProcessorKey& key,
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000143 const char* outputColor,
144 const char* inputColor,
145 const TransformedCoordsArray&,
146 const TextureSamplerArray&) SK_OVERRIDE;
147
joshualittb0a8a372014-09-23 09:50:21 -0700148 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000149
joshualittb0a8a372014-09-23 09:50:21 -0700150 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000151
152private:
kkinnunen7510b222014-07-30 00:04:16 -0700153 GrGLProgramDataManager::UniformHandle fInnerRectUniform;
154 GrGLProgramDataManager::UniformHandle fRadiusPlusHalfUniform;
155 SkRRect fPrevRRect;
joshualittb0a8a372014-09-23 09:50:21 -0700156 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000157};
158
joshualittb0a8a372014-09-23 09:50:21 -0700159GLCircularRRectEffect::GLCircularRRectEffect(const GrBackendProcessorFactory& factory,
160 const GrProcessor& )
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000161 : INHERITED (factory) {
162 fPrevRRect.setEmpty();
163}
164
joshualitt15988992014-10-09 15:04:05 -0700165void GLCircularRRectEffect::emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700166 const GrFragmentProcessor& fp,
167 const GrProcessorKey& key,
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000168 const char* outputColor,
169 const char* inputColor,
170 const TransformedCoordsArray&,
171 const TextureSamplerArray& samplers) {
joshualittb0a8a372014-09-23 09:50:21 -0700172 const CircularRRectEffect& crre = fp.cast<CircularRRectEffect>();
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000173 const char *rectName;
174 const char *radiusPlusHalfName;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000175 // The inner rect is the rrect bounds inset by the radius. Its left, top, right, and bottom
176 // edges correspond to components x, y, z, and w, respectively. When a side of the rrect has
177 // only rectangular corners, that side's value corresponds to the rect edge's value outset by
178 // half a pixel.
joshualitt30ba4362014-08-21 20:18:45 -0700179 fInnerRectUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000180 kVec4f_GrSLType,
181 "innerRect",
182 &rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700183 fRadiusPlusHalfUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000184 kFloat_GrSLType,
185 "radiusPlusHalf",
186 &radiusPlusHalfName);
joshualitt30ba4362014-08-21 20:18:45 -0700187
joshualitt15988992014-10-09 15:04:05 -0700188 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700189 const char* fragmentPos = fsBuilder->fragmentPosition();
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000190 // At each quarter-circle corner we compute a vector that is the offset of the fragment position
191 // from the circle center. The vector is pinned in x and y to be in the quarter-plane relevant
192 // to that corner. This means that points near the interior near the rrect top edge will have
193 // a vector that points straight up for both the TL left and TR corners. Computing an
194 // alpha from this vector at either the TR or TL corner will give the correct result. Similarly,
195 // fragments near the other three edges will get the correct AA. Fragments in the interior of
196 // the rrect will have a (0,0) vector at all four corners. So long as the radius > 0.5 they will
197 // correctly produce an alpha value of 1 at all four corners. We take the min of all the alphas.
198 // The code below is a simplified version of the above that performs maxs on the vector
199 // components before computing distances and alpha values so that only one distance computation
200 // need be computed to determine the min alpha.
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000201 //
202 // For the cases where one half of the rrect is rectangular we drop one of the x or y
203 // computations, compute a separate rect edge alpha for the rect side, and mul the two computed
204 // alphas together.
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000205 switch (crre.getCircularCornerFlags()) {
206 case CircularRRectEffect::kAll_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700207 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
208 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
209 fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n");
210 fsBuilder->codeAppendf("\t\tfloat alpha = clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000211 radiusPlusHalfName);
212 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000213 case CircularRRectEffect::kTopLeft_CornerFlag:
joshualitt30ba4362014-08-21 20:18:45 -0700214 fsBuilder->codeAppendf("\t\tvec2 dxy = max(%s.xy - %s.xy, 0.0);\n",
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000215 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700216 fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000217 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700218 fsBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000219 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700220 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 +0000221 radiusPlusHalfName);
222 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000223 case CircularRRectEffect::kTopRight_CornerFlag:
joshualitt30ba4362014-08-21 20:18:45 -0700224 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 +0000225 fragmentPos, rectName, rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700226 fsBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000227 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700228 fsBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000229 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700230 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 +0000231 radiusPlusHalfName);
232 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000233 case CircularRRectEffect::kBottomRight_CornerFlag:
joshualitt30ba4362014-08-21 20:18:45 -0700234 fsBuilder->codeAppendf("\t\tvec2 dxy = max(%s.xy - %s.zw, 0.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000235 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700236 fsBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000237 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700238 fsBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000239 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700240 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 +0000241 radiusPlusHalfName);
242 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000243 case CircularRRectEffect::kBottomLeft_CornerFlag:
joshualitt30ba4362014-08-21 20:18:45 -0700244 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 +0000245 rectName, fragmentPos, fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700246 fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000247 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700248 fsBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000249 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700250 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 +0000251 radiusPlusHalfName);
252 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000253 case CircularRRectEffect::kLeft_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700254 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
255 fsBuilder->codeAppendf("\t\tfloat dy1 = %s.y - %s.w;\n", fragmentPos, rectName);
256 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(dxy0.x, max(dxy0.y, dy1)), 0.0);\n");
257 fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000258 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700259 fsBuilder->codeAppendf("\t\tfloat alpha = rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000260 radiusPlusHalfName);
261 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000262 case CircularRRectEffect::kTop_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700263 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
264 fsBuilder->codeAppendf("\t\tfloat dx1 = %s.x - %s.z;\n", fragmentPos, rectName);
265 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(max(dxy0.x, dx1), dxy0.y), 0.0);\n");
266 fsBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000267 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700268 fsBuilder->codeAppendf("\t\tfloat alpha = bottomAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000269 radiusPlusHalfName);
270 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000271 case CircularRRectEffect::kRight_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700272 fsBuilder->codeAppendf("\t\tfloat dy0 = %s.y - %s.y;\n", rectName, fragmentPos);
273 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
274 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(dxy1.x, max(dy0, dxy1.y)), 0.0);\n");
275 fsBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000276 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700277 fsBuilder->codeAppendf("\t\tfloat alpha = leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000278 radiusPlusHalfName);
279 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000280 case CircularRRectEffect::kBottom_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700281 fsBuilder->codeAppendf("\t\tfloat dx0 = %s.x - %s.x;\n", rectName, fragmentPos);
282 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
283 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(max(dx0, dxy1.x), dxy1.y), 0.0);\n");
284 fsBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000285 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700286 fsBuilder->codeAppendf("\t\tfloat alpha = topAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000287 radiusPlusHalfName);
288 break;
289 }
skia.committer@gmail.com06acb582014-03-06 03:02:32 +0000290
joshualittb0a8a372014-09-23 09:50:21 -0700291 if (kInverseFillAA_GrProcessorEdgeType == crre.getEdgeType()) {
joshualitt30ba4362014-08-21 20:18:45 -0700292 fsBuilder->codeAppend("\t\talpha = 1.0 - alpha;\n");
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000293 }
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000294
joshualitt30ba4362014-08-21 20:18:45 -0700295 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor,
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000296 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
297}
298
joshualittb0a8a372014-09-23 09:50:21 -0700299void GLCircularRRectEffect::GenKey(const GrProcessor& processor, const GrGLCaps&,
300 GrProcessorKeyBuilder* b) {
301 const CircularRRectEffect& crre = processor.cast<CircularRRectEffect>();
302 GR_STATIC_ASSERT(kGrProcessorEdgeTypeCnt <= 8);
bsalomon63e99f72014-07-21 08:03:14 -0700303 b->add32((crre.getCircularCornerFlags() << 3) | crre.getEdgeType());
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000304}
305
kkinnunen7510b222014-07-30 00:04:16 -0700306void GLCircularRRectEffect::setData(const GrGLProgramDataManager& pdman,
joshualittb0a8a372014-09-23 09:50:21 -0700307 const GrProcessor& processor) {
308 const CircularRRectEffect& crre = processor.cast<CircularRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000309 const SkRRect& rrect = crre.getRRect();
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000310 if (rrect != fPrevRRect) {
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000311 SkRect rect = rrect.getBounds();
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000312 SkScalar radius = 0;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000313 switch (crre.getCircularCornerFlags()) {
314 case CircularRRectEffect::kAll_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000315 SkASSERT(rrect.isSimpleCircular());
316 radius = rrect.getSimpleRadii().fX;
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000317 SkASSERT(radius >= kRadiusMin);
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000318 rect.inset(radius, radius);
319 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000320 case CircularRRectEffect::kTopLeft_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000321 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
322 rect.fLeft += radius;
323 rect.fTop += radius;
324 rect.fRight += 0.5f;
325 rect.fBottom += 0.5f;
326 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000327 case CircularRRectEffect::kTopRight_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000328 radius = rrect.radii(SkRRect::kUpperRight_Corner).fX;
bsalomon@google.comde9f2512014-03-11 17:09:17 +0000329 rect.fLeft -= 0.5f;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000330 rect.fTop += radius;
331 rect.fRight -= radius;
332 rect.fBottom += 0.5f;
333 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000334 case CircularRRectEffect::kBottomRight_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000335 radius = rrect.radii(SkRRect::kLowerRight_Corner).fX;
bsalomon@google.comde9f2512014-03-11 17:09:17 +0000336 rect.fLeft -= 0.5f;
337 rect.fTop -= 0.5f;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000338 rect.fRight -= radius;
339 rect.fBottom -= radius;
340 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000341 case CircularRRectEffect::kBottomLeft_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000342 radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX;
343 rect.fLeft += radius;
bsalomon@google.comde9f2512014-03-11 17:09:17 +0000344 rect.fTop -= 0.5f;
345 rect.fRight += 0.5f;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000346 rect.fBottom -= radius;
347 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000348 case CircularRRectEffect::kLeft_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000349 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
350 rect.fLeft += radius;
351 rect.fTop += radius;
352 rect.fRight += 0.5f;
353 rect.fBottom -= radius;
354 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000355 case CircularRRectEffect::kTop_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000356 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
357 rect.fLeft += radius;
358 rect.fTop += radius;
359 rect.fRight -= radius;
360 rect.fBottom += 0.5f;
361 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000362 case CircularRRectEffect::kRight_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000363 radius = rrect.radii(SkRRect::kUpperRight_Corner).fX;
364 rect.fLeft -= 0.5f;
365 rect.fTop += radius;
366 rect.fRight -= radius;
367 rect.fBottom -= radius;
368 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000369 case CircularRRectEffect::kBottom_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000370 radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX;
371 rect.fLeft += radius;
372 rect.fTop -= 0.5f;
373 rect.fRight -= radius;
374 rect.fBottom -= radius;
375 break;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000376 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000377 SkFAIL("Should have been one of the above cases.");
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000378 }
kkinnunen7510b222014-07-30 00:04:16 -0700379 pdman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
380 pdman.set1f(fRadiusPlusHalfUniform, radius + 0.5f);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000381 fPrevRRect = rrect;
382 }
383}
384
385//////////////////////////////////////////////////////////////////////////////
386
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000387class GLEllipticalRRectEffect;
388
joshualittb0a8a372014-09-23 09:50:21 -0700389class EllipticalRRectEffect : public GrFragmentProcessor {
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000390public:
joshualittb0a8a372014-09-23 09:50:21 -0700391 static GrFragmentProcessor* Create(GrPrimitiveEdgeType, const SkRRect&);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000392
393 virtual ~EllipticalRRectEffect() {};
394 static const char* Name() { return "EllipticalRRect"; }
395
396 const SkRRect& getRRect() const { return fRRect; }
397
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000398
joshualittb0a8a372014-09-23 09:50:21 -0700399 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000400
joshualittb0a8a372014-09-23 09:50:21 -0700401 typedef GLEllipticalRRectEffect GLProcessor;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000402
joshualittb0a8a372014-09-23 09:50:21 -0700403 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERRIDE;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000404
405private:
joshualittb0a8a372014-09-23 09:50:21 -0700406 EllipticalRRectEffect(GrPrimitiveEdgeType, const SkRRect&);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000407
bsalomon0e08fc12014-10-15 08:19:04 -0700408 virtual bool onIsEqual(const GrFragmentProcessor& other) const SK_OVERRIDE;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000409
egdaniel605dd0f2014-11-12 08:35:25 -0800410 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700411
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000412 SkRRect fRRect;
joshualittb0a8a372014-09-23 09:50:21 -0700413 GrPrimitiveEdgeType fEdgeType;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000414
joshualittb0a8a372014-09-23 09:50:21 -0700415 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000416
joshualittb0a8a372014-09-23 09:50:21 -0700417 typedef GrFragmentProcessor INHERITED;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000418};
419
joshualittb0a8a372014-09-23 09:50:21 -0700420GrFragmentProcessor*
421EllipticalRRectEffect::Create(GrPrimitiveEdgeType edgeType, const SkRRect& rrect) {
422 if (kFillAA_GrProcessorEdgeType != edgeType && kInverseFillAA_GrProcessorEdgeType != edgeType) {
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000423 return NULL;
424 }
bsalomon55fad7a2014-07-08 07:34:20 -0700425 return SkNEW_ARGS(EllipticalRRectEffect, (edgeType, rrect));
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000426}
427
egdaniel605dd0f2014-11-12 08:35:25 -0800428void EllipticalRRectEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
egdanielccb2e382014-10-13 12:53:46 -0700429 inout->mulByUnknownAlpha();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000430}
431
joshualittb0a8a372014-09-23 09:50:21 -0700432const GrBackendFragmentProcessorFactory& EllipticalRRectEffect::getFactory() const {
433 return GrTBackendFragmentProcessorFactory<EllipticalRRectEffect>::getInstance();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000434}
435
joshualittb0a8a372014-09-23 09:50:21 -0700436EllipticalRRectEffect::EllipticalRRectEffect(GrPrimitiveEdgeType edgeType, const SkRRect& rrect)
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000437 : fRRect(rrect)
438 , fEdgeType(edgeType){
439 this->setWillReadFragmentPosition();
440}
441
bsalomon0e08fc12014-10-15 08:19:04 -0700442bool EllipticalRRectEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700443 const EllipticalRRectEffect& erre = other.cast<EllipticalRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000444 return fEdgeType == erre.fEdgeType && fRRect == erre.fRRect;
445}
446
447//////////////////////////////////////////////////////////////////////////////
448
joshualittb0a8a372014-09-23 09:50:21 -0700449GR_DEFINE_FRAGMENT_PROCESSOR_TEST(EllipticalRRectEffect);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000450
joshualittb0a8a372014-09-23 09:50:21 -0700451GrFragmentProcessor* EllipticalRRectEffect::TestCreate(SkRandom* random,
452 GrContext*,
453 const GrDrawTargetCaps& caps,
454 GrTexture*[]) {
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000455 SkScalar w = random->nextRangeScalar(20.f, 1000.f);
456 SkScalar h = random->nextRangeScalar(20.f, 1000.f);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000457 SkVector r[4];
458 r[SkRRect::kUpperLeft_Corner].fX = random->nextRangeF(kRadiusMin, 9.f);
459 // ensure at least one corner really is elliptical
460 do {
461 r[SkRRect::kUpperLeft_Corner].fY = random->nextRangeF(kRadiusMin, 9.f);
462 } while (r[SkRRect::kUpperLeft_Corner].fY == r[SkRRect::kUpperLeft_Corner].fX);
463
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000464 SkRRect rrect;
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000465 if (random->nextBool()) {
466 // half the time create a four-radii rrect.
467 r[SkRRect::kLowerRight_Corner].fX = random->nextRangeF(kRadiusMin, 9.f);
468 r[SkRRect::kLowerRight_Corner].fY = random->nextRangeF(kRadiusMin, 9.f);
469
470 r[SkRRect::kUpperRight_Corner].fX = r[SkRRect::kLowerRight_Corner].fX;
471 r[SkRRect::kUpperRight_Corner].fY = r[SkRRect::kUpperLeft_Corner].fY;
472
473 r[SkRRect::kLowerLeft_Corner].fX = r[SkRRect::kUpperLeft_Corner].fX;
474 r[SkRRect::kLowerLeft_Corner].fY = r[SkRRect::kLowerRight_Corner].fY;
475
476 rrect.setRectRadii(SkRect::MakeWH(w, h), r);
477 } else {
478 rrect.setRectXY(SkRect::MakeWH(w, h), r[SkRRect::kUpperLeft_Corner].fX,
479 r[SkRRect::kUpperLeft_Corner].fY);
480 }
joshualittb0a8a372014-09-23 09:50:21 -0700481 GrFragmentProcessor* fp;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000482 do {
joshualittb0a8a372014-09-23 09:50:21 -0700483 GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)random->nextULessThan(kGrProcessorEdgeTypeCnt);
484 fp = GrRRectEffect::Create(et, rrect);
485 } while (NULL == fp);
486 return fp;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000487}
488
489//////////////////////////////////////////////////////////////////////////////
490
joshualittb0a8a372014-09-23 09:50:21 -0700491class GLEllipticalRRectEffect : public GrGLFragmentProcessor {
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000492public:
joshualittb0a8a372014-09-23 09:50:21 -0700493 GLEllipticalRRectEffect(const GrBackendProcessorFactory&, const GrProcessor&);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000494
joshualitt15988992014-10-09 15:04:05 -0700495 virtual void emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700496 const GrFragmentProcessor& effect,
497 const GrProcessorKey& key,
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000498 const char* outputColor,
499 const char* inputColor,
500 const TransformedCoordsArray&,
501 const TextureSamplerArray&) SK_OVERRIDE;
502
joshualittb0a8a372014-09-23 09:50:21 -0700503 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000504
joshualittb0a8a372014-09-23 09:50:21 -0700505 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000506
507private:
kkinnunen7510b222014-07-30 00:04:16 -0700508 GrGLProgramDataManager::UniformHandle fInnerRectUniform;
509 GrGLProgramDataManager::UniformHandle fInvRadiiSqdUniform;
510 SkRRect fPrevRRect;
joshualittb0a8a372014-09-23 09:50:21 -0700511 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000512};
513
joshualittb0a8a372014-09-23 09:50:21 -0700514GLEllipticalRRectEffect::GLEllipticalRRectEffect(const GrBackendProcessorFactory& factory,
515 const GrProcessor& effect)
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000516 : INHERITED (factory) {
517 fPrevRRect.setEmpty();
518}
519
joshualitt15988992014-10-09 15:04:05 -0700520void GLEllipticalRRectEffect::emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700521 const GrFragmentProcessor& effect,
522 const GrProcessorKey& key,
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000523 const char* outputColor,
524 const char* inputColor,
525 const TransformedCoordsArray&,
526 const TextureSamplerArray& samplers) {
joshualitt49586be2014-09-16 08:21:41 -0700527 const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000528 const char *rectName;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000529 // The inner rect is the rrect bounds inset by the x/y radii
joshualitt30ba4362014-08-21 20:18:45 -0700530 fInnerRectUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000531 kVec4f_GrSLType,
532 "innerRect",
533 &rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700534
joshualitt15988992014-10-09 15:04:05 -0700535 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700536 const char* fragmentPos = fsBuilder->fragmentPosition();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000537 // At each quarter-ellipse corner we compute a vector that is the offset of the fragment pos
538 // to the ellipse center. The vector is pinned in x and y to be in the quarter-plane relevant
539 // to that corner. This means that points near the interior near the rrect top edge will have
540 // a vector that points straight up for both the TL left and TR corners. Computing an
541 // alpha from this vector at either the TR or TL corner will give the correct result. Similarly,
542 // fragments near the other three edges will get the correct AA. Fragments in the interior of
543 // the rrect will have a (0,0) vector at all four corners. So long as the radii > 0.5 they will
544 // correctly produce an alpha value of 1 at all four corners. We take the min of all the alphas.
545 // The code below is a simplified version of the above that performs maxs on the vector
546 // components before computing distances and alpha values so that only one distance computation
547 // need be computed to determine the min alpha.
joshualitt30ba4362014-08-21 20:18:45 -0700548 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
549 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000550 switch (erre.getRRect().getType()) {
551 case SkRRect::kSimple_Type: {
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000552 const char *invRadiiXYSqdName;
joshualitt30ba4362014-08-21 20:18:45 -0700553 fInvRadiiSqdUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000554 kVec2f_GrSLType,
555 "invRadiiXY",
556 &invRadiiXYSqdName);
joshualitt30ba4362014-08-21 20:18:45 -0700557 fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000558 // Z is the x/y offsets divided by squared radii.
joshualitt30ba4362014-08-21 20:18:45 -0700559 fsBuilder->codeAppendf("\t\tvec2 Z = dxy * %s;\n", invRadiiXYSqdName);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000560 break;
561 }
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000562 case SkRRect::kNinePatch_Type: {
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000563 const char *invRadiiLTRBSqdName;
joshualitt30ba4362014-08-21 20:18:45 -0700564 fInvRadiiSqdUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000565 kVec4f_GrSLType,
566 "invRadiiLTRB",
567 &invRadiiLTRBSqdName);
joshualitt30ba4362014-08-21 20:18:45 -0700568 fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000569 // Z is the x/y offsets divided by squared radii. We only care about the (at most) one
570 // corner where both the x and y offsets are positive, hence the maxes. (The inverse
571 // squared radii will always be positive.)
joshualitt30ba4362014-08-21 20:18:45 -0700572 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 +0000573 invRadiiLTRBSqdName, invRadiiLTRBSqdName);
574 break;
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000575 }
576 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000577 SkFAIL("RRect should always be simple or nine-patch.");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000578 }
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000579 // implicit is the evaluation of (x/a)^2 + (y/b)^2 - 1.
joshualitt30ba4362014-08-21 20:18:45 -0700580 fsBuilder->codeAppend("\t\tfloat implicit = dot(Z, dxy) - 1.0;\n");
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000581 // grad_dot is the squared length of the gradient of the implicit.
joshualitt30ba4362014-08-21 20:18:45 -0700582 fsBuilder->codeAppendf("\t\tfloat grad_dot = 4.0 * dot(Z, Z);\n");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000583 // avoid calling inversesqrt on zero.
joshualitt30ba4362014-08-21 20:18:45 -0700584 fsBuilder->codeAppend("\t\tgrad_dot = max(grad_dot, 1.0e-4);\n");
585 fsBuilder->codeAppendf("\t\tfloat approx_dist = implicit * inversesqrt(grad_dot);\n");
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000586
joshualittb0a8a372014-09-23 09:50:21 -0700587 if (kFillAA_GrProcessorEdgeType == erre.getEdgeType()) {
joshualitt30ba4362014-08-21 20:18:45 -0700588 fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 - approx_dist, 0.0, 1.0);\n");
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000589 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700590 fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 + approx_dist, 0.0, 1.0);\n");
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000591 }
592
joshualitt30ba4362014-08-21 20:18:45 -0700593 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor,
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000594 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
595}
596
joshualittb0a8a372014-09-23 09:50:21 -0700597void GLEllipticalRRectEffect::GenKey(const GrProcessor& effect, const GrGLCaps&,
598 GrProcessorKeyBuilder* b) {
joshualitt49586be2014-09-16 08:21:41 -0700599 const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>();
joshualittb0a8a372014-09-23 09:50:21 -0700600 GR_STATIC_ASSERT(kLast_GrProcessorEdgeType < (1 << 3));
bsalomon63e99f72014-07-21 08:03:14 -0700601 b->add32(erre.getRRect().getType() | erre.getEdgeType() << 3);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000602}
603
kkinnunen7510b222014-07-30 00:04:16 -0700604void GLEllipticalRRectEffect::setData(const GrGLProgramDataManager& pdman,
joshualittb0a8a372014-09-23 09:50:21 -0700605 const GrProcessor& effect) {
joshualitt49586be2014-09-16 08:21:41 -0700606 const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000607 const SkRRect& rrect = erre.getRRect();
608 if (rrect != fPrevRRect) {
609 SkRect rect = rrect.getBounds();
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000610 const SkVector& r0 = rrect.radii(SkRRect::kUpperLeft_Corner);
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000611 SkASSERT(r0.fX >= kRadiusMin);
612 SkASSERT(r0.fY >= kRadiusMin);
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000613 switch (erre.getRRect().getType()) {
614 case SkRRect::kSimple_Type:
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000615 rect.inset(r0.fX, r0.fY);
kkinnunen7510b222014-07-30 00:04:16 -0700616 pdman.set2f(fInvRadiiSqdUniform, 1.f / (r0.fX * r0.fX),
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000617 1.f / (r0.fY * r0.fY));
618 break;
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000619 case SkRRect::kNinePatch_Type: {
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000620 const SkVector& r1 = rrect.radii(SkRRect::kLowerRight_Corner);
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000621 SkASSERT(r1.fX >= kRadiusMin);
622 SkASSERT(r1.fY >= kRadiusMin);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000623 rect.fLeft += r0.fX;
624 rect.fTop += r0.fY;
625 rect.fRight -= r1.fX;
626 rect.fBottom -= r1.fY;
kkinnunen7510b222014-07-30 00:04:16 -0700627 pdman.set4f(fInvRadiiSqdUniform, 1.f / (r0.fX * r0.fX),
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000628 1.f / (r0.fY * r0.fY),
629 1.f / (r1.fX * r1.fX),
630 1.f / (r1.fY * r1.fY));
631 break;
632 }
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000633 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000634 SkFAIL("RRect should always be simple or nine-patch.");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000635 }
kkinnunen7510b222014-07-30 00:04:16 -0700636 pdman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000637 fPrevRRect = rrect;
638 }
639}
640
641//////////////////////////////////////////////////////////////////////////////
642
joshualittb0a8a372014-09-23 09:50:21 -0700643GrFragmentProcessor* GrRRectEffect::Create(GrPrimitiveEdgeType edgeType, const SkRRect& rrect) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000644 if (rrect.isRect()) {
645 return GrConvexPolyEffect::Create(edgeType, rrect.getBounds());
646 }
647
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000648 if (rrect.isOval()) {
649 return GrOvalEffect::Create(edgeType, rrect.getBounds());
650 }
651
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000652 if (rrect.isSimple()) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000653 if (rrect.getSimpleRadii().fX < kRadiusMin || rrect.getSimpleRadii().fY < kRadiusMin) {
654 // In this case the corners are extremely close to rectangular and we collapse the
655 // clip to a rectangular clip.
656 return GrConvexPolyEffect::Create(edgeType, rrect.getBounds());
657 }
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000658 if (rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY) {
skia.committer@gmail.com6e4eb212014-03-25 03:02:32 +0000659 return CircularRRectEffect::Create(edgeType, CircularRRectEffect::kAll_CornerFlags,
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000660 rrect);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000661 } else {
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000662 return EllipticalRRectEffect::Create(edgeType, rrect);
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000663 }
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000664 }
665
666 if (rrect.isComplex() || rrect.isNinePatch()) {
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000667 // Check for the "tab" cases - two adjacent circular corners and two square corners.
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000668 SkScalar circularRadius = 0;
669 uint32_t cornerFlags = 0;
670
671 SkVector radii[4];
672 bool squashedRadii = false;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000673 for (int c = 0; c < 4; ++c) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000674 radii[c] = rrect.radii((SkRRect::Corner)c);
675 SkASSERT((0 == radii[c].fX) == (0 == radii[c].fY));
676 if (0 == radii[c].fX) {
677 // The corner is square, so no need to squash or flag as circular.
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000678 continue;
679 }
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000680 if (radii[c].fX < kRadiusMin || radii[c].fY < kRadiusMin) {
681 radii[c].set(0, 0);
682 squashedRadii = true;
683 continue;
684 }
685 if (radii[c].fX != radii[c].fY) {
bsalomon@google.com44a435b2014-03-13 19:20:32 +0000686 cornerFlags = ~0U;
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000687 break;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000688 }
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000689 if (!cornerFlags) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000690 circularRadius = radii[c].fX;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000691 cornerFlags = 1 << c;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000692 } else {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000693 if (radii[c].fX != circularRadius) {
bsalomon@google.com44a435b2014-03-13 19:20:32 +0000694 cornerFlags = ~0U;
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000695 break;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000696 }
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000697 cornerFlags |= 1 << c;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000698 }
699 }
700
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000701 switch (cornerFlags) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000702 case CircularRRectEffect::kAll_CornerFlags:
703 // This rrect should have been caught in the simple case above. Though, it would
704 // be correctly handled in the fallthrough code.
705 SkASSERT(false);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000706 case CircularRRectEffect::kTopLeft_CornerFlag:
707 case CircularRRectEffect::kTopRight_CornerFlag:
708 case CircularRRectEffect::kBottomRight_CornerFlag:
709 case CircularRRectEffect::kBottomLeft_CornerFlag:
710 case CircularRRectEffect::kLeft_CornerFlags:
711 case CircularRRectEffect::kTop_CornerFlags:
712 case CircularRRectEffect::kRight_CornerFlags:
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000713 case CircularRRectEffect::kBottom_CornerFlags: {
714 SkTCopyOnFirstWrite<SkRRect> rr(rrect);
715 if (squashedRadii) {
716 rr.writable()->setRectRadii(rrect.getBounds(), radii);
717 }
718 return CircularRRectEffect::Create(edgeType, cornerFlags, *rr);
719 }
720 case CircularRRectEffect::kNone_CornerFlags:
721 return GrConvexPolyEffect::Create(edgeType, rrect.getBounds());
722 default: {
723 if (squashedRadii) {
724 // If we got here then we squashed some but not all the radii to zero. (If all
725 // had been squashed cornerFlags would be 0.) The elliptical effect doesn't
726 // support some rounded and some square corners.
727 return NULL;
728 }
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000729 if (rrect.isNinePatch()) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000730 return EllipticalRRectEffect::Create(edgeType, rrect);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000731 }
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000732 return NULL;
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000733 }
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000734 }
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000735 }
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000736
737 return NULL;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000738}