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"
12
13#include "src/core/SkUtils.h"
14#include "src/gpu/GrTexture.h"
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"
20class GrGLSLTwoPointConicalGradientLayout : public GrGLSLFragmentProcessor {
21public:
22 GrGLSLTwoPointConicalGradientLayout() {}
23 void emitCode(EmitArgs& args) override {
24 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
25 const GrTwoPointConicalGradientLayout& _outer =
26 args.fFp.cast<GrTwoPointConicalGradientLayout>();
27 (void)_outer;
28 auto type = _outer.type;
29 (void)type;
30 auto isRadiusIncreasing = _outer.isRadiusIncreasing;
31 (void)isRadiusIncreasing;
32 auto isFocalOnCircle = _outer.isFocalOnCircle;
33 (void)isFocalOnCircle;
34 auto isWellBehaved = _outer.isWellBehaved;
35 (void)isWellBehaved;
36 auto isSwapped = _outer.isSwapped;
37 (void)isSwapped;
38 auto isNativelyFocal = _outer.isNativelyFocal;
39 (void)isNativelyFocal;
40 auto focalParams = _outer.focalParams;
41 (void)focalParams;
42 focalParamsVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag,
43 kHalf2_GrSLType, "focalParams");
44 fragBuilder->codeAppendf(
45 R"SkSL(float t = -1.0;
46half v = 1.0;
47@switch (%d) {
48 case 1:
49 {
50 half r0_2 = %s.y;
51 t = float(r0_2) - %s.y * %s.y;
52 if (t >= 0.0) {
53 t = %s.x + sqrt(t);
54 } else {
55 v = -1.0;
56 }
57 }
58 break;
59 case 0:
60 {
61 half r0 = %s.x;
62 @if (%s) {
63 t = length(%s) - float(r0);
64 } else {
65 t = -length(%s) - float(r0);
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) {
75 x_t = dot(%s, %s) / %s.x;
76 } else if (%s) {
77 x_t = length(%s) - %s.x * float(invR1);
78 } else {
79 float temp = %s.x * %s.x - %s.y * %s.y;
80 if (temp >= 0.0) {
81 @if (%s || !%s) {
82 x_t = -sqrt(temp) - %s.x * float(invR1);
83 } else {
84 x_t = sqrt(temp) - %s.x * float(invR1);
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",
114 (int)_outer.type, args.fUniformHandler->getUniformCStr(focalParamsVar),
115 args.fSampleCoord, args.fSampleCoord, args.fSampleCoord,
116 args.fUniformHandler->getUniformCStr(focalParamsVar),
117 (_outer.isRadiusIncreasing ? "true" : "false"), args.fSampleCoord,
118 args.fSampleCoord, args.fUniformHandler->getUniformCStr(focalParamsVar),
119 args.fUniformHandler->getUniformCStr(focalParamsVar),
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"),
126 (_outer.isRadiusIncreasing ? "true" : "false"),
127 (_outer.isNativelyFocal ? "true" : "false"),
128 (_outer.isNativelyFocal ? "true" : "false"), (_outer.isSwapped ? "true" : "false"),
129 args.fOutputColor);
130 }
131
132private:
133 void onSetData(const GrGLSLProgramDataManager& pdman,
134 const GrFragmentProcessor& _proc) override {
135 const GrTwoPointConicalGradientLayout& _outer =
136 _proc.cast<GrTwoPointConicalGradientLayout>();
137 {
138 const SkPoint& focalParamsValue = _outer.focalParams;
139 if (focalParamsPrev != focalParamsValue) {
140 focalParamsPrev = focalParamsValue;
141 pdman.set2f(focalParamsVar, focalParamsValue.fX, focalParamsValue.fY);
142 }
143 }
144 }
145 SkPoint focalParamsPrev = SkPoint::Make(SK_FloatNaN, SK_FloatNaN);
146 UniformHandle focalParamsVar;
147};
148GrGLSLFragmentProcessor* GrTwoPointConicalGradientLayout::onCreateGLSLInstance() const {
149 return new GrGLSLTwoPointConicalGradientLayout();
150}
151void GrTwoPointConicalGradientLayout::onGetGLSLProcessorKey(const GrShaderCaps& caps,
152 GrProcessorKeyBuilder* b) const {
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);
159}
160bool GrTwoPointConicalGradientLayout::onIsEqual(const GrFragmentProcessor& other) const {
161 const GrTwoPointConicalGradientLayout& that = other.cast<GrTwoPointConicalGradientLayout>();
162 (void)that;
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;
170 return true;
171}
172GrTwoPointConicalGradientLayout::GrTwoPointConicalGradientLayout(
173 const GrTwoPointConicalGradientLayout& src)
174 : INHERITED(kGrTwoPointConicalGradientLayout_ClassID, src.optimizationFlags())
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) {
182 this->cloneAndRegisterAllChildProcessors(src);
183 this->setUsesSampleCoordsDirectly();
184}
185std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::clone() const {
186 return std::make_unique<GrTwoPointConicalGradientLayout>(*this);
187}
188#if GR_TEST_UTILS
189SkString GrTwoPointConicalGradientLayout::onDumpInfo() const {
190 return SkStringPrintf(
191 "(type=%d, isRadiusIncreasing=%s, isFocalOnCircle=%s, isWellBehaved=%s, isSwapped=%s, "
192 "isNativelyFocal=%s, focalParams=half2(%f, %f))",
193 (int)type, (isRadiusIncreasing ? "true" : "false"),
194 (isFocalOnCircle ? "true" : "false"), (isWellBehaved ? "true" : "false"),
195 (isSwapped ? "true" : "false"), (isNativelyFocal ? "true" : "false"), focalParams.fX,
196 focalParams.fY);
197}
198#endif
199GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTwoPointConicalGradientLayout);
200#if GR_TEST_UTILS
201std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::TestCreate(
202 GrProcessorTestData* d) {
203 SkScalar scale = GrGradientShader::RandomParams::kGradientScale;
204 SkScalar offset = scale / 32.0f;
205
206 SkPoint center1 = {d->fRandom->nextRangeScalar(0.0f, scale),
207 d->fRandom->nextRangeScalar(0.0f, scale)};
208 SkPoint center2 = {d->fRandom->nextRangeScalar(0.0f, scale),
209 d->fRandom->nextRangeScalar(0.0f, scale)};
210 SkScalar radius1 = d->fRandom->nextRangeScalar(0.0f, scale);
211 SkScalar radius2 = d->fRandom->nextRangeScalar(0.0f, scale);
212
213 constexpr int kTestTypeMask = (1 << 2) - 1, kTestNativelyFocalBit = (1 << 2),
214 kTestFocalOnCircleBit = (1 << 3), kTestSwappedBit = (1 << 4);
215 // We won't treat isWellDefined and isRadiusIncreasing specially because they
216 // should have high probability to be turned on and off as we're getting random
217 // radii and centers.
218
219 int mask = d->fRandom->nextU();
220 int type = mask & kTestTypeMask;
221 if (type == static_cast<int>(Type::kRadial)) {
222 center2 = center1;
223 // Make sure that the radii are different
224 if (SkScalarNearlyZero(radius1 - radius2)) {
225 radius2 += offset;
226 }
227 } else if (type == static_cast<int>(Type::kStrip)) {
228 radius1 = std::max(radius1, .1f); // Make sure that the radius is non-zero
229 radius2 = radius1;
230 // Make sure that the centers are different
231 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
232 center2.fX += offset;
233 }
234 } else { // kFocal_Type
235 // Make sure that the centers are different
236 if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
237 center2.fX += offset;
238 }
239
240 if (kTestNativelyFocalBit & mask) {
241 radius1 = 0;
242 }
243 if (kTestFocalOnCircleBit & mask) {
244 radius2 = radius1 + SkPoint::Distance(center1, center2);
245 }
246 if (kTestSwappedBit & mask) {
247 std::swap(radius1, radius2);
248 radius2 = 0;
249 }
250
251 // Make sure that the radii are different
252 if (SkScalarNearlyZero(radius1 - radius2)) {
253 radius2 += offset;
254 }
255 }
256
257 if (SkScalarNearlyZero(radius1 - radius2) &&
258 SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
259 radius2 += offset; // make sure that we're not degenerated
260 }
261
262 GrGradientShader::RandomParams params(d->fRandom);
263 auto shader = params.fUseColors4f
264 ? SkGradientShader::MakeTwoPointConical(
265 center1, radius1, center2, radius2, params.fColors4f,
266 params.fColorSpace, params.fStops, params.fColorCount,
267 params.fTileMode)
268 : SkGradientShader::MakeTwoPointConical(
269 center1, radius1, center2, radius2, params.fColors,
270 params.fStops, params.fColorCount, params.fTileMode);
271 GrTest::TestAsFPArgs asFPArgs(d);
272 std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args());
273
274 SkASSERT_RELEASE(fp);
275 return fp;
276}
277#endif
278
279// .fp files do not let you reference outside enum definitions, so we have to explicitly map
280// between the two compatible enum defs
281GrTwoPointConicalGradientLayout::Type convert_type(SkTwoPointConicalGradient::Type type) {
282 switch (type) {
283 case SkTwoPointConicalGradient::Type::kRadial:
284 return GrTwoPointConicalGradientLayout::Type::kRadial;
285 case SkTwoPointConicalGradient::Type::kStrip:
286 return GrTwoPointConicalGradientLayout::Type::kStrip;
287 case SkTwoPointConicalGradient::Type::kFocal:
288 return GrTwoPointConicalGradientLayout::Type::kFocal;
289 }
290 SkDEBUGFAIL("Should not be reachable");
291 return GrTwoPointConicalGradientLayout::Type::kRadial;
292}
293
294std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::Make(
295 const SkTwoPointConicalGradient& grad, const GrFPArgs& args) {
296 GrTwoPointConicalGradientLayout::Type grType = convert_type(grad.getType());
297
298 // The focalData struct is only valid if isFocal is true
299 const SkTwoPointConicalGradient::FocalData& focalData = grad.getFocalData();
300 bool isFocal = grType == Type::kFocal;
301
302 // Calculate optimization switches from gradient specification
303 bool isFocalOnCircle = isFocal && focalData.isFocalOnCircle();
304 bool isWellBehaved = isFocal && focalData.isWellBehaved();
305 bool isSwapped = isFocal && focalData.isSwapped();
306 bool isNativelyFocal = isFocal && focalData.isNativelyFocal();
307
308 // Type-specific calculations: isRadiusIncreasing, focalParams, and the gradient matrix.
309 // However, all types start with the total inverse local matrix calculated from the shader
310 // and args
311 bool isRadiusIncreasing;
312 SkPoint focalParams; // really just a 2D tuple
313 SkMatrix matrix;
314
315 // Initialize the base matrix
316 if (!grad.totalLocalMatrix(args.fPreLocalMatrix)->invert(&matrix)) {
317 return nullptr;
318 }
319
320 if (isFocal) {
321 isRadiusIncreasing = (1 - focalData.fFocalX) > 0;
322
323 focalParams.set(1.0 / focalData.fR1, focalData.fFocalX);
324
325 matrix.postConcat(grad.getGradientMatrix());
326 } else if (grType == Type::kRadial) {
327 SkScalar dr = grad.getDiffRadius();
328 isRadiusIncreasing = dr >= 0;
329
330 SkScalar r0 = grad.getStartRadius() / dr;
331 focalParams.set(r0, r0 * r0);
332
333 // GPU radial matrix is different from the original matrix, since we map the diff radius
334 // to have |dr| = 1, so manually compute the final gradient matrix here.
335
336 // Map center to (0, 0)
337 matrix.postTranslate(-grad.getStartCenter().fX, -grad.getStartCenter().fY);
338
339 // scale |diffRadius| to 1
340 matrix.postScale(1 / dr, 1 / dr);
341 } else { // kStrip
342 isRadiusIncreasing = false; // kStrip doesn't use this flag
343
344 SkScalar r0 = grad.getStartRadius() / grad.getCenterX1();
345 focalParams.set(r0, r0 * r0);
346
347 matrix.postConcat(grad.getGradientMatrix());
348 }
349
350 return GrMatrixEffect::Make(
351 matrix, std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(
352 grType, isRadiusIncreasing, isFocalOnCircle, isWellBehaved, isSwapped,
353 isNativelyFocal, focalParams)));
354}
355