blob: 24d5077d48e41f4d0922efaf3225a5ec2e543a44 [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"
joshualitteb2a6762014-12-04 11:35:33 -080015#include "gl/GrGLProcessor.h"
16#include "gl/GrGLSL.h"
17#include "gl/builders/GrGLProgramBuilder.h"
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +000018
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +000019// The effects defined here only handle rrect radii >= kRadiusMin.
20static const SkScalar kRadiusMin = SK_ScalarHalf;
21
22//////////////////////////////////////////////////////////////////////////////
23
joshualittb0a8a372014-09-23 09:50:21 -070024class CircularRRectEffect : public GrFragmentProcessor {
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000025public:
skia.committer@gmail.comf1f66c02014-03-05 03:02:06 +000026
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000027 enum CornerFlags {
28 kTopLeft_CornerFlag = (1 << SkRRect::kUpperLeft_Corner),
29 kTopRight_CornerFlag = (1 << SkRRect::kUpperRight_Corner),
30 kBottomRight_CornerFlag = (1 << SkRRect::kLowerRight_Corner),
31 kBottomLeft_CornerFlag = (1 << SkRRect::kLowerLeft_Corner),
32
33 kLeft_CornerFlags = kTopLeft_CornerFlag | kBottomLeft_CornerFlag,
34 kTop_CornerFlags = kTopLeft_CornerFlag | kTopRight_CornerFlag,
35 kRight_CornerFlags = kTopRight_CornerFlag | kBottomRight_CornerFlag,
36 kBottom_CornerFlags = kBottomLeft_CornerFlag | kBottomRight_CornerFlag,
37
38 kAll_CornerFlags = kTopLeft_CornerFlag | kTopRight_CornerFlag |
39 kBottomLeft_CornerFlag | kBottomRight_CornerFlag,
40
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +000041 kNone_CornerFlags = 0
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +000042 };
43
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000044 // The flags are used to indicate which corners are circluar (unflagged corners are assumed to
45 // be square).
joshualittb0a8a372014-09-23 09:50:21 -070046 static GrFragmentProcessor* Create(GrPrimitiveEdgeType, uint32_t circularCornerFlags,
47 const SkRRect&);
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000048
commit-bot@chromium.org4355f212014-03-12 15:32:50 +000049 virtual ~CircularRRectEffect() {};
joshualitteb2a6762014-12-04 11:35:33 -080050
mtklein36352bf2015-03-25 18:17:31 -070051 const char* name() const override { return "CircularRRect"; }
joshualitteb2a6762014-12-04 11:35:33 -080052
mtklein36352bf2015-03-25 18:17:31 -070053 void getGLProcessorKey(const GrGLCaps&, GrProcessorKeyBuilder*) const override;
joshualitteb2a6762014-12-04 11:35:33 -080054
mtklein36352bf2015-03-25 18:17:31 -070055 GrGLFragmentProcessor* createGLInstance() const override;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000056
57 const SkRRect& getRRect() const { return fRRect; }
58
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000059 uint32_t getCircularCornerFlags() const { return fCircularCornerFlags; }
skia.committer@gmail.com06acb582014-03-06 03:02:32 +000060
joshualittb0a8a372014-09-23 09:50:21 -070061 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +000062
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000063private:
joshualittb0a8a372014-09-23 09:50:21 -070064 CircularRRectEffect(GrPrimitiveEdgeType, uint32_t circularCornerFlags, const SkRRect&);
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000065
mtklein36352bf2015-03-25 18:17:31 -070066 bool onIsEqual(const GrFragmentProcessor& other) const override;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000067
mtklein36352bf2015-03-25 18:17:31 -070068 void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
egdaniel1a8ecdf2014-10-03 06:24:12 -070069
joshualittb0a8a372014-09-23 09:50:21 -070070 SkRRect fRRect;
71 GrPrimitiveEdgeType fEdgeType;
72 uint32_t fCircularCornerFlags;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000073
joshualittb0a8a372014-09-23 09:50:21 -070074 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000075
joshualittb0a8a372014-09-23 09:50:21 -070076 typedef GrFragmentProcessor INHERITED;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000077};
78
joshualittb0a8a372014-09-23 09:50:21 -070079GrFragmentProcessor* CircularRRectEffect::Create(GrPrimitiveEdgeType edgeType,
80 uint32_t circularCornerFlags,
81 const SkRRect& rrect) {
82 if (kFillAA_GrProcessorEdgeType != edgeType && kInverseFillAA_GrProcessorEdgeType != edgeType) {
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000083 return NULL;
84 }
bsalomon55fad7a2014-07-08 07:34:20 -070085 return SkNEW_ARGS(CircularRRectEffect, (edgeType, circularCornerFlags, rrect));
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000086}
87
egdaniel605dd0f2014-11-12 08:35:25 -080088void CircularRRectEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
joshualitt56995b52014-12-11 15:44:02 -080089 inout->mulByUnknownSingleComponent();
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000090}
91
joshualittb0a8a372014-09-23 09:50:21 -070092CircularRRectEffect::CircularRRectEffect(GrPrimitiveEdgeType edgeType, uint32_t circularCornerFlags,
93 const SkRRect& rrect)
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +000094 : fRRect(rrect)
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +000095 , fEdgeType(edgeType)
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000096 , fCircularCornerFlags(circularCornerFlags) {
joshualitteb2a6762014-12-04 11:35:33 -080097 this->initClassID<CircularRRectEffect>();
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000098 this->setWillReadFragmentPosition();
99}
100
bsalomon0e08fc12014-10-15 08:19:04 -0700101bool CircularRRectEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700102 const CircularRRectEffect& crre = other.cast<CircularRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000103 // The corner flags are derived from fRRect, so no need to check them.
104 return fEdgeType == crre.fEdgeType && fRRect == crre.fRRect;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000105}
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000106
107//////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000108
joshualittb0a8a372014-09-23 09:50:21 -0700109GR_DEFINE_FRAGMENT_PROCESSOR_TEST(CircularRRectEffect);
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000110
joshualittb0a8a372014-09-23 09:50:21 -0700111GrFragmentProcessor* CircularRRectEffect::TestCreate(SkRandom* random,
112 GrContext*,
113 const GrDrawTargetCaps& caps,
114 GrTexture*[]) {
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000115 SkScalar w = random->nextRangeScalar(20.f, 1000.f);
116 SkScalar h = random->nextRangeScalar(20.f, 1000.f);
117 SkScalar r = random->nextRangeF(kRadiusMin, 9.f);
118 SkRRect rrect;
119 rrect.setRectXY(SkRect::MakeWH(w, h), r, r);
joshualittb0a8a372014-09-23 09:50:21 -0700120 GrFragmentProcessor* fp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000121 do {
joshualittb0a8a372014-09-23 09:50:21 -0700122 GrPrimitiveEdgeType et =
123 (GrPrimitiveEdgeType)random->nextULessThan(kGrProcessorEdgeTypeCnt);
124 fp = GrRRectEffect::Create(et, rrect);
125 } while (NULL == fp);
126 return fp;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000127}
128
129//////////////////////////////////////////////////////////////////////////////
130
joshualittb0a8a372014-09-23 09:50:21 -0700131class GLCircularRRectEffect : public GrGLFragmentProcessor {
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000132public:
joshualitteb2a6762014-12-04 11:35:33 -0800133 GLCircularRRectEffect(const GrProcessor&);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000134
joshualitt15988992014-10-09 15:04:05 -0700135 virtual void emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700136 const GrFragmentProcessor& fp,
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000137 const char* outputColor,
138 const char* inputColor,
139 const TransformedCoordsArray&,
mtklein36352bf2015-03-25 18:17:31 -0700140 const TextureSamplerArray&) override;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000141
joshualittb0a8a372014-09-23 09:50:21 -0700142 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000143
mtklein36352bf2015-03-25 18:17:31 -0700144 void setData(const GrGLProgramDataManager&, const GrProcessor&) override;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000145
146private:
kkinnunen7510b222014-07-30 00:04:16 -0700147 GrGLProgramDataManager::UniformHandle fInnerRectUniform;
148 GrGLProgramDataManager::UniformHandle fRadiusPlusHalfUniform;
149 SkRRect fPrevRRect;
joshualittb0a8a372014-09-23 09:50:21 -0700150 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000151};
152
joshualitteb2a6762014-12-04 11:35:33 -0800153GLCircularRRectEffect::GLCircularRRectEffect(const GrProcessor& ) {
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000154 fPrevRRect.setEmpty();
155}
156
joshualitt15988992014-10-09 15:04:05 -0700157void GLCircularRRectEffect::emitCode(GrGLFPBuilder* builder,
joshualitt267ce482014-11-25 14:52:21 -0800158 const GrFragmentProcessor& fp,
159 const char* outputColor,
160 const char* inputColor,
161 const TransformedCoordsArray&,
162 const TextureSamplerArray& samplers) {
joshualittb0a8a372014-09-23 09:50:21 -0700163 const CircularRRectEffect& crre = fp.cast<CircularRRectEffect>();
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000164 const char *rectName;
165 const char *radiusPlusHalfName;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000166 // The inner rect is the rrect bounds inset by the radius. Its left, top, right, and bottom
167 // edges correspond to components x, y, z, and w, respectively. When a side of the rrect has
168 // only rectangular corners, that side's value corresponds to the rect edge's value outset by
169 // half a pixel.
joshualitt30ba4362014-08-21 20:18:45 -0700170 fInnerRectUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800171 kVec4f_GrSLType, kDefault_GrSLPrecision,
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000172 "innerRect",
173 &rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700174 fRadiusPlusHalfUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800175 kFloat_GrSLType, kDefault_GrSLPrecision,
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000176 "radiusPlusHalf",
177 &radiusPlusHalfName);
joshualitt30ba4362014-08-21 20:18:45 -0700178
joshualitt15988992014-10-09 15:04:05 -0700179 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700180 const char* fragmentPos = fsBuilder->fragmentPosition();
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000181 // At each quarter-circle corner we compute a vector that is the offset of the fragment position
182 // from the circle center. The vector is pinned in x and y to be in the quarter-plane relevant
183 // to that corner. This means that points near the interior near the rrect top edge will have
184 // a vector that points straight up for both the TL left and TR corners. Computing an
185 // alpha from this vector at either the TR or TL corner will give the correct result. Similarly,
186 // fragments near the other three edges will get the correct AA. Fragments in the interior of
187 // the rrect will have a (0,0) vector at all four corners. So long as the radius > 0.5 they will
188 // correctly produce an alpha value of 1 at all four corners. We take the min of all the alphas.
189 // The code below is a simplified version of the above that performs maxs on the vector
190 // components before computing distances and alpha values so that only one distance computation
191 // need be computed to determine the min alpha.
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000192 //
193 // For the cases where one half of the rrect is rectangular we drop one of the x or y
194 // computations, compute a separate rect edge alpha for the rect side, and mul the two computed
195 // alphas together.
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000196 switch (crre.getCircularCornerFlags()) {
197 case CircularRRectEffect::kAll_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700198 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
199 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
200 fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n");
201 fsBuilder->codeAppendf("\t\tfloat alpha = clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000202 radiusPlusHalfName);
203 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000204 case CircularRRectEffect::kTopLeft_CornerFlag:
joshualitt30ba4362014-08-21 20:18:45 -0700205 fsBuilder->codeAppendf("\t\tvec2 dxy = max(%s.xy - %s.xy, 0.0);\n",
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000206 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700207 fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000208 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700209 fsBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000210 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700211 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 +0000212 radiusPlusHalfName);
213 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000214 case CircularRRectEffect::kTopRight_CornerFlag:
joshualitt30ba4362014-08-21 20:18:45 -0700215 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 +0000216 fragmentPos, rectName, rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700217 fsBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000218 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700219 fsBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000220 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700221 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 +0000222 radiusPlusHalfName);
223 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000224 case CircularRRectEffect::kBottomRight_CornerFlag:
joshualitt30ba4362014-08-21 20:18:45 -0700225 fsBuilder->codeAppendf("\t\tvec2 dxy = max(%s.xy - %s.zw, 0.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 leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000228 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700229 fsBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000230 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700231 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 +0000232 radiusPlusHalfName);
233 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000234 case CircularRRectEffect::kBottomLeft_CornerFlag:
joshualitt30ba4362014-08-21 20:18:45 -0700235 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 +0000236 rectName, fragmentPos, fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700237 fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000238 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700239 fsBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000240 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700241 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 +0000242 radiusPlusHalfName);
243 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000244 case CircularRRectEffect::kLeft_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700245 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
246 fsBuilder->codeAppendf("\t\tfloat dy1 = %s.y - %s.w;\n", fragmentPos, rectName);
247 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(dxy0.x, max(dxy0.y, dy1)), 0.0);\n");
248 fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000249 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700250 fsBuilder->codeAppendf("\t\tfloat alpha = rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000251 radiusPlusHalfName);
252 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000253 case CircularRRectEffect::kTop_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 dx1 = %s.x - %s.z;\n", fragmentPos, rectName);
256 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(max(dxy0.x, dx1), dxy0.y), 0.0);\n");
257 fsBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000258 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700259 fsBuilder->codeAppendf("\t\tfloat alpha = bottomAlpha * 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::kRight_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700263 fsBuilder->codeAppendf("\t\tfloat dy0 = %s.y - %s.y;\n", rectName, fragmentPos);
264 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
265 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(dxy1.x, max(dy0, dxy1.y)), 0.0);\n");
266 fsBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000267 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700268 fsBuilder->codeAppendf("\t\tfloat alpha = leftAlpha * 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::kBottom_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700272 fsBuilder->codeAppendf("\t\tfloat dx0 = %s.x - %s.x;\n", rectName, fragmentPos);
273 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
274 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(max(dx0, dxy1.x), dxy1.y), 0.0);\n");
275 fsBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000276 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700277 fsBuilder->codeAppendf("\t\tfloat alpha = topAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000278 radiusPlusHalfName);
279 break;
280 }
skia.committer@gmail.com06acb582014-03-06 03:02:32 +0000281
joshualittb0a8a372014-09-23 09:50:21 -0700282 if (kInverseFillAA_GrProcessorEdgeType == crre.getEdgeType()) {
joshualitt30ba4362014-08-21 20:18:45 -0700283 fsBuilder->codeAppend("\t\talpha = 1.0 - alpha;\n");
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000284 }
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000285
joshualitt30ba4362014-08-21 20:18:45 -0700286 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor,
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000287 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
288}
289
joshualittb0a8a372014-09-23 09:50:21 -0700290void GLCircularRRectEffect::GenKey(const GrProcessor& processor, const GrGLCaps&,
291 GrProcessorKeyBuilder* b) {
292 const CircularRRectEffect& crre = processor.cast<CircularRRectEffect>();
293 GR_STATIC_ASSERT(kGrProcessorEdgeTypeCnt <= 8);
bsalomon63e99f72014-07-21 08:03:14 -0700294 b->add32((crre.getCircularCornerFlags() << 3) | crre.getEdgeType());
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000295}
296
kkinnunen7510b222014-07-30 00:04:16 -0700297void GLCircularRRectEffect::setData(const GrGLProgramDataManager& pdman,
joshualittb0a8a372014-09-23 09:50:21 -0700298 const GrProcessor& processor) {
299 const CircularRRectEffect& crre = processor.cast<CircularRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000300 const SkRRect& rrect = crre.getRRect();
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000301 if (rrect != fPrevRRect) {
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000302 SkRect rect = rrect.getBounds();
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000303 SkScalar radius = 0;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000304 switch (crre.getCircularCornerFlags()) {
305 case CircularRRectEffect::kAll_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000306 SkASSERT(rrect.isSimpleCircular());
307 radius = rrect.getSimpleRadii().fX;
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000308 SkASSERT(radius >= kRadiusMin);
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000309 rect.inset(radius, radius);
310 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000311 case CircularRRectEffect::kTopLeft_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000312 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
313 rect.fLeft += radius;
314 rect.fTop += radius;
315 rect.fRight += 0.5f;
316 rect.fBottom += 0.5f;
317 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000318 case CircularRRectEffect::kTopRight_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000319 radius = rrect.radii(SkRRect::kUpperRight_Corner).fX;
bsalomon@google.comde9f2512014-03-11 17:09:17 +0000320 rect.fLeft -= 0.5f;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000321 rect.fTop += radius;
322 rect.fRight -= radius;
323 rect.fBottom += 0.5f;
324 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000325 case CircularRRectEffect::kBottomRight_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000326 radius = rrect.radii(SkRRect::kLowerRight_Corner).fX;
bsalomon@google.comde9f2512014-03-11 17:09:17 +0000327 rect.fLeft -= 0.5f;
328 rect.fTop -= 0.5f;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000329 rect.fRight -= radius;
330 rect.fBottom -= radius;
331 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000332 case CircularRRectEffect::kBottomLeft_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000333 radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX;
334 rect.fLeft += radius;
bsalomon@google.comde9f2512014-03-11 17:09:17 +0000335 rect.fTop -= 0.5f;
336 rect.fRight += 0.5f;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000337 rect.fBottom -= radius;
338 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000339 case CircularRRectEffect::kLeft_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000340 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
341 rect.fLeft += radius;
342 rect.fTop += radius;
343 rect.fRight += 0.5f;
344 rect.fBottom -= radius;
345 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000346 case CircularRRectEffect::kTop_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000347 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
348 rect.fLeft += radius;
349 rect.fTop += radius;
350 rect.fRight -= radius;
351 rect.fBottom += 0.5f;
352 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000353 case CircularRRectEffect::kRight_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000354 radius = rrect.radii(SkRRect::kUpperRight_Corner).fX;
355 rect.fLeft -= 0.5f;
356 rect.fTop += radius;
357 rect.fRight -= radius;
358 rect.fBottom -= radius;
359 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000360 case CircularRRectEffect::kBottom_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000361 radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX;
362 rect.fLeft += radius;
363 rect.fTop -= 0.5f;
364 rect.fRight -= radius;
365 rect.fBottom -= radius;
366 break;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000367 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000368 SkFAIL("Should have been one of the above cases.");
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000369 }
kkinnunen7510b222014-07-30 00:04:16 -0700370 pdman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
371 pdman.set1f(fRadiusPlusHalfUniform, radius + 0.5f);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000372 fPrevRRect = rrect;
373 }
374}
375
joshualitteb2a6762014-12-04 11:35:33 -0800376////////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000377
joshualitteb2a6762014-12-04 11:35:33 -0800378void CircularRRectEffect::getGLProcessorKey(const GrGLCaps& caps,
379 GrProcessorKeyBuilder* b) const {
380 GLCircularRRectEffect::GenKey(*this, caps, b);
381}
382
383GrGLFragmentProcessor* CircularRRectEffect::createGLInstance() const {
384 return SkNEW_ARGS(GLCircularRRectEffect, (*this));
385}
386
387//////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000388
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() {};
joshualitteb2a6762014-12-04 11:35:33 -0800394
mtklein36352bf2015-03-25 18:17:31 -0700395 const char* name() const override { return "EllipticalRRect"; }
joshualitteb2a6762014-12-04 11:35:33 -0800396
mtklein36352bf2015-03-25 18:17:31 -0700397 void getGLProcessorKey(const GrGLCaps&, GrProcessorKeyBuilder*) const override;
joshualitteb2a6762014-12-04 11:35:33 -0800398
mtklein36352bf2015-03-25 18:17:31 -0700399 GrGLFragmentProcessor* createGLInstance() const override;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000400
401 const SkRRect& getRRect() const { return fRRect; }
402
joshualittb0a8a372014-09-23 09:50:21 -0700403 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000404
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000405private:
joshualittb0a8a372014-09-23 09:50:21 -0700406 EllipticalRRectEffect(GrPrimitiveEdgeType, const SkRRect&);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000407
mtklein36352bf2015-03-25 18:17:31 -0700408 bool onIsEqual(const GrFragmentProcessor& other) const override;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000409
mtklein36352bf2015-03-25 18:17:31 -0700410 void onComputeInvariantOutput(GrInvariantOutput* inout) const 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 {
joshualitt56995b52014-12-11 15:44:02 -0800429 inout->mulByUnknownSingleComponent();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000430}
431
joshualittb0a8a372014-09-23 09:50:21 -0700432EllipticalRRectEffect::EllipticalRRectEffect(GrPrimitiveEdgeType edgeType, const SkRRect& rrect)
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000433 : fRRect(rrect)
joshualitteb2a6762014-12-04 11:35:33 -0800434 , fEdgeType(edgeType) {
435 this->initClassID<EllipticalRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000436 this->setWillReadFragmentPosition();
437}
438
bsalomon0e08fc12014-10-15 08:19:04 -0700439bool EllipticalRRectEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700440 const EllipticalRRectEffect& erre = other.cast<EllipticalRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000441 return fEdgeType == erre.fEdgeType && fRRect == erre.fRRect;
442}
443
444//////////////////////////////////////////////////////////////////////////////
445
joshualittb0a8a372014-09-23 09:50:21 -0700446GR_DEFINE_FRAGMENT_PROCESSOR_TEST(EllipticalRRectEffect);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000447
joshualittb0a8a372014-09-23 09:50:21 -0700448GrFragmentProcessor* EllipticalRRectEffect::TestCreate(SkRandom* random,
449 GrContext*,
450 const GrDrawTargetCaps& caps,
451 GrTexture*[]) {
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000452 SkScalar w = random->nextRangeScalar(20.f, 1000.f);
453 SkScalar h = random->nextRangeScalar(20.f, 1000.f);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000454 SkVector r[4];
455 r[SkRRect::kUpperLeft_Corner].fX = random->nextRangeF(kRadiusMin, 9.f);
456 // ensure at least one corner really is elliptical
457 do {
458 r[SkRRect::kUpperLeft_Corner].fY = random->nextRangeF(kRadiusMin, 9.f);
459 } while (r[SkRRect::kUpperLeft_Corner].fY == r[SkRRect::kUpperLeft_Corner].fX);
460
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000461 SkRRect rrect;
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000462 if (random->nextBool()) {
463 // half the time create a four-radii rrect.
464 r[SkRRect::kLowerRight_Corner].fX = random->nextRangeF(kRadiusMin, 9.f);
465 r[SkRRect::kLowerRight_Corner].fY = random->nextRangeF(kRadiusMin, 9.f);
466
467 r[SkRRect::kUpperRight_Corner].fX = r[SkRRect::kLowerRight_Corner].fX;
468 r[SkRRect::kUpperRight_Corner].fY = r[SkRRect::kUpperLeft_Corner].fY;
469
470 r[SkRRect::kLowerLeft_Corner].fX = r[SkRRect::kUpperLeft_Corner].fX;
471 r[SkRRect::kLowerLeft_Corner].fY = r[SkRRect::kLowerRight_Corner].fY;
472
473 rrect.setRectRadii(SkRect::MakeWH(w, h), r);
474 } else {
475 rrect.setRectXY(SkRect::MakeWH(w, h), r[SkRRect::kUpperLeft_Corner].fX,
476 r[SkRRect::kUpperLeft_Corner].fY);
477 }
joshualittb0a8a372014-09-23 09:50:21 -0700478 GrFragmentProcessor* fp;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000479 do {
joshualittb0a8a372014-09-23 09:50:21 -0700480 GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)random->nextULessThan(kGrProcessorEdgeTypeCnt);
481 fp = GrRRectEffect::Create(et, rrect);
482 } while (NULL == fp);
483 return fp;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000484}
485
486//////////////////////////////////////////////////////////////////////////////
487
joshualittb0a8a372014-09-23 09:50:21 -0700488class GLEllipticalRRectEffect : public GrGLFragmentProcessor {
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000489public:
joshualitteb2a6762014-12-04 11:35:33 -0800490 GLEllipticalRRectEffect(const GrProcessor&);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000491
joshualitt15988992014-10-09 15:04:05 -0700492 virtual void emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700493 const GrFragmentProcessor& effect,
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000494 const char* outputColor,
495 const char* inputColor,
496 const TransformedCoordsArray&,
mtklein36352bf2015-03-25 18:17:31 -0700497 const TextureSamplerArray&) override;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000498
joshualittb0a8a372014-09-23 09:50:21 -0700499 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000500
mtklein36352bf2015-03-25 18:17:31 -0700501 void setData(const GrGLProgramDataManager&, const GrProcessor&) override;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000502
503private:
kkinnunen7510b222014-07-30 00:04:16 -0700504 GrGLProgramDataManager::UniformHandle fInnerRectUniform;
505 GrGLProgramDataManager::UniformHandle fInvRadiiSqdUniform;
506 SkRRect fPrevRRect;
joshualittb0a8a372014-09-23 09:50:21 -0700507 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000508};
509
joshualitteb2a6762014-12-04 11:35:33 -0800510GLEllipticalRRectEffect::GLEllipticalRRectEffect(const GrProcessor& effect) {
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000511 fPrevRRect.setEmpty();
512}
513
joshualitt15988992014-10-09 15:04:05 -0700514void GLEllipticalRRectEffect::emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700515 const GrFragmentProcessor& effect,
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000516 const char* outputColor,
517 const char* inputColor,
518 const TransformedCoordsArray&,
519 const TextureSamplerArray& samplers) {
joshualitt49586be2014-09-16 08:21:41 -0700520 const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000521 const char *rectName;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000522 // The inner rect is the rrect bounds inset by the x/y radii
joshualitt30ba4362014-08-21 20:18:45 -0700523 fInnerRectUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800524 kVec4f_GrSLType, kDefault_GrSLPrecision,
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000525 "innerRect",
526 &rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700527
joshualitt15988992014-10-09 15:04:05 -0700528 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700529 const char* fragmentPos = fsBuilder->fragmentPosition();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000530 // At each quarter-ellipse corner we compute a vector that is the offset of the fragment pos
531 // to the ellipse center. The vector is pinned in x and y to be in the quarter-plane relevant
532 // to that corner. This means that points near the interior near the rrect top edge will have
533 // a vector that points straight up for both the TL left and TR corners. Computing an
534 // alpha from this vector at either the TR or TL corner will give the correct result. Similarly,
535 // fragments near the other three edges will get the correct AA. Fragments in the interior of
536 // the rrect will have a (0,0) vector at all four corners. So long as the radii > 0.5 they will
537 // correctly produce an alpha value of 1 at all four corners. We take the min of all the alphas.
538 // The code below is a simplified version of the above that performs maxs on the vector
539 // components before computing distances and alpha values so that only one distance computation
540 // need be computed to determine the min alpha.
joshualitt30ba4362014-08-21 20:18:45 -0700541 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
542 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000543 switch (erre.getRRect().getType()) {
544 case SkRRect::kSimple_Type: {
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000545 const char *invRadiiXYSqdName;
joshualitt30ba4362014-08-21 20:18:45 -0700546 fInvRadiiSqdUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800547 kVec2f_GrSLType, kDefault_GrSLPrecision,
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000548 "invRadiiXY",
549 &invRadiiXYSqdName);
joshualitt30ba4362014-08-21 20:18:45 -0700550 fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000551 // Z is the x/y offsets divided by squared radii.
joshualitt30ba4362014-08-21 20:18:45 -0700552 fsBuilder->codeAppendf("\t\tvec2 Z = dxy * %s;\n", invRadiiXYSqdName);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000553 break;
554 }
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000555 case SkRRect::kNinePatch_Type: {
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000556 const char *invRadiiLTRBSqdName;
joshualitt30ba4362014-08-21 20:18:45 -0700557 fInvRadiiSqdUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800558 kVec4f_GrSLType, kDefault_GrSLPrecision,
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000559 "invRadiiLTRB",
560 &invRadiiLTRBSqdName);
joshualitt30ba4362014-08-21 20:18:45 -0700561 fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000562 // Z is the x/y offsets divided by squared radii. We only care about the (at most) one
563 // corner where both the x and y offsets are positive, hence the maxes. (The inverse
564 // squared radii will always be positive.)
joshualitt30ba4362014-08-21 20:18:45 -0700565 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 +0000566 invRadiiLTRBSqdName, invRadiiLTRBSqdName);
567 break;
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000568 }
569 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000570 SkFAIL("RRect should always be simple or nine-patch.");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000571 }
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000572 // implicit is the evaluation of (x/a)^2 + (y/b)^2 - 1.
joshualitt30ba4362014-08-21 20:18:45 -0700573 fsBuilder->codeAppend("\t\tfloat implicit = dot(Z, dxy) - 1.0;\n");
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000574 // grad_dot is the squared length of the gradient of the implicit.
joshualitt30ba4362014-08-21 20:18:45 -0700575 fsBuilder->codeAppendf("\t\tfloat grad_dot = 4.0 * dot(Z, Z);\n");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000576 // avoid calling inversesqrt on zero.
joshualitt30ba4362014-08-21 20:18:45 -0700577 fsBuilder->codeAppend("\t\tgrad_dot = max(grad_dot, 1.0e-4);\n");
578 fsBuilder->codeAppendf("\t\tfloat approx_dist = implicit * inversesqrt(grad_dot);\n");
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000579
joshualittb0a8a372014-09-23 09:50:21 -0700580 if (kFillAA_GrProcessorEdgeType == erre.getEdgeType()) {
joshualitt30ba4362014-08-21 20:18:45 -0700581 fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 - approx_dist, 0.0, 1.0);\n");
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000582 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700583 fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 + approx_dist, 0.0, 1.0);\n");
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000584 }
585
joshualitt30ba4362014-08-21 20:18:45 -0700586 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor,
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000587 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
588}
589
joshualittb0a8a372014-09-23 09:50:21 -0700590void GLEllipticalRRectEffect::GenKey(const GrProcessor& effect, const GrGLCaps&,
591 GrProcessorKeyBuilder* b) {
joshualitt49586be2014-09-16 08:21:41 -0700592 const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>();
joshualittb0a8a372014-09-23 09:50:21 -0700593 GR_STATIC_ASSERT(kLast_GrProcessorEdgeType < (1 << 3));
bsalomon63e99f72014-07-21 08:03:14 -0700594 b->add32(erre.getRRect().getType() | erre.getEdgeType() << 3);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000595}
596
kkinnunen7510b222014-07-30 00:04:16 -0700597void GLEllipticalRRectEffect::setData(const GrGLProgramDataManager& pdman,
joshualittb0a8a372014-09-23 09:50:21 -0700598 const GrProcessor& effect) {
joshualitt49586be2014-09-16 08:21:41 -0700599 const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000600 const SkRRect& rrect = erre.getRRect();
601 if (rrect != fPrevRRect) {
602 SkRect rect = rrect.getBounds();
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000603 const SkVector& r0 = rrect.radii(SkRRect::kUpperLeft_Corner);
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000604 SkASSERT(r0.fX >= kRadiusMin);
605 SkASSERT(r0.fY >= kRadiusMin);
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000606 switch (erre.getRRect().getType()) {
607 case SkRRect::kSimple_Type:
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000608 rect.inset(r0.fX, r0.fY);
kkinnunen7510b222014-07-30 00:04:16 -0700609 pdman.set2f(fInvRadiiSqdUniform, 1.f / (r0.fX * r0.fX),
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000610 1.f / (r0.fY * r0.fY));
611 break;
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000612 case SkRRect::kNinePatch_Type: {
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000613 const SkVector& r1 = rrect.radii(SkRRect::kLowerRight_Corner);
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000614 SkASSERT(r1.fX >= kRadiusMin);
615 SkASSERT(r1.fY >= kRadiusMin);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000616 rect.fLeft += r0.fX;
617 rect.fTop += r0.fY;
618 rect.fRight -= r1.fX;
619 rect.fBottom -= r1.fY;
kkinnunen7510b222014-07-30 00:04:16 -0700620 pdman.set4f(fInvRadiiSqdUniform, 1.f / (r0.fX * r0.fX),
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000621 1.f / (r0.fY * r0.fY),
622 1.f / (r1.fX * r1.fX),
623 1.f / (r1.fY * r1.fY));
624 break;
625 }
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000626 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000627 SkFAIL("RRect should always be simple or nine-patch.");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000628 }
kkinnunen7510b222014-07-30 00:04:16 -0700629 pdman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000630 fPrevRRect = rrect;
631 }
632}
633
joshualitteb2a6762014-12-04 11:35:33 -0800634////////////////////////////////////////////////////////////////////////////////////////////////////
635
636void EllipticalRRectEffect::getGLProcessorKey(const GrGLCaps& caps,
637 GrProcessorKeyBuilder* b) const {
638 GLEllipticalRRectEffect::GenKey(*this, caps, b);
639}
640
641GrGLFragmentProcessor* EllipticalRRectEffect::createGLInstance() const {
642 return SkNEW_ARGS(GLEllipticalRRectEffect, (*this));
643}
644
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000645//////////////////////////////////////////////////////////////////////////////
646
joshualittb0a8a372014-09-23 09:50:21 -0700647GrFragmentProcessor* GrRRectEffect::Create(GrPrimitiveEdgeType edgeType, const SkRRect& rrect) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000648 if (rrect.isRect()) {
649 return GrConvexPolyEffect::Create(edgeType, rrect.getBounds());
650 }
651
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000652 if (rrect.isOval()) {
653 return GrOvalEffect::Create(edgeType, rrect.getBounds());
654 }
655
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000656 if (rrect.isSimple()) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000657 if (rrect.getSimpleRadii().fX < kRadiusMin || rrect.getSimpleRadii().fY < kRadiusMin) {
658 // In this case the corners are extremely close to rectangular and we collapse the
659 // clip to a rectangular clip.
660 return GrConvexPolyEffect::Create(edgeType, rrect.getBounds());
661 }
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000662 if (rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY) {
skia.committer@gmail.com6e4eb212014-03-25 03:02:32 +0000663 return CircularRRectEffect::Create(edgeType, CircularRRectEffect::kAll_CornerFlags,
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000664 rrect);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000665 } else {
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000666 return EllipticalRRectEffect::Create(edgeType, rrect);
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000667 }
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000668 }
669
670 if (rrect.isComplex() || rrect.isNinePatch()) {
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000671 // Check for the "tab" cases - two adjacent circular corners and two square corners.
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000672 SkScalar circularRadius = 0;
673 uint32_t cornerFlags = 0;
674
675 SkVector radii[4];
676 bool squashedRadii = false;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000677 for (int c = 0; c < 4; ++c) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000678 radii[c] = rrect.radii((SkRRect::Corner)c);
679 SkASSERT((0 == radii[c].fX) == (0 == radii[c].fY));
680 if (0 == radii[c].fX) {
681 // The corner is square, so no need to squash or flag as circular.
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000682 continue;
683 }
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000684 if (radii[c].fX < kRadiusMin || radii[c].fY < kRadiusMin) {
685 radii[c].set(0, 0);
686 squashedRadii = true;
687 continue;
688 }
689 if (radii[c].fX != radii[c].fY) {
bsalomon@google.com44a435b2014-03-13 19:20:32 +0000690 cornerFlags = ~0U;
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000691 break;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000692 }
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000693 if (!cornerFlags) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000694 circularRadius = radii[c].fX;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000695 cornerFlags = 1 << c;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000696 } else {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000697 if (radii[c].fX != circularRadius) {
bsalomon@google.com44a435b2014-03-13 19:20:32 +0000698 cornerFlags = ~0U;
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000699 break;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000700 }
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000701 cornerFlags |= 1 << c;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000702 }
703 }
704
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000705 switch (cornerFlags) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000706 case CircularRRectEffect::kAll_CornerFlags:
707 // This rrect should have been caught in the simple case above. Though, it would
708 // be correctly handled in the fallthrough code.
709 SkASSERT(false);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000710 case CircularRRectEffect::kTopLeft_CornerFlag:
711 case CircularRRectEffect::kTopRight_CornerFlag:
712 case CircularRRectEffect::kBottomRight_CornerFlag:
713 case CircularRRectEffect::kBottomLeft_CornerFlag:
714 case CircularRRectEffect::kLeft_CornerFlags:
715 case CircularRRectEffect::kTop_CornerFlags:
716 case CircularRRectEffect::kRight_CornerFlags:
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000717 case CircularRRectEffect::kBottom_CornerFlags: {
718 SkTCopyOnFirstWrite<SkRRect> rr(rrect);
719 if (squashedRadii) {
720 rr.writable()->setRectRadii(rrect.getBounds(), radii);
721 }
722 return CircularRRectEffect::Create(edgeType, cornerFlags, *rr);
723 }
724 case CircularRRectEffect::kNone_CornerFlags:
725 return GrConvexPolyEffect::Create(edgeType, rrect.getBounds());
726 default: {
727 if (squashedRadii) {
728 // If we got here then we squashed some but not all the radii to zero. (If all
729 // had been squashed cornerFlags would be 0.) The elliptical effect doesn't
730 // support some rounded and some square corners.
731 return NULL;
732 }
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000733 if (rrect.isNinePatch()) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000734 return EllipticalRRectEffect::Create(edgeType, rrect);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000735 }
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000736 return NULL;
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000737 }
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000738 }
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000739 }
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000740
741 return NULL;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000742}