GrLinearGradientLayout.fp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. in half4x4 gradientMatrix;
  8. @coordTransform {
  9. gradientMatrix
  10. }
  11. void main() {
  12. // We add a tiny delta to t. When gradient stops are set up so that a hard stop in a vertically
  13. // or horizontally oriented gradient falls exactly at a column or row of pixel centers we can
  14. // we can get slightly different interpolated t values along the column/row. By adding the delta
  15. // we will consistently get the color to the "right" of the stop. Of course if the hard stop
  16. // falls at X.5 - delta then we still could get inconsistent results, but that is much less
  17. // likely. crbug.com/938592
  18. // If/when we add filtering of the gradient this can be removed.
  19. half t = half(sk_TransformedCoords2D[0].x) + 0.00001;
  20. sk_OutColor = half4(t, 1, 0, 0); // y = 1 for always valid
  21. }
  22. //////////////////////////////////////////////////////////////////////////////
  23. @header {
  24. #include "src/gpu/gradients/GrGradientShader.h"
  25. #include "src/shaders/gradients/SkLinearGradient.h"
  26. }
  27. // The linear gradient never rejects a pixel so it doesn't change opacity
  28. @optimizationFlags {
  29. kPreservesOpaqueInput_OptimizationFlag
  30. }
  31. @make {
  32. static std::unique_ptr<GrFragmentProcessor> Make(const SkLinearGradient& gradient,
  33. const GrFPArgs& args);
  34. }
  35. @cppEnd {
  36. std::unique_ptr<GrFragmentProcessor> GrLinearGradientLayout::Make(
  37. const SkLinearGradient& grad, const GrFPArgs& args) {
  38. SkMatrix matrix;
  39. if (!grad.totalLocalMatrix(args.fPreLocalMatrix, args.fPostLocalMatrix)->invert(&matrix)) {
  40. return nullptr;
  41. }
  42. matrix.postConcat(grad.getGradientMatrix());
  43. return std::unique_ptr<GrFragmentProcessor>(new GrLinearGradientLayout(matrix));
  44. }
  45. }
  46. //////////////////////////////////////////////////////////////////////////////
  47. @test(d) {
  48. SkScalar scale = GrGradientShader::RandomParams::kGradientScale;
  49. SkPoint points[] = {{d->fRandom->nextRangeScalar(0.0f, scale),
  50. d->fRandom->nextRangeScalar(0.0f, scale)},
  51. {d->fRandom->nextRangeScalar(0.0f, scale),
  52. d->fRandom->nextRangeScalar(0.0f, scale)}};
  53. GrGradientShader::RandomParams params(d->fRandom);
  54. auto shader = params.fUseColors4f ?
  55. SkGradientShader::MakeLinear(points, params.fColors4f, params.fColorSpace, params.fStops,
  56. params.fColorCount, params.fTileMode) :
  57. SkGradientShader::MakeLinear(points, params.fColors, params.fStops,
  58. params.fColorCount, params.fTileMode);
  59. GrTest::TestAsFPArgs asFPArgs(d);
  60. std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args());
  61. GrAlwaysAssert(fp);
  62. return fp;
  63. }