blob: cf1438f0303fe53c96133bd04214d464a4831de0 [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
John Stiles6030e0a2021-10-08 16:23:05 -040023using Vec4 = skvx::Vec<4, float>;
24
Michael Ludwig72535fb2018-09-28 11:53:32 -040025// Intervals smaller than this (that aren't hard stops) on low-precision-only devices force us to
26// use the textured gradient
27static const SkScalar kLowPrecisionIntervalLimit = 0.01f;
28
Michael Ludwiga7914d32018-09-14 09:47:21 -040029// Each cache entry costs 1K or 2K of RAM. Each bitmap will be 1x256 at either 32bpp or 64bpp.
30static const int kMaxNumCachedGradientBitmaps = 32;
31static const int kGradientTextureSize = 256;
32
33// NOTE: signature takes raw pointers to the color/pos arrays and a count to make it easy for
34// MakeColorizer to transparently take care of hard stops at the end points of the gradient.
Brian Osman021ed512018-10-16 15:19:44 -040035static std::unique_ptr<GrFragmentProcessor> make_textured_colorizer(const SkPMColor4f* colors,
Michael Ludwiga7914d32018-09-14 09:47:21 -040036 const SkScalar* positions, int count, bool premul, const GrFPArgs& args) {
37 static GrGradientBitmapCache gCache(kMaxNumCachedGradientBitmaps, kGradientTextureSize);
38
39 // Use 8888 or F16, depending on the destination config.
40 // TODO: Use 1010102 for opaque gradients, at least if destination is 1010102?
41 SkColorType colorType = kRGBA_8888_SkColorType;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040042 if (GrColorTypeIsWiderThan(args.fDstColorInfo->colorType(), 8)) {
Greg Daniel7bfc9132019-08-14 14:23:53 -040043 auto f16Format = args.fContext->priv().caps()->getDefaultBackendFormat(
44 GrColorType::kRGBA_F16, GrRenderable::kNo);
45 if (f16Format.isValid()) {
46 colorType = kRGBA_F16_SkColorType;
47 }
Michael Ludwiga7914d32018-09-14 09:47:21 -040048 }
49 SkAlphaType alphaType = premul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
50
51 SkBitmap bitmap;
52 gCache.getGradient(colors, positions, count, colorType, alphaType, &bitmap);
53 SkASSERT(1 == bitmap.height() && SkIsPow2(bitmap.width()));
54 SkASSERT(bitmap.isImmutable());
55
Brian Salomon27c42022021-04-28 12:39:21 -040056 auto view = std::get<0>(GrMakeCachedBitmapProxyView(args.fContext, bitmap, GrMipmapped::kNo));
57 if (!view) {
Michael Ludwiga7914d32018-09-14 09:47:21 -040058 SkDebugf("Gradient won't draw. Could not create texture.");
59 return nullptr;
60 }
John Stiles1cf15932020-07-31 16:08:24 -040061
Brian Salomon17473752020-06-19 09:53:50 -040062 auto m = SkMatrix::Scale(view.width(), 1.f);
John Stiles1cf15932020-07-31 16:08:24 -040063 return GrTextureEffect::Make(std::move(view), alphaType, m, GrSamplerState::Filter::kLinear);
Michael Ludwiga7914d32018-09-14 09:47:21 -040064}
Michael Ludwig4f94ef62018-09-12 15:22:16 -040065
Brian Osman65b45972021-06-25 15:58:43 +000066
67static std::unique_ptr<GrFragmentProcessor> make_single_interval_colorizer(const SkPMColor4f& start,
68 const SkPMColor4f& end) {
69 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
70 uniform half4 start;
71 uniform half4 end;
72 half4 main(float2 coord) {
73 // Clamping and/or wrapping was already handled by the parent shader so the output
74 // color is a simple lerp.
75 return mix(start, end, half(coord.x));
76 }
77 )");
78 return GrSkSLFP::Make(effect, "SingleIntervalColorizer", /*inputFP=*/nullptr,
79 GrSkSLFP::OptFlags::kNone,
80 "start", start,
81 "end", end);
82}
83
84static std::unique_ptr<GrFragmentProcessor> make_dual_interval_colorizer(const SkPMColor4f& c0,
85 const SkPMColor4f& c1,
86 const SkPMColor4f& c2,
87 const SkPMColor4f& c3,
88 float threshold) {
89 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
John Stiles59b5f522021-10-07 15:27:37 -040090 uniform float4 scale[2];
91 uniform float4 bias[2];
Brian Osman65b45972021-06-25 15:58:43 +000092 uniform half threshold;
93
94 half4 main(float2 coord) {
95 half t = half(coord.x);
96
John Stiles59b5f522021-10-07 15:27:37 -040097 float4 s, b;
Brian Osman65b45972021-06-25 15:58:43 +000098 if (t < threshold) {
John Stiles59b5f522021-10-07 15:27:37 -040099 s = scale[0];
100 b = bias[0];
Brian Osman65b45972021-06-25 15:58:43 +0000101 } else {
John Stiles59b5f522021-10-07 15:27:37 -0400102 s = scale[1];
103 b = bias[1];
Brian Osman65b45972021-06-25 15:58:43 +0000104 }
105
John Stiles59b5f522021-10-07 15:27:37 -0400106 return half4(t * s + b);
Brian Osman65b45972021-06-25 15:58:43 +0000107 }
108 )");
109
Brian Osman65b45972021-06-25 15:58:43 +0000110 // Derive scale and biases from the 4 colors and threshold
John Stiles6030e0a2021-10-08 16:23:05 -0400111 Vec4 vc0 = Vec4::Load(c0.vec());
112 Vec4 vc1 = Vec4::Load(c1.vec());
113 Vec4 vc2 = Vec4::Load(c2.vec());
114 Vec4 vc3 = Vec4::Load(c3.vec());
Brian Osman65b45972021-06-25 15:58:43 +0000115
John Stiles6030e0a2021-10-08 16:23:05 -0400116 const Vec4 scale[2] = {(vc1 - vc0) / threshold,
John Stiles59b5f522021-10-07 15:27:37 -0400117 (vc3 - vc2) / (1 - threshold)};
John Stiles6030e0a2021-10-08 16:23:05 -0400118 const Vec4 bias[2] = {vc0,
John Stiles59b5f522021-10-07 15:27:37 -0400119 vc2 - threshold * scale[1]};
Brian Osman65b45972021-06-25 15:58:43 +0000120 return GrSkSLFP::Make(effect, "DualIntervalColorizer", /*inputFP=*/nullptr,
121 GrSkSLFP::OptFlags::kNone,
John Stiles59b5f522021-10-07 15:27:37 -0400122 "scale", SkMakeSpan(scale),
123 "bias", SkMakeSpan(bias),
Brian Osman65b45972021-06-25 15:58:43 +0000124 "threshold", threshold);
125}
126
John Stiles6030e0a2021-10-08 16:23:05 -0400127// The "unrolled" colorizer contains hand-written nested ifs which perform a binary search.
128// This works on ES2 hardware that doesn't support non-constant array indexes.
129// However, to keep code size under control, we are limited to a small number of stops.
Brian Osman07bf3882021-07-01 12:56:23 -0400130static constexpr int kMaxUnrolledColorCount = 16;
131static constexpr int kMaxUnrolledIntervalCount = 8;
132
133static std::unique_ptr<GrFragmentProcessor> make_unrolled_colorizer(int intervalCount,
134 const SkPMColor4f* scale,
135 const SkPMColor4f* bias,
136 SkRect thresholds1_7,
137 SkRect thresholds9_13) {
138 SkASSERT(intervalCount >= 1 && intervalCount <= 8);
139
140 static SkOnce once[kMaxUnrolledIntervalCount];
141 static sk_sp<SkRuntimeEffect> effects[kMaxUnrolledIntervalCount];
142
143 once[intervalCount - 1]([intervalCount] {
144 SkString sksl;
145
146 // The 7 threshold positions that define the boundaries of the 8 intervals (excluding t = 0,
147 // and t = 1) are packed into two half4s instead of having up to 7 separate scalar uniforms.
148 // For low interval counts, the extra components are ignored in the shader, but the uniform
149 // simplification is worth it. It is assumed thresholds are provided in increasing value,
150 // mapped as:
151 // - thresholds1_7.x = boundary between (0,1) and (2,3) -> 1_2
152 // - .y = boundary between (2,3) and (4,5) -> 3_4
153 // - .z = boundary between (4,5) and (6,7) -> 5_6
154 // - .w = boundary between (6,7) and (8,9) -> 7_8
155 // - thresholds9_13.x = boundary between (8,9) and (10,11) -> 9_10
156 // - .y = boundary between (10,11) and (12,13) -> 11_12
157 // - .z = boundary between (12,13) and (14,15) -> 13_14
158 // - .w = unused
159 sksl.append("uniform half4 thresholds1_7, thresholds9_13;");
160
161 // With the current hardstop detection threshold of 0.00024, the maximum scale and bias
162 // values will be on the order of 4k (since they divide by dt). That is well outside the
163 // precision capabilities of half floats, which can lead to inaccurate gradient calculations
John Stiles63727522021-10-07 11:20:50 -0400164 sksl.appendf("uniform float4 scale[%d];", intervalCount);
165 sksl.appendf("uniform float4 bias[%d];", intervalCount);
Brian Osman07bf3882021-07-01 12:56:23 -0400166
John Stiles63727522021-10-07 11:20:50 -0400167 // Explicit binary search for the proper interval that t falls within. The interval
168 // count checks are constant expressions, which are then optimized to the minimal number
169 // of branches for the specific interval count.
Brian Osman07bf3882021-07-01 12:56:23 -0400170 sksl.appendf(R"(
John Stiles63727522021-10-07 11:20:50 -0400171 half4 main(float2 coord) {
172 half t = half(coord.x);
173 float4 s, b;
Brian Osman07bf3882021-07-01 12:56:23 -0400174 // thresholds1_7.w is mid point for intervals (0,7) and (8,15)
175 if (%d <= 4 || t < thresholds1_7.w) {
176 // thresholds1_7.y is mid point for intervals (0,3) and (4,7)
177 if (%d <= 2 || t < thresholds1_7.y) {
178 // thresholds1_7.x is mid point for intervals (0,1) and (2,3)
179 if (%d <= 1 || t < thresholds1_7.x) {
John Stiles63727522021-10-07 11:20:50 -0400180 %s s = scale[0]; b = bias[0];
Brian Osman07bf3882021-07-01 12:56:23 -0400181 } else {
John Stiles63727522021-10-07 11:20:50 -0400182 %s s = scale[1]; b = bias[1];
Brian Osman07bf3882021-07-01 12:56:23 -0400183 }
184 } else {
185 // thresholds1_7.z is mid point for intervals (4,5) and (6,7)
186 if (%d <= 3 || t < thresholds1_7.z) {
John Stiles63727522021-10-07 11:20:50 -0400187 %s s = scale[2]; b = bias[2];
Brian Osman07bf3882021-07-01 12:56:23 -0400188 } else {
John Stiles63727522021-10-07 11:20:50 -0400189 %s s = scale[3]; b = bias[3];
Brian Osman07bf3882021-07-01 12:56:23 -0400190 }
191 }
192 } else {
193 // thresholds9_13.y is mid point for intervals (8,11) and (12,15)
194 if (%d <= 6 || t < thresholds9_13.y) {
195 // thresholds9_13.x is mid point for intervals (8,9) and (10,11)
196 if (%d <= 5 || t < thresholds9_13.x) {
John Stiles63727522021-10-07 11:20:50 -0400197 %s s = scale[4]; b = bias[4];
Brian Osman07bf3882021-07-01 12:56:23 -0400198 } else {
John Stiles63727522021-10-07 11:20:50 -0400199 %s s = scale[5]; b = bias[5];
Brian Osman07bf3882021-07-01 12:56:23 -0400200 }
201 } else {
202 // thresholds9_13.z is mid point for intervals (12,13) and (14,15)
203 if (%d <= 7 || t < thresholds9_13.z) {
John Stiles63727522021-10-07 11:20:50 -0400204 %s s = scale[6]; b = bias[6];
Brian Osman07bf3882021-07-01 12:56:23 -0400205 } else {
John Stiles63727522021-10-07 11:20:50 -0400206 %s s = scale[7]; b = bias[7];
Brian Osman07bf3882021-07-01 12:56:23 -0400207 }
208 }
209 }
John Stiles63727522021-10-07 11:20:50 -0400210 return t * s + b;
211 }
212 )", intervalCount,
213 intervalCount,
214 intervalCount,
215 (intervalCount <= 0) ? "//" : "",
216 (intervalCount <= 1) ? "//" : "",
217 intervalCount,
218 (intervalCount <= 2) ? "//" : "",
219 (intervalCount <= 3) ? "//" : "",
220 intervalCount,
221 intervalCount,
222 (intervalCount <= 4) ? "//" : "",
223 (intervalCount <= 5) ? "//" : "",
224 intervalCount,
225 (intervalCount <= 6) ? "//" : "",
226 (intervalCount <= 7) ? "//" : "");
Brian Osman07bf3882021-07-01 12:56:23 -0400227
228 auto result = SkRuntimeEffect::MakeForShader(std::move(sksl));
229 SkASSERTF(result.effect, "%s", result.errorText.c_str());
230 effects[intervalCount - 1] = std::move(result.effect);
231 });
232
Brian Osman07bf3882021-07-01 12:56:23 -0400233 return GrSkSLFP::Make(effects[intervalCount - 1], "UnrolledBinaryColorizer",
234 /*inputFP=*/nullptr, GrSkSLFP::OptFlags::kNone,
235 "thresholds1_7", thresholds1_7,
236 "thresholds9_13", thresholds9_13,
John Stiles63727522021-10-07 11:20:50 -0400237 "scale", SkMakeSpan(scale, intervalCount),
238 "bias", SkMakeSpan(bias, intervalCount));
Brian Osman07bf3882021-07-01 12:56:23 -0400239}
240
John Stiles6030e0a2021-10-08 16:23:05 -0400241// Converts an input array of {colors, positions} into an array of {scales, biases, thresholds}.
242// The length of the result array may differ from the input due to hard-stops or empty intervals.
243int build_intervals(int inputLength,
244 const SkPMColor4f* inColors,
245 const SkScalar* inPositions,
246 int outputLength,
247 SkPMColor4f* outScales,
248 SkPMColor4f* outBiases,
249 SkScalar* outThresholds) {
Brian Osman07bf3882021-07-01 12:56:23 -0400250 // Depending on how the positions resolve into hard stops or regular stops, the number of
251 // intervals specified by the number of colors/positions can change. For instance, a plain
252 // 3 color gradient is two intervals, but a 4 color gradient with a hard stop is also
253 // two intervals. At the most extreme end, an 8 interval gradient made entirely of hard
254 // stops has 16 colors.
Brian Osman07bf3882021-07-01 12:56:23 -0400255 int intervalCount = 0;
John Stiles6030e0a2021-10-08 16:23:05 -0400256 for (int i = 0; i < inputLength - 1; i++) {
257 if (intervalCount >= outputLength) {
258 // Already reached our output limit, and haven't run out of color stops. This gradient
259 // cannot be represented without more intervals.
260 return 0;
Brian Osman07bf3882021-07-01 12:56:23 -0400261 }
262
John Stiles6030e0a2021-10-08 16:23:05 -0400263 SkScalar t0 = inPositions[i];
264 SkScalar t1 = inPositions[i + 1];
Brian Osman07bf3882021-07-01 12:56:23 -0400265 SkScalar dt = t1 - t0;
266 // If the interval is empty, skip to the next interval. This will automatically create
267 // distinct hard stop intervals as needed. It also protects against malformed gradients
268 // that have repeated hard stops at the very beginning that are effectively unreachable.
269 if (SkScalarNearlyZero(dt)) {
270 continue;
271 }
272
John Stiles6030e0a2021-10-08 16:23:05 -0400273 Vec4 c0 = Vec4::Load(inColors[i].vec());
274 Vec4 c1 = Vec4::Load(inColors[i + 1].vec());
275 Vec4 scale = (c1 - c0) / dt;
276 Vec4 bias = c0 - t0 * scale;
Brian Osman07bf3882021-07-01 12:56:23 -0400277
John Stiles6030e0a2021-10-08 16:23:05 -0400278 scale.store(outScales + intervalCount);
279 bias.store(outBiases + intervalCount);
280 outThresholds[intervalCount] = t1;
Brian Osman07bf3882021-07-01 12:56:23 -0400281 intervalCount++;
282 }
John Stiles6030e0a2021-10-08 16:23:05 -0400283 return intervalCount;
284}
285
286static std::unique_ptr<GrFragmentProcessor> make_unrolled_binary_colorizer(
287 const SkPMColor4f* colors, const SkScalar* positions, int count) {
288 if (count > kMaxUnrolledColorCount) {
289 // Definitely cannot represent this gradient configuration
290 return nullptr;
291 }
292
293 SkPMColor4f scales[kMaxUnrolledIntervalCount];
294 SkPMColor4f biases[kMaxUnrolledIntervalCount];
295 SkScalar thresholds[kMaxUnrolledIntervalCount] = {};
296 int intervalCount = build_intervals(count, colors, positions,
297 kMaxUnrolledIntervalCount, scales, biases, thresholds);
298 if (intervalCount <= 0) {
299 return nullptr;
300 }
Brian Osman07bf3882021-07-01 12:56:23 -0400301
302 SkRect thresholds1_7 = {thresholds[0], thresholds[1], thresholds[2], thresholds[3]},
303 thresholds9_13 = {thresholds[4], thresholds[5], thresholds[6], 0.0};
304
305 return make_unrolled_colorizer(intervalCount, scales, biases, thresholds1_7, thresholds9_13);
306}
307
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400308// Analyze the shader's color stops and positions and chooses an appropriate colorizer to represent
309// the gradient.
Brian Osman021ed512018-10-16 15:19:44 -0400310static std::unique_ptr<GrFragmentProcessor> make_colorizer(const SkPMColor4f* colors,
Michael Ludwiga7914d32018-09-14 09:47:21 -0400311 const SkScalar* positions, int count, bool premul, const GrFPArgs& args) {
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400312 // If there are hard stops at the beginning or end, the first and/or last color should be
313 // ignored by the colorizer since it should only be used in a clamped border color. By detecting
314 // and removing these stops at the beginning, it makes optimizing the remaining color stops
315 // simpler.
316
Kevin Lubickbe03ef12021-06-16 15:28:00 -0400317 // SkGradientShaderBase guarantees that pos[0] == 0 by adding a default value.
Michael Ludwig0495f7a2018-09-12 15:23:33 -0400318 bool bottomHardStop = SkScalarNearlyEqual(positions[0], positions[1]);
319 // The same is true for pos[end] == 1
Michael Ludwiga7914d32018-09-14 09:47:21 -0400320 bool topHardStop = SkScalarNearlyEqual(positions[count - 2], positions[count - 1]);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400321
322 int offset = 0;
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400323 if (bottomHardStop) {
324 offset += 1;
325 count--;
326 }
327 if (topHardStop) {
328 count--;
329 }
330
Michael Ludwig0495f7a2018-09-12 15:23:33 -0400331 // Two remaining colors means a single interval from 0 to 1
332 // (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 -0400333 if (count == 2) {
Brian Osman65b45972021-06-25 15:58:43 +0000334 return make_single_interval_colorizer(colors[offset], colors[offset + 1]);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400335 }
336
Robert Phillips9da87e02019-02-04 13:26:26 -0500337 const GrShaderCaps* caps = args.fContext->priv().caps()->shaderCaps();
John Stiles6030e0a2021-10-08 16:23:05 -0400338 auto intervalsExceedPrecisionLimit = [&]() -> bool {
339 // The remaining analytic colorizers use scale*t+bias, and the scale/bias values can become
340 // quite large when thresholds are close (but still outside the hardstop limit). If float
341 // isn't 32-bit, output can be incorrect if the thresholds are too close together. However,
342 // the analytic shaders are higher quality, so they can be used with lower precision
343 // hardware when the thresholds are not ill-conditioned.
344 if (!caps->floatIs32Bits()) {
345 // Could run into problems. Check if thresholds are close together (with a limit of .01,
346 // so that scales will be less than 100, which leaves 4 decimals of precision on
347 // 16-bit).
John Stiles4e6353d2021-10-08 17:33:25 -0400348 for (int i = 0; i < count - 1; i++) {
349 SkScalar dt = SkScalarAbs(positions[offset + i] - positions[offset + i + 1]);
John Stiles6030e0a2021-10-08 16:23:05 -0400350 if (dt <= kLowPrecisionIntervalLimit && dt > SK_ScalarNearlyZero) {
351 return true;
352 }
Michael Ludwig72535fb2018-09-28 11:53:32 -0400353 }
354 }
John Stiles6030e0a2021-10-08 16:23:05 -0400355 return false;
356 };
Michael Ludwig72535fb2018-09-28 11:53:32 -0400357
John Stiles6030e0a2021-10-08 16:23:05 -0400358 auto makeDualIntervalColorizer = [&]() -> std::unique_ptr<GrFragmentProcessor> {
359 // The dual-interval colorizer uses the same principles as the binary-search colorizer, but
360 // is limited to exactly 2 intervals.
Michael Ludwig72535fb2018-09-28 11:53:32 -0400361 if (count == 3) {
John Stiles6030e0a2021-10-08 16:23:05 -0400362 // Must be a dual interval gradient, where the middle point is at offset+1 and the
363 // two intervals share the middle color stop.
Brian Osman65b45972021-06-25 15:58:43 +0000364 return make_dual_interval_colorizer(colors[offset], colors[offset + 1],
365 colors[offset + 1], colors[offset + 2],
366 positions[offset + 1]);
John Stiles6030e0a2021-10-08 16:23:05 -0400367 }
368 if (count == 4 && SkScalarNearlyEqual(positions[offset + 1],
Michael Ludwig72535fb2018-09-28 11:53:32 -0400369 positions[offset + 2])) {
370 // Two separate intervals that join at the same threshold position
Brian Osman65b45972021-06-25 15:58:43 +0000371 return make_dual_interval_colorizer(colors[offset], colors[offset + 1],
372 colors[offset + 2], colors[offset + 3],
373 positions[offset + 1]);
Michael Ludwig72535fb2018-09-28 11:53:32 -0400374 }
John Stiles6030e0a2021-10-08 16:23:05 -0400375 // The gradient can't be represented in only two intervals.
376 return nullptr;
377 };
Michael Ludwig72535fb2018-09-28 11:53:32 -0400378
John Stiles6030e0a2021-10-08 16:23:05 -0400379 // Attempt to create an analytic colorizer.
380 if ((count <= kMaxUnrolledColorCount) && !intervalsExceedPrecisionLimit()) {
381 std::unique_ptr<GrFragmentProcessor> colorizer = makeDualIntervalColorizer();
382 if (colorizer) {
383 return colorizer;
384 }
385 colorizer = make_unrolled_binary_colorizer(colors + offset, positions + offset, count);
386 if (colorizer) {
387 return colorizer;
Michael Ludwig72535fb2018-09-28 11:53:32 -0400388 }
389 }
390
391 // Otherwise fall back to a rasterized gradient sampled by a texture, which can handle
John Stiles6030e0a2021-10-08 16:23:05 -0400392 // arbitrary gradients. (This has limited sampling resolution, and always blurs hard-stops.)
Michael Ludwiga7914d32018-09-14 09:47:21 -0400393 return make_textured_colorizer(colors + offset, positions + offset, count, premul, args);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400394}
395
Brian Osmanc6804ed2021-06-29 11:11:49 -0400396// This top-level effect implements clamping on the layout coordinate and requires specifying the
397// border colors that are used when outside the clamped boundary. Gradients with the
398// SkTileMode::kClamp should use the colors at their first and last stop (after adding default stops
399// for t=0,t=1) as the border color. This will automatically replicate the edge color, even when
400// there is a hard stop.
401//
402// The SkTileMode::kDecal can be produced by specifying transparent black as the border colors,
403// regardless of the gradient's stop colors.
404static std::unique_ptr<GrFragmentProcessor> make_clamped_gradient(
405 std::unique_ptr<GrFragmentProcessor> colorizer,
406 std::unique_ptr<GrFragmentProcessor> gradLayout,
407 SkPMColor4f leftBorderColor,
408 SkPMColor4f rightBorderColor,
409 bool makePremul,
410 bool colorsAreOpaque) {
411 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
412 uniform shader colorizer;
413 uniform shader gradLayout;
414
415 uniform half4 leftBorderColor; // t < 0.0
416 uniform half4 rightBorderColor; // t > 1.0
417
418 uniform int makePremul; // specialized
419 uniform int layoutPreservesOpacity; // specialized
420
421 half4 main(float2 coord) {
Brian Osmancbfa34a2021-09-02 09:26:27 -0400422 half4 t = gradLayout.eval(coord);
Brian Osmanc6804ed2021-06-29 11:11:49 -0400423 half4 outColor;
424
425 // If t.x is below 0, use the left border color without invoking the child processor.
426 // If any t.x is above 1, use the right border color. Otherwise, t is in the [0, 1]
427 // range assumed by the colorizer FP, so delegate to the child processor.
428 if (!bool(layoutPreservesOpacity) && t.y < 0) {
429 // layout has rejected this fragment (rely on sksl to remove this branch if the
430 // layout FP preserves opacity is false)
431 outColor = half4(0);
432 } else if (t.x < 0) {
433 outColor = leftBorderColor;
434 } else if (t.x > 1.0) {
435 outColor = rightBorderColor;
436 } else {
437 // Always sample from (x, 0), discarding y, since the layout FP can use y as a
438 // side-channel.
Brian Osmancbfa34a2021-09-02 09:26:27 -0400439 outColor = colorizer.eval(t.x0);
Brian Osmanc6804ed2021-06-29 11:11:49 -0400440 }
441 if (bool(makePremul)) {
442 outColor.rgb *= outColor.a;
443 }
444 return outColor;
445 }
446 )");
447
448 // If the layout does not preserve opacity, remove the opaque optimization,
449 // but otherwise respect the provided color opacity state (which should take
450 // into account the opacity of the border colors).
451 bool layoutPreservesOpacity = gradLayout->preservesOpaqueInput();
452 GrSkSLFP::OptFlags optFlags = GrSkSLFP::OptFlags::kCompatibleWithCoverageAsAlpha;
453 if (colorsAreOpaque && layoutPreservesOpacity) {
454 optFlags |= GrSkSLFP::OptFlags::kPreservesOpaqueInput;
455 }
456
457 return GrSkSLFP::Make(effect, "ClampedGradient", /*inputFP=*/nullptr, optFlags,
458 "colorizer", GrSkSLFP::IgnoreOptFlags(std::move(colorizer)),
459 "gradLayout", GrSkSLFP::IgnoreOptFlags(std::move(gradLayout)),
460 "leftBorderColor", leftBorderColor,
461 "rightBorderColor", rightBorderColor,
462 "makePremul", GrSkSLFP::Specialize<int>(makePremul),
463 "layoutPreservesOpacity",
464 GrSkSLFP::Specialize<int>(layoutPreservesOpacity));
465}
466
467static std::unique_ptr<GrFragmentProcessor> make_tiled_gradient(
468 const GrFPArgs& args,
469 std::unique_ptr<GrFragmentProcessor> colorizer,
470 std::unique_ptr<GrFragmentProcessor> gradLayout,
471 bool mirror,
472 bool makePremul,
473 bool colorsAreOpaque) {
474 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
475 uniform shader colorizer;
476 uniform shader gradLayout;
477
478 uniform int mirror; // specialized
479 uniform int makePremul; // specialized
480 uniform int layoutPreservesOpacity; // specialized
481 uniform int useFloorAbsWorkaround; // specialized
482
483 half4 main(float2 coord) {
Brian Osmancbfa34a2021-09-02 09:26:27 -0400484 half4 t = gradLayout.eval(coord);
Brian Osmanc6804ed2021-06-29 11:11:49 -0400485
486 if (!bool(layoutPreservesOpacity) && t.y < 0) {
487 // layout has rejected this fragment (rely on sksl to remove this branch if the
488 // layout FP preserves opacity is false)
489 return half4(0);
490 } else {
491 if (bool(mirror)) {
492 half t_1 = t.x - 1;
493 half tiled_t = t_1 - 2 * floor(t_1 * 0.5) - 1;
494 if (bool(useFloorAbsWorkaround)) {
495 // At this point the expected value of tiled_t should between -1 and 1, so
496 // this clamp has no effect other than to break up the floor and abs calls
497 // and make sure the compiler doesn't merge them back together.
498 tiled_t = clamp(tiled_t, -1, 1);
499 }
500 t.x = abs(tiled_t);
501 } else {
502 // Simple repeat mode
503 t.x = fract(t.x);
504 }
505
506 // Always sample from (x, 0), discarding y, since the layout FP can use y as a
507 // side-channel.
Brian Osmancbfa34a2021-09-02 09:26:27 -0400508 half4 outColor = colorizer.eval(t.x0);
Brian Osmanc6804ed2021-06-29 11:11:49 -0400509 if (bool(makePremul)) {
510 outColor.rgb *= outColor.a;
511 }
512 return outColor;
513 }
514 }
515 )");
516
517 // If the layout does not preserve opacity, remove the opaque optimization,
518 // but otherwise respect the provided color opacity state (which should take
519 // into account the opacity of the border colors).
520 bool layoutPreservesOpacity = gradLayout->preservesOpaqueInput();
521 GrSkSLFP::OptFlags optFlags = GrSkSLFP::OptFlags::kCompatibleWithCoverageAsAlpha;
522 if (colorsAreOpaque && layoutPreservesOpacity) {
523 optFlags |= GrSkSLFP::OptFlags::kPreservesOpaqueInput;
524 }
525 const bool useFloorAbsWorkaround =
526 args.fContext->priv().caps()->shaderCaps()->mustDoOpBetweenFloorAndAbs();
527
528 return GrSkSLFP::Make(effect, "TiledGradient", /*inputFP=*/nullptr, optFlags,
529 "colorizer", GrSkSLFP::IgnoreOptFlags(std::move(colorizer)),
530 "gradLayout", GrSkSLFP::IgnoreOptFlags(std::move(gradLayout)),
531 "mirror", GrSkSLFP::Specialize<int>(mirror),
532 "makePremul", GrSkSLFP::Specialize<int>(makePremul),
533 "layoutPreservesOpacity",
534 GrSkSLFP::Specialize<int>(layoutPreservesOpacity),
535 "useFloorAbsWorkaround",
536 GrSkSLFP::Specialize<int>(useFloorAbsWorkaround));
537}
538
Michael Ludwig672c4762020-08-05 17:28:42 -0400539// Combines the colorizer and layout with an appropriately configured top-level effect based on the
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400540// gradient's tile mode
Brian Osman65b45972021-06-25 15:58:43 +0000541static std::unique_ptr<GrFragmentProcessor> make_gradient(
542 const SkGradientShaderBase& shader,
543 const GrFPArgs& args,
544 std::unique_ptr<GrFragmentProcessor> layout,
545 const SkMatrix* overrideMatrix = nullptr) {
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400546 // No shader is possible if a layout couldn't be created, e.g. a layout-specific Make() returned
547 // null.
548 if (layout == nullptr) {
549 return nullptr;
550 }
551
Brian Osman65b45972021-06-25 15:58:43 +0000552 // Wrap the layout in a matrix effect to apply the gradient's matrix:
553 SkMatrix matrix;
554 if (!shader.totalLocalMatrix(args.fPreLocalMatrix)->invert(&matrix)) {
555 return nullptr;
556 }
557 // Some two-point conical gradients use a custom matrix here
558 matrix.postConcat(overrideMatrix ? *overrideMatrix : shader.getGradientMatrix());
559 layout = GrMatrixEffect::Make(matrix, std::move(layout));
560
Brian Osman021ed512018-10-16 15:19:44 -0400561 // Convert all colors into destination space and into SkPMColor4fs, and handle
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400562 // premul issues depending on the interpolation mode
563 bool inputPremul = shader.getGradFlags() & SkGradientShader::kInterpolateColorsInPremul_Flag;
Michael Ludwigb96cba32018-09-14 13:59:24 -0400564 bool allOpaque = true;
Brian Osman021ed512018-10-16 15:19:44 -0400565 SkAutoSTMalloc<4, SkPMColor4f> colors(shader.fColorCount);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400566 SkColor4fXformer xformedColors(shader.fOrigColors4f, shader.fColorCount,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400567 shader.fColorSpace.get(), args.fDstColorInfo->colorSpace());
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400568 for (int i = 0; i < shader.fColorCount; i++) {
Brian Osman021ed512018-10-16 15:19:44 -0400569 const SkColor4f& upmColor = xformedColors.fColors[i];
570 colors[i] = inputPremul ? upmColor.premul()
571 : SkPMColor4f{ upmColor.fR, upmColor.fG, upmColor.fB, upmColor.fA };
572 if (allOpaque && !SkScalarNearlyEqual(colors[i].fA, 1.0)) {
Michael Ludwigb96cba32018-09-14 13:59:24 -0400573 allOpaque = false;
574 }
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400575 }
576
Michael Ludwig0495f7a2018-09-12 15:23:33 -0400577 // SkGradientShader stores positions implicitly when they are evenly spaced, but the getPos()
578 // implementation performs a branch for every position index. Since the shader conversion
579 // requires lots of position tests, calculate all of the positions up front if needed.
580 SkTArray<SkScalar, true> implicitPos;
581 SkScalar* positions;
582 if (shader.fOrigPos) {
583 positions = shader.fOrigPos;
584 } else {
John Stilesf4bda742020-10-14 16:57:41 -0400585 implicitPos.reserve_back(shader.fColorCount);
Mike Kleind3ed3012018-11-06 19:23:08 -0500586 SkScalar posScale = SK_Scalar1 / (shader.fColorCount - 1);
Michael Ludwig0495f7a2018-09-12 15:23:33 -0400587 for (int i = 0 ; i < shader.fColorCount; i++) {
588 implicitPos.push_back(SkIntToScalar(i) * posScale);
589 }
590 positions = implicitPos.begin();
591 }
592
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400593 // All gradients are colorized the same way, regardless of layout
Michael Ludwiga7914d32018-09-14 09:47:21 -0400594 std::unique_ptr<GrFragmentProcessor> colorizer = make_colorizer(
595 colors.get(), positions, shader.fColorCount, inputPremul, args);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400596 if (colorizer == nullptr) {
597 return nullptr;
598 }
599
Michael Ludwig672c4762020-08-05 17:28:42 -0400600 // The top-level effect has to export premul colors, but under certain conditions it doesn't
601 // need to do anything to achieve that: i.e. its interpolating already premul colors
602 // (inputPremul) or all the colors have a = 1, in which case premul is a no op. Note that this
603 // allOpaque check is more permissive than SkGradientShaderBase's isOpaque(), since we can
604 // optimize away the make-premul op for two point conical gradients (which report false for
605 // isOpaque).
Michael Ludwigb96cba32018-09-14 13:59:24 -0400606 bool makePremul = !inputPremul && !allOpaque;
607
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400608 // All tile modes are supported (unless something was added to SkShader)
Michael Ludwig672c4762020-08-05 17:28:42 -0400609 std::unique_ptr<GrFragmentProcessor> gradient;
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400610 switch(shader.getTileMode()) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400611 case SkTileMode::kRepeat:
Brian Osmanc6804ed2021-06-29 11:11:49 -0400612 gradient = make_tiled_gradient(args, std::move(colorizer), std::move(layout),
613 /* mirror */ false, makePremul, allOpaque);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400614 break;
Mike Reedfae8fce2019-04-03 10:27:45 -0400615 case SkTileMode::kMirror:
Brian Osmanc6804ed2021-06-29 11:11:49 -0400616 gradient = make_tiled_gradient(args, std::move(colorizer), std::move(layout),
617 /* mirror */ true, makePremul, allOpaque);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400618 break;
Mike Reedfae8fce2019-04-03 10:27:45 -0400619 case SkTileMode::kClamp:
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400620 // For the clamped mode, the border colors are the first and last colors, corresponding
621 // to t=0 and t=1, because SkGradientShaderBase enforces that by adding color stops as
622 // appropriate. If there is a hard stop, this grabs the expected outer colors for the
623 // border.
Brian Osmanc6804ed2021-06-29 11:11:49 -0400624 gradient = make_clamped_gradient(std::move(colorizer), std::move(layout),
625 colors[0], colors[shader.fColorCount - 1],
626 makePremul, allOpaque);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400627 break;
Mike Reedfae8fce2019-04-03 10:27:45 -0400628 case SkTileMode::kDecal:
Michael Ludwigb96cba32018-09-14 13:59:24 -0400629 // Even if the gradient colors are opaque, the decal borders are transparent so
630 // disable that optimization
Brian Osmanc6804ed2021-06-29 11:11:49 -0400631 gradient = make_clamped_gradient(std::move(colorizer), std::move(layout),
632 SK_PMColor4fTRANSPARENT, SK_PMColor4fTRANSPARENT,
633 makePremul, /* colorsAreOpaque */ false);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400634 break;
635 }
636
Michael Ludwig672c4762020-08-05 17:28:42 -0400637 if (gradient == nullptr) {
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400638 // Unexpected tile mode
639 return nullptr;
640 }
Brian Salomonc0d79e52019-04-10 15:02:11 -0400641 if (args.fInputColorIsOpaque) {
Brian Osman55cbc752021-05-27 11:57:13 -0400642 // If the input alpha is known to be 1, we don't need to take the kSrcIn path. This is
643 // just an optimization. However, we can't just return 'gradient' here. We need to actually
644 // inhibit the coverage-as-alpha optimization, or we'll fail to incorporate AA correctly.
645 // The OverrideInput FP happens to do that, so wrap our fp in one of those. The gradient FP
646 // doesn't actually use the input color at all, so the overridden input is irrelevant.
Michael Ludwig672c4762020-08-05 17:28:42 -0400647 return GrFragmentProcessor::OverrideInput(std::move(gradient), SK_PMColor4fWHITE, false);
Brian Salomonc0d79e52019-04-10 15:02:11 -0400648 }
Michael Ludwig672c4762020-08-05 17:28:42 -0400649 return GrFragmentProcessor::MulChildByInputAlpha(std::move(gradient));
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400650}
651
652namespace GrGradientShader {
653
654std::unique_ptr<GrFragmentProcessor> MakeLinear(const SkLinearGradient& shader,
655 const GrFPArgs& args) {
Brian Osman65b45972021-06-25 15:58:43 +0000656 // We add a tiny delta to t. When gradient stops are set up so that a hard stop in a vertically
657 // or horizontally oriented gradient falls exactly at a column or row of pixel centers we can
658 // we can get slightly different interpolated t values along the column/row. By adding the delta
659 // we will consistently get the color to the "right" of the stop. Of course if the hard stop
660 // falls at X.5 - delta then we still could get inconsistent results, but that is much less
661 // likely. crbug.com/938592
662 // If/when we add filtering of the gradient this can be removed.
663 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
664 half4 main(float2 coord) {
665 return half4(half(coord.x) + 0.00001, 1, 0, 0); // y = 1 for always valid
666 }
667 )");
668 // The linear gradient never rejects a pixel so it doesn't change opacity
669 auto fp = GrSkSLFP::Make(effect, "LinearLayout", /*inputFP=*/nullptr,
670 GrSkSLFP::OptFlags::kPreservesOpaqueInput);
671 return make_gradient(shader, args, std::move(fp));
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400672}
673
Michael Ludwig4089df82018-09-12 15:22:37 -0400674std::unique_ptr<GrFragmentProcessor> MakeRadial(const SkRadialGradient& shader,
675 const GrFPArgs& args) {
Brian Osman65b45972021-06-25 15:58:43 +0000676 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
677 half4 main(float2 coord) {
678 return half4(half(length(coord)), 1, 0, 0); // y = 1 for always valid
679 }
680 )");
681 // The radial gradient never rejects a pixel so it doesn't change opacity
682 auto fp = GrSkSLFP::Make(effect, "RadialLayout", /*inputFP=*/nullptr,
683 GrSkSLFP::OptFlags::kPreservesOpaqueInput);
684 return make_gradient(shader, args, std::move(fp));
Michael Ludwig4089df82018-09-12 15:22:37 -0400685}
686
Michael Ludwig24d438b2018-09-12 15:22:50 -0400687std::unique_ptr<GrFragmentProcessor> MakeSweep(const SkSweepGradient& shader,
688 const GrFPArgs& args) {
Brian Osman65b45972021-06-25 15:58:43 +0000689 // On some devices they incorrectly implement atan2(y,x) as atan(y/x). In actuality it is
690 // atan2(y,x) = 2 * atan(y / (sqrt(x^2 + y^2) + x)). So to work around this we pass in (sqrt(x^2
691 // + y^2) + x) as the second parameter to atan2 in these cases. We let the device handle the
692 // undefined behavior of the second paramenter being 0 instead of doing the divide ourselves and
693 // using atan instead.
694 int useAtanWorkaround =
695 args.fContext->priv().caps()->shaderCaps()->atan2ImplementedAsAtanYOverX();
696 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
697 uniform half bias;
698 uniform half scale;
699 uniform int useAtanWorkaround; // specialized
700
701 half4 main(float2 coord) {
702 half angle = bool(useAtanWorkaround)
703 ? half(2 * atan(-coord.y, length(coord) - coord.x))
704 : half(atan(-coord.y, -coord.x));
705
706 // 0.1591549430918 is 1/(2*pi), used since atan returns values [-pi, pi]
707 half t = (angle * 0.1591549430918 + 0.5 + bias) * scale;
708 return half4(t, 1, 0, 0); // y = 1 for always valid
709 }
710 )");
711 // The sweep gradient never rejects a pixel so it doesn't change opacity
712 auto fp = GrSkSLFP::Make(effect, "SweepLayout", /*inputFP=*/nullptr,
713 GrSkSLFP::OptFlags::kPreservesOpaqueInput,
714 "bias", shader.getTBias(),
715 "scale", shader.getTScale(),
716 "useAtanWorkaround", GrSkSLFP::Specialize(useAtanWorkaround));
717 return make_gradient(shader, args, std::move(fp));
Michael Ludwig24d438b2018-09-12 15:22:50 -0400718}
719
Michael Ludwig8f685082018-09-12 15:23:01 -0400720std::unique_ptr<GrFragmentProcessor> MakeConical(const SkTwoPointConicalGradient& shader,
721 const GrFPArgs& args) {
Brian Osman65b45972021-06-25 15:58:43 +0000722 // The 2 point conical gradient can reject a pixel so it does change opacity even if the input
723 // was opaque. Thus, all of these layout FPs disable that optimization.
724 std::unique_ptr<GrFragmentProcessor> fp;
725 SkTLazy<SkMatrix> matrix;
726 switch (shader.getType()) {
727 case SkTwoPointConicalGradient::Type::kStrip: {
728 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
729 uniform half r0_2;
730 half4 main(float2 p) {
731 half v = 1; // validation flag, set to negative to discard fragment later
732 float t = r0_2 - p.y * p.y;
733 if (t >= 0) {
734 t = p.x + sqrt(t);
735 } else {
736 v = -1;
737 }
738 return half4(half(t), v, 0, 0);
739 }
740 )");
741 float r0 = shader.getStartRadius() / shader.getCenterX1();
742 fp = GrSkSLFP::Make(effect, "TwoPointConicalStripLayout", /*inputFP=*/nullptr,
743 GrSkSLFP::OptFlags::kNone,
744 "r0_2", r0 * r0);
745 } break;
746
747 case SkTwoPointConicalGradient::Type::kRadial: {
748 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
749 uniform half r0;
750 uniform half lengthScale;
751 half4 main(float2 p) {
752 half v = 1; // validation flag, set to negative to discard fragment later
753 float t = length(p) * lengthScale - r0;
754 return half4(half(t), v, 0, 0);
755 }
756 )");
757 float dr = shader.getDiffRadius();
758 float r0 = shader.getStartRadius() / dr;
759 bool isRadiusIncreasing = dr >= 0;
760 fp = GrSkSLFP::Make(effect, "TwoPointConicalRadialLayout", /*inputFP=*/nullptr,
761 GrSkSLFP::OptFlags::kNone,
762 "r0", r0,
763 "lengthScale", isRadiusIncreasing ? 1.0f : -1.0f);
764
765 // GPU radial matrix is different from the original matrix, since we map the diff radius
766 // to have |dr| = 1, so manually compute the final gradient matrix here.
767
768 // Map center to (0, 0)
769 matrix.set(SkMatrix::Translate(-shader.getStartCenter().fX,
770 -shader.getStartCenter().fY));
771 // scale |diffRadius| to 1
772 matrix->postScale(1 / dr, 1 / dr);
773 } break;
774
775 case SkTwoPointConicalGradient::Type::kFocal: {
776 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
777 // Optimization flags, all specialized:
778 uniform int isRadiusIncreasing;
779 uniform int isFocalOnCircle;
780 uniform int isWellBehaved;
781 uniform int isSwapped;
782 uniform int isNativelyFocal;
783
784 uniform half invR1; // 1/r1
785 uniform half fx; // focalX = r0/(r0-r1)
786
787 half4 main(float2 p) {
788 float t = -1;
789 half v = 1; // validation flag, set to negative to discard fragment later
790
791 float x_t = -1;
792 if (bool(isFocalOnCircle)) {
793 x_t = dot(p, p) / p.x;
794 } else if (bool(isWellBehaved)) {
795 x_t = length(p) - p.x * invR1;
796 } else {
797 float temp = p.x * p.x - p.y * p.y;
798
799 // Only do sqrt if temp >= 0; this is significantly slower than checking
800 // temp >= 0 in the if statement that checks r(t) >= 0. But GPU may break if
801 // we sqrt a negative float. (Although I havevn't observed that on any
802 // devices so far, and the old approach also does sqrt negative value
803 // without a check.) If the performance is really critical, maybe we should
804 // just compute the area where temp and x_t are always valid and drop all
805 // these ifs.
806 if (temp >= 0) {
807 if (bool(isSwapped) || !bool(isRadiusIncreasing)) {
808 x_t = -sqrt(temp) - p.x * invR1;
809 } else {
810 x_t = sqrt(temp) - p.x * invR1;
811 }
812 }
813 }
814
815 // The final calculation of t from x_t has lots of static optimizations but only
816 // do them when x_t is positive (which can be assumed true if isWellBehaved is
817 // true)
818 if (!bool(isWellBehaved)) {
819 // This will still calculate t even though it will be ignored later in the
820 // pipeline to avoid a branch
821 if (x_t <= 0.0) {
822 v = -1;
823 }
824 }
825 if (bool(isRadiusIncreasing)) {
826 if (bool(isNativelyFocal)) {
827 t = x_t;
828 } else {
829 t = x_t + fx;
830 }
831 } else {
832 if (bool(isNativelyFocal)) {
833 t = -x_t;
834 } else {
835 t = -x_t + fx;
836 }
837 }
838
839 if (bool(isSwapped)) {
840 t = 1 - t;
841 }
842
843 return half4(half(t), v, 0, 0);
844 }
845 )");
846
847 const SkTwoPointConicalGradient::FocalData& focalData = shader.getFocalData();
848 bool isRadiusIncreasing = (1 - focalData.fFocalX) > 0,
849 isFocalOnCircle = focalData.isFocalOnCircle(),
850 isWellBehaved = focalData.isWellBehaved(),
851 isSwapped = focalData.isSwapped(),
852 isNativelyFocal = focalData.isNativelyFocal();
853
854 fp = GrSkSLFP::Make(effect, "TwoPointConicalFocalLayout", /*inputFP=*/nullptr,
855 GrSkSLFP::OptFlags::kNone,
856 "isRadiusIncreasing", GrSkSLFP::Specialize<int>(isRadiusIncreasing),
857 "isFocalOnCircle", GrSkSLFP::Specialize<int>(isFocalOnCircle),
858 "isWellBehaved", GrSkSLFP::Specialize<int>(isWellBehaved),
859 "isSwapped", GrSkSLFP::Specialize<int>(isSwapped),
860 "isNativelyFocal", GrSkSLFP::Specialize<int>(isNativelyFocal),
861 "invR1", 1.0f / focalData.fR1,
862 "fx", focalData.fFocalX);
863 } break;
864 }
865 return make_gradient(shader, args, std::move(fp), matrix.getMaybeNull());
Michael Ludwig8f685082018-09-12 15:23:01 -0400866}
867
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400868#if GR_TEST_UTILS
869RandomParams::RandomParams(SkRandom* random) {
870 // Set color count to min of 2 so that we don't trigger the const color optimization and make
871 // a non-gradient processor.
872 fColorCount = random->nextRangeU(2, kMaxRandomGradientColors);
873 fUseColors4f = random->nextBool();
874
875 // if one color, omit stops, otherwise randomly decide whether or not to
876 if (fColorCount == 1 || (fColorCount >= 2 && random->nextBool())) {
877 fStops = nullptr;
878 } else {
879 fStops = fStopStorage;
880 }
881
882 // if using SkColor4f, attach a random (possibly null) color space (with linear gamma)
883 if (fUseColors4f) {
884 fColorSpace = GrTest::TestColorSpace(random);
885 }
886
887 SkScalar stop = 0.f;
888 for (int i = 0; i < fColorCount; ++i) {
889 if (fUseColors4f) {
890 fColors4f[i].fR = random->nextUScalar1();
891 fColors4f[i].fG = random->nextUScalar1();
892 fColors4f[i].fB = random->nextUScalar1();
893 fColors4f[i].fA = random->nextUScalar1();
894 } else {
895 fColors[i] = random->nextU();
896 }
897 if (fStops) {
898 fStops[i] = stop;
899 stop = i < fColorCount - 1 ? stop + random->nextUScalar1() * (1.f - stop) : 1.f;
900 }
901 }
Mike Reedfae8fce2019-04-03 10:27:45 -0400902 fTileMode = static_cast<SkTileMode>(random->nextULessThan(kSkTileModeCount));
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400903}
904#endif
905
John Stilesa6841be2020-08-06 14:11:56 -0400906} // namespace GrGradientShader