Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/gradients/GrGradientShader.h" |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/gpu/gradients/GrGradientBitmapCache.h" |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 11 | |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 12 | #include "include/gpu/GrRecordingContext.h" |
Brian Osman | 65b4597 | 2021-06-25 15:58:43 +0000 | [diff] [blame] | 13 | #include "src/core/SkRuntimeEffectPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/GrCaps.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrColor.h" |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrColorInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrRecordingContextPriv.h" |
| 18 | #include "src/gpu/SkGr.h" |
Brian Osman | 65b4597 | 2021-06-25 15:58:43 +0000 | [diff] [blame] | 19 | #include "src/gpu/effects/GrMatrixEffect.h" |
| 20 | #include "src/gpu/effects/GrSkSLFP.h" |
Brian Salomon | 1747375 | 2020-06-19 09:53:50 -0400 | [diff] [blame] | 21 | #include "src/gpu/effects/GrTextureEffect.h" |
Michael Ludwig | a7914d3 | 2018-09-14 09:47:21 -0400 | [diff] [blame] | 22 | |
John Stiles | 6030e0a | 2021-10-08 16:23:05 -0400 | [diff] [blame] | 23 | using Vec4 = skvx::Vec<4, float>; |
| 24 | |
Michael Ludwig | 72535fb | 2018-09-28 11:53:32 -0400 | [diff] [blame] | 25 | // Intervals smaller than this (that aren't hard stops) on low-precision-only devices force us to |
| 26 | // use the textured gradient |
| 27 | static const SkScalar kLowPrecisionIntervalLimit = 0.01f; |
| 28 | |
Michael Ludwig | a7914d3 | 2018-09-14 09:47:21 -0400 | [diff] [blame] | 29 | // Each cache entry costs 1K or 2K of RAM. Each bitmap will be 1x256 at either 32bpp or 64bpp. |
| 30 | static const int kMaxNumCachedGradientBitmaps = 32; |
| 31 | static 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 Osman | 021ed51 | 2018-10-16 15:19:44 -0400 | [diff] [blame] | 35 | static std::unique_ptr<GrFragmentProcessor> make_textured_colorizer(const SkPMColor4f* colors, |
Michael Ludwig | a7914d3 | 2018-09-14 09:47:21 -0400 | [diff] [blame] | 36 | 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 Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 42 | if (GrColorTypeIsWiderThan(args.fDstColorInfo->colorType(), 8)) { |
Greg Daniel | 7bfc913 | 2019-08-14 14:23:53 -0400 | [diff] [blame] | 43 | auto f16Format = args.fContext->priv().caps()->getDefaultBackendFormat( |
| 44 | GrColorType::kRGBA_F16, GrRenderable::kNo); |
| 45 | if (f16Format.isValid()) { |
| 46 | colorType = kRGBA_F16_SkColorType; |
| 47 | } |
Michael Ludwig | a7914d3 | 2018-09-14 09:47:21 -0400 | [diff] [blame] | 48 | } |
| 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 Salomon | 27c4202 | 2021-04-28 12:39:21 -0400 | [diff] [blame] | 56 | auto view = std::get<0>(GrMakeCachedBitmapProxyView(args.fContext, bitmap, GrMipmapped::kNo)); |
| 57 | if (!view) { |
Michael Ludwig | a7914d3 | 2018-09-14 09:47:21 -0400 | [diff] [blame] | 58 | SkDebugf("Gradient won't draw. Could not create texture."); |
| 59 | return nullptr; |
| 60 | } |
John Stiles | 1cf1593 | 2020-07-31 16:08:24 -0400 | [diff] [blame] | 61 | |
Brian Salomon | 1747375 | 2020-06-19 09:53:50 -0400 | [diff] [blame] | 62 | auto m = SkMatrix::Scale(view.width(), 1.f); |
John Stiles | 1cf1593 | 2020-07-31 16:08:24 -0400 | [diff] [blame] | 63 | return GrTextureEffect::Make(std::move(view), alphaType, m, GrSamplerState::Filter::kLinear); |
Michael Ludwig | a7914d3 | 2018-09-14 09:47:21 -0400 | [diff] [blame] | 64 | } |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 65 | |
Brian Osman | 65b4597 | 2021-06-25 15:58:43 +0000 | [diff] [blame] | 66 | |
| 67 | static 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 | |
| 84 | static 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 Stiles | 59b5f52 | 2021-10-07 15:27:37 -0400 | [diff] [blame] | 90 | uniform float4 scale[2]; |
| 91 | uniform float4 bias[2]; |
Brian Osman | 65b4597 | 2021-06-25 15:58:43 +0000 | [diff] [blame] | 92 | uniform half threshold; |
| 93 | |
| 94 | half4 main(float2 coord) { |
| 95 | half t = half(coord.x); |
| 96 | |
John Stiles | 59b5f52 | 2021-10-07 15:27:37 -0400 | [diff] [blame] | 97 | float4 s, b; |
Brian Osman | 65b4597 | 2021-06-25 15:58:43 +0000 | [diff] [blame] | 98 | if (t < threshold) { |
John Stiles | 59b5f52 | 2021-10-07 15:27:37 -0400 | [diff] [blame] | 99 | s = scale[0]; |
| 100 | b = bias[0]; |
Brian Osman | 65b4597 | 2021-06-25 15:58:43 +0000 | [diff] [blame] | 101 | } else { |
John Stiles | 59b5f52 | 2021-10-07 15:27:37 -0400 | [diff] [blame] | 102 | s = scale[1]; |
| 103 | b = bias[1]; |
Brian Osman | 65b4597 | 2021-06-25 15:58:43 +0000 | [diff] [blame] | 104 | } |
| 105 | |
John Stiles | 59b5f52 | 2021-10-07 15:27:37 -0400 | [diff] [blame] | 106 | return half4(t * s + b); |
Brian Osman | 65b4597 | 2021-06-25 15:58:43 +0000 | [diff] [blame] | 107 | } |
| 108 | )"); |
| 109 | |
Brian Osman | 65b4597 | 2021-06-25 15:58:43 +0000 | [diff] [blame] | 110 | // Derive scale and biases from the 4 colors and threshold |
John Stiles | 6030e0a | 2021-10-08 16:23:05 -0400 | [diff] [blame] | 111 | 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 Osman | 65b4597 | 2021-06-25 15:58:43 +0000 | [diff] [blame] | 115 | |
John Stiles | 6030e0a | 2021-10-08 16:23:05 -0400 | [diff] [blame] | 116 | const Vec4 scale[2] = {(vc1 - vc0) / threshold, |
John Stiles | 59b5f52 | 2021-10-07 15:27:37 -0400 | [diff] [blame] | 117 | (vc3 - vc2) / (1 - threshold)}; |
John Stiles | 6030e0a | 2021-10-08 16:23:05 -0400 | [diff] [blame] | 118 | const Vec4 bias[2] = {vc0, |
John Stiles | 59b5f52 | 2021-10-07 15:27:37 -0400 | [diff] [blame] | 119 | vc2 - threshold * scale[1]}; |
Brian Osman | 65b4597 | 2021-06-25 15:58:43 +0000 | [diff] [blame] | 120 | return GrSkSLFP::Make(effect, "DualIntervalColorizer", /*inputFP=*/nullptr, |
| 121 | GrSkSLFP::OptFlags::kNone, |
John Stiles | 59b5f52 | 2021-10-07 15:27:37 -0400 | [diff] [blame] | 122 | "scale", SkMakeSpan(scale), |
| 123 | "bias", SkMakeSpan(bias), |
Brian Osman | 65b4597 | 2021-06-25 15:58:43 +0000 | [diff] [blame] | 124 | "threshold", threshold); |
| 125 | } |
| 126 | |
John Stiles | 6030e0a | 2021-10-08 16:23:05 -0400 | [diff] [blame] | 127 | // 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 Osman | 07bf388 | 2021-07-01 12:56:23 -0400 | [diff] [blame] | 130 | static constexpr int kMaxUnrolledColorCount = 16; |
| 131 | static constexpr int kMaxUnrolledIntervalCount = 8; |
| 132 | |
| 133 | static 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 Stiles | 6372752 | 2021-10-07 11:20:50 -0400 | [diff] [blame] | 164 | sksl.appendf("uniform float4 scale[%d];", intervalCount); |
| 165 | sksl.appendf("uniform float4 bias[%d];", intervalCount); |
Brian Osman | 07bf388 | 2021-07-01 12:56:23 -0400 | [diff] [blame] | 166 | |
John Stiles | 6372752 | 2021-10-07 11:20:50 -0400 | [diff] [blame] | 167 | // 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 Osman | 07bf388 | 2021-07-01 12:56:23 -0400 | [diff] [blame] | 170 | sksl.appendf(R"( |
John Stiles | 6372752 | 2021-10-07 11:20:50 -0400 | [diff] [blame] | 171 | half4 main(float2 coord) { |
| 172 | half t = half(coord.x); |
| 173 | float4 s, b; |
Brian Osman | 07bf388 | 2021-07-01 12:56:23 -0400 | [diff] [blame] | 174 | // 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 Stiles | 6372752 | 2021-10-07 11:20:50 -0400 | [diff] [blame] | 180 | %s s = scale[0]; b = bias[0]; |
Brian Osman | 07bf388 | 2021-07-01 12:56:23 -0400 | [diff] [blame] | 181 | } else { |
John Stiles | 6372752 | 2021-10-07 11:20:50 -0400 | [diff] [blame] | 182 | %s s = scale[1]; b = bias[1]; |
Brian Osman | 07bf388 | 2021-07-01 12:56:23 -0400 | [diff] [blame] | 183 | } |
| 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 Stiles | 6372752 | 2021-10-07 11:20:50 -0400 | [diff] [blame] | 187 | %s s = scale[2]; b = bias[2]; |
Brian Osman | 07bf388 | 2021-07-01 12:56:23 -0400 | [diff] [blame] | 188 | } else { |
John Stiles | 6372752 | 2021-10-07 11:20:50 -0400 | [diff] [blame] | 189 | %s s = scale[3]; b = bias[3]; |
Brian Osman | 07bf388 | 2021-07-01 12:56:23 -0400 | [diff] [blame] | 190 | } |
| 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 Stiles | 6372752 | 2021-10-07 11:20:50 -0400 | [diff] [blame] | 197 | %s s = scale[4]; b = bias[4]; |
Brian Osman | 07bf388 | 2021-07-01 12:56:23 -0400 | [diff] [blame] | 198 | } else { |
John Stiles | 6372752 | 2021-10-07 11:20:50 -0400 | [diff] [blame] | 199 | %s s = scale[5]; b = bias[5]; |
Brian Osman | 07bf388 | 2021-07-01 12:56:23 -0400 | [diff] [blame] | 200 | } |
| 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 Stiles | 6372752 | 2021-10-07 11:20:50 -0400 | [diff] [blame] | 204 | %s s = scale[6]; b = bias[6]; |
Brian Osman | 07bf388 | 2021-07-01 12:56:23 -0400 | [diff] [blame] | 205 | } else { |
John Stiles | 6372752 | 2021-10-07 11:20:50 -0400 | [diff] [blame] | 206 | %s s = scale[7]; b = bias[7]; |
Brian Osman | 07bf388 | 2021-07-01 12:56:23 -0400 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | } |
John Stiles | 6372752 | 2021-10-07 11:20:50 -0400 | [diff] [blame] | 210 | 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 Osman | 07bf388 | 2021-07-01 12:56:23 -0400 | [diff] [blame] | 227 | |
| 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 Osman | 07bf388 | 2021-07-01 12:56:23 -0400 | [diff] [blame] | 233 | return GrSkSLFP::Make(effects[intervalCount - 1], "UnrolledBinaryColorizer", |
| 234 | /*inputFP=*/nullptr, GrSkSLFP::OptFlags::kNone, |
| 235 | "thresholds1_7", thresholds1_7, |
| 236 | "thresholds9_13", thresholds9_13, |
John Stiles | 6372752 | 2021-10-07 11:20:50 -0400 | [diff] [blame] | 237 | "scale", SkMakeSpan(scale, intervalCount), |
| 238 | "bias", SkMakeSpan(bias, intervalCount)); |
Brian Osman | 07bf388 | 2021-07-01 12:56:23 -0400 | [diff] [blame] | 239 | } |
| 240 | |
John Stiles | 6030e0a | 2021-10-08 16:23:05 -0400 | [diff] [blame] | 241 | // 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. |
| 243 | int 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 Osman | 07bf388 | 2021-07-01 12:56:23 -0400 | [diff] [blame] | 250 | // 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 Osman | 07bf388 | 2021-07-01 12:56:23 -0400 | [diff] [blame] | 255 | int intervalCount = 0; |
John Stiles | 6030e0a | 2021-10-08 16:23:05 -0400 | [diff] [blame] | 256 | 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 Osman | 07bf388 | 2021-07-01 12:56:23 -0400 | [diff] [blame] | 261 | } |
| 262 | |
John Stiles | 6030e0a | 2021-10-08 16:23:05 -0400 | [diff] [blame] | 263 | SkScalar t0 = inPositions[i]; |
| 264 | SkScalar t1 = inPositions[i + 1]; |
Brian Osman | 07bf388 | 2021-07-01 12:56:23 -0400 | [diff] [blame] | 265 | 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 Stiles | 6030e0a | 2021-10-08 16:23:05 -0400 | [diff] [blame] | 273 | 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 Osman | 07bf388 | 2021-07-01 12:56:23 -0400 | [diff] [blame] | 277 | |
John Stiles | 6030e0a | 2021-10-08 16:23:05 -0400 | [diff] [blame] | 278 | scale.store(outScales + intervalCount); |
| 279 | bias.store(outBiases + intervalCount); |
| 280 | outThresholds[intervalCount] = t1; |
Brian Osman | 07bf388 | 2021-07-01 12:56:23 -0400 | [diff] [blame] | 281 | intervalCount++; |
| 282 | } |
John Stiles | 6030e0a | 2021-10-08 16:23:05 -0400 | [diff] [blame] | 283 | return intervalCount; |
| 284 | } |
| 285 | |
| 286 | static 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 Osman | 07bf388 | 2021-07-01 12:56:23 -0400 | [diff] [blame] | 301 | |
| 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 Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 308 | // Analyze the shader's color stops and positions and chooses an appropriate colorizer to represent |
| 309 | // the gradient. |
Brian Osman | 021ed51 | 2018-10-16 15:19:44 -0400 | [diff] [blame] | 310 | static std::unique_ptr<GrFragmentProcessor> make_colorizer(const SkPMColor4f* colors, |
Michael Ludwig | a7914d3 | 2018-09-14 09:47:21 -0400 | [diff] [blame] | 311 | const SkScalar* positions, int count, bool premul, const GrFPArgs& args) { |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 312 | // 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 Lubick | be03ef1 | 2021-06-16 15:28:00 -0400 | [diff] [blame] | 317 | // SkGradientShaderBase guarantees that pos[0] == 0 by adding a default value. |
Michael Ludwig | 0495f7a | 2018-09-12 15:23:33 -0400 | [diff] [blame] | 318 | bool bottomHardStop = SkScalarNearlyEqual(positions[0], positions[1]); |
| 319 | // The same is true for pos[end] == 1 |
Michael Ludwig | a7914d3 | 2018-09-14 09:47:21 -0400 | [diff] [blame] | 320 | bool topHardStop = SkScalarNearlyEqual(positions[count - 2], positions[count - 1]); |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 321 | |
| 322 | int offset = 0; |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 323 | if (bottomHardStop) { |
| 324 | offset += 1; |
| 325 | count--; |
| 326 | } |
| 327 | if (topHardStop) { |
| 328 | count--; |
| 329 | } |
| 330 | |
Michael Ludwig | 0495f7a | 2018-09-12 15:23:33 -0400 | [diff] [blame] | 331 | // 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 Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 333 | if (count == 2) { |
Brian Osman | 65b4597 | 2021-06-25 15:58:43 +0000 | [diff] [blame] | 334 | return make_single_interval_colorizer(colors[offset], colors[offset + 1]); |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 335 | } |
| 336 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 337 | const GrShaderCaps* caps = args.fContext->priv().caps()->shaderCaps(); |
John Stiles | 6030e0a | 2021-10-08 16:23:05 -0400 | [diff] [blame] | 338 | 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 Stiles | 4e6353d | 2021-10-08 17:33:25 -0400 | [diff] [blame] | 348 | for (int i = 0; i < count - 1; i++) { |
| 349 | SkScalar dt = SkScalarAbs(positions[offset + i] - positions[offset + i + 1]); |
John Stiles | 6030e0a | 2021-10-08 16:23:05 -0400 | [diff] [blame] | 350 | if (dt <= kLowPrecisionIntervalLimit && dt > SK_ScalarNearlyZero) { |
| 351 | return true; |
| 352 | } |
Michael Ludwig | 72535fb | 2018-09-28 11:53:32 -0400 | [diff] [blame] | 353 | } |
| 354 | } |
John Stiles | 6030e0a | 2021-10-08 16:23:05 -0400 | [diff] [blame] | 355 | return false; |
| 356 | }; |
Michael Ludwig | 72535fb | 2018-09-28 11:53:32 -0400 | [diff] [blame] | 357 | |
John Stiles | 6030e0a | 2021-10-08 16:23:05 -0400 | [diff] [blame] | 358 | 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 Ludwig | 72535fb | 2018-09-28 11:53:32 -0400 | [diff] [blame] | 361 | if (count == 3) { |
John Stiles | 6030e0a | 2021-10-08 16:23:05 -0400 | [diff] [blame] | 362 | // 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 Osman | 65b4597 | 2021-06-25 15:58:43 +0000 | [diff] [blame] | 364 | return make_dual_interval_colorizer(colors[offset], colors[offset + 1], |
| 365 | colors[offset + 1], colors[offset + 2], |
| 366 | positions[offset + 1]); |
John Stiles | 6030e0a | 2021-10-08 16:23:05 -0400 | [diff] [blame] | 367 | } |
| 368 | if (count == 4 && SkScalarNearlyEqual(positions[offset + 1], |
Michael Ludwig | 72535fb | 2018-09-28 11:53:32 -0400 | [diff] [blame] | 369 | positions[offset + 2])) { |
| 370 | // Two separate intervals that join at the same threshold position |
Brian Osman | 65b4597 | 2021-06-25 15:58:43 +0000 | [diff] [blame] | 371 | return make_dual_interval_colorizer(colors[offset], colors[offset + 1], |
| 372 | colors[offset + 2], colors[offset + 3], |
| 373 | positions[offset + 1]); |
Michael Ludwig | 72535fb | 2018-09-28 11:53:32 -0400 | [diff] [blame] | 374 | } |
John Stiles | 6030e0a | 2021-10-08 16:23:05 -0400 | [diff] [blame] | 375 | // The gradient can't be represented in only two intervals. |
| 376 | return nullptr; |
| 377 | }; |
Michael Ludwig | 72535fb | 2018-09-28 11:53:32 -0400 | [diff] [blame] | 378 | |
John Stiles | 6030e0a | 2021-10-08 16:23:05 -0400 | [diff] [blame] | 379 | // 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 Ludwig | 72535fb | 2018-09-28 11:53:32 -0400 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
| 391 | // Otherwise fall back to a rasterized gradient sampled by a texture, which can handle |
John Stiles | 6030e0a | 2021-10-08 16:23:05 -0400 | [diff] [blame] | 392 | // arbitrary gradients. (This has limited sampling resolution, and always blurs hard-stops.) |
Michael Ludwig | a7914d3 | 2018-09-14 09:47:21 -0400 | [diff] [blame] | 393 | return make_textured_colorizer(colors + offset, positions + offset, count, premul, args); |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 394 | } |
| 395 | |
Brian Osman | c6804ed | 2021-06-29 11:11:49 -0400 | [diff] [blame] | 396 | // 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. |
| 404 | static 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 Osman | cbfa34a | 2021-09-02 09:26:27 -0400 | [diff] [blame] | 422 | half4 t = gradLayout.eval(coord); |
Brian Osman | c6804ed | 2021-06-29 11:11:49 -0400 | [diff] [blame] | 423 | 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 Osman | cbfa34a | 2021-09-02 09:26:27 -0400 | [diff] [blame] | 439 | outColor = colorizer.eval(t.x0); |
Brian Osman | c6804ed | 2021-06-29 11:11:49 -0400 | [diff] [blame] | 440 | } |
| 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 | |
| 467 | static 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 Osman | cbfa34a | 2021-09-02 09:26:27 -0400 | [diff] [blame] | 484 | half4 t = gradLayout.eval(coord); |
Brian Osman | c6804ed | 2021-06-29 11:11:49 -0400 | [diff] [blame] | 485 | |
| 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 Osman | cbfa34a | 2021-09-02 09:26:27 -0400 | [diff] [blame] | 508 | half4 outColor = colorizer.eval(t.x0); |
Brian Osman | c6804ed | 2021-06-29 11:11:49 -0400 | [diff] [blame] | 509 | 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 Ludwig | 672c476 | 2020-08-05 17:28:42 -0400 | [diff] [blame] | 539 | // Combines the colorizer and layout with an appropriately configured top-level effect based on the |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 540 | // gradient's tile mode |
Brian Osman | 65b4597 | 2021-06-25 15:58:43 +0000 | [diff] [blame] | 541 | static 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 Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 546 | // 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 Osman | 65b4597 | 2021-06-25 15:58:43 +0000 | [diff] [blame] | 552 | // 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 Osman | 021ed51 | 2018-10-16 15:19:44 -0400 | [diff] [blame] | 561 | // Convert all colors into destination space and into SkPMColor4fs, and handle |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 562 | // premul issues depending on the interpolation mode |
| 563 | bool inputPremul = shader.getGradFlags() & SkGradientShader::kInterpolateColorsInPremul_Flag; |
Michael Ludwig | b96cba3 | 2018-09-14 13:59:24 -0400 | [diff] [blame] | 564 | bool allOpaque = true; |
Brian Osman | 021ed51 | 2018-10-16 15:19:44 -0400 | [diff] [blame] | 565 | SkAutoSTMalloc<4, SkPMColor4f> colors(shader.fColorCount); |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 566 | SkColor4fXformer xformedColors(shader.fOrigColors4f, shader.fColorCount, |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 567 | shader.fColorSpace.get(), args.fDstColorInfo->colorSpace()); |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 568 | for (int i = 0; i < shader.fColorCount; i++) { |
Brian Osman | 021ed51 | 2018-10-16 15:19:44 -0400 | [diff] [blame] | 569 | 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 Ludwig | b96cba3 | 2018-09-14 13:59:24 -0400 | [diff] [blame] | 573 | allOpaque = false; |
| 574 | } |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 575 | } |
| 576 | |
Michael Ludwig | 0495f7a | 2018-09-12 15:23:33 -0400 | [diff] [blame] | 577 | // 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 Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 585 | implicitPos.reserve_back(shader.fColorCount); |
Mike Klein | d3ed301 | 2018-11-06 19:23:08 -0500 | [diff] [blame] | 586 | SkScalar posScale = SK_Scalar1 / (shader.fColorCount - 1); |
Michael Ludwig | 0495f7a | 2018-09-12 15:23:33 -0400 | [diff] [blame] | 587 | for (int i = 0 ; i < shader.fColorCount; i++) { |
| 588 | implicitPos.push_back(SkIntToScalar(i) * posScale); |
| 589 | } |
| 590 | positions = implicitPos.begin(); |
| 591 | } |
| 592 | |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 593 | // All gradients are colorized the same way, regardless of layout |
Michael Ludwig | a7914d3 | 2018-09-14 09:47:21 -0400 | [diff] [blame] | 594 | std::unique_ptr<GrFragmentProcessor> colorizer = make_colorizer( |
| 595 | colors.get(), positions, shader.fColorCount, inputPremul, args); |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 596 | if (colorizer == nullptr) { |
| 597 | return nullptr; |
| 598 | } |
| 599 | |
Michael Ludwig | 672c476 | 2020-08-05 17:28:42 -0400 | [diff] [blame] | 600 | // 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 Ludwig | b96cba3 | 2018-09-14 13:59:24 -0400 | [diff] [blame] | 606 | bool makePremul = !inputPremul && !allOpaque; |
| 607 | |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 608 | // All tile modes are supported (unless something was added to SkShader) |
Michael Ludwig | 672c476 | 2020-08-05 17:28:42 -0400 | [diff] [blame] | 609 | std::unique_ptr<GrFragmentProcessor> gradient; |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 610 | switch(shader.getTileMode()) { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 611 | case SkTileMode::kRepeat: |
Brian Osman | c6804ed | 2021-06-29 11:11:49 -0400 | [diff] [blame] | 612 | gradient = make_tiled_gradient(args, std::move(colorizer), std::move(layout), |
| 613 | /* mirror */ false, makePremul, allOpaque); |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 614 | break; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 615 | case SkTileMode::kMirror: |
Brian Osman | c6804ed | 2021-06-29 11:11:49 -0400 | [diff] [blame] | 616 | gradient = make_tiled_gradient(args, std::move(colorizer), std::move(layout), |
| 617 | /* mirror */ true, makePremul, allOpaque); |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 618 | break; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 619 | case SkTileMode::kClamp: |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 620 | // 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 Osman | c6804ed | 2021-06-29 11:11:49 -0400 | [diff] [blame] | 624 | gradient = make_clamped_gradient(std::move(colorizer), std::move(layout), |
| 625 | colors[0], colors[shader.fColorCount - 1], |
| 626 | makePremul, allOpaque); |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 627 | break; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 628 | case SkTileMode::kDecal: |
Michael Ludwig | b96cba3 | 2018-09-14 13:59:24 -0400 | [diff] [blame] | 629 | // Even if the gradient colors are opaque, the decal borders are transparent so |
| 630 | // disable that optimization |
Brian Osman | c6804ed | 2021-06-29 11:11:49 -0400 | [diff] [blame] | 631 | gradient = make_clamped_gradient(std::move(colorizer), std::move(layout), |
| 632 | SK_PMColor4fTRANSPARENT, SK_PMColor4fTRANSPARENT, |
| 633 | makePremul, /* colorsAreOpaque */ false); |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 634 | break; |
| 635 | } |
| 636 | |
Michael Ludwig | 672c476 | 2020-08-05 17:28:42 -0400 | [diff] [blame] | 637 | if (gradient == nullptr) { |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 638 | // Unexpected tile mode |
| 639 | return nullptr; |
| 640 | } |
Brian Salomon | c0d79e5 | 2019-04-10 15:02:11 -0400 | [diff] [blame] | 641 | if (args.fInputColorIsOpaque) { |
Brian Osman | 55cbc75 | 2021-05-27 11:57:13 -0400 | [diff] [blame] | 642 | // 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 Ludwig | 672c476 | 2020-08-05 17:28:42 -0400 | [diff] [blame] | 647 | return GrFragmentProcessor::OverrideInput(std::move(gradient), SK_PMColor4fWHITE, false); |
Brian Salomon | c0d79e5 | 2019-04-10 15:02:11 -0400 | [diff] [blame] | 648 | } |
Michael Ludwig | 672c476 | 2020-08-05 17:28:42 -0400 | [diff] [blame] | 649 | return GrFragmentProcessor::MulChildByInputAlpha(std::move(gradient)); |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | namespace GrGradientShader { |
| 653 | |
| 654 | std::unique_ptr<GrFragmentProcessor> MakeLinear(const SkLinearGradient& shader, |
| 655 | const GrFPArgs& args) { |
Brian Osman | 65b4597 | 2021-06-25 15:58:43 +0000 | [diff] [blame] | 656 | // 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 Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 672 | } |
| 673 | |
Michael Ludwig | 4089df8 | 2018-09-12 15:22:37 -0400 | [diff] [blame] | 674 | std::unique_ptr<GrFragmentProcessor> MakeRadial(const SkRadialGradient& shader, |
| 675 | const GrFPArgs& args) { |
Brian Osman | 65b4597 | 2021-06-25 15:58:43 +0000 | [diff] [blame] | 676 | 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 Ludwig | 4089df8 | 2018-09-12 15:22:37 -0400 | [diff] [blame] | 685 | } |
| 686 | |
Michael Ludwig | 24d438b | 2018-09-12 15:22:50 -0400 | [diff] [blame] | 687 | std::unique_ptr<GrFragmentProcessor> MakeSweep(const SkSweepGradient& shader, |
| 688 | const GrFPArgs& args) { |
Brian Osman | 65b4597 | 2021-06-25 15:58:43 +0000 | [diff] [blame] | 689 | // 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 Ludwig | 24d438b | 2018-09-12 15:22:50 -0400 | [diff] [blame] | 718 | } |
| 719 | |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 720 | std::unique_ptr<GrFragmentProcessor> MakeConical(const SkTwoPointConicalGradient& shader, |
| 721 | const GrFPArgs& args) { |
Brian Osman | 65b4597 | 2021-06-25 15:58:43 +0000 | [diff] [blame] | 722 | // 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 Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 866 | } |
| 867 | |
Michael Ludwig | 7f8c524 | 2018-09-14 15:07:55 -0400 | [diff] [blame] | 868 | #if GR_TEST_UTILS |
| 869 | RandomParams::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 Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 902 | fTileMode = static_cast<SkTileMode>(random->nextULessThan(kSkTileModeCount)); |
Michael Ludwig | 7f8c524 | 2018-09-14 15:07:55 -0400 | [diff] [blame] | 903 | } |
| 904 | #endif |
| 905 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 906 | } // namespace GrGradientShader |