commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 1 | /* |
| 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 Nicholas | 222e275 | 2018-10-11 11:21:34 -0400 | [diff] [blame^] | 10 | #include "GrContext.h" |
| 11 | #include "GrContextPriv.h" |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 12 | #include "GrConvexPolyEffect.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 13 | #include "GrFragmentProcessor.h" |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 14 | #include "GrOvalEffect.h" |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 15 | #include "GrShaderCaps.h" |
Mike Reed | 242135a | 2018-02-22 13:41:39 -0500 | [diff] [blame] | 16 | #include "SkRRectPriv.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 17 | #include "SkTLazy.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 18 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 19 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 20 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 21 | #include "glsl/GrGLSLUniformHandler.h" |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 22 | |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 23 | // The effects defined here only handle rrect radii >= kRadiusMin. |
| 24 | static const SkScalar kRadiusMin = SK_ScalarHalf; |
| 25 | |
| 26 | ////////////////////////////////////////////////////////////////////////////// |
| 27 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 28 | class CircularRRectEffect : public GrFragmentProcessor { |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 29 | public: |
skia.committer@gmail.com | f1f66c0 | 2014-03-05 03:02:06 +0000 | [diff] [blame] | 30 | |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 31 | 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.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 45 | kNone_CornerFlags = 0 |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 48 | // The flags are used to indicate which corners are circluar (unflagged corners are assumed to |
| 49 | // be square). |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 50 | static std::unique_ptr<GrFragmentProcessor> Make(GrClipEdgeType, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 51 | uint32_t circularCornerFlags, const SkRRect&); |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 52 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 53 | ~CircularRRectEffect() override {} |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 54 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 55 | const char* name() const override { return "CircularRRect"; } |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 56 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 57 | std::unique_ptr<GrFragmentProcessor> clone() const override; |
Brian Salomon | 1a2a7ab | 2017-07-26 13:11:51 -0400 | [diff] [blame] | 58 | |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 59 | const SkRRect& getRRect() const { return fRRect; } |
| 60 | |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 61 | uint32_t getCircularCornerFlags() const { return fCircularCornerFlags; } |
skia.committer@gmail.com | 06acb58 | 2014-03-06 03:02:32 +0000 | [diff] [blame] | 62 | |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 63 | GrClipEdgeType getEdgeType() const { return fEdgeType; } |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 64 | |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 65 | private: |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 66 | CircularRRectEffect(GrClipEdgeType, uint32_t circularCornerFlags, const SkRRect&); |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 67 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 68 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 69 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 70 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 71 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 72 | bool onIsEqual(const GrFragmentProcessor& other) const override; |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 73 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 74 | SkRRect fRRect; |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 75 | GrClipEdgeType fEdgeType; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 76 | uint32_t fCircularCornerFlags; |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 77 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 78 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 79 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 80 | typedef GrFragmentProcessor INHERITED; |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 83 | std::unique_ptr<GrFragmentProcessor> CircularRRectEffect::Make(GrClipEdgeType edgeType, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 84 | uint32_t circularCornerFlags, |
| 85 | const SkRRect& rrect) { |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 86 | if (GrClipEdgeType::kFillAA != edgeType && GrClipEdgeType::kInverseFillAA != edgeType) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 87 | return nullptr; |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 88 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 89 | return std::unique_ptr<GrFragmentProcessor>( |
| 90 | new CircularRRectEffect(edgeType, circularCornerFlags, rrect)); |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 93 | CircularRRectEffect::CircularRRectEffect(GrClipEdgeType edgeType, uint32_t circularCornerFlags, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 94 | const SkRRect& rrect) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 95 | : INHERITED(kCircularRRectEffect_ClassID, kCompatibleWithCoverageAsAlpha_OptimizationFlag) |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 96 | , fRRect(rrect) |
| 97 | , fEdgeType(edgeType) |
| 98 | , fCircularCornerFlags(circularCornerFlags) { |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 101 | std::unique_ptr<GrFragmentProcessor> CircularRRectEffect::clone() const { |
| 102 | return std::unique_ptr<GrFragmentProcessor>( |
Brian Salomon | 1a2a7ab | 2017-07-26 13:11:51 -0400 | [diff] [blame] | 103 | new CircularRRectEffect(fEdgeType, fCircularCornerFlags, fRRect)); |
| 104 | } |
| 105 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 106 | bool CircularRRectEffect::onIsEqual(const GrFragmentProcessor& other) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 107 | const CircularRRectEffect& crre = other.cast<CircularRRectEffect>(); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 108 | // The corner flags are derived from fRRect, so no need to check them. |
| 109 | return fEdgeType == crre.fEdgeType && fRRect == crre.fRRect; |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 110 | } |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 111 | |
| 112 | ////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 113 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 114 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(CircularRRectEffect); |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 115 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 116 | #if GR_TEST_UTILS |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 117 | std::unique_ptr<GrFragmentProcessor> CircularRRectEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 118 | 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.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 121 | SkRRect rrect; |
| 122 | rrect.setRectXY(SkRect::MakeWH(w, h), r, r); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 123 | std::unique_ptr<GrFragmentProcessor> fp; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 124 | do { |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 125 | GrClipEdgeType et = |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 126 | (GrClipEdgeType)d->fRandom->nextULessThan(kGrClipEdgeTypeCnt); |
Ethan Nicholas | 222e275 | 2018-10-11 11:21:34 -0400 | [diff] [blame^] | 127 | fp = GrRRectEffect::Make(et, rrect, d->context()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 128 | } while (nullptr == fp); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 129 | return fp; |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 130 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 131 | #endif |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 132 | |
| 133 | ////////////////////////////////////////////////////////////////////////////// |
| 134 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 135 | class GLCircularRRectEffect : public GrGLSLFragmentProcessor { |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 136 | public: |
Brian Salomon | 0a241ce | 2017-12-15 11:31:05 -0500 | [diff] [blame] | 137 | GLCircularRRectEffect() = default; |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 138 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 139 | virtual void emitCode(EmitArgs&) override; |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 140 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 141 | static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*); |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 142 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 143 | protected: |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 144 | void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 145 | |
| 146 | private: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 147 | GrGLSLProgramDataManager::UniformHandle fInnerRectUniform; |
| 148 | GrGLSLProgramDataManager::UniformHandle fRadiusPlusHalfUniform; |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 149 | SkRRect fPrevRRect; |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 150 | typedef GrGLSLFragmentProcessor INHERITED; |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 153 | void GLCircularRRectEffect::emitCode(EmitArgs& args) { |
| 154 | const CircularRRectEffect& crre = args.fFp.cast<CircularRRectEffect>(); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 155 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 156 | const char *rectName; |
| 157 | const char *radiusPlusHalfName; |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 158 | // 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 Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 162 | fInnerRectUniform = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType, |
| 163 | "innerRect", &rectName); |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 164 | // x is (r + .5) and y is 1/(r + .5) |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 165 | fRadiusPlusHalfUniform = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType, |
| 166 | "radiusPlusHalf", &radiusPlusHalfName); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 167 | |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 168 | // If we're on a device where float != fp32 then the length calculation could overflow. |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 169 | SkString clampedCircleDistance; |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 170 | if (!args.fShaderCaps->floatIs32Bits()) { |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 171 | clampedCircleDistance.printf("saturate(%s.x * (1.0 - length(dxy * %s.y)));", |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 172 | radiusPlusHalfName, radiusPlusHalfName); |
| 173 | } else { |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 174 | clampedCircleDistance.printf("saturate(%s.x - length(dxy));", radiusPlusHalfName); |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 175 | } |
| 176 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 177 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 178 | // 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.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 189 | // |
| 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.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 193 | switch (crre.getCircularCornerFlags()) { |
| 194 | case CircularRRectEffect::kAll_CornerFlags: |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 195 | 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 Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 198 | fragBuilder->codeAppendf("half alpha = %s;", clampedCircleDistance.c_str()); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 199 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 200 | case CircularRRectEffect::kTopLeft_CornerFlag: |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 201 | fragBuilder->codeAppendf("float2 dxy = max(%s.xy - sk_FragCoord.xy, 0.0);", |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 202 | rectName); |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 203 | fragBuilder->codeAppendf("half rightAlpha = saturate(%s.z - sk_FragCoord.x);", |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 204 | rectName); |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 205 | fragBuilder->codeAppendf("half bottomAlpha = saturate(%s.w - sk_FragCoord.y);", |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 206 | rectName); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 207 | fragBuilder->codeAppendf("half alpha = bottomAlpha * rightAlpha * %s;", |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 208 | clampedCircleDistance.c_str()); |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 209 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 210 | case CircularRRectEffect::kTopRight_CornerFlag: |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 211 | fragBuilder->codeAppendf("float2 dxy = max(float2(sk_FragCoord.x - %s.z, " |
| 212 | "%s.y - sk_FragCoord.y), 0.0);", |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 213 | rectName, rectName); |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 214 | fragBuilder->codeAppendf("half leftAlpha = saturate(sk_FragCoord.x - %s.x);", |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 215 | rectName); |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 216 | fragBuilder->codeAppendf("half bottomAlpha = saturate(%s.w - sk_FragCoord.y);", |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 217 | rectName); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 218 | fragBuilder->codeAppendf("half alpha = bottomAlpha * leftAlpha * %s;", |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 219 | clampedCircleDistance.c_str()); |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 220 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 221 | case CircularRRectEffect::kBottomRight_CornerFlag: |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 222 | fragBuilder->codeAppendf("float2 dxy = max(sk_FragCoord.xy - %s.zw, 0.0);", |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 223 | rectName); |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 224 | fragBuilder->codeAppendf("half leftAlpha = saturate(sk_FragCoord.x - %s.x);", |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 225 | rectName); |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 226 | fragBuilder->codeAppendf("half topAlpha = saturate(sk_FragCoord.y - %s.y);", |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 227 | rectName); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 228 | fragBuilder->codeAppendf("half alpha = topAlpha * leftAlpha * %s;", |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 229 | clampedCircleDistance.c_str()); |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 230 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 231 | case CircularRRectEffect::kBottomLeft_CornerFlag: |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 232 | fragBuilder->codeAppendf("float2 dxy = max(float2(%s.x - sk_FragCoord.x, " |
| 233 | "sk_FragCoord.y - %s.w), 0.0);", |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 234 | rectName, rectName); |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 235 | fragBuilder->codeAppendf("half rightAlpha = saturate(%s.z - sk_FragCoord.x);", |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 236 | rectName); |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 237 | fragBuilder->codeAppendf("half topAlpha = saturate(sk_FragCoord.y - %s.y);", |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 238 | rectName); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 239 | fragBuilder->codeAppendf("half alpha = topAlpha * rightAlpha * %s;", |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 240 | clampedCircleDistance.c_str()); |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 241 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 242 | case CircularRRectEffect::kLeft_CornerFlags: |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 243 | 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 Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 246 | fragBuilder->codeAppendf("half rightAlpha = saturate(%s.z - sk_FragCoord.x);", |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 247 | rectName); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 248 | fragBuilder->codeAppendf("half alpha = rightAlpha * %s;", |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 249 | clampedCircleDistance.c_str()); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 250 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 251 | case CircularRRectEffect::kTop_CornerFlags: |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 252 | 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 Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 255 | fragBuilder->codeAppendf("half bottomAlpha = saturate(%s.w - sk_FragCoord.y);", |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 256 | rectName); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 257 | fragBuilder->codeAppendf("half alpha = bottomAlpha * %s;", |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 258 | clampedCircleDistance.c_str()); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 259 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 260 | case CircularRRectEffect::kRight_CornerFlags: |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 261 | 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 Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 264 | fragBuilder->codeAppendf("half leftAlpha = saturate(sk_FragCoord.x - %s.x);", |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 265 | rectName); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 266 | fragBuilder->codeAppendf("half alpha = leftAlpha * %s;", |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 267 | clampedCircleDistance.c_str()); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 268 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 269 | case CircularRRectEffect::kBottom_CornerFlags: |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 270 | 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 Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 273 | fragBuilder->codeAppendf("half topAlpha = saturate(sk_FragCoord.y - %s.y);", |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 274 | rectName); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 275 | fragBuilder->codeAppendf("half alpha = topAlpha * %s;", |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 276 | clampedCircleDistance.c_str()); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 277 | break; |
| 278 | } |
skia.committer@gmail.com | 06acb58 | 2014-03-06 03:02:32 +0000 | [diff] [blame] | 279 | |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 280 | if (GrClipEdgeType::kInverseFillAA == crre.getEdgeType()) { |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 281 | fragBuilder->codeAppend("alpha = 1.0 - alpha;"); |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 282 | } |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 283 | |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 284 | fragBuilder->codeAppendf("%s = %s * alpha;", args.fOutputColor, args.fInputColor); |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 287 | void GLCircularRRectEffect::GenKey(const GrProcessor& processor, const GrShaderCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 288 | GrProcessorKeyBuilder* b) { |
| 289 | const CircularRRectEffect& crre = processor.cast<CircularRRectEffect>(); |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 290 | GR_STATIC_ASSERT(kGrClipEdgeTypeCnt <= 8); |
| 291 | b->add32((crre.getCircularCornerFlags() << 3) | (int) crre.getEdgeType()); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 292 | } |
| 293 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 294 | void GLCircularRRectEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 295 | const GrFragmentProcessor& processor) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 296 | const CircularRRectEffect& crre = processor.cast<CircularRRectEffect>(); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 297 | const SkRRect& rrect = crre.getRRect(); |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 298 | if (rrect != fPrevRRect) { |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 299 | SkRect rect = rrect.getBounds(); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 300 | SkScalar radius = 0; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 301 | switch (crre.getCircularCornerFlags()) { |
| 302 | case CircularRRectEffect::kAll_CornerFlags: |
Mike Reed | 242135a | 2018-02-22 13:41:39 -0500 | [diff] [blame] | 303 | SkASSERT(SkRRectPriv::IsSimpleCircular(rrect)); |
| 304 | radius = SkRRectPriv::GetSimpleRadii(rrect).fX; |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 305 | SkASSERT(radius >= kRadiusMin); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 306 | rect.inset(radius, radius); |
| 307 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 308 | case CircularRRectEffect::kTopLeft_CornerFlag: |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 309 | 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.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 315 | case CircularRRectEffect::kTopRight_CornerFlag: |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 316 | radius = rrect.radii(SkRRect::kUpperRight_Corner).fX; |
bsalomon@google.com | de9f251 | 2014-03-11 17:09:17 +0000 | [diff] [blame] | 317 | rect.fLeft -= 0.5f; |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 318 | rect.fTop += radius; |
| 319 | rect.fRight -= radius; |
| 320 | rect.fBottom += 0.5f; |
| 321 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 322 | case CircularRRectEffect::kBottomRight_CornerFlag: |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 323 | radius = rrect.radii(SkRRect::kLowerRight_Corner).fX; |
bsalomon@google.com | de9f251 | 2014-03-11 17:09:17 +0000 | [diff] [blame] | 324 | rect.fLeft -= 0.5f; |
| 325 | rect.fTop -= 0.5f; |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 326 | rect.fRight -= radius; |
| 327 | rect.fBottom -= radius; |
| 328 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 329 | case CircularRRectEffect::kBottomLeft_CornerFlag: |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 330 | radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX; |
| 331 | rect.fLeft += radius; |
bsalomon@google.com | de9f251 | 2014-03-11 17:09:17 +0000 | [diff] [blame] | 332 | rect.fTop -= 0.5f; |
| 333 | rect.fRight += 0.5f; |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 334 | rect.fBottom -= radius; |
| 335 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 336 | case CircularRRectEffect::kLeft_CornerFlags: |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 337 | 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.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 343 | case CircularRRectEffect::kTop_CornerFlags: |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 344 | 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.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 350 | case CircularRRectEffect::kRight_CornerFlags: |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 351 | 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.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 357 | case CircularRRectEffect::kBottom_CornerFlags: |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 358 | 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.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 364 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 365 | SK_ABORT("Should have been one of the above cases."); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 366 | } |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 367 | pdman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom); |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 368 | radius += 0.5f; |
| 369 | pdman.set2f(fRadiusPlusHalfUniform, radius, 1.f / radius); |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 370 | fPrevRRect = rrect; |
| 371 | } |
| 372 | } |
| 373 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 374 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 375 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 376 | void CircularRRectEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 377 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 378 | GLCircularRRectEffect::GenKey(*this, caps, b); |
| 379 | } |
| 380 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 381 | GrGLSLFragmentProcessor* CircularRRectEffect::onCreateGLSLInstance() const { |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 382 | return new GLCircularRRectEffect; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | ////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 386 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 387 | class EllipticalRRectEffect : public GrFragmentProcessor { |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 388 | public: |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 389 | static std::unique_ptr<GrFragmentProcessor> Make(GrClipEdgeType, const SkRRect&); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 390 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 391 | ~EllipticalRRectEffect() override {} |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 392 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 393 | const char* name() const override { return "EllipticalRRect"; } |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 394 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 395 | std::unique_ptr<GrFragmentProcessor> clone() const override; |
Brian Salomon | 1a2a7ab | 2017-07-26 13:11:51 -0400 | [diff] [blame] | 396 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 397 | const SkRRect& getRRect() const { return fRRect; } |
| 398 | |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 399 | GrClipEdgeType getEdgeType() const { return fEdgeType; } |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 400 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 401 | private: |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 402 | EllipticalRRectEffect(GrClipEdgeType, const SkRRect&); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 403 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 404 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 405 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 406 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 407 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 408 | bool onIsEqual(const GrFragmentProcessor& other) const override; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 409 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 410 | SkRRect fRRect; |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 411 | GrClipEdgeType fEdgeType; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 412 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 413 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 414 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 415 | typedef GrFragmentProcessor INHERITED; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 416 | }; |
| 417 | |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 418 | std::unique_ptr<GrFragmentProcessor> EllipticalRRectEffect::Make(GrClipEdgeType edgeType, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 419 | const SkRRect& rrect) { |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 420 | if (GrClipEdgeType::kFillAA != edgeType && GrClipEdgeType::kInverseFillAA != edgeType) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 421 | return nullptr; |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 422 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 423 | return std::unique_ptr<GrFragmentProcessor>(new EllipticalRRectEffect(edgeType, rrect)); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 426 | EllipticalRRectEffect::EllipticalRRectEffect(GrClipEdgeType edgeType, const SkRRect& rrect) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 427 | : INHERITED(kEllipticalRRectEffect_ClassID, kCompatibleWithCoverageAsAlpha_OptimizationFlag) |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 428 | , fRRect(rrect) |
| 429 | , fEdgeType(edgeType) { |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 432 | std::unique_ptr<GrFragmentProcessor> EllipticalRRectEffect::clone() const { |
| 433 | return std::unique_ptr<GrFragmentProcessor>(new EllipticalRRectEffect(fEdgeType, fRRect)); |
Brian Salomon | 1a2a7ab | 2017-07-26 13:11:51 -0400 | [diff] [blame] | 434 | } |
| 435 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 436 | bool EllipticalRRectEffect::onIsEqual(const GrFragmentProcessor& other) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 437 | const EllipticalRRectEffect& erre = other.cast<EllipticalRRectEffect>(); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 438 | return fEdgeType == erre.fEdgeType && fRRect == erre.fRRect; |
| 439 | } |
| 440 | |
| 441 | ////////////////////////////////////////////////////////////////////////////// |
| 442 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 443 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(EllipticalRRectEffect); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 444 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 445 | #if GR_TEST_UTILS |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 446 | std::unique_ptr<GrFragmentProcessor> EllipticalRRectEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 447 | SkScalar w = d->fRandom->nextRangeScalar(20.f, 1000.f); |
| 448 | SkScalar h = d->fRandom->nextRangeScalar(20.f, 1000.f); |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 449 | SkVector r[4]; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 450 | r[SkRRect::kUpperLeft_Corner].fX = d->fRandom->nextRangeF(kRadiusMin, 9.f); |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 451 | // ensure at least one corner really is elliptical |
| 452 | do { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 453 | r[SkRRect::kUpperLeft_Corner].fY = d->fRandom->nextRangeF(kRadiusMin, 9.f); |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 454 | } while (r[SkRRect::kUpperLeft_Corner].fY == r[SkRRect::kUpperLeft_Corner].fX); |
| 455 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 456 | SkRRect rrect; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 457 | if (d->fRandom->nextBool()) { |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 458 | // half the time create a four-radii rrect. |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 459 | 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.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 461 | |
| 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 Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 473 | std::unique_ptr<GrFragmentProcessor> fp; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 474 | do { |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 475 | GrClipEdgeType et = (GrClipEdgeType)d->fRandom->nextULessThan(kGrClipEdgeTypeCnt); |
Ethan Nicholas | 222e275 | 2018-10-11 11:21:34 -0400 | [diff] [blame^] | 476 | fp = GrRRectEffect::Make(et, rrect, d->context()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 477 | } while (nullptr == fp); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 478 | return fp; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 479 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 480 | #endif |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 481 | |
| 482 | ////////////////////////////////////////////////////////////////////////////// |
| 483 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 484 | class GLEllipticalRRectEffect : public GrGLSLFragmentProcessor { |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 485 | public: |
Brian Salomon | 0a241ce | 2017-12-15 11:31:05 -0500 | [diff] [blame] | 486 | GLEllipticalRRectEffect() = default; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 487 | |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 488 | void emitCode(EmitArgs&) override; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 489 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 490 | static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 491 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 492 | protected: |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 493 | void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 494 | |
| 495 | private: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 496 | GrGLSLProgramDataManager::UniformHandle fInnerRectUniform; |
| 497 | GrGLSLProgramDataManager::UniformHandle fInvRadiiSqdUniform; |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 498 | GrGLSLProgramDataManager::UniformHandle fScaleUniform; |
| 499 | SkRRect fPrevRRect; |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 500 | typedef GrGLSLFragmentProcessor INHERITED; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 501 | }; |
| 502 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 503 | void GLEllipticalRRectEffect::emitCode(EmitArgs& args) { |
| 504 | const EllipticalRRectEffect& erre = args.fFp.cast<EllipticalRRectEffect>(); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 505 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 506 | const char *rectName; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 507 | // The inner rect is the rrect bounds inset by the x/y radii |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 508 | fInnerRectUniform = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType, |
| 509 | "innerRect", &rectName); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 510 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 511 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 512 | // 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. |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 520 | // |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 521 | // 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 Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 524 | fragBuilder->codeAppendf("float2 dxy0 = %s.xy - sk_FragCoord.xy;", rectName); |
| 525 | fragBuilder->codeAppendf("float2 dxy1 = sk_FragCoord.xy - %s.zw;", rectName); |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 526 | |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 527 | // If we're on a device where float != fp32 then we'll do the distance computation in a space |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 528 | // 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 Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 531 | if (!args.fShaderCaps->floatIs32Bits()) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 532 | fScaleUniform = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType, "scale", |
| 533 | &scaleName); |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 534 | } |
| 535 | |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 536 | // The uniforms with the inv squared radii are highp to prevent underflow. |
commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 537 | switch (erre.getRRect().getType()) { |
| 538 | case SkRRect::kSimple_Type: { |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 539 | const char *invRadiiXYSqdName; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 540 | fInvRadiiSqdUniform = uniformHandler->addUniform(kFragment_GrShaderFlag, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 541 | kHalf2_GrSLType, |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 542 | "invRadiiXY", |
| 543 | &invRadiiXYSqdName); |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 544 | fragBuilder->codeAppend("float2 dxy = max(max(dxy0, dxy1), 0.0);"); |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 545 | if (scaleName) { |
| 546 | fragBuilder->codeAppendf("dxy *= %s.y;", scaleName); |
| 547 | } |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 548 | // Z is the x/y offsets divided by squared radii. |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 549 | fragBuilder->codeAppendf("float2 Z = dxy * %s.xy;", invRadiiXYSqdName); |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 550 | break; |
| 551 | } |
commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 552 | case SkRRect::kNinePatch_Type: { |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 553 | const char *invRadiiLTRBSqdName; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 554 | fInvRadiiSqdUniform = uniformHandler->addUniform(kFragment_GrShaderFlag, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 555 | kHalf4_GrSLType, |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 556 | "invRadiiLTRB", |
| 557 | &invRadiiLTRBSqdName); |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 558 | if (scaleName) { |
| 559 | fragBuilder->codeAppendf("dxy0 *= %s.y;", scaleName); |
| 560 | fragBuilder->codeAppendf("dxy1 *= %s.y;", scaleName); |
| 561 | } |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 562 | fragBuilder->codeAppend("float2 dxy = max(max(dxy0, dxy1), 0.0);"); |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 563 | // 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 Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 566 | fragBuilder->codeAppendf("float2 Z = max(max(dxy0 * %s.xy, dxy1 * %s.zw), 0.0);", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 567 | invRadiiLTRBSqdName, invRadiiLTRBSqdName); |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 568 | |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 569 | break; |
commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 570 | } |
| 571 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 572 | SK_ABORT("RRect should always be simple or nine-patch."); |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 573 | } |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 574 | // implicit is the evaluation of (x/a)^2 + (y/b)^2 - 1. |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 575 | fragBuilder->codeAppend("float implicit = dot(Z, dxy) - 1.0;"); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 576 | // grad_dot is the squared length of the gradient of the implicit. |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 577 | fragBuilder->codeAppend("float grad_dot = 4.0 * dot(Z, Z);"); |
commit-bot@chromium.org | 1b035d8 | 2014-04-09 17:11:09 +0000 | [diff] [blame] | 578 | // avoid calling inversesqrt on zero. |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 579 | fragBuilder->codeAppend("grad_dot = max(grad_dot, 1.0e-4);"); |
Chris Dalton | 47c8ed3 | 2017-11-15 18:27:09 -0700 | [diff] [blame] | 580 | fragBuilder->codeAppend("float approx_dist = implicit * inversesqrt(grad_dot);"); |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 581 | if (scaleName) { |
| 582 | fragBuilder->codeAppendf("approx_dist *= %s.x;", scaleName); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 585 | if (GrClipEdgeType::kFillAA == erre.getEdgeType()) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 586 | fragBuilder->codeAppend("half alpha = clamp(0.5 - approx_dist, 0.0, 1.0);"); |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 587 | } else { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 588 | fragBuilder->codeAppend("half alpha = clamp(0.5 + approx_dist, 0.0, 1.0);"); |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 589 | } |
| 590 | |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 591 | fragBuilder->codeAppendf("%s = %s * alpha;", args.fOutputColor, args.fInputColor); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 594 | void GLEllipticalRRectEffect::GenKey(const GrProcessor& effect, const GrShaderCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 595 | GrProcessorKeyBuilder* b) { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 596 | const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>(); |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 597 | GR_STATIC_ASSERT((int) GrClipEdgeType::kLast < (1 << 3)); |
| 598 | b->add32(erre.getRRect().getType() | (int) erre.getEdgeType() << 3); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 599 | } |
| 600 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 601 | void GLEllipticalRRectEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 602 | const GrFragmentProcessor& effect) { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 603 | const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>(); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 604 | const SkRRect& rrect = erre.getRRect(); |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 605 | // 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.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 607 | if (rrect != fPrevRRect) { |
| 608 | SkRect rect = rrect.getBounds(); |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 609 | const SkVector& r0 = rrect.radii(SkRRect::kUpperLeft_Corner); |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 610 | SkASSERT(r0.fX >= kRadiusMin); |
| 611 | SkASSERT(r0.fY >= kRadiusMin); |
commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 612 | switch (erre.getRRect().getType()) { |
| 613 | case SkRRect::kSimple_Type: |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 614 | rect.inset(r0.fX, r0.fY); |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 615 | 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.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 627 | break; |
commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 628 | case SkRRect::kNinePatch_Type: { |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 629 | const SkVector& r1 = rrect.radii(SkRRect::kLowerRight_Corner); |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 630 | SkASSERT(r1.fX >= kRadiusMin); |
| 631 | SkASSERT(r1.fY >= kRadiusMin); |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 632 | rect.fLeft += r0.fX; |
| 633 | rect.fTop += r0.fY; |
| 634 | rect.fRight -= r1.fX; |
| 635 | rect.fBottom -= r1.fY; |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 636 | 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.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 650 | break; |
| 651 | } |
commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 652 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 653 | SK_ABORT("RRect should always be simple or nine-patch."); |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 654 | } |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 655 | pdman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 656 | fPrevRRect = rrect; |
| 657 | } |
| 658 | } |
| 659 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 660 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 661 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 662 | void EllipticalRRectEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 663 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 664 | GLEllipticalRRectEffect::GenKey(*this, caps, b); |
| 665 | } |
| 666 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 667 | GrGLSLFragmentProcessor* EllipticalRRectEffect::onCreateGLSLInstance() const { |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 668 | return new GLEllipticalRRectEffect; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 669 | } |
| 670 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 671 | ////////////////////////////////////////////////////////////////////////////// |
| 672 | |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 673 | std::unique_ptr<GrFragmentProcessor> GrRRectEffect::Make(GrClipEdgeType edgeType, |
Brian Salomon | 1447177 | 2017-12-05 10:35:15 -0500 | [diff] [blame] | 674 | const SkRRect& rrect, |
Ethan Nicholas | 222e275 | 2018-10-11 11:21:34 -0400 | [diff] [blame^] | 675 | GrContext* context) { |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 676 | if (rrect.isRect()) { |
Ethan Nicholas | 222e275 | 2018-10-11 11:21:34 -0400 | [diff] [blame^] | 677 | return GrConvexPolyEffect::Make(edgeType, rrect.getBounds(), context); |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 678 | } |
| 679 | |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 680 | if (rrect.isOval()) { |
Ethan Nicholas | 222e275 | 2018-10-11 11:21:34 -0400 | [diff] [blame^] | 681 | return GrOvalEffect::Make(edgeType, rrect.getBounds(), |
| 682 | *context->contextPriv().caps()->shaderCaps()); |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 683 | } |
| 684 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 685 | if (rrect.isSimple()) { |
Mike Reed | 242135a | 2018-02-22 13:41:39 -0500 | [diff] [blame] | 686 | if (SkRRectPriv::GetSimpleRadii(rrect).fX < kRadiusMin || |
| 687 | SkRRectPriv::GetSimpleRadii(rrect).fY < kRadiusMin) { |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 688 | // In this case the corners are extremely close to rectangular and we collapse the |
| 689 | // clip to a rectangular clip. |
Ethan Nicholas | 222e275 | 2018-10-11 11:21:34 -0400 | [diff] [blame^] | 690 | return GrConvexPolyEffect::Make(edgeType, rrect.getBounds(), context); |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 691 | } |
Mike Reed | 242135a | 2018-02-22 13:41:39 -0500 | [diff] [blame] | 692 | if (SkRRectPriv::GetSimpleRadii(rrect).fX == SkRRectPriv::GetSimpleRadii(rrect).fY) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 693 | return CircularRRectEffect::Make(edgeType, CircularRRectEffect::kAll_CornerFlags, |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 694 | rrect); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 695 | } else { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 696 | return EllipticalRRectEffect::Make(edgeType, rrect); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 697 | } |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | if (rrect.isComplex() || rrect.isNinePatch()) { |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 701 | // Check for the "tab" cases - two adjacent circular corners and two square corners. |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 702 | SkScalar circularRadius = 0; |
| 703 | uint32_t cornerFlags = 0; |
| 704 | |
| 705 | SkVector radii[4]; |
| 706 | bool squashedRadii = false; |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 707 | for (int c = 0; c < 4; ++c) { |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 708 | 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.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 712 | continue; |
| 713 | } |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 714 | 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.com | 44a435b | 2014-03-13 19:20:32 +0000 | [diff] [blame] | 720 | cornerFlags = ~0U; |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 721 | break; |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 722 | } |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 723 | if (!cornerFlags) { |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 724 | circularRadius = radii[c].fX; |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 725 | cornerFlags = 1 << c; |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 726 | } else { |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 727 | if (radii[c].fX != circularRadius) { |
bsalomon@google.com | 44a435b | 2014-03-13 19:20:32 +0000 | [diff] [blame] | 728 | cornerFlags = ~0U; |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 729 | break; |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 730 | } |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 731 | cornerFlags |= 1 << c; |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 735 | switch (cornerFlags) { |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 736 | 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.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 740 | 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.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 747 | case CircularRRectEffect::kBottom_CornerFlags: { |
| 748 | SkTCopyOnFirstWrite<SkRRect> rr(rrect); |
| 749 | if (squashedRadii) { |
| 750 | rr.writable()->setRectRadii(rrect.getBounds(), radii); |
| 751 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 752 | return CircularRRectEffect::Make(edgeType, cornerFlags, *rr); |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 753 | } |
| 754 | case CircularRRectEffect::kNone_CornerFlags: |
Ethan Nicholas | 222e275 | 2018-10-11 11:21:34 -0400 | [diff] [blame^] | 755 | return GrConvexPolyEffect::Make(edgeType, rrect.getBounds(), context); |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 756 | 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. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 761 | return nullptr; |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 762 | } |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 763 | if (rrect.isNinePatch()) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 764 | return EllipticalRRectEffect::Make(edgeType, rrect); |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 765 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 766 | return nullptr; |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 767 | } |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 768 | } |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 769 | } |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 770 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 771 | return nullptr; |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 772 | } |