blob: 432e6ff402529996494ad02c5bdbcd97505cd5a2 [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
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +000010#include "GrConvexPolyEffect.h"
joshualitteb2a6762014-12-04 11:35:33 -080011#include "GrFragmentProcessor.h"
egdaniel605dd0f2014-11-12 08:35:25 -080012#include "GrInvariantOutput.h"
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000013#include "GrOvalEffect.h"
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000014#include "SkRRect.h"
wangyix6af0c932015-07-22 10:21:17 -070015#include "gl/GrGLFragmentProcessor.h"
joshualitteb2a6762014-12-04 11:35:33 -080016#include "gl/builders/GrGLProgramBuilder.h"
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +000017
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +000018// The effects defined here only handle rrect radii >= kRadiusMin.
19static const SkScalar kRadiusMin = SK_ScalarHalf;
20
21//////////////////////////////////////////////////////////////////////////////
22
joshualittb0a8a372014-09-23 09:50:21 -070023class CircularRRectEffect : public GrFragmentProcessor {
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000024public:
skia.committer@gmail.comf1f66c02014-03-05 03:02:06 +000025
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000026 enum CornerFlags {
27 kTopLeft_CornerFlag = (1 << SkRRect::kUpperLeft_Corner),
28 kTopRight_CornerFlag = (1 << SkRRect::kUpperRight_Corner),
29 kBottomRight_CornerFlag = (1 << SkRRect::kLowerRight_Corner),
30 kBottomLeft_CornerFlag = (1 << SkRRect::kLowerLeft_Corner),
31
32 kLeft_CornerFlags = kTopLeft_CornerFlag | kBottomLeft_CornerFlag,
33 kTop_CornerFlags = kTopLeft_CornerFlag | kTopRight_CornerFlag,
34 kRight_CornerFlags = kTopRight_CornerFlag | kBottomRight_CornerFlag,
35 kBottom_CornerFlags = kBottomLeft_CornerFlag | kBottomRight_CornerFlag,
36
37 kAll_CornerFlags = kTopLeft_CornerFlag | kTopRight_CornerFlag |
38 kBottomLeft_CornerFlag | kBottomRight_CornerFlag,
39
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +000040 kNone_CornerFlags = 0
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +000041 };
42
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000043 // The flags are used to indicate which corners are circluar (unflagged corners are assumed to
44 // be square).
joshualittb0a8a372014-09-23 09:50:21 -070045 static GrFragmentProcessor* Create(GrPrimitiveEdgeType, uint32_t circularCornerFlags,
46 const SkRRect&);
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000047
commit-bot@chromium.org4355f212014-03-12 15:32:50 +000048 virtual ~CircularRRectEffect() {};
joshualitteb2a6762014-12-04 11:35:33 -080049
mtklein36352bf2015-03-25 18:17:31 -070050 const char* name() const override { return "CircularRRect"; }
joshualitteb2a6762014-12-04 11:35:33 -080051
jvanverthcfc18862015-04-28 08:48:20 -070052 void getGLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
joshualitteb2a6762014-12-04 11:35:33 -080053
mtklein36352bf2015-03-25 18:17:31 -070054 GrGLFragmentProcessor* createGLInstance() const override;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000055
56 const SkRRect& getRRect() const { return fRRect; }
57
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000058 uint32_t getCircularCornerFlags() const { return fCircularCornerFlags; }
skia.committer@gmail.com06acb582014-03-06 03:02:32 +000059
joshualittb0a8a372014-09-23 09:50:21 -070060 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +000061
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000062private:
joshualittb0a8a372014-09-23 09:50:21 -070063 CircularRRectEffect(GrPrimitiveEdgeType, uint32_t circularCornerFlags, const SkRRect&);
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000064
mtklein36352bf2015-03-25 18:17:31 -070065 bool onIsEqual(const GrFragmentProcessor& other) const override;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000066
mtklein36352bf2015-03-25 18:17:31 -070067 void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
egdaniel1a8ecdf2014-10-03 06:24:12 -070068
joshualittb0a8a372014-09-23 09:50:21 -070069 SkRRect fRRect;
70 GrPrimitiveEdgeType fEdgeType;
71 uint32_t fCircularCornerFlags;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000072
joshualittb0a8a372014-09-23 09:50:21 -070073 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000074
joshualittb0a8a372014-09-23 09:50:21 -070075 typedef GrFragmentProcessor INHERITED;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000076};
77
joshualittb0a8a372014-09-23 09:50:21 -070078GrFragmentProcessor* CircularRRectEffect::Create(GrPrimitiveEdgeType edgeType,
79 uint32_t circularCornerFlags,
80 const SkRRect& rrect) {
81 if (kFillAA_GrProcessorEdgeType != edgeType && kInverseFillAA_GrProcessorEdgeType != edgeType) {
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000082 return NULL;
83 }
bsalomon55fad7a2014-07-08 07:34:20 -070084 return SkNEW_ARGS(CircularRRectEffect, (edgeType, circularCornerFlags, rrect));
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000085}
86
egdaniel605dd0f2014-11-12 08:35:25 -080087void CircularRRectEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
joshualitt56995b52014-12-11 15:44:02 -080088 inout->mulByUnknownSingleComponent();
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000089}
90
joshualittb0a8a372014-09-23 09:50:21 -070091CircularRRectEffect::CircularRRectEffect(GrPrimitiveEdgeType edgeType, uint32_t circularCornerFlags,
92 const SkRRect& rrect)
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +000093 : fRRect(rrect)
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000094 , fEdgeType(edgeType)
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000095 , fCircularCornerFlags(circularCornerFlags) {
joshualitteb2a6762014-12-04 11:35:33 -080096 this->initClassID<CircularRRectEffect>();
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000097 this->setWillReadFragmentPosition();
98}
99
bsalomon0e08fc12014-10-15 08:19:04 -0700100bool CircularRRectEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700101 const CircularRRectEffect& crre = other.cast<CircularRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000102 // The corner flags are derived from fRRect, so no need to check them.
103 return fEdgeType == crre.fEdgeType && fRRect == crre.fRRect;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000104}
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000105
106//////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000107
joshualittb0a8a372014-09-23 09:50:21 -0700108GR_DEFINE_FRAGMENT_PROCESSOR_TEST(CircularRRectEffect);
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000109
joshualitt0067ff52015-07-08 14:26:19 -0700110GrFragmentProcessor* CircularRRectEffect::TestCreate(GrProcessorTestData* d) {
111 SkScalar w = d->fRandom->nextRangeScalar(20.f, 1000.f);
112 SkScalar h = d->fRandom->nextRangeScalar(20.f, 1000.f);
113 SkScalar r = d->fRandom->nextRangeF(kRadiusMin, 9.f);
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000114 SkRRect rrect;
115 rrect.setRectXY(SkRect::MakeWH(w, h), r, r);
joshualittb0a8a372014-09-23 09:50:21 -0700116 GrFragmentProcessor* fp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000117 do {
joshualittb0a8a372014-09-23 09:50:21 -0700118 GrPrimitiveEdgeType et =
joshualitt0067ff52015-07-08 14:26:19 -0700119 (GrPrimitiveEdgeType)d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt);
joshualittb0a8a372014-09-23 09:50:21 -0700120 fp = GrRRectEffect::Create(et, rrect);
121 } while (NULL == fp);
122 return fp;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000123}
124
125//////////////////////////////////////////////////////////////////////////////
126
joshualittb0a8a372014-09-23 09:50:21 -0700127class GLCircularRRectEffect : public GrGLFragmentProcessor {
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000128public:
joshualitteb2a6762014-12-04 11:35:33 -0800129 GLCircularRRectEffect(const GrProcessor&);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000130
joshualitt15988992014-10-09 15:04:05 -0700131 virtual void emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700132 const GrFragmentProcessor& fp,
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000133 const char* outputColor,
134 const char* inputColor,
135 const TransformedCoordsArray&,
mtklein36352bf2015-03-25 18:17:31 -0700136 const TextureSamplerArray&) override;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000137
jvanverthcfc18862015-04-28 08:48:20 -0700138 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000139
mtklein36352bf2015-03-25 18:17:31 -0700140 void setData(const GrGLProgramDataManager&, const GrProcessor&) override;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000141
142private:
kkinnunen7510b222014-07-30 00:04:16 -0700143 GrGLProgramDataManager::UniformHandle fInnerRectUniform;
144 GrGLProgramDataManager::UniformHandle fRadiusPlusHalfUniform;
145 SkRRect fPrevRRect;
joshualittb0a8a372014-09-23 09:50:21 -0700146 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000147};
148
joshualitteb2a6762014-12-04 11:35:33 -0800149GLCircularRRectEffect::GLCircularRRectEffect(const GrProcessor& ) {
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000150 fPrevRRect.setEmpty();
151}
152
joshualitt15988992014-10-09 15:04:05 -0700153void GLCircularRRectEffect::emitCode(GrGLFPBuilder* builder,
joshualitt267ce482014-11-25 14:52:21 -0800154 const GrFragmentProcessor& fp,
155 const char* outputColor,
156 const char* inputColor,
157 const TransformedCoordsArray&,
158 const TextureSamplerArray& samplers) {
joshualittb0a8a372014-09-23 09:50:21 -0700159 const CircularRRectEffect& crre = fp.cast<CircularRRectEffect>();
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000160 const char *rectName;
161 const char *radiusPlusHalfName;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000162 // The inner rect is the rrect bounds inset by the radius. Its left, top, right, and bottom
163 // edges correspond to components x, y, z, and w, respectively. When a side of the rrect has
164 // only rectangular corners, that side's value corresponds to the rect edge's value outset by
165 // half a pixel.
joshualitt30ba4362014-08-21 20:18:45 -0700166 fInnerRectUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800167 kVec4f_GrSLType, kDefault_GrSLPrecision,
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000168 "innerRect",
169 &rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700170 fRadiusPlusHalfUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800171 kFloat_GrSLType, kDefault_GrSLPrecision,
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000172 "radiusPlusHalf",
173 &radiusPlusHalfName);
joshualitt30ba4362014-08-21 20:18:45 -0700174
egdaniel29bee0f2015-04-29 11:54:42 -0700175 GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700176 const char* fragmentPos = fsBuilder->fragmentPosition();
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000177 // At each quarter-circle corner we compute a vector that is the offset of the fragment position
178 // from the circle center. The vector is pinned in x and y to be in the quarter-plane relevant
179 // to that corner. This means that points near the interior near the rrect top edge will have
180 // a vector that points straight up for both the TL left and TR corners. Computing an
181 // alpha from this vector at either the TR or TL corner will give the correct result. Similarly,
182 // fragments near the other three edges will get the correct AA. Fragments in the interior of
183 // the rrect will have a (0,0) vector at all four corners. So long as the radius > 0.5 they will
184 // correctly produce an alpha value of 1 at all four corners. We take the min of all the alphas.
185 // The code below is a simplified version of the above that performs maxs on the vector
186 // components before computing distances and alpha values so that only one distance computation
187 // need be computed to determine the min alpha.
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000188 //
189 // For the cases where one half of the rrect is rectangular we drop one of the x or y
190 // computations, compute a separate rect edge alpha for the rect side, and mul the two computed
191 // alphas together.
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000192 switch (crre.getCircularCornerFlags()) {
193 case CircularRRectEffect::kAll_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700194 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
195 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
196 fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n");
197 fsBuilder->codeAppendf("\t\tfloat alpha = clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000198 radiusPlusHalfName);
199 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000200 case CircularRRectEffect::kTopLeft_CornerFlag:
joshualitt30ba4362014-08-21 20:18:45 -0700201 fsBuilder->codeAppendf("\t\tvec2 dxy = max(%s.xy - %s.xy, 0.0);\n",
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000202 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700203 fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000204 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700205 fsBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000206 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700207 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 +0000208 radiusPlusHalfName);
209 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000210 case CircularRRectEffect::kTopRight_CornerFlag:
joshualitt30ba4362014-08-21 20:18:45 -0700211 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 +0000212 fragmentPos, rectName, rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700213 fsBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000214 fragmentPos, rectName);
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 * leftAlpha * 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::kBottomRight_CornerFlag:
joshualitt30ba4362014-08-21 20:18:45 -0700221 fsBuilder->codeAppendf("\t\tvec2 dxy = max(%s.xy - %s.zw, 0.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000222 fragmentPos, rectName);
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 topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000226 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700227 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 +0000228 radiusPlusHalfName);
229 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000230 case CircularRRectEffect::kBottomLeft_CornerFlag:
joshualitt30ba4362014-08-21 20:18:45 -0700231 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 +0000232 rectName, fragmentPos, fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700233 fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000234 rectName, fragmentPos);
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 * rightAlpha * 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::kLeft_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700241 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
242 fsBuilder->codeAppendf("\t\tfloat dy1 = %s.y - %s.w;\n", fragmentPos, rectName);
243 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(dxy0.x, max(dxy0.y, dy1)), 0.0);\n");
244 fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000245 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700246 fsBuilder->codeAppendf("\t\tfloat alpha = rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000247 radiusPlusHalfName);
248 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000249 case CircularRRectEffect::kTop_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700250 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
251 fsBuilder->codeAppendf("\t\tfloat dx1 = %s.x - %s.z;\n", fragmentPos, rectName);
252 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(max(dxy0.x, dx1), dxy0.y), 0.0);\n");
253 fsBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000254 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700255 fsBuilder->codeAppendf("\t\tfloat alpha = bottomAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000256 radiusPlusHalfName);
257 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000258 case CircularRRectEffect::kRight_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700259 fsBuilder->codeAppendf("\t\tfloat dy0 = %s.y - %s.y;\n", rectName, fragmentPos);
260 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
261 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(dxy1.x, max(dy0, dxy1.y)), 0.0);\n");
262 fsBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000263 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700264 fsBuilder->codeAppendf("\t\tfloat alpha = leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000265 radiusPlusHalfName);
266 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000267 case CircularRRectEffect::kBottom_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700268 fsBuilder->codeAppendf("\t\tfloat dx0 = %s.x - %s.x;\n", rectName, fragmentPos);
269 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
270 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(max(dx0, dxy1.x), dxy1.y), 0.0);\n");
271 fsBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000272 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700273 fsBuilder->codeAppendf("\t\tfloat alpha = topAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000274 radiusPlusHalfName);
275 break;
276 }
skia.committer@gmail.com06acb582014-03-06 03:02:32 +0000277
joshualittb0a8a372014-09-23 09:50:21 -0700278 if (kInverseFillAA_GrProcessorEdgeType == crre.getEdgeType()) {
joshualitt30ba4362014-08-21 20:18:45 -0700279 fsBuilder->codeAppend("\t\talpha = 1.0 - alpha;\n");
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000280 }
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000281
joshualitt30ba4362014-08-21 20:18:45 -0700282 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor,
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000283 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
284}
285
jvanverthcfc18862015-04-28 08:48:20 -0700286void GLCircularRRectEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700287 GrProcessorKeyBuilder* b) {
288 const CircularRRectEffect& crre = processor.cast<CircularRRectEffect>();
289 GR_STATIC_ASSERT(kGrProcessorEdgeTypeCnt <= 8);
bsalomon63e99f72014-07-21 08:03:14 -0700290 b->add32((crre.getCircularCornerFlags() << 3) | crre.getEdgeType());
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000291}
292
kkinnunen7510b222014-07-30 00:04:16 -0700293void GLCircularRRectEffect::setData(const GrGLProgramDataManager& pdman,
joshualittb0a8a372014-09-23 09:50:21 -0700294 const GrProcessor& processor) {
295 const CircularRRectEffect& crre = processor.cast<CircularRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000296 const SkRRect& rrect = crre.getRRect();
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000297 if (rrect != fPrevRRect) {
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000298 SkRect rect = rrect.getBounds();
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000299 SkScalar radius = 0;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000300 switch (crre.getCircularCornerFlags()) {
301 case CircularRRectEffect::kAll_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000302 SkASSERT(rrect.isSimpleCircular());
303 radius = rrect.getSimpleRadii().fX;
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000304 SkASSERT(radius >= kRadiusMin);
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000305 rect.inset(radius, radius);
306 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000307 case CircularRRectEffect::kTopLeft_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000308 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
309 rect.fLeft += radius;
310 rect.fTop += radius;
311 rect.fRight += 0.5f;
312 rect.fBottom += 0.5f;
313 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000314 case CircularRRectEffect::kTopRight_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000315 radius = rrect.radii(SkRRect::kUpperRight_Corner).fX;
bsalomon@google.comde9f2512014-03-11 17:09:17 +0000316 rect.fLeft -= 0.5f;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000317 rect.fTop += radius;
318 rect.fRight -= radius;
319 rect.fBottom += 0.5f;
320 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000321 case CircularRRectEffect::kBottomRight_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000322 radius = rrect.radii(SkRRect::kLowerRight_Corner).fX;
bsalomon@google.comde9f2512014-03-11 17:09:17 +0000323 rect.fLeft -= 0.5f;
324 rect.fTop -= 0.5f;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000325 rect.fRight -= radius;
326 rect.fBottom -= radius;
327 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000328 case CircularRRectEffect::kBottomLeft_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000329 radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX;
330 rect.fLeft += radius;
bsalomon@google.comde9f2512014-03-11 17:09:17 +0000331 rect.fTop -= 0.5f;
332 rect.fRight += 0.5f;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000333 rect.fBottom -= radius;
334 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000335 case CircularRRectEffect::kLeft_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000336 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
337 rect.fLeft += radius;
338 rect.fTop += radius;
339 rect.fRight += 0.5f;
340 rect.fBottom -= radius;
341 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000342 case CircularRRectEffect::kTop_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000343 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
344 rect.fLeft += radius;
345 rect.fTop += radius;
346 rect.fRight -= radius;
347 rect.fBottom += 0.5f;
348 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000349 case CircularRRectEffect::kRight_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000350 radius = rrect.radii(SkRRect::kUpperRight_Corner).fX;
351 rect.fLeft -= 0.5f;
352 rect.fTop += radius;
353 rect.fRight -= radius;
354 rect.fBottom -= radius;
355 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000356 case CircularRRectEffect::kBottom_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000357 radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX;
358 rect.fLeft += radius;
359 rect.fTop -= 0.5f;
360 rect.fRight -= radius;
361 rect.fBottom -= radius;
362 break;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000363 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000364 SkFAIL("Should have been one of the above cases.");
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000365 }
kkinnunen7510b222014-07-30 00:04:16 -0700366 pdman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
367 pdman.set1f(fRadiusPlusHalfUniform, radius + 0.5f);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000368 fPrevRRect = rrect;
369 }
370}
371
joshualitteb2a6762014-12-04 11:35:33 -0800372////////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000373
jvanverthcfc18862015-04-28 08:48:20 -0700374void CircularRRectEffect::getGLProcessorKey(const GrGLSLCaps& caps,
joshualitteb2a6762014-12-04 11:35:33 -0800375 GrProcessorKeyBuilder* b) const {
376 GLCircularRRectEffect::GenKey(*this, caps, b);
377}
378
379GrGLFragmentProcessor* CircularRRectEffect::createGLInstance() const {
380 return SkNEW_ARGS(GLCircularRRectEffect, (*this));
381}
382
383//////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000384
joshualittb0a8a372014-09-23 09:50:21 -0700385class EllipticalRRectEffect : public GrFragmentProcessor {
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000386public:
joshualittb0a8a372014-09-23 09:50:21 -0700387 static GrFragmentProcessor* Create(GrPrimitiveEdgeType, const SkRRect&);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000388
389 virtual ~EllipticalRRectEffect() {};
joshualitteb2a6762014-12-04 11:35:33 -0800390
mtklein36352bf2015-03-25 18:17:31 -0700391 const char* name() const override { return "EllipticalRRect"; }
joshualitteb2a6762014-12-04 11:35:33 -0800392
jvanverthcfc18862015-04-28 08:48:20 -0700393 void getGLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
joshualitteb2a6762014-12-04 11:35:33 -0800394
mtklein36352bf2015-03-25 18:17:31 -0700395 GrGLFragmentProcessor* createGLInstance() const override;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000396
397 const SkRRect& getRRect() const { return fRRect; }
398
joshualittb0a8a372014-09-23 09:50:21 -0700399 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000400
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000401private:
joshualittb0a8a372014-09-23 09:50:21 -0700402 EllipticalRRectEffect(GrPrimitiveEdgeType, const SkRRect&);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000403
mtklein36352bf2015-03-25 18:17:31 -0700404 bool onIsEqual(const GrFragmentProcessor& other) const override;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000405
mtklein36352bf2015-03-25 18:17:31 -0700406 void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700407
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000408 SkRRect fRRect;
joshualittb0a8a372014-09-23 09:50:21 -0700409 GrPrimitiveEdgeType fEdgeType;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000410
joshualittb0a8a372014-09-23 09:50:21 -0700411 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000412
joshualittb0a8a372014-09-23 09:50:21 -0700413 typedef GrFragmentProcessor INHERITED;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000414};
415
joshualittb0a8a372014-09-23 09:50:21 -0700416GrFragmentProcessor*
417EllipticalRRectEffect::Create(GrPrimitiveEdgeType edgeType, const SkRRect& rrect) {
418 if (kFillAA_GrProcessorEdgeType != edgeType && kInverseFillAA_GrProcessorEdgeType != edgeType) {
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000419 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
egdaniel605dd0f2014-11-12 08:35:25 -0800424void EllipticalRRectEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
joshualitt56995b52014-12-11 15:44:02 -0800425 inout->mulByUnknownSingleComponent();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000426}
427
joshualittb0a8a372014-09-23 09:50:21 -0700428EllipticalRRectEffect::EllipticalRRectEffect(GrPrimitiveEdgeType edgeType, const SkRRect& rrect)
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000429 : fRRect(rrect)
joshualitteb2a6762014-12-04 11:35:33 -0800430 , fEdgeType(edgeType) {
431 this->initClassID<EllipticalRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000432 this->setWillReadFragmentPosition();
433}
434
bsalomon0e08fc12014-10-15 08:19:04 -0700435bool EllipticalRRectEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700436 const EllipticalRRectEffect& erre = other.cast<EllipticalRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000437 return fEdgeType == erre.fEdgeType && fRRect == erre.fRRect;
438}
439
440//////////////////////////////////////////////////////////////////////////////
441
joshualittb0a8a372014-09-23 09:50:21 -0700442GR_DEFINE_FRAGMENT_PROCESSOR_TEST(EllipticalRRectEffect);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000443
joshualitt0067ff52015-07-08 14:26:19 -0700444GrFragmentProcessor* EllipticalRRectEffect::TestCreate(GrProcessorTestData* d) {
445 SkScalar w = d->fRandom->nextRangeScalar(20.f, 1000.f);
446 SkScalar h = d->fRandom->nextRangeScalar(20.f, 1000.f);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000447 SkVector r[4];
joshualitt0067ff52015-07-08 14:26:19 -0700448 r[SkRRect::kUpperLeft_Corner].fX = d->fRandom->nextRangeF(kRadiusMin, 9.f);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000449 // ensure at least one corner really is elliptical
450 do {
joshualitt0067ff52015-07-08 14:26:19 -0700451 r[SkRRect::kUpperLeft_Corner].fY = d->fRandom->nextRangeF(kRadiusMin, 9.f);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000452 } while (r[SkRRect::kUpperLeft_Corner].fY == r[SkRRect::kUpperLeft_Corner].fX);
453
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000454 SkRRect rrect;
joshualitt0067ff52015-07-08 14:26:19 -0700455 if (d->fRandom->nextBool()) {
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000456 // half the time create a four-radii rrect.
joshualitt0067ff52015-07-08 14:26:19 -0700457 r[SkRRect::kLowerRight_Corner].fX = d->fRandom->nextRangeF(kRadiusMin, 9.f);
458 r[SkRRect::kLowerRight_Corner].fY = d->fRandom->nextRangeF(kRadiusMin, 9.f);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000459
460 r[SkRRect::kUpperRight_Corner].fX = r[SkRRect::kLowerRight_Corner].fX;
461 r[SkRRect::kUpperRight_Corner].fY = r[SkRRect::kUpperLeft_Corner].fY;
462
463 r[SkRRect::kLowerLeft_Corner].fX = r[SkRRect::kUpperLeft_Corner].fX;
464 r[SkRRect::kLowerLeft_Corner].fY = r[SkRRect::kLowerRight_Corner].fY;
465
466 rrect.setRectRadii(SkRect::MakeWH(w, h), r);
467 } else {
468 rrect.setRectXY(SkRect::MakeWH(w, h), r[SkRRect::kUpperLeft_Corner].fX,
469 r[SkRRect::kUpperLeft_Corner].fY);
470 }
joshualittb0a8a372014-09-23 09:50:21 -0700471 GrFragmentProcessor* fp;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000472 do {
joshualitt0067ff52015-07-08 14:26:19 -0700473 GrPrimitiveEdgeType et =
474 (GrPrimitiveEdgeType)d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt);
joshualittb0a8a372014-09-23 09:50:21 -0700475 fp = GrRRectEffect::Create(et, rrect);
476 } while (NULL == fp);
477 return fp;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000478}
479
480//////////////////////////////////////////////////////////////////////////////
481
joshualittb0a8a372014-09-23 09:50:21 -0700482class GLEllipticalRRectEffect : public GrGLFragmentProcessor {
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000483public:
joshualitteb2a6762014-12-04 11:35:33 -0800484 GLEllipticalRRectEffect(const GrProcessor&);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000485
joshualitt15988992014-10-09 15:04:05 -0700486 virtual void emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700487 const GrFragmentProcessor& effect,
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000488 const char* outputColor,
489 const char* inputColor,
490 const TransformedCoordsArray&,
mtklein36352bf2015-03-25 18:17:31 -0700491 const TextureSamplerArray&) override;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000492
jvanverthcfc18862015-04-28 08:48:20 -0700493 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000494
mtklein36352bf2015-03-25 18:17:31 -0700495 void setData(const GrGLProgramDataManager&, const GrProcessor&) override;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000496
497private:
kkinnunen7510b222014-07-30 00:04:16 -0700498 GrGLProgramDataManager::UniformHandle fInnerRectUniform;
499 GrGLProgramDataManager::UniformHandle fInvRadiiSqdUniform;
500 SkRRect fPrevRRect;
joshualittb0a8a372014-09-23 09:50:21 -0700501 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000502};
503
joshualitteb2a6762014-12-04 11:35:33 -0800504GLEllipticalRRectEffect::GLEllipticalRRectEffect(const GrProcessor& effect) {
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000505 fPrevRRect.setEmpty();
506}
507
joshualitt15988992014-10-09 15:04:05 -0700508void GLEllipticalRRectEffect::emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700509 const GrFragmentProcessor& effect,
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000510 const char* outputColor,
511 const char* inputColor,
512 const TransformedCoordsArray&,
513 const TextureSamplerArray& samplers) {
joshualitt49586be2014-09-16 08:21:41 -0700514 const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000515 const char *rectName;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000516 // The inner rect is the rrect bounds inset by the x/y radii
joshualitt30ba4362014-08-21 20:18:45 -0700517 fInnerRectUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800518 kVec4f_GrSLType, kDefault_GrSLPrecision,
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000519 "innerRect",
520 &rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700521
egdaniel29bee0f2015-04-29 11:54:42 -0700522 GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700523 const char* fragmentPos = fsBuilder->fragmentPosition();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000524 // At each quarter-ellipse corner we compute a vector that is the offset of the fragment pos
525 // to the ellipse center. The vector is pinned in x and y to be in the quarter-plane relevant
526 // to that corner. This means that points near the interior near the rrect top edge will have
527 // a vector that points straight up for both the TL left and TR corners. Computing an
528 // alpha from this vector at either the TR or TL corner will give the correct result. Similarly,
529 // fragments near the other three edges will get the correct AA. Fragments in the interior of
530 // the rrect will have a (0,0) vector at all four corners. So long as the radii > 0.5 they will
531 // correctly produce an alpha value of 1 at all four corners. We take the min of all the alphas.
532 // The code below is a simplified version of the above that performs maxs on the vector
533 // components before computing distances and alpha values so that only one distance computation
534 // need be computed to determine the min alpha.
joshualitt30ba4362014-08-21 20:18:45 -0700535 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
536 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000537 switch (erre.getRRect().getType()) {
538 case SkRRect::kSimple_Type: {
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000539 const char *invRadiiXYSqdName;
joshualitt30ba4362014-08-21 20:18:45 -0700540 fInvRadiiSqdUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800541 kVec2f_GrSLType, kDefault_GrSLPrecision,
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000542 "invRadiiXY",
543 &invRadiiXYSqdName);
joshualitt30ba4362014-08-21 20:18:45 -0700544 fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000545 // Z is the x/y offsets divided by squared radii.
joshualitt30ba4362014-08-21 20:18:45 -0700546 fsBuilder->codeAppendf("\t\tvec2 Z = dxy * %s;\n", invRadiiXYSqdName);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000547 break;
548 }
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000549 case SkRRect::kNinePatch_Type: {
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000550 const char *invRadiiLTRBSqdName;
joshualitt30ba4362014-08-21 20:18:45 -0700551 fInvRadiiSqdUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800552 kVec4f_GrSLType, kDefault_GrSLPrecision,
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000553 "invRadiiLTRB",
554 &invRadiiLTRBSqdName);
joshualitt30ba4362014-08-21 20:18:45 -0700555 fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000556 // Z is the x/y offsets divided by squared radii. We only care about the (at most) one
557 // corner where both the x and y offsets are positive, hence the maxes. (The inverse
558 // squared radii will always be positive.)
joshualitt30ba4362014-08-21 20:18:45 -0700559 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 +0000560 invRadiiLTRBSqdName, invRadiiLTRBSqdName);
561 break;
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000562 }
563 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000564 SkFAIL("RRect should always be simple or nine-patch.");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000565 }
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000566 // implicit is the evaluation of (x/a)^2 + (y/b)^2 - 1.
joshualitt30ba4362014-08-21 20:18:45 -0700567 fsBuilder->codeAppend("\t\tfloat implicit = dot(Z, dxy) - 1.0;\n");
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000568 // grad_dot is the squared length of the gradient of the implicit.
joshualitt30ba4362014-08-21 20:18:45 -0700569 fsBuilder->codeAppendf("\t\tfloat grad_dot = 4.0 * dot(Z, Z);\n");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000570 // avoid calling inversesqrt on zero.
joshualitt30ba4362014-08-21 20:18:45 -0700571 fsBuilder->codeAppend("\t\tgrad_dot = max(grad_dot, 1.0e-4);\n");
572 fsBuilder->codeAppendf("\t\tfloat approx_dist = implicit * inversesqrt(grad_dot);\n");
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000573
joshualittb0a8a372014-09-23 09:50:21 -0700574 if (kFillAA_GrProcessorEdgeType == erre.getEdgeType()) {
joshualitt30ba4362014-08-21 20:18:45 -0700575 fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 - approx_dist, 0.0, 1.0);\n");
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000576 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700577 fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 + approx_dist, 0.0, 1.0);\n");
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000578 }
579
joshualitt30ba4362014-08-21 20:18:45 -0700580 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor,
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000581 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
582}
583
jvanverthcfc18862015-04-28 08:48:20 -0700584void GLEllipticalRRectEffect::GenKey(const GrProcessor& effect, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700585 GrProcessorKeyBuilder* b) {
joshualitt49586be2014-09-16 08:21:41 -0700586 const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>();
joshualittb0a8a372014-09-23 09:50:21 -0700587 GR_STATIC_ASSERT(kLast_GrProcessorEdgeType < (1 << 3));
bsalomon63e99f72014-07-21 08:03:14 -0700588 b->add32(erre.getRRect().getType() | erre.getEdgeType() << 3);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000589}
590
kkinnunen7510b222014-07-30 00:04:16 -0700591void GLEllipticalRRectEffect::setData(const GrGLProgramDataManager& pdman,
joshualittb0a8a372014-09-23 09:50:21 -0700592 const GrProcessor& effect) {
joshualitt49586be2014-09-16 08:21:41 -0700593 const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000594 const SkRRect& rrect = erre.getRRect();
595 if (rrect != fPrevRRect) {
596 SkRect rect = rrect.getBounds();
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000597 const SkVector& r0 = rrect.radii(SkRRect::kUpperLeft_Corner);
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000598 SkASSERT(r0.fX >= kRadiusMin);
599 SkASSERT(r0.fY >= kRadiusMin);
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000600 switch (erre.getRRect().getType()) {
601 case SkRRect::kSimple_Type:
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000602 rect.inset(r0.fX, r0.fY);
kkinnunen7510b222014-07-30 00:04:16 -0700603 pdman.set2f(fInvRadiiSqdUniform, 1.f / (r0.fX * r0.fX),
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000604 1.f / (r0.fY * r0.fY));
605 break;
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000606 case SkRRect::kNinePatch_Type: {
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000607 const SkVector& r1 = rrect.radii(SkRRect::kLowerRight_Corner);
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000608 SkASSERT(r1.fX >= kRadiusMin);
609 SkASSERT(r1.fY >= kRadiusMin);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000610 rect.fLeft += r0.fX;
611 rect.fTop += r0.fY;
612 rect.fRight -= r1.fX;
613 rect.fBottom -= r1.fY;
kkinnunen7510b222014-07-30 00:04:16 -0700614 pdman.set4f(fInvRadiiSqdUniform, 1.f / (r0.fX * r0.fX),
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000615 1.f / (r0.fY * r0.fY),
616 1.f / (r1.fX * r1.fX),
617 1.f / (r1.fY * r1.fY));
618 break;
619 }
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000620 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000621 SkFAIL("RRect should always be simple or nine-patch.");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000622 }
kkinnunen7510b222014-07-30 00:04:16 -0700623 pdman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000624 fPrevRRect = rrect;
625 }
626}
627
joshualitteb2a6762014-12-04 11:35:33 -0800628////////////////////////////////////////////////////////////////////////////////////////////////////
629
jvanverthcfc18862015-04-28 08:48:20 -0700630void EllipticalRRectEffect::getGLProcessorKey(const GrGLSLCaps& caps,
joshualitteb2a6762014-12-04 11:35:33 -0800631 GrProcessorKeyBuilder* b) const {
632 GLEllipticalRRectEffect::GenKey(*this, caps, b);
633}
634
635GrGLFragmentProcessor* EllipticalRRectEffect::createGLInstance() const {
636 return SkNEW_ARGS(GLEllipticalRRectEffect, (*this));
637}
638
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000639//////////////////////////////////////////////////////////////////////////////
640
joshualittb0a8a372014-09-23 09:50:21 -0700641GrFragmentProcessor* GrRRectEffect::Create(GrPrimitiveEdgeType edgeType, const SkRRect& rrect) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000642 if (rrect.isRect()) {
643 return GrConvexPolyEffect::Create(edgeType, rrect.getBounds());
644 }
645
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000646 if (rrect.isOval()) {
647 return GrOvalEffect::Create(edgeType, rrect.getBounds());
648 }
649
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000650 if (rrect.isSimple()) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000651 if (rrect.getSimpleRadii().fX < kRadiusMin || rrect.getSimpleRadii().fY < kRadiusMin) {
652 // In this case the corners are extremely close to rectangular and we collapse the
653 // clip to a rectangular clip.
654 return GrConvexPolyEffect::Create(edgeType, rrect.getBounds());
655 }
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000656 if (rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY) {
skia.committer@gmail.com6e4eb212014-03-25 03:02:32 +0000657 return CircularRRectEffect::Create(edgeType, CircularRRectEffect::kAll_CornerFlags,
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000658 rrect);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000659 } else {
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000660 return EllipticalRRectEffect::Create(edgeType, rrect);
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000661 }
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000662 }
663
664 if (rrect.isComplex() || rrect.isNinePatch()) {
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000665 // Check for the "tab" cases - two adjacent circular corners and two square corners.
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000666 SkScalar circularRadius = 0;
667 uint32_t cornerFlags = 0;
668
669 SkVector radii[4];
670 bool squashedRadii = false;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000671 for (int c = 0; c < 4; ++c) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000672 radii[c] = rrect.radii((SkRRect::Corner)c);
673 SkASSERT((0 == radii[c].fX) == (0 == radii[c].fY));
674 if (0 == radii[c].fX) {
675 // The corner is square, so no need to squash or flag as circular.
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000676 continue;
677 }
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000678 if (radii[c].fX < kRadiusMin || radii[c].fY < kRadiusMin) {
679 radii[c].set(0, 0);
680 squashedRadii = true;
681 continue;
682 }
683 if (radii[c].fX != radii[c].fY) {
bsalomon@google.com44a435b2014-03-13 19:20:32 +0000684 cornerFlags = ~0U;
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000685 break;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000686 }
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000687 if (!cornerFlags) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000688 circularRadius = radii[c].fX;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000689 cornerFlags = 1 << c;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000690 } else {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000691 if (radii[c].fX != circularRadius) {
bsalomon@google.com44a435b2014-03-13 19:20:32 +0000692 cornerFlags = ~0U;
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000693 break;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000694 }
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000695 cornerFlags |= 1 << c;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000696 }
697 }
698
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000699 switch (cornerFlags) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000700 case CircularRRectEffect::kAll_CornerFlags:
701 // This rrect should have been caught in the simple case above. Though, it would
702 // be correctly handled in the fallthrough code.
703 SkASSERT(false);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000704 case CircularRRectEffect::kTopLeft_CornerFlag:
705 case CircularRRectEffect::kTopRight_CornerFlag:
706 case CircularRRectEffect::kBottomRight_CornerFlag:
707 case CircularRRectEffect::kBottomLeft_CornerFlag:
708 case CircularRRectEffect::kLeft_CornerFlags:
709 case CircularRRectEffect::kTop_CornerFlags:
710 case CircularRRectEffect::kRight_CornerFlags:
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000711 case CircularRRectEffect::kBottom_CornerFlags: {
712 SkTCopyOnFirstWrite<SkRRect> rr(rrect);
713 if (squashedRadii) {
714 rr.writable()->setRectRadii(rrect.getBounds(), radii);
715 }
716 return CircularRRectEffect::Create(edgeType, cornerFlags, *rr);
717 }
718 case CircularRRectEffect::kNone_CornerFlags:
719 return GrConvexPolyEffect::Create(edgeType, rrect.getBounds());
720 default: {
721 if (squashedRadii) {
722 // If we got here then we squashed some but not all the radii to zero. (If all
723 // had been squashed cornerFlags would be 0.) The elliptical effect doesn't
724 // support some rounded and some square corners.
725 return NULL;
726 }
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000727 if (rrect.isNinePatch()) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000728 return EllipticalRRectEffect::Create(edgeType, rrect);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000729 }
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000730 return NULL;
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000731 }
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000732 }
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000733 }
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000734
735 return NULL;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000736}