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