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