blob: b3e2fc1eb817d25e27a2b486b65fd732281cee9f [file] [log] [blame]
Michael Ludwig4f94ef62018-09-12 15:22:16 -04001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/gradients/GrGradientShader.h"
Michael Ludwig4f94ef62018-09-12 15:22:16 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/gradients/GrGradientBitmapCache.h"
Michael Ludwig4f94ef62018-09-12 15:22:16 -040011
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040012#include "include/gpu/GrRecordingContext.h"
Brian Osman65b45972021-06-25 15:58:43 +000013#include "src/core/SkRuntimeEffectPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrCaps.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040015#include "src/gpu/GrColor.h"
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040016#include "src/gpu/GrColorInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrRecordingContextPriv.h"
18#include "src/gpu/SkGr.h"
Brian Osman65b45972021-06-25 15:58:43 +000019#include "src/gpu/effects/GrMatrixEffect.h"
20#include "src/gpu/effects/GrSkSLFP.h"
Brian Salomon17473752020-06-19 09:53:50 -040021#include "src/gpu/effects/GrTextureEffect.h"
Michael Ludwiga7914d32018-09-14 09:47:21 -040022
Michael Ludwig72535fb2018-09-28 11:53:32 -040023// Intervals smaller than this (that aren't hard stops) on low-precision-only devices force us to
24// use the textured gradient
25static const SkScalar kLowPrecisionIntervalLimit = 0.01f;
26
Michael Ludwiga7914d32018-09-14 09:47:21 -040027// Each cache entry costs 1K or 2K of RAM. Each bitmap will be 1x256 at either 32bpp or 64bpp.
28static const int kMaxNumCachedGradientBitmaps = 32;
29static const int kGradientTextureSize = 256;
30
31// NOTE: signature takes raw pointers to the color/pos arrays and a count to make it easy for
32// MakeColorizer to transparently take care of hard stops at the end points of the gradient.
Brian Osman021ed512018-10-16 15:19:44 -040033static std::unique_ptr<GrFragmentProcessor> make_textured_colorizer(const SkPMColor4f* colors,
Michael Ludwiga7914d32018-09-14 09:47:21 -040034 const SkScalar* positions, int count, bool premul, const GrFPArgs& args) {
35 static GrGradientBitmapCache gCache(kMaxNumCachedGradientBitmaps, kGradientTextureSize);
36
37 // Use 8888 or F16, depending on the destination config.
38 // TODO: Use 1010102 for opaque gradients, at least if destination is 1010102?
39 SkColorType colorType = kRGBA_8888_SkColorType;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040040 if (GrColorTypeIsWiderThan(args.fDstColorInfo->colorType(), 8)) {
Greg Daniel7bfc9132019-08-14 14:23:53 -040041 auto f16Format = args.fContext->priv().caps()->getDefaultBackendFormat(
42 GrColorType::kRGBA_F16, GrRenderable::kNo);
43 if (f16Format.isValid()) {
44 colorType = kRGBA_F16_SkColorType;
45 }
Michael Ludwiga7914d32018-09-14 09:47:21 -040046 }
47 SkAlphaType alphaType = premul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
48
49 SkBitmap bitmap;
50 gCache.getGradient(colors, positions, count, colorType, alphaType, &bitmap);
51 SkASSERT(1 == bitmap.height() && SkIsPow2(bitmap.width()));
52 SkASSERT(bitmap.isImmutable());
53
Brian Salomon27c42022021-04-28 12:39:21 -040054 auto view = std::get<0>(GrMakeCachedBitmapProxyView(args.fContext, bitmap, GrMipmapped::kNo));
55 if (!view) {
Michael Ludwiga7914d32018-09-14 09:47:21 -040056 SkDebugf("Gradient won't draw. Could not create texture.");
57 return nullptr;
58 }
John Stiles1cf15932020-07-31 16:08:24 -040059
Brian Salomon17473752020-06-19 09:53:50 -040060 auto m = SkMatrix::Scale(view.width(), 1.f);
John Stiles1cf15932020-07-31 16:08:24 -040061 return GrTextureEffect::Make(std::move(view), alphaType, m, GrSamplerState::Filter::kLinear);
Michael Ludwiga7914d32018-09-14 09:47:21 -040062}
Michael Ludwig4f94ef62018-09-12 15:22:16 -040063
Brian Osman65b45972021-06-25 15:58:43 +000064
65static std::unique_ptr<GrFragmentProcessor> make_single_interval_colorizer(const SkPMColor4f& start,
66 const SkPMColor4f& end) {
67 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
68 uniform half4 start;
69 uniform half4 end;
70 half4 main(float2 coord) {
71 // Clamping and/or wrapping was already handled by the parent shader so the output
72 // color is a simple lerp.
73 return mix(start, end, half(coord.x));
74 }
75 )");
76 return GrSkSLFP::Make(effect, "SingleIntervalColorizer", /*inputFP=*/nullptr,
77 GrSkSLFP::OptFlags::kNone,
78 "start", start,
79 "end", end);
80}
81
82static std::unique_ptr<GrFragmentProcessor> make_dual_interval_colorizer(const SkPMColor4f& c0,
83 const SkPMColor4f& c1,
84 const SkPMColor4f& c2,
85 const SkPMColor4f& c3,
86 float threshold) {
87 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
88 uniform float4 scale01;
89 uniform float4 bias01;
90 uniform float4 scale23;
91 uniform float4 bias23;
92 uniform half threshold;
93
94 half4 main(float2 coord) {
95 half t = half(coord.x);
96
97 float4 scale, bias;
98 if (t < threshold) {
99 scale = scale01;
100 bias = bias01;
101 } else {
102 scale = scale23;
103 bias = bias23;
104 }
105
106 return half4(t * scale + bias);
107 }
108 )");
109
110 using sk4f = skvx::Vec<4, float>;
111
112 // Derive scale and biases from the 4 colors and threshold
113 auto vc0 = sk4f::Load(c0.vec());
114 auto vc1 = sk4f::Load(c1.vec());
115 auto scale01 = (vc1 - vc0) / threshold;
116 // bias01 = c0
117
118 auto vc2 = sk4f::Load(c2.vec());
119 auto vc3 = sk4f::Load(c3.vec());
120 auto scale23 = (vc3 - vc2) / (1 - threshold);
121 auto bias23 = vc2 - threshold * scale23;
122
123 return GrSkSLFP::Make(effect, "DualIntervalColorizer", /*inputFP=*/nullptr,
124 GrSkSLFP::OptFlags::kNone,
125 "scale01", scale01,
126 "bias01", c0,
127 "scale23", scale23,
128 "bias23", bias23,
129 "threshold", threshold);
130}
131
Brian Osman07bf3882021-07-01 12:56:23 -0400132static constexpr int kMaxUnrolledColorCount = 16;
133static constexpr int kMaxUnrolledIntervalCount = 8;
134
135static std::unique_ptr<GrFragmentProcessor> make_unrolled_colorizer(int intervalCount,
136 const SkPMColor4f* scale,
137 const SkPMColor4f* bias,
138 SkRect thresholds1_7,
139 SkRect thresholds9_13) {
140 SkASSERT(intervalCount >= 1 && intervalCount <= 8);
141
142 static SkOnce once[kMaxUnrolledIntervalCount];
143 static sk_sp<SkRuntimeEffect> effects[kMaxUnrolledIntervalCount];
144
145 once[intervalCount - 1]([intervalCount] {
146 SkString sksl;
147
148 // The 7 threshold positions that define the boundaries of the 8 intervals (excluding t = 0,
149 // and t = 1) are packed into two half4s instead of having up to 7 separate scalar uniforms.
150 // For low interval counts, the extra components are ignored in the shader, but the uniform
151 // simplification is worth it. It is assumed thresholds are provided in increasing value,
152 // mapped as:
153 // - thresholds1_7.x = boundary between (0,1) and (2,3) -> 1_2
154 // - .y = boundary between (2,3) and (4,5) -> 3_4
155 // - .z = boundary between (4,5) and (6,7) -> 5_6
156 // - .w = boundary between (6,7) and (8,9) -> 7_8
157 // - thresholds9_13.x = boundary between (8,9) and (10,11) -> 9_10
158 // - .y = boundary between (10,11) and (12,13) -> 11_12
159 // - .z = boundary between (12,13) and (14,15) -> 13_14
160 // - .w = unused
161 sksl.append("uniform half4 thresholds1_7, thresholds9_13;");
162
163 // With the current hardstop detection threshold of 0.00024, the maximum scale and bias
164 // values will be on the order of 4k (since they divide by dt). That is well outside the
165 // precision capabilities of half floats, which can lead to inaccurate gradient calculations
166 for (int i = 0; i < intervalCount; ++i) {
167 sksl.appendf("uniform float4 scale%d_%d;", 2 * i, 2 * i + 1);
168 sksl.appendf("uniform float4 bias%d_%d;", 2 * i, 2 * i + 1);
169 }
170
171 sksl.append("half4 main(float2 coord) {");
172 sksl.append(" half t = half(coord.x);");
173 sksl.append(" float4 scale, bias;");
174
175 // To ensure that the code below always compiles, inject local variables with the names of
176 // the uniforms that we *didn't* emit above. These will all end up unused and removed.
177 for (int i = intervalCount; i < kMaxUnrolledIntervalCount; i++) {
178 sksl.appendf("float4 scale%d_%d, bias%d_%d;", i * 2, i * 2 + 1, i * 2, i * 2 + 1);
179 }
180
181 sksl.appendf(R"(
182 // Explicit binary search for the proper interval that t falls within. The interval
183 // count checks are constant expressions, which are then optimized to the minimal number
184 // of branches for the specific interval count.
185
186 // thresholds1_7.w is mid point for intervals (0,7) and (8,15)
187 if (%d <= 4 || t < thresholds1_7.w) {
188 // thresholds1_7.y is mid point for intervals (0,3) and (4,7)
189 if (%d <= 2 || t < thresholds1_7.y) {
190 // thresholds1_7.x is mid point for intervals (0,1) and (2,3)
191 if (%d <= 1 || t < thresholds1_7.x) {
192 scale = scale0_1;
193 bias = bias0_1;
194 } else {
195 scale = scale2_3;
196 bias = bias2_3;
197 }
198 } else {
199 // thresholds1_7.z is mid point for intervals (4,5) and (6,7)
200 if (%d <= 3 || t < thresholds1_7.z) {
201 scale = scale4_5;
202 bias = bias4_5;
203 } else {
204 scale = scale6_7;
205 bias = bias6_7;
206 }
207 }
208 } else {
209 // thresholds9_13.y is mid point for intervals (8,11) and (12,15)
210 if (%d <= 6 || t < thresholds9_13.y) {
211 // thresholds9_13.x is mid point for intervals (8,9) and (10,11)
212 if (%d <= 5 || t < thresholds9_13.x) {
213 // interval 8-9
214 scale = scale8_9;
215 bias = bias8_9;
216 } else {
217 // interval 10-11
218 scale = scale10_11;
219 bias = bias10_11;
220 }
221 } else {
222 // thresholds9_13.z is mid point for intervals (12,13) and (14,15)
223 if (%d <= 7 || t < thresholds9_13.z) {
224 // interval 12-13
225 scale = scale12_13;
226 bias = bias12_13;
227 } else {
228 // interval 14-15
229 scale = scale14_15;
230 bias = bias14_15;
231 }
232 }
233 }
234
235 )", intervalCount, intervalCount, intervalCount, intervalCount, intervalCount,
236 intervalCount, intervalCount);
237
238 sksl.append("return half4(t * scale + bias); }");
239
240 auto result = SkRuntimeEffect::MakeForShader(std::move(sksl));
241 SkASSERTF(result.effect, "%s", result.errorText.c_str());
242 effects[intervalCount - 1] = std::move(result.effect);
243 });
244
245#define ADD_STOP(N, suffix) \
246 "scale" #suffix, GrSkSLFP::When(intervalCount > N, scale[N]), \
247 "bias" #suffix, GrSkSLFP::When(intervalCount > N, bias[N])
248
249 return GrSkSLFP::Make(effects[intervalCount - 1], "UnrolledBinaryColorizer",
250 /*inputFP=*/nullptr, GrSkSLFP::OptFlags::kNone,
251 "thresholds1_7", thresholds1_7,
252 "thresholds9_13", thresholds9_13,
253 "scale0_1", scale[0], "bias0_1", bias[0],
254 ADD_STOP(1, 2_3),
255 ADD_STOP(2, 4_5),
256 ADD_STOP(3, 6_7),
257 ADD_STOP(4, 8_9),
258 ADD_STOP(5, 10_11),
259 ADD_STOP(6, 12_13),
260 ADD_STOP(7, 14_15));
261#undef ADD_STOP
262}
263
264static std::unique_ptr<GrFragmentProcessor> make_unrolled_binary_colorizer(
265 const SkPMColor4f* colors, const SkScalar* positions, int count) {
266 // Depending on how the positions resolve into hard stops or regular stops, the number of
267 // intervals specified by the number of colors/positions can change. For instance, a plain
268 // 3 color gradient is two intervals, but a 4 color gradient with a hard stop is also
269 // two intervals. At the most extreme end, an 8 interval gradient made entirely of hard
270 // stops has 16 colors.
271
272 if (count > kMaxUnrolledColorCount) {
273 // Definitely cannot represent this gradient configuration
274 return nullptr;
275 }
276
277 // The raster implementation also uses scales and biases, but since they must be calculated
278 // after the dst color space is applied, it limits our ability to cache their values.
279 SkPMColor4f scales[kMaxUnrolledIntervalCount];
280 SkPMColor4f biases[kMaxUnrolledIntervalCount];
281 SkScalar thresholds[kMaxUnrolledIntervalCount] = { 0 };
282
283 int intervalCount = 0;
284
285 for (int i = 0; i < count - 1; i++) {
286 if (intervalCount >= kMaxUnrolledIntervalCount) {
287 // Already reached kMaxUnrolledIntervalCount, and haven't run out of color stops so this
288 // gradient cannot be represented by this shader.
289 return nullptr;
290 }
291
292 SkScalar t0 = positions[i];
293 SkScalar t1 = positions[i + 1];
294 SkScalar dt = t1 - t0;
295 // If the interval is empty, skip to the next interval. This will automatically create
296 // distinct hard stop intervals as needed. It also protects against malformed gradients
297 // that have repeated hard stops at the very beginning that are effectively unreachable.
298 if (SkScalarNearlyZero(dt)) {
299 continue;
300 }
301
302 auto c0 = Sk4f::Load(colors[i].vec());
303 auto c1 = Sk4f::Load(colors[i + 1].vec());
304
305 auto scale = (c1 - c0) / dt;
306 auto bias = c0 - t0 * scale;
307
308 scale.store(scales + intervalCount);
309 bias.store(biases + intervalCount);
310 thresholds[intervalCount] = t1;
311 intervalCount++;
312 }
313
314 SkRect thresholds1_7 = {thresholds[0], thresholds[1], thresholds[2], thresholds[3]},
315 thresholds9_13 = {thresholds[4], thresholds[5], thresholds[6], 0.0};
316
317 return make_unrolled_colorizer(intervalCount, scales, biases, thresholds1_7, thresholds9_13);
318}
319
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400320// Analyze the shader's color stops and positions and chooses an appropriate colorizer to represent
321// the gradient.
Brian Osman021ed512018-10-16 15:19:44 -0400322static std::unique_ptr<GrFragmentProcessor> make_colorizer(const SkPMColor4f* colors,
Michael Ludwiga7914d32018-09-14 09:47:21 -0400323 const SkScalar* positions, int count, bool premul, const GrFPArgs& args) {
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400324 // If there are hard stops at the beginning or end, the first and/or last color should be
325 // ignored by the colorizer since it should only be used in a clamped border color. By detecting
326 // and removing these stops at the beginning, it makes optimizing the remaining color stops
327 // simpler.
328
Kevin Lubickbe03ef12021-06-16 15:28:00 -0400329 // SkGradientShaderBase guarantees that pos[0] == 0 by adding a default value.
Michael Ludwig0495f7a2018-09-12 15:23:33 -0400330 bool bottomHardStop = SkScalarNearlyEqual(positions[0], positions[1]);
331 // The same is true for pos[end] == 1
Michael Ludwiga7914d32018-09-14 09:47:21 -0400332 bool topHardStop = SkScalarNearlyEqual(positions[count - 2], positions[count - 1]);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400333
334 int offset = 0;
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400335 if (bottomHardStop) {
336 offset += 1;
337 count--;
338 }
339 if (topHardStop) {
340 count--;
341 }
342
Michael Ludwig0495f7a2018-09-12 15:23:33 -0400343 // Two remaining colors means a single interval from 0 to 1
344 // (but it may have originally been a 3 or 4 color gradient with 1-2 hard stops at the ends)
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400345 if (count == 2) {
Brian Osman65b45972021-06-25 15:58:43 +0000346 return make_single_interval_colorizer(colors[offset], colors[offset + 1]);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400347 }
348
Michael Ludwig72535fb2018-09-28 11:53:32 -0400349 // Do an early test for the texture fallback to skip all of the other tests for specific
350 // analytic support of the gradient (and compatibility with the hardware), when it's definitely
351 // impossible to use an analytic solution.
Brian Osman07bf3882021-07-01 12:56:23 -0400352 bool tryAnalyticColorizer = count <= kMaxUnrolledColorCount;
Michael Ludwig72535fb2018-09-28 11:53:32 -0400353
354 // The remaining analytic colorizers use scale*t+bias, and the scale/bias values can become
355 // quite large when thresholds are close (but still outside the hardstop limit). If float isn't
356 // 32-bit, output can be incorrect if the thresholds are too close together. However, the
357 // analytic shaders are higher quality, so they can be used with lower precision hardware when
358 // the thresholds are not ill-conditioned.
Robert Phillips9da87e02019-02-04 13:26:26 -0500359 const GrShaderCaps* caps = args.fContext->priv().caps()->shaderCaps();
Michael Ludwig72535fb2018-09-28 11:53:32 -0400360 if (!caps->floatIs32Bits() && tryAnalyticColorizer) {
361 // Could run into problems, check if thresholds are close together (with a limit of .01, so
362 // that scales will be less than 100, which leaves 4 decimals of precision on 16-bit).
363 for (int i = offset; i < count - 1; i++) {
364 SkScalar dt = SkScalarAbs(positions[i] - positions[i + 1]);
365 if (dt <= kLowPrecisionIntervalLimit && dt > SK_ScalarNearlyZero) {
366 tryAnalyticColorizer = false;
367 break;
368 }
369 }
370 }
371
372 if (tryAnalyticColorizer) {
373 if (count == 3) {
374 // Must be a dual interval gradient, where the middle point is at offset+1 and the two
375 // intervals share the middle color stop.
Brian Osman65b45972021-06-25 15:58:43 +0000376 return make_dual_interval_colorizer(colors[offset], colors[offset + 1],
377 colors[offset + 1], colors[offset + 2],
378 positions[offset + 1]);
Michael Ludwig72535fb2018-09-28 11:53:32 -0400379 } else if (count == 4 && SkScalarNearlyEqual(positions[offset + 1],
380 positions[offset + 2])) {
381 // Two separate intervals that join at the same threshold position
Brian Osman65b45972021-06-25 15:58:43 +0000382 return make_dual_interval_colorizer(colors[offset], colors[offset + 1],
383 colors[offset + 2], colors[offset + 3],
384 positions[offset + 1]);
Michael Ludwig72535fb2018-09-28 11:53:32 -0400385 }
386
387 // The single and dual intervals are a specialized case of the unrolled binary search
388 // colorizer which can analytically render gradients of up to 8 intervals (up to 9 or 16
389 // colors depending on how many hard stops are inserted).
Brian Osman07bf3882021-07-01 12:56:23 -0400390 std::unique_ptr<GrFragmentProcessor> unrolled =
391 make_unrolled_binary_colorizer(colors + offset, positions + offset, count);
Michael Ludwig72535fb2018-09-28 11:53:32 -0400392 if (unrolled) {
393 return unrolled;
394 }
395 }
396
397 // Otherwise fall back to a rasterized gradient sampled by a texture, which can handle
398 // arbitrary gradients (the only downside being sampling resolution).
Michael Ludwiga7914d32018-09-14 09:47:21 -0400399 return make_textured_colorizer(colors + offset, positions + offset, count, premul, args);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400400}
401
Brian Osmanc6804ed2021-06-29 11:11:49 -0400402// This top-level effect implements clamping on the layout coordinate and requires specifying the
403// border colors that are used when outside the clamped boundary. Gradients with the
404// SkTileMode::kClamp should use the colors at their first and last stop (after adding default stops
405// for t=0,t=1) as the border color. This will automatically replicate the edge color, even when
406// there is a hard stop.
407//
408// The SkTileMode::kDecal can be produced by specifying transparent black as the border colors,
409// regardless of the gradient's stop colors.
410static std::unique_ptr<GrFragmentProcessor> make_clamped_gradient(
411 std::unique_ptr<GrFragmentProcessor> colorizer,
412 std::unique_ptr<GrFragmentProcessor> gradLayout,
413 SkPMColor4f leftBorderColor,
414 SkPMColor4f rightBorderColor,
415 bool makePremul,
416 bool colorsAreOpaque) {
417 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
418 uniform shader colorizer;
419 uniform shader gradLayout;
420
421 uniform half4 leftBorderColor; // t < 0.0
422 uniform half4 rightBorderColor; // t > 1.0
423
424 uniform int makePremul; // specialized
425 uniform int layoutPreservesOpacity; // specialized
426
427 half4 main(float2 coord) {
Brian Osmancbfa34a2021-09-02 09:26:27 -0400428 half4 t = gradLayout.eval(coord);
Brian Osmanc6804ed2021-06-29 11:11:49 -0400429 half4 outColor;
430
431 // If t.x is below 0, use the left border color without invoking the child processor.
432 // If any t.x is above 1, use the right border color. Otherwise, t is in the [0, 1]
433 // range assumed by the colorizer FP, so delegate to the child processor.
434 if (!bool(layoutPreservesOpacity) && t.y < 0) {
435 // layout has rejected this fragment (rely on sksl to remove this branch if the
436 // layout FP preserves opacity is false)
437 outColor = half4(0);
438 } else if (t.x < 0) {
439 outColor = leftBorderColor;
440 } else if (t.x > 1.0) {
441 outColor = rightBorderColor;
442 } else {
443 // Always sample from (x, 0), discarding y, since the layout FP can use y as a
444 // side-channel.
Brian Osmancbfa34a2021-09-02 09:26:27 -0400445 outColor = colorizer.eval(t.x0);
Brian Osmanc6804ed2021-06-29 11:11:49 -0400446 }
447 if (bool(makePremul)) {
448 outColor.rgb *= outColor.a;
449 }
450 return outColor;
451 }
452 )");
453
454 // If the layout does not preserve opacity, remove the opaque optimization,
455 // but otherwise respect the provided color opacity state (which should take
456 // into account the opacity of the border colors).
457 bool layoutPreservesOpacity = gradLayout->preservesOpaqueInput();
458 GrSkSLFP::OptFlags optFlags = GrSkSLFP::OptFlags::kCompatibleWithCoverageAsAlpha;
459 if (colorsAreOpaque && layoutPreservesOpacity) {
460 optFlags |= GrSkSLFP::OptFlags::kPreservesOpaqueInput;
461 }
462
463 return GrSkSLFP::Make(effect, "ClampedGradient", /*inputFP=*/nullptr, optFlags,
464 "colorizer", GrSkSLFP::IgnoreOptFlags(std::move(colorizer)),
465 "gradLayout", GrSkSLFP::IgnoreOptFlags(std::move(gradLayout)),
466 "leftBorderColor", leftBorderColor,
467 "rightBorderColor", rightBorderColor,
468 "makePremul", GrSkSLFP::Specialize<int>(makePremul),
469 "layoutPreservesOpacity",
470 GrSkSLFP::Specialize<int>(layoutPreservesOpacity));
471}
472
473static std::unique_ptr<GrFragmentProcessor> make_tiled_gradient(
474 const GrFPArgs& args,
475 std::unique_ptr<GrFragmentProcessor> colorizer,
476 std::unique_ptr<GrFragmentProcessor> gradLayout,
477 bool mirror,
478 bool makePremul,
479 bool colorsAreOpaque) {
480 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
481 uniform shader colorizer;
482 uniform shader gradLayout;
483
484 uniform int mirror; // specialized
485 uniform int makePremul; // specialized
486 uniform int layoutPreservesOpacity; // specialized
487 uniform int useFloorAbsWorkaround; // specialized
488
489 half4 main(float2 coord) {
Brian Osmancbfa34a2021-09-02 09:26:27 -0400490 half4 t = gradLayout.eval(coord);
Brian Osmanc6804ed2021-06-29 11:11:49 -0400491
492 if (!bool(layoutPreservesOpacity) && t.y < 0) {
493 // layout has rejected this fragment (rely on sksl to remove this branch if the
494 // layout FP preserves opacity is false)
495 return half4(0);
496 } else {
497 if (bool(mirror)) {
498 half t_1 = t.x - 1;
499 half tiled_t = t_1 - 2 * floor(t_1 * 0.5) - 1;
500 if (bool(useFloorAbsWorkaround)) {
501 // At this point the expected value of tiled_t should between -1 and 1, so
502 // this clamp has no effect other than to break up the floor and abs calls
503 // and make sure the compiler doesn't merge them back together.
504 tiled_t = clamp(tiled_t, -1, 1);
505 }
506 t.x = abs(tiled_t);
507 } else {
508 // Simple repeat mode
509 t.x = fract(t.x);
510 }
511
512 // Always sample from (x, 0), discarding y, since the layout FP can use y as a
513 // side-channel.
Brian Osmancbfa34a2021-09-02 09:26:27 -0400514 half4 outColor = colorizer.eval(t.x0);
Brian Osmanc6804ed2021-06-29 11:11:49 -0400515 if (bool(makePremul)) {
516 outColor.rgb *= outColor.a;
517 }
518 return outColor;
519 }
520 }
521 )");
522
523 // If the layout does not preserve opacity, remove the opaque optimization,
524 // but otherwise respect the provided color opacity state (which should take
525 // into account the opacity of the border colors).
526 bool layoutPreservesOpacity = gradLayout->preservesOpaqueInput();
527 GrSkSLFP::OptFlags optFlags = GrSkSLFP::OptFlags::kCompatibleWithCoverageAsAlpha;
528 if (colorsAreOpaque && layoutPreservesOpacity) {
529 optFlags |= GrSkSLFP::OptFlags::kPreservesOpaqueInput;
530 }
531 const bool useFloorAbsWorkaround =
532 args.fContext->priv().caps()->shaderCaps()->mustDoOpBetweenFloorAndAbs();
533
534 return GrSkSLFP::Make(effect, "TiledGradient", /*inputFP=*/nullptr, optFlags,
535 "colorizer", GrSkSLFP::IgnoreOptFlags(std::move(colorizer)),
536 "gradLayout", GrSkSLFP::IgnoreOptFlags(std::move(gradLayout)),
537 "mirror", GrSkSLFP::Specialize<int>(mirror),
538 "makePremul", GrSkSLFP::Specialize<int>(makePremul),
539 "layoutPreservesOpacity",
540 GrSkSLFP::Specialize<int>(layoutPreservesOpacity),
541 "useFloorAbsWorkaround",
542 GrSkSLFP::Specialize<int>(useFloorAbsWorkaround));
543}
544
Michael Ludwig672c4762020-08-05 17:28:42 -0400545// Combines the colorizer and layout with an appropriately configured top-level effect based on the
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400546// gradient's tile mode
Brian Osman65b45972021-06-25 15:58:43 +0000547static std::unique_ptr<GrFragmentProcessor> make_gradient(
548 const SkGradientShaderBase& shader,
549 const GrFPArgs& args,
550 std::unique_ptr<GrFragmentProcessor> layout,
551 const SkMatrix* overrideMatrix = nullptr) {
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400552 // No shader is possible if a layout couldn't be created, e.g. a layout-specific Make() returned
553 // null.
554 if (layout == nullptr) {
555 return nullptr;
556 }
557
Brian Osman65b45972021-06-25 15:58:43 +0000558 // Wrap the layout in a matrix effect to apply the gradient's matrix:
559 SkMatrix matrix;
560 if (!shader.totalLocalMatrix(args.fPreLocalMatrix)->invert(&matrix)) {
561 return nullptr;
562 }
563 // Some two-point conical gradients use a custom matrix here
564 matrix.postConcat(overrideMatrix ? *overrideMatrix : shader.getGradientMatrix());
565 layout = GrMatrixEffect::Make(matrix, std::move(layout));
566
Brian Osman021ed512018-10-16 15:19:44 -0400567 // Convert all colors into destination space and into SkPMColor4fs, and handle
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400568 // premul issues depending on the interpolation mode
569 bool inputPremul = shader.getGradFlags() & SkGradientShader::kInterpolateColorsInPremul_Flag;
Michael Ludwigb96cba32018-09-14 13:59:24 -0400570 bool allOpaque = true;
Brian Osman021ed512018-10-16 15:19:44 -0400571 SkAutoSTMalloc<4, SkPMColor4f> colors(shader.fColorCount);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400572 SkColor4fXformer xformedColors(shader.fOrigColors4f, shader.fColorCount,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400573 shader.fColorSpace.get(), args.fDstColorInfo->colorSpace());
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400574 for (int i = 0; i < shader.fColorCount; i++) {
Brian Osman021ed512018-10-16 15:19:44 -0400575 const SkColor4f& upmColor = xformedColors.fColors[i];
576 colors[i] = inputPremul ? upmColor.premul()
577 : SkPMColor4f{ upmColor.fR, upmColor.fG, upmColor.fB, upmColor.fA };
578 if (allOpaque && !SkScalarNearlyEqual(colors[i].fA, 1.0)) {
Michael Ludwigb96cba32018-09-14 13:59:24 -0400579 allOpaque = false;
580 }
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400581 }
582
Michael Ludwig0495f7a2018-09-12 15:23:33 -0400583 // SkGradientShader stores positions implicitly when they are evenly spaced, but the getPos()
584 // implementation performs a branch for every position index. Since the shader conversion
585 // requires lots of position tests, calculate all of the positions up front if needed.
586 SkTArray<SkScalar, true> implicitPos;
587 SkScalar* positions;
588 if (shader.fOrigPos) {
589 positions = shader.fOrigPos;
590 } else {
John Stilesf4bda742020-10-14 16:57:41 -0400591 implicitPos.reserve_back(shader.fColorCount);
Mike Kleind3ed3012018-11-06 19:23:08 -0500592 SkScalar posScale = SK_Scalar1 / (shader.fColorCount - 1);
Michael Ludwig0495f7a2018-09-12 15:23:33 -0400593 for (int i = 0 ; i < shader.fColorCount; i++) {
594 implicitPos.push_back(SkIntToScalar(i) * posScale);
595 }
596 positions = implicitPos.begin();
597 }
598
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400599 // All gradients are colorized the same way, regardless of layout
Michael Ludwiga7914d32018-09-14 09:47:21 -0400600 std::unique_ptr<GrFragmentProcessor> colorizer = make_colorizer(
601 colors.get(), positions, shader.fColorCount, inputPremul, args);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400602 if (colorizer == nullptr) {
603 return nullptr;
604 }
605
Michael Ludwig672c4762020-08-05 17:28:42 -0400606 // The top-level effect has to export premul colors, but under certain conditions it doesn't
607 // need to do anything to achieve that: i.e. its interpolating already premul colors
608 // (inputPremul) or all the colors have a = 1, in which case premul is a no op. Note that this
609 // allOpaque check is more permissive than SkGradientShaderBase's isOpaque(), since we can
610 // optimize away the make-premul op for two point conical gradients (which report false for
611 // isOpaque).
Michael Ludwigb96cba32018-09-14 13:59:24 -0400612 bool makePremul = !inputPremul && !allOpaque;
613
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400614 // All tile modes are supported (unless something was added to SkShader)
Michael Ludwig672c4762020-08-05 17:28:42 -0400615 std::unique_ptr<GrFragmentProcessor> gradient;
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400616 switch(shader.getTileMode()) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400617 case SkTileMode::kRepeat:
Brian Osmanc6804ed2021-06-29 11:11:49 -0400618 gradient = make_tiled_gradient(args, std::move(colorizer), std::move(layout),
619 /* mirror */ false, makePremul, allOpaque);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400620 break;
Mike Reedfae8fce2019-04-03 10:27:45 -0400621 case SkTileMode::kMirror:
Brian Osmanc6804ed2021-06-29 11:11:49 -0400622 gradient = make_tiled_gradient(args, std::move(colorizer), std::move(layout),
623 /* mirror */ true, makePremul, allOpaque);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400624 break;
Mike Reedfae8fce2019-04-03 10:27:45 -0400625 case SkTileMode::kClamp:
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400626 // For the clamped mode, the border colors are the first and last colors, corresponding
627 // to t=0 and t=1, because SkGradientShaderBase enforces that by adding color stops as
628 // appropriate. If there is a hard stop, this grabs the expected outer colors for the
629 // border.
Brian Osmanc6804ed2021-06-29 11:11:49 -0400630 gradient = make_clamped_gradient(std::move(colorizer), std::move(layout),
631 colors[0], colors[shader.fColorCount - 1],
632 makePremul, allOpaque);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400633 break;
Mike Reedfae8fce2019-04-03 10:27:45 -0400634 case SkTileMode::kDecal:
Michael Ludwigb96cba32018-09-14 13:59:24 -0400635 // Even if the gradient colors are opaque, the decal borders are transparent so
636 // disable that optimization
Brian Osmanc6804ed2021-06-29 11:11:49 -0400637 gradient = make_clamped_gradient(std::move(colorizer), std::move(layout),
638 SK_PMColor4fTRANSPARENT, SK_PMColor4fTRANSPARENT,
639 makePremul, /* colorsAreOpaque */ false);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400640 break;
641 }
642
Michael Ludwig672c4762020-08-05 17:28:42 -0400643 if (gradient == nullptr) {
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400644 // Unexpected tile mode
645 return nullptr;
646 }
Brian Salomonc0d79e52019-04-10 15:02:11 -0400647 if (args.fInputColorIsOpaque) {
Brian Osman55cbc752021-05-27 11:57:13 -0400648 // If the input alpha is known to be 1, we don't need to take the kSrcIn path. This is
649 // just an optimization. However, we can't just return 'gradient' here. We need to actually
650 // inhibit the coverage-as-alpha optimization, or we'll fail to incorporate AA correctly.
651 // The OverrideInput FP happens to do that, so wrap our fp in one of those. The gradient FP
652 // doesn't actually use the input color at all, so the overridden input is irrelevant.
Michael Ludwig672c4762020-08-05 17:28:42 -0400653 return GrFragmentProcessor::OverrideInput(std::move(gradient), SK_PMColor4fWHITE, false);
Brian Salomonc0d79e52019-04-10 15:02:11 -0400654 }
Michael Ludwig672c4762020-08-05 17:28:42 -0400655 return GrFragmentProcessor::MulChildByInputAlpha(std::move(gradient));
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400656}
657
658namespace GrGradientShader {
659
660std::unique_ptr<GrFragmentProcessor> MakeLinear(const SkLinearGradient& shader,
661 const GrFPArgs& args) {
Brian Osman65b45972021-06-25 15:58:43 +0000662 // We add a tiny delta to t. When gradient stops are set up so that a hard stop in a vertically
663 // or horizontally oriented gradient falls exactly at a column or row of pixel centers we can
664 // we can get slightly different interpolated t values along the column/row. By adding the delta
665 // we will consistently get the color to the "right" of the stop. Of course if the hard stop
666 // falls at X.5 - delta then we still could get inconsistent results, but that is much less
667 // likely. crbug.com/938592
668 // If/when we add filtering of the gradient this can be removed.
669 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
670 half4 main(float2 coord) {
671 return half4(half(coord.x) + 0.00001, 1, 0, 0); // y = 1 for always valid
672 }
673 )");
674 // The linear gradient never rejects a pixel so it doesn't change opacity
675 auto fp = GrSkSLFP::Make(effect, "LinearLayout", /*inputFP=*/nullptr,
676 GrSkSLFP::OptFlags::kPreservesOpaqueInput);
677 return make_gradient(shader, args, std::move(fp));
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400678}
679
Michael Ludwig4089df82018-09-12 15:22:37 -0400680std::unique_ptr<GrFragmentProcessor> MakeRadial(const SkRadialGradient& shader,
681 const GrFPArgs& args) {
Brian Osman65b45972021-06-25 15:58:43 +0000682 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
683 half4 main(float2 coord) {
684 return half4(half(length(coord)), 1, 0, 0); // y = 1 for always valid
685 }
686 )");
687 // The radial gradient never rejects a pixel so it doesn't change opacity
688 auto fp = GrSkSLFP::Make(effect, "RadialLayout", /*inputFP=*/nullptr,
689 GrSkSLFP::OptFlags::kPreservesOpaqueInput);
690 return make_gradient(shader, args, std::move(fp));
Michael Ludwig4089df82018-09-12 15:22:37 -0400691}
692
Michael Ludwig24d438b2018-09-12 15:22:50 -0400693std::unique_ptr<GrFragmentProcessor> MakeSweep(const SkSweepGradient& shader,
694 const GrFPArgs& args) {
Brian Osman65b45972021-06-25 15:58:43 +0000695 // On some devices they incorrectly implement atan2(y,x) as atan(y/x). In actuality it is
696 // atan2(y,x) = 2 * atan(y / (sqrt(x^2 + y^2) + x)). So to work around this we pass in (sqrt(x^2
697 // + y^2) + x) as the second parameter to atan2 in these cases. We let the device handle the
698 // undefined behavior of the second paramenter being 0 instead of doing the divide ourselves and
699 // using atan instead.
700 int useAtanWorkaround =
701 args.fContext->priv().caps()->shaderCaps()->atan2ImplementedAsAtanYOverX();
702 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
703 uniform half bias;
704 uniform half scale;
705 uniform int useAtanWorkaround; // specialized
706
707 half4 main(float2 coord) {
708 half angle = bool(useAtanWorkaround)
709 ? half(2 * atan(-coord.y, length(coord) - coord.x))
710 : half(atan(-coord.y, -coord.x));
711
712 // 0.1591549430918 is 1/(2*pi), used since atan returns values [-pi, pi]
713 half t = (angle * 0.1591549430918 + 0.5 + bias) * scale;
714 return half4(t, 1, 0, 0); // y = 1 for always valid
715 }
716 )");
717 // The sweep gradient never rejects a pixel so it doesn't change opacity
718 auto fp = GrSkSLFP::Make(effect, "SweepLayout", /*inputFP=*/nullptr,
719 GrSkSLFP::OptFlags::kPreservesOpaqueInput,
720 "bias", shader.getTBias(),
721 "scale", shader.getTScale(),
722 "useAtanWorkaround", GrSkSLFP::Specialize(useAtanWorkaround));
723 return make_gradient(shader, args, std::move(fp));
Michael Ludwig24d438b2018-09-12 15:22:50 -0400724}
725
Michael Ludwig8f685082018-09-12 15:23:01 -0400726std::unique_ptr<GrFragmentProcessor> MakeConical(const SkTwoPointConicalGradient& shader,
727 const GrFPArgs& args) {
Brian Osman65b45972021-06-25 15:58:43 +0000728 // The 2 point conical gradient can reject a pixel so it does change opacity even if the input
729 // was opaque. Thus, all of these layout FPs disable that optimization.
730 std::unique_ptr<GrFragmentProcessor> fp;
731 SkTLazy<SkMatrix> matrix;
732 switch (shader.getType()) {
733 case SkTwoPointConicalGradient::Type::kStrip: {
734 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
735 uniform half r0_2;
736 half4 main(float2 p) {
737 half v = 1; // validation flag, set to negative to discard fragment later
738 float t = r0_2 - p.y * p.y;
739 if (t >= 0) {
740 t = p.x + sqrt(t);
741 } else {
742 v = -1;
743 }
744 return half4(half(t), v, 0, 0);
745 }
746 )");
747 float r0 = shader.getStartRadius() / shader.getCenterX1();
748 fp = GrSkSLFP::Make(effect, "TwoPointConicalStripLayout", /*inputFP=*/nullptr,
749 GrSkSLFP::OptFlags::kNone,
750 "r0_2", r0 * r0);
751 } break;
752
753 case SkTwoPointConicalGradient::Type::kRadial: {
754 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
755 uniform half r0;
756 uniform half lengthScale;
757 half4 main(float2 p) {
758 half v = 1; // validation flag, set to negative to discard fragment later
759 float t = length(p) * lengthScale - r0;
760 return half4(half(t), v, 0, 0);
761 }
762 )");
763 float dr = shader.getDiffRadius();
764 float r0 = shader.getStartRadius() / dr;
765 bool isRadiusIncreasing = dr >= 0;
766 fp = GrSkSLFP::Make(effect, "TwoPointConicalRadialLayout", /*inputFP=*/nullptr,
767 GrSkSLFP::OptFlags::kNone,
768 "r0", r0,
769 "lengthScale", isRadiusIncreasing ? 1.0f : -1.0f);
770
771 // GPU radial matrix is different from the original matrix, since we map the diff radius
772 // to have |dr| = 1, so manually compute the final gradient matrix here.
773
774 // Map center to (0, 0)
775 matrix.set(SkMatrix::Translate(-shader.getStartCenter().fX,
776 -shader.getStartCenter().fY));
777 // scale |diffRadius| to 1
778 matrix->postScale(1 / dr, 1 / dr);
779 } break;
780
781 case SkTwoPointConicalGradient::Type::kFocal: {
782 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
783 // Optimization flags, all specialized:
784 uniform int isRadiusIncreasing;
785 uniform int isFocalOnCircle;
786 uniform int isWellBehaved;
787 uniform int isSwapped;
788 uniform int isNativelyFocal;
789
790 uniform half invR1; // 1/r1
791 uniform half fx; // focalX = r0/(r0-r1)
792
793 half4 main(float2 p) {
794 float t = -1;
795 half v = 1; // validation flag, set to negative to discard fragment later
796
797 float x_t = -1;
798 if (bool(isFocalOnCircle)) {
799 x_t = dot(p, p) / p.x;
800 } else if (bool(isWellBehaved)) {
801 x_t = length(p) - p.x * invR1;
802 } else {
803 float temp = p.x * p.x - p.y * p.y;
804
805 // Only do sqrt if temp >= 0; this is significantly slower than checking
806 // temp >= 0 in the if statement that checks r(t) >= 0. But GPU may break if
807 // we sqrt a negative float. (Although I havevn't observed that on any
808 // devices so far, and the old approach also does sqrt negative value
809 // without a check.) If the performance is really critical, maybe we should
810 // just compute the area where temp and x_t are always valid and drop all
811 // these ifs.
812 if (temp >= 0) {
813 if (bool(isSwapped) || !bool(isRadiusIncreasing)) {
814 x_t = -sqrt(temp) - p.x * invR1;
815 } else {
816 x_t = sqrt(temp) - p.x * invR1;
817 }
818 }
819 }
820
821 // The final calculation of t from x_t has lots of static optimizations but only
822 // do them when x_t is positive (which can be assumed true if isWellBehaved is
823 // true)
824 if (!bool(isWellBehaved)) {
825 // This will still calculate t even though it will be ignored later in the
826 // pipeline to avoid a branch
827 if (x_t <= 0.0) {
828 v = -1;
829 }
830 }
831 if (bool(isRadiusIncreasing)) {
832 if (bool(isNativelyFocal)) {
833 t = x_t;
834 } else {
835 t = x_t + fx;
836 }
837 } else {
838 if (bool(isNativelyFocal)) {
839 t = -x_t;
840 } else {
841 t = -x_t + fx;
842 }
843 }
844
845 if (bool(isSwapped)) {
846 t = 1 - t;
847 }
848
849 return half4(half(t), v, 0, 0);
850 }
851 )");
852
853 const SkTwoPointConicalGradient::FocalData& focalData = shader.getFocalData();
854 bool isRadiusIncreasing = (1 - focalData.fFocalX) > 0,
855 isFocalOnCircle = focalData.isFocalOnCircle(),
856 isWellBehaved = focalData.isWellBehaved(),
857 isSwapped = focalData.isSwapped(),
858 isNativelyFocal = focalData.isNativelyFocal();
859
860 fp = GrSkSLFP::Make(effect, "TwoPointConicalFocalLayout", /*inputFP=*/nullptr,
861 GrSkSLFP::OptFlags::kNone,
862 "isRadiusIncreasing", GrSkSLFP::Specialize<int>(isRadiusIncreasing),
863 "isFocalOnCircle", GrSkSLFP::Specialize<int>(isFocalOnCircle),
864 "isWellBehaved", GrSkSLFP::Specialize<int>(isWellBehaved),
865 "isSwapped", GrSkSLFP::Specialize<int>(isSwapped),
866 "isNativelyFocal", GrSkSLFP::Specialize<int>(isNativelyFocal),
867 "invR1", 1.0f / focalData.fR1,
868 "fx", focalData.fFocalX);
869 } break;
870 }
871 return make_gradient(shader, args, std::move(fp), matrix.getMaybeNull());
Michael Ludwig8f685082018-09-12 15:23:01 -0400872}
873
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400874#if GR_TEST_UTILS
875RandomParams::RandomParams(SkRandom* random) {
876 // Set color count to min of 2 so that we don't trigger the const color optimization and make
877 // a non-gradient processor.
878 fColorCount = random->nextRangeU(2, kMaxRandomGradientColors);
879 fUseColors4f = random->nextBool();
880
881 // if one color, omit stops, otherwise randomly decide whether or not to
882 if (fColorCount == 1 || (fColorCount >= 2 && random->nextBool())) {
883 fStops = nullptr;
884 } else {
885 fStops = fStopStorage;
886 }
887
888 // if using SkColor4f, attach a random (possibly null) color space (with linear gamma)
889 if (fUseColors4f) {
890 fColorSpace = GrTest::TestColorSpace(random);
891 }
892
893 SkScalar stop = 0.f;
894 for (int i = 0; i < fColorCount; ++i) {
895 if (fUseColors4f) {
896 fColors4f[i].fR = random->nextUScalar1();
897 fColors4f[i].fG = random->nextUScalar1();
898 fColors4f[i].fB = random->nextUScalar1();
899 fColors4f[i].fA = random->nextUScalar1();
900 } else {
901 fColors[i] = random->nextU();
902 }
903 if (fStops) {
904 fStops[i] = stop;
905 stop = i < fColorCount - 1 ? stop + random->nextUScalar1() * (1.f - stop) : 1.f;
906 }
907 }
Mike Reedfae8fce2019-04-03 10:27:45 -0400908 fTileMode = static_cast<SkTileMode>(random->nextULessThan(kSkTileModeCount));
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400909}
910#endif
911
John Stilesa6841be2020-08-06 14:11:56 -0400912} // namespace GrGradientShader