blob: dfb7418b8285d82a95f133acb496d39bce03bd5e [file] [log] [blame]
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +00001/*
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.org83f23d82014-05-22 12:27:41 +00007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#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 Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/effects/SkGradientShader.h"
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +000023
24namespace skiagm {
25
26struct GradData {
27 int fCount;
28 const SkColor* fColors;
29 const SkScalar* fPos;
30};
31
mtkleindbfd7ab2016-09-01 11:24:54 -070032constexpr SkColor gColors[] = {
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +000033 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
34};
mtkleindbfd7ab2016-09-01 11:24:54 -070035constexpr SkScalar gPos0[] = { 0, SK_Scalar1 };
36constexpr SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
37constexpr SkScalar gPos2[] = {
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +000038 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1
39};
40
mtkleindbfd7ab2016-09-01 11:24:54 -070041constexpr SkScalar gPosClamp[] = {0.0f, 0.0f, 1.0f, 1.0f};
42constexpr SkColor gColorClamp[] = {
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +000043 SK_ColorRED, SK_ColorGREEN, SK_ColorGREEN, SK_ColorBLUE
44};
45
mtkleindbfd7ab2016-09-01 11:24:54 -070046constexpr GradData gGradData[] = {
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +000047 { 2, gColors, gPos0 },
48 { 2, gColors, gPos1 },
49 { 5, gColors, gPos2 },
50 { 4, gColorClamp, gPosClamp }
51};
52
reed1a9b9642016-03-13 14:13:58 -070053static sk_sp<SkShader> Make2ConicalOutside(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -040054 SkTileMode tm, const SkMatrix& localMatrix) {
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +000055 SkPoint center0, center1;
reed80ea19c2015-05-12 10:37:34 -070056 SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10;
57 SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3;
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +000058 center0.set(pts[0].fX + radius0, pts[0].fY + radius0);
59 center1.set(pts[1].fX - radius1, pts[1].fY - radius1);
reed1a9b9642016-03-13 14:13:58 -070060 return SkGradientShader::MakeTwoPointConical(center0, radius0, center1, radius1, data.fColors,
61 data.fPos, data.fCount, tm, 0, &localMatrix);
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +000062}
63
Yuqian Li81d9f0d2017-12-13 15:47:26 -050064static sk_sp<SkShader> Make2ConicalOutsideStrip(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -040065 SkTileMode tm, const SkMatrix& localMatrix) {
Yuqian Li81d9f0d2017-12-13 15:47:26 -050066 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
reed1a9b9642016-03-13 14:13:58 -070074static sk_sp<SkShader> Make2ConicalOutsideFlip(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -040075 SkTileMode tm, const SkMatrix& localMatrix) {
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +000076 SkPoint center0, center1;
reed80ea19c2015-05-12 10:37:34 -070077 SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10;
78 SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3;
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +000079 center0.set(pts[0].fX + radius0, pts[0].fY + radius0);
80 center1.set(pts[1].fX - radius1, pts[1].fY - radius1);
reed1a9b9642016-03-13 14:13:58 -070081 return SkGradientShader::MakeTwoPointConical(center1, radius1, center0, radius0, data.fColors,
82 data.fPos, data.fCount, tm, 0, &localMatrix);
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +000083}
84
reed1a9b9642016-03-13 14:13:58 -070085static sk_sp<SkShader> Make2ConicalInside(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -040086 SkTileMode tm, const SkMatrix& localMatrix) {
reed1a9b9642016-03-13 14:13:58 -070087 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
98static sk_sp<SkShader> Make2ConicalInsideFlip(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -040099 SkTileMode tm, const SkMatrix& localMatrix) {
reed1a9b9642016-03-13 14:13:58 -0700100 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
111static sk_sp<SkShader> Make2ConicalInsideCenter(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -0400112 SkTileMode tm, const SkMatrix& localMatrix) {
Yuqian Li650dd902018-03-19 14:23:48 -0400113 SkPoint center0;
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000114 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
115 SkScalarAve(pts[0].fY, pts[1].fY));
reed1a9b9642016-03-13 14:13:58 -0700116 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.orgc8379d72014-04-22 20:56:37 +0000120}
121
Yuqian Li650dd902018-03-19 14:23:48 -0400122static sk_sp<SkShader> Make2ConicalInsideCenterReversed(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -0400123 SkTileMode tm, const SkMatrix& localMatrix) {
Yuqian Li650dd902018-03-19 14:23:48 -0400124 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
reed1a9b9642016-03-13 14:13:58 -0700133static sk_sp<SkShader> Make2ConicalZeroRad(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -0400134 SkTileMode tm, const SkMatrix& localMatrix) {
reed1a9b9642016-03-13 14:13:58 -0700135 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
146static sk_sp<SkShader> Make2ConicalZeroRadFlip(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -0400147 SkTileMode tm, const SkMatrix& localMatrix) {
reed1a9b9642016-03-13 14:13:58 -0700148 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
159static sk_sp<SkShader> Make2ConicalZeroRadCenter(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -0400160 SkTileMode tm, const SkMatrix& localMatrix) {
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000161 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));
reed1a9b9642016-03-13 14:13:58 -0700166 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.orgc8379d72014-04-22 20:56:37 +0000169}
170
reed1a9b9642016-03-13 14:13:58 -0700171static sk_sp<SkShader> Make2ConicalZeroRadOutside(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -0400172 SkTileMode tm,
reed1a9b9642016-03-13 14:13:58 -0700173 const SkMatrix& localMatrix) {
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000174 SkPoint center0, center1;
commit-bot@chromium.org80894672014-04-22 21:24:22 +0000175 SkScalar radius0 = 0.f;
reed80ea19c2015-05-12 10:37:34 -0700176 SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3;
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000177 center0.set(pts[0].fX + radius0, pts[0].fY + radius0);
178 center1.set(pts[1].fX - radius1, pts[1].fY - radius1);
reed1a9b9642016-03-13 14:13:58 -0700179 return SkGradientShader::MakeTwoPointConical(center0, radius0, center1, radius1,
180 data.fColors, data.fPos,
181 data.fCount, tm, 0, &localMatrix);
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000182}
183
reed1a9b9642016-03-13 14:13:58 -0700184static sk_sp<SkShader> Make2ConicalZeroRadFlipOutside(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -0400185 SkTileMode tm,
reed1a9b9642016-03-13 14:13:58 -0700186 const SkMatrix& localMatrix) {
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000187 SkPoint center0, center1;
commit-bot@chromium.org80894672014-04-22 21:24:22 +0000188 SkScalar radius0 = 0.f;
reed80ea19c2015-05-12 10:37:34 -0700189 SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3;
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000190 center0.set(pts[0].fX + radius0, pts[0].fY + radius0);
191 center1.set(pts[1].fX - radius1, pts[1].fY - radius1);
reed1a9b9642016-03-13 14:13:58 -0700192 return SkGradientShader::MakeTwoPointConical(center1, radius1, center0, radius0, data.fColors,
193 data.fPos, data.fCount, tm, 0, &localMatrix);
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000194}
195
reed1a9b9642016-03-13 14:13:58 -0700196static sk_sp<SkShader> Make2ConicalEdgeX(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -0400197 SkTileMode tm, const SkMatrix& localMatrix) {
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000198 SkPoint center0, center1;
reed80ea19c2015-05-12 10:37:34 -0700199 SkScalar radius0 = (pts[1].fX - pts[0].fX) / 7;
200 SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3;
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000201 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);
reed1a9b9642016-03-13 14:13:58 -0700204 return SkGradientShader::MakeTwoPointConical(center0, radius0, center1, radius1, data.fColors,
205 data.fPos, data.fCount, tm, 0, &localMatrix);
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000206}
207
reed1a9b9642016-03-13 14:13:58 -0700208static sk_sp<SkShader> Make2ConicalEdgeY(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -0400209 SkTileMode tm, const SkMatrix& localMatrix) {
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000210 SkPoint center0, center1;
reed80ea19c2015-05-12 10:37:34 -0700211 SkScalar radius0 = (pts[1].fX - pts[0].fX) / 7;
212 SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3;
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000213 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);
reed1a9b9642016-03-13 14:13:58 -0700216 return SkGradientShader::MakeTwoPointConical(center0, radius0, center1, radius1, data.fColors,
217 data.fPos, data.fCount, tm, 0, &localMatrix);
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000218}
egdaniel8405ef92014-06-09 11:57:28 -0700219
reed1a9b9642016-03-13 14:13:58 -0700220static sk_sp<SkShader> Make2ConicalZeroRadEdgeX(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -0400221 SkTileMode tm,
reed1a9b9642016-03-13 14:13:58 -0700222 const SkMatrix& localMatrix) {
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000223 SkPoint center0, center1;
commit-bot@chromium.org80894672014-04-22 21:24:22 +0000224 SkScalar radius0 = 0.f;
reed80ea19c2015-05-12 10:37:34 -0700225 SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3;
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000226 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);
reed1a9b9642016-03-13 14:13:58 -0700229 return SkGradientShader::MakeTwoPointConical(center0, radius0, center1, radius1, data.fColors,
230 data.fPos, data.fCount, tm, 0, &localMatrix);
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000231}
232
reed1a9b9642016-03-13 14:13:58 -0700233static sk_sp<SkShader> Make2ConicalZeroRadEdgeY(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -0400234 SkTileMode tm, const SkMatrix& localMatrix) {
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000235 SkPoint center0, center1;
commit-bot@chromium.org80894672014-04-22 21:24:22 +0000236 SkScalar radius0 = 0.f;
reed80ea19c2015-05-12 10:37:34 -0700237 SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3;
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000238 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);
reed1a9b9642016-03-13 14:13:58 -0700241 return SkGradientShader::MakeTwoPointConical(center0, radius0, center1, radius1, data.fColors,
242 data.fPos, data.fCount, tm, 0, &localMatrix);
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000243}
244
reed1a9b9642016-03-13 14:13:58 -0700245static sk_sp<SkShader> Make2ConicalTouchX(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -0400246 SkTileMode tm, const SkMatrix& localMatrix) {
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000247 SkPoint center0, center1;
reed80ea19c2015-05-12 10:37:34 -0700248 SkScalar radius0 = (pts[1].fX - pts[0].fX) / 7;
249 SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3;
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000250 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);
reed1a9b9642016-03-13 14:13:58 -0700253 return SkGradientShader::MakeTwoPointConical(center0, radius0, center1, radius1, data.fColors,
254 data.fPos, data.fCount, tm, 0, &localMatrix);
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000255}
256
reed1a9b9642016-03-13 14:13:58 -0700257static sk_sp<SkShader> Make2ConicalTouchY(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -0400258 SkTileMode tm, const SkMatrix& localMatrix) {
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000259 SkPoint center0, center1;
reed80ea19c2015-05-12 10:37:34 -0700260 SkScalar radius0 = (pts[1].fX - pts[0].fX) / 7;
261 SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3;
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000262 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);
reed1a9b9642016-03-13 14:13:58 -0700265 return SkGradientShader::MakeTwoPointConical(center0, radius0, center1, radius1, data.fColors,
266 data.fPos, data.fCount, tm, 0, &localMatrix);
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000267}
268
reed1a9b9642016-03-13 14:13:58 -0700269static sk_sp<SkShader> Make2ConicalInsideSmallRad(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -0400270 SkTileMode tm, const SkMatrix& localMatrix) {
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000271 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));
reed1a9b9642016-03-13 14:13:58 -0700276 return SkGradientShader::MakeTwoPointConical(center0, 0.0000000000000000001f,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000277 center0, (pts[1].fX - pts[0].fX) / 2,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +0000278 data.fColors, data.fPos, data.fCount, tm,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000279 0, &localMatrix);
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000280}
281
reed1a9b9642016-03-13 14:13:58 -0700282typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData& data,
Mike Reedfae8fce2019-04-03 10:27:45 -0400283 SkTileMode tm, const SkMatrix& localMatrix);
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000284
mtkleindbfd7ab2016-09-01 11:24:54 -0700285constexpr GradMaker gGradMakersOutside[] = {
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000286 Make2ConicalOutside, Make2ConicalOutsideFlip,
Yuqian Li81d9f0d2017-12-13 15:47:26 -0500287 Make2ConicalZeroRadOutside, Make2ConicalZeroRadFlipOutside,
288 Make2ConicalOutsideStrip
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000289};
290
mtkleindbfd7ab2016-09-01 11:24:54 -0700291constexpr GradMaker gGradMakersInside[] = {
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000292 Make2ConicalInside, Make2ConicalInsideFlip, Make2ConicalInsideCenter,
293 Make2ConicalZeroRad, Make2ConicalZeroRadFlip, Make2ConicalZeroRadCenter,
Yuqian Li650dd902018-03-19 14:23:48 -0400294 Make2ConicalInsideCenterReversed
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000295};
296
mtkleindbfd7ab2016-09-01 11:24:54 -0700297constexpr GradMaker gGradMakersEdgeCases[] = {
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000298 Make2ConicalEdgeX, Make2ConicalEdgeY,
299 Make2ConicalZeroRadEdgeX, Make2ConicalZeroRadEdgeY,
300 Make2ConicalTouchX, Make2ConicalTouchY,
301 Make2ConicalInsideSmallRad
302};
303
304
mtkleindbfd7ab2016-09-01 11:24:54 -0700305constexpr struct {
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000306 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
315enum GradCaseType { // these must match the order in gGradCases
316 kOutside_GradCaseType,
317 kInside_GradCaseType,
318 kEdge_GradCaseType,
319};
320
321///////////////////////////////////////////////////////////////////////////////
322
323class ConicalGradientsGM : public GM {
324public:
Florin Malita26249e02017-06-27 13:38:26 -0400325 ConicalGradientsGM(GradCaseType gradCaseType, bool dither,
Mike Reedfae8fce2019-04-03 10:27:45 -0400326 SkTileMode mode = SkTileMode::kClamp)
fmalita063675b2015-10-12 10:41:48 -0700327 : fGradCaseType(gradCaseType)
Florin Malita26249e02017-06-27 13:38:26 -0400328 , fDither(dither)
329 , fMode(mode) {
fmalita063675b2015-10-12 10:41:48 -0700330 fName.printf("gradients_2pt_conical_%s%s", gGradCases[gradCaseType].fName,
331 fDither ? "" : "_nodither");
Florin Malita26249e02017-06-27 13:38:26 -0400332 switch (mode) {
Mike Reedfae8fce2019-04-03 10:27:45 -0400333 case SkTileMode::kRepeat:
Florin Malita26249e02017-06-27 13:38:26 -0400334 fName.appendf("_repeat");
335 break;
Mike Reedfae8fce2019-04-03 10:27:45 -0400336 case SkTileMode::kMirror:
Florin Malita26249e02017-06-27 13:38:26 -0400337 fName.appendf("_mirror");
338 break;
339 default:
340 break;
341 }
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000342 }
343
Hal Canaryfa3305a2019-07-18 12:36:54 -0400344private:
345 void onOnceBeforeDraw() override { this->setBGColor(0xFFDDDDDD); }
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000346
Hal Canaryfa3305a2019-07-18 12:36:54 -0400347 SkString onShortName() override { return fName; }
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000348
Hal Canaryfa3305a2019-07-18 12:36:54 -0400349 SkISize onISize() override { return {840, 815}; }
350
351 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000352
353 SkPoint pts[2] = {
354 { 0, 0 },
355 { SkIntToScalar(100), SkIntToScalar(100) }
356 };
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000357 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
358 SkPaint paint;
359 paint.setAntiAlias(true);
fmalita063675b2015-10-12 10:41:48 -0700360 paint.setDither(fDither);
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000361
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.org9c9005a2014-04-28 14:55:39 +0000370 SkMatrix scale = SkMatrix::I();
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000371
372 if (i == 3) { // if the clamp case
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000373 scale.setScale(0.5f, 0.5f);
374 scale.postTranslate(25.f, 25.f);
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000375 }
376
Florin Malita26249e02017-06-27 13:38:26 -0400377 paint.setShader(gradMaker[j](pts, gGradData[i], fMode, scale));
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000378 canvas->drawRect(r, paint);
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000379 canvas->translate(0, SkIntToScalar(120));
380 }
381 canvas->restore();
382 canvas->translate(SkIntToScalar(120), 0);
383 }
384 }
385
386private:
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000387 GradCaseType fGradCaseType;
388 SkString fName;
fmalita063675b2015-10-12 10:41:48 -0700389 bool fDither;
Mike Reedfae8fce2019-04-03 10:27:45 -0400390 SkTileMode fMode;
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000391};
392///////////////////////////////////////////////////////////////////////////////
393
fmalita063675b2015-10-12 10:41:48 -0700394DEF_GM( return new ConicalGradientsGM(kInside_GradCaseType, true); )
395DEF_GM( return new ConicalGradientsGM(kOutside_GradCaseType, true); )
396DEF_GM( return new ConicalGradientsGM(kEdge_GradCaseType, true); )
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000397
Mike Reedfae8fce2019-04-03 10:27:45 -0400398DEF_GM( return new ConicalGradientsGM(kInside_GradCaseType, true, SkTileMode::kRepeat); )
399DEF_GM( return new ConicalGradientsGM(kOutside_GradCaseType, true, SkTileMode::kRepeat); )
400DEF_GM( return new ConicalGradientsGM(kEdge_GradCaseType, true, SkTileMode::kRepeat); )
Florin Malita26249e02017-06-27 13:38:26 -0400401
Mike Reedfae8fce2019-04-03 10:27:45 -0400402DEF_GM( return new ConicalGradientsGM(kInside_GradCaseType, true, SkTileMode::kMirror); )
403DEF_GM( return new ConicalGradientsGM(kOutside_GradCaseType, true, SkTileMode::kMirror); )
404DEF_GM( return new ConicalGradientsGM(kEdge_GradCaseType, true, SkTileMode::kMirror); )
Florin Malita26249e02017-06-27 13:38:26 -0400405
fmalita063675b2015-10-12 10:41:48 -0700406DEF_GM( return new ConicalGradientsGM(kInside_GradCaseType, false); )
407DEF_GM( return new ConicalGradientsGM(kOutside_GradCaseType, false); )
408DEF_GM( return new ConicalGradientsGM(kEdge_GradCaseType, false); )
commit-bot@chromium.orgc8379d72014-04-22 20:56:37 +0000409
John Stilesa6841be2020-08-06 14:11:56 -0400410} // namespace skiagm