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