blob: 9571a8f8f83b5373ad158d3b79a115e91410cb3f [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/gradients/GrGradientShader.h"
Michael Ludwig4f94ef62018-09-12 15:22:16 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/gradients/generated/GrClampedGradientEffect.h"
11#include "src/gpu/gradients/generated/GrTiledGradientEffect.h"
Michael Ludwig4f94ef62018-09-12 15:22:16 -040012
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/gradients/generated/GrLinearGradientLayout.h"
14#include "src/gpu/gradients/generated/GrRadialGradientLayout.h"
15#include "src/gpu/gradients/generated/GrSweepGradientLayout.h"
16#include "src/gpu/gradients/generated/GrTwoPointConicalGradientLayout.h"
Michael Ludwig4089df82018-09-12 15:22:37 -040017
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/gradients/GrGradientBitmapCache.h"
19#include "src/gpu/gradients/generated/GrDualIntervalGradientColorizer.h"
20#include "src/gpu/gradients/generated/GrSingleIntervalGradientColorizer.h"
21#include "src/gpu/gradients/generated/GrTextureGradientColorizer.h"
22#include "src/gpu/gradients/generated/GrUnrolledBinaryGradientColorizer.h"
Michael Ludwig4f94ef62018-09-12 15:22:16 -040023
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/private/GrRecordingContext.h"
25#include "src/gpu/GrCaps.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040026#include "src/gpu/GrColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrColorSpaceInfo.h"
28#include "src/gpu/GrRecordingContextPriv.h"
29#include "src/gpu/SkGr.h"
Michael Ludwiga7914d32018-09-14 09:47:21 -040030
Michael Ludwig72535fb2018-09-28 11:53:32 -040031// Intervals smaller than this (that aren't hard stops) on low-precision-only devices force us to
32// use the textured gradient
33static const SkScalar kLowPrecisionIntervalLimit = 0.01f;
34
Michael Ludwiga7914d32018-09-14 09:47:21 -040035// Each cache entry costs 1K or 2K of RAM. Each bitmap will be 1x256 at either 32bpp or 64bpp.
36static const int kMaxNumCachedGradientBitmaps = 32;
37static const int kGradientTextureSize = 256;
38
39// NOTE: signature takes raw pointers to the color/pos arrays and a count to make it easy for
40// MakeColorizer to transparently take care of hard stops at the end points of the gradient.
Brian Osman021ed512018-10-16 15:19:44 -040041static std::unique_ptr<GrFragmentProcessor> make_textured_colorizer(const SkPMColor4f* colors,
Michael Ludwiga7914d32018-09-14 09:47:21 -040042 const SkScalar* positions, int count, bool premul, const GrFPArgs& args) {
43 static GrGradientBitmapCache gCache(kMaxNumCachedGradientBitmaps, kGradientTextureSize);
44
45 // Use 8888 or F16, depending on the destination config.
46 // TODO: Use 1010102 for opaque gradients, at least if destination is 1010102?
47 SkColorType colorType = kRGBA_8888_SkColorType;
48 if (kLow_GrSLPrecision != GrSLSamplerPrecision(args.fDstColorSpaceInfo->config()) &&
Robert Phillips9da87e02019-02-04 13:26:26 -050049 args.fContext->priv().caps()->isConfigTexturable(kRGBA_half_GrPixelConfig)) {
Michael Ludwiga7914d32018-09-14 09:47:21 -040050 colorType = kRGBA_F16_SkColorType;
51 }
52 SkAlphaType alphaType = premul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
53
54 SkBitmap bitmap;
55 gCache.getGradient(colors, positions, count, colorType, alphaType, &bitmap);
56 SkASSERT(1 == bitmap.height() && SkIsPow2(bitmap.width()));
57 SkASSERT(bitmap.isImmutable());
58
59 sk_sp<GrTextureProxy> proxy = GrMakeCachedBitmapProxy(
Robert Phillips9da87e02019-02-04 13:26:26 -050060 args.fContext->priv().proxyProvider(), bitmap);
Michael Ludwiga7914d32018-09-14 09:47:21 -040061 if (proxy == nullptr) {
62 SkDebugf("Gradient won't draw. Could not create texture.");
63 return nullptr;
64 }
65
66 return GrTextureGradientColorizer::Make(std::move(proxy));
67}
Michael Ludwig4f94ef62018-09-12 15:22:16 -040068
69// Analyze the shader's color stops and positions and chooses an appropriate colorizer to represent
70// the gradient.
Brian Osman021ed512018-10-16 15:19:44 -040071static std::unique_ptr<GrFragmentProcessor> make_colorizer(const SkPMColor4f* colors,
Michael Ludwiga7914d32018-09-14 09:47:21 -040072 const SkScalar* positions, int count, bool premul, const GrFPArgs& args) {
Michael Ludwig4f94ef62018-09-12 15:22:16 -040073 // If there are hard stops at the beginning or end, the first and/or last color should be
74 // ignored by the colorizer since it should only be used in a clamped border color. By detecting
75 // and removing these stops at the beginning, it makes optimizing the remaining color stops
76 // simpler.
77
Michael Ludwig0495f7a2018-09-12 15:23:33 -040078 // SkGradientShaderBase guarantees that pos[0] == 0 by adding a dummy
79 bool bottomHardStop = SkScalarNearlyEqual(positions[0], positions[1]);
80 // The same is true for pos[end] == 1
Michael Ludwiga7914d32018-09-14 09:47:21 -040081 bool topHardStop = SkScalarNearlyEqual(positions[count - 2], positions[count - 1]);
Michael Ludwig4f94ef62018-09-12 15:22:16 -040082
83 int offset = 0;
Michael Ludwig4f94ef62018-09-12 15:22:16 -040084 if (bottomHardStop) {
85 offset += 1;
86 count--;
87 }
88 if (topHardStop) {
89 count--;
90 }
91
Michael Ludwig0495f7a2018-09-12 15:23:33 -040092 // Two remaining colors means a single interval from 0 to 1
93 // (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 -040094 if (count == 2) {
95 return GrSingleIntervalGradientColorizer::Make(colors[offset], colors[offset + 1]);
96 }
97
Michael Ludwig72535fb2018-09-28 11:53:32 -040098 // Do an early test for the texture fallback to skip all of the other tests for specific
99 // analytic support of the gradient (and compatibility with the hardware), when it's definitely
100 // impossible to use an analytic solution.
101 bool tryAnalyticColorizer = count <= GrUnrolledBinaryGradientColorizer::kMaxColorCount;
102
103 // The remaining analytic colorizers use scale*t+bias, and the scale/bias values can become
104 // quite large when thresholds are close (but still outside the hardstop limit). If float isn't
105 // 32-bit, output can be incorrect if the thresholds are too close together. However, the
106 // analytic shaders are higher quality, so they can be used with lower precision hardware when
107 // the thresholds are not ill-conditioned.
Robert Phillips9da87e02019-02-04 13:26:26 -0500108 const GrShaderCaps* caps = args.fContext->priv().caps()->shaderCaps();
Michael Ludwig72535fb2018-09-28 11:53:32 -0400109 if (!caps->floatIs32Bits() && tryAnalyticColorizer) {
110 // Could run into problems, check if thresholds are close together (with a limit of .01, so
111 // that scales will be less than 100, which leaves 4 decimals of precision on 16-bit).
112 for (int i = offset; i < count - 1; i++) {
113 SkScalar dt = SkScalarAbs(positions[i] - positions[i + 1]);
114 if (dt <= kLowPrecisionIntervalLimit && dt > SK_ScalarNearlyZero) {
115 tryAnalyticColorizer = false;
116 break;
117 }
118 }
119 }
120
121 if (tryAnalyticColorizer) {
122 if (count == 3) {
123 // Must be a dual interval gradient, where the middle point is at offset+1 and the two
124 // intervals share the middle color stop.
125 return GrDualIntervalGradientColorizer::Make(colors[offset], colors[offset + 1],
126 colors[offset + 1], colors[offset + 2],
127 positions[offset + 1]);
128 } else if (count == 4 && SkScalarNearlyEqual(positions[offset + 1],
129 positions[offset + 2])) {
130 // Two separate intervals that join at the same threshold position
131 return GrDualIntervalGradientColorizer::Make(colors[offset], colors[offset + 1],
132 colors[offset + 2], colors[offset + 3],
133 positions[offset + 1]);
134 }
135
136 // The single and dual intervals are a specialized case of the unrolled binary search
137 // colorizer which can analytically render gradients of up to 8 intervals (up to 9 or 16
138 // colors depending on how many hard stops are inserted).
139 std::unique_ptr<GrFragmentProcessor> unrolled = GrUnrolledBinaryGradientColorizer::Make(
140 colors + offset, positions + offset, count);
141 if (unrolled) {
142 return unrolled;
143 }
144 }
145
146 // Otherwise fall back to a rasterized gradient sampled by a texture, which can handle
147 // arbitrary gradients (the only downside being sampling resolution).
Michael Ludwiga7914d32018-09-14 09:47:21 -0400148 return make_textured_colorizer(colors + offset, positions + offset, count, premul, args);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400149}
150
151// Combines the colorizer and layout with an appropriately configured master effect based on the
152// gradient's tile mode
153static std::unique_ptr<GrFragmentProcessor> make_gradient(const SkGradientShaderBase& shader,
154 const GrFPArgs& args, std::unique_ptr<GrFragmentProcessor> layout) {
155 // No shader is possible if a layout couldn't be created, e.g. a layout-specific Make() returned
156 // null.
157 if (layout == nullptr) {
158 return nullptr;
159 }
160
Brian Osman021ed512018-10-16 15:19:44 -0400161 // Convert all colors into destination space and into SkPMColor4fs, and handle
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400162 // premul issues depending on the interpolation mode
163 bool inputPremul = shader.getGradFlags() & SkGradientShader::kInterpolateColorsInPremul_Flag;
Michael Ludwigb96cba32018-09-14 13:59:24 -0400164 bool allOpaque = true;
Brian Osman021ed512018-10-16 15:19:44 -0400165 SkAutoSTMalloc<4, SkPMColor4f> colors(shader.fColorCount);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400166 SkColor4fXformer xformedColors(shader.fOrigColors4f, shader.fColorCount,
167 shader.fColorSpace.get(), args.fDstColorSpaceInfo->colorSpace());
168 for (int i = 0; i < shader.fColorCount; i++) {
Brian Osman021ed512018-10-16 15:19:44 -0400169 const SkColor4f& upmColor = xformedColors.fColors[i];
170 colors[i] = inputPremul ? upmColor.premul()
171 : SkPMColor4f{ upmColor.fR, upmColor.fG, upmColor.fB, upmColor.fA };
172 if (allOpaque && !SkScalarNearlyEqual(colors[i].fA, 1.0)) {
Michael Ludwigb96cba32018-09-14 13:59:24 -0400173 allOpaque = false;
174 }
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400175 }
176
Michael Ludwig0495f7a2018-09-12 15:23:33 -0400177 // SkGradientShader stores positions implicitly when they are evenly spaced, but the getPos()
178 // implementation performs a branch for every position index. Since the shader conversion
179 // requires lots of position tests, calculate all of the positions up front if needed.
180 SkTArray<SkScalar, true> implicitPos;
181 SkScalar* positions;
182 if (shader.fOrigPos) {
183 positions = shader.fOrigPos;
184 } else {
185 implicitPos.reserve(shader.fColorCount);
Mike Kleind3ed3012018-11-06 19:23:08 -0500186 SkScalar posScale = SK_Scalar1 / (shader.fColorCount - 1);
Michael Ludwig0495f7a2018-09-12 15:23:33 -0400187 for (int i = 0 ; i < shader.fColorCount; i++) {
188 implicitPos.push_back(SkIntToScalar(i) * posScale);
189 }
190 positions = implicitPos.begin();
191 }
192
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400193 // All gradients are colorized the same way, regardless of layout
Michael Ludwiga7914d32018-09-14 09:47:21 -0400194 std::unique_ptr<GrFragmentProcessor> colorizer = make_colorizer(
195 colors.get(), positions, shader.fColorCount, inputPremul, args);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400196 if (colorizer == nullptr) {
197 return nullptr;
198 }
199
Michael Ludwigb96cba32018-09-14 13:59:24 -0400200 // The master effect has to export premul colors, but under certain conditions it doesn't need
201 // to do anything to achieve that: i.e. its interpolating already premul colors (inputPremul)
202 // or all the colors have a = 1, in which case premul is a no op. Note that this allOpaque
203 // check is more permissive than SkGradientShaderBase's isOpaque(), since we can optimize away
204 // the make-premul op for two point conical gradients (which report false for isOpaque).
205 bool makePremul = !inputPremul && !allOpaque;
206
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400207 // All tile modes are supported (unless something was added to SkShader)
208 std::unique_ptr<GrFragmentProcessor> master;
209 switch(shader.getTileMode()) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400210 case SkTileMode::kRepeat:
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400211 master = GrTiledGradientEffect::Make(std::move(colorizer), std::move(layout),
Michael Ludwigb96cba32018-09-14 13:59:24 -0400212 /* mirror */ false, makePremul, allOpaque);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400213 break;
Mike Reedfae8fce2019-04-03 10:27:45 -0400214 case SkTileMode::kMirror:
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400215 master = GrTiledGradientEffect::Make(std::move(colorizer), std::move(layout),
Michael Ludwigb96cba32018-09-14 13:59:24 -0400216 /* mirror */ true, makePremul, allOpaque);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400217 break;
Mike Reedfae8fce2019-04-03 10:27:45 -0400218 case SkTileMode::kClamp:
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400219 // For the clamped mode, the border colors are the first and last colors, corresponding
220 // to t=0 and t=1, because SkGradientShaderBase enforces that by adding color stops as
221 // appropriate. If there is a hard stop, this grabs the expected outer colors for the
222 // border.
223 master = GrClampedGradientEffect::Make(std::move(colorizer), std::move(layout),
Michael Ludwigb96cba32018-09-14 13:59:24 -0400224 colors[0], colors[shader.fColorCount - 1], makePremul, allOpaque);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400225 break;
Mike Reedfae8fce2019-04-03 10:27:45 -0400226 case SkTileMode::kDecal:
Michael Ludwigb96cba32018-09-14 13:59:24 -0400227 // Even if the gradient colors are opaque, the decal borders are transparent so
228 // disable that optimization
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400229 master = GrClampedGradientEffect::Make(std::move(colorizer), std::move(layout),
Brian Osman021ed512018-10-16 15:19:44 -0400230 SK_PMColor4fTRANSPARENT, SK_PMColor4fTRANSPARENT,
Michael Ludwigb96cba32018-09-14 13:59:24 -0400231 makePremul, /* colorsAreOpaque */ false);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400232 break;
233 }
234
235 if (master == nullptr) {
236 // Unexpected tile mode
237 return nullptr;
238 }
Brian Salomonc0d79e52019-04-10 15:02:11 -0400239 if (args.fInputColorIsOpaque) {
240 return GrFragmentProcessor::OverrideInput(std::move(master), SK_PMColor4fWHITE, false);
241 }
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400242 return GrFragmentProcessor::MulChildByInputAlpha(std::move(master));
243}
244
245namespace GrGradientShader {
246
247std::unique_ptr<GrFragmentProcessor> MakeLinear(const SkLinearGradient& shader,
248 const GrFPArgs& args) {
249 return make_gradient(shader, args, GrLinearGradientLayout::Make(shader, args));
250}
251
Michael Ludwig4089df82018-09-12 15:22:37 -0400252std::unique_ptr<GrFragmentProcessor> MakeRadial(const SkRadialGradient& shader,
253 const GrFPArgs& args) {
254 return make_gradient(shader,args, GrRadialGradientLayout::Make(shader, args));
255}
256
Michael Ludwig24d438b2018-09-12 15:22:50 -0400257std::unique_ptr<GrFragmentProcessor> MakeSweep(const SkSweepGradient& shader,
258 const GrFPArgs& args) {
259 return make_gradient(shader,args, GrSweepGradientLayout::Make(shader, args));
260}
261
Michael Ludwig8f685082018-09-12 15:23:01 -0400262std::unique_ptr<GrFragmentProcessor> MakeConical(const SkTwoPointConicalGradient& shader,
263 const GrFPArgs& args) {
264 return make_gradient(shader, args, GrTwoPointConicalGradientLayout::Make(shader, args));
265}
266
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400267#if GR_TEST_UTILS
268RandomParams::RandomParams(SkRandom* random) {
269 // Set color count to min of 2 so that we don't trigger the const color optimization and make
270 // a non-gradient processor.
271 fColorCount = random->nextRangeU(2, kMaxRandomGradientColors);
272 fUseColors4f = random->nextBool();
273
274 // if one color, omit stops, otherwise randomly decide whether or not to
275 if (fColorCount == 1 || (fColorCount >= 2 && random->nextBool())) {
276 fStops = nullptr;
277 } else {
278 fStops = fStopStorage;
279 }
280
281 // if using SkColor4f, attach a random (possibly null) color space (with linear gamma)
282 if (fUseColors4f) {
283 fColorSpace = GrTest::TestColorSpace(random);
284 }
285
286 SkScalar stop = 0.f;
287 for (int i = 0; i < fColorCount; ++i) {
288 if (fUseColors4f) {
289 fColors4f[i].fR = random->nextUScalar1();
290 fColors4f[i].fG = random->nextUScalar1();
291 fColors4f[i].fB = random->nextUScalar1();
292 fColors4f[i].fA = random->nextUScalar1();
293 } else {
294 fColors[i] = random->nextU();
295 }
296 if (fStops) {
297 fStops[i] = stop;
298 stop = i < fColorCount - 1 ? stop + random->nextUScalar1() * (1.f - stop) : 1.f;
299 }
300 }
Mike Reedfae8fce2019-04-03 10:27:45 -0400301 fTileMode = static_cast<SkTileMode>(random->nextULessThan(kSkTileModeCount));
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400302}
303#endif
304
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400305}