blob: 0666089abb8dc5e762e2826f8015b5b40804a02c [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
Ethan Nicholas222e2752018-10-11 11:21:34 -040010#include "GrContext.h"
11#include "GrContextPriv.h"
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +000012#include "GrConvexPolyEffect.h"
joshualitteb2a6762014-12-04 11:35:33 -080013#include "GrFragmentProcessor.h"
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000014#include "GrOvalEffect.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050015#include "GrShaderCaps.h"
Mike Reed242135a2018-02-22 13:41:39 -050016#include "SkRRectPriv.h"
egdaniel7ea439b2015-12-03 09:20:44 -080017#include "SkTLazy.h"
egdaniel64c47282015-11-13 06:54:19 -080018#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080019#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070020#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080021#include "glsl/GrGLSLUniformHandler.h"
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +000022
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +000023// The effects defined here only handle rrect radii >= kRadiusMin.
24static const SkScalar kRadiusMin = SK_ScalarHalf;
25
26//////////////////////////////////////////////////////////////////////////////
27
joshualittb0a8a372014-09-23 09:50:21 -070028class CircularRRectEffect : public GrFragmentProcessor {
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000029public:
skia.committer@gmail.comf1f66c02014-03-05 03:02:06 +000030
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000031 enum CornerFlags {
32 kTopLeft_CornerFlag = (1 << SkRRect::kUpperLeft_Corner),
33 kTopRight_CornerFlag = (1 << SkRRect::kUpperRight_Corner),
34 kBottomRight_CornerFlag = (1 << SkRRect::kLowerRight_Corner),
35 kBottomLeft_CornerFlag = (1 << SkRRect::kLowerLeft_Corner),
36
37 kLeft_CornerFlags = kTopLeft_CornerFlag | kBottomLeft_CornerFlag,
38 kTop_CornerFlags = kTopLeft_CornerFlag | kTopRight_CornerFlag,
39 kRight_CornerFlags = kTopRight_CornerFlag | kBottomRight_CornerFlag,
40 kBottom_CornerFlags = kBottomLeft_CornerFlag | kBottomRight_CornerFlag,
41
42 kAll_CornerFlags = kTopLeft_CornerFlag | kTopRight_CornerFlag |
43 kBottomLeft_CornerFlag | kBottomRight_CornerFlag,
44
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +000045 kNone_CornerFlags = 0
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +000046 };
47
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000048 // The flags are used to indicate which corners are circluar (unflagged corners are assumed to
49 // be square).
Ethan Nicholas0f3c7322017-11-09 14:51:17 -050050 static std::unique_ptr<GrFragmentProcessor> Make(GrClipEdgeType,
Brian Salomonaff329b2017-08-11 09:40:37 -040051 uint32_t circularCornerFlags, const SkRRect&);
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000052
Brian Salomond3b65972017-03-22 12:05:03 -040053 ~CircularRRectEffect() override {}
joshualitteb2a6762014-12-04 11:35:33 -080054
mtklein36352bf2015-03-25 18:17:31 -070055 const char* name() const override { return "CircularRRect"; }
joshualitteb2a6762014-12-04 11:35:33 -080056
Brian Salomonaff329b2017-08-11 09:40:37 -040057 std::unique_ptr<GrFragmentProcessor> clone() const override;
Brian Salomon1a2a7ab2017-07-26 13:11:51 -040058
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000059 const SkRRect& getRRect() const { return fRRect; }
60
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +000061 uint32_t getCircularCornerFlags() const { return fCircularCornerFlags; }
skia.committer@gmail.com06acb582014-03-06 03:02:32 +000062
Ethan Nicholas0f3c7322017-11-09 14:51:17 -050063 GrClipEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +000064
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000065private:
Ethan Nicholas0f3c7322017-11-09 14:51:17 -050066 CircularRRectEffect(GrClipEdgeType, uint32_t circularCornerFlags, const SkRRect&);
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000067
egdaniel57d3b032015-11-13 11:57:27 -080068 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -070069
Brian Salomon94efbf52016-11-29 13:43:05 -050070 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
wangyix4b3050b2015-08-04 07:59:37 -070071
mtklein36352bf2015-03-25 18:17:31 -070072 bool onIsEqual(const GrFragmentProcessor& other) const override;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000073
joshualittb0a8a372014-09-23 09:50:21 -070074 SkRRect fRRect;
Ethan Nicholas0f3c7322017-11-09 14:51:17 -050075 GrClipEdgeType fEdgeType;
joshualittb0a8a372014-09-23 09:50:21 -070076 uint32_t fCircularCornerFlags;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000077
Brian Salomon0c26a9d2017-07-06 10:09:38 -040078 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000079
joshualittb0a8a372014-09-23 09:50:21 -070080 typedef GrFragmentProcessor INHERITED;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000081};
82
Ethan Nicholas0f3c7322017-11-09 14:51:17 -050083std::unique_ptr<GrFragmentProcessor> CircularRRectEffect::Make(GrClipEdgeType edgeType,
Brian Salomonaff329b2017-08-11 09:40:37 -040084 uint32_t circularCornerFlags,
85 const SkRRect& rrect) {
Ethan Nicholas1706f842017-11-10 11:58:19 -050086 if (GrClipEdgeType::kFillAA != edgeType && GrClipEdgeType::kInverseFillAA != edgeType) {
halcanary96fcdcc2015-08-27 07:41:13 -070087 return nullptr;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +000088 }
Brian Salomonaff329b2017-08-11 09:40:37 -040089 return std::unique_ptr<GrFragmentProcessor>(
90 new CircularRRectEffect(edgeType, circularCornerFlags, rrect));
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000091}
92
Ethan Nicholas0f3c7322017-11-09 14:51:17 -050093CircularRRectEffect::CircularRRectEffect(GrClipEdgeType edgeType, uint32_t circularCornerFlags,
joshualittb0a8a372014-09-23 09:50:21 -070094 const SkRRect& rrect)
Ethan Nicholasabff9562017-10-09 10:54:08 -040095 : INHERITED(kCircularRRectEffect_ClassID, kCompatibleWithCoverageAsAlpha_OptimizationFlag)
Brian Salomon587e08f2017-01-27 10:59:27 -050096 , fRRect(rrect)
97 , fEdgeType(edgeType)
98 , fCircularCornerFlags(circularCornerFlags) {
commit-bot@chromium.orge5280892014-02-21 17:52:29 +000099}
100
Brian Salomonaff329b2017-08-11 09:40:37 -0400101std::unique_ptr<GrFragmentProcessor> CircularRRectEffect::clone() const {
102 return std::unique_ptr<GrFragmentProcessor>(
Brian Salomon1a2a7ab2017-07-26 13:11:51 -0400103 new CircularRRectEffect(fEdgeType, fCircularCornerFlags, fRRect));
104}
105
bsalomon0e08fc12014-10-15 08:19:04 -0700106bool CircularRRectEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700107 const CircularRRectEffect& crre = other.cast<CircularRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000108 // The corner flags are derived from fRRect, so no need to check them.
109 return fEdgeType == crre.fEdgeType && fRRect == crre.fRRect;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000110}
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000111
112//////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000113
joshualittb0a8a372014-09-23 09:50:21 -0700114GR_DEFINE_FRAGMENT_PROCESSOR_TEST(CircularRRectEffect);
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000115
Hal Canary6f6961e2017-01-31 13:50:44 -0500116#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400117std::unique_ptr<GrFragmentProcessor> CircularRRectEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700118 SkScalar w = d->fRandom->nextRangeScalar(20.f, 1000.f);
119 SkScalar h = d->fRandom->nextRangeScalar(20.f, 1000.f);
120 SkScalar r = d->fRandom->nextRangeF(kRadiusMin, 9.f);
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000121 SkRRect rrect;
122 rrect.setRectXY(SkRect::MakeWH(w, h), r, r);
Brian Salomonaff329b2017-08-11 09:40:37 -0400123 std::unique_ptr<GrFragmentProcessor> fp;
commit-bot@chromium.orgcabf4b22014-03-05 18:27:43 +0000124 do {
Ethan Nicholas0f3c7322017-11-09 14:51:17 -0500125 GrClipEdgeType et =
Ethan Nicholas1706f842017-11-10 11:58:19 -0500126 (GrClipEdgeType)d->fRandom->nextULessThan(kGrClipEdgeTypeCnt);
Ethan Nicholas222e2752018-10-11 11:21:34 -0400127 fp = GrRRectEffect::Make(et, rrect, d->context());
halcanary96fcdcc2015-08-27 07:41:13 -0700128 } while (nullptr == fp);
joshualittb0a8a372014-09-23 09:50:21 -0700129 return fp;
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000130}
Hal Canary6f6961e2017-01-31 13:50:44 -0500131#endif
commit-bot@chromium.orge5280892014-02-21 17:52:29 +0000132
133//////////////////////////////////////////////////////////////////////////////
134
egdaniel64c47282015-11-13 06:54:19 -0800135class GLCircularRRectEffect : public GrGLSLFragmentProcessor {
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000136public:
Brian Salomon0a241ce2017-12-15 11:31:05 -0500137 GLCircularRRectEffect() = default;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000138
wangyix7c157a92015-07-22 15:08:53 -0700139 virtual void emitCode(EmitArgs&) override;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000140
Brian Salomon94efbf52016-11-29 13:43:05 -0500141 static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000142
wangyixb1daa862015-08-18 11:29:31 -0700143protected:
Brian Salomonab015ef2017-04-04 10:15:51 -0400144 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000145
146private:
egdaniel018fb622015-10-28 07:26:40 -0700147 GrGLSLProgramDataManager::UniformHandle fInnerRectUniform;
148 GrGLSLProgramDataManager::UniformHandle fRadiusPlusHalfUniform;
robertphillipsbf536af2016-02-04 06:11:53 -0800149 SkRRect fPrevRRect;
egdaniel64c47282015-11-13 06:54:19 -0800150 typedef GrGLSLFragmentProcessor INHERITED;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000151};
152
wangyix7c157a92015-07-22 15:08:53 -0700153void GLCircularRRectEffect::emitCode(EmitArgs& args) {
154 const CircularRRectEffect& crre = args.fFp.cast<CircularRRectEffect>();
egdaniel7ea439b2015-12-03 09:20:44 -0800155 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000156 const char *rectName;
157 const char *radiusPlusHalfName;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000158 // The inner rect is the rrect bounds inset by the radius. Its left, top, right, and bottom
159 // edges correspond to components x, y, z, and w, respectively. When a side of the rrect has
160 // only rectangular corners, that side's value corresponds to the rect edge's value outset by
161 // half a pixel.
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400162 fInnerRectUniform = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType,
163 "innerRect", &rectName);
bsalomoncd074912015-12-09 10:33:51 -0800164 // x is (r + .5) and y is 1/(r + .5)
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400165 fRadiusPlusHalfUniform = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType,
166 "radiusPlusHalf", &radiusPlusHalfName);
joshualitt30ba4362014-08-21 20:18:45 -0700167
Chris Dalton47c8ed32017-11-15 18:27:09 -0700168 // If we're on a device where float != fp32 then the length calculation could overflow.
bsalomoncd074912015-12-09 10:33:51 -0800169 SkString clampedCircleDistance;
Chris Dalton47c8ed32017-11-15 18:27:09 -0700170 if (!args.fShaderCaps->floatIs32Bits()) {
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400171 clampedCircleDistance.printf("saturate(%s.x * (1.0 - length(dxy * %s.y)));",
bsalomoncd074912015-12-09 10:33:51 -0800172 radiusPlusHalfName, radiusPlusHalfName);
173 } else {
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400174 clampedCircleDistance.printf("saturate(%s.x - length(dxy));", radiusPlusHalfName);
bsalomoncd074912015-12-09 10:33:51 -0800175 }
176
cdalton85285412016-02-18 12:37:07 -0800177 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000178 // At each quarter-circle corner we compute a vector that is the offset of the fragment position
179 // from the circle center. The vector is pinned in x and y to be in the quarter-plane relevant
180 // to that corner. This means that points near the interior near the rrect top edge will have
181 // a vector that points straight up for both the TL left and TR corners. Computing an
182 // alpha from this vector at either the TR or TL corner will give the correct result. Similarly,
183 // fragments near the other three edges will get the correct AA. Fragments in the interior of
184 // the rrect will have a (0,0) vector at all four corners. So long as the radius > 0.5 they will
185 // correctly produce an alpha value of 1 at all four corners. We take the min of all the alphas.
186 // The code below is a simplified version of the above that performs maxs on the vector
187 // components before computing distances and alpha values so that only one distance computation
188 // need be computed to determine the min alpha.
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000189 //
190 // For the cases where one half of the rrect is rectangular we drop one of the x or y
191 // computations, compute a separate rect edge alpha for the rect side, and mul the two computed
192 // alphas together.
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000193 switch (crre.getCircularCornerFlags()) {
194 case CircularRRectEffect::kAll_CornerFlags:
Chris Dalton47c8ed32017-11-15 18:27:09 -0700195 fragBuilder->codeAppendf("float2 dxy0 = %s.xy - sk_FragCoord.xy;", rectName);
196 fragBuilder->codeAppendf("float2 dxy1 = sk_FragCoord.xy - %s.zw;", rectName);
197 fragBuilder->codeAppend("float2 dxy = max(max(dxy0, dxy1), 0.0);");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400198 fragBuilder->codeAppendf("half alpha = %s;", clampedCircleDistance.c_str());
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000199 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000200 case CircularRRectEffect::kTopLeft_CornerFlag:
Chris Dalton47c8ed32017-11-15 18:27:09 -0700201 fragBuilder->codeAppendf("float2 dxy = max(%s.xy - sk_FragCoord.xy, 0.0);",
Ethan Nicholas38657112017-02-09 17:01:22 -0500202 rectName);
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400203 fragBuilder->codeAppendf("half rightAlpha = saturate(%s.z - sk_FragCoord.x);",
Ethan Nicholas38657112017-02-09 17:01:22 -0500204 rectName);
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400205 fragBuilder->codeAppendf("half bottomAlpha = saturate(%s.w - sk_FragCoord.y);",
Ethan Nicholas38657112017-02-09 17:01:22 -0500206 rectName);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400207 fragBuilder->codeAppendf("half alpha = bottomAlpha * rightAlpha * %s;",
bsalomoncd074912015-12-09 10:33:51 -0800208 clampedCircleDistance.c_str());
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000209 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000210 case CircularRRectEffect::kTopRight_CornerFlag:
Chris Dalton47c8ed32017-11-15 18:27:09 -0700211 fragBuilder->codeAppendf("float2 dxy = max(float2(sk_FragCoord.x - %s.z, "
212 "%s.y - sk_FragCoord.y), 0.0);",
Ethan Nicholas38657112017-02-09 17:01:22 -0500213 rectName, rectName);
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400214 fragBuilder->codeAppendf("half leftAlpha = saturate(sk_FragCoord.x - %s.x);",
Ethan Nicholas38657112017-02-09 17:01:22 -0500215 rectName);
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400216 fragBuilder->codeAppendf("half bottomAlpha = saturate(%s.w - sk_FragCoord.y);",
Ethan Nicholas38657112017-02-09 17:01:22 -0500217 rectName);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400218 fragBuilder->codeAppendf("half alpha = bottomAlpha * leftAlpha * %s;",
bsalomoncd074912015-12-09 10:33:51 -0800219 clampedCircleDistance.c_str());
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000220 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000221 case CircularRRectEffect::kBottomRight_CornerFlag:
Chris Dalton47c8ed32017-11-15 18:27:09 -0700222 fragBuilder->codeAppendf("float2 dxy = max(sk_FragCoord.xy - %s.zw, 0.0);",
Ethan Nicholas38657112017-02-09 17:01:22 -0500223 rectName);
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400224 fragBuilder->codeAppendf("half leftAlpha = saturate(sk_FragCoord.x - %s.x);",
Ethan Nicholas38657112017-02-09 17:01:22 -0500225 rectName);
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400226 fragBuilder->codeAppendf("half topAlpha = saturate(sk_FragCoord.y - %s.y);",
Ethan Nicholas38657112017-02-09 17:01:22 -0500227 rectName);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400228 fragBuilder->codeAppendf("half alpha = topAlpha * leftAlpha * %s;",
bsalomoncd074912015-12-09 10:33:51 -0800229 clampedCircleDistance.c_str());
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000230 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000231 case CircularRRectEffect::kBottomLeft_CornerFlag:
Chris Dalton47c8ed32017-11-15 18:27:09 -0700232 fragBuilder->codeAppendf("float2 dxy = max(float2(%s.x - sk_FragCoord.x, "
233 "sk_FragCoord.y - %s.w), 0.0);",
Ethan Nicholas38657112017-02-09 17:01:22 -0500234 rectName, rectName);
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400235 fragBuilder->codeAppendf("half rightAlpha = saturate(%s.z - sk_FragCoord.x);",
Ethan Nicholas38657112017-02-09 17:01:22 -0500236 rectName);
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400237 fragBuilder->codeAppendf("half topAlpha = saturate(sk_FragCoord.y - %s.y);",
Ethan Nicholas38657112017-02-09 17:01:22 -0500238 rectName);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400239 fragBuilder->codeAppendf("half alpha = topAlpha * rightAlpha * %s;",
bsalomoncd074912015-12-09 10:33:51 -0800240 clampedCircleDistance.c_str());
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000241 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000242 case CircularRRectEffect::kLeft_CornerFlags:
Chris Dalton47c8ed32017-11-15 18:27:09 -0700243 fragBuilder->codeAppendf("float2 dxy0 = %s.xy - sk_FragCoord.xy;", rectName);
244 fragBuilder->codeAppendf("float dy1 = sk_FragCoord.y - %s.w;", rectName);
245 fragBuilder->codeAppend("float2 dxy = max(float2(dxy0.x, max(dxy0.y, dy1)), 0.0);");
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400246 fragBuilder->codeAppendf("half rightAlpha = saturate(%s.z - sk_FragCoord.x);",
Ethan Nicholas38657112017-02-09 17:01:22 -0500247 rectName);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400248 fragBuilder->codeAppendf("half alpha = rightAlpha * %s;",
bsalomoncd074912015-12-09 10:33:51 -0800249 clampedCircleDistance.c_str());
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000250 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000251 case CircularRRectEffect::kTop_CornerFlags:
Chris Dalton47c8ed32017-11-15 18:27:09 -0700252 fragBuilder->codeAppendf("float2 dxy0 = %s.xy - sk_FragCoord.xy;", rectName);
253 fragBuilder->codeAppendf("float dx1 = sk_FragCoord.x - %s.z;", rectName);
254 fragBuilder->codeAppend("float2 dxy = max(float2(max(dxy0.x, dx1), dxy0.y), 0.0);");
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400255 fragBuilder->codeAppendf("half bottomAlpha = saturate(%s.w - sk_FragCoord.y);",
Ethan Nicholas38657112017-02-09 17:01:22 -0500256 rectName);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400257 fragBuilder->codeAppendf("half alpha = bottomAlpha * %s;",
bsalomoncd074912015-12-09 10:33:51 -0800258 clampedCircleDistance.c_str());
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000259 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000260 case CircularRRectEffect::kRight_CornerFlags:
Chris Dalton47c8ed32017-11-15 18:27:09 -0700261 fragBuilder->codeAppendf("float dy0 = %s.y - sk_FragCoord.y;", rectName);
262 fragBuilder->codeAppendf("float2 dxy1 = sk_FragCoord.xy - %s.zw;", rectName);
263 fragBuilder->codeAppend("float2 dxy = max(float2(dxy1.x, max(dy0, dxy1.y)), 0.0);");
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400264 fragBuilder->codeAppendf("half leftAlpha = saturate(sk_FragCoord.x - %s.x);",
Ethan Nicholas38657112017-02-09 17:01:22 -0500265 rectName);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400266 fragBuilder->codeAppendf("half alpha = leftAlpha * %s;",
bsalomoncd074912015-12-09 10:33:51 -0800267 clampedCircleDistance.c_str());
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000268 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000269 case CircularRRectEffect::kBottom_CornerFlags:
Chris Dalton47c8ed32017-11-15 18:27:09 -0700270 fragBuilder->codeAppendf("float dx0 = %s.x - sk_FragCoord.x;", rectName);
271 fragBuilder->codeAppendf("float2 dxy1 = sk_FragCoord.xy - %s.zw;", rectName);
272 fragBuilder->codeAppend("float2 dxy = max(float2(max(dx0, dxy1.x), dxy1.y), 0.0);");
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400273 fragBuilder->codeAppendf("half topAlpha = saturate(sk_FragCoord.y - %s.y);",
Ethan Nicholas38657112017-02-09 17:01:22 -0500274 rectName);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400275 fragBuilder->codeAppendf("half alpha = topAlpha * %s;",
bsalomoncd074912015-12-09 10:33:51 -0800276 clampedCircleDistance.c_str());
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000277 break;
278 }
skia.committer@gmail.com06acb582014-03-06 03:02:32 +0000279
Ethan Nicholas1706f842017-11-10 11:58:19 -0500280 if (GrClipEdgeType::kInverseFillAA == crre.getEdgeType()) {
bsalomoncd074912015-12-09 10:33:51 -0800281 fragBuilder->codeAppend("alpha = 1.0 - alpha;");
commit-bot@chromium.orgfbde87f2014-03-04 16:25:34 +0000282 }
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000283
Ethan Nicholas2983f402017-05-08 09:36:08 -0400284 fragBuilder->codeAppendf("%s = %s * alpha;", args.fOutputColor, args.fInputColor);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000285}
286
Brian Salomon94efbf52016-11-29 13:43:05 -0500287void GLCircularRRectEffect::GenKey(const GrProcessor& processor, const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700288 GrProcessorKeyBuilder* b) {
289 const CircularRRectEffect& crre = processor.cast<CircularRRectEffect>();
Ethan Nicholas1706f842017-11-10 11:58:19 -0500290 GR_STATIC_ASSERT(kGrClipEdgeTypeCnt <= 8);
291 b->add32((crre.getCircularCornerFlags() << 3) | (int) crre.getEdgeType());
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000292}
293
egdaniel018fb622015-10-28 07:26:40 -0700294void GLCircularRRectEffect::onSetData(const GrGLSLProgramDataManager& pdman,
Brian Salomonab015ef2017-04-04 10:15:51 -0400295 const GrFragmentProcessor& processor) {
joshualittb0a8a372014-09-23 09:50:21 -0700296 const CircularRRectEffect& crre = processor.cast<CircularRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000297 const SkRRect& rrect = crre.getRRect();
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000298 if (rrect != fPrevRRect) {
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000299 SkRect rect = rrect.getBounds();
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000300 SkScalar radius = 0;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000301 switch (crre.getCircularCornerFlags()) {
302 case CircularRRectEffect::kAll_CornerFlags:
Mike Reed242135a2018-02-22 13:41:39 -0500303 SkASSERT(SkRRectPriv::IsSimpleCircular(rrect));
304 radius = SkRRectPriv::GetSimpleRadii(rrect).fX;
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000305 SkASSERT(radius >= kRadiusMin);
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000306 rect.inset(radius, radius);
307 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000308 case CircularRRectEffect::kTopLeft_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000309 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
310 rect.fLeft += radius;
311 rect.fTop += radius;
312 rect.fRight += 0.5f;
313 rect.fBottom += 0.5f;
314 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000315 case CircularRRectEffect::kTopRight_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000316 radius = rrect.radii(SkRRect::kUpperRight_Corner).fX;
bsalomon@google.comde9f2512014-03-11 17:09:17 +0000317 rect.fLeft -= 0.5f;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000318 rect.fTop += radius;
319 rect.fRight -= radius;
320 rect.fBottom += 0.5f;
321 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000322 case CircularRRectEffect::kBottomRight_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000323 radius = rrect.radii(SkRRect::kLowerRight_Corner).fX;
bsalomon@google.comde9f2512014-03-11 17:09:17 +0000324 rect.fLeft -= 0.5f;
325 rect.fTop -= 0.5f;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000326 rect.fRight -= radius;
327 rect.fBottom -= radius;
328 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000329 case CircularRRectEffect::kBottomLeft_CornerFlag:
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000330 radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX;
331 rect.fLeft += radius;
bsalomon@google.comde9f2512014-03-11 17:09:17 +0000332 rect.fTop -= 0.5f;
333 rect.fRight += 0.5f;
commit-bot@chromium.orgc5c748c2014-03-11 15:54:51 +0000334 rect.fBottom -= radius;
335 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000336 case CircularRRectEffect::kLeft_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000337 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
338 rect.fLeft += radius;
339 rect.fTop += radius;
340 rect.fRight += 0.5f;
341 rect.fBottom -= radius;
342 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000343 case CircularRRectEffect::kTop_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000344 radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX;
345 rect.fLeft += radius;
346 rect.fTop += radius;
347 rect.fRight -= radius;
348 rect.fBottom += 0.5f;
349 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000350 case CircularRRectEffect::kRight_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000351 radius = rrect.radii(SkRRect::kUpperRight_Corner).fX;
352 rect.fLeft -= 0.5f;
353 rect.fTop += radius;
354 rect.fRight -= radius;
355 rect.fBottom -= radius;
356 break;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000357 case CircularRRectEffect::kBottom_CornerFlags:
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000358 radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX;
359 rect.fLeft += radius;
360 rect.fTop -= 0.5f;
361 rect.fRight -= radius;
362 rect.fBottom -= radius;
363 break;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000364 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400365 SK_ABORT("Should have been one of the above cases.");
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000366 }
kkinnunen7510b222014-07-30 00:04:16 -0700367 pdman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
bsalomoncd074912015-12-09 10:33:51 -0800368 radius += 0.5f;
369 pdman.set2f(fRadiusPlusHalfUniform, radius, 1.f / radius);
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000370 fPrevRRect = rrect;
371 }
372}
373
joshualitteb2a6762014-12-04 11:35:33 -0800374////////////////////////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000375
Brian Salomon94efbf52016-11-29 13:43:05 -0500376void CircularRRectEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800377 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800378 GLCircularRRectEffect::GenKey(*this, caps, b);
379}
380
egdaniel57d3b032015-11-13 11:57:27 -0800381GrGLSLFragmentProcessor* CircularRRectEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -0800382 return new GLCircularRRectEffect;
joshualitteb2a6762014-12-04 11:35:33 -0800383}
384
385//////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000386
joshualittb0a8a372014-09-23 09:50:21 -0700387class EllipticalRRectEffect : public GrFragmentProcessor {
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000388public:
Ethan Nicholas0f3c7322017-11-09 14:51:17 -0500389 static std::unique_ptr<GrFragmentProcessor> Make(GrClipEdgeType, const SkRRect&);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000390
Brian Salomond3b65972017-03-22 12:05:03 -0400391 ~EllipticalRRectEffect() override {}
joshualitteb2a6762014-12-04 11:35:33 -0800392
mtklein36352bf2015-03-25 18:17:31 -0700393 const char* name() const override { return "EllipticalRRect"; }
joshualitteb2a6762014-12-04 11:35:33 -0800394
Brian Salomonaff329b2017-08-11 09:40:37 -0400395 std::unique_ptr<GrFragmentProcessor> clone() const override;
Brian Salomon1a2a7ab2017-07-26 13:11:51 -0400396
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000397 const SkRRect& getRRect() const { return fRRect; }
398
Ethan Nicholas0f3c7322017-11-09 14:51:17 -0500399 GrClipEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000400
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000401private:
Ethan Nicholas0f3c7322017-11-09 14:51:17 -0500402 EllipticalRRectEffect(GrClipEdgeType, const SkRRect&);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000403
egdaniel57d3b032015-11-13 11:57:27 -0800404 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -0700405
Brian Salomon94efbf52016-11-29 13:43:05 -0500406 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
wangyix4b3050b2015-08-04 07:59:37 -0700407
mtklein36352bf2015-03-25 18:17:31 -0700408 bool onIsEqual(const GrFragmentProcessor& other) const override;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000409
Brian Salomon587e08f2017-01-27 10:59:27 -0500410 SkRRect fRRect;
Ethan Nicholas0f3c7322017-11-09 14:51:17 -0500411 GrClipEdgeType fEdgeType;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000412
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400413 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000414
joshualittb0a8a372014-09-23 09:50:21 -0700415 typedef GrFragmentProcessor INHERITED;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000416};
417
Ethan Nicholas0f3c7322017-11-09 14:51:17 -0500418std::unique_ptr<GrFragmentProcessor> EllipticalRRectEffect::Make(GrClipEdgeType edgeType,
Brian Salomonaff329b2017-08-11 09:40:37 -0400419 const SkRRect& rrect) {
Ethan Nicholas1706f842017-11-10 11:58:19 -0500420 if (GrClipEdgeType::kFillAA != edgeType && GrClipEdgeType::kInverseFillAA != edgeType) {
halcanary96fcdcc2015-08-27 07:41:13 -0700421 return nullptr;
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000422 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400423 return std::unique_ptr<GrFragmentProcessor>(new EllipticalRRectEffect(edgeType, rrect));
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000424}
425
Ethan Nicholas0f3c7322017-11-09 14:51:17 -0500426EllipticalRRectEffect::EllipticalRRectEffect(GrClipEdgeType edgeType, const SkRRect& rrect)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400427 : INHERITED(kEllipticalRRectEffect_ClassID, kCompatibleWithCoverageAsAlpha_OptimizationFlag)
Brian Salomonf3b995b2017-02-15 10:22:23 -0500428 , fRRect(rrect)
429 , fEdgeType(edgeType) {
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000430}
431
Brian Salomonaff329b2017-08-11 09:40:37 -0400432std::unique_ptr<GrFragmentProcessor> EllipticalRRectEffect::clone() const {
433 return std::unique_ptr<GrFragmentProcessor>(new EllipticalRRectEffect(fEdgeType, fRRect));
Brian Salomon1a2a7ab2017-07-26 13:11:51 -0400434}
435
bsalomon0e08fc12014-10-15 08:19:04 -0700436bool EllipticalRRectEffect::onIsEqual(const GrFragmentProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700437 const EllipticalRRectEffect& erre = other.cast<EllipticalRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000438 return fEdgeType == erre.fEdgeType && fRRect == erre.fRRect;
439}
440
441//////////////////////////////////////////////////////////////////////////////
442
joshualittb0a8a372014-09-23 09:50:21 -0700443GR_DEFINE_FRAGMENT_PROCESSOR_TEST(EllipticalRRectEffect);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000444
Hal Canary6f6961e2017-01-31 13:50:44 -0500445#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -0400446std::unique_ptr<GrFragmentProcessor> EllipticalRRectEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700447 SkScalar w = d->fRandom->nextRangeScalar(20.f, 1000.f);
448 SkScalar h = d->fRandom->nextRangeScalar(20.f, 1000.f);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000449 SkVector r[4];
joshualitt0067ff52015-07-08 14:26:19 -0700450 r[SkRRect::kUpperLeft_Corner].fX = d->fRandom->nextRangeF(kRadiusMin, 9.f);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000451 // ensure at least one corner really is elliptical
452 do {
joshualitt0067ff52015-07-08 14:26:19 -0700453 r[SkRRect::kUpperLeft_Corner].fY = d->fRandom->nextRangeF(kRadiusMin, 9.f);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000454 } while (r[SkRRect::kUpperLeft_Corner].fY == r[SkRRect::kUpperLeft_Corner].fX);
455
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000456 SkRRect rrect;
joshualitt0067ff52015-07-08 14:26:19 -0700457 if (d->fRandom->nextBool()) {
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000458 // half the time create a four-radii rrect.
joshualitt0067ff52015-07-08 14:26:19 -0700459 r[SkRRect::kLowerRight_Corner].fX = d->fRandom->nextRangeF(kRadiusMin, 9.f);
460 r[SkRRect::kLowerRight_Corner].fY = d->fRandom->nextRangeF(kRadiusMin, 9.f);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000461
462 r[SkRRect::kUpperRight_Corner].fX = r[SkRRect::kLowerRight_Corner].fX;
463 r[SkRRect::kUpperRight_Corner].fY = r[SkRRect::kUpperLeft_Corner].fY;
464
465 r[SkRRect::kLowerLeft_Corner].fX = r[SkRRect::kUpperLeft_Corner].fX;
466 r[SkRRect::kLowerLeft_Corner].fY = r[SkRRect::kLowerRight_Corner].fY;
467
468 rrect.setRectRadii(SkRect::MakeWH(w, h), r);
469 } else {
470 rrect.setRectXY(SkRect::MakeWH(w, h), r[SkRRect::kUpperLeft_Corner].fX,
471 r[SkRRect::kUpperLeft_Corner].fY);
472 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400473 std::unique_ptr<GrFragmentProcessor> fp;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000474 do {
Ethan Nicholas1706f842017-11-10 11:58:19 -0500475 GrClipEdgeType et = (GrClipEdgeType)d->fRandom->nextULessThan(kGrClipEdgeTypeCnt);
Ethan Nicholas222e2752018-10-11 11:21:34 -0400476 fp = GrRRectEffect::Make(et, rrect, d->context());
halcanary96fcdcc2015-08-27 07:41:13 -0700477 } while (nullptr == fp);
joshualittb0a8a372014-09-23 09:50:21 -0700478 return fp;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000479}
Hal Canary6f6961e2017-01-31 13:50:44 -0500480#endif
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000481
482//////////////////////////////////////////////////////////////////////////////
483
egdaniel64c47282015-11-13 06:54:19 -0800484class GLEllipticalRRectEffect : public GrGLSLFragmentProcessor {
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000485public:
Brian Salomon0a241ce2017-12-15 11:31:05 -0500486 GLEllipticalRRectEffect() = default;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000487
robertphillips9cdb9922016-02-03 12:25:40 -0800488 void emitCode(EmitArgs&) override;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000489
Brian Salomon94efbf52016-11-29 13:43:05 -0500490 static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000491
wangyixb1daa862015-08-18 11:29:31 -0700492protected:
Brian Salomonab015ef2017-04-04 10:15:51 -0400493 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000494
495private:
egdaniel018fb622015-10-28 07:26:40 -0700496 GrGLSLProgramDataManager::UniformHandle fInnerRectUniform;
497 GrGLSLProgramDataManager::UniformHandle fInvRadiiSqdUniform;
bsalomone87256c42015-12-09 17:14:40 -0800498 GrGLSLProgramDataManager::UniformHandle fScaleUniform;
499 SkRRect fPrevRRect;
egdaniel64c47282015-11-13 06:54:19 -0800500 typedef GrGLSLFragmentProcessor INHERITED;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000501};
502
wangyix7c157a92015-07-22 15:08:53 -0700503void GLEllipticalRRectEffect::emitCode(EmitArgs& args) {
504 const EllipticalRRectEffect& erre = args.fFp.cast<EllipticalRRectEffect>();
egdaniel7ea439b2015-12-03 09:20:44 -0800505 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000506 const char *rectName;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000507 // The inner rect is the rrect bounds inset by the x/y radii
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400508 fInnerRectUniform = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType,
509 "innerRect", &rectName);
joshualitt30ba4362014-08-21 20:18:45 -0700510
cdalton85285412016-02-18 12:37:07 -0800511 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000512 // At each quarter-ellipse corner we compute a vector that is the offset of the fragment pos
513 // to the ellipse center. The vector is pinned in x and y to be in the quarter-plane relevant
514 // to that corner. This means that points near the interior near the rrect top edge will have
515 // a vector that points straight up for both the TL left and TR corners. Computing an
516 // alpha from this vector at either the TR or TL corner will give the correct result. Similarly,
517 // fragments near the other three edges will get the correct AA. Fragments in the interior of
518 // the rrect will have a (0,0) vector at all four corners. So long as the radii > 0.5 they will
519 // 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 -0700520 //
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000521 // The code below is a simplified version of the above that performs maxs on the vector
522 // components before computing distances and alpha values so that only one distance computation
523 // need be computed to determine the min alpha.
Chris Dalton47c8ed32017-11-15 18:27:09 -0700524 fragBuilder->codeAppendf("float2 dxy0 = %s.xy - sk_FragCoord.xy;", rectName);
525 fragBuilder->codeAppendf("float2 dxy1 = sk_FragCoord.xy - %s.zw;", rectName);
bsalomone87256c42015-12-09 17:14:40 -0800526
Chris Dalton47c8ed32017-11-15 18:27:09 -0700527 // If we're on a device where float != fp32 then we'll do the distance computation in a space
bsalomone87256c42015-12-09 17:14:40 -0800528 // that is normalized by the largest radius. The scale uniform will be scale, 1/scale. The
529 // radii uniform values are already in this normalized space.
530 const char* scaleName = nullptr;
Chris Dalton47c8ed32017-11-15 18:27:09 -0700531 if (!args.fShaderCaps->floatIs32Bits()) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400532 fScaleUniform = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType, "scale",
533 &scaleName);
bsalomone87256c42015-12-09 17:14:40 -0800534 }
535
bsalomonc41f4d62015-08-03 14:23:03 -0700536 // The uniforms with the inv squared radii are highp to prevent underflow.
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000537 switch (erre.getRRect().getType()) {
538 case SkRRect::kSimple_Type: {
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000539 const char *invRadiiXYSqdName;
cdalton5e58cee2016-02-11 12:49:47 -0800540 fInvRadiiSqdUniform = uniformHandler->addUniform(kFragment_GrShaderFlag,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400541 kHalf2_GrSLType,
cdalton5e58cee2016-02-11 12:49:47 -0800542 "invRadiiXY",
543 &invRadiiXYSqdName);
Chris Dalton47c8ed32017-11-15 18:27:09 -0700544 fragBuilder->codeAppend("float2 dxy = max(max(dxy0, dxy1), 0.0);");
bsalomone87256c42015-12-09 17:14:40 -0800545 if (scaleName) {
546 fragBuilder->codeAppendf("dxy *= %s.y;", scaleName);
547 }
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000548 // Z is the x/y offsets divided by squared radii.
Chris Dalton47c8ed32017-11-15 18:27:09 -0700549 fragBuilder->codeAppendf("float2 Z = dxy * %s.xy;", invRadiiXYSqdName);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000550 break;
551 }
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000552 case SkRRect::kNinePatch_Type: {
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000553 const char *invRadiiLTRBSqdName;
cdalton5e58cee2016-02-11 12:49:47 -0800554 fInvRadiiSqdUniform = uniformHandler->addUniform(kFragment_GrShaderFlag,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400555 kHalf4_GrSLType,
cdalton5e58cee2016-02-11 12:49:47 -0800556 "invRadiiLTRB",
557 &invRadiiLTRBSqdName);
bsalomone87256c42015-12-09 17:14:40 -0800558 if (scaleName) {
559 fragBuilder->codeAppendf("dxy0 *= %s.y;", scaleName);
560 fragBuilder->codeAppendf("dxy1 *= %s.y;", scaleName);
561 }
Chris Dalton47c8ed32017-11-15 18:27:09 -0700562 fragBuilder->codeAppend("float2 dxy = max(max(dxy0, dxy1), 0.0);");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000563 // Z is the x/y offsets divided by squared radii. We only care about the (at most) one
564 // corner where both the x and y offsets are positive, hence the maxes. (The inverse
565 // squared radii will always be positive.)
Chris Dalton47c8ed32017-11-15 18:27:09 -0700566 fragBuilder->codeAppendf("float2 Z = max(max(dxy0 * %s.xy, dxy1 * %s.zw), 0.0);",
egdaniel4ca2e602015-11-18 08:01:26 -0800567 invRadiiLTRBSqdName, invRadiiLTRBSqdName);
bsalomone87256c42015-12-09 17:14:40 -0800568
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000569 break;
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000570 }
571 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400572 SK_ABORT("RRect should always be simple or nine-patch.");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000573 }
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000574 // implicit is the evaluation of (x/a)^2 + (y/b)^2 - 1.
Chris Dalton47c8ed32017-11-15 18:27:09 -0700575 fragBuilder->codeAppend("float implicit = dot(Z, dxy) - 1.0;");
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000576 // grad_dot is the squared length of the gradient of the implicit.
Chris Dalton47c8ed32017-11-15 18:27:09 -0700577 fragBuilder->codeAppend("float grad_dot = 4.0 * dot(Z, Z);");
commit-bot@chromium.org1b035d82014-04-09 17:11:09 +0000578 // avoid calling inversesqrt on zero.
bsalomone87256c42015-12-09 17:14:40 -0800579 fragBuilder->codeAppend("grad_dot = max(grad_dot, 1.0e-4);");
Chris Dalton47c8ed32017-11-15 18:27:09 -0700580 fragBuilder->codeAppend("float approx_dist = implicit * inversesqrt(grad_dot);");
bsalomone87256c42015-12-09 17:14:40 -0800581 if (scaleName) {
582 fragBuilder->codeAppendf("approx_dist *= %s.x;", scaleName);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000583 }
584
Ethan Nicholas1706f842017-11-10 11:58:19 -0500585 if (GrClipEdgeType::kFillAA == erre.getEdgeType()) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400586 fragBuilder->codeAppend("half alpha = clamp(0.5 - approx_dist, 0.0, 1.0);");
bsalomone87256c42015-12-09 17:14:40 -0800587 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400588 fragBuilder->codeAppend("half alpha = clamp(0.5 + approx_dist, 0.0, 1.0);");
bsalomone87256c42015-12-09 17:14:40 -0800589 }
590
Ethan Nicholas2983f402017-05-08 09:36:08 -0400591 fragBuilder->codeAppendf("%s = %s * alpha;", args.fOutputColor, args.fInputColor);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000592}
593
Brian Salomon94efbf52016-11-29 13:43:05 -0500594void GLEllipticalRRectEffect::GenKey(const GrProcessor& effect, const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700595 GrProcessorKeyBuilder* b) {
joshualitt49586be2014-09-16 08:21:41 -0700596 const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>();
Ethan Nicholas1706f842017-11-10 11:58:19 -0500597 GR_STATIC_ASSERT((int) GrClipEdgeType::kLast < (1 << 3));
598 b->add32(erre.getRRect().getType() | (int) erre.getEdgeType() << 3);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000599}
600
egdaniel018fb622015-10-28 07:26:40 -0700601void GLEllipticalRRectEffect::onSetData(const GrGLSLProgramDataManager& pdman,
Brian Salomonab015ef2017-04-04 10:15:51 -0400602 const GrFragmentProcessor& effect) {
joshualitt49586be2014-09-16 08:21:41 -0700603 const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>();
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000604 const SkRRect& rrect = erre.getRRect();
bsalomone87256c42015-12-09 17:14:40 -0800605 // If we're using a scale factor to work around precision issues, choose the largest radius
606 // as the scale factor. The inv radii need to be pre-adjusted by the scale factor.
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000607 if (rrect != fPrevRRect) {
608 SkRect rect = rrect.getBounds();
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000609 const SkVector& r0 = rrect.radii(SkRRect::kUpperLeft_Corner);
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000610 SkASSERT(r0.fX >= kRadiusMin);
611 SkASSERT(r0.fY >= kRadiusMin);
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000612 switch (erre.getRRect().getType()) {
613 case SkRRect::kSimple_Type:
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000614 rect.inset(r0.fX, r0.fY);
bsalomone87256c42015-12-09 17:14:40 -0800615 if (fScaleUniform.isValid()) {
616 if (r0.fX > r0.fY) {
617 pdman.set2f(fInvRadiiSqdUniform, 1.f, (r0.fX * r0.fX) / (r0.fY * r0.fY));
618 pdman.set2f(fScaleUniform, r0.fX, 1.f / r0.fX);
619 } else {
620 pdman.set2f(fInvRadiiSqdUniform, (r0.fY * r0.fY) / (r0.fX * r0.fX), 1.f);
621 pdman.set2f(fScaleUniform, r0.fY, 1.f / r0.fY);
622 }
623 } else {
624 pdman.set2f(fInvRadiiSqdUniform, 1.f / (r0.fX * r0.fX),
625 1.f / (r0.fY * r0.fY));
626 }
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000627 break;
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000628 case SkRRect::kNinePatch_Type: {
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000629 const SkVector& r1 = rrect.radii(SkRRect::kLowerRight_Corner);
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000630 SkASSERT(r1.fX >= kRadiusMin);
631 SkASSERT(r1.fY >= kRadiusMin);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000632 rect.fLeft += r0.fX;
633 rect.fTop += r0.fY;
634 rect.fRight -= r1.fX;
635 rect.fBottom -= r1.fY;
bsalomone87256c42015-12-09 17:14:40 -0800636 if (fScaleUniform.isValid()) {
637 float scale = SkTMax(SkTMax(r0.fX, r0.fY), SkTMax(r1.fX, r1.fY));
638 float scaleSqd = scale * scale;
639 pdman.set4f(fInvRadiiSqdUniform, scaleSqd / (r0.fX * r0.fX),
640 scaleSqd / (r0.fY * r0.fY),
641 scaleSqd / (r1.fX * r1.fX),
642 scaleSqd / (r1.fY * r1.fY));
643 pdman.set2f(fScaleUniform, scale, 1.f / scale);
644 } else {
645 pdman.set4f(fInvRadiiSqdUniform, 1.f / (r0.fX * r0.fX),
646 1.f / (r0.fY * r0.fY),
647 1.f / (r1.fX * r1.fX),
648 1.f / (r1.fY * r1.fY));
649 }
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000650 break;
651 }
commit-bot@chromium.org9615d5f2014-03-20 21:39:03 +0000652 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400653 SK_ABORT("RRect should always be simple or nine-patch.");
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000654 }
kkinnunen7510b222014-07-30 00:04:16 -0700655 pdman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000656 fPrevRRect = rrect;
657 }
658}
659
joshualitteb2a6762014-12-04 11:35:33 -0800660////////////////////////////////////////////////////////////////////////////////////////////////////
661
Brian Salomon94efbf52016-11-29 13:43:05 -0500662void EllipticalRRectEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800663 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800664 GLEllipticalRRectEffect::GenKey(*this, caps, b);
665}
666
egdaniel57d3b032015-11-13 11:57:27 -0800667GrGLSLFragmentProcessor* EllipticalRRectEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -0800668 return new GLEllipticalRRectEffect;
joshualitteb2a6762014-12-04 11:35:33 -0800669}
670
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000671//////////////////////////////////////////////////////////////////////////////
672
Ethan Nicholas0f3c7322017-11-09 14:51:17 -0500673std::unique_ptr<GrFragmentProcessor> GrRRectEffect::Make(GrClipEdgeType edgeType,
Brian Salomon14471772017-12-05 10:35:15 -0500674 const SkRRect& rrect,
Ethan Nicholas222e2752018-10-11 11:21:34 -0400675 GrContext* context) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000676 if (rrect.isRect()) {
Ethan Nicholas222e2752018-10-11 11:21:34 -0400677 return GrConvexPolyEffect::Make(edgeType, rrect.getBounds(), context);
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000678 }
679
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000680 if (rrect.isOval()) {
Ethan Nicholas222e2752018-10-11 11:21:34 -0400681 return GrOvalEffect::Make(edgeType, rrect.getBounds(),
682 *context->contextPriv().caps()->shaderCaps());
commit-bot@chromium.org3eedb802014-03-28 15:58:31 +0000683 }
684
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000685 if (rrect.isSimple()) {
Mike Reed242135a2018-02-22 13:41:39 -0500686 if (SkRRectPriv::GetSimpleRadii(rrect).fX < kRadiusMin ||
687 SkRRectPriv::GetSimpleRadii(rrect).fY < kRadiusMin) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000688 // In this case the corners are extremely close to rectangular and we collapse the
689 // clip to a rectangular clip.
Ethan Nicholas222e2752018-10-11 11:21:34 -0400690 return GrConvexPolyEffect::Make(edgeType, rrect.getBounds(), context);
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000691 }
Mike Reed242135a2018-02-22 13:41:39 -0500692 if (SkRRectPriv::GetSimpleRadii(rrect).fX == SkRRectPriv::GetSimpleRadii(rrect).fY) {
bungeman06ca8ec2016-06-09 08:01:03 -0700693 return CircularRRectEffect::Make(edgeType, CircularRRectEffect::kAll_CornerFlags,
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000694 rrect);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000695 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700696 return EllipticalRRectEffect::Make(edgeType, rrect);
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000697 }
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000698 }
699
700 if (rrect.isComplex() || rrect.isNinePatch()) {
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000701 // Check for the "tab" cases - two adjacent circular corners and two square corners.
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000702 SkScalar circularRadius = 0;
703 uint32_t cornerFlags = 0;
704
705 SkVector radii[4];
706 bool squashedRadii = false;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000707 for (int c = 0; c < 4; ++c) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000708 radii[c] = rrect.radii((SkRRect::Corner)c);
709 SkASSERT((0 == radii[c].fX) == (0 == radii[c].fY));
710 if (0 == radii[c].fX) {
711 // The corner is square, so no need to squash or flag as circular.
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000712 continue;
713 }
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000714 if (radii[c].fX < kRadiusMin || radii[c].fY < kRadiusMin) {
715 radii[c].set(0, 0);
716 squashedRadii = true;
717 continue;
718 }
719 if (radii[c].fX != radii[c].fY) {
bsalomon@google.com44a435b2014-03-13 19:20:32 +0000720 cornerFlags = ~0U;
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000721 break;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000722 }
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000723 if (!cornerFlags) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000724 circularRadius = radii[c].fX;
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000725 cornerFlags = 1 << c;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000726 } else {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000727 if (radii[c].fX != circularRadius) {
bsalomon@google.com44a435b2014-03-13 19:20:32 +0000728 cornerFlags = ~0U;
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000729 break;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000730 }
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000731 cornerFlags |= 1 << c;
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000732 }
733 }
734
commit-bot@chromium.orgdd584222014-03-10 16:08:22 +0000735 switch (cornerFlags) {
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000736 case CircularRRectEffect::kAll_CornerFlags:
737 // This rrect should have been caught in the simple case above. Though, it would
738 // be correctly handled in the fallthrough code.
739 SkASSERT(false);
commit-bot@chromium.org4355f212014-03-12 15:32:50 +0000740 case CircularRRectEffect::kTopLeft_CornerFlag:
741 case CircularRRectEffect::kTopRight_CornerFlag:
742 case CircularRRectEffect::kBottomRight_CornerFlag:
743 case CircularRRectEffect::kBottomLeft_CornerFlag:
744 case CircularRRectEffect::kLeft_CornerFlags:
745 case CircularRRectEffect::kTop_CornerFlags:
746 case CircularRRectEffect::kRight_CornerFlags:
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000747 case CircularRRectEffect::kBottom_CornerFlags: {
748 SkTCopyOnFirstWrite<SkRRect> rr(rrect);
749 if (squashedRadii) {
750 rr.writable()->setRectRadii(rrect.getBounds(), radii);
751 }
bungeman06ca8ec2016-06-09 08:01:03 -0700752 return CircularRRectEffect::Make(edgeType, cornerFlags, *rr);
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000753 }
754 case CircularRRectEffect::kNone_CornerFlags:
Ethan Nicholas222e2752018-10-11 11:21:34 -0400755 return GrConvexPolyEffect::Make(edgeType, rrect.getBounds(), context);
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000756 default: {
757 if (squashedRadii) {
758 // If we got here then we squashed some but not all the radii to zero. (If all
759 // had been squashed cornerFlags would be 0.) The elliptical effect doesn't
760 // support some rounded and some square corners.
halcanary96fcdcc2015-08-27 07:41:13 -0700761 return nullptr;
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000762 }
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000763 if (rrect.isNinePatch()) {
bungeman06ca8ec2016-06-09 08:01:03 -0700764 return EllipticalRRectEffect::Make(edgeType, rrect);
commit-bot@chromium.orgfa5edbe2014-03-13 18:01:05 +0000765 }
halcanary96fcdcc2015-08-27 07:41:13 -0700766 return nullptr;
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000767 }
commit-bot@chromium.orgcb3672e2014-02-21 22:41:56 +0000768 }
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000769 }
commit-bot@chromium.org2a8be902014-03-24 19:24:59 +0000770
halcanary96fcdcc2015-08-27 07:41:13 -0700771 return nullptr;
commit-bot@chromium.orgc2f78242014-02-19 15:18:05 +0000772}