blob: 0fbcd43ad8e71c991dd300aee67aa82530048cb5 [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"
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040027#include "src/gpu/GrColorInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#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;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040048 if (GrColorTypeIsWiderThan(args.fDstColorInfo->colorType(), 8)) {
Greg Daniel7bfc9132019-08-14 14:23:53 -040049 auto f16Format = args.fContext->priv().caps()->getDefaultBackendFormat(
50 GrColorType::kRGBA_F16, GrRenderable::kNo);
51 if (f16Format.isValid()) {
52 colorType = kRGBA_F16_SkColorType;
53 }
Michael Ludwiga7914d32018-09-14 09:47:21 -040054 }
55 SkAlphaType alphaType = premul ? kPremul_SkAlphaType : kUnpremul_SkAlphaType;
56
57 SkBitmap bitmap;
58 gCache.getGradient(colors, positions, count, colorType, alphaType, &bitmap);
59 SkASSERT(1 == bitmap.height() && SkIsPow2(bitmap.width()));
60 SkASSERT(bitmap.isImmutable());
61
Greg Danielc52db712020-01-28 17:03:46 -050062 auto view = GrMakeCachedBitmapProxyView(args.fContext, bitmap);
63 if (!view.proxy()) {
Michael Ludwiga7914d32018-09-14 09:47:21 -040064 SkDebugf("Gradient won't draw. Could not create texture.");
65 return nullptr;
66 }
67
Greg Danielc52db712020-01-28 17:03:46 -050068 return GrTextureGradientColorizer::Make(view.detachProxy());
Michael Ludwiga7914d32018-09-14 09:47:21 -040069}
Michael Ludwig4f94ef62018-09-12 15:22:16 -040070
71// Analyze the shader's color stops and positions and chooses an appropriate colorizer to represent
72// the gradient.
Brian Osman021ed512018-10-16 15:19:44 -040073static std::unique_ptr<GrFragmentProcessor> make_colorizer(const SkPMColor4f* colors,
Michael Ludwiga7914d32018-09-14 09:47:21 -040074 const SkScalar* positions, int count, bool premul, const GrFPArgs& args) {
Michael Ludwig4f94ef62018-09-12 15:22:16 -040075 // If there are hard stops at the beginning or end, the first and/or last color should be
76 // ignored by the colorizer since it should only be used in a clamped border color. By detecting
77 // and removing these stops at the beginning, it makes optimizing the remaining color stops
78 // simpler.
79
Michael Ludwig0495f7a2018-09-12 15:23:33 -040080 // SkGradientShaderBase guarantees that pos[0] == 0 by adding a dummy
81 bool bottomHardStop = SkScalarNearlyEqual(positions[0], positions[1]);
82 // The same is true for pos[end] == 1
Michael Ludwiga7914d32018-09-14 09:47:21 -040083 bool topHardStop = SkScalarNearlyEqual(positions[count - 2], positions[count - 1]);
Michael Ludwig4f94ef62018-09-12 15:22:16 -040084
85 int offset = 0;
Michael Ludwig4f94ef62018-09-12 15:22:16 -040086 if (bottomHardStop) {
87 offset += 1;
88 count--;
89 }
90 if (topHardStop) {
91 count--;
92 }
93
Michael Ludwig0495f7a2018-09-12 15:23:33 -040094 // Two remaining colors means a single interval from 0 to 1
95 // (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 -040096 if (count == 2) {
97 return GrSingleIntervalGradientColorizer::Make(colors[offset], colors[offset + 1]);
98 }
99
Michael Ludwig72535fb2018-09-28 11:53:32 -0400100 // Do an early test for the texture fallback to skip all of the other tests for specific
101 // analytic support of the gradient (and compatibility with the hardware), when it's definitely
102 // impossible to use an analytic solution.
103 bool tryAnalyticColorizer = count <= GrUnrolledBinaryGradientColorizer::kMaxColorCount;
104
105 // The remaining analytic colorizers use scale*t+bias, and the scale/bias values can become
106 // quite large when thresholds are close (but still outside the hardstop limit). If float isn't
107 // 32-bit, output can be incorrect if the thresholds are too close together. However, the
108 // analytic shaders are higher quality, so they can be used with lower precision hardware when
109 // the thresholds are not ill-conditioned.
Robert Phillips9da87e02019-02-04 13:26:26 -0500110 const GrShaderCaps* caps = args.fContext->priv().caps()->shaderCaps();
Michael Ludwig72535fb2018-09-28 11:53:32 -0400111 if (!caps->floatIs32Bits() && tryAnalyticColorizer) {
112 // Could run into problems, check if thresholds are close together (with a limit of .01, so
113 // that scales will be less than 100, which leaves 4 decimals of precision on 16-bit).
114 for (int i = offset; i < count - 1; i++) {
115 SkScalar dt = SkScalarAbs(positions[i] - positions[i + 1]);
116 if (dt <= kLowPrecisionIntervalLimit && dt > SK_ScalarNearlyZero) {
117 tryAnalyticColorizer = false;
118 break;
119 }
120 }
121 }
122
123 if (tryAnalyticColorizer) {
124 if (count == 3) {
125 // Must be a dual interval gradient, where the middle point is at offset+1 and the two
126 // intervals share the middle color stop.
127 return GrDualIntervalGradientColorizer::Make(colors[offset], colors[offset + 1],
128 colors[offset + 1], colors[offset + 2],
129 positions[offset + 1]);
130 } else if (count == 4 && SkScalarNearlyEqual(positions[offset + 1],
131 positions[offset + 2])) {
132 // Two separate intervals that join at the same threshold position
133 return GrDualIntervalGradientColorizer::Make(colors[offset], colors[offset + 1],
134 colors[offset + 2], colors[offset + 3],
135 positions[offset + 1]);
136 }
137
138 // The single and dual intervals are a specialized case of the unrolled binary search
139 // colorizer which can analytically render gradients of up to 8 intervals (up to 9 or 16
140 // colors depending on how many hard stops are inserted).
141 std::unique_ptr<GrFragmentProcessor> unrolled = GrUnrolledBinaryGradientColorizer::Make(
142 colors + offset, positions + offset, count);
143 if (unrolled) {
144 return unrolled;
145 }
146 }
147
148 // Otherwise fall back to a rasterized gradient sampled by a texture, which can handle
149 // arbitrary gradients (the only downside being sampling resolution).
Michael Ludwiga7914d32018-09-14 09:47:21 -0400150 return make_textured_colorizer(colors + offset, positions + offset, count, premul, args);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400151}
152
153// Combines the colorizer and layout with an appropriately configured master effect based on the
154// gradient's tile mode
155static std::unique_ptr<GrFragmentProcessor> make_gradient(const SkGradientShaderBase& shader,
156 const GrFPArgs& args, std::unique_ptr<GrFragmentProcessor> layout) {
157 // No shader is possible if a layout couldn't be created, e.g. a layout-specific Make() returned
158 // null.
159 if (layout == nullptr) {
160 return nullptr;
161 }
162
Brian Osman021ed512018-10-16 15:19:44 -0400163 // Convert all colors into destination space and into SkPMColor4fs, and handle
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400164 // premul issues depending on the interpolation mode
165 bool inputPremul = shader.getGradFlags() & SkGradientShader::kInterpolateColorsInPremul_Flag;
Michael Ludwigb96cba32018-09-14 13:59:24 -0400166 bool allOpaque = true;
Brian Osman021ed512018-10-16 15:19:44 -0400167 SkAutoSTMalloc<4, SkPMColor4f> colors(shader.fColorCount);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400168 SkColor4fXformer xformedColors(shader.fOrigColors4f, shader.fColorCount,
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400169 shader.fColorSpace.get(), args.fDstColorInfo->colorSpace());
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400170 for (int i = 0; i < shader.fColorCount; i++) {
Brian Osman021ed512018-10-16 15:19:44 -0400171 const SkColor4f& upmColor = xformedColors.fColors[i];
172 colors[i] = inputPremul ? upmColor.premul()
173 : SkPMColor4f{ upmColor.fR, upmColor.fG, upmColor.fB, upmColor.fA };
174 if (allOpaque && !SkScalarNearlyEqual(colors[i].fA, 1.0)) {
Michael Ludwigb96cba32018-09-14 13:59:24 -0400175 allOpaque = false;
176 }
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400177 }
178
Michael Ludwig0495f7a2018-09-12 15:23:33 -0400179 // SkGradientShader stores positions implicitly when they are evenly spaced, but the getPos()
180 // implementation performs a branch for every position index. Since the shader conversion
181 // requires lots of position tests, calculate all of the positions up front if needed.
182 SkTArray<SkScalar, true> implicitPos;
183 SkScalar* positions;
184 if (shader.fOrigPos) {
185 positions = shader.fOrigPos;
186 } else {
187 implicitPos.reserve(shader.fColorCount);
Mike Kleind3ed3012018-11-06 19:23:08 -0500188 SkScalar posScale = SK_Scalar1 / (shader.fColorCount - 1);
Michael Ludwig0495f7a2018-09-12 15:23:33 -0400189 for (int i = 0 ; i < shader.fColorCount; i++) {
190 implicitPos.push_back(SkIntToScalar(i) * posScale);
191 }
192 positions = implicitPos.begin();
193 }
194
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400195 // All gradients are colorized the same way, regardless of layout
Michael Ludwiga7914d32018-09-14 09:47:21 -0400196 std::unique_ptr<GrFragmentProcessor> colorizer = make_colorizer(
197 colors.get(), positions, shader.fColorCount, inputPremul, args);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400198 if (colorizer == nullptr) {
199 return nullptr;
200 }
201
Michael Ludwigb96cba32018-09-14 13:59:24 -0400202 // The master effect has to export premul colors, but under certain conditions it doesn't need
203 // to do anything to achieve that: i.e. its interpolating already premul colors (inputPremul)
204 // or all the colors have a = 1, in which case premul is a no op. Note that this allOpaque
205 // check is more permissive than SkGradientShaderBase's isOpaque(), since we can optimize away
206 // the make-premul op for two point conical gradients (which report false for isOpaque).
207 bool makePremul = !inputPremul && !allOpaque;
208
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400209 // All tile modes are supported (unless something was added to SkShader)
210 std::unique_ptr<GrFragmentProcessor> master;
211 switch(shader.getTileMode()) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400212 case SkTileMode::kRepeat:
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400213 master = GrTiledGradientEffect::Make(std::move(colorizer), std::move(layout),
Michael Ludwigb96cba32018-09-14 13:59:24 -0400214 /* mirror */ false, makePremul, allOpaque);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400215 break;
Mike Reedfae8fce2019-04-03 10:27:45 -0400216 case SkTileMode::kMirror:
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400217 master = GrTiledGradientEffect::Make(std::move(colorizer), std::move(layout),
Michael Ludwigb96cba32018-09-14 13:59:24 -0400218 /* mirror */ true, makePremul, allOpaque);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400219 break;
Mike Reedfae8fce2019-04-03 10:27:45 -0400220 case SkTileMode::kClamp:
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400221 // For the clamped mode, the border colors are the first and last colors, corresponding
222 // to t=0 and t=1, because SkGradientShaderBase enforces that by adding color stops as
223 // appropriate. If there is a hard stop, this grabs the expected outer colors for the
224 // border.
225 master = GrClampedGradientEffect::Make(std::move(colorizer), std::move(layout),
Michael Ludwigb96cba32018-09-14 13:59:24 -0400226 colors[0], colors[shader.fColorCount - 1], makePremul, allOpaque);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400227 break;
Mike Reedfae8fce2019-04-03 10:27:45 -0400228 case SkTileMode::kDecal:
Michael Ludwigb96cba32018-09-14 13:59:24 -0400229 // Even if the gradient colors are opaque, the decal borders are transparent so
230 // disable that optimization
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400231 master = GrClampedGradientEffect::Make(std::move(colorizer), std::move(layout),
Brian Osman021ed512018-10-16 15:19:44 -0400232 SK_PMColor4fTRANSPARENT, SK_PMColor4fTRANSPARENT,
Michael Ludwigb96cba32018-09-14 13:59:24 -0400233 makePremul, /* colorsAreOpaque */ false);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400234 break;
235 }
236
237 if (master == nullptr) {
238 // Unexpected tile mode
239 return nullptr;
240 }
Brian Salomonc0d79e52019-04-10 15:02:11 -0400241 if (args.fInputColorIsOpaque) {
242 return GrFragmentProcessor::OverrideInput(std::move(master), SK_PMColor4fWHITE, false);
243 }
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400244 return GrFragmentProcessor::MulChildByInputAlpha(std::move(master));
245}
246
247namespace GrGradientShader {
248
249std::unique_ptr<GrFragmentProcessor> MakeLinear(const SkLinearGradient& shader,
250 const GrFPArgs& args) {
251 return make_gradient(shader, args, GrLinearGradientLayout::Make(shader, args));
252}
253
Michael Ludwig4089df82018-09-12 15:22:37 -0400254std::unique_ptr<GrFragmentProcessor> MakeRadial(const SkRadialGradient& shader,
255 const GrFPArgs& args) {
256 return make_gradient(shader,args, GrRadialGradientLayout::Make(shader, args));
257}
258
Michael Ludwig24d438b2018-09-12 15:22:50 -0400259std::unique_ptr<GrFragmentProcessor> MakeSweep(const SkSweepGradient& shader,
260 const GrFPArgs& args) {
261 return make_gradient(shader,args, GrSweepGradientLayout::Make(shader, args));
262}
263
Michael Ludwig8f685082018-09-12 15:23:01 -0400264std::unique_ptr<GrFragmentProcessor> MakeConical(const SkTwoPointConicalGradient& shader,
265 const GrFPArgs& args) {
266 return make_gradient(shader, args, GrTwoPointConicalGradientLayout::Make(shader, args));
267}
268
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400269#if GR_TEST_UTILS
270RandomParams::RandomParams(SkRandom* random) {
271 // Set color count to min of 2 so that we don't trigger the const color optimization and make
272 // a non-gradient processor.
273 fColorCount = random->nextRangeU(2, kMaxRandomGradientColors);
274 fUseColors4f = random->nextBool();
275
276 // if one color, omit stops, otherwise randomly decide whether or not to
277 if (fColorCount == 1 || (fColorCount >= 2 && random->nextBool())) {
278 fStops = nullptr;
279 } else {
280 fStops = fStopStorage;
281 }
282
283 // if using SkColor4f, attach a random (possibly null) color space (with linear gamma)
284 if (fUseColors4f) {
285 fColorSpace = GrTest::TestColorSpace(random);
286 }
287
288 SkScalar stop = 0.f;
289 for (int i = 0; i < fColorCount; ++i) {
290 if (fUseColors4f) {
291 fColors4f[i].fR = random->nextUScalar1();
292 fColors4f[i].fG = random->nextUScalar1();
293 fColors4f[i].fB = random->nextUScalar1();
294 fColors4f[i].fA = random->nextUScalar1();
295 } else {
296 fColors[i] = random->nextU();
297 }
298 if (fStops) {
299 fStops[i] = stop;
300 stop = i < fColorCount - 1 ? stop + random->nextUScalar1() * (1.f - stop) : 1.f;
301 }
302 }
Mike Reedfae8fce2019-04-03 10:27:45 -0400303 fTileMode = static_cast<SkTileMode>(random->nextULessThan(kSkTileModeCount));
Michael Ludwig7f8c5242018-09-14 15:07:55 -0400304}
305#endif
306
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400307}