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