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" |
| 14 | #include "GrSingleIntervalGradientColorizer.h" |
| 15 | |
| 16 | #include "SkGradientShaderPriv.h" |
| 17 | #include "GrColor.h" |
| 18 | |
| 19 | // Analyze the shader's color stops and positions and chooses an appropriate colorizer to represent |
| 20 | // the gradient. |
| 21 | static std::unique_ptr<GrFragmentProcessor> make_colorizer(const SkGradientShaderBase& shader, |
| 22 | const GrFPArgs& args, const GrColor4f* colors) { |
| 23 | // If there are hard stops at the beginning or end, the first and/or last color should be |
| 24 | // ignored by the colorizer since it should only be used in a clamped border color. By detecting |
| 25 | // and removing these stops at the beginning, it makes optimizing the remaining color stops |
| 26 | // simpler. |
| 27 | |
| 28 | // SkGradientShaderBase guarantees that fOrigPos[0] == 0 by adding a dummy |
| 29 | bool bottomHardStop = shader.fOrigPos && SkScalarNearlyEqual(shader.fOrigPos[0], |
| 30 | shader.fOrigPos[1]); |
| 31 | // The same is true for fOrigPos[end] == 1 |
| 32 | bool topHardStop = shader.fOrigPos && |
| 33 | SkScalarNearlyEqual(shader.fOrigPos[shader.fColorCount - 2], |
| 34 | shader.fOrigPos[shader.fColorCount - 1]); |
| 35 | |
| 36 | int offset = 0; |
| 37 | int count = shader.fColorCount; |
| 38 | if (bottomHardStop) { |
| 39 | offset += 1; |
| 40 | count--; |
| 41 | } |
| 42 | if (topHardStop) { |
| 43 | count--; |
| 44 | } |
| 45 | |
| 46 | // Currently only supports 2-color single intervals. However, when the gradient has hard stops |
| 47 | // and is clamped, certain 3 or 4 color gradients are equivalent to a two color interval |
| 48 | if (count == 2) { |
| 49 | return GrSingleIntervalGradientColorizer::Make(colors[offset], colors[offset + 1]); |
| 50 | } |
| 51 | |
| 52 | return nullptr; |
| 53 | } |
| 54 | |
| 55 | // Combines the colorizer and layout with an appropriately configured master effect based on the |
| 56 | // gradient's tile mode |
| 57 | static std::unique_ptr<GrFragmentProcessor> make_gradient(const SkGradientShaderBase& shader, |
| 58 | const GrFPArgs& args, std::unique_ptr<GrFragmentProcessor> layout) { |
| 59 | // No shader is possible if a layout couldn't be created, e.g. a layout-specific Make() returned |
| 60 | // null. |
| 61 | if (layout == nullptr) { |
| 62 | return nullptr; |
| 63 | } |
| 64 | |
| 65 | // Convert all colors into destination space and into GrColor4fs, and handle |
| 66 | // premul issues depending on the interpolation mode |
| 67 | bool inputPremul = shader.getGradFlags() & SkGradientShader::kInterpolateColorsInPremul_Flag; |
| 68 | SkAutoSTMalloc<4, GrColor4f> colors(shader.fColorCount); |
| 69 | SkColor4fXformer xformedColors(shader.fOrigColors4f, shader.fColorCount, |
| 70 | shader.fColorSpace.get(), args.fDstColorSpaceInfo->colorSpace()); |
| 71 | for (int i = 0; i < shader.fColorCount; i++) { |
| 72 | colors[i] = GrColor4f::FromSkColor4f(xformedColors.fColors[i]); |
| 73 | if (inputPremul) { |
| 74 | colors[i] = colors[i].premul(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // All gradients are colorized the same way, regardless of layout |
| 79 | std::unique_ptr<GrFragmentProcessor> colorizer = make_colorizer(shader, args, colors.get()); |
| 80 | if (colorizer == nullptr) { |
| 81 | return nullptr; |
| 82 | } |
| 83 | |
| 84 | // All tile modes are supported (unless something was added to SkShader) |
| 85 | std::unique_ptr<GrFragmentProcessor> master; |
| 86 | switch(shader.getTileMode()) { |
| 87 | case SkShader::kRepeat_TileMode: |
| 88 | master = GrTiledGradientEffect::Make(std::move(colorizer), std::move(layout), |
| 89 | /* mirror */ false); |
| 90 | break; |
| 91 | case SkShader::kMirror_TileMode: |
| 92 | master = GrTiledGradientEffect::Make(std::move(colorizer), std::move(layout), |
| 93 | /* mirror */ true); |
| 94 | break; |
| 95 | case SkShader::kClamp_TileMode: |
| 96 | // For the clamped mode, the border colors are the first and last colors, corresponding |
| 97 | // to t=0 and t=1, because SkGradientShaderBase enforces that by adding color stops as |
| 98 | // appropriate. If there is a hard stop, this grabs the expected outer colors for the |
| 99 | // border. |
| 100 | master = GrClampedGradientEffect::Make(std::move(colorizer), std::move(layout), |
| 101 | colors[0], colors[shader.fColorCount - 1]); |
| 102 | break; |
| 103 | case SkShader::kDecal_TileMode: |
| 104 | master = GrClampedGradientEffect::Make(std::move(colorizer), std::move(layout), |
| 105 | GrColor4f::TransparentBlack(), |
| 106 | GrColor4f::TransparentBlack()); |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | if (master == nullptr) { |
| 111 | // Unexpected tile mode |
| 112 | return nullptr; |
| 113 | } |
| 114 | |
| 115 | if (!inputPremul) { |
| 116 | // When interpolating unpremul colors, the output of the gradient |
| 117 | // effect fp's will also be unpremul, so wrap it to ensure its premul. |
| 118 | // - this is unnecessary when interpolating premul colors since the |
| 119 | // output color is premul by nature |
| 120 | master = GrFragmentProcessor::PremulOutput(std::move(master)); |
| 121 | } |
| 122 | |
| 123 | return GrFragmentProcessor::MulChildByInputAlpha(std::move(master)); |
| 124 | } |
| 125 | |
| 126 | namespace GrGradientShader { |
| 127 | |
| 128 | std::unique_ptr<GrFragmentProcessor> MakeLinear(const SkLinearGradient& shader, |
| 129 | const GrFPArgs& args) { |
| 130 | return make_gradient(shader, args, GrLinearGradientLayout::Make(shader, args)); |
| 131 | } |
| 132 | |
| 133 | } |