GrTiledGradientEffect.fp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. // Provides tiling for the repeat or mirror modes.
  8. in fragmentProcessor colorizer;
  9. in fragmentProcessor gradLayout;
  10. layout(key) in bool mirror;
  11. layout(key) in bool makePremul;
  12. // Trust the creator that this matches the color spec of the gradient
  13. in bool colorsAreOpaque;
  14. void main() {
  15. half4 t = process(gradLayout);
  16. if (!gradLayout.preservesOpaqueInput && t.y < 0) {
  17. // layout has rejected this fragment (rely on sksl to remove this branch if the layout FP
  18. // preserves opacity is false)
  19. sk_OutColor = half4(0);
  20. } else {
  21. @if(mirror) {
  22. half t_1 = t.x - 1;
  23. half tiled_t = t_1 - 2 * floor(t_1 * 0.5) - 1;
  24. if (sk_Caps.mustDoOpBetweenFloorAndAbs) {
  25. // At this point the expected value of tiled_t should between -1 and 1, so this
  26. // clamp has no effect other than to break up the floor and abs calls and make sure
  27. // the compiler doesn't merge them back together.
  28. tiled_t = clamp(tiled_t, -1, 1);
  29. }
  30. t.x = abs(tiled_t);
  31. } else {
  32. // Simple repeat mode
  33. t.x = fract(t.x);
  34. }
  35. // t.x has been tiled (repeat or mirrored), but pass through remaining 3 components
  36. // unmodified.
  37. sk_OutColor = process(colorizer, t);
  38. }
  39. @if (makePremul) {
  40. sk_OutColor.xyz *= sk_OutColor.w;
  41. }
  42. }
  43. //////////////////////////////////////////////////////////////////////////////
  44. // If the layout does not preserve opacity, remove the opaque optimization,
  45. // but otherwise respect the provided color opacity state.
  46. @optimizationFlags {
  47. kCompatibleWithCoverageAsAlpha_OptimizationFlag |
  48. (colorsAreOpaque && gradLayout->preservesOpaqueInput() ? kPreservesOpaqueInput_OptimizationFlag
  49. : kNone_OptimizationFlags)
  50. }