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; |
John Stiles | 9b660ef | 2021-03-04 15:45:00 -0500 | [diff] [blame] | 42 | focalParamsVar = args.fUniformHandler->addUniform( |
| 43 | &_outer, kFragment_GrShaderFlag, 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 | } |
John Stiles | 118706a | 2020-10-09 10:07:38 -0400 | [diff] [blame] | 112 | return half4(half(t), v, 0.0, 0.0); |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 113 | )SkSL", |
John Stiles | 9b660ef | 2021-03-04 15:45:00 -0500 | [diff] [blame] | 114 | (int)_outer.type, |
Brian Salomon | bf5c0c0 | 2019-11-11 14:55:28 -0500 | [diff] [blame] | 115 | args.fUniformHandler->getUniformCStr(focalParamsVar), |
John Stiles | 9b660ef | 2021-03-04 15:45:00 -0500 | [diff] [blame] | 116 | args.fSampleCoord, |
| 117 | args.fSampleCoord, |
| 118 | args.fSampleCoord, |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 119 | args.fUniformHandler->getUniformCStr(focalParamsVar), |
John Stiles | 9b660ef | 2021-03-04 15:45:00 -0500 | [diff] [blame] | 120 | (_outer.isRadiusIncreasing ? "true" : "false"), |
| 121 | args.fSampleCoord, |
| 122 | args.fSampleCoord, |
| 123 | args.fUniformHandler->getUniformCStr(focalParamsVar), |
| 124 | args.fUniformHandler->getUniformCStr(focalParamsVar), |
| 125 | (_outer.isFocalOnCircle ? "true" : "false"), |
| 126 | args.fSampleCoord, |
| 127 | args.fSampleCoord, |
| 128 | args.fSampleCoord, |
| 129 | (_outer.isWellBehaved ? "true" : "false"), |
| 130 | args.fSampleCoord, |
| 131 | args.fSampleCoord, |
| 132 | args.fSampleCoord, |
| 133 | args.fSampleCoord, |
| 134 | args.fSampleCoord, |
| 135 | args.fSampleCoord, |
| 136 | (_outer.isSwapped ? "true" : "false"), |
| 137 | (_outer.isRadiusIncreasing ? "true" : "false"), |
| 138 | args.fSampleCoord, |
| 139 | args.fSampleCoord, |
| 140 | (_outer.isWellBehaved ? "true" : "false"), |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 141 | (_outer.isRadiusIncreasing ? "true" : "false"), |
| 142 | (_outer.isNativelyFocal ? "true" : "false"), |
John Stiles | 9b660ef | 2021-03-04 15:45:00 -0500 | [diff] [blame] | 143 | (_outer.isNativelyFocal ? "true" : "false"), |
| 144 | (_outer.isSwapped ? "true" : "false")); |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | private: |
| 148 | void onSetData(const GrGLSLProgramDataManager& pdman, |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 149 | const GrFragmentProcessor& _proc) override { |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 150 | const GrTwoPointConicalGradientLayout& _outer = |
| 151 | _proc.cast<GrTwoPointConicalGradientLayout>(); |
| 152 | { |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 153 | const SkPoint& focalParamsValue = _outer.focalParams; |
| 154 | if (focalParamsPrev != focalParamsValue) { |
| 155 | focalParamsPrev = focalParamsValue; |
| 156 | pdman.set2f(focalParamsVar, focalParamsValue.fX, focalParamsValue.fY); |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | } |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 160 | SkPoint focalParamsPrev = SkPoint::Make(SK_FloatNaN, SK_FloatNaN); |
| 161 | UniformHandle focalParamsVar; |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 162 | }; |
Brian Salomon | 18ab203 | 2021-02-23 10:07:05 -0500 | [diff] [blame] | 163 | std::unique_ptr<GrGLSLFragmentProcessor> GrTwoPointConicalGradientLayout::onMakeProgramImpl() |
| 164 | const { |
| 165 | return std::make_unique<GrGLSLTwoPointConicalGradientLayout>(); |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 166 | } |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 167 | void GrTwoPointConicalGradientLayout::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 168 | GrProcessorKeyBuilder* b) const { |
Brian Osman | 4c59437 | 2021-03-02 16:57:09 -0500 | [diff] [blame] | 169 | b->addBits(2, (uint32_t)type, "type"); |
Brian Osman | 48d7f7c | 2021-03-03 13:38:08 -0500 | [diff] [blame] | 170 | b->addBool(isRadiusIncreasing, "isRadiusIncreasing"); |
| 171 | b->addBool(isFocalOnCircle, "isFocalOnCircle"); |
| 172 | b->addBool(isWellBehaved, "isWellBehaved"); |
| 173 | b->addBool(isSwapped, "isSwapped"); |
| 174 | b->addBool(isNativelyFocal, "isNativelyFocal"); |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 175 | } |
| 176 | bool GrTwoPointConicalGradientLayout::onIsEqual(const GrFragmentProcessor& other) const { |
| 177 | const GrTwoPointConicalGradientLayout& that = other.cast<GrTwoPointConicalGradientLayout>(); |
| 178 | (void)that; |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 179 | if (type != that.type) return false; |
| 180 | if (isRadiusIncreasing != that.isRadiusIncreasing) return false; |
| 181 | if (isFocalOnCircle != that.isFocalOnCircle) return false; |
| 182 | if (isWellBehaved != that.isWellBehaved) return false; |
| 183 | if (isSwapped != that.isSwapped) return false; |
| 184 | if (isNativelyFocal != that.isNativelyFocal) return false; |
| 185 | if (focalParams != that.focalParams) return false; |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 186 | return true; |
| 187 | } |
| 188 | GrTwoPointConicalGradientLayout::GrTwoPointConicalGradientLayout( |
| 189 | const GrTwoPointConicalGradientLayout& src) |
| 190 | : INHERITED(kGrTwoPointConicalGradientLayout_ClassID, src.optimizationFlags()) |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 191 | , type(src.type) |
| 192 | , isRadiusIncreasing(src.isRadiusIncreasing) |
| 193 | , isFocalOnCircle(src.isFocalOnCircle) |
| 194 | , isWellBehaved(src.isWellBehaved) |
| 195 | , isSwapped(src.isSwapped) |
| 196 | , isNativelyFocal(src.isNativelyFocal) |
| 197 | , focalParams(src.focalParams) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 198 | this->cloneAndRegisterAllChildProcessors(src); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 199 | this->setUsesSampleCoordsDirectly(); |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 200 | } |
| 201 | std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::clone() const { |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 202 | return std::make_unique<GrTwoPointConicalGradientLayout>(*this); |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 203 | } |
John Stiles | 8d9bf64 | 2020-08-12 15:07:45 -0400 | [diff] [blame] | 204 | #if GR_TEST_UTILS |
John Stiles | cab5886 | 2020-08-12 15:47:06 -0400 | [diff] [blame] | 205 | SkString GrTwoPointConicalGradientLayout::onDumpInfo() const { |
John Stiles | 47b4e22 | 2020-08-12 09:56:50 -0400 | [diff] [blame] | 206 | return SkStringPrintf( |
John Stiles | cab5886 | 2020-08-12 15:47:06 -0400 | [diff] [blame] | 207 | "(type=%d, isRadiusIncreasing=%s, isFocalOnCircle=%s, isWellBehaved=%s, isSwapped=%s, " |
| 208 | "isNativelyFocal=%s, focalParams=half2(%f, %f))", |
John Stiles | 9b660ef | 2021-03-04 15:45:00 -0500 | [diff] [blame] | 209 | (int)type, |
| 210 | (isRadiusIncreasing ? "true" : "false"), |
| 211 | (isFocalOnCircle ? "true" : "false"), |
| 212 | (isWellBehaved ? "true" : "false"), |
| 213 | (isSwapped ? "true" : "false"), |
| 214 | (isNativelyFocal ? "true" : "false"), |
| 215 | focalParams.fX, |
John Stiles | 47b4e22 | 2020-08-12 09:56:50 -0400 | [diff] [blame] | 216 | focalParams.fY); |
| 217 | } |
| 218 | #endif |
Michael Ludwig | 7f8c524 | 2018-09-14 15:07:55 -0400 | [diff] [blame] | 219 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTwoPointConicalGradientLayout); |
| 220 | #if GR_TEST_UTILS |
| 221 | std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::TestCreate( |
| 222 | GrProcessorTestData* d) { |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 223 | SkScalar scale = GrGradientShader::RandomParams::kGradientScale; |
Michael Ludwig | 083bc15 | 2018-10-01 17:15:15 -0400 | [diff] [blame] | 224 | SkScalar offset = scale / 32.0f; |
| 225 | |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 226 | SkPoint center1 = {d->fRandom->nextRangeScalar(0.0f, scale), |
Michael Ludwig | 083bc15 | 2018-10-01 17:15:15 -0400 | [diff] [blame] | 227 | d->fRandom->nextRangeScalar(0.0f, scale)}; |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 228 | SkPoint center2 = {d->fRandom->nextRangeScalar(0.0f, scale), |
Michael Ludwig | 083bc15 | 2018-10-01 17:15:15 -0400 | [diff] [blame] | 229 | d->fRandom->nextRangeScalar(0.0f, scale)}; |
| 230 | SkScalar radius1 = d->fRandom->nextRangeScalar(0.0f, scale); |
| 231 | SkScalar radius2 = d->fRandom->nextRangeScalar(0.0f, scale); |
Michael Ludwig | 7f8c524 | 2018-09-14 15:07:55 -0400 | [diff] [blame] | 232 | |
| 233 | constexpr int kTestTypeMask = (1 << 2) - 1, kTestNativelyFocalBit = (1 << 2), |
| 234 | kTestFocalOnCircleBit = (1 << 3), kTestSwappedBit = (1 << 4); |
| 235 | // We won't treat isWellDefined and isRadiusIncreasing specially because they |
| 236 | // should have high probability to be turned on and off as we're getting random |
| 237 | // radii and centers. |
| 238 | |
| 239 | int mask = d->fRandom->nextU(); |
| 240 | int type = mask & kTestTypeMask; |
| 241 | if (type == static_cast<int>(Type::kRadial)) { |
| 242 | center2 = center1; |
| 243 | // Make sure that the radii are different |
| 244 | if (SkScalarNearlyZero(radius1 - radius2)) { |
Michael Ludwig | 083bc15 | 2018-10-01 17:15:15 -0400 | [diff] [blame] | 245 | radius2 += offset; |
Michael Ludwig | 7f8c524 | 2018-09-14 15:07:55 -0400 | [diff] [blame] | 246 | } |
| 247 | } else if (type == static_cast<int>(Type::kStrip)) { |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 248 | 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] | 249 | radius2 = radius1; |
| 250 | // Make sure that the centers are different |
| 251 | if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) { |
Michael Ludwig | 083bc15 | 2018-10-01 17:15:15 -0400 | [diff] [blame] | 252 | center2.fX += offset; |
Michael Ludwig | 7f8c524 | 2018-09-14 15:07:55 -0400 | [diff] [blame] | 253 | } |
| 254 | } else { // kFocal_Type |
| 255 | // Make sure that the centers are different |
| 256 | if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) { |
Michael Ludwig | 083bc15 | 2018-10-01 17:15:15 -0400 | [diff] [blame] | 257 | center2.fX += offset; |
Michael Ludwig | 7f8c524 | 2018-09-14 15:07:55 -0400 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | if (kTestNativelyFocalBit & mask) { |
| 261 | radius1 = 0; |
| 262 | } |
| 263 | if (kTestFocalOnCircleBit & mask) { |
| 264 | radius2 = radius1 + SkPoint::Distance(center1, center2); |
| 265 | } |
| 266 | if (kTestSwappedBit & mask) { |
| 267 | std::swap(radius1, radius2); |
| 268 | radius2 = 0; |
| 269 | } |
| 270 | |
| 271 | // Make sure that the radii are different |
| 272 | if (SkScalarNearlyZero(radius1 - radius2)) { |
Michael Ludwig | 083bc15 | 2018-10-01 17:15:15 -0400 | [diff] [blame] | 273 | radius2 += offset; |
Michael Ludwig | 7f8c524 | 2018-09-14 15:07:55 -0400 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | if (SkScalarNearlyZero(radius1 - radius2) && |
| 278 | SkScalarNearlyZero(SkPoint::Distance(center1, center2))) { |
Michael Ludwig | 083bc15 | 2018-10-01 17:15:15 -0400 | [diff] [blame] | 279 | radius2 += offset; // make sure that we're not degenerated |
Michael Ludwig | 7f8c524 | 2018-09-14 15:07:55 -0400 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | GrGradientShader::RandomParams params(d->fRandom); |
John Stiles | 9b660ef | 2021-03-04 15:45:00 -0500 | [diff] [blame] | 283 | auto shader = params.fUseColors4f ? SkGradientShader::MakeTwoPointConical(center1, |
| 284 | radius1, |
| 285 | center2, |
| 286 | radius2, |
| 287 | params.fColors4f, |
| 288 | params.fColorSpace, |
| 289 | params.fStops, |
| 290 | params.fColorCount, |
| 291 | params.fTileMode) |
| 292 | : SkGradientShader::MakeTwoPointConical(center1, |
| 293 | radius1, |
| 294 | center2, |
| 295 | radius2, |
| 296 | params.fColors, |
| 297 | params.fStops, |
| 298 | params.fColorCount, |
| 299 | params.fTileMode); |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 300 | GrTest::TestAsFPArgs asFPArgs(d); |
Michael Ludwig | 7f8c524 | 2018-09-14 15:07:55 -0400 | [diff] [blame] | 301 | std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args()); |
| 302 | |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 303 | SkASSERT_RELEASE(fp); |
Michael Ludwig | 7f8c524 | 2018-09-14 15:07:55 -0400 | [diff] [blame] | 304 | return fp; |
| 305 | } |
| 306 | #endif |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 307 | |
| 308 | // .fp files do not let you reference outside enum definitions, so we have to explicitly map |
| 309 | // between the two compatible enum defs |
| 310 | GrTwoPointConicalGradientLayout::Type convert_type(SkTwoPointConicalGradient::Type type) { |
| 311 | switch (type) { |
| 312 | case SkTwoPointConicalGradient::Type::kRadial: |
| 313 | return GrTwoPointConicalGradientLayout::Type::kRadial; |
| 314 | case SkTwoPointConicalGradient::Type::kStrip: |
| 315 | return GrTwoPointConicalGradientLayout::Type::kStrip; |
| 316 | case SkTwoPointConicalGradient::Type::kFocal: |
| 317 | return GrTwoPointConicalGradientLayout::Type::kFocal; |
| 318 | } |
| 319 | SkDEBUGFAIL("Should not be reachable"); |
| 320 | return GrTwoPointConicalGradientLayout::Type::kRadial; |
| 321 | } |
| 322 | |
| 323 | std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::Make( |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 324 | const SkTwoPointConicalGradient& grad, const GrFPArgs& args) { |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 325 | GrTwoPointConicalGradientLayout::Type grType = convert_type(grad.getType()); |
| 326 | |
| 327 | // The focalData struct is only valid if isFocal is true |
| 328 | const SkTwoPointConicalGradient::FocalData& focalData = grad.getFocalData(); |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 329 | bool isFocal = grType == Type::kFocal; |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 330 | |
| 331 | // Calculate optimization switches from gradient specification |
| 332 | bool isFocalOnCircle = isFocal && focalData.isFocalOnCircle(); |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 333 | bool isWellBehaved = isFocal && focalData.isWellBehaved(); |
| 334 | bool isSwapped = isFocal && focalData.isSwapped(); |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 335 | bool isNativelyFocal = isFocal && focalData.isNativelyFocal(); |
| 336 | |
| 337 | // Type-specific calculations: isRadiusIncreasing, focalParams, and the gradient matrix. |
| 338 | // However, all types start with the total inverse local matrix calculated from the shader |
| 339 | // and args |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 340 | bool isRadiusIncreasing; |
| 341 | SkPoint focalParams; // really just a 2D tuple |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 342 | SkMatrix matrix; |
| 343 | |
| 344 | // Initialize the base matrix |
Florin Malita | 52f0291 | 2020-03-09 16:33:17 -0400 | [diff] [blame] | 345 | if (!grad.totalLocalMatrix(args.fPreLocalMatrix)->invert(&matrix)) { |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 346 | return nullptr; |
| 347 | } |
| 348 | |
| 349 | if (isFocal) { |
| 350 | isRadiusIncreasing = (1 - focalData.fFocalX) > 0; |
| 351 | |
| 352 | focalParams.set(1.0 / focalData.fR1, focalData.fFocalX); |
| 353 | |
| 354 | matrix.postConcat(grad.getGradientMatrix()); |
| 355 | } else if (grType == Type::kRadial) { |
Mike Klein | d6ab77a | 2019-03-21 08:18:24 -0500 | [diff] [blame] | 356 | SkScalar dr = grad.getDiffRadius(); |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 357 | isRadiusIncreasing = dr >= 0; |
| 358 | |
| 359 | SkScalar r0 = grad.getStartRadius() / dr; |
| 360 | focalParams.set(r0, r0 * r0); |
| 361 | |
| 362 | // GPU radial matrix is different from the original matrix, since we map the diff radius |
| 363 | // to have |dr| = 1, so manually compute the final gradient matrix here. |
| 364 | |
| 365 | // Map center to (0, 0) |
| 366 | matrix.postTranslate(-grad.getStartCenter().fX, -grad.getStartCenter().fY); |
| 367 | |
| 368 | // scale |diffRadius| to 1 |
| 369 | matrix.postScale(1 / dr, 1 / dr); |
| 370 | } else { // kStrip |
| 371 | isRadiusIncreasing = false; // kStrip doesn't use this flag |
| 372 | |
| 373 | SkScalar r0 = grad.getStartRadius() / grad.getCenterX1(); |
| 374 | focalParams.set(r0, r0 * r0); |
| 375 | |
| 376 | matrix.postConcat(grad.getGradientMatrix()); |
| 377 | } |
| 378 | |
John Stiles | 9b660ef | 2021-03-04 15:45:00 -0500 | [diff] [blame] | 379 | return GrMatrixEffect::Make(matrix, |
| 380 | std::unique_ptr<GrFragmentProcessor>( |
| 381 | new GrTwoPointConicalGradientLayout(grType, |
| 382 | isRadiusIncreasing, |
| 383 | isFocalOnCircle, |
| 384 | isWellBehaved, |
| 385 | isSwapped, |
| 386 | isNativelyFocal, |
| 387 | focalParams))); |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 388 | } |