blob: 7e140259ba074a9e56d4b7b008206d67b61b5e95 [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;
118 SkAutoSTMalloc<4, GrColor4f> colors(shader.fColorCount);
119 SkColor4fXformer xformedColors(shader.fOrigColors4f, shader.fColorCount,
120 shader.fColorSpace.get(), args.fDstColorSpaceInfo->colorSpace());
121 for (int i = 0; i < shader.fColorCount; i++) {
122 colors[i] = GrColor4f::FromSkColor4f(xformedColors.fColors[i]);
123 if (inputPremul) {
124 colors[i] = colors[i].premul();
125 }
126 }
127
Michael Ludwig0495f7a2018-09-12 15:23:33 -0400128 // SkGradientShader stores positions implicitly when they are evenly spaced, but the getPos()
129 // implementation performs a branch for every position index. Since the shader conversion
130 // requires lots of position tests, calculate all of the positions up front if needed.
131 SkTArray<SkScalar, true> implicitPos;
132 SkScalar* positions;
133 if (shader.fOrigPos) {
134 positions = shader.fOrigPos;
135 } else {
136 implicitPos.reserve(shader.fColorCount);
137 SkScalar posScale = SkScalarFastInvert(shader.fColorCount - 1);
138 for (int i = 0 ; i < shader.fColorCount; i++) {
139 implicitPos.push_back(SkIntToScalar(i) * posScale);
140 }
141 positions = implicitPos.begin();
142 }
143
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400144 // All gradients are colorized the same way, regardless of layout
Michael Ludwiga7914d32018-09-14 09:47:21 -0400145 std::unique_ptr<GrFragmentProcessor> colorizer = make_colorizer(
146 colors.get(), positions, shader.fColorCount, inputPremul, args);
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400147 if (colorizer == nullptr) {
148 return nullptr;
149 }
150
151 // All tile modes are supported (unless something was added to SkShader)
152 std::unique_ptr<GrFragmentProcessor> master;
153 switch(shader.getTileMode()) {
154 case SkShader::kRepeat_TileMode:
155 master = GrTiledGradientEffect::Make(std::move(colorizer), std::move(layout),
156 /* mirror */ false);
157 break;
158 case SkShader::kMirror_TileMode:
159 master = GrTiledGradientEffect::Make(std::move(colorizer), std::move(layout),
160 /* mirror */ true);
161 break;
162 case SkShader::kClamp_TileMode:
163 // For the clamped mode, the border colors are the first and last colors, corresponding
164 // to t=0 and t=1, because SkGradientShaderBase enforces that by adding color stops as
165 // appropriate. If there is a hard stop, this grabs the expected outer colors for the
166 // border.
167 master = GrClampedGradientEffect::Make(std::move(colorizer), std::move(layout),
168 colors[0], colors[shader.fColorCount - 1]);
169 break;
170 case SkShader::kDecal_TileMode:
171 master = GrClampedGradientEffect::Make(std::move(colorizer), std::move(layout),
172 GrColor4f::TransparentBlack(),
173 GrColor4f::TransparentBlack());
174 break;
175 }
176
177 if (master == nullptr) {
178 // Unexpected tile mode
179 return nullptr;
180 }
181
182 if (!inputPremul) {
183 // When interpolating unpremul colors, the output of the gradient
184 // effect fp's will also be unpremul, so wrap it to ensure its premul.
185 // - this is unnecessary when interpolating premul colors since the
186 // output color is premul by nature
187 master = GrFragmentProcessor::PremulOutput(std::move(master));
188 }
189
190 return GrFragmentProcessor::MulChildByInputAlpha(std::move(master));
191}
192
193namespace GrGradientShader {
194
195std::unique_ptr<GrFragmentProcessor> MakeLinear(const SkLinearGradient& shader,
196 const GrFPArgs& args) {
197 return make_gradient(shader, args, GrLinearGradientLayout::Make(shader, args));
198}
199
Michael Ludwig4089df82018-09-12 15:22:37 -0400200std::unique_ptr<GrFragmentProcessor> MakeRadial(const SkRadialGradient& shader,
201 const GrFPArgs& args) {
202 return make_gradient(shader,args, GrRadialGradientLayout::Make(shader, args));
203}
204
Michael Ludwig24d438b2018-09-12 15:22:50 -0400205std::unique_ptr<GrFragmentProcessor> MakeSweep(const SkSweepGradient& shader,
206 const GrFPArgs& args) {
207 return make_gradient(shader,args, GrSweepGradientLayout::Make(shader, args));
208}
209
Michael Ludwig8f685082018-09-12 15:23:01 -0400210std::unique_ptr<GrFragmentProcessor> MakeConical(const SkTwoPointConicalGradient& shader,
211 const GrFPArgs& args) {
212 return make_gradient(shader, args, GrTwoPointConicalGradientLayout::Make(shader, args));
213}
214
Michael Ludwig4f94ef62018-09-12 15:22:16 -0400215}