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 | |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 10 | #include "GrConvexPolyEffect.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 11 | #include "GrFragmentProcessor.h" |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 12 | #include "GrOvalEffect.h" |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 13 | #include "GrShaderCaps.h" |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 14 | #include "SkRRect.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 15 | #include "SkTLazy.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 16 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 17 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 18 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 19 | #include "glsl/GrGLSLUniformHandler.h" |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 20 | |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 21 | // The effects defined here only handle rrect radii >= kRadiusMin. |
| 22 | static const SkScalar kRadiusMin = SK_ScalarHalf; |
| 23 | |
| 24 | ////////////////////////////////////////////////////////////////////////////// |
| 25 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 26 | class CircularRRectEffect : public GrFragmentProcessor { |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 27 | public: |
skia.committer@gmail.com | f1f66c0 | 2014-03-05 03:02:06 +0000 | [diff] [blame] | 28 | |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 29 | enum CornerFlags { |
| 30 | kTopLeft_CornerFlag = (1 << SkRRect::kUpperLeft_Corner), |
| 31 | kTopRight_CornerFlag = (1 << SkRRect::kUpperRight_Corner), |
| 32 | kBottomRight_CornerFlag = (1 << SkRRect::kLowerRight_Corner), |
| 33 | kBottomLeft_CornerFlag = (1 << SkRRect::kLowerLeft_Corner), |
| 34 | |
| 35 | kLeft_CornerFlags = kTopLeft_CornerFlag | kBottomLeft_CornerFlag, |
| 36 | kTop_CornerFlags = kTopLeft_CornerFlag | kTopRight_CornerFlag, |
| 37 | kRight_CornerFlags = kTopRight_CornerFlag | kBottomRight_CornerFlag, |
| 38 | kBottom_CornerFlags = kBottomLeft_CornerFlag | kBottomRight_CornerFlag, |
| 39 | |
| 40 | kAll_CornerFlags = kTopLeft_CornerFlag | kTopRight_CornerFlag | |
| 41 | kBottomLeft_CornerFlag | kBottomRight_CornerFlag, |
| 42 | |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 43 | kNone_CornerFlags = 0 |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 46 | // The flags are used to indicate which corners are circluar (unflagged corners are assumed to |
| 47 | // be square). |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 48 | static sk_sp<GrFragmentProcessor> Make(GrPrimitiveEdgeType, uint32_t circularCornerFlags, |
| 49 | const SkRRect&); |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 50 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 51 | ~CircularRRectEffect() override {} |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 52 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 53 | const char* name() const override { return "CircularRRect"; } |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 54 | |
Brian Salomon | 1a2a7ab | 2017-07-26 13:11:51 -0400 | [diff] [blame] | 55 | sk_sp<GrFragmentProcessor> clone() const override; |
| 56 | |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 57 | const SkRRect& getRRect() const { return fRRect; } |
| 58 | |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 59 | uint32_t getCircularCornerFlags() const { return fCircularCornerFlags; } |
skia.committer@gmail.com | 06acb58 | 2014-03-06 03:02:32 +0000 | [diff] [blame] | 60 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 61 | GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; } |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 62 | |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 63 | private: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 64 | CircularRRectEffect(GrPrimitiveEdgeType, uint32_t circularCornerFlags, const SkRRect&); |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 65 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 66 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 67 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 68 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 69 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 70 | bool onIsEqual(const GrFragmentProcessor& other) const override; |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 71 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 72 | SkRRect fRRect; |
| 73 | GrPrimitiveEdgeType fEdgeType; |
| 74 | uint32_t fCircularCornerFlags; |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 75 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 76 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 77 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 78 | typedef GrFragmentProcessor INHERITED; |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 81 | sk_sp<GrFragmentProcessor> CircularRRectEffect::Make(GrPrimitiveEdgeType edgeType, |
| 82 | uint32_t circularCornerFlags, |
| 83 | const SkRRect& rrect) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 84 | if (kFillAA_GrProcessorEdgeType != edgeType && kInverseFillAA_GrProcessorEdgeType != edgeType) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 85 | return nullptr; |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 86 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 87 | return sk_sp<GrFragmentProcessor>( |
| 88 | new CircularRRectEffect(edgeType, circularCornerFlags, rrect)); |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 89 | } |
| 90 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 91 | CircularRRectEffect::CircularRRectEffect(GrPrimitiveEdgeType edgeType, uint32_t circularCornerFlags, |
| 92 | const SkRRect& rrect) |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 93 | : INHERITED(kCompatibleWithCoverageAsAlpha_OptimizationFlag) |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 94 | , fRRect(rrect) |
| 95 | , fEdgeType(edgeType) |
| 96 | , fCircularCornerFlags(circularCornerFlags) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 97 | this->initClassID<CircularRRectEffect>(); |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Brian Salomon | 1a2a7ab | 2017-07-26 13:11:51 -0400 | [diff] [blame] | 100 | sk_sp<GrFragmentProcessor> CircularRRectEffect::clone() const { |
| 101 | return sk_sp<GrFragmentProcessor>( |
| 102 | new CircularRRectEffect(fEdgeType, fCircularCornerFlags, fRRect)); |
| 103 | } |
| 104 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 105 | bool CircularRRectEffect::onIsEqual(const GrFragmentProcessor& other) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 106 | const CircularRRectEffect& crre = other.cast<CircularRRectEffect>(); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 107 | // The corner flags are derived from fRRect, so no need to check them. |
| 108 | return fEdgeType == crre.fEdgeType && fRRect == crre.fRRect; |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 109 | } |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 110 | |
| 111 | ////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 112 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 113 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(CircularRRectEffect); |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 114 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 115 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 116 | sk_sp<GrFragmentProcessor> CircularRRectEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 117 | SkScalar w = d->fRandom->nextRangeScalar(20.f, 1000.f); |
| 118 | SkScalar h = d->fRandom->nextRangeScalar(20.f, 1000.f); |
| 119 | SkScalar r = d->fRandom->nextRangeF(kRadiusMin, 9.f); |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 120 | SkRRect rrect; |
| 121 | rrect.setRectXY(SkRect::MakeWH(w, h), r, r); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 122 | sk_sp<GrFragmentProcessor> fp; |
commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 123 | do { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 124 | GrPrimitiveEdgeType et = |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 125 | (GrPrimitiveEdgeType)d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 126 | fp = GrRRectEffect::Make(et, rrect); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 127 | } while (nullptr == fp); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 128 | return fp; |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 129 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 130 | #endif |
commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 131 | |
| 132 | ////////////////////////////////////////////////////////////////////////////// |
| 133 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 134 | class GLCircularRRectEffect : public GrGLSLFragmentProcessor { |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 135 | public: |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 136 | GLCircularRRectEffect() { |
| 137 | fPrevRRect.setEmpty(); |
| 138 | } |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 139 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 140 | virtual void emitCode(EmitArgs&) override; |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 141 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 142 | static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*); |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 143 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 144 | protected: |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 145 | void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 146 | |
| 147 | private: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 148 | GrGLSLProgramDataManager::UniformHandle fInnerRectUniform; |
| 149 | GrGLSLProgramDataManager::UniformHandle fRadiusPlusHalfUniform; |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 150 | SkRRect fPrevRRect; |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 151 | typedef GrGLSLFragmentProcessor INHERITED; |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 154 | void GLCircularRRectEffect::emitCode(EmitArgs& args) { |
| 155 | const CircularRRectEffect& crre = args.fFp.cast<CircularRRectEffect>(); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 156 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 157 | const char *rectName; |
| 158 | const char *radiusPlusHalfName; |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 159 | // The inner rect is the rrect bounds inset by the radius. Its left, top, right, and bottom |
| 160 | // edges correspond to components x, y, z, and w, respectively. When a side of the rrect has |
| 161 | // only rectangular corners, that side's value corresponds to the rect edge's value outset by |
| 162 | // half a pixel. |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 163 | fInnerRectUniform = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 164 | kVec4f_GrSLType, kDefault_GrSLPrecision, |
| 165 | "innerRect", |
| 166 | &rectName); |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 167 | // x is (r + .5) and y is 1/(r + .5) |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 168 | fRadiusPlusHalfUniform = uniformHandler->addUniform(kFragment_GrShaderFlag, |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 169 | kVec2f_GrSLType, kDefault_GrSLPrecision, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 170 | "radiusPlusHalf", |
| 171 | &radiusPlusHalfName); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 172 | |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 173 | // If we're on a device with a "real" mediump then the length calculation could overflow. |
| 174 | SkString clampedCircleDistance; |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 175 | if (args.fShaderCaps->floatPrecisionVaries()) { |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 176 | clampedCircleDistance.printf("clamp(%s.x * (1.0 - length(dxy * %s.y)), 0.0, 1.0);", |
| 177 | radiusPlusHalfName, radiusPlusHalfName); |
| 178 | } else { |
| 179 | clampedCircleDistance.printf("clamp(%s.x - length(dxy), 0.0, 1.0);", radiusPlusHalfName); |
| 180 | } |
| 181 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 182 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 183 | // At each quarter-circle corner we compute a vector that is the offset of the fragment position |
| 184 | // from the circle center. The vector is pinned in x and y to be in the quarter-plane relevant |
| 185 | // to that corner. This means that points near the interior near the rrect top edge will have |
| 186 | // a vector that points straight up for both the TL left and TR corners. Computing an |
| 187 | // alpha from this vector at either the TR or TL corner will give the correct result. Similarly, |
| 188 | // fragments near the other three edges will get the correct AA. Fragments in the interior of |
| 189 | // the rrect will have a (0,0) vector at all four corners. So long as the radius > 0.5 they will |
| 190 | // correctly produce an alpha value of 1 at all four corners. We take the min of all the alphas. |
| 191 | // The code below is a simplified version of the above that performs maxs on the vector |
| 192 | // components before computing distances and alpha values so that only one distance computation |
| 193 | // need be computed to determine the min alpha. |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 194 | // |
| 195 | // For the cases where one half of the rrect is rectangular we drop one of the x or y |
| 196 | // computations, compute a separate rect edge alpha for the rect side, and mul the two computed |
| 197 | // alphas together. |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 198 | switch (crre.getCircularCornerFlags()) { |
| 199 | case CircularRRectEffect::kAll_CornerFlags: |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 200 | fragBuilder->codeAppendf("float2 dxy0 = %s.xy - sk_FragCoord.xy;", rectName); |
| 201 | fragBuilder->codeAppendf("float2 dxy1 = sk_FragCoord.xy - %s.zw;", rectName); |
| 202 | fragBuilder->codeAppend("float2 dxy = max(max(dxy0, dxy1), 0.0);"); |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 203 | fragBuilder->codeAppendf("float alpha = %s;", clampedCircleDistance.c_str()); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 204 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 205 | case CircularRRectEffect::kTopLeft_CornerFlag: |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 206 | fragBuilder->codeAppendf("float2 dxy = max(%s.xy - sk_FragCoord.xy, 0.0);", |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 207 | rectName); |
| 208 | fragBuilder->codeAppendf("float rightAlpha = clamp(%s.z - sk_FragCoord.x, 0.0, 1.0);", |
| 209 | rectName); |
| 210 | fragBuilder->codeAppendf("float bottomAlpha = clamp(%s.w - sk_FragCoord.y, 0.0, 1.0);", |
| 211 | rectName); |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 212 | fragBuilder->codeAppendf("float alpha = bottomAlpha * rightAlpha * %s;", |
| 213 | clampedCircleDistance.c_str()); |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 214 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 215 | case CircularRRectEffect::kTopRight_CornerFlag: |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 216 | fragBuilder->codeAppendf("float2 dxy = max(float2(sk_FragCoord.x - %s.z, " |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 217 | "%s.y - sk_FragCoord.y), 0.0);", |
| 218 | rectName, rectName); |
| 219 | fragBuilder->codeAppendf("float leftAlpha = clamp(sk_FragCoord.x - %s.x, 0.0, 1.0);", |
| 220 | rectName); |
| 221 | fragBuilder->codeAppendf("float bottomAlpha = clamp(%s.w - sk_FragCoord.y, 0.0, 1.0);", |
| 222 | rectName); |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 223 | fragBuilder->codeAppendf("float alpha = bottomAlpha * leftAlpha * %s;", |
| 224 | clampedCircleDistance.c_str()); |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 225 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 226 | case CircularRRectEffect::kBottomRight_CornerFlag: |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 227 | fragBuilder->codeAppendf("float2 dxy = max(sk_FragCoord.xy - %s.zw, 0.0);", |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 228 | rectName); |
| 229 | fragBuilder->codeAppendf("float leftAlpha = clamp(sk_FragCoord.x - %s.x, 0.0, 1.0);", |
| 230 | rectName); |
| 231 | fragBuilder->codeAppendf("float topAlpha = clamp(sk_FragCoord.y - %s.y, 0.0, 1.0);", |
| 232 | rectName); |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 233 | fragBuilder->codeAppendf("float alpha = topAlpha * leftAlpha * %s;", |
| 234 | clampedCircleDistance.c_str()); |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 235 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 236 | case CircularRRectEffect::kBottomLeft_CornerFlag: |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 237 | fragBuilder->codeAppendf("float2 dxy = max(float2(%s.x - sk_FragCoord.x, sk_FragCoord.y - " |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 238 | "%s.w), 0.0);", |
| 239 | rectName, rectName); |
| 240 | fragBuilder->codeAppendf("float rightAlpha = clamp(%s.z - sk_FragCoord.x, 0.0, 1.0);", |
| 241 | rectName); |
| 242 | fragBuilder->codeAppendf("float topAlpha = clamp(sk_FragCoord.y - %s.y, 0.0, 1.0);", |
| 243 | rectName); |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 244 | fragBuilder->codeAppendf("float alpha = topAlpha * rightAlpha * %s;", |
| 245 | clampedCircleDistance.c_str()); |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 246 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 247 | case CircularRRectEffect::kLeft_CornerFlags: |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 248 | fragBuilder->codeAppendf("float2 dxy0 = %s.xy - sk_FragCoord.xy;", rectName); |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 249 | fragBuilder->codeAppendf("float dy1 = sk_FragCoord.y - %s.w;", rectName); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 250 | fragBuilder->codeAppend("float2 dxy = max(float2(dxy0.x, max(dxy0.y, dy1)), 0.0);"); |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 251 | fragBuilder->codeAppendf("float rightAlpha = clamp(%s.z - sk_FragCoord.x, 0.0, 1.0);", |
| 252 | rectName); |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 253 | fragBuilder->codeAppendf("float alpha = rightAlpha * %s;", |
| 254 | clampedCircleDistance.c_str()); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 255 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 256 | case CircularRRectEffect::kTop_CornerFlags: |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 257 | fragBuilder->codeAppendf("float2 dxy0 = %s.xy - sk_FragCoord.xy;", rectName); |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 258 | fragBuilder->codeAppendf("float dx1 = sk_FragCoord.x - %s.z;", rectName); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 259 | fragBuilder->codeAppend("float2 dxy = max(float2(max(dxy0.x, dx1), dxy0.y), 0.0);"); |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 260 | fragBuilder->codeAppendf("float bottomAlpha = clamp(%s.w - sk_FragCoord.y, 0.0, 1.0);", |
| 261 | rectName); |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 262 | fragBuilder->codeAppendf("float alpha = bottomAlpha * %s;", |
| 263 | clampedCircleDistance.c_str()); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 264 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 265 | case CircularRRectEffect::kRight_CornerFlags: |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 266 | fragBuilder->codeAppendf("float dy0 = %s.y - sk_FragCoord.y;", rectName); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 267 | fragBuilder->codeAppendf("float2 dxy1 = sk_FragCoord.xy - %s.zw;", rectName); |
| 268 | fragBuilder->codeAppend("float2 dxy = max(float2(dxy1.x, max(dy0, dxy1.y)), 0.0);"); |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 269 | fragBuilder->codeAppendf("float leftAlpha = clamp(sk_FragCoord.x - %s.x, 0.0, 1.0);", |
| 270 | rectName); |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 271 | fragBuilder->codeAppendf("float alpha = leftAlpha * %s;", |
| 272 | clampedCircleDistance.c_str()); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 273 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 274 | case CircularRRectEffect::kBottom_CornerFlags: |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 275 | fragBuilder->codeAppendf("float dx0 = %s.x - sk_FragCoord.x;", rectName); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 276 | fragBuilder->codeAppendf("float2 dxy1 = sk_FragCoord.xy - %s.zw;", rectName); |
| 277 | fragBuilder->codeAppend("float2 dxy = max(float2(max(dx0, dxy1.x), dxy1.y), 0.0);"); |
Ethan Nicholas | 3865711 | 2017-02-09 17:01:22 -0500 | [diff] [blame] | 278 | fragBuilder->codeAppendf("float topAlpha = clamp(sk_FragCoord.y - %s.y, 0.0, 1.0);", |
| 279 | rectName); |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 280 | fragBuilder->codeAppendf("float alpha = topAlpha * %s;", |
| 281 | clampedCircleDistance.c_str()); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 282 | break; |
| 283 | } |
skia.committer@gmail.com | 06acb58 | 2014-03-06 03:02:32 +0000 | [diff] [blame] | 284 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 285 | if (kInverseFillAA_GrProcessorEdgeType == crre.getEdgeType()) { |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 286 | fragBuilder->codeAppend("alpha = 1.0 - alpha;"); |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 287 | } |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 288 | |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 289 | fragBuilder->codeAppendf("%s = %s * alpha;", args.fOutputColor, args.fInputColor); |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 292 | void GLCircularRRectEffect::GenKey(const GrProcessor& processor, const GrShaderCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 293 | GrProcessorKeyBuilder* b) { |
| 294 | const CircularRRectEffect& crre = processor.cast<CircularRRectEffect>(); |
| 295 | GR_STATIC_ASSERT(kGrProcessorEdgeTypeCnt <= 8); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 296 | b->add32((crre.getCircularCornerFlags() << 3) | crre.getEdgeType()); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 297 | } |
| 298 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 299 | void GLCircularRRectEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 300 | const GrFragmentProcessor& processor) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 301 | const CircularRRectEffect& crre = processor.cast<CircularRRectEffect>(); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 302 | const SkRRect& rrect = crre.getRRect(); |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 303 | if (rrect != fPrevRRect) { |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 304 | SkRect rect = rrect.getBounds(); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 305 | SkScalar radius = 0; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 306 | switch (crre.getCircularCornerFlags()) { |
| 307 | case CircularRRectEffect::kAll_CornerFlags: |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 308 | SkASSERT(rrect.isSimpleCircular()); |
| 309 | radius = rrect.getSimpleRadii().fX; |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 310 | SkASSERT(radius >= kRadiusMin); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 311 | rect.inset(radius, radius); |
| 312 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 313 | case CircularRRectEffect::kTopLeft_CornerFlag: |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 314 | radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX; |
| 315 | rect.fLeft += radius; |
| 316 | rect.fTop += radius; |
| 317 | rect.fRight += 0.5f; |
| 318 | rect.fBottom += 0.5f; |
| 319 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 320 | case CircularRRectEffect::kTopRight_CornerFlag: |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 321 | radius = rrect.radii(SkRRect::kUpperRight_Corner).fX; |
bsalomon@google.com | de9f251 | 2014-03-11 17:09:17 +0000 | [diff] [blame] | 322 | rect.fLeft -= 0.5f; |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 323 | rect.fTop += radius; |
| 324 | rect.fRight -= radius; |
| 325 | rect.fBottom += 0.5f; |
| 326 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 327 | case CircularRRectEffect::kBottomRight_CornerFlag: |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 328 | radius = rrect.radii(SkRRect::kLowerRight_Corner).fX; |
bsalomon@google.com | de9f251 | 2014-03-11 17:09:17 +0000 | [diff] [blame] | 329 | rect.fLeft -= 0.5f; |
| 330 | rect.fTop -= 0.5f; |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 331 | rect.fRight -= radius; |
| 332 | rect.fBottom -= radius; |
| 333 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 334 | case CircularRRectEffect::kBottomLeft_CornerFlag: |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 335 | radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX; |
| 336 | rect.fLeft += radius; |
bsalomon@google.com | de9f251 | 2014-03-11 17:09:17 +0000 | [diff] [blame] | 337 | rect.fTop -= 0.5f; |
| 338 | rect.fRight += 0.5f; |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 339 | rect.fBottom -= radius; |
| 340 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 341 | case CircularRRectEffect::kLeft_CornerFlags: |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 342 | radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX; |
| 343 | rect.fLeft += radius; |
| 344 | rect.fTop += radius; |
| 345 | rect.fRight += 0.5f; |
| 346 | rect.fBottom -= radius; |
| 347 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 348 | case CircularRRectEffect::kTop_CornerFlags: |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 349 | radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX; |
| 350 | rect.fLeft += radius; |
| 351 | rect.fTop += radius; |
| 352 | rect.fRight -= radius; |
| 353 | rect.fBottom += 0.5f; |
| 354 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 355 | case CircularRRectEffect::kRight_CornerFlags: |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 356 | radius = rrect.radii(SkRRect::kUpperRight_Corner).fX; |
| 357 | rect.fLeft -= 0.5f; |
| 358 | rect.fTop += radius; |
| 359 | rect.fRight -= radius; |
| 360 | rect.fBottom -= radius; |
| 361 | break; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 362 | case CircularRRectEffect::kBottom_CornerFlags: |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 363 | radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX; |
| 364 | rect.fLeft += radius; |
| 365 | rect.fTop -= 0.5f; |
| 366 | rect.fRight -= radius; |
| 367 | rect.fBottom -= radius; |
| 368 | break; |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 369 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 370 | SkFAIL("Should have been one of the above cases."); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 371 | } |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 372 | pdman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom); |
bsalomon | cd07491 | 2015-12-09 10:33:51 -0800 | [diff] [blame] | 373 | radius += 0.5f; |
| 374 | pdman.set2f(fRadiusPlusHalfUniform, radius, 1.f / radius); |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 375 | fPrevRRect = rrect; |
| 376 | } |
| 377 | } |
| 378 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 379 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 380 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 381 | void CircularRRectEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 382 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 383 | GLCircularRRectEffect::GenKey(*this, caps, b); |
| 384 | } |
| 385 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 386 | GrGLSLFragmentProcessor* CircularRRectEffect::onCreateGLSLInstance() const { |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 387 | return new GLCircularRRectEffect; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | ////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 391 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 392 | class EllipticalRRectEffect : public GrFragmentProcessor { |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 393 | public: |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 394 | static sk_sp<GrFragmentProcessor> Make(GrPrimitiveEdgeType, const SkRRect&); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 395 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 396 | ~EllipticalRRectEffect() override {} |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 397 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 398 | const char* name() const override { return "EllipticalRRect"; } |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 399 | |
Brian Salomon | 1a2a7ab | 2017-07-26 13:11:51 -0400 | [diff] [blame] | 400 | sk_sp<GrFragmentProcessor> clone() const override; |
| 401 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 402 | const SkRRect& getRRect() const { return fRRect; } |
| 403 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 404 | GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; } |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 405 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 406 | private: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 407 | EllipticalRRectEffect(GrPrimitiveEdgeType, const SkRRect&); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 408 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 409 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 410 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 411 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 412 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 413 | bool onIsEqual(const GrFragmentProcessor& other) const override; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 414 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 415 | SkRRect fRRect; |
| 416 | GrPrimitiveEdgeType fEdgeType; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 417 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 418 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 419 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 420 | typedef GrFragmentProcessor INHERITED; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 421 | }; |
| 422 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 423 | sk_sp<GrFragmentProcessor> |
| 424 | EllipticalRRectEffect::Make(GrPrimitiveEdgeType edgeType, const SkRRect& rrect) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 425 | if (kFillAA_GrProcessorEdgeType != edgeType && kInverseFillAA_GrProcessorEdgeType != edgeType) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 426 | return nullptr; |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 427 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 428 | return sk_sp<GrFragmentProcessor>(new EllipticalRRectEffect(edgeType, rrect)); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 429 | } |
| 430 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 431 | EllipticalRRectEffect::EllipticalRRectEffect(GrPrimitiveEdgeType edgeType, const SkRRect& rrect) |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 432 | : INHERITED(kCompatibleWithCoverageAsAlpha_OptimizationFlag) |
| 433 | , fRRect(rrect) |
| 434 | , fEdgeType(edgeType) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 435 | this->initClassID<EllipticalRRectEffect>(); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Brian Salomon | 1a2a7ab | 2017-07-26 13:11:51 -0400 | [diff] [blame] | 438 | sk_sp<GrFragmentProcessor> EllipticalRRectEffect::clone() const { |
| 439 | return sk_sp<GrFragmentProcessor>(new EllipticalRRectEffect(fEdgeType, fRRect)); |
| 440 | } |
| 441 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 442 | bool EllipticalRRectEffect::onIsEqual(const GrFragmentProcessor& other) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 443 | const EllipticalRRectEffect& erre = other.cast<EllipticalRRectEffect>(); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 444 | return fEdgeType == erre.fEdgeType && fRRect == erre.fRRect; |
| 445 | } |
| 446 | |
| 447 | ////////////////////////////////////////////////////////////////////////////// |
| 448 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 449 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(EllipticalRRectEffect); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 450 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 451 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 452 | sk_sp<GrFragmentProcessor> EllipticalRRectEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 453 | SkScalar w = d->fRandom->nextRangeScalar(20.f, 1000.f); |
| 454 | SkScalar h = d->fRandom->nextRangeScalar(20.f, 1000.f); |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 455 | SkVector r[4]; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 456 | 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] | 457 | // ensure at least one corner really is elliptical |
| 458 | do { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 459 | 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] | 460 | } while (r[SkRRect::kUpperLeft_Corner].fY == r[SkRRect::kUpperLeft_Corner].fX); |
| 461 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 462 | SkRRect rrect; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 463 | if (d->fRandom->nextBool()) { |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 464 | // half the time create a four-radii rrect. |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 465 | r[SkRRect::kLowerRight_Corner].fX = d->fRandom->nextRangeF(kRadiusMin, 9.f); |
| 466 | 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] | 467 | |
| 468 | r[SkRRect::kUpperRight_Corner].fX = r[SkRRect::kLowerRight_Corner].fX; |
| 469 | r[SkRRect::kUpperRight_Corner].fY = r[SkRRect::kUpperLeft_Corner].fY; |
| 470 | |
| 471 | r[SkRRect::kLowerLeft_Corner].fX = r[SkRRect::kUpperLeft_Corner].fX; |
| 472 | r[SkRRect::kLowerLeft_Corner].fY = r[SkRRect::kLowerRight_Corner].fY; |
| 473 | |
| 474 | rrect.setRectRadii(SkRect::MakeWH(w, h), r); |
| 475 | } else { |
| 476 | rrect.setRectXY(SkRect::MakeWH(w, h), r[SkRRect::kUpperLeft_Corner].fX, |
| 477 | r[SkRRect::kUpperLeft_Corner].fY); |
| 478 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 479 | sk_sp<GrFragmentProcessor> fp; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 480 | do { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 481 | GrPrimitiveEdgeType et = |
| 482 | (GrPrimitiveEdgeType)d->fRandom->nextULessThan(kGrProcessorEdgeTypeCnt); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 483 | fp = GrRRectEffect::Make(et, rrect); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 484 | } while (nullptr == fp); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 485 | return fp; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 486 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 487 | #endif |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 488 | |
| 489 | ////////////////////////////////////////////////////////////////////////////// |
| 490 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 491 | class GLEllipticalRRectEffect : public GrGLSLFragmentProcessor { |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 492 | public: |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 493 | GLEllipticalRRectEffect() { |
| 494 | fPrevRRect.setEmpty(); |
| 495 | } |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 496 | |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 497 | void emitCode(EmitArgs&) override; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 498 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 499 | static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 500 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 501 | protected: |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 502 | void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 503 | |
| 504 | private: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 505 | GrGLSLProgramDataManager::UniformHandle fInnerRectUniform; |
| 506 | GrGLSLProgramDataManager::UniformHandle fInvRadiiSqdUniform; |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 507 | GrGLSLProgramDataManager::UniformHandle fScaleUniform; |
| 508 | SkRRect fPrevRRect; |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 509 | typedef GrGLSLFragmentProcessor INHERITED; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 510 | }; |
| 511 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 512 | void GLEllipticalRRectEffect::emitCode(EmitArgs& args) { |
| 513 | const EllipticalRRectEffect& erre = args.fFp.cast<EllipticalRRectEffect>(); |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 514 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 515 | const char *rectName; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 516 | // The inner rect is the rrect bounds inset by the x/y radii |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 517 | fInnerRectUniform = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 518 | kVec4f_GrSLType, kDefault_GrSLPrecision, |
| 519 | "innerRect", |
| 520 | &rectName); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 521 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 522 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 523 | // At each quarter-ellipse corner we compute a vector that is the offset of the fragment pos |
| 524 | // to the ellipse center. The vector is pinned in x and y to be in the quarter-plane relevant |
| 525 | // to that corner. This means that points near the interior near the rrect top edge will have |
| 526 | // a vector that points straight up for both the TL left and TR corners. Computing an |
| 527 | // alpha from this vector at either the TR or TL corner will give the correct result. Similarly, |
| 528 | // fragments near the other three edges will get the correct AA. Fragments in the interior of |
| 529 | // the rrect will have a (0,0) vector at all four corners. So long as the radii > 0.5 they will |
| 530 | // 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] | 531 | // |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 532 | // The code below is a simplified version of the above that performs maxs on the vector |
| 533 | // components before computing distances and alpha values so that only one distance computation |
| 534 | // need be computed to determine the min alpha. |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 535 | fragBuilder->codeAppendf("float2 dxy0 = %s.xy - sk_FragCoord.xy;", rectName); |
| 536 | fragBuilder->codeAppendf("float2 dxy1 = sk_FragCoord.xy - %s.zw;", rectName); |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 537 | |
| 538 | // If we're on a device with a "real" mediump then we'll do the distance computation in a space |
| 539 | // that is normalized by the largest radius. The scale uniform will be scale, 1/scale. The |
| 540 | // radii uniform values are already in this normalized space. |
| 541 | const char* scaleName = nullptr; |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 542 | if (args.fShaderCaps->floatPrecisionVaries()) { |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 543 | fScaleUniform = uniformHandler->addUniform(kFragment_GrShaderFlag, |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 544 | kVec2f_GrSLType, kDefault_GrSLPrecision, |
| 545 | "scale", &scaleName); |
| 546 | } |
| 547 | |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 548 | // 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] | 549 | switch (erre.getRRect().getType()) { |
| 550 | case SkRRect::kSimple_Type: { |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 551 | const char *invRadiiXYSqdName; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 552 | fInvRadiiSqdUniform = uniformHandler->addUniform(kFragment_GrShaderFlag, |
| 553 | kVec2f_GrSLType, |
| 554 | kDefault_GrSLPrecision, |
| 555 | "invRadiiXY", |
| 556 | &invRadiiXYSqdName); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 557 | fragBuilder->codeAppend("float2 dxy = max(max(dxy0, dxy1), 0.0);"); |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 558 | if (scaleName) { |
| 559 | fragBuilder->codeAppendf("dxy *= %s.y;", scaleName); |
| 560 | } |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 561 | // Z is the x/y offsets divided by squared radii. |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 562 | fragBuilder->codeAppendf("float2 Z = dxy * %s.xy;", invRadiiXYSqdName); |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 563 | break; |
| 564 | } |
commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 565 | case SkRRect::kNinePatch_Type: { |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 566 | const char *invRadiiLTRBSqdName; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 567 | fInvRadiiSqdUniform = uniformHandler->addUniform(kFragment_GrShaderFlag, |
| 568 | kVec4f_GrSLType, |
| 569 | kDefault_GrSLPrecision, |
| 570 | "invRadiiLTRB", |
| 571 | &invRadiiLTRBSqdName); |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 572 | if (scaleName) { |
| 573 | fragBuilder->codeAppendf("dxy0 *= %s.y;", scaleName); |
| 574 | fragBuilder->codeAppendf("dxy1 *= %s.y;", scaleName); |
| 575 | } |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 576 | fragBuilder->codeAppend("float2 dxy = max(max(dxy0, dxy1), 0.0);"); |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 577 | // Z is the x/y offsets divided by squared radii. We only care about the (at most) one |
| 578 | // corner where both the x and y offsets are positive, hence the maxes. (The inverse |
| 579 | // squared radii will always be positive.) |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 580 | 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] | 581 | invRadiiLTRBSqdName, invRadiiLTRBSqdName); |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 582 | |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 583 | break; |
commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 584 | } |
| 585 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 586 | SkFAIL("RRect should always be simple or nine-patch."); |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 587 | } |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 588 | // implicit is the evaluation of (x/a)^2 + (y/b)^2 - 1. |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 589 | fragBuilder->codeAppend("float implicit = dot(Z, dxy) - 1.0;"); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 590 | // grad_dot is the squared length of the gradient of the implicit. |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 591 | fragBuilder->codeAppend("float grad_dot = 4.0 * dot(Z, Z);"); |
commit-bot@chromium.org | 1b035d8 | 2014-04-09 17:11:09 +0000 | [diff] [blame] | 592 | // avoid calling inversesqrt on zero. |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 593 | fragBuilder->codeAppend("grad_dot = max(grad_dot, 1.0e-4);"); |
| 594 | fragBuilder->codeAppend("float approx_dist = implicit * inversesqrt(grad_dot);"); |
| 595 | if (scaleName) { |
| 596 | fragBuilder->codeAppendf("approx_dist *= %s.x;", scaleName); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 597 | } |
| 598 | |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 599 | if (kFillAA_GrProcessorEdgeType == erre.getEdgeType()) { |
| 600 | fragBuilder->codeAppend("float alpha = clamp(0.5 - approx_dist, 0.0, 1.0);"); |
| 601 | } else { |
| 602 | fragBuilder->codeAppend("float alpha = clamp(0.5 + approx_dist, 0.0, 1.0);"); |
| 603 | } |
| 604 | |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 605 | fragBuilder->codeAppendf("%s = %s * alpha;", args.fOutputColor, args.fInputColor); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 608 | void GLEllipticalRRectEffect::GenKey(const GrProcessor& effect, const GrShaderCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 609 | GrProcessorKeyBuilder* b) { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 610 | const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>(); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 611 | GR_STATIC_ASSERT(kLast_GrProcessorEdgeType < (1 << 3)); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 612 | b->add32(erre.getRRect().getType() | erre.getEdgeType() << 3); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 613 | } |
| 614 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 615 | void GLEllipticalRRectEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 616 | const GrFragmentProcessor& effect) { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 617 | const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>(); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 618 | const SkRRect& rrect = erre.getRRect(); |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 619 | // If we're using a scale factor to work around precision issues, choose the largest radius |
| 620 | // 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] | 621 | if (rrect != fPrevRRect) { |
| 622 | SkRect rect = rrect.getBounds(); |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 623 | const SkVector& r0 = rrect.radii(SkRRect::kUpperLeft_Corner); |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 624 | SkASSERT(r0.fX >= kRadiusMin); |
| 625 | SkASSERT(r0.fY >= kRadiusMin); |
commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 626 | switch (erre.getRRect().getType()) { |
| 627 | case SkRRect::kSimple_Type: |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 628 | rect.inset(r0.fX, r0.fY); |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 629 | if (fScaleUniform.isValid()) { |
| 630 | if (r0.fX > r0.fY) { |
| 631 | pdman.set2f(fInvRadiiSqdUniform, 1.f, (r0.fX * r0.fX) / (r0.fY * r0.fY)); |
| 632 | pdman.set2f(fScaleUniform, r0.fX, 1.f / r0.fX); |
| 633 | } else { |
| 634 | pdman.set2f(fInvRadiiSqdUniform, (r0.fY * r0.fY) / (r0.fX * r0.fX), 1.f); |
| 635 | pdman.set2f(fScaleUniform, r0.fY, 1.f / r0.fY); |
| 636 | } |
| 637 | } else { |
| 638 | pdman.set2f(fInvRadiiSqdUniform, 1.f / (r0.fX * r0.fX), |
| 639 | 1.f / (r0.fY * r0.fY)); |
| 640 | } |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 641 | break; |
commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 642 | case SkRRect::kNinePatch_Type: { |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 643 | const SkVector& r1 = rrect.radii(SkRRect::kLowerRight_Corner); |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 644 | SkASSERT(r1.fX >= kRadiusMin); |
| 645 | SkASSERT(r1.fY >= kRadiusMin); |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 646 | rect.fLeft += r0.fX; |
| 647 | rect.fTop += r0.fY; |
| 648 | rect.fRight -= r1.fX; |
| 649 | rect.fBottom -= r1.fY; |
bsalomon | e87256c4 | 2015-12-09 17:14:40 -0800 | [diff] [blame] | 650 | if (fScaleUniform.isValid()) { |
| 651 | float scale = SkTMax(SkTMax(r0.fX, r0.fY), SkTMax(r1.fX, r1.fY)); |
| 652 | float scaleSqd = scale * scale; |
| 653 | pdman.set4f(fInvRadiiSqdUniform, scaleSqd / (r0.fX * r0.fX), |
| 654 | scaleSqd / (r0.fY * r0.fY), |
| 655 | scaleSqd / (r1.fX * r1.fX), |
| 656 | scaleSqd / (r1.fY * r1.fY)); |
| 657 | pdman.set2f(fScaleUniform, scale, 1.f / scale); |
| 658 | } else { |
| 659 | pdman.set4f(fInvRadiiSqdUniform, 1.f / (r0.fX * r0.fX), |
| 660 | 1.f / (r0.fY * r0.fY), |
| 661 | 1.f / (r1.fX * r1.fX), |
| 662 | 1.f / (r1.fY * r1.fY)); |
| 663 | } |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 664 | break; |
| 665 | } |
commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 666 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 667 | SkFAIL("RRect should always be simple or nine-patch."); |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 668 | } |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 669 | 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] | 670 | fPrevRRect = rrect; |
| 671 | } |
| 672 | } |
| 673 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 674 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 675 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 676 | void EllipticalRRectEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 677 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 678 | GLEllipticalRRectEffect::GenKey(*this, caps, b); |
| 679 | } |
| 680 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 681 | GrGLSLFragmentProcessor* EllipticalRRectEffect::onCreateGLSLInstance() const { |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 682 | return new GLEllipticalRRectEffect; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 683 | } |
| 684 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 685 | ////////////////////////////////////////////////////////////////////////////// |
| 686 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 687 | sk_sp<GrFragmentProcessor> GrRRectEffect::Make(GrPrimitiveEdgeType edgeType, const SkRRect& rrect) { |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 688 | if (rrect.isRect()) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 689 | return GrConvexPolyEffect::Make(edgeType, rrect.getBounds()); |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 690 | } |
| 691 | |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 692 | if (rrect.isOval()) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 693 | return GrOvalEffect::Make(edgeType, rrect.getBounds()); |
commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 694 | } |
| 695 | |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 696 | if (rrect.isSimple()) { |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 697 | if (rrect.getSimpleRadii().fX < kRadiusMin || rrect.getSimpleRadii().fY < kRadiusMin) { |
| 698 | // In this case the corners are extremely close to rectangular and we collapse the |
| 699 | // clip to a rectangular clip. |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 700 | return GrConvexPolyEffect::Make(edgeType, rrect.getBounds()); |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 701 | } |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 702 | if (rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 703 | return CircularRRectEffect::Make(edgeType, CircularRRectEffect::kAll_CornerFlags, |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 704 | rrect); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 705 | } else { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 706 | return EllipticalRRectEffect::Make(edgeType, rrect); |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 707 | } |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | if (rrect.isComplex() || rrect.isNinePatch()) { |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 711 | // 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] | 712 | SkScalar circularRadius = 0; |
| 713 | uint32_t cornerFlags = 0; |
| 714 | |
| 715 | SkVector radii[4]; |
| 716 | bool squashedRadii = false; |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 717 | for (int c = 0; c < 4; ++c) { |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 718 | radii[c] = rrect.radii((SkRRect::Corner)c); |
| 719 | SkASSERT((0 == radii[c].fX) == (0 == radii[c].fY)); |
| 720 | if (0 == radii[c].fX) { |
| 721 | // 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] | 722 | continue; |
| 723 | } |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 724 | if (radii[c].fX < kRadiusMin || radii[c].fY < kRadiusMin) { |
| 725 | radii[c].set(0, 0); |
| 726 | squashedRadii = true; |
| 727 | continue; |
| 728 | } |
| 729 | if (radii[c].fX != radii[c].fY) { |
bsalomon@google.com | 44a435b | 2014-03-13 19:20:32 +0000 | [diff] [blame] | 730 | cornerFlags = ~0U; |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 731 | break; |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 732 | } |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 733 | if (!cornerFlags) { |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 734 | circularRadius = radii[c].fX; |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 735 | cornerFlags = 1 << c; |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 736 | } else { |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 737 | if (radii[c].fX != circularRadius) { |
bsalomon@google.com | 44a435b | 2014-03-13 19:20:32 +0000 | [diff] [blame] | 738 | cornerFlags = ~0U; |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 739 | break; |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 740 | } |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 741 | cornerFlags |= 1 << c; |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | |
commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 745 | switch (cornerFlags) { |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 746 | case CircularRRectEffect::kAll_CornerFlags: |
| 747 | // This rrect should have been caught in the simple case above. Though, it would |
| 748 | // be correctly handled in the fallthrough code. |
| 749 | SkASSERT(false); |
commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 750 | case CircularRRectEffect::kTopLeft_CornerFlag: |
| 751 | case CircularRRectEffect::kTopRight_CornerFlag: |
| 752 | case CircularRRectEffect::kBottomRight_CornerFlag: |
| 753 | case CircularRRectEffect::kBottomLeft_CornerFlag: |
| 754 | case CircularRRectEffect::kLeft_CornerFlags: |
| 755 | case CircularRRectEffect::kTop_CornerFlags: |
| 756 | case CircularRRectEffect::kRight_CornerFlags: |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 757 | case CircularRRectEffect::kBottom_CornerFlags: { |
| 758 | SkTCopyOnFirstWrite<SkRRect> rr(rrect); |
| 759 | if (squashedRadii) { |
| 760 | rr.writable()->setRectRadii(rrect.getBounds(), radii); |
| 761 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 762 | return CircularRRectEffect::Make(edgeType, cornerFlags, *rr); |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 763 | } |
| 764 | case CircularRRectEffect::kNone_CornerFlags: |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 765 | return GrConvexPolyEffect::Make(edgeType, rrect.getBounds()); |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 766 | default: { |
| 767 | if (squashedRadii) { |
| 768 | // If we got here then we squashed some but not all the radii to zero. (If all |
| 769 | // had been squashed cornerFlags would be 0.) The elliptical effect doesn't |
| 770 | // support some rounded and some square corners. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 771 | return nullptr; |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 772 | } |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 773 | if (rrect.isNinePatch()) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 774 | return EllipticalRRectEffect::Make(edgeType, rrect); |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 775 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 776 | return nullptr; |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 777 | } |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 778 | } |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 779 | } |
commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 780 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 781 | return nullptr; |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 782 | } |