xref: /aosp_15_r20/external/skia/src/gpu/ganesh/gradients/GrGradientShader.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 #include "src/gpu/ganesh/gradients/GrGradientShader.h"
8 
9 #include "include/core/SkAlphaType.h"
10 #include "include/core/SkBitmap.h"
11 #include "include/core/SkColorSpace.h"
12 #include "include/core/SkColorType.h"
13 #include "include/core/SkMatrix.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkSamplingOptions.h"
16 #include "include/core/SkString.h"
17 #include "include/core/SkTileMode.h"
18 #include "include/effects/SkGradientShader.h"
19 #include "include/effects/SkRuntimeEffect.h"
20 #include "include/gpu/GpuTypes.h"
21 #include "include/gpu/ganesh/GrBackendSurface.h"
22 #include "include/gpu/ganesh/GrRecordingContext.h"
23 #include "include/gpu/ganesh/GrTypes.h"
24 #include "include/private/SkColorData.h"
25 #include "include/private/base/SkAssert.h"
26 #include "include/private/base/SkDebug.h"
27 #include "include/private/base/SkMath.h"
28 #include "include/private/base/SkOnce.h"
29 #include "include/private/base/SkSpan_impl.h"
30 #include "include/private/base/SkTemplates.h"
31 #include "include/private/gpu/ganesh/GrTypesPriv.h"
32 #include "src/base/SkArenaAlloc.h"
33 #include "src/base/SkMathPriv.h"
34 #include "src/base/SkVx.h"
35 #include "src/core/SkColorSpacePriv.h"
36 #include "src/core/SkRasterPipeline.h"
37 #include "src/core/SkRasterPipelineOpContexts.h"
38 #include "src/core/SkRasterPipelineOpList.h"
39 #include "src/core/SkRuntimeEffectPriv.h"
40 #include "src/gpu/ganesh/GrCaps.h"
41 #include "src/gpu/ganesh/GrColorInfo.h"
42 #include "src/gpu/ganesh/GrColorSpaceXform.h"
43 #include "src/gpu/ganesh/GrFPArgs.h"
44 #include "src/gpu/ganesh/GrFragmentProcessor.h"
45 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
46 #include "src/gpu/ganesh/GrSamplerState.h"
47 #include "src/gpu/ganesh/GrShaderCaps.h"
48 #include "src/gpu/ganesh/SkGr.h"
49 #include "src/gpu/ganesh/effects/GrMatrixEffect.h"
50 #include "src/gpu/ganesh/effects/GrSkSLFP.h"
51 #include "src/gpu/ganesh/effects/GrTextureEffect.h"
52 #include "src/gpu/ganesh/gradients/GrGradientBitmapCache.h"
53 #include "src/shaders/SkShaderBase.h"
54 #include "src/shaders/gradients/SkGradientBaseShader.h"
55 
56 #include <algorithm>
57 #include <array>
58 #include <cstddef>
59 #include <tuple>
60 #include <utility>
61 
62 struct SkV4;
63 
64 #if defined(GPU_TEST_UTILS)
65 #include "src/base/SkRandom.h"
66 #include "src/gpu/ganesh/GrTestUtils.h"
67 #endif
68 
69 using namespace skia_private;
70 
71 using Vec4 = skvx::Vec<4, float>;
72 
73 // Intervals smaller than this (that aren't hard stops) on low-precision-only devices force us to
74 // use the textured gradient
75 static const SkScalar kLowPrecisionIntervalLimit = 0.01f;
76 
77 // Each cache entry costs 1K or 2K of RAM. Each bitmap will be 1x256 at either 32bpp or 64bpp.
78 static const int kMaxNumCachedGradientBitmaps = 32;
79 static const int kGradientTextureSize = 256;
80 
81 // NOTE: signature takes raw pointers to the color/pos arrays and a count to make it easy for
82 // MakeColorizer to transparently take care of hard stops at the end points of the gradient.
make_textured_colorizer(const SkPMColor4f * colors,const SkScalar * positions,int count,bool colorsAreOpaque,const SkGradientShader::Interpolation & interpolation,const SkColorSpace * intermediateColorSpace,const SkColorSpace * dstColorSpace,const GrFPArgs & args)83 static std::unique_ptr<GrFragmentProcessor> make_textured_colorizer(
84         const SkPMColor4f* colors,
85         const SkScalar* positions,
86         int count,
87         bool colorsAreOpaque,
88         const SkGradientShader::Interpolation& interpolation,
89         const SkColorSpace* intermediateColorSpace,
90         const SkColorSpace* dstColorSpace,
91         const GrFPArgs& args) {
92     static GrGradientBitmapCache gCache(kMaxNumCachedGradientBitmaps, kGradientTextureSize);
93 
94     // Use 8888 or F16, depending on the destination config.
95     // TODO: Use 1010102 for opaque gradients, at least if destination is 1010102?
96     SkColorType colorType = kRGBA_8888_SkColorType;
97     if (GrColorTypeIsWiderThan(args.fDstColorInfo->colorType(), 8)) {
98         auto f16Format = args.fContext->priv().caps()->getDefaultBackendFormat(
99                 GrColorType::kRGBA_F16, GrRenderable::kNo);
100         if (f16Format.isValid()) {
101             colorType = kRGBA_F16_SkColorType;
102         }
103     }
104     SkAlphaType alphaType = static_cast<bool>(interpolation.fInPremul) ? kPremul_SkAlphaType
105                                                                        : kUnpremul_SkAlphaType;
106 
107     SkBitmap bitmap;
108     gCache.getGradient(colors,
109                        positions,
110                        count,
111                        colorsAreOpaque,
112                        interpolation,
113                        intermediateColorSpace,
114                        dstColorSpace,
115                        colorType,
116                        alphaType,
117                        &bitmap);
118     SkASSERT(1 == bitmap.height() && SkIsPow2(bitmap.width()));
119     SkASSERT(bitmap.isImmutable());
120 
121     auto view = std::get<0>(GrMakeCachedBitmapProxyView(
122             args.fContext, bitmap, /*label=*/"MakeTexturedColorizer", skgpu::Mipmapped::kNo));
123     if (!view) {
124         SkDebugf("Gradient won't draw. Could not create texture.");
125         return nullptr;
126     }
127 
128     auto m = SkMatrix::Scale(view.width(), 1.f);
129     return GrTextureEffect::Make(std::move(view), alphaType, m, GrSamplerState::Filter::kLinear);
130 }
131 
make_single_interval_colorizer(const SkPMColor4f & start,const SkPMColor4f & end)132 static std::unique_ptr<GrFragmentProcessor> make_single_interval_colorizer(const SkPMColor4f& start,
133                                                                            const SkPMColor4f& end) {
134     static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader,
135         "uniform half4 start;"
136         "uniform half4 end;"
137         "half4 main(float2 coord) {"
138             // Clamping and/or wrapping was already handled by the parent shader so the output
139             // color is a simple lerp.
140             "return mix(start, end, half(coord.x));"
141         "}"
142     );
143     return GrSkSLFP::Make(effect, "SingleIntervalColorizer", /*inputFP=*/nullptr,
144                           GrSkSLFP::OptFlags::kNone,
145                           "start", start,
146                           "end", end);
147 }
148 
make_dual_interval_colorizer(const SkPMColor4f & c0,const SkPMColor4f & c1,const SkPMColor4f & c2,const SkPMColor4f & c3,float threshold)149 static std::unique_ptr<GrFragmentProcessor> make_dual_interval_colorizer(const SkPMColor4f& c0,
150                                                                          const SkPMColor4f& c1,
151                                                                          const SkPMColor4f& c2,
152                                                                          const SkPMColor4f& c3,
153                                                                          float threshold) {
154     static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader,
155         "uniform float4 scale[2];"
156         "uniform float4 bias[2];"
157         "uniform half threshold;"
158 
159         "half4 main(float2 coord) {"
160             "half t = half(coord.x);"
161 
162             "float4 s, b;"
163             "if (t < threshold) {"
164                 "s = scale[0];"
165                 "b = bias[0];"
166             "} else {"
167                 "s = scale[1];"
168                 "b = bias[1];"
169             "}"
170 
171             "return half4(t * s + b);"
172         "}"
173     );
174 
175     // Derive scale and biases from the 4 colors and threshold
176     Vec4 vc0 = Vec4::Load(c0.vec());
177     Vec4 vc1 = Vec4::Load(c1.vec());
178     Vec4 vc2 = Vec4::Load(c2.vec());
179     Vec4 vc3 = Vec4::Load(c3.vec());
180 
181     const Vec4 scale[2] = {(vc1 - vc0) / threshold,
182                            (vc3 - vc2) / (1 - threshold)};
183     const Vec4 bias[2]  = {vc0,
184                            vc2 - threshold * scale[1]};
185     return GrSkSLFP::Make(effect, "DualIntervalColorizer", /*inputFP=*/nullptr,
186                           GrSkSLFP::OptFlags::kNone,
187                           "scale", SkSpan(scale),
188                           "bias", SkSpan(bias),
189                           "threshold", threshold);
190 }
191 
192 // The "unrolled" colorizer contains hand-written nested ifs which perform a binary search.
193 // This works on ES2 hardware that doesn't support non-constant array indexes.
194 // However, to keep code size under control, we are limited to a small number of stops.
195 static constexpr int kMaxUnrolledColorCount    = 16;
196 static constexpr int kMaxUnrolledIntervalCount = kMaxUnrolledColorCount / 2;
197 
make_unrolled_colorizer(int intervalCount,const SkPMColor4f * scale,const SkPMColor4f * bias,SkRect thresholds1_7,SkRect thresholds9_13)198 static std::unique_ptr<GrFragmentProcessor> make_unrolled_colorizer(int intervalCount,
199                                                                     const SkPMColor4f* scale,
200                                                                     const SkPMColor4f* bias,
201                                                                     SkRect thresholds1_7,
202                                                                     SkRect thresholds9_13) {
203     SkASSERT(intervalCount >= 1 && intervalCount <= 8);
204 
205     static SkOnce                 once[kMaxUnrolledIntervalCount];
206     static const SkRuntimeEffect* effects[kMaxUnrolledIntervalCount];
207 
208     once[intervalCount - 1]([intervalCount] {
209         SkString sksl;
210 
211         // The 7 threshold positions that define the boundaries of the 8 intervals (excluding t = 0,
212         // and t = 1) are packed into two half4s instead of having up to 7 separate scalar uniforms.
213         // For low interval counts, the extra components are ignored in the shader, but the uniform
214         // simplification is worth it. It is assumed thresholds are provided in increasing value,
215         // mapped as:
216         //  - thresholds1_7.x = boundary between (0,1) and (2,3) -> 1_2
217         //  -              .y = boundary between (2,3) and (4,5) -> 3_4
218         //  -              .z = boundary between (4,5) and (6,7) -> 5_6
219         //  -              .w = boundary between (6,7) and (8,9) -> 7_8
220         //  - thresholds9_13.x = boundary between (8,9) and (10,11) -> 9_10
221         //  -               .y = boundary between (10,11) and (12,13) -> 11_12
222         //  -               .z = boundary between (12,13) and (14,15) -> 13_14
223         //  -               .w = unused
224         sksl.append("uniform half4 thresholds1_7, thresholds9_13;");
225 
226         // With the current hardstop detection threshold of 0.00024, the maximum scale and bias
227         // values will be on the order of 4k (since they divide by dt). That is well outside the
228         // precision capabilities of half floats, which can lead to inaccurate gradient calculations
229         sksl.appendf("uniform float4 scale[%d];", intervalCount);
230         sksl.appendf("uniform float4 bias[%d];", intervalCount);
231 
232         // Explicit binary search for the proper interval that t falls within. The interval
233         // count checks are constant expressions, which are then optimized to the minimal number
234         // of branches for the specific interval count.
235         sksl.appendf(
236         "half4 main(float2 coord) {"
237             "half t = half(coord.x);"
238             "float4 s, b;"
239             // thresholds1_7.w is mid point for intervals (0,7) and (8,15)
240             "if (%d <= 4 || t < thresholds1_7.w) {"
241                 // thresholds1_7.y is mid point for intervals (0,3) and (4,7)
242                 "if (%d <= 2 || t < thresholds1_7.y) {"
243                     // thresholds1_7.x is mid point for intervals (0,1) and (2,3)
244                     "if (%d <= 1 || t < thresholds1_7.x) {"
245                         "%s" // s = scale[0]; b = bias[0];
246                     "} else {"
247                         "%s" // s = scale[1]; b = bias[1];
248                     "}"
249                 "} else {"
250                     // thresholds1_7.z is mid point for intervals (4,5) and (6,7)
251                     "if (%d <= 3 || t < thresholds1_7.z) {"
252                         "%s" // s = scale[2]; b = bias[2];
253                     "} else {"
254                         "%s" // s = scale[3]; b = bias[3];
255                     "}"
256                 "}"
257             "} else {"
258                 // thresholds9_13.y is mid point for intervals (8,11) and (12,15)
259                 "if (%d <= 6 || t < thresholds9_13.y) {"
260                     // thresholds9_13.x is mid point for intervals (8,9) and (10,11)
261                     "if (%d <= 5 || t < thresholds9_13.x) {"
262                         "%s"
263                     "} else {"
264                         "%s" // s = scale[5]; b = bias[5];
265                     "}"
266                 "} else {"
267                     // thresholds9_13.z is mid point for intervals (12,13) and (14,15)
268                     "if (%d <= 7 || t < thresholds9_13.z) {"
269                         "%s" // s = scale[6]; b = bias[6];
270                     "} else {"
271                         "%s" // s = scale[7]; b = bias[7];
272                     "}"
273                 "}"
274             "}"
275             "return t * s + b;"
276         "}"
277         , intervalCount,
278               intervalCount,
279                 intervalCount,
280                   (intervalCount <= 0) ? "" : "s = scale[0]; b = bias[0];",
281                   (intervalCount <= 1) ? "" : "s = scale[1]; b = bias[1];",
282                 intervalCount,
283                   (intervalCount <= 2) ? "" : "s = scale[2]; b = bias[2];",
284                   (intervalCount <= 3) ? "" : "s = scale[3]; b = bias[3];",
285               intervalCount,
286                 intervalCount,
287                   (intervalCount <= 4) ? "" : "s = scale[4]; b = bias[4];",
288                   (intervalCount <= 5) ? "" : "s = scale[5]; b = bias[5];",
289                 intervalCount,
290                   (intervalCount <= 6) ? "" : "s = scale[6]; b = bias[6];",
291                   (intervalCount <= 7) ? "" : "s = scale[7]; b = bias[7];");
292 
293         auto result = SkRuntimeEffect::MakeForShader(std::move(sksl));
294         SkASSERTF(result.effect, "%s", result.errorText.c_str());
295         effects[intervalCount - 1] = result.effect.release();
296     });
297 
298     return GrSkSLFP::Make(effects[intervalCount - 1], "UnrolledBinaryColorizer",
299                           /*inputFP=*/nullptr, GrSkSLFP::OptFlags::kNone,
300                           "thresholds1_7", thresholds1_7,
301                           "thresholds9_13", thresholds9_13,
302                           "scale", SkSpan(scale, intervalCount),
303                           "bias", SkSpan(bias, intervalCount));
304 }
305 
306 // The "looping" colorizer uses a real loop to binary-search the array of gradient stops.
307 static constexpr int kMaxLoopingColorCount    = 128;
308 static constexpr int kMaxLoopingIntervalCount = kMaxLoopingColorCount / 2;
309 
make_looping_colorizer(int intervalCount,const SkPMColor4f * scale,const SkPMColor4f * bias,const SkScalar * thresholds)310 static std::unique_ptr<GrFragmentProcessor> make_looping_colorizer(int intervalCount,
311                                                                    const SkPMColor4f* scale,
312                                                                    const SkPMColor4f* bias,
313                                                                    const SkScalar* thresholds) {
314     SkASSERT(intervalCount >= 1 && intervalCount <= kMaxLoopingIntervalCount);
315     SkASSERT((intervalCount & 3) == 0);  // intervals are required to come in groups of four
316     int intervalChunks = intervalCount / 4;
317     int cacheIndex = (size_t)intervalChunks - 1;
318 
319     struct EffectCacheEntry {
320         SkOnce once;
321         const SkRuntimeEffect* effect;
322     };
323 
324     static EffectCacheEntry effectCache[kMaxLoopingIntervalCount / 4];
325     SkASSERT(cacheIndex >= 0 && cacheIndex < (int)std::size(effectCache));
326     EffectCacheEntry* cacheEntry = &effectCache[cacheIndex];
327 
328     cacheEntry->once([intervalCount, intervalChunks, cacheEntry] {
329         SkString sksl;
330 
331         // Binary search for the interval that `t` falls within. We can precalculate the number of
332         // loop iterations we need, and we know `t` will always be in range, so we can just loop a
333         // fixed number of times and can be guaranteed to have found the proper element.
334         //
335         // Threshold values are stored in half4s to keep them compact, so the last two rounds of
336         // binary search are hand-unrolled to allow them to use swizzles.
337         //
338         // Note that this colorizer is also designed to handle the case of exactly 4 intervals (a
339         // single chunk). In this case, the binary search for-loop will optimize away entirely, as
340         // it can be proven to execute zero times. We also optimize away the calculation of `4 *
341         // chunk` near the end via an if statement, as the result will always be in chunk 0.
342         int loopCount = SkNextLog2(intervalChunks);
343         sksl.appendf(
344         "#version 300\n" // important space to separate token.
345         "uniform float4 thresholds[%d];"
346         "uniform float4 scale[%d];"
347         "uniform float4 bias[%d];"
348 
349         "half4 main(float2 coord) {"
350             "float t = coord.x;"
351 
352             // Choose a chunk from thresholds via binary search in a loop.
353             "int low = 0;"
354             "int high = %d;"
355             "int chunk = %d;"
356             "for (int loop = 0; loop < %d; ++loop) {"
357                 "if (t < thresholds[chunk].w) {"
358                     "high = chunk;"
359                 "} else {"
360                     "low = chunk + 1;"
361                 "}"
362                 "chunk = (low + high) / 2;"
363             "}"
364 
365             // Choose the final position via explicit 4-way binary search.
366             "int pos;"
367             "if (t < thresholds[chunk].y) {"
368                 "pos = (t < thresholds[chunk].x) ? 0 : 1;"
369             "} else {"
370                 "pos = (t < thresholds[chunk].z) ? 2 : 3;"
371             "}"
372             "if (%d > 0) {"
373                 "pos += 4 * chunk;"
374             "}"
375             "return t * scale[pos] + bias[pos];"
376         "}"
377         , /* thresholds: */ intervalChunks,
378             /* scale: */ intervalCount,
379             /* bias: */ intervalCount,
380             /* high: */ intervalChunks - 1,
381             /* chunk: */ (intervalChunks - 1) / 2,
382             /* loopCount: */ loopCount,
383             /* if (loopCount > 0): */ loopCount);
384 
385         auto result = SkRuntimeEffect::MakeForShader(std::move(sksl));
386         SkASSERTF(result.effect, "%s", result.errorText.c_str());
387         cacheEntry->effect = result.effect.release();
388     });
389 
390     return GrSkSLFP::Make(cacheEntry->effect, "LoopingBinaryColorizer",
391                           /*inputFP=*/nullptr, GrSkSLFP::OptFlags::kNone,
392                           "thresholds", SkSpan((const SkV4*)thresholds, intervalChunks),
393                           "scale", SkSpan(scale, intervalCount),
394                           "bias", SkSpan(bias, intervalCount));
395 }
396 
397 // Converts an input array of {colors, positions} into an array of {scales, biases, thresholds}.
398 // The length of the result array may differ from the input due to hard-stops or empty intervals.
build_intervals(int inputLength,const SkPMColor4f * inColors,const SkScalar * inPositions,int outputLength,SkPMColor4f * outScales,SkPMColor4f * outBiases,SkScalar * outThresholds)399 int build_intervals(int inputLength,
400                     const SkPMColor4f* inColors,
401                     const SkScalar* inPositions,
402                     int outputLength,
403                     SkPMColor4f* outScales,
404                     SkPMColor4f* outBiases,
405                     SkScalar* outThresholds) {
406     // Depending on how the positions resolve into hard stops or regular stops, the number of
407     // intervals specified by the number of colors/positions can change. For instance, a plain
408     // 3 color gradient is two intervals, but a 4 color gradient with a hard stop is also
409     // two intervals. At the most extreme end, an 8 interval gradient made entirely of hard
410     // stops has 16 colors.
411     int intervalCount = 0;
412     for (int i = 0; i < inputLength - 1; i++) {
413         if (intervalCount >= outputLength) {
414             // Already reached our output limit, and haven't run out of color stops. This gradient
415             // cannot be represented without more intervals.
416             return 0;
417         }
418 
419         SkScalar t0 = inPositions[i];
420         SkScalar t1 = inPositions[i + 1];
421         SkScalar dt = t1 - t0;
422         // If the interval is empty, skip to the next interval. This will automatically create
423         // distinct hard stop intervals as needed. It also protects against malformed gradients
424         // that have repeated hard stops at the very beginning that are effectively unreachable.
425         if (SkScalarNearlyZero(dt)) {
426             continue;
427         }
428 
429         Vec4 c0 = Vec4::Load(inColors[i].vec());
430         Vec4 c1 = Vec4::Load(inColors[i + 1].vec());
431         Vec4 scale = (c1 - c0) / dt;
432         Vec4 bias = c0 - t0 * scale;
433 
434         scale.store(outScales + intervalCount);
435         bias.store(outBiases + intervalCount);
436         outThresholds[intervalCount] = t1;
437         intervalCount++;
438     }
439     return intervalCount;
440 }
441 
make_unrolled_binary_colorizer(const SkPMColor4f * colors,const SkScalar * positions,int count)442 static std::unique_ptr<GrFragmentProcessor> make_unrolled_binary_colorizer(
443         const SkPMColor4f* colors, const SkScalar* positions, int count) {
444     if (count > kMaxUnrolledColorCount) {
445         // Definitely cannot represent this gradient configuration
446         return nullptr;
447     }
448 
449     SkPMColor4f scales[kMaxUnrolledIntervalCount];
450     SkPMColor4f biases[kMaxUnrolledIntervalCount];
451     SkScalar thresholds[kMaxUnrolledIntervalCount] = {};
452     int intervalCount = build_intervals(count, colors, positions,
453                                         kMaxUnrolledIntervalCount, scales, biases, thresholds);
454     if (intervalCount <= 0) {
455         return nullptr;
456     }
457 
458     SkRect thresholds1_7  = {thresholds[0], thresholds[1], thresholds[2], thresholds[3]},
459            thresholds9_13 = {thresholds[4], thresholds[5], thresholds[6], 0.0};
460 
461     return make_unrolled_colorizer(intervalCount, scales, biases, thresholds1_7, thresholds9_13);
462 }
463 
make_looping_binary_colorizer(const SkPMColor4f * colors,const SkScalar * positions,int count)464 static std::unique_ptr<GrFragmentProcessor> make_looping_binary_colorizer(const SkPMColor4f* colors,
465                                                                           const SkScalar* positions,
466                                                                           int count) {
467     if (count > kMaxLoopingColorCount) {
468         // Definitely cannot represent this gradient configuration
469         return nullptr;
470     }
471 
472     SkPMColor4f scales[kMaxLoopingIntervalCount];
473     SkPMColor4f biases[kMaxLoopingIntervalCount];
474     SkScalar thresholds[kMaxLoopingIntervalCount] = {};
475     int intervalCount = build_intervals(count, colors, positions,
476                                         kMaxLoopingIntervalCount, scales, biases, thresholds);
477     if (intervalCount <= 0) {
478         return nullptr;
479     }
480 
481     // We round up the number of intervals to the next power of two. This reduces the number of
482     // unique shaders and doesn't require any additional GPU processing power, but this does waste a
483     // handful of uniforms.
484     int roundedSize = std::max(4, SkNextPow2(intervalCount));
485     SkASSERT(roundedSize <= kMaxLoopingIntervalCount);
486     for (; intervalCount < roundedSize; ++intervalCount) {
487         thresholds[intervalCount] = thresholds[intervalCount - 1];
488         scales[intervalCount] = scales[intervalCount - 1];
489         biases[intervalCount] = biases[intervalCount - 1];
490     }
491 
492     return make_looping_colorizer(intervalCount, scales, biases, thresholds);
493 }
494 
495 // Analyze the shader's color stops and positions and chooses an appropriate colorizer to represent
496 // the gradient.
make_uniform_colorizer(const SkPMColor4f * colors,const SkScalar * positions,int count,bool premul,const GrFPArgs & args)497 static std::unique_ptr<GrFragmentProcessor> make_uniform_colorizer(const SkPMColor4f* colors,
498                                                                    const SkScalar* positions,
499                                                                    int count,
500                                                                    bool premul,
501                                                                    const GrFPArgs& args) {
502     // If there are hard stops at the beginning or end, the first and/or last color should be
503     // ignored by the colorizer since it should only be used in a clamped border color. By detecting
504     // and removing these stops at the beginning, it makes optimizing the remaining color stops
505     // simpler.
506 
507     // SkGradientBaseShader guarantees that pos[0] == 0 by adding a default value.
508     bool bottomHardStop = SkScalarNearlyEqual(positions[0], positions[1]);
509     // The same is true for pos[end] == 1
510     bool topHardStop = SkScalarNearlyEqual(positions[count - 2], positions[count - 1]);
511 
512     if (bottomHardStop) {
513         colors++;
514         positions++;
515         count--;
516     }
517     if (topHardStop) {
518         count--;
519     }
520 
521     // Two remaining colors means a single interval from 0 to 1
522     // (but it may have originally been a 3 or 4 color gradient with 1-2 hard stops at the ends)
523     if (count == 2) {
524         return make_single_interval_colorizer(colors[0], colors[1]);
525     }
526 
527     const GrShaderCaps* caps = args.fContext->priv().caps()->shaderCaps();
528     auto intervalsExceedPrecisionLimit = [&]() -> bool {
529         // The remaining analytic colorizers use scale*t+bias, and the scale/bias values can become
530         // quite large when thresholds are close (but still outside the hardstop limit). If float
531         // isn't 32-bit, output can be incorrect if the thresholds are too close together. However,
532         // the analytic shaders are higher quality, so they can be used with lower precision
533         // hardware when the thresholds are not ill-conditioned.
534         if (!caps->fFloatIs32Bits) {
535             // Could run into problems. Check if thresholds are close together (with a limit of .01,
536             // so that scales will be less than 100, which leaves 4 decimals of precision on
537             // 16-bit).
538             for (int i = 0; i < count - 1; i++) {
539                 SkScalar dt = SkScalarAbs(positions[i] - positions[i + 1]);
540                 if (dt <= kLowPrecisionIntervalLimit && dt > SK_ScalarNearlyZero) {
541                     return true;
542                 }
543             }
544         }
545         return false;
546     };
547 
548     auto makeDualIntervalColorizer = [&]() -> std::unique_ptr<GrFragmentProcessor> {
549         // The dual-interval colorizer uses the same principles as the binary-search colorizer, but
550         // is limited to exactly 2 intervals.
551         if (count == 3) {
552             // Must be a dual interval gradient, where the middle point is at 1 and the
553             // two intervals share the middle color stop.
554             return make_dual_interval_colorizer(colors[0], colors[1],
555                                                 colors[1], colors[2],
556                                                 positions[1]);
557         }
558         if (count == 4 && SkScalarNearlyEqual(positions[1], positions[2])) {
559             // Two separate intervals that join at the same threshold position
560             return make_dual_interval_colorizer(colors[0], colors[1],
561                                                 colors[2], colors[3],
562                                                 positions[1]);
563         }
564         // The gradient can't be represented in only two intervals.
565         return nullptr;
566     };
567 
568     int binaryColorizerLimit = caps->fNonconstantArrayIndexSupport ? kMaxLoopingColorCount
569                                                                    : kMaxUnrolledColorCount;
570     if ((count <= binaryColorizerLimit) && !intervalsExceedPrecisionLimit()) {
571         // The dual-interval colorizer uses the same principles as the binary-search colorizer, but
572         // is limited to exactly 2 intervals.
573         std::unique_ptr<GrFragmentProcessor> colorizer = makeDualIntervalColorizer();
574         if (colorizer) {
575             return colorizer;
576         }
577         // Attempt to create an analytic colorizer that uses a binary-search loop.
578         colorizer = caps->fNonconstantArrayIndexSupport
579                             ? make_looping_binary_colorizer(colors, positions, count)
580                             : make_unrolled_binary_colorizer(colors, positions, count);
581         if (colorizer) {
582             return colorizer;
583         }
584     }
585 
586     // This gradient is too complex for our uniform colorizers. The calling code will fall back to
587     // creating a textured colorizer, instead.
588     return nullptr;
589 }
590 
591 // This top-level effect implements clamping on the layout coordinate and requires specifying the
592 // border colors that are used when outside the clamped boundary. Gradients with the
593 // SkTileMode::kClamp should use the colors at their first and last stop (after adding default stops
594 // for t=0,t=1) as the border color. This will automatically replicate the edge color, even when
595 // there is a hard stop.
596 //
597 // The SkTileMode::kDecal can be produced by specifying transparent black as the border colors,
598 // regardless of the gradient's stop colors.
make_clamped_gradient(std::unique_ptr<GrFragmentProcessor> colorizer,std::unique_ptr<GrFragmentProcessor> gradLayout,SkPMColor4f leftBorderColor,SkPMColor4f rightBorderColor,bool colorsAreOpaque)599 static std::unique_ptr<GrFragmentProcessor> make_clamped_gradient(
600         std::unique_ptr<GrFragmentProcessor> colorizer,
601         std::unique_ptr<GrFragmentProcessor> gradLayout,
602         SkPMColor4f leftBorderColor,
603         SkPMColor4f rightBorderColor,
604         bool colorsAreOpaque) {
605     static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader,
606         "uniform shader colorizer;"
607         "uniform shader gradLayout;"
608 
609         "uniform half4 leftBorderColor;"  // t < 0.0
610         "uniform half4 rightBorderColor;" // t > 1.0
611 
612         "uniform int layoutPreservesOpacity;"  // specialized
613 
614         "half4 main(float2 coord) {"
615             "half4 t = gradLayout.eval(coord);"
616             "half4 outColor;"
617 
618             // If t.x is below 0, use the left border color without invoking the child processor.
619             // If any t.x is above 1, use the right border color. Otherwise, t is in the [0, 1]
620             // range assumed by the colorizer FP, so delegate to the child processor.
621             "if (!bool(layoutPreservesOpacity) && t.y < 0) {"
622                 // layout has rejected this fragment (rely on sksl to remove this branch if the
623                 // layout FP preserves opacity is false)
624                 "outColor = half4(0);"
625             "} else if (t.x < 0) {"
626                 "outColor = leftBorderColor;"
627             "} else if (t.x > 1.0) {"
628                 "outColor = rightBorderColor;"
629             "} else {"
630                 // Always sample from (x, 0), discarding y, since the layout FP can use y as a
631                 // side-channel.
632                 "outColor = colorizer.eval(t.x0);"
633             "}"
634             "return outColor;"
635         "}"
636     );
637 
638     // If the layout does not preserve opacity, remove the opaque optimization,
639     // but otherwise respect the provided color opacity state (which should take
640     // into account the opacity of the border colors).
641     bool layoutPreservesOpacity = gradLayout->preservesOpaqueInput();
642     GrSkSLFP::OptFlags optFlags = GrSkSLFP::OptFlags::kCompatibleWithCoverageAsAlpha;
643     if (colorsAreOpaque && layoutPreservesOpacity) {
644         optFlags |= GrSkSLFP::OptFlags::kPreservesOpaqueInput;
645     }
646 
647     return GrSkSLFP::Make(effect, "ClampedGradient", /*inputFP=*/nullptr, optFlags,
648                           "colorizer", GrSkSLFP::IgnoreOptFlags(std::move(colorizer)),
649                           "gradLayout", GrSkSLFP::IgnoreOptFlags(std::move(gradLayout)),
650                           "leftBorderColor", leftBorderColor,
651                           "rightBorderColor", rightBorderColor,
652                           "layoutPreservesOpacity",
653                               GrSkSLFP::Specialize<int>(layoutPreservesOpacity));
654 }
655 
make_tiled_gradient(const GrFPArgs & args,std::unique_ptr<GrFragmentProcessor> colorizer,std::unique_ptr<GrFragmentProcessor> gradLayout,bool mirror,bool colorsAreOpaque)656 static std::unique_ptr<GrFragmentProcessor> make_tiled_gradient(
657         const GrFPArgs& args,
658         std::unique_ptr<GrFragmentProcessor> colorizer,
659         std::unique_ptr<GrFragmentProcessor> gradLayout,
660         bool mirror,
661         bool colorsAreOpaque) {
662     static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader,
663         "uniform shader colorizer;"
664         "uniform shader gradLayout;"
665 
666         "uniform int mirror;"                  // specialized
667         "uniform int layoutPreservesOpacity;"  // specialized
668         "uniform int useFloorAbsWorkaround;"   // specialized
669 
670         "half4 main(float2 coord) {"
671             "float4 t = gradLayout.eval(coord);"
672 
673             "if (!bool(layoutPreservesOpacity) && t.y < 0) {"
674                 // layout has rejected this fragment (rely on sksl to remove this branch if the
675                 // layout FP preserves opacity is false)
676                 "return half4(0);"
677             "} else {"
678                 "if (bool(mirror)) {"
679                     "float t_1 = t.x - 1;"
680                     "float tiled_t = t_1 - 2 * floor(t_1 * 0.5) - 1;"
681                     "if (bool(useFloorAbsWorkaround)) {"
682                         // At this point the expected value of tiled_t should between -1 and 1, so
683                         // this clamp has no effect other than to break up the floor and abs calls
684                         // and make sure the compiler doesn't merge them back together.
685                         "tiled_t = clamp(tiled_t, -1, 1);"
686                     "}"
687                     "t.x = abs(tiled_t);"
688                 "} else {"
689                     // Simple repeat mode
690                     "t.x = fract(t.x);"
691                 "}"
692 
693                 // Always sample from (x, 0), discarding y, since the layout FP can use y as a
694                 // side-channel.
695                 "half4 outColor = colorizer.eval(t.x0);"
696                 "return outColor;"
697             "}"
698         "}"
699     );
700 
701     // If the layout does not preserve opacity, remove the opaque optimization,
702     // but otherwise respect the provided color opacity state (which should take
703     // into account the opacity of the border colors).
704     bool layoutPreservesOpacity = gradLayout->preservesOpaqueInput();
705     GrSkSLFP::OptFlags optFlags = GrSkSLFP::OptFlags::kCompatibleWithCoverageAsAlpha;
706     if (colorsAreOpaque && layoutPreservesOpacity) {
707         optFlags |= GrSkSLFP::OptFlags::kPreservesOpaqueInput;
708     }
709     const bool useFloorAbsWorkaround =
710             args.fContext->priv().caps()->shaderCaps()->fMustDoOpBetweenFloorAndAbs;
711 
712     return GrSkSLFP::Make(effect, "TiledGradient", /*inputFP=*/nullptr, optFlags,
713                           "colorizer", GrSkSLFP::IgnoreOptFlags(std::move(colorizer)),
714                           "gradLayout", GrSkSLFP::IgnoreOptFlags(std::move(gradLayout)),
715                           "mirror", GrSkSLFP::Specialize<int>(mirror),
716                           "layoutPreservesOpacity",
717                                 GrSkSLFP::Specialize<int>(layoutPreservesOpacity),
718                           "useFloorAbsWorkaround",
719                                 GrSkSLFP::Specialize<int>(useFloorAbsWorkaround));
720 }
721 
make_interpolated_to_dst(std::unique_ptr<GrFragmentProcessor> gradient,const SkGradientShader::Interpolation & interpolation,SkColorSpace * intermediateColorSpace,const GrColorInfo & dstInfo,bool allOpaque)722 static std::unique_ptr<GrFragmentProcessor> make_interpolated_to_dst(
723         std::unique_ptr<GrFragmentProcessor> gradient,
724         const SkGradientShader::Interpolation& interpolation,
725         SkColorSpace* intermediateColorSpace,
726         const GrColorInfo& dstInfo,
727         bool allOpaque) {
728     using ColorSpace = SkGradientShader::Interpolation::ColorSpace;
729 
730     // If these values change, you will need to edit sksl_shared
731     static_assert(static_cast<int>(ColorSpace::kDestination)   == 0);
732     static_assert(static_cast<int>(ColorSpace::kSRGBLinear)    == 1);
733     static_assert(static_cast<int>(ColorSpace::kLab)           == 2);
734     static_assert(static_cast<int>(ColorSpace::kOKLab)         == 3);
735     static_assert(static_cast<int>(ColorSpace::kOKLabGamutMap) == 4);
736     static_assert(static_cast<int>(ColorSpace::kLCH)           == 5);
737     static_assert(static_cast<int>(ColorSpace::kOKLCH)         == 6);
738     static_assert(static_cast<int>(ColorSpace::kOKLCHGamutMap) == 7);
739     static_assert(static_cast<int>(ColorSpace::kSRGB)          == 8);
740     static_assert(static_cast<int>(ColorSpace::kHSL)           == 9);
741     static_assert(static_cast<int>(ColorSpace::kHWB)           == 10);
742 
743     static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter,
744         "uniform int colorSpace;"    // specialized
745         "uniform int do_unpremul;"   // specialized
746 
747         "half4 main(half4 color) {"
748             "return $interpolated_to_rgb_unpremul(color, colorSpace, do_unpremul);"
749         "}"
750     );
751 
752     // Are we interpreting premul colors? We use this later to decide if we need to inject a final
753     // premultiplication step.
754     bool inputPremul = static_cast<bool>(interpolation.fInPremul);
755 
756     switch (interpolation.fColorSpace) {
757         case ColorSpace::kLab:
758         case ColorSpace::kOKLab:
759         case ColorSpace::kOKLabGamutMap:
760         case ColorSpace::kLCH:
761         case ColorSpace::kOKLCH:
762         case ColorSpace::kOKLCHGamutMap:
763         case ColorSpace::kHSL:
764         case ColorSpace::kHWB:
765             // In these exotic spaces, unpremul the colors if necessary (no need to do this if
766             // they're all opaque), and then convert them to the intermediate SkColorSpace
767             gradient = GrSkSLFP::Make(effect, "GradientCS", std::move(gradient),
768                                       GrSkSLFP::OptFlags::kAll,
769                                       "colorSpace", GrSkSLFP::Specialize<int>(
770                                         static_cast<int>(interpolation.fColorSpace)),
771                                       "do_unpremul", GrSkSLFP::Specialize<int>(
772                                         inputPremul && !allOpaque));
773             // We just forced the colors back to unpremul. Remember that for below
774             inputPremul = false;
775             break;
776         default:
777             break;
778     }
779 
780     // Now transform from intermediate to destination color space. There are two tricky things here:
781     // 1) Normally, we'd pass dstInfo to the transform effect. However, if someone is rendering to
782     //    a non-color managed surface (nullptr dst color space), and they chose to interpolate in
783     //    any of the exotic spaces, that transform would do nothing, and leave the colors in
784     //    whatever intermediate space we chose. That could even be something like XYZ, which will
785     //    produce nonsense. So, in this particular case, we break Skia's rules, and treat a null
786     //    destination as sRGB.
787     SkColorSpace* dstColorSpace = dstInfo.colorSpace() ? dstInfo.colorSpace() : sk_srgb_singleton();
788 
789     // 2) Alpha type: We already tweaked our idea of "inputPremul" above -- if we interpolated in a
790     //    non-RGB space, then we had to unpremul the colors to get proper conversion back to RGB.
791     //    Our final goal is to emit premul colors, but under certain conditions we don't need to do
792     //    anything to achieve that: i.e. its interpolating already premul colors (inputPremul) or
793     //    all the colors have a = 1, in which case premul is a no op. Note that this allOpaque check
794     //    is more permissive than SkGradientBaseShader's isOpaque(), since we can optimize away the
795     //    make-premul op for two point conical gradients (which report false for isOpaque).
796     SkAlphaType intermediateAlphaType = inputPremul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
797     SkAlphaType dstAlphaType = kPremul_SkAlphaType;
798 
799     // If all the colors were opaque, then we don't need to do any premultiplication. We describe
800     // all the colors as *unpremul*, though. That will eliminate any extra unpremul/premul pair
801     // that would be injected if we have to do a color-space conversion here.
802     if (allOpaque) {
803         intermediateAlphaType = dstAlphaType = kUnpremul_SkAlphaType;
804     }
805 
806     return GrColorSpaceXformEffect::Make(std::move(gradient),
807                                          intermediateColorSpace, intermediateAlphaType,
808                                          dstColorSpace, dstAlphaType);
809 }
810 
811 namespace GrGradientShader {
812 
813 /**
814  * Produces an FP that muls its input coords by the inverse of the pending matrix and then
815  * samples the passed FP with those coordinates. 'postInv' is an additional matrix to
816  * post-apply to the inverted pending matrix. If the pending matrix is not invertible the
817  * GrFPResult's bool will be false and the passed FP will be returned to the caller in the
818  * GrFPResult.
819  */
apply_matrix(std::unique_ptr<GrFragmentProcessor> fp,const SkShaders::MatrixRec & rec,const SkMatrix & postInv)820 static GrFPResult apply_matrix(std::unique_ptr<GrFragmentProcessor> fp,
821                                const SkShaders::MatrixRec& rec,
822                                const SkMatrix& postInv) {
823     auto [total, ok] = rec.applyForFragmentProcessor(postInv);
824     if (!ok) {
825         return {false, std::move(fp)};
826     }
827     // GrMatrixEffect returns 'fp' if total worked out to identity.
828     return {true, GrMatrixEffect::Make(total, std::move(fp))};
829 }
830 
831 // Combines the colorizer and layout with an appropriately configured top-level effect based on the
832 // gradient's tile mode
MakeGradientFP(const SkGradientBaseShader & shader,const GrFPArgs & args,const SkShaders::MatrixRec & mRec,std::unique_ptr<GrFragmentProcessor> layout,const SkMatrix * overrideMatrix)833 std::unique_ptr<GrFragmentProcessor> MakeGradientFP(const SkGradientBaseShader& shader,
834                                                     const GrFPArgs& args,
835                                                     const SkShaders::MatrixRec& mRec,
836                                                     std::unique_ptr<GrFragmentProcessor> layout,
837                                                     const SkMatrix* overrideMatrix) {
838     // No shader is possible if a layout couldn't be created, e.g. a layout-specific Make() returned
839     // null.
840     if (layout == nullptr) {
841         return nullptr;
842     }
843 
844     // Some two-point conical gradients use a custom matrix here. Otherwise, use
845     // SkGradientBaseShader's matrix;
846     if (!overrideMatrix) {
847         overrideMatrix = &shader.getGradientMatrix();
848     }
849     bool success;
850     std::tie(success, layout) = apply_matrix(std::move(layout), mRec, *overrideMatrix);
851     if (!success) {
852         return nullptr;
853     }
854 
855     // Convert all colors into destination space and into SkPMColor4fs, and handle
856     // premul issues depending on the interpolation mode.
857     //
858     // SkGradientShader stores positions implicitly when they are evenly spaced, but the getPos()
859     // implementation performs a branch for every position index. Since the shader conversion
860     // requires lots of position tests, instruct the xformer to calculate all of the positions up
861     // front if needed.
862     SkColor4fXformer xformedColors(
863             &shader, args.fDstColorInfo->colorSpace(), /*forceExplicitPositions=*/true);
864     const SkPMColor4f* colors = xformedColors.fColors.begin();
865     const SkScalar* positions = xformedColors.fPositions;
866     const int colorCount = xformedColors.fColors.size();
867 
868     bool allOpaque = true;
869     for (int i = 0; i < colorCount; i++) {
870         if (allOpaque && !SkScalarNearlyEqual(colors[i].fA, 1.0)) {
871             allOpaque = false;
872         }
873     }
874 
875     // All gradients are colorized the same way, regardless of layout
876     std::unique_ptr<GrFragmentProcessor> colorizer = make_uniform_colorizer(
877             colors, positions, colorCount, shader.interpolateInPremul(), args);
878 
879     if (colorizer) {
880         // If we made a uniform colorizer, wrap it in a conversion from interpolated space to
881         // destination. This also applies any final premultiplication.
882         colorizer = make_interpolated_to_dst(std::move(colorizer),
883                                              shader.fInterpolation,
884                                              xformedColors.fIntermediateColorSpace.get(),
885                                              *args.fDstColorInfo,
886                                              allOpaque);
887     } else {
888         // If we failed to make a uniform colorizer, we need to rasterize the gradient to a
889         // texture (which can handle arbitrarily complex gradients). This method directly encodes
890         // the result of the interpolated-to-dst into the texture, so we skip the wrapper FP above.
891         //
892         // Also, note that the texture technique has limited sampling resolution, and always blurs
893         // hard-stops.)
894         colorizer = make_textured_colorizer(colors,
895                                             positions,
896                                             colorCount,
897                                             allOpaque,
898                                             shader.fInterpolation,
899                                             xformedColors.fIntermediateColorSpace.get(),
900                                             args.fDstColorInfo->colorSpace(),
901                                             args);
902     }
903 
904     if (colorizer == nullptr) {
905         return nullptr;
906     }
907 
908     // If interpolation space is different than destination, wrap the colorizer in a conversion.
909     // This also handles any final premultiplication, etc.
910 
911     // All tile modes are supported (unless something was added to SkShader)
912     std::unique_ptr<GrFragmentProcessor> gradient;
913     switch(shader.getTileMode()) {
914         case SkTileMode::kRepeat:
915             gradient = make_tiled_gradient(args, std::move(colorizer), std::move(layout),
916                                            /* mirror */ false, allOpaque);
917             break;
918         case SkTileMode::kMirror:
919             gradient = make_tiled_gradient(args, std::move(colorizer), std::move(layout),
920                                            /* mirror */ true, allOpaque);
921             break;
922         case SkTileMode::kClamp: {
923             // For the clamped mode, the border colors are the first and last colors, corresponding
924             // to t=0 and t=1, because SkGradientBaseShader enforces that by adding color stops as
925             // appropriate. If there is a hard stop, this grabs the expected outer colors for the
926             // border.
927 
928             // However, we need to finish converting to destination color space. (These are still
929             // in the interpolated color space).
930             SkPMColor4f borderColors[2] = { colors[0], colors[colorCount - 1] };
931             SkArenaAlloc alloc(/*firstHeapAllocation=*/0);
932             SkRasterPipeline p(&alloc);
933             SkRasterPipeline_MemoryCtx ctx = { borderColors, 0 };
934 
935             p.append(SkRasterPipelineOp::load_f32, &ctx);
936             SkGradientBaseShader::AppendInterpolatedToDstStages(
937                     &p,
938                     &alloc,
939                     allOpaque,
940                     shader.fInterpolation,
941                     xformedColors.fIntermediateColorSpace.get(),
942                     args.fDstColorInfo->colorSpace());
943             p.append(SkRasterPipelineOp::store_f32, &ctx);
944             p.run(0, 0, 2, 1);
945 
946             gradient = make_clamped_gradient(std::move(colorizer), std::move(layout),
947                                              borderColors[0], borderColors[1], allOpaque);
948             break;
949         }
950         case SkTileMode::kDecal:
951             // Even if the gradient colors are opaque, the decal borders are transparent so
952             // disable that optimization
953             gradient = make_clamped_gradient(std::move(colorizer), std::move(layout),
954                                              SK_PMColor4fTRANSPARENT, SK_PMColor4fTRANSPARENT,
955                                              /* colorsAreOpaque */ false);
956             break;
957     }
958 
959     return gradient;
960 }
961 
MakeLinear(const SkLinearGradient & shader,const GrFPArgs & args,const SkShaders::MatrixRec & mRec)962 std::unique_ptr<GrFragmentProcessor> MakeLinear(const SkLinearGradient& shader,
963                                                 const GrFPArgs& args,
964                                                 const SkShaders::MatrixRec& mRec) {
965     // We add a tiny delta to t. When gradient stops are set up so that a hard stop in a vertically
966     // or horizontally oriented gradient falls exactly at a column or row of pixel centers we can
967     // get slightly different interpolated t values along the column/row. By adding the delta
968     // we will consistently get the color to the "right" of the stop. Of course if the hard stop
969     // falls at X.5 - delta then we still could get inconsistent results, but that is much less
970     // likely. crbug.com/938592
971     // If/when we add filtering of the gradient this can be removed.
972     static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader,
973         "half4 main(float2 coord) {"
974             "return half4(half(coord.x) + 0.00001, 1, 0, 0);" // y = 1 for always valid
975         "}"
976     );
977     // The linear gradient never rejects a pixel so it doesn't change opacity
978     auto fp = GrSkSLFP::Make(effect, "LinearLayout", /*inputFP=*/nullptr,
979                              GrSkSLFP::OptFlags::kPreservesOpaqueInput);
980     return MakeGradientFP(shader, args, mRec, std::move(fp));
981 }
982 
983 #if defined(GPU_TEST_UTILS)
RandomParams(SkRandom * random)984 RandomParams::RandomParams(SkRandom* random) {
985     // Set color count to min of 2 so that we don't trigger the const color optimization and make
986     // a non-gradient processor.
987     fColorCount = random->nextRangeU(2, kMaxRandomGradientColors);
988     fUseColors4f = random->nextBool();
989 
990     // if one color, omit stops, otherwise randomly decide whether or not to
991     if (fColorCount == 1 || (fColorCount >= 2 && random->nextBool())) {
992         fStops = nullptr;
993     } else {
994         fStops = fStopStorage;
995     }
996 
997     // if using SkColor4f, attach a random (possibly null) color space (with linear gamma)
998     if (fUseColors4f) {
999         fColorSpace = GrTest::TestColorSpace(random);
1000     }
1001 
1002     SkScalar stop = 0.f;
1003     for (int i = 0; i < fColorCount; ++i) {
1004         if (fUseColors4f) {
1005             fColors4f[i].fR = random->nextUScalar1();
1006             fColors4f[i].fG = random->nextUScalar1();
1007             fColors4f[i].fB = random->nextUScalar1();
1008             fColors4f[i].fA = random->nextUScalar1();
1009         } else {
1010             fColors[i] = random->nextU();
1011         }
1012         if (fStops) {
1013             fStops[i] = stop;
1014             stop = i < fColorCount - 1 ? stop + random->nextUScalar1() * (1.f - stop) : 1.f;
1015         }
1016     }
1017     fTileMode = static_cast<SkTileMode>(random->nextULessThan(kSkTileModeCount));
1018 }
1019 #endif
1020 
1021 }  // namespace GrGradientShader
1022