blob: 12b9270695086e577941519390f9cd4ff9f5b4a5 [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 Ludwig8f685082018-09-12 15:23:01 -040016#include "GrTwoPointConicalGradientLayout.h"
Michael Ludwig4089df82018-09-12 15:22:37 -040017
Michael Ludwig0495f7a2018-09-12 15:23:33 -040018#include "GrDualIntervalGradientColorizer.h"
Michael Ludwig4f94ef62018-09-12 15:22:16 -040019#include "GrSingleIntervalGradientColorizer.h"
Michael Ludwiga7914d32018-09-14 09:47:21 -040020#include "GrTextureGradientColorizer.h"
21#include "GrGradientBitmapCache.h"
Michael Ludwig4f94ef62018-09-12 15:22:16 -040022
Michael Ludwiga7914d32018-09-14 09:47:21 -040023#include "SkGr.h"
Michael Ludwig4f94ef62018-09-12 15:22:16 -040024#include "GrColor.h"
Michael Ludwiga7914d32018-09-14 09:47:21 -040025#include "GrContext.h"
26#include "GrContextPriv.h"
27
28// Each cache entry costs 1K or 2K of RAM. Each bitmap will be 1x256 at either 32bpp or 64bpp.
29static const int kMaxNumCachedGradientBitmaps = 32;
30static const int kGradientTextureSize = 256;
31
32// NOTE: signature takes raw pointers to the color/pos arrays and a count to make it easy for
33// MakeColorizer to transparently take care of hard stops at the end points of the gradient.
34static std::unique_ptr<GrFragmentProcessor> make_textured_colorizer(const GrColor4f* colors,
35 const SkScalar* positions, int count, bool premul, const GrFPArgs& args) {
36 static GrGradientBitmapCache gCache(kMaxNumCachedGradientBitmaps, kGradientTextureSize);
37
38 // Use 8888 or F16, depending on the destination config.
39 // TODO: Use 1010102 for opaque gradients, at least if destination is 1010102?
40 SkColorType colorType = kRGBA_8888_SkColorType;
41 if (kLow_GrSLPrecision != GrSLSamplerPrecision(args.fDstColorSpaceInfo->config()) &&
42 args.fContext->contextPriv().caps()->isConfigTexturable(kRGBA_half_GrPixelConfig)) {
43 colorType = kRGBA_F16_SkColorType;
44 }
45 SkAlphaType alphaType = premul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
46
47 SkBitmap bitmap;
48 gCache.getGradient(colors, positions, count, colorType, alphaType, &bitmap);
49 SkASSERT(1 == bitmap.height() && SkIsPow2(bitmap.width()));
50 SkASSERT(bitmap.isImmutable());
51
52 sk_sp<GrTextureProxy> proxy = GrMakeCachedBitmapProxy(
53 args.fContext->contextPriv().proxyProvider(), bitmap);
54 if (proxy == nullptr) {
55 SkDebugf("Gradient won't draw. Could not create texture.");
56 return nullptr;
57 }
58
59 return GrTextureGradientColorizer::Make(std::move(proxy));
60}
Michael Ludwig4f94ef62018-09-12 15:22:16 -040061
62// Analyze the shader's color stops and positions and chooses an appropriate colorizer to represent
63// the gradient.
Michael Ludwig0495f7a2018-09-12 15:23:33 -040064static std::unique_ptr<GrFragmentProcessor> make_colorizer(const GrColor4f* colors,
Michael Ludwiga7914d32018-09-14 09:47:21 -040065 const SkScalar* positions, int count, bool premul, const GrFPArgs& args) {
Michael Ludwig4f94ef62018-09-12 15:22:16 -040066 // If there are hard stops at the beginning or end, the first and/or last color should be
67 // ignored by the colorizer since it should only be used in a clamped border color. By detecting
68 // and removing these stops at the beginning, it makes optimizing the remaining color stops
69 // simpler.
70
Michael Ludwig0495f7a2018-09-12 15:23:33 -040071 // SkGradientShaderBase guarantees that pos[0] == 0 by adding a dummy
72 bool bottomHardStop = SkScalarNearlyEqual(positions[0], positions[1]);
73 // The same is true for pos[end] == 1
Michael Ludwiga7914d32018-09-14 09:47:21 -040074 bool topHardStop = SkScalarNearlyEqual(positions[count - 2], positions[count - 1]);
Michael Ludwig4f94ef62018-09-12 15:22:16 -040075
76 int offset = 0;
Michael Ludwig4f94ef62018-09-12 15:22:16 -040077 if (bottomHardStop) {
78 offset += 1;
79 count--;
80 }
81 if (topHardStop) {
82 count--;
83 }
84
Michael Ludwig0495f7a2018-09-12 15:23:33 -040085 // Two remaining colors means a single interval from 0 to 1
86 // (but it may have originally been a 3 or 4 color gradient with 1-2 hard stops at the ends)
Michael Ludwig4f94ef62018-09-12 15:22:16 -040087 if (count == 2) {
88 return GrSingleIntervalGradientColorizer::Make(colors[offset], colors[offset + 1]);
Michael Ludwig0495f7a2018-09-12 15:23:33 -040089 } else if (count == 3) {
90 // Must be a dual interval gradient, where the middle point is at offset+1 and the two
91 // intervals share the middle color stop.
92 return GrDualIntervalGradientColorizer::Make(colors[offset], colors[offset + 1],
93 colors[offset + 1], colors[offset + 2],
94 positions[offset + 1]);
95 } else if (count == 4 && SkScalarNearlyEqual(positions[offset + 1], positions[offset + 2])) {
96 // Two separate intervals that join at the same threshold position
97 return GrDualIntervalGradientColorizer::Make(colors[offset], colors[offset + 1],
98 colors[offset + 2], colors[offset + 3],
99 positions[offset + 1]);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400100 }
101
Michael Ludwiga7914d32018-09-14 09:47:21 -0400102 return make_textured_colorizer(colors + offset, positions + offset, count, premul, args);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400103}
104
105// Combines the colorizer and layout with an appropriately configured master effect based on the
106// gradient's tile mode
107static std::unique_ptr<GrFragmentProcessor> make_gradient(const SkGradientShaderBase& shader,
108 const GrFPArgs& args, std::unique_ptr<GrFragmentProcessor> layout) {
109 // No shader is possible if a layout couldn't be created, e.g. a layout-specific Make() returned
110 // null.
111 if (layout == nullptr) {
112 return nullptr;
113 }
114
115 // Convert all colors into destination space and into GrColor4fs, and handle
116 // premul issues depending on the interpolation mode
117 bool inputPremul = shader.getGradFlags() & SkGradientShader::kInterpolateColorsInPremul_Flag;
Michael Ludwigb96cba32018-09-14 13:59:24 -0400118 bool allOpaque = true;
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400119 SkAutoSTMalloc<4, GrColor4f> colors(shader.fColorCount);
120 SkColor4fXformer xformedColors(shader.fOrigColors4f, shader.fColorCount,
121 shader.fColorSpace.get(), args.fDstColorSpaceInfo->colorSpace());
122 for (int i = 0; i < shader.fColorCount; i++) {
Brian Osmand25b7c12018-09-21 16:01:59 -0400123 colors[i] = GrColor4f::FromRGBA4f(xformedColors.fColors[i]);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400124 if (inputPremul) {
125 colors[i] = colors[i].premul();
126 }
Michael Ludwigb96cba32018-09-14 13:59:24 -0400127 if (allOpaque && !SkScalarNearlyEqual(colors[i].fRGBA[3], 1.0)) {
128 allOpaque = false;
129 }
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400130 }
131
Michael Ludwig0495f7a2018-09-12 15:23:33 -0400132 // SkGradientShader stores positions implicitly when they are evenly spaced, but the getPos()
133 // implementation performs a branch for every position index. Since the shader conversion
134 // requires lots of position tests, calculate all of the positions up front if needed.
135 SkTArray<SkScalar, true> implicitPos;
136 SkScalar* positions;
137 if (shader.fOrigPos) {
138 positions = shader.fOrigPos;
139 } else {
140 implicitPos.reserve(shader.fColorCount);
141 SkScalar posScale = SkScalarFastInvert(shader.fColorCount - 1);
142 for (int i = 0 ; i < shader.fColorCount; i++) {
143 implicitPos.push_back(SkIntToScalar(i) * posScale);
144 }
145 positions = implicitPos.begin();
146 }
147
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400148 // All gradients are colorized the same way, regardless of layout
Michael Ludwiga7914d32018-09-14 09:47:21 -0400149 std::unique_ptr<GrFragmentProcessor> colorizer = make_colorizer(
150 colors.get(), positions, shader.fColorCount, inputPremul, args);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400151 if (colorizer == nullptr) {
152 return nullptr;
153 }
154
Michael Ludwigb96cba32018-09-14 13:59:24 -0400155 // The master effect has to export premul colors, but under certain conditions it doesn't need
156 // to do anything to achieve that: i.e. its interpolating already premul colors (inputPremul)
157 // or all the colors have a = 1, in which case premul is a no op. Note that this allOpaque
158 // check is more permissive than SkGradientShaderBase's isOpaque(), since we can optimize away
159 // the make-premul op for two point conical gradients (which report false for isOpaque).
160 bool makePremul = !inputPremul && !allOpaque;
161
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400162 // All tile modes are supported (unless something was added to SkShader)
163 std::unique_ptr<GrFragmentProcessor> master;
164 switch(shader.getTileMode()) {
165 case SkShader::kRepeat_TileMode:
166 master = GrTiledGradientEffect::Make(std::move(colorizer), std::move(layout),
Michael Ludwigb96cba32018-09-14 13:59:24 -0400167 /* mirror */ false, makePremul, allOpaque);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400168 break;
169 case SkShader::kMirror_TileMode:
170 master = GrTiledGradientEffect::Make(std::move(colorizer), std::move(layout),
Michael Ludwigb96cba32018-09-14 13:59:24 -0400171 /* mirror */ true, makePremul, allOpaque);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400172 break;
173 case SkShader::kClamp_TileMode:
174 // For the clamped mode, the border colors are the first and last colors, corresponding
175 // to t=0 and t=1, because SkGradientShaderBase enforces that by adding color stops as
176 // appropriate. If there is a hard stop, this grabs the expected outer colors for the
177 // border.
178 master = GrClampedGradientEffect::Make(std::move(colorizer), std::move(layout),
Michael Ludwigb96cba32018-09-14 13:59:24 -0400179 colors[0], colors[shader.fColorCount - 1], makePremul, allOpaque);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400180 break;
181 case SkShader::kDecal_TileMode:
Michael Ludwigb96cba32018-09-14 13:59:24 -0400182 // Even if the gradient colors are opaque, the decal borders are transparent so
183 // disable that optimization
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400184 master = GrClampedGradientEffect::Make(std::move(colorizer), std::move(layout),
Michael Ludwigb96cba32018-09-14 13:59:24 -0400185 GrColor4f::TransparentBlack(), GrColor4f::TransparentBlack(),
186 makePremul, /* colorsAreOpaque */ false);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400187 break;
188 }
189
190 if (master == nullptr) {
191 // Unexpected tile mode
192 return nullptr;
193 }
194
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400195 return GrFragmentProcessor::MulChildByInputAlpha(std::move(master));
196}
197
198namespace GrGradientShader {
199
200std::unique_ptr<GrFragmentProcessor> MakeLinear(const SkLinearGradient& shader,
201 const GrFPArgs& args) {
202 return make_gradient(shader, args, GrLinearGradientLayout::Make(shader, args));
203}
204
Michael Ludwig4089df82018-09-12 15:22:37 -0400205std::unique_ptr<GrFragmentProcessor> MakeRadial(const SkRadialGradient& shader,
206 const GrFPArgs& args) {
207 return make_gradient(shader,args, GrRadialGradientLayout::Make(shader, args));
208}
209
Michael Ludwig24d438b2018-09-12 15:22:50 -0400210std::unique_ptr<GrFragmentProcessor> MakeSweep(const SkSweepGradient& shader,
211 const GrFPArgs& args) {
212 return make_gradient(shader,args, GrSweepGradientLayout::Make(shader, args));
213}
214
Michael Ludwig8f685082018-09-12 15:23:01 -0400215std::unique_ptr<GrFragmentProcessor> MakeConical(const SkTwoPointConicalGradient& shader,
216 const GrFPArgs& args) {
217 return make_gradient(shader, args, GrTwoPointConicalGradientLayout::Make(shader, args));
218}
219
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400220#if GR_TEST_UTILS
221RandomParams::RandomParams(SkRandom* random) {
222 // Set color count to min of 2 so that we don't trigger the const color optimization and make
223 // a non-gradient processor.
224 fColorCount = random->nextRangeU(2, kMaxRandomGradientColors);
225 fUseColors4f = random->nextBool();
226
227 // if one color, omit stops, otherwise randomly decide whether or not to
228 if (fColorCount == 1 || (fColorCount >= 2 && random->nextBool())) {
229 fStops = nullptr;
230 } else {
231 fStops = fStopStorage;
232 }
233
234 // if using SkColor4f, attach a random (possibly null) color space (with linear gamma)
235 if (fUseColors4f) {
236 fColorSpace = GrTest::TestColorSpace(random);
237 }
238
239 SkScalar stop = 0.f;
240 for (int i = 0; i < fColorCount; ++i) {
241 if (fUseColors4f) {
242 fColors4f[i].fR = random->nextUScalar1();
243 fColors4f[i].fG = random->nextUScalar1();
244 fColors4f[i].fB = random->nextUScalar1();
245 fColors4f[i].fA = random->nextUScalar1();
246 } else {
247 fColors[i] = random->nextU();
248 }
249 if (fStops) {
250 fStops[i] = stop;
251 stop = i < fColorCount - 1 ? stop + random->nextUScalar1() * (1.f - stop) : 1.f;
252 }
253 }
254 fTileMode = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileModeCount));
255}
256#endif
257
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400258}