| 1 |
/* |
| 2 |
* Copyright 2013 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/core/SkColor.h" |
| 11 |
#include "include/core/SkPaint.h" |
| 12 |
#include "include/core/SkPoint.h" |
| 13 |
#include "include/core/SkRect.h" |
| 14 |
#include "include/core/SkRefCnt.h" |
| 15 |
#include "include/core/SkScalar.h" |
| 16 |
#include "include/core/SkShader.h" |
| 17 |
#include "include/core/SkSize.h" |
| 18 |
#include "include/core/SkString.h" |
| 19 |
#include "include/core/SkTileMode.h" |
| 20 |
#include "include/core/SkTypes.h" |
| 21 |
#include "include/effects/SkGradient.h" |
| 22 |
#include "include/private/base/SkFloatingPoint.h" |
| 23 |
|
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
using namespace skiagm; |
| 27 |
|
| 28 |
struct GradData { |
| 29 |
size_t fCount; |
| 30 |
const SkColor4f* fColors; |
| 31 |
const float* fPos; |
| 32 |
|
| 33 |
SkGradient operator()(SkTileMode tm) const { |
| 34 |
SkSpan<const float> pos; |
| 35 |
if (fPos) { |
| 36 |
pos = {fPos, fCount}; |
| 37 |
} |
| 38 |
return {{{fColors, fCount}, pos, tm}, {}}; |
| 39 |
} |
| 40 |
}; |
| 41 |
|
| 42 |
constexpr SkColor4f gColors[] = { |
| 43 |
SkColors::kRed, SkColors::kGreen, SkColors::kBlue, SkColors::kWhite, |
| 44 |
}; |
| 45 |
|
| 46 |
constexpr GradData gGradData[] = { |
| 47 |
{ 1, gColors, nullptr }, |
| 48 |
{ 2, gColors, nullptr }, |
| 49 |
{ 3, gColors, nullptr }, |
| 50 |
{ 4, gColors, nullptr }, |
| 51 |
}; |
| 52 |
|
| 53 |
static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& grad, SkTileMode tm) { |
| 54 |
return SkShaders::LinearGradient(pts, grad(tm)); |
| 55 |
} |
| 56 |
|
| 57 |
static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& grad, SkTileMode tm) { |
| 58 |
SkPoint center; |
| 59 |
center.set(sk_float_midpoint(pts[0].fX, pts[1].fX), |
| 60 |
sk_float_midpoint(pts[0].fY, pts[1].fY)); |
| 61 |
return SkShaders::RadialGradient(center, center.fX, grad(tm)); |
| 62 |
} |
| 63 |
|
| 64 |
static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& grad, SkTileMode tm) { |
| 65 |
SkPoint center; |
| 66 |
center.set(sk_float_midpoint(pts[0].fX, pts[1].fX), |
| 67 |
sk_float_midpoint(pts[0].fY, pts[1].fY)); |
| 68 |
return SkShaders::SweepGradient(center, grad(tm)); |
| 69 |
} |
| 70 |
|
| 71 |
static sk_sp<SkShader> Make2Radial(const SkPoint pts[2], const GradData& grad, SkTileMode tm) { |
| 72 |
SkPoint center0, center1; |
| 73 |
center0.set(sk_float_midpoint(pts[0].fX, pts[1].fX), |
| 74 |
sk_float_midpoint(pts[0].fY, pts[1].fY)); |
| 75 |
center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 76 |
SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
| 77 |
return SkShaders::TwoPointConicalGradient( |
| 78 |
center1, (pts[1].fX - pts[0].fX) / 7, |
| 79 |
center0, (pts[1].fX - pts[0].fX) / 2, |
| 80 |
grad(tm)); |
| 81 |
} |
| 82 |
|
| 83 |
static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& grad, SkTileMode tm) { |
| 84 |
SkPoint center0, center1; |
| 85 |
SkScalar radius0 = (pts[1].fX - pts[0].fX) / 10; |
| 86 |
SkScalar radius1 = (pts[1].fX - pts[0].fX) / 3; |
| 87 |
center0.set(pts[0].fX + radius0, pts[0].fY + radius0); |
| 88 |
center1.set(pts[1].fX - radius1, pts[1].fY - radius1); |
| 89 |
return SkShaders::TwoPointConicalGradient(center1, radius1, center0, radius0, grad(tm)); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData& data, SkTileMode tm); |
| 94 |
|
| 95 |
constexpr GradMaker gGradMakers[] = { |
| 96 |
MakeLinear, MakeRadial, MakeSweep, Make2Radial, Make2Conical, |
| 97 |
}; |
| 98 |
|
| 99 |
/////////////////////////////////////////////////////////////////////////////// |
| 100 |
|
| 101 |
class GradientsNoTextureGM : public GM { |
| 102 |
public: |
| 103 |
GradientsNoTextureGM(bool dither) : fDither(dither) { |
| 104 |
this->setBGColor(0xFFDDDDDD); |
| 105 |
} |
| 106 |
|
| 107 |
protected: |
| 108 |
SkString getName() const override { |
| 109 |
return SkString(fDither ? "gradients_no_texture" : "gradients_no_texture_nodither"); |
| 110 |
} |
| 111 |
|
| 112 |
SkISize getISize() override { return SkISize::Make(640, 615); } |
| 113 |
|
| 114 |
void onDraw(SkCanvas* canvas) override { |
| 115 |
constexpr SkPoint kPts[2] = { { 0, 0 }, |
| 116 |
{ SkIntToScalar(50), SkIntToScalar(50) } }; |
| 117 |
constexpr SkTileMode kTM = SkTileMode::kClamp; |
| 118 |
SkRect kRect = { 0, 0, SkIntToScalar(50), SkIntToScalar(50) }; |
| 119 |
SkPaint paint; |
| 120 |
paint.setAntiAlias(true); |
| 121 |
paint.setDither(fDither); |
| 122 |
|
| 123 |
canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 124 |
constexpr uint8_t kAlphas[] = { 0xff, 0x40 }; |
| 125 |
for (size_t a = 0; a < std::size(kAlphas); ++a) { |
| 126 |
for (size_t i = 0; i < std::size(gGradData); ++i) { |
| 127 |
canvas->save(); |
| 128 |
for (size_t j = 0; j < std::size(gGradMakers); ++j) { |
| 129 |
paint.setShader(gGradMakers[j](kPts, gGradData[i], kTM)); |
| 130 |
paint.setAlpha(kAlphas[a]); |
| 131 |
canvas->drawRect(kRect, paint); |
| 132 |
canvas->translate(0, SkIntToScalar(kRect.height() + 20)); |
| 133 |
} |
| 134 |
canvas->restore(); |
| 135 |
canvas->translate(SkIntToScalar(kRect.width() + 20), 0); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
private: |
| 141 |
bool fDither; |
| 142 |
|
| 143 |
using INHERITED = GM; |
| 144 |
}; |
| 145 |
|
| 146 |
/////////////////////////////////////////////////////////////////////////////// |
| 147 |
|
| 148 |
struct ColorPos { |
| 149 |
SkColor4f* fColors; |
| 150 |
float* fPos; |
| 151 |
size_t fCount; |
| 152 |
|
| 153 |
ColorPos() : fColors(nullptr), fPos(nullptr), fCount(0) {} |
| 154 |
~ColorPos() { |
| 155 |
delete[] fColors; |
| 156 |
delete[] fPos; |
| 157 |
} |
| 158 |
|
| 159 |
void construct(const SkColor colors[], const float pos[], int count) { |
| 160 |
fColors = new SkColor4f[count]; |
| 161 |
std::transform(colors, colors + count, fColors, SkColor4f::FromColor); |
| 162 |
if (pos) { |
| 163 |
fPos = new float[count]; |
| 164 |
std::copy(pos, pos + count, fPos); |
| 165 |
fPos[0] = 0; |
| 166 |
fPos[count - 1] = 1; |
| 167 |
} |
| 168 |
fCount = count; |
| 169 |
} |
| 170 |
|
| 171 |
SkGradient operator()(SkTileMode tm) const { |
| 172 |
SkSpan<const float> pos; |
| 173 |
if (fPos) { |
| 174 |
pos = {fPos, fCount}; |
| 175 |
} |
| 176 |
return {{{fColors, fCount}, pos, tm}, {}}; |
| 177 |
} |
| 178 |
}; |
| 179 |
|
| 180 |
static void make0(ColorPos* rec) { |
| 181 |
#if 0 |
| 182 |
From http://jsfiddle.net/3fe2a/ |
| 183 |
|
| 184 |
background-image: -webkit-linear-gradient(left, #22d1cd 1%, #22d1cd 0.9510157507590116%, #df4b37 2.9510157507590113%, #df4b37 23.695886056604927%, #22d1cd 25.695886056604927%, #22d1cd 25.39321881940624%, #e6de36 27.39321881940624%, #e6de36 31.849399922570655%, #3267ff 33.849399922570655%, #3267ff 44.57735802921938%, #9d47d1 46.57735802921938%, #9d47d1 53.27185850805876%, #3267ff 55.27185850805876%, #3267ff 61.95718972227316%, #5cdd9d 63.95718972227316%, #5cdd9d 69.89166004442%, #3267ff 71.89166004442%, #3267ff 74.45795382765857%, #9d47d1 76.45795382765857%, #9d47d1 82.78364610713776%, #3267ff 84.78364610713776%, #3267ff 94.52743647737229%, #e3d082 96.52743647737229%, #e3d082 96.03934633331295%); |
| 185 |
height: 30px; |
| 186 |
#endif |
| 187 |
|
| 188 |
const SkColor colors[] = { |
| 189 |
0xFF22d1cd, 0xFF22d1cd, 0xFFdf4b37, 0xFFdf4b37, 0xFF22d1cd, 0xFF22d1cd, 0xFFe6de36, 0xFFe6de36, |
| 190 |
0xFF3267ff, 0xFF3267ff, 0xFF9d47d1, 0xFF9d47d1, 0xFF3267ff, 0xFF3267ff, 0xFF5cdd9d, 0xFF5cdd9d, |
| 191 |
0xFF3267ff, 0xFF3267ff, 0xFF9d47d1, 0xFF9d47d1, 0xFF3267ff, 0xFF3267ff, 0xFFe3d082, 0xFFe3d082 |
| 192 |
}; |
| 193 |
const double percent[] = { |
| 194 |
1, 0.9510157507590116, 2.9510157507590113, 23.695886056604927, |
| 195 |
25.695886056604927, 25.39321881940624, 27.39321881940624, 31.849399922570655, |
| 196 |
33.849399922570655, 44.57735802921938, 46.57735802921938, 53.27185850805876, |
| 197 |
55.27185850805876, 61.95718972227316, 63.95718972227316, 69.89166004442, |
| 198 |
71.89166004442, 74.45795382765857, 76.45795382765857, 82.78364610713776, |
| 199 |
84.78364610713776, 94.52743647737229, 96.52743647737229, 96.03934633331295, |
| 200 |
}; |
| 201 |
const int N = std::size(percent); |
| 202 |
SkScalar pos[N]; |
| 203 |
for (int i = 0; i < N; ++i) { |
| 204 |
pos[i] = SkDoubleToScalar(percent[i] / 100); |
| 205 |
} |
| 206 |
rec->construct(colors, pos, N); |
| 207 |
} |
| 208 |
|
| 209 |
static void make1(ColorPos* rec) { |
| 210 |
const SkColor colors[] = { |
| 211 |
SK_ColorBLACK, SK_ColorWHITE, SK_ColorBLACK, SK_ColorWHITE, |
| 212 |
SK_ColorBLACK, SK_ColorWHITE, SK_ColorBLACK, SK_ColorWHITE, |
| 213 |
SK_ColorBLACK, |
| 214 |
}; |
| 215 |
rec->construct(colors, nullptr, std::size(colors)); |
| 216 |
} |
| 217 |
|
| 218 |
static void make2(ColorPos* rec) { |
| 219 |
const SkColor colors[] = { |
| 220 |
SK_ColorBLACK, SK_ColorWHITE, SK_ColorBLACK, SK_ColorWHITE, |
| 221 |
SK_ColorBLACK, SK_ColorWHITE, SK_ColorBLACK, SK_ColorWHITE, |
| 222 |
SK_ColorBLACK, |
| 223 |
}; |
| 224 |
const int N = std::size(colors); |
| 225 |
SkScalar pos[N]; |
| 226 |
for (int i = 0; i < N; ++i) { |
| 227 |
pos[i] = SK_Scalar1 * i / (N - 1); |
| 228 |
} |
| 229 |
rec->construct(colors, pos, N); |
| 230 |
} |
| 231 |
|
| 232 |
static void make3(ColorPos* rec) { |
| 233 |
const SkColor colors[] = { |
| 234 |
SK_ColorRED, SK_ColorBLUE, SK_ColorBLUE, SK_ColorGREEN, SK_ColorGREEN, SK_ColorBLACK, |
| 235 |
}; |
| 236 |
const SkScalar pos[] = { |
| 237 |
0, 0, 0.5f, 0.5, 1, 1, |
| 238 |
}; |
| 239 |
rec->construct(colors, pos, std::size(colors)); |
| 240 |
} |
| 241 |
|
| 242 |
class GradientsManyColorsGM : public GM { |
| 243 |
enum { |
| 244 |
W = 800, |
| 245 |
}; |
| 246 |
sk_sp<SkShader> fShader; |
| 247 |
|
| 248 |
typedef void (*Proc)(ColorPos*); |
| 249 |
public: |
| 250 |
GradientsManyColorsGM(bool dither) : fDither(dither) {} |
| 251 |
|
| 252 |
protected: |
| 253 |
SkString getName() const override { |
| 254 |
return SkString(fDither ? "gradients_many" : "gradients_many_nodither"); |
| 255 |
} |
| 256 |
|
| 257 |
SkISize getISize() override { return SkISize::Make(880, 400); } |
| 258 |
|
| 259 |
void onDraw(SkCanvas* canvas) override { |
| 260 |
const Proc procs[] = { |
| 261 |
make0, make1, make2, make3, |
| 262 |
}; |
| 263 |
const SkPoint pts[] = { |
| 264 |
{ 0, 0 }, |
| 265 |
{ SkIntToScalar(W), 0 }, |
| 266 |
}; |
| 267 |
const SkRect r = SkRect::MakeWH(SkIntToScalar(W), 30); |
| 268 |
|
| 269 |
SkPaint paint; |
| 270 |
paint.setDither(fDither); |
| 271 |
|
| 272 |
canvas->translate(40, 20); |
| 273 |
|
| 274 |
for (int i = 0; i <= 8; ++i) { |
| 275 |
SkScalar x = r.width() * i / 8; |
| 276 |
canvas->drawLine(x, 0, x, 10000, paint); |
| 277 |
} |
| 278 |
|
| 279 |
// expand the drawing rect so we exercise clampping in the gradients |
| 280 |
const SkRect drawR = r.makeOutset(20, 0); |
| 281 |
for (size_t i = 0; i < std::size(procs); ++i) { |
| 282 |
ColorPos rec; |
| 283 |
procs[i](&rec); |
| 284 |
paint.setShader(SkShaders::LinearGradient(pts, rec(SkTileMode::kClamp))); |
| 285 |
canvas->drawRect(drawR, paint); |
| 286 |
|
| 287 |
canvas->save(); |
| 288 |
canvas->translate(r.centerX(), r.height() + 4); |
| 289 |
canvas->scale(-1, 1); |
| 290 |
canvas->translate(-r.centerX(), 0); |
| 291 |
canvas->drawRect(drawR, paint); |
| 292 |
canvas->restore(); |
| 293 |
|
| 294 |
canvas->translate(0, r.height() + 2*r.height() + 8); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
private: |
| 299 |
bool fDither; |
| 300 |
|
| 301 |
using INHERITED = GM; |
| 302 |
}; |
| 303 |
|
| 304 |
/////////////////////////////////////////////////////////////////////////////// |
| 305 |
|
| 306 |
DEF_GM(return new GradientsNoTextureGM(true);) |
| 307 |
DEF_GM(return new GradientsNoTextureGM(false);) |
| 308 |
DEF_GM(return new GradientsManyColorsGM(true);) |
| 309 |
DEF_GM(return new GradientsManyColorsGM(false);) |