blob: b5b95679f5e6c29e1f7a248d9c67f0222f32ece3 [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
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000052 const SkRRect& getRRect() const { return fRRect; }
53
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000054 uint32_t getCircularCornerFlags() const { return fCircularCornerFlags; }
skia.committer@gmail.com06acb582014-03-06 03:02:32 +000055
joshualittb0a8a372014-09-23 09:50:21 -070056 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +000057
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000058private:
joshualittb0a8a372014-09-23 09:50:21 -070059 CircularRRectEffect(GrPrimitiveEdgeType, uint32_t circularCornerFlags, const SkRRect&);
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000060
wangyixb1daa862015-08-18 11:29:31 -070061 GrGLFragmentProcessor* onCreateGLInstance() const override;
62
wangyix4b3050b2015-08-04 07:59:37 -070063 void onGetGLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
64
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) {
halcanary96fcdcc2015-08-27 07:41:13 -070082 return nullptr;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000083 }
halcanary385fe4d2015-08-26 13:07:48 -070084 return new 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
bsalomonc21b09e2015-08-28 18:46:56 -0700110const GrFragmentProcessor* CircularRRectEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700111 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);
halcanary96fcdcc2015-08-27 07:41:13 -0700121 } while (nullptr == fp);
joshualittb0a8a372014-09-23 09:50:21 -0700122 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
wangyix7c157a92015-07-22 15:08:53 -0700131 virtual void emitCode(EmitArgs&) override;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000132
jvanverthcfc18862015-04-28 08:48:20 -0700133 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000134
wangyixb1daa862015-08-18 11:29:31 -0700135protected:
136 void onSetData(const GrGLProgramDataManager&, const GrProcessor&) override;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000137
138private:
kkinnunen7510b222014-07-30 00:04:16 -0700139 GrGLProgramDataManager::UniformHandle fInnerRectUniform;
140 GrGLProgramDataManager::UniformHandle fRadiusPlusHalfUniform;
141 SkRRect fPrevRRect;
joshualittb0a8a372014-09-23 09:50:21 -0700142 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000143};
144
joshualitteb2a6762014-12-04 11:35:33 -0800145GLCircularRRectEffect::GLCircularRRectEffect(const GrProcessor& ) {
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000146 fPrevRRect.setEmpty();
147}
148
wangyix7c157a92015-07-22 15:08:53 -0700149void GLCircularRRectEffect::emitCode(EmitArgs& args) {
150 const CircularRRectEffect& crre = args.fFp.cast<CircularRRectEffect>();
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000151 const char *rectName;
152 const char *radiusPlusHalfName;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000153 // The inner rect is the rrect bounds inset by the radius. Its left, top, right, and bottom
154 // edges correspond to components x, y, z, and w, respectively. When a side of the rrect has
155 // only rectangular corners, that side's value corresponds to the rect edge's value outset by
156 // half a pixel.
wangyix7c157a92015-07-22 15:08:53 -0700157 fInnerRectUniform = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800158 kVec4f_GrSLType, kDefault_GrSLPrecision,
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000159 "innerRect",
160 &rectName);
wangyix7c157a92015-07-22 15:08:53 -0700161 fRadiusPlusHalfUniform = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800162 kFloat_GrSLType, kDefault_GrSLPrecision,
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000163 "radiusPlusHalf",
164 &radiusPlusHalfName);
joshualitt30ba4362014-08-21 20:18:45 -0700165
wangyix7c157a92015-07-22 15:08:53 -0700166 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700167 const char* fragmentPos = fsBuilder->fragmentPosition();
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000168 // At each quarter-circle corner we compute a vector that is the offset of the fragment position
169 // from the circle center. The vector is pinned in x and y to be in the quarter-plane relevant
170 // to that corner. This means that points near the interior near the rrect top edge will have
171 // a vector that points straight up for both the TL left and TR corners. Computing an
172 // alpha from this vector at either the TR or TL corner will give the correct result. Similarly,
173 // fragments near the other three edges will get the correct AA. Fragments in the interior of
174 // the rrect will have a (0,0) vector at all four corners. So long as the radius > 0.5 they will
175 // correctly produce an alpha value of 1 at all four corners. We take the min of all the alphas.
176 // The code below is a simplified version of the above that performs maxs on the vector
177 // components before computing distances and alpha values so that only one distance computation
178 // need be computed to determine the min alpha.
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000179 //
180 // For the cases where one half of the rrect is rectangular we drop one of the x or y
181 // computations, compute a separate rect edge alpha for the rect side, and mul the two computed
182 // alphas together.
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000183 switch (crre.getCircularCornerFlags()) {
184 case CircularRRectEffect::kAll_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700185 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
186 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
187 fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n");
188 fsBuilder->codeAppendf("\t\tfloat alpha = clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000189 radiusPlusHalfName);
190 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000191 case CircularRRectEffect::kTopLeft_CornerFlag:
joshualitt30ba4362014-08-21 20:18:45 -0700192 fsBuilder->codeAppendf("\t\tvec2 dxy = max(%s.xy - %s.xy, 0.0);\n",
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000193 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700194 fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000195 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700196 fsBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000197 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700198 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 +0000199 radiusPlusHalfName);
200 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000201 case CircularRRectEffect::kTopRight_CornerFlag:
joshualitt30ba4362014-08-21 20:18:45 -0700202 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 +0000203 fragmentPos, rectName, rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700204 fsBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000205 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700206 fsBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000207 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700208 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 +0000209 radiusPlusHalfName);
210 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000211 case CircularRRectEffect::kBottomRight_CornerFlag:
joshualitt30ba4362014-08-21 20:18:45 -0700212 fsBuilder->codeAppendf("\t\tvec2 dxy = max(%s.xy - %s.zw, 0.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000213 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700214 fsBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000215 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700216 fsBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000217 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700218 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 +0000219 radiusPlusHalfName);
220 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000221 case CircularRRectEffect::kBottomLeft_CornerFlag:
joshualitt30ba4362014-08-21 20:18:45 -0700222 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 +0000223 rectName, fragmentPos, fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700224 fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000225 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700226 fsBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000227 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700228 fsBuilder->codeAppendf("\t\tfloat alpha = topAlpha * rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000229 radiusPlusHalfName);
230 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000231 case CircularRRectEffect::kLeft_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700232 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
233 fsBuilder->codeAppendf("\t\tfloat dy1 = %s.y - %s.w;\n", fragmentPos, rectName);
234 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(dxy0.x, max(dxy0.y, dy1)), 0.0);\n");
235 fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000236 rectName, fragmentPos);
joshualitt30ba4362014-08-21 20:18:45 -0700237 fsBuilder->codeAppendf("\t\tfloat alpha = rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000238 radiusPlusHalfName);
239 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000240 case CircularRRectEffect::kTop_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 dx1 = %s.x - %s.z;\n", fragmentPos, rectName);
243 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(max(dxy0.x, dx1), dxy0.y), 0.0);\n");
244 fsBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 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 = bottomAlpha * 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::kRight_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700250 fsBuilder->codeAppendf("\t\tfloat dy0 = %s.y - %s.y;\n", rectName, fragmentPos);
251 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
252 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(dxy1.x, max(dy0, dxy1.y)), 0.0);\n");
253 fsBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000254 fragmentPos, rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700255 fsBuilder->codeAppendf("\t\tfloat alpha = leftAlpha * 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::kBottom_CornerFlags:
joshualitt30ba4362014-08-21 20:18:45 -0700259 fsBuilder->codeAppendf("\t\tfloat dx0 = %s.x - %s.x;\n", rectName, fragmentPos);
260 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
261 fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(max(dx0, dxy1.x), dxy1.y), 0.0);\n");
262 fsBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 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 = topAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n",
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000265 radiusPlusHalfName);
266 break;
267 }
skia.committer@gmail.com06acb582014-03-06 03:02:32 +0000268
joshualittb0a8a372014-09-23 09:50:21 -0700269 if (kInverseFillAA_GrProcessorEdgeType == crre.getEdgeType()) {
joshualitt30ba4362014-08-21 20:18:45 -0700270 fsBuilder->codeAppend("\t\talpha = 1.0 - alpha;\n");
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000271 }
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000272
wangyix7c157a92015-07-22 15:08:53 -0700273 fsBuilder->codeAppendf("\t\t%s = %s;\n", args.fOutputColor,
274 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha")).c_str());
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000275}
276
jvanverthcfc18862015-04-28 08:48:20 -0700277void GLCircularRRectEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700278 GrProcessorKeyBuilder* b) {
279 const CircularRRectEffect& crre = processor.cast<CircularRRectEffect>();
280 GR_STATIC_ASSERT(kGrProcessorEdgeTypeCnt <= 8);
bsalomon63e99f72014-07-21 08:03:14 -0700281 b->add32((crre.getCircularCornerFlags() << 3) | crre.getEdgeType());
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000282}
283
wangyixb1daa862015-08-18 11:29:31 -0700284void GLCircularRRectEffect::onSetData(const GrGLProgramDataManager& pdman,
joshualittb0a8a372014-09-23 09:50:21 -0700285 const GrProcessor& processor) {
286 const CircularRRectEffect& crre = processor.cast<CircularRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000287 const SkRRect& rrect = crre.getRRect();
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000288 if (rrect != fPrevRRect) {
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000289 SkRect rect = rrect.getBounds();
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000290 SkScalar radius = 0;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000291 switch (crre.getCircularCornerFlags()) {
292 case CircularRRectEffect::kAll_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000293 SkASSERT(rrect.isSimpleCircular());
294 radius = rrect.getSimpleRadii().fX;
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000295 SkASSERT(radius >= kRadiusMin);
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000296 rect.inset(radius, radius);
297 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000298 case CircularRRectEffect::kTopLeft_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000299 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
300 rect.fLeft += radius;
301 rect.fTop += radius;
302 rect.fRight += 0.5f;
303 rect.fBottom += 0.5f;
304 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000305 case CircularRRectEffect::kTopRight_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000306 radius = rrect.radii(SkRRect::kUpperRight_Corner).fX;
bsalomon@google.comde9f2512014-03-11 17:09:17 +0000307 rect.fLeft -= 0.5f;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000308 rect.fTop += radius;
309 rect.fRight -= radius;
310 rect.fBottom += 0.5f;
311 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000312 case CircularRRectEffect::kBottomRight_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000313 radius = rrect.radii(SkRRect::kLowerRight_Corner).fX;
bsalomon@google.comde9f2512014-03-11 17:09:17 +0000314 rect.fLeft -= 0.5f;
315 rect.fTop -= 0.5f;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000316 rect.fRight -= radius;
317 rect.fBottom -= radius;
318 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000319 case CircularRRectEffect::kBottomLeft_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000320 radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX;
321 rect.fLeft += radius;
bsalomon@google.comde9f2512014-03-11 17:09:17 +0000322 rect.fTop -= 0.5f;
323 rect.fRight += 0.5f;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000324 rect.fBottom -= radius;
325 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000326 case CircularRRectEffect::kLeft_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000327 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
328 rect.fLeft += radius;
329 rect.fTop += radius;
330 rect.fRight += 0.5f;
331 rect.fBottom -= radius;
332 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000333 case CircularRRectEffect::kTop_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000334 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
335 rect.fLeft += radius;
336 rect.fTop += radius;
337 rect.fRight -= radius;
338 rect.fBottom += 0.5f;
339 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000340 case CircularRRectEffect::kRight_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000341 radius = rrect.radii(SkRRect::kUpperRight_Corner).fX;
342 rect.fLeft -= 0.5f;
343 rect.fTop += radius;
344 rect.fRight -= radius;
345 rect.fBottom -= radius;
346 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000347 case CircularRRectEffect::kBottom_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000348 radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX;
349 rect.fLeft += radius;
350 rect.fTop -= 0.5f;
351 rect.fRight -= radius;
352 rect.fBottom -= radius;
353 break;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000354 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000355 SkFAIL("Should have been one of the above cases.");
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000356 }
kkinnunen7510b222014-07-30 00:04:16 -0700357 pdman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
358 pdman.set1f(fRadiusPlusHalfUniform, radius + 0.5f);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000359 fPrevRRect = rrect;
360 }
361}
362
joshualitteb2a6762014-12-04 11:35:33 -0800363////////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000364
wangyix4b3050b2015-08-04 07:59:37 -0700365void CircularRRectEffect::onGetGLProcessorKey(const GrGLSLCaps& caps,
joshualitteb2a6762014-12-04 11:35:33 -0800366 GrProcessorKeyBuilder* b) const {
367 GLCircularRRectEffect::GenKey(*this, caps, b);
368}
369
wangyixb1daa862015-08-18 11:29:31 -0700370GrGLFragmentProcessor* CircularRRectEffect::onCreateGLInstance() const {
halcanary385fe4d2015-08-26 13:07:48 -0700371 return new GLCircularRRectEffect(*this);
joshualitteb2a6762014-12-04 11:35:33 -0800372}
373
374//////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000375
joshualittb0a8a372014-09-23 09:50:21 -0700376class EllipticalRRectEffect : public GrFragmentProcessor {
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000377public:
joshualittb0a8a372014-09-23 09:50:21 -0700378 static GrFragmentProcessor* Create(GrPrimitiveEdgeType, const SkRRect&);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000379
380 virtual ~EllipticalRRectEffect() {};
joshualitteb2a6762014-12-04 11:35:33 -0800381
mtklein36352bf2015-03-25 18:17:31 -0700382 const char* name() const override { return "EllipticalRRect"; }
joshualitteb2a6762014-12-04 11:35:33 -0800383
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000384 const SkRRect& getRRect() const { return fRRect; }
385
joshualittb0a8a372014-09-23 09:50:21 -0700386 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000387
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000388private:
joshualittb0a8a372014-09-23 09:50:21 -0700389 EllipticalRRectEffect(GrPrimitiveEdgeType, const SkRRect&);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000390
wangyixb1daa862015-08-18 11:29:31 -0700391 GrGLFragmentProcessor* onCreateGLInstance() const override;
392
wangyix4b3050b2015-08-04 07:59:37 -0700393 void onGetGLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
394
mtklein36352bf2015-03-25 18:17:31 -0700395 bool onIsEqual(const GrFragmentProcessor& other) const override;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000396
mtklein36352bf2015-03-25 18:17:31 -0700397 void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700398
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000399 SkRRect fRRect;
joshualittb0a8a372014-09-23 09:50:21 -0700400 GrPrimitiveEdgeType fEdgeType;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000401
joshualittb0a8a372014-09-23 09:50:21 -0700402 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000403
joshualittb0a8a372014-09-23 09:50:21 -0700404 typedef GrFragmentProcessor INHERITED;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000405};
406
joshualittb0a8a372014-09-23 09:50:21 -0700407GrFragmentProcessor*
408EllipticalRRectEffect::Create(GrPrimitiveEdgeType edgeType, const SkRRect& rrect) {
409 if (kFillAA_GrProcessorEdgeType != edgeType && kInverseFillAA_GrProcessorEdgeType != edgeType) {
halcanary96fcdcc2015-08-27 07:41:13 -0700410 return nullptr;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000411 }
halcanary385fe4d2015-08-26 13:07:48 -0700412 return new EllipticalRRectEffect(edgeType, rrect);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000413}
414
egdaniel605dd0f2014-11-12 08:35:25 -0800415void EllipticalRRectEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
joshualitt56995b52014-12-11 15:44:02 -0800416 inout->mulByUnknownSingleComponent();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000417}
418
joshualittb0a8a372014-09-23 09:50:21 -0700419EllipticalRRectEffect::EllipticalRRectEffect(GrPrimitiveEdgeType edgeType, const SkRRect& rrect)
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000420 : fRRect(rrect)
joshualitteb2a6762014-12-04 11:35:33 -0800421 , fEdgeType(edgeType) {
422 this->initClassID<EllipticalRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000423 this->setWillReadFragmentPosition();
424}
425
bsalomon0e08fc12014-10-15 08:19:04 -0700426bool EllipticalRRectEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700427 const EllipticalRRectEffect& erre = other.cast<EllipticalRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000428 return fEdgeType == erre.fEdgeType && fRRect == erre.fRRect;
429}
430
431//////////////////////////////////////////////////////////////////////////////
432
joshualittb0a8a372014-09-23 09:50:21 -0700433GR_DEFINE_FRAGMENT_PROCESSOR_TEST(EllipticalRRectEffect);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000434
bsalomonc21b09e2015-08-28 18:46:56 -0700435const GrFragmentProcessor* EllipticalRRectEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700436 SkScalar w = d->fRandom->nextRangeScalar(20.f, 1000.f);
437 SkScalar h = d->fRandom->nextRangeScalar(20.f, 1000.f);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000438 SkVector r[4];
joshualitt0067ff52015-07-08 14:26:19 -0700439 r[SkRRect::kUpperLeft_Corner].fX = d->fRandom->nextRangeF(kRadiusMin, 9.f);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000440 // ensure at least one corner really is elliptical
441 do {
joshualitt0067ff52015-07-08 14:26:19 -0700442 r[SkRRect::kUpperLeft_Corner].fY = d->fRandom->nextRangeF(kRadiusMin, 9.f);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000443 } while (r[SkRRect::kUpperLeft_Corner].fY == r[SkRRect::kUpperLeft_Corner].fX);
444
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000445 SkRRect rrect;
joshualitt0067ff52015-07-08 14:26:19 -0700446 if (d->fRandom->nextBool()) {
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000447 // half the time create a four-radii rrect.
joshualitt0067ff52015-07-08 14:26:19 -0700448 r[SkRRect::kLowerRight_Corner].fX = d->fRandom->nextRangeF(kRadiusMin, 9.f);
449 r[SkRRect::kLowerRight_Corner].fY = d->fRandom->nextRangeF(kRadiusMin, 9.f);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000450
451 r[SkRRect::kUpperRight_Corner].fX = r[SkRRect::kLowerRight_Corner].fX;
452 r[SkRRect::kUpperRight_Corner].fY = r[SkRRect::kUpperLeft_Corner].fY;
453
454 r[SkRRect::kLowerLeft_Corner].fX = r[SkRRect::kUpperLeft_Corner].fX;
455 r[SkRRect::kLowerLeft_Corner].fY = r[SkRRect::kLowerRight_Corner].fY;
456
457 rrect.setRectRadii(SkRect::MakeWH(w, h), r);
458 } else {
459 rrect.setRectXY(SkRect::MakeWH(w, h), r[SkRRect::kUpperLeft_Corner].fX,
460 r[SkRRect::kUpperLeft_Corner].fY);
461 }
joshualittb0a8a372014-09-23 09:50:21 -0700462 GrFragmentProcessor* fp;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000463 do {
joshualitt0067ff52015-07-08 14:26:19 -0700464 GrPrimitiveEdgeType et =
465 (GrPrimitiveEdgeType)d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt);
joshualittb0a8a372014-09-23 09:50:21 -0700466 fp = GrRRectEffect::Create(et, rrect);
halcanary96fcdcc2015-08-27 07:41:13 -0700467 } while (nullptr == fp);
joshualittb0a8a372014-09-23 09:50:21 -0700468 return fp;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000469}
470
471//////////////////////////////////////////////////////////////////////////////
472
joshualittb0a8a372014-09-23 09:50:21 -0700473class GLEllipticalRRectEffect : public GrGLFragmentProcessor {
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000474public:
joshualitteb2a6762014-12-04 11:35:33 -0800475 GLEllipticalRRectEffect(const GrProcessor&);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000476
wangyix7c157a92015-07-22 15:08:53 -0700477 virtual void emitCode(EmitArgs&) override;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000478
jvanverthcfc18862015-04-28 08:48:20 -0700479 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000480
wangyixb1daa862015-08-18 11:29:31 -0700481protected:
482 void onSetData(const GrGLProgramDataManager&, const GrProcessor&) override;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000483
484private:
kkinnunen7510b222014-07-30 00:04:16 -0700485 GrGLProgramDataManager::UniformHandle fInnerRectUniform;
486 GrGLProgramDataManager::UniformHandle fInvRadiiSqdUniform;
487 SkRRect fPrevRRect;
joshualittb0a8a372014-09-23 09:50:21 -0700488 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000489};
490
joshualitteb2a6762014-12-04 11:35:33 -0800491GLEllipticalRRectEffect::GLEllipticalRRectEffect(const GrProcessor& effect) {
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000492 fPrevRRect.setEmpty();
493}
494
wangyix7c157a92015-07-22 15:08:53 -0700495void GLEllipticalRRectEffect::emitCode(EmitArgs& args) {
496 const EllipticalRRectEffect& erre = args.fFp.cast<EllipticalRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000497 const char *rectName;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000498 // The inner rect is the rrect bounds inset by the x/y radii
wangyix7c157a92015-07-22 15:08:53 -0700499 fInnerRectUniform = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800500 kVec4f_GrSLType, kDefault_GrSLPrecision,
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000501 "innerRect",
502 &rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700503
wangyix7c157a92015-07-22 15:08:53 -0700504 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700505 const char* fragmentPos = fsBuilder->fragmentPosition();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000506 // At each quarter-ellipse corner we compute a vector that is the offset of the fragment pos
507 // to the ellipse center. The vector is pinned in x and y to be in the quarter-plane relevant
508 // to that corner. This means that points near the interior near the rrect top edge will have
509 // a vector that points straight up for both the TL left and TR corners. Computing an
510 // alpha from this vector at either the TR or TL corner will give the correct result. Similarly,
511 // fragments near the other three edges will get the correct AA. Fragments in the interior of
512 // the rrect will have a (0,0) vector at all four corners. So long as the radii > 0.5 they will
513 // correctly produce an alpha value of 1 at all four corners. We take the min of all the alphas.
bsalomonc41f4d62015-08-03 14:23:03 -0700514 //
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000515 // The code below is a simplified version of the above that performs maxs on the vector
516 // components before computing distances and alpha values so that only one distance computation
517 // need be computed to determine the min alpha.
joshualitt30ba4362014-08-21 20:18:45 -0700518 fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos);
519 fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName);
bsalomonc41f4d62015-08-03 14:23:03 -0700520 // The uniforms with the inv squared radii are highp to prevent underflow.
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000521 switch (erre.getRRect().getType()) {
522 case SkRRect::kSimple_Type: {
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000523 const char *invRadiiXYSqdName;
wangyix7c157a92015-07-22 15:08:53 -0700524 fInvRadiiSqdUniform = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomonc41f4d62015-08-03 14:23:03 -0700525 kVec2f_GrSLType, kHigh_GrSLPrecision,
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000526 "invRadiiXY",
527 &invRadiiXYSqdName);
joshualitt30ba4362014-08-21 20:18:45 -0700528 fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000529 // Z is the x/y offsets divided by squared radii.
joshualitt30ba4362014-08-21 20:18:45 -0700530 fsBuilder->codeAppendf("\t\tvec2 Z = dxy * %s;\n", invRadiiXYSqdName);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000531 break;
532 }
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000533 case SkRRect::kNinePatch_Type: {
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000534 const char *invRadiiLTRBSqdName;
wangyix7c157a92015-07-22 15:08:53 -0700535 fInvRadiiSqdUniform = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomonc41f4d62015-08-03 14:23:03 -0700536 kVec4f_GrSLType, kHigh_GrSLPrecision,
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000537 "invRadiiLTRB",
538 &invRadiiLTRBSqdName);
joshualitt30ba4362014-08-21 20:18:45 -0700539 fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000540 // Z is the x/y offsets divided by squared radii. We only care about the (at most) one
541 // corner where both the x and y offsets are positive, hence the maxes. (The inverse
542 // squared radii will always be positive.)
joshualitt30ba4362014-08-21 20:18:45 -0700543 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 +0000544 invRadiiLTRBSqdName, invRadiiLTRBSqdName);
545 break;
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000546 }
547 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000548 SkFAIL("RRect should always be simple or nine-patch.");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000549 }
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000550 // implicit is the evaluation of (x/a)^2 + (y/b)^2 - 1.
joshualitt30ba4362014-08-21 20:18:45 -0700551 fsBuilder->codeAppend("\t\tfloat implicit = dot(Z, dxy) - 1.0;\n");
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000552 // grad_dot is the squared length of the gradient of the implicit.
joshualitt30ba4362014-08-21 20:18:45 -0700553 fsBuilder->codeAppendf("\t\tfloat grad_dot = 4.0 * dot(Z, Z);\n");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000554 // avoid calling inversesqrt on zero.
joshualitt30ba4362014-08-21 20:18:45 -0700555 fsBuilder->codeAppend("\t\tgrad_dot = max(grad_dot, 1.0e-4);\n");
556 fsBuilder->codeAppendf("\t\tfloat approx_dist = implicit * inversesqrt(grad_dot);\n");
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000557
joshualittb0a8a372014-09-23 09:50:21 -0700558 if (kFillAA_GrProcessorEdgeType == erre.getEdgeType()) {
joshualitt30ba4362014-08-21 20:18:45 -0700559 fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 - approx_dist, 0.0, 1.0);\n");
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000560 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700561 fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 + approx_dist, 0.0, 1.0);\n");
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000562 }
563
wangyix7c157a92015-07-22 15:08:53 -0700564 fsBuilder->codeAppendf("\t\t%s = %s;\n", args.fOutputColor,
565 (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr1("alpha")).c_str());
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000566}
567
jvanverthcfc18862015-04-28 08:48:20 -0700568void GLEllipticalRRectEffect::GenKey(const GrProcessor& effect, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700569 GrProcessorKeyBuilder* b) {
joshualitt49586be2014-09-16 08:21:41 -0700570 const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>();
joshualittb0a8a372014-09-23 09:50:21 -0700571 GR_STATIC_ASSERT(kLast_GrProcessorEdgeType < (1 << 3));
bsalomon63e99f72014-07-21 08:03:14 -0700572 b->add32(erre.getRRect().getType() | erre.getEdgeType() << 3);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000573}
574
wangyixb1daa862015-08-18 11:29:31 -0700575void GLEllipticalRRectEffect::onSetData(const GrGLProgramDataManager& pdman,
joshualittb0a8a372014-09-23 09:50:21 -0700576 const GrProcessor& effect) {
joshualitt49586be2014-09-16 08:21:41 -0700577 const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000578 const SkRRect& rrect = erre.getRRect();
579 if (rrect != fPrevRRect) {
580 SkRect rect = rrect.getBounds();
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000581 const SkVector& r0 = rrect.radii(SkRRect::kUpperLeft_Corner);
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000582 SkASSERT(r0.fX >= kRadiusMin);
583 SkASSERT(r0.fY >= kRadiusMin);
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000584 switch (erre.getRRect().getType()) {
585 case SkRRect::kSimple_Type:
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000586 rect.inset(r0.fX, r0.fY);
kkinnunen7510b222014-07-30 00:04:16 -0700587 pdman.set2f(fInvRadiiSqdUniform, 1.f / (r0.fX * r0.fX),
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000588 1.f / (r0.fY * r0.fY));
589 break;
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000590 case SkRRect::kNinePatch_Type: {
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000591 const SkVector& r1 = rrect.radii(SkRRect::kLowerRight_Corner);
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000592 SkASSERT(r1.fX >= kRadiusMin);
593 SkASSERT(r1.fY >= kRadiusMin);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000594 rect.fLeft += r0.fX;
595 rect.fTop += r0.fY;
596 rect.fRight -= r1.fX;
597 rect.fBottom -= r1.fY;
kkinnunen7510b222014-07-30 00:04:16 -0700598 pdman.set4f(fInvRadiiSqdUniform, 1.f / (r0.fX * r0.fX),
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000599 1.f / (r0.fY * r0.fY),
600 1.f / (r1.fX * r1.fX),
601 1.f / (r1.fY * r1.fY));
602 break;
603 }
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000604 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000605 SkFAIL("RRect should always be simple or nine-patch.");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000606 }
kkinnunen7510b222014-07-30 00:04:16 -0700607 pdman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000608 fPrevRRect = rrect;
609 }
610}
611
joshualitteb2a6762014-12-04 11:35:33 -0800612////////////////////////////////////////////////////////////////////////////////////////////////////
613
wangyix4b3050b2015-08-04 07:59:37 -0700614void EllipticalRRectEffect::onGetGLProcessorKey(const GrGLSLCaps& caps,
joshualitteb2a6762014-12-04 11:35:33 -0800615 GrProcessorKeyBuilder* b) const {
616 GLEllipticalRRectEffect::GenKey(*this, caps, b);
617}
618
wangyixb1daa862015-08-18 11:29:31 -0700619GrGLFragmentProcessor* EllipticalRRectEffect::onCreateGLInstance() const {
halcanary385fe4d2015-08-26 13:07:48 -0700620 return new GLEllipticalRRectEffect(*this);
joshualitteb2a6762014-12-04 11:35:33 -0800621}
622
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000623//////////////////////////////////////////////////////////////////////////////
624
joshualittb0a8a372014-09-23 09:50:21 -0700625GrFragmentProcessor* GrRRectEffect::Create(GrPrimitiveEdgeType edgeType, const SkRRect& rrect) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000626 if (rrect.isRect()) {
627 return GrConvexPolyEffect::Create(edgeType, rrect.getBounds());
628 }
629
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000630 if (rrect.isOval()) {
631 return GrOvalEffect::Create(edgeType, rrect.getBounds());
632 }
633
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000634 if (rrect.isSimple()) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000635 if (rrect.getSimpleRadii().fX < kRadiusMin || rrect.getSimpleRadii().fY < kRadiusMin) {
636 // In this case the corners are extremely close to rectangular and we collapse the
637 // clip to a rectangular clip.
638 return GrConvexPolyEffect::Create(edgeType, rrect.getBounds());
639 }
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000640 if (rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY) {
skia.committer@gmail.com6e4eb212014-03-25 03:02:32 +0000641 return CircularRRectEffect::Create(edgeType, CircularRRectEffect::kAll_CornerFlags,
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000642 rrect);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000643 } else {
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000644 return EllipticalRRectEffect::Create(edgeType, rrect);
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000645 }
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000646 }
647
648 if (rrect.isComplex() || rrect.isNinePatch()) {
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000649 // Check for the "tab" cases - two adjacent circular corners and two square corners.
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000650 SkScalar circularRadius = 0;
651 uint32_t cornerFlags = 0;
652
653 SkVector radii[4];
654 bool squashedRadii = false;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000655 for (int c = 0; c < 4; ++c) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000656 radii[c] = rrect.radii((SkRRect::Corner)c);
657 SkASSERT((0 == radii[c].fX) == (0 == radii[c].fY));
658 if (0 == radii[c].fX) {
659 // The corner is square, so no need to squash or flag as circular.
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000660 continue;
661 }
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000662 if (radii[c].fX < kRadiusMin || radii[c].fY < kRadiusMin) {
663 radii[c].set(0, 0);
664 squashedRadii = true;
665 continue;
666 }
667 if (radii[c].fX != radii[c].fY) {
bsalomon@google.com44a435b2014-03-13 19:20:32 +0000668 cornerFlags = ~0U;
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000669 break;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000670 }
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000671 if (!cornerFlags) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000672 circularRadius = radii[c].fX;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000673 cornerFlags = 1 << c;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000674 } else {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000675 if (radii[c].fX != circularRadius) {
bsalomon@google.com44a435b2014-03-13 19:20:32 +0000676 cornerFlags = ~0U;
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000677 break;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000678 }
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000679 cornerFlags |= 1 << c;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000680 }
681 }
682
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000683 switch (cornerFlags) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000684 case CircularRRectEffect::kAll_CornerFlags:
685 // This rrect should have been caught in the simple case above. Though, it would
686 // be correctly handled in the fallthrough code.
687 SkASSERT(false);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000688 case CircularRRectEffect::kTopLeft_CornerFlag:
689 case CircularRRectEffect::kTopRight_CornerFlag:
690 case CircularRRectEffect::kBottomRight_CornerFlag:
691 case CircularRRectEffect::kBottomLeft_CornerFlag:
692 case CircularRRectEffect::kLeft_CornerFlags:
693 case CircularRRectEffect::kTop_CornerFlags:
694 case CircularRRectEffect::kRight_CornerFlags:
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000695 case CircularRRectEffect::kBottom_CornerFlags: {
696 SkTCopyOnFirstWrite<SkRRect> rr(rrect);
697 if (squashedRadii) {
698 rr.writable()->setRectRadii(rrect.getBounds(), radii);
699 }
700 return CircularRRectEffect::Create(edgeType, cornerFlags, *rr);
701 }
702 case CircularRRectEffect::kNone_CornerFlags:
703 return GrConvexPolyEffect::Create(edgeType, rrect.getBounds());
704 default: {
705 if (squashedRadii) {
706 // If we got here then we squashed some but not all the radii to zero. (If all
707 // had been squashed cornerFlags would be 0.) The elliptical effect doesn't
708 // support some rounded and some square corners.
halcanary96fcdcc2015-08-27 07:41:13 -0700709 return nullptr;
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000710 }
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000711 if (rrect.isNinePatch()) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000712 return EllipticalRRectEffect::Create(edgeType, rrect);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000713 }
halcanary96fcdcc2015-08-27 07:41:13 -0700714 return nullptr;
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000715 }
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000716 }
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000717 }
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000718
halcanary96fcdcc2015-08-27 07:41:13 -0700719 return nullptr;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000720}