blob: 50a4c8a82138ca4dc44410fdf52e51d7624ff34d [file] [log] [blame]
Michael Ludwig4f94ef62018-09-12 15:22:16 -04001/*
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
8#include "GrGradientShader.h"
9
10#include "GrClampedGradientEffect.h"
11#include "GrTiledGradientEffect.h"
12
13#include "GrLinearGradientLayout.h"
Michael Ludwig4089df82018-09-12 15:22:37 -040014#include "GrRadialGradientLayout.h"
Michael Ludwig24d438b2018-09-12 15:22:50 -040015#include "GrSweepGradientLayout.h"
Michael Ludwig4089df82018-09-12 15:22:37 -040016
Michael Ludwig4f94ef62018-09-12 15:22:16 -040017#include "GrSingleIntervalGradientColorizer.h"
18
19#include "SkGradientShaderPriv.h"
20#include "GrColor.h"
21
22// Analyze the shader's color stops and positions and chooses an appropriate colorizer to represent
23// the gradient.
24static std::unique_ptr<GrFragmentProcessor> make_colorizer(const SkGradientShaderBase& shader,
25 const GrFPArgs& args, const GrColor4f* colors) {
26 // If there are hard stops at the beginning or end, the first and/or last color should be
27 // ignored by the colorizer since it should only be used in a clamped border color. By detecting
28 // and removing these stops at the beginning, it makes optimizing the remaining color stops
29 // simpler.
30
31 // SkGradientShaderBase guarantees that fOrigPos[0] == 0 by adding a dummy
32 bool bottomHardStop = shader.fOrigPos && SkScalarNearlyEqual(shader.fOrigPos[0],
33 shader.fOrigPos[1]);
34 // The same is true for fOrigPos[end] == 1
35 bool topHardStop = shader.fOrigPos &&
36 SkScalarNearlyEqual(shader.fOrigPos[shader.fColorCount - 2],
37 shader.fOrigPos[shader.fColorCount - 1]);
38
39 int offset = 0;
40 int count = shader.fColorCount;
41 if (bottomHardStop) {
42 offset += 1;
43 count--;
44 }
45 if (topHardStop) {
46 count--;
47 }
48
49 // Currently only supports 2-color single intervals. However, when the gradient has hard stops
50 // and is clamped, certain 3 or 4 color gradients are equivalent to a two color interval
51 if (count == 2) {
52 return GrSingleIntervalGradientColorizer::Make(colors[offset], colors[offset + 1]);
53 }
54
55 return nullptr;
56}
57
58// Combines the colorizer and layout with an appropriately configured master effect based on the
59// gradient's tile mode
60static std::unique_ptr<GrFragmentProcessor> make_gradient(const SkGradientShaderBase& shader,
61 const GrFPArgs& args, std::unique_ptr<GrFragmentProcessor> layout) {
62 // No shader is possible if a layout couldn't be created, e.g. a layout-specific Make() returned
63 // null.
64 if (layout == nullptr) {
65 return nullptr;
66 }
67
68 // Convert all colors into destination space and into GrColor4fs, and handle
69 // premul issues depending on the interpolation mode
70 bool inputPremul = shader.getGradFlags() & SkGradientShader::kInterpolateColorsInPremul_Flag;
71 SkAutoSTMalloc<4, GrColor4f> colors(shader.fColorCount);
72 SkColor4fXformer xformedColors(shader.fOrigColors4f, shader.fColorCount,
73 shader.fColorSpace.get(), args.fDstColorSpaceInfo->colorSpace());
74 for (int i = 0; i < shader.fColorCount; i++) {
75 colors[i] = GrColor4f::FromSkColor4f(xformedColors.fColors[i]);
76 if (inputPremul) {
77 colors[i] = colors[i].premul();
78 }
79 }
80
81 // All gradients are colorized the same way, regardless of layout
82 std::unique_ptr<GrFragmentProcessor> colorizer = make_colorizer(shader, args, colors.get());
83 if (colorizer == nullptr) {
84 return nullptr;
85 }
86
87 // All tile modes are supported (unless something was added to SkShader)
88 std::unique_ptr<GrFragmentProcessor> master;
89 switch(shader.getTileMode()) {
90 case SkShader::kRepeat_TileMode:
91 master = GrTiledGradientEffect::Make(std::move(colorizer), std::move(layout),
92 /* mirror */ false);
93 break;
94 case SkShader::kMirror_TileMode:
95 master = GrTiledGradientEffect::Make(std::move(colorizer), std::move(layout),
96 /* mirror */ true);
97 break;
98 case SkShader::kClamp_TileMode:
99 // For the clamped mode, the border colors are the first and last colors, corresponding
100 // to t=0 and t=1, because SkGradientShaderBase enforces that by adding color stops as
101 // appropriate. If there is a hard stop, this grabs the expected outer colors for the
102 // border.
103 master = GrClampedGradientEffect::Make(std::move(colorizer), std::move(layout),
104 colors[0], colors[shader.fColorCount - 1]);
105 break;
106 case SkShader::kDecal_TileMode:
107 master = GrClampedGradientEffect::Make(std::move(colorizer), std::move(layout),
108 GrColor4f::TransparentBlack(),
109 GrColor4f::TransparentBlack());
110 break;
111 }
112
113 if (master == nullptr) {
114 // Unexpected tile mode
115 return nullptr;
116 }
117
118 if (!inputPremul) {
119 // When interpolating unpremul colors, the output of the gradient
120 // effect fp's will also be unpremul, so wrap it to ensure its premul.
121 // - this is unnecessary when interpolating premul colors since the
122 // output color is premul by nature
123 master = GrFragmentProcessor::PremulOutput(std::move(master));
124 }
125
126 return GrFragmentProcessor::MulChildByInputAlpha(std::move(master));
127}
128
129namespace GrGradientShader {
130
131std::unique_ptr<GrFragmentProcessor> MakeLinear(const SkLinearGradient& shader,
132 const GrFPArgs& args) {
133 return make_gradient(shader, args, GrLinearGradientLayout::Make(shader, args));
134}
135
Michael Ludwig4089df82018-09-12 15:22:37 -0400136std::unique_ptr<GrFragmentProcessor> MakeRadial(const SkRadialGradient& shader,
137 const GrFPArgs& args) {
138 return make_gradient(shader,args, GrRadialGradientLayout::Make(shader, args));
139}
140
Michael Ludwig24d438b2018-09-12 15:22:50 -0400141std::unique_ptr<GrFragmentProcessor> MakeSweep(const SkSweepGradient& shader,
142 const GrFPArgs& args) {
143 return make_gradient(shader,args, GrSweepGradientLayout::Make(shader, args));
144}
145
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400146}