Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | /************************************************************************************************** |
| 9 | *** This file was autogenerated from GrTwoPointConicalGradientLayout.fp; do not modify. |
| 10 | **************************************************************************************************/ |
| 11 | #include "GrTwoPointConicalGradientLayout.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 13 | #include "src/gpu/GrTexture.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 15 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 16 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" |
| 17 | #include "src/sksl/SkSLCPP.h" |
| 18 | #include "src/sksl/SkSLUtil.h" |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 19 | class GrGLSLTwoPointConicalGradientLayout : public GrGLSLFragmentProcessor { |
| 20 | public: |
| 21 | GrGLSLTwoPointConicalGradientLayout() {} |
| 22 | void emitCode(EmitArgs& args) override { |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 23 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 24 | const GrTwoPointConicalGradientLayout& _outer = |
| 25 | args.fFp.cast<GrTwoPointConicalGradientLayout>(); |
| 26 | (void)_outer; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 27 | auto type = _outer.type; |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 28 | (void)type; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 29 | auto isRadiusIncreasing = _outer.isRadiusIncreasing; |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 30 | (void)isRadiusIncreasing; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 31 | auto isFocalOnCircle = _outer.isFocalOnCircle; |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 32 | (void)isFocalOnCircle; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 33 | auto isWellBehaved = _outer.isWellBehaved; |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 34 | (void)isWellBehaved; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 35 | auto isSwapped = _outer.isSwapped; |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 36 | (void)isSwapped; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 37 | auto isNativelyFocal = _outer.isNativelyFocal; |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 38 | (void)isNativelyFocal; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 39 | auto focalParams = _outer.focalParams; |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 40 | (void)focalParams; |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 41 | focalParamsVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, |
| 42 | kHalf2_GrSLType, "focalParams"); |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 43 | fragBuilder->codeAppendf( |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 44 | R"SkSL(float t = -1.0; |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 45 | half v = 1.0; |
| 46 | @switch (%d) { |
| 47 | case 1: |
| 48 | { |
| 49 | half r0_2 = %s.y; |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 50 | t = float(r0_2) - %s.y * %s.y; |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 51 | if (t >= 0.0) { |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 52 | t = %s.x + sqrt(t); |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 53 | } else { |
| 54 | v = -1.0; |
| 55 | } |
| 56 | } |
| 57 | break; |
| 58 | case 0: |
| 59 | { |
| 60 | half r0 = %s.x; |
| 61 | @if (%s) { |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 62 | t = length(%s) - float(r0); |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 63 | } else { |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 64 | t = -length(%s) - float(r0); |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | break; |
| 68 | case 2: |
| 69 | { |
| 70 | half invR1 = %s.x; |
| 71 | half fx = %s.y; |
| 72 | float x_t = -1.0; |
| 73 | @if (%s) { |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 74 | x_t = dot(%s, %s) / %s.x; |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 75 | } else if (%s) { |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 76 | x_t = length(%s) - %s.x * float(invR1); |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 77 | } else { |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 78 | float temp = %s.x * %s.x - %s.y * %s.y; |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 79 | if (temp >= 0.0) { |
| 80 | @if (%s || !%s) { |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 81 | x_t = -sqrt(temp) - %s.x * float(invR1); |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 82 | } else { |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 83 | x_t = sqrt(temp) - %s.x * float(invR1); |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | } |
| 87 | @if (!%s) { |
| 88 | if (x_t <= 0.0) { |
| 89 | v = -1.0; |
| 90 | } |
| 91 | } |
| 92 | @if (%s) { |
| 93 | @if (%s) { |
| 94 | t = x_t; |
| 95 | } else { |
| 96 | t = x_t + float(fx); |
| 97 | } |
| 98 | } else { |
| 99 | @if (%s) { |
| 100 | t = -x_t; |
| 101 | } else { |
| 102 | t = -x_t + float(fx); |
| 103 | } |
| 104 | } |
| 105 | @if (%s) { |
| 106 | t = 1.0 - t; |
| 107 | } |
| 108 | } |
| 109 | break; |
| 110 | } |
| 111 | %s = half4(half(t), v, 0.0, 0.0); |
| 112 | )SkSL", |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 113 | (int)_outer.type, args.fUniformHandler->getUniformCStr(focalParamsVar), |
| 114 | args.fSampleCoord, args.fSampleCoord, args.fSampleCoord, |
Brian Salomon | bf5c0c0 | 2019-11-11 14:55:28 -0500 | [diff] [blame] | 115 | args.fUniformHandler->getUniformCStr(focalParamsVar), |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 116 | (_outer.isRadiusIncreasing ? "true" : "false"), args.fSampleCoord, |
| 117 | args.fSampleCoord, args.fUniformHandler->getUniformCStr(focalParamsVar), |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 118 | args.fUniformHandler->getUniformCStr(focalParamsVar), |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 119 | (_outer.isFocalOnCircle ? "true" : "false"), args.fSampleCoord, args.fSampleCoord, |
| 120 | args.fSampleCoord, (_outer.isWellBehaved ? "true" : "false"), args.fSampleCoord, |
| 121 | args.fSampleCoord, args.fSampleCoord, args.fSampleCoord, args.fSampleCoord, |
| 122 | args.fSampleCoord, (_outer.isSwapped ? "true" : "false"), |
| 123 | (_outer.isRadiusIncreasing ? "true" : "false"), args.fSampleCoord, |
| 124 | args.fSampleCoord, (_outer.isWellBehaved ? "true" : "false"), |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 125 | (_outer.isRadiusIncreasing ? "true" : "false"), |
| 126 | (_outer.isNativelyFocal ? "true" : "false"), |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 127 | (_outer.isNativelyFocal ? "true" : "false"), (_outer.isSwapped ? "true" : "false"), |
| 128 | args.fOutputColor); |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | private: |
| 132 | void onSetData(const GrGLSLProgramDataManager& pdman, |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 133 | const GrFragmentProcessor& _proc) override { |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 134 | const GrTwoPointConicalGradientLayout& _outer = |
| 135 | _proc.cast<GrTwoPointConicalGradientLayout>(); |
| 136 | { |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 137 | const SkPoint& focalParamsValue = _outer.focalParams; |
| 138 | if (focalParamsPrev != focalParamsValue) { |
| 139 | focalParamsPrev = focalParamsValue; |
| 140 | pdman.set2f(focalParamsVar, focalParamsValue.fX, focalParamsValue.fY); |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | } |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 144 | SkPoint focalParamsPrev = SkPoint::Make(SK_FloatNaN, SK_FloatNaN); |
| 145 | UniformHandle focalParamsVar; |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 146 | }; |
| 147 | GrGLSLFragmentProcessor* GrTwoPointConicalGradientLayout::onCreateGLSLInstance() const { |
| 148 | return new GrGLSLTwoPointConicalGradientLayout(); |
| 149 | } |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 150 | void GrTwoPointConicalGradientLayout::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 151 | GrProcessorKeyBuilder* b) const { |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 152 | b->add32((int32_t)type); |
| 153 | b->add32((int32_t)isRadiusIncreasing); |
| 154 | b->add32((int32_t)isFocalOnCircle); |
| 155 | b->add32((int32_t)isWellBehaved); |
| 156 | b->add32((int32_t)isSwapped); |
| 157 | b->add32((int32_t)isNativelyFocal); |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 158 | } |
| 159 | bool GrTwoPointConicalGradientLayout::onIsEqual(const GrFragmentProcessor& other) const { |
| 160 | const GrTwoPointConicalGradientLayout& that = other.cast<GrTwoPointConicalGradientLayout>(); |
| 161 | (void)that; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 162 | if (type != that.type) return false; |
| 163 | if (isRadiusIncreasing != that.isRadiusIncreasing) return false; |
| 164 | if (isFocalOnCircle != that.isFocalOnCircle) return false; |
| 165 | if (isWellBehaved != that.isWellBehaved) return false; |
| 166 | if (isSwapped != that.isSwapped) return false; |
| 167 | if (isNativelyFocal != that.isNativelyFocal) return false; |
| 168 | if (focalParams != that.focalParams) return false; |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 169 | return true; |
| 170 | } |
| 171 | GrTwoPointConicalGradientLayout::GrTwoPointConicalGradientLayout( |
| 172 | const GrTwoPointConicalGradientLayout& src) |
| 173 | : INHERITED(kGrTwoPointConicalGradientLayout_ClassID, src.optimizationFlags()) |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 174 | , type(src.type) |
| 175 | , isRadiusIncreasing(src.isRadiusIncreasing) |
| 176 | , isFocalOnCircle(src.isFocalOnCircle) |
| 177 | , isWellBehaved(src.isWellBehaved) |
| 178 | , isSwapped(src.isSwapped) |
| 179 | , isNativelyFocal(src.isNativelyFocal) |
| 180 | , focalParams(src.focalParams) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame^] | 181 | this->cloneAndRegisterAllChildProcessors(src); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 182 | this->setUsesSampleCoordsDirectly(); |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 183 | } |
| 184 | std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::clone() const { |
| 185 | return std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(*this)); |
| 186 | } |
Michael Ludwig | 7f8c524 | 2018-09-14 15:07:55 -0400 | [diff] [blame] | 187 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTwoPointConicalGradientLayout); |
| 188 | #if GR_TEST_UTILS |
| 189 | std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::TestCreate( |
| 190 | GrProcessorTestData* d) { |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 191 | SkScalar scale = GrGradientShader::RandomParams::kGradientScale; |
Michael Ludwig | 083bc15 | 2018-10-01 17:15:15 -0400 | [diff] [blame] | 192 | SkScalar offset = scale / 32.0f; |
| 193 | |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 194 | SkPoint center1 = {d->fRandom->nextRangeScalar(0.0f, scale), |
Michael Ludwig | 083bc15 | 2018-10-01 17:15:15 -0400 | [diff] [blame] | 195 | d->fRandom->nextRangeScalar(0.0f, scale)}; |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 196 | SkPoint center2 = {d->fRandom->nextRangeScalar(0.0f, scale), |
Michael Ludwig | 083bc15 | 2018-10-01 17:15:15 -0400 | [diff] [blame] | 197 | d->fRandom->nextRangeScalar(0.0f, scale)}; |
| 198 | SkScalar radius1 = d->fRandom->nextRangeScalar(0.0f, scale); |
| 199 | SkScalar radius2 = d->fRandom->nextRangeScalar(0.0f, scale); |
Michael Ludwig | 7f8c524 | 2018-09-14 15:07:55 -0400 | [diff] [blame] | 200 | |
| 201 | constexpr int kTestTypeMask = (1 << 2) - 1, kTestNativelyFocalBit = (1 << 2), |
| 202 | kTestFocalOnCircleBit = (1 << 3), kTestSwappedBit = (1 << 4); |
| 203 | // We won't treat isWellDefined and isRadiusIncreasing specially because they |
| 204 | // should have high probability to be turned on and off as we're getting random |
| 205 | // radii and centers. |
| 206 | |
| 207 | int mask = d->fRandom->nextU(); |
| 208 | int type = mask & kTestTypeMask; |
| 209 | if (type == static_cast<int>(Type::kRadial)) { |
| 210 | center2 = center1; |
| 211 | // Make sure that the radii are different |
| 212 | if (SkScalarNearlyZero(radius1 - radius2)) { |
Michael Ludwig | 083bc15 | 2018-10-01 17:15:15 -0400 | [diff] [blame] | 213 | radius2 += offset; |
Michael Ludwig | 7f8c524 | 2018-09-14 15:07:55 -0400 | [diff] [blame] | 214 | } |
| 215 | } else if (type == static_cast<int>(Type::kStrip)) { |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 216 | radius1 = std::max(radius1, .1f); // Make sure that the radius is non-zero |
Michael Ludwig | 7f8c524 | 2018-09-14 15:07:55 -0400 | [diff] [blame] | 217 | radius2 = radius1; |
| 218 | // Make sure that the centers are different |
| 219 | if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) { |
Michael Ludwig | 083bc15 | 2018-10-01 17:15:15 -0400 | [diff] [blame] | 220 | center2.fX += offset; |
Michael Ludwig | 7f8c524 | 2018-09-14 15:07:55 -0400 | [diff] [blame] | 221 | } |
| 222 | } else { // kFocal_Type |
| 223 | // Make sure that the centers are different |
| 224 | if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) { |
Michael Ludwig | 083bc15 | 2018-10-01 17:15:15 -0400 | [diff] [blame] | 225 | center2.fX += offset; |
Michael Ludwig | 7f8c524 | 2018-09-14 15:07:55 -0400 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | if (kTestNativelyFocalBit & mask) { |
| 229 | radius1 = 0; |
| 230 | } |
| 231 | if (kTestFocalOnCircleBit & mask) { |
| 232 | radius2 = radius1 + SkPoint::Distance(center1, center2); |
| 233 | } |
| 234 | if (kTestSwappedBit & mask) { |
| 235 | std::swap(radius1, radius2); |
| 236 | radius2 = 0; |
| 237 | } |
| 238 | |
| 239 | // Make sure that the radii are different |
| 240 | if (SkScalarNearlyZero(radius1 - radius2)) { |
Michael Ludwig | 083bc15 | 2018-10-01 17:15:15 -0400 | [diff] [blame] | 241 | radius2 += offset; |
Michael Ludwig | 7f8c524 | 2018-09-14 15:07:55 -0400 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
| 245 | if (SkScalarNearlyZero(radius1 - radius2) && |
| 246 | SkScalarNearlyZero(SkPoint::Distance(center1, center2))) { |
Michael Ludwig | 083bc15 | 2018-10-01 17:15:15 -0400 | [diff] [blame] | 247 | radius2 += offset; // make sure that we're not degenerated |
Michael Ludwig | 7f8c524 | 2018-09-14 15:07:55 -0400 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | GrGradientShader::RandomParams params(d->fRandom); |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 251 | auto shader = params.fUseColors4f |
| 252 | ? SkGradientShader::MakeTwoPointConical( |
| 253 | center1, radius1, center2, radius2, params.fColors4f, |
| 254 | params.fColorSpace, params.fStops, params.fColorCount, |
| 255 | params.fTileMode) |
| 256 | : SkGradientShader::MakeTwoPointConical( |
| 257 | center1, radius1, center2, radius2, params.fColors, |
| 258 | params.fStops, params.fColorCount, params.fTileMode); |
| 259 | GrTest::TestAsFPArgs asFPArgs(d); |
Michael Ludwig | 7f8c524 | 2018-09-14 15:07:55 -0400 | [diff] [blame] | 260 | std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args()); |
| 261 | |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 262 | SkASSERT_RELEASE(fp); |
Michael Ludwig | 7f8c524 | 2018-09-14 15:07:55 -0400 | [diff] [blame] | 263 | return fp; |
| 264 | } |
| 265 | #endif |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 266 | |
| 267 | // .fp files do not let you reference outside enum definitions, so we have to explicitly map |
| 268 | // between the two compatible enum defs |
| 269 | GrTwoPointConicalGradientLayout::Type convert_type(SkTwoPointConicalGradient::Type type) { |
| 270 | switch (type) { |
| 271 | case SkTwoPointConicalGradient::Type::kRadial: |
| 272 | return GrTwoPointConicalGradientLayout::Type::kRadial; |
| 273 | case SkTwoPointConicalGradient::Type::kStrip: |
| 274 | return GrTwoPointConicalGradientLayout::Type::kStrip; |
| 275 | case SkTwoPointConicalGradient::Type::kFocal: |
| 276 | return GrTwoPointConicalGradientLayout::Type::kFocal; |
| 277 | } |
| 278 | SkDEBUGFAIL("Should not be reachable"); |
| 279 | return GrTwoPointConicalGradientLayout::Type::kRadial; |
| 280 | } |
| 281 | |
| 282 | std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::Make( |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 283 | const SkTwoPointConicalGradient& grad, const GrFPArgs& args) { |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 284 | GrTwoPointConicalGradientLayout::Type grType = convert_type(grad.getType()); |
| 285 | |
| 286 | // The focalData struct is only valid if isFocal is true |
| 287 | const SkTwoPointConicalGradient::FocalData& focalData = grad.getFocalData(); |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 288 | bool isFocal = grType == Type::kFocal; |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 289 | |
| 290 | // Calculate optimization switches from gradient specification |
| 291 | bool isFocalOnCircle = isFocal && focalData.isFocalOnCircle(); |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 292 | bool isWellBehaved = isFocal && focalData.isWellBehaved(); |
| 293 | bool isSwapped = isFocal && focalData.isSwapped(); |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 294 | bool isNativelyFocal = isFocal && focalData.isNativelyFocal(); |
| 295 | |
| 296 | // Type-specific calculations: isRadiusIncreasing, focalParams, and the gradient matrix. |
| 297 | // However, all types start with the total inverse local matrix calculated from the shader |
| 298 | // and args |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 299 | bool isRadiusIncreasing; |
| 300 | SkPoint focalParams; // really just a 2D tuple |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 301 | SkMatrix matrix; |
| 302 | |
| 303 | // Initialize the base matrix |
Florin Malita | 52f0291 | 2020-03-09 16:33:17 -0400 | [diff] [blame] | 304 | if (!grad.totalLocalMatrix(args.fPreLocalMatrix)->invert(&matrix)) { |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 305 | return nullptr; |
| 306 | } |
| 307 | |
| 308 | if (isFocal) { |
| 309 | isRadiusIncreasing = (1 - focalData.fFocalX) > 0; |
| 310 | |
| 311 | focalParams.set(1.0 / focalData.fR1, focalData.fFocalX); |
| 312 | |
| 313 | matrix.postConcat(grad.getGradientMatrix()); |
| 314 | } else if (grType == Type::kRadial) { |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 315 | SkScalar dr = grad.getDiffRadius(); |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 316 | isRadiusIncreasing = dr >= 0; |
| 317 | |
| 318 | SkScalar r0 = grad.getStartRadius() / dr; |
| 319 | focalParams.set(r0, r0 * r0); |
| 320 | |
| 321 | // GPU radial matrix is different from the original matrix, since we map the diff radius |
| 322 | // to have |dr| = 1, so manually compute the final gradient matrix here. |
| 323 | |
| 324 | // Map center to (0, 0) |
| 325 | matrix.postTranslate(-grad.getStartCenter().fX, -grad.getStartCenter().fY); |
| 326 | |
| 327 | // scale |diffRadius| to 1 |
| 328 | matrix.postScale(1 / dr, 1 / dr); |
| 329 | } else { // kStrip |
| 330 | isRadiusIncreasing = false; // kStrip doesn't use this flag |
| 331 | |
| 332 | SkScalar r0 = grad.getStartRadius() / grad.getCenterX1(); |
| 333 | focalParams.set(r0, r0 * r0); |
| 334 | |
| 335 | matrix.postConcat(grad.getGradientMatrix()); |
| 336 | } |
| 337 | |
Michael Ludwig | 1b7399b | 2020-06-26 09:56:48 -0400 | [diff] [blame] | 338 | return GrMatrixEffect::Make( |
| 339 | matrix, std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout( |
| 340 | grType, isRadiusIncreasing, isFocalOnCircle, isWellBehaved, isSwapped, |
| 341 | isNativelyFocal, focalParams))); |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 342 | } |