commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 7 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 8 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 10 | #include "SkGradientShader.h" |
| 11 | |
| 12 | namespace skiagm { |
| 13 | |
| 14 | struct GradData { |
| 15 | int fCount; |
| 16 | const SkColor* fColors; |
| 17 | const SkScalar* fPos; |
| 18 | }; |
| 19 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 20 | constexpr SkColor gColors[] = { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 21 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK |
| 22 | }; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 23 | constexpr SkScalar gPos0[] = { 0, SK_Scalar1 }; |
| 24 | constexpr SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 }; |
| 25 | constexpr SkScalar gPos2[] = { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 26 | 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1 |
| 27 | }; |
| 28 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 29 | constexpr SkScalar gPosClamp[] = {0.0f, 0.0f, 1.0f, 1.0f}; |
| 30 | constexpr SkColor gColorClamp[] = { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 31 | SK_ColorRED, SK_ColorGREEN, SK_ColorGREEN, SK_ColorBLUE |
| 32 | }; |
| 33 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 34 | constexpr GradData gGradData[] = { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 35 | { 2, gColors, gPos0 }, |
| 36 | { 2, gColors, gPos1 }, |
| 37 | { 5, gColors, gPos2 }, |
| 38 | { 4, gColorClamp, gPosClamp } |
| 39 | }; |
| 40 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 41 | static sk_sp<SkShader> Make2ConicalOutside(const SkPoint pts[2], const GradData& data, |
| 42 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 43 | SkPoint center0, center1; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 44 | SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10; |
| 45 | SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 46 | center0.set(pts[0].fX + radius0, pts[0].fY + radius0); |
| 47 | center1.set(pts[1].fX - radius1, pts[1].fY - radius1); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 48 | return SkGradientShader::MakeTwoPointConical(center0, radius0, center1, radius1, data.fColors, |
| 49 | data.fPos, data.fCount, tm, 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 50 | } |
| 51 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 52 | static sk_sp<SkShader> Make2ConicalOutsideFlip(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 53 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 54 | SkPoint center0, center1; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 55 | SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10; |
| 56 | SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 57 | center0.set(pts[0].fX + radius0, pts[0].fY + radius0); |
| 58 | center1.set(pts[1].fX - radius1, pts[1].fY - radius1); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 59 | return SkGradientShader::MakeTwoPointConical(center1, radius1, center0, radius0, data.fColors, |
| 60 | data.fPos, data.fCount, tm, 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 61 | } |
| 62 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 63 | static sk_sp<SkShader> Make2ConicalInside(const SkPoint pts[2], const GradData& data, |
| 64 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
| 65 | SkPoint center0, center1; |
| 66 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 67 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 68 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 69 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
| 70 | return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7, |
| 71 | center0, (pts[1].fX - pts[0].fX) / 2, |
| 72 | data.fColors, data.fPos, data.fCount, tm, |
| 73 | 0, &localMatrix); |
| 74 | } |
| 75 | |
| 76 | static sk_sp<SkShader> Make2ConicalInsideFlip(const SkPoint pts[2], const GradData& data, |
| 77 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
| 78 | SkPoint center0, center1; |
| 79 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 80 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 81 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 82 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
| 83 | return SkGradientShader::MakeTwoPointConical(center0, (pts[1].fX - pts[0].fX) / 2, |
| 84 | center1, (pts[1].fX - pts[0].fX) / 7, |
| 85 | data.fColors, data.fPos, data.fCount, tm, |
| 86 | 0, &localMatrix); |
| 87 | } |
| 88 | |
| 89 | static sk_sp<SkShader> Make2ConicalInsideCenter(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 90 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 91 | SkPoint center0, center1; |
| 92 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 93 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 94 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 95 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 96 | return SkGradientShader::MakeTwoPointConical(center0, (pts[1].fX - pts[0].fX) / 7, |
| 97 | center0, (pts[1].fX - pts[0].fX) / 2, |
| 98 | data.fColors, data.fPos, data.fCount, tm, |
| 99 | 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 100 | } |
| 101 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 102 | static sk_sp<SkShader> Make2ConicalZeroRad(const SkPoint pts[2], const GradData& data, |
| 103 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
| 104 | SkPoint center0, center1; |
| 105 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 106 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 107 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 108 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
| 109 | return SkGradientShader::MakeTwoPointConical(center1, 0.f, |
| 110 | center0, (pts[1].fX - pts[0].fX) / 2, |
| 111 | data.fColors, data.fPos, data.fCount, tm, |
| 112 | 0, &localMatrix); |
| 113 | } |
| 114 | |
| 115 | static sk_sp<SkShader> Make2ConicalZeroRadFlip(const SkPoint pts[2], const GradData& data, |
| 116 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
| 117 | SkPoint center0, center1; |
| 118 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 119 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 120 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 121 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
| 122 | return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 2, |
| 123 | center0, 0.f, |
| 124 | data.fColors, data.fPos, data.fCount, tm, |
| 125 | 0, &localMatrix); |
| 126 | } |
| 127 | |
| 128 | static sk_sp<SkShader> Make2ConicalZeroRadCenter(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 129 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 130 | SkPoint center0, center1; |
| 131 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 132 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 133 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 134 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 135 | return SkGradientShader::MakeTwoPointConical(center0, 0.f, center0, (pts[1].fX - pts[0].fX) / 2, |
| 136 | data.fColors, data.fPos, data.fCount, tm, |
| 137 | 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 138 | } |
| 139 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 140 | static sk_sp<SkShader> Make2ConicalZeroRadOutside(const SkPoint pts[2], const GradData& data, |
| 141 | SkShader::TileMode tm, |
| 142 | const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 143 | SkPoint center0, center1; |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 144 | SkScalar radius0 = 0.f; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 145 | SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 146 | center0.set(pts[0].fX + radius0, pts[0].fY + radius0); |
| 147 | center1.set(pts[1].fX - radius1, pts[1].fY - radius1); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 148 | return SkGradientShader::MakeTwoPointConical(center0, radius0, center1, radius1, |
| 149 | data.fColors, data.fPos, |
| 150 | data.fCount, tm, 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 151 | } |
| 152 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 153 | static sk_sp<SkShader> Make2ConicalZeroRadFlipOutside(const SkPoint pts[2], const GradData& data, |
| 154 | SkShader::TileMode tm, |
| 155 | const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 156 | SkPoint center0, center1; |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 157 | SkScalar radius0 = 0.f; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 158 | SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 159 | center0.set(pts[0].fX + radius0, pts[0].fY + radius0); |
| 160 | center1.set(pts[1].fX - radius1, pts[1].fY - radius1); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 161 | return SkGradientShader::MakeTwoPointConical(center1, radius1, center0, radius0, data.fColors, |
| 162 | data.fPos, data.fCount, tm, 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 163 | } |
| 164 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 165 | static sk_sp<SkShader> Make2ConicalEdgeX(const SkPoint pts[2], const GradData& data, |
| 166 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 167 | SkPoint center0, center1; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 168 | SkScalar radius0 = (pts[1].fX - pts[0].fX) / 7; |
| 169 | SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 170 | center1.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 171 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 172 | center0.set(center1.fX + radius1, center1.fY); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 173 | return SkGradientShader::MakeTwoPointConical(center0, radius0, center1, radius1, data.fColors, |
| 174 | data.fPos, data.fCount, tm, 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 175 | } |
| 176 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 177 | static sk_sp<SkShader> Make2ConicalEdgeY(const SkPoint pts[2], const GradData& data, |
| 178 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 179 | SkPoint center0, center1; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 180 | SkScalar radius0 = (pts[1].fX - pts[0].fX) / 7; |
| 181 | SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 182 | center1.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 183 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 184 | center0.set(center1.fX, center1.fY + radius1); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 185 | return SkGradientShader::MakeTwoPointConical(center0, radius0, center1, radius1, data.fColors, |
| 186 | data.fPos, data.fCount, tm, 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 187 | } |
egdaniel | 8405ef9 | 2014-06-09 11:57:28 -0700 | [diff] [blame] | 188 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 189 | static sk_sp<SkShader> Make2ConicalZeroRadEdgeX(const SkPoint pts[2], const GradData& data, |
| 190 | SkShader::TileMode tm, |
| 191 | const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 192 | SkPoint center0, center1; |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 193 | SkScalar radius0 = 0.f; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 194 | SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 195 | center1.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 196 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 197 | center0.set(center1.fX + radius1, center1.fY); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 198 | return SkGradientShader::MakeTwoPointConical(center0, radius0, center1, radius1, data.fColors, |
| 199 | data.fPos, data.fCount, tm, 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 200 | } |
| 201 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 202 | static sk_sp<SkShader> Make2ConicalZeroRadEdgeY(const SkPoint pts[2], const GradData& data, |
| 203 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 204 | SkPoint center0, center1; |
commit-bot@chromium.org | 8089467 | 2014-04-22 21:24:22 +0000 | [diff] [blame] | 205 | SkScalar radius0 = 0.f; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 206 | SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 207 | center1.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 208 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 209 | center0.set(center1.fX, center1.fY + radius1); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 210 | return SkGradientShader::MakeTwoPointConical(center0, radius0, center1, radius1, data.fColors, |
| 211 | data.fPos, data.fCount, tm, 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 212 | } |
| 213 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 214 | static sk_sp<SkShader> Make2ConicalTouchX(const SkPoint pts[2], const GradData& data, |
| 215 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 216 | SkPoint center0, center1; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 217 | SkScalar radius0 = (pts[1].fX - pts[0].fX) / 7; |
| 218 | SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 219 | center1.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 220 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 221 | center0.set(center1.fX - radius1 + radius0, center1.fY); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 222 | return SkGradientShader::MakeTwoPointConical(center0, radius0, center1, radius1, data.fColors, |
| 223 | data.fPos, data.fCount, tm, 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 224 | } |
| 225 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 226 | static sk_sp<SkShader> Make2ConicalTouchY(const SkPoint pts[2], const GradData& data, |
| 227 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 228 | SkPoint center0, center1; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 229 | SkScalar radius0 = (pts[1].fX - pts[0].fX) / 7; |
| 230 | SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 231 | center1.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 232 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 233 | center0.set(center1.fX, center1.fY + radius1 - radius0); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 234 | return SkGradientShader::MakeTwoPointConical(center0, radius0, center1, radius1, data.fColors, |
| 235 | data.fPos, data.fCount, tm, 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 236 | } |
| 237 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 238 | static sk_sp<SkShader> Make2ConicalInsideSmallRad(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 239 | SkShader::TileMode tm, const SkMatrix& localMatrix) { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 240 | SkPoint center0, center1; |
| 241 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 242 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 243 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 244 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 245 | return SkGradientShader::MakeTwoPointConical(center0, 0.0000000000000000001f, |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 246 | center0, (pts[1].fX - pts[0].fX) / 2, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 247 | data.fColors, data.fPos, data.fCount, tm, |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 248 | 0, &localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 249 | } |
| 250 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 251 | typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData& data, |
| 252 | SkShader::TileMode tm, const SkMatrix& localMatrix); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 253 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 254 | constexpr GradMaker gGradMakersOutside[] = { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 255 | Make2ConicalOutside, Make2ConicalOutsideFlip, |
| 256 | Make2ConicalZeroRadOutside, Make2ConicalZeroRadFlipOutside |
| 257 | }; |
| 258 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 259 | constexpr GradMaker gGradMakersInside[] = { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 260 | Make2ConicalInside, Make2ConicalInsideFlip, Make2ConicalInsideCenter, |
| 261 | Make2ConicalZeroRad, Make2ConicalZeroRadFlip, Make2ConicalZeroRadCenter, |
| 262 | }; |
| 263 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 264 | constexpr GradMaker gGradMakersEdgeCases[] = { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 265 | Make2ConicalEdgeX, Make2ConicalEdgeY, |
| 266 | Make2ConicalZeroRadEdgeX, Make2ConicalZeroRadEdgeY, |
| 267 | Make2ConicalTouchX, Make2ConicalTouchY, |
| 268 | Make2ConicalInsideSmallRad |
| 269 | }; |
| 270 | |
| 271 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 272 | constexpr struct { |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 273 | const GradMaker* fMaker; |
| 274 | const int fCount; |
| 275 | const char* fName; |
| 276 | } gGradCases[] = { |
| 277 | { gGradMakersOutside, SK_ARRAY_COUNT(gGradMakersOutside), "outside" }, |
| 278 | { gGradMakersInside, SK_ARRAY_COUNT(gGradMakersInside), "inside" }, |
| 279 | { gGradMakersEdgeCases, SK_ARRAY_COUNT(gGradMakersEdgeCases), "edge" }, |
| 280 | }; |
| 281 | |
| 282 | enum GradCaseType { // these must match the order in gGradCases |
| 283 | kOutside_GradCaseType, |
| 284 | kInside_GradCaseType, |
| 285 | kEdge_GradCaseType, |
| 286 | }; |
| 287 | |
| 288 | /////////////////////////////////////////////////////////////////////////////// |
| 289 | |
| 290 | class ConicalGradientsGM : public GM { |
| 291 | public: |
Florin Malita | 26249e0 | 2017-06-27 13:38:26 -0400 | [diff] [blame] | 292 | ConicalGradientsGM(GradCaseType gradCaseType, bool dither, |
| 293 | SkShader::TileMode mode = SkShader::kClamp_TileMode) |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 294 | : fGradCaseType(gradCaseType) |
Florin Malita | 26249e0 | 2017-06-27 13:38:26 -0400 | [diff] [blame] | 295 | , fDither(dither) |
| 296 | , fMode(mode) { |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 297 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 298 | fName.printf("gradients_2pt_conical_%s%s", gGradCases[gradCaseType].fName, |
| 299 | fDither ? "" : "_nodither"); |
Florin Malita | 26249e0 | 2017-06-27 13:38:26 -0400 | [diff] [blame] | 300 | switch (mode) { |
| 301 | case SkShader::kRepeat_TileMode: |
| 302 | fName.appendf("_repeat"); |
| 303 | break; |
| 304 | case SkShader::kMirror_TileMode: |
| 305 | fName.appendf("_mirror"); |
| 306 | break; |
| 307 | default: |
| 308 | break; |
| 309 | } |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | protected: |
| 313 | SkString onShortName() { |
| 314 | return fName; |
| 315 | } |
| 316 | |
| 317 | virtual SkISize onISize() { return SkISize::Make(840, 815); } |
| 318 | |
| 319 | virtual void onDraw(SkCanvas* canvas) { |
| 320 | |
| 321 | SkPoint pts[2] = { |
| 322 | { 0, 0 }, |
| 323 | { SkIntToScalar(100), SkIntToScalar(100) } |
| 324 | }; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 325 | SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; |
| 326 | SkPaint paint; |
| 327 | paint.setAntiAlias(true); |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 328 | paint.setDither(fDither); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 329 | |
| 330 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 331 | |
| 332 | const GradMaker* gradMaker = gGradCases[fGradCaseType].fMaker; |
| 333 | const int count = gGradCases[fGradCaseType].fCount; |
| 334 | |
| 335 | for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { |
| 336 | canvas->save(); |
| 337 | for (int j = 0; j < count; j++) { |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 338 | SkMatrix scale = SkMatrix::I(); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 339 | |
| 340 | if (i == 3) { // if the clamp case |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 341 | scale.setScale(0.5f, 0.5f); |
| 342 | scale.postTranslate(25.f, 25.f); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Florin Malita | 26249e0 | 2017-06-27 13:38:26 -0400 | [diff] [blame] | 345 | paint.setShader(gradMaker[j](pts, gGradData[i], fMode, scale)); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 346 | canvas->drawRect(r, paint); |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 347 | canvas->translate(0, SkIntToScalar(120)); |
| 348 | } |
| 349 | canvas->restore(); |
| 350 | canvas->translate(SkIntToScalar(120), 0); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | private: |
| 355 | typedef GM INHERITED; |
| 356 | |
| 357 | GradCaseType fGradCaseType; |
| 358 | SkString fName; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 359 | bool fDither; |
Florin Malita | 26249e0 | 2017-06-27 13:38:26 -0400 | [diff] [blame] | 360 | SkShader::TileMode fMode; |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 361 | }; |
| 362 | /////////////////////////////////////////////////////////////////////////////// |
| 363 | |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 364 | DEF_GM( return new ConicalGradientsGM(kInside_GradCaseType, true); ) |
| 365 | DEF_GM( return new ConicalGradientsGM(kOutside_GradCaseType, true); ) |
| 366 | DEF_GM( return new ConicalGradientsGM(kEdge_GradCaseType, true); ) |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 367 | |
Florin Malita | 26249e0 | 2017-06-27 13:38:26 -0400 | [diff] [blame] | 368 | DEF_GM( return new ConicalGradientsGM(kInside_GradCaseType, true, SkShader::kRepeat_TileMode); ) |
| 369 | DEF_GM( return new ConicalGradientsGM(kOutside_GradCaseType, true, SkShader::kRepeat_TileMode); ) |
| 370 | DEF_GM( return new ConicalGradientsGM(kEdge_GradCaseType, true, SkShader::kRepeat_TileMode); ) |
| 371 | |
| 372 | DEF_GM( return new ConicalGradientsGM(kInside_GradCaseType, true, SkShader::kMirror_TileMode); ) |
| 373 | DEF_GM( return new ConicalGradientsGM(kOutside_GradCaseType, true, SkShader::kMirror_TileMode); ) |
| 374 | DEF_GM( return new ConicalGradientsGM(kEdge_GradCaseType, true, SkShader::kMirror_TileMode); ) |
| 375 | |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 376 | DEF_GM( return new ConicalGradientsGM(kInside_GradCaseType, false); ) |
| 377 | DEF_GM( return new ConicalGradientsGM(kOutside_GradCaseType, false); ) |
| 378 | DEF_GM( return new ConicalGradientsGM(kEdge_GradCaseType, false); ) |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 379 | |
commit-bot@chromium.org | c8379d7 | 2014-04-22 20:56:37 +0000 | [diff] [blame] | 380 | } |