blob: 9062fce914cbf361fef8cce3c1fdb5bfc798c6e9 [file] [log] [blame]
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +00001/*
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#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04008#include "sk_tool_utils.h"
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +00009#include "SkGradientShader.h"
10
11using namespace skiagm;
12
13struct GradData {
14 int fCount;
15 const SkColor* fColors;
16 const SkScalar* fPos;
17};
18
mtkleindbfd7ab2016-09-01 11:24:54 -070019constexpr SkColor gColors[] = {
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000020 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
21 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
22 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
23 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
24 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
25 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
26 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
27 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
28};
29
mtkleindbfd7ab2016-09-01 11:24:54 -070030//constexpr SkScalar gPos[] = { SK_Scalar1*999/2000, SK_Scalar1*1001/2000 };
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000031
mtkleindbfd7ab2016-09-01 11:24:54 -070032constexpr GradData gGradData[] = {
halcanary96fcdcc2015-08-27 07:41:13 -070033 { 40, gColors, nullptr },
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000034 // { 2, gColors, gPos },
halcanary96fcdcc2015-08-27 07:41:13 -070035 // { 2, gCol2, nullptr },
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000036};
37
reed1a9b9642016-03-13 14:13:58 -070038static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
39 return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm);
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000040}
41
reed1a9b9642016-03-13 14:13:58 -070042static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
43 const SkPoint pt{ SkScalarAve(pts[0].fX, pts[1].fX), SkScalarAve(pts[0].fY, pts[1].fY) };
44 return SkGradientShader::MakeRadial(pt, pt.fX, data.fColors, data.fPos, data.fCount, tm);
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000045}
46
reed1a9b9642016-03-13 14:13:58 -070047static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode) {
48 const SkPoint pt{ SkScalarAve(pts[0].fX, pts[1].fX), SkScalarAve(pts[0].fY, pts[1].fY) };
49 return SkGradientShader::MakeSweep(pt.fX, pt.fY, data.fColors, data.fPos, data.fCount);
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000050}
51
52
reed1a9b9642016-03-13 14:13:58 -070053typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData&, SkShader::TileMode);
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000054
mtkleindbfd7ab2016-09-01 11:24:54 -070055constexpr GradMaker gGradMakers[] = {
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000056 MakeLinear, MakeRadial, MakeSweep,
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000057};
58
59///////////////////////////////////////////////////////////////////////////////
60
61class GradientsGM : public GM {
62public:
63 GradientsGM() {
caryclark65cdba62015-06-15 06:51:08 -070064 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000065 }
66
67protected:
mtklein36352bf2015-03-25 18:17:31 -070068 SkString onShortName() override { return SkString("gradient_dirty_laundry"); }
69 SkISize onISize() override { return SkISize::Make(640, 615); }
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000070
mtklein36352bf2015-03-25 18:17:31 -070071 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000072 SkPoint pts[2] = { { 0, 0 },
73 { SkIntToScalar(100), SkIntToScalar(100) }
74 };
75 SkShader::TileMode tm = SkShader::kClamp_TileMode;
76 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
77 SkPaint paint;
78 paint.setAntiAlias(true);
79
80 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
81 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
82 canvas->save();
83 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
reed1a9b9642016-03-13 14:13:58 -070084 paint.setShader(gGradMakers[j](pts, gGradData[i], tm));
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000085 canvas->drawRect(r, paint);
86 canvas->translate(0, SkIntToScalar(120));
87 }
88 canvas->restore();
89 canvas->translate(SkIntToScalar(120), 0);
90 }
91 }
92
93private:
94 typedef GM INHERITED;
95};
96
97///////////////////////////////////////////////////////////////////////////////
98
99static GM* MyFactory(void*) { return new GradientsGM; }
100static GMRegistry reg(MyFactory);