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