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