Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 1 | /* |
| 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 Ludwig | 4089df8 | 2018-09-12 15:22:37 -0400 | [diff] [blame] | 14 | #include "GrRadialGradientLayout.h" |
Michael Ludwig | 24d438b | 2018-09-12 15:22:50 -0400 | [diff] [blame] | 15 | #include "GrSweepGradientLayout.h" |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 16 | #include "GrTwoPointConicalGradientLayout.h" |
Michael Ludwig | 4089df8 | 2018-09-12 15:22:37 -0400 | [diff] [blame] | 17 | |
Michael Ludwig | 0495f7a | 2018-09-12 15:23:33 -0400 | [diff] [blame] | 18 | #include "GrDualIntervalGradientColorizer.h" |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 19 | #include "GrSingleIntervalGradientColorizer.h" |
Michael Ludwig | a7914d3 | 2018-09-14 09:47:21 -0400 | [diff] [blame] | 20 | #include "GrTextureGradientColorizer.h" |
| 21 | #include "GrGradientBitmapCache.h" |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 22 | |
Michael Ludwig | a7914d3 | 2018-09-14 09:47:21 -0400 | [diff] [blame] | 23 | #include "SkGr.h" |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 24 | #include "GrColor.h" |
Michael Ludwig | a7914d3 | 2018-09-14 09:47:21 -0400 | [diff] [blame] | 25 | #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. |
| 29 | static const int kMaxNumCachedGradientBitmaps = 32; |
| 30 | static 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. |
| 34 | static 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 Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 61 | |
| 62 | // Analyze the shader's color stops and positions and chooses an appropriate colorizer to represent |
| 63 | // the gradient. |
Michael Ludwig | 0495f7a | 2018-09-12 15:23:33 -0400 | [diff] [blame] | 64 | static std::unique_ptr<GrFragmentProcessor> make_colorizer(const GrColor4f* colors, |
Michael Ludwig | a7914d3 | 2018-09-14 09:47:21 -0400 | [diff] [blame] | 65 | const SkScalar* positions, int count, bool premul, const GrFPArgs& args) { |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 66 | // 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 Ludwig | 0495f7a | 2018-09-12 15:23:33 -0400 | [diff] [blame] | 71 | // 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 Ludwig | a7914d3 | 2018-09-14 09:47:21 -0400 | [diff] [blame] | 74 | bool topHardStop = SkScalarNearlyEqual(positions[count - 2], positions[count - 1]); |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 75 | |
| 76 | int offset = 0; |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 77 | if (bottomHardStop) { |
| 78 | offset += 1; |
| 79 | count--; |
| 80 | } |
| 81 | if (topHardStop) { |
| 82 | count--; |
| 83 | } |
| 84 | |
Michael Ludwig | 0495f7a | 2018-09-12 15:23:33 -0400 | [diff] [blame] | 85 | // 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 Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 87 | if (count == 2) { |
| 88 | return GrSingleIntervalGradientColorizer::Make(colors[offset], colors[offset + 1]); |
Michael Ludwig | 0495f7a | 2018-09-12 15:23:33 -0400 | [diff] [blame] | 89 | } 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 Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Michael Ludwig | a7914d3 | 2018-09-14 09:47:21 -0400 | [diff] [blame] | 102 | return make_textured_colorizer(colors + offset, positions + offset, count, premul, args); |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // Combines the colorizer and layout with an appropriately configured master effect based on the |
| 106 | // gradient's tile mode |
| 107 | static 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 Ludwig | b96cba3 | 2018-09-14 13:59:24 -0400 | [diff] [blame] | 118 | bool allOpaque = true; |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 119 | 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++) { |
| 123 | colors[i] = GrColor4f::FromSkColor4f(xformedColors.fColors[i]); |
| 124 | if (inputPremul) { |
| 125 | colors[i] = colors[i].premul(); |
| 126 | } |
Michael Ludwig | b96cba3 | 2018-09-14 13:59:24 -0400 | [diff] [blame] | 127 | if (allOpaque && !SkScalarNearlyEqual(colors[i].fRGBA[3], 1.0)) { |
| 128 | allOpaque = false; |
| 129 | } |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 130 | } |
| 131 | |
Michael Ludwig | 0495f7a | 2018-09-12 15:23:33 -0400 | [diff] [blame] | 132 | // 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 Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 148 | // All gradients are colorized the same way, regardless of layout |
Michael Ludwig | a7914d3 | 2018-09-14 09:47:21 -0400 | [diff] [blame] | 149 | std::unique_ptr<GrFragmentProcessor> colorizer = make_colorizer( |
| 150 | colors.get(), positions, shader.fColorCount, inputPremul, args); |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 151 | if (colorizer == nullptr) { |
| 152 | return nullptr; |
| 153 | } |
| 154 | |
Michael Ludwig | b96cba3 | 2018-09-14 13:59:24 -0400 | [diff] [blame] | 155 | // 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 Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 162 | // 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 Ludwig | b96cba3 | 2018-09-14 13:59:24 -0400 | [diff] [blame] | 167 | /* mirror */ false, makePremul, allOpaque); |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 168 | break; |
| 169 | case SkShader::kMirror_TileMode: |
| 170 | master = GrTiledGradientEffect::Make(std::move(colorizer), std::move(layout), |
Michael Ludwig | b96cba3 | 2018-09-14 13:59:24 -0400 | [diff] [blame] | 171 | /* mirror */ true, makePremul, allOpaque); |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 172 | 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 Ludwig | b96cba3 | 2018-09-14 13:59:24 -0400 | [diff] [blame] | 179 | colors[0], colors[shader.fColorCount - 1], makePremul, allOpaque); |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 180 | break; |
| 181 | case SkShader::kDecal_TileMode: |
Michael Ludwig | b96cba3 | 2018-09-14 13:59:24 -0400 | [diff] [blame] | 182 | // Even if the gradient colors are opaque, the decal borders are transparent so |
| 183 | // disable that optimization |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 184 | master = GrClampedGradientEffect::Make(std::move(colorizer), std::move(layout), |
Michael Ludwig | b96cba3 | 2018-09-14 13:59:24 -0400 | [diff] [blame] | 185 | GrColor4f::TransparentBlack(), GrColor4f::TransparentBlack(), |
| 186 | makePremul, /* colorsAreOpaque */ false); |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 187 | break; |
| 188 | } |
| 189 | |
| 190 | if (master == nullptr) { |
| 191 | // Unexpected tile mode |
| 192 | return nullptr; |
| 193 | } |
| 194 | |
Michael Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 195 | return GrFragmentProcessor::MulChildByInputAlpha(std::move(master)); |
| 196 | } |
| 197 | |
| 198 | namespace GrGradientShader { |
| 199 | |
| 200 | std::unique_ptr<GrFragmentProcessor> MakeLinear(const SkLinearGradient& shader, |
| 201 | const GrFPArgs& args) { |
| 202 | return make_gradient(shader, args, GrLinearGradientLayout::Make(shader, args)); |
| 203 | } |
| 204 | |
Michael Ludwig | 4089df8 | 2018-09-12 15:22:37 -0400 | [diff] [blame] | 205 | std::unique_ptr<GrFragmentProcessor> MakeRadial(const SkRadialGradient& shader, |
| 206 | const GrFPArgs& args) { |
| 207 | return make_gradient(shader,args, GrRadialGradientLayout::Make(shader, args)); |
| 208 | } |
| 209 | |
Michael Ludwig | 24d438b | 2018-09-12 15:22:50 -0400 | [diff] [blame] | 210 | std::unique_ptr<GrFragmentProcessor> MakeSweep(const SkSweepGradient& shader, |
| 211 | const GrFPArgs& args) { |
| 212 | return make_gradient(shader,args, GrSweepGradientLayout::Make(shader, args)); |
| 213 | } |
| 214 | |
Michael Ludwig | 8f68508 | 2018-09-12 15:23:01 -0400 | [diff] [blame] | 215 | std::unique_ptr<GrFragmentProcessor> MakeConical(const SkTwoPointConicalGradient& shader, |
| 216 | const GrFPArgs& args) { |
| 217 | return make_gradient(shader, args, GrTwoPointConicalGradientLayout::Make(shader, args)); |
| 218 | } |
| 219 | |
Michael Ludwig | 7f8c524 | 2018-09-14 15:07:55 -0400 | [diff] [blame^] | 220 | #if GR_TEST_UTILS |
| 221 | RandomParams::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 Ludwig | 4f94ef6 | 2018-09-12 15:22:16 -0400 | [diff] [blame] | 258 | } |