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