| 1 |
/* |
| 2 |
* Copyright 2024 Google LLC |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "src/core/SkKnownRuntimeEffects.h" |
| 9 |
|
| 10 |
#include "include/core/SkString.h" |
| 11 |
#include "include/effects/SkRuntimeEffect.h" |
| 12 |
#include "include/private/base/SkAssert.h" |
| 13 |
#include "src/core/SkRuntimeEffectPriv.h" |
| 14 |
#include "src/effects/imagefilters/SkMatrixConvolutionImageFilter.h" |
| 15 |
|
| 16 |
namespace SkKnownRuntimeEffects { |
| 17 |
|
| 18 |
namespace { |
| 19 |
|
| 20 |
SkRuntimeEffect::Options get_options(StableKey stableKey) { |
| 21 |
SkRuntimeEffect::Options options; |
| 22 |
SkRuntimeEffectPriv::SetStableKeyOnOptions(&options, static_cast<uint32_t>(stableKey)); |
| 23 |
SkRuntimeEffectPriv::AllowPrivateAccess(&options); |
| 24 |
return options; |
| 25 |
} |
| 26 |
|
| 27 |
// This must be kept in sync w/ the version in BlurUtils.h |
| 28 |
static constexpr int kMaxBlurSamples = 28; |
| 29 |
|
| 30 |
SkRuntimeEffect* make_blur_1D_shader(int kernelWidth, StableKey stableKey) { |
| 31 |
SkRuntimeEffect::Options options = get_options(stableKey); |
| 32 |
|
| 33 |
SkASSERT(kernelWidth <= kMaxBlurSamples); |
| 34 |
// The SkSL structure performs two kernel taps; if the kernel has an odd width the last |
| 35 |
// sample will be skipped with the current loop limit calculation. |
| 36 |
SkASSERT(kernelWidth % 2 == 0); |
| 37 |
return SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, |
| 38 |
SkStringPrintf( |
| 39 |
// The coefficients are always stored for the max radius to keep the |
| 40 |
// uniform block consistent across all effects. |
| 41 |
"const int kMaxUniformKernelSize = %d / 2;" |
| 42 |
// But we generate an exact loop over the kernel size. Note that this |
| 43 |
// program can be used for kernels smaller than the constructed max as long |
| 44 |
// as the kernel weights for excess entries are set to 0. |
| 45 |
"const int kMaxLoopLimit = %d / 2;" |
| 46 |
|
| 47 |
"uniform half4 offsetsAndKernel[kMaxUniformKernelSize];" |
| 48 |
"uniform half2 dir;" |
| 49 |
|
| 50 |
"uniform shader child;" |
| 51 |
|
| 52 |
"half4 main(float2 coord) {" |
| 53 |
"half4 sum = half4(0);" |
| 54 |
"for (int i = 0; i < kMaxLoopLimit; ++i) {" |
| 55 |
"half4 s = offsetsAndKernel[i];" |
| 56 |
"sum += s.y * child.eval(coord + s.x*dir);" |
| 57 |
"sum += s.w * child.eval(coord + s.z*dir);" |
| 58 |
"}" |
| 59 |
"return sum;" |
| 60 |
"}", kMaxBlurSamples, kernelWidth).c_str(), |
| 61 |
options); |
| 62 |
} |
| 63 |
|
| 64 |
SkRuntimeEffect* make_blur_2D_shader(int maxKernelSize, StableKey stableKey) { |
| 65 |
SkRuntimeEffect::Options options = get_options(stableKey); |
| 66 |
|
| 67 |
SkASSERT(maxKernelSize % 4 == 0); |
| 68 |
return SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, |
| 69 |
SkStringPrintf( |
| 70 |
// The coefficients are always stored for the max radius to keep the |
| 71 |
// uniform block consistent across all effects. |
| 72 |
"const int kMaxUniformKernelSize = %d / 4;" |
| 73 |
"const int kMaxUniformOffsetsSize = 2*kMaxUniformKernelSize;" |
| 74 |
// But we generate an exact loop over the kernel size. Note that this |
| 75 |
// program can be used for kernels smaller than the constructed max as long |
| 76 |
// as the kernel weights for excess entries are set to 0. |
| 77 |
"const int kMaxLoopLimit = %d / 4;" |
| 78 |
|
| 79 |
// Pack scalar coefficients into half4 for better packing on std140, and |
| 80 |
// upload offsets to avoid having to transform the 1D index into a 2D coord |
| 81 |
"uniform half4 kernel[kMaxUniformKernelSize];" |
| 82 |
"uniform half4 offsets[kMaxUniformOffsetsSize];" |
| 83 |
|
| 84 |
"uniform shader child;" |
| 85 |
|
| 86 |
"half4 main(float2 coord) {" |
| 87 |
"half4 sum = half4(0);" |
| 88 |
|
| 89 |
"for (int i = 0; i < kMaxLoopLimit; ++i) {" |
| 90 |
"half4 k = kernel[i];" |
| 91 |
"half4 o = offsets[2*i];" |
| 92 |
"sum += k.x * child.eval(coord + o.xy);" |
| 93 |
"sum += k.y * child.eval(coord + o.zw);" |
| 94 |
"o = offsets[2*i + 1];" |
| 95 |
"sum += k.z * child.eval(coord + o.xy);" |
| 96 |
"sum += k.w * child.eval(coord + o.zw);" |
| 97 |
"}" |
| 98 |
"return sum;" |
| 99 |
"}", kMaxBlurSamples, maxKernelSize).c_str(), |
| 100 |
options); |
| 101 |
} |
| 102 |
|
| 103 |
SkRuntimeEffect* make_blend_shader() { |
| 104 |
SkRuntimeEffect::Options options = get_options(StableKey::kBlend); |
| 105 |
|
| 106 |
static constexpr char kBlendShaderCode[] = |
| 107 |
"uniform shader s, d;" |
| 108 |
"uniform blender b;" |
| 109 |
"half4 main(float2 xy) {" |
| 110 |
"return b.eval(s.eval(xy), d.eval(xy));" |
| 111 |
"}"; |
| 112 |
|
| 113 |
return SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, |
| 114 |
kBlendShaderCode, |
| 115 |
options); |
| 116 |
} |
| 117 |
|
| 118 |
SkRuntimeEffect* make_lerp_shader() { |
| 119 |
SkRuntimeEffect::Options options = get_options(StableKey::kLerp); |
| 120 |
|
| 121 |
static constexpr char kLerpFilterCode[] = |
| 122 |
"uniform colorFilter cf0;" |
| 123 |
"uniform colorFilter cf1;" |
| 124 |
"uniform half weight;" |
| 125 |
|
| 126 |
"half4 main(half4 color) {" |
| 127 |
"return mix(cf0.eval(color), cf1.eval(color), weight);" |
| 128 |
"}"; |
| 129 |
|
| 130 |
return SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter, |
| 131 |
kLerpFilterCode, |
| 132 |
options); |
| 133 |
} |
| 134 |
|
| 135 |
enum class MatrixConvolutionImpl { |
| 136 |
kUniformBased, |
| 137 |
kTextureBasedSm, |
| 138 |
kTextureBasedLg, |
| 139 |
}; |
| 140 |
|
| 141 |
// There are three shader variants: |
| 142 |
// a smaller kernel version that stores the matrix in uniforms and iterates in 1D |
| 143 |
// a larger kernel version that stores the matrix in a 1D texture. The texture version has small |
| 144 |
// and large variants w/ the actual kernel size uploaded as a uniform. |
| 145 |
SkRuntimeEffect* make_matrix_conv_shader(MatrixConvolutionImpl impl, StableKey stableKey) { |
| 146 |
SkRuntimeEffect::Options options = get_options(stableKey); |
| 147 |
|
| 148 |
// While the uniforms and kernel access are different, pieces of the algorithm are common and |
| 149 |
// defined statically for re-use in the two shaders: |
| 150 |
static const char* kHeaderAndBeginLoopSkSL = |
| 151 |
"uniform int2 size;" |
| 152 |
"uniform int2 offset;" |
| 153 |
"uniform half2 gainAndBias;" |
| 154 |
"uniform int convolveAlpha;" // FIXME not a full int? Put in a half3 w/ gainAndBias? |
| 155 |
|
| 156 |
"uniform shader child;" |
| 157 |
|
| 158 |
"half4 main(float2 coord) {" |
| 159 |
"half4 sum = half4(0);" |
| 160 |
"half origAlpha = 0;" |
| 161 |
"int2 kernelPos = int2(0);" |
| 162 |
"for (int i = 0; i < kMaxKernelSize; ++i) {" |
| 163 |
"if (kernelPos.y >= size.y) { break; }"; |
| 164 |
|
| 165 |
// Used in the inner loop to accumulate convolution sum and increment the kernel position |
| 166 |
static const char* kAccumulateAndIncrementSkSL = |
| 167 |
"half4 c = child.eval(coord + half2(kernelPos) - half2(offset));" |
| 168 |
"if (convolveAlpha == 0) {" |
| 169 |
// When not convolving alpha, remember the original alpha for actual sample |
| 170 |
// coord, and perform accumulation on unpremul colors. |
| 171 |
"if (kernelPos == offset) {" |
| 172 |
"origAlpha = c.a;" |
| 173 |
"}" |
| 174 |
"c = unpremul(c);" |
| 175 |
"}" |
| 176 |
"sum += c*k;" |
| 177 |
"kernelPos.x += 1;" |
| 178 |
"if (kernelPos.x >= size.x) {" |
| 179 |
"kernelPos.x = 0;" |
| 180 |
"kernelPos.y += 1;" |
| 181 |
"}"; |
| 182 |
|
| 183 |
// Closes the loop and calculates final color |
| 184 |
static const char* kCloseLoopAndFooterSkSL = |
| 185 |
"}" |
| 186 |
"half4 color = sum*gainAndBias.x + gainAndBias.y;" |
| 187 |
"if (convolveAlpha == 0) {" |
| 188 |
// Reset the alpha to the original and convert to premul RGB |
| 189 |
"color = half4(color.rgb*origAlpha, origAlpha);" |
| 190 |
"} else {" |
| 191 |
// Ensure convolved alpha is within [0, 1] |
| 192 |
"color.a = saturate(color.a);" |
| 193 |
"}" |
| 194 |
// Make RGB valid premul w/ respect to the alpha (either original or convolved) |
| 195 |
"color.rgb = clamp(color.rgb, 0, color.a);" |
| 196 |
"return color;" |
| 197 |
"}"; |
| 198 |
|
| 199 |
static const auto makeTextureEffect = [](int maxTextureKernelSize, |
| 200 |
const SkRuntimeEffect::Options& options) { |
| 201 |
return SkMakeRuntimeEffect( |
| 202 |
SkRuntimeEffect::MakeForShader, |
| 203 |
SkStringPrintf("const int kMaxKernelSize = %d;" |
| 204 |
"uniform shader kernel;" |
| 205 |
"uniform half2 innerGainAndBias;" |
| 206 |
"%s" // kHeaderAndBeginLoopSkSL |
| 207 |
"half k = kernel.eval(half2(half(i) + 0.5, 0.5)).a;" |
| 208 |
"k = k * innerGainAndBias.x + innerGainAndBias.y;" |
| 209 |
"%s" // kAccumulateAndIncrementSkSL |
| 210 |
"%s", // kCloseLoopAndFooterSkSL |
| 211 |
maxTextureKernelSize, |
| 212 |
kHeaderAndBeginLoopSkSL, |
| 213 |
kAccumulateAndIncrementSkSL, |
| 214 |
kCloseLoopAndFooterSkSL).c_str(), |
| 215 |
options); |
| 216 |
}; |
| 217 |
|
| 218 |
switch (impl) { |
| 219 |
case MatrixConvolutionImpl::kUniformBased: { |
| 220 |
return SkMakeRuntimeEffect( |
| 221 |
SkRuntimeEffect::MakeForShader, |
| 222 |
SkStringPrintf("const int kMaxKernelSize = %d / 4;" |
| 223 |
"uniform half4 kernel[kMaxKernelSize];" |
| 224 |
"%s" // kHeaderAndBeginLoopSkSL |
| 225 |
"half4 k4 = kernel[i];" |
| 226 |
"for (int j = 0; j < 4; ++j) {" |
| 227 |
"if (kernelPos.y >= size.y) { break; }" |
| 228 |
"half k = k4[j];" |
| 229 |
"%s" // kAccumulateAndIncrementSkSL |
| 230 |
"}" |
| 231 |
"%s", // kCloseLoopAndFooterSkSL |
| 232 |
MatrixConvolutionImageFilter::kMaxUniformKernelSize, |
| 233 |
kHeaderAndBeginLoopSkSL, |
| 234 |
kAccumulateAndIncrementSkSL, |
| 235 |
kCloseLoopAndFooterSkSL).c_str(), |
| 236 |
options); |
| 237 |
} |
| 238 |
case MatrixConvolutionImpl::kTextureBasedSm: |
| 239 |
return makeTextureEffect(MatrixConvolutionImageFilter::kSmallKernelSize, options); |
| 240 |
case MatrixConvolutionImpl::kTextureBasedLg: |
| 241 |
return makeTextureEffect(MatrixConvolutionImageFilter::kLargeKernelSize, options); |
| 242 |
} |
| 243 |
|
| 244 |
SkUNREACHABLE; |
| 245 |
} |
| 246 |
|
| 247 |
SkRuntimeEffect* make_decal_shader() { |
| 248 |
SkRuntimeEffect::Options options = get_options(StableKey::kDecal); |
| 249 |
|
| 250 |
static constexpr char kDecalShaderCode[] = |
| 251 |
"uniform shader image;" |
| 252 |
"uniform float4 decalBounds;" |
| 253 |
|
| 254 |
"half4 main(float2 coord) {" |
| 255 |
"return sk_decal(image, coord, decalBounds);" |
| 256 |
"}"; |
| 257 |
|
| 258 |
return SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, |
| 259 |
kDecalShaderCode, |
| 260 |
options); |
| 261 |
} |
| 262 |
|
| 263 |
SkRuntimeEffect* make_displacement_shader() { |
| 264 |
SkRuntimeEffect::Options options = get_options(StableKey::kDisplacement); |
| 265 |
|
| 266 |
// NOTE: This uses dot product selection to work on all GLES2 hardware (enforced by |
| 267 |
// public runtime effect restrictions). Otherwise, this would use a "uniform ivec2" |
| 268 |
// and component indexing to convert the displacement color into a vector. |
| 269 |
static constexpr char kDisplacementShaderCode[] = |
| 270 |
"uniform shader displMap;" |
| 271 |
"uniform shader colorMap;" |
| 272 |
"uniform half2 scale;" |
| 273 |
"uniform half4 xSelect;" // Only one of RGBA will be 1, the rest are 0 |
| 274 |
"uniform half4 ySelect;" |
| 275 |
|
| 276 |
"half4 main(float2 coord) {" |
| 277 |
"return sk_displacement(displMap, colorMap, coord, scale, xSelect, ySelect);" |
| 278 |
"}"; |
| 279 |
|
| 280 |
return SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, |
| 281 |
kDisplacementShaderCode, |
| 282 |
options); |
| 283 |
} |
| 284 |
|
| 285 |
SkRuntimeEffect* make_lighting_shader() { |
| 286 |
SkRuntimeEffect::Options options = get_options(StableKey::kLighting); |
| 287 |
|
| 288 |
static constexpr char kLightingShaderCode[] = |
| 289 |
"uniform shader normalMap;" |
| 290 |
|
| 291 |
// Packs surface depth, shininess, material type (0 == diffuse) and light type |
| 292 |
// (< 0 = distant, 0 = point, > 0 = spot) |
| 293 |
"uniform half4 materialAndLightType;" |
| 294 |
|
| 295 |
"uniform half4 lightPosAndSpotFalloff;" // (x,y,z) are lightPos, w is spot falloff |
| 296 |
// exponent |
| 297 |
"uniform half4 lightDirAndSpotCutoff;" // (x,y,z) are lightDir, |
| 298 |
// w is spot cos(cutoffAngle) |
| 299 |
"uniform half3 lightColor;" // Material's k has already been multiplied in |
| 300 |
|
| 301 |
"half4 main(float2 coord) {" |
| 302 |
"return sk_lighting(normalMap, coord," |
| 303 |
/*depth=*/"materialAndLightType.x," |
| 304 |
/*shininess=*/"materialAndLightType.y," |
| 305 |
/*materialType=*/"materialAndLightType.z," |
| 306 |
/*lightType=*/"materialAndLightType.w," |
| 307 |
/*lightPos=*/"lightPosAndSpotFalloff.xyz," |
| 308 |
/*spotFalloff=*/"lightPosAndSpotFalloff.w," |
| 309 |
/*lightDir=*/"lightDirAndSpotCutoff.xyz," |
| 310 |
/*cosCutoffAngle=*/"lightDirAndSpotCutoff.w," |
| 311 |
"lightColor);" |
| 312 |
"}"; |
| 313 |
|
| 314 |
return SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, |
| 315 |
kLightingShaderCode, |
| 316 |
options); |
| 317 |
} |
| 318 |
|
| 319 |
SkRuntimeEffect* make_linear_morphology_shader() { |
| 320 |
SkRuntimeEffect::Options options = get_options(StableKey::kLinearMorphology); |
| 321 |
|
| 322 |
static constexpr char kLinearMorphologyShaderCode[] = |
| 323 |
"uniform shader child;" |
| 324 |
"uniform half2 offset;" |
| 325 |
"uniform half flip;" // -1 converts the max() calls to min() |
| 326 |
"uniform int radius;" |
| 327 |
|
| 328 |
"half4 main(float2 coord) {" |
| 329 |
"return sk_linear_morphology(child, coord, offset, flip, radius);" |
| 330 |
"}"; |
| 331 |
|
| 332 |
return SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, |
| 333 |
kLinearMorphologyShaderCode, |
| 334 |
options); |
| 335 |
} |
| 336 |
|
| 337 |
SkRuntimeEffect* make_magnifier_shader() { |
| 338 |
SkRuntimeEffect::Options options = get_options(StableKey::kMagnifier); |
| 339 |
|
| 340 |
static constexpr char kMagnifierShaderCode[] = |
| 341 |
"uniform shader src;" |
| 342 |
"uniform float4 lensBounds;" |
| 343 |
"uniform float4 zoomXform;" |
| 344 |
"uniform float2 invInset;" |
| 345 |
|
| 346 |
"half4 main(float2 coord) {" |
| 347 |
"return sk_magnifier(src, coord, lensBounds, zoomXform, invInset);" |
| 348 |
"}"; |
| 349 |
|
| 350 |
return SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, |
| 351 |
kMagnifierShaderCode, |
| 352 |
options); |
| 353 |
} |
| 354 |
|
| 355 |
SkRuntimeEffect* make_normal_shader() { |
| 356 |
SkRuntimeEffect::Options options = get_options(StableKey::kNormal); |
| 357 |
|
| 358 |
static constexpr char kNormalShaderCode[] = |
| 359 |
"uniform shader alphaMap;" |
| 360 |
"uniform float4 edgeBounds;" |
| 361 |
"uniform half negSurfaceDepth;" |
| 362 |
|
| 363 |
"half4 main(float2 coord) {" |
| 364 |
"return sk_normal(alphaMap, coord, edgeBounds, negSurfaceDepth);" |
| 365 |
"}"; |
| 366 |
|
| 367 |
return SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, |
| 368 |
kNormalShaderCode, |
| 369 |
options); |
| 370 |
} |
| 371 |
|
| 372 |
SkRuntimeEffect* make_sparse_morphology_shader() { |
| 373 |
SkRuntimeEffect::Options options = get_options(StableKey::kSparseMorphology); |
| 374 |
|
| 375 |
static constexpr char kSparseMorphologyShaderCode[] = |
| 376 |
"uniform shader child;" |
| 377 |
"uniform half2 offset;" |
| 378 |
"uniform half flip;" |
| 379 |
|
| 380 |
"half4 main(float2 coord) {" |
| 381 |
"return sk_sparse_morphology(child, coord, offset, flip);" |
| 382 |
"}"; |
| 383 |
|
| 384 |
return SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, |
| 385 |
kSparseMorphologyShaderCode, |
| 386 |
options); |
| 387 |
} |
| 388 |
|
| 389 |
SkRuntimeEffect* make_arithmetic_blender() { |
| 390 |
SkRuntimeEffect::Options options = get_options(StableKey::kArithmetic); |
| 391 |
|
| 392 |
static constexpr char kArithmeticBlenderCode[] = |
| 393 |
"uniform half4 k;" |
| 394 |
"uniform half pmClamp;" |
| 395 |
|
| 396 |
"half4 main(half4 src, half4 dst) {" |
| 397 |
"return sk_arithmetic_blend(src, dst, k, pmClamp);" |
| 398 |
"}"; |
| 399 |
|
| 400 |
return SkMakeRuntimeEffect(SkRuntimeEffect::MakeForBlender, |
| 401 |
kArithmeticBlenderCode, |
| 402 |
options); |
| 403 |
} |
| 404 |
|
| 405 |
SkRuntimeEffect* make_high_contrast_color_filter() { |
| 406 |
SkRuntimeEffect::Options options = get_options(StableKey::kHighContrast); |
| 407 |
|
| 408 |
static constexpr char kHighContrastFilterCode[] = |
| 409 |
"uniform half grayscale, invertStyle, contrast;" |
| 410 |
"half4 main(half4 color) {" |
| 411 |
"return half4(sk_high_contrast(color.rgb, grayscale, invertStyle, contrast), color.a);" |
| 412 |
"}"; |
| 413 |
|
| 414 |
return SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter, |
| 415 |
kHighContrastFilterCode, |
| 416 |
options); |
| 417 |
} |
| 418 |
|
| 419 |
SkRuntimeEffect* make_luma_color_filter() { |
| 420 |
SkRuntimeEffect::Options options = get_options(StableKey::kLuma); |
| 421 |
|
| 422 |
static constexpr char kLumaFilterCode[] = |
| 423 |
"half4 main(half4 color) {" |
| 424 |
"return sk_luma(color.rgb);" |
| 425 |
"}"; |
| 426 |
|
| 427 |
return SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter, |
| 428 |
kLumaFilterCode, |
| 429 |
options); |
| 430 |
} |
| 431 |
|
| 432 |
SkRuntimeEffect* make_overdraw_color_filter() { |
| 433 |
SkRuntimeEffect::Options options = get_options(StableKey::kOverdraw); |
| 434 |
|
| 435 |
static constexpr char kOverdrawFilterCode[] = |
| 436 |
"uniform half4 color0, color1, color2, color3, color4, color5;" |
| 437 |
|
| 438 |
"half4 main(half4 color) {" |
| 439 |
"return sk_overdraw(color.a, color0, color1, color2, color3, color4, color5);" |
| 440 |
"}"; |
| 441 |
|
| 442 |
return SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter, |
| 443 |
kOverdrawFilterCode, |
| 444 |
options); |
| 445 |
} |
| 446 |
|
| 447 |
} // anonymous namespace |
| 448 |
|
| 449 |
bool IsSkiaKnownRuntimeEffect(int candidate) { |
| 450 |
return (candidate >= static_cast<int>(StableKey::kStart) && |
| 451 |
candidate <= static_cast<int>(StableKey::kLast)); |
| 452 |
} |
| 453 |
|
| 454 |
bool IsUserDefinedRuntimeEffect(int candidate) { |
| 455 |
return candidate >= kUnknownRuntimeEffectIDStart; |
| 456 |
} |
| 457 |
|
| 458 |
bool IsViableUserDefinedKnownRuntimeEffect(int candidate) { |
| 459 |
return candidate >= kUserDefinedKnownRuntimeEffectsStart && |
| 460 |
candidate < kUserDefinedKnownRuntimeEffectsEnd; |
| 461 |
} |
| 462 |
|
| 463 |
sk_sp<SkRuntimeEffect> MaybeGetKnownRuntimeEffect(uint32_t candidate) { |
| 464 |
if (IsSkiaKnownRuntimeEffect(candidate)) { |
| 465 |
SkKnownRuntimeEffects::StableKey stableKey = |
| 466 |
static_cast<SkKnownRuntimeEffects::StableKey>(candidate); |
| 467 |
|
| 468 |
return sk_ref_sp(GetKnownRuntimeEffect(stableKey)); |
| 469 |
} |
| 470 |
|
| 471 |
return nullptr; |
| 472 |
} |
| 473 |
|
| 474 |
const SkRuntimeEffect* GetKnownRuntimeEffect(StableKey stableKey) { |
| 475 |
switch (stableKey) { |
| 476 |
case StableKey::kInvalid: |
| 477 |
return nullptr; |
| 478 |
|
| 479 |
// Shaders |
| 480 |
case StableKey::k1DBlur4: { |
| 481 |
static SkRuntimeEffect* s1DBlurEffect = make_blur_1D_shader(4, stableKey); |
| 482 |
return s1DBlurEffect; |
| 483 |
} |
| 484 |
case StableKey::k1DBlur8: { |
| 485 |
static SkRuntimeEffect* s1DBlurEffect = make_blur_1D_shader(8, stableKey); |
| 486 |
return s1DBlurEffect; |
| 487 |
} |
| 488 |
case StableKey::k1DBlur12: { |
| 489 |
static SkRuntimeEffect* s1DBlurEffect = make_blur_1D_shader(12, stableKey); |
| 490 |
return s1DBlurEffect; |
| 491 |
} |
| 492 |
case StableKey::k1DBlur16: { |
| 493 |
static SkRuntimeEffect* s1DBlurEffect = make_blur_1D_shader(16, stableKey); |
| 494 |
return s1DBlurEffect; |
| 495 |
} |
| 496 |
case StableKey::k1DBlur20: { |
| 497 |
static SkRuntimeEffect* s1DBlurEffect = make_blur_1D_shader(20, stableKey); |
| 498 |
return s1DBlurEffect; |
| 499 |
} |
| 500 |
case StableKey::k1DBlur28: { |
| 501 |
static SkRuntimeEffect* s1DBlurEffect = make_blur_1D_shader(28, stableKey); |
| 502 |
return s1DBlurEffect; |
| 503 |
} |
| 504 |
case StableKey::k2DBlur4: { |
| 505 |
static SkRuntimeEffect* s2DBlurEffect = make_blur_2D_shader(4, stableKey); |
| 506 |
return s2DBlurEffect; |
| 507 |
} |
| 508 |
case StableKey::k2DBlur8: { |
| 509 |
static SkRuntimeEffect* s2DBlurEffect = make_blur_2D_shader(8, stableKey); |
| 510 |
return s2DBlurEffect; |
| 511 |
} |
| 512 |
case StableKey::k2DBlur12: { |
| 513 |
static SkRuntimeEffect* s2DBlurEffect = make_blur_2D_shader(12, stableKey); |
| 514 |
return s2DBlurEffect; |
| 515 |
} |
| 516 |
case StableKey::k2DBlur16: { |
| 517 |
static SkRuntimeEffect* s2DBlurEffect = make_blur_2D_shader(16, stableKey); |
| 518 |
return s2DBlurEffect; |
| 519 |
} |
| 520 |
case StableKey::k2DBlur20: { |
| 521 |
static SkRuntimeEffect* s2DBlurEffect = make_blur_2D_shader(20, stableKey); |
| 522 |
return s2DBlurEffect; |
| 523 |
} |
| 524 |
case StableKey::k2DBlur28: { |
| 525 |
static SkRuntimeEffect* s2DBlurEffect = make_blur_2D_shader(28, stableKey); |
| 526 |
return s2DBlurEffect; |
| 527 |
} |
| 528 |
case StableKey::kBlend: { |
| 529 |
static const SkRuntimeEffect* sBlendEffect = make_blend_shader(); |
| 530 |
return sBlendEffect; |
| 531 |
} |
| 532 |
case StableKey::kLerp: { |
| 533 |
static const SkRuntimeEffect* sLerpEffect = make_lerp_shader(); |
| 534 |
return sLerpEffect; |
| 535 |
} |
| 536 |
case StableKey::kMatrixConvUniforms: { |
| 537 |
static const SkRuntimeEffect* sMatrixConvUniformsEffect = |
| 538 |
make_matrix_conv_shader(MatrixConvolutionImpl::kUniformBased, stableKey); |
| 539 |
return sMatrixConvUniformsEffect; |
| 540 |
} |
| 541 |
case StableKey::kMatrixConvTexSm: { |
| 542 |
static const SkRuntimeEffect* sMatrixConvTexSmEffect = |
| 543 |
make_matrix_conv_shader(MatrixConvolutionImpl::kTextureBasedSm, stableKey); |
| 544 |
return sMatrixConvTexSmEffect; |
| 545 |
} |
| 546 |
case StableKey::kMatrixConvTexLg: { |
| 547 |
static const SkRuntimeEffect* sMatrixConvTexMaxEffect = |
| 548 |
make_matrix_conv_shader(MatrixConvolutionImpl::kTextureBasedLg, stableKey); |
| 549 |
return sMatrixConvTexMaxEffect; |
| 550 |
} |
| 551 |
case StableKey::kDecal: { |
| 552 |
static const SkRuntimeEffect* sDecalEffect = make_decal_shader(); |
| 553 |
return sDecalEffect; |
| 554 |
} |
| 555 |
case StableKey::kDisplacement: { |
| 556 |
static const SkRuntimeEffect* sDisplacementEffect = make_displacement_shader(); |
| 557 |
return sDisplacementEffect; |
| 558 |
} |
| 559 |
case StableKey::kLighting: { |
| 560 |
static const SkRuntimeEffect* sLightingEffect = make_lighting_shader(); |
| 561 |
return sLightingEffect; |
| 562 |
} |
| 563 |
case StableKey::kLinearMorphology: { |
| 564 |
static const SkRuntimeEffect* sLinearMorphologyEffect = make_linear_morphology_shader(); |
| 565 |
return sLinearMorphologyEffect; |
| 566 |
} |
| 567 |
case StableKey::kMagnifier: { |
| 568 |
static const SkRuntimeEffect* sMagnifierEffect = make_magnifier_shader(); |
| 569 |
return sMagnifierEffect; |
| 570 |
} |
| 571 |
case StableKey::kNormal: { |
| 572 |
static const SkRuntimeEffect* sNormalEffect = make_normal_shader(); |
| 573 |
return sNormalEffect; |
| 574 |
} |
| 575 |
case StableKey::kSparseMorphology: { |
| 576 |
static const SkRuntimeEffect* sSparseMorphologyEffect = make_sparse_morphology_shader(); |
| 577 |
return sSparseMorphologyEffect; |
| 578 |
} |
| 579 |
|
| 580 |
// Blenders |
| 581 |
case StableKey::kArithmetic: { |
| 582 |
static const SkRuntimeEffect* sArithmeticEffect = make_arithmetic_blender(); |
| 583 |
return sArithmeticEffect; |
| 584 |
} |
| 585 |
|
| 586 |
// Color Filters |
| 587 |
case StableKey::kHighContrast: { |
| 588 |
static const SkRuntimeEffect* sHighContrastEffect = make_high_contrast_color_filter(); |
| 589 |
return sHighContrastEffect; |
| 590 |
} |
| 591 |
case StableKey::kLuma: { |
| 592 |
static const SkRuntimeEffect* sLumaEffect = make_luma_color_filter(); |
| 593 |
return sLumaEffect; |
| 594 |
} |
| 595 |
case StableKey::kOverdraw: { |
| 596 |
static const SkRuntimeEffect* sOverdrawEffect = make_overdraw_color_filter(); |
| 597 |
return sOverdrawEffect; |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
SkUNREACHABLE; |
| 602 |
} |
| 603 |
|
| 604 |
} // namespace SkKnownRuntimeEffects |